summaryrefslogtreecommitdiff
path: root/sfx2/source/dialog/taskpane.cxx
blob: 69aedf64ca33d599c4eb03c715167ced0503f574 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */


#include "sfx2/taskpane.hxx"
#include "imagemgr.hxx"
#include "sfx2/sfxsids.hrc"
#include "sfx2/bindings.hxx"
#include "sfx2/dispatch.hxx"
#include "sfxresid.hxx"
#include "sfxlocal.hrc"
#include "helpid.hrc"

#include <com/sun/star/frame/ModuleManager.hpp>
#include <com/sun/star/container/XNameAccess.hpp>
#include <com/sun/star/ui/XToolPanel.hpp>
#include <com/sun/star/ui/XUIElementFactory.hpp>
#include <com/sun/star/awt/XWindowPeer.hpp>
#include <com/sun/star/awt/PosSize.hpp>
#include <com/sun/star/graphic/GraphicProvider.hpp>
#include <com/sun/star/graphic/XGraphicProvider.hpp>
#include <com/sun/star/accessibility/XAccessible.hpp>
#include <com/sun/star/awt/XControl.hpp>
#include <com/sun/star/ui/UIElementFactoryManager.hpp>

#include <comphelper/componentcontext.hxx>
#include <comphelper/namedvaluecollection.hxx>
#include <comphelper/types.hxx>
#include <comphelper/processfactory.hxx>
#include <tools/diagnose_ex.h>
#include <svtools/toolpanel/toolpaneldeck.hxx>
#include <svtools/toolpanel/tablayouter.hxx>
#include <svtools/toolpanel/drawerlayouter.hxx>
#include <unotools/confignode.hxx>
#include <vcl/menu.hxx>
#include <vcl/svapp.hxx>
#include <toolkit/helper/vclunohelper.hxx>
#include <tools/urlobj.hxx>
#include <boost/noncopyable.hpp>

//......................................................................................................................
namespace sfx2
{
//......................................................................................................................

    using ::com::sun::star::uno::Reference;
    using ::com::sun::star::uno::XComponentContext;
    using ::com::sun::star::uno::XInterface;
    using ::com::sun::star::uno::UNO_QUERY;
    using ::com::sun::star::uno::UNO_QUERY_THROW;
    using ::com::sun::star::uno::UNO_SET_THROW;
    using ::com::sun::star::uno::Exception;
    using ::com::sun::star::uno::RuntimeException;
    using ::com::sun::star::uno::Any;
    using ::com::sun::star::uno::makeAny;
    using ::com::sun::star::uno::Sequence;
    using ::com::sun::star::uno::Type;
    using ::com::sun::star::frame::ModuleManager;
    using ::com::sun::star::frame::XModuleManager2;
    using ::com::sun::star::container::XNameAccess;
    using ::com::sun::star::ui::XToolPanel;
    using ::com::sun::star::ui::XUIElementFactory;
    using ::com::sun::star::ui::XUIElementFactoryManager;
    using ::com::sun::star::ui::UIElementFactoryManager;
    using ::com::sun::star::ui::XUIElement;
    using ::com::sun::star::awt::XWindow;
    using ::com::sun::star::frame::XFrame;
    using ::com::sun::star::lang::XComponent;
    using ::com::sun::star::graphic::XGraphicProvider;
    using ::com::sun::star::graphic::XGraphic;
    using ::com::sun::star::accessibility::XAccessible;
    using ::com::sun::star::awt::XControl;

    namespace PosSize = ::com::sun::star::awt::PosSize;

    //==================================================================================================================
    //= helpers
    //==================================================================================================================
    namespace
    {
        //--------------------------------------------------------------------------------------------------------------
        ::utl::OConfigurationTreeRoot lcl_getModuleUIElementStatesConfig( const OUString& i_rModuleIdentifier,
            const OUString& i_rResourceURL = OUString() )
        {
            const Reference<XComponentContext> xContext( ::comphelper::getProcessComponentContext() );
            OUStringBuffer aPathComposer;
            try
            {
                const Reference< XModuleManager2 > xModuleAccess( ModuleManager::create(xContext) );
                const ::comphelper::NamedValueCollection aModuleProps( xModuleAccess->getByName( i_rModuleIdentifier ) );

                const OUString sWindowStateRef( aModuleProps.getOrDefault( "ooSetupFactoryWindowStateConfigRef", OUString() ) );

                aPathComposer.appendAscii(RTL_CONSTASCII_STRINGPARAM(
                    "org.openoffice.Office.UI."));
                aPathComposer.append( sWindowStateRef );
                aPathComposer.appendAscii(RTL_CONSTASCII_STRINGPARAM("/UIElements/States"));
                if ( !i_rResourceURL.isEmpty() )
                {
                    aPathComposer.append('/').append( i_rResourceURL );
                }
            }
            catch( const Exception& )
            {
                DBG_UNHANDLED_EXCEPTION();
            }
            return ::utl::OConfigurationTreeRoot( xContext, aPathComposer.makeStringAndClear(), false );
        }

        //--------------------------------------------------------------------------------------------------------------
        OUString lcl_identifyModule( const Reference< XFrame >& i_rDocumentFrame )
        {
            OUString sModuleName;
            try
            {
                const Reference< XComponentContext > xContext( ::comphelper::getProcessComponentContext() );
                const Reference< XModuleManager2 > xModuleManager( ModuleManager::create(xContext) );
                sModuleName = xModuleManager->identify( i_rDocumentFrame );
            }
            catch( const Exception& )
            {
                DBG_UNHANDLED_EXCEPTION();
            }
            return sModuleName;
        }

        //--------------------------------------------------------------------------------------------------------------
        Reference< XFrame > lcl_getFrame( const SfxBindings* i_pBindings )
        {
            const SfxViewFrame* pViewFrame = i_pBindings->GetDispatcher()->GetFrame();
            const SfxFrame& rFrame = pViewFrame->GetFrame();
            const Reference< XFrame > xFrame( rFrame.GetFrameInterface() );
            return xFrame;
        }

        //--------------------------------------------------------------------------------------------------------------
        OUString lcl_getPanelHelpURL( const ::utl::OConfigurationNode& i_rPanelConfigNode )
        {
            const OUString sHelpURL( ::comphelper::getString( i_rPanelConfigNode.getNodeValue( "HelpURL" ) ) );
            return sHelpURL;
        }

        //--------------------------------------------------------------------------------------------------------------
        Image lcl_getPanelImage( const Reference< XFrame >& i_rDocFrame, const ::utl::OConfigurationNode& i_rPanelConfigNode )
        {
            const OUString sImageURL( ::comphelper::getString( i_rPanelConfigNode.getNodeValue( "ImageURL" ) ) );
            if ( !sImageURL.isEmpty() )
            {
                try
                {
                    ::comphelper::NamedValueCollection aMediaProperties;
                    aMediaProperties.put( "URL", sImageURL );

                    // special handling: if the ImageURL denotes a CommandName, then retrieve the image for that command
                    static const sal_Char pCommandImagePrefix[] = "private:commandimage/";
                    const sal_Int32 nCommandImagePrefixLen = strlen( pCommandImagePrefix );
                    if ( sImageURL.startsWith( pCommandImagePrefix ) )
                    {
                        OUStringBuffer aCommandName;
                        aCommandName.appendAscii(RTL_CONSTASCII_STRINGPARAM(".uno:"));
                        aCommandName.append( sImageURL.copy( nCommandImagePrefixLen ) );
                        const OUString sCommandName( aCommandName.makeStringAndClear() );

                        const Image aPanelImage( GetImage( i_rDocFrame, sCommandName, sal_False ) );
                        return aPanelImage.GetXGraphic();
                    }

                    // otherwise, delegate to the GraphicProvider
                    const Reference< XComponentContext > xContext( ::comphelper::getProcessComponentContext() );
                    const Reference< XGraphicProvider > xGraphicProvider( com::sun::star::graphic::GraphicProvider::create(xContext) );

                    const Reference< XGraphic > xGraphic( xGraphicProvider->queryGraphic( aMediaProperties.getPropertyValues() ), UNO_SET_THROW );
                    return Image( xGraphic );
                }
                catch( const Exception& )
                {
                    DBG_UNHANDLED_EXCEPTION();
                }
            }
            return Image();
        }
    }

    //==================================================================================================================
    //= TaskPaneDockingWindow
    //==================================================================================================================
    //------------------------------------------------------------------------------------------------------------------
    TaskPaneDockingWindow::TaskPaneDockingWindow( SfxBindings* i_pBindings, TaskPaneWrapper& i_rWrapper, Window* i_pParent, WinBits i_nBits )
        :TitledDockingWindow( i_pBindings, &i_rWrapper, i_pParent, i_nBits )
        ,m_aTaskPane( GetContentWindow(), lcl_getFrame( i_pBindings ) )
        ,m_aPaneController( m_aTaskPane, *this )
    {
        m_aTaskPane.Show();
        SetText( SfxResId( SID_TASKPANE ).toString() );
    }

    //------------------------------------------------------------------------------------------------------------------
    void TaskPaneDockingWindow::ActivateToolPanel( const OUString& i_rPanelURL )
    {
        m_aPaneController.ActivateToolPanel( i_rPanelURL );
    }

    //------------------------------------------------------------------------------------------------------------------
    void TaskPaneDockingWindow::GetFocus()
    {
        TitledDockingWindow::GetFocus();
        m_aTaskPane.GrabFocus();
    }

    //------------------------------------------------------------------------------------------------------------------
    void TaskPaneDockingWindow::onLayoutDone()
    {
        m_aTaskPane.SetPosSizePixel( Point(), GetContentWindow().GetOutputSizePixel() );
    }

    //==================================================================================================================
    //= TaskPaneWrapper
    //==================================================================================================================
    //------------------------------------------------------------------------------------------------------------------
    SFX_IMPL_DOCKINGWINDOW( TaskPaneWrapper, SID_TASKPANE );

    //------------------------------------------------------------------------------------------------------------------
    TaskPaneWrapper::TaskPaneWrapper( Window* i_pParent, sal_uInt16 i_nId, SfxBindings* i_pBindings, SfxChildWinInfo* i_pInfo )
        :SfxChildWindow( i_pParent, i_nId )
    {
        pWindow = new TaskPaneDockingWindow( i_pBindings, *this, i_pParent,
            WB_STDDOCKWIN | WB_CLIPCHILDREN | WB_SIZEABLE | WB_3DLOOK | WB_ROLLABLE);
        eChildAlignment = SFX_ALIGN_RIGHT;

        pWindow->SetHelpId( HID_TASKPANE_WINDOW );
        pWindow->SetOutputSizePixel( Size( 300, 450 ) );

        dynamic_cast< SfxDockingWindow* >( pWindow )->Initialize( i_pInfo );
        SetHideNotDelete( sal_True );

        pWindow->Show();
    }

    //------------------------------------------------------------------------------------------------------------------
    void TaskPaneWrapper::ActivateToolPanel( const OUString& i_rPanelURL )
    {
        TaskPaneDockingWindow* pDockingWindow = dynamic_cast< TaskPaneDockingWindow* >( GetWindow() );
        ENSURE_OR_RETURN_VOID( pDockingWindow, "TaskPaneWrapper::ActivateToolPanel: invalid docking window implementation!" );
        pDockingWindow->ActivateToolPanel( i_rPanelURL );
    }

    //==================================================================================================================
    //= CustomPanelUIElement
    //==================================================================================================================
    class CustomPanelUIElement
    {
    public:
        CustomPanelUIElement()
            :m_xUIElement()
            ,m_xToolPanel()
            ,m_xPanelWindow()
        {
        }

        CustomPanelUIElement( const Reference< XUIElement >& i_rUIElement )
            :m_xUIElement( i_rUIElement, UNO_SET_THROW )
            ,m_xToolPanel( i_rUIElement->getRealInterface(), UNO_QUERY_THROW )
            ,m_xPanelWindow( m_xToolPanel->getWindow(), UNO_SET_THROW )
        {
        }

        bool is() const { return m_xPanelWindow.is(); }

        const Reference< XUIElement >&  getUIElement() const { return m_xUIElement; }
        const Reference< XToolPanel >&  getToolPanel() const { return m_xToolPanel; }
        const Reference< XWindow >&     getPanelWindow() const { return m_xPanelWindow; }

    private:
        Reference< XUIElement > m_xUIElement;
        Reference< XToolPanel > m_xToolPanel;
        Reference< XWindow >    m_xPanelWindow;
    };

    //==================================================================================================================
    //= CustomToolPanel
    //==================================================================================================================
    class CustomToolPanel : public ::svt::ToolPanelBase
    {
    public:
        CustomToolPanel( const ::utl::OConfigurationNode& i_rPanelWindowState, const Reference< XFrame >& i_rFrame );

        virtual OUString GetDisplayName() const;
        virtual Image GetImage() const;
        virtual OString GetHelpID() const;
        virtual void Activate( Window& i_rParentWindow );
        virtual void Deactivate();
        virtual void SetSizePixel( const Size& i_rPanelWindowSize );
        virtual void GrabFocus();
        virtual void Dispose();
        virtual Reference< XAccessible >
                    CreatePanelAccessible( const Reference< XAccessible >& i_rParentAccessible );

        const OUString&
                    GetResourceURL() const { return m_sResourceURL; }

    protected:
        ~CustomToolPanel();

    private:
        bool    impl_ensureToolPanelWindow( Window& i_rPanelParentWindow );
        void    impl_updatePanelConfig( const bool i_bVisible ) const;

    private:
        const OUString   m_sUIName;
        const Image             m_aPanelImage;
        const OUString   m_aPanelHelpURL;
        const OUString   m_sResourceURL;
        const OUString   m_sPanelConfigPath;
        Reference< XFrame >     m_xFrame;
        CustomPanelUIElement    m_aCustomPanel;
        bool                    m_bAttemptedCreation;
    };

    //------------------------------------------------------------------------------------------------------------------
    CustomToolPanel::CustomToolPanel( const ::utl::OConfigurationNode& i_rPanelWindowState, const Reference< XFrame >& i_rFrame )
        :m_sUIName( ::comphelper::getString( i_rPanelWindowState.getNodeValue( "UIName" ) ) )
        ,m_aPanelImage( lcl_getPanelImage( i_rFrame, i_rPanelWindowState ) )
        ,m_aPanelHelpURL( lcl_getPanelHelpURL( i_rPanelWindowState ) )
        ,m_sResourceURL( i_rPanelWindowState.getLocalName() )
        ,m_sPanelConfigPath( i_rPanelWindowState.getNodePath() )
        ,m_xFrame( i_rFrame )
        ,m_aCustomPanel()
        ,m_bAttemptedCreation( false )
    {
    }

    //------------------------------------------------------------------------------------------------------------------
    CustomToolPanel::~CustomToolPanel()
    {
    }

    //------------------------------------------------------------------------------------------------------------------
    bool CustomToolPanel::impl_ensureToolPanelWindow( Window& i_rPanelParentWindow )
    {
        if ( m_bAttemptedCreation )
            return m_aCustomPanel.is();

        m_bAttemptedCreation = true;
        try
        {
            const Reference< XUIElementFactoryManager > xFactory = UIElementFactoryManager::create( ::comphelper::getProcessComponentContext() );

            ::comphelper::NamedValueCollection aCreationArgs;
            aCreationArgs.put( "Frame", makeAny( m_xFrame ) );
            aCreationArgs.put( "ParentWindow", makeAny( i_rPanelParentWindow.GetComponentInterface() ) );

            const Reference< XUIElement > xElement(
                xFactory->createUIElement( m_sResourceURL, aCreationArgs.getPropertyValues() ),
                UNO_SET_THROW );

            m_aCustomPanel = CustomPanelUIElement( xElement );
        }
        catch( const Exception& )
        {
            DBG_UNHANDLED_EXCEPTION();
        }
        return m_aCustomPanel.is();
    }

    //------------------------------------------------------------------------------------------------------------------
    void CustomToolPanel::impl_updatePanelConfig( const bool i_bVisible ) const
    {
        ::utl::OConfigurationTreeRoot aConfig( ::comphelper::getProcessComponentContext(), m_sPanelConfigPath, true );

        aConfig.setNodeValue( "Visible", makeAny( i_bVisible ) );
        aConfig.commit();
    }

    //------------------------------------------------------------------------------------------------------------------
    OUString CustomToolPanel::GetDisplayName() const
    {
        return m_sUIName;
    }

    //------------------------------------------------------------------------------------------------------------------
    Image CustomToolPanel::GetImage() const
    {
        return m_aPanelImage;
    }

    static OString lcl_getHelpId( const OUString& _rHelpURL )
    {
        INetURLObject aHID( _rHelpURL );
        if ( aHID.GetProtocol() == INET_PROT_HID )
            return OUStringToOString( aHID.GetURLPath(), RTL_TEXTENCODING_UTF8 );
        else
            return OUStringToOString( _rHelpURL, RTL_TEXTENCODING_UTF8 );
    }

    //------------------------------------------------------------------------------------------------------------------
    OString CustomToolPanel::GetHelpID() const
    {
        return lcl_getHelpId( m_aPanelHelpURL );
    }

    //------------------------------------------------------------------------------------------------------------------
    void CustomToolPanel::Activate( Window& i_rParentWindow )
    {
        ENSURE_OR_RETURN_VOID( impl_ensureToolPanelWindow( i_rParentWindow ), "no panel to activate!" );

        // TODO: we might need a mechanism to decide whether the panel should be destroyed/re-created, or (as it is
        // done now) hidden/shown
        m_aCustomPanel.getPanelWindow()->setVisible( sal_True );

        // update the panel's configuration
        impl_updatePanelConfig( true );
    }

    //------------------------------------------------------------------------------------------------------------------
    void CustomToolPanel::Deactivate()
    {
        ENSURE_OR_RETURN_VOID( m_aCustomPanel.is(), "no panel to deactivate!" );

        m_aCustomPanel.getPanelWindow()->setVisible( sal_False );

        // update the panel's configuration
        impl_updatePanelConfig( false );
    }

    //------------------------------------------------------------------------------------------------------------------
    void CustomToolPanel::SetSizePixel( const Size& i_rPanelWindowSize )
    {
        ENSURE_OR_RETURN_VOID( m_aCustomPanel.is(), "no panel/window to position!" );

        try
        {
            m_aCustomPanel.getPanelWindow()->setPosSize( 0, 0, i_rPanelWindowSize.Width(), i_rPanelWindowSize.Height(),
                PosSize::POSSIZE );
        }
        catch( const Exception& )
        {
            DBG_UNHANDLED_EXCEPTION();
        }
    }

    //------------------------------------------------------------------------------------------------------------------
    void CustomToolPanel::GrabFocus()
    {
        ENSURE_OR_RETURN_VOID( m_aCustomPanel.is(), "no panel/window to focus!" );

        m_aCustomPanel.getPanelWindow()->setFocus();
    }

    //------------------------------------------------------------------------------------------------------------------
    void CustomToolPanel::Dispose()
    {
        if ( !m_bAttemptedCreation )
            // nothing to dispose
            return;

        ENSURE_OR_RETURN_VOID( m_aCustomPanel.is(), "no panel to destroy!" );
        try
        {
            Reference< XComponent > xUIElementComponent( m_aCustomPanel.getUIElement(), UNO_QUERY_THROW );
            xUIElementComponent->dispose();
        }
        catch( const Exception& )
        {
            DBG_UNHANDLED_EXCEPTION();
        }
    }

    //------------------------------------------------------------------------------------------------------------------
    Reference< XAccessible > CustomToolPanel::CreatePanelAccessible( const Reference< XAccessible >& i_rParentAccessible )
    {
        ENSURE_OR_RETURN( m_aCustomPanel.is(), "no panel to ask!", NULL );

        Reference< XAccessible > xPanelAccessible;
        try
        {
            xPanelAccessible.set( m_aCustomPanel.getToolPanel()->createAccessible( i_rParentAccessible ), UNO_SET_THROW );
        }
        catch( const Exception& )
        {
            DBG_UNHANDLED_EXCEPTION();
        }
        return xPanelAccessible;
    }

    //==================================================================================================================
    //= ModuleTaskPane_Impl
    //==================================================================================================================
    class ModuleTaskPane_Impl : public ::boost::noncopyable
    {
    public:
        ModuleTaskPane_Impl( ModuleTaskPane& i_rAntiImpl, const Reference< XFrame >& i_rDocumentFrame,
                const IToolPanelCompare* i_pPanelCompare )
            :m_rAntiImpl( i_rAntiImpl )
            ,m_sModuleIdentifier( lcl_identifyModule( i_rDocumentFrame ) )
            ,m_xFrame( i_rDocumentFrame )
            ,m_aPanelDeck( i_rAntiImpl )
        {
            m_aPanelDeck.Show();
            OnResize();
            impl_initFromConfiguration( i_pPanelCompare );
        }

        ~ModuleTaskPane_Impl()
        {
        }

        void    OnResize();
        void    OnGetFocus();

        static bool ModuleHasToolPanels( const OUString& i_rModuleIdentifier );

              ::svt::ToolPanelDeck& GetPanelDeck()          { return m_aPanelDeck; }
        const ::svt::ToolPanelDeck& GetPanelDeck() const    { return m_aPanelDeck; }

        ::boost::optional< size_t >
                    GetPanelPos( const OUString& i_rResourceURL );
        OUString
                    GetPanelResourceURL( const size_t i_nPanelPos ) const;

        void        SetDrawersLayout();
        void        SetTabsLayout( const ::svt::TabAlignment i_eTabAlignment, const ::svt::TabItemContent i_eTabContent );

    private:
        void    impl_initFromConfiguration( const IToolPanelCompare* i_pPanelCompare );

        static bool
                impl_isToolPanelResource( const OUString& i_rResourceURL );

        DECL_LINK( OnActivatePanel, void* );

    private:
        ModuleTaskPane&             m_rAntiImpl;
        const OUString       m_sModuleIdentifier;
        const Reference< XFrame >   m_xFrame;
        ::svt::ToolPanelDeck        m_aPanelDeck;
    };

    //------------------------------------------------------------------------------------------------------------------
    void ModuleTaskPane_Impl::OnResize()
    {
        m_aPanelDeck.SetPosSizePixel( Point(), m_rAntiImpl.GetOutputSizePixel() );
    }

    //------------------------------------------------------------------------------------------------------------------
    void ModuleTaskPane_Impl::OnGetFocus()
    {
        m_aPanelDeck.GrabFocus();
    }

    //------------------------------------------------------------------------------------------------------------------
    IMPL_LINK( ModuleTaskPane_Impl, OnActivatePanel, void*, i_pArg )
    {
        m_aPanelDeck.ActivatePanel( reinterpret_cast< size_t >( i_pArg ) );
        return 1L;
    }

    //------------------------------------------------------------------------------------------------------------------
    bool ModuleTaskPane_Impl::impl_isToolPanelResource( const OUString& i_rResourceURL )
    {
        return i_rResourceURL.matchAsciiL( RTL_CONSTASCII_STRINGPARAM( "private:resource/toolpanel/" ) );
    }

    //------------------------------------------------------------------------------------------------------------------
    void ModuleTaskPane_Impl::impl_initFromConfiguration( const IToolPanelCompare* i_pPanelCompare )
    {
        const ::utl::OConfigurationTreeRoot aWindowStateConfig( lcl_getModuleUIElementStatesConfig( m_sModuleIdentifier ) );
        if ( !aWindowStateConfig.isValid() )
            return;

        OUString sFirstVisiblePanelResource;
        OUString sFirstPanelResource;

        const Sequence< OUString > aUIElements( aWindowStateConfig.getNodeNames() );
        for (   const OUString* resource = aUIElements.getConstArray();
                resource != aUIElements.getConstArray() + aUIElements.getLength();
                ++resource
            )
        {
            if ( !impl_isToolPanelResource( *resource ) )
                continue;

            sFirstPanelResource = *resource;

            ::utl::OConfigurationNode aResourceNode( aWindowStateConfig.openNode( *resource ) );
            ::svt::PToolPanel pCustomPanel( new CustomToolPanel( aResourceNode, m_xFrame ) );

            size_t nPanelPos = m_aPanelDeck.GetPanelCount();
            if ( i_pPanelCompare )
            {
                // assuming that nobody will insert hundreths of panels, a simple O(n) search should suffice here ...
                while ( nPanelPos > 0 )
                {
                    const short nCompare = i_pPanelCompare->compareToolPanelsURLs(
                        *resource,
                        GetPanelResourceURL( --nPanelPos )
                    );
                    if ( nCompare >= 0 )
                    {
                        ++nPanelPos;
                        break;
                    }
                }
            }
            nPanelPos = m_aPanelDeck.InsertPanel( pCustomPanel, nPanelPos );

            if ( ::comphelper::getBOOL( aResourceNode.getNodeValue( "Visible" ) ) )
                sFirstVisiblePanelResource = *resource;
        }

        if ( sFirstVisiblePanelResource.isEmpty() )
            sFirstVisiblePanelResource = sFirstPanelResource;

        if ( !sFirstVisiblePanelResource.isEmpty() )
        {
            ::boost::optional< size_t > aPanelPos( GetPanelPos( sFirstVisiblePanelResource ) );
            OSL_ENSURE( !!aPanelPos, "ModuleTaskPane_Impl::impl_isToolPanelResource: just inserted it, and it's not there?!" );
            if ( !!aPanelPos )
                m_rAntiImpl.PostUserEvent( LINK( this, ModuleTaskPane_Impl, OnActivatePanel ), reinterpret_cast< void* >( *aPanelPos ) );
        }
    }

    //------------------------------------------------------------------------------------------------------------------
    bool ModuleTaskPane_Impl::ModuleHasToolPanels( const OUString& i_rModuleIdentifier )
    {
        const ::utl::OConfigurationTreeRoot aWindowStateConfig( lcl_getModuleUIElementStatesConfig( i_rModuleIdentifier ) );
        if ( !aWindowStateConfig.isValid() )
            return false;

        const Sequence< OUString > aUIElements( aWindowStateConfig.getNodeNames() );
        for (   const OUString* resource = aUIElements.getConstArray();
                resource != aUIElements.getConstArray() + aUIElements.getLength();
                ++resource
            )
        {
            if ( impl_isToolPanelResource( *resource ) )
                return true;
        }
        return false;
    }

    //------------------------------------------------------------------------------------------------------------------
    ::boost::optional< size_t > ModuleTaskPane_Impl::GetPanelPos( const OUString& i_rResourceURL )
    {
        ::boost::optional< size_t > aPanelPos;
        for ( size_t i = 0; i < m_aPanelDeck.GetPanelCount(); ++i )
        {
            const ::svt::PToolPanel pPanel( m_aPanelDeck.GetPanel( i ) );
            const CustomToolPanel* pCustomPanel = dynamic_cast< const CustomToolPanel* >( pPanel.get() );
            if ( !pCustomPanel )
            {
                SAL_WARN( "sfx2.dialog", "ModuleTaskPane_Impl::GetPanelPos: illegal panel implementation!" );
                continue;
            }

            if ( pCustomPanel->GetResourceURL() == i_rResourceURL )
            {
                aPanelPos = i;
                break;
            }
        }
        return aPanelPos;
    }

    //------------------------------------------------------------------------------------------------------------------
    OUString ModuleTaskPane_Impl::GetPanelResourceURL( const size_t i_nPanelPos ) const
    {
        ENSURE_OR_RETURN( i_nPanelPos < m_aPanelDeck.GetPanelCount(), "ModuleTaskPane_Impl::GetPanelResourceURL: illegal panel position!", OUString() );
        const ::svt::PToolPanel pPanel( m_aPanelDeck.GetPanel( i_nPanelPos ) );
        const CustomToolPanel* pCustomPanel = dynamic_cast< const CustomToolPanel* >( pPanel.get() );
        ENSURE_OR_RETURN( pCustomPanel != NULL, "ModuleTaskPane_Impl::GetPanelPos: illegal panel implementation!", OUString() );
        return pCustomPanel->GetResourceURL();
    }

    //------------------------------------------------------------------------------------------------------------------
    void ModuleTaskPane_Impl::SetDrawersLayout()
    {
        const ::svt::PDeckLayouter pLayouter( m_aPanelDeck.GetLayouter() );
        const ::svt::DrawerDeckLayouter* pDrawerLayouter = dynamic_cast< const ::svt::DrawerDeckLayouter* >( pLayouter.get() );
        if ( pDrawerLayouter != NULL )
            // already have the proper layout
            return;
        m_aPanelDeck.SetLayouter( new ::svt::DrawerDeckLayouter( m_aPanelDeck, m_aPanelDeck ) );
    }

    //------------------------------------------------------------------------------------------------------------------
    void ModuleTaskPane_Impl::SetTabsLayout( const ::svt::TabAlignment i_eTabAlignment, const ::svt::TabItemContent i_eTabContent )
    {
        ::svt::PDeckLayouter pLayouter( m_aPanelDeck.GetLayouter() );
        ::svt::TabDeckLayouter* pTabLayouter = dynamic_cast< ::svt::TabDeckLayouter* >( pLayouter.get() );
        if  (   ( pTabLayouter != NULL )
            &&  ( pTabLayouter->GetTabAlignment() == i_eTabAlignment )
            &&  ( pTabLayouter->GetTabItemContent() == i_eTabContent )
            )
            // already have the requested layout
            return;

        if ( pTabLayouter && ( pTabLayouter->GetTabAlignment() == i_eTabAlignment ) )
        {
            // changing only the item content does not require a new layouter instance
            pTabLayouter->SetTabItemContent( i_eTabContent );
            return;
        }

        m_aPanelDeck.SetLayouter( new ::svt::TabDeckLayouter( m_aPanelDeck, m_aPanelDeck, i_eTabAlignment, i_eTabContent ) );
    }

    //==================================================================================================================
    //= ModuleTaskPane
    //==================================================================================================================
    //------------------------------------------------------------------------------------------------------------------
    ModuleTaskPane::ModuleTaskPane( Window& i_rParentWindow, const Reference< XFrame >& i_rDocumentFrame )
        :Window( &i_rParentWindow, WB_DIALOGCONTROL )
        ,m_pImpl( new ModuleTaskPane_Impl( *this, i_rDocumentFrame, NULL ) )
    {
    }

    //------------------------------------------------------------------------------------------------------------------
    ModuleTaskPane::ModuleTaskPane( Window& i_rParentWindow, const Reference< XFrame >& i_rDocumentFrame,
            const IToolPanelCompare& i_rCompare )
        :Window( &i_rParentWindow, WB_DIALOGCONTROL )
        ,m_pImpl( new ModuleTaskPane_Impl( *this, i_rDocumentFrame, &i_rCompare ) )
    {
    }

    //------------------------------------------------------------------------------------------------------------------
    ModuleTaskPane::~ModuleTaskPane()
    {
    }

    //------------------------------------------------------------------------------------------------------------------
    bool ModuleTaskPane::ModuleHasToolPanels( const Reference< XFrame >& i_rDocumentFrame )
    {
        return ModuleTaskPane_Impl::ModuleHasToolPanels( lcl_identifyModule( i_rDocumentFrame ) );
    }

    //------------------------------------------------------------------------------------------------------------------
    void ModuleTaskPane::Resize()
    {
        Window::Resize();
        m_pImpl->OnResize();
    }

    //------------------------------------------------------------------------------------------------------------------
    void ModuleTaskPane::GetFocus()
    {
        Window::GetFocus();
        m_pImpl->OnGetFocus();
    }

    //------------------------------------------------------------------------------------------------------------------
    ::svt::ToolPanelDeck& ModuleTaskPane::GetPanelDeck()
    {
        return m_pImpl->GetPanelDeck();
    }

    //------------------------------------------------------------------------------------------------------------------
    const ::svt::ToolPanelDeck& ModuleTaskPane::GetPanelDeck() const
    {
        return m_pImpl->GetPanelDeck();
    }

    //------------------------------------------------------------------------------------------------------------------
    ::boost::optional< size_t > ModuleTaskPane::GetPanelPos( const OUString& i_rResourceURL )
    {
        return m_pImpl->GetPanelPos( i_rResourceURL );
    }

    //------------------------------------------------------------------------------------------------------------------
    OUString ModuleTaskPane::GetPanelResourceURL( const size_t i_nPanelPos ) const
    {
        return m_pImpl->GetPanelResourceURL( i_nPanelPos );
    }

    //------------------------------------------------------------------------------------------------------------------
    void ModuleTaskPane::SetDrawersLayout()
    {
        m_pImpl->SetDrawersLayout();
    }

    //------------------------------------------------------------------------------------------------------------------
    void ModuleTaskPane::SetTabsLayout( const ::svt::TabAlignment i_eTabAlignment, const ::svt::TabItemContent i_eTabContent )
    {
        m_pImpl->SetTabsLayout( i_eTabAlignment, i_eTabContent );
    }

    // =====================================================================================================================
    // = PanelSelectorLayout
    // =====================================================================================================================
    enum PanelSelectorLayout
    {
        LAYOUT_DRAWERS,
        LAYOUT_TABS_RIGHT,
        LAYOUT_TABS_LEFT,
        LAYOUT_TABS_TOP,
        LAYOUT_TABS_BOTTOM
    };

    //==================================================================================================================
    //= helper
    //==================================================================================================================
    namespace
    {
        PanelSelectorLayout lcl_getTabLayoutFromAlignment( const SfxChildAlignment i_eAlignment )
        {
            switch ( i_eAlignment )
            {
            case SFX_ALIGN_LEFT:
                return LAYOUT_TABS_LEFT;
            case SFX_ALIGN_TOP:
                return LAYOUT_TABS_TOP;
            case SFX_ALIGN_BOTTOM:
                return LAYOUT_TABS_BOTTOM;
            default:
                return LAYOUT_TABS_RIGHT;
            }
        }
    }

    // =====================================================================================================================
    // = PanelDescriptor
    // =====================================================================================================================
    /** is a helper class for TaskPaneController_Impl, holding the details about a single panel which is not
        contained in the IToolPanel implementation itself.
    */
    struct PanelDescriptor
    {
        ::svt::PToolPanel   pPanel;
        bool                bHidden;

        PanelDescriptor()
            :pPanel()
            ,bHidden( false )
        {
        }

        PanelDescriptor( const ::svt::PToolPanel& i_rPanel )
            :pPanel( i_rPanel )
            ,bHidden( false )
        {
        }
    };

    //==================================================================================================================
    //= TaskPaneController_Impl
    //==================================================================================================================
    class TaskPaneController_Impl   :public ::boost::noncopyable
                                    ,public ::svt::IToolPanelDeckListener
    {
    public:
        TaskPaneController_Impl(
            ModuleTaskPane& i_rTaskPane,
            TitledDockingWindow& i_rDockingWindow
        );
        virtual ~TaskPaneController_Impl();

        void    SetDefaultTitle( const String& i_rTitle );
        void    ActivateToolPanel( const OUString& i_rPanelURL );

    protected:
        // IToolPanelDeckListener overridables
        virtual void PanelInserted( const ::svt::PToolPanel& i_pPanel, const size_t i_nPosition );
        virtual void PanelRemoved( const size_t i_nPosition );
        virtual void ActivePanelChanged( const ::boost::optional< size_t >& i_rOldActive, const ::boost::optional< size_t >& i_rNewActive );
        virtual void LayouterChanged( const ::svt::PDeckLayouter& i_rNewLayouter );
        virtual void Dying();

    private:
        DECL_LINK( OnToolboxClicked, ToolBox* );
        DECL_LINK( OnMenuItemSelected, Menu* );
        DECL_LINK( DockingChanged, TitledDockingWindow* );
        SAL_WNODEPRECATED_DECLARATIONS_PUSH
        ::std::auto_ptr< PopupMenu > impl_createPopupMenu() const;
        SAL_WNODEPRECATED_DECLARATIONS_POP

        /// sets the given layout for the panel selector
        void    impl_setLayout( const PanelSelectorLayout i_eLayout, const bool i_bForce = false );

        /// returns the current layout of the panel selector
        PanelSelectorLayout
                impl_getLayout() const { return m_eCurrentLayout; }

        void    impl_updateDockingWindowTitle();
        void    impl_togglePanelVisibility( const size_t i_nLogicalPanelIndex );
        size_t  impl_getLogicalPanelIndex( const size_t i_nVisibleIndex );

    private:
        enum MenuId
        {
            MID_UNLOCK_TASK_PANEL = 1,
            MID_LOCK_TASK_PANEL = 2,
            MID_LAYOUT_TABS = 3,
            MID_LAYOUT_DRAWERS = 4,
            MID_FIRST_PANEL = 5
        };

    private:
        typedef ::std::vector< PanelDescriptor >    PanelDescriptors;

        ModuleTaskPane&         m_rTaskPane;
        TitledDockingWindow&    m_rDockingWindow;
        sal_uInt16                  m_nViewMenuID;
        PanelSelectorLayout     m_eCurrentLayout;
        PanelDescriptors        m_aPanelRepository;
        bool                    m_bTogglingPanelVisibility;
        OUString         m_sDefaultTitle;
    };

    //------------------------------------------------------------------------------------------------------------------
    TaskPaneController_Impl::TaskPaneController_Impl( ModuleTaskPane& i_rTaskPane, TitledDockingWindow& i_rDockingWindow )
        :m_rTaskPane( i_rTaskPane )
        ,m_rDockingWindow( i_rDockingWindow )
        ,m_nViewMenuID( 0 )
        ,m_eCurrentLayout( LAYOUT_DRAWERS )
        ,m_aPanelRepository()
        ,m_bTogglingPanelVisibility( false )
        ,m_sDefaultTitle()
    {
        m_rDockingWindow.ResetToolBox();
        m_nViewMenuID = m_rDockingWindow.AddDropDownToolBoxItem(
            SfxResId( STR_SFX_TASK_PANE_VIEW ).toString(),
            HID_TASKPANE_VIEW_MENU,
            LINK( this, TaskPaneController_Impl, OnToolboxClicked )
        );
        m_rDockingWindow.SetEndDockingHdl( LINK( this, TaskPaneController_Impl, DockingChanged ) );
        impl_setLayout(LAYOUT_TABS_RIGHT, true);

        m_rTaskPane.GetPanelDeck().AddListener( *this );

        // initialize the panel repository
        for ( size_t i = 0; i < m_rTaskPane.GetPanelDeck().GetPanelCount(); ++i )
        {
            ::svt::PToolPanel pPanel( m_rTaskPane.GetPanelDeck().GetPanel( i ) );
            m_aPanelRepository.push_back( PanelDescriptor( pPanel ) );
        }

        SetDefaultTitle( SfxResId( STR_SFX_TASKS ).toString() );
    }

    //------------------------------------------------------------------------------------------------------------------
    TaskPaneController_Impl::~TaskPaneController_Impl()
    {
        m_rTaskPane.GetPanelDeck().RemoveListener( *this );
        int i = 0;

        // remove the panels which are not under the control of the panel deck currently
        for (   PanelDescriptors::iterator panelPos = m_aPanelRepository.begin();
                panelPos != m_aPanelRepository.end();
                ++panelPos, ++i
            )
        {
            if ( panelPos->bHidden )
                impl_togglePanelVisibility( i );
        }
        m_aPanelRepository.clear();
    }

    // -----------------------------------------------------------------------------------------------------------------
    void TaskPaneController_Impl::SetDefaultTitle( const String& i_rTitle )
    {
        m_sDefaultTitle = i_rTitle;
        impl_updateDockingWindowTitle();
    }

    //------------------------------------------------------------------------------------------------------------------
    void TaskPaneController_Impl::ActivateToolPanel( const OUString& i_rPanelURL )
    {
        ::boost::optional< size_t > aPanelPos( m_rTaskPane.GetPanelPos( i_rPanelURL ) );
        ENSURE_OR_RETURN_VOID( !!aPanelPos, "TaskPaneController_Impl::ActivateToolPanel: no such panel!" );

        if ( aPanelPos == m_rTaskPane.GetPanelDeck().GetActivePanel() )
        {
            ::svt::PToolPanel pPanel( m_rTaskPane.GetPanelDeck().GetPanel( *aPanelPos ) );
            pPanel->GrabFocus();
        }
        else
        {
            m_rTaskPane.GetPanelDeck().ActivatePanel( aPanelPos );
        }
    }

    // -----------------------------------------------------------------------------------------------------------------
    IMPL_LINK( TaskPaneController_Impl, DockingChanged, TitledDockingWindow*, i_pDockingWindow )
    {
        ENSURE_OR_RETURN( i_pDockingWindow && &m_rDockingWindow, "TaskPaneController_Impl::DockingChanged: where does this come from?", 0L );

        if ( impl_getLayout() == LAYOUT_DRAWERS )
            return 0L;

        impl_setLayout( lcl_getTabLayoutFromAlignment( i_pDockingWindow->GetAlignment() ) );
        return 1L;
    }

    // -----------------------------------------------------------------------------------------------------------------
    IMPL_LINK( TaskPaneController_Impl, OnToolboxClicked, ToolBox*, i_pToolBox )
    {
        if ( i_pToolBox->GetCurItemId() == m_nViewMenuID )
        {
            i_pToolBox->EndSelection();

            SAL_WNODEPRECATED_DECLARATIONS_PUSH
            ::std::auto_ptr< PopupMenu > pMenu = impl_createPopupMenu();
            SAL_WNODEPRECATED_DECLARATIONS_POP
            pMenu->SetSelectHdl( LINK( this, TaskPaneController_Impl, OnMenuItemSelected ) );

            // pass toolbox button rect so the menu can stay open on button up
            Rectangle aMenuRect( i_pToolBox->GetItemRect( m_nViewMenuID ) );
            aMenuRect.SetPos( i_pToolBox->GetPosPixel() );
            pMenu->Execute( &m_rDockingWindow, aMenuRect, POPUPMENU_EXECUTE_DOWN );
        }

        return 0;
    }

    // ---------------------------------------------------------------------------------------------------------------------
    IMPL_LINK( TaskPaneController_Impl, OnMenuItemSelected, Menu*, i_pMenu )
    {
        ENSURE_OR_RETURN( i_pMenu, "TaskPaneController_Impl::OnMenuItemSelected: illegal menu!", 0L );

        i_pMenu->Deactivate();
        switch ( i_pMenu->GetCurItemId() )
        {
            case MID_UNLOCK_TASK_PANEL:
                m_rDockingWindow.SetFloatingMode( sal_True );
                break;

            case MID_LOCK_TASK_PANEL:
                m_rDockingWindow.SetFloatingMode( sal_False );
                break;

            case MID_LAYOUT_DRAWERS:
                impl_setLayout( LAYOUT_DRAWERS );
                break;

            case MID_LAYOUT_TABS:
                impl_setLayout( lcl_getTabLayoutFromAlignment( m_rDockingWindow.GetAlignment() ) );
                break;

            default:
            {
                size_t nPanelIndex = size_t( i_pMenu->GetCurItemId() - MID_FIRST_PANEL );
                impl_togglePanelVisibility( nPanelIndex );
            }
            break;
        }

        return 1L;
    }

    // ---------------------------------------------------------------------------------------------------------------------
    size_t TaskPaneController_Impl::impl_getLogicalPanelIndex( const size_t i_nVisibleIndex )
    {
        size_t nLogicalIndex = 0;
        size_t nVisibleIndex( i_nVisibleIndex );
        for ( size_t i=0; i < m_aPanelRepository.size(); ++i )
        {
            if ( !m_aPanelRepository[i].bHidden )
            {
                if ( !nVisibleIndex )
                    break;
                --nVisibleIndex;
            }
            ++nLogicalIndex;
        }
        return nLogicalIndex;
    }

    // ---------------------------------------------------------------------------------------------------------------------
    void TaskPaneController_Impl::PanelInserted( const ::svt::PToolPanel& i_pPanel, const size_t i_nPosition )
    {
        if ( m_bTogglingPanelVisibility )
            return;

        const size_t nLogicalIndex( impl_getLogicalPanelIndex( i_nPosition ) );
        m_aPanelRepository.insert( m_aPanelRepository.begin() + nLogicalIndex, PanelDescriptor( i_pPanel ) );
    }

    // ---------------------------------------------------------------------------------------------------------------------
    void TaskPaneController_Impl::PanelRemoved( const size_t i_nPosition )
    {
        if ( m_bTogglingPanelVisibility )
            return;

        const size_t nLogicalIndex( impl_getLogicalPanelIndex( i_nPosition ) );
        m_aPanelRepository.erase( m_aPanelRepository.begin() + nLogicalIndex );
    }

    // ---------------------------------------------------------------------------------------------------------------------
    void TaskPaneController_Impl::ActivePanelChanged( const ::boost::optional< size_t >& i_rOldActive, const ::boost::optional< size_t >& i_rNewActive )
    {
        if ( impl_getLayout() == LAYOUT_DRAWERS )
            // no adjustment of the title when we use the classical "drawers" layout
            return;

        impl_updateDockingWindowTitle( );
        (void)i_rOldActive;
        (void)i_rNewActive;
    }

    // ---------------------------------------------------------------------------------------------------------------------
    void TaskPaneController_Impl::LayouterChanged( const ::svt::PDeckLayouter& i_rNewLayouter )
    {
        // not interested in
        (void)i_rNewLayouter;
    }

    // ---------------------------------------------------------------------------------------------------------------------
    void TaskPaneController_Impl::Dying()
    {
        OSL_FAIL( "TaskPaneController_Impl::Dying: unexpected call!" );
        // We are expected to live longer than the ToolPanelDeck we work with. Since we remove ourself, in our dtor,
        // as listener from the panel deck, this method here should never be called.
    }

    // ---------------------------------------------------------------------------------------------------------------------
    void TaskPaneController_Impl::impl_togglePanelVisibility( const size_t i_nLogicalPanelIndex )
    {
        ENSURE_OR_RETURN_VOID( i_nLogicalPanelIndex < m_aPanelRepository.size(), "illegal index" );

        // get the actual panel index, within the deck
        size_t nActualPanelIndex(0);
        for ( size_t i=0; i < i_nLogicalPanelIndex; ++i )
        {
            if ( !m_aPanelRepository[i].bHidden )
                ++nActualPanelIndex;
        }

        ::boost::optional< size_t > aActivatePanel;

        m_bTogglingPanelVisibility = true;
        if ( m_aPanelRepository[ i_nLogicalPanelIndex ].bHidden )
        {
            OSL_VERIFY( m_rTaskPane.GetPanelDeck().InsertPanel( m_aPanelRepository[ i_nLogicalPanelIndex ].pPanel, nActualPanelIndex ) == nActualPanelIndex );
            // if there has not been an active panel before, activate the newly inserted one
            ::boost::optional< size_t > aActivePanel( m_rTaskPane.GetPanelDeck().GetActivePanel() );
            if ( !aActivePanel )
                aActivatePanel = nActualPanelIndex;
        }
        else
        {
            OSL_VERIFY( m_rTaskPane.GetPanelDeck().RemovePanel( nActualPanelIndex ).get() == m_aPanelRepository[ i_nLogicalPanelIndex ].pPanel.get() );
        }
        m_bTogglingPanelVisibility = false;
        m_aPanelRepository[ i_nLogicalPanelIndex ].bHidden = !m_aPanelRepository[ i_nLogicalPanelIndex ].bHidden;

        if ( !!aActivatePanel )
            m_rTaskPane.GetPanelDeck().ActivatePanel( *aActivatePanel );
    }

    // ---------------------------------------------------------------------------------------------------------------------
    void TaskPaneController_Impl::impl_setLayout( const PanelSelectorLayout i_eLayout, const bool i_bForce )
    {
        if ( !i_bForce && ( m_eCurrentLayout == i_eLayout ) )
            return;

        switch ( i_eLayout )
        {
        case LAYOUT_DRAWERS:
            m_rTaskPane.SetDrawersLayout();
            break;
        case LAYOUT_TABS_TOP:
            m_rTaskPane.SetTabsLayout( ::svt::TABS_TOP, ::svt::TABITEM_IMAGE_ONLY );
            break;
        case LAYOUT_TABS_BOTTOM:
            m_rTaskPane.SetTabsLayout( ::svt::TABS_BOTTOM, ::svt::TABITEM_IMAGE_ONLY );
            break;
        case LAYOUT_TABS_LEFT:
            m_rTaskPane.SetTabsLayout( ::svt::TABS_LEFT, ::svt::TABITEM_IMAGE_ONLY );
            break;
        case LAYOUT_TABS_RIGHT:
            m_rTaskPane.SetTabsLayout( ::svt::TABS_RIGHT, ::svt::TABITEM_IMAGE_ONLY );
            break;
        }
        m_eCurrentLayout = i_eLayout;

        impl_updateDockingWindowTitle();
    }

    // ---------------------------------------------------------------------------------------------------------------------
    void TaskPaneController_Impl::impl_updateDockingWindowTitle()
    {
        ::boost::optional< size_t > aActivePanel( m_rTaskPane.GetPanelDeck().GetActivePanel() );
        if ( !aActivePanel || ( impl_getLayout() == LAYOUT_DRAWERS ) )
            m_rDockingWindow.SetTitle( m_sDefaultTitle );
        else
        {
            size_t nNewActive( *aActivePanel );
            for ( size_t i=0; i < m_aPanelRepository.size(); ++i )
            {
                if ( m_aPanelRepository[i].bHidden )
                    continue;

                if ( !nNewActive )
                {
                    m_rDockingWindow.SetTitle( m_aPanelRepository[i].pPanel->GetDisplayName() );
                    break;
                }
                --nNewActive;
            }
        }
    }

    // ---------------------------------------------------------------------------------------------------------------------
    SAL_WNODEPRECATED_DECLARATIONS_PUSH
    ::std::auto_ptr< PopupMenu > TaskPaneController_Impl::impl_createPopupMenu() const
    {
        ::std::auto_ptr<PopupMenu> pMenu( new PopupMenu );
        FloatingWindow* pMenuWindow = static_cast< FloatingWindow* >( pMenu->GetWindow() );
        if ( pMenuWindow != NULL )
        {
            pMenuWindow->SetPopupModeFlags ( pMenuWindow->GetPopupModeFlags() | FLOATWIN_POPUPMODE_NOMOUSEUPCLOSE );
        }

        // Add one entry for every tool panel element to individually make
        // them visible or hide them.
        sal_uInt16 nIndex = MID_FIRST_PANEL;
        for ( size_t i=0; i<m_aPanelRepository.size(); ++i, ++nIndex )
        {
            const PanelDescriptor& rPanelDesc( m_aPanelRepository[i] );
            pMenu->InsertItem( nIndex, rPanelDesc.pPanel->GetDisplayName(), MIB_CHECKABLE );
            pMenu->CheckItem( nIndex, !rPanelDesc.bHidden );
        }
        pMenu->InsertSeparator();

    #if OSL_DEBUG_LEVEL > 0
        pMenu->InsertItem( MID_LAYOUT_TABS, OUString("Tab-Layout (exp.)"), MIB_CHECKABLE );
        pMenu->CheckItem( MID_LAYOUT_TABS, impl_getLayout() != LAYOUT_DRAWERS );
        pMenu->InsertItem( MID_LAYOUT_DRAWERS, OUString("Drawer-Layout"), MIB_CHECKABLE );
        pMenu->CheckItem( MID_LAYOUT_DRAWERS, impl_getLayout() == LAYOUT_DRAWERS );

        pMenu->InsertSeparator();
    #endif

        // Add entry for docking or un-docking the tool panel.
        if ( m_rDockingWindow.IsFloatingMode() )
            pMenu->InsertItem(
                MID_LOCK_TASK_PANEL,
                SfxResId( STR_SFX_DOCK ).toString()
            );
        else
            pMenu->InsertItem(
                MID_UNLOCK_TASK_PANEL,
                SfxResId( STR_SFX_UNDOCK ).toString()
            );

        pMenu->RemoveDisabledEntries( sal_False, sal_False );

        return pMenu;
    }
    SAL_WNODEPRECATED_DECLARATIONS_POP

    //==================================================================================================================
    //= TaskPaneController
    //==================================================================================================================
    //------------------------------------------------------------------------------------------------------------------
    TaskPaneController::TaskPaneController( ModuleTaskPane& i_rTaskPane, TitledDockingWindow& i_rDockingWindow )
        :m_pImpl( new TaskPaneController_Impl( i_rTaskPane, i_rDockingWindow ) )
    {
    }

    //------------------------------------------------------------------------------------------------------------------
    TaskPaneController::~TaskPaneController()
    {
    }

    //------------------------------------------------------------------------------------------------------------------
    void TaskPaneController::ActivateToolPanel( const OUString& i_rPanelURL )
    {
        m_pImpl->ActivateToolPanel( i_rPanelURL );
    }

//......................................................................................................................
} // namespace sfx2
//......................................................................................................................

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */