demo-dialogs.lua 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. return function(parent, dir)
  2. local lgi = require 'lgi'
  3. local GObject = lgi.GObject
  4. local Gtk = lgi.Gtk
  5. local Gdk = lgi.Gdk
  6. local GdkPixbuf = lgi.GdkPixbuf
  7. local window = Gtk.Window {
  8. title = "Dialogs",
  9. border_width = 8,
  10. Gtk.Frame {
  11. label = "Dialogs",
  12. Gtk.Box {
  13. orientation = 'VERTICAL',
  14. border_width = 8,
  15. spacing = 8,
  16. Gtk.Box {
  17. orientation = 'HORIZONTAL',
  18. spacing = 8,
  19. Gtk.Button {
  20. id = 'message_button',
  21. label = "_Message Dialog",
  22. use_underline = true,
  23. },
  24. },
  25. Gtk.Separator { orientation = 'HORIZONTAL' },
  26. Gtk.Box {
  27. orientation = 'HORIZONTAL',
  28. spacing = 8,
  29. Gtk.Box {
  30. orientation = 'VERTICAL',
  31. Gtk.Button {
  32. id = 'interactive_button',
  33. label = "_Interactive Dialog",
  34. use_underline = true,
  35. },
  36. },
  37. Gtk.Grid {
  38. row_spacing = 4,
  39. column_spacing = 4,
  40. {
  41. left_attach = 0, top_attach = 0,
  42. Gtk.Label {
  43. label = "_Entry 1",
  44. use_underline = true,
  45. },
  46. },
  47. {
  48. left_attach = 1, top_attach = 0,
  49. Gtk.Entry {
  50. id = 'entry1',
  51. },
  52. },
  53. {
  54. left_attach = 0, top_attach = 1,
  55. Gtk.Label {
  56. label = "E_ntry 2",
  57. use_underline = true,
  58. },
  59. },
  60. {
  61. left_attach = 1, top_attach = 1,
  62. Gtk.Entry {
  63. id = 'entry2',
  64. },
  65. },
  66. },
  67. },
  68. },
  69. },
  70. }
  71. local popup_count = 1
  72. function window.child.message_button:on_clicked()
  73. local dialog = Gtk.MessageDialog {
  74. transient_for = window,
  75. modal = true,
  76. destroy_with_parent = true,
  77. message_type = 'INFO',
  78. buttons = 'OK',
  79. text = "This message box has been popped up the following\n"
  80. .. "number of times:",
  81. secondary_text = ('%d'):format(popup_count),
  82. }
  83. dialog:run()
  84. dialog:destroy()
  85. popup_count = popup_count + 1
  86. end
  87. function window.child.interactive_button:on_clicked()
  88. local dialog = Gtk.Dialog {
  89. title = "Interactive Dialog",
  90. transient_for = window,
  91. modal = true,
  92. destroy_with_parent = true,
  93. buttons = {
  94. { Gtk.STOCK_OK, Gtk.ResponseType.OK },
  95. { "_Non-stock Button", Gtk.ResponseType.CANCEL },
  96. },
  97. }
  98. local hbox = Gtk.Box {
  99. orientation = 'HORIZONTAL',
  100. spacing = 8,
  101. border_width = 8,
  102. Gtk.Image {
  103. stock = Gtk.STOCK_DIALOG_QUESTION,
  104. icon_size = Gtk.IconSize.DIALOG,
  105. },
  106. Gtk.Grid {
  107. row_spacing = 4,
  108. column_spacing = 4,
  109. {
  110. left_attach = 0, top_attach = 0,
  111. Gtk.Label {
  112. label = "_Entry 1",
  113. use_underline = true,
  114. },
  115. },
  116. {
  117. left_attach = 1, top_attach = 0,
  118. Gtk.Entry {
  119. id = 'entry1',
  120. text = window.child.entry1.text,
  121. },
  122. },
  123. {
  124. left_attach = 0, top_attach = 1,
  125. Gtk.Label {
  126. label = "E_ntry 2",
  127. use_underline = true,
  128. },
  129. },
  130. {
  131. left_attach = 1, top_attach = 1,
  132. Gtk.Entry {
  133. id = 'entry2',
  134. text = window.child.entry2.text,
  135. },
  136. },
  137. }
  138. }
  139. dialog:get_content_area():add(hbox)
  140. hbox:show_all()
  141. if dialog:run() == Gtk.ResponseType.OK then
  142. window.child.entry1.text = hbox.child.entry1.text
  143. window.child.entry2.text = hbox.child.entry2.text
  144. end
  145. dialog:destroy()
  146. end
  147. window:show_all()
  148. return window
  149. end,
  150. "Dialog and Message Boxes",
  151. table.concat {
  152. "Dialog widgets are used to pop up a transient window for user feedback.",
  153. }