xref: /trunk/main/svtools/source/contnr/templwin.cxx (revision 31d35622ee258902b338f9bfdfb2a2ed84b7bb6c)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 // MARKER(update_precomp.py): autogen include statement, do not remove
23 #include "precompiled_svtools.hxx"
24 #include "templwin.hxx"
25 #include <svtools/templdlg.hxx>
26 #include <svtools/svtdata.hxx>
27 #include <unotools/pathoptions.hxx>
28 #include <unotools/dynamicmenuoptions.hxx>
29 #include <unotools/extendedsecurityoptions.hxx>
30 #include <svtools/xtextedt.hxx>
31 #include <svl/inettype.hxx>
32 #include <svtools/imagemgr.hxx>
33 #include <svtools/miscopt.hxx>
34 #include <svtools/templatefoldercache.hxx>
35 #include <svtools/imgdef.hxx>
36 #include <svtools/txtattr.hxx>
37 #include <svtools/svtools.hrc>
38 #include "templwin.hrc"
39 #include <svtools/helpid.hrc>
40 #include <unotools/pathoptions.hxx>
41 #include <unotools/viewoptions.hxx>
42 #include <unotools/ucbhelper.hxx>
43 #include "unotools/configmgr.hxx"
44 #include <com/sun/star/awt/XWindow.hpp>
45 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
46 #include <com/sun/star/frame/XFrame.hpp>
47 #include <toolkit/helper/vclunohelper.hxx>
48 #include <com/sun/star/util/URL.hpp>
49 #include <com/sun/star/util/XURLTransformer.hpp>
50 #include <com/sun/star/util/XOfficeInstallationDirectories.hpp>
51 #include <com/sun/star/frame/XDispatchProvider.hpp>
52 #include <com/sun/star/frame/XDocumentTemplates.hpp>
53 #include <com/sun/star/frame/XComponentLoader.hpp>
54 #include <com/sun/star/beans/PropertyValue.hpp>
55 #include <com/sun/star/ucb/XContent.hpp>
56 #include <com/sun/star/ucb/XCommandEnvironment.hpp>
57 #include <com/sun/star/view/XPrintable.hpp>
58 #include <com/sun/star/awt/XWindow.hpp>
59 #include <com/sun/star/document/XDocumentProperties.hpp>
60 #include <com/sun/star/beans/XPropertySet.hpp>
61 #include <com/sun/star/beans/XMultiPropertySet.hpp>
62 #include <com/sun/star/beans/XPropertySetInfo.hpp>
63 #include <com/sun/star/io/IOException.hpp>
64 #include <com/sun/star/util/DateTime.hpp>
65 #include <com/sun/star/script/XTypeConverter.hpp>
66 #include <com/sun/star/system/SystemShellExecute.hpp>
67 #include <com/sun/star/system/SystemShellExecuteFlags.hpp>
68 #include <unotools/localedatawrapper.hxx>
69 #include <com/sun/star/container/XNameContainer.hpp>
70 #include <vcl/waitobj.hxx>
71 #include <comphelper/processfactory.hxx>
72 #include <tools/urlobj.hxx>
73 #include <tools/datetime.hxx>
74 #include <vcl/svapp.hxx>
75 #include <vcl/split.hxx>
76 #include <vcl/msgbox.hxx>
77 #include <svtools/DocumentInfoPreview.hxx>
78 #include <vcl/mnemonic.hxx>
79 
80 #include <ucbhelper/content.hxx>
81 #include <comphelper/string.hxx>
82 
83 using namespace ::com::sun::star;
84 using namespace ::com::sun::star::beans;
85 using namespace ::com::sun::star::container;
86 using namespace ::com::sun::star::frame;
87 using namespace ::com::sun::star::document;
88 using namespace ::com::sun::star::lang;
89 using namespace ::com::sun::star::ucb;
90 using namespace ::com::sun::star::uno;
91 using namespace ::com::sun::star::view;
92 using namespace svtools;
93 
94 extern ::rtl::OUString CreateExactSizeText_Impl( sal_Int64 nSize ); // fileview.cxx
95 
96 #define SPLITSET_ID         0
97 #define COLSET_ID           1
98 #define ICONWIN_ID          2
99 #define FILEWIN_ID          3
100 #define FRAMEWIN_ID         4
101 
102 #define ICON_POS_NEWDOC     0
103 #define ICON_POS_TEMPLATES  1
104 #define ICON_POS_MYDOCS     2
105 #define ICON_POS_SAMPLES    3
106 
107 #define ASCII_STR(s)                    ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(s) )
108 #define VIEWSETTING_NEWFROMTEMPLATE     ASCII_STR("NewFromTemplate")
109 #define VIEWSETTING_SELECTEDGROUP       ASCII_STR("SelectedGroup")
110 #define VIEWSETTING_SELECTEDVIEW        ASCII_STR("SelectedView")
111 #define VIEWSETTING_SPLITRATIO          ASCII_STR("SplitRatio")
112 #define VIEWSETTING_LASTFOLDER          ASCII_STR("LastFolder")
113 
114 struct FolderHistory
115 {
116     String      m_sURL;
117     sal_uLong       m_nGroup;
118 
119     FolderHistory( const String& _rURL, sal_Int32 _nGroup ) :
120         m_sURL( _rURL ), m_nGroup( _nGroup ) {}
121 };
122 
123 DECLARE_LIST( HistoryList_Impl, FolderHistory* )
124 DECLARE_LIST( NewDocList_Impl, ::rtl::OUString* )
125 
126 ODocumentInfoPreview::ODocumentInfoPreview( Window* pParent ,WinBits _nBits) : Window(pParent,WB_DIALOGCONTROL)
127 {
128     m_pEditWin = new SvtExtendedMultiLineEdit_Impl(this,_nBits);
129     m_pEditWin->Show();
130     m_pEditWin->EnableCursor( sal_False );
131     m_pInfoTable = new SvtDocInfoTable_Impl();
132     // detect application language
133     m_aLocale = SvtPathOptions().GetLocale();
134 }
135 // -----------------------------------------------------------------------------
136 ODocumentInfoPreview::~ODocumentInfoPreview()
137 {
138     delete m_pEditWin;
139     delete m_pInfoTable;
140 }
141 // -----------------------------------------------------------------------------
142 void ODocumentInfoPreview::Resize()
143 {
144     Size aOutputSize( GetOutputSize() );
145     m_pEditWin->SetPosSizePixel( Point(0,0),aOutputSize);
146 }
147 // -----------------------------------------------------------------------------
148 void ODocumentInfoPreview::Clear()
149 {
150     m_pEditWin->Clear();
151 }
152 // -----------------------------------------------------------------------------
153 
154 void lcl_insertDateTimeEntry(SvtExtendedMultiLineEdit_Impl* i_pEditWin,
155     const ::rtl::OUString & i_rName, const util::DateTime & i_rUDT)
156 {
157     DateTime aToolsDT =
158         DateTime( Date( i_rUDT.Day, i_rUDT.Month, i_rUDT.Year ),
159                 Time( i_rUDT.Hours, i_rUDT.Minutes,
160                       i_rUDT.Seconds, i_rUDT.HundredthSeconds ) );
161     if ( aToolsDT.IsValid() )
162     {
163         LocaleDataWrapper aLocaleWrapper(
164             ::comphelper::getProcessServiceFactory(),
165             Application::GetSettings().GetLocale() );
166         String aDateStr = aLocaleWrapper.getDate( aToolsDT );
167         aDateStr += String( RTL_CONSTASCII_STRINGPARAM(", ") );
168         aDateStr += aLocaleWrapper.getTime( aToolsDT );
169         i_pEditWin->InsertEntry( i_rName, aDateStr );
170     }
171 }
172 
173 void ODocumentInfoPreview::fill(
174     const Reference< XDocumentProperties >& i_xDocProps, const String& i_rURL)
175 {
176     if (!i_xDocProps.is()) throw RuntimeException();
177 
178     ::rtl::OUString aStr;
179     m_pEditWin->SetAutoScroll( sal_False );
180 
181     aStr = i_xDocProps->getTitle();
182     if (aStr.getLength()) {
183         m_pEditWin->InsertEntry( m_pInfoTable->GetString( DI_TITLE ), aStr );
184     }
185 
186     aStr = i_xDocProps->getAuthor();
187     if (aStr.getLength()) {
188         m_pEditWin->InsertEntry( m_pInfoTable->GetString( DI_FROM ), aStr );
189     }
190 
191     lcl_insertDateTimeEntry(m_pEditWin,
192             m_pInfoTable->GetString( DI_DATE ),
193             i_xDocProps->getCreationDate());
194 
195     aStr = i_xDocProps->getModifiedBy();
196     if (aStr.getLength()) {
197         m_pEditWin->InsertEntry( m_pInfoTable->GetString(DI_MODIFIEDBY), aStr );
198     }
199 
200     lcl_insertDateTimeEntry(m_pEditWin,
201             m_pInfoTable->GetString( DI_MODIFIEDDATE ),
202             i_xDocProps->getModificationDate());
203 
204     aStr = i_xDocProps->getPrintedBy();
205     if (aStr.getLength()) {
206         m_pEditWin->InsertEntry( m_pInfoTable->GetString( DI_PRINTBY ), aStr );
207     }
208 
209     lcl_insertDateTimeEntry(m_pEditWin,
210             m_pInfoTable->GetString( DI_PRINTDATE ),
211             i_xDocProps->getPrintDate());
212 
213     aStr = i_xDocProps->getSubject();
214     if (aStr.getLength()) {
215         m_pEditWin->InsertEntry( m_pInfoTable->GetString( DI_THEME ), aStr );
216     }
217 
218     aStr =
219         ::comphelper::string::convertCommaSeparated(i_xDocProps->getKeywords());
220     if (aStr.getLength()) {
221         m_pEditWin->InsertEntry( m_pInfoTable->GetString( DI_KEYWORDS ), aStr );
222     }
223 
224     aStr = i_xDocProps->getDescription();
225     if (aStr.getLength()) {
226         m_pEditWin->InsertEntry( m_pInfoTable->GetString( DI_DESCRIPTION ),
227             aStr );
228     }
229 
230     // size
231     if ( i_rURL.Len() > 0 )
232     {
233         sal_uLong nDocSize = ::utl::UCBContentHelper::GetSize( i_rURL );
234         m_pEditWin->InsertEntry(
235             m_pInfoTable->GetString( DI_SIZE ),
236             CreateExactSizeText_Impl( nDocSize ) );
237     }
238 
239     // MIMEType
240     if ( i_rURL.Len() > 0 )
241     {
242         INetContentType eTypeID =
243             INetContentTypes::GetContentTypeFromURL( i_rURL );
244         if ( eTypeID != CONTENT_TYPE_APP_OCTSTREAM ) {
245             aStr = INetContentTypes::GetPresentation( eTypeID, m_aLocale );
246         } else {
247             aStr = SvFileInformationManager::GetDescription(
248                         INetURLObject(i_rURL) );
249         }
250         if (aStr.getLength()) {
251             m_pEditWin->InsertEntry( m_pInfoTable->GetString( DI_MIMETYPE ),
252                 aStr );
253         }
254     }
255 
256     // user-defined (custom) properties
257     Reference< XPropertySet > xUserDefined(
258         i_xDocProps->getUserDefinedProperties(), UNO_QUERY_THROW );
259     Reference< XPropertySetInfo > xUDInfo = xUserDefined->getPropertySetInfo();
260     Sequence< Property > props = xUDInfo->getProperties();
261     for (sal_Int32 i = 0; i < props.getLength(); ++i) {
262         const ::rtl::OUString name = props[i].Name;
263         uno::Any aAny;
264         try {
265             aAny = xUserDefined->getPropertyValue(name);
266             uno::Reference < script::XTypeConverter > xConverter(
267                 comphelper::getProcessServiceFactory()->createInstance(
268                     ASCII_STR("com.sun.star.script.Converter")),
269                 UNO_QUERY );
270             uno::Any aNew;
271             aNew = xConverter->convertToSimpleType( aAny, TypeClass_STRING );
272             if ((aNew >>= aStr) && aStr.getLength()) {
273                 m_pEditWin->InsertEntry( name, aStr);
274             }
275         } catch (uno::Exception &) {
276             // ignore
277         }
278     }
279 
280     m_pEditWin->SetSelection( Selection( 0, 0 ) );
281     m_pEditWin->SetAutoScroll( sal_True );
282 }
283 
284 // -----------------------------------------------------------------------------
285 void ODocumentInfoPreview::InsertEntry( const String& rTitle, const String& rValue )
286 {
287     m_pEditWin->InsertEntry( rTitle, rValue);
288 }
289 // -----------------------------------------------------------------------------
290 
291 // class SvtDummyHeaderBar_Impl ------------------------------------------
292 
293 void SvtDummyHeaderBar_Impl::UpdateBackgroundColor()
294 {
295     SetBackground( Wallpaper( GetSettings().GetStyleSettings().GetWindowColor() ) );
296 }
297 
298 SvtDummyHeaderBar_Impl::SvtDummyHeaderBar_Impl( Window* pPar ) : Window( pPar )
299 {
300     SetSizePixel( HeaderBar( this, 0 ).CalcWindowSizePixel() ); // HeaderBar used only to calculate size
301 
302     UpdateBackgroundColor();
303 }
304 
305 SvtDummyHeaderBar_Impl::~SvtDummyHeaderBar_Impl()
306 {
307 }
308 
309 void SvtDummyHeaderBar_Impl::DataChanged( const DataChangedEvent& r )
310 {
311     Window::DataChanged( r );
312     if( r.GetType() == DATACHANGED_SETTINGS )
313         UpdateBackgroundColor();
314 }
315 
316 // class SvtIconWindow_Impl ----------------------------------------------
317 
318 SvtIconWindow_Impl::SvtIconWindow_Impl( Window* pParent ) :
319 
320     Window( pParent, WB_DIALOGCONTROL | WB_BORDER | WB_3DLOOK ),
321 
322     aDummyHeaderBar( this ),
323     aIconCtrl( this, WB_ICON | WB_NOCOLUMNHEADER | WB_HIGHLIGHTFRAME | /*!WB_NOSELECTION |*/
324                      WB_NODRAGSELECTION | WB_TABSTOP | WB_CLIPCHILDREN ),
325     aNewDocumentRootURL( ASCII_STR("private:newdoc") ),
326     aMyDocumentsRootURL( SvtPathOptions().GetWorkPath() ),
327     aSamplesFolderRootURL( SvtPathOptions().
328         SubstituteVariable( String( ASCII_STR("$(insturl)/share/samples/$(vlang)") ) ) ),
329     nMaxTextLength( 0 )
330 
331 {
332     aDummyHeaderBar.Show();
333 
334     aIconCtrl.SetAccessibleName( String( RTL_CONSTASCII_USTRINGPARAM("Groups") ) );
335     aIconCtrl.SetHelpId( HID_TEMPLATEDLG_ICONCTRL );
336     aIconCtrl.SetChoiceWithCursor( sal_True );
337     aIconCtrl.SetSelectionMode( SINGLE_SELECTION );
338     aIconCtrl.Show();
339 
340     // detect the root URL of templates
341     Reference< XDocumentTemplates > xTemplates( ::comphelper::getProcessServiceFactory()->
342         createInstance( ASCII_STR("com.sun.star.frame.DocumentTemplates") ), UNO_QUERY );
343 
344     if ( xTemplates.is() )
345     {
346         Reference < XContent > aRootContent = xTemplates->getContent();
347         Reference < XCommandEnvironment > aCmdEnv;
348 
349         if ( aRootContent.is() )
350             aTemplateRootURL = aRootContent->getIdentifier()->getContentIdentifier();
351     }
352 
353     // insert the categories
354     // "New Document"
355     sal_Bool bHiContrast = GetSettings().GetStyleSettings().GetHighContrastMode();
356     Image aImage( SvtResId( bHiContrast ? IMG_SVT_NEWDOC_HC : IMG_SVT_NEWDOC ) );
357     nMaxTextLength = aImage.GetSizePixel().Width();
358     String aEntryStr = String( SvtResId( STR_SVT_NEWDOC ) );
359     SvxIconChoiceCtrlEntry* pEntry =
360         aIconCtrl.InsertEntry( aEntryStr, aImage, ICON_POS_NEWDOC );
361     pEntry->SetUserData( new String( aNewDocumentRootURL ) );
362     pEntry->SetQuickHelpText( String( SvtResId( STR_SVT_NEWDOC_HELP ) ) );
363     DBG_ASSERT( !pEntry->GetBoundRect().IsEmpty(), "empty rectangle" );
364     long nTemp = pEntry->GetBoundRect().GetSize().Width();
365     if (nTemp > nMaxTextLength)
366         nMaxTextLength = nTemp;
367 
368     // "Templates"
369     if( aTemplateRootURL.Len() > 0 )
370     {
371         aEntryStr = String( SvtResId( STR_SVT_TEMPLATES ) );
372         pEntry = aIconCtrl.InsertEntry(
373             aEntryStr, Image( SvtResId( bHiContrast ? IMG_SVT_TEMPLATES_HC : IMG_SVT_TEMPLATES ) ), ICON_POS_TEMPLATES );
374         pEntry->SetUserData( new String( aTemplateRootURL ) );
375         pEntry->SetQuickHelpText( String( SvtResId( STR_SVT_TEMPLATES_HELP ) ) );
376         DBG_ASSERT( !pEntry->GetBoundRect().IsEmpty(), "empty rectangle" );
377         nTemp = pEntry->GetBoundRect().GetSize().Width();
378         if (nTemp > nMaxTextLength)
379             nMaxTextLength = nTemp;
380     }
381 
382     // "My Documents"
383     aEntryStr = String( SvtResId( STR_SVT_MYDOCS ) );
384     pEntry = aIconCtrl.InsertEntry(
385         aEntryStr, Image( SvtResId( bHiContrast ? IMG_SVT_MYDOCS_HC : IMG_SVT_MYDOCS ) ), ICON_POS_MYDOCS );
386     pEntry->SetUserData( new String( aMyDocumentsRootURL ) );
387     pEntry->SetQuickHelpText( String( SvtResId( STR_SVT_MYDOCS_HELP ) ) );
388     DBG_ASSERT( !pEntry->GetBoundRect().IsEmpty(), "empty rectangle" );
389     nTemp = pEntry->GetBoundRect().GetSize().Width();
390     if( nTemp > nMaxTextLength )
391         nMaxTextLength = nTemp;
392 
393     // "Samples"
394     aEntryStr = String( SvtResId( STR_SVT_SAMPLES ) );
395     pEntry = aIconCtrl.InsertEntry(
396         aEntryStr, Image( SvtResId( bHiContrast ? IMG_SVT_SAMPLES_HC : IMG_SVT_SAMPLES ) ), ICON_POS_SAMPLES );
397     pEntry->SetUserData( new String( aSamplesFolderRootURL ) );
398     pEntry->SetQuickHelpText( String( SvtResId( STR_SVT_SAMPLES_HELP ) ) );
399     DBG_ASSERT( !pEntry->GetBoundRect().IsEmpty(), "empty rectangle" );
400     nTemp = pEntry->GetBoundRect().GetSize().Width();
401     if (nTemp > nMaxTextLength)
402         nMaxTextLength = nTemp;
403 
404     aIconCtrl.CreateAutoMnemonics();
405 }
406 
407 SvtIconWindow_Impl::~SvtIconWindow_Impl()
408 {
409     for ( sal_uLong i = 0; i < aIconCtrl.GetEntryCount(); ++i )
410     {
411         SvxIconChoiceCtrlEntry* pEntry = aIconCtrl.GetEntry( i );
412         delete (String*)pEntry->GetUserData();
413     }
414 }
415 
416 SvxIconChoiceCtrlEntry* SvtIconWindow_Impl::GetEntry( const String& rURL ) const
417 {
418     SvxIconChoiceCtrlEntry* pEntry = NULL;
419     for ( sal_uLong i = 0; i < aIconCtrl.GetEntryCount(); ++i )
420     {
421         SvxIconChoiceCtrlEntry* pTemp = aIconCtrl.GetEntry( i );
422         String aURL( *( (String*)pTemp->GetUserData() ) );
423         if ( aURL == rURL )
424         {
425             pEntry = pTemp;
426             break;
427         }
428     }
429 
430     return pEntry;
431 }
432 
433 void SvtIconWindow_Impl::Resize()
434 {
435     Size aWinSize = GetOutputSizePixel();
436     Size aHeaderSize = aDummyHeaderBar.GetSizePixel();
437     aHeaderSize.Width() = aWinSize.Width();
438     aDummyHeaderBar.SetSizePixel( aHeaderSize );
439     long nHeaderHeight = aHeaderSize.Height();
440     aWinSize.Height() -= nHeaderHeight;
441     aIconCtrl.SetPosSizePixel( Point( 0, nHeaderHeight ), aWinSize );
442     aIconCtrl.ArrangeIcons();
443 }
444 
445 String SvtIconWindow_Impl::GetCursorPosIconURL() const
446 {
447     String aURL;
448     SvxIconChoiceCtrlEntry* pEntry = aIconCtrl.GetCursor( );
449     if ( pEntry )
450         aURL = *static_cast<String*>(pEntry->GetUserData());
451     return aURL;
452 
453 }
454 
455 String SvtIconWindow_Impl::GetSelectedIconURL() const
456 {
457     sal_uLong nPos;
458     SvxIconChoiceCtrlEntry* pEntry = aIconCtrl.GetSelectedEntry( nPos );
459     String aURL;
460     if ( pEntry )
461         aURL = *static_cast<String*>(pEntry->GetUserData());
462     return aURL;
463 }
464 
465 String SvtIconWindow_Impl::GetSelectedIconText() const
466 {
467     sal_uLong nPos;
468     return MnemonicGenerator::EraseAllMnemonicChars( aIconCtrl.GetSelectedEntry( nPos )->GetText() );
469 }
470 
471 String SvtIconWindow_Impl::GetIconText( const String& rURL ) const
472 {
473     String aText;
474     SvxIconChoiceCtrlEntry* pEntry = GetEntry( rURL );
475     if ( pEntry )
476         aText = MnemonicGenerator::EraseAllMnemonicChars( pEntry->GetText() );
477     return aText;
478 }
479 
480 void SvtIconWindow_Impl::InvalidateIconControl()
481 {
482     aIconCtrl.Invalidate();
483 }
484 
485 sal_uLong SvtIconWindow_Impl::GetCursorPos() const
486 {
487     sal_uLong nPos = ~sal_uLong(0);
488 
489     SvxIconChoiceCtrlEntry* pCursorEntry = aIconCtrl.GetCursor( );
490     if ( pCursorEntry )
491         nPos = aIconCtrl.GetEntryListPos( pCursorEntry );
492 
493     return nPos;
494 }
495 
496 sal_uLong SvtIconWindow_Impl::GetSelectEntryPos() const
497 {
498     sal_uLong nPos;
499     if ( !aIconCtrl.GetSelectedEntry( nPos ) )
500         nPos = ~sal_uLong(0);
501     return nPos;
502 }
503 
504 void SvtIconWindow_Impl::SetCursorPos( sal_uLong nPos )
505 {
506     SvxIconChoiceCtrlEntry* pEntry = aIconCtrl.GetEntry( nPos );
507     aIconCtrl.SetCursor( pEntry );
508     aIconCtrl.Invalidate();
509     aIconCtrl.Update();
510 }
511 
512 void SvtIconWindow_Impl::SetFocus()
513 {
514     aIconCtrl.GrabFocus();
515 }
516 
517 long SvtIconWindow_Impl::CalcHeight() const
518 {
519     // calculate the required height of the IconControl
520     long nHeight = 0;
521     sal_uLong nCount = aIconCtrl.GetEntryCount();
522     if ( nCount > 0 )
523         // bottom of the last icon
524         nHeight = aIconCtrl.GetEntry(nCount-1)->GetBoundRect().Bottom();
525 
526     // + headerbar height
527     nHeight += aDummyHeaderBar.GetSizePixel().Height();
528 
529     return nHeight;
530 }
531 
532 sal_Bool SvtIconWindow_Impl::IsRootURL( const String& rURL ) const
533 {
534     return  rURL == aNewDocumentRootURL ||
535             rURL == aTemplateRootURL ||
536             rURL == aMyDocumentsRootURL ||
537             rURL == aSamplesFolderRootURL;
538 }
539 
540 sal_uLong SvtIconWindow_Impl::GetRootPos( const String& rURL ) const
541 {
542     sal_uLong nPos = ~sal_uLong(0);
543     if ( aNewDocumentRootURL.Match( rURL ) == STRING_MATCH )
544         nPos = 0;
545     else if ( aTemplateRootURL.Match( rURL ) == STRING_MATCH )
546         nPos = 1;
547     else if ( aMyDocumentsRootURL.Match( rURL ) == STRING_MATCH )
548         nPos = 2;
549     else if ( aSamplesFolderRootURL.Match( rURL ) == STRING_MATCH )
550         nPos = 3;
551     else if ( rURL.Match( aMyDocumentsRootURL ) == STRING_MATCH )
552         nPos = 2;
553     else
554     {
555         DBG_WARNING( "SvtIconWindow_Impl::GetRootPos(): invalid position" );
556         nPos = 2;
557     }
558 
559     return nPos;
560 }
561 
562 void SvtIconWindow_Impl::UpdateIcons( sal_Bool _bHiContrast )
563 {
564     aIconCtrl.GetEntry( ICON_POS_NEWDOC )->SetImage(
565         Image( SvtResId( _bHiContrast ? IMG_SVT_NEWDOC_HC : IMG_SVT_NEWDOC ) ) );
566     aIconCtrl.GetEntry( ICON_POS_TEMPLATES )->SetImage(
567         Image( SvtResId( _bHiContrast ? IMG_SVT_TEMPLATES_HC : IMG_SVT_TEMPLATES ) ) );
568     aIconCtrl.GetEntry( ICON_POS_MYDOCS )->SetImage(
569         Image( SvtResId( _bHiContrast ? IMG_SVT_MYDOCS_HC : IMG_SVT_MYDOCS ) ) );
570     aIconCtrl.GetEntry( ICON_POS_SAMPLES )->SetImage(
571         Image( SvtResId( _bHiContrast ? IMG_SVT_SAMPLES_HC : IMG_SVT_SAMPLES ) ) );
572 }
573 /* -----------------27.11.2002 16:58-----------------
574  *
575  * --------------------------------------------------*/
576 void SvtIconWindow_Impl::SelectFolder(sal_Int32 nFolderPosition)
577 {
578     SvxIconChoiceCtrlEntry* pEntry = aIconCtrl.GetEntry( nFolderPosition );
579     if(pEntry)
580     {
581         aIconCtrl.SetCursor( pEntry );
582         aIconCtrl.GetClickHdl().Call(&aIconCtrl);
583     }
584 }
585 
586 // class SvtFileViewWindow_Impl -----------------------------------------_
587 
588 SvtFileViewWindow_Impl::SvtFileViewWindow_Impl( SvtTemplateWindow* pParent ) :
589 
590     Window( pParent, WB_DIALOGCONTROL | WB_TABSTOP | WB_BORDER | WB_3DLOOK ),
591 
592     rParent             ( *pParent ),
593     aFileView           ( this, SvtResId( CTRL_FILEVIEW ), FILEVIEW_SHOW_TITLE ),
594     bIsTemplateFolder   ( sal_False )
595 
596 {
597     aFileView.SetStyle( aFileView.GetStyle() | WB_DIALOGCONTROL | WB_TABSTOP );
598     aFileView.SetHelpId( HID_TEMPLATEDLG_FILEVIEW );
599     aFileView.Show();
600     aFileView.SetPosPixel( Point( 0, 0 ) );
601     aFileView.EnableAutoResize();
602     aFileView.EnableContextMenu( sal_False );
603     aFileView.EnableDelete( sal_False );
604 }
605 
606 SvtFileViewWindow_Impl::~SvtFileViewWindow_Impl()
607 {
608 }
609 
610 void GetMenuEntry_Impl
611 (
612     Sequence< PropertyValue >& aDynamicMenuEntry,
613     ::rtl::OUString& rTitle,
614     ::rtl::OUString& rURL,
615     ::rtl::OUString& rFrame,
616     ::rtl::OUString& rImageId
617 )
618 {
619     for ( int i = 0; i < aDynamicMenuEntry.getLength(); i++ )
620     {
621         if ( aDynamicMenuEntry[i].Name == DYNAMICMENU_PROPERTYNAME_URL )
622             aDynamicMenuEntry[i].Value >>= rURL;
623         else if ( aDynamicMenuEntry[i].Name == DYNAMICMENU_PROPERTYNAME_TITLE )
624             aDynamicMenuEntry[i].Value >>= rTitle;
625         else if ( aDynamicMenuEntry[i].Name == DYNAMICMENU_PROPERTYNAME_IMAGEIDENTIFIER )
626             aDynamicMenuEntry[i].Value >>= rImageId;
627         else if ( aDynamicMenuEntry[i].Name == DYNAMICMENU_PROPERTYNAME_TARGETNAME )
628             aDynamicMenuEntry[i].Value >>= rFrame;
629     }
630 }
631 Sequence< ::rtl::OUString > SvtFileViewWindow_Impl::GetNewDocContents() const
632 {
633     NewDocList_Impl aNewDocs;
634     Sequence< Sequence< PropertyValue > > aDynamicMenuEntries;
635     aDynamicMenuEntries = SvtDynamicMenuOptions().GetMenu( E_NEWMENU );
636 
637     ::rtl::OUString aTitle;
638     ::rtl::OUString aURL;
639     ::rtl::OUString aImageURL;
640     ::rtl::OUString aTargetFrame;
641 
642     sal_uInt32 i, nCount = aDynamicMenuEntries.getLength();
643     ::rtl::OUString sSeparator( ASCII_STR("private:separator") );
644     ::rtl::OUString sSlotURL( ASCII_STR("slot:5500") );
645 
646     for ( i = 0; i < nCount; ++i )
647     {
648         GetMenuEntry_Impl( aDynamicMenuEntries[i], aTitle, aURL, aTargetFrame, aImageURL );
649 
650         if ( aURL == sSeparator || aURL == sSlotURL )
651             continue;
652         else
653         {
654             // title
655             String aRow = MnemonicGenerator::EraseAllMnemonicChars( String( aTitle ) );
656             aRow += '\t';
657             // no type
658             aRow += '\t';
659             // no size
660             aRow += '\t';
661             // no date
662             aRow += '\t';
663             // url
664             aRow += String( aURL );
665             aRow += '\t';
666             // folder == false
667             aRow += '0';
668             // image url?
669             if ( aImageURL.getLength() > 0 )
670             {
671                 aRow += '\t';
672                 aRow += String( aImageURL );
673             }
674 
675             ::rtl::OUString* pRow = new ::rtl::OUString( aRow );
676             aNewDocs.Insert( pRow, LIST_APPEND );
677         }
678     }
679 
680     nCount = aNewDocs.Count();
681     Sequence < ::rtl::OUString > aRet( nCount );
682     ::rtl::OUString* pRet = aRet.getArray();
683     for ( i = 0; i < nCount; ++i )
684     {
685         ::rtl::OUString* pNewDoc = aNewDocs.GetObject(i);
686         pRet[i] = *( pNewDoc );
687         delete pNewDoc;
688     }
689 
690     return aRet;
691 }
692 
693 void SvtFileViewWindow_Impl::Resize()
694 {
695     Size aWinSize = GetOutputSizePixel();
696 
697     static int  x = 0;
698     static int  y = 0;
699 
700     aWinSize.nA += x;
701     aWinSize.nB += y;
702     aFileView.SetSizePixel( aWinSize );
703 }
704 
705 String SvtFileViewWindow_Impl::GetSelectedFile() const
706 {
707     return aFileView.GetCurrentURL();
708 }
709 
710 void SvtFileViewWindow_Impl::OpenFolder( const String& rURL )
711 {
712     aFolderURL = rURL;
713 
714     rParent.SetPrevLevelButtonState( rURL );
715 
716     aFileView.SetUrlFilter( &aURLFilter );
717 
718     INetProtocol eProt = INetURLObject( rURL ).GetProtocol();
719     bIsTemplateFolder = ( eProt == INET_PROT_VND_SUN_STAR_HIER );
720     bool isNewDocumentFolder = ( eProt == INET_PROT_PRIVATE );
721 
722     aURLFilter.enableFilter( !bIsTemplateFolder && !isNewDocumentFolder );
723 
724     if ( isNewDocumentFolder )
725     {
726         aFileView.EnableNameReplacing( sal_False );
727         aFileView.Initialize( GetNewDocContents() );
728     }
729     else
730     {
731         xub_StrLen nSampFoldLen = aSamplesFolderURL.Len();
732         aFileView.EnableNameReplacing(
733                     nSampFoldLen && rURL.CompareTo( aSamplesFolderURL, nSampFoldLen ) == COMPARE_EQUAL );
734         aFileView.Initialize( rURL, String(), NULL );
735     }
736     aNewFolderLink.Call( this );
737 }
738 
739 sal_Bool SvtFileViewWindow_Impl::HasPreviousLevel( String& rURL ) const
740 {
741     INetURLObject aViewObj( aFileView.GetViewURL() );
742     INetURLObject aRootObj( aCurrentRootURL );
743     INetURLObject aMyDocObj( aMyDocumentsURL );
744 
745     return ( ( aViewObj != aRootObj || aRootObj == aMyDocObj ) && aFileView.GetParentURL( rURL ) );
746 }
747 
748 String SvtFileViewWindow_Impl::GetFolderTitle() const
749 {
750     String aTitle;
751     ::utl::UCBContentHelper::GetTitle( aFolderURL, aTitle );
752     return aTitle;
753 }
754 
755 void SvtFileViewWindow_Impl::SetFocus()
756 {
757     aFileView.SetFocus();
758 }
759 
760 // class SvtDocInfoTable_Impl --------------------------------------------
761 
762 SvtDocInfoTable_Impl::SvtDocInfoTable_Impl() :
763 
764     ResStringArray( SvtResId( STRARY_SVT_DOCINFO ) )
765 
766 {
767 }
768 // -----------------------------------------------------------------------------
769 // class SvtExtendedMultiLineEdit_Impl --------------------------------------------
770 SvtExtendedMultiLineEdit_Impl::SvtExtendedMultiLineEdit_Impl( Window* pParent,WinBits _nBits ) :
771 
772     ExtMultiLineEdit( pParent, _nBits )
773 
774 {
775     SetLeftMargin( 10 );
776 }
777 // -----------------------------------------------------------------------------
778 void SvtExtendedMultiLineEdit_Impl::InsertEntry( const String& rTitle, const String& rValue )
779 {
780     String aText( '\n' );
781     aText += rTitle;
782     aText += ':';
783     InsertText( aText );
784     sal_uLong nPara = GetParagraphCount() - 1;
785     SetAttrib( TextAttribFontWeight( WEIGHT_BOLD ), nPara, 0, aText.Len() );
786 
787     aText = '\n';
788     aText += rValue;
789     InsertText( aText );
790     nPara = GetParagraphCount() - 1;
791     SetAttrib( TextAttribFontWeight( WEIGHT_NORMAL ), nPara, 0, aText.Len() );
792 
793     InsertText( String( '\n' ) );
794 }
795 // -----------------------------------------------------------------------------
796 
797 // -----------------------------------------------------------------------
798 
799 const String& SvtDocInfoTable_Impl::GetString( long nId ) const
800 {
801     sal_uInt32 nPos( FindIndex( nId ) );
802 
803     if ( RESARRAY_INDEX_NOTFOUND != nPos )
804         return ResStringArray::GetString( nPos );
805     else
806         return aEmptyString;
807 }
808 
809 // class SvtFrameWindow_Impl ---------------------------------------------
810 
811 SvtFrameWindow_Impl::SvtFrameWindow_Impl( Window* pParent ) :
812 
813     Window( pParent )
814 
815 {
816     // detect application language
817     aLocale= SvtPathOptions().GetLocale();
818 
819     // create windows and frame
820     pEditWin = new ODocumentInfoPreview( this ,WB_LEFT | WB_VSCROLL | WB_READONLY | WB_BORDER | WB_3DLOOK);
821     pTextWin = new Window( this );
822     xFrame = Reference < XFrame > ( ::comphelper::getProcessServiceFactory()->
823         createInstance( ASCII_STR("com.sun.star.frame.Frame") ), UNO_QUERY );
824     xWindow = VCLUnoHelper::GetInterface( pTextWin );
825     xFrame->initialize( xWindow );
826 
827     // create docinfo instance
828     m_xDocProps.set( ::comphelper::getProcessServiceFactory()->createInstance(
829             ASCII_STR("com.sun.star.document.DocumentProperties") ),
830         UNO_QUERY );
831 
832     pEmptyWin = new Window( this, WB_BORDER | WB_3DLOOK );
833 }
834 
835 SvtFrameWindow_Impl::~SvtFrameWindow_Impl()
836 {
837     delete pEditWin;
838     delete pEmptyWin;
839     xFrame->dispose();
840 }
841 
842 void SvtFrameWindow_Impl::ViewEditWin()
843 {
844     pEmptyWin->Hide();
845     xWindow->setVisible( sal_False );
846     pTextWin->Hide();
847     pEditWin->Show();
848 }
849 
850 void SvtFrameWindow_Impl::ViewTextWin()
851 {
852     pEmptyWin->Hide();
853     pEditWin->Hide();
854     xWindow->setVisible( sal_True );
855     pTextWin->Show();
856 }
857 
858 void SvtFrameWindow_Impl::ViewEmptyWin()
859 {
860     xWindow->setVisible( sal_False );
861     pTextWin->Hide();
862     pEditWin->Hide();
863     pEmptyWin->Show();
864 }
865 
866 void SvtFrameWindow_Impl::ViewNonEmptyWin()
867 {
868     if( bDocInfo )
869         ViewEditWin();
870     else
871         ViewTextWin();
872 }
873 
874 IMPL_STATIC_LINK_NOINSTANCE( SvtFrameWindow_Impl, ExecuteHdl_Impl, SvtExecuteInfo*, pExecuteInfo )
875 {
876     try
877     {
878         pExecuteInfo->xDispatch->dispatch( pExecuteInfo->aTargetURL, Sequence < PropertyValue >() );
879     }
880     catch ( Exception& )
881     {
882     }
883 
884     delete pExecuteInfo;
885     return 0;
886 }
887 
888 void SvtFrameWindow_Impl::ShowDocInfo( const String& rURL )
889 {
890     try
891     {
892         uno::Reference < task::XInteractionHandler > xInteractionHandler( ::comphelper::getProcessServiceFactory()->createInstance(
893             ::rtl::OUString::createFromAscii("com.sun.star.task.InteractionHandler") ), uno::UNO_QUERY );
894         uno::Sequence < beans::PropertyValue> aProps(1);
895         aProps[0].Name = ::rtl::OUString::createFromAscii("InteractionHandler");
896         aProps[0].Value <<= xInteractionHandler;
897         m_xDocProps->loadFromMedium( rURL, aProps );
898         pEditWin->fill( m_xDocProps, rURL );
899     }
900     catch ( UnknownPropertyException& ) {}
901     catch ( Exception& ) {}
902 }
903 
904 void SvtFrameWindow_Impl::Resize()
905 {
906     Size aWinSize = GetOutputSizePixel();
907     pEditWin->SetSizePixel( aWinSize );
908     pTextWin->SetSizePixel( aWinSize );
909     pEmptyWin->SetSizePixel( aWinSize );
910 }
911 
912 void SvtFrameWindow_Impl::OpenFile( const String& rURL, sal_Bool bPreview, sal_Bool bIsTemplate, sal_Bool bAsTemplate )
913 {
914     if ( bPreview )
915         aCurrentURL = rURL;
916 
917     ViewNonEmptyWin();
918     pEditWin->Clear();
919 
920     if ( rURL.Len() > 0 && bPreview && m_xDocProps.is() )
921         ShowDocInfo( rURL );
922 
923     if ( rURL.Len() == 0 )
924     {
925         xFrame->setComponent( Reference < com::sun::star::awt::XWindow >(), Reference < XController >() );
926         ViewEmptyWin();
927     }
928     else if ( !::utl::UCBContentHelper::IsFolder( rURL ) )
929     {
930         com::sun::star::util::URL aURL;
931         aURL.Complete = rURL;
932         Reference < com::sun::star::util::XURLTransformer > xTrans( ::comphelper::getProcessServiceFactory()->
933             createInstance( ASCII_STR("com.sun.star.util.URLTransformer" ) ), UNO_QUERY );
934         xTrans->parseStrict( aURL );
935 
936         String aTarget;
937         Reference < XDispatchProvider > xProv( xFrame, UNO_QUERY );
938         if ( bPreview )
939             aTarget = ASCII_STR("_self");
940         else
941         {
942             // can be removed if the database application change its URL
943             String sServiceScheme( RTL_CONSTASCII_STRINGPARAM( "service:" ) );
944             if ( rURL.Match( sServiceScheme ) != sServiceScheme.Len() )
945                 // service URL has no default target
946                 aTarget = ASCII_STR("_default");
947             xProv = Reference < XDispatchProvider >( ::comphelper::getProcessServiceFactory()->
948                 createInstance( ASCII_STR("com.sun.star.frame.Desktop") ), UNO_QUERY );
949         }
950 
951         Reference < XDispatch > xDisp = xProv.is() ?
952             xProv->queryDispatch( aURL, aTarget, 0 ) : Reference < XDispatch >();
953 
954         if ( xDisp.is() )
955         {
956             if ( bPreview )
957             {
958                 if ( m_aOpenURL != aURL.Complete )
959                 {
960                     WaitObject aWaitCursor( GetParent() );
961                     // disabling must be done here, does not work in ctor because
962                     // execute of the dialog will overwrite it
963                     // ( own execute method would help )
964                     pTextWin->EnableInput( sal_False, sal_True );
965                     if ( pTextWin->IsReallyVisible() )
966                     {
967                         sal_Bool    b = sal_True;
968                         Sequence < PropertyValue > aArgs( 4 );
969                         aArgs[0].Name = ASCII_STR("Preview");
970                         aArgs[0].Value.setValue( &b, ::getBooleanCppuType() );
971                         aArgs[1].Name = ASCII_STR("ReadOnly");
972                         aArgs[1].Value.setValue( &b, ::getBooleanCppuType() );
973                         aArgs[2].Name = ASCII_STR("AsTemplate");    // prevents getting an empty URL with getURL()!
974 
975                         uno::Reference < task::XInteractionHandler > xInteractionHandler( ::comphelper::getProcessServiceFactory()->createInstance(
976                             ::rtl::OUString::createFromAscii("com.sun.star.task.InteractionHandler") ), uno::UNO_QUERY );
977                         aArgs[3].Name = ::rtl::OUString::createFromAscii("InteractionHandler");
978                         aArgs[3].Value <<= xInteractionHandler;
979 
980                         b = sal_False;
981                         aArgs[2].Value.setValue( &b, ::getBooleanCppuType() );
982                         xDisp->dispatch( aURL, aArgs );
983 
984                         ::rtl::OUString                                         aDispURL;
985                         Reference< ::com::sun::star::frame::XController >       xCtrl = xFrame->getController();
986                         if( xCtrl.is() )
987                         {
988                             Reference< ::com::sun::star::frame::XModel >        xMdl = xCtrl->getModel();
989                             if( xMdl.is() )
990                                 aDispURL = xMdl->getURL();
991                         }
992 
993                         if( aDispURL != aURL.Complete )
994                         {
995                             xFrame->setComponent( Reference < com::sun::star::awt::XWindow >(), Reference < XController >() );
996                             ViewEmptyWin();
997                             m_aOpenURL = rtl::OUString();
998                         }
999                         else
1000                             m_aOpenURL = aDispURL;
1001                     }
1002                 }
1003             }
1004             else if ( bIsTemplate )
1005             {
1006                 Sequence < PropertyValue > aArgs( 1 );
1007                 aArgs[0].Name = ASCII_STR("AsTemplate");
1008                 aArgs[0].Value <<= bAsTemplate;
1009                 xDisp->dispatch( aURL, aArgs );
1010                 m_aOpenURL = rtl::OUString();
1011             }
1012             else
1013             {
1014             /*
1015                 SvtExecuteInfo* pExecuteInfo = new SvtExecuteInfo;
1016                 pExecuteInfo->xDispatch = xDisp;
1017                 pExecuteInfo->aTargetURL = aURL;
1018                 Application::PostUserEvent(
1019                     STATIC_LINK(0, SvtFrameWindow_Impl, ExecuteHdl_Impl), pExecuteInfo );
1020             */
1021                 Sequence < PropertyValue > aArgs;
1022                 xDisp->dispatch( aURL, aArgs );
1023                 m_aOpenURL = rtl::OUString();
1024             }
1025         }
1026     }
1027 }
1028 
1029 void SvtFrameWindow_Impl::ToggleView( sal_Bool bDI )
1030 {
1031     bDocInfo = bDI;
1032 
1033     // view is set properly in OpenFile()
1034 
1035     OpenFile( aCurrentURL, sal_True, sal_False, sal_False );
1036 }
1037 
1038 // class SvtTemplateWindow -----------------------------------------------
1039 
1040 SvtTemplateWindow::SvtTemplateWindow( Window* pParent ) :
1041 
1042     Window( pParent, WB_DIALOGCONTROL ),
1043 
1044     aFileViewTB             ( this, SvtResId( TB_SVT_FILEVIEW ) ),
1045     aFrameWinTB             ( this, SvtResId( TB_SVT_FRAMEWIN ) ),
1046     aSplitWin               ( this, WB_DIALOGCONTROL | WB_NOSPLITDRAW ),
1047     pHistoryList            ( NULL )
1048 
1049 {
1050     // create windows
1051     pIconWin = new SvtIconWindow_Impl( this );
1052     pFileWin = new SvtFileViewWindow_Impl( this );
1053     pFileWin->SetMyDocumentsURL( pIconWin->GetMyDocumentsRootURL() );
1054     pFileWin->SetSamplesFolderURL( pIconWin->GetSamplesFolderURL() );
1055     pFrameWin = new SvtFrameWindow_Impl( this );
1056 
1057     // set handlers
1058     pIconWin->SetClickHdl( LINK( this, SvtTemplateWindow, IconClickHdl_Impl ) );
1059     pFileWin->SetSelectHdl( LINK( this, SvtTemplateWindow, FileSelectHdl_Impl ) );
1060     pFileWin->SetDoubleClickHdl( LINK( this, SvtTemplateWindow, FileDblClickHdl_Impl ) );
1061     pFileWin->SetNewFolderHdl( LINK( this, SvtTemplateWindow, NewFolderHdl_Impl ) );
1062 
1063     // create the split items
1064     aSplitWin.SetAlign( WINDOWALIGN_LEFT );
1065     long nWidth = pIconWin->GetMaxTextLength() * 8 / 7 + 1; // extra space for border
1066     aSplitWin.InsertItem( ICONWIN_ID, pIconWin, nWidth, SPLITWINDOW_APPEND, 0, SWIB_FIXED );
1067     aSplitWin.InsertItem( FILEWIN_ID, pFileWin, 50, SPLITWINDOW_APPEND, 0, SWIB_PERCENTSIZE );
1068     aSplitWin.InsertItem( FRAMEWIN_ID, pFrameWin, 50, SPLITWINDOW_APPEND, 0, SWIB_PERCENTSIZE );
1069     aSplitWin.SetSplitHdl( LINK( this, SvtTemplateWindow, ResizeHdl_Impl ) );
1070 
1071     // show the windows
1072     pIconWin->Show();
1073     pFileWin->Show();
1074     pFrameWin->Show();
1075     aSplitWin.Show();
1076 
1077     // initialize the timers
1078     aSelectTimer.SetTimeout( 200 );
1079     aSelectTimer.SetTimeoutHdl( LINK( this, SvtTemplateWindow, TimeoutHdl_Impl ) );
1080 
1081     // initialize the toolboxes and then show them
1082     InitToolBoxes();
1083     aFileViewTB.Show();
1084     aFrameWinTB.Show();
1085 
1086     ReadViewSettings( );
1087 
1088     Application::PostUserEvent( LINK( this, SvtTemplateWindow, ResizeHdl_Impl ) );
1089 }
1090 
1091 // ------------------------------------------------------------------------
1092 
1093 SvtTemplateWindow::~SvtTemplateWindow()
1094 {
1095     WriteViewSettings( );
1096 
1097     delete pIconWin;
1098     delete pFileWin;
1099     delete pFrameWin;
1100     if ( pHistoryList )
1101     {
1102         for ( sal_uInt32 i = 0; i < pHistoryList->Count(); ++i )
1103             delete pHistoryList->GetObject(i);
1104         delete pHistoryList;
1105     }
1106 }
1107 
1108 // ------------------------------------------------------------------------
1109 
1110 IMPL_LINK ( SvtTemplateWindow , IconClickHdl_Impl, SvtIconChoiceCtrl *, EMPTYARG )
1111 {
1112     String aURL = pIconWin->GetSelectedIconURL();
1113     if ( !aURL.Len() )
1114         aURL = pIconWin->GetCursorPosIconURL();
1115     if ( pFileWin->GetRootURL() != aURL )
1116     {
1117         pFileWin->OpenRoot( aURL );
1118         pIconWin->InvalidateIconControl();
1119         aFileViewTB.EnableItem( TI_DOCTEMPLATE_PRINT, sal_False );
1120     }
1121     return 0;
1122 }
1123 
1124 // ------------------------------------------------------------------------
1125 
1126 IMPL_LINK ( SvtTemplateWindow , FileSelectHdl_Impl, SvtFileView *, EMPTYARG )
1127 {
1128     aSelectTimer.Start();
1129     return 0;
1130 }
1131 
1132 // ------------------------------------------------------------------------
1133 
1134 IMPL_LINK ( SvtTemplateWindow , FileDblClickHdl_Impl, SvtFileView *, EMPTYARG )
1135 {
1136     if ( aSelectTimer.IsActive() )
1137         aSelectTimer.Stop();
1138 
1139     String aURL = pFileWin->GetSelectedFile();
1140     if ( aURL.Len() > 0 )
1141     {
1142         if ( ::utl::UCBContentHelper::IsFolder( aURL ) )
1143             pFileWin->OpenFolder( aURL );
1144         else
1145             aDoubleClickHdl.Call( this );
1146     }
1147 
1148     return 0;
1149 }
1150 
1151 // ------------------------------------------------------------------------
1152 
1153 IMPL_LINK ( SvtTemplateWindow , NewFolderHdl_Impl, SvtFileView *, EMPTYARG )
1154 {
1155     pFrameWin->OpenFile( String(), sal_True, sal_False, sal_False );
1156     aFileViewTB.EnableItem( TI_DOCTEMPLATE_PRINT, sal_False );
1157 
1158     String sURL = pFileWin->GetFolderURL();
1159     sal_uLong nPos = pIconWin->GetRootPos( sURL );
1160     AppendHistoryURL( sURL, nPos );
1161 
1162     aNewFolderHdl.Call( this );
1163     return 0;
1164 }
1165 
1166 // ------------------------------------------------------------------------
1167 
1168 IMPL_LINK ( SvtTemplateWindow , TimeoutHdl_Impl, Timer *, EMPTYARG )
1169 {
1170     aSelectHdl.Call( this );
1171     String sURL = pFileWin->GetSelectedFile();
1172     sal_Bool bIsNewDoc = ( pIconWin->GetSelectEntryPos() == ICON_POS_NEWDOC );
1173     sal_Bool bIsFile = ( sURL.Len() != 0 && !::utl::UCBContentHelper::IsFolder( sURL ) &&
1174                          INetURLObject( sURL ).GetProtocol() != INET_PROT_PRIVATE && !bIsNewDoc );
1175     aFileViewTB.EnableItem( TI_DOCTEMPLATE_PRINT, bIsFile );
1176     aFrameWinTB.EnableItem( TI_DOCTEMPLATE_PREVIEW, !bIsNewDoc );
1177 
1178     if ( bIsFile )
1179         pFrameWin->OpenFile( sURL, sal_True, sal_False, sal_False );
1180     else if ( bIsNewDoc && aFrameWinTB.IsItemChecked( TI_DOCTEMPLATE_PREVIEW ) )
1181     {
1182         aFrameWinTB.CheckItem( TI_DOCTEMPLATE_DOCINFO );
1183         DoAction( TI_DOCTEMPLATE_DOCINFO );
1184     }
1185     return 0;
1186 }
1187 
1188 // ------------------------------------------------------------------------
1189 
1190 IMPL_LINK ( SvtTemplateWindow , ClickHdl_Impl, ToolBox *, pToolBox )
1191 {
1192     DoAction( pToolBox->GetCurItemId() );
1193     return 0;
1194 }
1195 
1196 // ------------------------------------------------------------------------
1197 
1198 IMPL_LINK ( SvtTemplateWindow , ResizeHdl_Impl, SplitWindow *, EMPTYARG )
1199 {
1200     Resize();
1201     return 0;
1202 }
1203 
1204 // ------------------------------------------------------------------------
1205 
1206 void SvtTemplateWindow::PrintFile( const String& rURL )
1207 {
1208     // open the file readonly and hidden
1209     Sequence < PropertyValue > aArgs( 2 );
1210     aArgs[0].Name = ASCII_STR("ReadOnly");
1211     aArgs[0].Value <<= sal_True;
1212     aArgs[1].Name = ASCII_STR("Hidden");
1213     aArgs[1].Value <<= sal_True;
1214 
1215     Reference < XComponentLoader > xDesktop( ::comphelper::getProcessServiceFactory()->
1216         createInstance( ASCII_STR("com.sun.star.frame.Desktop") ), UNO_QUERY );
1217     Reference < XModel > xModel( xDesktop->loadComponentFromURL(
1218         rURL, ASCII_STR("_blank"), 0, aArgs ), UNO_QUERY );
1219     if ( xModel.is() )
1220     {
1221         // print
1222         Reference < XPrintable > xPrintable( xModel, UNO_QUERY );
1223         if ( xPrintable.is() )
1224             xPrintable->print( Sequence < PropertyValue >() );
1225     }
1226 }
1227 
1228 // ------------------------------------------------------------------------
1229 
1230 void SvtTemplateWindow::AppendHistoryURL( const String& rURL, sal_uLong nGroup )
1231 {
1232     sal_Bool bInsert = sal_True;
1233     if ( !pHistoryList )
1234         pHistoryList = new HistoryList_Impl;
1235     else if ( pHistoryList->Count() > 0 )
1236     {
1237         FolderHistory* pLastEntry = pHistoryList->GetObject( pHistoryList->Count() - 1 );
1238         bInsert = ( rURL != pLastEntry->m_sURL);
1239     }
1240 
1241     if ( bInsert )
1242     {
1243         FolderHistory* pEntry = new FolderHistory( rURL, nGroup );
1244         pHistoryList->Insert( pEntry, LIST_APPEND );
1245         aFileViewTB.EnableItem( TI_DOCTEMPLATE_BACK, pHistoryList->Count() > 1 );
1246     }
1247 }
1248 
1249 // ------------------------------------------------------------------------
1250 
1251 void SvtTemplateWindow::OpenHistory()
1252 {
1253     FolderHistory* pEntry = pHistoryList->Remove( pHistoryList->Count() - 1 );
1254     pEntry = pHistoryList->Remove( pHistoryList->Count() - 1 );
1255     aFileViewTB.EnableItem( TI_DOCTEMPLATE_BACK, pHistoryList->Count() > 1 );
1256     pFileWin->OpenFolder( pEntry->m_sURL );
1257     pIconWin->SetCursorPos( pEntry->m_nGroup );
1258     delete pEntry;
1259 }
1260 
1261 // ------------------------------------------------------------------------
1262 
1263 void SvtTemplateWindow::DoAction( sal_uInt16 nAction )
1264 {
1265     switch( nAction )
1266     {
1267         case TI_DOCTEMPLATE_BACK :
1268         {
1269             if ( pHistoryList && pHistoryList->Count() > 1 )
1270                 OpenHistory();
1271             break;
1272         }
1273 
1274         case TI_DOCTEMPLATE_PREV :
1275         {
1276             String aURL;
1277             if ( pFileWin->HasPreviousLevel( aURL ) )
1278                 pFileWin->OpenFolder( aURL );
1279             break;
1280         }
1281 
1282         case TI_DOCTEMPLATE_PRINT :
1283         {
1284             String sPrintFile( pFileWin->GetSelectedFile() );
1285             if ( sPrintFile.Len() > 0 )
1286                 PrintFile( sPrintFile );
1287             break;
1288         }
1289 
1290         case TI_DOCTEMPLATE_DOCINFO :
1291         case TI_DOCTEMPLATE_PREVIEW :
1292         {
1293             pFrameWin->ToggleView( TI_DOCTEMPLATE_DOCINFO == nAction );
1294             break;
1295         }
1296     }
1297 }
1298 
1299 // ------------------------------------------------------------------------
1300 
1301 void SvtTemplateWindow::InitToolBoxes()
1302 {
1303     InitToolBoxImages();
1304 
1305     Size aSize = aFileViewTB.CalcWindowSizePixel();
1306     aSize.Height() += 4;
1307     aFileViewTB.SetPosSizePixel( Point( 0, 2 ), aSize );
1308     aSize = aFrameWinTB.CalcWindowSizePixel();
1309     aSize.Height() += 4;
1310     aFrameWinTB.SetPosSizePixel( Point( pFrameWin->GetPosPixel().X() + 2, 2 ), aSize );
1311 
1312     sal_Bool bFlat = ( SvtMiscOptions().GetToolboxStyle() == TOOLBOX_STYLE_FLAT );
1313     if ( bFlat )
1314     {
1315         aFileViewTB.SetOutStyle( TOOLBOX_STYLE_FLAT );
1316         aFrameWinTB.SetOutStyle( TOOLBOX_STYLE_FLAT );
1317     }
1318 
1319     aFileViewTB.EnableItem( TI_DOCTEMPLATE_BACK, sal_False );
1320     aFileViewTB.EnableItem( TI_DOCTEMPLATE_PREV, sal_False );
1321     aFileViewTB.EnableItem( TI_DOCTEMPLATE_PRINT, sal_False );
1322 
1323     Link aLink = LINK( this, SvtTemplateWindow, ClickHdl_Impl );
1324     aFileViewTB.SetClickHdl( aLink );
1325     aFrameWinTB.SetClickHdl( aLink );
1326 }
1327 
1328 // ------------------------------------------------------------------------
1329 
1330 void SvtTemplateWindow::InitToolBoxImages()
1331 {
1332     SvtMiscOptions aMiscOpt;
1333     sal_Bool bLarge = aMiscOpt.AreCurrentSymbolsLarge();
1334     sal_Bool bHiContrast = aFileViewTB.GetSettings().GetStyleSettings().GetHighContrastMode();
1335 
1336     aFileViewTB.SetItemImage( TI_DOCTEMPLATE_BACK, Image( SvtResId(
1337         bLarge ? bHiContrast ? IMG_SVT_DOCTEMPL_HC_BACK_LARGE : IMG_SVT_DOCTEMPLATE_BACK_LARGE
1338                : bHiContrast ? IMG_SVT_DOCTEMPL_HC_BACK_SMALL : IMG_SVT_DOCTEMPLATE_BACK_SMALL ) ) );
1339     aFileViewTB.SetItemImage( TI_DOCTEMPLATE_PREV, Image( SvtResId(
1340         bLarge ? bHiContrast ? IMG_SVT_DOCTEMPL_HC_PREV_LARGE : IMG_SVT_DOCTEMPLATE_PREV_LARGE
1341                : bHiContrast ? IMG_SVT_DOCTEMPL_HC_PREV_SMALL : IMG_SVT_DOCTEMPLATE_PREV_SMALL ) ) );
1342     aFileViewTB.SetItemImage( TI_DOCTEMPLATE_PRINT, Image( SvtResId(
1343         bLarge ? bHiContrast ? IMG_SVT_DOCTEMPL_HC_PRINT_LARGE : IMG_SVT_DOCTEMPLATE_PRINT_LARGE
1344                : bHiContrast ? IMG_SVT_DOCTEMPL_HC_PRINT_SMALL : IMG_SVT_DOCTEMPLATE_PRINT_SMALL ) ) );
1345 
1346     aFrameWinTB.SetItemImage( TI_DOCTEMPLATE_DOCINFO, Image( SvtResId(
1347         bLarge ? bHiContrast ? IMG_SVT_DOCTEMPL_HC_DOCINFO_LARGE : IMG_SVT_DOCTEMPLATE_DOCINFO_LARGE
1348                : bHiContrast ? IMG_SVT_DOCTEMPL_HC_DOCINFO_SMALL : IMG_SVT_DOCTEMPLATE_DOCINFO_SMALL ) ) );
1349     aFrameWinTB.SetItemImage( TI_DOCTEMPLATE_PREVIEW, Image( SvtResId(
1350         bLarge ? bHiContrast ? IMG_SVT_DOCTEMPL_HC_PREVIEW_LARGE : IMG_SVT_DOCTEMPLATE_PREVIEW_LARGE
1351                : bHiContrast ? IMG_SVT_DOCTEMPL_HC_PREVIEW_SMALL : IMG_SVT_DOCTEMPLATE_PREVIEW_SMALL ) ) );
1352 }
1353 
1354 // ------------------------------------------------------------------------
1355 
1356 void SvtTemplateWindow::UpdateIcons()
1357 {
1358     pIconWin->UpdateIcons( aFileViewTB.GetSettings().GetStyleSettings().GetHighContrastMode() );
1359 }
1360 
1361 // ------------------------------------------------------------------------
1362 
1363 long SvtTemplateWindow::PreNotify( NotifyEvent& rNEvt )
1364 {
1365     sal_uInt16 nType = rNEvt.GetType();
1366     long nRet = 0;
1367 
1368     if ( EVENT_KEYINPUT == nType && rNEvt.GetKeyEvent() )
1369     {
1370         const KeyCode& rKeyCode = rNEvt.GetKeyEvent()->GetKeyCode();
1371         sal_uInt16 nCode = rKeyCode.GetCode();
1372 
1373         if ( KEY_BACKSPACE == nCode && !rKeyCode.GetModifier() && pFileWin->HasChildPathFocus() )
1374         {
1375             DoAction( TI_DOCTEMPLATE_BACK );
1376             nRet = 1;
1377         }
1378         else if ( pIconWin->ProcessKeyEvent( *rNEvt.GetKeyEvent() ) )
1379         {
1380             nRet = 1;
1381         }
1382     }
1383 
1384     return nRet ? nRet : Window::PreNotify( rNEvt );
1385 }
1386 
1387 // -----------------------------------------------------------------------------
1388 
1389 void SvtTemplateWindow::DataChanged( const DataChangedEvent& rDCEvt )
1390 {
1391     Window::DataChanged( rDCEvt );
1392 
1393     if ( ( ( rDCEvt.GetType() == DATACHANGED_SETTINGS ) ||
1394            ( rDCEvt.GetType() == DATACHANGED_DISPLAY ) ) &&
1395          ( rDCEvt.GetFlags() & SETTINGS_STYLE ) )
1396     {
1397         // update of the background for the area left of the FileView toolbox
1398         SetBackground( Wallpaper( GetSettings().GetStyleSettings().GetFaceColor() ) );
1399         // update of the images of the IconChoiceControl
1400         UpdateIcons();
1401         // update of the toolbox images
1402         InitToolBoxImages();
1403     }
1404 }
1405 // ------------------------------------------------------------------------
1406 
1407 void SvtTemplateWindow::Resize()
1408 {
1409     long nItemSize = aSplitWin.GetItemSize( ICONWIN_ID );
1410     long nSplitterWidth = Splitter( this, 0 ).GetSizePixel().Width();
1411 
1412     Point aPos = aFileViewTB.GetPosPixel();
1413     aPos.X() = nItemSize + nSplitterWidth / 2;
1414     aFileViewTB.SetPosPixel( aPos );
1415 
1416     Size aWinSize = GetOutputSizePixel();
1417     long nWidth = aWinSize.Width() - aPos.X();
1418 
1419     nItemSize = nWidth * aSplitWin.GetItemSize( FILEWIN_ID ) / 100;
1420     aPos.X() = pFrameWin->GetPosPixel().X() + 2;
1421     aFrameWinTB.SetPosPixel( aPos );
1422 
1423     Size aSize = aFileViewTB.GetSizePixel();
1424     aSize.Width() = nItemSize;
1425     aFileViewTB.SetSizePixel( aSize );
1426 
1427     aSize = aFrameWinTB.GetSizePixel();
1428     aSize.Width() = nWidth - nItemSize;
1429     aFrameWinTB.SetSizePixel( aSize );
1430 
1431     long nToolBoxHeight = aSize.Height() + aFrameWinTB.GetPosPixel().Y();
1432     aSize = aWinSize;
1433     aSize.Height() -= nToolBoxHeight;
1434     aSplitWin.SetPosSizePixel( Point( 0, nToolBoxHeight ), aSize );
1435 }
1436 
1437 // ------------------------------------------------------------------------
1438 
1439 String SvtTemplateWindow::GetSelectedFile() const
1440 {
1441     return pFileWin->GetSelectedFile();
1442 }
1443 
1444 // ------------------------------------------------------------------------
1445 
1446 sal_Bool SvtTemplateWindow::IsFileSelected() const
1447 {
1448     String aURL = pFileWin->GetSelectedFile();
1449     sal_Bool bRet = ( aURL.Len() > 0 && !::utl::UCBContentHelper::IsFolder( aURL ) );
1450     return bRet;
1451 }
1452 
1453 // ------------------------------------------------------------------------
1454 
1455 void SvtTemplateWindow::OpenFile( sal_Bool bNotAsTemplate )
1456 {
1457     String aURL = pFileWin->GetSelectedFile();
1458     if ( aURL.Len() > 0 && !::utl::UCBContentHelper::IsFolder( aURL ) )
1459         pFrameWin->OpenFile( aURL, sal_False, pFileWin->IsTemplateFolder(), !bNotAsTemplate );
1460 }
1461 
1462 // ------------------------------------------------------------------------
1463 
1464 String SvtTemplateWindow::GetFolderTitle() const
1465 {
1466     String sTitle;
1467     String sFolderURL = pFileWin->GetFolderURL();
1468     if ( pIconWin->IsRootURL( sFolderURL ) )
1469         sTitle = pIconWin->GetIconText( sFolderURL );
1470     else
1471         sTitle = pFileWin->GetFolderTitle();
1472     return sTitle;
1473 }
1474 
1475 // ------------------------------------------------------------------------
1476 
1477 String SvtTemplateWindow::GetFolderURL() const
1478 {
1479     return pFileWin->GetFolderURL();
1480 }
1481 
1482 
1483 // ------------------------------------------------------------------------
1484 
1485 void SvtTemplateWindow::SetFocus( sal_Bool bIconWin )
1486 {
1487     if ( bIconWin )
1488         pIconWin->SetFocus();
1489     else
1490         pFileWin->SetFocus();
1491 }
1492 
1493 // ------------------------------------------------------------------------
1494 
1495 void SvtTemplateWindow::OpenTemplateRoot()
1496 {
1497     pFileWin->OpenFolder( pIconWin->GetTemplateRootURL() );
1498 }
1499 
1500 // ------------------------------------------------------------------------
1501 
1502 void SvtTemplateWindow::SetPrevLevelButtonState( const String& rURL )
1503 {
1504     // disable the prev level button on root folder of the icon pane (except My Documents)
1505     // and on the root of all (file:/// -> count == 0)
1506     INetURLObject aObj( rURL );
1507     sal_Int32 nCount = aObj.getSegmentCount();
1508     sal_Bool bEnable =
1509         ( nCount > 0 &&
1510             ( !pIconWin->IsRootURL( rURL ) || rURL == pIconWin->GetMyDocumentsRootURL() ) );
1511     aFileViewTB.EnableItem( TI_DOCTEMPLATE_PREV, bEnable );
1512 }
1513 
1514 // ------------------------------------------------------------------------
1515 
1516 void SvtTemplateWindow::ClearHistory()
1517 {
1518     if( pHistoryList )
1519         pHistoryList->Clear();
1520 }
1521 
1522 // ------------------------------------------------------------------------
1523 
1524 long SvtTemplateWindow::CalcHeight() const
1525 {
1526     // toolbox height
1527     long nHeight = aFileViewTB.GetSizePixel().Height();
1528     // + iconwin height
1529     nHeight += pIconWin->CalcHeight();
1530     // + little offset
1531     nHeight += 8;
1532     return nHeight;
1533 }
1534 
1535 // ------------------------------------------------------------------------
1536 
1537 void SvtTemplateWindow::ReadViewSettings()
1538 {
1539     // defaults
1540     sal_Int32 nSelectedGroup    =   ICON_POS_TEMPLATES;
1541     sal_Int32 nSelectedView     =   TI_DOCTEMPLATE_DOCINFO;
1542     double nSplitRatio          =   0.5;
1543     ::rtl::OUString sLastFolder;
1544 
1545     SvtViewOptions aViewSettings( E_DIALOG, VIEWSETTING_NEWFROMTEMPLATE );
1546     if ( aViewSettings.Exists() )
1547     {
1548         // read the settings
1549         Sequence< NamedValue > aSettings = aViewSettings.GetUserData( );
1550 
1551         aViewSettings.GetUserItem( VIEWSETTING_SELECTEDGROUP ) >>= nSelectedGroup;
1552         aViewSettings.GetUserItem( VIEWSETTING_SELECTEDVIEW ) >>= nSelectedView;
1553         aViewSettings.GetUserItem( VIEWSETTING_SPLITRATIO ) >>= nSplitRatio;
1554         aViewSettings.GetUserItem( VIEWSETTING_LASTFOLDER ) >>= sLastFolder;
1555     }
1556     // normalize
1557     if ( nSelectedGroup < ICON_POS_NEWDOC )     nSelectedGroup = ICON_POS_NEWDOC;
1558     if ( nSelectedGroup > ICON_POS_SAMPLES )    nSelectedGroup = ICON_POS_SAMPLES;
1559 
1560     if ( ( TI_DOCTEMPLATE_DOCINFO != nSelectedView ) && ( TI_DOCTEMPLATE_PREVIEW != nSelectedView ) )
1561         nSelectedView = TI_DOCTEMPLATE_DOCINFO;
1562 
1563     if ( nSplitRatio < 0.2 ) nSplitRatio = 0.2;
1564     if ( nSplitRatio > 0.8 ) nSplitRatio = 0.8;
1565 
1566     // change our view according to the settings
1567 
1568     // the selected view (details or preview)
1569     pFrameWin->ToggleView( TI_DOCTEMPLATE_DOCINFO == nSelectedView );
1570     aFrameWinTB.CheckItem( (sal_uInt16)nSelectedView, sal_True );
1571 
1572     // the split ratio
1573     sal_Int32 nSplitFileAndFrameSize = aSplitWin.GetItemSize( FILEWIN_ID ) + aSplitWin.GetItemSize( FRAMEWIN_ID );
1574     sal_Int32 nSplitFileSize = (sal_Int32)(nSplitFileAndFrameSize * nSplitRatio);
1575     sal_Int32 nSplitFrameSize = nSplitFileAndFrameSize - nSplitFileSize;
1576     aSplitWin.SetItemSize( FILEWIN_ID, nSplitFileSize );
1577     aSplitWin.SetItemSize( FRAMEWIN_ID, nSplitFrameSize );
1578     Resize();
1579 
1580     // the selected folder
1581     pIconWin->SetCursorPos( nSelectedGroup );
1582 
1583     // open the last folder or the selected group
1584     if ( sLastFolder.getLength() > 0 )
1585         pFileWin->OpenFolder( sLastFolder );
1586     else
1587         IconClickHdl_Impl( NULL );
1588 }
1589 
1590 // ------------------------------------------------------------------------
1591 
1592 void SvtTemplateWindow::WriteViewSettings()
1593 {
1594     // collect
1595     Sequence< NamedValue > aSettings(4);
1596 
1597     // the selected group
1598     aSettings[0].Name   =   VIEWSETTING_SELECTEDGROUP;
1599     pIconWin->SetFocus();
1600     aSettings[0].Value  <<= (sal_Int32)pIconWin->GetCursorPos( );
1601 
1602     // the selected view mode
1603     aSettings[1].Name   =   VIEWSETTING_SELECTEDVIEW;
1604     aSettings[1].Value  <<= sal_Int32( aFrameWinTB.IsItemChecked( TI_DOCTEMPLATE_DOCINFO ) ? TI_DOCTEMPLATE_DOCINFO : TI_DOCTEMPLATE_PREVIEW );
1605 
1606     // the split ratio
1607     aSettings[2].Name   =   VIEWSETTING_SPLITRATIO;
1608     sal_Int32 nSplitFileSize = aSplitWin.GetItemSize( FILEWIN_ID );
1609     sal_Int32 nSplitFileAndFrameSize = nSplitFileSize + aSplitWin.GetItemSize( FRAMEWIN_ID );
1610     aSettings[2].Value  <<= double( 1.0 * nSplitFileSize / nSplitFileAndFrameSize );
1611 
1612     // last folder
1613     aSettings[3].Name   =   VIEWSETTING_LASTFOLDER;
1614     aSettings[3].Value  <<= ::rtl::OUString( pFileWin->GetFolderURL() );
1615 
1616     // write
1617     SvtViewOptions aViewSettings( E_DIALOG, VIEWSETTING_NEWFROMTEMPLATE );
1618     aViewSettings.SetUserData( aSettings );
1619 }
1620 /* -----------------27.11.2002 17:20-----------------
1621  *
1622  * --------------------------------------------------*/
1623 
1624 void SvtTemplateWindow::SelectFolder(sal_Int32 nFolderPosition)
1625 {
1626     pIconWin->SelectFolder(nFolderPosition);
1627 }
1628 // struct SvtTmplDlg_Impl ------------------------------------------------
1629 
1630 struct SvtTmplDlg_Impl
1631 {
1632     SvtTemplateWindow*  pWin;
1633     String              aTitle;
1634     Timer               aUpdateTimer;
1635     sal_Bool            bSelectNoOpen;
1636 
1637     uno::Reference< util::XOfficeInstallationDirectories > m_xOfficeInstDirs;
1638 
1639 
1640     SvtTmplDlg_Impl( Window* pParent ) : pWin( new SvtTemplateWindow( pParent ) ) ,bSelectNoOpen( sal_False ) {}
1641 
1642     ~SvtTmplDlg_Impl() { delete pWin; }
1643 
1644     uno::Reference< util::XOfficeInstallationDirectories > getOfficeInstDirs();
1645 };
1646 
1647 uno::Reference< util::XOfficeInstallationDirectories > SvtTmplDlg_Impl::getOfficeInstDirs()
1648 {
1649     if ( !m_xOfficeInstDirs.is() )
1650     {
1651         try
1652         {
1653             uno::Reference< lang::XMultiServiceFactory > xSMgr = comphelper::getProcessServiceFactory();
1654 
1655             uno::Reference< beans::XPropertySet > xPropSet( xSMgr, uno::UNO_QUERY );
1656             if ( xPropSet.is() )
1657             {
1658                 uno::Reference< uno::XComponentContext > xCtx;
1659                 xPropSet->getPropertyValue(
1660                     rtl::OUString(
1661                         RTL_CONSTASCII_USTRINGPARAM( "DefaultContext" ) ) )
1662                 >>= xCtx;
1663 
1664                 if ( xCtx.is() )
1665                 {
1666                     xCtx->getValueByName(
1667                         rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
1668                             "/singletons/com.sun.star.util.theOfficeInstallationDirectories" ) ) )
1669                     >>= m_xOfficeInstDirs;
1670                 }
1671             }
1672         }
1673         catch( uno::Exception& )
1674         {}
1675     }
1676 
1677     return m_xOfficeInstDirs;
1678 }
1679 
1680 // class SvtDocumentTemplateDialog ---------------------------------------
1681 
1682 SvtDocumentTemplateDialog::SvtDocumentTemplateDialog( Window* _pParent, SelectOnly ) :
1683     ModalDialog( _pParent, SvtResId( DLG_DOCTEMPLATE ) ),
1684 
1685     aMoreTemplatesLink  ( this, SvtResId( FT_DOCTEMPLATE_LINK ) ),
1686     aLine               ( this, SvtResId( FL_DOCTEMPLATE ) ),
1687     aManageBtn          ( this, SvtResId( BTN_DOCTEMPLATE_MANAGE ) ),
1688     aEditBtn            ( this, SvtResId( BTN_DOCTEMPLATE_EDIT ) ),
1689     aOKBtn              ( this, SvtResId( BTN_DOCTEMPLATE_OPEN ) ),
1690     aCancelBtn          ( this, SvtResId( BTN_DOCTEMPLATE_CANCEL ) ),
1691     aHelpBtn            ( this, SvtResId( BTN_DOCTEMPLATE_HELP ) ),
1692     pImpl               ( NULL )
1693 {
1694     FreeResource();
1695     InitImpl( );
1696 
1697     // no editing of templates
1698     aEditBtn.Hide();
1699 
1700     pImpl->bSelectNoOpen = sal_True;
1701 }
1702 
1703 // ------------------------------------------------------------------------
1704 
1705 SvtDocumentTemplateDialog::SvtDocumentTemplateDialog( Window* pParent ) :
1706 
1707     ModalDialog( pParent, SvtResId( DLG_DOCTEMPLATE ) ),
1708 
1709     aMoreTemplatesLink  ( this, SvtResId( FT_DOCTEMPLATE_LINK ) ),
1710     aLine               ( this, SvtResId( FL_DOCTEMPLATE ) ),
1711     aManageBtn          ( this, SvtResId( BTN_DOCTEMPLATE_MANAGE ) ),
1712     aEditBtn            ( this, SvtResId( BTN_DOCTEMPLATE_EDIT ) ),
1713     aOKBtn              ( this, SvtResId( BTN_DOCTEMPLATE_OPEN ) ),
1714     aCancelBtn          ( this, SvtResId( BTN_DOCTEMPLATE_CANCEL ) ),
1715     aHelpBtn            ( this, SvtResId( BTN_DOCTEMPLATE_HELP ) ),
1716     pImpl               ( NULL )
1717 {
1718     FreeResource();
1719     InitImpl( );
1720 }
1721 
1722 // ------------------------------------------------------------------------
1723 
1724 void SvtDocumentTemplateDialog::InitImpl( )
1725 {
1726     pImpl = new SvtTmplDlg_Impl( this );
1727     pImpl->aTitle = GetText();
1728 
1729     bool bHideLink = ( SvtExtendedSecurityOptions().GetOpenHyperlinkMode()
1730                     == SvtExtendedSecurityOptions::OPEN_NEVER );
1731     if ( !bHideLink )
1732     {
1733     aMoreTemplatesLink.SetURL( String(
1734         RTL_CONSTASCII_STRINGPARAM( "https://templates.openoffice.org" ) ) );
1735     aMoreTemplatesLink.SetClickHdl( LINK( this, SvtDocumentTemplateDialog, OpenLinkHdl_Impl ) );
1736     }
1737     else
1738         aMoreTemplatesLink.Hide();
1739 
1740     aManageBtn.SetClickHdl( LINK( this, SvtDocumentTemplateDialog, OrganizerHdl_Impl ) );
1741     Link aLink = LINK( this, SvtDocumentTemplateDialog, OKHdl_Impl );
1742     aEditBtn.SetClickHdl( aLink );
1743     aOKBtn.SetClickHdl( aLink );
1744 
1745     pImpl->pWin->SetSelectHdl( LINK( this, SvtDocumentTemplateDialog, SelectHdl_Impl ) );
1746     pImpl->pWin->SetDoubleClickHdl( LINK( this, SvtDocumentTemplateDialog, DoubleClickHdl_Impl ) );
1747     pImpl->pWin->SetNewFolderHdl( LINK( this, SvtDocumentTemplateDialog, NewFolderHdl_Impl ) );
1748     pImpl->pWin->SetSendFocusHdl( LINK( this, SvtDocumentTemplateDialog, SendFocusHdl_Impl ) );
1749 
1750     // dynamic height adjustment
1751     long nHeight = pImpl->pWin->CalcHeight();
1752 
1753     Size aSize = GetOutputSizePixel();
1754     Point aPos = aMoreTemplatesLink.GetPosPixel();
1755     Size a6Size = LogicToPixel( Size( 6, 6 ), MAP_APPFONT );
1756     if ( bHideLink )
1757         aPos.Y() += aMoreTemplatesLink.GetSizePixel().Height();
1758     else
1759         aPos.Y() -= a6Size.Height();
1760     long nDelta = aPos.Y() - nHeight;
1761     aSize.Height() -= nDelta;
1762     SetOutputSizePixel( aSize );
1763 
1764     aSize.Height() = nHeight;
1765     aSize.Width() -= ( a6Size.Width() * 2 );
1766     pImpl->pWin->SetPosSizePixel( Point( a6Size.Width(), 0 ), aSize );
1767 
1768     aPos = aMoreTemplatesLink.GetPosPixel();
1769     aPos.Y() -= nDelta;
1770     aMoreTemplatesLink.SetPosPixel( aPos );
1771     aPos = aLine.GetPosPixel();
1772     aPos.Y() -= nDelta;
1773     aLine.SetPosPixel( aPos );
1774     aPos = aManageBtn.GetPosPixel();
1775     aPos.Y() -= nDelta;
1776     aManageBtn.SetPosPixel( aPos );
1777     aPos = aEditBtn.GetPosPixel();
1778     aPos.Y() -= nDelta;
1779     aEditBtn.SetPosPixel( aPos );
1780     aPos = aOKBtn.GetPosPixel();
1781     aPos.Y() -= nDelta;
1782     aOKBtn.SetPosPixel( aPos );
1783     aPos = aCancelBtn.GetPosPixel();
1784     aPos.Y() -= nDelta;
1785     aCancelBtn.SetPosPixel( aPos );
1786     aPos = aHelpBtn.GetPosPixel();
1787     aPos.Y() -= nDelta;
1788     aHelpBtn.SetPosPixel( aPos );
1789 
1790     pImpl->pWin->Show();
1791 
1792     SelectHdl_Impl( NULL );
1793     NewFolderHdl_Impl( NULL );
1794 
1795     UpdateHdl_Impl( NULL );
1796 }
1797 
1798 // ------------------------------------------------------------------------
1799 
1800 SvtDocumentTemplateDialog::~SvtDocumentTemplateDialog()
1801 {
1802     delete pImpl;
1803 }
1804 
1805 // ------------------------------------------------------------------------
1806 
1807 sal_Bool SvtDocumentTemplateDialog::IsFileSelected( ) const
1808 {
1809     return pImpl->pWin->IsFileSelected();
1810 }
1811 
1812 // ------------------------------------------------------------------------
1813 
1814 String SvtDocumentTemplateDialog::GetSelectedFileURL( ) const
1815 {
1816     return pImpl->pWin->GetSelectedFile();
1817 }
1818 
1819 // ------------------------------------------------------------------------
1820 
1821 sal_Bool SvtDocumentTemplateDialog::CanEnableEditBtn() const
1822 {
1823     sal_Bool bEnable = sal_False;
1824 
1825     ::rtl::OUString aFolderURL = pImpl->pWin->GetFolderURL();
1826     if ( pImpl->pWin->IsFileSelected() && aFolderURL.getLength() )
1827     {
1828         ::rtl::OUString aFileTargetURL = pImpl->pWin->GetSelectedFile();
1829         bEnable = aFileTargetURL.getLength() > 0;
1830     }
1831 
1832     return bEnable;
1833 }
1834 
1835 // ------------------------------------------------------------------------
1836 
1837 IMPL_LINK ( SvtDocumentTemplateDialog , SelectHdl_Impl, SvtTemplateWindow *, EMPTYARG )
1838 {
1839     aEditBtn.Enable( pImpl->pWin->IsTemplateFolderOpen() && CanEnableEditBtn() );
1840     aOKBtn.Enable( pImpl->pWin->IsFileSelected() );
1841     return 0;
1842 }
1843 
1844 // ------------------------------------------------------------------------
1845 
1846 IMPL_LINK ( SvtDocumentTemplateDialog , DoubleClickHdl_Impl, SvtTemplateWindow *, EMPTYARG )
1847 {
1848     EndDialog( RET_OK );
1849 
1850     if ( !pImpl->bSelectNoOpen )
1851         pImpl->pWin->OpenFile( !pImpl->pWin->IsTemplateFolderOpen() );
1852     return 0;
1853 }
1854 
1855 // ------------------------------------------------------------------------
1856 
1857 IMPL_LINK ( SvtDocumentTemplateDialog , NewFolderHdl_Impl, SvtTemplateWindow *, EMPTYARG )
1858 {
1859     String aNewTitle( pImpl->aTitle );
1860     aNewTitle += String( ASCII_STR(" - ") );
1861     aNewTitle += pImpl->pWin->GetFolderTitle();
1862     SetText( aNewTitle );
1863 
1864     SelectHdl_Impl( NULL );
1865     return 0;
1866 }
1867 
1868 // ------------------------------------------------------------------------
1869 
1870 IMPL_LINK ( SvtDocumentTemplateDialog , SendFocusHdl_Impl, SvtTemplateWindow *, EMPTYARG )
1871 {
1872     if ( pImpl->pWin->HasIconWinFocus() )
1873         aHelpBtn.GrabFocus();
1874     else
1875     {
1876         if ( aEditBtn.IsEnabled() )
1877             aEditBtn.GrabFocus();
1878         else if ( aOKBtn.IsEnabled() )
1879             aOKBtn.GrabFocus();
1880         else
1881             aCancelBtn.GrabFocus();
1882     }
1883 
1884     return 0;
1885 }
1886 
1887 // ------------------------------------------------------------------------
1888 
1889 IMPL_LINK ( SvtDocumentTemplateDialog , OKHdl_Impl, PushButton *, pBtn )
1890 {
1891     if ( pImpl->pWin->IsFileSelected() )
1892     {
1893         EndDialog( RET_OK );
1894 
1895         if ( !pImpl->bSelectNoOpen )
1896             pImpl->pWin->OpenFile( &aEditBtn == pBtn );
1897     }
1898     return 0;
1899 }
1900 
1901 // ------------------------------------------------------------------------
1902 
1903 IMPL_LINK ( SvtDocumentTemplateDialog , OrganizerHdl_Impl, PushButton *, EMPTYARG )
1904 {
1905     Window* pOldDefWin = Application::GetDefDialogParent();
1906     Application::SetDefDialogParent( this );
1907     Reference < XFramesSupplier > xDesktop = Reference < XFramesSupplier >(
1908         ::comphelper::getProcessServiceFactory()->
1909         createInstance( ASCII_STR("com.sun.star.frame.Desktop") ), UNO_QUERY );
1910     Reference < XFrame > xFrame( xDesktop->getActiveFrame() );
1911     if ( !xFrame.is() )
1912         xFrame = Reference < XFrame >( xDesktop, UNO_QUERY );
1913 
1914     com::sun::star::util::URL aTargetURL;
1915     aTargetURL.Complete = ASCII_STR("slot:5540");
1916     Reference < com::sun::star::util::XURLTransformer > xTrans( ::comphelper::getProcessServiceFactory()->
1917         createInstance( ASCII_STR("com.sun.star.util.URLTransformer") ), UNO_QUERY );
1918     xTrans->parseStrict( aTargetURL );
1919 
1920     Reference < XDispatchProvider > xProv( xFrame, UNO_QUERY );
1921     Reference < XDispatch > xDisp;
1922     xDisp = xProv->queryDispatch( aTargetURL, ::rtl::OUString(), 0 );
1923 
1924     if ( xDisp.is() )
1925     {
1926         Sequence<PropertyValue> aArgs(1);
1927         PropertyValue* pArg = aArgs.getArray();
1928         pArg[0].Name = ASCII_STR("Referer");
1929         pArg[0].Value <<= ASCII_STR("private:user");
1930         xDisp->dispatch( aTargetURL, aArgs );
1931     }
1932 
1933     Application::SetDefDialogParent( pOldDefWin );
1934     return 0;
1935 }
1936 
1937 // ------------------------------------------------------------------------
1938 
1939 IMPL_LINK ( SvtDocumentTemplateDialog, UpdateHdl_Impl, Timer*, _pEventSource )
1940 {
1941     pImpl->pWin->SetFocus( sal_False );
1942     Reference< XDocumentTemplates > xTemplates( ::comphelper::getProcessServiceFactory()->
1943         createInstance( ASCII_STR("com.sun.star.frame.DocumentTemplates") ), UNO_QUERY );
1944     if ( xTemplates.is() )
1945     {
1946         if ( _pEventSource )
1947         {   // it was no direct call, which means it was triggered by the timer, which means we already checked the necessity
1948             WaitObject aWaitCursor( this );
1949             xTemplates->update();
1950             if ( pImpl->pWin->IsTemplateFolderOpen() )
1951             {
1952                 pImpl->pWin->ClearHistory();
1953                 pImpl->pWin->OpenTemplateRoot();
1954             }
1955         }
1956         else
1957         {
1958             // check if we really need to do the update
1959             ::svt::TemplateFolderCache aCache;
1960             if ( aCache.needsUpdate() )
1961             {   // yes -> do it asynchronous (it will take a noticeable time)
1962 
1963                 // (but first store the current state)
1964                 aCache.storeState();
1965 
1966                 // start the timer for the async update
1967                 pImpl->aUpdateTimer.SetTimeout( 300 );
1968                 pImpl->aUpdateTimer.SetTimeoutHdl( LINK( this, SvtDocumentTemplateDialog, UpdateHdl_Impl ) );
1969                 pImpl->aUpdateTimer.Start();
1970             }
1971         }
1972     }
1973     return 0;
1974 }
1975 
1976 // ------------------------------------------------------------------------
1977 
1978 IMPL_LINK ( SvtDocumentTemplateDialog, OpenLinkHdl_Impl, svt::FixedHyperlink*, EMPTYARG )
1979 {
1980     ::rtl::OUString sURL( aMoreTemplatesLink.GetURL() );
1981     if ( sURL.getLength() > 0 )
1982     {
1983         try
1984         {
1985             uno::Reference< com::sun::star::system::XSystemShellExecute > xSystemShell(
1986                 com::sun::star::system::SystemShellExecute::create(
1987                     ::comphelper::getProcessComponentContext() ) );
1988             if ( xSystemShell.is() )
1989                 xSystemShell->execute( sURL, ::rtl::OUString(), com::sun::star::system::SystemShellExecuteFlags::DEFAULTS );
1990             EndDialog( RET_CANCEL );
1991         }
1992         catch( const uno::Exception& e )
1993         {
1994              OSL_TRACE( "Caught exception: %s\n thread terminated.\n",
1995                 rtl::OUStringToOString(e.Message, RTL_TEXTENCODING_UTF8).getStr());
1996         }
1997     }
1998     return 0;
1999 }
2000 
2001 /* -----------------27.11.2002 16:54-----------------
2002  *
2003  * --------------------------------------------------*/
2004 void SvtDocumentTemplateDialog::SelectTemplateFolder()
2005 {
2006     pImpl->pWin->SelectFolder(ICON_POS_TEMPLATES);
2007 }
2008 
2009 /* vim: set noet sw=4 ts=4: */
2010