[gtkmm] demos: Generate and use resource files



commit 4936df66082eeefbc847e09187900d4f161441ff
Author: Kjell Ahlstedt <kjell ahlstedt bredband net>
Date:   Wed May 13 15:24:48 2015 +0200

    demos: Generate and use resource files
    
    * .gitignore: Add the generated demo_resources.c file.
    * configure.ac: Add check for C compiler and glib-compile-resources.
    * demos/Makefile.am: Add make rules for the resource file.
    * demos/gtk-demo/demo-common.[h|cc]: Removed files.
    * demos/gtk-demo/demo.gresource.xml: New file.
    * demos/gtk-demo/demowindow.[h|cc]: Remove read_line(). Read source files
    from resources (Gio::Resource).
    * demos/gtk-demo/example_iconview.cc:
    * demos/gtk-demo/example_images.cc:
    * demos/gtk-demo/example_pixbufs.cc:
    * demos/gtk-demo/example_textview.cc:
    * demos/pixbuf-demo.cc: Read images from resources.
    These changes bring gtkmm's demos more in sync with gtk+. Bug #748657.

 .gitignore                         |    1 +
 configure.ac                       |    7 ++-
 demos/Makefile.am                  |   21 ++++--
 demos/gtk-demo/demo-common.cc      |   63 -----------------
 demos/gtk-demo/demo-common.h       |    8 --
 demos/gtk-demo/demo.gresource.xml  |   52 ++++++++++++++
 demos/gtk-demo/demowindow.cc       |  133 ++++++++----------------------------
 demos/gtk-demo/demowindow.h        |    3 -
 demos/gtk-demo/example_iconview.cc |    9 +--
 demos/gtk-demo/example_images.cc   |   45 ++++++------
 demos/gtk-demo/example_pixbufs.cc  |   40 +++++++----
 demos/gtk-demo/example_textview.cc |   35 +++++----
 demos/pixbuf-demo.cc               |   26 ++++---
 13 files changed, 186 insertions(+), 257 deletions(-)
---
diff --git a/.gitignore b/.gitignore
index e8f48d5..1025aa7 100644
--- a/.gitignore
+++ b/.gitignore
@@ -40,6 +40,7 @@ MSVC_Net2010/gtkmm/gtkmm.rc
 MSVC_Net2010/gtkmm/gtkmmconfig.h
 
 # demos
+demos/gtk-demo/demo_resources.c
 demos/gtk-demo/gtkmm-demo
 demos/pixbuf-demo
 
diff --git a/configure.ac b/configure.ac
index d3afdbd..9974e43 100644
--- a/configure.ac
+++ b/configure.ac
@@ -123,8 +123,13 @@ MM_ARG_WITH_TAGFILE_DOC([cairomm-1.0.tag], [cairomm-1.0])
 MM_ARG_WITH_TAGFILE_DOC([pangomm-1.4.tag], [pangomm-1.4])
 MM_ARG_WITH_TAGFILE_DOC([atkmm-1.6.tag], [atkmm-1.6])
 
+# glib-compile-resources and C compiler are needed to generate and compile
+# resource files in demo programs.
+AC_SUBST(GLIB_COMPILE_RESOURCES, [`$PKG_CONFIG --variable=glib_compile_resources gio-2.0`])
+AC_PROG_CC
+
 AC_LANG([C++])
-AC_CHECK_FUNCS([flockfile funlockfile getc_unlocked mkfifo])
+AC_CHECK_FUNCS([mkfifo])
 
 # Evaluate the --enable-warnings=level option.
 MM_ARG_ENABLE_WARNINGS([GTKMM_WXXFLAGS],
diff --git a/demos/Makefile.am b/demos/Makefile.am
index 8f6df6e..6e72312 100644
--- a/demos/Makefile.am
+++ b/demos/Makefile.am
@@ -19,11 +19,12 @@ AUTOMAKE_OPTIONS = subdir-objects
 
 check_PROGRAMS = pixbuf-demo gtk-demo/gtkmm-demo
 
-pixbuf_demo_SOURCES = pixbuf-demo.cc
+pixbuf_demo_SOURCES = \
+  gtk-demo/demo_resources.c \
+  pixbuf-demo.cc
 
 gtk_demo_gtkmm_demo_SOURCES =                          \
-       gtk-demo/demo-common.cc                         \
-       gtk-demo/demo-common.h                          \
+       gtk-demo/demo_resources.c                               \
        gtk-demo/demos.h                                \
        gtk-demo/demowindow.cc                          \
        gtk-demo/demowindow.h                           \
@@ -54,7 +55,11 @@ gtk_demo_gtkmm_demo_SOURCES =                                \
        gtk-demo/textwidget.cc                          \
        gtk-demo/textwidget.h
 
-dist_noinst_DATA =                     \
+dist_noinst_DATA = \
+  gtk-demo/demo.gresource.xml \
+  $(GTK_DEMO_RESOURCES)
+
+GTK_DEMO_RESOURCES = \
        gtk-demo/alphatest.png          \
        gtk-demo/apple-red.png          \
        gtk-demo/background.jpg         \
@@ -67,9 +72,15 @@ dist_noinst_DATA =                   \
        gtk-demo/gnome-gimp.png         \
        gtk-demo/gnome-gmush.png        \
        gtk-demo/gnome-gsame.png        \
-       gtk-demo/gnu-keys.png           \
+       gtk-demo/gnu-keys.png     \
        gtk-demo/gtk-logo-rgb.gif
 
+# Delete built sources on make maintainer-clean.
+MAINTAINERCLEANFILES = gtk-demo/demo_resources.c
+
+gtk-demo/demo_resources.c: gtk-demo/demo.gresource.xml $(GTK_DEMO_RESOURCES)
+       $(AM_V_GEN) $(GLIB_COMPILE_RESOURCES) --target=$@ --sourcedir=$(dir $<) --generate-source $<
+
 gdkmm_includes = -I$(top_builddir)/gdk $(if $(srcdir:.=),-I$(top_srcdir)/gdk)
 gdkmm_ldadd    = $(top_builddir)/gdk/gdkmm/libgdkmm-$(GDKMM_API_VERSION).la
 
diff --git a/demos/gtk-demo/demo.gresource.xml b/demos/gtk-demo/demo.gresource.xml
new file mode 100644
index 0000000..63e5be7
--- /dev/null
+++ b/demos/gtk-demo/demo.gresource.xml
@@ -0,0 +1,52 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<gresources>
+  <gresource prefix="/iconview">
+    <file preprocess="to-pixdata">gnome-fs-directory.png</file>
+    <file preprocess="to-pixdata">gnome-fs-regular.png</file>
+  </gresource>
+  <gresource prefix="/images">
+    <file>alphatest.png</file>
+    <file>floppybuddy.gif</file>
+    <file>gtk-logo-rgb.gif</file>
+  </gresource>
+  <gresource prefix="/pixbufs">
+    <file>apple-red.png</file>
+    <file>background.jpg</file>
+    <file>gnome-applets.png</file>
+    <file>gnome-calendar.png</file>
+    <file>gnome-foot.png</file>
+    <file>gnome-gimp.png</file>
+    <file>gnome-gmush.png</file>
+    <file>gnome-gsame.png</file>
+    <file>gnu-keys.png</file>
+  </gresource>
+  <gresource prefix="/sources">
+    <file>example_appwindow.cc</file>
+    <file>example_buttonbox.cc</file>
+    <file>example_change_display.cc</file>
+    <file>example_colorsel.cc</file>
+    <file>example_dialog.cc</file>
+    <file>example_drawingarea.cc</file>
+    <file>example_flowbox.cc</file>
+    <file>example_gestures.cc</file>
+    <file>example_headerbar.cc</file>
+    <file>example_icontheme.cc</file>
+    <file>example_iconview.cc</file>
+    <file>example_images.cc</file>
+    <file>example_menus.cc</file>
+    <file>example_overlay.cc</file>
+    <file>example_panes.cc</file>
+    <file>example_pixbufs.cc</file>
+    <file>example_sizegroup.cc</file>
+    <file>example_stack.cc</file>
+    <file>example_stacksidebar.cc</file>
+    <file>example_textview.cc</file>
+    <file>example_treeview_editable_cells.cc</file>
+    <file>example_treeview_liststore.cc</file>
+    <file>example_treeview_treestore.cc</file>
+  </gresource>
+  <gresource prefix="/textview">
+    <file>floppybuddy.gif</file>
+    <file>gtk-logo-rgb.gif</file>
+  </gresource>
+</gresources>
diff --git a/demos/gtk-demo/demowindow.cc b/demos/gtk-demo/demowindow.cc
index 4e90ef8..da635d8 100644
--- a/demos/gtk-demo/demowindow.cc
+++ b/demos/gtk-demo/demowindow.cc
@@ -1,5 +1,3 @@
-/* $Id$ */
-
 /* Copyright (C) 2001 The gtkmm Development Team
  *
  * This library is free software; you can redistribute it and/or
@@ -21,37 +19,19 @@
 #include "config.h"
 #endif
 
-#include <gtkmm/main.h>
 #include <gtkmm/cellrenderertext.h>
 #include <gtkmm/treeviewcolumn.h>
-#include <gtkmm/box.h>
+#include <glibmm/bytes.h>
 #include <glibmm/convert.h>
-#include <glibmm/fileutils.h>
+#include <giomm/resource.h>
 #include "demowindow.h"
 #include "textwidget.h"
 #include "demos.h"
-#include <vector>
-#include <cctype>
-#include <cerrno>
-#include <stdio.h>
-
 #include <cstring>
 
 using std::isspace;
 using std::strlen;
 
-#include "demo-common.h"
-
-#ifdef NEED_FLOCKFILE_PROTO
-extern "C" void flockfile (FILE *);
-#endif
-#ifdef NEED_FUNLOCKFILE_PROTO
-extern "C" void funlockfile (FILE *);
-#endif
-#ifdef NEED_GETC_UNLOCKED_PROTO
-extern "C" int getc_unlocked (FILE *);
-#endif
-
 namespace
 {
 
@@ -236,61 +216,6 @@ void DemoWindow::on_treeselection_changed()
   }
 }
 
-bool DemoWindow::read_line (FILE *stream, GString *str)
-{
-  int n_read = 0;
-
-#ifdef HAVE_FLOCKFILE
-  flockfile (stream);
-#endif
-
-  g_string_truncate (str, 0);
-
-  while (1)
-    {
-      int c;
-
-#ifdef HAVE_GETC_UNLOCKED
-      c = getc_unlocked (stream);
-#else
-      c = getc (stream);
-#endif
-      if (c == EOF)
-       goto done;
-      else
-       n_read++;
-
-      switch (c)
-       {
-       case '\r':
-       case '\n':
-         {
-#ifdef HAVE_GETC_UNLOCKED
-      int next_c = getc_unlocked (stream);
-#else
-      int next_c = getc (stream);
-#endif
-           if (!(next_c == EOF ||
-                 (c == '\r' && next_c == '\n') ||
-                 (c == '\n' && next_c == '\r')))
-             ungetc (next_c, stream);
-       
-           goto done;
-         }
-       default:
-         g_string_append_c (str, c);
-       }
-    }
-
- done:
-
-#ifdef HAVE_FUNLOCKFILE
-  funlockfile (stream);
-#endif
-  return n_read > 0;
-}
-
-
 void DemoWindow::load_file(const std::string& filename)
 {
   if ( m_current_filename == filename )
@@ -307,39 +232,35 @@ void DemoWindow::load_file(const std::string& filename)
     Glib::RefPtr<Gtk::TextBuffer> refBufferInfo = m_TextWidget_Info.get_buffer();
     Glib::RefPtr<Gtk::TextBuffer> refBufferSource = m_TextWidget_Source.get_buffer();
 
-    FILE* file = fopen (filename.c_str(), "r");
-    if (!file)
+    Glib::RefPtr<const Glib::Bytes> bytes;
+    try
     {
-      try
-      {
-        std::string installed = demo_find_file(filename);
-        file = fopen (installed.c_str(), "r");
-      }
-      catch (const Glib::FileError& ex)
-      {
-        g_warning ("%s\n", ex.what().c_str());
-        return;
-      }
+      bytes = Gio::Resource::lookup_data_global("/sources/" + filename);
     }
-
-    if (!file)
+    catch (const Gio::ResourceError& ex)
     {
-      g_warning ("Cannot open %s: %s\n", filename.c_str(), g_strerror (errno));
+      g_warning ("Cannot open source for %s: %s\n", filename.c_str(), ex.what().c_str());
       return;
     }
 
-    GString *buffer = g_string_new (NULL);
+    gsize data_size = 0;
+    gchar** lines = g_strsplit(static_cast<const gchar*>(bytes->get_data(data_size)), "\n", -1);
+    bytes.reset();
+
     int state = 0;
     bool in_para = false;
     Gtk::TextBuffer::iterator start = refBufferInfo->get_iter_at_offset(0);
-    while (read_line (file, buffer))
+    for (int i = 0; lines[i] != NULL; i++)
     {
-      gchar *p = buffer->str;
+      /* Make sure \r is stripped at the end for the poor windows people */
+      lines[i] = g_strchomp(lines[i]);
+
+      gchar *p = lines[i];
       gchar *q = 0;
       gchar *r = 0;
 
       switch (state)
-       {
+      {
        case 0:
          /* Reading title */
          while (*p == '/' || *p == '*' || isspace (*p))
@@ -361,7 +282,7 @@ void DemoWindow::load_file(const std::string& filename)
            {
              Gtk::TextBuffer::iterator end = start;
 
-              const Glib::ustring strTemp (p, q);
+            const Glib::ustring strTemp (p, q);
              end = refBufferInfo->insert(end, strTemp);
              start = end;
 
@@ -418,12 +339,12 @@ void DemoWindow::load_file(const std::string& filename)
          while (isspace (*p))
            p++;
 
-         if (*p)
-           {
-             p = buffer->str;
-             state++;
-             /* Fall through */
-           }
+          if (*p)
+          {
+            p = lines[i];
+            state++;
+            /* Fall through */
+          }
          else
            break;
                
@@ -432,8 +353,10 @@ void DemoWindow::load_file(const std::string& filename)
          start = refBufferSource->insert(start, p);
          start = refBufferSource->insert(start, "\n");
          break;
-       }
-     }
+      } // end switch state
+    } // end for i
+
+    g_strfreev (lines);
 
     m_TextWidget_Source.fontify();
   }
diff --git a/demos/gtk-demo/demowindow.h b/demos/gtk-demo/demowindow.h
index 1ae9cd9..437901b 100644
--- a/demos/gtk-demo/demowindow.h
+++ b/demos/gtk-demo/demowindow.h
@@ -1,5 +1,3 @@
-/* $Id$ */
-
 /* Copyright (C) 2001 The gtkmm Development Team
  *
  * This library is free software; you can redistribute it and/or
@@ -45,7 +43,6 @@ protected:
   void fill_tree();
 
   void load_file(const std::string& filename);
-  bool read_line (FILE *stream, GString *str);
 
   //Signal handlers:
   static bool select_function(const Glib::RefPtr<Gtk::TreeModel>& model, const Gtk::TreeModel::Path& path, 
bool currently_selected);
diff --git a/demos/gtk-demo/example_iconview.cc b/demos/gtk-demo/example_iconview.cc
index 007b055..b698aa2 100644
--- a/demos/gtk-demo/example_iconview.cc
+++ b/demos/gtk-demo/example_iconview.cc
@@ -8,7 +8,6 @@
 
 #include <gtkmm.h>
 #include <iostream> //For std::cout.
-#include "demo-common.h" //For demo_find_file().
 
 class Example_IconView : public Gtk::Window
 {
@@ -73,12 +72,10 @@ Example_IconView::Example_IconView()
   
   try
   {
-    Glib::ustring filename = demo_find_file("gnome-fs-regular.png");
-    m_refPixbufFile = Gdk::Pixbuf::create_from_file(filename);
-    filename = demo_find_file("gnome-fs-directory.png");
-    m_refPixbufFolder = Gdk::Pixbuf::create_from_file(filename);
+    m_refPixbufFile = Gdk::Pixbuf::create_from_resource("/iconview/gnome-fs-regular.png");
+    m_refPixbufFolder = Gdk::Pixbuf::create_from_resource("/iconview/gnome-fs-directory.png");
   }
-  catch(const Glib::FileError& error)
+  catch(const Glib::Error& error)
   {
     std::cout << error.what() << std::endl;
   }
diff --git a/demos/gtk-demo/example_images.cc b/demos/gtk-demo/example_images.cc
index ee550d3..c78b8b3 100644
--- a/demos/gtk-demo/example_images.cc
+++ b/demos/gtk-demo/example_images.cc
@@ -13,7 +13,6 @@
  */
 
 #include <gtkmm.h>
-#include "demo-common.h"
 
 class Example_Images : public Gtk::Window
 {
@@ -36,7 +35,7 @@ protected:
   Gtk::Image m_Image_Progressive;
   Glib::RefPtr<Gdk::PixbufLoader> m_refPixbufLoader;
 
-  Glib::RefPtr<Glib::IOChannel> m_image_stream;
+  Glib::RefPtr<Gio::InputStream> m_image_stream;
 };
 
 //Called by DemoWindow;
@@ -48,7 +47,7 @@ Gtk::Window* do_images()
 Example_Images::Example_Images()
 :
   m_VBox                (Gtk::ORIENTATION_VERTICAL, 8),
-  m_image_stream        (0)
+  m_image_stream        ()
 {
   set_title("Images");
   set_border_width(8);
@@ -67,7 +66,8 @@ Example_Images::Example_Images()
   m_Frame_Image.set_valign(Gtk::ALIGN_CENTER);
   m_VBox.pack_start(m_Frame_Image, Gtk::PACK_SHRINK);
 
-  Gtk::Image* pImage = Gtk::manage(new Gtk::Image(demo_find_file("gtk-logo-rgb.gif")));
+  Gtk::Image* pImage = Gtk::manage(new Gtk::Image());
+  pImage->set_from_resource("/images/gtk-logo-rgb.gif");
   m_Frame_Image.add(*pImage);
 
   /* Animation */
@@ -81,7 +81,8 @@ Example_Images::Example_Images()
   m_Frame_Animation.set_valign(Gtk::ALIGN_CENTER);
   m_VBox.pack_start(m_Frame_Animation, Gtk::PACK_SHRINK);
 
-  pImage = Gtk::manage(new Gtk::Image(demo_find_file("floppybuddy.gif")));
+  pImage = Gtk::manage(new Gtk::Image());
+  pImage->set_from_resource("/images/floppybuddy.gif");
   m_Frame_Animation.add(*pImage);
 
   /* Progressive */
@@ -132,16 +133,15 @@ bool Example_Images::on_timeout()
   {
     guint8 buf[256];
     gsize bytes_read = 0;
-    Glib::IOStatus status = Glib::IO_STATUS_NORMAL;
 
     try
     {
-      status = m_image_stream->read(reinterpret_cast<char*>(&buf[0]), sizeof(buf), bytes_read);
+      bytes_read = m_image_stream->read(buf, sizeof(buf));
     }
     catch(const Glib::Error& error)
     {
      
-      Glib::ustring strMsg = "Failure reading image file 'alphatest.png': ";
+      Glib::ustring strMsg = "Failure reading image 'alphatest.png': ";
       strMsg += error.what();
 
       Gtk::MessageDialog dialog(strMsg, false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_CLOSE);
@@ -159,7 +159,6 @@ bool Example_Images::on_timeout()
     catch(const Glib::Error& error)
     {
       Glib::ustring strMsg = "Failed to load image: ";
-
       strMsg += error.what();
 
       Gtk::MessageDialog dialog(strMsg, false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_CLOSE);
@@ -170,7 +169,7 @@ bool Example_Images::on_timeout()
       return false; // uninstall the timeout
     }
 
-    if(status == Glib::IO_STATUS_EOF)
+    if(bytes_read == 0)
     {
       m_image_stream.reset();
 
@@ -185,8 +184,7 @@ bool Example_Images::on_timeout()
       }
       catch(const Glib::Error& error)
       {
-        Glib::ustring strMsg = "Failed to load image: ";
-
+        Glib::ustring strMsg = "Failed to close image: ";
         strMsg += error.what();
 
         Gtk::MessageDialog dialog(strMsg, false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_CLOSE);
@@ -204,12 +202,11 @@ bool Example_Images::on_timeout()
   {
     try
     {
-      m_image_stream = Glib::IOChannel::create_from_file(demo_find_file("alphatest.png"), "r");
+      m_image_stream = Gio::Resource::open_stream_global("/images/alphatest.png");
     }
     catch(const Glib::Error& error)
     {
-
-      Glib::ustring strMsg = "Unable to open image file 'alphatest.png': ";
+      Glib::ustring strMsg = "Unable to open image 'alphatest.png': ";
       strMsg += error.what();
 
       Gtk::MessageDialog dialog(strMsg, false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_CLOSE);
@@ -218,8 +215,6 @@ bool Example_Images::on_timeout()
       return false; // uninstall the timeout
     }
 
-    m_image_stream->set_encoding(); // no encoding == binary
-
     if(m_refPixbufLoader)
     {
       m_refPixbufLoader->close();
@@ -253,12 +248,16 @@ void Example_Images::on_loader_area_prepared()
 void Example_Images::on_loader_area_updated(int/*x*/, int/*y*/, int/*width*/, int/*height*/)
 {
   /* We know the pixbuf inside the Gtk::Image has changed, but the image
-   * itself doesn't know this; so queue a redraw.  If we wanted to be
-   * really efficient, we could use a drawing area or something
-   * instead of a GtkImage, so we could control the exact position of
-   * the pixbuf on the display, then we could queue a draw for only
-   * the updated area of the image.
+   * itself doesn't know this; so give it a hint by setting the pixbuf
+   * again. Queuing a redraw used to be sufficient, but nowadays Gtk::Image
+   * uses GtkIconHelper which caches the pixbuf state and will just redraw
+   * from the cache.
+   * If we wanted to be really efficient, we could use a drawing area or
+   * something instead of a Gtk::Image, so we could control the exact
+   * position of the pixbuf on the display, then we could queue a draw
+   * for only the updated area of the image.
    */
-  m_Image_Progressive.queue_draw();
+  Glib::RefPtr<Gdk::Pixbuf> refPixbuf = m_Image_Progressive.get_pixbuf();
+  m_Image_Progressive.set(refPixbuf);
 }
 
diff --git a/demos/gtk-demo/example_pixbufs.cc b/demos/gtk-demo/example_pixbufs.cc
index d283b31..59f54a1 100644
--- a/demos/gtk-demo/example_pixbufs.cc
+++ b/demos/gtk-demo/example_pixbufs.cc
@@ -16,7 +16,6 @@
 
 #include <gtkmm.h>
 #include <math.h>
-#include "demo-common.h"
 
 #define FRAME_DELAY 50
 
@@ -82,42 +81,53 @@ Example_Pixbufs::Example_Pixbufs()
   set_title("Pixbufs");
   set_resizable(false);
 
-  load_pixbufs();
+  try
+  {
+    load_pixbufs();
 
-  set_size_request(m_back_width, m_back_height);
-  m_refPixbuf = Gdk::Pixbuf::create(Gdk::COLORSPACE_RGB, FALSE, 8, m_back_width, m_back_height);
-  m_DrawingArea.signal_draw().connect(sigc::mem_fun(*this, &Example_Pixbufs::on_drawingarea_draw));
-  add(m_DrawingArea);
+    set_size_request(m_back_width, m_back_height);
+    m_refPixbuf = Gdk::Pixbuf::create(Gdk::COLORSPACE_RGB, FALSE, 8, m_back_width, m_back_height);
+    m_DrawingArea.signal_draw().connect(sigc::mem_fun(*this, &Example_Pixbufs::on_drawingarea_draw));
+    add(m_DrawingArea);
 
-  m_TimeoutConnection = Glib::signal_timeout().connect(
-      sigc::mem_fun(*this, &Example_Pixbufs::on_timeout), FRAME_DELAY);
+    m_TimeoutConnection = Glib::signal_timeout().connect(
+        sigc::mem_fun(*this, &Example_Pixbufs::on_timeout), FRAME_DELAY);
+  }
+  catch (const Glib::Error& error)
+  {
+    Glib::ustring strMsg = "Failed to load an image: ";
+    strMsg += error.what();
+
+    Gtk::MessageDialog dialog(strMsg, false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_CLOSE);
+    dialog.run();
+  }
 
   show_all();
 }
 
 Example_Pixbufs::~Example_Pixbufs()
 {
-  m_TimeoutConnection.disconnect(); //Will probably happen anyway, in the destrctor.
+  m_TimeoutConnection.disconnect(); //Will probably happen anyway, in the destructor.
 }
 
-/* Loads the m_images for the demo and returns whether the operation succeeded */
+/* Loads the m_images for the demo and throws and exception if the operation failed */
 void Example_Pixbufs::load_pixbufs()
 {
   if(m_refPixbuf_Background)
     return; /* already loaded earlier */
 
-  std::string filename_background = BACKGROUND_NAME;
+  std::string resource_name_background = std::string("/pixbufs/") + BACKGROUND_NAME;
 
-  m_refPixbuf_Background = Gdk::Pixbuf::create_from_file(demo_find_file(filename_background));
+  m_refPixbuf_Background = Gdk::Pixbuf::create_from_resource(resource_name_background);
 
   m_back_width = m_refPixbuf_Background->get_width();
   m_back_height = m_refPixbuf_Background->get_height();
 
   for(unsigned i = 0; i < N_IMAGES; ++i)
   {
-    std::string filename = image_names[i];
+    std::string resource_name = std::string("/pixbufs/") + image_names[i];
 
-    Glib::RefPtr<Gdk::Pixbuf> pixbuf = Gdk::Pixbuf::create_from_file(demo_find_file(filename));
+    Glib::RefPtr<Gdk::Pixbuf> pixbuf = Gdk::Pixbuf::create_from_resource(resource_name);
 
     m_images[i] = pixbuf;
   }
@@ -126,7 +136,7 @@ void Example_Pixbufs::load_pixbufs()
 /* Draw callback for the drawing area */
 bool Example_Pixbufs::on_drawingarea_draw(const Cairo::RefPtr<Cairo::Context>& cr)
 {
-  Gdk::Cairo::set_source_pixbuf(cr, m_refPixbuf_Background);
+  Gdk::Cairo::set_source_pixbuf(cr, m_refPixbuf);
   cr->paint();
 
   return true;
diff --git a/demos/gtk-demo/example_textview.cc b/demos/gtk-demo/example_textview.cc
index 4b5bbf8..fe689dd 100644
--- a/demos/gtk-demo/example_textview.cc
+++ b/demos/gtk-demo/example_textview.cc
@@ -7,11 +7,9 @@
  *
  */
 
-#include <cstdlib>
 #include <gtkmm.h>
 #include <gtk/gtk.h>
-
-#include "demo-common.h"
+#include <iostream> //For std::cout.
 
 using std::exit;
 
@@ -173,17 +171,17 @@ void Example_TextView::create_tags(Glib::RefPtr<Gtk::TextBuffer>& refBuffer)
 
 void Example_TextView::insert_text(Glib::RefPtr<Gtk::TextBuffer>& refBuffer)
 {
-  Glib::RefPtr<Gdk::Pixbuf> refPixbuf = Gdk::Pixbuf::create_from_file(demo_find_file("gtk-logo-rgb.gif"));
-
-  if(!refPixbuf)
+  Glib::RefPtr<Gdk::Pixbuf> refPixbuf;
+  try
   {
-    //TODO: This is not real error handling.
-    g_printerr ("Failed to load image file gtk-logo-rgb.gif\n");
-    exit (1);
+    refPixbuf = Gdk::Pixbuf::create_from_resource("/textview/gtk-logo-rgb.gif");
+    Glib::RefPtr<Gdk::Pixbuf> refScaled = refPixbuf->scale_simple(32, 32, Gdk::INTERP_BILINEAR);
+    refPixbuf = refScaled;
+  }
+  catch (const Glib::Error& error)
+  {
+    std::cout << "Failed to load image gtk-logo-rgb.gif: " << error.what() << std::endl;
   }
-
-  Glib::RefPtr<Gdk::Pixbuf> refScaled = refPixbuf->scale_simple(32, 32, Gdk::INTERP_BILINEAR);
-  refPixbuf = refScaled;
 
   /* get start of buffer; each insertion will revalidate the
    * iterator to point to just after the inserted text.
@@ -243,9 +241,12 @@ void Example_TextView::insert_text(Glib::RefPtr<Gtk::TextBuffer>& refBuffer)
   iter = refBuffer->insert_with_tag(iter, "Images. ", "heading");
 
   iter = refBuffer->insert(iter, "The buffer can have images in it: ");
-  iter = refBuffer->insert_pixbuf(iter, refPixbuf);
-  iter = refBuffer->insert_pixbuf(iter, refPixbuf);
-  iter = refBuffer->insert_pixbuf(iter, refPixbuf);
+  if (refPixbuf)
+  {
+    iter = refBuffer->insert_pixbuf(iter, refPixbuf);
+    iter = refBuffer->insert_pixbuf(iter, refPixbuf);
+    iter = refBuffer->insert_pixbuf(iter, refPixbuf);
+  }
   iter = refBuffer->insert(iter, " for example.\n\n");
 
   iter = refBuffer->insert_with_tag(iter, "Spacing. ", "heading");
@@ -406,7 +407,9 @@ void Example_TextView::attach_widgets(Gtk::TextView& text_view)
     }
     else if (i == 3)
       {
-        pWidget = Gtk::manage( new Gtk::Image(demo_find_file("floppybuddy.gif")) );
+        Gtk::Image* pImage = Gtk::manage( new Gtk::Image() );
+        pImage->set_from_resource("/textview/floppybuddy.gif");
+        pWidget = pImage;
       }
     else if (i == 4)
     {
diff --git a/demos/pixbuf-demo.cc b/demos/pixbuf-demo.cc
index 15f76ae..127db35 100644
--- a/demos/pixbuf-demo.cc
+++ b/demos/pixbuf-demo.cc
@@ -51,18 +51,18 @@ enum
 };
 
 const char * const background_name =
-  "gtk-demo/background.jpg";
+  "background.jpg";
 
 const char * const image_names[] =
 {
-  "gtk-demo/apple-red.png",
-  "gtk-demo/gnome-applets.png",
-  "gtk-demo/gnome-calendar.png",
-  "gtk-demo/gnome-foot.png",
-  "gtk-demo/gnome-gmush.png",
-  "gtk-demo/gnome-gimp.png",
-  "gtk-demo/gnome-gsame.png",
-  "gtk-demo/gnu-keys.png"
+  "apple-red.png",
+  "gnome-applets.png",
+  "gnome-calendar.png",
+  "gnome-foot.png",
+  "gnome-gmush.png",
+  "gnome-gimp.png",
+  "gnome-gsame.png",
+  "gnu-keys.png"
 };
 
 class DemoRenderArea : public Gtk::DrawingArea
@@ -85,19 +85,21 @@ private:
 
 Glib::RefPtr<Gdk::Pixbuf> create_pixbuf(const std::string& name)
 {
-  return Gdk::Pixbuf::create_from_file(name);
+  std::string resource_name = std::string("/pixbufs/") + name;
+  return Gdk::Pixbuf::create_from_resource(resource_name);
 }
 
 /*
  * Load all image files, create an empty buffer for storing the current frame,
  * and install a timeout handler that will be called periodically.  The show
- * will start as soon as Gtk::Main::run() is invoked.
+ * will start as soon as Gtk::Application::run() is invoked.
  */
 DemoRenderArea::DemoRenderArea()
 :
   frame_num_ (0)
 {
-  background_ = Gdk::Pixbuf::create_from_file(background_name);
+  std::string resource_name = std::string("/pixbufs/") + background_name;
+  background_ = Gdk::Pixbuf::create_from_resource(resource_name);
   std::transform(
       &image_names[0], &image_names[G_N_ELEMENTS(image_names)],
       std::back_inserter(images_),


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