Debian Codegraph @

examples/book/buildapp/step5/exampleappprefs.cc

raw
1 /* gtkmm example Copyright (C) 2016 gtkmm development team
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program 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
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, see <https://www.gnu.org/licenses/>.
15 */
16
17 #include "exampleappprefs.h"
18 #include "exampleappwindow.h"
19 #include <array>
20 #include <stdexcept>
21
22 namespace
23 {
24 struct TransitionTypeStruct
25 {
26 Glib::ustring id; // Value of "transition" key in Gio::Settings
27 Glib::ustring text; // Text in the DropDown list
28 };
29
30 const std::array<TransitionTypeStruct, 3> transitionTypes =
31 {
32 TransitionTypeStruct{"none", "None"},
33 TransitionTypeStruct{"crossfade", "Fade"},
34 TransitionTypeStruct{"slide-left-right", "Slide"}
35 };
36
37 } // anonymous namespace
38
39 ExampleAppPrefs::ExampleAppPrefs(BaseObjectType* cobject,
40 const Glib::RefPtr<Gtk::Builder>& refBuilder)
41 : Gtk::Window(cobject),
42 m_refBuilder(refBuilder)
43 {
44 m_font = m_refBuilder->get_widget<Gtk::FontDialogButton>("font");
45 if (!m_font)
46 throw std::runtime_error("No \"font\" object in prefs.ui");
47
48 m_transition = m_refBuilder->get_widget<Gtk::DropDown>("transition");
49 if (!m_transition)
50 throw std::runtime_error("No \"transition\" object in prefs.ui");
51
52 // DropDown for transition type.
53 auto string_list = Gtk::StringList::create();
54 for (const auto& transitionType : transitionTypes)
55 string_list->append(transitionType.text);
56
57 m_transition->set_model(string_list);
58
59 m_settings = Gio::Settings::create("org.gtkmm.exampleapp");
60
61 // Connect preference properties to the Gio::Settings.
62 #if HAS_GIO_SETTINGS_BIND_WITH_MAPPING
63 m_settings->bind<Glib::ustring, Pango::FontDescription>("font",
64 m_font->property_font_desc(), Gio::Settings::BindFlags::DEFAULT,
65 [](const auto& font) { return Pango::FontDescription(font); },
66 [](const auto& fontdesc) { return fontdesc.to_string(); }
67 );
68 m_settings->bind<Glib::ustring, unsigned int>("transition",
69 m_transition->property_selected(), Gio::Settings::BindFlags::DEFAULT,
70 [](const auto& transition) { return map_from_ustring_to_int(transition); },
71 [](const auto& pos) { return map_from_int_to_ustring(pos); }
72 );
73 #else
74 // This is easier when g_settings_bind_with_mapping() is
75 // wrapped in a Gio::Settings method.
76 m_settings->signal_changed("font").connect(
77 sigc::mem_fun(*this, &ExampleAppPrefs::on_font_setting_changed));
78 m_font->property_font_desc().signal_changed().connect(
79 sigc::mem_fun(*this, &ExampleAppPrefs::on_font_selection_changed));
80
81 m_settings->signal_changed("transition").connect(
82 sigc::mem_fun(*this, &ExampleAppPrefs::on_transition_setting_changed));
83 m_transition->property_selected().signal_changed().connect(
84 sigc::mem_fun(*this, &ExampleAppPrefs::on_transition_selection_changed));
85
86 // Synchronize the preferences dialog with m_settings.
87 on_font_setting_changed("font");
88 on_transition_setting_changed("transition");
89 #endif
90 }
91
92 //static
93 ExampleAppPrefs* ExampleAppPrefs::create(Gtk::Window& parent)
94 {
95 // Load the Builder file and instantiate its widgets.
96 auto refBuilder = Gtk::Builder::create_from_resource("/org/gtkmm/exampleapp/prefs.ui");
97
98 auto dialog = Gtk::Builder::get_widget_derived<ExampleAppPrefs>(refBuilder, "prefs_dialog");
99 if (!dialog)
100 throw std::runtime_error("No \"prefs_dialog\" object in prefs.ui");
101
102 dialog->set_transient_for(parent);
103
104 return dialog;
105 }
106
107 #if HAS_GIO_SETTINGS_BIND_WITH_MAPPING
108 std::optional<unsigned int> ExampleAppPrefs::map_from_ustring_to_int(const Glib::ustring& transition)
109 {
110 for (std::size_t i = 0; i < transitionTypes.size(); ++i)
111 {
112 if (transitionTypes[i].id == transition)
113 return i;
114 }
115 return std::nullopt;
116 }
117
118 std::optional<Glib::ustring> ExampleAppPrefs::map_from_int_to_ustring(const unsigned int& pos)
119 {
120 if (pos >= transitionTypes.size())
121 return std::nullopt;
122 return transitionTypes[pos].id;
123 }
124 #else
125 void ExampleAppPrefs::on_font_setting_changed(const Glib::ustring& /* key */)
126 {
127 const auto font_setting = m_settings->get_string("font");
128 const auto font_button = m_font->get_font_desc().to_string();
129 if (font_setting != font_button)
130 m_font->set_font_desc(Pango::FontDescription(font_setting));
131 }
132
133 void ExampleAppPrefs::on_font_selection_changed()
134 {
135 const auto font_setting = m_settings->get_string("font");
136 const auto font_button = m_font->get_font_desc().to_string();
137 if (font_setting != font_button)
138 m_settings->set_string("font", font_button);
139 }
140
141 void ExampleAppPrefs::on_transition_setting_changed(const Glib::ustring& /* key */)
142 {
143 const auto transition_setting = m_settings->get_string("transition");
144 const auto transition_button = transitionTypes[m_transition->get_selected()].id;
145 if (transition_setting != transition_button)
146 {
147 for (std::size_t i = 0; i < transitionTypes.size(); ++i)
148 {
149 if (transitionTypes[i].id == transition_setting)
150 {
151 m_transition->set_selected(i);
152 break;
153 }
154 }
155 }
156 }
157
158 void ExampleAppPrefs::on_transition_selection_changed()
159 {
160 const auto pos = m_transition->get_selected();
161 if (pos >= transitionTypes.size())
162 return;
163 const auto transition_setting = m_settings->get_string("transition");
164 const auto transition_button = transitionTypes[pos].id;
165 if (transition_setting != transition_button)
166 m_settings->set_string("transition", transition_button);
167 }
168 #endif