This patch does two things:
-Add getGTKStockIcon function for using gtk stock icons where available
in menus and toolbars.
-Put icons next to command items in menus.

Note that GMenubar.C now includes ToolbarBackend.h for the getIcon
function.  Perhaps this function should be moved somewhere more general?

John
? tmp
Index: ChangeLog
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/frontends/gtk/ChangeLog,v
retrieving revision 1.83
diff -u -p -r1.83 ChangeLog
--- ChangeLog	14 Nov 2004 18:26:16 -0000	1.83
+++ ChangeLog	15 Nov 2004 00:50:06 -0000
@@ -1,3 +1,10 @@
+2004-11-15  John Spray  <[EMAIL PROTECTED]>
+
+	* ghelpers.[Ch]: getGTKStockIcon added to choose gtk
+		stock icons for FuncRequests
+	* GToolbar.C: use getGTKStockIcon for toolbutton icons
+	* GMenubar.C: add icons to menu items
+
 2004-11-14  John Spray  <[EMAIL PROTECTED]>
 
 	* The ERT dialog:
Index: GMenubar.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/frontends/gtk/GMenubar.C,v
retrieving revision 1.18
diff -u -p -r1.18 GMenubar.C
--- GMenubar.C	8 Nov 2004 10:54:29 -0000	1.18
+++ GMenubar.C	15 Nov 2004 00:50:06 -0000
@@ -4,6 +4,7 @@
  * Licence details can be found in the file COPYING.
  *
  * \author Huang Ying
+ * \author John Spray
  *
  * Full author contact details are available in file CREDITS.
  */
@@ -12,6 +13,10 @@
 
 #include "GMenubar.h"
 #include "GView.h"
+#include "ghelpers.h"
+
+#include "ToolbarBackend.h" // for getIcon
+
 #include "debug.h"
 #include "lyxfunc.h"
 
@@ -133,6 +138,11 @@ void GMenubar::onSubMenuActivate(MenuIte
 		item->submenu() :
 		&menubackend.getMenu(item->submenuname());
 
+	// Choose size for icons on command items
+	int iconwidth = 16;
+	int iconheight = 16;
+	Gtk::IconSize::lookup(Gtk::ICON_SIZE_MENU, iconwidth, iconheight);
+
 	menubackend.expand(*fmenu, lyxmenu->getBackMenu(), view_);
 	Menu::const_iterator i = lyxmenu->getBackMenu().begin();
 	Menu::const_iterator end = lyxmenu->getBackMenu().end();
@@ -168,10 +178,30 @@ void GMenubar::onSubMenuActivate(MenuIte
 						gmenu->items().back());
 				citem.set_active(on);
 			} else {
-				// This is necessary because add_accel_label is protected,
+				// Choose an icon from the funcrequest
+				Gtk::BuiltinStockID stockID = getGTKStockIcon(i->func());
+				Glib::ustring xpmName =
+					Glib::locale_to_utf8(toolbarbackend.getIcon(i->func()));
+				Gtk::Image * image = NULL;
+				// Prefer stock graphics
+				if (stockID != Gtk::Stock::MISSING_IMAGE) {
+					image = Gtk::manage(new Gtk::Image(stockID, Gtk::ICON_SIZE_MENU));
+				} else if (xpmName.find("unknown.xpm") == -1) {
+					// Load icon and shrink it for menu size
+					Glib::RefPtr<Gdk::Pixbuf> bigicon =
+						Gdk::Pixbuf::create_from_file(xpmName);
+					Glib::RefPtr<Gdk::Pixbuf> smallicon =
+						bigicon->scale_simple(iconwidth,iconheight,Gdk::INTERP_TILES);
+					image = Gtk::manage(new Gtk::Image(smallicon));
+				}
+
+				Gtk::ImageMenuItem * item = Gtk::manage(new Gtk::ImageMenuItem);
+				if (image)
+					item->set_image(*image);
+
+				// This hbox is necessary because add_accel_label is protected,
 				// and even if you subclass Gtk::MenuItem then add_accel_label
 				// doesn't do what you'd expect.
-				Gtk::MenuItem * item = Gtk::manage(new Gtk::MenuItem);
 				Gtk::HBox * hbox = Gtk::manage(new Gtk::HBox);
 				Gtk::Label * label1 = Gtk::manage(new Gtk::Label(
 					labelTrans(i->label(), i->shortcut()), true));
Index: GToolbar.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/frontends/gtk/GToolbar.C,v
retrieving revision 1.25
diff -u -p -r1.25 GToolbar.C
--- GToolbar.C	9 Nov 2004 12:40:33 -0000	1.25
+++ GToolbar.C	15 Nov 2004 00:50:06 -0000
@@ -4,6 +4,7 @@
  * Licence details can be found in the file COPYING.
  *
  * \author Huang Ying
+ * \author John Spray
  *
  * Full author contact details are available in file CREDITS.
  */
@@ -13,6 +14,8 @@
 #include "GToolbar.h"
 #include "GView.h"
 
+#include "ghelpers.h"
+
 #include "buffer.h"
 #include "bufferparams.h"
 #include "debug.h"
@@ -219,17 +222,26 @@ void GToolbar::add(FuncRequest const & f
 	}
 
 	default: {
+		// choose an icon from the funcrequest
+		Gtk::BuiltinStockID stockID = getGTKStockIcon(func);
 		Glib::ustring xpmName =
 			Glib::locale_to_utf8(toolbarbackend.getIcon(func));
+
 		Glib::ustring tip = Glib::locale_to_utf8(tooltip);
+
 		Gtk::ToolButton * toolbutton;
-		if (xpmName.size() == 0) {
-			toolbutton = Gtk::manage(new Gtk::ToolButton);
+		if (stockID != Gtk::Stock::MISSING_IMAGE) {
+			// Prefer stock gtk graphics
+			Gtk::IconSize size(Gtk::ICON_SIZE_LARGE_TOOLBAR);
+			Gtk::Image * image = Gtk::manage(new Gtk::Image(stockID, size));
+			image->show();
+			toolbutton = Gtk::manage(new Gtk::ToolButton(*image));
 		} else {
 			Gtk::Image * image = Gtk::manage(new Gtk::Image(xpmName));
 			image->show();
 			toolbutton = Gtk::manage(new Gtk::ToolButton(*image));
 		}
+
 		// This code is putting a function reference into the GObject data field
 		// named gToolData.  That's how we know how to update the status of the
 		// toolitem later.
@@ -237,7 +249,6 @@ void GToolbar::add(FuncRequest const & f
 			reinterpret_cast<void*>(&const_cast<FuncRequest &>(func)));
 
 		toolbutton->set_tooltip(*toolbar_.get_tooltips_object(),tip);
-		/*toolbar_.get_tooltips_object()->set_tip(*toolbutton, tip);*/
 
 		toolbutton->signal_clicked().connect(sigc::bind(sigc::mem_fun(*this,
 			&GToolbar::clicked), FuncRequest(func)));
Index: ghelpers.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/frontends/gtk/ghelpers.C,v
retrieving revision 1.5
diff -u -p -r1.5 ghelpers.C
--- ghelpers.C	14 Nov 2004 17:10:04 -0000	1.5
+++ ghelpers.C	15 Nov 2004 00:50:06 -0000
@@ -4,6 +4,7 @@
  * Licence details can be found in the file COPYING.
  *
  * \author Angus Leeming
+ * \author John Spray
  *
  * Full author contact details are available in file CREDITS.
  */
@@ -13,6 +14,7 @@
 #include "ghelpers.h"
 
 #include "lyxrc.h"
+#include "funcrequest.h"
 #include "debug.h"
 
 #include "support/filetools.h"
@@ -23,6 +25,44 @@ using std::vector;
 
 namespace lyx {
 namespace frontend {
+
+Gtk::BuiltinStockID getGTKStockIcon(FuncRequest const & func)
+{
+	switch (func.action) {
+
+		case LFUN_MENUWRITE: return Gtk::Stock::SAVE;
+		case LFUN_MENUNEW: return Gtk::Stock::NEW;
+		case LFUN_WRITEAS: return Gtk::Stock::SAVE_AS;
+
+		case LFUN_CENTER: return Gtk::Stock::JUSTIFY_CENTER;
+		case LFUN_TOCVIEW: return Gtk::Stock::INDEX;
+		case LFUN_CLOSEBUFFER: return Gtk::Stock::CLOSE;
+		case LFUN_QUIT: return Gtk::Stock::QUIT;
+		case LFUN_UNDO: return Gtk::Stock::UNDO;
+		case LFUN_REDO: return Gtk::Stock::REDO;
+		case LFUN_PASTE: return Gtk::Stock::PASTE;
+		case LFUN_PASTESELECTION: return Gtk::Stock::PASTE;
+		case LFUN_CUT: return Gtk::Stock::CUT;
+		case LFUN_COPY: return Gtk::Stock::COPY;
+		case LFUN_BOLD: return Gtk::Stock::BOLD;
+		case LFUN_ITAL: return Gtk::Stock::ITALIC;
+		case LFUN_FILE_OPEN: return Gtk::Stock::OPEN;
+		case LFUN_RECONFIGURE: return Gtk::Stock::REFRESH;
+		case LFUN_DIALOG_SHOW:
+			if (func.argument == "findreplace")
+				return Gtk::Stock::FIND_AND_REPLACE;
+			else if (func.argument == "print")
+				return Gtk::Stock::PRINT;
+			else if (func.argument == "spellchecker")
+				return Gtk::Stock::SPELL_CHECK;
+			else if (func.argument == "prefs")
+				return Gtk::Stock::PREFERENCES;
+			else
+				return Gtk::Stock::MISSING_IMAGE;
+			break;
+		default: return Gtk::Stock::MISSING_IMAGE;
+	}
+}
 
 string const getDefaultUnit()
 {
Index: ghelpers.h
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/frontends/gtk/ghelpers.h,v
retrieving revision 1.5
diff -u -p -r1.5 ghelpers.h
--- ghelpers.h	14 Nov 2004 17:10:04 -0000	1.5
+++ ghelpers.h	15 Nov 2004 00:50:06 -0000
@@ -19,8 +19,14 @@
 #include <string>
 #include <vector>
 
+class FuncRequest;
+
 namespace lyx {
 namespace frontend {
+
+// Get a GTK stockID from a lyx function id.
+// Return Gtk::Stock::MISSING_IMAGE if no suitable stock found
+Gtk::BuiltinStockID getGTKStockIcon(FuncRequest const & func);
 
 std::string const getDefaultUnit();
 

Reply via email to