demo-colorselector.lua 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. return function(parent, dir)
  2. local lgi = require 'lgi'
  3. local Gtk = lgi.Gtk
  4. local Gdk = lgi.Gdk
  5. local cairo = lgi.cairo
  6. local window = Gtk.Window {
  7. title = "Color Selection",
  8. border_width = 8,
  9. Gtk.Box {
  10. orientation = 'VERTICAL',
  11. spacing = 8,
  12. border_width = 8,
  13. Gtk.Frame {
  14. shadow_type = 'IN',
  15. Gtk.DrawingArea {
  16. id = 'area',
  17. expand = true,
  18. width = 200,
  19. height = 200,
  20. },
  21. },
  22. Gtk.Button {
  23. id = 'change',
  24. label = "_Change the above color",
  25. use_underline = true,
  26. halign = 'END',
  27. valign = 'CENTER',
  28. },
  29. },
  30. }
  31. local area = window.child.area
  32. area:override_background_color(
  33. 0, Gdk.RGBA { red = 0, green = 0, blue = 1, alpha = 1 })
  34. function area:on_draw(cr)
  35. cr:set_source_rgba(self.style_context:get_background_color('NORMAL'))
  36. cr:paint()
  37. return true
  38. end
  39. function window.child.change:on_clicked()
  40. local dialog
  41. if Gtk.ColorChooserDialog then
  42. dialog = Gtk.ColorChooserDialog {
  43. title = "Changing color",
  44. transient_for = window,
  45. rgba = self.style_context:get_background_color('NORMAL')
  46. }
  47. function dialog:on_response(response_id)
  48. if response_id == Gtk.ResponseType.OK then
  49. area:override_background_color(0, self.rgba)
  50. end
  51. dialog:hide()
  52. end
  53. else
  54. dialog = Gtk.ColorSelectionDialog {
  55. title = "Changing color",
  56. transient_for = window,
  57. }
  58. dialog.color_selection.current_rgba =
  59. self.style_context:get_background_color('NORMAL')
  60. function dialog:on_response(response_id)
  61. if response_id == Gtk.ResponseType.OK then
  62. area:override_background_color(
  63. 0, self.color_selection.current_rgba)
  64. end
  65. dialog:hide()
  66. end
  67. end
  68. dialog:show_all()
  69. end
  70. window:show_all()
  71. return window
  72. end,
  73. "Color Selector",
  74. table.concat {
  75. [[Gtk.ColorSelection lets the user choose a color. ]],
  76. [[Gtk.ColorSelectionDialog is a prebuilt dialog containing ]],
  77. [[a Gtk.ColorSelection.]],
  78. }