Re: Non-stock icons in toolbar/menu?



ons 2004-11-24 klockan 09:32 +0100 skrev Murray Cumming:
> That would be nice. You could add a non-stock image to the existing example:
> http://cvs.gnome.org/viewcvs/gtkmm/examples/book/menus_and_toolbars/

Attaching a patch. Also attaching a silly icon. The C source code
version was created with:
gdk-pixbuf-csource --raw --name rain rain.png > rain.h

-- 
Fredrik Arnerup <e97_far e kth se>
http://www.stacken.kth.se/~foo/
? foo
? icon.patch
? rain.h
? rain.png
? rain.xcf
Index: examplewindow.cc
===================================================================
RCS file: /cvs/gnome/gtkmm/examples/book/menus_and_toolbars/examplewindow.cc,v
retrieving revision 1.2
diff -u -r1.2 examplewindow.cc
--- examplewindow.cc	27 Mar 2004 00:37:28 -0000	1.2
+++ examplewindow.cc	24 Nov 2004 18:29:43 -0000
@@ -18,6 +18,7 @@
 
 #include "examplewindow.h"
 #include <gtkmm/stock.h>
+#include <gtkmm/toggleaction.h>
 #include <iostream>
 
 ExampleWindow::ExampleWindow()
@@ -35,6 +36,9 @@
     sigc::mem_fun(*this, &ExampleWindow::on_action_file_new) );
   m_refActionGroup->add( Gtk::Action::create("Open", Gtk::Stock::OPEN),
     sigc::mem_fun(*this, &ExampleWindow::on_action_others) );
+  m_refActionGroup->add( Gtk::ToggleAction::create("Rain",
+                                                   Gtk::StockID("rain") ),
+    sigc::mem_fun(*this, &ExampleWindow::on_action_others) );
   m_refActionGroup->add( Gtk::Action::create("Quit", Gtk::Stock::QUIT),
     sigc::mem_fun(*this, &ExampleWindow::on_action_file_quit) );
 
@@ -61,6 +65,8 @@
         "      <menuitem action='New'/>"
         "      <menuitem action='Open'/>"
         "      <separator/>"
+        "      <menuitem action='Rain'/>"
+        "      <separator/>"
         "      <menuitem action='Quit'/>"
         "    </menu>"
         "    <menu action='MenuEdit'>"
@@ -71,6 +77,7 @@
         "  </menubar>"
         "  <toolbar  name='ToolBar'>"
         "    <toolitem action='Open'/>"
+        "    <toolitem action='Rain'/>"
         "    <toolitem action='Quit'/>"
         "  </toolbar>"
         "</ui>";
Index: main.cc
===================================================================
RCS file: /cvs/gnome/gtkmm/examples/book/menus_and_toolbars/main.cc,v
retrieving revision 1.1
diff -u -r1.1 main.cc
--- main.cc	27 Mar 2004 00:30:36 -0000	1.1
+++ main.cc	24 Nov 2004 18:29:43 -0000
@@ -16,13 +16,45 @@
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  */
 
+#include <gtkmm/iconfactory.h>
+#include <gtkmm/iconset.h>
+#include <gtkmm/iconsource.h>
+#include <gtkmm/stock.h>
+#include <gtkmm/stockitem.h>
 #include <gtkmm/main.h>
 #include "examplewindow.h"
+#include "rain.h" //Image created with 'gdk-pixbuf-csource --raw'.
+
+namespace
+{
+  void add_stock_item(Glib::RefPtr<Gtk::IconFactory> factory,
+                      const guint8 *data, int data_size,
+                      const Glib::ustring &id, const Glib::ustring &label)
+  {
+    Gtk::IconSource source;
+    source.set_pixbuf(Gdk::Pixbuf::create_from_inline(data_size, data));
+    source.set_size(Gtk::ICON_SIZE_SMALL_TOOLBAR);
+    source.set_size_wildcarded(); //Icon may be scaled.
+    Gtk::IconSet set;
+    set.add_source(source); //More than on source per set is allowed.
+    const Gtk::StockID stock_id(id);
+    factory->add(stock_id, set);
+    Gtk::Stock::add(Gtk::StockItem(stock_id, label));
+  }
+
+  void register_stock_items()
+  {
+    const Glib::RefPtr<Gtk::IconFactory> factory = Gtk::IconFactory::create();
+    add_stock_item(factory, rain, sizeof(rain), "rain", "Stay dry");
+    factory->add_default(); //Add factory to list of factories.
+  }
+}
 
 int main(int argc, char *argv[])
 {
   Gtk::Main kit(argc, argv);
 
+  register_stock_items(); //Makes the "rain" stock item available.
   ExampleWindow window;
   Gtk::Main::run(window); //Shows the window and returns when it is closed.
 
/* GdkPixbuf RGBA C-Source image dump */

#ifdef __SUNPRO_C
#pragma align 4 (rain)
#endif
#ifdef __GNUC__
static const guint8 rain[] __attribute__ ((__aligned__ (4))) = 
#else
static const guint8 rain[] = 
#endif
{ ""
  /* Pixbuf magic (0x47646b50) */
  "GdkP"
  /* length: header (24) + pixel_data (2304) */
  "\0\0\11\30"
  /* pixdata_type (0x1010002) */
  "\1\1\0\2"
  /* rowstride (96) */
  "\0\0\0`"
  /* width (24) */
  "\0\0\0\30"
  /* height (24) */
  "\0\0\0\30"
  /* pixel_data: */
  "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
  "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
  "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
  "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
  "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
  "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
  "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
  "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
  "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
  "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\377\0\0\0"
  "\16\0\0\0\3\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
  "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
  "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\377\0\0\0G\0\0\0\21"
  "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
  "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\22"
  "\0\0\0""7\0\0\0\232\0\0\0\377\0\0\0\377\0\0\0\377\0\0\0\377\0\0\0\377"
  "\0\0\0\240\0\0\0 \0\0\0\27\0\0\0\1\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
  "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\36-P-\377"
  "G\177G\377-P-\377G\177G\377-P-\377Y\237Y\377-P-\377G\177G\377-P-\377"
  "G\177G\377-P-\377\0\0\0""4\0\0\0\6\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
  "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\36-P-\377G\177G\377-P-\377"
  "Y\237Y\377-P-\377Y\237Y\377Y\237Y\377Y\237Y\377-P-\377Y\237Y\377-P-\377"
  "G\177G\377-P-\377\0\0\0 \0\0\0\7\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
  "\0\0\0\0\0\0\0\0\0\0\0\0\0\36-P-\377G\177G\377-P-\377Y\237Y\377G\177"
  "G\377-P-\377Y\237Y\377Y\237Y\377Y\237Y\377-P-\377G\177G\377Y\237Y\377"
  "-P-\377G\177G\377-P-\377\0\0\0 \0\0\0\7\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
  "\0\0\0\0\0\0\0\0\0\0\0\0\232Y\237Y\3775_5\377Y\237Y\377Y\237Y\377-P-"
  "\377Y\237Y\377Y\237Y\377Y\237Y\377Y\237Y\377Y\237Y\377-P-\377Y\237Y\377"
  "Y\237Y\3775_5\377Y\237Y\377\0\0\0\300\0\0\0!\0\0\0\4\0\0\0\0\0\0\0\0"
  "\0\0\0\0\0\0\0\0\0\0\0""4\0\0\0\377Y\237Y\377\0\0\0\377Y\237Y\377Y\237"
  "Y\377\0\0\0\377Y\237Y\377Y\237Y\377Y\237Y\377Y\237Y\377Y\237Y\377\0\0"
  "\0\377Y\237Y\377Y\237Y\377\0\0\0\377Y\237Y\377\0\0\0\377\0\0\0m\0\0\0"
  "\17\0\0\0\1\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\232\0\0\0$\0\0\0X\0\0\0\304"
  "\0\0\0t\0\0\0m\0\0\0\304\0\0\0t\0\0\0m\0\0\0\377\0\0\0y\0\0\0o\0\0\0"
  "\304\0\0\0t\0\0\0m\0\0\0\304\0\0\0t\0\0\0l\0\0\0\274\0\0\0$\0\0\0\5\0"
  "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\11\0\0\0(\0\0\0\32\0\0\0\35\0\0\0""8\0\0"
  "\0\35\0\0\0\36\0\0\0""8\0\0\0\35\0\0\0\377\0\0\0]\0\0\0'\0\0\0\36\0\0"
  "\0""8\0\0\0\35\0\0\0\36\0\0\0""8\0\0\0\35\0\0\0\33\0\0\0(\0\0\0\11\0"
  "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\2\0\0\0\10\0\0\0\2\0\0\0\2\0\0\0\10\0\0"
  "\0\2\0\0\0\2\0\0\0\10\0\0\0\2\0\0\0\377\0\0\0V\0\0\0\25\0\0\0\2\0\0\0"
  "\10\0\0\0\2\0\0\0\2\0\0\0\10\0\0\0\2\0\0\0\2\0\0\0\10\0\0\0\2\0\0\0\0"
  "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
  "\0\0\0\0\0\0\0\0\0\0\0\0\0\377\0\0\0V\0\0\0\25\0\0\0\0\0\0\0\0\0\0\0"
  "\0\0\0\0\0\0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0\0\0\0\0\0\0\0\0\0\0\0\0"
  "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
  "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\377\0\0\0\224\0\0\0\32\0\0\0\377\0"
  "\0\0G\0\0\0\21\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
  "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
  "\0\0\0\0\0\0\0\0\0\0\0\0\0\0E\0\0\0\377\0\0\0\377\0\0\0W\0\0\0N\0\0\0"
  "\22\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
  "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
  "\0\0\0\0\0\0\0\0\7\0\0\0*\0\0\0S\0\0\0O\0\0\0)\0\0\0\7\0\0\0\0\0\0\0"
  "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
  "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
  "\1\0\0\0\6\0\0\0\22\0\0\0\22\0\0\0\7\0\0\0\1\0\0\0\0\0\0\0\0\0\0\0\0"
  "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"};


Attachment: rain.png
Description: PNG image



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