[gtkmm-documentation] builder/derived example: Add constructor with additional parameter



commit 388de3233637bfd9f77ba73852425b48efd5994c
Author: Kjell Ahlstedt <kjell ahlstedt bredband net>
Date:   Fri Jan 29 12:49:49 2016 +0100

    builder/derived example: Add constructor with additional parameter
    
    * examples/book/builder/derived/deriveddialog.[h|cc]:
    * examples/book/builder/derived/main.cc: Add a constructor with an additional
    parameter to DerivedDialog, and call it from Builder::get_widget_derived()
    with an additional argument. Possible when get_widget_derived() is a
    variadic template. Bug #134161.

 examples/book/builder/derived/deriveddialog.cc |   19 +++++++++++--
 examples/book/builder/derived/deriveddialog.h  |    5 +--
 examples/book/builder/derived/main.cc          |   34 +++++++++++++++++++----
 3 files changed, 46 insertions(+), 12 deletions(-)
---
diff --git a/examples/book/builder/derived/deriveddialog.cc b/examples/book/builder/derived/deriveddialog.cc
index 5f730a3..4a4b0ca 100644
--- a/examples/book/builder/derived/deriveddialog.cc
+++ b/examples/book/builder/derived/deriveddialog.cc
@@ -1,5 +1,3 @@
-//$Id: deriveddialog.cc 588 2004-02-13 17:10:43Z murrayc $ -*- c++ -*-
-
 /* libglademm example Copyright (C) 2003 libglademm development team
  *
  * This program is free software; you can redistribute it and/or modify
@@ -31,11 +29,26 @@ DerivedDialog::DerivedDialog(BaseObjectType* cobject, const Glib::RefPtr<Gtk::Bu
   }
 }
 
+// The first two parameters are mandatory in a constructor that will be called
+// from Gtk::Builder::get_widget_derived().
+// Additional parameters, if any, correspond to additional arguments in the call
+// to Gtk::Builder::get_widget_derived().
+DerivedDialog::DerivedDialog(BaseObjectType* cobject, const Glib::RefPtr<Gtk::Builder>& refGlade,
+  bool is_glad)
+: DerivedDialog(cobject, refGlade) // Delegate to the other constructor
+{
+  // Show an icon.
+  auto pImage = Gtk::manage(new Gtk::Image());
+  pImage->set_from_icon_name(is_glad ? "face-smile" : "face-sad", Gtk::ICON_SIZE_DIALOG);
+  pImage->show_all();
+  get_content_area()->pack_start(*pImage);
+}
+
 DerivedDialog::~DerivedDialog()
 {
 }
 
 void DerivedDialog::on_button_quit()
 {
-  hide(); //hide() will cause main::run() to end.
+  hide(); //hide() will cause Gtk::Application::run() to end.
 }
diff --git a/examples/book/builder/derived/deriveddialog.h b/examples/book/builder/derived/deriveddialog.h
index 8012e7d..252dad3 100644
--- a/examples/book/builder/derived/deriveddialog.h
+++ b/examples/book/builder/derived/deriveddialog.h
@@ -1,5 +1,3 @@
-//$Id: deriveddialog.h 691 2004-06-02 13:33:10Z mxpxpod $ -*- c++ -*-
-
 /* libglademm example Copyright (C) 2003 libglademm development team
  *
  * This program is free software; you can redistribute it and/or modify
@@ -21,11 +19,12 @@
 
 #include <gtkmm.h>
 
-
 class DerivedDialog : public Gtk::Dialog
 {
 public:
   DerivedDialog(BaseObjectType* cobject, const Glib::RefPtr<Gtk::Builder>& refGlade);
+  DerivedDialog(BaseObjectType* cobject, const Glib::RefPtr<Gtk::Builder>& refGlade,
+    bool is_glad);
   virtual ~DerivedDialog();
 
 protected:
diff --git a/examples/book/builder/derived/main.cc b/examples/book/builder/derived/main.cc
index a76ed04..8df3370 100644
--- a/examples/book/builder/derived/main.cc
+++ b/examples/book/builder/derived/main.cc
@@ -1,5 +1,3 @@
-//$Id: main.cc 824 2006-02-22 21:46:32Z murrayc $ -*- c++ -*-
-
 /* libglademm example Copyright (C) 2003 libglademm development team
  *
  * This program is free software; you can redistribute it and/or modify
@@ -18,12 +16,32 @@
 
 #include "deriveddialog.h"
 #include <iostream>
+#include <cstring>
 
 int main (int argc, char **argv)
 {
-  auto app = Gtk::Application::create(argc, argv, "org.gtkmm.example");
+  bool show_icon = false;
+  bool is_glad = true;
+  int argc1 = argc;
+  if (argc > 1)
+  {
+    if (std::strcmp(argv[1], "--glad") == 0)
+    {
+      show_icon = true;
+      is_glad = true;
+      argc1 = 1; // Don't give the command line arguments to Gtk::Application.
+    }
+    else if (std::strcmp(argv[1], "--sad") == 0)
+    {
+      show_icon = true;
+      is_glad = false;
+      argc1 = 1; // Don't give the command line arguments to Gtk::Application.
+    }
+  }
+
+  auto app = Gtk::Application::create(argc1, argv, "org.gtkmm.example");
 
-  //Load the Glade file and instiate its widgets:
+  //Load the Glade file and instantiate its widgets:
   auto refBuilder = Gtk::Builder::create();
   try
   {
@@ -45,9 +63,13 @@ int main (int argc, char **argv)
     return 1;
   }
 
-  //Get the GtkBuilder-instantiated dialog::
+  //Get the GtkBuilder-instantiated dialog:
   DerivedDialog* pDialog = nullptr;
-  refBuilder->get_widget_derived("DialogDerived", pDialog);
+  if (show_icon)
+    // This call to get_widget_derived() requires gtkmm 3.19.7 or higher.
+    refBuilder->get_widget_derived("DialogDerived", pDialog, is_glad);
+  else
+    refBuilder->get_widget_derived("DialogDerived", pDialog);
   if(pDialog)
   {
     //Start:


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