[gtkmm-documentation] Gtk::Application example: open an empty document if none is specified.



commit 0bfe42ed4bc3a9ff1cd1106e4ead7cf3a3dbad5f
Author: Murray Cumming <murrayc murrayc com>
Date:   Wed Mar 9 12:17:03 2011 +0100

    Gtk::Application example: open an empty document if none is specified.
    
    * examples/book/application/exampleapplication.cc: Do not fail if no file
    was specified.

 ChangeLog                                       |    7 ++++
 examples/book/application/exampleapplication.cc |   39 +++++++++++++---------
 2 files changed, 30 insertions(+), 16 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index c24f115..34e8733 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2011-03-09  Murray Cumming  <murrayc murrayc/cp,>
+
+	Gtk::Application example: open an empty document if none is specified.
+
+	* examples/book/application/exampleapplication.cc: Do not fail if no file
+	was specified.
+
 2011-03-09  Murray Cumming  <murrayc murrayc com>
 
 	Gtk::Builder example: Remove a now non-existent property.
diff --git a/examples/book/application/exampleapplication.cc b/examples/book/application/exampleapplication.cc
index 530da7c..49cfa93 100644
--- a/examples/book/application/exampleapplication.cc
+++ b/examples/book/application/exampleapplication.cc
@@ -33,17 +33,24 @@ Glib::RefPtr<ExampleApplication> ExampleApplication::create()
 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;
@@ -56,7 +63,7 @@ void ExampleApplication::on_window_hide(Gtk::Window* window)
 
 void ExampleApplication::on_activate()
 {
-  std::cout << "debug1: " << G_STRFUNC << std::endl;
+  //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.
@@ -68,7 +75,7 @@ void ExampleApplication::on_open(const Gio::Application::type_vec_files& files,
 {
   // 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;
+  //std::cout << "debug: files.size()=" << files.size() << std::endl;
   for(guint i = 0; i < files.size(); i++)
   {
     Glib::RefPtr<Gio::File> file = files[0];
@@ -79,22 +86,22 @@ void ExampleApplication::on_open(const Gio::Application::type_vec_files& files,
     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 
+  //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);
@@ -105,8 +112,8 @@ int ExampleApplication::on_command_line(const Glib::RefPtr<Gio::ApplicationComma
     std::cerr << context.get_help() << std::endl;
     return EXIT_FAILURE;
   }
-  
-  // The GOption documentation says that options without names will be returned 
+
+  // 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)
@@ -115,16 +122,16 @@ int ExampleApplication::on_command_line(const Glib::RefPtr<Gio::ApplicationComma
     if(pch)
       filepath = pch;
   }
-  
+
   if(filepath.empty())
   {
-    std::cerr << "No filepath was provided." << std::endl;
-    std::cerr << context.get_help() << std::endl;
+    //Open a new "document" instead:
+    activate();
     return EXIT_FAILURE;
   }
 
   std::cout << "debug: parsed values: " << std::endl <<
-    "  foo = " << group.m_arg_foo << std::endl << 
+    "  foo = " << group.m_arg_foo << std::endl <<
     "  goo = " << group.m_arg_goo << std::endl <<
     "  filepath = " << filepath << std::endl;
 



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