[gtkmm] Gtk::Stack::get_pages(): Return a Gtk::SelectionListModelImpl



commit e52e6085813bca367348f22ee0aa405d9605c7b0
Author: Kjell Ahlstedt <kjellahlstedt gmail com>
Date:   Fri Apr 9 20:02:25 2021 +0200

    Gtk::Stack::get_pages(): Return a Gtk::SelectionListModelImpl
    
    * gtk/gtkmm/selectionlistmodelimpl.[cc|h]: New files.
    SelectionListModelImpl implements Gio::ListModel and Gtk::SelectionModel.
    It wraps the private gtk class that gtk_stack_get_pages() returns.
    * gtk/gtkmm.h:
    * gtk/gtkmm/filelist.am:
    * gtk/gtkmm/meson.build: List new files.
    * gtk/src/stack.[ccg|hg]: get_pages() returns a SelectionListModelImpl
    instance.  Fixes #92

 gtk/gtkmm.h                         |  1 +
 gtk/gtkmm/filelist.am               |  2 ++
 gtk/gtkmm/meson.build               |  1 +
 gtk/gtkmm/selectionlistmodelimpl.cc | 43 ++++++++++++++++++++++++
 gtk/gtkmm/selectionlistmodelimpl.h  | 65 +++++++++++++++++++++++++++++++++++++
 gtk/src/stack.ccg                   | 19 +++++++++++
 gtk/src/stack.hg                    | 22 ++++++++++++-
 7 files changed, 152 insertions(+), 1 deletion(-)
---
diff --git a/gtk/gtkmm.h b/gtk/gtkmm.h
index e5a5dbce..c3d88626 100644
--- a/gtk/gtkmm.h
+++ b/gtk/gtkmm.h
@@ -261,6 +261,7 @@ extern const int gtkmm_micro_version;
 #include <gtkmm/searchentry.h>
 #include <gtkmm/separator.h>
 #include <gtkmm/selectionfiltermodel.h>
+#include <gtkmm/selectionlistmodelimpl.h>
 #include <gtkmm/settings.h>
 #include <gtkmm/shortcutcontroller.h>
 #include <gtkmm/shortcutlabel.h>
diff --git a/gtk/gtkmm/filelist.am b/gtk/gtkmm/filelist.am
index 74a92fe7..a184f5e1 100644
--- a/gtk/gtkmm/filelist.am
+++ b/gtk/gtkmm/filelist.am
@@ -11,6 +11,7 @@ gtkmm_files_extra_any_cc =            \
        init.cc \
        listviewtext.cc                 \
        object.cc \
+       selectionlistmodelimpl.cc \
        treemodelcolumn.cc              \
        treeview_private.cc
 
@@ -24,6 +25,7 @@ gtkmm_files_extra_any_h =                     \
        init.h \
        listviewtext.h                  \
        object.h \
+       selectionlistmodelimpl.h \
        treemodelcolumn.h               \
        treeview_private.h              \
        version.h \
diff --git a/gtk/gtkmm/meson.build b/gtk/gtkmm/meson.build
index 58f7b2b9..412c8901 100644
--- a/gtk/gtkmm/meson.build
+++ b/gtk/gtkmm/meson.build
@@ -292,6 +292,7 @@ gtkmm_extra_any_h_cc_basenames = [
   'init',
   'listviewtext',
   'object',
+  'selectionlistmodelimpl',
   'treemodelcolumn',
   'treeview_private',
 ]
diff --git a/gtk/gtkmm/selectionlistmodelimpl.cc b/gtk/gtkmm/selectionlistmodelimpl.cc
new file mode 100644
index 00000000..cd41ed0a
--- /dev/null
+++ b/gtk/gtkmm/selectionlistmodelimpl.cc
@@ -0,0 +1,43 @@
+/* Copyright (C) 2021 The gtkmm Development Team
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <gtkmm/selectionlistmodelimpl.h>
+#include <utility> // std::move()
+
+namespace Gtk
+{
+SelectionListModelImpl::SelectionListModelImpl(GObject* castitem)
+: Glib::ObjectBase(nullptr), Glib::Object(castitem)
+{}
+
+SelectionListModelImpl::SelectionListModelImpl(SelectionListModelImpl&& src) noexcept
+: Gio::ListModel(std::move(src)),
+  SelectionModel(std::move(src)),
+  Glib::Object(std::move(src))
+{}
+
+SelectionListModelImpl& SelectionListModelImpl::operator=(SelectionListModelImpl&& src) noexcept
+{
+  Gio::ListModel::operator=(std::move(src));
+  SelectionModel::operator=(std::move(src));
+  Glib::Object::operator=(std::move(src));
+  return *this;
+}
+
+SelectionListModelImpl::~SelectionListModelImpl() noexcept
+{}
+
+} // namespace Gtk
diff --git a/gtk/gtkmm/selectionlistmodelimpl.h b/gtk/gtkmm/selectionlistmodelimpl.h
new file mode 100644
index 00000000..a63c6566
--- /dev/null
+++ b/gtk/gtkmm/selectionlistmodelimpl.h
@@ -0,0 +1,65 @@
+#ifndef _GTKMM_SELECTIONLISTMODELIMPL_H
+#define _GTKMM_SELECTIONLISTMODELIMPL_H
+
+/* Copyright (C) 2021 The gtkmm Development Team
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <gtkmmconfig.h>
+#include <glibmm/object.h>
+#include <giomm/listmodel.h>
+#include <gtkmm/selectionmodel.h>
+
+namespace Gtk
+{
+/** %Gtk::SelectionListModelImpl is an object that implements the Gio::ListModel
+ * and Gtk::SelectionModel interfaces.
+ *
+ * gtk_stack_get_pages() returns an object of a class that implements both
+ * GListModel and GtkSelectionModel. Since that C class is not public,
+ * it's not wrapped in a C++ class. A C object of such a class can be wrapped
+ * in a %Gtk::SelectionListModelImpl object. %Gtk::SelectionListModelImpl does not
+ * directly correspond to any GTK class.
+ *
+ * This class is intended only for wrapping C objects returned from GTK functions.
+ *
+ * @see Gtk::Stack::get_pages()
+ * @newin{4,2}
+ */
+
+class GTKMM_API SelectionListModelImpl : public Gio::ListModel, public SelectionModel, public Glib::Object
+{
+#ifndef DOXYGEN_SHOULD_SKIP_THIS
+public:
+  // noncopyable
+  SelectionListModelImpl(const SelectionListModelImpl&) = delete;
+  SelectionListModelImpl& operator=(const SelectionListModelImpl&) = delete;
+
+protected:
+  explicit SelectionListModelImpl(GObject* castitem);
+  friend class Stack;
+
+#endif /* DOXYGEN_SHOULD_SKIP_THIS */
+
+public:
+  SelectionListModelImpl(SelectionListModelImpl&& src) noexcept;
+  SelectionListModelImpl& operator=(SelectionListModelImpl&& src) noexcept;
+
+  ~SelectionListModelImpl() noexcept override;
+};
+
+} // namespace Gtk
+
+#endif /* _GTKMM_SELECTIONLISTMODELIMPL_H */
diff --git a/gtk/src/stack.ccg b/gtk/src/stack.ccg
index acb379f4..75bf6405 100644
--- a/gtk/src/stack.ccg
+++ b/gtk/src/stack.ccg
@@ -16,9 +16,28 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  */
 
+#include <gtkmm/selectionlistmodelimpl.h>
 #include <gtk/gtk.h>
 
 namespace Gtk
 {
 
+Glib::RefPtr<SelectionModel> Stack::get_pages()
+{
+  // gtk_stack_get_pages() returns a pointer to a private GObject class that
+  // implements GListModel and GtkSelectionModel.
+  // Such an instance is wrapped in a Gtk::SelectionListModelImpl.
+  // gtk_stack_get_pages() gives us a ref (transfer full).
+  GtkSelectionModel* stack_pages = gtk_stack_get_pages(gobj());
+  if (G_IS_LIST_MODEL(stack_pages))
+  {
+    // Look up current C++ wrapper instance:
+    Glib::ObjectBase* pCppObject = Glib::ObjectBase::_get_current_wrapper((GObject*)stack_pages);
+    if (!pCppObject)
+      pCppObject = new SelectionListModelImpl((GObject*)stack_pages);
+    return Glib::make_refptr_for_instance<SelectionModel>(dynamic_cast<SelectionModel*>(pCppObject));
+  }
+  return Glib::wrap(stack_pages);
+}
+
 } //namespace Gtk
diff --git a/gtk/src/stack.hg b/gtk/src/stack.hg
index 3ce55092..f56058af 100644
--- a/gtk/src/stack.hg
+++ b/gtk/src/stack.hg
@@ -88,7 +88,27 @@ public:
   _WRAP_METHOD(void set_interpolate_size(bool interpolate_size), gtk_stack_set_interpolate_size, newin 
"3,20")
   _WRAP_METHOD(bool get_interpolate_size() const, gtk_stack_get_interpolate_size, newin "3,20")
 
-  _WRAP_METHOD(Glib::RefPtr<SelectionModel> get_pages(), gtk_stack_get_pages)
+  /** Returns a Gio::ListModel that contains the pages of the stack.
+   *
+   * It can be used to keep an up-to-date view. The model also
+   * implements Gtk::SelectionModel and can be used to track and
+   * modify the visible page.
+   *
+   * To use Gio::ListModel API, do something like
+   * @code
+   * auto pages = stack->get_pages();
+   * auto list_model = std::dynamic_pointer_cast<Gio::ListModel>(pages);
+   * if (list_model)
+   *   list_model->do_something();
+   * @endcode
+   *
+   * @return A Gtk::SelectionModel for the stack's children. The caller becomes
+   * the owner of the returned instance. When the caller's reference is dropped,
+   * the instance is deleted.
+   */
+  Glib::RefPtr<SelectionModel> get_pages();
+
+  /// See the non-const version.
   _WRAP_METHOD(Glib::RefPtr<const SelectionModel> get_pages() const, gtk_stack_get_pages, constversion)
 
   _WRAP_PROPERTY("hhomogeneous", bool)


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