Debian Codegraph @

demos/gtk-demo/example_panes.cc

raw
1 /* Paned Widgets
2 *
3 * The GtkHPaned and GtkVPaned Widgets divide their content
4 * area into two panes with a divider in between that the
5 * user can adjust. A separate child is placed into each
6 * pane.
7 *
8 * There are a number of options that can be set for each pane.
9 * This test contains both a horizontal (HPaned) and a vertical
10 * (VPaned) widget, and allows you to adjust the options for
11 * each side of each widget.
12 */
13
14 #include <gtkmm.h>
15
16 namespace
17 {
18
19 class Example_Panes : public Gtk::Window
20 {
21 public:
22 Example_Panes();
23 ~Example_Panes() override;
24 };
25
26 class PaneOptions : public Gtk::Frame
27 {
28 public:
29 PaneOptions(Gtk::Paned& paned, const Glib::ustring& frame_label,
30 const Glib::ustring& label1, const Glib::ustring& label2);
31 ~PaneOptions() override;
32
33 private:
34 // member widgets:
35 Gtk::Paned* m_pPaned;
36 Gtk::CheckButton m_CheckButton_resize1;
37 Gtk::CheckButton m_CheckButton_shrink1;
38 Gtk::CheckButton m_CheckButton_resize2;
39 Gtk::CheckButton m_CheckButton_shrink2;
40
41 // signal handlers:
42 void on_checkbutton1();
43 void on_checkbutton2();
44 };
45
46
47 Example_Panes::Example_Panes()
48 {
49 set_title("Panes");
50 set_border_width(0);
51
52 Gtk::Box *const pVBox = new Gtk::Box(Gtk::ORIENTATION_VERTICAL);
53 add(*Gtk::manage(pVBox));
54
55 Gtk::Paned *const pVPaned = new Gtk::Paned(Gtk::ORIENTATION_VERTICAL);
56 pVBox->pack_start(*Gtk::manage(pVPaned));
57 pVPaned->set_border_width(5);
58
59 Gtk::Paned *const pHPaned = new Gtk::Paned(Gtk::ORIENTATION_HORIZONTAL);
60 pVPaned->add1(*Gtk::manage(pHPaned));
61
62 Gtk::Frame *const pFrame1 = new Gtk::Frame();
63 pHPaned->add1(*Gtk::manage(pFrame1));
64 pFrame1->set_shadow_type(Gtk::SHADOW_IN);
65 pFrame1->set_size_request(60, 60);
66 pFrame1->add(*Gtk::make_managed<Gtk::Button>("_Hi there", true));
67
68 Gtk::Frame *const pFrame2 = new Gtk::Frame();
69 pHPaned->add2(*Gtk::manage(pFrame2));
70 pFrame2->set_shadow_type(Gtk::SHADOW_IN);
71 pFrame2->set_size_request(80, 60);
72
73 Gtk::Frame *const pFrame3 = new Gtk::Frame();
74 pVPaned->add2(*Gtk::manage(pFrame3));
75 pFrame3->set_shadow_type(Gtk::SHADOW_IN);
76 pFrame3->set_size_request(60, 80);
77
78 // Now create check buttons to control sizing
79 pVBox->pack_start(*Gtk::make_managed<PaneOptions>(*pHPaned, "Horizontal", "Left", "Right"), Gtk::PACK_SHRINK);
80 pVBox->pack_start(*Gtk::make_managed<PaneOptions>(*pVPaned, "Vertical", "Top", "Bottom"), Gtk::PACK_SHRINK);
81
82 show_all();
83 }
84
85 Example_Panes::~Example_Panes()
86 {}
87
88
89 PaneOptions::PaneOptions(Gtk::Paned& paned, const Glib::ustring& frame_label,
90 const Glib::ustring& label1, const Glib::ustring& label2)
91 :
92 Gtk::Frame (frame_label),
93 m_pPaned (&paned),
94 m_CheckButton_resize1 ("_Resize", true),
95 m_CheckButton_shrink1 ("_Shrink", true),
96 m_CheckButton_resize2 ("_Resize", true),
97 m_CheckButton_shrink2 ("_Shrink", true)
98 {
99 set_border_width(4);
100
101 Gtk::Grid *const pGrid = new Gtk::Grid();
102 add(*Gtk::manage(pGrid));
103
104 pGrid->attach(*Gtk::make_managed<Gtk::Label>(label1), 0, 0, 1, 1);
105 pGrid->attach(*Gtk::make_managed<Gtk::Label>(label2), 1, 0, 1, 1);
106
107 pGrid->attach(m_CheckButton_resize1, 0, 1, 1, 1);
108 pGrid->attach(m_CheckButton_shrink1, 0, 2, 1, 1);
109 pGrid->attach(m_CheckButton_resize2, 1, 1, 1, 1);
110 pGrid->attach(m_CheckButton_shrink2, 1, 2, 1, 1);
111
112 m_CheckButton_resize1.set_active(false);
113 m_CheckButton_shrink1.set_active(true);
114 m_CheckButton_resize2.set_active(true);
115 m_CheckButton_shrink2.set_active(true);
116
117 m_CheckButton_resize1.signal_toggled().connect(sigc::mem_fun(*this, &PaneOptions::on_checkbutton1));
118 m_CheckButton_shrink1.signal_toggled().connect(sigc::mem_fun(*this, &PaneOptions::on_checkbutton1));
119 m_CheckButton_resize2.signal_toggled().connect(sigc::mem_fun(*this, &PaneOptions::on_checkbutton2));
120 m_CheckButton_shrink2.signal_toggled().connect(sigc::mem_fun(*this, &PaneOptions::on_checkbutton2));
121
122 // Sync Gtk::Paned options with the CheckButtons' state. Actually, the
123 // preset state of the buttons should match the Gtk::Paned default settings,
124 // but it is definitely cleaner this way.
125 on_checkbutton1();
126 on_checkbutton2();
127 }
128
129 PaneOptions::~PaneOptions()
130 {}
131
132 void PaneOptions::on_checkbutton1()
133 {
134 Gtk::AttachOptions options = Gtk::AttachOptions(0);
135
136 if(m_CheckButton_resize1.get_active()) options = (options | Gtk::EXPAND);
137 if(m_CheckButton_shrink1.get_active()) options = (options | Gtk::SHRINK);
138
139 Gtk::Widget *const pChild = m_pPaned->get_child1();
140
141 m_pPaned->remove(*pChild);
142 m_pPaned->pack1(*pChild, options);
143 }
144
145 void PaneOptions::on_checkbutton2()
146 {
147 Gtk::AttachOptions options = Gtk::AttachOptions(0);
148
149 if(m_CheckButton_resize2.get_active()) options = (options | Gtk::EXPAND);
150 if(m_CheckButton_shrink2.get_active()) options = (options | Gtk::SHRINK);
151
152 Gtk::Widget *const pChild = m_pPaned->get_child2();
153
154 m_pPaned->remove(*pChild);
155 m_pPaned->pack2(*pChild, options);
156 }
157
158 } // anonymous namespace
159
160
161 // called by DemoWindow
162 Gtk::Window* do_panes()
163 {
164 return new Example_Panes();
165 }
166