gtk4 4.1.2 → 4.1.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (29) hide show
  1. checksums.yaml +4 -4
  2. data/sample/examples/application1/exampleapp.rb +2 -2
  3. data/sample/examples/application2/README.md +10 -0
  4. data/sample/examples/application2/exampleapp.rb +80 -0
  5. data/sample/examples/application3/README.md +1 -0
  6. data/sample/examples/application3/exampleapp.rb +108 -0
  7. data/sample/examples/application4/README.md +13 -0
  8. data/sample/examples/application4/exampleapp.rb +158 -0
  9. data/sample/examples/application5/README.md +30 -0
  10. data/sample/examples/application5/Rakefile +27 -0
  11. data/sample/examples/application5/exampleapp.rb +171 -0
  12. data/sample/examples/application5/org.gtk.exampleapp.gschema.xml +20 -0
  13. data/sample/examples/application6/README.md +15 -0
  14. data/sample/examples/application6/Rakefile +27 -0
  15. data/sample/examples/application6/exampleapp.rb +265 -0
  16. data/sample/examples/application6/org.gtk.exampleapp.gschema.xml +20 -0
  17. data/sample/examples/application7/README.md +15 -0
  18. data/sample/examples/application7/Rakefile +27 -0
  19. data/sample/examples/application7/exampleapp.rb +307 -0
  20. data/sample/examples/application7/org.gtk.exampleapp.gschema.xml +20 -0
  21. data/sample/examples/application8/README.md +15 -0
  22. data/sample/examples/application8/Rakefile +27 -0
  23. data/sample/examples/application8/exampleapp.rb +357 -0
  24. data/sample/examples/application8/org.gtk.exampleapp.gschema.xml +25 -0
  25. data/sample/examples/application9/README.md +15 -0
  26. data/sample/examples/application9/Rakefile +27 -0
  27. data/sample/examples/application9/exampleapp.rb +387 -0
  28. data/sample/examples/application9/org.gtk.exampleapp.gschema.xml +25 -0
  29. metadata +32 -6
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 031bdff3af35f8c9ca1993b8857fe51b9c6e658d327a3fb263d8df8e0fd476ee
4
- data.tar.gz: ac2485eb8b4f99c3c714fcaa4ab18eccffd347980705a32e028c4503fa136585
3
+ metadata.gz: 3046d15df9010c560d2b714291c5ec3b27dd119600ea487407e59e4b92306ac4
4
+ data.tar.gz: 6442d209ea930b5637f3d228b671fac88ff45925a66db2439e13e5d32a2f731f
5
5
  SHA512:
6
- metadata.gz: 687e5abb4016942dbfb0228d6371ddb0a64263b90192575c21cb14470fb8066402d5323b1086ca74dbdec18e4df92d3206689020b6864f53d571b46b4b40d302
7
- data.tar.gz: f7b56275f7613f8f9a979d7b96ddfe16ef1c75b562c37516fd1da6da3840e236059d06426d0ee8008e3f8ba7b8a92d9c8f66890e00687d51e2ecbe0a47ea6938
6
+ metadata.gz: aaab417c8f0106174987778df08f8218aa2f87fc85fdbdde231d0cfa2686c923e26a64d20cff271556377cb054d215c7c7d40560f9cd80f4d11e306cbdea03e0
7
+ data.tar.gz: 70f22d049e6f21cd0f43d5cdb09e20b57547aba3ef098b32c98a9fbe3bafbe4bf9c6fe09a7d2489d63376c669aaf3f10cfa63143e5a1cdfc0f9b127392829b5f
@@ -18,7 +18,7 @@
18
18
  #
19
19
  # Example from:
20
20
  # * https://gitlab.gnome.org/GNOME/gtk/-/blob/main/examples/application1/exampleapp.c
21
- # * https://gitlab.gnome.org/GNOME/gtk/-/blob/main/examples/application1/exampleappwin.c#
21
+ # * https://gitlab.gnome.org/GNOME/gtk/-/blob/main/examples/application1/exampleappwin.c
22
22
  # License: LGPL2.1-or-later
23
23
 
24
24
  require "gtk4"
@@ -36,7 +36,7 @@ class ExampleApp < Gtk::Application
36
36
  window = ExampleAppWindow.new(application)
37
37
  window.present
38
38
  end
39
- signal_connect "open" do |application, files, hin|
39
+ signal_connect "open" do |application, files, hint|
40
40
  window = application.windows[0] || ExampleAppWindow.new(application)
41
41
  files.each do |file|
42
42
  window.open(file)
@@ -0,0 +1,10 @@
1
+ # Step 2: Populating the window
2
+
3
+ The original files are located in the following directory.
4
+
5
+ - https://gitlab.gnome.org/GNOME/gtk/-/blob/main/examples/application2
6
+
7
+ They are written in C language and use GResource because it is the best way in C language.
8
+ But when it comes to Ruby language, GResource is not the best way especially when the resource only includes ui files.
9
+ So, the program here uses a string instead of a resource.
10
+ If you want to know how to program with GResource in Ruby language, see gtk3/sample/tutorial/exampleapp2 directory.
@@ -0,0 +1,80 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # Copyright (C) 2023 Ruby-GNOME Project Team
4
+ #
5
+ # This library is free software; you can redistribute it and/or
6
+ # modify it under the terms of the GNU Lesser General Public
7
+ # License as published by the Free Software Foundation; either
8
+ # version 2.1 of the License, or (at your option) any later version.
9
+ #
10
+ # This library is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
+ # Lesser General Public License for more details.
14
+ #
15
+ # You should have received a copy of the GNU Lesser General Public
16
+ # License along with this library; if not, write to the Free Software
17
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+ #
19
+ # Example from:
20
+ # * https://gitlab.gnome.org/GNOME/gtk/-/blob/main/examples/application2/exampleapp.c
21
+ # * https://gitlab.gnome.org/GNOME/gtk/-/blob/main/examples/application2/exampleappwin.c
22
+ # * https://gitlab.gnome.org/GNOME/gtk/-/blob/main/examples/application2/window.ui
23
+ # License: LGPL2.1-or-later
24
+
25
+ require "gtk4"
26
+
27
+ class ExampleAppWindow < Gtk::ApplicationWindow
28
+ type_register
29
+ class << self
30
+ def init
31
+ template = <<~TEMPLATE
32
+ <?xml version="1.0" encoding="UTF-8"?>
33
+ <interface>
34
+ <template class="ExampleAppWindow" parent="GtkApplicationWindow">
35
+ <property name="title" translatable="yes">Example Application</property>
36
+ <property name="default-width">600</property>
37
+ <property name="default-height">400</property>
38
+ <child>
39
+ <object class="GtkBox" id="content_box">
40
+ <property name="orientation">vertical</property>
41
+ <child>
42
+ <object class="GtkStack" id="stack"/>
43
+ </child>
44
+ </object>
45
+ </child>
46
+ </template>
47
+ </interface>
48
+ TEMPLATE
49
+ set_template(data: template)
50
+ end
51
+ end
52
+
53
+ def initialize(application)
54
+ super(application: application)
55
+ end
56
+
57
+ def open(file)
58
+ end
59
+ end
60
+
61
+ class ExampleApp < Gtk::Application
62
+ def initialize
63
+ super("org.gtk.exampleapp", :handles_open)
64
+
65
+ signal_connect "activate" do |application|
66
+ window = ExampleAppWindow.new(application)
67
+ window.present
68
+ end
69
+ signal_connect "open" do |application, files, hint|
70
+ window = application.windows[0] || ExampleAppWindow.new(application)
71
+ files.each do |file|
72
+ window.open(file)
73
+ end
74
+ window.present
75
+ end
76
+ end
77
+ end
78
+
79
+ app = ExampleApp.new
80
+ app.run
@@ -0,0 +1 @@
1
+ # Step 3: Opening files, add a stack switcher
@@ -0,0 +1,108 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # Copyright (C) 2023 Ruby-GNOME Project Team
4
+ #
5
+ # This library is free software; you can redistribute it and/or
6
+ # modify it under the terms of the GNU Lesser General Public
7
+ # License as published by the Free Software Foundation; either
8
+ # version 2.1 of the License, or (at your option) any later version.
9
+ #
10
+ # This library is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
+ # Lesser General Public License for more details.
14
+ #
15
+ # You should have received a copy of the GNU Lesser General Public
16
+ # License along with this library; if not, write to the Free Software
17
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+ #
19
+ # Example from:
20
+ # * https://gitlab.gnome.org/GNOME/gtk/-/blob/main/examples/application3/exampleapp.c
21
+ # * https://gitlab.gnome.org/GNOME/gtk/-/blob/main/examples/application3/exampleappwin.c
22
+ # * https://gitlab.gnome.org/GNOME/gtk/-/blob/main/examples/application3/window.ui
23
+ # License: LGPL2.1-or-later
24
+
25
+ require "gtk4"
26
+
27
+ class ExampleAppWindow < Gtk::ApplicationWindow
28
+ type_register
29
+ class << self
30
+ def init
31
+ template = <<~TEMPLATE
32
+ <?xml version="1.0" encoding="UTF-8"?>
33
+ <interface>
34
+ <template class="ExampleAppWindow" parent="GtkApplicationWindow">
35
+ <property name="title" translatable="yes">Example Application</property>
36
+ <property name="default-width">600</property>
37
+ <property name="default-height">400</property>
38
+ <child type="titlebar">
39
+ <object class="GtkHeaderBar" id="header">
40
+ <child type="title">
41
+ <object class="GtkStackSwitcher" id="tabs">
42
+ <property name="stack">stack</property>
43
+ </object>
44
+ </child>
45
+ </object>
46
+ </child>
47
+ <child>
48
+ <object class="GtkBox" id="content_box">
49
+ <property name="orientation">vertical</property>
50
+ <child>
51
+ <object class="GtkStack" id="stack"/>
52
+ </child>
53
+ </object>
54
+ </child>
55
+ </template>
56
+ </interface>
57
+ TEMPLATE
58
+ set_template(data: template)
59
+ bind_template_child("stack")
60
+ end
61
+ end
62
+
63
+ def initialize(application)
64
+ super(application: application)
65
+ end
66
+
67
+ def open(file)
68
+ basename = file.basename
69
+ scrolled = Gtk::ScrolledWindow.new
70
+ scrolled.hexpand = true
71
+ scrolled.vexpand = true
72
+ view = Gtk::TextView.new
73
+ view.editable = false
74
+ view.cursor_visible = false
75
+ scrolled.child = view
76
+ stack.add_titled(scrolled, basename, basename)
77
+ buffer = view.buffer
78
+ file.read do |stream|
79
+ buffer.text = stream.read.force_encoding(Encoding::UTF_8)
80
+ end
81
+ end
82
+ end
83
+
84
+ class ExampleApp < Gtk::Application
85
+ def initialize
86
+ super("org.gtk.exampleapp", :handles_open)
87
+
88
+ signal_connect "activate" do |application|
89
+ window = ExampleAppWindow.new(application)
90
+ window.present
91
+ end
92
+ signal_connect "open" do |application, files, hint|
93
+ window = application.windows[0] || ExampleAppWindow.new(application)
94
+ files.each do |file|
95
+ window.open(file)
96
+ end
97
+ window.present
98
+ end
99
+ end
100
+ end
101
+
102
+ app = ExampleApp.new
103
+
104
+ # Gtk::Application#run needs C style argv ([prog, arg1, arg2, ...,argn]).
105
+ # The ARGV ruby variable only contains the arguments ([arg1, arg2, ...,argb])
106
+ # and not the program name. We have to add it explicitly.
107
+
108
+ app.run([$PROGRAM_NAME] + ARGV)
@@ -0,0 +1,13 @@
1
+ # Step 4: Add a menu
2
+
3
+ The original files are located in the following directory.
4
+
5
+ - https://gitlab.gnome.org/GNOME/gtk/-/blob/main/examples/application4
6
+
7
+ They are written in the C language and use `g_action_map_add_action entries`.
8
+ It is a convenience function for creating multiple GSimpleAction instances and adding them to a GActionMap.
9
+ If you write a program in C, it is the best way.
10
+
11
+ However, the program in this directory doesn't use `add_action_entries` method.
12
+ Instead, it uses `each` method of an array of action names.
13
+ The way above is flexible and simple.
@@ -0,0 +1,158 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # Copyright (C) 2023 Ruby-GNOME Project Team
4
+ #
5
+ # This library is free software; you can redistribute it and/or
6
+ # modify it under the terms of the GNU Lesser General Public
7
+ # License as published by the Free Software Foundation; either
8
+ # version 2.1 of the License, or (at your option) any later version.
9
+ #
10
+ # This library is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
+ # Lesser General Public License for more details.
14
+ #
15
+ # You should have received a copy of the GNU Lesser General Public
16
+ # License along with this library; if not, write to the Free Software
17
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+ #
19
+ # Example from:
20
+ # * https://gitlab.gnome.org/GNOME/gtk/-/blob/main/examples/application4/exampleapp.c
21
+ # * https://gitlab.gnome.org/GNOME/gtk/-/blob/main/examples/application4/exampleappwin.c
22
+ # * https://gitlab.gnome.org/GNOME/gtk/-/blob/main/examples/application4/window.ui
23
+ # * https://gitlab.gnome.org/GNOME/gtk/-/blob/main/examples/application4/gears-menu.ui
24
+ # License: LGPL2.1-or-later
25
+
26
+ require "gtk4"
27
+
28
+ class ExampleAppWindow < Gtk::ApplicationWindow
29
+ type_register
30
+ class << self
31
+ def init
32
+ template = <<~TEMPLATE
33
+ <?xml version="1.0" encoding="UTF-8"?>
34
+ <interface>
35
+ <template class="ExampleAppWindow" parent="GtkApplicationWindow">
36
+ <property name="title" translatable="yes">Example Application</property>
37
+ <property name="default-width">600</property>
38
+ <property name="default-height">400</property>
39
+ <child type="titlebar">
40
+ <object class="GtkHeaderBar" id="header">
41
+ <child type="title">
42
+ <object class="GtkStackSwitcher" id="tabs">
43
+ <property name="stack">stack</property>
44
+ </object>
45
+ </child>
46
+ <child type="end">
47
+ <object class="GtkMenuButton" id="gears">
48
+ <property name="direction">none</property>
49
+ </object>
50
+ </child>
51
+ </object>
52
+ </child>
53
+ <child>
54
+ <object class="GtkBox" id="content_box">
55
+ <property name="orientation">vertical</property>
56
+ <child>
57
+ <object class="GtkStack" id="stack"/>
58
+ </child>
59
+ </object>
60
+ </child>
61
+ </template>
62
+ </interface>
63
+ TEMPLATE
64
+ set_template(data: template)
65
+ bind_template_child("stack")
66
+ bind_template_child("gears")
67
+ end
68
+ end
69
+
70
+ def initialize(application)
71
+ menu_ui = <<~MENU
72
+ <?xml version="1.0" encoding="UTF-8"?>
73
+ <interface>
74
+ <menu id="menu">
75
+ <section>
76
+ <item>
77
+ <attribute name="label" translatable="yes">_Preferences</attribute>
78
+ <attribute name="action">app.preferences</attribute>
79
+ </item>
80
+ </section>
81
+ <section>
82
+ <item>
83
+ <attribute name="label" translatable="yes">_Quit</attribute>
84
+ <attribute name="action">app.quit</attribute>
85
+ </item>
86
+ </section>
87
+ </menu>
88
+ </interface>
89
+ MENU
90
+ super(application: application)
91
+ builder = Gtk::Builder.new(string: menu_ui)
92
+ gears.menu_model = builder["menu"]
93
+ end
94
+
95
+ def open(file)
96
+ basename = file.basename
97
+ scrolled = Gtk::ScrolledWindow.new
98
+ scrolled.hexpand = true
99
+ scrolled.vexpand = true
100
+ view = Gtk::TextView.new
101
+ view.editable = false
102
+ view.cursor_visible = false
103
+ scrolled.child = view
104
+ stack.add_titled(scrolled, basename, basename)
105
+ buffer = view.buffer
106
+ file.read do |stream|
107
+ buffer.text = stream.read.force_encoding(Encoding::UTF_8)
108
+ end
109
+ end
110
+ end
111
+
112
+ class ExampleApp < Gtk::Application
113
+ def initialize
114
+ super("org.gtk.exampleapp", :handles_open)
115
+
116
+ signal_connect "startup" do |application|
117
+ [
118
+ "preferences",
119
+ "quit"
120
+ ].each do |action_name|
121
+ action = Gio::SimpleAction.new(action_name)
122
+ action.signal_connect("activate") do |_action, _parameter|
123
+ __send__("#{action_name}_activated")
124
+ end
125
+ application.add_action(action)
126
+ end
127
+ application.set_accels_for_action("app.quit", ["<Control>Q"])
128
+ end
129
+ signal_connect "activate" do |application|
130
+ window = ExampleAppWindow.new(application)
131
+ window.present
132
+ end
133
+ signal_connect "open" do |application, files, hint|
134
+ window = application.windows[0] || ExampleAppWindow.new(application)
135
+ files.each do |file|
136
+ window.open(file)
137
+ end
138
+ window.present
139
+ end
140
+ end
141
+
142
+ private
143
+
144
+ def preferences_activated
145
+ end
146
+
147
+ def quit_activated
148
+ quit
149
+ end
150
+ end
151
+
152
+ app = ExampleApp.new
153
+
154
+ # Gtk::Application#run needs C style argv ([prog, arg1, arg2, ...,argn]).
155
+ # The ARGV ruby variable only contains the arguments ([arg1, arg2, ...,argb])
156
+ # and not the program name. We have to add it explicitly.
157
+
158
+ app.run([$PROGRAM_NAME] + ARGV)
@@ -0,0 +1,30 @@
1
+ # Step 5: Add a GSettings object
2
+
3
+ The original files are located in the following directory.
4
+
5
+ - https://gitlab.gnome.org/GNOME/gtk/-/blob/main/examples/application5
6
+
7
+ The original schema file is [here](https://gitlab.gnome.org/GNOME/gtk/-/blob/main/examples/application5/org.gtk.exampleapp.gschema.xml) and the license is LGPL 2.1 or later.
8
+
9
+ This program uses GSettings object of the GIO library.
10
+ You need to compile the schema file (`org.gtk.exampleapp.gschema.xml`) in advance.
11
+ The instruction is written in the `Rakefile`, so you just run `rake` to compile it.
12
+
13
+ ```console
14
+ $ cd gtk4/sample/examples/application5
15
+ $ rake
16
+ ```
17
+
18
+ The Ruby script `exampleapp.rb` works with `gschemas.compiled`.
19
+
20
+ ```console
21
+ $ ruby exampleapp.rb README.md Rakefile
22
+ ```
23
+
24
+ You will see the contents of the two files `README.md` and `Rakefile` in the window.
25
+
26
+ If you want to remove `gschemas.compiled`, do like this:
27
+
28
+ ```console
29
+ $ rake clean
30
+ ```
@@ -0,0 +1,27 @@
1
+ # Copyright (C) 2023 Ruby-GNOME Project Team
2
+ #
3
+ # This library is free software; you can redistribute it and/or
4
+ # modify it under the terms of the GNU Lesser General Public
5
+ # License as published by the Free Software Foundation; either
6
+ # version 2.1 of the License, or (at your option) any later version.
7
+ #
8
+ # This library is distributed in the hope that it will be useful,
9
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11
+ # Lesser General Public License for more details.
12
+ #
13
+ # You should have received a copy of the GNU Lesser General Public
14
+ # License along with this library; if not, write to the Free Software
15
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
+
17
+ require "rake/clean"
18
+
19
+ task :default => "gschemas.compiled"
20
+
21
+ file "gschemas.compiled" => "org.gtk.exampleapp.gschema.xml" do
22
+ sh("glib-compile-schemas", ".")
23
+ end
24
+
25
+ CLEAN << "gschemas.compiled"
26
+
27
+ task :clean
@@ -0,0 +1,171 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # Copyright (C) 2023 Ruby-GNOME Project Team
4
+ #
5
+ # This library is free software; you can redistribute it and/or
6
+ # modify it under the terms of the GNU Lesser General Public
7
+ # License as published by the Free Software Foundation; either
8
+ # version 2.1 of the License, or (at your option) any later version.
9
+ #
10
+ # This library is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
+ # Lesser General Public License for more details.
14
+ #
15
+ # You should have received a copy of the GNU Lesser General Public
16
+ # License along with this library; if not, write to the Free Software
17
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+ #
19
+ # Example from:
20
+ # * https://gitlab.gnome.org/GNOME/gtk/-/blob/main/examples/application5/exampleapp.c
21
+ # * https://gitlab.gnome.org/GNOME/gtk/-/blob/main/examples/application5/exampleappwin.c
22
+ # * https://gitlab.gnome.org/GNOME/gtk/-/blob/main/examples/application5/window.ui
23
+ # * https://gitlab.gnome.org/GNOME/gtk/-/blob/main/examples/application5/gears-menu.ui
24
+ # License: LGPL2.1-or-later
25
+
26
+ # GSETTINGS_SCHEMA_DIR must be set before requiring "gtk4" gem because it is used in the GIO initialization.
27
+
28
+ if File.exist?(File.join(__dir__, "gschemas.compiled"))
29
+ ENV["GSETTINGS_SCHEMA_DIR"] = __dir__
30
+ else
31
+ raise %{gschemas.compiled doesn't exist. Run "rake" to generate it.}
32
+ end
33
+
34
+ require "gtk4"
35
+
36
+ class ExampleAppWindow < Gtk::ApplicationWindow
37
+ type_register
38
+ class << self
39
+ def init
40
+ template = <<~TEMPLATE
41
+ <?xml version="1.0" encoding="UTF-8"?>
42
+ <interface>
43
+ <template class="ExampleAppWindow" parent="GtkApplicationWindow">
44
+ <property name="title" translatable="yes">Example Application</property>
45
+ <property name="default-width">600</property>
46
+ <property name="default-height">400</property>
47
+ <child type="titlebar">
48
+ <object class="GtkHeaderBar" id="header">
49
+ <child type="title">
50
+ <object class="GtkStackSwitcher" id="tabs">
51
+ <property name="stack">stack</property>
52
+ </object>
53
+ </child>
54
+ <child type="end">
55
+ <object class="GtkMenuButton" id="gears">
56
+ <property name="direction">none</property>
57
+ </object>
58
+ </child>
59
+ </object>
60
+ </child>
61
+ <child>
62
+ <object class="GtkBox" id="content_box">
63
+ <property name="orientation">vertical</property>
64
+ <child>
65
+ <object class="GtkStack" id="stack"/>
66
+ </child>
67
+ </object>
68
+ </child>
69
+ </template>
70
+ </interface>
71
+ TEMPLATE
72
+ set_template(data: template)
73
+ bind_template_child("stack")
74
+ bind_template_child("gears")
75
+ end
76
+ end
77
+
78
+ def initialize(application)
79
+ menu_ui = <<~MENU
80
+ <?xml version="1.0" encoding="UTF-8"?>
81
+ <interface>
82
+ <menu id="menu">
83
+ <section>
84
+ <item>
85
+ <attribute name="label" translatable="yes">_Preferences</attribute>
86
+ <attribute name="action">app.preferences</attribute>
87
+ </item>
88
+ </section>
89
+ <section>
90
+ <item>
91
+ <attribute name="label" translatable="yes">_Quit</attribute>
92
+ <attribute name="action">app.quit</attribute>
93
+ </item>
94
+ </section>
95
+ </menu>
96
+ </interface>
97
+ MENU
98
+ super(application: application)
99
+ builder = Gtk::Builder.new(string: menu_ui)
100
+ gears.menu_model = builder["menu"]
101
+ @settings = Gio::Settings.new("org.gtk.exampleapp")
102
+ @settings.bind("transition", stack, "transition-type", :default)
103
+ end
104
+
105
+ def open(file)
106
+ basename = file.basename
107
+ scrolled = Gtk::ScrolledWindow.new
108
+ scrolled.hexpand = true
109
+ scrolled.vexpand = true
110
+ view = Gtk::TextView.new
111
+ view.editable = false
112
+ view.cursor_visible = false
113
+ scrolled.child = view
114
+ stack.add_titled(scrolled, basename, basename)
115
+ buffer = view.buffer
116
+ file.read do |stream|
117
+ buffer.text = stream.read.force_encoding(Encoding::UTF_8)
118
+ end
119
+ tag = buffer.create_tag
120
+ @settings.bind("font", tag, "font", :default)
121
+ buffer.apply_tag(tag, buffer.start_iter, buffer.end_iter)
122
+ end
123
+ end
124
+
125
+ class ExampleApp < Gtk::Application
126
+ def initialize
127
+ super("org.gtk.exampleapp", :handles_open)
128
+
129
+ signal_connect "startup" do |application|
130
+ [
131
+ "preferences",
132
+ "quit"
133
+ ].each do |action_name|
134
+ action = Gio::SimpleAction.new(action_name)
135
+ action.signal_connect("activate") do |_action, _parameter|
136
+ __send__("#{action_name}_activated")
137
+ end
138
+ application.add_action(action)
139
+ end
140
+ application.set_accels_for_action("app.quit", ["<Control>Q"])
141
+ end
142
+ signal_connect "activate" do |application|
143
+ window = ExampleAppWindow.new(application)
144
+ window.present
145
+ end
146
+ signal_connect "open" do |application, files, hint|
147
+ window = application.windows[0] || ExampleAppWindow.new(application)
148
+ files.each do |file|
149
+ window.open(file)
150
+ end
151
+ window.present
152
+ end
153
+ end
154
+
155
+ private
156
+
157
+ def preferences_activated
158
+ end
159
+
160
+ def quit_activated
161
+ quit
162
+ end
163
+ end
164
+
165
+ app = ExampleApp.new
166
+
167
+ # Gtk::Application#run needs C style argv ([prog, arg1, arg2, ...,argn]).
168
+ # The ARGV ruby variable only contains the arguments ([arg1, arg2, ...,argb])
169
+ # and not the program name. We have to add it explicitly.
170
+
171
+ app.run([$PROGRAM_NAME] + ARGV)
@@ -0,0 +1,20 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <schemalist>
3
+ <schema path="/org/gtk/exampleapp/" id="org.gtk.exampleapp">
4
+ <key name="font" type="s">
5
+ <default>'Monospace 12'</default>
6
+ <summary>Font</summary>
7
+ <description>The font to be used for content.</description>
8
+ </key>
9
+ <key name="transition" type="s">
10
+ <choices>
11
+ <choice value='none'/>
12
+ <choice value='crossfade'/>
13
+ <choice value='slide-left-right'/>
14
+ </choices>
15
+ <default>'none'</default>
16
+ <summary>Transition</summary>
17
+ <description>The transition to use when switching tabs.</description>
18
+ </key>
19
+ </schema>
20
+ </schemalist>
@@ -0,0 +1,15 @@
1
+ # Step 6: Add a preference dialog
2
+
3
+ The original files are located in the following directory.
4
+
5
+ - https://gitlab.gnome.org/GNOME/gtk/-/blob/main/examples/application6
6
+
7
+ The original schema file is [here](https://gitlab.gnome.org/GNOME/gtk/-/blob/main/examples/application6/org.gtk.exampleapp.gschema.xml) and the license is LGPL 2.1 or later.
8
+
9
+ Run `rake` before trying `exampleapp.rb`.
10
+
11
+ ```console
12
+ $ cd gtk4/sample/examples/application6
13
+ $ rake
14
+ $ ruby exampleapp.rb README.md Rakefile
15
+ ```