demo-uimanager.lua 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. return function(parent, dir)
  2. local lgi = require 'lgi'
  3. local Gtk = lgi.Gtk
  4. local log = lgi.log.domain('uimanager-demo')
  5. local function activate_action(action)
  6. log.message('Action "%s" activated', action.name)
  7. end
  8. local function activate_radio_action(action)
  9. log.message('Radio action "%s" selected', action.name)
  10. end
  11. local COLOR = { RED = 1, GREEN = 2, BLUE = 3 }
  12. local SHAPE = { SQUARE = 1, RECTANGLE = 2, OVAL = 3 }
  13. local actions = Gtk.ActionGroup {
  14. name = 'Actions',
  15. Gtk.Action { name = 'FileMenu', label = "_File" },
  16. Gtk.Action { name = 'PreferencesMenu', label = "_Preferences" },
  17. Gtk.Action { name = 'ColorMenu', label = "_Color" },
  18. Gtk.Action { name = 'ShapeMenu', label = "_Shape" },
  19. Gtk.Action { name = 'HelpMenu', label = "_Help" },
  20. { Gtk.Action { name = 'New', stock_id = Gtk.STOCK_NEW, label = "_New",
  21. tooltip = "Create a new file",
  22. on_activate = activate_action, },
  23. accelerator = '<control>N', },
  24. { Gtk.Action { name = 'Open', stock_id = Gtk.STOCK_OPEN, label = "_Open",
  25. tooltip = "Open a file",
  26. on_activate = activate_action, },
  27. accelerator = '<control>O', },
  28. { Gtk.Action { name = 'Save', stock_id = Gtk.STOCK_SAVE, label = "_Save",
  29. tooltip = "Save current file",
  30. on_activate = activate_action, },
  31. accelerator = '<control>S', },
  32. Gtk.Action { name = 'SaveAs', stock_id = Gtk.STOCK_SAVE,
  33. label = "Save _As...", tooltip = "Save to a file",
  34. on_activate = activate_action, },
  35. { Gtk.Action { name = 'Quit', stock_id = Gtk.STOCK_QUIT, label = "_Quit",
  36. tooltip = "Quit",
  37. on_activate = activate_action, },
  38. accelerator = '<control>Q', },
  39. { Gtk.Action { name = 'About', stock_id = Gtk.STOCK_ABOUT, label = "_About",
  40. tooltip = "About",
  41. on_activate = activate_action, },
  42. accelerator = '<control>A', },
  43. Gtk.Action { name = 'Logo', stock_id = 'demo-gtk-logo', tooltip = "GTK+",
  44. on_activate = activate_action, },
  45. { Gtk.ToggleAction { name = 'Bold', stock_id = Gtk.STOCK_BOLD,
  46. label = "_Bold", tooltip = "Bold", active = true,
  47. on_activate = activate_action },
  48. accelerator = "<control>B", },
  49. {
  50. { Gtk.RadioAction { name = 'Red', label = "_Red", tooltip = "Blood",
  51. value = COLOR.RED, active = true, },
  52. accelerator = '<control>R', },
  53. { Gtk.RadioAction { name = 'Green', label = "_Green", tooltip = "Grass",
  54. value = COLOR.GREEN },
  55. accelerator = '<control>G', },
  56. { Gtk.RadioAction { name = 'Blue', label = "_Blue", tooltip = "Sky",
  57. value = COLOR.BLUE },
  58. accelerator = '<control>B', },
  59. on_change = activate_radio_action,
  60. },
  61. {
  62. { Gtk.RadioAction { name = 'Square', label = "_Square",
  63. tooltip = "Square", value = SHAPE.SQUARE, },
  64. accelerator = '<control>S', },
  65. { Gtk.RadioAction { name = 'Rectangle', label = "_Rectangle",
  66. tooltip = "Rectangle", value = SHAPE.RECTANGLE },
  67. accelerator = '<control>R', },
  68. { Gtk.RadioAction { name = 'Oval', label = "_Oval",
  69. tooltip = "Oval", value = SHAPE.OVAL, active = true },
  70. accelerator = '<control>O', },
  71. on_change = activate_radio_action,
  72. },
  73. }
  74. local ui = Gtk.UIManager()
  75. ui:insert_action_group(actions, 0)
  76. local ok, err = ui:add_ui_from_string(
  77. [[
  78. <ui>
  79. <menubar name='MenuBar'>
  80. <menu action='FileMenu'>
  81. <menuitem action='New'/>
  82. <menuitem action='Open'/>
  83. <menuitem action='Save'/>
  84. <menuitem action='SaveAs'/>
  85. <separator/>
  86. <menuitem action='Quit'/>
  87. </menu>
  88. <menu action='PreferencesMenu'>
  89. <menu action='ColorMenu'>
  90. <menuitem action='Red'/>
  91. <menuitem action='Green'/>
  92. <menuitem action='Blue'/>
  93. </menu>
  94. <menu action='ShapeMenu'>
  95. <menuitem action='Square'/>
  96. <menuitem action='Rectangle'/>
  97. <menuitem action='Oval'/>
  98. </menu>
  99. <menuitem action='Bold'/>
  100. </menu>
  101. <menu action='HelpMenu'>
  102. <menuitem action='About'/>
  103. </menu>
  104. </menubar>
  105. <toolbar name='ToolBar'>
  106. <toolitem action='Open'/>
  107. <toolitem action='Quit'/>
  108. <separator action='Sep1'/>
  109. <toolitem action='Logo'/>
  110. </toolbar>
  111. </ui>]], -1)
  112. if not ok then
  113. log.message('building menus failed: %s', err)
  114. end
  115. local window = Gtk.Window {
  116. title = "UI Manager",
  117. Gtk.Box {
  118. orientation = 'VERTICAL',
  119. ui:get_widget('/MenuBar'),
  120. Gtk.Label {
  121. id = 'label',
  122. label = "Type\n<alt>\n to start",
  123. halign = 'CENTER',
  124. valign = 'CENTER',
  125. expand = true,
  126. },
  127. Gtk.Separator {
  128. orientation = 'HORIZONTAL',
  129. },
  130. Gtk.Box {
  131. orientation = 'VERTICAL',
  132. spacing = 10,
  133. border_width = 10,
  134. Gtk.Button {
  135. id = 'close',
  136. label = "close",
  137. can_default = true,
  138. },
  139. },
  140. },
  141. }
  142. window:add_accel_group(ui:get_accel_group())
  143. function window.child.close:on_clicked()
  144. window:destroy()
  145. end
  146. window.child.close:grab_default()
  147. window.child.label:set_size_request(200, 200)
  148. window:show_all()
  149. return window
  150. end,
  151. "UI Manager",
  152. table.concat {
  153. [[The Gtk.UIManager object allows the easy creation of menus from ]],
  154. [[an array of actions and a description of the menu hierarchy.]],
  155. }