[gtkmm/gtkmm-4-2] SizeGroup demo: Derive from Gtk::Window instead of Dialog



commit f66c2f14d4552c3babe6cf0b08b344afe26498b9
Author: Kjell Ahlstedt <kjellahlstedt gmail com>
Date:   Sun Jun 27 13:08:59 2021 +0200

    SizeGroup demo: Derive from Gtk::Window instead of Dialog
    
    and set active items in the combo boxes, so something is shown.

 demos/gtk-demo/example_sizegroup.cc | 93 ++++++++++++++++---------------------
 1 file changed, 39 insertions(+), 54 deletions(-)
---
diff --git a/demos/gtk-demo/example_sizegroup.cc b/demos/gtk-demo/example_sizegroup.cc
index 40a4db55..85eb2a34 100644
--- a/demos/gtk-demo/example_sizegroup.cc
+++ b/demos/gtk-demo/example_sizegroup.cc
@@ -1,22 +1,21 @@
 /* Size Groups
  *
- * GtkSizeGroup provides a mechanism for grouping a number of
+ * Gtk::SizeGroup provides a mechanism for grouping a number of
  * widgets together so they all request the same amount of space.
  * This is typically useful when you want a column of widgets to
- * have the same size, but you can't use a GtkGrid widget.
+ * have the same size, but you can't use a Gtk::Grid widget.
  *
  * Note that size groups only affect the amount of space requested,
  * not the size that the widgets finally receive. If you want the
- * widgets in a GtkSizeGroup to actually be the same size, you need
+ * widgets in a Gtk::SizeGroup to actually be the same size, you need
  * to pack them in such a way that they get the size they request
- * and not more. For example, if you are packing your widgets
- * into a table, you would not include the Gtk::FILL flag.
+ * and not more.
  */
 
 #include <gtkmm.h>
 #include <list>
 
-class Example_SizeGroup : public Gtk::Dialog
+class Example_SizeGroup : public Gtk::Window
 {
 public:
   Example_SizeGroup();
@@ -24,21 +23,21 @@ public:
 
 protected:
   //Signal handlers:
-  virtual void on_checkbutton_toggled();
+  void on_checkbutton_toggled();
+  void on_close();
 
-  typedef std::list<Glib::ustring> type_listStrings;
-  virtual void add_row(Gtk::Grid& grid, int row, const Glib::RefPtr<Gtk::SizeGroup>& size_group, const 
Glib::ustring& label_text, const std::list<Glib::ustring>& options);
-  virtual Gtk::ComboBoxText* create_combobox(const std::list<Glib::ustring>& strings);
-
-  void on_response(int response_id) override;
+  using type_listStrings = std::list<Glib::ustring>;
+  void add_row(Gtk::Grid& grid, int row, const Glib::RefPtr<Gtk::SizeGroup>& size_group,
+    const Glib::ustring& label_text, const std::list<Glib::ustring>& options);
+  Gtk::ComboBoxText* create_combobox(const std::list<Glib::ustring>& strings);
 
   //Member widgets:
   Gtk::Frame m_Frame_Color, m_Frame_Line;
   Gtk::Box m_VBox;
-  Gtk::Box m_HBox;
   Glib::RefPtr<Gtk::SizeGroup> m_refSizeGroup;
   Gtk::Grid m_Grid_Color, m_Grid_Line;
   Gtk::CheckButton m_CheckButton;
+  Gtk::Button m_CloseButton;
 };
 
 //Called by DemoWindow;
@@ -48,25 +47,21 @@ Gtk::Window* do_sizegroup()
 }
 
 Example_SizeGroup::Example_SizeGroup()
-: Gtk::Dialog("Gtk::SizeGroup"),
-  m_Frame_Color("Color Options"),
+: m_Frame_Color("Color Options"),
   m_Frame_Line("Line Options"),
   m_VBox(Gtk::Orientation::VERTICAL, 5),
-  m_HBox(Gtk::Orientation::HORIZONTAL, 5),
-  m_CheckButton("_Enable grouping", true)
+  m_CheckButton("_Enable grouping", true),
+  m_CloseButton("_Close", true)
 {
+  set_title("Gtk::SizeGroup");
   set_resizable(false);
-  add_button("_Close", Gtk::ResponseType::CLOSE);
 
-  get_content_area()->append(m_VBox);
+  set_child(m_VBox);
   m_VBox.set_margin(5);
-  m_VBox.set_expand();
 
   m_refSizeGroup = Gtk::SizeGroup::create(Gtk::SizeGroup::Mode::HORIZONTAL),
 
-  /* Create one frame holding color options
-   */
-  m_Frame_Color.set_expand();
+  // Create one frame holding color options
   m_VBox.append(m_Frame_Color);
 
   m_Grid_Color.set_margin(5);
@@ -74,16 +69,11 @@ Example_SizeGroup::Example_SizeGroup()
   m_Grid_Color.set_column_spacing(10);
   m_Frame_Color.set_child(m_Grid_Color);
 
-  type_listStrings color_options;
-  color_options.push_back("Red");
-  color_options.push_back("Green");
-  color_options.push_back("Blue");
-
+  const type_listStrings color_options{"Red", "Green", "Blue"};
   add_row(m_Grid_Color, 0, m_refSizeGroup, "_Foreground", color_options);
   add_row(m_Grid_Color, 1, m_refSizeGroup, "_Background", color_options);
 
-  /* And another frame holding line style options
-   */
+  // and another frame holding line style options
   m_VBox.append(m_Frame_Line);
 
   m_Grid_Line.set_margin(5);
@@ -91,25 +81,21 @@ Example_SizeGroup::Example_SizeGroup()
   m_Grid_Line.set_column_spacing(10);
   m_Frame_Line.set_child(m_Grid_Line);
 
-
-  type_listStrings dash_options;
-  dash_options.push_back("Solid");
-  dash_options.push_back("Dashed");
-  dash_options.push_back("Dotted");
-
+  const type_listStrings dash_options{"Solid", "Dashed", "Dotted"};
   add_row(m_Grid_Line, 0, m_refSizeGroup, "_Dashing", dash_options);
 
-  type_listStrings end_options;
-  end_options.push_back("Square");
-  end_options.push_back("Round");
-  end_options.push_back("Arrow");
-
+  const type_listStrings end_options{"Square", "Round", "Double Arrow"};
   add_row(m_Grid_Line, 1, m_refSizeGroup, "_Line ends", end_options);
 
-  /* And a check button to turn grouping on and off */
+  // and a check button to turn grouping on and off
   m_VBox.append(m_CheckButton);
   m_CheckButton.set_active();
   m_CheckButton.signal_toggled().connect(sigc::mem_fun(*this, &Example_SizeGroup::on_checkbutton_toggled));
+
+  // and finally a close button
+  m_VBox.append(m_CloseButton);
+  m_CloseButton.set_halign(Gtk::Align::END);
+  m_CloseButton.signal_clicked().connect(sigc::mem_fun(*this, &Example_SizeGroup::on_close));
 }
 
 Example_SizeGroup::~Example_SizeGroup()
@@ -118,11 +104,10 @@ Example_SizeGroup::~Example_SizeGroup()
 
 void Example_SizeGroup::on_checkbutton_toggled()
 {
+  // Gtk::SizeGroup::Mode::NONE is not generally useful, but is useful
+  // here to show the effect of Gtk::SizeGroup::Mode::HORIZONTAL by contrast.
   auto new_mode = Gtk::SizeGroup::Mode::HORIZONTAL;
-
-  if(m_CheckButton.get_active())
-    new_mode = Gtk::SizeGroup::Mode::HORIZONTAL;
-  else
+  if (!m_CheckButton.get_active())
     new_mode = Gtk::SizeGroup::Mode::NONE;
 
   m_refSizeGroup->set_mode(new_mode);
@@ -133,20 +118,20 @@ void Example_SizeGroup::add_row(Gtk::Grid& grid, int row,
                                 const Glib::ustring& label_text,
                                 const std::list<Glib::ustring>& options)
 {
-  auto pLabel = Gtk::make_managed<Gtk::Label>(label_text, Gtk::Align::START, Gtk::Align::END, true);
+  auto pLabel = Gtk::make_managed<Gtk::Label>(label_text, Gtk::Align::START, Gtk::Align::BASELINE, true);
 
-  grid.attach(*pLabel, 0, row, 1, 1);
   pLabel->set_hexpand();
+  grid.attach(*pLabel, 0, row);
 
   auto pComboBoxText = create_combobox(options);
   pLabel->set_mnemonic_widget(*pComboBoxText);
+  pComboBoxText->set_halign(Gtk::Align::END);
+  pComboBoxText->set_valign(Gtk::Align::BASELINE);
   size_group->add_widget(*pComboBoxText);
-
-  grid.attach(*pComboBoxText, 1, row, 1, 1);
+  grid.attach(*pComboBoxText, 1, row);
 }
 
-/* Convenience function to create an option menu holding a number of strings
- */
+// Convenience function to create an option menu holding a number of strings
 Gtk::ComboBoxText* Example_SizeGroup::create_combobox(const std::list<Glib::ustring>& strings)
 {
   auto pCombo = Gtk::make_managed<Gtk::ComboBoxText>();
@@ -155,12 +140,12 @@ Gtk::ComboBoxText* Example_SizeGroup::create_combobox(const std::list<Glib::ustr
   {
     pCombo->append(str);
   }
+  pCombo->set_active(0);
 
   return pCombo;
 }
 
-void Example_SizeGroup::on_response(int)
+void Example_SizeGroup::on_close()
 {
   hide();
 }
-


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]