Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 : #include <uielement/statusbarmerger.hxx>
20 :
21 : using rtl::OUString;
22 : using com::sun::star::frame::XFrame;
23 : using com::sun::star::uno::Reference;
24 : using com::sun::star::beans::PropertyValue;
25 : using com::sun::star::uno::Sequence;
26 :
27 : namespace framework
28 : {
29 : namespace {
30 :
31 : static const char MERGE_STATUSBAR_URL[] = "URL";
32 : static const char MERGE_STATUSBAR_TITLE[] = "Title";
33 : static const char MERGE_STATUSBAR_CONTEXT[] = "Context";
34 : static const char MERGE_STATUSBAR_ALIGN[] = "Alignment";
35 : static const char MERGE_STATUSBAR_AUTOSIZE[] = "AutoSize";
36 : static const char MERGE_STATUSBAR_OWNERDRAW[] = "OwnerDraw";
37 : static const char MERGE_STATUSBAR_WIDTH[] = "Width";
38 :
39 : static const char STATUSBAR_ALIGN_CENTER[] = "center";
40 : static const char STATUSBAR_ALIGN_RIGHT[] = "right";
41 :
42 : static const char MERGECOMMAND_ADDAFTER[] = "AddAfter";
43 : static const char MERGECOMMAND_ADDBEFORE[] = "AddBefore";
44 : static const char MERGECOMMAND_REPLACE[] = "Replace";
45 : static const char MERGECOMMAND_REMOVE[] = "Remove";
46 :
47 : static const char MERGEFALLBACK_ADDLAST[] = "AddLast";
48 : static const char MERGEFALLBACK_ADDFIRST[] = "AddFirst";
49 : static const char MERGEFALLBACK_IGNORE[] = "Ignore";
50 :
51 0 : static void lcl_ConvertSequenceToValues(
52 : const Sequence< PropertyValue > &rSequence,
53 : AddonStatusbarItem &rItem )
54 : {
55 0 : OUString sAlignment;
56 0 : bool bAutoSize = false;
57 0 : bool bOwnerDraw = false;
58 :
59 0 : PropertyValue aPropVal;
60 0 : for ( sal_Int32 i = 0; i < rSequence.getLength(); i++ )
61 : {
62 0 : aPropVal = rSequence[i];
63 0 : if ( aPropVal.Name == MERGE_STATUSBAR_URL )
64 0 : aPropVal.Value >>= rItem.aCommandURL;
65 0 : else if ( aPropVal.Name == MERGE_STATUSBAR_TITLE )
66 0 : aPropVal.Value >>= rItem.aLabel;
67 0 : else if ( aPropVal.Name == MERGE_STATUSBAR_CONTEXT )
68 0 : aPropVal.Value >>= rItem.aContext;
69 0 : else if ( aPropVal.Name == MERGE_STATUSBAR_ALIGN )
70 0 : aPropVal.Value >>= sAlignment;
71 0 : else if ( aPropVal.Name == MERGE_STATUSBAR_AUTOSIZE )
72 0 : aPropVal.Value >>= bAutoSize;
73 0 : else if ( aPropVal.Name == MERGE_STATUSBAR_OWNERDRAW )
74 0 : aPropVal.Value >>= bOwnerDraw;
75 0 : else if ( aPropVal.Name == MERGE_STATUSBAR_WIDTH )
76 : {
77 0 : sal_Int32 aWidth = 0;
78 0 : aPropVal.Value >>= aWidth;
79 0 : rItem.nWidth = sal_uInt16( aWidth );
80 : }
81 : }
82 :
83 0 : sal_uInt16 nItemBits(0);
84 0 : if ( bAutoSize )
85 0 : nItemBits |= SIB_AUTOSIZE;
86 0 : if ( bOwnerDraw )
87 0 : nItemBits |= SIB_USERDRAW;
88 0 : if ( sAlignment == STATUSBAR_ALIGN_CENTER )
89 0 : nItemBits |= SIB_CENTER;
90 0 : else if ( sAlignment == STATUSBAR_ALIGN_RIGHT )
91 0 : nItemBits |= SIB_RIGHT;
92 : else
93 : // if unset, defaults to left alignment
94 0 : nItemBits |= SIB_LEFT;
95 0 : rItem.nItemBits = nItemBits;
96 0 : }
97 :
98 0 : static void lcl_CreateStatusbarItem( StatusBar* pStatusbar,
99 : sal_uInt16 nPos,
100 : sal_uInt16 nItemId,
101 : const AddonStatusbarItem& rAddonItem )
102 : {
103 : pStatusbar->InsertItem( nItemId,
104 : rAddonItem.nWidth,
105 : rAddonItem.nItemBits,
106 : STATUSBAR_OFFSET,
107 0 : nPos );
108 0 : pStatusbar->SetItemCommand( nItemId, rAddonItem.aCommandURL );
109 0 : pStatusbar->SetQuickHelpText( nItemId, rAddonItem.aLabel );
110 0 : pStatusbar->SetAccessibleName( nItemId, rAddonItem.aLabel );
111 :
112 : // add-on specific data
113 0 : AddonStatusbarItemData *pUserData = new AddonStatusbarItemData;
114 0 : pUserData->aLabel = rAddonItem.aLabel;
115 0 : pUserData->nItemBits = rAddonItem.nItemBits;
116 0 : pStatusbar->SetItemData( nItemId, pUserData );
117 0 : }
118 :
119 0 : static bool lcl_MergeItems( StatusBar* pStatusbar,
120 : sal_uInt16 nPos,
121 : sal_uInt16 nModIndex,
122 : sal_uInt16& rItemId,
123 : const ::rtl::OUString& rModuleIdentifier,
124 : const AddonStatusbarItemContainer& rAddonItems )
125 : {
126 0 : const sal_uInt16 nSize( rAddonItems.size() );
127 0 : for ( sal_Int32 i = 0; i < nSize; i++ )
128 : {
129 0 : const AddonStatusbarItem& rItem = rAddonItems[i];
130 0 : if ( !StatusbarMerger::IsCorrectContext( rItem.aContext, rModuleIdentifier ) )
131 0 : continue;
132 :
133 0 : sal_uInt16 nInsPos = nPos + nModIndex + i;
134 0 : if ( nInsPos > pStatusbar->GetItemCount() )
135 0 : nInsPos = STATUSBAR_APPEND;
136 :
137 0 : lcl_CreateStatusbarItem( pStatusbar, nInsPos, rItemId, rItem );
138 0 : ++rItemId;
139 : }
140 :
141 0 : return true;
142 : }
143 :
144 0 : static bool lcl_ReplaceItem( StatusBar* pStatusbar,
145 : sal_uInt16 nPos,
146 : sal_uInt16& rItemId,
147 : const ::rtl::OUString& rModuleIdentifier,
148 : const AddonStatusbarItemContainer& rAddonToolbarItems )
149 : {
150 0 : pStatusbar->RemoveItem( pStatusbar->GetItemId( nPos ) );
151 0 : return lcl_MergeItems( pStatusbar, nPos, 0, rItemId, rModuleIdentifier, rAddonToolbarItems );
152 : }
153 :
154 0 : static bool lcl_RemoveItems( StatusBar* pStatusbar,
155 : sal_uInt16 nPos,
156 : const ::rtl::OUString& rMergeCommandParameter )
157 : {
158 0 : sal_Int32 nCount = rMergeCommandParameter.toInt32();
159 0 : if ( nCount > 0 )
160 : {
161 0 : for ( sal_Int32 i = 0; i < nCount; i++ )
162 : {
163 0 : if ( nPos < pStatusbar->GetItemCount() )
164 0 : pStatusbar->RemoveItem( nPos );
165 : }
166 : }
167 0 : return true;
168 : }
169 :
170 : }
171 :
172 0 : bool StatusbarMerger::IsCorrectContext(
173 : const OUString& rContext,
174 : const OUString& rModuleIdentifier )
175 : {
176 0 : return (( rContext.getLength() == 0 ) || ( rContext.indexOf( rModuleIdentifier ) >= 0 ));
177 : }
178 :
179 0 : bool StatusbarMerger::ConvertSeqSeqToVector(
180 : const Sequence< Sequence< PropertyValue > > &rSequence,
181 : AddonStatusbarItemContainer& rContainer )
182 : {
183 0 : for ( sal_Int32 i = 0; i < rSequence.getLength(); i++ )
184 : {
185 0 : AddonStatusbarItem aStatusBarItem;
186 0 : lcl_ConvertSequenceToValues( rSequence[i], aStatusBarItem );
187 0 : rContainer.push_back( aStatusBarItem );
188 0 : }
189 :
190 0 : return true;
191 : }
192 :
193 0 : sal_uInt16 StatusbarMerger::FindReferencePos(
194 : StatusBar* pStatusbar,
195 : const OUString& rReferencePoint )
196 : {
197 0 : for ( sal_uInt16 nPos = 0; nPos < pStatusbar->GetItemCount(); nPos++ )
198 : {
199 0 : const ::rtl::OUString rCmd = pStatusbar->GetItemCommand( pStatusbar->GetItemId( nPos ) );
200 0 : if ( rReferencePoint == rCmd )
201 0 : return nPos;
202 0 : }
203 :
204 0 : return STATUSBAR_ITEM_NOTFOUND;
205 : }
206 :
207 0 : bool StatusbarMerger::ProcessMergeOperation(
208 : StatusBar* pStatusbar,
209 : sal_uInt16 nPos,
210 : sal_uInt16& rItemId,
211 : const ::rtl::OUString& rModuleIdentifier,
212 : const ::rtl::OUString& rMergeCommand,
213 : const ::rtl::OUString& rMergeCommandParameter,
214 : const AddonStatusbarItemContainer& rItems )
215 : {
216 0 : if ( rMergeCommand == MERGECOMMAND_ADDAFTER )
217 0 : return lcl_MergeItems( pStatusbar, nPos, 1, rItemId, rModuleIdentifier, rItems );
218 0 : else if ( rMergeCommand == MERGECOMMAND_ADDBEFORE )
219 0 : return lcl_MergeItems( pStatusbar, nPos, 0, rItemId, rModuleIdentifier, rItems );
220 0 : else if ( rMergeCommand == MERGECOMMAND_REPLACE )
221 0 : return lcl_ReplaceItem( pStatusbar, nPos, rItemId, rModuleIdentifier, rItems );
222 0 : else if ( rMergeCommand == MERGECOMMAND_REMOVE )
223 0 : return lcl_RemoveItems( pStatusbar, nPos, rMergeCommandParameter );
224 :
225 0 : return false;
226 : }
227 :
228 0 : bool StatusbarMerger::ProcessMergeFallback(
229 : StatusBar* pStatusbar,
230 : sal_uInt16 /*nPos*/,
231 : sal_uInt16& rItemId,
232 : const ::rtl::OUString& rModuleIdentifier,
233 : const ::rtl::OUString& rMergeCommand,
234 : const ::rtl::OUString& rMergeFallback,
235 : const AddonStatusbarItemContainer& rItems )
236 : {
237 : // fallback IGNORE or REPLACE/REMOVE item not found
238 0 : if (( rMergeFallback == MERGEFALLBACK_IGNORE ) ||
239 0 : ( rMergeCommand == MERGECOMMAND_REPLACE ) ||
240 0 : ( rMergeCommand == MERGECOMMAND_REMOVE ) )
241 : {
242 0 : return true;
243 : }
244 0 : else if (( rMergeCommand == MERGECOMMAND_ADDBEFORE ) ||
245 0 : ( rMergeCommand == MERGECOMMAND_ADDAFTER ) )
246 : {
247 0 : if ( rMergeFallback == MERGEFALLBACK_ADDFIRST )
248 0 : return lcl_MergeItems( pStatusbar, 0, 0, rItemId, rModuleIdentifier, rItems );
249 0 : else if ( rMergeFallback == MERGEFALLBACK_ADDLAST )
250 0 : return lcl_MergeItems( pStatusbar, STATUSBAR_APPEND, 0, rItemId, rModuleIdentifier, rItems );
251 : }
252 :
253 0 : return false;
254 : }
255 :
256 951 : }
257 :
258 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|