demo-sizegroup.lua 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. return function(parent, dir)
  2. local lgi = require 'lgi'
  3. local Gtk = lgi.Gtk
  4. local window = Gtk.Dialog {
  5. title = "Gtk.SizeGroup",
  6. transient_for = parent,
  7. buttons = {
  8. { Gtk.STOCK_CLOSE, Gtk.ResponseType.NONE },
  9. },
  10. resizable = false,
  11. on_response = Gtk.Widget.destroy,
  12. }
  13. window:get_content_area():add(
  14. Gtk.Box {
  15. orientation = 'VERTICAL',
  16. border_width = 5,
  17. spacing = 5,
  18. Gtk.Frame {
  19. label = "Color Options",
  20. Gtk.Grid {
  21. id = 'colors',
  22. row_spacing = 5,
  23. column_spacing = 10,
  24. }
  25. },
  26. Gtk.Frame {
  27. label = "Line Options",
  28. Gtk.Grid {
  29. id = 'lines',
  30. row_spacing = 5,
  31. column_spacing = 10,
  32. }
  33. },
  34. Gtk.CheckButton {
  35. id = 'enable_grouping',
  36. label = "_Enable grouping",
  37. use_underline = true,
  38. active = true,
  39. },
  40. })
  41. local size_group = Gtk.SizeGroup { mode = 'HORIZONTAL' }
  42. local function add_row(grid, row, label, strings)
  43. local combo = Gtk.ComboBoxText {}
  44. for _, text in ipairs(strings) do
  45. combo:append_text(text)
  46. end
  47. combo.active = 0
  48. size_group:add_widget(combo)
  49. grid:add {
  50. left_attach = 0, top_attach = row,
  51. Gtk.Label {
  52. label = label,
  53. use_underline = true,
  54. halign = 'START',
  55. valign = 'END',
  56. hexpand = true,
  57. mnemonic_widget = combo,
  58. }
  59. }
  60. grid:add {
  61. left_attach = 1, top_attach = row,
  62. combo
  63. }
  64. end
  65. add_row(window.child.colors, 0, "_Foreground", {
  66. "Red", "Green", "Blue",
  67. })
  68. add_row(window.child.colors, 1, "_Background", {
  69. "Red", "Green", "Blue",
  70. })
  71. add_row(window.child.lines, 0, "_Dashing", {
  72. "Solid", "Dashed", "Dotted",
  73. })
  74. add_row(window.child.lines, 1, "_Line ends", {
  75. "Square", "Round", "Arrow",
  76. })
  77. function window.child.enable_grouping:on_toggled()
  78. size_group.mode = self.active and 'HORIZONTAL' or 'NONE'
  79. end
  80. window:show_all()
  81. return window
  82. end,
  83. "Size Groups",
  84. table.concat {
  85. [[Gtk.SizeGroup provides a mechanism for grouping a number of widgets ]],
  86. [[together so they all request the same amount of space. This is ]],
  87. [[typically useful when you want a column of widgets to have the same ]],
  88. [[size, but you can't use a Gtk.Grid widget.
  89. ]],
  90. [[Note that size groups only affect the amount of space requested, ]],
  91. [[not the size that the widgets finally receive. If you want ]],
  92. [[the widgets in a Gtk.SizeGroup to actually be the same size, ]],
  93. [[you need to pack them in such a way that they get the size they ]],
  94. [[request and not more. For example, if you are packing your widgets ]],
  95. [[into a grid, you would not include the 'FILL' flag.]],
  96. }