Skip to content

Commit 05a6a14

Browse files
committed
A more extensive fix for #3692
Previous fix didn't cover the auto-sizing method.
1 parent 6957728 commit 05a6a14

File tree

1 file changed

+26
-20
lines changed

1 file changed

+26
-20
lines changed

CodenameOne/src/com/codename1/ui/layouts/GridLayout.java

+26-20
Original file line numberDiff line numberDiff line change
@@ -161,43 +161,49 @@ public static Container encloseIn(int columns, Component... cmp) {
161161
private boolean isLandscapeMode() {
162162
return landscapeRows > -1 && (!Display.getInstance().isPortrait());
163163
}
164-
165-
private void autoSizeCols(Container parent, int width, boolean landscapeMode) {
164+
165+
private int autoSizeCols(Container parent, int width, boolean landscapeMode) {
166+
int numOfcomponents = parent.getComponentCount();
167+
int totalComponentCount = numOfcomponents;
166168
if(isAutoFit()) {
167-
int numOfcomponents = parent.getComponentCount();
168169
int maxWidth = 0;
169170
for(int iter = 0 ; iter < numOfcomponents ; iter++) {
170171
Component cmp = parent.getComponentAt(iter);
171-
Style s = cmp.getStyle();
172-
maxWidth = Math.max(cmp.getPreferredW() + s.getHorizontalMargins(), maxWidth);
172+
if(hideZeroSized && cmp.isHidden()) {
173+
totalComponentCount--;
174+
} else {
175+
Style s = cmp.getStyle();
176+
maxWidth = Math.max(cmp.getPreferredW() + s.getHorizontalMargins(), maxWidth);
177+
}
173178
}
174179
if(width < maxWidth) {
175180
width = Display.getInstance().getDisplayWidth();
176181
}
177182
if(landscapeMode) {
178-
// prevent arithmentic exception
183+
// prevent arithmetic exception
179184
if(maxWidth <= 0) {
180185
landscapeColumns = 1;
181186
} else {
182187
landscapeColumns = Math.max(width / maxWidth, 1);
183188
}
184-
landscapeRows = Math.max(1, numOfcomponents / landscapeColumns);
185-
if(numOfcomponents % landscapeColumns > 0 && numOfcomponents > landscapeColumns) {
189+
landscapeRows = Math.max(1, totalComponentCount / landscapeColumns);
190+
if(totalComponentCount % landscapeColumns > 0 && totalComponentCount > landscapeColumns) {
186191
landscapeRows++;
187-
}
192+
}
188193
} else {
189194
// prevent arithmentic exception
190195
if(maxWidth <= 0) {
191196
portraitColumns = 1;
192197
} else {
193198
portraitColumns = Math.max(width / maxWidth, 1);
194199
}
195-
portraitRows = Math.max(1, numOfcomponents / portraitColumns);
196-
if(numOfcomponents % portraitColumns > 0 && numOfcomponents > portraitColumns) {
200+
portraitRows = Math.max(1, totalComponentCount / portraitColumns);
201+
if(totalComponentCount % portraitColumns > 0 && totalComponentCount > portraitColumns) {
197202
portraitRows++;
198-
}
203+
}
199204
}
200205
}
206+
return totalComponentCount;
201207
}
202208

203209
/**
@@ -276,11 +282,11 @@ public void layoutContainer(Container parent) {
276282

277283
/**
278284
* {@inheritDoc}
279-
*/
280-
public Dimension getPreferredSize(Container parent) {
285+
*/
286+
public Dimension getPreferredSize(Container parent) {
281287
int width = 0;
282288
int height = 0;
283-
289+
284290
int numOfcomponents = parent.getComponentCount();
285291
int totalComponentCount = numOfcomponents;
286292
for(int i=0; i< numOfcomponents; i++){
@@ -293,7 +299,7 @@ public Dimension getPreferredSize(Container parent) {
293299
}
294300
}
295301

296-
boolean landscapeMode = isLandscapeMode();
302+
boolean landscapeMode = isLandscapeMode();
297303
autoSizeCols(parent, parent.getWidth(), landscapeMode);
298304
int rows, columns;
299305
if(landscapeMode) {
@@ -307,18 +313,18 @@ public Dimension getPreferredSize(Container parent) {
307313
if(columns > 1){
308314
width = width*columns;
309315
}
310-
316+
311317
if(rows > 1){
312318
if(totalComponentCount>rows*columns){ //if there are more components than planned
313-
height = height * (totalComponentCount/columns + (totalComponentCount%columns == 0 ? 0 : 1));
319+
height = height * (totalComponentCount/columns + (totalComponentCount%columns == 0 ? 0 : 1));
314320
}else{
315321
height = height*rows;
316322
}
317323
}
318-
324+
319325
Style s = parent.getStyle();
320326
return new Dimension(width + s.getHorizontalPadding(),
321-
height + s.getVerticalPadding());
327+
height + s.getVerticalPadding());
322328
}
323329

324330
/**

0 commit comments

Comments
 (0)