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 :
20 :
21 : #include "taskpane/ControlContainer.hxx"
22 :
23 : #include "taskpane/TaskPaneTreeNode.hxx"
24 :
25 : #include <vcl/window.hxx>
26 : #include <vcl/svapp.hxx>
27 :
28 : namespace sd { namespace toolpanel {
29 :
30 :
31 0 : ControlContainer::ControlContainer (TreeNode* pNode)
32 : : mpNode(pNode),
33 : mnActiveControlIndex((sal_uInt32)-1),
34 0 : mbMultiSelection(false)
35 : {
36 0 : }
37 :
38 :
39 :
40 :
41 0 : ControlContainer::~ControlContainer (void)
42 : {
43 : // Set mpNode to NULL so that no one calls it from now on.
44 0 : mpNode = NULL;
45 0 : DeleteChildren();
46 0 : }
47 :
48 :
49 :
50 :
51 0 : void ControlContainer::DeleteChildren (void)
52 : {
53 : // Deleting the children may lead to calls back to the container. To
54 : // prevent the container from accessing the just deleted children, the
55 : // maControlList member is first cleared (by transferring its content to
56 : // a local list) before the children are destroyed.
57 0 : ControlList maList;
58 0 : maList.swap(maControlList);
59 0 : ControlList::iterator I;
60 0 : ControlList::iterator Iend (maList.end());
61 0 : for (I=maList.begin(); I!=Iend; ++I)
62 0 : delete *I;
63 :
64 0 : if (mpNode != NULL)
65 0 : mpNode->FireStateChangeEvent(EID_ALL_CHILDREN_REMOVED);
66 0 : }
67 :
68 :
69 :
70 0 : sal_uInt32 ControlContainer::AddControl (::std::auto_ptr<TreeNode> pControl)
71 : {
72 0 : ::osl::MutexGuard aGuard (maMutex);
73 :
74 0 : pControl->GetWindow()->Show();
75 0 : sal_uInt32 nIndex = maControlList.size();
76 0 : maControlList.push_back (pControl.get());
77 0 : pControl.release();
78 :
79 0 : ListHasChanged ();
80 :
81 0 : if (mpNode != NULL)
82 0 : mpNode->FireStateChangeEvent(EID_CHILD_ADDED, pControl.get());
83 :
84 0 : return nIndex;
85 : }
86 :
87 :
88 :
89 :
90 0 : void ControlContainer::SetExpansionState (
91 : sal_uInt32 nIndex,
92 : ExpansionState aState)
93 : {
94 0 : ::osl::MutexGuard aGuard (maMutex);
95 :
96 0 : bool bResizeNecessary (false);
97 :
98 0 : if (mbMultiSelection)
99 : {
100 0 : TreeNode* pControl = GetControl(nIndex);
101 0 : switch (aState)
102 : {
103 : case ES_TOGGLE:
104 0 : bResizeNecessary = pControl->Expand( ! pControl->IsExpanded());
105 0 : break;
106 :
107 : case ES_EXPAND:
108 0 : bResizeNecessary = pControl->Expand(true);
109 0 : break;
110 :
111 : case ES_COLLAPSE:
112 0 : bResizeNecessary = pControl->Expand(false);
113 0 : break;
114 : }
115 : }
116 : else
117 : {
118 : // When bExpansionState is true then the control to expand is the
119 : // one with the given index. If bExpansionState is false and the
120 : // given index points to the active control then then following
121 : // control (in cyclic order) it is expanded. When there is only one
122 : // control then that is always expanded.
123 : do
124 : {
125 : // Ignore a call with an invalid index. (The seperate comparison
126 : // with -1 is not strictly necessary but it is here just in
127 : // case.)
128 0 : if (nIndex>=GetControlCount() || nIndex==(sal_uInt32)-1)
129 0 : break;
130 :
131 : bool bExpand;
132 0 : switch (aState)
133 : {
134 : default:
135 : case ES_TOGGLE:
136 0 : bExpand = ! GetControl(nIndex)->IsExpanded();
137 0 : break;
138 :
139 : case ES_EXPAND:
140 0 : bExpand = true;
141 0 : break;
142 :
143 : case ES_COLLAPSE:
144 0 : bExpand = false;
145 0 : break;
146 : }
147 0 : if (bExpand)
148 : {
149 : // Make the specified control the active one and expand it.
150 0 : mnActiveControlIndex = nIndex;
151 : }
152 : else
153 : {
154 0 : if (nIndex == mnActiveControlIndex)
155 : {
156 : // We have to determine a new active control since the
157 : // current one is about to be collapsed. Choose the
158 : // previous one for the last and the next one for all
159 : // other.
160 0 : if (mnActiveControlIndex+1 == GetControlCount())
161 : mnActiveControlIndex
162 0 : = GetPreviousIndex(mnActiveControlIndex);
163 : else
164 : mnActiveControlIndex
165 0 : = GetNextIndex (mnActiveControlIndex);
166 : }
167 : }
168 :
169 : // Update the expansion state of all controls.
170 0 : for (sal_uInt32 i=0; i<GetControlCount(); i=GetNextIndex(i))
171 : {
172 0 : TreeNode* pControl = GetControl(i);
173 0 : bResizeNecessary |= pControl->Expand(i == mnActiveControlIndex);
174 : }
175 : }
176 : while (false);
177 : }
178 :
179 0 : if (bResizeNecessary && mpNode != NULL)
180 0 : mpNode->RequestResize();
181 0 : }
182 :
183 :
184 :
185 :
186 0 : void ControlContainer::SetExpansionState (
187 : TreeNode* pControl,
188 : ExpansionState aState)
189 : {
190 0 : SetExpansionState (GetControlIndex(pControl), aState);
191 0 : }
192 :
193 :
194 :
195 :
196 0 : sal_uInt32 ControlContainer::GetControlIndex (TreeNode* pControlToExpand) const
197 : {
198 : sal_uInt32 nIndex;
199 0 : for (nIndex=0; nIndex<GetControlCount(); nIndex++)
200 : {
201 0 : TreeNode* pControl = GetControl(nIndex);
202 0 : if (pControl == pControlToExpand)
203 0 : break;
204 : }
205 0 : return nIndex;
206 : }
207 :
208 :
209 :
210 :
211 0 : void ControlContainer::ListHasChanged (void)
212 : {
213 0 : }
214 :
215 :
216 :
217 :
218 0 : sal_uInt32 ControlContainer::GetControlCount (void) const
219 : {
220 0 : return maControlList.size();
221 : }
222 :
223 :
224 :
225 :
226 0 : sal_uInt32 ControlContainer::GetVisibleControlCount (void) const
227 : {
228 0 : sal_uInt32 nCount (0);
229 :
230 : sal_uInt32 nIndex;
231 0 : sal_uInt32 nAllCount (maControlList.size());
232 0 : for (nIndex=0; nIndex<nAllCount; nIndex=GetNextIndex(nIndex,true))
233 : {
234 0 : if (maControlList[nIndex]->GetWindow()->IsVisible())
235 0 : nCount += 1;
236 : }
237 :
238 0 : return nCount;
239 : }
240 :
241 :
242 :
243 :
244 0 : TreeNode* ControlContainer::GetControl (sal_uInt32 nIndex) const
245 : {
246 0 : if (nIndex<maControlList.size() && nIndex!=(sal_uInt32)-1)
247 0 : return maControlList[nIndex];
248 : else
249 0 : return NULL;
250 : }
251 :
252 :
253 :
254 :
255 0 : sal_uInt32 ControlContainer::GetPreviousIndex (
256 : sal_uInt32 nIndex,
257 : bool bIncludeHidden,
258 : bool bCycle) const
259 : {
260 0 : sal_uInt32 nCandidate (nIndex);
261 :
262 0 : while (true)
263 : {
264 0 : if (nCandidate==0)
265 0 : if ( ! bCycle)
266 : {
267 : // We have reached the head of the list of controls and must
268 : // not cycle to its end.
269 0 : nCandidate = maControlList.size();
270 0 : break;
271 : }
272 : else
273 : {
274 : // Cycle to the end of the list.
275 0 : nCandidate = maControlList.size() - 1;
276 : }
277 : else
278 : // Go to the regular predecessor.
279 0 : nCandidate -= 1;
280 :
281 0 : if (nCandidate == nIndex)
282 : {
283 : // Made one full loop and found no valid control.
284 0 : nCandidate = maControlList.size();
285 0 : break;
286 : }
287 0 : else if (bIncludeHidden)
288 : {
289 : // Return the candidate index regardless of whether the control
290 : // is hidden or not.
291 0 : break;
292 : }
293 0 : else if (maControlList[nCandidate]->GetWindow()->IsVisible())
294 : {
295 : // Found a visible control.
296 0 : break;
297 : }
298 :
299 : // The candidate does not meet our constraints so do one more loop.
300 : }
301 :
302 0 : return nCandidate;
303 : }
304 :
305 :
306 :
307 0 : sal_uInt32 ControlContainer::GetNextIndex (
308 : sal_uInt32 nIndex,
309 : bool bIncludeHidden,
310 : bool bCycle) const
311 : {
312 0 : sal_uInt32 nCandidate (nIndex);
313 :
314 0 : while (true)
315 : {
316 : // Go to the regular successor.
317 0 : nCandidate += 1;
318 0 : if (nCandidate==maControlList.size())
319 : {
320 0 : if ( ! bCycle)
321 : {
322 : // We have reached the end of the list of controls and must
323 : // not cycle to its head.
324 0 : break;
325 : }
326 : else
327 : {
328 : // Cycle to the head of the list.
329 0 : nCandidate = 0;
330 : }
331 : }
332 :
333 0 : if (nCandidate == nIndex)
334 : {
335 : // Made one full loop and found no valid control.
336 0 : nCandidate = maControlList.size();
337 0 : break;
338 : }
339 0 : else if (bIncludeHidden)
340 : {
341 : // Return the candidate index regardless of whether the control
342 : // is hidden or not.
343 0 : break;
344 : }
345 0 : else if (maControlList[nCandidate]->GetWindow()->IsVisible())
346 : {
347 : // Found a visible control.
348 0 : break;
349 : }
350 :
351 : // The candidate does not meet our constraints so do one more loop.
352 : }
353 :
354 0 : return nCandidate;
355 : }
356 :
357 :
358 :
359 :
360 0 : void ControlContainer::SetMultiSelection (bool bFlag)
361 : {
362 0 : mbMultiSelection = bFlag;
363 0 : }
364 :
365 :
366 :
367 :
368 0 : void ControlContainer::SetVisibilityState (
369 : sal_uInt32 nControlIndex,
370 : VisibilityState aState)
371 : {
372 0 : TreeNode* pControl = GetControl (nControlIndex);
373 0 : if (pControl != NULL)
374 : {
375 : bool bShow;
376 0 : switch (aState)
377 : {
378 : default:
379 : case VS_TOGGLE:
380 0 : bShow = ! pControl->IsShowing();
381 0 : break;
382 : case VS_SHOW:
383 0 : bShow = true;
384 0 : break;
385 : case VS_HIDE:
386 0 : bShow = false;
387 0 : break;
388 : }
389 :
390 0 : bool bControlWasExpanded = pControl->IsExpanded();
391 0 : if (bShow != pControl->IsShowing())
392 : {
393 0 : pControl->Show (bShow);
394 :
395 0 : if (bShow)
396 : {
397 : // If we just turned on the first control then expand it, too.
398 : // If we turned on another control collapse it.
399 0 : if (GetVisibleControlCount() == 1)
400 0 : SetExpansionState (nControlIndex, ES_EXPAND);
401 : else
402 0 : SetExpansionState (nControlIndex, ES_COLLAPSE);
403 : }
404 : else
405 : {
406 0 : if (GetVisibleControlCount() > 0)
407 : {
408 0 : if (bControlWasExpanded)
409 : {
410 : // We turned off an expanded control. Make sure that
411 : // one of the still visible ones is expanded.
412 : sal_uInt32 nIndex = GetNextIndex(
413 : nControlIndex,
414 : false,
415 0 : false);
416 0 : if (nIndex == GetControlCount())
417 : nIndex = GetPreviousIndex(
418 : nControlIndex,
419 : false,
420 0 : false);
421 0 : SetExpansionState (nIndex, ES_EXPAND);
422 : }
423 : }
424 : }
425 :
426 0 : if (mpNode != NULL)
427 0 : mpNode->RequestResize();
428 : }
429 : }
430 0 : }
431 :
432 :
433 :
434 :
435 : } } // end of namespace ::sd::toolpanel
436 :
437 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|