demo-menus.lua 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. return function(parent, dir)
  2. local lgi = require 'lgi'
  3. local Gtk = lgi.Gtk
  4. local function create_menu(depth, tearoff)
  5. if depth < 1 then return nil end
  6. local menu = Gtk.Menu()
  7. if tearoff then
  8. menu:append(Gtk.TearoffMenuItem { visible = true })
  9. end
  10. local group = nil
  11. for i = 1, 5 do
  12. local item = Gtk.RadioMenuItem {
  13. group = group,
  14. label = ("item %2d - %d"):format(depth, i),
  15. submenu = create_menu(depth - 1, true),
  16. sensitive = (i ~= 4),
  17. }
  18. if not group then group = item end
  19. menu:append(item)
  20. end
  21. return menu
  22. end
  23. local window = Gtk.Window {
  24. title = "Menus",
  25. Gtk.Box {
  26. orientation = 'VERTICAL',
  27. Gtk.MenuBar {
  28. id = 'menubar',
  29. Gtk.MenuItem {
  30. label = "test\nline2",
  31. visible = true,
  32. submenu = create_menu(2, true),
  33. },
  34. Gtk.MenuItem {
  35. label = "foo",
  36. visible = true,
  37. submenu = create_menu(3),
  38. },
  39. Gtk.MenuItem {
  40. label = "bar",
  41. visible = true,
  42. submenu = create_menu(4, true),
  43. },
  44. },
  45. Gtk.Box {
  46. orientation = 'VERTICAL',
  47. spacing = 10,
  48. border_width = 10,
  49. Gtk.Button {
  50. id = 'flip',
  51. label = "Flip",
  52. },
  53. Gtk.Button {
  54. id = 'close',
  55. label = "Close",
  56. },
  57. },
  58. },
  59. }
  60. function window.child.close:on_clicked()
  61. window:destroy()
  62. end
  63. function window.child.flip:on_clicked()
  64. local menubar = window.child.menubar
  65. local orientation = menubar.parent.orientation
  66. orientation = (orientation == 'HORIZONTAL'
  67. and 'VERTICAL' or 'HORIZONTAL')
  68. menubar.parent.orientation = orientation
  69. menubar.pack_direction = (orientation == 'VERTICAL'
  70. and 'LTR' or 'TTB')
  71. end
  72. window:show_all()
  73. return window
  74. end,
  75. "Menus",
  76. table.concat {
  77. [[There are several widgets involved in displaying menus. ]],
  78. [[The Gtk.MenuBar widget is a menu bar, which normally appears ]],
  79. [[horizontally at the top of an application, but can also be ]],
  80. [[layed out vertically. The Gtk.Menu widget is the actual menu ]],
  81. [[that pops up. Both Gtk.MenuBar and Gtk.Menu are subclasses ]],
  82. [[of Gtk.MenuShell; a Gtk.MenuShell contains menu items (Gtk.MenuItem). ]],
  83. [[Each menu item contains text and/or images and can be selected ]],
  84. [[by the user.
  85. ]],
  86. [[There are several kinds of menu item, including plain ]],
  87. [[Gtk.MenuItem, Gtk.CheckMenuItem which can be checked/unchecked, ]],
  88. [[Gtk.RadioMenuItem which is a check menu item that's in a mutually ]],
  89. [[exclusive group, Gtk.SeparatorMenuItem which is a separator bar, ]],
  90. [[Gtk.TearoffMenuItem which allows a Gtk.Menu to be torn off, ]],
  91. [[and Gtk.ImageMenuItem which can place a Gtk.Image or other widget ]],
  92. [[next to the menu text.
  93. ]],
  94. [[A Gtk.MenuItem can have a submenu, which is simply a Gtk.Menu ]],
  95. [[to pop up when the menu item is selected. Typically, all menu ]],
  96. [[items in a menu bar have submenus.
  97. ]],
  98. [[Gtk.UIManager provides a higher-level interface for creating ]],
  99. [[menu bars and menus; while you can construct menus manually, ]],
  100. [[most people don't do that. There's a separate demo for ]],
  101. [[Gtk.UIManager.]],
  102. }