2
$\begingroup$

In all my apps, I am facing a problem about placing the Gui controls inside Grid.

For this purpose, I wrote three functions.

The first function, createGridList, returns empty list.

createGridList[noOfRows_, noOfColumns_] := ConstantArray["", {noOfRows, noOfColumns}]

The second function, layoutGrid, returns a GraphicsGrid.

layoutGrid[noOfRows_, noOfColumns_, list_, meshFactor_] :=
(
   GraphicsGrid[
     list,
     ImageSize -> {((noOfColumns*meshFactor)), ((noOfRows*meshFactor))},
     Background -> RGBColor[.745, .886, 1]
     ]
)

The third function, PlaceControl, returns an updated list with the Gui elements.

placeControl[yCoOrdinate_, xCoOrdinate_, controlID_, controlWidth_, 
  controlHeight_, list1_, meshFactor_] :=
(
  rowNumber = yCoOrdinate/meshFactor;
  columnNumber = xCoOrdinate/meshFactor;
  rowSpan = controlWidth/meshFactor;
  columnSpan = controlHeight/meshFactor;
  list = list1;
  Table[
  (
    If[(i == rowNumber),
      (*True condition, i value is equals to rowNumber*)
      (
        If[(j == columnNumber),
        (*True condition,j value is equals to column Number*)
        (
          list = ReplacePart[list, {i, j} -> controlID];
          Table[
            (
            list = ReplacePart[list, {i, m} -> SpanFromLeft]
            ),
            {m, (j + 1), (j + (rowSpan - 1))}
          ];
          j = (columnNumber + (rowSpan - 1));
        )
        ]
      ),
      (*False condition, i value is not equals to rowNumber*)
      (
        If[((i > rowNumber) && (i <= ((rowNumber + columnSpan) - 1))),
          (*True condition, i value is in between rowNumber,(rowNumber+ColumnSpan)*)
          (
            If[(j == columnNumber),
            (*True condition, j value is equals to column Number*)
            (
              list = ReplacePart[list, {i, j} -> SpanFromAbove];
              Table[
                (
                list = ReplacePart[list, {i, m} -> SpanFromBoth]
                ),
                {m, (j + 1), (j + (rowSpan - 1))}
              ];
              j = (columnNumber + (rowSpan - 1));
            )
            ]
         )
       ]
       )
    ]
    ),
    {i, 1, Length[list]}, {j, 1, Length[Part[list, 1]]}
  ];
  list
)   

I am calling the above function.that corresponding code is following below.

(*The following variable specifies the no of rows in grid*)
noOfRows =20;
(*The following variable specifies the no of columns in grid*)
noOfColumns = 20;
(*The following variable specifies the Button visualization*)
buttonElement = Button["Ok", "", ImageSize -> {150, 60}];
(*The following variable specifies the each cell size in grid*)
cellSize = 10;
(*The following code corresponds to the calling the above functions*)
list = createGridList[noOfRows, noOfColumns];
list = Quiet[placeControl[20, 40, Button["Ok", "", ImageSize -> {150, 60}], 
  150, 60, list, cellSize]];
layoutGrid[noOfRows, noOfColumns, list, cellSize]

Visualization-wise and logical-wise everything is fine, but the problem is: If you place so many Gui-elements inside a GraphicsGrid, it takes more time for evaluation.

My understanding is that GraphicsGrid slows down because GraphicsGrid converts all the Gui-elements into Graphics,

How can I reduce and improve the code?

$\endgroup$
2
  • $\begingroup$ Where is the function layoutGrid defined? $\endgroup$
    – bill s
    Commented Apr 27, 2013 at 15:27
  • $\begingroup$ can't you simply use grid, instead of graphics grid? $\endgroup$
    – magma
    Commented Apr 27, 2013 at 15:27

1 Answer 1

1
$\begingroup$

Use Grid instead of GraphicsGrid

$\endgroup$
1
  • $\begingroup$ but we can't set the ImageSize for the Grid,as Grid don't have ImageSize option. $\endgroup$
    – subbu
    Commented Apr 27, 2013 at 17:00

Not the answer you're looking for? Browse other questions tagged or ask your own question.