Skip to content

Commit 15c0a34

Browse files
committed
[Gtk] Use foreach
Make the code slightly more readable
1 parent 9eea8f6 commit 15c0a34

File tree

3 files changed

+33
-60
lines changed
  • bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets
  • examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/controlexample

3 files changed

+33
-60
lines changed

bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/Canvas.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -425,9 +425,7 @@ public void scroll (int destX, int destY, int x, int y, int width, int height, b
425425
Cairo.cairo_region_destroy (copyRegion);
426426
Cairo.cairo_region_destroy (invalidateRegion);
427427
if (all) {
428-
Control [] children = _getChildren ();
429-
for (int i=0; i<children.length; i++) {
430-
Control child = children [i];
428+
for (Control child : _getChildren ()) {
431429
Rectangle rect = child.getBoundsInPixels ();
432430
if (Math.min(x + width, rect.x + rect.width) >= Math.max (x, rect.x) &&
433431
Math.min(y + height, rect.y + rect.height) >= Math.max (y, rect.y)) {

bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/Composite.java

Lines changed: 31 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -445,8 +445,7 @@ void fixClippings () {
445445
if (fixClipHandle == 0 || fixClipMap.isEmpty()) {
446446
return;
447447
} else {
448-
Control [] children = _getChildren();
449-
for (Control child : children) {
448+
for (Control child : _getChildren()) {
450449
if (fixClipMap.containsKey(child)) {
451450
long [] childHandles = fixClipMap.get(child);
452451
for (long widget : childHandles) {
@@ -604,9 +603,7 @@ Composite findDeferredControl () {
604603
Menu [] findMenus (Control control) {
605604
if (control == this) return new Menu [0];
606605
Menu result [] = super.findMenus (control);
607-
Control [] children = _getChildren ();
608-
for (int i=0; i<children.length; i++) {
609-
Control child = children [i];
606+
for (Control child : _getChildren ()) {
610607
Menu [] menuList = child.findMenus (control);
611608
if (menuList.length != 0) {
612609
Menu [] newResult = new Menu [result.length + menuList.length];
@@ -621,9 +618,8 @@ Composite findDeferredControl () {
621618
@Override
622619
void fixChildren (Shell newShell, Shell oldShell, Decorations newDecorations, Decorations oldDecorations, Menu [] menus) {
623620
super.fixChildren (newShell, oldShell, newDecorations, oldDecorations, menus);
624-
Control [] children = _getChildren ();
625-
for (int i=0; i<children.length; i++) {
626-
children [i].fixChildren (newShell, oldShell, newDecorations, oldDecorations, menus);
621+
for (Control child : _getChildren ()) {
622+
child.fixChildren (newShell, oldShell, newDecorations, oldDecorations, menus);
627623
}
628624
}
629625

@@ -638,19 +634,17 @@ void fixParentGdkResource() {
638634

639635
@Override
640636
void fixModal(long group, long modalGroup) {
641-
Control[] controls = _getChildren ();
642-
for (int i = 0; i < controls.length; i++) {
643-
controls[i].fixModal (group, modalGroup);
637+
for (Control control : _getChildren ()) {
638+
control.fixModal (group, modalGroup);
644639
}
645640
}
646641

647642
@Override
648643
void fixStyle () {
649644
super.fixStyle ();
650645
if (scrolledHandle == 0) fixStyle (handle);
651-
Control[] children = _getChildren ();
652-
for (int i = 0; i < children.length; i++) {
653-
children [i].fixStyle ();
646+
for (Control child : _getChildren ()) {
647+
child.fixStyle ();
654648
}
655649
}
656650

@@ -865,14 +859,14 @@ public boolean getLayoutDeferred () {
865859
if (tabList == null) {
866860
int count = 0;
867861
Control [] list =_getChildren ();
868-
for (int i=0; i<list.length; i++) {
869-
if (list [i].isTabGroup ()) count++;
862+
for (Control element : list) {
863+
if (element.isTabGroup ()) count++;
870864
}
871865
tabList = new Control [count];
872866
int index = 0;
873-
for (int i=0; i<list.length; i++) {
874-
if (list [i].isTabGroup ()) {
875-
tabList [index++] = list [i];
867+
for (Control element : list) {
868+
if (element.isTabGroup ()) {
869+
tabList [index++] = element;
876870
}
877871
}
878872
}
@@ -1337,9 +1331,8 @@ void markLayout (boolean changed, boolean all) {
13371331
if (changed) state |= LAYOUT_CHANGED;
13381332
}
13391333
if (all) {
1340-
Control [] children = _getChildren ();
1341-
for (int i=0; i<children.length; i++) {
1342-
children [i].markLayout (changed, all);
1334+
for (Control child : _getChildren ()) {
1335+
child.markLayout (changed, all);
13431336
}
13441337
}
13451338
}
@@ -1380,9 +1373,7 @@ void moveBelow (long child, long sibling) {
13801373

13811374
@Override
13821375
void moveChildren(int oldWidth) {
1383-
Control[] children = _getChildren ();
1384-
for (int i = 0; i < children.length; i++) {
1385-
Control child = children[i];
1376+
for (Control child : _getChildren ()) {
13861377
long topHandle = child.topHandle ();
13871378
GtkAllocation allocation = new GtkAllocation();
13881379
GTK.gtk_widget_get_allocation (topHandle, allocation);
@@ -1416,15 +1407,14 @@ void moveChildren(int oldWidth) {
14161407
}
14171408

14181409
Point minimumSize (int wHint, int hHint, boolean changed) {
1419-
Control [] children = _getChildren ();
14201410
/*
14211411
* Since getClientArea can be overridden by subclasses, we cannot
14221412
* call getClientAreaInPixels directly.
14231413
*/
14241414
Rectangle clientArea = getClientArea ();
14251415
int width = 0, height = 0;
1426-
for (int i=0; i<children.length; i++) {
1427-
Rectangle rect = children [i].getBounds ();
1416+
for (Control child : _getChildren ()) {
1417+
Rectangle rect = child.getBounds ();
14281418
width = Math.max (width, rect.x - clientArea.x + rect.width);
14291419
height = Math.max (height, rect.y - clientArea.y + rect.height);
14301420
}
@@ -1543,9 +1533,7 @@ void propagateDraw (long container, long cairo) {
15431533
@Override
15441534
void redrawChildren () {
15451535
super.redrawChildren ();
1546-
Control [] children = _getChildren ();
1547-
for (int i = 0; i < children.length; i++) {
1548-
Control child = children [i];
1536+
for (Control child : _getChildren ()) {
15491537
if ((child.state & PARENT_BACKGROUND) != 0) {
15501538
child.redrawWidget (0, 0, 0, 0, true, false, true);
15511539
child.redrawChildren ();
@@ -1598,9 +1586,7 @@ void removeControl (Control control) {
15981586
@Override
15991587
void reskinChildren (int flags) {
16001588
super.reskinChildren (flags);
1601-
Control [] children = _getChildren ();
1602-
for (int i=0; i<children.length; i++) {
1603-
Control child = children [i];
1589+
for (Control child : _getChildren ()) {
16041590
if (child != null) child.reskin (flags);
16051591
}
16061592
}
@@ -1633,9 +1619,8 @@ void resizeHandle (int width, int height) {
16331619
public void setBackgroundMode (int mode) {
16341620
checkWidget ();
16351621
backgroundMode = mode;
1636-
Control[] children = _getChildren ();
1637-
for (int i = 0; i < children.length; i++) {
1638-
children [i].updateBackgroundMode ();
1622+
for (Control child : _getChildren ()) {
1623+
child.updateBackgroundMode ();
16391624
}
16401625
}
16411626

@@ -1676,9 +1661,7 @@ && getVisible() && !GTK.gtk_widget_get_visible(topHandle) //if SWT State is not
16761661
@Override
16771662
public boolean setFocus () {
16781663
checkWidget();
1679-
Control [] children = _getChildren ();
1680-
for (int i=0; i<children.length; i++) {
1681-
Control child = children [i];
1664+
for (Control child : _getChildren ()) {
16821665
if (child.getVisible () && child.setFocus ()) return true;
16831666
}
16841667
return super.setFocus ();
@@ -1741,9 +1724,8 @@ void setOrientation (boolean create) {
17411724
if (!create) {
17421725
int flags = SWT.RIGHT_TO_LEFT | SWT.LEFT_TO_RIGHT;
17431726
int orientation = style & flags;
1744-
Control [] children = _getChildren ();
1745-
for (int i=0; i<children.length; i++) {
1746-
children[i].setOrientation (orientation);
1727+
for (Control child : _getChildren ()) {
1728+
child.setOrientation (orientation);
17471729
}
17481730
if (((style & SWT.RIGHT_TO_LEFT) != 0) != ((style & SWT.MIRRORED) != 0)) {
17491731
moveChildren (-1);
@@ -1768,9 +1750,7 @@ boolean setTabGroupFocus (boolean next) {
17681750
if ((state & CANVAS) != 0) takeFocus = hooksKeys ();
17691751
if (socketHandle != 0) takeFocus = true;
17701752
if (takeFocus && setTabItemFocus (next)) return true;
1771-
Control [] children = _getChildren ();
1772-
for (int i=0; i<children.length; i++) {
1773-
Control child = children [i];
1753+
for (Control child : _getChildren ()) {
17741754
/*
17751755
* It is unlikely but possible that a child is disposed at this point, for more
17761756
* details refer bug 381668.
@@ -1840,9 +1820,7 @@ boolean checkSubwindow () {
18401820
boolean translateMnemonic (Event event, Control control) {
18411821
if (super.translateMnemonic (event, control)) return true;
18421822
if (control != null) {
1843-
Control [] children = _getChildren ();
1844-
for (int i=0; i<children.length; i++) {
1845-
Control child = children [i];
1823+
for (Control child : _getChildren ()) {
18461824
if (child.translateMnemonic (event, control)) return true;
18471825
}
18481826
}
@@ -1867,9 +1845,8 @@ boolean translateTraversal (long event) {
18671845
@Override
18681846
void updateBackgroundMode () {
18691847
super.updateBackgroundMode ();
1870-
Control [] children = _getChildren ();
1871-
for (int i = 0; i < children.length; i++) {
1872-
children [i].updateBackgroundMode ();
1848+
for (Control child : _getChildren ()) {
1849+
child.updateBackgroundMode ();
18731850
}
18741851
}
18751852

@@ -1888,9 +1865,8 @@ void updateLayout (boolean all) {
18881865
}
18891866
if (all) {
18901867
state &= ~LAYOUT_CHILD;
1891-
Control [] children = _getChildren ();
1892-
for (int i=0; i<children.length; i++) {
1893-
children [i].updateLayout (all);
1868+
for (Control child : _getChildren ()) {
1869+
child.updateLayout (all);
18941870
}
18951871
}
18961872
}

examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/controlexample/Tab.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,8 +341,7 @@ void createControlGroup () {
341341
} else {
342342
if (child instanceof Composite composite) {
343343
/* Look down one more level of children in the style group. */
344-
Control [] grandchildren = composite.getChildren ();
345-
for (Control grandchild : grandchildren) {
344+
for (Control grandchild : composite.getChildren ()) {
346345
if (grandchild instanceof Button button) {
347346
button.addSelectionListener (selectionListener);
348347
}

0 commit comments

Comments
 (0)