[gtkmm] Add ColumnView demo



commit a05aae06b5d8a5c2ad4d1e8cffdfca422afd26fd
Author: Kjell Ahlstedt <kjellahlstedt gmail com>
Date:   Wed Jul 21 15:02:22 2021 +0200

    Add ColumnView demo
    
    Based on a test program by Andreas Persson.

 demos/Makefile.am                             |   1 +
 demos/gtk-demo/demo.gresource.xml             |   1 +
 demos/gtk-demo/demos.h                        |   2 +
 demos/gtk-demo/example_listview_columnview.cc | 118 ++++++++++++++++++++++++++
 demos/gtk-demo/meson.build                    |   1 +
 5 files changed, 123 insertions(+)
---
diff --git a/demos/Makefile.am b/demos/Makefile.am
index 37b86205..01715c1e 100644
--- a/demos/Makefile.am
+++ b/demos/Makefile.am
@@ -50,6 +50,7 @@ GTK_DEMOS = \
   gtk-demo/example_iconview.cc \
   gtk-demo/example_images.cc \
   gtk-demo/example_listview_applauncher.cc \
+  gtk-demo/example_listview_columnview.cc \
   gtk-demo/example_overlay.cc \
   gtk-demo/example_panes.cc \
   gtk-demo/example_pixbufs.cc \
diff --git a/demos/gtk-demo/demo.gresource.xml b/demos/gtk-demo/demo.gresource.xml
index a68c7d0b..98f68d52 100644
--- a/demos/gtk-demo/demo.gresource.xml
+++ b/demos/gtk-demo/demo.gresource.xml
@@ -51,6 +51,7 @@
     <file>example_iconview.cc</file>
     <file>example_images.cc</file>
     <file>example_listview_applauncher.cc</file>
+    <file>example_listview_columnview.cc</file>
     <file>example_overlay.cc</file>
     <file>example_panes.cc</file>
     <file>example_pixbufs.cc</file>
diff --git a/demos/gtk-demo/demos.h b/demos/gtk-demo/demos.h
index e4c04614..a8295a9c 100644
--- a/demos/gtk-demo/demos.h
+++ b/demos/gtk-demo/demos.h
@@ -27,6 +27,7 @@ Gtk::Window* do_iconbrowser();
 Gtk::Window* do_iconview();
 Gtk::Window* do_images();
 Gtk::Window* do_listview_applauncher();
+Gtk::Window* do_listview_columnview();
 Gtk::Window* do_overlay();
 Gtk::Window* do_panes();
 Gtk::Window* do_pixbufs();
@@ -51,6 +52,7 @@ Demo child0[] =
 Demo child1[] =
 {
   { "Application launcher", "example_listview_applauncher.cc", sigc::ptr_fun(&do_listview_applauncher), 
nullptr },
+  { "Column View", "example_listview_columnview.cc", sigc::ptr_fun(&do_listview_columnview), nullptr },
   { nullptr, nullptr, type_slotDo(), nullptr }
 };
 
diff --git a/demos/gtk-demo/example_listview_columnview.cc b/demos/gtk-demo/example_listview_columnview.cc
new file mode 100644
index 00000000..ed412386
--- /dev/null
+++ b/demos/gtk-demo/example_listview_columnview.cc
@@ -0,0 +1,118 @@
+/* Column View
+ *
+ * Gtk::ColumnView is a widget to present a view into a large dynamic list of
+ * items using multiple columns with headers. However, the list in this demo is
+ * neither large nor dynamic.
+ */
+
+#include <gtkmm.h>
+
+class MyObject : public Glib::Object
+{
+public:
+  enum NamePart
+  {
+    FIRST,
+    SECOND
+  };
+
+  static Glib::RefPtr<MyObject> create(
+    const Glib::ustring& first_name, const Glib::ustring& second_name)
+  {
+    return Glib::make_refptr_for_instance<MyObject>(new MyObject{first_name, second_name});
+  }
+
+  void set_name(const Glib::ustring& first_name, const Glib::ustring& second_name)
+  {
+    m_name[FIRST] = first_name;
+    m_name[SECOND] = second_name;
+  }
+
+  Glib::ustring get_name(NamePart name_part) const { return m_name[name_part]; }
+
+protected:
+  MyObject(const Glib::ustring& first_name, const Glib::ustring& second_name)
+  : m_name{first_name, second_name}
+  {
+  }
+
+private:
+  Glib::ustring m_name[2];
+};
+
+class Example_Listview_ColumnView : public Gtk::Window
+{
+public:
+  Example_Listview_ColumnView();
+  ~Example_Listview_ColumnView() override;
+
+protected:
+  // Signal handlers:
+  void on_setup_listitem(const Glib::RefPtr<Gtk::ListItem>& list_item);
+  void on_bind_listitem(const Glib::RefPtr<Gtk::ListItem>& list_item,
+                        MyObject::NamePart name_part);
+
+  // Member widget:
+  Gtk::ColumnView m_ColumnView;
+};
+
+// Called by DemoWindow
+Gtk::Window* do_listview_columnview()
+{
+  return new Example_Listview_ColumnView();
+}
+
+Example_Listview_ColumnView::Example_Listview_ColumnView()
+{
+  set_title("Gtk::ColumnView");
+  set_default_size(300, 200);
+
+  auto store = Gio::ListStore<MyObject>::create();
+  store->append(MyObject::create("Joe", "Brown"));
+  store->append(MyObject::create("Mary", "West"));
+  store->append(MyObject::create("Danny", "Jones"));
+
+  m_ColumnView.set_model(Gtk::SingleSelection::create(store));
+  set_child(m_ColumnView);
+
+  // First column
+  auto factory = Gtk::SignalListItemFactory::create();
+  factory->signal_setup().connect(
+    sigc::mem_fun(*this, &Example_Listview_ColumnView::on_setup_listitem));
+  factory->signal_bind().connect(
+    sigc::bind(sigc::mem_fun(*this, &Example_Listview_ColumnView::on_bind_listitem),
+      MyObject::FIRST));
+  auto col = Gtk::ColumnViewColumn::create("First Name", factory);
+  m_ColumnView.append_column(col);
+
+  // Second column
+  factory = Gtk::SignalListItemFactory::create();
+  factory->signal_setup().connect(
+    sigc::mem_fun(*this, &Example_Listview_ColumnView::on_setup_listitem));
+  factory->signal_bind().connect(
+    sigc::bind(sigc::mem_fun(*this, &Example_Listview_ColumnView::on_bind_listitem),
+      MyObject::SECOND));
+  col = Gtk::ColumnViewColumn::create("Second Name", factory);
+  m_ColumnView.append_column(col);
+}
+
+Example_Listview_ColumnView::~Example_Listview_ColumnView()
+{
+}
+
+void Example_Listview_ColumnView::on_setup_listitem(const Glib::RefPtr<Gtk::ListItem>& list_item)
+{
+  list_item->set_child(*Gtk::make_managed<Gtk::Label>());
+}
+
+void Example_Listview_ColumnView::on_bind_listitem(
+  const Glib::RefPtr<Gtk::ListItem>& list_item, MyObject::NamePart name_part)
+{
+  auto label = dynamic_cast<Gtk::Label*>(list_item->get_child());
+  if (label)
+  {
+    auto item = list_item->get_item();
+    if (auto mo = std::dynamic_pointer_cast<MyObject>(item))
+      label->set_text(mo->get_name(name_part));
+  }
+}
diff --git a/demos/gtk-demo/meson.build b/demos/gtk-demo/meson.build
index 2fd9f84b..9211c9c2 100644
--- a/demos/gtk-demo/meson.build
+++ b/demos/gtk-demo/meson.build
@@ -21,6 +21,7 @@ gtkmm_demo_cc_files = [
   'example_iconview.cc',
   'example_images.cc',
   'example_listview_applauncher.cc',
+  'example_listview_columnview.cc',
   'example_overlay.cc',
   'example_panes.cc',
   'example_pixbufs.cc',


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