1*6d739b60SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*6d739b60SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*6d739b60SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*6d739b60SAndrew Rist  * distributed with this work for additional information
6*6d739b60SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*6d739b60SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*6d739b60SAndrew Rist  * "License"); you may not use this file except in compliance
9*6d739b60SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*6d739b60SAndrew Rist  *
11*6d739b60SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*6d739b60SAndrew Rist  *
13*6d739b60SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*6d739b60SAndrew Rist  * software distributed under the License is distributed on an
15*6d739b60SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*6d739b60SAndrew Rist  * KIND, either express or implied.  See the License for the
17*6d739b60SAndrew Rist  * specific language governing permissions and limitations
18*6d739b60SAndrew Rist  * under the License.
19*6d739b60SAndrew Rist  *
20*6d739b60SAndrew Rist  *************************************************************/
21*6d739b60SAndrew Rist 
22*6d739b60SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_framework.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <uielement/toolbarmerger.hxx>
28cdf0e10cSrcweir #include <uielement/generictoolbarcontroller.hxx>
29cdf0e10cSrcweir #include <framework/imageproducer.hxx>
30cdf0e10cSrcweir 
31cdf0e10cSrcweir #include <svtools/miscopt.hxx>
32cdf0e10cSrcweir 
33cdf0e10cSrcweir namespace framework
34cdf0e10cSrcweir {
35cdf0e10cSrcweir 
36cdf0e10cSrcweir static const char MERGE_TOOLBAR_URL[]              = "URL";
37cdf0e10cSrcweir static const sal_uInt32 MERGE_TOOLBAR_URL_LEN      = 3;
38cdf0e10cSrcweir static const char MERGE_TOOLBAR_TITLE[]            = "Title";
39cdf0e10cSrcweir static const sal_uInt32 MERGE_TOOLBAR_TITLE_LEN    = 5;
40cdf0e10cSrcweir static const char MERGE_TOOLBAR_IMAGEID[]          = "ImageIdentifier";
41cdf0e10cSrcweir static const sal_uInt32 MERGE_TOOLBAR_IMAGEID_LEN  = 15;
42cdf0e10cSrcweir static const char MERGE_TOOLBAR_CONTEXT[]          = "Context";
43cdf0e10cSrcweir static const sal_uInt32 MERGE_TOOLBAR_CONTEXT_LEN  = 7;
44cdf0e10cSrcweir static const char MERGE_TOOLBAR_TARGET[]           = "Target";
45cdf0e10cSrcweir static const sal_uInt32 MERGE_TOOLBAR_TARGET_LEN   = 6;
46cdf0e10cSrcweir static const char MERGE_TOOLBAR_CONTROLTYPE[]      = "ControlType";
47cdf0e10cSrcweir static const char MERGE_TOOLBAR_CONTROLTYPE_LEN    = 11;
48cdf0e10cSrcweir static const char MERGE_TOOLBAR_WIDTH[]            = "Width";
49cdf0e10cSrcweir static const char MERGE_TOOLBAR_WIDTH_LEN          = 5;
50cdf0e10cSrcweir 
51cdf0e10cSrcweir static const char MERGECOMMAND_ADDAFTER[]          = "AddAfter";
52cdf0e10cSrcweir static const sal_uInt32 MERGECOMMAND_ADDAFTER_LEN  = 8;
53cdf0e10cSrcweir static const char MERGECOMMAND_ADDBEFORE[]         = "AddBefore";
54cdf0e10cSrcweir static const sal_uInt32 MERGECOMMAND_ADDBEFORE_LEN = 9;
55cdf0e10cSrcweir static const char MERGECOMMAND_REPLACE[]           = "Replace";
56cdf0e10cSrcweir static const sal_uInt32 MERGECOMMAND_REPLACE_LEN   = 7;
57cdf0e10cSrcweir static const char MERGECOMMAND_REMOVE[]            = "Remove";
58cdf0e10cSrcweir static const sal_uInt32 MERGECOMMAND_REMOVE_LEN    = 6;
59cdf0e10cSrcweir 
60cdf0e10cSrcweir static const char MERGEFALLBACK_ADDLAST[]          = "AddLast";
61cdf0e10cSrcweir static const char MERGEFALLBACK_ADDLAST_LEN        = 7;
62cdf0e10cSrcweir static const char MERGEFALLBACK_ADDFIRST[]         = "AddFirst";
63cdf0e10cSrcweir static const char MERGEFALLBACK_ADDFIRST_LEN       = 8;
64cdf0e10cSrcweir static const char MERGEFALLBACK_IGNORE[]           = "Ignore";
65cdf0e10cSrcweir static const char MERGEFALLBACK_IGNORE_LEN         = 6;
66cdf0e10cSrcweir 
67cdf0e10cSrcweir static const char TOOLBARCONTROLLER_BUTTON[]              = "Button";
68cdf0e10cSrcweir static const sal_uInt32 TOOLBARCONTROLLER_BUTTON_LEN      = 6;
69cdf0e10cSrcweir static const char TOOLBARCONTROLLER_COMBOBOX[]            = "Combobox";
70cdf0e10cSrcweir static const sal_uInt32 TOOLBARCONTROLLER_COMBOBOX_LEN    = 8;
71cdf0e10cSrcweir static const char TOOLBARCONTROLLER_EDIT[]                = "Editfield";
72cdf0e10cSrcweir static const sal_uInt32 TOOLBARCONTROLLER_EDIT_LEN        = 9;
73cdf0e10cSrcweir static const char TOOLBARCONTROLLER_SPINFIELD[]           = "Spinfield";
74cdf0e10cSrcweir static const sal_uInt32 TOOLBARCONTROLLER_SPINFIELD_LEN   = 9;
75cdf0e10cSrcweir static const char TOOLBARCONTROLLER_IMGBUTTON[]           = "ImageButton";
76cdf0e10cSrcweir static const sal_uInt32 TOOLBARCONTROLLER_IMGBUTTON_LEN   = 11;
77cdf0e10cSrcweir static const char TOOLBARCONTROLLER_DROPDOWNBOX[]         = "Dropdownbox";
78cdf0e10cSrcweir static const sal_uInt32 TOOLBARCONTROLLER_DROPDOWNBOX_LEN = 11;
79cdf0e10cSrcweir static const char TOOLBARCONTROLLER_DROPDOWNBTN[]         = "DropdownButton";
80cdf0e10cSrcweir static const sal_uInt32 TOOLBARCONTROLLER_DROPDOWNBTN_LEN = 14;
81cdf0e10cSrcweir static const char TOOLBARCONTROLLER_TOGGLEDDBTN[]         = "ToggleDropdownButton";
82cdf0e10cSrcweir static const sal_uInt32 TOOLBARCONTROLLER_TOGGLEDDBTN_LEN = 20;
83cdf0e10cSrcweir 
84cdf0e10cSrcweir static const char   TOOLBOXITEM_SEPARATOR_STR[]   = "private:separator";
85cdf0e10cSrcweir static const sal_uInt16 TOOLBOXITEM_SEPARATOR_STR_LEN = sizeof( TOOLBOXITEM_SEPARATOR_STR )-1;
86cdf0e10cSrcweir 
87cdf0e10cSrcweir using namespace ::com::sun::star;
88cdf0e10cSrcweir 
89cdf0e10cSrcweir /**
90cdf0e10cSrcweir  Check whether a module identifier is part of a context
91cdf0e10cSrcweir  defined by a colon separated list of module identifier.
92cdf0e10cSrcweir 
93cdf0e10cSrcweir  @param
94cdf0e10cSrcweir      rContext
95cdf0e10cSrcweir 
96cdf0e10cSrcweir      Describes a context string list where all contexts
97cdf0e10cSrcweir      are delimited by a colon. For more information about
98cdf0e10cSrcweir      the module identifier used as context strings see the
99cdf0e10cSrcweir      IDL description of com::sun::star::frame::XModuleManager
100cdf0e10cSrcweir 
101cdf0e10cSrcweir  @param
102cdf0e10cSrcweir      rModuleIdentifier
103cdf0e10cSrcweir 
104cdf0e10cSrcweir      A string describing a module identifier. See IDL
105cdf0e10cSrcweir      description of com::sun::star::frame::XModuleManager.
106cdf0e10cSrcweir 
107cdf0e10cSrcweir  @result
108cdf0e10cSrcweir      The result is true if the rContext is an empty string
109cdf0e10cSrcweir      or rModuleIdentifier is part of the context string.
110cdf0e10cSrcweir 
111cdf0e10cSrcweir */
112cdf0e10cSrcweir bool ToolBarMerger::IsCorrectContext(
113cdf0e10cSrcweir     const ::rtl::OUString& rContext,
114cdf0e10cSrcweir     const ::rtl::OUString& rModuleIdentifier )
115cdf0e10cSrcweir {
116cdf0e10cSrcweir     return (( rContext.getLength() == 0 ) || ( rContext.indexOf( rModuleIdentifier ) >= 0 ));
117cdf0e10cSrcweir }
118cdf0e10cSrcweir 
119cdf0e10cSrcweir /**
120cdf0e10cSrcweir  Converts a sequence, sequence of property values to
121cdf0e10cSrcweir  a vector of structs.
122cdf0e10cSrcweir 
123cdf0e10cSrcweir  @param
124cdf0e10cSrcweir      rSequence
125cdf0e10cSrcweir 
126cdf0e10cSrcweir      Provides a sequence, sequence of property values.
127cdf0e10cSrcweir 
128cdf0e10cSrcweir  @param
129cdf0e10cSrcweir      rContainer
130cdf0e10cSrcweir 
131cdf0e10cSrcweir      A vector of AddonToolbarItems which will hold the
132cdf0e10cSrcweir      conversion from the rSequence argument.
133cdf0e10cSrcweir 
134cdf0e10cSrcweir  @result
135cdf0e10cSrcweir      The result is true if the sequence, sequence of property
136cdf0e10cSrcweir      values could be converted to a vector of structs.
137cdf0e10cSrcweir 
138cdf0e10cSrcweir */
139cdf0e10cSrcweir bool ToolBarMerger::ConvertSeqSeqToVector(
140cdf0e10cSrcweir     const uno::Sequence< uno::Sequence< beans::PropertyValue > > rSequence,
141cdf0e10cSrcweir     AddonToolbarItemContainer& rContainer )
142cdf0e10cSrcweir {
143cdf0e10cSrcweir     sal_Int32 nLen( rSequence.getLength() );
144cdf0e10cSrcweir     for ( sal_Int32 i = 0; i < nLen; i++ )
145cdf0e10cSrcweir     {
146cdf0e10cSrcweir         AddonToolbarItem aAddonToolbarItem;
147cdf0e10cSrcweir         ConvertSequenceToValues( rSequence[i],
148cdf0e10cSrcweir                                  aAddonToolbarItem.aCommandURL,
149cdf0e10cSrcweir                                  aAddonToolbarItem.aLabel,
150cdf0e10cSrcweir                                  aAddonToolbarItem.aImageIdentifier,
151cdf0e10cSrcweir                                  aAddonToolbarItem.aTarget,
152cdf0e10cSrcweir                                  aAddonToolbarItem.aContext,
153cdf0e10cSrcweir                                  aAddonToolbarItem.aControlType,
154cdf0e10cSrcweir                                  aAddonToolbarItem.nWidth );
155cdf0e10cSrcweir         rContainer.push_back( aAddonToolbarItem );
156cdf0e10cSrcweir     }
157cdf0e10cSrcweir 
158cdf0e10cSrcweir     return true;
159cdf0e10cSrcweir }
160cdf0e10cSrcweir 
161cdf0e10cSrcweir /**
162cdf0e10cSrcweir  Converts a sequence of property values to single
163cdf0e10cSrcweir  values.
164cdf0e10cSrcweir 
165cdf0e10cSrcweir  @param
166cdf0e10cSrcweir      rSequence
167cdf0e10cSrcweir 
168cdf0e10cSrcweir      Provides a sequence of property values.
169cdf0e10cSrcweir 
170cdf0e10cSrcweir  @param
171cdf0e10cSrcweir      rCommandURL
172cdf0e10cSrcweir 
173cdf0e10cSrcweir      Contains the value of the property with
174cdf0e10cSrcweir      Name="CommandURL".
175cdf0e10cSrcweir 
176cdf0e10cSrcweir  @param
177cdf0e10cSrcweir      rLabel
178cdf0e10cSrcweir 
179cdf0e10cSrcweir      Contains the value of the property with
180cdf0e10cSrcweir      Name="Title"
181cdf0e10cSrcweir 
182cdf0e10cSrcweir  @param
183cdf0e10cSrcweir      rImageIdentifier
184cdf0e10cSrcweir 
185cdf0e10cSrcweir      Contains the value of the property with
186cdf0e10cSrcweir      Name="ImageIdentifier"
187cdf0e10cSrcweir 
188cdf0e10cSrcweir  @param
189cdf0e10cSrcweir      rTarget
190cdf0e10cSrcweir 
191cdf0e10cSrcweir      Contains the value of the property with
192cdf0e10cSrcweir      Name="Target"
193cdf0e10cSrcweir 
194cdf0e10cSrcweir  @param
195cdf0e10cSrcweir      rContext
196cdf0e10cSrcweir 
197cdf0e10cSrcweir      Contains the value of the property with
198cdf0e10cSrcweir      Name="Context"
199cdf0e10cSrcweir 
200cdf0e10cSrcweir  @param
201cdf0e10cSrcweir      rControlType
202cdf0e10cSrcweir 
203cdf0e10cSrcweir      Contains the value of the property with
204cdf0e10cSrcweir      Name="ControlType"
205cdf0e10cSrcweir 
206cdf0e10cSrcweir  @result
207cdf0e10cSrcweir      All possible mapping between sequence of property
208cdf0e10cSrcweir      values and the single values are done.
209cdf0e10cSrcweir 
210cdf0e10cSrcweir */
211cdf0e10cSrcweir void ToolBarMerger::ConvertSequenceToValues(
212cdf0e10cSrcweir     const uno::Sequence< beans::PropertyValue > rSequence,
213cdf0e10cSrcweir     ::rtl::OUString& rCommandURL,
214cdf0e10cSrcweir     ::rtl::OUString& rLabel,
215cdf0e10cSrcweir     ::rtl::OUString& rImageIdentifier,
216cdf0e10cSrcweir     ::rtl::OUString& rTarget,
217cdf0e10cSrcweir     ::rtl::OUString& rContext,
218cdf0e10cSrcweir     ::rtl::OUString& rControlType,
219cdf0e10cSrcweir     sal_uInt16&      rWidth )
220cdf0e10cSrcweir {
221cdf0e10cSrcweir     for ( sal_Int32 i = 0; i < rSequence.getLength(); i++ )
222cdf0e10cSrcweir     {
223cdf0e10cSrcweir         if ( rSequence[i].Name.equalsAsciiL( MERGE_TOOLBAR_URL, MERGE_TOOLBAR_URL_LEN ))
224cdf0e10cSrcweir             rSequence[i].Value >>= rCommandURL;
225cdf0e10cSrcweir         else if ( rSequence[i].Name.equalsAsciiL( MERGE_TOOLBAR_TITLE, MERGE_TOOLBAR_TITLE_LEN ))
226cdf0e10cSrcweir             rSequence[i].Value >>= rLabel;
227cdf0e10cSrcweir         else if ( rSequence[i].Name.equalsAsciiL( MERGE_TOOLBAR_IMAGEID, MERGE_TOOLBAR_IMAGEID_LEN ))
228cdf0e10cSrcweir             rSequence[i].Value >>= rImageIdentifier;
229cdf0e10cSrcweir         else if ( rSequence[i].Name.equalsAsciiL( MERGE_TOOLBAR_CONTEXT, MERGE_TOOLBAR_CONTEXT_LEN ))
230cdf0e10cSrcweir             rSequence[i].Value >>= rContext;
231cdf0e10cSrcweir         else if ( rSequence[i].Name.equalsAsciiL( MERGE_TOOLBAR_TARGET, MERGE_TOOLBAR_TARGET_LEN ))
232cdf0e10cSrcweir             rSequence[i].Value >>= rTarget;
233cdf0e10cSrcweir         else if ( rSequence[i].Name.equalsAsciiL( MERGE_TOOLBAR_CONTROLTYPE, MERGE_TOOLBAR_CONTROLTYPE_LEN ))
234cdf0e10cSrcweir             rSequence[i].Value >>= rControlType;
235cdf0e10cSrcweir         else if ( rSequence[i].Name.equalsAsciiL( MERGE_TOOLBAR_WIDTH, MERGE_TOOLBAR_WIDTH_LEN ))
236cdf0e10cSrcweir         {
237cdf0e10cSrcweir             sal_Int32 aValue = 0;
238cdf0e10cSrcweir             rSequence[i].Value >>= aValue;
239cdf0e10cSrcweir             rWidth = sal_uInt16( aValue );
240cdf0e10cSrcweir         }
241cdf0e10cSrcweir     }
242cdf0e10cSrcweir }
243cdf0e10cSrcweir 
244cdf0e10cSrcweir /**
245cdf0e10cSrcweir  Tries to find the reference point provided and delivers
246cdf0e10cSrcweir  position and result of the search process.
247cdf0e10cSrcweir 
248cdf0e10cSrcweir  @param
249cdf0e10cSrcweir      pToolbar
250cdf0e10cSrcweir 
251cdf0e10cSrcweir      Must be a valid pointer to a toolbar with items which
252cdf0e10cSrcweir      should be searched.
253cdf0e10cSrcweir 
254cdf0e10cSrcweir  @param
255cdf0e10cSrcweir      rReferencePoint
256cdf0e10cSrcweir 
257cdf0e10cSrcweir      A command URL which should be the reference point for
258cdf0e10cSrcweir      the coming merge operation.
259cdf0e10cSrcweir 
260cdf0e10cSrcweir  @result
261cdf0e10cSrcweir      Provides information about the search result, the
262cdf0e10cSrcweir      position of the reference point and the toolbar used.
263cdf0e10cSrcweir */
264cdf0e10cSrcweir ReferenceToolbarPathInfo ToolBarMerger::FindReferencePoint(
265cdf0e10cSrcweir     ToolBox*               pToolbar,
266cdf0e10cSrcweir     const ::rtl::OUString& rReferencePoint )
267cdf0e10cSrcweir {
268cdf0e10cSrcweir     ReferenceToolbarPathInfo aResult;
269cdf0e10cSrcweir     aResult.bResult  = false;
270cdf0e10cSrcweir     aResult.pToolbar = pToolbar;
271cdf0e10cSrcweir     aResult.nPos     = TOOLBOX_ITEM_NOTFOUND;
272cdf0e10cSrcweir 
273cdf0e10cSrcweir     const sal_uInt16 nSize( pToolbar->GetItemCount() );
274cdf0e10cSrcweir 
275cdf0e10cSrcweir     for ( sal_uInt16 i = 0; i < nSize; i++ )
276cdf0e10cSrcweir     {
277cdf0e10cSrcweir         const sal_uInt16 nItemId = pToolbar->GetItemId( i );
278cdf0e10cSrcweir         if ( nItemId > 0 )
279cdf0e10cSrcweir         {
280cdf0e10cSrcweir             const ::rtl::OUString rCmd = pToolbar->GetItemCommand( nItemId );
281cdf0e10cSrcweir             if ( rCmd == rReferencePoint )
282cdf0e10cSrcweir             {
283cdf0e10cSrcweir                 aResult.bResult = true;
284cdf0e10cSrcweir                 aResult.nPos    = i;
285cdf0e10cSrcweir                 return aResult;
286cdf0e10cSrcweir             }
287cdf0e10cSrcweir         }
288cdf0e10cSrcweir     }
289cdf0e10cSrcweir 
290cdf0e10cSrcweir     return aResult;
291cdf0e10cSrcweir }
292cdf0e10cSrcweir 
293cdf0e10cSrcweir /**
294cdf0e10cSrcweir  Processes a merge operation.
295cdf0e10cSrcweir 
296cdf0e10cSrcweir  @param
297cdf0e10cSrcweir      xFrame
298cdf0e10cSrcweir 
299cdf0e10cSrcweir      Must be a valid reference to a frame.
300cdf0e10cSrcweir 
301cdf0e10cSrcweir  @param
302cdf0e10cSrcweir      pToolbar
303cdf0e10cSrcweir 
304cdf0e10cSrcweir      A valid pointer to the toolbar where the merge
305cdf0e10cSrcweir      operation is applied to.
306cdf0e10cSrcweir 
307cdf0e10cSrcweir  @param
308cdf0e10cSrcweir      nPos
309cdf0e10cSrcweir 
310cdf0e10cSrcweir      The reference position of the toolbar item for
311cdf0e10cSrcweir      the merge operation. Value must be between
312cdf0e10cSrcweir      0 and number of toolbar items - 1.
313cdf0e10cSrcweir 
314cdf0e10cSrcweir  @param
315cdf0e10cSrcweir      rItemId
316cdf0e10cSrcweir 
317cdf0e10cSrcweir      A unique item ID.
318cdf0e10cSrcweir 
319cdf0e10cSrcweir  @param
320cdf0e10cSrcweir      rModuleIdentifier
321cdf0e10cSrcweir 
322cdf0e10cSrcweir      The current application module context.
323cdf0e10cSrcweir 
324cdf0e10cSrcweir  @param
325cdf0e10cSrcweir      rMergeCommand
326cdf0e10cSrcweir 
327cdf0e10cSrcweir      A merge command.
328cdf0e10cSrcweir 
329cdf0e10cSrcweir  @param
330cdf0e10cSrcweir      rMergeCommandParameter.
331cdf0e10cSrcweir 
332cdf0e10cSrcweir      An optional argument for the merge command.
333cdf0e10cSrcweir 
334cdf0e10cSrcweir  @param
335cdf0e10cSrcweir      rItems
336cdf0e10cSrcweir 
337cdf0e10cSrcweir      Toolbar items which are associated to the merge
338cdf0e10cSrcweir      command.
339cdf0e10cSrcweir 
340cdf0e10cSrcweir  @result
341cdf0e10cSrcweir      Returns true for a successful operation otherwise
342cdf0e10cSrcweir      false.
343cdf0e10cSrcweir */
344cdf0e10cSrcweir bool ToolBarMerger::ProcessMergeOperation(
345cdf0e10cSrcweir     const uno::Reference< frame::XFrame >& xFrame,
346cdf0e10cSrcweir     ToolBox*                               pToolbar,
347cdf0e10cSrcweir     sal_uInt16                             nPos,
348cdf0e10cSrcweir     sal_uInt16&                            rItemId,
349cdf0e10cSrcweir     CommandToInfoMap&                      rCommandMap,
350cdf0e10cSrcweir     const ::rtl::OUString&                 rModuleIdentifier,
351cdf0e10cSrcweir     const ::rtl::OUString&                 rMergeCommand,
352cdf0e10cSrcweir     const ::rtl::OUString&                 rMergeCommandParameter,
353cdf0e10cSrcweir     const AddonToolbarItemContainer&       rItems )
354cdf0e10cSrcweir {
355cdf0e10cSrcweir     if ( rMergeCommand.equalsAsciiL( MERGECOMMAND_ADDAFTER, MERGECOMMAND_ADDAFTER_LEN ))
356cdf0e10cSrcweir         return MergeItems( xFrame, pToolbar, nPos, 1, rItemId, rCommandMap, rModuleIdentifier, rItems );
357cdf0e10cSrcweir     else if ( rMergeCommand.equalsAsciiL( MERGECOMMAND_ADDBEFORE, MERGECOMMAND_ADDBEFORE_LEN ))
358cdf0e10cSrcweir         return MergeItems( xFrame, pToolbar, nPos, 0, rItemId, rCommandMap, rModuleIdentifier, rItems );
359cdf0e10cSrcweir     else if ( rMergeCommand.equalsAsciiL( MERGECOMMAND_REPLACE, MERGECOMMAND_REPLACE_LEN ))
360cdf0e10cSrcweir         return ReplaceItem( xFrame, pToolbar, nPos, rItemId, rCommandMap, rModuleIdentifier, rItems );
361cdf0e10cSrcweir     else if ( rMergeCommand.equalsAsciiL( MERGECOMMAND_REMOVE, MERGECOMMAND_REMOVE_LEN ))
362cdf0e10cSrcweir         return RemoveItems( pToolbar, nPos, rMergeCommandParameter );
363cdf0e10cSrcweir 
364cdf0e10cSrcweir     return false;
365cdf0e10cSrcweir }
366cdf0e10cSrcweir 
367cdf0e10cSrcweir /**
368cdf0e10cSrcweir  Processes a merge fallback operation.
369cdf0e10cSrcweir 
370cdf0e10cSrcweir  @param
371cdf0e10cSrcweir      xFrame
372cdf0e10cSrcweir 
373cdf0e10cSrcweir      Must be a valid reference to a frame.
374cdf0e10cSrcweir 
375cdf0e10cSrcweir  @param
376cdf0e10cSrcweir      pToolbar
377cdf0e10cSrcweir 
378cdf0e10cSrcweir      A valid pointer to the toolbar where the merge
379cdf0e10cSrcweir      fall back operation is applied to.
380cdf0e10cSrcweir 
381cdf0e10cSrcweir  @param
382cdf0e10cSrcweir      nPos
383cdf0e10cSrcweir 
384cdf0e10cSrcweir      The reference position of the toolbar item for
385cdf0e10cSrcweir      the merge operation. Value must be between
386cdf0e10cSrcweir      0 and number of toolbar items - 1.
387cdf0e10cSrcweir 
388cdf0e10cSrcweir  @param
389cdf0e10cSrcweir      rItemId
390cdf0e10cSrcweir 
391cdf0e10cSrcweir      A unique item ID.
392cdf0e10cSrcweir 
393cdf0e10cSrcweir  @param
394cdf0e10cSrcweir      rModuleIdentifier
395cdf0e10cSrcweir 
396cdf0e10cSrcweir      The current application module context.
397cdf0e10cSrcweir 
398cdf0e10cSrcweir  @param
399cdf0e10cSrcweir      rMergeCommand
400cdf0e10cSrcweir 
401cdf0e10cSrcweir      A merge command.
402cdf0e10cSrcweir 
403cdf0e10cSrcweir  @param
404cdf0e10cSrcweir      rItems
405cdf0e10cSrcweir 
406cdf0e10cSrcweir      Toolbar items which are associated to the merge
407cdf0e10cSrcweir      command.
408cdf0e10cSrcweir 
409cdf0e10cSrcweir  @result
410cdf0e10cSrcweir      Returns true for a successful operation otherwise
411cdf0e10cSrcweir      false.
412cdf0e10cSrcweir */
413cdf0e10cSrcweir bool ToolBarMerger::ProcessMergeFallback(
414cdf0e10cSrcweir     const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XFrame >& xFrame,
415cdf0e10cSrcweir     ToolBox*                         pToolbar,
416cdf0e10cSrcweir     sal_uInt16                       /*nPos*/,
417cdf0e10cSrcweir     sal_uInt16&                      rItemId,
418cdf0e10cSrcweir     CommandToInfoMap&                rCommandMap,
419cdf0e10cSrcweir     const ::rtl::OUString&           rModuleIdentifier,
420cdf0e10cSrcweir     const ::rtl::OUString&           rMergeCommand,
421cdf0e10cSrcweir     const ::rtl::OUString&           rMergeFallback,
422cdf0e10cSrcweir     const AddonToolbarItemContainer& rItems )
423cdf0e10cSrcweir {
424cdf0e10cSrcweir     if (( rMergeFallback.equalsAsciiL( MERGEFALLBACK_IGNORE, MERGEFALLBACK_IGNORE_LEN )) ||
425cdf0e10cSrcweir         ( rMergeCommand.equalsAsciiL(  MERGECOMMAND_REPLACE, MERGECOMMAND_REPLACE_LEN )) ||
426cdf0e10cSrcweir         ( rMergeCommand.equalsAsciiL(  MERGECOMMAND_REMOVE, MERGECOMMAND_REMOVE_LEN   )) )
427cdf0e10cSrcweir     {
428cdf0e10cSrcweir         return true;
429cdf0e10cSrcweir     }
430cdf0e10cSrcweir     else if (( rMergeCommand.equalsAsciiL( MERGECOMMAND_ADDBEFORE, MERGECOMMAND_ADDBEFORE_LEN )) ||
431cdf0e10cSrcweir              ( rMergeCommand.equalsAsciiL( MERGECOMMAND_ADDAFTER, MERGECOMMAND_ADDAFTER_LEN   )) )
432cdf0e10cSrcweir     {
433cdf0e10cSrcweir         if ( rMergeFallback.equalsAsciiL( MERGEFALLBACK_ADDFIRST, MERGEFALLBACK_ADDFIRST_LEN ))
434cdf0e10cSrcweir             return MergeItems( xFrame, pToolbar, 0, 0, rItemId, rCommandMap, rModuleIdentifier, rItems );
435cdf0e10cSrcweir         else if ( rMergeFallback.equalsAsciiL( MERGEFALLBACK_ADDLAST, MERGEFALLBACK_ADDLAST_LEN ))
436cdf0e10cSrcweir             return MergeItems( xFrame, pToolbar, TOOLBOX_APPEND, 0, rItemId, rCommandMap, rModuleIdentifier, rItems );
437cdf0e10cSrcweir     }
438cdf0e10cSrcweir 
439cdf0e10cSrcweir     return false;
440cdf0e10cSrcweir }
441cdf0e10cSrcweir 
442cdf0e10cSrcweir /**
443cdf0e10cSrcweir  Merges (adds) toolbar items into an existing toolbar.
444cdf0e10cSrcweir 
445cdf0e10cSrcweir  @param
446cdf0e10cSrcweir      xFrame
447cdf0e10cSrcweir 
448cdf0e10cSrcweir      Must be a valid reference to a frame.
449cdf0e10cSrcweir 
450cdf0e10cSrcweir  @param
451cdf0e10cSrcweir      pToolbar
452cdf0e10cSrcweir 
453cdf0e10cSrcweir      A valid pointer to the toolbar where the merge
454cdf0e10cSrcweir      fall back operation is applied to.
455cdf0e10cSrcweir 
456cdf0e10cSrcweir  @param
457cdf0e10cSrcweir      nPos
458cdf0e10cSrcweir 
459cdf0e10cSrcweir      The reference position of the toolbar item for
460cdf0e10cSrcweir      the merge operation. Value must be between
461cdf0e10cSrcweir      0 and number of toolbar items - 1.
462cdf0e10cSrcweir 
463cdf0e10cSrcweir  @param
464cdf0e10cSrcweir      rItemId
465cdf0e10cSrcweir 
466cdf0e10cSrcweir      A unique item ID.
467cdf0e10cSrcweir 
468cdf0e10cSrcweir  @param
469cdf0e10cSrcweir      rModuleIdentifier
470cdf0e10cSrcweir 
471cdf0e10cSrcweir      The current application module context.
472cdf0e10cSrcweir 
473cdf0e10cSrcweir  @param
474cdf0e10cSrcweir      rItems
475cdf0e10cSrcweir 
476cdf0e10cSrcweir      Toolbar items which are associated to the merge
477cdf0e10cSrcweir      command.
478cdf0e10cSrcweir 
479cdf0e10cSrcweir  @result
480cdf0e10cSrcweir      Returns true for a successful operation otherwise
481cdf0e10cSrcweir      false.
482cdf0e10cSrcweir */
483cdf0e10cSrcweir bool ToolBarMerger::MergeItems(
484cdf0e10cSrcweir     const uno::Reference< frame::XFrame >& rFrame,
485cdf0e10cSrcweir     ToolBox*                               pToolbar,
486cdf0e10cSrcweir     sal_uInt16                             nPos,
487cdf0e10cSrcweir     sal_uInt16                             nModIndex,
488cdf0e10cSrcweir     sal_uInt16&                            rItemId,
489cdf0e10cSrcweir     CommandToInfoMap&                      rCommandMap,
490cdf0e10cSrcweir     const ::rtl::OUString&                 rModuleIdentifier,
491cdf0e10cSrcweir     const AddonToolbarItemContainer&       rAddonToolbarItems )
492cdf0e10cSrcweir {
493cdf0e10cSrcweir     const sal_Int32 nSize( rAddonToolbarItems.size() );
494cdf0e10cSrcweir 
495cdf0e10cSrcweir     uno::Reference< frame::XFrame > xFrame( rFrame );
496cdf0e10cSrcweir 
497cdf0e10cSrcweir     sal_uInt16 nIndex( 0 );
498cdf0e10cSrcweir     for ( sal_Int32 i = 0; i < nSize; i++ )
499cdf0e10cSrcweir     {
500cdf0e10cSrcweir         const AddonToolbarItem& rItem = rAddonToolbarItems[i];
501cdf0e10cSrcweir         if ( IsCorrectContext( rItem.aContext, rModuleIdentifier ))
502cdf0e10cSrcweir         {
503cdf0e10cSrcweir             sal_Int32 nInsPos = nPos+nModIndex+i;
504cdf0e10cSrcweir             if ( nInsPos > sal_Int32( pToolbar->GetItemCount() ))
505cdf0e10cSrcweir                 nInsPos = TOOLBOX_APPEND;
506cdf0e10cSrcweir 
507cdf0e10cSrcweir             if ( rItem.aCommandURL.equalsAsciiL( TOOLBOXITEM_SEPARATOR_STR, TOOLBOXITEM_SEPARATOR_STR_LEN ))
508cdf0e10cSrcweir                 pToolbar->InsertSeparator( sal_uInt16( nInsPos ));
509cdf0e10cSrcweir             else
510cdf0e10cSrcweir             {
511cdf0e10cSrcweir                 CommandToInfoMap::iterator pIter = rCommandMap.find( rItem.aCommandURL );
512cdf0e10cSrcweir                 if ( pIter == rCommandMap.end())
513cdf0e10cSrcweir                 {
514cdf0e10cSrcweir                     CommandInfo aCmdInfo;
515cdf0e10cSrcweir                     aCmdInfo.nId = rItemId;
516cdf0e10cSrcweir                     rCommandMap.insert( CommandToInfoMap::value_type( rItem.aCommandURL, aCmdInfo ));
517cdf0e10cSrcweir                 }
518cdf0e10cSrcweir                 else
519cdf0e10cSrcweir                 {
520cdf0e10cSrcweir                     pIter->second.aIds.push_back( rItemId );
521cdf0e10cSrcweir                 }
522cdf0e10cSrcweir 
523cdf0e10cSrcweir                 ToolBarMerger::CreateToolbarItem( pToolbar, rCommandMap, sal_uInt16( nInsPos ), rItemId, rItem );
524cdf0e10cSrcweir             }
525cdf0e10cSrcweir 
526cdf0e10cSrcweir             ++nIndex;
527cdf0e10cSrcweir             ++rItemId;
528cdf0e10cSrcweir         }
529cdf0e10cSrcweir     }
530cdf0e10cSrcweir 
531cdf0e10cSrcweir     return true;
532cdf0e10cSrcweir }
533cdf0e10cSrcweir 
534cdf0e10cSrcweir /**
535cdf0e10cSrcweir  Replaces a toolbar item with new items for an
536cdf0e10cSrcweir  existing toolbar.
537cdf0e10cSrcweir 
538cdf0e10cSrcweir  @param
539cdf0e10cSrcweir      xFrame
540cdf0e10cSrcweir 
541cdf0e10cSrcweir      Must be a valid reference to a frame.
542cdf0e10cSrcweir 
543cdf0e10cSrcweir  @param
544cdf0e10cSrcweir      pToolbar
545cdf0e10cSrcweir 
546cdf0e10cSrcweir      A valid pointer to the toolbar where the merge
547cdf0e10cSrcweir      fall back operation is applied to.
548cdf0e10cSrcweir 
549cdf0e10cSrcweir  @param
550cdf0e10cSrcweir      nPos
551cdf0e10cSrcweir 
552cdf0e10cSrcweir      The reference position of the toolbar item for
553cdf0e10cSrcweir      the merge operation. Value must be between
554cdf0e10cSrcweir      0 and number of toolbar items - 1.
555cdf0e10cSrcweir 
556cdf0e10cSrcweir  @param
557cdf0e10cSrcweir      rItemId
558cdf0e10cSrcweir 
559cdf0e10cSrcweir      A unique item ID.
560cdf0e10cSrcweir 
561cdf0e10cSrcweir  @param
562cdf0e10cSrcweir      rModuleIdentifier
563cdf0e10cSrcweir 
564cdf0e10cSrcweir      The current application module context.
565cdf0e10cSrcweir 
566cdf0e10cSrcweir  @param
567cdf0e10cSrcweir      rItems
568cdf0e10cSrcweir 
569cdf0e10cSrcweir      Toolbar items which are associated to the merge
570cdf0e10cSrcweir      command.
571cdf0e10cSrcweir 
572cdf0e10cSrcweir  @result
573cdf0e10cSrcweir      Returns true for a successful operation otherwise
574cdf0e10cSrcweir      false.
575cdf0e10cSrcweir */
576cdf0e10cSrcweir bool ToolBarMerger::ReplaceItem(
577cdf0e10cSrcweir     const uno::Reference< frame::XFrame >& xFrame,
578cdf0e10cSrcweir     ToolBox*                               pToolbar,
579cdf0e10cSrcweir     sal_uInt16                             nPos,
580cdf0e10cSrcweir     sal_uInt16&                            rItemId,
581cdf0e10cSrcweir     CommandToInfoMap&                      rCommandMap,
582cdf0e10cSrcweir     const ::rtl::OUString&                 rModuleIdentifier,
583cdf0e10cSrcweir     const AddonToolbarItemContainer&       rAddonToolbarItems )
584cdf0e10cSrcweir {
585cdf0e10cSrcweir     pToolbar->RemoveItem( nPos );
586cdf0e10cSrcweir     return MergeItems( xFrame, pToolbar, nPos, 0, rItemId, rCommandMap, rModuleIdentifier, rAddonToolbarItems );
587cdf0e10cSrcweir }
588cdf0e10cSrcweir 
589cdf0e10cSrcweir /**
590cdf0e10cSrcweir  Removes toolbar items from an existing toolbar.
591cdf0e10cSrcweir 
592cdf0e10cSrcweir  @param
593cdf0e10cSrcweir      pToolbar
594cdf0e10cSrcweir 
595cdf0e10cSrcweir      A valid pointer to the toolbar where the merge
596cdf0e10cSrcweir      fall back operation is applied to.
597cdf0e10cSrcweir 
598cdf0e10cSrcweir  @param
599cdf0e10cSrcweir      nPos
600cdf0e10cSrcweir 
601cdf0e10cSrcweir      The reference position of the toolbar item for
602cdf0e10cSrcweir      the merge operation. Value must be between
603cdf0e10cSrcweir      0 and number of toolbar items - 1.
604cdf0e10cSrcweir 
605cdf0e10cSrcweir  @param
606cdf0e10cSrcweir      rMergeCommandParameter.
607cdf0e10cSrcweir 
608cdf0e10cSrcweir      An optional argument for the merge command.
609cdf0e10cSrcweir 
610cdf0e10cSrcweir  @result
611cdf0e10cSrcweir      Returns true for a successful operation otherwise
612cdf0e10cSrcweir      false.
613cdf0e10cSrcweir */
614cdf0e10cSrcweir bool ToolBarMerger::RemoveItems(
615cdf0e10cSrcweir     ToolBox*                  pToolbar,
616cdf0e10cSrcweir     sal_uInt16                nPos,
617cdf0e10cSrcweir     const ::rtl::OUString&    rMergeCommandParameter )
618cdf0e10cSrcweir {
619cdf0e10cSrcweir     sal_Int32 nCount = rMergeCommandParameter.toInt32();
620cdf0e10cSrcweir     if ( nCount > 0 )
621cdf0e10cSrcweir     {
622cdf0e10cSrcweir         for ( sal_Int32 i = 0; i < nCount; i++ )
623cdf0e10cSrcweir         {
624cdf0e10cSrcweir             if ( nPos < pToolbar->GetItemCount() )
625cdf0e10cSrcweir                 pToolbar->RemoveItem( nPos );
626cdf0e10cSrcweir         }
627cdf0e10cSrcweir     }
628cdf0e10cSrcweir     return true;
629cdf0e10cSrcweir }
630cdf0e10cSrcweir 
631cdf0e10cSrcweir /**
632cdf0e10cSrcweir  Removes toolbar items from an existing toolbar.
633cdf0e10cSrcweir 
634cdf0e10cSrcweir  @param
635cdf0e10cSrcweir      pToolbar
636cdf0e10cSrcweir 
637cdf0e10cSrcweir      A valid pointer to the toolbar where the merge
638cdf0e10cSrcweir      fall back operation is applied to.
639cdf0e10cSrcweir 
640cdf0e10cSrcweir  @param
641cdf0e10cSrcweir      nPos
642cdf0e10cSrcweir 
643cdf0e10cSrcweir      The reference position of the toolbar item for
644cdf0e10cSrcweir      the merge operation. Value must be between
645cdf0e10cSrcweir      0 and number of toolbar items - 1.
646cdf0e10cSrcweir 
647cdf0e10cSrcweir  @param
648cdf0e10cSrcweir      rMergeCommandParameter.
649cdf0e10cSrcweir 
650cdf0e10cSrcweir      An optional argument for the merge command.
651cdf0e10cSrcweir 
652cdf0e10cSrcweir  @result
653cdf0e10cSrcweir      Returns true for a successful operation otherwise
654cdf0e10cSrcweir      false.
655cdf0e10cSrcweir */
656cdf0e10cSrcweir ::cppu::OWeakObject* ToolBarMerger::CreateController(
657cdf0e10cSrcweir     uno::Reference< lang::XMultiServiceFactory > xSMGR,
658cdf0e10cSrcweir     uno::Reference< frame::XFrame > xFrame,
659cdf0e10cSrcweir     ToolBox*               pToolbar,
660cdf0e10cSrcweir     const ::rtl::OUString& rCommandURL,
661cdf0e10cSrcweir     sal_uInt16             nId,
662cdf0e10cSrcweir     sal_uInt16             nWidth,
663cdf0e10cSrcweir     const ::rtl::OUString& rControlType )
664cdf0e10cSrcweir {
665cdf0e10cSrcweir     ::cppu::OWeakObject* pResult( 0 );
666cdf0e10cSrcweir 
667cdf0e10cSrcweir     if ( rControlType.equalsAsciiL( TOOLBARCONTROLLER_BUTTON, TOOLBARCONTROLLER_BUTTON_LEN ))
668cdf0e10cSrcweir         pResult = new ButtonToolbarController( xSMGR, pToolbar, rCommandURL );
669cdf0e10cSrcweir     else if ( rControlType.equalsAsciiL( TOOLBARCONTROLLER_COMBOBOX, TOOLBARCONTROLLER_COMBOBOX_LEN ))
670cdf0e10cSrcweir         pResult = new ComboboxToolbarController( xSMGR, xFrame, pToolbar, nId, nWidth, rCommandURL );
671cdf0e10cSrcweir     else if ( rControlType.equalsAsciiL( TOOLBARCONTROLLER_EDIT, TOOLBARCONTROLLER_EDIT_LEN ))
672cdf0e10cSrcweir         pResult = new EditToolbarController( xSMGR, xFrame, pToolbar, nId, nWidth, rCommandURL );
673cdf0e10cSrcweir     else if ( rControlType.equalsAsciiL( TOOLBARCONTROLLER_SPINFIELD, TOOLBARCONTROLLER_SPINFIELD_LEN ))
674cdf0e10cSrcweir         pResult = new SpinfieldToolbarController( xSMGR, xFrame, pToolbar, nId, nWidth, rCommandURL );
675cdf0e10cSrcweir     else if ( rControlType.equalsAsciiL( TOOLBARCONTROLLER_IMGBUTTON, TOOLBARCONTROLLER_IMGBUTTON_LEN ))
676cdf0e10cSrcweir         pResult = new ImageButtonToolbarController( xSMGR, xFrame, pToolbar, nId, rCommandURL );
677cdf0e10cSrcweir     else if ( rControlType.equalsAsciiL( TOOLBARCONTROLLER_DROPDOWNBOX, TOOLBARCONTROLLER_DROPDOWNBOX_LEN ))
678cdf0e10cSrcweir         pResult = new DropdownToolbarController( xSMGR, xFrame, pToolbar, nId, nWidth, rCommandURL );
679cdf0e10cSrcweir     else if ( rControlType.equalsAsciiL( TOOLBARCONTROLLER_DROPDOWNBTN, TOOLBARCONTROLLER_DROPDOWNBTN_LEN ))
680cdf0e10cSrcweir         pResult = new ToggleButtonToolbarController( xSMGR, xFrame, pToolbar, nId,
681cdf0e10cSrcweir                                                      ToggleButtonToolbarController::STYLE_DROPDOWNBUTTON, rCommandURL );
682cdf0e10cSrcweir     else if ( rControlType.equalsAsciiL( TOOLBARCONTROLLER_TOGGLEDDBTN, TOOLBARCONTROLLER_TOGGLEDDBTN_LEN ))
683cdf0e10cSrcweir         pResult = new ToggleButtonToolbarController( xSMGR, xFrame, pToolbar, nId,
684cdf0e10cSrcweir                                                      ToggleButtonToolbarController::STYLE_TOGGLE_DROPDOWNBUTTON, rCommandURL );
685cdf0e10cSrcweir     else
686cdf0e10cSrcweir         pResult = new GenericToolbarController( xSMGR, xFrame, pToolbar, nId, rCommandURL );
687cdf0e10cSrcweir 
688cdf0e10cSrcweir     return pResult;
689cdf0e10cSrcweir }
690cdf0e10cSrcweir 
691cdf0e10cSrcweir void ToolBarMerger::CreateToolbarItem( ToolBox* pToolbar, CommandToInfoMap& rCommandMap, sal_uInt16 nPos, sal_uInt16 nItemId, const AddonToolbarItem& rItem )
692cdf0e10cSrcweir {
693cdf0e10cSrcweir     pToolbar->InsertItem( nItemId, rItem.aLabel, 0, nPos );
694cdf0e10cSrcweir     pToolbar->SetItemCommand( nItemId, rItem.aCommandURL );
695cdf0e10cSrcweir     pToolbar->SetQuickHelpText( nItemId, rItem.aLabel );
696cdf0e10cSrcweir     pToolbar->SetItemText( nItemId, rItem.aLabel );
697cdf0e10cSrcweir     pToolbar->EnableItem( nItemId, sal_True );
698cdf0e10cSrcweir     pToolbar->SetItemState( nItemId, STATE_NOCHECK );
699cdf0e10cSrcweir 
700cdf0e10cSrcweir     CommandToInfoMap::iterator pIter = rCommandMap.find( rItem.aCommandURL );
701cdf0e10cSrcweir     if ( pIter != rCommandMap.end() )
702cdf0e10cSrcweir         pIter->second.nWidth = rItem.nWidth;
703cdf0e10cSrcweir 
704cdf0e10cSrcweir     // Use the user data to store add-on specific data with the toolbar item
705cdf0e10cSrcweir     AddonsParams* pAddonParams = new AddonsParams;
706cdf0e10cSrcweir     pAddonParams->aImageId     = rItem.aImageIdentifier;
707cdf0e10cSrcweir     pAddonParams->aTarget      = rItem.aTarget;
708cdf0e10cSrcweir     pAddonParams->aControlType = rItem.aControlType;
709cdf0e10cSrcweir     pToolbar->SetItemData( nItemId, pAddonParams );
710cdf0e10cSrcweir }
711cdf0e10cSrcweir 
712cdf0e10cSrcweir } // namespace framework
713