-
Notifications
You must be signed in to change notification settings - Fork 118
/
CTabFolder.java
4280 lines (4135 loc) · 134 KB
/
CTabFolder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
* Copyright (c) 2000, 2020 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.swt.custom;
import org.eclipse.swt.*;
import org.eclipse.swt.accessibility.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.internal.*;
import org.eclipse.swt.internal.DPIUtil.*;
import org.eclipse.swt.widgets.*;
/**
*
* Instances of this class implement the notebook user interface
* metaphor. It allows the user to select a notebook page from
* set of pages.
* <p>
* The item children that may be added to instances of this class
* must be of type <code>CTabItem</code>.
* <code>Control</code> children are created and then set into a
* tab item using <code>CTabItem#setControl</code>.
* </p><p>
* Note that although this class is a subclass of <code>Composite</code>,
* it does not make sense to set a layout on it.
* </p>
* <dl>
* <dt><b>Styles:</b></dt>
* <dd>CLOSE, TOP, BOTTOM, FLAT, BORDER, SINGLE, MULTI</dd>
* <dt><b>Events:</b></dt>
* <dd>Selection</dd>
* <dd>"CTabFolder2"</dd>
* </dl>
* Note: Only one of the styles TOP and BOTTOM
* may be specified.
* <p>
* IMPORTANT: This class is <em>not</em> intended to be subclassed.
* </p>
*
* @see <a href="http://www.eclipse.org/swt/snippets/#ctabfolder">CTabFolder, CTabItem snippets</a>
* @see <a href="http://www.eclipse.org/swt/examples.php">SWT Example: CustomControlExample</a>
* @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
* @noextend This class is not intended to be subclassed by clients.
*/
public class CTabFolder extends Composite {
/**
* marginWidth specifies the number of points of horizontal margin
* that will be placed along the left and right edges of the form.
*
* The default value is 0.
*/
public int marginWidth = 0;
/**
* marginHeight specifies the number of points of vertical margin
* that will be placed along the top and bottom edges of the form.
*
* The default value is 0.
*/
public int marginHeight = 0;
/**
* A multiple of the tab height that specifies the minimum width to which a tab
* will be compressed before scrolling arrows are used to navigate the tabs.
*
* NOTE This field is badly named and can not be fixed for backwards compatibility.
* It should not be capitalized.
*
* @deprecated This field is no longer used. See setMinimumCharacters(int)
*/
@Deprecated
public int MIN_TAB_WIDTH = 4;
/**
* Color of innermost line of drop shadow border.
*
* NOTE This field is badly named and can not be fixed for backwards compatibility.
* It should be capitalized.
*
* @deprecated drop shadow border is no longer drawn in 3.0
*/
@Deprecated
public static RGB borderInsideRGB = new RGB (132, 130, 132);
/**
* Color of middle line of drop shadow border.
*
* NOTE This field is badly named and can not be fixed for backwards compatibility.
* It should be capitalized.
*
* @deprecated drop shadow border is no longer drawn in 3.0
*/
@Deprecated
public static RGB borderMiddleRGB = new RGB (143, 141, 138);
/**
* Color of outermost line of drop shadow border.
*
* NOTE This field is badly named and can not be fixed for backwards compatibility.
* It should be capitalized.
*
* @deprecated drop shadow border is no longer drawn in 3.0
*/
@Deprecated
public static RGB borderOutsideRGB = new RGB (171, 168, 165);
/* sizing, positioning */
boolean onBottom = false;
boolean single = false;
boolean simple = true;
int fixedTabHeight = SWT.DEFAULT;
int tabHeight;
int minChars = 20;
boolean borderVisible = false;
/* item management */
CTabFolderRenderer renderer;
CTabItem items[] = new CTabItem[0];
/** index of the left most visible tab. */
int firstIndex = -1;
int selectedIndex = -1;
/**
* Indices of the elements in the {@link #items} array, used to manage tab
* visibility and candidates to be hidden/shown next.
* <p>
* If there is not enough place for all tabs, tabs starting from the end of
* the {@link #priority} array will be hidden first (independently from the
* {@link #mru} flag!) => the right most elements have the highest priority
* to be hidden.
* <p>
* If there is more place to show previously hidden tabs, tabs starting from
* the beginning of the {@link #priority} array will be made visible first
* (independently from the {@link #mru} flag!) => the left most elements
* have the highest priority to be shown.
* <p>
* The update strategy of the {@link #priority} array however depends on the
* {@link #mru} flag.
* <p>
* If {@link #mru} flag is set, the first index is always the index of the
* currently selected tab, next one is the tab selected before current
* etc...
* <p>
* Example: [4,2,5,1,3,0], just representing the last selection order.
* <p>
* If {@link #mru} flag is not set, the first index is always the index of
* the left most visible tab ({@link #firstIndex} field), next indices are
* incremented by one up to <code>priority.length-1</code>, and the rest
* filled with indices starting with <code>firstIndex-1</code> and
* decremented by one until 0 index is reached.
* <p>
* The tabs between first index and the index of the currently selected tab
* are always visible.
* <p>
* Example: 6 tabs, 2 and 3 are indices of currently shown tabs:
* [2,3,4,5,1,0]. The array consists of two blocks: sorted ascending from
* first visible (2) to last available (5), and the rest sorted descending
* (1,0). 4 and 5 are the hidden tabs on the right side, 0 and 1 are the
* hidden tabs on the left side from the visible tabs 2 and 3.
*
* @see #updateItems(int)
* @see #setItemLocation(GC)
*/
int[] priority = new int[0];
boolean mru = false;
Listener listener;
boolean ignoreTraverse;
boolean useDefaultRenderer;
/* External Listener management */
CTabFolder2Listener[] folderListeners = new CTabFolder2Listener[0];
// support for deprecated listener mechanism
CTabFolderListener[] tabListeners = new CTabFolderListener[0];
/* Selected item appearance */
Image selectionBgImage;
Color[] selectionGradientColors;
int[] selectionGradientPercents;
boolean selectionGradientVertical;
Color selectionForeground;
Color selectionBackground;
int selectionHighlightBarThickness = 2;
/* Unselected item appearance */
Color[] gradientColors;
int[] gradientPercents;
boolean gradientVertical;
boolean showUnselectedImage = true;
boolean showSelectedImage = true;
// close, min/max and chevron buttons
boolean showClose = false;
boolean showUnselectedClose = true;
boolean showMin = false;
boolean minimized = false;
boolean showMax = false;
boolean maximized = false;
ToolBar minMaxTb;
ToolItem maxItem;
ToolItem minItem;
Image maxImage;
Image minImage;
boolean hoverTb;
Rectangle hoverRect = new Rectangle(0,0,0,0);
boolean hovering;
boolean hoverTimerRunning;
boolean highlight;
boolean highlightEnabled = true;
boolean showChevron = false;
Menu showMenu;
ToolBar chevronTb;
ToolItem chevronItem;
int chevronCount;
boolean chevronVisible = true;
Image chevronImage;
Control topRight;
int topRightAlignment = SWT.RIGHT;
boolean ignoreResize;
Control[] controls;
int[] controlAlignments;
Rectangle[] controlRects;
Rectangle[] bkImageBounds;
Image[] controlBkImages;
int updateFlags;
final static int REDRAW = 1 << 1;
final static int REDRAW_TABS = 1 << 2;
final static int UPDATE_TAB_HEIGHT = 1 << 3;
Runnable updateRun;
// when disposing CTabFolder, don't try to layout the items or
// change the selection as each child is destroyed.
boolean inDispose = false;
// keep track of size changes in order to redraw only affected area
// on Resize
Point oldSize;
Font oldFont;
// internal constants
static final int DEFAULT_WIDTH = 64;
static final int DEFAULT_HEIGHT = 64;
static final int SELECTION_FOREGROUND = SWT.COLOR_LIST_FOREGROUND;
static final int SELECTION_BACKGROUND = SWT.COLOR_LIST_BACKGROUND;
static final int FOREGROUND = SWT.COLOR_WIDGET_FOREGROUND;
static final int BACKGROUND = SWT.COLOR_WIDGET_BACKGROUND;
//TODO: add setter for spacing?
static final int SPACING = 3;
/**
* Constructs a new instance of this class given its parent
* and a style value describing its behavior and appearance.
* <p>
* The style value is either one of the style constants defined in
* class <code>SWT</code> which is applicable to instances of this
* class, or must be built by <em>bitwise OR</em>'ing together
* (that is, using the <code>int</code> "|" operator) two or more
* of those <code>SWT</code> style constants. The class description
* lists the style constants that are applicable to the class.
* Style bits are also inherited from superclasses.
* </p>
*
* @param parent a widget which will be the parent of the new instance (cannot be null)
* @param style the style of widget to construct
*
* @exception IllegalArgumentException <ul>
* <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
* </ul>
* @exception SWTException <ul>
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
* </ul>
*
* @see SWT#TOP
* @see SWT#BOTTOM
* @see SWT#FLAT
* @see SWT#BORDER
* @see SWT#SINGLE
* @see SWT#MULTI
* @see #getStyle()
*/
public CTabFolder(Composite parent, int style) {
super(parent, checkStyle (parent, style));
init(style);
}
void init(int style) {
super.setLayout(new CTabFolderLayout());
int style2 = super.getStyle();
oldFont = getFont();
onBottom = (style2 & SWT.BOTTOM) != 0;
showClose = (style2 & SWT.CLOSE) != 0;
// showMin = (style2 & SWT.MIN) != 0; - conflicts with SWT.TOP
// showMax = (style2 & SWT.MAX) != 0; - conflicts with SWT.BOTTOM
single = (style2 & SWT.SINGLE) != 0;
borderVisible = (style & SWT.BORDER) != 0;
//set up default colors
Display display = getDisplay();
selectionForeground = display.getSystemColor(SELECTION_FOREGROUND);
selectionBackground = display.getSystemColor(SELECTION_BACKGROUND);
renderer = new CTabFolderRenderer(this);
useDefaultRenderer = true;
controls = new Control[0];
controlAlignments = new int[0];
controlRects = new Rectangle[0];
controlBkImages = new Image[0];
updateTabHeight(false);
// Add all listeners
listener = event -> {
switch (event.type) {
case SWT.Dispose: onDispose(event); break;
case SWT.DragDetect: onDragDetect(event); break;
case SWT.FocusIn: onFocus(event); break;
case SWT.FocusOut: onFocus(event); break;
case SWT.KeyDown: onKeyDown(event); break;
case SWT.MenuDetect: onMenuDetect(event); break;
case SWT.MouseDoubleClick: onMouseDoubleClick(event); break;
case SWT.MouseDown: onMouse(event); break;
case SWT.MouseEnter: onMouse(event); break;
case SWT.MouseExit: onMouse(event); break;
case SWT.MouseHover: onMouse(event); break;
case SWT.MouseMove: onMouse(event); break;
case SWT.MouseUp: onMouse(event); break;
case SWT.Paint: onPaint(event); break;
case SWT.Resize: onResize(event); break;
case SWT.Traverse: onTraverse(event); break;
case SWT.Selection: onSelection(event); break;
case SWT.Activate: onActivate(event); break;
case SWT.Deactivate: onDeactivate(event); break;
}
};
int[] folderEvents = new int[]{
SWT.Dispose,
SWT.DragDetect,
SWT.FocusIn,
SWT.FocusOut,
SWT.KeyDown,
SWT.MenuDetect,
SWT.MouseDoubleClick,
SWT.MouseDown,
SWT.MouseEnter,
SWT.MouseExit,
SWT.MouseHover,
SWT.MouseMove,
SWT.MouseUp,
SWT.Paint,
SWT.Resize,
SWT.Traverse,
SWT.Activate,
SWT.Deactivate
};
for (int folderEvent : folderEvents) {
addListener(folderEvent, listener);
}
initAccessible();
}
void onDeactivate(Event event) {
if (!highlightEnabled) {
return;
}
this.highlight = false;
redraw();
}
void onActivate(Event event) {
if (!highlightEnabled) {
return;
}
this.highlight = true;
redraw();
}
static int checkStyle (Composite parent, int style) {
int mask = SWT.CLOSE | SWT.TOP | SWT.BOTTOM | SWT.FLAT | SWT.LEFT_TO_RIGHT | SWT.RIGHT_TO_LEFT | SWT.SINGLE | SWT.MULTI;
style = style & mask;
// TOP and BOTTOM are mutually exclusive.
// TOP is the default
if ((style & SWT.TOP) != 0) style = style & ~SWT.BOTTOM;
// SINGLE and MULTI are mutually exclusive.
// MULTI is the default
if ((style & SWT.MULTI) != 0) style = style & ~SWT.SINGLE;
// reduce the flash by not redrawing the entire area on a Resize event
style |= SWT.NO_REDRAW_RESIZE;
//TEMPORARY CODE
/*
* In Right To Left orientation on Windows, all GC calls that use a brush are drawing
* offset by one pixel. This results in some parts of the CTabFolder not drawing correctly.
* To alleviate some of the appearance problems, allow the OS to draw the background.
* This does not draw correctly but the result is less obviously wrong.
*/
if ((style & SWT.RIGHT_TO_LEFT) != 0) return style;
if ((parent.getStyle() & SWT.MIRRORED) != 0 && (style & SWT.LEFT_TO_RIGHT) == 0) return style;
return style | SWT.DOUBLE_BUFFERED;
}
/**
*
* Adds the listener to the collection of listeners who will
* be notified when a tab item is closed, minimized, maximized,
* restored, or to show the list of items that are not
* currently visible.
*
* @param listener the listener which should be notified
*
* @exception IllegalArgumentException <ul>
* <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
* </ul>
*
* @exception SWTException <ul>
* <li>ERROR_THREAD_INVALID_ACCESS when called from the wrong thread</li>
* <li>ERROR_WIDGET_DISPOSED when the widget has been disposed</li>
* </ul>
*
* @see CTabFolder2Listener
* @see #removeCTabFolder2Listener(CTabFolder2Listener)
*
* @since 3.0
*/
public void addCTabFolder2Listener(CTabFolder2Listener listener) {
checkWidget();
if (listener == null) SWT.error (SWT.ERROR_NULL_ARGUMENT);
// add to array
CTabFolder2Listener[] newListeners = new CTabFolder2Listener[folderListeners.length + 1];
System.arraycopy(folderListeners, 0, newListeners, 0, folderListeners.length);
folderListeners = newListeners;
folderListeners[folderListeners.length - 1] = listener;
}
/**
* Adds the listener to the collection of listeners who will
* be notified when a tab item is closed.
*
* @param listener the listener which should be notified
*
* @exception IllegalArgumentException <ul>
* <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
* </ul>
* @exception SWTException <ul>
* <li>ERROR_THREAD_INVALID_ACCESS when called from the wrong thread</li>
* <li>ERROR_WIDGET_DISPOSED when the widget has been disposed</li>
* </ul>
*
* @see CTabFolderListener
* @see #removeCTabFolderListener(CTabFolderListener)
*
* @deprecated use addCTabFolder2Listener(CTabFolder2Listener)
*/
@Deprecated
public void addCTabFolderListener(CTabFolderListener listener) {
checkWidget();
if (listener == null) SWT.error (SWT.ERROR_NULL_ARGUMENT);
// add to array
CTabFolderListener[] newTabListeners = new CTabFolderListener[tabListeners.length + 1];
System.arraycopy(tabListeners, 0, newTabListeners, 0, tabListeners.length);
tabListeners = newTabListeners;
tabListeners[tabListeners.length - 1] = listener;
// display close button to be backwards compatible
if (!showClose) {
showClose = true;
updateFolder(REDRAW);
}
}
/**
* Adds the listener to the collection of listeners who will
* be notified when the user changes the receiver's selection, by sending
* it one of the messages defined in the <code>SelectionListener</code>
* interface.
* <p>
* <code>widgetSelected</code> is called when the user changes the selected tab.
* <code>widgetDefaultSelected</code> is not called.
* </p>
*
* @param listener the listener which should be notified when the user changes the receiver's selection
*
* @exception IllegalArgumentException <ul>
* <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
* </ul>
* @exception SWTException <ul>
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
* </ul>
*
* @see SelectionListener
* @see #removeSelectionListener
* @see SelectionEvent
*/
public void addSelectionListener(SelectionListener listener) {
addTypedListener(listener, SWT.Selection, SWT.DefaultSelection);
}
Rectangle[] computeControlBounds (Point size, boolean[][] position) {
if (controls == null || controls.length == 0) return new Rectangle[0];
Rectangle[] rects = new Rectangle[controls.length];
for (int i = 0; i < rects.length; i++) {
rects[i] = new Rectangle(0, 0, 0, 0);
}
Rectangle trim = renderer.computeTrim(CTabFolderRenderer.PART_BORDER, SWT.NONE, 0, 0, 0, 0);
int borderRight = trim.width + trim.x;
int borderLeft = -trim.x;
int borderBottom = trim.height + trim.y;
int borderTop = -trim.y;
Point[] tabControlSize = new Point[controls.length];
boolean[] overflow = new boolean [controls.length];
//Left Control
int leftWidth = 0;
int x = borderLeft + SPACING;
int rightWidth = 0;
int allWidth = 0;
boolean spacingRight = false;
for (int i = 0; i < controls.length; i++) {
Point ctrlSize = tabControlSize[i] = !controls[i].isDisposed() && controls[i].getVisible() ? controls[i].computeSize(SWT.DEFAULT, SWT.DEFAULT) : new Point(0,0);
int alignment = controlAlignments[i];
if ((alignment & SWT.LEAD) != 0) {
rects[i].width = ctrlSize.x;
rects[i].height = getControlHeight(ctrlSize);
rects[i].x = x;
rects[i].y = getControlY(size, rects, borderBottom, borderTop, i);
x += ctrlSize.x;
leftWidth += ctrlSize.x;
} else {
if ((alignment & SWT.WRAP) == 0 && ctrlSize.x > 0) {
spacingRight = true;
}
if ((alignment & (SWT.FILL | SWT.WRAP)) == 0) {
rightWidth += ctrlSize.x;
}
allWidth += ctrlSize.x;
}
}
if (leftWidth > 0) leftWidth += SPACING * 2;
int itemWidth = 0;
for (CTabItem item : items) {
if (item.showing) itemWidth += item.width;
}
int maxWidth = size.x - borderLeft - leftWidth - borderRight;
int availableWidth = Math.max(0, maxWidth - itemWidth - rightWidth);
if (spacingRight) availableWidth -= SPACING * 2;
x = size.x - borderRight - SPACING;
if (itemWidth + allWidth <= maxWidth) {
//Everything fits
for (int i = 0; i < controls.length; i++) {
int alignment = controlAlignments[i];
if ((alignment & SWT.TRAIL) != 0) {
Point ctrlSize = tabControlSize[i];
x -= ctrlSize.x;
rects[i].width = ctrlSize.x;
rects[i].height = getControlHeight(ctrlSize);
rects[i].x = x;
rects[i].y = getControlY(size, rects, borderBottom, borderTop, i);
if ((alignment & (SWT.FILL | SWT.WRAP)) != 0) availableWidth -= ctrlSize.x;
}
if (tabControlSize[i].y >= tabHeight && fixedTabHeight == SWT.DEFAULT) {
overflow[i] = true;
}
}
} else {
for (int i = 0; i < controls.length; i++) {
int alignment = controlAlignments[i];
Point ctrlSize = tabControlSize[i];
if ((alignment & SWT.TRAIL) != 0) {
if ((alignment & (SWT.FILL | SWT.WRAP)) == 0) {
x -= ctrlSize.x;
rects[i].width = ctrlSize.x;
rects[i].height = getControlHeight(ctrlSize);
rects[i].x = x;
rects[i].y = getControlY(size, rects, borderBottom, borderTop, i);
} else if (((alignment & (SWT.WRAP)) != 0 && ctrlSize.x < availableWidth)) {
x -= ctrlSize.x;
rects[i].width = ctrlSize.x;
rects[i].height = getControlHeight(ctrlSize);
rects[i].x = x;
rects[i].y = getControlY(size, rects, borderBottom, borderTop, i);
availableWidth -= ctrlSize.x;
} else if ((alignment & (SWT.FILL)) != 0 && (alignment & (SWT.WRAP)) == 0) {
rects[i].width = 0;
rects[i].height = getControlHeight(ctrlSize);
rects[i].x = x;
rects[i].y = getControlY(size, rects, borderBottom, borderTop, i);
} else {
if ((alignment & (SWT.WRAP)) != 0) {
overflow[i] = true;
}
}
}
}
}
//Any space, distribute amongst FILL
if (availableWidth > 0) {
int fillCount = 0;
for (int i = 0; i < controls.length; i++) {
int alignment = controlAlignments[i];
if ((alignment & SWT.TRAIL) != 0 && (alignment & SWT.FILL) != 0 && !overflow[i]) {
fillCount++;
}
}
if (fillCount != 0) {
int extraSpace = availableWidth/fillCount;
int addedSpace = 0;
for (int i = 0; i < controls.length; i++) {
int alignment = controlAlignments[i];
if ((alignment & SWT.TRAIL) != 0) {
if ((alignment & SWT.FILL) != 0 && !overflow[i]) {
rects[i].width += extraSpace;
addedSpace += extraSpace;
}
if (!overflow[i]) {
rects[i].x -= addedSpace;
}
}
}
}
}
//Go through overflow laying out all wrapped controls
Rectangle bodyTrim = renderer.computeTrim(CTabFolderRenderer.PART_BODY, SWT.NONE, 0, 0, 0, 0);
int bodyRight = bodyTrim.width + bodyTrim.x;
int bodyLeft = -bodyTrim.x;
int bodyWidth = size.x - bodyLeft - bodyRight;
x = size.x - bodyRight;
int y = onBottom ? this.getSize().y - getTabHeight() + 2*bodyTrim.y : -bodyTrim.y;
availableWidth = bodyWidth;
int maxHeight = 0;
for (int i = 0; i < controls.length; i++) {
Point ctrlSize = tabControlSize[i];
if (overflow[i]) {
if (availableWidth > ctrlSize.x) {
x -= ctrlSize.x;
rects[i].width = ctrlSize.x;
rects[i].y = onBottom ? y - ctrlSize.y : y;
rects[i].height = ctrlSize.y;
rects[i].x = x;
availableWidth -= ctrlSize.x;
maxHeight = Math.max(maxHeight, ctrlSize.y);
} else {
x = size.x - bodyRight;
y += maxHeight;
maxHeight = 0;
availableWidth = bodyWidth;
if (availableWidth > ctrlSize.x) {
//Relayout this control in the next line
i--;
} else {
ctrlSize = controls[i].isDisposed() ? new Point(0,0) : controls[i].computeSize(bodyWidth, SWT.DEFAULT);
rects[i].width = bodyWidth;
rects[i].y = onBottom ? y - ctrlSize.y : y;
rects[i].height = ctrlSize.y;
rects[i].x = size.x - ctrlSize.x - bodyRight;
y += ctrlSize.y;
}
}
}
}
if (showChevron) {
int i = 0, lastIndex = -1;
while (i < priority.length && items[priority[i]].showing) {
lastIndex = Math.max(lastIndex, priority[i++]);
}
if (lastIndex == -1) lastIndex = selectedIndex;
if (lastIndex != -1) {
CTabItem lastItem = items[lastIndex];
int w = lastItem.x + lastItem.width + SPACING;
if (!simple && lastIndex == selectedIndex) w -= (renderer.curveIndent - 7);
rects[controls.length - 1].x = w;
}
}
if (position != null) position[0] = overflow;
return rects;
}
int getControlHeight(Point ctrlSize) {
return fixedTabHeight == SWT.DEFAULT ? Math.max(tabHeight - 1, ctrlSize.y) : ctrlSize.y;
}
/*
* This class was not intended to be subclassed but this restriction
* cannot be enforced without breaking backward compatibility.
*/
//protected void checkSubclass () {
// String name = getClass ().getName ();
// int index = name.lastIndexOf ('.');
// if (!name.substring (0, index + 1).equals ("org.eclipse.swt.custom.")) {
// SWT.error (SWT.ERROR_INVALID_SUBCLASS);
// }
//}
@Override
public Rectangle computeTrim (int x, int y, int width, int height) {
checkWidget();
Rectangle trim = renderer.computeTrim(CTabFolderRenderer.PART_BODY, SWT.NONE, x, y, width, height);
Point size = new Point(width, height);
int wrapHeight = getWrappedHeight(size);
if (onBottom) {
trim.height += wrapHeight;
} else {
trim.y -= wrapHeight;
trim.height += wrapHeight;
}
return trim;
}
Image createButtonImage(Display display, int button) {
GC tempGC = new GC (this);
Point size = renderer.computeSize(button, SWT.NONE, tempGC, SWT.DEFAULT, SWT.DEFAULT);
tempGC.dispose();
Rectangle trim = renderer.computeTrim(button, SWT.NONE, 0, 0, 0, 0);
Image image = new Image (display, size.x - trim.width, size.y - trim.height);
GC gc = new GC (image);
Color transColor = renderer.parent.getBackground();
gc.setBackground(transColor);
gc.fillRectangle(image.getBounds());
renderer.draw(button, SWT.NONE, new Rectangle(trim.x, trim.y, size.x, size.y), gc);
gc.dispose ();
final ImageData imageData = image.getImageData (DPIUtil.getDeviceZoom ());
imageData.transparentPixel = imageData.palette.getPixel(transColor.getRGB());
image.dispose();
image = new Image(display, new AutoScaleImageDataProvider(display, imageData, DPIUtil.getDeviceZoom()));
return image;
}
private void notifyItemCountChange() {
CTabFolderEvent e = new CTabFolderEvent(this);
for (CTabFolder2Listener listener : folderListeners) {
listener.itemsCount(e);
}
}
void createItem (CTabItem item, int index) {
if (0 > index || index > getItemCount ())SWT.error (SWT.ERROR_INVALID_RANGE);
item.parent = this;
CTabItem[] newItems = new CTabItem [items.length + 1];
System.arraycopy(items, 0, newItems, 0, index);
newItems[index] = item;
System.arraycopy(items, index, newItems, index + 1, items.length - index);
items = newItems;
if (selectedIndex >= index) selectedIndex ++;
int[] newPriority = new int[priority.length + 1];
int next = 0, priorityIndex = priority.length;
for (int element : priority) {
if (!mru && element == index) {
priorityIndex = next++;
}
newPriority[next++] = element >= index ? element + 1 : element;
}
newPriority[priorityIndex] = index;
priority = newPriority;
if (items.length == 1) {
updateFolder(UPDATE_TAB_HEIGHT | REDRAW);
} else {
updateFolder(REDRAW_TABS);
}
notifyItemCountChange();
}
void destroyItem (CTabItem item) {
if (inDispose) return;
int index = indexOf(item);
if (index == -1) return;
if (items.length == 1) {
items = new CTabItem[0];
priority = new int[0];
firstIndex = -1;
selectedIndex = -1;
Control control = item.control;
if (control != null && !control.isDisposed()) {
control.setVisible(false);
}
setToolTipText(null);
updateButtons();
setButtonBounds();
redraw();
notifyItemCountChange();
return;
}
CTabItem[] newItems = new CTabItem [items.length - 1];
System.arraycopy(items, 0, newItems, 0, index);
System.arraycopy(items, index + 1, newItems, index, items.length - index - 1);
items = newItems;
int[] newPriority = new int[priority.length - 1];
int next = 0;
for (int element : priority) {
if (element == index) continue;
newPriority[next++] = element > index ? element - 1 : element;
}
priority = newPriority;
// move the selection if this item is selected
if (selectedIndex == index) {
Control control = item.getControl();
selectedIndex = -1;
int nextSelection = mru ? priority[0] : Math.max(0, index - 1);
setSelection(nextSelection, true);
if (control != null && !control.isDisposed()) {
control.setVisible(false);
}
} else if (selectedIndex > index) {
selectedIndex --;
}
requestLayout();
updateFolder(UPDATE_TAB_HEIGHT | REDRAW_TABS);
notifyItemCountChange();
}
/**
* Returns <code>true</code> if the receiver's border is visible.
*
* @return the receiver's border visibility state
*
* @exception SWTException <ul>
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
* </ul>
*
* @since 3.0
*/
public boolean getBorderVisible() {
checkWidget();
return borderVisible;
}
ToolBar getChevron() {
if (chevronTb == null) {
chevronTb = new ToolBar(this, SWT.FLAT);
initAccessibleChevronTb();
addTabControl(chevronTb, SWT.TRAIL, -1, false);
}
if (chevronItem == null) {
chevronItem = new ToolItem(chevronTb, SWT.PUSH);
chevronItem.setToolTipText(SWT.getMessage("SWT_ShowList"));
chevronItem.addListener(SWT.Selection, listener);
}
return chevronTb;
}
/**
* Returns <code>true</code> if the chevron button
* is visible when necessary.
*
* @return the visibility of the chevron button
*
* @exception SWTException <ul>
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
* </ul>
*/
/*public*/ boolean getChevronVisible() {
checkWidget();
return chevronVisible;
}
@Override
public Rectangle getClientArea() {
checkWidget();
//TODO: HACK - find a better way to get padding
Rectangle trim = renderer.computeTrim(CTabFolderRenderer.PART_BODY, SWT.FILL, 0, 0, 0, 0);
Point size = getSize();
int wrapHeight = getWrappedHeight(size);
if (onBottom) {
trim.height += wrapHeight;
} else {
trim.y -= wrapHeight;
trim.height += wrapHeight;
}
if (minimized) return new Rectangle(-trim.x, -trim.y, 0, 0);
int width = size.x - trim.width;
int height = size.y - trim.height;
return new Rectangle(-trim.x, -trim.y, width, height);
}
/**
* Return the tab that is located at the specified index.
*
* @param index the index of the tab item
* @return the item at the specified index
*
* @exception IllegalArgumentException <ul>
* <li>ERROR_INVALID_RANGE - if the index is out of range</li>
* </ul>
* @exception SWTException <ul>
* <li>ERROR_THREAD_INVALID_ACCESS when called from the wrong thread</li>
* <li>ERROR_WIDGET_DISPOSED when the widget has been disposed</li>
* </ul>
*/
public CTabItem getItem (int index) {
/*
* This call is intentionally commented out, to allow this getter method to be
* called from a thread which is different from one that created the widget.
*/
//checkWidget();
if (index < 0 || index >= items.length)
SWT.error(SWT.ERROR_INVALID_RANGE);
return items [index];
}
/**
* Gets the item at a point in the widget.
*
* @param pt the point in coordinates relative to the CTabFolder
* @return the item at a point or null
*
* @exception SWTException <ul>
* <li>ERROR_THREAD_INVALID_ACCESS when called from the wrong thread</li>
* <li>ERROR_WIDGET_DISPOSED when the widget has been disposed</li>
* </ul>
*/
public CTabItem getItem (Point pt) {
/*
* This call is intentionally commented out, to allow this getter method to be
* called from a thread which is different from one that created the widget.
*/
//checkWidget();
if (items.length == 0) return null;
runUpdate();
Point size = getSize();
Rectangle trim = renderer.computeTrim(CTabFolderRenderer.PART_BORDER, SWT.NONE, 0, 0, 0, 0);
if (size.x <= trim.width) return null;
for (int element : priority) {
CTabItem item = items[element];
Rectangle rect = item.getBounds();
if (rect.contains(pt)) return item;
}
return null;
}
/**
* Return the number of tabs in the folder.
*
* @return the number of tabs in the folder
*
* @exception SWTException <ul>
* <li>ERROR_THREAD_INVALID_ACCESS when called from the wrong thread</li>
* <li>ERROR_WIDGET_DISPOSED when the widget has been disposed</li>
* </ul>
*/
public int getItemCount(){
//checkWidget();
return items.length;
}
/**
* Return the tab items.
*
* @return the tab items
*
* @exception SWTException <ul>
* <li>ERROR_THREAD_INVALID_ACCESS when called from the wrong thread</li>
* <li>ERROR_WIDGET_DISPOSED when the widget has been disposed</li>
* </ul>
*/
public CTabItem [] getItems() {
/*
* This call is intentionally commented out, to allow this getter method to be
* called from a thread which is different from one that created the widget.
*/
//checkWidget();
CTabItem[] tabItems = new CTabItem [items.length];
System.arraycopy(items, 0, tabItems, 0, items.length);
return tabItems;
}
int getLeftItemEdge (GC gc, int part){
Rectangle trim = renderer.computeTrim(part, SWT.NONE, 0, 0, 0, 0);
int x = -trim.x;
int width = 0;
for (int i = 0; i < controls.length; i++) {
if ((controlAlignments[i] & SWT.LEAD) != 0 && !controls[i].isDisposed() && controls[i].getVisible()) {
width += controls[i].computeSize(SWT.DEFAULT, SWT.DEFAULT).x;
}
}
if (width != 0) width += SPACING * 2;
x += width;
return Math.max(0, x);
}
/*
* Return the lowercase of the first non-'&' character following
* an '&' character in the given string. If there are no '&'
* characters in the given string, return '\0'.
*/
char _findMnemonic (String string) {
if (string == null) return '\0';