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
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*************************************************************************
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* Copyright 2000, 2010 Oracle and/or its affiliates.
*
* OpenOffice.org - a multi-platform office productivity suite
*
* This file is part of OpenOffice.org.
*
* OpenOffice.org is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3
* only, as published by the Free Software Foundation.
*
* OpenOffice.org is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License version 3 for more details
* (a copy is included in the LICENSE file that accompanied this code).
*
* You should have received a copy of the GNU Lesser General Public License
* version 3 along with OpenOffice.org. If not, see
* <http://www.openoffice.org/license.html>
* for a copy of the LGPLv3 License.
*
************************************************************************/
//_________________________________________________________________________________________________________________
// my own includes
//_________________________________________________________________________________________________________________
#include <classes/menumanager.hxx>
#include <framework/menuconfiguration.hxx>
#include <framework/bmkmenu.hxx>
#include <framework/addonmenu.hxx>
#include <framework/imageproducer.hxx>
#include <threadhelp/resetableguard.hxx>
#include "framework/addonsoptions.hxx"
#include <classes/fwkresid.hxx>
#include <services.h>
#include "classes/resource.hrc"
//_________________________________________________________________________________________________________________
// interface includes
//_________________________________________________________________________________________________________________
#include <com/sun/star/frame/XDispatchProvider.hpp>
#include <com/sun/star/frame/XDispatch.hpp>
#include <com/sun/star/util/XURLTransformer.hpp>
#include <com/sun/star/lang/XMultiServiceFactory.hpp>
#include <com/sun/star/beans/XPropertySet.hpp>
#include <com/sun/star/frame/XFramesSupplier.hpp>
#include <com/sun/star/frame/XDesktop.hpp>
#include <com/sun/star/container/XEnumeration.hpp>
#include <com/sun/star/util/XStringWidth.hpp>
//_________________________________________________________________________________________________________________
// includes of other projects
//_________________________________________________________________________________________________________________
#include <comphelper/processfactory.hxx>
#include <comphelper/extract.hxx>
#include <svtools/menuoptions.hxx>
#include <unotools/historyoptions.hxx>
#include <unotools/pathoptions.hxx>
#include <unotools/localfilehelper.hxx>
#include <toolkit/unohlp.hxx>
#include <tools/urlobj.hxx>
#include <vcl/svapp.hxx>
#include <vcl/window.hxx>
#include <osl/mutex.hxx>
#include <vcl/svapp.hxx>
#include <osl/file.hxx>
#include <cppuhelper/implbase1.hxx>
//_________________________________________________________________________________________________________________
// namespace
//_________________________________________________________________________________________________________________
using namespace ::cppu;
using namespace ::com::sun::star::uno;
using namespace ::com::sun::star::util;
using namespace ::com::sun::star::beans;
using namespace ::com::sun::star::frame;
using namespace ::com::sun::star::lang;
using namespace ::com::sun::star::container;
class StringLength : public ::cppu::WeakImplHelper1< ::com::sun::star::util::XStringWidth >
{
public:
StringLength() {}
virtual ~StringLength() {}
// XStringWidth
sal_Int32 SAL_CALL queryStringWidth( const ::rtl::OUString& aString )
throw (::com::sun::star::uno::RuntimeException)
{
return aString.getLength();
}
};
namespace framework
{
// special menu ids/command ids for dynamic popup menus
#define SID_SFX_START 5000
#define SID_NEWDOCDIRECT (SID_SFX_START + 537)
#define SID_AUTOPILOTMENU (SID_SFX_START + 1381)
#define SID_PICKLIST (SID_SFX_START + 510)
#define SID_MDIWINDOWLIST (SID_SFX_START + 610)
#define SID_ADDONLIST (SID_SFX_START + 1677)
#define SID_HELPMENU (SID_SFX_START + 410)
#define SFX_REFERER_USER "private:user"
#define aSlotNewDocDirect "slot:5537"
#define aSlotAutoPilot "slot:6381"
#define aSpecialFileMenu "file"
#define aSpecialWindowMenu "window"
#define aSlotSpecialFileMenu "slot:5510"
#define aSlotSpecialWindowMenu "slot:5610"
#define aSlotSpecialToolsMenu "slot:6677"
// special uno commands for picklist and window list
#define aSpecialFileCommand "PickList"
#define aSpecialWindowCommand "WindowList"
const char UNO_COMMAND[] = ".uno:";
MenuManager::MenuManager(
const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xServiceFactory,
REFERENCE< XFRAME >& rFrame, Menu* pMenu, sal_Bool bDelete, sal_Bool bDeleteChildren )
:
ThreadHelpBase( &Application::GetSolarMutex() ),
mxServiceFactory(xServiceFactory)
{
m_bActive = sal_False;
m_bDeleteMenu = bDelete;
m_bDeleteChildren = bDeleteChildren;
m_pVCLMenu = pMenu;
m_xFrame = rFrame;
m_bInitialized = sal_False;
m_bIsBookmarkMenu = sal_False;
acquire();
const StyleSettings& rSettings = Application::GetSettings().GetStyleSettings();
m_bShowMenuImages = rSettings.GetUseImagesInMenus();
sal_Int32 nAddonsURLPrefixLength = ADDONSPOPUPMENU_URL_PREFIX.getLength();
sal_uInt16 nItemCount = pMenu->GetItemCount();
m_aMenuItemHandlerVector.reserve(nItemCount);
::rtl::OUString aItemCommand;
for ( sal_uInt16 i = 0; i < nItemCount; i++ )
{
sal_uInt16 nItemId = FillItemCommand(aItemCommand,pMenu, i );
bool bShowMenuImages( m_bShowMenuImages );
// overwrite the show icons on menu option?
if (!bShowMenuImages)
{
MenuItemBits nBits = pMenu->GetItemBits( nItemId );
bShowMenuImages = ( ( nBits & MIB_ICON ) == MIB_ICON );
}
PopupMenu* pPopupMenu = pMenu->GetPopupMenu( nItemId );
if ( pPopupMenu )
{
AddMenu(pPopupMenu,aItemCommand,nItemId,bDeleteChildren,bDeleteChildren);
if (! (( aItemCommand.getLength() > nAddonsURLPrefixLength ) &&
( aItemCommand.indexOf( ADDONSPOPUPMENU_URL_PREFIX ) == 0 )) )
{
// Create addon popup menu if there exist elements and this is the tools popup menu
if ( ( nItemId == SID_ADDONLIST || aItemCommand == aSlotSpecialToolsMenu )
&& AddonMenuManager::HasAddonMenuElements() )
{
AddonMenu* pSubMenu = AddonMenuManager::CreateAddonMenu( rFrame );
if ( pSubMenu && ( pSubMenu->GetItemCount() > 0 ))
{
sal_uInt16 nCount = 0;
if ( pPopupMenu->GetItemType( nCount-1 ) != MENUITEM_SEPARATOR )
pPopupMenu->InsertSeparator();
// Use resource to load popup menu title
String aAddonsStrRes = String( FwkResId( STR_MENU_ADDONS ));
pPopupMenu->InsertItem( ITEMID_ADDONLIST, aAddonsStrRes );
pPopupMenu->SetPopupMenu( ITEMID_ADDONLIST, pSubMenu );
// Set item command for popup menu to enable it for GetImageFromURL
const static ::rtl::OUString aSlotString( RTL_CONSTASCII_USTRINGPARAM( "slot:" ));
aItemCommand = aSlotString;
aItemCommand += ::rtl::OUString::valueOf( (sal_Int32)ITEMID_ADDONLIST );
pPopupMenu->SetItemCommand( ITEMID_ADDONLIST, aItemCommand );
AddMenu(pSubMenu,::rtl::OUString(),nItemId,sal_True,sal_False);
// Set image for the addon popup menu item
if ( bShowMenuImages && !pPopupMenu->GetItemImage( ITEMID_ADDONLIST ))
{
Image aImage = GetImageFromURL( rFrame, aItemCommand, false );
if ( !!aImage )
pPopupMenu->SetItemImage( ITEMID_ADDONLIST, aImage );
}
}
else
delete pSubMenu;
}
}
}
else
{
if ( nItemId == SID_NEWDOCDIRECT || aItemCommand == aSlotNewDocDirect )
{
MenuConfiguration aMenuCfg( getServiceFactory() );
BmkMenu* pSubMenu = (BmkMenu*)aMenuCfg.CreateBookmarkMenu( rFrame, BOOKMARK_NEWMENU );
pMenu->SetPopupMenu( nItemId, pSubMenu );
AddMenu(pSubMenu,::rtl::OUString(),nItemId,sal_True,sal_False);
if ( bShowMenuImages && !pMenu->GetItemImage( nItemId ))
{
Image aImage = GetImageFromURL( rFrame, aItemCommand, false );
if ( !!aImage )
pMenu->SetItemImage( nItemId, aImage );
}
}
else if ( nItemId == SID_AUTOPILOTMENU || aItemCommand == aSlotAutoPilot )
{
MenuConfiguration aMenuCfg( getServiceFactory() );
BmkMenu* pSubMenu = (BmkMenu*)aMenuCfg.CreateBookmarkMenu( rFrame, BOOKMARK_WIZARDMENU );
pMenu->SetPopupMenu( nItemId, pSubMenu );
AddMenu(pSubMenu,::rtl::OUString(),nItemId,sal_True,sal_False);
if ( bShowMenuImages && !pMenu->GetItemImage( nItemId ))
{
Image aImage = GetImageFromURL( rFrame, aItemCommand, false );
if ( !!aImage )
pMenu->SetItemImage( nItemId, aImage );
}
}
else if ( pMenu->GetItemType( i ) != MENUITEM_SEPARATOR )
{
if ( bShowMenuImages )
{
if ( AddonMenuManager::IsAddonMenuId( nItemId ))
{
// Add-Ons uses a images from different places
Image aImage;
rtl::OUString aImageId;
MenuConfiguration::Attributes* pMenuAttributes =
(MenuConfiguration::Attributes*)pMenu->GetUserValue( nItemId );
if ( pMenuAttributes && !pMenuAttributes->aImageId.isEmpty() )
{
// Retrieve image id from menu attributes
aImage = GetImageFromURL( rFrame, aImageId, false );
}
if ( !aImage )
{
aImage = GetImageFromURL( rFrame, aItemCommand, false );
if ( !aImage )
aImage = AddonsOptions().GetImageFromURL( aItemCommand, false );
}
if ( !!aImage )
pMenu->SetItemImage( nItemId, aImage );
}
else if ( !pMenu->GetItemImage( nItemId ))
{
Image aImage = GetImageFromURL( rFrame, aItemCommand, false );
if ( !!aImage )
pMenu->SetItemImage( nItemId, aImage );
}
}
REFERENCE< XDISPATCH > aXDispatchRef;
m_aMenuItemHandlerVector.push_back( new MenuItemHandler( nItemId, NULL, aXDispatchRef ));
}
}
}
// retrieve label information for all menu items without item text
SetHdl();
}
void MenuManager::SetHdl()
{
m_pVCLMenu->SetHighlightHdl( LINK( this, MenuManager, Highlight ));
m_pVCLMenu->SetActivateHdl( LINK( this, MenuManager, Activate ));
m_pVCLMenu->SetDeactivateHdl( LINK( this, MenuManager, Deactivate ));
m_pVCLMenu->SetSelectHdl( LINK( this, MenuManager, Select ));
if ( mxServiceFactory.is() )
m_xURLTransformer.set( mxServiceFactory->createInstance(SERVICENAME_URLTRANSFORMER),UNO_QUERY );
}
MenuManager::~MenuManager()
{
std::vector< MenuItemHandler* >::iterator p;
for ( p = m_aMenuItemHandlerVector.begin(); p != m_aMenuItemHandlerVector.end(); ++p )
{
MenuItemHandler* pItemHandler = *p;
pItemHandler->xMenuItemDispatch.clear();
if ( pItemHandler->pSubMenuManager )
SAL_STATIC_CAST( ::com::sun::star::uno::XInterface*, (OWeakObject*)pItemHandler->pSubMenuManager )->release();
delete pItemHandler;
}
if ( m_bDeleteMenu )
delete m_pVCLMenu;
}
MenuManager::MenuItemHandler* MenuManager::GetMenuItemHandler( sal_uInt16 nItemId )
{
ResetableGuard aGuard( m_aLock );
std::vector< MenuItemHandler* >::iterator p;
for ( p = m_aMenuItemHandlerVector.begin(); p != m_aMenuItemHandlerVector.end(); ++p )
{
MenuItemHandler* pItemHandler = *p;
if ( pItemHandler->nItemId == nItemId )
return pItemHandler;
}
return 0;
}
void SAL_CALL MenuManager::statusChanged( const FEATURSTATEEVENT& Event )
throw ( RuntimeException )
{
::rtl::OUString aFeatureURL = Event.FeatureURL.Complete;
MenuItemHandler* pStatusChangedMenu = NULL;
{
ResetableGuard aGuard( m_aLock );
std::vector< MenuItemHandler* >::iterator p;
for ( p = m_aMenuItemHandlerVector.begin(); p != m_aMenuItemHandlerVector.end(); ++p )
{
MenuItemHandler* pMenuItemHandler = *p;
if ( pMenuItemHandler->aMenuItemURL == aFeatureURL )
{
pStatusChangedMenu = pMenuItemHandler;
break;
}
}
}
if ( pStatusChangedMenu )
{
SolarMutexGuard aSolarGuard;
{
ResetableGuard aGuard( m_aLock );
sal_Bool bSetCheckmark = sal_False;
sal_Bool bCheckmark = sal_False;
sal_Bool bMenuItemEnabled = m_pVCLMenu->IsItemEnabled( pStatusChangedMenu->nItemId );
if ( Event.IsEnabled != bMenuItemEnabled )
m_pVCLMenu->EnableItem( pStatusChangedMenu->nItemId, Event.IsEnabled );
if ( Event.State >>= bCheckmark )
bSetCheckmark = sal_True;
if ( bSetCheckmark )
m_pVCLMenu->CheckItem( pStatusChangedMenu->nItemId, bCheckmark );
}
if ( Event.Requery )
{
URL aTargetURL;
aTargetURL.Complete = pStatusChangedMenu->aMenuItemURL;
m_xURLTransformer->parseStrict( aTargetURL );
REFERENCE< XDISPATCHPROVIDER > xDispatchProvider( m_xFrame, UNO_QUERY );
REFERENCE< XDISPATCH > xMenuItemDispatch = xDispatchProvider->queryDispatch(
aTargetURL, ::rtl::OUString(), 0 );
if ( xMenuItemDispatch.is() )
{
pStatusChangedMenu->xMenuItemDispatch = xMenuItemDispatch;
pStatusChangedMenu->aMenuItemURL = aTargetURL.Complete;
xMenuItemDispatch->addStatusListener( SAL_STATIC_CAST( XSTATUSLISTENER*, this ), aTargetURL );
}
}
}
}
void MenuManager::RemoveListener()
{
ResetableGuard aGuard( m_aLock );
ClearMenuDispatch();
}
void MenuManager::ClearMenuDispatch(const EVENTOBJECT& Source,bool _bRemoveOnly)
{
// disposing called from parent dispatcher
// remove all listener to prepare shutdown
std::vector< MenuItemHandler* >::iterator p;
for ( p = m_aMenuItemHandlerVector.begin(); p != m_aMenuItemHandlerVector.end(); ++p )
{
MenuItemHandler* pItemHandler = *p;
if ( pItemHandler->xMenuItemDispatch.is() )
{
URL aTargetURL;
aTargetURL.Complete = pItemHandler->aMenuItemURL;
m_xURLTransformer->parseStrict( aTargetURL );
pItemHandler->xMenuItemDispatch->removeStatusListener(
SAL_STATIC_CAST( XSTATUSLISTENER*, this ), aTargetURL );
}
pItemHandler->xMenuItemDispatch.clear();
if ( pItemHandler->pSubMenuManager )
{
if ( _bRemoveOnly )
pItemHandler->pSubMenuManager->RemoveListener();
else
pItemHandler->pSubMenuManager->disposing( Source );
}
}
}
void SAL_CALL MenuManager::disposing( const EVENTOBJECT& Source ) throw ( RUNTIMEEXCEPTION )
{
if ( Source.Source == m_xFrame )
{
ResetableGuard aGuard( m_aLock );
ClearMenuDispatch(Source,false);
}
else
{
// disposing called from menu item dispatcher, remove listener
MenuItemHandler* pMenuItemDisposing = NULL;
{
ResetableGuard aGuard( m_aLock );
std::vector< MenuItemHandler* >::iterator p;
for ( p = m_aMenuItemHandlerVector.begin(); p != m_aMenuItemHandlerVector.end(); ++p )
{
MenuItemHandler* pMenuItemHandler = *p;
if ( pMenuItemHandler->xMenuItemDispatch == Source.Source )
{
pMenuItemDisposing = pMenuItemHandler;
break;
}
}
if ( pMenuItemDisposing )
{
URL aTargetURL;
aTargetURL.Complete = pMenuItemDisposing->aMenuItemURL;
m_xURLTransformer->parseStrict( aTargetURL );
pMenuItemDisposing->xMenuItemDispatch->removeStatusListener(SAL_STATIC_CAST( XSTATUSLISTENER*, this ), aTargetURL );
pMenuItemDisposing->xMenuItemDispatch.clear();
}
}
}
}
void MenuManager::UpdateSpecialFileMenu( Menu* pMenu )
{
// update picklist
Sequence< Sequence< PropertyValue > > aHistoryList = SvtHistoryOptions().GetList( ePICKLIST );
::std::vector< MenuItemHandler* > aNewPickVector;
Reference< XStringWidth > xStringLength( new StringLength );
sal_uInt16 nPickItemId = START_ITEMID_PICKLIST;
int nPickListMenuItems = ( aHistoryList.getLength() > 99 ) ? 99 : aHistoryList.getLength();
aNewPickVector.reserve(nPickListMenuItems);
for ( int i = 0; i < nPickListMenuItems; i++ )
{
Sequence< PropertyValue > aPickListEntry = aHistoryList[i];
REFERENCE< XDISPATCH > aXDispatchRef;
MenuItemHandler* pNewMenuItemHandler = new MenuItemHandler(
nPickItemId++,
NULL,
aXDispatchRef );
for ( int j = 0; j < aPickListEntry.getLength(); j++ )
{
Any a = aPickListEntry[j].Value;
if ( aPickListEntry[j].Name == HISTORY_PROPERTYNAME_URL )
a >>= pNewMenuItemHandler->aMenuItemURL;
else if ( aPickListEntry[j].Name == HISTORY_PROPERTYNAME_FILTER )
a >>= pNewMenuItemHandler->aFilter;
else if ( aPickListEntry[j].Name == HISTORY_PROPERTYNAME_TITLE )
a >>= pNewMenuItemHandler->aTitle;
else if ( aPickListEntry[j].Name == HISTORY_PROPERTYNAME_PASSWORD )
a >>= pNewMenuItemHandler->aPassword;
}
aNewPickVector.push_back( pNewMenuItemHandler );
}
if ( !aNewPickVector.empty() )
{
URL aTargetURL;
REFERENCE< XDISPATCHPROVIDER > xDispatchProvider( m_xFrame, UNO_QUERY );
REFERENCE< XDISPATCH > xMenuItemDispatch;
static const ::rtl::OUString s_sDefault(RTL_CONSTASCII_USTRINGPARAM("_default"));
// query for dispatcher
std::vector< MenuItemHandler* >::iterator p;
for ( p = aNewPickVector.begin(); p != aNewPickVector.end(); ++p )
{
MenuItemHandler* pMenuItemHandler = *p;
aTargetURL.Complete = pMenuItemHandler->aMenuItemURL;
m_xURLTransformer->parseStrict( aTargetURL );
if ( !xMenuItemDispatch.is() )
{
// attention: this code assume that "_blank" can only be consumed by desktop service
xMenuItemDispatch = xDispatchProvider->queryDispatch( aTargetURL, s_sDefault, 0 );
}
if ( xMenuItemDispatch.is() )
{
pMenuItemHandler->xMenuItemDispatch = xMenuItemDispatch;
pMenuItemHandler->aMenuItemURL = aTargetURL.Complete;
}
}
{
ResetableGuard aGuard( m_aLock );
int nRemoveItemCount = 0;
int nItemCount = pMenu->GetItemCount();
if ( nItemCount > 0 )
{
// remove all old picklist entries from menu
sal_uInt16 nPos = pMenu->GetItemPos( START_ITEMID_PICKLIST );
for ( sal_uInt16 n = nPos; n < pMenu->GetItemCount(); )
{
pMenu->RemoveItem( n );
++nRemoveItemCount;
}
if ( pMenu->GetItemType( pMenu->GetItemCount()-1 ) == MENUITEM_SEPARATOR )
pMenu->RemoveItem( pMenu->GetItemCount()-1 );
// remove all old picklist entries from menu handler
if ( nRemoveItemCount > 0 )
{
for( sal_uInt32 nIndex = m_aMenuItemHandlerVector.size() - nRemoveItemCount;
nIndex < m_aMenuItemHandlerVector.size(); )
{
delete m_aMenuItemHandlerVector.at( nIndex );
m_aMenuItemHandlerVector.erase( m_aMenuItemHandlerVector.begin() + nIndex );
}
}
}
// append new picklist menu entries
aNewPickVector.reserve(aNewPickVector.size());
pMenu->InsertSeparator();
const sal_uInt32 nCount = aNewPickVector.size();
for ( sal_uInt32 i = 0; i < nCount; i++ )
{
char menuShortCut[5] = "~n: ";
::rtl::OUString aMenuShortCut;
if ( i <= 9 )
{
if ( i == 9 )
aMenuShortCut = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "1~0: " ));
else
{
menuShortCut[1] = (char)( '1' + i );
aMenuShortCut = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( menuShortCut ));
}
}
else
{
aMenuShortCut = rtl::OUString::valueOf((sal_Int32)( i + 1 ));
aMenuShortCut += rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ": " ));
}
// Abbreviate URL
rtl::OUString aURLString( aNewPickVector.at( i )->aMenuItemURL );
rtl::OUString aTipHelpText;
rtl::OUString aMenuTitle;
INetURLObject aURL( aURLString );
if ( aURL.GetProtocol() == INET_PROT_FILE )
{
// Do handle file URL differently => convert it to a system
// path and abbreviate it with a special function:
String aFileSystemPath( aURL.getFSysPath( INetURLObject::FSYS_DETECT ) );
::rtl::OUString aSystemPath( aFileSystemPath );
::rtl::OUString aCompactedSystemPath;
aTipHelpText = aSystemPath;
oslFileError nError = osl_abbreviateSystemPath( aSystemPath.pData, &aCompactedSystemPath.pData, 46, NULL );
if ( !nError )
aMenuTitle = String( aCompactedSystemPath );
else
aMenuTitle = aSystemPath;
}
else
{
// Use INetURLObject to abbreviate all other URLs
String aShortURL;
aShortURL = aURL.getAbbreviated( xStringLength, 46, INetURLObject::DECODE_UNAMBIGUOUS );
aMenuTitle += aShortURL;
aTipHelpText = aURLString;
}
::rtl::OUString aTitle( aMenuShortCut + aMenuTitle );
MenuItemHandler* pMenuItemHandler = aNewPickVector.at( i );
pMenu->InsertItem( pMenuItemHandler->nItemId, aTitle );
pMenu->SetTipHelpText( pMenuItemHandler->nItemId, aTipHelpText );
m_aMenuItemHandlerVector.push_back( pMenuItemHandler );
}
}
}
}
void MenuManager::UpdateSpecialWindowMenu( Menu* pMenu,const Reference< XMultiServiceFactory >& xServiceFactory,framework::IMutex& _rMutex )
{
// update window list
::std::vector< ::rtl::OUString > aNewWindowListVector;
Reference< XDesktop > xDesktop( xServiceFactory->createInstance( SERVICENAME_DESKTOP ), UNO_QUERY );
sal_uInt16 nActiveItemId = 0;
sal_uInt16 nItemId = START_ITEMID_WINDOWLIST;
if ( xDesktop.is() )
{
Reference< XFramesSupplier > xTasksSupplier( xDesktop, UNO_QUERY );
Reference< XFrame > xCurrentFrame = xDesktop->getCurrentFrame();
Reference< XIndexAccess > xList( xTasksSupplier->getFrames(), UNO_QUERY );
sal_Int32 nCount = xList->getCount();
aNewWindowListVector.reserve(nCount);
for (sal_Int32 i=0; i<nCount; ++i )
{
Reference< XFrame > xFrame;
xList->getByIndex(i) >>= xFrame;
if (xFrame.is())
{
if ( xFrame == xCurrentFrame )
nActiveItemId = nItemId;
Window* pWin = VCLUnoHelper::GetWindow( xFrame->getContainerWindow() );
if ( pWin && pWin->IsVisible() )
{
aNewWindowListVector.push_back( pWin->GetText() );
++nItemId;
}
}
}
}
{
ResetableGuard aGuard( _rMutex );
int nItemCount = pMenu->GetItemCount();
if ( nItemCount > 0 )
{
// remove all old window list entries from menu
sal_uInt16 nPos = pMenu->GetItemPos( START_ITEMID_WINDOWLIST );
for ( sal_uInt16 n = nPos; n < pMenu->GetItemCount(); )
pMenu->RemoveItem( n );
if ( pMenu->GetItemType( pMenu->GetItemCount()-1 ) == MENUITEM_SEPARATOR )
pMenu->RemoveItem( pMenu->GetItemCount()-1 );
}
if ( !aNewWindowListVector.empty() )
{
// append new window list entries to menu
pMenu->InsertSeparator();
nItemId = START_ITEMID_WINDOWLIST;
const sal_uInt32 nCount = aNewWindowListVector.size();
for ( sal_uInt32 i = 0; i < nCount; i++ )
{
pMenu->InsertItem( nItemId, aNewWindowListVector.at( i ), MIB_RADIOCHECK );
if ( nItemId == nActiveItemId )
pMenu->CheckItem( nItemId );
++nItemId;
}
}
}
}
void MenuManager::CreatePicklistArguments( Sequence< PropertyValue >& aArgsList, const MenuItemHandler* pMenuItemHandler )
{
int NUM_OF_PICKLIST_ARGS = 3;
Any a;
aArgsList.realloc( NUM_OF_PICKLIST_ARGS );
aArgsList[0].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "FileName" ));
a <<= pMenuItemHandler->aMenuItemURL;
aArgsList[0].Value = a;
aArgsList[1].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Referer" ));
a <<= ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( SFX_REFERER_USER ));
aArgsList[1].Value = a;
::rtl::OUString aFilter( pMenuItemHandler->aFilter );
sal_Int32 nPos = aFilter.indexOf( '|' );
if ( nPos >= 0 )
{
::rtl::OUString aFilterOptions;
if ( nPos < ( aFilter.getLength() - 1 ) )
aFilterOptions = aFilter.copy( nPos+1 );
aArgsList[2].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "FilterOptions" ));
a <<= aFilterOptions;
aArgsList[2].Value = a;
aFilter = aFilter.copy( 0, nPos-1 );
aArgsList.realloc( ++NUM_OF_PICKLIST_ARGS );
}
aArgsList[NUM_OF_PICKLIST_ARGS-1].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "FilterName" ));
a <<= aFilter;
aArgsList[NUM_OF_PICKLIST_ARGS-1].Value = a;
}
//_________________________________________________________________________________________________________________
// vcl handler
//_________________________________________________________________________________________________________________
IMPL_LINK( MenuManager, Activate, Menu *, pMenu )
{
if ( pMenu == m_pVCLMenu )
{
// set/unset hiding disabled menu entries
sal_Bool bDontHide = SvtMenuOptions().IsEntryHidingEnabled();
const StyleSettings& rSettings = Application::GetSettings().GetStyleSettings();
sal_Bool bShowMenuImages = rSettings.GetUseImagesInMenus();
sal_uInt16 nFlag = pMenu->GetMenuFlags();
if ( bDontHide )
nFlag &= ~MENU_FLAG_HIDEDISABLEDENTRIES;
else
nFlag |= MENU_FLAG_HIDEDISABLEDENTRIES;
pMenu->SetMenuFlags( nFlag );
if ( m_bActive )
return 0;
m_bActive = sal_True;
::rtl::OUString aCommand( m_aMenuItemCommand );
if (m_aMenuItemCommand.matchIgnoreAsciiCaseAsciiL(RTL_CONSTASCII_STRINGPARAM(UNO_COMMAND)))
{
// Remove protocol part from command so we can use an easier comparision method
aCommand = aCommand.copy(RTL_CONSTASCII_LENGTH(UNO_COMMAND));
}
if ( m_aMenuItemCommand == aSpecialFileMenu || m_aMenuItemCommand == aSlotSpecialFileMenu || aCommand == aSpecialFileCommand )
UpdateSpecialFileMenu( pMenu );
else if ( m_aMenuItemCommand == aSpecialWindowMenu || m_aMenuItemCommand == aSlotSpecialWindowMenu || aCommand == aSpecialWindowCommand )
UpdateSpecialWindowMenu( pMenu, getServiceFactory(), m_aLock );
// Check if some modes have changed so we have to update our menu images
if ( bShowMenuImages != m_bShowMenuImages )
{
// The mode changed so we have to replace all images
m_bShowMenuImages = bShowMenuImages;
FillMenuImages( m_xFrame, pMenu, bShowMenuImages );
}
if ( m_bInitialized )
return 0;
else
{
URL aTargetURL;
ResetableGuard aGuard( m_aLock );
REFERENCE< XDISPATCHPROVIDER > xDispatchProvider( m_xFrame, UNO_QUERY );
if ( xDispatchProvider.is() )
{
std::vector< MenuItemHandler* >::iterator p;
for ( p = m_aMenuItemHandlerVector.begin(); p != m_aMenuItemHandlerVector.end(); ++p )
{
MenuItemHandler* pMenuItemHandler = *p;
if ( pMenuItemHandler &&
pMenuItemHandler->pSubMenuManager == 0 &&
!pMenuItemHandler->xMenuItemDispatch.is() )
{
// There is no dispatch mechanism for the special window list menu items,
// because they are handled directly through XFrame->activate!!!
if ( pMenuItemHandler->nItemId < START_ITEMID_WINDOWLIST ||
pMenuItemHandler->nItemId > END_ITEMID_WINDOWLIST )
{
::rtl::OUString aItemCommand = pMenu->GetItemCommand( pMenuItemHandler->nItemId );
if ( aItemCommand.isEmpty() )
{
const static ::rtl::OUString aSlotString( RTL_CONSTASCII_USTRINGPARAM( "slot:" ));
aItemCommand = aSlotString;
aItemCommand += ::rtl::OUString::valueOf( (sal_Int32)pMenuItemHandler->nItemId );
pMenu->SetItemCommand( pMenuItemHandler->nItemId, aItemCommand );
}
aTargetURL.Complete = aItemCommand;
m_xURLTransformer->parseStrict( aTargetURL );
REFERENCE< XDISPATCH > xMenuItemDispatch;
if ( m_bIsBookmarkMenu )
xMenuItemDispatch = xDispatchProvider->queryDispatch( aTargetURL, pMenuItemHandler->aTargetFrame, 0 );
else
xMenuItemDispatch = xDispatchProvider->queryDispatch( aTargetURL, ::rtl::OUString(), 0 );
if ( xMenuItemDispatch.is() )
{
pMenuItemHandler->xMenuItemDispatch = xMenuItemDispatch;
pMenuItemHandler->aMenuItemURL = aTargetURL.Complete;
xMenuItemDispatch->addStatusListener( SAL_STATIC_CAST( XSTATUSLISTENER*, this ), aTargetURL );
}
else
pMenu->EnableItem( pMenuItemHandler->nItemId, sal_False );
}
}
}
}
}
}
return 1;
}
IMPL_LINK( MenuManager, Deactivate, Menu *, pMenu )
{
if ( pMenu == m_pVCLMenu )
m_bActive = sal_False;
return 1;
}
IMPL_LINK( MenuManager, Select, Menu *, pMenu )
{
URL aTargetURL;
Sequence<PropertyValue> aArgs;
REFERENCE< XDISPATCH > xDispatch;
{
ResetableGuard aGuard( m_aLock );
sal_uInt16 nCurItemId = pMenu->GetCurItemId();
if ( pMenu == m_pVCLMenu &&
pMenu->GetItemType( nCurItemId ) != MENUITEM_SEPARATOR )
{
if ( nCurItemId >= START_ITEMID_WINDOWLIST &&
nCurItemId <= END_ITEMID_WINDOWLIST )
{
// window list menu item selected
Reference< XFramesSupplier > xDesktop( getServiceFactory()->createInstance( SERVICENAME_DESKTOP ), UNO_QUERY );
if ( xDesktop.is() )
{
sal_uInt16 nTaskId = START_ITEMID_WINDOWLIST;
Reference< XIndexAccess > xList( xDesktop->getFrames(), UNO_QUERY );
sal_Int32 nCount = xList->getCount();
for ( sal_Int32 i=0; i<nCount; ++i )
{
Reference< XFrame > xFrame;
xList->getByIndex(i) >>= xFrame;
if ( xFrame.is() && nTaskId == nCurItemId )
{
Window* pWin = VCLUnoHelper::GetWindow( xFrame->getContainerWindow() );
pWin->GrabFocus();
pWin->ToTop( TOTOP_RESTOREWHENMIN );
break;
}
nTaskId++;
}
}
}
else
{
MenuItemHandler* pMenuItemHandler = GetMenuItemHandler( nCurItemId );
if ( pMenuItemHandler && pMenuItemHandler->xMenuItemDispatch.is() )
{
aTargetURL.Complete = pMenuItemHandler->aMenuItemURL;
m_xURLTransformer->parseStrict( aTargetURL );
if ( nCurItemId >= START_ITEMID_PICKLIST &&
nCurItemId < START_ITEMID_WINDOWLIST )
{
// picklist menu item selected
CreatePicklistArguments( aArgs, pMenuItemHandler );
}
else if ( m_bIsBookmarkMenu )
{
// bookmark menu item selected
aArgs.realloc( 1 );
aArgs[0].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Referer" ));
aArgs[0].Value <<= ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( SFX_REFERER_USER ));
}
xDispatch = pMenuItemHandler->xMenuItemDispatch;
}
}
}
}
if ( xDispatch.is() )
xDispatch->dispatch( aTargetURL, aArgs );
return 1;
}
IMPL_LINK_NOARG(MenuManager, Highlight)
{
return 0;
}
const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& MenuManager::getServiceFactory()
{
return mxServiceFactory;
}
void MenuManager::AddMenu(PopupMenu* _pPopupMenu,const ::rtl::OUString& _sItemCommand,sal_uInt16 _nItemId,sal_Bool _bDelete,sal_Bool _bDeleteChildren)
{
MenuManager* pSubMenuManager = new MenuManager( getServiceFactory(), m_xFrame, _pPopupMenu, _bDelete, _bDeleteChildren );
// store menu item command as we later have to know which menu is active (see Activate handler)
pSubMenuManager->m_aMenuItemCommand = _sItemCommand;
REFERENCE< XDISPATCH > aXDispatchRef;
MenuItemHandler* pMenuItemHandler = new MenuItemHandler(
_nItemId,
pSubMenuManager,
aXDispatchRef );
m_aMenuItemHandlerVector.push_back( pMenuItemHandler );
}
sal_uInt16 MenuManager::FillItemCommand(::rtl::OUString& _rItemCommand,Menu* _pMenu,sal_uInt16 _nIndex) const
{
sal_uInt16 nItemId = _pMenu->GetItemId( _nIndex );
_rItemCommand = _pMenu->GetItemCommand( nItemId );
if ( _rItemCommand.isEmpty() )
{
const static ::rtl::OUString aSlotString( RTL_CONSTASCII_USTRINGPARAM( "slot:" ));
_rItemCommand = aSlotString;
_rItemCommand += ::rtl::OUString::valueOf( (sal_Int32)nItemId );
_pMenu->SetItemCommand( nItemId, _rItemCommand );
}
return nItemId;
}
void MenuManager::FillMenuImages(Reference< XFrame >& _xFrame,Menu* _pMenu,sal_Bool bShowMenuImages)
{
AddonsOptions aAddonOptions;
for ( sal_uInt16 nPos = 0; nPos < _pMenu->GetItemCount(); nPos++ )
{
sal_uInt16 nId = _pMenu->GetItemId( nPos );
if ( _pMenu->GetItemType( nPos ) != MENUITEM_SEPARATOR )
{
bool bTmpShowMenuImages( bShowMenuImages );
// overwrite the show icons on menu option?
if (!bTmpShowMenuImages)
{
MenuItemBits nBits = _pMenu->GetItemBits( nId );
bTmpShowMenuImages = ( ( nBits & MIB_ICON ) == MIB_ICON );
}
if ( bTmpShowMenuImages )
{
sal_Bool bImageSet = sal_False;
::rtl::OUString aImageId;
::framework::MenuConfiguration::Attributes* pMenuAttributes =
(::framework::MenuConfiguration::Attributes*)_pMenu->GetUserValue( nId );
if ( pMenuAttributes )
aImageId = pMenuAttributes->aImageId; // Retrieve image id from menu attributes
if ( !aImageId.isEmpty() )
{
Image aImage = GetImageFromURL( _xFrame, aImageId, false );
if ( !!aImage )
{
bImageSet = sal_True;
_pMenu->SetItemImage( nId, aImage );
}
}
if ( !bImageSet )
{
rtl::OUString aMenuItemCommand = _pMenu->GetItemCommand( nId );
Image aImage = GetImageFromURL( _xFrame, aMenuItemCommand, false );
if ( !aImage )
aImage = aAddonOptions.GetImageFromURL( aMenuItemCommand, false );
_pMenu->SetItemImage( nId, aImage );
}
}
else
_pMenu->SetItemImage( nId, Image() );
}
}
}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|