1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <uielement/toolbarmerger.hxx>
21 #include <uielement/generictoolbarcontroller.hxx>
22 #include <framework/imageproducer.hxx>
24 #include <svtools/miscopt.hxx>
25 #include <comphelper/processfactory.hxx>
30 static const char MERGE_TOOLBAR_URL
[] = "URL";
31 static const char MERGE_TOOLBAR_TITLE
[] = "Title";
32 static const char MERGE_TOOLBAR_IMAGEID
[] = "ImageIdentifier";
33 static const char MERGE_TOOLBAR_CONTEXT
[] = "Context";
34 static const char MERGE_TOOLBAR_TARGET
[] = "Target";
35 static const char MERGE_TOOLBAR_CONTROLTYPE
[] = "ControlType";
36 static const char MERGE_TOOLBAR_WIDTH
[] = "Width";
38 static const char MERGECOMMAND_ADDAFTER
[] = "AddAfter";
39 static const char MERGECOMMAND_ADDBEFORE
[] = "AddBefore";
40 static const char MERGECOMMAND_REPLACE
[] = "Replace";
41 static const char MERGECOMMAND_REMOVE
[] = "Remove";
43 static const char MERGEFALLBACK_ADDLAST
[] = "AddLast";
44 static const char MERGEFALLBACK_ADDFIRST
[] = "AddFirst";
45 static const char MERGEFALLBACK_IGNORE
[] = "Ignore";
47 static const char TOOLBARCONTROLLER_BUTTON
[] = "Button";
48 static const char TOOLBARCONTROLLER_COMBOBOX
[] = "Combobox";
49 static const char TOOLBARCONTROLLER_EDIT
[] = "Editfield";
50 static const char TOOLBARCONTROLLER_SPINFIELD
[] = "Spinfield";
51 static const char TOOLBARCONTROLLER_IMGBUTTON
[] = "ImageButton";
52 static const char TOOLBARCONTROLLER_DROPDOWNBOX
[] = "Dropdownbox";
53 static const char TOOLBARCONTROLLER_DROPDOWNBTN
[] = "DropdownButton";
54 static const char TOOLBARCONTROLLER_TOGGLEDDBTN
[] = "ToggleDropdownButton";
56 static const char TOOLBOXITEM_SEPARATOR_STR
[] = "private:separator";
58 using namespace ::com::sun::star
;
61 Check whether a module identifier is part of a context
62 defined by a colon separated list of module identifier.
67 Describes a context string list where all contexts
68 are delimited by a colon. For more information about
69 the module identifier used as context strings see the
70 IDL description of com::sun::star::frame::XModuleManager
75 A string describing a module identifier. See IDL
76 description of com::sun::star::frame::XModuleManager.
79 The result is true if the rContext is an empty string
80 or rModuleIdentifier is part of the context string.
83 bool ToolBarMerger::IsCorrectContext(
84 const OUString
& rContext
,
85 const OUString
& rModuleIdentifier
)
87 return ( rContext
.isEmpty() || ( rContext
.indexOf( rModuleIdentifier
) >= 0 ));
91 Converts a sequence, sequence of property values to
97 Provides a sequence, sequence of property values.
102 A vector of AddonToolbarItems which will hold the
103 conversion from the rSequence argument.
106 The result is true if the sequence, sequence of property
107 values could be converted to a vector of structs.
110 bool ToolBarMerger::ConvertSeqSeqToVector(
111 const uno::Sequence
< uno::Sequence
< beans::PropertyValue
> >& rSequence
,
112 AddonToolbarItemContainer
& rContainer
)
114 sal_Int32
nLen( rSequence
.getLength() );
115 for ( sal_Int32 i
= 0; i
< nLen
; i
++ )
117 AddonToolbarItem aAddonToolbarItem
;
118 ConvertSequenceToValues( rSequence
[i
],
119 aAddonToolbarItem
.aCommandURL
,
120 aAddonToolbarItem
.aLabel
,
121 aAddonToolbarItem
.aImageIdentifier
,
122 aAddonToolbarItem
.aTarget
,
123 aAddonToolbarItem
.aContext
,
124 aAddonToolbarItem
.aControlType
,
125 aAddonToolbarItem
.nWidth
);
126 rContainer
.push_back( aAddonToolbarItem
);
133 Converts a sequence of property values to single
139 Provides a sequence of property values.
144 Contains the value of the property with
150 Contains the value of the property with
156 Contains the value of the property with
157 Name="ImageIdentifier"
162 Contains the value of the property with
168 Contains the value of the property with
174 Contains the value of the property with
178 All possible mapping between sequence of property
179 values and the single values are done.
182 void ToolBarMerger::ConvertSequenceToValues(
183 const uno::Sequence
< beans::PropertyValue
>& rSequence
,
184 OUString
& rCommandURL
,
186 OUString
& rImageIdentifier
,
189 OUString
& rControlType
,
192 for ( sal_Int32 i
= 0; i
< rSequence
.getLength(); i
++ )
194 if ( rSequence
[i
].Name
== MERGE_TOOLBAR_URL
)
195 rSequence
[i
].Value
>>= rCommandURL
;
196 else if ( rSequence
[i
].Name
== MERGE_TOOLBAR_TITLE
)
197 rSequence
[i
].Value
>>= rLabel
;
198 else if ( rSequence
[i
].Name
== MERGE_TOOLBAR_IMAGEID
)
199 rSequence
[i
].Value
>>= rImageIdentifier
;
200 else if ( rSequence
[i
].Name
== MERGE_TOOLBAR_CONTEXT
)
201 rSequence
[i
].Value
>>= rContext
;
202 else if ( rSequence
[i
].Name
== MERGE_TOOLBAR_TARGET
)
203 rSequence
[i
].Value
>>= rTarget
;
204 else if ( rSequence
[i
].Name
== MERGE_TOOLBAR_CONTROLTYPE
)
205 rSequence
[i
].Value
>>= rControlType
;
206 else if ( rSequence
[i
].Name
== MERGE_TOOLBAR_WIDTH
)
208 sal_Int32 aValue
= 0;
209 rSequence
[i
].Value
>>= aValue
;
210 rWidth
= sal_uInt16( aValue
);
216 Tries to find the reference point provided and delivers
217 position and result of the search process.
222 Must be a valid pointer to a toolbar with items which
228 A command URL which should be the reference point for
229 the coming merge operation.
232 Provides information about the search result, the
233 position of the reference point and the toolbar used.
235 ReferenceToolbarPathInfo
ToolBarMerger::FindReferencePoint(
237 const OUString
& rReferencePoint
)
239 ReferenceToolbarPathInfo aResult
;
240 aResult
.bResult
= false;
241 aResult
.pToolbar
= pToolbar
;
242 aResult
.nPos
= TOOLBOX_ITEM_NOTFOUND
;
244 const sal_uInt16
nSize( pToolbar
->GetItemCount() );
246 for ( sal_uInt16 i
= 0; i
< nSize
; i
++ )
248 const sal_uInt16 nItemId
= pToolbar
->GetItemId( i
);
251 const OUString rCmd
= pToolbar
->GetItemCommand( nItemId
);
252 if ( rCmd
== rReferencePoint
)
254 aResult
.bResult
= true;
265 Processes a merge operation.
270 Must be a valid reference to a frame.
275 A valid pointer to the toolbar where the merge
276 operation is applied to.
281 The reference position of the toolbar item for
282 the merge operation. Value must be between
283 0 and number of toolbar items - 1.
293 The current application module context.
301 rMergeCommandParameter.
303 An optional argument for the merge command.
308 Toolbar items which are associated to the merge
312 Returns true for a successful operation otherwise
315 bool ToolBarMerger::ProcessMergeOperation(
316 const uno::Reference
< frame::XFrame
>& xFrame
,
320 CommandToInfoMap
& rCommandMap
,
321 const OUString
& rModuleIdentifier
,
322 const OUString
& rMergeCommand
,
323 const OUString
& rMergeCommandParameter
,
324 const AddonToolbarItemContainer
& rItems
)
326 if ( rMergeCommand
== MERGECOMMAND_ADDAFTER
)
327 return MergeItems( xFrame
, pToolbar
, nPos
, 1, rItemId
, rCommandMap
, rModuleIdentifier
, rItems
);
328 else if ( rMergeCommand
== MERGECOMMAND_ADDBEFORE
)
329 return MergeItems( xFrame
, pToolbar
, nPos
, 0, rItemId
, rCommandMap
, rModuleIdentifier
, rItems
);
330 else if ( rMergeCommand
== MERGECOMMAND_REPLACE
)
331 return ReplaceItem( xFrame
, pToolbar
, nPos
, rItemId
, rCommandMap
, rModuleIdentifier
, rItems
);
332 else if ( rMergeCommand
== MERGECOMMAND_REMOVE
)
333 return RemoveItems( pToolbar
, nPos
, rMergeCommandParameter
);
339 Processes a merge fallback operation.
344 Must be a valid reference to a frame.
349 A valid pointer to the toolbar where the merge
350 fall back operation is applied to.
355 The reference position of the toolbar item for
356 the merge operation. Value must be between
357 0 and number of toolbar items - 1.
367 The current application module context.
377 Toolbar items which are associated to the merge
381 Returns true for a successful operation otherwise
384 bool ToolBarMerger::ProcessMergeFallback(
385 const ::com::sun::star::uno::Reference
< ::com::sun::star::frame::XFrame
>& xFrame
,
389 CommandToInfoMap
& rCommandMap
,
390 const OUString
& rModuleIdentifier
,
391 const OUString
& rMergeCommand
,
392 const OUString
& rMergeFallback
,
393 const AddonToolbarItemContainer
& rItems
)
395 if (( rMergeFallback
== MERGEFALLBACK_IGNORE
) ||
396 ( rMergeCommand
== MERGECOMMAND_REPLACE
) ||
397 ( rMergeCommand
== MERGECOMMAND_REMOVE
) )
401 else if (( rMergeCommand
== MERGECOMMAND_ADDBEFORE
) ||
402 ( rMergeCommand
== MERGECOMMAND_ADDAFTER
) )
404 if ( rMergeFallback
== MERGEFALLBACK_ADDFIRST
)
405 return MergeItems( xFrame
, pToolbar
, 0, 0, rItemId
, rCommandMap
, rModuleIdentifier
, rItems
);
406 else if ( rMergeFallback
== MERGEFALLBACK_ADDLAST
)
407 return MergeItems( xFrame
, pToolbar
, TOOLBOX_APPEND
, 0, rItemId
, rCommandMap
, rModuleIdentifier
, rItems
);
414 Merges (adds) toolbar items into an existing toolbar.
419 Must be a valid reference to a frame.
424 A valid pointer to the toolbar where the merge
425 fall back operation is applied to.
430 The reference position of the toolbar item for
431 the merge operation. Value must be between
432 0 and number of toolbar items - 1.
442 The current application module context.
447 Toolbar items which are associated to the merge
451 Returns true for a successful operation otherwise
454 bool ToolBarMerger::MergeItems(
455 const uno::Reference
< frame::XFrame
>& rFrame
,
458 sal_uInt16 nModIndex
,
460 CommandToInfoMap
& rCommandMap
,
461 const OUString
& rModuleIdentifier
,
462 const AddonToolbarItemContainer
& rAddonToolbarItems
)
464 const sal_Int32
nSize( rAddonToolbarItems
.size() );
466 uno::Reference
< frame::XFrame
> xFrame( rFrame
);
468 sal_uInt16
nIndex( 0 );
469 for ( sal_Int32 i
= 0; i
< nSize
; i
++ )
471 const AddonToolbarItem
& rItem
= rAddonToolbarItems
[i
];
472 if ( IsCorrectContext( rItem
.aContext
, rModuleIdentifier
))
474 sal_Int32 nInsPos
= nPos
+nModIndex
+i
;
475 if ( nInsPos
> sal_Int32( pToolbar
->GetItemCount() ))
476 nInsPos
= TOOLBOX_APPEND
;
478 if ( rItem
.aCommandURL
== TOOLBOXITEM_SEPARATOR_STR
)
479 pToolbar
->InsertSeparator( sal_uInt16( nInsPos
));
482 CommandToInfoMap::iterator pIter
= rCommandMap
.find( rItem
.aCommandURL
);
483 if ( pIter
== rCommandMap
.end())
485 CommandInfo aCmdInfo
;
486 aCmdInfo
.nId
= rItemId
;
487 const CommandToInfoMap::value_type
aValue( rItem
.aCommandURL
, aCmdInfo
);
488 rCommandMap
.insert( aValue
);
492 pIter
->second
.aIds
.push_back( rItemId
);
495 ToolBarMerger::CreateToolbarItem( pToolbar
, rCommandMap
, sal_uInt16( nInsPos
), rItemId
, rItem
);
507 Replaces a toolbar item with new items for an
513 Must be a valid reference to a frame.
518 A valid pointer to the toolbar where the merge
519 fall back operation is applied to.
524 The reference position of the toolbar item for
525 the merge operation. Value must be between
526 0 and number of toolbar items - 1.
536 The current application module context.
541 Toolbar items which are associated to the merge
545 Returns true for a successful operation otherwise
548 bool ToolBarMerger::ReplaceItem(
549 const uno::Reference
< frame::XFrame
>& xFrame
,
553 CommandToInfoMap
& rCommandMap
,
554 const OUString
& rModuleIdentifier
,
555 const AddonToolbarItemContainer
& rAddonToolbarItems
)
557 pToolbar
->RemoveItem( nPos
);
558 return MergeItems( xFrame
, pToolbar
, nPos
, 0, rItemId
, rCommandMap
, rModuleIdentifier
, rAddonToolbarItems
);
562 Removes toolbar items from an existing toolbar.
567 A valid pointer to the toolbar where the merge
568 fall back operation is applied to.
573 The reference position of the toolbar item for
574 the merge operation. Value must be between
575 0 and number of toolbar items - 1.
578 rMergeCommandParameter.
580 An optional argument for the merge command.
583 Returns true for a successful operation otherwise
586 bool ToolBarMerger::RemoveItems(
589 const OUString
& rMergeCommandParameter
)
591 sal_Int32 nCount
= rMergeCommandParameter
.toInt32();
594 for ( sal_Int32 i
= 0; i
< nCount
; i
++ )
596 if ( nPos
< pToolbar
->GetItemCount() )
597 pToolbar
->RemoveItem( nPos
);
604 Removes toolbar items from an existing toolbar.
609 A valid pointer to the toolbar where the merge
610 fall back operation is applied to.
615 The reference position of the toolbar item for
616 the merge operation. Value must be between
617 0 and number of toolbar items - 1.
620 rMergeCommandParameter.
622 An optional argument for the merge command.
625 Returns true for a successful operation otherwise
628 ::cppu::OWeakObject
* ToolBarMerger::CreateController(
629 const uno::Reference
< uno::XComponentContext
>& rxContext
,
630 const uno::Reference
< frame::XFrame
> & xFrame
,
632 const OUString
& rCommandURL
,
635 const OUString
& rControlType
)
637 ::cppu::OWeakObject
* pResult( 0 );
639 if ( rControlType
== TOOLBARCONTROLLER_BUTTON
)
640 pResult
= new ButtonToolbarController( rxContext
, pToolbar
, rCommandURL
);
641 else if ( rControlType
== TOOLBARCONTROLLER_COMBOBOX
)
642 pResult
= new ComboboxToolbarController( rxContext
, xFrame
, pToolbar
, nId
, nWidth
, rCommandURL
);
643 else if ( rControlType
== TOOLBARCONTROLLER_EDIT
)
644 pResult
= new EditToolbarController( rxContext
, xFrame
, pToolbar
, nId
, nWidth
, rCommandURL
);
645 else if ( rControlType
== TOOLBARCONTROLLER_SPINFIELD
)
646 pResult
= new SpinfieldToolbarController( rxContext
, xFrame
, pToolbar
, nId
, nWidth
, rCommandURL
);
647 else if ( rControlType
== TOOLBARCONTROLLER_IMGBUTTON
)
648 pResult
= new ImageButtonToolbarController( rxContext
, xFrame
, pToolbar
, nId
, rCommandURL
);
649 else if ( rControlType
== TOOLBARCONTROLLER_DROPDOWNBOX
)
650 pResult
= new DropdownToolbarController( rxContext
, xFrame
, pToolbar
, nId
, nWidth
, rCommandURL
);
651 else if ( rControlType
== TOOLBARCONTROLLER_DROPDOWNBTN
)
652 pResult
= new ToggleButtonToolbarController( rxContext
, xFrame
, pToolbar
, nId
,
653 ToggleButtonToolbarController::STYLE_DROPDOWNBUTTON
, rCommandURL
);
654 else if ( rControlType
== TOOLBARCONTROLLER_TOGGLEDDBTN
)
655 pResult
= new ToggleButtonToolbarController( rxContext
, xFrame
, pToolbar
, nId
,
656 ToggleButtonToolbarController::STYLE_TOGGLE_DROPDOWNBUTTON
, rCommandURL
);
658 pResult
= new GenericToolbarController( rxContext
, xFrame
, pToolbar
, nId
, rCommandURL
);
663 void ToolBarMerger::CreateToolbarItem( ToolBox
* pToolbar
, CommandToInfoMap
& rCommandMap
, sal_uInt16 nPos
, sal_uInt16 nItemId
, const AddonToolbarItem
& rItem
)
665 pToolbar
->InsertItem( nItemId
, rItem
.aLabel
, ToolBoxItemBits::NONE
, nPos
);
666 pToolbar
->SetItemCommand( nItemId
, rItem
.aCommandURL
);
667 pToolbar
->SetQuickHelpText( nItemId
, rItem
.aLabel
);
668 pToolbar
->SetItemText( nItemId
, rItem
.aLabel
);
669 pToolbar
->EnableItem( nItemId
, true );
670 pToolbar
->SetItemState( nItemId
, TRISTATE_FALSE
);
672 CommandToInfoMap::iterator pIter
= rCommandMap
.find( rItem
.aCommandURL
);
673 if ( pIter
!= rCommandMap
.end() )
674 pIter
->second
.nWidth
= rItem
.nWidth
;
676 // Use the user data to store add-on specific data with the toolbar item
677 AddonsParams
* pAddonParams
= new AddonsParams
;
678 pAddonParams
->aImageId
= rItem
.aImageIdentifier
;
679 pAddonParams
->aTarget
= rItem
.aTarget
;
680 pAddonParams
->aControlType
= rItem
.aControlType
;
681 pToolbar
->SetItemData( nItemId
, pAddonParams
);
684 } // namespace framework
686 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */