[gtkmm-documentation] Improve the Assistant example



commit 58a40bf203a1d745f094535a57d3a7415b3873c3
Author: David King <davidk openismus com>
Date:   Fri May 7 14:31:03 2010 +0200

    Improve the Assistant example
    
    * examples/book/assistant/*: Move examplewindow.[cc|h] to
    exampleassistant.[cc|h]]. Add another window which opens the assistant
    and receives the result from it.

 ChangeLog                                   |    8 ++
 examples/Makefile.am                        |    2 +
 examples/book/assistant/exampleassistant.cc |  111 +++++++++++++++++++++++++++
 examples/book/assistant/exampleassistant.h  |   48 ++++++++++++
 examples/book/assistant/examplewindow.cc    |   87 ++++++---------------
 examples/book/assistant/examplewindow.h     |   15 ++--
 6 files changed, 200 insertions(+), 71 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 19ab7a2..fde4aef 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,13 @@
 2010-05-07  David King  <davidk openismus com>
 
+	Improve the Assistant example
+
+	* examples/book/assistant/*: Move examplewindow.[cc|h] to
+	exampleassistant.[cc|h]]. Add another window which opens the assistant
+	and receives the result from it.
+
+2010-05-07  David King  <davidk openismus com>
+
 	Recommend non-deprecated Tooltip API in Tooltips chapter
 
 	* docs/tutorial/C/gtkmm-tutorial-in.xml: Remove text that recommended
diff --git a/examples/Makefile.am b/examples/Makefile.am
index 88af17b..7be786a 100644
--- a/examples/Makefile.am
+++ b/examples/Makefile.am
@@ -140,6 +140,8 @@ book_aspectframe_example_SOURCES =		\
 	book/aspectframe/main.cc
 
 book_assistant_example_SOURCES =		\
+	book/assistant/exampleassistant.cc	\
+	book/assistant/exampleassistant.h	\
 	book/assistant/examplewindow.cc		\
 	book/assistant/examplewindow.h		\
 	book/assistant/main.cc
diff --git a/examples/book/assistant/exampleassistant.cc b/examples/book/assistant/exampleassistant.cc
new file mode 100644
index 0000000..557ba58
--- /dev/null
+++ b/examples/book/assistant/exampleassistant.cc
@@ -0,0 +1,111 @@
+/* gtkmm example Copyright (C) 2010 Openismus GmbH
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2
+ * as published by the Free Software Foundation.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include <iostream>
+#include "exampleassistant.h"
+
+ExampleAssistant::ExampleAssistant()
+: m_box(false, 12),
+  m_label1("Type text to allow the assistant to continue:"),
+  m_label2("Confirmation page"),
+  m_check("Optional extra information")
+{
+  set_title("Gtk::Assistant example");
+  set_border_width(12);
+  set_default_size(400, 300);
+
+  m_box.pack_start(m_label1);
+  m_box.pack_start(m_entry);
+
+  append_page(m_box);
+  append_page(m_check);
+  append_page(m_label2);
+
+  set_page_title(*get_nth_page(0), "Page 1");
+  set_page_title(*get_nth_page(1), "Page 2");
+  set_page_title(*get_nth_page(2), "Confirmation");
+
+  set_page_complete(m_check, true);
+  set_page_complete(m_label2, true);
+
+  set_page_type(m_box, Gtk::ASSISTANT_PAGE_INTRO);
+  set_page_type(m_label2, Gtk::ASSISTANT_PAGE_CONFIRM);
+
+  signal_apply().connect(sigc::mem_fun(*this,
+    &ExampleAssistant::on_assistant_apply));
+  signal_cancel().connect(sigc::mem_fun(*this,
+    &ExampleAssistant::on_assistant_cancel));
+  signal_close().connect(sigc::mem_fun(*this,
+    &ExampleAssistant::on_assistant_close));
+  signal_prepare().connect(sigc::mem_fun(*this,
+    &ExampleAssistant::on_assistant_prepare));
+
+  m_entry.signal_changed().connect(sigc::mem_fun(*this,
+    &ExampleAssistant::on_entry_changed));
+
+  show_all_children();
+}
+
+ExampleAssistant::~ExampleAssistant()
+{
+}
+
+void ExampleAssistant::get_result(bool& check_state, Glib::ustring& entry_text)
+{
+  check_state = m_check.get_active();
+  entry_text = m_entry.get_text();
+}
+
+void ExampleAssistant::on_assistant_apply()
+{
+  std::cout << "Apply was clicked";
+  print_status();
+}
+
+void ExampleAssistant::on_assistant_cancel()
+{
+  std::cout << "Cancel was clicked";
+  print_status();
+  hide();
+}
+
+void ExampleAssistant::on_assistant_close()
+{
+  std::cout << "Assistant was closed";
+  print_status();
+  hide();
+}
+
+void ExampleAssistant::on_assistant_prepare(Gtk::Widget* /* widget */)
+{
+  set_title(Glib::ustring::compose("Gtk::Assistant example (Page %1 of %2)",
+    get_current_page() + 1, get_n_pages()));
+}
+
+void ExampleAssistant::on_entry_changed()
+{
+  // The page is only complete if the entry contains text.
+  if(m_entry.get_text_length())
+    set_page_complete(m_box, true);
+  else
+    set_page_complete(m_box, false);
+}
+
+void ExampleAssistant::print_status()
+{
+  std::cout << ", entry contents: \"" << m_entry.get_text()
+    << "\", checkbutton status: " << m_check.get_active() << std::endl;
+}
diff --git a/examples/book/assistant/exampleassistant.h b/examples/book/assistant/exampleassistant.h
new file mode 100644
index 0000000..83e95a0
--- /dev/null
+++ b/examples/book/assistant/exampleassistant.h
@@ -0,0 +1,48 @@
+/* gtkmm example Copyright (C) 2010 Openismus GmbH
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2
+ * as published by the Free Software Foundation.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef GTKMM_EXAMPLEASSISTANT_H
+#define GTKMM_EXAMPLEASSISTANT_H
+
+#include <gtkmm.h>
+
+class ExampleAssistant : public Gtk::Assistant
+{
+public:
+  ExampleAssistant();
+  virtual ~ExampleAssistant();
+
+  void get_result(bool& check_state, Glib::ustring& entry_text);
+
+private:
+  // Signal handlers:
+  void on_assistant_apply();
+  void on_assistant_cancel();
+  void on_assistant_close();
+  void on_assistant_prepare(Gtk::Widget* widget);
+  void on_entry_changed();
+
+  // Member functions:
+  void print_status();
+
+  // Child widgets:
+  Gtk::HBox m_box;
+  Gtk::Label m_label1, m_label2;
+  Gtk::CheckButton m_check;
+  Gtk::Entry m_entry;
+};
+
+#endif /* GTKMM_EXAMPLEASSISTANT_H */
diff --git a/examples/book/assistant/examplewindow.cc b/examples/book/assistant/examplewindow.cc
index b1a8cde..f26bdc2 100644
--- a/examples/book/assistant/examplewindow.cc
+++ b/examples/book/assistant/examplewindow.cc
@@ -14,47 +14,35 @@
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
-#include <iostream>
 #include "examplewindow.h"
+#include "exampleassistant.h"
 
 ExampleWindow::ExampleWindow()
-: m_box(false, 12),
-  m_label1("Type text to allow the assistant to continue:"),
-  m_label2("Confirmation page"),
-  m_check("Optional extra information")
+: m_table(3, 2),
+  m_button("Show the assistant"),
+  m_label1("State of assistant checkbutton:"),
+  m_label2("Contents of assistant entry:")
 {
   set_title("Gtk::Assistant example");
   set_border_width(12);
-  set_default_size(400, 300);
 
-  m_box.pack_start(m_label1);
-  m_box.pack_start(m_entry);
+  m_table.attach(m_button, 0, 2, 0, 1, Gtk::FILL, Gtk::EXPAND);
+  m_table.attach(m_label1, 0, 1, 1, 2, Gtk::FILL, Gtk::EXPAND);
+  m_table.attach(m_label2, 0, 1, 2, 3, Gtk::FILL, Gtk::EXPAND);
+  m_table.attach(m_check, 1, 2, 1, 2);
+  m_table.attach(m_entry, 1, 2, 2, 3);
+  add(m_table);
 
-  append_page(m_box);
-  append_page(m_check);
-  append_page(m_label2);
+  m_label1.set_alignment(0.0, 0.5);
+  m_label2.set_alignment(0.0, 0.5);
 
-  set_page_title(*get_nth_page(0), "Page 1");
-  set_page_title(*get_nth_page(1), "Page 2");
-  set_page_title(*get_nth_page(2), "Confirmation");
-
-  set_page_complete(m_check, true);
-  set_page_complete(m_label2, true);
-
-  set_page_type(m_box, Gtk::ASSISTANT_PAGE_INTRO);
-  set_page_type(m_label2, Gtk::ASSISTANT_PAGE_CONFIRM);
-
-  signal_apply().connect(sigc::mem_fun(*this,
+  m_button.signal_clicked().connect(sigc::mem_fun(*this,
+    &ExampleWindow::on_button_clicked));
+  m_assistant.signal_apply().connect(sigc::mem_fun(*this,
     &ExampleWindow::on_assistant_apply));
-  signal_cancel().connect(sigc::mem_fun(*this,
-    &ExampleWindow::on_assistant_cancel));
-  signal_close().connect(sigc::mem_fun(*this,
-    &ExampleWindow::on_assistant_close));
-  signal_prepare().connect(sigc::mem_fun(*this,
-    &ExampleWindow::on_assistant_prepare));
 
-  m_entry.signal_changed().connect(sigc::mem_fun(*this,
-    &ExampleWindow::on_entry_changed));
+  m_check.set_sensitive(false);
+  m_entry.set_sensitive(false);
 
   show_all_children();
 }
@@ -65,40 +53,15 @@ ExampleWindow::~ExampleWindow()
 
 void ExampleWindow::on_assistant_apply()
 {
-  std::cout << "Apply was clicked";
-  print_status();
-}
-
-void ExampleWindow::on_assistant_cancel()
-{
-  std::cout << "Cancel was clicked";
-  print_status();
-}
-
-void ExampleWindow::on_assistant_close()
-{
-  std::cout << "Assistant was closed";
-  print_status();
-}
+  bool check_state;
+  Glib::ustring entry_text;
 
-void ExampleWindow::on_assistant_prepare(Gtk::Widget* /* widget */)
-{
-  set_title(Glib::ustring::compose("Gtk::Assistant example (Page %1 of %2)",
-    get_current_page() + 1, get_n_pages()));
-}
-
-void ExampleWindow::on_entry_changed()
-{
-  // The page is only complete if the entry contains text.
-  if(m_entry.get_text_length())
-    set_page_complete(m_box, true);
-  else
-    set_page_complete(m_box, false);
+  m_assistant.get_result(check_state, entry_text);
+  m_check.set_active(check_state);
+  m_entry.set_text(entry_text);
 }
 
-void ExampleWindow::print_status()
+void ExampleWindow::on_button_clicked()
 {
-  std::cout << ", entry contents: \"" << m_entry.get_text()
-    << "\", checkbutton status: " << m_check.get_active() << std::endl;
-  hide();
+  m_assistant.show();
 }
diff --git a/examples/book/assistant/examplewindow.h b/examples/book/assistant/examplewindow.h
index 4b23c3b..7724221 100644
--- a/examples/book/assistant/examplewindow.h
+++ b/examples/book/assistant/examplewindow.h
@@ -17,9 +17,10 @@
 #ifndef GTKMM_EXAMPLEWINDOW_H
 #define GTKMM_EXAMPLEWINDOW_H
 
+#include "exampleassistant.h"
 #include <gtkmm.h>
 
-class ExampleWindow : public Gtk::Assistant
+class ExampleWindow : public Gtk::Window
 {
 public:
   ExampleWindow();
@@ -27,20 +28,16 @@ public:
 
 private:
   // Signal handlers:
+  void on_button_clicked();
   void on_assistant_apply();
-  void on_assistant_cancel();
-  void on_assistant_close();
-  void on_assistant_prepare(Gtk::Widget* widget);
-  void on_entry_changed();
-
-  // Member functions:
-  void print_status();
 
   // Child widgets:
-  Gtk::HBox m_box;
+  Gtk::Table m_table;
+  Gtk::Button m_button;
   Gtk::Label m_label1, m_label2;
   Gtk::CheckButton m_check;
   Gtk::Entry m_entry;
+  ExampleAssistant m_assistant;
 };
 
 #endif /* GTKMM_EXAMPLEWINDOW_H */



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