@@ -67,8 +67,10 @@ public class GridLayout extends Layout{
6767 private boolean fillLastRow ;
6868 private int portraitRows ;
6969 private int portraitColumns ;
70+
7071 private int landscapeRows = -1 ;
7172 private int landscapeColumns = -1 ;
73+
7274 /**
7375 * When set to true components that have 0 size will be hidden and won't occupy a cell within the grid. This
7476 * makes animating a grid layout component MUCH easier.
@@ -207,68 +209,55 @@ private int autoSizeCols(Container parent, int width, boolean landscapeMode) {
207209 /**
208210 * {@inheritDoc}
209211 */
210- @ Override
211212 public void layoutContainer (Container parent ) {
212- Style s = parent .getStyle ();
213- int width = parent .getLayoutWidth () - parent .getSideGap () - s .getHorizontalPadding ();
214- int height = parent .getLayoutHeight () - parent .getBottomGap () - s .getVerticalPadding ();
213+ Style s = parent .getStyle ();
214+ int width = parent .getLayoutWidth () - parent .getSideGap () - s .getHorizontalPadding ();
215+ int height = parent .getLayoutHeight () - parent .getBottomGap () - s .getVerticalPadding ();
215216 int numOfcomponents = parent .getComponentCount ();
216217
217218 boolean landscapeMode = isLandscapeMode ();
218219 autoSizeCols (parent , width , landscapeMode );
219- //-----------------------------------------------------
220- //Checking the hidden components to recalculate rows.
221- //-----------------------------------------------------
222- int totalComponentCountVisible = numOfcomponents ;
223- for (int i = 0 ; i < numOfcomponents ; i ++) {
224- Component cmp = parent .getComponentAt (i );
225- if (hideZeroSized && cmp .isHidden ()) {
226- totalComponentCountVisible --;
227- }
220+
221+ int rows , columns ;
222+ if (landscapeMode ) {
223+ rows = landscapeRows ;
224+ columns = landscapeColumns ;
225+ } else {
226+ rows = portraitRows ;
227+ columns = portraitColumns ;
228228 }
229- //--------------------------
230- //Calculating dinamic rows
231- // Solution to issue : https://github.com/codenameone/CodenameOne/issues/3692
232- // Date: 12-04-23
233- //--------------------------
234- CalculateDinamicRowsLayout rc = new CalculateDinamicRowsLayout (totalComponentCountVisible , (landscapeMode ? landscapeColumns : portraitColumns ));
235- int rows = rc .getRows ();
236- int columns = rc .getColumns ();
237- //--------------------------
229+
238230 int x = s .getPaddingLeft (parent .isRTL ());
239231 int y = s .getPaddingTop ();
240232
241233 boolean rtl = parent .isRTL ();
242234 if (rtl ) {
243- x += parent .getSideGap ();
235+ x += parent .getSideGap ();
244236 }
245237 int localColumns = columns ;
246238 int cmpWidth = width / columns ;
247- int cmpHeight = cmpHeight = height / rows ;
248-
249- int row = 0 ;
239+ int cmpHeight ;
240+ if (numOfcomponents > rows * columns ) {
241+ // actual rows number
242+ cmpHeight = height / (numOfcomponents / columns + (numOfcomponents % columns == 0 ? 0 : 1 ));
243+ } else {
244+ cmpHeight = height / rows ;
245+ }
246+ int row = 0 ;
247+
250248 int offset = 0 ;
251249 for (int iter = 0 ; iter < numOfcomponents ; iter ++){
252- Component cmp = parent .getComponentAt (iter );
253- Style cmpStyle = cmp .getStyle ();
254- int marginLeft = cmpStyle .getMarginLeft (parent .isRTL ());
255- int marginTop = cmpStyle .getMarginTop ();
256- int marginRight = cmpStyle .getMarginRight (parent .isRTL ());
257- int marginBottom = cmpStyle .getMarginBottom ();
258-
250+ Component cmp = parent .getComponentAt (iter );
251+ Style cmpStyle = cmp .getStyle ();
252+ int marginLeft = cmpStyle .getMarginLeft (parent .isRTL ());
253+ int marginTop = cmpStyle .getMarginTop ();
259254 if (hideZeroSized ) {
260255 if (cmp .isHidden ()) {
261256 continue ;
262257 }
263258 }
264- //---------------------------------------------------
265- //Setting component size
266- //---------------------------------------------------
267- cmp .setWidth (cmpWidth - marginLeft - marginRight );
268- cmp .setHeight (cmpHeight - marginTop - marginBottom );
269- //---------------------------------------------------
270- //Setting component position
271- //---------------------------------------------------
259+ cmp .setWidth (cmpWidth - marginLeft - cmpStyle .getMarginRight (parent .isRTL ()));
260+ cmp .setHeight (cmpHeight - marginTop - cmpStyle .getMarginBottom ());
272261 if (rtl ) {
273262 cmp .setX (x + (localColumns - 1 - (offset % localColumns )) * cmpWidth + marginLeft );
274263 } else {
@@ -277,6 +266,7 @@ public void layoutContainer(Container parent) {
277266 cmp .setY (y + row * cmpHeight + marginTop );
278267 if ((offset + 1 ) % columns == 0 ){
279268 row ++;
269+
280270 // check if we need to recalculate component widths
281271 if (fillLastRow && row == rows - 1 ) {
282272 localColumns = numOfcomponents % columns ;
@@ -302,38 +292,39 @@ public Dimension getPreferredSize(Container parent) {
302292 for (int i =0 ; i < numOfcomponents ; i ++){
303293 Component cmp = parent .getComponentAt (i );
304294 if (hideZeroSized && cmp .isHidden ()) {
295+ totalComponentCount --;
305296 } else {
306297 width = Math .max (width , cmp .getPreferredW () + cmp .getStyle ().getMarginLeftNoRTL () + cmp .getStyle ().getMarginRightNoRTL ());
307298 height = Math .max (height , cmp .getPreferredH () + cmp .getStyle ().getMarginTop () + cmp .getStyle ().getMarginBottom ());
308299 }
309300 }
310- //-----------------------------------------------------
311301
312302 boolean landscapeMode = isLandscapeMode ();
313303 autoSizeCols (parent , parent .getWidth (), landscapeMode );
314- //--------------------------
315- //Calculating dinamic rows
316- // Solution to issue : https://github.com/codenameone/CodenameOne/issues/3692
317- // Date: 12-04-23
318- //--------------------------
319- CalculateDinamicRowsLayout rc = new CalculateDinamicRowsLayout ( totalComponentCountVisible , ( landscapeMode ? landscapeColumns : portraitColumns )) ;
320- int rows = rc . getRows () ;
321- int columns = rc . getColumns ();
322- //--------------------------
304+ int rows , columns ;
305+ if ( landscapeMode ) {
306+ rows = landscapeRows ;
307+ columns = landscapeColumns ;
308+ } else {
309+ rows = portraitRows ;
310+ columns = portraitColumns ;
311+ }
312+
323313 if (columns > 1 ){
324314 width = width *columns ;
325315 }
326- totalComponentCount --;
327- if (rows > 1 ){
328316
317+ if (rows > 1 ){
318+ if (totalComponentCount >rows *columns ){ //if there are more components than planned
319+ height = height * (totalComponentCount /columns + (totalComponentCount %columns == 0 ? 0 : 1 ));
329320 }else {
330321 height = height *rows ;
331322 }
332323 }
333324
334325 Style s = parent .getStyle ();
335- if ( totalComponentCount > rows * columns ){ //if there are more components than planned
336- height = height * ( totalComponentCount / columns + ( totalComponentCount % columns == 0 ? 0 : 1 ));
326+ return new Dimension ( width + s . getHorizontalPadding (),
327+ height + s . getVerticalPadding ( ));
337328 }
338329
339330 /**
@@ -425,38 +416,4 @@ public boolean isHideZeroSized() {
425416 public void setHideZeroSized (boolean hideZeroSized ) {
426417 this .hideZeroSized = hideZeroSized ;
427418 }
428-
429- /**
430- * used to calculate the dinamic rows
431- */
432- public class CalculateDinamicRowsLayout {
433- int rows = 1 ;
434- int columns = 1 ;
435- /**
436- *
437- * @param qtyComponents quantity of components in layout
438- * @param maxColums max columns
439- */
440- //-------------------------------
441- public CalculateDinamicRowsLayout (int qtyComponents , int maxColums ) {
442- if (qtyComponents > maxColums ) {
443- rows = qtyComponents / maxColums ;
444- if (!(qtyComponents % maxColums == 0 )) {
445- rows ++;
446- }
447- }
448- columns = maxColums ;
449- }
450- //-------------------------------
451- public int getRows () {
452- return rows ;
453- }
454- //-------------------------------
455- public int getColumns () {
456- return columns ;
457- }
458- //-------------------------------
459- }//endClass
460- }
461- return new Dimension (width + s .getHorizontalPadding (),
462- height + s .getVerticalPadding ());
419+ }
0 commit comments