1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "sal/config.h"
22 #include <comphelper/processfactory.hxx>
23 #include <tools/datetime.hxx>
24 #include <unotools/datetime.hxx>
25 #include <vcl/msgbox.hxx>
26 #include <ucbhelper/content.hxx>
27 #include <sfx2/app.hxx>
29 #include "svx/gallery1.hxx"
30 #include "svx/galtheme.hxx"
31 #include "svx/galmisc.hxx"
32 #include "galbrws1.hxx"
33 #include <com/sun/star/util/DateTime.hpp>
34 #include "gallery.hrc"
36 #include <svx/dialogs.hrc>
37 #include <svx/dialmgr.hxx>
39 #include <svx/svxdlg.hxx>
45 using namespace ::rtl
;
46 using namespace ::com::sun::star
;
52 GalleryButton::GalleryButton( GalleryBrowser1
* pParent
, WinBits nWinBits
) :
53 PushButton( pParent
, nWinBits
)
57 // -----------------------------------------------------------------------------
59 GalleryButton::~GalleryButton()
63 // -----------------------------------------------------------------------------
65 void GalleryButton::KeyInput( const KeyEvent
& rKEvt
)
67 if( !static_cast< GalleryBrowser1
* >( GetParent() )->KeyInput( rKEvt
, this ) )
68 PushButton::KeyInput( rKEvt
);
71 // -----------------------
72 // - GalleryThemeListBox -
73 // -----------------------
75 GalleryThemeListBox::GalleryThemeListBox( GalleryBrowser1
* pParent
, WinBits nWinBits
) :
76 ListBox( pParent
, nWinBits
)
81 // -----------------------------------------------------------------------------
83 GalleryThemeListBox::~GalleryThemeListBox()
87 // ------------------------------------------------------------------------
89 void GalleryThemeListBox::InitSettings()
91 SetBackground( Wallpaper( GALLERY_BG_COLOR
) );
92 SetControlBackground( GALLERY_BG_COLOR
);
93 SetControlForeground( GALLERY_FG_COLOR
);
96 // -----------------------------------------------------------------------
98 void GalleryThemeListBox::DataChanged( const DataChangedEvent
& rDCEvt
)
100 if ( ( rDCEvt
.GetType() == DATACHANGED_SETTINGS
) && ( rDCEvt
.GetFlags() & SETTINGS_STYLE
) )
103 ListBox::DataChanged( rDCEvt
);
106 // -----------------------------------------------------------------------------
108 long GalleryThemeListBox::PreNotify( NotifyEvent
& rNEvt
)
112 if( rNEvt
.GetType() == EVENT_COMMAND
)
114 const CommandEvent
* pCEvt
= rNEvt
.GetCommandEvent();
116 if( pCEvt
&& pCEvt
->GetCommand() == COMMAND_CONTEXTMENU
)
117 static_cast< GalleryBrowser1
* >( GetParent() )->ShowContextMenu();
119 else if( rNEvt
.GetType() == EVENT_KEYINPUT
)
121 const KeyEvent
* pKEvt
= rNEvt
.GetKeyEvent();
124 nDone
= static_cast< GalleryBrowser1
* >( GetParent() )->KeyInput( *pKEvt
, this );
127 return( nDone
? nDone
: ListBox::PreNotify( rNEvt
) );
130 // -------------------
131 // - GalleryBrowser1 -
132 // -------------------
134 GalleryBrowser1::GalleryBrowser1(
138 const ::boost::function
<sal_Bool(const KeyEvent
&,Window
*)>& rKeyInputHandler
,
139 const ::boost::function
<void(void)>& rThemeSlectionHandler
)
141 Control ( pParent
, rResId
),
142 maNewTheme ( this, WB_3DLOOK
),
143 mpThemes ( new GalleryThemeListBox( this, WB_TABSTOP
| WB_3DLOOK
| WB_BORDER
| WB_HSCROLL
| WB_VSCROLL
| WB_AUTOHSCROLL
| WB_SORT
) ),
144 mpGallery ( pGallery
),
145 mpExchangeData ( new ExchangeData
),
146 mpThemePropsDlgItemSet( NULL
),
147 aImgNormal ( GalleryResGetBitmapEx( RID_SVXBMP_THEME_NORMAL
) ),
148 aImgDefault ( GalleryResGetBitmapEx( RID_SVXBMP_THEME_DEFAULT
) ),
149 aImgReadOnly ( GalleryResGetBitmapEx( RID_SVXBMP_THEME_READONLY
) ),
150 maKeyInputHandler(rKeyInputHandler
),
151 maThemeSlectionHandler(rThemeSlectionHandler
)
153 StartListening( *mpGallery
);
155 maNewTheme
.SetHelpId( HID_GALLERY_NEWTHEME
);
156 maNewTheme
.SetText( GAL_RESSTR(RID_SVXSTR_GALLERY_CREATETHEME
));
157 maNewTheme
.SetClickHdl( LINK( this, GalleryBrowser1
, ClickNewThemeHdl
) );
159 // disable creation of new themes if a writable directory is not available
160 if( mpGallery
->GetUserURL().GetProtocol() == INET_PROT_NOT_VALID
)
161 maNewTheme
.Disable();
163 mpThemes
->SetHelpId( HID_GALLERY_THEMELIST
);
164 mpThemes
->SetSelectHdl( LINK( this, GalleryBrowser1
, SelectThemeHdl
) );
165 mpThemes
->SetAccessibleName(SVX_RESSTR(RID_SVXSTR_GALLERYPROPS_GALTHEME
));
167 for( sal_uIntPtr i
= 0, nCount
= mpGallery
->GetThemeCount(); i
< nCount
; i
++ )
168 ImplInsertThemeEntry( mpGallery
->GetThemeInfo( i
) );
170 ImplAdjustControls();
171 maNewTheme
.Show( sal_True
);
172 mpThemes
->Show( sal_True
);
175 // -----------------------------------------------------------------------------
177 GalleryBrowser1::~GalleryBrowser1()
179 EndListening( *mpGallery
);
182 delete mpExchangeData
;
183 mpExchangeData
= NULL
;
186 // -----------------------------------------------------------------------------
188 sal_uIntPtr
GalleryBrowser1::ImplInsertThemeEntry( const GalleryThemeEntry
* pEntry
)
190 static const sal_Bool bShowHiddenThemes
= ( getenv( "GALLERY_SHOW_HIDDEN_THEMES" ) != NULL
);
192 sal_uIntPtr nRet
= LISTBOX_ENTRY_NOTFOUND
;
194 if( pEntry
&& ( !pEntry
->IsHidden() || bShowHiddenThemes
) )
198 if( pEntry
->IsReadOnly() )
199 pImage
= &aImgReadOnly
;
200 else if( pEntry
->IsDefault() )
201 pImage
= &aImgDefault
;
203 pImage
= &aImgNormal
;
205 nRet
= mpThemes
->InsertEntry( pEntry
->GetThemeName(), *pImage
);
211 // -----------------------------------------------------------------------------
213 void GalleryBrowser1::ImplAdjustControls()
215 const Size
aOutSize( GetOutputSizePixel() );
216 const long nNewThemeHeight
= LogicToPixel( Size( 0, 14 ), MAP_APPFONT
).Height();
217 const long nStartY
= nNewThemeHeight
+ 4;
219 maNewTheme
.SetPosSizePixel( Point(),
220 Size( aOutSize
.Width(), nNewThemeHeight
) );
222 mpThemes
->SetPosSizePixel( Point( 0, nStartY
),
223 Size( aOutSize
.Width(), aOutSize
.Height() - nStartY
) );
226 // -----------------------------------------------------------------------------
228 void GalleryBrowser1::ImplFillExchangeData( const GalleryTheme
* pThm
, ExchangeData
& rData
)
230 rData
.pTheme
= (GalleryTheme
*) pThm
;
231 rData
.aEditedTitle
= pThm
->GetName();
235 ::ucbhelper::Content
aCnt( pThm
->GetThmURL().GetMainURL( INetURLObject::NO_DECODE
), uno::Reference
< ucb::XCommandEnvironment
>(), comphelper::getProcessComponentContext() );
236 util::DateTime aDateTimeModified
;
237 DateTime
aDateTime( DateTime::EMPTY
);
239 aCnt
.getPropertyValue( OUString( "DateModified" ) ) >>= aDateTimeModified
;
240 ::utl::typeConvert( aDateTimeModified
, aDateTime
);
241 rData
.aThemeChangeDate
= aDateTime
;
242 rData
.aThemeChangeTime
= aDateTime
;
244 catch( const ucb::ContentCreationException
& )
247 catch( const uno::RuntimeException
& )
250 catch( const uno::Exception
& )
255 // -----------------------------------------------------------------------------
257 void GalleryBrowser1::ImplGetExecuteVector(::std::vector
< sal_uInt16
>& o_aExec
)
259 GalleryTheme
* pTheme
= mpGallery
->AcquireTheme( GetSelectedTheme(), *this );
263 sal_Bool bUpdateAllowed
, bRenameAllowed
, bRemoveAllowed
;
264 static const sal_Bool bIdDialog
= ( getenv( "GALLERY_ENABLE_ID_DIALOG" ) != NULL
);
266 if( pTheme
->IsReadOnly() )
267 bUpdateAllowed
= bRenameAllowed
= bRemoveAllowed
= sal_False
;
268 else if( pTheme
->IsDefault() )
270 bUpdateAllowed
= bRenameAllowed
= sal_True
;
271 bRemoveAllowed
= sal_False
;
274 bUpdateAllowed
= bRenameAllowed
= bRemoveAllowed
= sal_True
;
276 if( bUpdateAllowed
&& pTheme
->GetObjectCount() )
277 o_aExec
.push_back( MN_ACTUALIZE
);
280 o_aExec
.push_back( MN_RENAME
);
283 o_aExec
.push_back( MN_DELETE
);
285 if( bIdDialog
&& !pTheme
->IsReadOnly() )
286 o_aExec
.push_back( MN_ASSIGN_ID
);
288 o_aExec
.push_back( MN_PROPERTIES
);
290 mpGallery
->ReleaseTheme( pTheme
, *this );
294 // -----------------------------------------------------------------------------
296 void GalleryBrowser1::ImplGalleryThemeProperties( const String
& rThemeName
, bool bCreateNew
)
298 DBG_ASSERT(!mpThemePropsDlgItemSet
, "mpThemePropsDlgItemSet already set!");
299 mpThemePropsDlgItemSet
= new SfxItemSet( SFX_APP()->GetPool() );
300 GalleryTheme
* pTheme
= mpGallery
->AcquireTheme( rThemeName
, *this );
302 ImplFillExchangeData( pTheme
, *mpExchangeData
);
304 SvxAbstractDialogFactory
* pFact
= SvxAbstractDialogFactory::Create();
305 DBG_ASSERT(pFact
, "Got no AbstractDialogFactory!");
306 VclAbstractDialog2
* pThemeProps
= pFact
->CreateGalleryThemePropertiesDialog( NULL
, mpExchangeData
, mpThemePropsDlgItemSet
);
307 DBG_ASSERT(pThemeProps
, "Got no GalleryThemePropertiesDialog!");
311 pThemeProps
->StartExecuteModal(
312 LINK( this, GalleryBrowser1
, EndNewThemePropertiesDlgHdl
) );
316 pThemeProps
->StartExecuteModal(
317 LINK( this, GalleryBrowser1
, EndThemePropertiesDlgHdl
) );
321 // -----------------------------------------------------------------------------
323 void GalleryBrowser1::ImplEndGalleryThemeProperties( VclAbstractDialog2
* pDialog
, bool bCreateNew
)
325 long nRet
= pDialog
->GetResult();
329 String
aName( mpExchangeData
->pTheme
->GetName() );
331 if( mpExchangeData
->aEditedTitle
.Len() && aName
!= mpExchangeData
->aEditedTitle
)
333 const String
aOldName( aName
);
334 String
aTitle( mpExchangeData
->aEditedTitle
);
335 sal_uInt16 nCount
= 0;
337 while( mpGallery
->HasTheme( aTitle
) && ( nCount
++ < 16000 ) )
339 aTitle
= mpExchangeData
->aEditedTitle
;
341 aTitle
+= OUString::number( nCount
);
344 mpGallery
->RenameTheme( aOldName
, aTitle
);
349 mpThemes
->SelectEntry( mpExchangeData
->pTheme
->GetName() );
350 SelectThemeHdl( NULL
);
354 String
aThemeName( mpExchangeData
->pTheme
->GetName() );
355 mpGallery
->ReleaseTheme( mpExchangeData
->pTheme
, *this );
357 if ( bCreateNew
&& ( nRet
!= RET_OK
) )
359 mpGallery
->RemoveTheme( aThemeName
);
362 // destroy mpThemeProps asynchronously
363 Application::PostUserEvent( LINK( this, GalleryBrowser1
, DestroyThemePropertiesDlgHdl
) );
366 // -----------------------------------------------------------------------------
368 IMPL_LINK( GalleryBrowser1
, EndNewThemePropertiesDlgHdl
, VclAbstractDialog2
*, pDialog
)
370 ImplEndGalleryThemeProperties( pDialog
, true );
374 // -----------------------------------------------------------------------------
376 IMPL_LINK( GalleryBrowser1
, EndThemePropertiesDlgHdl
, VclAbstractDialog2
*, pDialog
)
378 ImplEndGalleryThemeProperties( pDialog
, false );
382 // -----------------------------------------------------------------------------
384 IMPL_LINK( GalleryBrowser1
, DestroyThemePropertiesDlgHdl
, VclAbstractDialog2
*, pDialog
)
387 delete mpThemePropsDlgItemSet
;
388 mpThemePropsDlgItemSet
= 0;
392 // -----------------------------------------------------------------------------
394 void GalleryBrowser1::ImplExecute( sal_uInt16 nId
)
398 case( MN_ACTUALIZE
):
400 GalleryTheme
* pTheme
= mpGallery
->AcquireTheme( GetSelectedTheme(), *this );
402 SvxAbstractDialogFactory
* pFact
= SvxAbstractDialogFactory::Create();
405 VclAbstractRefreshableDialog
* aActualizeProgress
= pFact
->CreateActualizeProgressDialog( this, pTheme
);
406 DBG_ASSERT(aActualizeProgress
, "Dialogdiet fail!");
408 aActualizeProgress
->Update();
409 aActualizeProgress
->Execute();
410 mpGallery
->ReleaseTheme( pTheme
, *this );
411 delete aActualizeProgress
;
418 if( QueryBox( NULL
, WB_YES_NO
, GAL_RESSTR(RID_SVXSTR_GALLERY_DELETETHEME
)).Execute() == RET_YES
)
419 mpGallery
->RemoveTheme( mpThemes
->GetSelectEntry() );
425 GalleryTheme
* pTheme
= mpGallery
->AcquireTheme( GetSelectedTheme(), *this );
426 const String
aOldName( pTheme
->GetName() );
428 SvxAbstractDialogFactory
* pFact
= SvxAbstractDialogFactory::Create();
429 DBG_ASSERT(pFact
, "Dialogdiet fail!");
430 AbstractTitleDialog
* aDlg
= pFact
->CreateTitleDialog( this, aOldName
);
431 DBG_ASSERT(aDlg
, "Dialogdiet fail!");
433 if( aDlg
->Execute() == RET_OK
)
435 const String
aNewName( aDlg
->GetTitle() );
437 if( aNewName
.Len() && ( aNewName
!= aOldName
) )
439 String
aName( aNewName
);
440 sal_uInt16 nCount
= 0;
442 while( mpGallery
->HasTheme( aName
) && ( nCount
++ < 16000 ) )
446 aName
+= OUString::number( nCount
);
449 mpGallery
->RenameTheme( aOldName
, aName
);
452 mpGallery
->ReleaseTheme( pTheme
, *this );
457 case( MN_ASSIGN_ID
):
459 GalleryTheme
* pTheme
= mpGallery
->AcquireTheme( GetSelectedTheme(), *this );
461 if (pTheme
&& !pTheme
->IsReadOnly())
464 SvxAbstractDialogFactory
* pFact
= SvxAbstractDialogFactory::Create();
467 AbstractGalleryIdDialog
* aDlg
= pFact
->CreateGalleryIdDialog( this, pTheme
);
468 DBG_ASSERT(aDlg
, "Dialogdiet fail!");
470 if( aDlg
->Execute() == RET_OK
)
471 pTheme
->SetId( aDlg
->GetId(), sal_True
);
476 mpGallery
->ReleaseTheme( pTheme
, *this );
480 case( MN_PROPERTIES
):
482 ImplGalleryThemeProperties( GetSelectedTheme(), false );
488 // -----------------------------------------------------------------------------
490 void GalleryBrowser1::Resize()
493 ImplAdjustControls();
496 // -----------------------------------------------------------------------------
498 void GalleryBrowser1::GetFocus()
502 mpThemes
->GrabFocus();
505 // -----------------------------------------------------------------------------
507 void GalleryBrowser1::Notify( SfxBroadcaster
&, const SfxHint
& rHint
)
509 const GalleryHint
& rGalleryHint
= (const GalleryHint
&) rHint
;
511 switch( rGalleryHint
.GetType() )
513 case( GALLERY_HINT_THEME_CREATED
):
514 ImplInsertThemeEntry( mpGallery
->GetThemeInfo( rGalleryHint
.GetThemeName() ) );
517 case( GALLERY_HINT_THEME_RENAMED
):
519 const sal_uInt16 nCurSelectPos
= mpThemes
->GetSelectEntryPos();
520 const sal_uInt16 nRenameEntryPos
= mpThemes
->GetEntryPos( rGalleryHint
.GetThemeName() );
522 mpThemes
->RemoveEntry( rGalleryHint
.GetThemeName() );
523 ImplInsertThemeEntry( mpGallery
->GetThemeInfo( rGalleryHint
.GetStringData() ) );
525 if( nCurSelectPos
== nRenameEntryPos
)
527 mpThemes
->SelectEntry( rGalleryHint
.GetStringData() );
528 SelectThemeHdl( NULL
);
533 case( GALLERY_HINT_THEME_REMOVED
):
535 mpThemes
->RemoveEntry( rGalleryHint
.GetThemeName() );
539 case( GALLERY_HINT_CLOSE_THEME
):
541 const sal_uInt16 nCurSelectPos
= mpThemes
->GetSelectEntryPos();
542 const sal_uInt16 nCloseEntryPos
= mpThemes
->GetEntryPos( rGalleryHint
.GetThemeName() );
544 if( nCurSelectPos
== nCloseEntryPos
)
546 if( nCurSelectPos
< ( mpThemes
->GetEntryCount() - 1 ) )
547 mpThemes
->SelectEntryPos( nCurSelectPos
+ 1 );
548 else if( nCurSelectPos
)
549 mpThemes
->SelectEntryPos( nCurSelectPos
- 1 );
551 mpThemes
->SetNoSelection();
553 SelectThemeHdl( NULL
);
563 // -----------------------------------------------------------------------------
565 void GalleryBrowser1::ShowContextMenu()
567 Application::PostUserEvent( LINK( this, GalleryBrowser1
, ShowContextMenuHdl
), this );
570 // -----------------------------------------------------------------------------
572 sal_Bool
GalleryBrowser1::KeyInput( const KeyEvent
& rKEvt
, Window
* pWindow
)
574 sal_Bool
bRet (sal_False
);
575 if (maKeyInputHandler
)
576 bRet
= maKeyInputHandler(rKEvt
, pWindow
);
580 ::std::vector
< sal_uInt16
> aExecVector
;
581 ImplGetExecuteVector(aExecVector
);
582 sal_uInt16 nExecuteId
= 0;
583 sal_Bool bMod1
= rKEvt
.GetKeyCode().IsMod1();
585 switch( rKEvt
.GetKeyCode().GetCode() )
588 ClickNewThemeHdl( NULL
);
594 ClickNewThemeHdl( NULL
);
601 nExecuteId
= MN_ACTUALIZE
;
606 nExecuteId
= MN_DELETE
;
612 nExecuteId
= MN_DELETE
;
619 nExecuteId
= MN_RENAME
;
626 nExecuteId
= MN_PROPERTIES
;
631 if( nExecuteId
&& ( ::std::find( aExecVector
.begin(), aExecVector
.end(), nExecuteId
) != aExecVector
.end() ) )
633 ImplExecute( nExecuteId
);
641 // -----------------------------------------------------------------------------
643 IMPL_LINK_NOARG(GalleryBrowser1
, ShowContextMenuHdl
)
645 ::std::vector
< sal_uInt16
> aExecVector
;
646 ImplGetExecuteVector(aExecVector
);
648 if( !aExecVector
.empty() )
650 PopupMenu
aMenu( GAL_RES( RID_SVXMN_GALLERY1
) );
652 aMenu
.EnableItem( MN_ACTUALIZE
, ::std::find( aExecVector
.begin(), aExecVector
.end(), MN_ACTUALIZE
) != aExecVector
.end() );
653 aMenu
.EnableItem( MN_RENAME
, ::std::find( aExecVector
.begin(), aExecVector
.end(), MN_RENAME
) != aExecVector
.end() );
654 aMenu
.EnableItem( MN_DELETE
, ::std::find( aExecVector
.begin(), aExecVector
.end(), MN_DELETE
) != aExecVector
.end() );
655 aMenu
.EnableItem( MN_ASSIGN_ID
, ::std::find( aExecVector
.begin(), aExecVector
.end(), MN_ASSIGN_ID
) != aExecVector
.end() );
656 aMenu
.EnableItem( MN_PROPERTIES
, ::std::find( aExecVector
.begin(), aExecVector
.end(), MN_PROPERTIES
) != aExecVector
.end() );
657 aMenu
.SetSelectHdl( LINK( this, GalleryBrowser1
, PopupMenuHdl
) );
658 aMenu
.RemoveDisabledEntries();
660 const Rectangle
aThemesRect( mpThemes
->GetPosPixel(), mpThemes
->GetOutputSizePixel() );
661 Point
aSelPos( mpThemes
->GetBoundingRectangle( mpThemes
->GetSelectEntryPos() ).Center() );
663 aSelPos
.X() = std::max( std::min( aSelPos
.X(), aThemesRect
.Right() ), aThemesRect
.Left() );
664 aSelPos
.Y() = std::max( std::min( aSelPos
.Y(), aThemesRect
.Bottom() ), aThemesRect
.Top() );
666 aMenu
.Execute( this, aSelPos
);
672 // -----------------------------------------------------------------------------
674 IMPL_LINK( GalleryBrowser1
, PopupMenuHdl
, Menu
*, pMenu
)
676 ImplExecute( pMenu
->GetCurItemId() );
680 // -----------------------------------------------------------------------------
682 IMPL_LINK_NOARG(GalleryBrowser1
, SelectThemeHdl
)
684 if (maThemeSlectionHandler
)
685 maThemeSlectionHandler();
689 // -----------------------------------------------------------------------------
691 IMPL_LINK_NOARG(GalleryBrowser1
, ClickNewThemeHdl
)
693 String
aNewTheme( GAL_RESSTR(RID_SVXSTR_GALLERY_NEWTHEME
) );
694 String
aName( aNewTheme
);
695 sal_uIntPtr nCount
= 0;
697 while( mpGallery
->HasTheme( aName
) && ( nCount
++ < 16000 ) )
701 aName
+= OUString::number( nCount
);
704 if( !mpGallery
->HasTheme( aName
) && mpGallery
->CreateTheme( aName
) )
706 ImplGalleryThemeProperties( aName
, true );
712 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */