[gtkmm-documentation] ToolPalette example: Skip non-existent and large icons



commit 0ba6c06eb96e780383bf69ab65faae2d200f3c4e
Author: Kjell Ahlstedt <kjell ahlstedt bredband net>
Date:   Wed Dec 4 18:29:13 2013 +0100

    ToolPalette example: Skip non-existent and large icons
    
    * examples/book/toolpalette/canvas.h: CanvasItem ctor: get label, if
    no icon name.
    * examples/book/toolpalette/canvas.cc: Catch Gtk::IconThemeError.
    * examples/book/toolpalette/examplewindow.cc: Make the ToolPalette look
    better by skipping large icons and icons that can't be loaded from
    the current icon theme.
    * docs/tutorial/C/figures/toolpalette.png: Updated.

 docs/tutorial/C/figures/toolpalette.png    |  Bin 75737 -> 53603 bytes
 examples/book/toolpalette/canvas.cc        |   54 +++++++++++++++-------------
 examples/book/toolpalette/canvas.h         |    4 ++-
 examples/book/toolpalette/examplewindow.cc |   42 ++++++++++++++++-----
 4 files changed, 64 insertions(+), 36 deletions(-)
---
diff --git a/docs/tutorial/C/figures/toolpalette.png b/docs/tutorial/C/figures/toolpalette.png
index 095c73a..f800ac3 100644
Binary files a/docs/tutorial/C/figures/toolpalette.png and b/docs/tutorial/C/figures/toolpalette.png differ
diff --git a/examples/book/toolpalette/canvas.cc b/examples/book/toolpalette/canvas.cc
index 707c38a..eef95bd 100644
--- a/examples/book/toolpalette/canvas.cc
+++ b/examples/book/toolpalette/canvas.cc
@@ -21,7 +21,7 @@
 
 Canvas::Canvas()
 : m_drag_data_requested_for_drop(false),
-  m_drop_item()
+  m_drop_item(0)
 {
   set_app_paintable();
 }
@@ -36,8 +36,7 @@ Canvas::~Canvas()
     m_canvas_items.erase(iter);
   }
 
-  if(m_drop_item)
-    delete m_drop_item;
+  delete m_drop_item;
 }
 
 void Canvas::item_draw(const CanvasItem *item,
@@ -112,7 +111,8 @@ bool Canvas::on_drag_motion(const Glib::RefPtr<Gdk::DragContext>& context,
 }
 
 
-void Canvas::on_drag_data_received(const Glib::RefPtr<Gdk::DragContext>& context, int x, int y, const 
Gtk::SelectionData& selection_data, guint info, guint time)
+void Canvas::on_drag_data_received(const Glib::RefPtr<Gdk::DragContext>& context,
+  int x, int y, const Gtk::SelectionData& selection_data, guint info, guint time)
 {
   // Find the tool button which is the source of this DnD operation.
   Gtk::Widget* widget = drag_get_source_widget(context);
@@ -133,34 +133,38 @@ void Canvas::on_drag_data_received(const Glib::RefPtr<Gdk::DragContext>& context
   if(!button)
     return;
 
-  if(m_drop_item)
-  {
-    delete m_drop_item;
-    m_drop_item = 0;
-  }
-
-  CanvasItem* item = new CanvasItem(this, button, x, y);
+  delete m_drop_item;
+  m_drop_item = 0;
 
-  if(m_drag_data_requested_for_drop)
+  try
   {
-    m_canvas_items.push_back(item);
+    CanvasItem* item = new CanvasItem(this, button, x, y);
+
+    if(m_drag_data_requested_for_drop)
+    {
+      m_canvas_items.push_back(item);
+
+      // Signal that the item was accepted and then redraw.
+      context->drag_finish(true /* success */, false /* del */, time);
+    }
+    else
+    {
+      m_drop_item = item;
+
+      // We are getting this data due to a request in drag_motion,
+      // rather than due to a request in drag_drop, so we are just
+      // supposed to call gdk_drag_status (), not actually paste in
+      // the data.
+      context->drag_status(Gdk::ACTION_COPY, time);
+    }
 
-    // Signal that the item was accepted and then redraw.
-    context->drag_finish(true /* success */, false /* del */, time);
+    queue_draw();
   }
-  else
+  catch (const Gtk::IconThemeError& ex)
   {
-    m_drop_item = item;
-
-    // We are getting this data due to a request in drag_motion,
-    // rather than due to a request in drag_drop, so we are just
-    // supposed to call gdk_drag_status (), not actually paste in
-    // the data.
-    context->drag_status(Gdk::ACTION_COPY, time);
+    std::cerr << "IconThemeError: " << ex.what() << std::endl;
   }
 
-  queue_draw();
-
   Gtk::DrawingArea::on_drag_data_received(context, x, y, selection_data, info, time);
 }
 
diff --git a/examples/book/toolpalette/canvas.h b/examples/book/toolpalette/canvas.h
index 0aac921..af655cf 100644
--- a/examples/book/toolpalette/canvas.h
+++ b/examples/book/toolpalette/canvas.h
@@ -37,7 +37,9 @@ private:
   public:
     CanvasItem(Gtk::Widget* canvas, Gtk::ToolButton* button, double x, double y)
     {
-      const Glib::ustring icon_name(button->get_icon_name());
+      Glib::ustring icon_name(button->get_icon_name());
+      if (icon_name.empty())
+        icon_name = button->get_label();
 
       Glib::RefPtr<Gtk::IconTheme> icon_theme = Gtk::IconTheme::get_for_screen(canvas->get_screen());
       int width = 0;
diff --git a/examples/book/toolpalette/examplewindow.cc b/examples/book/toolpalette/examplewindow.cc
index f1c75bd..7baa12f 100644
--- a/examples/book/toolpalette/examplewindow.cc
+++ b/examples/book/toolpalette/examplewindow.cc
@@ -27,7 +27,13 @@ void ExampleWindow::load_icon_items()
   type_stringvec icon_names = icon_theme->list_icons();
 
   // Obtain the names of all contexts, and the icon names per context.
-  const type_stringvec contexts = icon_theme->list_contexts();
+  type_stringvec contexts = icon_theme->list_contexts();
+  std::sort(contexts.begin(), contexts.end());
+
+  int requested_icon_size = 0;
+  int requested_icon_height = 0; //ignored
+  Gtk::IconSize::lookup(Gtk::ICON_SIZE_BUTTON, requested_icon_size, requested_icon_height);
+  const guint max_icons_per_group = 10;
 
   for (type_stringvec::const_iterator iter = contexts.begin(); iter != contexts.end(); ++iter)
   {
@@ -36,23 +42,39 @@ void ExampleWindow::load_icon_items()
       Gtk::manage(new Gtk::ToolItemGroup(context_name));
     m_ToolPalette.add(*group);
 
-    // Iterate through the icon names, populating the ListStore as appropriate.
+    // Iterate through the icon names, populating the ToolItemGroup as appropriate.
     type_stringvec icon_names = icon_theme->list_icons(context_name);
     std::sort(icon_names.begin(), icon_names.end());
-    const guint max_icons = 10;
     guint icons_count = 0;
     for (type_stringvec::const_iterator iconiter = icon_names.begin(); iconiter != icon_names.end(); 
++iconiter)
     {
-      const Glib::ustring id = *iconiter;
-      Gtk::ToolButton* button = Gtk::manage(new Gtk::ToolButton());
-      button->set_icon_name(id);
-      button->set_tooltip_text(id);
+      const Glib::ustring icon_name = *iconiter;
+      Glib::RefPtr<Gdk::Pixbuf> pixbuf;
+      try
+      {
+        pixbuf = icon_theme->load_icon(icon_name, requested_icon_size, Gtk::ICON_LOOKUP_GENERIC_FALLBACK);
+      }
+      catch (const Gtk::IconThemeError& /* ex */)
+      {
+        // Gtk::IconTheme::list_icons() may return some names of icons
+        // that can't be loaded.
+        continue;
+      }
+
+      // Skip large icons, just to make the ToolPalette look better.
+      if (pixbuf->get_width() > 2*requested_icon_size ||
+          pixbuf->get_height() > 2*requested_icon_size)
+        continue;
+
+      Gtk::Image* image = Gtk::manage(new Gtk::Image(pixbuf));
+      Gtk::ToolButton* button = Gtk::manage(new Gtk::ToolButton(*image, icon_name));
+      button->set_tooltip_text(icon_name);
       button->set_is_important();
       group->insert(*button);
 
-      /* Prevent us having an insane number of icons: */
+      // Prevent us having an insane number of icons:
       ++icons_count;
-      if(icons_count >= max_icons)
+      if(icons_count >= max_icons_per_group)
         break;
     }
   }
@@ -153,7 +175,7 @@ void ExampleWindow::load_special_items()
                            NULL);
 
   button = Gtk::manage(new Gtk::ToolButton());
-  button->set_icon_name("help-browser");
+  button->set_icon_name("help-contents");
   button->set_tooltip_text("A regular item");
   group->insert(*button);
 }


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