[gtkmm] Move Gtk::PrintSettings::PageRange out of PrintSettings



commit fb1906febeec767d8463ec8772b6c845bf120455
Author: Kjell Ahlstedt <kjell ahlstedt bredband net>
Date:   Fri Mar 10 14:58:16 2017 +0100

    Move Gtk::PrintSettings::PageRange out of PrintSettings
    
    * gtk/src/printjob.[ccg|hg]:
    * gtk/src/printsettings.[ccg|hg]:
    Move class Gtk::PrintSettings::PageRange out of PrintSettings,
    making it Gtk::PageRange.
    Fix set/get_page_ranges(). The implementations can't be almost identical
    in PrintJob and PrintSettings. gtk_print_job_set_page_ranges() takes
    ownership of the GtkPageRange array, but gtk_print_settings_set_page_ranges()
    does not. gtk_print_job_get_page_ranges() does not give the caller a newly
    created GtkPageRange array, but gtk_print_settings_get_page_ranges() does.

 gtk/src/printjob.ccg      |   26 +++++++++++++++++---------
 gtk/src/printjob.hg       |    4 ++--
 gtk/src/printsettings.ccg |   32 +++++++++++++++++++++-----------
 gtk/src/printsettings.hg  |   39 ++++++++++++---------------------------
 4 files changed, 52 insertions(+), 49 deletions(-)
---
diff --git a/gtk/src/printjob.ccg b/gtk/src/printjob.ccg
index 8b0820d..f38070a 100644
--- a/gtk/src/printjob.ccg
+++ b/gtk/src/printjob.ccg
@@ -56,22 +56,30 @@ void PrintJob::send(const SlotPrintJobComplete& slot)
   gtk_print_job_send(gobj(), &SignalProxy_Custom_gtk_callback, slot_copy, 
&SignalProxy_Custom_gtk_callback_destroy);
 }
 
-std::vector<PrintSettings::PageRange> PrintJob::get_page_ranges() const
+std::vector<PageRange> PrintJob::get_page_ranges() const
 {
   int num_ranges (0);
-  GtkPageRange* page_ranges =
+  const GtkPageRange* page_ranges =
     gtk_print_job_get_page_ranges(const_cast<GtkPrintJob*>(gobj()), &num_ranges);
 
-  // no need for deep ownership.
-  return Glib::ArrayHandler<PrintSettings::PageRange, 
PrintSettings::PageRangeTraits>::array_to_vector(page_ranges, num_ranges, Glib::OWNERSHIP_SHALLOW);
+  std::vector<PageRange> cpp_ranges;
+  for (int i = 0; i < num_ranges; ++i)
+    cpp_ranges.emplace_back(page_ranges[i].start, page_ranges[i].end);
+
+  // gtk_print_job_get_page_ranges() does not give us a copy. Don't free page_ranges!
+  return cpp_ranges;
 }
 
-void PrintJob::set_page_ranges(const std::vector<PrintSettings::PageRange>& page_ranges)
+void PrintJob::set_page_ranges(const std::vector<PageRange>& page_ranges)
 {
-  gtk_print_job_set_page_ranges(gobj(),
-    Glib::ArrayHandler<PrintSettings::PageRange, 
PrintSettings::PageRangeTraits>::vector_to_array(page_ranges).data (),
-    page_ranges.size ());
+  // gtk_print_job_set_page_ranges() will take ownership of c_ranges, and free it with g_free().
+  GtkPageRange* c_ranges = g_new0(GtkPageRange, page_ranges.size());
+  for (std::size_t i = 0; i < page_ranges.size(); ++i)
+  {
+    c_ranges[i].start = page_ranges[i].start;
+    c_ranges[i].end = page_ranges[i].end;
+  }
+  gtk_print_job_set_page_ranges(gobj(), c_ranges, page_ranges.size());
 }
 
-
 } // namespace Gtk
diff --git a/gtk/src/printjob.hg b/gtk/src/printjob.hg
index ccab5d3..a76cce2 100644
--- a/gtk/src/printjob.hg
+++ b/gtk/src/printjob.hg
@@ -86,10 +86,10 @@ public:
   _WRAP_METHOD(PrintPages get_pages() const, gtk_print_job_get_pages)
   _WRAP_METHOD(void set_pages(PrintPages pages), gtk_print_job_set_pages)
 
-  std::vector<PrintSettings::PageRange> get_page_ranges() const;
+  std::vector<PageRange> get_page_ranges() const;
   _IGNORE(gtk_print_job_get_page_ranges)
 
-  void set_page_ranges(const std::vector<PrintSettings::PageRange>& ranges);
+  void set_page_ranges(const std::vector<PageRange>& ranges);
   _IGNORE(gtk_print_job_set_page_ranges)
 
 
diff --git a/gtk/src/printsettings.ccg b/gtk/src/printsettings.ccg
index 27c5485..b772a26 100644
--- a/gtk/src/printsettings.ccg
+++ b/gtk/src/printsettings.ccg
@@ -18,8 +18,8 @@
 #include <glibmm/vectorutils.h>
 
 #include <gtk/gtk.h>
-
 #include <glib.h>
+#include <memory> // std::unique_ptr
 
 namespace // anonymous
 {
@@ -107,36 +107,46 @@ void PrintSettings::setting_foreach(const SlotForeach& slot)
   gtk_print_settings_foreach(const_cast<GtkPrintSettings*>(gobj()), &proxy_foreach_callback, &slot_copy);
 }
 
-PrintSettings::PageRange::PageRange()
+PageRange::PageRange()
 :
   start(0),
   end(0)
 {}
 
-PrintSettings::PageRange::PageRange(int start_, int end_)
+PageRange::PageRange(int start_, int end_)
 :
   start(start_),
   end(end_)
 {}
 
-std::vector<PrintSettings::PageRange> PrintSettings::get_page_ranges() const
+std::vector<PageRange> PrintSettings::get_page_ranges() const
 {
   int num_ranges (0);
   GtkPageRange* page_ranges =
     gtk_print_settings_get_page_ranges(const_cast<GtkPrintSettings*>(gobj()), &num_ranges);
 
-  // no need for deep ownership.
-  return Glib::ArrayHandler<PageRange, PageRangeTraits>::array_to_vector(page_ranges, num_ranges, 
Glib::OWNERSHIP_SHALLOW);
+  std::vector<PageRange> cpp_ranges;
+  for (int i = 0; i < num_ranges; ++i)
+    cpp_ranges.emplace_back(page_ranges[i].start, page_ranges[i].end);
+
+  // gtk_print_settings_get_page_ranges() gives us a copy. Free page_ranges!
+  g_free(page_ranges);
+  return cpp_ranges;
 }
 
-void PrintSettings::set_page_ranges(const std::vector<PrintSettings::PageRange>& page_ranges)
+void PrintSettings::set_page_ranges(const std::vector<PageRange>& page_ranges)
 {
-  gtk_print_settings_set_page_ranges(gobj(),
-                                     Glib::ArrayHandler<PageRange, 
PageRangeTraits>::vector_to_array(page_ranges).data (),
-                                     page_ranges.size ());
+  // gtk_print_settings_set_page_ranges() does not take ownership of c_ranges.
+  // std::unique_ptr will delete it.
+  std::unique_ptr<GtkPageRange[]> c_ranges(new GtkPageRange[page_ranges.size()]);
+  for (std::size_t i = 0; i < page_ranges.size(); ++i)
+  {
+    c_ranges[i].start = page_ranges[i].start;
+    c_ranges[i].end = page_ranges[i].end;
+  }
+  gtk_print_settings_set_page_ranges(gobj(), c_ranges.get(), page_ranges.size());
 }
 
-
 bool PrintSettings::load_from_key_file(const Glib::KeyFile& key_file)
 {
   GError* gerror = nullptr;
diff --git a/gtk/src/printsettings.hg b/gtk/src/printsettings.hg
index fb42ba4..514a0fd 100644
--- a/gtk/src/printsettings.hg
+++ b/gtk/src/printsettings.hg
@@ -33,6 +33,18 @@ _WRAP_ENUM(PrintPages, GtkPrintPages)
 _WRAP_ENUM(PageSet, GtkPageSet)
 _WRAP_ENUM(NumberUpLayout, GtkNumberUpLayout)
 
+/** A page range.
+ */
+class PageRange
+{
+public:
+  PageRange();
+  PageRange(int start, int end);
+
+  int start;
+  int end;
+};
+
 /** A PrintSettings object represents the settings of a print dialog in a system-independent way.
  * The main use for this object is that once you've printed you can get a settings object that
  * represents the settings the user chose, and the next time you print you can pass that object
@@ -263,33 +275,6 @@ public:
   _WRAP_METHOD(PrintPages get_print_pages() const, gtk_print_settings_get_print_pages)
   _WRAP_METHOD(void set_print_pages(PrintPages pages), gtk_print_settings_set_print_pages)
 
-  //TODO: Move this out of the class when we can break ABI,
-  //because this is also used in PringJob.
-  //This corresponds to GtkPageRange.
-  class PageRange
-  {
-  public:
-    PageRange();
-    PageRange(int start, int end);
-
-    int start;
-    int end;
-  };
-
-#ifndef DOXYGEN_SHOULD_SKIP_THIS
-  struct PageRangeTraits
-  {
-    typedef PageRange          CppType;
-    typedef GtkPageRange       CType;
-    typedef GtkPageRange       CTypeNonConst;
-
-    static CType   to_c_type      (CType c_obj)            { return c_obj; }
-    static void    release_c_type (CType)                  {}
-    static CType   to_c_type      (const CppType& cpp_obj) { CTypeNonConst c_obj = {cpp_obj.start, 
cpp_obj.end}; return c_obj; }
-    static CppType to_cpp_type    (CType c_obj)            { return CppType (c_obj.start, c_obj.end); }
-  };
-#endif // DOXYGEN_SHOULD_SKIP_THIS
-
   std::vector<PageRange> get_page_ranges() const;
   void set_page_ranges(const std::vector<PageRange>& page_ranges);
 


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