demo-application.lua 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. return function(parent, dir)
  2. local lgi = require 'lgi'
  3. local GObject = lgi.GObject
  4. local Gtk = lgi.Gtk
  5. local GdkPixbuf = lgi.GdkPixbuf
  6. local assert = lgi.assert
  7. -- Register icon.
  8. Gtk.stock_add {
  9. Gtk.StockItem {
  10. stock_id = 'demo-gtk-logo',
  11. label = "_GTK!",
  12. }
  13. }
  14. local logo_pixbuf = GdkPixbuf.Pixbuf.new_from_file(
  15. dir:get_child('gtk-logo-rgb.gif'):get_path())
  16. logo_pixbuf = logo_pixbuf and logo_pixbuf:add_alpha(true, 0xff, 0xff, 0xff)
  17. local icon_factory = Gtk.IconFactory()
  18. icon_factory:add_default()
  19. icon_factory:add('demo-gtk-logo', Gtk.IconSet.new_from_pixbuf(logo_pixbuf))
  20. local window = Gtk.Window {
  21. title = "Application Window",
  22. icon_name = 'document-open',
  23. default_width = 200,
  24. default_height = 200,
  25. }
  26. local function activate_action(action)
  27. local dialog = Gtk.MessageDialog {
  28. transient_for = window, destroy_with_parent = true,
  29. message_type = 'INFO', buttons = 'CLOSE',
  30. text = ("You activated action: '%s' of type '%s'"):format(
  31. action.name, GObject.Type.name(action._type)),
  32. on_response = Gtk.Widget.destroy
  33. }
  34. dialog:show()
  35. end
  36. local message_label = Gtk.Label()
  37. message_label:show()
  38. local function change_radio_action(action)
  39. if action.active then
  40. message_label.label = (("You activated radio action: '%s' of type '%s'."
  41. .."\nCurrent value: %d"):format(
  42. action.name, GObject.Type.name(action._type),
  43. action.value))
  44. window.child.infobar.message_type = action.value
  45. window.child.infobar:show()
  46. end
  47. end
  48. local function set_theme(action)
  49. local settings = Gtk.Settings.get_default()
  50. settings.gtk_application_prefer_dark_theme = action.active
  51. end
  52. local function about_cb()
  53. local about = Gtk.AboutDialog {
  54. program_name = "GTK+ Lgi Code Demos",
  55. version = ("Running against GTK+ %d.%d.%d, Lgi %s"):format(
  56. Gtk.get_major_version(),
  57. Gtk.get_minor_version(),
  58. Gtk.get_micro_version(),
  59. lgi._VERSION),
  60. copyright = "(C) 2012 Pavel Holejsovsky",
  61. license_type = 'MIT_X11',
  62. website = 'http://github.org/pavouk/lgi',
  63. comments = "Port of original GTK+ demo, (C) 1997-2009 The GTK+ Team",
  64. authors = {
  65. "Pavel Holejsovsky",
  66. "Peter Mattis",
  67. "Spencer Kimball",
  68. "Josh MacDonald",
  69. "and many more...",
  70. },
  71. documenters = {
  72. "Owen Taylor",
  73. "Tony Gale",
  74. "Matthias Clasen <mclasen@redhat.com>",
  75. "and many more...",
  76. },
  77. logo = logo_pixbuf,
  78. title = "About GTK+ Code Demos",
  79. }
  80. about:run()
  81. about:hide()
  82. end
  83. local color = { RED = 1, GREEN = 2, BLUE = 3 }
  84. local shape = { SQUARE = 1, RECTANGLE = 2, OVAL = 3 }
  85. local action_group = Gtk.ActionGroup {
  86. name = 'AppWindowActions',
  87. Gtk.Action { name = 'FileMenu', label = "_File" },
  88. Gtk.Action { name = 'OpenMenu', label = "_Open" },
  89. Gtk.Action { name = 'PreferencesMenu', label = "_Preferences" },
  90. Gtk.Action { name = 'ColorMenu', label = "_Color" },
  91. Gtk.Action { name = 'ShapeMenu', label = "_Shape" },
  92. Gtk.Action { name = 'HelpMenu', label = "_Help" },
  93. { Gtk.Action { name = 'New', label = "_New", stock_id = Gtk.STOCK_NEW,
  94. tooltip = "Create a new file",
  95. on_activate = activate_action },
  96. accelerator = '<control>N' },
  97. Gtk.Action { name = 'File1', label = "File1", tooltip = "Open first file",
  98. on_activate = activate_action },
  99. Gtk.Action { name = 'Open', label = "_Open", stock_id = Gtk.STOCK_OPEN,
  100. tooltip = "Open a file",
  101. on_activate = activate_action },
  102. Gtk.Action { name = 'File1', label = "File1", tooltip = "Open first file",
  103. on_activate = activate_action },
  104. { Gtk.Action { name = 'Save', label = "_Save",
  105. tooltip = "Save current file", stock_id = Gtk.STOCK_SAVE,
  106. on_activate = activate_action },
  107. accelerator = '<control>S' },
  108. Gtk.Action { name = 'SaveAs', label = "Save _As",
  109. tooltip = "Save to a file", stock_id = Gtk.STOCK_SAVE,
  110. on_activate = activate_action },
  111. { Gtk.Action { name = 'Quit', label = "_Quit",
  112. tooltip = "Quit", stock_id = Gtk.STOCK_QUIT,
  113. on_activate = activate_action },
  114. accelerator = '<control>Q' },
  115. Gtk.Action { name = 'About', label = "_About",
  116. tooltip = "About",
  117. on_activate = about_cb },
  118. Gtk.Action { name = 'Logo', stock_id = 'demo-gtk-logo',
  119. tooltip = "GTK+ on LGI",
  120. on_activate = activate_action },
  121. { Gtk.ToggleAction { name = 'Bold', stock_id = Gtk.STOCK_BOLD,
  122. label = "_Bold", tooltip = "Bold",
  123. on_activate = activate_action,
  124. active = true },
  125. accelerator = '<control>B' },
  126. Gtk.ToggleAction { name = 'DarkTheme', label = "_Prefer dark theme",
  127. tooltip = "Prefer dark theme", active = false,
  128. on_activate = set_theme },
  129. {
  130. { Gtk.RadioAction { name = 'Red', label = "_Red", tooltip = "Blood",
  131. value = color.RED, },
  132. accelerator = '<control>R' },
  133. { Gtk.RadioAction { name = 'Green', label = "_Green", tooltip = "Grass",
  134. value = color.GREEN, },
  135. accelerator = '<control>G' },
  136. { Gtk.RadioAction { name = 'Blue', label = "_Blue", tooltip = "Sky",
  137. value = color.BLUE, },
  138. accelerator = '<control>B' },
  139. on_change = change_radio_action,
  140. },
  141. {
  142. { Gtk.RadioAction { name = 'Square', label = "_Square",
  143. tooltip = "Square",
  144. value = shape.SQUARE, },
  145. accelerator = '<control>S' },
  146. { Gtk.RadioAction { name = 'Rectangle', label = "_Rectangle",
  147. tooltip = "Rectangle", value = shape.RECTANGLE, },
  148. accelerator = '<control>R' },
  149. { Gtk.RadioAction { name = 'Oval', label = "_Oval",
  150. tooltip = "Oval", value = shape.OVAL, },
  151. accelerator = '<control>O' },
  152. on_change = change_radio_action,
  153. },
  154. }
  155. local merge = Gtk.UIManager {}
  156. merge:insert_action_group (action_group, 0)
  157. window:add_accel_group(merge:get_accel_group())
  158. assert(merge:add_ui_from_string([[
  159. <ui>
  160. <menubar name='MenuBar'>
  161. <menu action='FileMenu'>
  162. <menuitem action='New'/>
  163. <menuitem action='Open'/>
  164. <menuitem action='Save'/>
  165. <menuitem action='SaveAs'/>
  166. <separator/>
  167. <menuitem action='Quit'/>
  168. </menu>
  169. <menu action='PreferencesMenu'>
  170. <menuitem action='DarkTheme'/>
  171. <menu action='ColorMenu'>
  172. <menuitem action='Red'/>
  173. <menuitem action='Green'/>
  174. <menuitem action='Blue'/>
  175. </menu>
  176. <menu action='ShapeMenu'>
  177. <menuitem action='Square'/>
  178. <menuitem action='Rectangle'/>
  179. <menuitem action='Oval'/>
  180. </menu>
  181. <menuitem action='Bold'/>
  182. </menu>
  183. <menu action='HelpMenu'>
  184. <menuitem action='About'/>
  185. </menu>
  186. </menubar>
  187. <toolbar name='ToolBar'>
  188. <toolitem action='Open'>
  189. <menu action='OpenMenu'>
  190. <menuitem action='File1'/>
  191. </menu>
  192. </toolitem>
  193. <toolitem action='Quit'/>
  194. <separator action='Sep1'/>
  195. <toolitem action='Logo'/>
  196. </toolbar>
  197. </ui>]], -1))
  198. local menubar = merge:get_widget('/MenuBar')
  199. menubar.halign = 'FILL'
  200. local toolbar = merge:get_widget('/ToolBar')
  201. toolbar.halign = 'FILL'
  202. window.child = Gtk.Grid {
  203. orientation = 'VERTICAL',
  204. menubar,
  205. toolbar,
  206. Gtk.InfoBar {
  207. id = 'infobar',
  208. no_show_all = true,
  209. halign = 'FILL',
  210. on_response = Gtk.Widget.hide
  211. },
  212. Gtk.ScrolledWindow {
  213. shadow_type = 'IN',
  214. halign = 'FILL', valign = 'FILL',
  215. expand = true,
  216. Gtk.TextView {
  217. id = 'view',
  218. },
  219. },
  220. Gtk.Statusbar {
  221. id = 'statusbar',
  222. halign = 'FILL',
  223. },
  224. }
  225. local buffer = window.child.view.buffer
  226. local statusbar = window.child.statusbar
  227. -- Updates statusbar according to the buffer of the view
  228. local function update_statusbar()
  229. statusbar:pop(0)
  230. local iter = buffer:get_iter_at_mark(buffer:get_insert())
  231. local msg =
  232. statusbar:push(
  233. 0, ("Cursor at row %d column %d - %d chars in document"):format(
  234. iter:get_line(), iter:get_line_offset(), buffer:get_char_count()))
  235. end
  236. function buffer:on_changed()
  237. update_statusbar()
  238. end
  239. function buffer:on_mark_set()
  240. update_statusbar()
  241. end
  242. -- Perform initial statusbar update.
  243. update_statusbar()
  244. -- Add infobar area.
  245. local info_area = window.child.infobar:get_content_area()
  246. info_area:add(message_label)
  247. window.child.infobar:add_button(Gtk.STOCK_OK, Gtk.ResponseType.OK)
  248. window:show_all()
  249. window.child.view:grab_focus()
  250. return window
  251. end,
  252. "Application main window",
  253. [[Demonstrates a typical application window with menubar, toolbar, statusbar.]]