| 1 | = Gtk Box Packing = |
| 2 | |
| 3 | Two useful widgets for packing are `HBox`, for packing widgets across the screen, and `VBox`, for packing widgets down the screen. Widgets can be added to these boxes using `packStart`. Parameters to the box constructor and `packStart` control how the packed widgets are laid out, and how they change when the containing window changes size. |
| 4 | |
| 5 | * `HBox/VBox` take two parameters: |
| 6 | 1. boolean parameter, `homogeneous`, to indicate if widgets should all be the same size |
| 7 | 2. integer parameter, `spacing`, to give the amount of space between widgets |
| 8 | * `packStart` takes four parameters: |
| 9 | 1. the widget to pack |
| 10 | 2. boolean parameter, `expand`, to indicate if widgets should expand if there is more space available |
| 11 | 3. boolean parameter, `fill`, to indicate if widgets should fill the space available to them (only applies if `expand` is true) |
| 12 | 4. integer parameter, `padding`, to give the amount of space between widgets |
| 13 | |
| 14 | The following program is used for illustration: parameters are changed in the `addWidgets` method to show different packing behaviours. |
| 15 | |
| 16 | {{{ |
| 17 | #!cobra |
| 18 | # @args -pkg:gtk-sharp-2.0 # remove initial '#' |
| 19 | use Gtk |
| 20 | |
| 21 | class DemoWindow inherits Window |
| 22 | cue init |
| 23 | base.init("Demo 1") |
| 24 | .setDefaultSize(200, 100) |
| 25 | listen .deleteEvent, ref .onDeleteEvent |
| 26 | .addWidgets |
| 27 | |
| 28 | def addWidgets |
| 29 | box = HBox(false, 0) # create a box to hold widgets |
| 30 | button1 = Button("Gtk") |
| 31 | button2 = Button("is a graphical user interface toolkit") |
| 32 | |
| 33 | box.packStart(button1, false, false, 0) # add button1 to box |
| 34 | box.packStart(button2, false, false, 0) # add button2 to box |
| 35 | .add(box) # add the box to our window |
| 36 | |
| 37 | def onDeleteEvent(obj, args as DeleteEventArgs) |
| 38 | Application.quit |
| 39 | |
| 40 | class MainProgram |
| 41 | def main |
| 42 | Application.init |
| 43 | window = DemoWindow() |
| 44 | window.showAll |
| 45 | Application.run |
| 46 | }}} |
| 47 | |
| 48 | == Example 1 == |
| 49 | |
| 50 | With the following parameters, the widgets in the box can be different sizes (HBox has homogeneous set to false), and neither |
| 51 | button will expand to fill their surrounding space when the window is resized. |
| 52 | |
| 53 | {{{ |
| 54 | #!cobra |
| 55 | box = HBox(false, 0) |
| 56 | box.packStart(button1, false, false, 0) |
| 57 | box.packStart(button2, false, false, 0) |
| 58 | }}} |
| 59 | |
| 60 | [[Image(packing1.png)]] |
| 61 | |
| 62 | == Example 2 == |
| 63 | |
| 64 | By changing the box definition so homogeneous is true, the two buttons will have the same size. |
| 65 | |
| 66 | {{{ |
| 67 | #!cobra |
| 68 | box = HBox(true, 0) |
| 69 | box.packStart(button1, false, false, 0) |
| 70 | box.packStart(button2, false, false, 0) |
| 71 | }}} |
| 72 | |
| 73 | [[Image(packing2.png)]] |
| 74 | |
| 75 | == Example 3 == |
| 76 | |
| 77 | By packing the first button with a true value for the expand property, the button's area will grow to fill the available space in the window. However, the button itself does not increase in size. |
| 78 | |
| 79 | {{{ |
| 80 | #!cobra |
| 81 | box = HBox(false, 0) |
| 82 | box.packStart(button1, true, false, 0) |
| 83 | box.packStart(button2, false, false, 0) |
| 84 | }}} |
| 85 | |
| 86 | [[Image(packing3.png)]] |
| 87 | |
| 88 | == Example 4 == |
| 89 | |
| 90 | By packing the first button with a true value for the expand and fill properties, the button will grow to fill the available space in the window. |
| 91 | |
| 92 | {{{ |
| 93 | #!cobra |
| 94 | box = HBox(false, 0) |
| 95 | box.packStart(button1, true, true, 0) |
| 96 | box.packStart(button2, false, false, 0) |
| 97 | }}} |
| 98 | |
| 99 | [[Image(packing4.png)]] |
| 100 | |
| 101 | == Example 5 == |
| 102 | |
| 103 | Set the `spacing` parameter for the box to put some space in between the widgets. |
| 104 | |
| 105 | {{{ |
| 106 | #!cobra |
| 107 | box = HBox(true, 20) |
| 108 | box.packStart(button1, true, true, 0) |
| 109 | box.packStart(button2, true, true, 0) |
| 110 | }}} |
| 111 | |
| 112 | [[Image(packing5.png)]] |
| 113 | |
| 114 | == Example 6 == |
| 115 | |
| 116 | Set the `padding` parameter in `packStart` to put space on either side of the widget. |
| 117 | |
| 118 | {{{ |
| 119 | #!cobra |
| 120 | box = HBox(true, 0) |
| 121 | box.packStart(button1, true, true, 20) |
| 122 | box.packStart(button2, true, true, 20) |
| 123 | }}} |
| 124 | |
| 125 | [[Image(packing6.png)]] |
| 126 | |
| 127 | == And There's More ... == |
| 128 | |
| 129 | Complex layouts can be managed by nesting boxes, and adjusting the parameters illustrated above. |
| 130 | |
| 131 | * `packStart` packs widgets by starting from the left (for `HBox`) or top (for `VBox`) and working to the right (or down). |
| 132 | |
| 133 | * `packEnd` is also available, to start packing from the right or bottom of the container. |