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
|
/* -*- 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 <bitmaps.hlst>
#include <cppuhelper/implbase.hxx>
#include <cppuhelper/supportsservice.hxx>
#include <comphelper/propertyvalue.hxx>
#include <menuconfiguration.hxx>
#include <svtools/imagemgr.hxx>
#include <svtools/toolboxcontroller.hxx>
#include <toolkit/awt/vclxmenu.hxx>
#include <toolkit/helper/vclunohelper.hxx>
#include <comphelper/diagnose_ex.hxx>
#include <tools/urlobj.hxx>
#include <utility>
#include <vcl/commandinfoprovider.hxx>
#include <vcl/menu.hxx>
#include <vcl/svapp.hxx>
#include <vcl/toolbox.hxx>
#include <com/sun/star/awt/PopupMenuDirection.hpp>
#include <com/sun/star/awt/XPopupMenu.hpp>
#include <com/sun/star/frame/thePopupMenuControllerFactory.hpp>
#include <com/sun/star/frame/XPopupMenuController.hpp>
#include <com/sun/star/frame/XStorable.hpp>
#include <com/sun/star/frame/XSubToolbarController.hpp>
#include <com/sun/star/frame/XUIControllerFactory.hpp>
#include <com/sun/star/frame/XController.hpp>
#include <com/sun/star/lang/XServiceInfo.hpp>
#include <com/sun/star/ucb/CommandFailedException.hpp>
#include <com/sun/star/ucb/ContentCreationException.hpp>
#include <com/sun/star/util/XModifiable.hpp>
using namespace framework;
namespace
{
typedef cppu::ImplInheritanceHelper< svt::ToolboxController,
css::lang::XServiceInfo >
ToolBarBase;
class PopupMenuToolbarController : public ToolBarBase
{
public:
// XComponent
virtual void SAL_CALL dispose() override;
// XInitialization
virtual void SAL_CALL initialize( const css::uno::Sequence< css::uno::Any >& aArguments ) override;
// XToolbarController
virtual css::uno::Reference< css::awt::XWindow > SAL_CALL createPopupWindow() override;
// XStatusListener
virtual void SAL_CALL statusChanged( const css::frame::FeatureStateEvent& rEvent ) override;
protected:
PopupMenuToolbarController( const css::uno::Reference< css::uno::XComponentContext >& rxContext,
OUString aPopupCommand = OUString() );
virtual void functionExecuted( const OUString &rCommand );
virtual ToolBoxItemBits getDropDownStyle() const;
void createPopupMenuController();
bool m_bHasController;
bool m_bResourceURL;
OUString m_aPopupCommand;
rtl::Reference< VCLXPopupMenu > m_xPopupMenu;
private:
css::uno::Reference< css::frame::XUIControllerFactory > m_xPopupMenuFactory;
css::uno::Reference< css::frame::XPopupMenuController > m_xPopupMenuController;
};
PopupMenuToolbarController::PopupMenuToolbarController(
const css::uno::Reference< css::uno::XComponentContext >& xContext,
OUString aPopupCommand )
: ToolBarBase( xContext, css::uno::Reference< css::frame::XFrame >(), /*aCommandURL*/OUString() )
, m_bHasController( false )
, m_bResourceURL( false )
, m_aPopupCommand(std::move( aPopupCommand ))
{
}
void SAL_CALL PopupMenuToolbarController::dispose()
{
svt::ToolboxController::dispose();
osl::MutexGuard aGuard( m_aMutex );
if( m_xPopupMenuController.is() )
{
css::uno::Reference< css::lang::XComponent > xComponent(
m_xPopupMenuController, css::uno::UNO_QUERY );
if( xComponent.is() )
{
try
{
xComponent->dispose();
}
catch (...)
{}
}
m_xPopupMenuController.clear();
}
m_xContext.clear();
m_xPopupMenuFactory.clear();
m_xPopupMenu.clear();
}
void SAL_CALL PopupMenuToolbarController::initialize(
const css::uno::Sequence< css::uno::Any >& aArguments )
{
ToolboxController::initialize( aArguments );
osl::MutexGuard aGuard( m_aMutex );
if ( !m_aPopupCommand.getLength() )
m_aPopupCommand = m_aCommandURL;
try
{
m_xPopupMenuFactory.set(
css::frame::thePopupMenuControllerFactory::get( m_xContext ) );
m_bHasController = m_xPopupMenuFactory->hasController(
m_aPopupCommand, getModuleName() );
}
catch (const css::uno::Exception&)
{
TOOLS_INFO_EXCEPTION( "fwk.uielement", "" );
}
if ( !m_bHasController && m_aPopupCommand.startsWith( "private:resource/" ) )
{
m_bResourceURL = true;
m_bHasController = true;
}
SolarMutexGuard aSolarLock;
ToolBox* pToolBox = nullptr;
ToolBoxItemId nItemId;
if ( getToolboxId( nItemId, &pToolBox ) )
{
ToolBoxItemBits nCurStyle( pToolBox->GetItemBits( nItemId ) );
ToolBoxItemBits nSetStyle( getDropDownStyle() );
pToolBox->SetItemBits( nItemId,
m_bHasController ?
nCurStyle | nSetStyle :
nCurStyle & ~nSetStyle );
}
}
void SAL_CALL PopupMenuToolbarController::statusChanged( const css::frame::FeatureStateEvent& rEvent )
{
if ( m_bResourceURL )
return;
ToolBox* pToolBox = nullptr;
ToolBoxItemId nItemId;
if ( getToolboxId( nItemId, &pToolBox ) )
{
SolarMutexGuard aSolarLock;
pToolBox->EnableItem( nItemId, rEvent.IsEnabled );
bool bValue;
if ( rEvent.State >>= bValue )
pToolBox->CheckItem( nItemId, bValue );
}
}
css::uno::Reference< css::awt::XWindow > SAL_CALL
PopupMenuToolbarController::createPopupWindow()
{
css::uno::Reference< css::awt::XWindow > xRet;
osl::MutexGuard aGuard( m_aMutex );
if ( !m_bHasController )
return xRet;
createPopupMenuController();
SolarMutexGuard aSolarLock;
VclPtr< ToolBox > pToolBox = static_cast< ToolBox* >( VCLUnoHelper::GetWindow( getParent() ) );
if ( !pToolBox )
return xRet;
pToolBox->SetItemDown( m_nToolBoxId, true );
WindowAlign eAlign( pToolBox->GetAlign() );
// If the parent ToolBox is in popup mode (e.g. sub toolbar, overflow popup),
// its ToolBarManager can be disposed along with our controller, destroying
// m_xPopupMenu, while the latter still in execute. This should be fixed at a
// different level, for now just hold it here so it won't crash.
css::uno::Reference< css::awt::XPopupMenu > xPopupMenu ( m_xPopupMenu );
sal_uInt16 nId = xPopupMenu->execute(
css::uno::Reference< css::awt::XWindowPeer >( getParent(), css::uno::UNO_QUERY ),
VCLUnoHelper::ConvertToAWTRect( pToolBox->GetItemRect( m_nToolBoxId ) ),
( eAlign == WindowAlign::Top || eAlign == WindowAlign::Bottom ) ?
css::awt::PopupMenuDirection::EXECUTE_DOWN :
css::awt::PopupMenuDirection::EXECUTE_RIGHT );
pToolBox->SetItemDown( m_nToolBoxId, false );
if ( nId )
functionExecuted( xPopupMenu->getCommand( nId ) );
return xRet;
}
void PopupMenuToolbarController::functionExecuted( const OUString &/*rCommand*/)
{
}
ToolBoxItemBits PopupMenuToolbarController::getDropDownStyle() const
{
return ToolBoxItemBits::DROPDOWN;
}
void PopupMenuToolbarController::createPopupMenuController()
{
if( !m_bHasController )
return;
if ( m_xPopupMenuController.is() )
{
m_xPopupMenuController->updatePopupMenu();
}
else
{
css::uno::Sequence<css::uno::Any> aArgs {
css::uno::Any(comphelper::makePropertyValue("Frame", m_xFrame)),
css::uno::Any(comphelper::makePropertyValue("ModuleIdentifier", m_sModuleName)),
css::uno::Any(comphelper::makePropertyValue("InToolbar", true))
};
try
{
m_xPopupMenu = new VCLXPopupMenu();
if (m_bResourceURL)
{
sal_Int32 nAppendIndex = aArgs.getLength();
aArgs.realloc(nAppendIndex + 1);
aArgs.getArray()[nAppendIndex] <<= comphelper::makePropertyValue("ResourceURL", m_aPopupCommand);
m_xPopupMenuController.set( m_xContext->getServiceManager()->createInstanceWithArgumentsAndContext(
"com.sun.star.comp.framework.ResourceMenuController", aArgs, m_xContext), css::uno::UNO_QUERY_THROW );
}
else
{
m_xPopupMenuController.set( m_xPopupMenuFactory->createInstanceWithArgumentsAndContext(
m_aPopupCommand, aArgs, m_xContext), css::uno::UNO_QUERY_THROW );
}
m_xPopupMenuController->setPopupMenu( m_xPopupMenu );
}
catch ( const css::uno::Exception & )
{
TOOLS_INFO_EXCEPTION( "fwk.uielement", "" );
m_xPopupMenu.clear();
}
}
}
class GenericPopupToolbarController : public PopupMenuToolbarController
{
public:
GenericPopupToolbarController( const css::uno::Reference< css::uno::XComponentContext >& rxContext,
const css::uno::Sequence< css::uno::Any >& rxArgs );
// XInitialization
virtual void SAL_CALL initialize( const css::uno::Sequence< css::uno::Any >& rxArgs ) override;
// XStatusListener
virtual void SAL_CALL statusChanged( const css::frame::FeatureStateEvent& rEvent ) override;
// XServiceInfo
virtual OUString SAL_CALL getImplementationName() override;
virtual sal_Bool SAL_CALL supportsService(OUString const & rServiceName) override;
virtual css::uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() override;
private:
bool m_bSplitButton, m_bReplaceWithLast;
void functionExecuted(const OUString &rCommand) override;
ToolBoxItemBits getDropDownStyle() const override;
};
GenericPopupToolbarController::GenericPopupToolbarController(
const css::uno::Reference< css::uno::XComponentContext >& xContext,
const css::uno::Sequence< css::uno::Any >& rxArgs )
: PopupMenuToolbarController( xContext )
, m_bReplaceWithLast( false )
{
css::beans::PropertyValue aPropValue;
for ( const auto& arg: rxArgs )
{
if ( ( arg >>= aPropValue ) && aPropValue.Name == "Value" )
{
sal_Int32 nIdx{ 0 };
OUString aValue;
aPropValue.Value >>= aValue;
m_aPopupCommand = aValue.getToken(0, ';', nIdx);
m_bReplaceWithLast = aValue.getToken(0, ';', nIdx).toBoolean();
break;
}
}
m_bSplitButton = m_bReplaceWithLast || !m_aPopupCommand.isEmpty();
}
OUString GenericPopupToolbarController::getImplementationName()
{
return "com.sun.star.comp.framework.GenericPopupToolbarController";
}
sal_Bool GenericPopupToolbarController::supportsService(OUString const & rServiceName)
{
return cppu::supportsService( this, rServiceName );
}
css::uno::Sequence<OUString> GenericPopupToolbarController::getSupportedServiceNames()
{
return {"com.sun.star.frame.ToolbarController"};
}
void GenericPopupToolbarController::initialize( const css::uno::Sequence< css::uno::Any >& rxArgs )
{
PopupMenuToolbarController::initialize( rxArgs );
if ( m_bReplaceWithLast )
// Create early, so we can use the menu is statusChanged method.
createPopupMenuController();
}
void GenericPopupToolbarController::statusChanged( const css::frame::FeatureStateEvent& rEvent )
{
SolarMutexGuard aGuard;
if ( m_bReplaceWithLast && !rEvent.IsEnabled && m_xPopupMenu.is() )
{
ToolBox* pToolBox = nullptr;
ToolBoxItemId nId;
if ( getToolboxId( nId, &pToolBox ) && pToolBox->IsItemEnabled( nId ) )
{
Menu* pVclMenu = m_xPopupMenu->GetMenu();
pVclMenu->Activate();
pVclMenu->Deactivate();
}
for (sal_uInt16 i = 0, nCount = m_xPopupMenu->getItemCount(); i < nCount; ++i )
{
sal_uInt16 nItemId = m_xPopupMenu->getItemId(i);
if (nItemId && m_xPopupMenu->isItemEnabled(nItemId) && !m_xPopupMenu->getPopupMenu(nItemId).is())
{
functionExecuted(m_xPopupMenu->getCommand(nItemId));
return;
}
}
}
PopupMenuToolbarController::statusChanged( rEvent );
}
void GenericPopupToolbarController::functionExecuted( const OUString& rCommand )
{
if ( !m_bReplaceWithLast )
return;
removeStatusListener( m_aCommandURL );
auto aProperties = vcl::CommandInfoProvider::GetCommandProperties(rCommand, m_sModuleName);
OUString aRealCommand( vcl::CommandInfoProvider::GetRealCommandForCommand(aProperties) );
m_aCommandURL = aRealCommand.isEmpty() ? rCommand : aRealCommand;
addStatusListener( m_aCommandURL );
ToolBox* pToolBox = nullptr;
ToolBoxItemId nId;
if ( getToolboxId( nId, &pToolBox ) )
{
pToolBox->SetItemCommand( nId, rCommand );
pToolBox->SetHelpText( nId, OUString() ); // Will retrieve the new one from help.
pToolBox->SetItemText(nId, vcl::CommandInfoProvider::GetLabelForCommand(aProperties));
pToolBox->SetQuickHelpText(nId, vcl::CommandInfoProvider::GetTooltipForCommand(rCommand, aProperties, m_xFrame));
Image aImage = vcl::CommandInfoProvider::GetImageForCommand(rCommand, m_xFrame, pToolBox->GetImageSize());
if ( !!aImage )
pToolBox->SetItemImage( nId, aImage );
}
}
ToolBoxItemBits GenericPopupToolbarController::getDropDownStyle() const
{
return m_bSplitButton ? ToolBoxItemBits::DROPDOWN : ToolBoxItemBits::DROPDOWNONLY;
}
class SaveToolbarController : public cppu::ImplInheritanceHelper< PopupMenuToolbarController,
css::frame::XSubToolbarController,
css::util::XModifyListener >
{
public:
explicit SaveToolbarController( const css::uno::Reference< css::uno::XComponentContext >& rxContext );
// XInitialization
virtual void SAL_CALL initialize( const css::uno::Sequence< css::uno::Any >& aArguments ) override;
// XSubToolbarController
// Make ToolBarManager ask our controller for updated image, in case of icon theme change.
virtual sal_Bool SAL_CALL opensSubToolbar() override;
virtual OUString SAL_CALL getSubToolbarName() override;
virtual void SAL_CALL functionSelected( const OUString& aCommand ) override;
virtual void SAL_CALL updateImage() override;
// XStatusListener
virtual void SAL_CALL statusChanged( const css::frame::FeatureStateEvent& rEvent ) override;
// XModifyListener
virtual void SAL_CALL modified( const css::lang::EventObject& rEvent ) override;
// XEventListener
virtual void SAL_CALL disposing( const css::lang::EventObject& rEvent ) override;
// XComponent
virtual void SAL_CALL dispose() override;
// XServiceInfo
virtual OUString SAL_CALL getImplementationName() override;
virtual sal_Bool SAL_CALL supportsService( OUString const & rServiceName ) override;
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override;
private:
bool m_bReadOnly;
bool m_bModified;
css::uno::Reference< css::frame::XStorable > m_xStorable;
css::uno::Reference< css::util::XModifiable > m_xModifiable;
};
SaveToolbarController::SaveToolbarController( const css::uno::Reference< css::uno::XComponentContext >& rxContext )
: ImplInheritanceHelper( rxContext, ".uno:SaveAsMenu" )
, m_bReadOnly( false )
, m_bModified( false )
{
}
void SaveToolbarController::initialize( const css::uno::Sequence< css::uno::Any >& aArguments )
{
PopupMenuToolbarController::initialize( aArguments );
ToolBox* pToolBox = nullptr;
ToolBoxItemId nId;
if ( !getToolboxId( nId, &pToolBox ) )
return;
css::uno::Reference< css::frame::XController > xController = m_xFrame->getController();
if ( xController.is() )
m_xModifiable.set( xController->getModel(), css::uno::UNO_QUERY );
if ( m_xModifiable.is() && pToolBox->GetItemCommand( nId ) == m_aCommandURL )
// Will also enable the save as only mode.
m_xStorable.set( m_xModifiable, css::uno::UNO_QUERY );
else if ( !m_xModifiable.is() )
// Can be in table/query design.
m_xModifiable.set( xController, css::uno::UNO_QUERY );
else
// Simple save button, without the dropdown.
pToolBox->SetItemBits( nId, pToolBox->GetItemBits( nId ) & ~ ToolBoxItemBits::DROPDOWN );
if ( m_xModifiable.is() )
{
m_xModifiable->addModifyListener( this );
modified( css::lang::EventObject() );
}
}
sal_Bool SaveToolbarController::opensSubToolbar()
{
return true;
}
OUString SaveToolbarController::getSubToolbarName()
{
return OUString();
}
void SaveToolbarController::functionSelected( const OUString& /*aCommand*/ )
{
}
void SaveToolbarController::updateImage()
{
SolarMutexGuard aGuard;
ToolBox* pToolBox = nullptr;
ToolBoxItemId nId;
if ( !getToolboxId( nId, &pToolBox ) )
return;
vcl::ImageType eImageType = pToolBox->GetImageSize();
Image aImage;
if ( m_bReadOnly )
{
aImage = vcl::CommandInfoProvider::GetImageForCommand(".uno:SaveAs", m_xFrame, eImageType);
}
else if ( m_bModified )
{
if (eImageType == vcl::ImageType::Size26)
aImage = Image(StockImage::Yes, BMP_SAVEMODIFIED_LARGE);
else if (eImageType == vcl::ImageType::Size32)
aImage = Image(StockImage::Yes, BMP_SAVEMODIFIED_EXTRALARGE);
else
aImage = Image(StockImage::Yes, BMP_SAVEMODIFIED_SMALL);
}
if ( !aImage )
aImage = vcl::CommandInfoProvider::GetImageForCommand(m_aCommandURL, m_xFrame, eImageType);
if ( !!aImage )
pToolBox->SetItemImage( nId, aImage );
}
void SaveToolbarController::statusChanged( const css::frame::FeatureStateEvent& rEvent )
{
ToolBox* pToolBox = nullptr;
ToolBoxItemId nId;
if ( !getToolboxId( nId, &pToolBox ) )
return;
bool bLastReadOnly = m_bReadOnly;
m_bReadOnly = m_xStorable.is() && m_xStorable->isReadonly();
if ( bLastReadOnly != m_bReadOnly )
{
OUString sCommand = m_bReadOnly ? OUString( ".uno:SaveAs" ) : m_aCommandURL;
auto aProperties = vcl::CommandInfoProvider::GetCommandProperties(sCommand,
vcl::CommandInfoProvider::GetModuleIdentifier(m_xFrame));
pToolBox->SetQuickHelpText( nId,
vcl::CommandInfoProvider::GetTooltipForCommand(sCommand, aProperties, m_xFrame) );
pToolBox->SetItemBits( nId, pToolBox->GetItemBits( nId ) & ~( m_bReadOnly ? ToolBoxItemBits::DROPDOWN : ToolBoxItemBits::DROPDOWNONLY ) );
pToolBox->SetItemBits( nId, pToolBox->GetItemBits( nId ) | ( m_bReadOnly ? ToolBoxItemBits::DROPDOWNONLY : ToolBoxItemBits::DROPDOWN ) );
updateImage();
}
if ( !m_bReadOnly )
pToolBox->EnableItem( nId, rEvent.IsEnabled );
}
void SaveToolbarController::modified( const css::lang::EventObject& /*rEvent*/ )
{
bool bLastModified = m_bModified;
m_bModified = m_xModifiable->isModified();
if ( bLastModified != m_bModified )
updateImage();
}
void SaveToolbarController::disposing( const css::lang::EventObject& rEvent )
{
if ( rEvent.Source == m_xModifiable )
{
m_xModifiable.clear();
m_xStorable.clear();
}
else
PopupMenuToolbarController::disposing( rEvent );
}
void SaveToolbarController::dispose()
{
PopupMenuToolbarController::dispose();
if ( m_xModifiable.is() )
{
m_xModifiable->removeModifyListener( this );
m_xModifiable.clear();
}
m_xStorable.clear();
}
OUString SaveToolbarController::getImplementationName()
{
return "com.sun.star.comp.framework.SaveToolbarController";
}
sal_Bool SaveToolbarController::supportsService( OUString const & rServiceName )
{
return cppu::supportsService( this, rServiceName );
}
css::uno::Sequence< OUString > SaveToolbarController::getSupportedServiceNames()
{
return {"com.sun.star.frame.ToolbarController"};
}
class NewToolbarController : public cppu::ImplInheritanceHelper<PopupMenuToolbarController, css::frame::XSubToolbarController>
{
public:
explicit NewToolbarController( const css::uno::Reference< css::uno::XComponentContext >& rxContext );
// XServiceInfo
OUString SAL_CALL getImplementationName() override;
virtual sal_Bool SAL_CALL supportsService(OUString const & rServiceName) override;
css::uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() override;
void SAL_CALL initialize( const css::uno::Sequence< css::uno::Any >& aArguments ) override;
// XSubToolbarController
// Make ToolBarManager ask our controller for updated image, in case of icon theme change.
sal_Bool SAL_CALL opensSubToolbar() override { return true; }
OUString SAL_CALL getSubToolbarName() override { return OUString(); }
void SAL_CALL functionSelected( const OUString& ) override {}
void SAL_CALL updateImage() override;
private:
void functionExecuted( const OUString &rCommand ) override;
void SAL_CALL statusChanged( const css::frame::FeatureStateEvent& rEvent ) override;
void SAL_CALL execute( sal_Int16 KeyModifier ) override;
sal_uInt16 getMenuIdForCommand( std::u16string_view rCommand );
sal_uInt16 m_nMenuId;
};
NewToolbarController::NewToolbarController(
const css::uno::Reference< css::uno::XComponentContext >& xContext )
: ImplInheritanceHelper( xContext )
, m_nMenuId( 0 )
{
}
OUString NewToolbarController::getImplementationName()
{
return "org.apache.openoffice.comp.framework.NewToolbarController";
}
sal_Bool NewToolbarController::supportsService(OUString const & rServiceName)
{
return cppu::supportsService( this, rServiceName );
}
css::uno::Sequence<OUString> NewToolbarController::getSupportedServiceNames()
{
return {"com.sun.star.frame.ToolbarController"};
}
void SAL_CALL NewToolbarController::initialize( const css::uno::Sequence< css::uno::Any >& aArguments )
{
PopupMenuToolbarController::initialize( aArguments );
osl::MutexGuard aGuard( m_aMutex );
createPopupMenuController();
}
void SAL_CALL NewToolbarController::statusChanged( const css::frame::FeatureStateEvent& rEvent )
{
if ( rEvent.IsEnabled )
{
OUString aState;
rEvent.State >>= aState;
try
{
// set the image even if the state is not a string
// the toolbar item command will be used as a fallback
functionExecuted( aState );
}
catch (const css::ucb::CommandFailedException&)
{
}
catch (const css::ucb::ContentCreationException&)
{
}
}
enable( rEvent.IsEnabled );
}
void SAL_CALL NewToolbarController::execute( sal_Int16 /*KeyModifier*/ )
{
osl::MutexGuard aGuard( m_aMutex );
OUString aURL, aTarget;
if ( m_xPopupMenu.is() && m_nMenuId )
{
SolarMutexGuard aSolarMutexGuard;
aURL = m_xPopupMenu->getCommand(m_nMenuId);
// TODO investigate how to wrap Get/SetUserValue in css::awt::XMenu
MenuAttributes* pMenuAttributes(static_cast<MenuAttributes*>(m_xPopupMenu->getUserValue(m_nMenuId)));
if ( pMenuAttributes )
aTarget = pMenuAttributes->aTargetFrame;
else
aTarget = "_default";
}
else
aURL = m_aCommandURL;
css::uno::Sequence< css::beans::PropertyValue > aArgs{ comphelper::makePropertyValue(
"Referer", OUString( "private:user" )) };
dispatchCommand( aURL, aArgs, aTarget );
}
void NewToolbarController::functionExecuted( const OUString &rCommand )
{
m_nMenuId = getMenuIdForCommand( rCommand );
updateImage();
}
sal_uInt16 NewToolbarController::getMenuIdForCommand( std::u16string_view rCommand )
{
if ( m_xPopupMenu.is() && !rCommand.empty() )
{
sal_uInt16 nCount = m_xPopupMenu->getItemCount();
for ( sal_uInt16 n = 0; n < nCount; ++n )
{
sal_uInt16 nId = m_xPopupMenu->getItemId(n);
OUString aCmd(m_xPopupMenu->getCommand(nId));
// match even if the menu command is more detailed
// (maybe an additional query) #i28667#
if ( aCmd.match( rCommand ) )
return nId;
}
}
return 0;
}
void SAL_CALL NewToolbarController::updateImage()
{
SolarMutexGuard aSolarLock;
VclPtr< ToolBox> pToolBox = static_cast< ToolBox* >( VCLUnoHelper::GetWindow( getParent() ) );
if ( !pToolBox )
return;
OUString aURL, aImageId;
if ( m_xPopupMenu.is() && m_nMenuId )
{
aURL = m_xPopupMenu->getCommand(m_nMenuId);
MenuAttributes* pMenuAttributes(static_cast<MenuAttributes*>(m_xPopupMenu->getUserValue(m_nMenuId)));
if ( pMenuAttributes )
aImageId = pMenuAttributes->aImageId;
}
else
aURL = m_aCommandURL;
INetURLObject aURLObj( aImageId.isEmpty() ? aURL : aImageId );
vcl::ImageType eImageType( pToolBox->GetImageSize() );
Image aImage = SvFileInformationManager::GetImageNoDefault( aURLObj, eImageType );
if ( !aImage )
aImage = vcl::CommandInfoProvider::GetImageForCommand( aURL, m_xFrame, eImageType );
if ( !aImage )
return;
pToolBox->SetItemImage( m_nToolBoxId, aImage );
}
}
extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
com_sun_star_comp_framework_GenericPopupToolbarController_get_implementation(
css::uno::XComponentContext *context,
css::uno::Sequence<css::uno::Any> const &args)
{
return cppu::acquire(new GenericPopupToolbarController(context, args));
}
extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
com_sun_star_comp_framework_SaveToolbarController_get_implementation(
css::uno::XComponentContext *context,
css::uno::Sequence<css::uno::Any> const &)
{
return cppu::acquire(new SaveToolbarController(context));
}
extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
org_apache_openoffice_comp_framework_NewToolbarController_get_implementation(
css::uno::XComponentContext *context,
css::uno::Sequence<css::uno::Any> const &)
{
return cppu::acquire(new NewToolbarController(context));
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|