fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / framework / source / uielement / toolbarmerger.cxx
blob798b76cdbec5579937b82e74e2b7ee2166f9b838
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
27 namespace framework
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;
60 /**
61 Check whether a module identifier is part of a context
62 defined by a colon separated list of module identifier.
64 @param
65 rContext
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
72 @param
73 rModuleIdentifier
75 A string describing a module identifier. See IDL
76 description of com::sun::star::frame::XModuleManager.
78 @result
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 ));
90 /**
91 Converts a sequence, sequence of property values to
92 a vector of structs.
94 @param
95 rSequence
97 Provides a sequence, sequence of property values.
99 @param
100 rContainer
102 A vector of AddonToolbarItems which will hold the
103 conversion from the rSequence argument.
105 @result
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 );
129 return true;
133 Converts a sequence of property values to single
134 values.
136 @param
137 rSequence
139 Provides a sequence of property values.
141 @param
142 rCommandURL
144 Contains the value of the property with
145 Name="CommandURL".
147 @param
148 rLabel
150 Contains the value of the property with
151 Name="Title"
153 @param
154 rImageIdentifier
156 Contains the value of the property with
157 Name="ImageIdentifier"
159 @param
160 rTarget
162 Contains the value of the property with
163 Name="Target"
165 @param
166 rContext
168 Contains the value of the property with
169 Name="Context"
171 @param
172 rControlType
174 Contains the value of the property with
175 Name="ControlType"
177 @result
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,
185 OUString& rLabel,
186 OUString& rImageIdentifier,
187 OUString& rTarget,
188 OUString& rContext,
189 OUString& rControlType,
190 sal_uInt16& rWidth )
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.
219 @param
220 pToolbar
222 Must be a valid pointer to a toolbar with items which
223 should be searched.
225 @param
226 rReferencePoint
228 A command URL which should be the reference point for
229 the coming merge operation.
231 @result
232 Provides information about the search result, the
233 position of the reference point and the toolbar used.
235 ReferenceToolbarPathInfo ToolBarMerger::FindReferencePoint(
236 ToolBox* pToolbar,
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 );
249 if ( nItemId > 0 )
251 const OUString rCmd = pToolbar->GetItemCommand( nItemId );
252 if ( rCmd == rReferencePoint )
254 aResult.bResult = true;
255 aResult.nPos = i;
256 return aResult;
261 return aResult;
265 Processes a merge operation.
267 @param
268 xFrame
270 Must be a valid reference to a frame.
272 @param
273 pToolbar
275 A valid pointer to the toolbar where the merge
276 operation is applied to.
278 @param
279 nPos
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.
285 @param
286 rItemId
288 A unique item ID.
290 @param
291 rModuleIdentifier
293 The current application module context.
295 @param
296 rMergeCommand
298 A merge command.
300 @param
301 rMergeCommandParameter.
303 An optional argument for the merge command.
305 @param
306 rItems
308 Toolbar items which are associated to the merge
309 command.
311 @result
312 Returns true for a successful operation otherwise
313 false.
315 bool ToolBarMerger::ProcessMergeOperation(
316 const uno::Reference< frame::XFrame >& xFrame,
317 ToolBox* pToolbar,
318 sal_uInt16 nPos,
319 sal_uInt16& rItemId,
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 );
335 return false;
339 Processes a merge fallback operation.
341 @param
342 xFrame
344 Must be a valid reference to a frame.
346 @param
347 pToolbar
349 A valid pointer to the toolbar where the merge
350 fall back operation is applied to.
352 @param
353 nPos
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.
359 @param
360 rItemId
362 A unique item ID.
364 @param
365 rModuleIdentifier
367 The current application module context.
369 @param
370 rMergeCommand
372 A merge command.
374 @param
375 rItems
377 Toolbar items which are associated to the merge
378 command.
380 @result
381 Returns true for a successful operation otherwise
382 false.
384 bool ToolBarMerger::ProcessMergeFallback(
385 const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XFrame >& xFrame,
386 ToolBox* pToolbar,
387 sal_uInt16 /*nPos*/,
388 sal_uInt16& rItemId,
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 ) )
399 return true;
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 );
410 return false;
414 Merges (adds) toolbar items into an existing toolbar.
416 @param
417 xFrame
419 Must be a valid reference to a frame.
421 @param
422 pToolbar
424 A valid pointer to the toolbar where the merge
425 fall back operation is applied to.
427 @param
428 nPos
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.
434 @param
435 rItemId
437 A unique item ID.
439 @param
440 rModuleIdentifier
442 The current application module context.
444 @param
445 rItems
447 Toolbar items which are associated to the merge
448 command.
450 @result
451 Returns true for a successful operation otherwise
452 false.
454 bool ToolBarMerger::MergeItems(
455 const uno::Reference< frame::XFrame >& rFrame,
456 ToolBox* pToolbar,
457 sal_uInt16 nPos,
458 sal_uInt16 nModIndex,
459 sal_uInt16& rItemId,
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 ));
480 else
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 );
490 else
492 pIter->second.aIds.push_back( rItemId );
495 ToolBarMerger::CreateToolbarItem( pToolbar, rCommandMap, sal_uInt16( nInsPos ), rItemId, rItem );
498 ++nIndex;
499 ++rItemId;
503 return true;
507 Replaces a toolbar item with new items for an
508 existing toolbar.
510 @param
511 xFrame
513 Must be a valid reference to a frame.
515 @param
516 pToolbar
518 A valid pointer to the toolbar where the merge
519 fall back operation is applied to.
521 @param
522 nPos
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.
528 @param
529 rItemId
531 A unique item ID.
533 @param
534 rModuleIdentifier
536 The current application module context.
538 @param
539 rItems
541 Toolbar items which are associated to the merge
542 command.
544 @result
545 Returns true for a successful operation otherwise
546 false.
548 bool ToolBarMerger::ReplaceItem(
549 const uno::Reference< frame::XFrame >& xFrame,
550 ToolBox* pToolbar,
551 sal_uInt16 nPos,
552 sal_uInt16& rItemId,
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.
564 @param
565 pToolbar
567 A valid pointer to the toolbar where the merge
568 fall back operation is applied to.
570 @param
571 nPos
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.
577 @param
578 rMergeCommandParameter.
580 An optional argument for the merge command.
582 @result
583 Returns true for a successful operation otherwise
584 false.
586 bool ToolBarMerger::RemoveItems(
587 ToolBox* pToolbar,
588 sal_uInt16 nPos,
589 const OUString& rMergeCommandParameter )
591 sal_Int32 nCount = rMergeCommandParameter.toInt32();
592 if ( nCount > 0 )
594 for ( sal_Int32 i = 0; i < nCount; i++ )
596 if ( nPos < pToolbar->GetItemCount() )
597 pToolbar->RemoveItem( nPos );
600 return true;
604 Removes toolbar items from an existing toolbar.
606 @param
607 pToolbar
609 A valid pointer to the toolbar where the merge
610 fall back operation is applied to.
612 @param
613 nPos
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.
619 @param
620 rMergeCommandParameter.
622 An optional argument for the merge command.
624 @result
625 Returns true for a successful operation otherwise
626 false.
628 ::cppu::OWeakObject* ToolBarMerger::CreateController(
629 const uno::Reference< uno::XComponentContext >& rxContext,
630 const uno::Reference< frame::XFrame > & xFrame,
631 ToolBox* pToolbar,
632 const OUString& rCommandURL,
633 sal_uInt16 nId,
634 sal_uInt16 nWidth,
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 );
657 else
658 pResult = new GenericToolbarController( rxContext, xFrame, pToolbar, nId, rCommandURL );
660 return pResult;
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: */