[gtkmm-documentation] Add back use of Gtk::Application so we can finish it.



commit b3103e99d14141893ead01ffd604006dd38e3116
Author: Murray Cumming <murrayc murrayc com>
Date:   Wed Aug 3 09:28:43 2011 +0200

    Add back use of Gtk::Application so we can finish it.
    
    * examples/book/application/: Added.
    * examples/Makefile.am: Mention of these examples.
    * examples/book/base/base.cc: Use Gtk::Application instead of Gtk::Main.

 ChangeLog                                          |    8 +
 examples/Makefile.am                               |    2 +
 .../command_line_handling/exampleapplication.cc    |  143 ++++++++++++++++++++
 .../command_line_handling/exampleapplication.h     |   43 ++++++
 .../command_line_handling/exampleoptiongroup.cc    |   42 ++++++
 .../command_line_handling/exampleoptiongroup.h     |   33 +++++
 .../command_line_handling/examplewindow.cc         |   66 +++++++++
 .../command_line_handling/examplewindow.h          |   38 +++++
 .../book/application/command_line_handling/main.cc |   35 +++++
 .../book/application/simple/exampleapplication.cc  |   89 ++++++++++++
 .../book/application/simple/exampleapplication.h   |   42 ++++++
 examples/book/application/simple/examplewindow.cc  |   66 +++++++++
 examples/book/application/simple/examplewindow.h   |   38 +++++
 examples/book/application/simple/main.cc           |   35 +++++
 examples/book/base/base.cc                         |    8 +-
 15 files changed, 684 insertions(+), 4 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 00c0145..b45afcb 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2011-03-24  Murray Cumming  <murrayc murrayc com>
+
+  Add back use of Gtk::Application so we can finish it.
+  
+	* examples/book/application/: Added.
+	* examples/Makefile.am: Mention of these examples.
+	* examples/book/base/base.cc: Use Gtk::Application instead of Gtk::Main.
+	
 2011-07-19  Murray Cumming  <murrayc murrayc com>
 
 	Fix the build with --enable-warnings=fatal.
diff --git a/examples/Makefile.am b/examples/Makefile.am
index 02cde7e..87ed7cf 100644
--- a/examples/Makefile.am
+++ b/examples/Makefile.am
@@ -8,6 +8,8 @@ LDADD       = $(GTKMM_LIBS)
 
 check_PROGRAMS =					\
 	book/alignment/example				\
+	book/application/simple/example \
+	book/application/command_line_handling/example \
 	book/aspectframe/example			\
 	book/assistant/example				\
 	book/base/base					\
diff --git a/examples/book/application/command_line_handling/exampleapplication.cc b/examples/book/application/command_line_handling/exampleapplication.cc
new file mode 100644
index 0000000..49cfa93
--- /dev/null
+++ b/examples/book/application/command_line_handling/exampleapplication.cc
@@ -0,0 +1,143 @@
+/* gtkmm example Copyright (C) 2002 gtkmm development team
+ *
+ * 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, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#include "exampleapplication.h"
+#include "examplewindow.h"
+#include "exampleoptiongroup.h"
+#include <iostream>
+
+ExampleApplication::ExampleApplication()
+: Gtk::Application("org.gtkmm.examples.application",
+    Gio::ApplicationFlags(Gio::APPLICATION_HANDLES_OPEN | Gio::APPLICATION_HANDLES_COMMAND_LINE))
+{
+}
+
+Glib::RefPtr<ExampleApplication> ExampleApplication::create()
+{
+  return Glib::RefPtr<ExampleApplication>( new ExampleApplication() );
+}
+
+void ExampleApplication::create_window(const Glib::RefPtr<Gio::File>& file)
+{
+  ExampleWindow* window = new ExampleWindow();
+
+  //Make sure that the application runs for as long this window is still open:
+  add_window(*window);
+
+  //Delete the window when it is hidden.
+  //That's enough for this simple example.
+  window->signal_hide().connect(sigc::bind<Gtk::Window*>(sigc::mem_fun(*this,
+    &ExampleApplication::on_window_hide), window));
+
+  window->show();
+
+  if(!file)
+  {
+    //This is probably an new empty file, as a result of an activation rather
+    //than an open.
+    return;
+  }
+
+  const bool loaded = window->load_file(file);
+  if(!loaded)
+    std::cerr << "This file could not be loaded: " << file->get_path() << std::endl;
+}
+
+void ExampleApplication::on_window_hide(Gtk::Window* window)
+{
+  delete window;
+}
+
+void ExampleApplication::on_activate()
+{
+  //std::cout << "debug1: " << G_STRFUNC << std::endl;
+  // The application has been started, so let's show a window.
+  // A real application might want to reuse this "empty" window in on_open(),
+  // when asked to open a file, if no changes have been made yet.
+  create_window();
+}
+
+void ExampleApplication::on_open(const Gio::Application::type_vec_files& files,
+  const Glib::ustring& hint)
+{
+  // The application has been asked to open some files,
+  // so let's open a new window for each one.
+  //std::cout << "debug: files.size()=" << files.size() << std::endl;
+  for(guint i = 0; i < files.size(); i++)
+  {
+    Glib::RefPtr<Gio::File> file = files[0];
+    if(!file)
+    {
+      std::cerr << G_STRFUNC << ": file is null." << std::endl;
+    }
+    else
+      create_window(file);
+  }
+
+  Application::on_open(files, hint);
+}
+
+int ExampleApplication::on_command_line(const Glib::RefPtr<Gio::ApplicationCommandLine>& command_line)
+{
+  //Parse command-line arguments that were passed either to the main (first) instance
+  //or to subsequent instances.
+  //Note that this parsing is happening in the main (not remote) instance.
+  int argc = 0;
+  char** argv =	command_line->get_arguments(argc);
+
+  Glib::OptionContext context;
+  ExampleOptionGroup group;
+  context.set_main_group(group);
+
+  try
+  {
+    context.parse(argc, argv);
+  }
+  catch(const Glib::Error& ex)
+  {
+    std::cerr << "Exception parsing command-line: " << ex.what() << std::endl;
+    std::cerr << context.get_help() << std::endl;
+    return EXIT_FAILURE;
+  }
+
+  // The GOption documentation says that options without names will be returned
+  // to the application as "rest arguments", meaning they will be left in the argv.
+  std::string filepath;
+  if(argc > 1)
+  {
+    const char* pch = argv[1];
+    if(pch)
+      filepath = pch;
+  }
+
+  if(filepath.empty())
+  {
+    //Open a new "document" instead:
+    activate();
+    return EXIT_FAILURE;
+  }
+
+  std::cout << "debug: parsed values: " << std::endl <<
+    "  foo = " << group.m_arg_foo << std::endl <<
+    "  goo = " << group.m_arg_goo << std::endl <<
+    "  filepath = " << filepath << std::endl;
+
+  Glib::RefPtr<Gio::File> file = Gio::File::create_for_path(filepath);
+  open(file);
+
+  //The local instance will eventually exit with this status code:
+  return EXIT_SUCCESS;
+}
diff --git a/examples/book/application/command_line_handling/exampleapplication.h b/examples/book/application/command_line_handling/exampleapplication.h
new file mode 100644
index 0000000..c968e02
--- /dev/null
+++ b/examples/book/application/command_line_handling/exampleapplication.h
@@ -0,0 +1,43 @@
+/* gtkmm example Copyright (C) 2002 gtkmm development team
+ *
+ * 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, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#ifndef GTKMM_EXAMPLEAPPLICATION_H
+#define GTKMM_EXAMPLEAPPLICATION_H
+
+#include <gtkmm.h>
+
+class ExampleApplication: public Gtk::Application
+{
+protected:
+  ExampleApplication();
+
+public:
+  static Glib::RefPtr<ExampleApplication> create();
+    
+protected:
+  //Overrides of default signal handlers:
+  virtual void on_activate();
+  virtual void on_open(const Gio::Application::type_vec_files& files,
+    const Glib::ustring& hint);
+  virtual int on_command_line(const Glib::RefPtr<Gio::ApplicationCommandLine>& command_line);
+
+private:
+  void create_window(const Glib::RefPtr<Gio::File>& file = Glib::RefPtr<Gio::File>());
+
+  void on_window_hide(Gtk::Window* window);
+};
+
+#endif /* GTKMM_EXAMPLEAPPLICATION_H */
diff --git a/examples/book/application/command_line_handling/exampleoptiongroup.cc b/examples/book/application/command_line_handling/exampleoptiongroup.cc
new file mode 100644
index 0000000..a3e1fdc
--- /dev/null
+++ b/examples/book/application/command_line_handling/exampleoptiongroup.cc
@@ -0,0 +1,42 @@
+/* gtkmm example Copyright (C) 2011 gtkmm development team
+ *
+ * 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, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#include "exampleoptiongroup.h"
+#include <iostream>
+
+ExampleOptionGroup::ExampleOptionGroup()
+: Glib::OptionGroup("example_group", "description of example group", "help description of example group"),
+  m_arg_foo(0)
+{
+  //These are just two pointless command-line arguments to show the use 
+  //of the API:
+  Glib::OptionEntry entry1;
+  entry1.set_long_name("foo");
+  entry1.set_short_name('f');
+  entry1.set_description("Enable foo.");
+  add_entry(entry1, m_arg_foo);
+      
+  Glib::OptionEntry entry2;
+  entry2.set_long_name("goo");
+  entry2.set_short_name('g');
+  entry2.set_description("The name of goo to use.");
+  add_entry_filename(entry2, m_arg_goo);
+}
+
+void ExampleOptionGroup::on_error(Glib::OptionContext& /* context */, Glib::OptionGroup& /* group */)
+{
+  std::cout << "on_error called" << std::endl;
+}
diff --git a/examples/book/application/command_line_handling/exampleoptiongroup.h b/examples/book/application/command_line_handling/exampleoptiongroup.h
new file mode 100644
index 0000000..d275ad4
--- /dev/null
+++ b/examples/book/application/command_line_handling/exampleoptiongroup.h
@@ -0,0 +1,33 @@
+/* gtkmm example Copyright (C) 2011 gtkmm development team
+ *
+ * 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, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#ifndef GTKMM_EXAMPLEOPTIONGROUP_H
+#define GTKMM_EXAMPLEOPTIONGROUP_H
+
+#include <gtkmm.h>
+
+class ExampleOptionGroup : public Glib::OptionGroup
+{ 
+public:
+  ExampleOptionGroup();
+
+  virtual void on_error(Glib::OptionContext& context, Glib::OptionGroup& group);
+
+  bool m_arg_foo;
+  std::string m_arg_goo;
+};
+
+#endif /* GTKMM_EXAMPLEOPTIONGROUP_H */
diff --git a/examples/book/application/command_line_handling/examplewindow.cc b/examples/book/application/command_line_handling/examplewindow.cc
new file mode 100644
index 0000000..3434d5d
--- /dev/null
+++ b/examples/book/application/command_line_handling/examplewindow.cc
@@ -0,0 +1,66 @@
+/* gtkmm example Copyright (C) 2002 gtkmm development team
+ *
+ * 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, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#include <iostream>
+
+#include "examplewindow.h"
+
+ExampleWindow::ExampleWindow()
+{
+  set_title("Gio::Application example");
+
+  add(m_scrolledwindow);
+  m_scrolledwindow.add(m_view);
+}
+
+bool ExampleWindow::load_file(const Glib::RefPtr<Gio::File>& file)
+{    
+  if(!file)
+    return false;
+
+  try
+  {
+    char* contents = 0;
+    gsize length = 0;
+
+    if(file->load_contents(contents, length))
+    {
+      if(contents && length)
+      {
+        const Glib::ustring text(contents);
+        Glib::RefPtr<Gtk::TextBuffer> buffer = m_view.get_buffer();
+        buffer->set_text(text);
+      }
+      g_free(contents);
+    }
+  }
+  catch (const Glib::Error& ex)
+  {
+    std::cerr << G_STRFUNC << ": exception while opening file: " << file->get_uri() <<  std::endl <<
+      "  exception: " << ex.what() << std::endl;
+    
+    //Tell the application that this window can no longer be useful to 
+    //this application, so it can forget about it. The instance might then exit 
+    //if this is its last open window.
+    //Note that we must be careful that the caller only calls this method _after_ 
+    //calling show(), or this would be useless: 
+    hide();
+    return false;
+  }
+
+  show_all_children();
+  return true;
+}
diff --git a/examples/book/application/command_line_handling/examplewindow.h b/examples/book/application/command_line_handling/examplewindow.h
new file mode 100644
index 0000000..80fc00f
--- /dev/null
+++ b/examples/book/application/command_line_handling/examplewindow.h
@@ -0,0 +1,38 @@
+/* gtkmm example Copyright (C) 2002 gtkmm development team
+ *
+ * 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, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#ifndef GTKMM_EXAMPLEWINDOW_H_
+#define GTKMM_EXAMPLEWINDOW_H_
+
+#include <gtkmm.h>
+
+class ExampleWindow: public Gtk::Window
+{
+public:
+
+  //We do not take the file as a constructor parameter,
+  //so we can separate window creation and hiding of the window
+  //when loading fails.
+  explicit ExampleWindow();
+  
+  bool load_file(const Glib::RefPtr<Gio::File>& file);
+
+private:
+  Gtk::ScrolledWindow m_scrolledwindow;
+  Gtk::TextView m_view;
+};
+
+#endif /* GTKMM_EXAMPLEWINDOW_H */
diff --git a/examples/book/application/command_line_handling/main.cc b/examples/book/application/command_line_handling/main.cc
new file mode 100644
index 0000000..05fdad7
--- /dev/null
+++ b/examples/book/application/command_line_handling/main.cc
@@ -0,0 +1,35 @@
+/* gtkmm example Copyright (C) 2010 gtkmm development team
+ *
+ * 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, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#include <gtkmm.h>
+#include <iostream>
+
+#include "exampleapplication.h"
+
+int main(int argc, char *argv[])
+{
+  Gtk::Main kit(argc, argv); //TODO: Make this unnecessary: Put it in Gtk::Application.
+
+  Glib::RefPtr<ExampleApplication> application = 
+    ExampleApplication::create();
+
+  // Start the application, showing the initial window, 
+  // and opening extra windows for any files that it is asked to open,
+  // for instance as a command-line parameter.
+  // run() will return when the last window has been closed by the user.
+  const int status = application->run(argc, argv);
+  return status;
+}
diff --git a/examples/book/application/simple/exampleapplication.cc b/examples/book/application/simple/exampleapplication.cc
new file mode 100644
index 0000000..245b635
--- /dev/null
+++ b/examples/book/application/simple/exampleapplication.cc
@@ -0,0 +1,89 @@
+/* gtkmm example Copyright (C) 2002 gtkmm development team
+ *
+ * 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, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#include "exampleapplication.h"
+#include "examplewindow.h"
+#include <iostream>
+
+ExampleApplication::ExampleApplication()
+: Gtk::Application("org.gtkmm.examples.application", Gio::APPLICATION_HANDLES_OPEN)
+{
+}
+
+Glib::RefPtr<ExampleApplication> ExampleApplication::create()
+{
+  return Glib::RefPtr<ExampleApplication>( new ExampleApplication() );
+}
+
+void ExampleApplication::create_window(const Glib::RefPtr<Gio::File>& file)
+{
+  ExampleWindow* window = new ExampleWindow();
+
+  //Make sure that the application runs for as long this window is still open:
+  add_window(*window);
+
+  //Delete the window when it is hidden.
+  //That's enough for this simple example.
+  window->signal_hide().connect(sigc::bind<Gtk::Window*>(sigc::mem_fun(*this,
+    &ExampleApplication::on_window_hide), window));
+
+  window->show();
+
+  if(!file)
+  {
+    //This is probably an new empty file, as a result of an activation rather
+    //than an open.
+    return;
+  }
+
+  const bool loaded = window->load_file(file);
+  if(!loaded)
+    std::cerr << "This file could not be loaded: " << file->get_path() << std::endl;
+}
+
+void ExampleApplication::on_window_hide(Gtk::Window* window)
+{
+  delete window;
+}
+
+void ExampleApplication::on_activate()
+{
+  //std::cout << "debug1: " << G_STRFUNC << std::endl;
+  // The application has been started, so let's show a window.
+  // A real application might want to reuse this "empty" window in on_open(),
+  // when asked to open a file, if no changes have been made yet.
+  create_window();
+}
+
+void ExampleApplication::on_open(const Gio::Application::type_vec_files& files,
+  const Glib::ustring& hint)
+{
+  // The application has been asked to open some files,
+  // so let's open a new window for each one.
+  //std::cout << "debug: files.size()=" << files.size() << std::endl;
+  for(guint i = 0; i < files.size(); i++)
+  {
+    Glib::RefPtr<Gio::File> file = files[0];
+    if(!file)
+    {
+      std::cerr << G_STRFUNC << ": file is null." << std::endl;
+    }
+    else
+      create_window(file);
+  }
+
+  Application::on_open(files, hint);
+}
diff --git a/examples/book/application/simple/exampleapplication.h b/examples/book/application/simple/exampleapplication.h
new file mode 100644
index 0000000..f8e1b0d
--- /dev/null
+++ b/examples/book/application/simple/exampleapplication.h
@@ -0,0 +1,42 @@
+/* gtkmm example Copyright (C) 2002 gtkmm development team
+ *
+ * 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, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#ifndef GTKMM_EXAMPLEAPPLICATION_H
+#define GTKMM_EXAMPLEAPPLICATION_H
+
+#include <gtkmm.h>
+
+class ExampleApplication: public Gtk::Application
+{
+protected:
+  ExampleApplication();
+
+public:
+  static Glib::RefPtr<ExampleApplication> create();
+    
+protected:
+  //Overrides of default signal handlers:
+  virtual void on_activate();
+  virtual void on_open(const Gio::Application::type_vec_files& files,
+    const Glib::ustring& hint);
+
+private:
+  void create_window(const Glib::RefPtr<Gio::File>& file = Glib::RefPtr<Gio::File>());
+
+  void on_window_hide(Gtk::Window* window);
+};
+
+#endif /* GTKMM_EXAMPLEAPPLICATION_H */
diff --git a/examples/book/application/simple/examplewindow.cc b/examples/book/application/simple/examplewindow.cc
new file mode 100644
index 0000000..3434d5d
--- /dev/null
+++ b/examples/book/application/simple/examplewindow.cc
@@ -0,0 +1,66 @@
+/* gtkmm example Copyright (C) 2002 gtkmm development team
+ *
+ * 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, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#include <iostream>
+
+#include "examplewindow.h"
+
+ExampleWindow::ExampleWindow()
+{
+  set_title("Gio::Application example");
+
+  add(m_scrolledwindow);
+  m_scrolledwindow.add(m_view);
+}
+
+bool ExampleWindow::load_file(const Glib::RefPtr<Gio::File>& file)
+{    
+  if(!file)
+    return false;
+
+  try
+  {
+    char* contents = 0;
+    gsize length = 0;
+
+    if(file->load_contents(contents, length))
+    {
+      if(contents && length)
+      {
+        const Glib::ustring text(contents);
+        Glib::RefPtr<Gtk::TextBuffer> buffer = m_view.get_buffer();
+        buffer->set_text(text);
+      }
+      g_free(contents);
+    }
+  }
+  catch (const Glib::Error& ex)
+  {
+    std::cerr << G_STRFUNC << ": exception while opening file: " << file->get_uri() <<  std::endl <<
+      "  exception: " << ex.what() << std::endl;
+    
+    //Tell the application that this window can no longer be useful to 
+    //this application, so it can forget about it. The instance might then exit 
+    //if this is its last open window.
+    //Note that we must be careful that the caller only calls this method _after_ 
+    //calling show(), or this would be useless: 
+    hide();
+    return false;
+  }
+
+  show_all_children();
+  return true;
+}
diff --git a/examples/book/application/simple/examplewindow.h b/examples/book/application/simple/examplewindow.h
new file mode 100644
index 0000000..80fc00f
--- /dev/null
+++ b/examples/book/application/simple/examplewindow.h
@@ -0,0 +1,38 @@
+/* gtkmm example Copyright (C) 2002 gtkmm development team
+ *
+ * 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, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#ifndef GTKMM_EXAMPLEWINDOW_H_
+#define GTKMM_EXAMPLEWINDOW_H_
+
+#include <gtkmm.h>
+
+class ExampleWindow: public Gtk::Window
+{
+public:
+
+  //We do not take the file as a constructor parameter,
+  //so we can separate window creation and hiding of the window
+  //when loading fails.
+  explicit ExampleWindow();
+  
+  bool load_file(const Glib::RefPtr<Gio::File>& file);
+
+private:
+  Gtk::ScrolledWindow m_scrolledwindow;
+  Gtk::TextView m_view;
+};
+
+#endif /* GTKMM_EXAMPLEWINDOW_H */
diff --git a/examples/book/application/simple/main.cc b/examples/book/application/simple/main.cc
new file mode 100644
index 0000000..05fdad7
--- /dev/null
+++ b/examples/book/application/simple/main.cc
@@ -0,0 +1,35 @@
+/* gtkmm example Copyright (C) 2010 gtkmm development team
+ *
+ * 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, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#include <gtkmm.h>
+#include <iostream>
+
+#include "exampleapplication.h"
+
+int main(int argc, char *argv[])
+{
+  Gtk::Main kit(argc, argv); //TODO: Make this unnecessary: Put it in Gtk::Application.
+
+  Glib::RefPtr<ExampleApplication> application = 
+    ExampleApplication::create();
+
+  // Start the application, showing the initial window, 
+  // and opening extra windows for any files that it is asked to open,
+  // for instance as a command-line parameter.
+  // run() will return when the last window has been closed by the user.
+  const int status = application->run(argc, argv);
+  return status;
+}
diff --git a/examples/book/base/base.cc b/examples/book/base/base.cc
index 36a6673..676bde2 100644
--- a/examples/book/base/base.cc
+++ b/examples/book/base/base.cc
@@ -2,11 +2,11 @@
 
 int main(int argc, char *argv[])
 {
-  Gtk::Main kit(argc, argv);
+  Glib::RefPtr<Gtk::Application> app =
+    Gtk::Application::create(argc, argv,
+      "org.gtkmm.examples.base");
 
   Gtk::Window window;
 
-  Gtk::Main::run(window);
-
-  return EXIT_SUCCESS;
+  return app->run(window);
 }



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