[gnome-devel-docs] Make the C++ examples less awful.



commit 725a4fd0fa9ec96db398b068c67bf7e7b6aa33ca
Author: Murray Cumming <murrayc murrayc com>
Date:   Thu Mar 1 14:24:32 2012 +0100

    Make the C++ examples less awful.
    
    Note that the image-viewer.cpp souce code is duplicated in
    the .page file, which is very unwise.
    
    Bug #647651

 platform-demos/C/guitar-tuner/guitar-tuner.cc |   14 ++----
 platform-demos/C/image-viewer.cpp.page        |   59 ++++++++++++-------------
 platform-demos/C/image-viewer/image-viewer.cc |   45 ++++++++----------
 3 files changed, 52 insertions(+), 66 deletions(-)
---
diff --git a/platform-demos/C/guitar-tuner/guitar-tuner.cc b/platform-demos/C/guitar-tuner/guitar-tuner.cc
index 42b07c9..e59fe27 100644
--- a/platform-demos/C/guitar-tuner/guitar-tuner.cc
+++ b/platform-demos/C/guitar-tuner/guitar-tuner.cc
@@ -1,12 +1,8 @@
 /* -*- Mode: C++; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
 
 #include <gtkmm.h>
-#include <iostream>
 #include <gstreamermm.h>
-
-#ifdef ENABLE_NLS
-#  include <libintl.h>
-#endif
+#include <iostream>
 
 /* For testing purposes, use the local (not installed) ui file */
 /* #define UI_FILE PACKAGE_DATA_DIR"/guitar_tuner_cpp/ui/guitar_tuner_cpp.ui" */
@@ -81,7 +77,7 @@ main(int argc, char *argv[])
 	builder->get_widget("main_window", main_win);
 
 	Sound sound;
-	Gtk::Button* button;
+	Gtk::Button* button = 0;
 	
 	builder->get_widget("button_E", button);
 	button->signal_clicked().connect (sigc::bind<double, Sound*>(sigc::ptr_fun(&on_button_clicked),
@@ -102,9 +98,7 @@ main(int argc, char *argv[])
 	button->signal_clicked().connect (sigc::bind<double, Sound*>(sigc::ptr_fun(&on_button_clicked),
 	                                              1318.5, &sound));
 	
-	if (main_win)
-	{
-		kit.run(*main_win);
-	}
+	kit.run(*main_win);
+
 	return 0;
 }
diff --git a/platform-demos/C/image-viewer.cpp.page b/platform-demos/C/image-viewer.cpp.page
index 09fd55e..3972d81 100644
--- a/platform-demos/C/image-viewer.cpp.page
+++ b/platform-demos/C/image-viewer.cpp.page
@@ -88,29 +88,25 @@ main (int argc, char *argv[])
 {
 	Gtk::Main kit(argc, argv);
 
-	Gtk::Window* main_win = new Gtk::Window(Gtk::WINDOW_TOPLEVEL);
-	main_win->set_title ("image-viewer-cpp");
+	Gtk::Window main_win;
+	main_win.set_title ("image-viewer-cpp");
 
-	Gtk::Box* box = new Gtk::Box();
+	Gtk::Box* box = Gtk::manage(new Gtk::Box());
 	box->set_orientation (Gtk::ORIENTATION_VERTICAL);
-	box->set_spacing(5);
+	box->set_spacing(6);
+	main_win.add(*box);
 
-	Gtk::Image* image = new Gtk::Image();
+	image = Gtk::manage(new Gtk::Image());
 	box->pack_start (*image, true, true);
 	
-	Gtk::Button* button = new Gtk::Button("Open Imageâ");
+	Gtk::Button* button = Gtk::manage(new Gtk::Button("Open Imageâ"));
+	button->signal_clicked().connect (
+		sigc::ptr_fun(&on_open_image));
 	box->pack_start (*button, false, false);
 
-	main_win->add(*box);
+	main_win.show_all_children();
+	kit.run(main_win);
 
-	button->signal_clicked().connect (sigc::bind<Gtk::Image*>(sigc::ptr_fun(&on_open_image), image));
-
-	main_win->show_all();
-	
-	if (main_win)
-	{
-		kit.run(*main_win);
-	}
 	return 0;
 }
 ]]></code>
@@ -126,13 +122,12 @@ main (int argc, char *argv[])
     </item>
     <item>
     <p>We need to define what happens when the user clicks on the button. GTKmm uses the concept of <em>signals</em>. When the button is clicked, it fires the <em>clicked</em> signal, which we can connect to some action. This is done using the <code>signal_clicked().connect</code>
-    method which tells GTKmm to call the <code>on_open_image</code> function when the button is clicked and
-    to pass the image as an additional argument to that function. We will define the <em>callback</em> in the next section.</p>
+    method which tells GTKmm to call the <code>on_open_image</code> function when the button is clicked. We will define the <em>callback</em> in the next section.</p>
     </item>
     <item>
     <p>The last step is to show all widgets in the window using
-    <code>show_all()</code>. This is equivalent to using the <code>show()</code>
-    method on all our widgets.</p>
+    <code>show_all_children()</code>. This is equivalent to using the <code>show()</code>
+    method on all our child widgets.</p>
     </item>
   </steps>
 </section>
@@ -143,8 +138,10 @@ main (int argc, char *argv[])
 button we mentioned before. Add this code before the <code>main</code>
 method.</p>
 <code mime="text/x-csrc"><![CDATA[
+Gtk::Image* image = 0;
+
 static void
-on_open_image (Gtk::Image* image)
+on_open_image ()
 {
 	Gtk::FileChooserDialog dialog("Open image",
 	                              Gtk::FILE_CHOOSER_ACTION_OPEN);
@@ -156,9 +153,13 @@ on_open_image (Gtk::Image* image)
 	Glib::RefPtr<Gtk::FileFilter> filter =
 		Gtk::FileFilter::create();
 	filter->add_pixbuf_formats();
+	filter->set_name("Images");
 	dialog.add_filter (filter);
 
-	switch (dialog.run())
+	const int response = dialog.run();
+	dialog.hide();
+
+	switch (response)
 	{
 		case Gtk::RESPONSE_ACCEPT:
 			image->set(dialog.get_filename());
@@ -166,16 +167,12 @@ on_open_image (Gtk::Image* image)
 		default:
 			break;
 	}
-	dialog.hide();
 }
 ]]></code>
   <p>This is a bit more complicated than anything we've attempted so far, so let's break it down:</p>
   <list>
-    <item><p>The argument to the signal is the image object that we passed while connecting the signal. Sometimes there are other arguments related to the signal, but <em>clicked</em> doesn't have any. </p>    </item> 
-    <item>
-      <p>The next interesting line is where the dialog for choosing the file is created using
-      <code>Gtk::FileChooserDialog</code>. The function takes the title of the dialog and
-      the type of dialog this should be. In our case, it is an <em>Open</em> dialog.</p>
+      <p>The dialog for choosing the file is created using the
+      <code>Gtk::FileChooserDialog</code> constructor. This takes the title and type of the dialog. In our case, it is an <em>Open</em> dialog.</p>
     </item>
     <item>
     <p>The next two lines add an <em>Open</em> and a <em>Close</em> button to the dialog.</p>
@@ -192,13 +189,13 @@ on_open_image (Gtk::Image* image)
     <item>
     <p><code>dialog.run</code> displays the <gui>Open</gui> dialog. The dialog will wait for the user to choose an image; when they do, <code>dialog.run</code> will return the value <code>Gtk::RESPONSE_ACCEPT</code> (it would return <code>Gtk::RESPONSE_CANCEL</code> if the user clicked <gui>Cancel</gui>). The <code>switch</code> statement tests for this.</p>
     </item>
+    <item>
+    <p>We hide the <gui>Open</gui> dialog because we don't need it any more. The dialog would be hidden later anyway, as it is only a local variable and is
+    destroyed (and therefore hidden) when the scope ends.</p>
+    </item>
     <item><p>Assuming that the user did click <gui>Open</gui>, the next line loads the
     file into the <code>Gtk::Image</code> so that it is displayed.</p>
     </item>
-    <item>
-    <p>In the final line of this method, we hide the <gui>Open</gui> dialog because we don't need it any more. The dialog would be hidden anyway, as it is only a local variable and is
-    destroyed (and as such hidden) when the scope ends.</p>
-    </item>
   </list>
 </section>
 
diff --git a/platform-demos/C/image-viewer/image-viewer.cc b/platform-demos/C/image-viewer/image-viewer.cc
index 271dfb7..68251b3 100644
--- a/platform-demos/C/image-viewer/image-viewer.cc
+++ b/platform-demos/C/image-viewer/image-viewer.cc
@@ -3,16 +3,10 @@
 #include <gtkmm.h>
 #include <iostream>
 
-#include "config.h"
-
-
-#ifdef ENABLE_NLS
-#  include <libintl.h>
-#endif
-
+Gtk::Image* image = 0;
 
 static void
-on_open_image (Gtk::Image* image)
+on_open_image ()
 {
 	Gtk::FileChooserDialog dialog("Open image",
 	                              Gtk::FILE_CHOOSER_ACTION_OPEN);
@@ -24,9 +18,13 @@ on_open_image (Gtk::Image* image)
 	Glib::RefPtr<Gtk::FileFilter> filter =
 		Gtk::FileFilter::create();
 	filter->add_pixbuf_formats();
+	filter->set_name("Images");
 	dialog.add_filter (filter);
 
-	switch (dialog.run())
+	const int response = dialog.run();
+	dialog.hide();
+
+	switch (response)
 	{
 		case Gtk::RESPONSE_ACCEPT:
 			image->set(dialog.get_filename());
@@ -34,7 +32,6 @@ on_open_image (Gtk::Image* image)
 		default:
 			break;
 	}
-	dialog.hide();
 }
 
 int
@@ -42,26 +39,24 @@ main (int argc, char *argv[])
 {
 	Gtk::Main kit(argc, argv);
 
-	Gtk::Window* main_win = new Gtk::Window(Gtk::WINDOW_TOPLEVEL);
-	main_win->set_title ("image-viewer-cpp");
+	Gtk::Window main_win;
+	main_win.set_title ("image-viewer-cpp");
 
-	Gtk::Box* box = new Gtk::Box();
+	Gtk::Box* box = Gtk::manage(new Gtk::Box());
 	box->set_orientation (Gtk::ORIENTATION_VERTICAL);
-	box->set_spacing(5);
-	main_win->add(*box);
+	box->set_spacing(6);
+	main_win.add(*box);
 
-	Gtk::Image* image = new Gtk::Image();
+	image = Gtk::manage(new Gtk::Image());
 	box->pack_start (*image, true, true);
 	
-	Gtk::Button* button = new Gtk::Button("Open Imageâ");
-	button->signal_clicked().connect (sigc::bind<Gtk::Image*>(sigc::ptr_fun(&on_open_image), image));
+	Gtk::Button* button = Gtk::manage(new Gtk::Button("Open Imageâ"));
+	button->signal_clicked().connect (
+		sigc::ptr_fun(&on_open_image));
 	box->pack_start (*button, false, false);
-	
-	main_win->show_all();
-	
-	if (main_win)
-	{
-		kit.run(*main_win);
-	}
+
+	main_win.show_all_children();
+	kit.run(main_win);
+
 	return 0;
 }



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