Debian Codegraph @

demos/gtk-demo/example_sizegroup.cc

raw
1 /* Size Groups
2 *
3 * Gtk::SizeGroup provides a mechanism for grouping a number of
4 * widgets together so they all request the same amount of space.
5 * This is typically useful when you want a column of widgets to
6 * have the same size, but you can't use a Gtk::Grid widget.
7 *
8 * Note that size groups only affect the amount of space requested,
9 * not the size that the widgets finally receive. If you want the
10 * widgets in a Gtk::SizeGroup to actually be the same size, you need
11 * to pack them in such a way that they get the size they request
12 * and not more. For example, if you are packing your widgets
13 * into a table, you would not include the Gtk::FILL flag.
14 */
15
16 #include <gtkmm.h>
17 #include <list>
18
19 class Example_SizeGroup : public Gtk::Window
20 {
21 public:
22 Example_SizeGroup();
23 ~Example_SizeGroup() override;
24
25 protected:
26 //Signal handlers:
27 void on_checkbutton_toggled();
28 void on_close();
29
30 using type_listStrings = std::list<Glib::ustring>;
31 void add_row(Gtk::Grid& grid, int row, const Glib::RefPtr<Gtk::SizeGroup>& size_group,
32 const Glib::ustring& label_text, const std::list<Glib::ustring>& options);
33 Gtk::ComboBoxText* create_combobox(const std::list<Glib::ustring>& strings);
34
35 //Member widgets:
36 Gtk::Frame m_Frame_Color, m_Frame_Line;
37 Gtk::Box m_VBox;
38 Glib::RefPtr<Gtk::SizeGroup> m_refSizeGroup;
39 Gtk::Grid m_Grid_Color, m_Grid_Line;
40 Gtk::CheckButton m_CheckButton;
41 Gtk::Button m_CloseButton;
42 };
43
44 //Called by DemoWindow;
45 Gtk::Window* do_sizegroup()
46 {
47 return new Example_SizeGroup();
48 }
49
50 Example_SizeGroup::Example_SizeGroup()
51 : m_Frame_Color("Color Options"),
52 m_Frame_Line("Line Options"),
53 m_VBox(Gtk::ORIENTATION_VERTICAL, 5),
54 m_CheckButton("_Enable grouping", true),
55 m_CloseButton("_Close", true)
56 {
57 set_title("Gtk::SizeGroup");
58 set_resizable(false);
59
60 add(m_VBox);
61 m_VBox.set_border_width(5);
62
63 m_refSizeGroup = Gtk::SizeGroup::create(Gtk::SIZE_GROUP_HORIZONTAL),
64
65 // Create one frame holding color options
66 m_VBox.pack_start(m_Frame_Color);
67
68 m_Grid_Color.set_border_width(5);
69 m_Grid_Color.set_row_spacing(5);
70 m_Grid_Color.set_column_spacing(10);
71 m_Frame_Color.add(m_Grid_Color);
72
73 const type_listStrings color_options{"Red", "Green", "Blue"};
74 add_row(m_Grid_Color, 0, m_refSizeGroup, "_Foreground", color_options);
75 add_row(m_Grid_Color, 1, m_refSizeGroup, "_Background", color_options);
76
77 // and another frame holding line style options
78 m_VBox.pack_start(m_Frame_Line, Gtk::PACK_SHRINK);
79
80 m_Grid_Line.set_border_width(5);
81 m_Grid_Line.set_row_spacing(5);
82 m_Grid_Line.set_column_spacing(10);
83 m_Frame_Line.add(m_Grid_Line);
84
85 const type_listStrings dash_options{"Solid", "Dashed", "Dotted"};
86 add_row(m_Grid_Line, 0, m_refSizeGroup, "_Dashing", dash_options);
87
88 const type_listStrings end_options{"Square", "Round", "Double Arrow"};
89 add_row(m_Grid_Line, 1, m_refSizeGroup, "_Line ends", end_options);
90
91 // and a check button to turn grouping on and off
92 m_VBox.pack_start(m_CheckButton, Gtk::PACK_SHRINK);
93 m_CheckButton.set_active();
94 m_CheckButton.signal_toggled().connect(sigc::mem_fun(*this, &Example_SizeGroup::on_checkbutton_toggled));
95
96 // and finally a close button
97 m_VBox.pack_start(m_CloseButton, Gtk::PACK_SHRINK);
98 m_CloseButton.set_halign(Gtk::ALIGN_END);
99 m_CloseButton.signal_clicked().connect(sigc::mem_fun(*this, &Example_SizeGroup::on_close));
100
101 show_all();
102 }
103
104 Example_SizeGroup::~Example_SizeGroup()
105 {
106 }
107
108 void Example_SizeGroup::on_checkbutton_toggled()
109 {
110 // Gtk::SIZE_GROUP_NONE is not generally useful, but is useful
111 // here to show the effect of Gtk::SIZE_GROUP_HORIZONTAL by contrast.
112 Gtk::SizeGroupMode new_mode = Gtk::SIZE_GROUP_HORIZONTAL;
113 if (!m_CheckButton.get_active())
114 new_mode = Gtk::SIZE_GROUP_NONE;
115
116 m_refSizeGroup->set_mode(new_mode);
117 }
118
119 void Example_SizeGroup::add_row(Gtk::Grid& grid, int row,
120 const Glib::RefPtr<Gtk::SizeGroup>& size_group,
121 const Glib::ustring& label_text,
122 const std::list<Glib::ustring>& options)
123 {
124 auto pLabel = Gtk::make_managed<Gtk::Label>(label_text, Gtk::ALIGN_START, Gtk::ALIGN_BASELINE, true);
125
126 pLabel->set_hexpand();
127 grid.attach(*pLabel, 0, row, 1, 1);
128
129 auto pComboBoxText = create_combobox(options);
130 pLabel->set_mnemonic_widget(*pComboBoxText);
131 pComboBoxText->set_halign(Gtk::ALIGN_END);
132 pComboBoxText->set_valign(Gtk::ALIGN_BASELINE);
133 size_group->add_widget(*pComboBoxText);
134 grid.attach(*pComboBoxText, 1, row, 1, 1);
135 }
136
137 // Convenience function to create an option menu holding a number of strings
138 Gtk::ComboBoxText* Example_SizeGroup::create_combobox(const std::list<Glib::ustring>& strings)
139 {
140 auto pCombo = Gtk::make_managed<Gtk::ComboBoxText>();
141
142 for(const auto& str : strings)
143 {
144 pCombo->append(str);
145 }
146 pCombo->set_active(0);
147
148 return pCombo;
149 }
150
151 void Example_SizeGroup::on_close()
152 {
153 hide();
154 }