25
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2000, 2006 IBM Corporation and others.
|
|
3 * All rights reserved. This program and the accompanying materials
|
|
4 * are made available under the terms of the Eclipse Public License v1.0
|
|
5 * which accompanies this distribution, and is available at
|
|
6 * http://www.eclipse.org/legal/epl-v10.html
|
|
7 *
|
|
8 * Contributors:
|
|
9 * IBM Corporation - initial API and implementation
|
|
10 * Port to the D programming language:
|
|
11 * Frank Benoit <benoit@tionex.de>
|
|
12 *******************************************************************************/
|
|
13
|
|
14 module dwtx.jface.action.ToolBarContributionItem;
|
|
15
|
|
16 import dwtx.jface.action.IContributionItem;
|
|
17 import dwtx.jface.action.ContributionItem;
|
|
18 import dwtx.jface.action.MenuManager;
|
|
19 import dwtx.jface.action.IToolBarManager;
|
|
20 import dwtx.jface.action.ToolBarManager;
|
|
21 import dwtx.jface.action.ActionContributionItem;
|
|
22 import dwtx.jface.action.SubContributionItem;
|
|
23 import dwtx.jface.action.ICoolBarManager;
|
|
24 import dwtx.jface.action.Separator;
|
|
25 import dwtx.jface.action.IContributionManager;
|
|
26
|
|
27 import tango.util.collection.ArraySeq;
|
|
28 // import java.util.Iterator;
|
|
29
|
|
30 import dwt.DWT;
|
|
31 import dwt.events.DisposeEvent;
|
|
32 import dwt.events.DisposeListener;
|
|
33 import dwt.events.SelectionAdapter;
|
|
34 import dwt.events.SelectionEvent;
|
|
35 import dwt.graphics.Point;
|
|
36 import dwt.graphics.Rectangle;
|
|
37 import dwt.widgets.Control;
|
|
38 import dwt.widgets.CoolBar;
|
|
39 import dwt.widgets.CoolItem;
|
|
40 import dwt.widgets.Event;
|
|
41 import dwt.widgets.Listener;
|
|
42 import dwt.widgets.Menu;
|
|
43 import dwt.widgets.ToolBar;
|
|
44 import dwt.widgets.ToolItem;
|
|
45 import dwtx.core.runtime.Assert;
|
|
46 import dwtx.jface.internal.provisional.action.IToolBarContributionItem;
|
|
47 import dwtx.jface.util.Policy;
|
|
48
|
|
49 import dwt.dwthelper.utils;
|
|
50 import tango.io.Stdout;
|
|
51
|
|
52 /**
|
|
53 * The <code>ToolBarContributionItem</code> class provides a wrapper for tool
|
|
54 * bar managers when used in cool bar managers. It extends <code>ContributionItem</code>
|
|
55 * but and provides some additional methods to customize the size of the cool
|
|
56 * item and to retrieve the underlying tool bar manager.
|
|
57 * <p>
|
|
58 * This class may be instantiated; it is not intended to be subclassed.
|
|
59 * </p>
|
|
60 *
|
|
61 * @since 3.0
|
|
62 */
|
|
63 public class ToolBarContributionItem : ContributionItem, IToolBarContributionItem {
|
|
64
|
|
65 public IContributionManager getParent() {
|
|
66 return super.getParent();
|
|
67 }
|
|
68
|
|
69 /**
|
|
70 * A constant used by <code>setMinimumItemsToShow</code> and <code>getMinimumItemsToShow</code>
|
|
71 * to indicate that all tool items should be shown in the cool item.
|
|
72 */
|
|
73 public static const int SHOW_ALL_ITEMS = -1;
|
|
74
|
|
75 /**
|
|
76 * The pull down menu used to list all hidden tool items if the current
|
|
77 * size is less than the preffered size.
|
|
78 */
|
|
79 private MenuManager chevronMenuManager = null;
|
|
80
|
|
81 /**
|
|
82 * The widget created for this item; <code>null</code> before creation
|
|
83 * and after disposal.
|
|
84 */
|
|
85 private CoolItem coolItem = null;
|
|
86
|
|
87 /**
|
|
88 * Current height of cool item
|
|
89 */
|
|
90 private int currentHeight = -1;
|
|
91
|
|
92 /**
|
|
93 * Current width of cool item.
|
|
94 */
|
|
95 private int currentWidth = -1;
|
|
96
|
|
97 /**
|
|
98 * A flag indicating that this item has been disposed. This prevents future
|
|
99 * method invocations from doing things they shouldn't.
|
|
100 */
|
|
101 private bool disposed = false;
|
|
102
|
|
103 /**
|
|
104 * Mininum number of tool items to show in the cool item widget.
|
|
105 */
|
|
106 private int minimumItemsToShow = SHOW_ALL_ITEMS;
|
|
107
|
|
108 /**
|
|
109 * The tool bar manager used to manage the tool items contained in the cool
|
|
110 * item widget.
|
|
111 */
|
|
112 private ToolBarManager toolBarManager = null;
|
|
113
|
|
114 /**
|
|
115 * Enable/disable chevron support.
|
|
116 */
|
|
117 private bool useChevron = true;
|
|
118
|
|
119 /**
|
|
120 * Convenience method equivalent to <code>ToolBarContributionItem(new ToolBarManager(), null)</code>.
|
|
121 */
|
|
122 public this() {
|
|
123 this(new ToolBarManager(), null);
|
|
124 }
|
|
125
|
|
126 /**
|
|
127 * Convenience method equivalent to <code>ToolBarContributionItem(toolBarManager, null)</code>.
|
|
128 *
|
|
129 * @param toolBarManager
|
|
130 * the tool bar manager
|
|
131 */
|
|
132 public this(IToolBarManager toolBarManager) {
|
|
133 this(toolBarManager, null);
|
|
134 }
|
|
135
|
|
136 /**
|
|
137 * Creates a tool bar contribution item.
|
|
138 *
|
|
139 * @param toolBarManager
|
|
140 * the tool bar manager to wrap
|
|
141 * @param id
|
|
142 * the contribution item id, or <code>null</code> if none
|
|
143 */
|
|
144 public this(IToolBarManager toolBarManager, String id) {
|
|
145 super(id);
|
|
146 Assert.isTrue( null !is cast(ToolBarManager)toolBarManager );
|
|
147 this.toolBarManager = cast(ToolBarManager) toolBarManager;
|
|
148 }
|
|
149
|
|
150 /**
|
|
151 * Checks whether this contribution item has been disposed. If it has, and
|
|
152 * the tracing options are active, then it prints some debugging
|
|
153 * information.
|
|
154 *
|
|
155 * @return <code>true</code> if the item is disposed; <code>false</code>
|
|
156 * otherwise.
|
|
157 *
|
|
158 */
|
|
159 private final bool checkDisposed() {
|
|
160 if (disposed) {
|
|
161 if (Policy.TRACE_TOOLBAR) {
|
|
162 Stdout.formatln("Method invocation on a disposed tool bar contribution item."); //$NON-NLS-1$
|
|
163 ExceptionPrintStackTrace( new Exception(null), Stdout );
|
|
164 }
|
|
165
|
|
166 return true;
|
|
167 }
|
|
168
|
|
169 return false;
|
|
170 }
|
|
171
|
|
172 /*
|
|
173 * (non-Javadoc)
|
|
174 *
|
|
175 * @see dwtx.jface.action.IContributionItem#dispose()
|
|
176 */
|
|
177 public void dispose() {
|
|
178 // Dispose of the ToolBar and all its contributions
|
|
179 if (toolBarManager !is null) {
|
|
180 toolBarManager.dispose();
|
|
181 toolBarManager = null;
|
|
182 }
|
|
183
|
|
184 /*
|
|
185 * We need to dispose the cool item or we might be left holding a cool
|
|
186 * item with a disposed control.
|
|
187 */
|
|
188 if ((coolItem !is null) && (!coolItem.isDisposed())) {
|
|
189 coolItem.dispose();
|
|
190 coolItem = null;
|
|
191 }
|
|
192
|
|
193 // Mark this item as disposed.
|
|
194 disposed = true;
|
|
195 }
|
|
196
|
|
197 /*
|
|
198 * (non-Javadoc)
|
|
199 *
|
|
200 * @see dwtx.jface.action.IContributionItem#fill(dwt.widgets.CoolBar,
|
|
201 * int)
|
|
202 */
|
|
203 public void fill(CoolBar coolBar, int index) {
|
|
204 if (checkDisposed()) {
|
|
205 return;
|
|
206 }
|
|
207
|
|
208 if (coolItem is null && coolBar !is null) {
|
|
209 ToolBar oldToolBar = toolBarManager.getControl();
|
|
210 ToolBar toolBar = toolBarManager.createControl(coolBar);
|
|
211 if ((oldToolBar !is null) && (oldToolBar.opEquals(toolBar))) {
|
|
212 // We are using an old tool bar, so we need to update.
|
|
213 toolBarManager.update(true);
|
|
214 }
|
|
215
|
|
216 // Do not create a coolItem if the toolbar is empty
|
|
217 if (toolBar.getItemCount() < 1) {
|
|
218 return;
|
|
219 }
|
|
220 int flags = DWT.DROP_DOWN;
|
|
221 if (index >= 0) {
|
|
222 coolItem = new CoolItem(coolBar, flags, index);
|
|
223 } else {
|
|
224 coolItem = new CoolItem(coolBar, flags);
|
|
225 }
|
|
226 // sets the back reference
|
|
227 coolItem.setData(this);
|
|
228 // Add the toolbar to the CoolItem widget
|
|
229 coolItem.setControl(toolBar);
|
|
230
|
|
231 // Handle Context Menu
|
|
232 // ToolBarManager.createControl can actually return a pre-existing control.
|
|
233 // Only add the listener if the toolbar was newly created (bug 62097).
|
|
234 if (oldToolBar !is toolBar) {
|
|
235 toolBar.addListener(DWT.MenuDetect, new class Listener {
|
|
236
|
|
237 public void handleEvent(Event event) {
|
|
238 // if the toolbar does not have its own context menu then
|
|
239 // handle the event
|
|
240 if (toolBarManager.getContextMenuManager() is null) {
|
|
241 handleContextMenu(event);
|
|
242 }
|
|
243 }
|
|
244 });
|
|
245 }
|
|
246
|
|
247 // Handle for chevron clicking
|
|
248 if (getUseChevron()) {
|
|
249 // Chevron Support
|
|
250 coolItem.addSelectionListener(new class SelectionAdapter {
|
|
251
|
|
252 public void widgetSelected(SelectionEvent event) {
|
|
253 if (event.detail is DWT.ARROW) {
|
|
254 handleChevron(event);
|
|
255 }
|
|
256 }
|
|
257 });
|
|
258 }
|
|
259
|
|
260 // Handle for disposal
|
|
261 coolItem.addDisposeListener(new class DisposeListener {
|
|
262
|
|
263 public void widgetDisposed(DisposeEvent event) {
|
|
264 handleWidgetDispose(event);
|
|
265 }
|
|
266 });
|
|
267
|
|
268 // Sets the size of the coolItem
|
|
269 updateSize(true);
|
|
270 }
|
|
271 }
|
|
272
|
|
273 /**
|
|
274 * Returns a consistent set of wrap indices. The return value will always
|
|
275 * include at least one entry and the first entry will always be zero.
|
|
276 * CoolBar.getWrapIndices() is inconsistent in whether or not it returns an
|
|
277 * index for the first row.
|
|
278 */
|
|
279 private int[] getAdjustedWrapIndices(int[] wraps) {
|
|
280 int[] adjustedWrapIndices;
|
|
281 if (wraps.length is 0) {
|
|
282 adjustedWrapIndices = [ 0 ];
|
|
283 } else {
|
|
284 if (wraps[0] !is 0) {
|
|
285 adjustedWrapIndices = new int[wraps.length + 1];
|
|
286 adjustedWrapIndices[0] = 0;
|
|
287 for (int i = 0; i < wraps.length; i++) {
|
|
288 adjustedWrapIndices[i + 1] = wraps[i];
|
|
289 }
|
|
290 } else {
|
|
291 adjustedWrapIndices = wraps;
|
|
292 }
|
|
293 }
|
|
294 return adjustedWrapIndices;
|
|
295 }
|
|
296
|
|
297 /**
|
|
298 * Returns the current height of the corresponding cool item.
|
|
299 *
|
|
300 * @return the current height
|
|
301 */
|
|
302 public int getCurrentHeight() {
|
|
303 if (checkDisposed()) {
|
|
304 return -1;
|
|
305 }
|
|
306 return currentHeight;
|
|
307 }
|
|
308
|
|
309 /**
|
|
310 * Returns the current width of the corresponding cool item.
|
|
311 *
|
|
312 * @return the current size
|
|
313 */
|
|
314 public int getCurrentWidth() {
|
|
315 if (checkDisposed()) {
|
|
316 return -1;
|
|
317 }
|
|
318 return currentWidth;
|
|
319 }
|
|
320
|
|
321 /**
|
|
322 * Returns the minimum number of tool items to show in the cool item.
|
|
323 *
|
|
324 * @return the minimum number of tool items to show, or <code>SHOW_ALL_ITEMS</code>
|
|
325 * if a value was not set
|
|
326 * @see #setMinimumItemsToShow(int)
|
|
327 */
|
|
328 public int getMinimumItemsToShow() {
|
|
329 if (checkDisposed()) {
|
|
330 return -1;
|
|
331 }
|
|
332 return minimumItemsToShow;
|
|
333 }
|
|
334
|
|
335 /**
|
|
336 * Returns the internal tool bar manager of the contribution item.
|
|
337 *
|
|
338 * @return the tool bar manager, or <code>null</code> if one is not
|
|
339 * defined.
|
|
340 * @see IToolBarManager
|
|
341 */
|
|
342 public IToolBarManager getToolBarManager() {
|
|
343 if (checkDisposed()) {
|
|
344 return null;
|
|
345 }
|
|
346 return toolBarManager;
|
|
347 }
|
|
348
|
|
349 /**
|
|
350 * Returns whether chevron support is enabled.
|
|
351 *
|
|
352 * @return <code>true</code> if chevron support is enabled, <code>false</code>
|
|
353 * otherwise
|
|
354 */
|
|
355 public bool getUseChevron() {
|
|
356 if (checkDisposed()) {
|
|
357 return false;
|
|
358 }
|
|
359 return useChevron;
|
|
360 }
|
|
361
|
|
362 /**
|
|
363 * Create and display the chevron menu.
|
|
364 */
|
|
365 private void handleChevron(SelectionEvent event) {
|
|
366 CoolItem item = cast(CoolItem) event.widget;
|
|
367 Control control = item.getControl();
|
|
368 if (!(cast(ToolBar)control ) ) {
|
|
369 return;
|
|
370 }
|
|
371 CoolBar coolBar = item.getParent();
|
|
372 ToolBar toolBar = cast(ToolBar) control;
|
|
373 Rectangle toolBarBounds = toolBar.getBounds();
|
|
374 ToolItem[] items = toolBar.getItems();
|
|
375 auto hidden = new ArraySeq!(Object);
|
|
376 for (int i = 0; i < items.length; ++i) {
|
|
377 Rectangle itemBounds = items[i].getBounds();
|
|
378 if (!((itemBounds.x + itemBounds.width <= toolBarBounds.width) && (itemBounds.y
|
|
379 + itemBounds.height <= toolBarBounds.height))) {
|
|
380 hidden.append(items[i]);
|
|
381 }
|
|
382 }
|
|
383
|
|
384 // Create a pop-up menu with items for each of the hidden buttons.
|
|
385 if (chevronMenuManager !is null) {
|
|
386 chevronMenuManager.dispose();
|
|
387 }
|
|
388 chevronMenuManager = new MenuManager();
|
|
389 foreach( it; hidden ){
|
|
390 ToolItem toolItem = cast(ToolItem) it;
|
|
391 IContributionItem data = cast(IContributionItem) toolItem.getData();
|
|
392 if (cast(ActionContributionItem)data ) {
|
|
393 ActionContributionItem contribution = new ActionContributionItem(
|
|
394 (cast(ActionContributionItem) data).getAction());
|
|
395 chevronMenuManager.add(contribution);
|
|
396 } else if (cast(SubContributionItem)data ) {
|
|
397 IContributionItem innerData = (cast(SubContributionItem) data)
|
|
398 .getInnerItem();
|
|
399 if (cast(ActionContributionItem)innerData ) {
|
|
400 ActionContributionItem contribution = new ActionContributionItem(
|
|
401 (cast(ActionContributionItem) innerData).getAction());
|
|
402 chevronMenuManager.add(contribution);
|
|
403 }
|
|
404 } else if (data.isSeparator()) {
|
|
405 chevronMenuManager.add(new Separator());
|
|
406 }
|
|
407 }
|
|
408 Menu popup = chevronMenuManager.createContextMenu(coolBar);
|
|
409 Point chevronPosition = coolBar.toDisplay(event.x, event.y);
|
|
410 popup.setLocation(chevronPosition.x, chevronPosition.y);
|
|
411 popup.setVisible(true);
|
|
412 }
|
|
413
|
|
414 /**
|
|
415 * Handles the event when the toobar item does not have its own context
|
|
416 * menu.
|
|
417 *
|
|
418 * @param event
|
|
419 * the event object
|
|
420 */
|
|
421 private void handleContextMenu(Event event) {
|
|
422 ToolBar toolBar = toolBarManager.getControl();
|
|
423 // If parent has a menu then use that one
|
|
424 Menu parentMenu = toolBar.getParent().getMenu();
|
|
425 if ((parentMenu !is null) && (!parentMenu.isDisposed())) {
|
|
426 toolBar.setMenu(parentMenu);
|
|
427 // Hook listener to remove menu once it has disapeared
|
|
428 parentMenu.addListener(DWT.Hide, new class Listener {
|
|
429
|
|
430 public void handleEvent(Event innerEvent) {
|
|
431 ToolBar innerToolBar = toolBarManager.getControl();
|
|
432 if (innerToolBar !is null) {
|
|
433 innerToolBar.setMenu(null);
|
|
434 Menu innerParentMenu = innerToolBar.getParent()
|
|
435 .getMenu();
|
|
436 if (innerParentMenu !is null) {
|
|
437 innerParentMenu.removeListener(DWT.Hide, this);
|
|
438 }
|
|
439 }
|
|
440 }
|
|
441 });
|
|
442 }
|
|
443 }
|
|
444
|
|
445 /**
|
|
446 * Handles the disposal of the widget.
|
|
447 *
|
|
448 * @param event
|
|
449 * the event object
|
|
450 */
|
|
451 private void handleWidgetDispose(DisposeEvent event) {
|
|
452 coolItem = null;
|
|
453 }
|
|
454
|
|
455 /**
|
|
456 * A contribution item is visible iff its internal state is visible <em>or</em>
|
|
457 * the tool bar manager contains something other than group markers and
|
|
458 * separators.
|
|
459 *
|
|
460 * @return <code>true</code> if the tool bar manager contains something
|
|
461 * other than group marks and separators, and the internal state is
|
|
462 * set to be visible.
|
|
463 */
|
|
464 public bool isVisible() {
|
|
465 if (checkDisposed()) {
|
|
466 return false;
|
|
467 }
|
|
468
|
|
469 bool visibleItem = false;
|
|
470 if (toolBarManager !is null) {
|
|
471 IContributionItem[] contributionItems = toolBarManager.getItems();
|
|
472 for (int i = 0; i < contributionItems.length; i++) {
|
|
473 IContributionItem contributionItem = contributionItems[i];
|
|
474 if ((!contributionItem.isGroupMarker())
|
|
475 && (!contributionItem.isSeparator())) {
|
|
476 visibleItem = true;
|
|
477 break;
|
|
478 }
|
|
479 }
|
|
480 }
|
|
481
|
|
482 return (visibleItem || super.isVisible());
|
|
483 }
|
|
484
|
|
485 /*
|
|
486 * (non-Javadoc)
|
|
487 *
|
|
488 * @see dwtx.jface.action.IContributionItem#saveWidgetState()
|
|
489 */
|
|
490 public void saveWidgetState() {
|
|
491 if (checkDisposed()) {
|
|
492 return;
|
|
493 }
|
|
494 if (coolItem is null) {
|
|
495 return;
|
|
496 }
|
|
497
|
|
498 //1. Save current size
|
|
499 CoolBar coolBar = coolItem.getParent();
|
|
500 bool isLastOnRow = false;
|
|
501 int lastIndex = coolBar.getItemCount() - 1;
|
|
502 int coolItemIndex = coolBar.indexOf(coolItem);
|
|
503 int[] wrapIndicies = getAdjustedWrapIndices(coolBar.getWrapIndices());
|
|
504 // Traverse through all wrap indicies backwards
|
|
505 for (int row = wrapIndicies.length - 1; row >= 0; row--) {
|
|
506 if (wrapIndicies[row] <= coolItemIndex) {
|
|
507
|
|
508 int nextRow = row + 1;
|
|
509 int nextRowStartIndex;
|
|
510 if (nextRow > (wrapIndicies.length - 1)) {
|
|
511 nextRowStartIndex = lastIndex + 1;
|
|
512 } else {
|
|
513 nextRowStartIndex = wrapIndicies[nextRow];
|
|
514 }
|
|
515
|
|
516 // Check to see if its the last item on the row
|
|
517 if (coolItemIndex is (nextRowStartIndex - 1)) {
|
|
518 isLastOnRow = true;
|
|
519 }
|
|
520 break;
|
|
521 }
|
|
522 }
|
|
523
|
|
524 // Save the preferred size as actual size for the last item on a row
|
|
525 int nCurrentWidth;
|
|
526 if (isLastOnRow) {
|
|
527 nCurrentWidth = coolItem.getPreferredSize().x;
|
|
528 } else {
|
|
529 nCurrentWidth = coolItem.getSize().x;
|
|
530 }
|
|
531 setCurrentWidth(nCurrentWidth);
|
|
532 setCurrentHeight(coolItem.getSize().y);
|
|
533 }
|
|
534
|
|
535 /**
|
|
536 * Sets the current height of the cool item. Update(SIZE) should be called
|
|
537 * to adjust the widget.
|
|
538 *
|
|
539 * @param currentHeight
|
|
540 * the current height to set
|
|
541 */
|
|
542 public void setCurrentHeight(int currentHeight) {
|
|
543 if (checkDisposed()) {
|
|
544 return;
|
|
545 }
|
|
546 this.currentHeight = currentHeight;
|
|
547 }
|
|
548
|
|
549 /**
|
|
550 * Sets the current width of the cool item. Update(SIZE) should be called
|
|
551 * to adjust the widget.
|
|
552 *
|
|
553 * @param currentWidth
|
|
554 * the current width to set
|
|
555 */
|
|
556 public void setCurrentWidth(int currentWidth) {
|
|
557 if (checkDisposed()) {
|
|
558 return;
|
|
559 }
|
|
560 this.currentWidth = currentWidth;
|
|
561 }
|
|
562
|
|
563 /**
|
|
564 * Sets the minimum number of tool items to show in the cool item. If this
|
|
565 * number is less than the total tool items, a chevron will appear and the
|
|
566 * hidden tool items appear in a drop down menu. By default, all the tool
|
|
567 * items are shown in the cool item.
|
|
568 *
|
|
569 * @param minimumItemsToShow
|
|
570 * the minimum number of tool items to show.
|
|
571 * @see #getMinimumItemsToShow()
|
|
572 * @see #setUseChevron(bool)
|
|
573 */
|
|
574 public void setMinimumItemsToShow(int minimumItemsToShow) {
|
|
575 if (checkDisposed()) {
|
|
576 return;
|
|
577 }
|
|
578 this.minimumItemsToShow = minimumItemsToShow;
|
|
579 }
|
|
580
|
|
581 /**
|
|
582 * Enables or disables chevron support for the cool item. By default,
|
|
583 * chevron support is enabled.
|
|
584 *
|
|
585 * @param value
|
|
586 * <code>true</code> to enable chevron support, <code>false</code>
|
|
587 * otherwise.
|
|
588 */
|
|
589 public void setUseChevron(bool value) {
|
|
590 if (checkDisposed()) {
|
|
591 return;
|
|
592 }
|
|
593 useChevron = value;
|
|
594 }
|
|
595
|
|
596 /*
|
|
597 * (non-Javadoc)
|
|
598 *
|
|
599 * @see dwtx.jface.action.IContributionItem#update(java.lang.String)
|
|
600 */
|
|
601 public void update(String propertyName) {
|
|
602 if (checkDisposed()) {
|
|
603 return;
|
|
604 }
|
|
605 if (coolItem !is null) {
|
|
606 IToolBarManager manager = getToolBarManager();
|
|
607 if (manager !is null) {
|
|
608 manager.update(true);
|
|
609 }
|
|
610
|
|
611 if ((propertyName is null)
|
|
612 || propertyName.equals(ICoolBarManager.SIZE)) {
|
|
613 updateSize(true);
|
|
614 }
|
|
615 }
|
|
616 }
|
|
617
|
|
618 /**
|
|
619 * Updates the cool items' preferred, minimum, and current size. The
|
|
620 * preferred size is calculated based on the tool bar size and extra trim.
|
|
621 *
|
|
622 * @param changeCurrentSize
|
|
623 * <code>true</code> if the current size should be changed to
|
|
624 * the preferred size, <code>false</code> to not change the
|
|
625 * current size
|
|
626 */
|
|
627 private void updateSize(bool changeCurrentSize) {
|
|
628 if (checkDisposed()) {
|
|
629 return;
|
|
630 }
|
|
631 // cannot set size if coolItem is null
|
|
632 if (coolItem is null || coolItem.isDisposed()) {
|
|
633 return;
|
|
634 }
|
|
635 bool locked = false;
|
|
636 CoolBar coolBar = coolItem.getParent();
|
|
637 try {
|
|
638 // Fix odd behaviour with locked tool bars
|
|
639 if (coolBar !is null) {
|
|
640 if (coolBar.getLocked()) {
|
|
641 coolBar.setLocked(false);
|
|
642 locked = true;
|
|
643 }
|
|
644 }
|
|
645 ToolBar toolBar = cast(ToolBar) coolItem.getControl();
|
|
646 if ((toolBar is null) || (toolBar.isDisposed())
|
|
647 || (toolBar.getItemCount() <= 0)) {
|
|
648 // if the toolbar does not contain any items then dispose of
|
|
649 // coolItem
|
|
650 coolItem.setData(null);
|
|
651 Control control = coolItem.getControl();
|
|
652 if ((control !is null) && !control.isDisposed()) {
|
|
653 control.dispose();
|
|
654 coolItem.setControl(null);
|
|
655 }
|
|
656 if (!coolItem.isDisposed()) {
|
|
657 coolItem.dispose();
|
|
658 }
|
|
659 } else {
|
|
660 // If the toolbar item exists then adjust the size of the cool
|
|
661 // item
|
|
662 Point toolBarSize = toolBar.computeSize(DWT.DEFAULT,
|
|
663 DWT.DEFAULT);
|
|
664 // Set the preffered size to the size of the toolbar plus trim
|
|
665 Point preferredSize = coolItem.computeSize(toolBarSize.x,
|
|
666 toolBarSize.y);
|
|
667 coolItem.setPreferredSize(preferredSize);
|
|
668 // note setMinimumSize must be called before setSize, see PR
|
|
669 // 15565
|
|
670 // Set minimum size
|
|
671 if (getMinimumItemsToShow() !is SHOW_ALL_ITEMS) {
|
|
672 int toolItemWidth = toolBar.getItems()[0].getWidth();
|
|
673 int minimumWidth = toolItemWidth * getMinimumItemsToShow();
|
|
674 coolItem.setMinimumSize(minimumWidth, toolBarSize.y);
|
|
675 } else {
|
|
676 coolItem.setMinimumSize(toolBarSize.x, toolBarSize.y);
|
|
677 }
|
|
678 if (changeCurrentSize) {
|
|
679 // Set current size to preferred size
|
|
680 coolItem.setSize(preferredSize);
|
|
681 }
|
|
682 }
|
|
683 } finally {
|
|
684 // If the cool bar was locked, then set it back to locked
|
|
685 if ((locked) && (coolBar !is null)) {
|
|
686 coolBar.setLocked(true);
|
|
687 }
|
|
688 }
|
|
689 }
|
|
690
|
|
691 }
|