gtkmm-documentation r38 - in trunk: . examples/book/custom/custom_widget examples/book/drag_and_drop examples/book/drawingarea/clock examples/book/giomm examples/book/giomm/getline examples/book/giomm/monitor_directory examples/book/giomm/read_file_async examples/book/giomm/usage



Author: murrayc
Date: Wed Mar  5 01:26:46 2008
New Revision: 38
URL: http://svn.gnome.org/viewvc/gtkmm-documentation?rev=38&view=rev

Log:
2008-03-05  Murray Cumming  <murrayc murrayc com>

* configure.in:
* examples/book/giomm/Makefile.am:
* examples/book/giomm/read_file_async/: Added a version of the 
read_file() example that uses the async methods. Seems to work fine.

* examples/book/giomm/getline/getline.cc:
* examples/book/giomm/monitor_directory/monitor_directory.cc:
* examples/book/giomm/usage/usage.cc: Whitespace corrections for 
consistency.

Added:
   trunk/examples/book/giomm/read_file_async/
      - copied from r37, /trunk/examples/book/giomm/read_file/
Modified:
   trunk/ChangeLog
   trunk/configure.in
   trunk/examples/book/custom/custom_widget/mywidget.cc
   trunk/examples/book/drag_and_drop/dndwindow.cc
   trunk/examples/book/drawingarea/clock/clock.cc
   trunk/examples/book/giomm/Makefile.am
   trunk/examples/book/giomm/getline/getline.cc
   trunk/examples/book/giomm/monitor_directory/monitor_directory.cc
   trunk/examples/book/giomm/read_file_async/main.cc
   trunk/examples/book/giomm/usage/usage.cc

Modified: trunk/configure.in
==============================================================================
--- trunk/configure.in	(original)
+++ trunk/configure.in	Wed Mar  5 01:26:46 2008
@@ -309,6 +309,7 @@
         examples/book/giomm/Makefile
           examples/book/giomm/directory_list/Makefile
           examples/book/giomm/read_file/Makefile
+          examples/book/giomm/read_file_async/Makefile
           examples/book/giomm/volumes/Makefile
           examples/book/giomm/getline/Makefile
           examples/book/giomm/monitor_directory/Makefile

Modified: trunk/examples/book/custom/custom_widget/mywidget.cc
==============================================================================
--- trunk/examples/book/custom/custom_widget/mywidget.cc	(original)
+++ trunk/examples/book/custom/custom_widget/mywidget.cc	Wed Mar  5 01:26:46 2008
@@ -158,7 +158,7 @@
     double scale_y = (double)get_allocation().get_height() / m_scale;
 
     Cairo::RefPtr<Cairo::Context> cr = m_refGdkWindow->create_cairo_context();
-    if (event)
+    if(event)
     {
       // clip to the area that needs to be re-exposed so we don't draw any
       // more than we need to.

Modified: trunk/examples/book/drag_and_drop/dndwindow.cc
==============================================================================
--- trunk/examples/book/drag_and_drop/dndwindow.cc	(original)
+++ trunk/examples/book/drag_and_drop/dndwindow.cc	Wed Mar  5 01:26:46 2008
@@ -74,7 +74,7 @@
         const Glib::RefPtr<Gdk::DragContext>& context, int, int,
         const Gtk::SelectionData& selection_data, guint, guint time)
 {
-  if ((selection_data.get_length() >= 0) && (selection_data.get_format() == 8))
+  if((selection_data.get_length() >= 0) && (selection_data.get_format() == 8))
   {
     std::cout << "Received \"" << selection_data.get_data_as_string()
         << "\" in label " << std::endl;

Modified: trunk/examples/book/drawingarea/clock/clock.cc
==============================================================================
--- trunk/examples/book/drawingarea/clock/clock.cc	(original)
+++ trunk/examples/book/drawingarea/clock/clock.cc	Wed Mar  5 01:26:46 2008
@@ -44,7 +44,7 @@
 
     Cairo::RefPtr<Cairo::Context> cr = window->create_cairo_context();
 
-    if (event)
+    if(event)
     {
         // clip to the area indicated by the expose event so that we only
         // redraw the portion of the window that needs to be redrawn
@@ -79,7 +79,7 @@
         cr->save();
         cr->set_line_cap(Cairo::LINE_CAP_ROUND);
 
-        if (i % 3 != 0)
+        if(i % 3 != 0)
         {
             inset *= 0.8;
             cr->set_line_width(0.03);

Modified: trunk/examples/book/giomm/Makefile.am
==============================================================================
--- trunk/examples/book/giomm/Makefile.am	(original)
+++ trunk/examples/book/giomm/Makefile.am	Wed Mar  5 01:26:46 2008
@@ -1 +1 @@
-SUBDIRS = directory_list read_file volumes getline monitor_directory usage
+SUBDIRS = directory_list read_file read_file_async volumes getline monitor_directory usage

Modified: trunk/examples/book/giomm/getline/getline.cc
==============================================================================
--- trunk/examples/book/giomm/getline/getline.cc	(original)
+++ trunk/examples/book/giomm/getline/getline.cc	Wed Mar  5 01:26:46 2008
@@ -27,17 +27,17 @@
  */
 int main(int argc, char** argv)
 {
-    Gio::init();
-    Glib::RefPtr<Gio::File> f = Gio::File::create_for_path ("/etc/profile");
-    Glib::RefPtr<Gio::FileInputStream> file_stream = f->read ();
-    Glib::RefPtr<Gio::DataInputStream> data_stream =
-        Gio::DataInputStream::create (file_stream);
-    std::string line;
-    int line_num = 1;
-    while (data_stream->read_line(line))
-    {
-        // print each line out prefixed with the line number
-        std::cout << std::setw (5) << line_num++ << ": >" << line << std::endl;
-    }
-    return 0;
+  Gio::init();
+  Glib::RefPtr<Gio::File> f = Gio::File::create_for_path("/etc/profile");
+  Glib::RefPtr<Gio::FileInputStream> file_stream = f->read();
+  Glib::RefPtr<Gio::DataInputStream> data_stream = Gio::DataInputStream::create(file_stream);
+
+  std::string line;
+  int line_num = 1;
+  while (data_stream->read_line(line))
+  {
+    // print each line out prefixed with the line number
+    std::cout << std::setw (5) << line_num++ << ": >" << line << std::endl;
+  }
+  return 0;
 }

Modified: trunk/examples/book/giomm/monitor_directory/monitor_directory.cc
==============================================================================
--- trunk/examples/book/giomm/monitor_directory/monitor_directory.cc	(original)
+++ trunk/examples/book/giomm/monitor_directory/monitor_directory.cc	Wed Mar  5 01:26:46 2008
@@ -25,98 +25,107 @@
 const std::string FILENAME = "./temp-file";
 
 void
-on_directory_changed (const Glib::RefPtr<Gio::File>& file,
-                      const Glib::RefPtr<Gio::File>& other_file,
-                      Gio::FileMonitorEvent event)
+on_directory_changed(const Glib::RefPtr<Gio::File>& file,
+           const Glib::RefPtr<Gio::File>& other_file,
+           Gio::FileMonitorEvent event)
 {
-    std::cout << "** Directory changed **" << std::endl;
-    if (file) {
-        std::cout << "   File 1: " << file->get_path () << std::endl;
-    }
-    if (other_file) {
-        std::cout << "   File 2: " << other_file->get_path () << std::endl;
-    }
-    std::cout << "   ";
-
-    switch (event)
-    {
-        case Gio::FILE_MONITOR_EVENT_CHANGED:
-            std::cout << "Event: A file is being changed" << std::endl;
-            break;
-        case Gio::FILE_MONITOR_EVENT_CHANGES_DONE_HINT:
-            std::cout << "Event: File changes are done" << std::endl;
-            break;
-        case Gio::FILE_MONITOR_EVENT_DELETED:
-            std::cout << "Event: A file was deleted" << std::endl;
-            break;
-        case Gio::FILE_MONITOR_EVENT_CREATED:
-            std::cout << "Event: A file was created" << std::endl;
-            break;
-        case Gio::FILE_MONITOR_EVENT_ATTRIBUTE_CHANGED:
-            std::cout << "Event: A file attribute was changed" << std::endl;
-            break;
-        case Gio::FILE_MONITOR_EVENT_PRE_UNMOUNT:
-            std::cout << "Event: pre-unmount event" << std::endl;
-            break;
-        case Gio::FILE_MONITOR_EVENT_UNMOUNTED:
-            std::cout << "Event: unmounted" << std::endl;
-            break;
-        default:
-            g_assert_not_reached ();
-    }
+  std::cout << "** Directory changed **" << std::endl;
+
+  if(file)
+    std::cout << "   File 1: " << file->get_path () << std::endl;
+
+  if(other_file)
+    std::cout << "   File 2: " << other_file->get_path () << std::endl;
+
+  std::cout << "   ";
+
+  switch(event)
+  {
+    case Gio::FILE_MONITOR_EVENT_CHANGED:
+      std::cout << "Event: A file is being changed" << std::endl;
+      break;
+    case Gio::FILE_MONITOR_EVENT_CHANGES_DONE_HINT:
+      std::cout << "Event: File changes are done" << std::endl;
+      break;
+    case Gio::FILE_MONITOR_EVENT_DELETED:
+      std::cout << "Event: A file was deleted" << std::endl;
+      break;
+    case Gio::FILE_MONITOR_EVENT_CREATED:
+      std::cout << "Event: A file was created" << std::endl;
+      break;
+    case Gio::FILE_MONITOR_EVENT_ATTRIBUTE_CHANGED:
+      std::cout << "Event: A file attribute was changed" << std::endl;
+      break;
+    case Gio::FILE_MONITOR_EVENT_PRE_UNMOUNT:
+      std::cout << "Event: pre-unmount event" << std::endl;
+      break;
+    case Gio::FILE_MONITOR_EVENT_UNMOUNTED:
+      std::cout << "Event: unmounted" << std::endl;
+      break;
+    default:
+      g_assert_not_reached ();
+  }
 }
 
-bool create_temp_file ()
+bool create_temp_file()
 {
-    try {
-        Glib::RefPtr<Gio::File> temp_file = Gio::File::create_for_path (FILENAME);
-        // FIXME: need to add an overload to giomm without a cancellable
-        Glib::RefPtr<Gio::Cancellable> cancellable = Gio::Cancellable::create ();
-        Glib::RefPtr<Gio::FileOutputStream> stream =
-            temp_file->create_file ();
-        stream->write ("this is only a test");
-        stream->close ();
-    }
-    catch (Gio::Error& e) {
-        std::cout << e.what () << std::endl;
-    }
-    // stop future timeouts from repeating
-    return false;
+  try
+  {
+    Glib::RefPtr<Gio::File> temp_file = Gio::File::create_for_path(FILENAME);
+
+    Glib::RefPtr<Gio::FileOutputStream> stream = temp_file->create_file ();
+    stream->write("This is only a test.");
+    stream->close();
+  }
+  catch (const Gio::Error& ex)
+  {
+    std::cout << ex.what () << std::endl;
+  }
+
+  // Stop future timeouts from repeating:
+  return false;
 }
 
 bool quit ()
 {
-    try {
-        Glib::RefPtr<Gio::File> temp_file = Gio::File::create_for_path (FILENAME);
-        // FIXME: need to add an overload to giomm without a cancellable
-        Glib::RefPtr<Gio::Cancellable> cancellable = Gio::Cancellable::create ();
-        temp_file->trash (cancellable);
-    }
-    catch (Gio::Error& e) {
-        std::cout << e.what () << std::endl;
-    }
-    // stop future timeouts from repeating
-    mainloop->quit ();
-    return false;
+  try
+  {
+    Glib::RefPtr<Gio::File> temp_file = Gio::File::create_for_path(FILENAME);
+    temp_file->trash();
+  }
+  catch (const Gio::Error& ex)
+  {
+    std::cout << ex.what () << std::endl;
+  }
+
+  // Stop future timeouts from repeating:
+  mainloop->quit();
+
+  return false;
 }
 
 int main(int argc, char** argv)
 {
-    Gio::init ();
-    mainloop = Glib::MainLoop::create();
-    std::string current_dir = Glib::get_current_dir ();
-    Glib::RefPtr<Gio::File> dir = Gio::File::create_for_path (current_dir);
-    Glib::RefPtr<Gio::FileMonitor> monitor = dir->monitor_directory ();
-    std::cout << "Monitoring directory '" << current_dir << "'..."
-        << std::endl << std::endl;
-    monitor->signal_changed ().connect (sigc::ptr_fun (on_directory_changed));
-    std::cout << "Creating test file '" << FILENAME << "' to see what happens..."
-        << std::endl << std::endl;
-    // wait a couple seconds and then create a temp file to trigger the
-    // directory monitor.
-    Glib::signal_timeout ().connect_seconds (sigc::ptr_fun (create_temp_file), 2);
-    // then exit a couple seconds later
-    Glib::signal_timeout ().connect_seconds (sigc::ptr_fun (quit), 4);
-    mainloop->run ();
-    return 0;
+  Gio::init();
+  mainloop = Glib::MainLoop::create();
+
+  std::string current_dir = Glib::get_current_dir();
+  Glib::RefPtr<Gio::File> dir = Gio::File::create_for_path(current_dir);
+  Glib::RefPtr<Gio::FileMonitor> monitor = dir->monitor_directory();
+
+  std::cout << "Monitoring directory '" << current_dir << "'..."
+    << std::endl << std::endl;
+  monitor->signal_changed().connect(sigc::ptr_fun(on_directory_changed));
+
+  std::cout << "Creating test file '" << FILENAME << "' to see what happens..."
+    << std::endl << std::endl;
+
+  // Wait a couple seconds and then create a temp file to trigger the
+  // directory monitor.
+  Glib::signal_timeout().connect_seconds(sigc::ptr_fun (&create_temp_file), 2);
+
+  // then exit a couple seconds later
+  Glib::signal_timeout().connect_seconds(sigc::ptr_fun (&quit), 4);
+  mainloop->run();
+  return 0;
 }

Modified: trunk/examples/book/giomm/read_file_async/main.cc
==============================================================================
--- /trunk/examples/book/giomm/read_file/main.cc	(original)
+++ trunk/examples/book/giomm/read_file_async/main.cc	Wed Mar  5 01:26:46 2008
@@ -20,23 +20,19 @@
 #include <iostream>
 
 
-int main(int argc, char** argv)
+
+Glib::RefPtr<Glib::MainLoop> mainloop;
+Glib::RefPtr<Gio::File> file;
+Glib::RefPtr<Gio::FileInputStream> stream;
+gchar buffer[1000]; //TODO: This is unpleasant.
+
+void on_stream_read_async_ready(Glib::RefPtr<Gio::AsyncResult>& result)
 {
-  Gio::init();
+  std::cout << "Stream Async handler called." << std::endl;
 
   try
   {
-    Glib::RefPtr<Gio::File> file = Gio::File::create_for_path("/etc/fstab");
-    if(!file)
-      std::cerr << "Gio::File::create_for_path() returned an empty RefPtr." << std::endl;
-
-    Glib::RefPtr<Gio::FileInputStream> stream = file->read();
-    if(!stream)
-      std::cerr << "Gio::File::read() returned an empty RefPtr." << std::endl;
-
-    gchar buffer[1000]; //TODO: This is unpleasant.
-    memset(buffer, 0, 1000);
-    const gsize bytes_read = stream->read(buffer, 1000);
+    const gsize bytes_read = stream->read_finish(result);
     
     if(bytes_read)
       std::cout << "File contents read: " << buffer << std::endl;
@@ -49,6 +45,51 @@
     std::cerr << "Exception caught: " << ex.what() << std::endl; 
   }
 
+  mainloop->quit();
+}
+
+void on_file_read_async_ready(Glib::RefPtr<Gio::AsyncResult>& result)
+{
+  std::cout << "File Async handler called." << std::endl;
+
+  try
+  {
+    stream = file->read_finish(result);
+    if(!stream)
+      std::cerr << "Gio::File::read() returned an empty RefPtr." << std::endl;
+  }
+  catch(const Glib::Exception& ex)
+  {
+    std::cerr << "Exception caught: " << ex.what() << std::endl;
+    mainloop->quit(); 
+  }
+
+  if(stream)
+  {
+    memset(buffer, 0, 1000);
+    stream->read_async(buffer, 1000, sigc::ptr_fun(&on_stream_read_async_ready));
+  }
+}
+
+int main(int argc, char** argv)
+{
+  Gio::init();
+
+  try
+  {
+    file = Gio::File::create_for_path("/etc/fstab");
+    if(!file)
+      std::cerr << "Gio::File::create_for_path() returned an empty RefPtr." << std::endl;
+  }
+  catch(const Glib::Exception& ex)
+  {
+    std::cerr << "Exception caught: " << ex.what() << std::endl; 
+  }
+
+  mainloop = Glib::MainLoop::create();
+
+  file->read_async( sigc::ptr_fun(&on_file_read_async_ready) );
+  mainloop->run();
 
   return 0;
 }

Modified: trunk/examples/book/giomm/usage/usage.cc
==============================================================================
--- trunk/examples/book/giomm/usage/usage.cc	(original)
+++ trunk/examples/book/giomm/usage/usage.cc	Wed Mar  5 01:26:46 2008
@@ -1,6 +1,6 @@
 /*******************************************************************************
  *
- *  Copyright (c) 2008 Jonathon Jongsma
+ *  Copyright(c) 2008 Jonathon Jongsma
  *
  *  This file is part of gtkmm
  *
@@ -21,47 +21,53 @@
 #include <giomm.h>
 #include <iostream>
 
-goffset get_size_recursively (const Glib::RefPtr<Gio::File>& file)
+goffset get_size_recursively(const Glib::RefPtr<Gio::File>& file)
 {
-    goffset total = 0;
-    g_return_val_if_fail (file, total);
-    try {
-        Glib::RefPtr<Gio::FileInfo> info = file->query_info ();
-        if (info->get_file_type () == Gio::FILE_TYPE_DIRECTORY)
-        {
-            Glib::RefPtr<Gio::FileEnumerator> children = file->enumerate_children ();
-            Glib::RefPtr<Gio::FileInfo> child_info;
-            while (child_info = children->next_file ())
-            {
-                goffset size = get_size_recursively (
-                        file->get_child (child_info->get_name ()));
-                total += size;
-            }
-            //std::cout << file->get_path () << ": " << total << std::endl;
-        }
-        else
-        {
-            total = info->get_size ();
-        }
+  goffset total = 0;
+  g_return_val_if_fail(file, total);
+
+  try
+  {
+    Glib::RefPtr<Gio::FileInfo> info = file->query_info();
+    if(info->get_file_type() == Gio::FILE_TYPE_DIRECTORY)
+    {
+      Glib::RefPtr<Gio::FileEnumerator> children = file->enumerate_children();
+      Glib::RefPtr<Gio::FileInfo> child_info;
+
+      while(child_info = children->next_file())
+      {
+        goffset size = get_size_recursively(
+            file->get_child(child_info->get_name()));
+        total += size;
+      }
+
+      //std::cout << file->get_path() << ": " << total << std::endl;
     }
-    catch (const Glib::Error& error)
+    else
     {
-        std::cerr << error.what () << std::endl;
+      total = info->get_size();
     }
-    return total;
+  }
+  catch(const Glib::Error& error)
+  {
+    std::cerr << error.what() << std::endl;
+  }
+  return total;
 }
 
 int main(int argc, char** argv)
 {
-    std::string root_dir = ".";
-    if (argc > 1) {
-        root_dir = argv[1];
-    }
-    Gio::init ();
-    Glib::RefPtr<Gio::File> file = Gio::File::create_for_path (root_dir);
-    std::cout << "Gathering disk usage information for '" << file->get_path () << "'" << std::endl;
-    goffset total = get_size_recursively (file);
-    std::cout << "Total: " << total << std::endl;
+  std::string root_dir = ".";
+  if(argc > 1)
+    root_dir = argv[1];
+
+  Gio::init();
+
+  Glib::RefPtr<Gio::File> file = Gio::File::create_for_path(root_dir);
+  std::cout << "Gathering disk usage information for '" << file->get_path() << "'" << std::endl;
+
+  const goffset total = get_size_recursively(file);
+  std::cout << "Total: " << total << std::endl;
 
-    return 0;
+  return 0;
 }



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