[gtkmm-documentation] drawingarea/image and menus_and_toolbars examples: Use resource files



commit d149819d0f939532dea3c2393afe3c2cf0971a88
Author: Kjell Ahlstedt <kjell ahlstedt bredband net>
Date:   Tue Dec 30 17:14:38 2014 +0100

    drawingarea/image and menus_and_toolbars examples: Use resource files
    
    * configure.ac: Add check for C compiler and glib-compile-resources.
    * examples/.gitignore: Ignore resources.c.
    * examples/Makefile.am: Add make rules for the resource files.
    * examples/book/drawingarea/image/myarea.cc: Use Gdk::Pixbuf::
    create_from_resource() instead of create_from_file().
    * examples/book/drawingarea/image/image.gresource.xml: New file.
    * examples/book/menus_and_toolbars/examplewindow.cc: Use Gtk::Builder::
    add_from_resource().
    * examples/book/menus_and_toolbars/toolbar.glade:
    * examples/book/menus_and_toolbars/toolbar.gresource.xml: New files.

 configure.ac                                       |    5 ++
 examples/.gitignore                                |    3 +-
 examples/Makefile.am                               |   29 +++++++++--
 .../book/drawingarea/image/image.gresource.xml     |    6 ++
 examples/book/drawingarea/image/myarea.cc          |    7 ++-
 examples/book/menus_and_toolbars/examplewindow.cc  |   52 +-------------------
 examples/book/menus_and_toolbars/toolbar.glade     |   53 ++++++++++++++++++++
 .../book/menus_and_toolbars/toolbar.gresource.xml  |    7 +++
 8 files changed, 103 insertions(+), 59 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 7f1f3b9..3705307 100644
--- a/configure.ac
+++ b/configure.ac
@@ -23,6 +23,11 @@ PKG_CHECK_MODULES([GIOMM], [giomm-2.4 >= 2.40.0])
 PKG_CHECK_MODULES([GTKMM], [gtkmm-3.0 >= 3.12.0])
 MM_PKG_CONFIG_SUBST([GTHREAD_CFLAGS], [--cflags-only-other gthread-2.0])
 
+# glib-compile-resources and C compiler are needed to generate and compile
+# resource files in example programs.
+AC_SUBST(GLIB_COMPILE_RESOURCES, [`$PKG_CONFIG --variable=glib_compile_resources gio-2.0`])
+AC_PROG_CC
+
 AC_LANG([C++])
 AC_CHECK_FUNCS([mkfifo])
 
diff --git a/examples/.gitignore b/examples/.gitignore
index a260b01..d917229 100644
--- a/examples/.gitignore
+++ b/examples/.gitignore
@@ -13,17 +13,18 @@ example
 /book/drawingarea/clock/cairoclock
 /book/drawingarea/curve/drawingareacurve
 /book/drawingarea/image/drawingareaimage
+/book/drawingarea/image/resources.c
 /book/drawingarea/joins/cairojoins
 /book/drawingarea/simple/drawingarea
 /book/giomm/getline/getline
 /book/giomm/monitor_directory/monitor_directory
 /book/giomm/usage/usage
-/boox/grid/example
 /book/helloworld/helloworld
 /book/helloworld2/helloworld2
 /book/idle/idle
 /book/menus/main_menu/main_menu
 /book/menus/popup/popup
+/book/menus_and_toolbars/resources.c
 /book/socket/plug
 /book/socket/socket
 /book/timeout/timeout
diff --git a/examples/Makefile.am b/examples/Makefile.am
index 4b0dfda..8e5c579 100644
--- a/examples/Makefile.am
+++ b/examples/Makefile.am
@@ -131,7 +131,7 @@ dist_noinst_DATA =                          \
        book/builder/derived/derived.glade      \
        book/buttons/button/info.xpm            \
        book/custom/custom_widget/custom_gtk.css        \
-       book/drawingarea/image/fractal_image.png        \
+       $(DRAWINGAREA_IMAGE_RESOURCES) \
        book/iconview/gnome-dice-1.svg          \
        book/iconview/gnome-dice-2.svg          \
        book/iconview/gnome-dice-3.svg          \
@@ -140,7 +140,7 @@ dist_noinst_DATA =                          \
        book/iconview/gnome-dice-6.svg          \
        book/iconview/mozilla-firefox.png       \
        book/iconview/xmms.xpm                  \
-       book/menus_and_toolbars/rain.png
+       $(MENUS_AND_TOOLBARS_RESOURCES)
 
 book_actionbar_example_SOURCES = \
        book/actionbar/examplewindow.cc \
@@ -335,6 +335,7 @@ book_drawingarea_curve_drawingareacurve_SOURCES =   \
        book/drawingarea/curve/myarea.h
 
 book_drawingarea_image_drawingareaimage_SOURCES =      \
+       book/drawingarea/image/resources.c \
        book/drawingarea/image/main.cc                  \
        book/drawingarea/image/myarea.cc                \
        book/drawingarea/image/myarea.h
@@ -506,6 +507,7 @@ book_menus_popup_popup_SOURCES =            \
        book/menus/popup/main.cc
 
 book_menus_and_toolbars_example_SOURCES =      \
+       book/menus_and_toolbars/resources.c \
        book/menus_and_toolbars/examplewindow.cc\
        book/menus_and_toolbars/examplewindow.h \
        book/menus_and_toolbars/main.cc
@@ -739,5 +741,24 @@ others_treemodelcustom_example_SOURCES =           \
        others/treemodelcustom/examplewindow.h          \
        others/treemodelcustom/main.cc
 
-others_window_wheelbarrow_SOURCES = \
-       others/window/wheelbarrow.cc
+#others_window_wheelbarrow_SOURCES = \
+#      others/window/wheelbarrow.cc
+
+MAINTAINERCLEANFILES = \
+  book/drawingarea/image/resources.c \
+  book/menus_and_toolbars/resources.c
+
+DRAWINGAREA_IMAGE_RESOURCES = \
+  book/drawingarea/image/image.gresource.xml \
+  book/drawingarea/image/fractal_image.png
+
+book/drawingarea/image/resources.c: $(DRAWINGAREA_IMAGE_RESOURCES)
+       $(AM_V_GEN) $(GLIB_COMPILE_RESOURCES) --target=$@ --sourcedir=$(dir $<) --generate-source $<
+
+MENUS_AND_TOOLBARS_RESOURCES = \
+  book/menus_and_toolbars/toolbar.gresource.xml \
+  book/menus_and_toolbars/rain.png \
+  book/menus_and_toolbars/toolbar.glade
+
+book/menus_and_toolbars/resources.c: $(MENUS_AND_TOOLBARS_RESOURCES)
+       $(AM_V_GEN) $(GLIB_COMPILE_RESOURCES) --target=$@ --sourcedir=$(dir $<) --generate-source $<
diff --git a/examples/book/drawingarea/image/image.gresource.xml 
b/examples/book/drawingarea/image/image.gresource.xml
new file mode 100644
index 0000000..ce18e70
--- /dev/null
+++ b/examples/book/drawingarea/image/image.gresource.xml
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<gresources>
+  <gresource prefix="/image">
+    <file>fractal_image.png</file>
+  </gresource>
+</gresources>
diff --git a/examples/book/drawingarea/image/myarea.cc b/examples/book/drawingarea/image/myarea.cc
index fb205ed..eec8eee 100644
--- a/examples/book/drawingarea/image/myarea.cc
+++ b/examples/book/drawingarea/image/myarea.cc
@@ -16,6 +16,7 @@
 
 #include "myarea.h"
 #include <cairomm/context.h>
+#include <giomm/resource.h>
 #include <gdkmm/general.h> // set_source_pixbuf()
 #include <glibmm/fileutils.h>
 #include <iostream>
@@ -26,11 +27,11 @@ MyArea::MyArea()
   {
     // The fractal image has been created by the XaoS program.
     // http://xaos.sourceforge.net
-    m_image = Gdk::Pixbuf::create_from_file("fractal_image.png");
+    m_image = Gdk::Pixbuf::create_from_resource("/image/fractal_image.png");
   }
-  catch(const Glib::FileError& ex)
+  catch(const Gio::ResourceError& ex)
   {
-    std::cerr << "FileError: " << ex.what() << std::endl;
+    std::cerr << "ResourceError: " << ex.what() << std::endl;
   }
   catch(const Gdk::PixbufError& ex)
   {
diff --git a/examples/book/menus_and_toolbars/examplewindow.cc 
b/examples/book/menus_and_toolbars/examplewindow.cc
index 010c497..c2775a5 100644
--- a/examples/book/menus_and_toolbars/examplewindow.cc
+++ b/examples/book/menus_and_toolbars/examplewindow.cc
@@ -105,57 +105,6 @@ ExampleWindow::ExampleWindow(const Glib::RefPtr<Gtk::Application>& app)
     "      </item>"
     "    </submenu>"
     "  </menu>"
-    ""
-    "<!-- Generated with glade 3.18.3 -->"
-    "  <requires lib='gtk+' version='3.4'/>"
-    "  <object class='GtkImage' id='image_rain'>"
-    "    <property name='visible'>True</property>"
-    "    <property name='can_focus'>False</property>"
-    "    <property name='pixbuf'>rain.png</property>"
-    "  </object>"
-    "  <object class='GtkToolbar' id='toolbar'>"
-    "    <property name='visible'>True</property>"
-    "    <property name='can_focus'>False</property>"
-    "    <child>"
-    "      <object class='GtkToolButton' id='toolbutton_new'>"
-    "        <property name='visible'>True</property>"
-    "        <property name='can_focus'>False</property>"
-    "        <property name='tooltip_text' translatable='yes'>New</property>"
-    "        <property name='action_name'>example.new</property>"
-    "        <property name='icon_name'>document-new</property>"
-    "      </object>"
-    "      <packing>"
-    "        <property name='expand'>False</property>"
-    "        <property name='homogeneous'>True</property>"
-    "      </packing>"
-    "    </child>"
-    "    <child>"
-    "      <object class='GtkToggleToolButton' id='toolbutton_rain'>"
-    "        <property name='visible'>True</property>"
-    "        <property name='can_focus'>False</property>"
-    "        <property name='tooltip_text' translatable='yes'>Stay dry in the rain</property>"
-    "        <property name='action_name'>example.rain</property>"
-    "        <property name='label_widget'>image_rain</property>"
-    "      </object>"
-    "      <packing>"
-    "        <property name='expand'>False</property>"
-    "        <property name='homogeneous'>True</property>"
-    "      </packing>"
-    "    </child>"
-    "    <child>"
-    "      <object class='GtkToolButton' id='toolbutton_quit'>"
-    "        <property name='visible'>True</property>"
-    "        <property name='can_focus'>False</property>"
-    "        <property name='tooltip_text' translatable='yes'>Quit</property>"
-    "        <property name='action_name'>example.quit</property>"
-    "        <property name='icon_name'>application-exit</property>"
-    "      </object>"
-    "      <packing>"
-    "        <property name='expand'>False</property>"
-    "        <property name='homogeneous'>True</property>"
-    "      </packing>"
-    "    </child>"
-    "  </object>"
     "</interface>";
 
   // When the menubar is a child of a Gtk::Window, keyboard accelerators are not
@@ -173,6 +122,7 @@ ExampleWindow::ExampleWindow(const Glib::RefPtr<Gtk::Application>& app)
   try
   {
     m_refBuilder->add_from_string(ui_info);
+    m_refBuilder->add_from_resource("/toolbar/toolbar.glade");
   }
   catch(const Glib::Error& ex)
   {
diff --git a/examples/book/menus_and_toolbars/toolbar.glade b/examples/book/menus_and_toolbars/toolbar.glade
new file mode 100644
index 0000000..65cc92a
--- /dev/null
+++ b/examples/book/menus_and_toolbars/toolbar.glade
@@ -0,0 +1,53 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!-- Generated with glade 3.18.3 -->
+<interface>
+  <requires lib="gtk+" version="3.8"/>
+  <object class="GtkImage" id="image_rain">
+    <property name="visible">True</property>
+    <property name="can_focus">False</property>
+    <property name="resource">/toolbar/rain.png</property>
+  </object>
+  <object class="GtkToolbar" id="toolbar">
+    <property name="visible">True</property>
+    <property name="can_focus">False</property>
+    <child>
+      <object class="GtkToolButton" id="toolbutton_new">
+        <property name="visible">True</property>
+        <property name="can_focus">False</property>
+        <property name="tooltip_text" translatable="yes">New</property>
+        <property name="action_name">example.new</property>
+        <property name="icon_name">document-new</property>
+      </object>
+      <packing>
+        <property name="expand">False</property>
+        <property name="homogeneous">True</property>
+      </packing>
+    </child>
+    <child>
+      <object class="GtkToggleToolButton" id="toolbutton_rain">
+        <property name="visible">True</property>
+        <property name="can_focus">False</property>
+        <property name="tooltip_text" translatable="yes">Stay dry in the rain</property>
+        <property name="action_name">example.rain</property>
+        <property name="label_widget">image_rain</property>
+      </object>
+      <packing>
+        <property name="expand">False</property>
+        <property name="homogeneous">True</property>
+      </packing>
+    </child>
+    <child>
+      <object class="GtkToolButton" id="toolbutton_quit">
+        <property name="visible">True</property>
+        <property name="can_focus">False</property>
+        <property name="tooltip_text" translatable="yes">Quit</property>
+        <property name="action_name">example.quit</property>
+        <property name="icon_name">application-exit</property>
+      </object>
+      <packing>
+        <property name="expand">False</property>
+        <property name="homogeneous">True</property>
+      </packing>
+    </child>
+  </object>
+</interface>
diff --git a/examples/book/menus_and_toolbars/toolbar.gresource.xml 
b/examples/book/menus_and_toolbars/toolbar.gresource.xml
new file mode 100644
index 0000000..8c1a5eb
--- /dev/null
+++ b/examples/book/menus_and_toolbars/toolbar.gresource.xml
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<gresources>
+  <gresource prefix="/toolbar">
+    <file preprocess="xml-stripblanks">toolbar.glade</file>
+    <file>rain.png</file>
+  </gresource>
+</gresources>


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