Title: assign icon to command sequences in toolbar

in case people are interested, with the attached patch i can assign icons to command sequences in my toolbar using an extra item entry in stdtoolbars.inc.

regards, edwin.

? tb.diff
Index: lib/ui/stdtoolbars.ui
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/lib/ui/stdtoolbars.ui,v
retrieving revision 1.19
diff -u -p -r1.19 stdtoolbars.ui
--- lib/ui/stdtoolbars.ui	22 May 2005 23:01:21 -0000	1.19
+++ lib/ui/stdtoolbars.ui	31 Jan 2006 16:33:59 -0000
@@ -18,11 +18,11 @@
 #
 # Only four commands are allowed inside the begin_toolbar and end_toolbar
 # directives:
-#   Item "The tooltip" "<action> [<parameter>]" adds an icon to the toolbar performing
+#   Item "The tooltip" "<action> [<parameter>]" "icon" adds an icon to the toolbar performing
 #          "<action> <parameter>"
 #      Examples:
-#      Item "Small font" "font-size small"
-#      Item Emphasized set-emph
+#      Item "Small font" "font-size small" "font-size small"
+#      Item "Small font" "command-sequence float-insert table;self-insert Change this!;break-paragraph;tabular-insert 5 5" "custom1"
 #
 #   Layouts adds the layouts combo-box to the toolbar
 #
@@ -41,104 +41,104 @@
 
 Toolbar "standard" "Standard"
 	Layouts
-	Item "New document" "buffer-new"
-	Item "Open document" "file-open"
-	Item "Save document" "buffer-write"
-	Item "Print document" "dialog-show print"
-	Separator
-	Item "Undo" "undo"
-	Item "Redo" "redo"
-	Item "Cut" "cut"
-	Item "Copy" "copy"
-	Item "Paste" "paste"
-	Item "Find and replace" "dialog-show findreplace"
-	Separator
-	Item "Toggle emphasis" "font-emph"
-	Item "Toggle noun" "font-noun"
-	Item "Apply last" "font-free-apply"
-	Separator
-	Item "Insert math" "math-mode"
-	Item "Insert graphics" "dialog-show-new-inset graphics"
-	Item "Insert table" "tabular-insert"
+	Item "New document" "buffer-new" "buffer-new"
+	Item "Open document" "file-open" "file-open"
+	Item "Save document" "buffer-write" "buffer-write"
+	Item "Print document" "dialog-show print" "dialog-show print"
+	Separator
+	Item "Undo" "undo" "undo"
+	Item "Redo" "redo" "redo"
+	Item "Cut" "cut" "cut"
+	Item "Copy" "copy" "copy"
+	Item "Paste" "paste" "paste"
+	Item "Find and replace" "dialog-show findreplace" "dialog-show findreplace"
+	Separator
+	Item "Toggle emphasis" "font-emph" "font-emph"
+	Item "Toggle noun" "font-noun" "font-noun"
+	Item "Apply last" "font-free-apply" "font-free-apply"
+	Separator
+	Item "Insert math" "math-mode" "math-mode"
+	Item "Insert graphics" "dialog-show-new-inset graphics" "dialog-show-new-inset graphics"
+	Item "Insert table" "tabular-insert" "tabular-insert"
 End
 
 Toolbar "extra" "Extra"
-	Item "Numbered list" "layout Enumerate"
-	Item "Itemized list" "layout Itemize"
-	Item "List" "layout List"
-	Item "Description" "layout Description"
-	Item "Increase depth" "depth-increment"
-	Item "Decrease depth" "depth-decrement"
-	Separator
-	Item "Insert figure float" "float-insert figure"
-	Item "Insert table float" "float-insert table"
-	Item "Insert label" "label-insert"
-	Item "Insert cross-reference" "dialog-show-new-inset ref"
-	Item "Insert citation" "dialog-show-new-inset citation"
-	Item "Insert index entry" "index-insert"
-	Separator
-	Item "Insert footnote" "footnote-insert"
-	Item "Insert margin note" "marginalnote-insert"
-	Item "Insert note" "note-insert"
-	Item "Insert URL" "url-insert"
-	Item "Insert TeX Code" "ert-insert"
-	Item "Include file" "dialog-show-new-inset include"
-	Separator
-	Item "Text style" "dialog-show character"
-	Item "Paragraph settings" "layout-paragraph"
-	Item "Table of contents" "toc-view"
-	Item "Check spelling" "dialog-show spellchecker"
-	Item "Thesaurus" "thesaurus-entry"
+	Item "Numbered list" "layout Enumerate" "layout Enumerate"
+	Item "Itemized list" "layout Itemize" "layout Itemize"
+	Item "List" "layout List" "layout List"
+	Item "Description" "layout Description" "layout Description"
+	Item "Increase depth" "depth-increment" "depth-increment"
+	Item "Decrease depth" "depth-decrement" "depth-decrement"
+	Separator
+	Item "Insert figure float" "float-insert figure" "float-insert figure"
+	Item "Insert table float" "float-insert table" "float-insert table"
+	Item "Insert label" "label-insert" "label-insert"
+	Item "Insert cross-reference" "dialog-show-new-inset ref" "dialog-show-new-inset ref"
+	Item "Insert citation" "dialog-show-new-inset citation" "dialog-show-new-inset citation"
+	Item "Insert index entry" "index-insert" "index-insert"
+	Separator
+	Item "Insert footnote" "footnote-insert" "footnote-insert"
+	Item "Insert margin note" "marginalnote-insert" "marginalnote-insert"
+	Item "Insert note" "note-insert" "note-insert"
+	Item "Insert URL" "url-insert" "url-insert"
+	Item "Insert TeX Code" "ert-insert" "ert-insert"
+	Item "Include file" "dialog-show-new-inset include" "dialog-show-new-inset include"
+	Separator
+	Item "Text style" "dialog-show character" "dialog-show character"
+	Item "Paragraph settings" "layout-paragraph" "layout-paragraph"
+	Item "Table of contents" "toc-view" "toc-view"
+	Item "Check spelling" "dialog-show spellchecker" "dialog-show spellchecker"
+	Item "Thesaurus" "thesaurus-entry" "thesaurus-entry"
 End
 
 Toolbar "table" "Table"
-	Item "Add row" "tabular-feature append-row"
-	Item "Add column" "tabular-feature append-column"
-	Item "Delete row" "tabular-feature delete-row"
-	Item "Delete column" "tabular-feature delete-column"
-	Separator
-	Item "Set top line" "tabular-feature toggle-line-top"
-	Item "Set bottom line" "tabular-feature toggle-line-bottom"
-	Item "Set left line" "tabular-feature toggle-line-left"
-	Item "Set right line" "tabular-feature toggle-line-right"
-	Item "Set all lines" "tabular-feature set-all-lines"
-	Item "Unset all lines" "tabular-feature unset-all-lines"
-	Separator
-	Item "Align left" "tabular-feature align-left"
-	Item "Align center" "tabular-feature align-center"
-	Item "Align right" "tabular-feature align-right"
-	Separator
-	Item "Align top" "tabular-feature valign-top"
-	Item "Align middle" "tabular-feature valign-middle"
-	Item "Align bottom" "tabular-feature valign-bottom"
-	Separator
-	Item "Rotate cell" "tabular-feature set-rotate-cell"
-	Item "Rotate table" "tabular-feature set-rotate-tabular"
-	Item "Set multi-column" "tabular-feature multicolumn"
+	Item "Add row" "tabular-feature append-row" "tabular-feature append-row"
+	Item "Add column" "tabular-feature append-column" "tabular-feature append-column"
+	Item "Delete row" "tabular-feature delete-row" "tabular-feature delete-row"
+	Item "Delete column" "tabular-feature delete-column" "tabular-feature delete-column"
+	Separator
+	Item "Set top line" "tabular-feature toggle-line-top" "tabular-feature toggle-line-top"
+	Item "Set bottom line" "tabular-feature toggle-line-bottom" "tabular-feature toggle-line-bottom"
+	Item "Set left line" "tabular-feature toggle-line-left" "tabular-feature toggle-line-left"
+	Item "Set right line" "tabular-feature toggle-line-right" "tabular-feature toggle-line-right"
+	Item "Set all lines" "tabular-feature set-all-lines" "tabular-feature set-all-lines"
+	Item "Unset all lines" "tabular-feature unset-all-lines" "tabular-feature unset-all-lines"
+	Separator
+	Item "Align left" "tabular-feature align-left" "tabular-feature align-left"
+	Item "Align center" "tabular-feature align-center" "tabular-feature align-center"
+	Item "Align right" "tabular-feature align-right" "tabular-feature align-right"
+	Separator
+	Item "Align top" "tabular-feature valign-top" "tabular-feature valign-top"
+	Item "Align middle" "tabular-feature valign-middle" "tabular-feature valign-middle"
+	Item "Align bottom" "tabular-feature valign-bottom" "tabular-feature valign-bottom"
+	Separator
+	Item "Rotate cell" "tabular-feature set-rotate-cell" "tabular-feature set-rotate-cell"
+	Item "Rotate table" "tabular-feature set-rotate-tabular" "tabular-feature set-rotate-tabular"
+	Item "Set multi-column" "tabular-feature multicolumn" "tabular-feature multicolumn"
 End
 
 Toolbar "math" "Math"
-	Item "Show math panel" "dialog-show mathpanel"
-	Item "Set display mode" "math-display"
-	Item "Subscript" "math-subscript"
-	Item "Superscript" "math-superscript"
-	Item "Insert root" "math-insert \root"
-	Item "Insert square root" "math-insert \sqrt"
-	Item "Insert sum" "math-insert \sum"
-	Item "Insert integral" "math-insert \int"
-	Item "Insert product" "math-insert \prod"
-	Separator
-	Item "Insert fraction" "math-insert \frac"
-	Item "Insert ( )" "math-delim ( )"
-	Item "Insert [ ]" "math-delim [ ]"
-	Item "Insert { }" "math-delim { }"
-	Separator
-	Item "Insert matrix" "math-matrix 2 2"
-	Item "Insert cases" "math-insert \cases"
-	Item "Add row" "tabular-feature append-row"
-	Item "Add column" "tabular-feature append-column"
-	Item "Delete row" "tabular-feature delete-row"
-	Item "Delete column" "tabular-feature delete-column"
+	Item "Show math panel" "dialog-show mathpanel" "dialog-show mathpanel"
+	Item "Set display mode" "math-display" "math-display"
+	Item "Subscript" "math-subscript" "math-subscript"
+	Item "Superscript" "math-superscript" "math-superscript"
+	Item "Insert root" "math-insert \root" "math-insert \root"
+	Item "Insert square root" "math-insert \sqrt" "math-insert \sqrt"
+	Item "Insert sum" "math-insert \sum" "math-insert \sum"
+	Item "Insert integral" "math-insert \int" "math-insert \int"
+	Item "Insert product" "math-insert \prod" "math-insert \prod"
+	Separator
+	Item "Insert fraction" "math-insert \frac" "math-insert \frac"
+	Item "Insert ( )" "math-delim ( )" "math-delim ( )"
+	Item "Insert [ ]" "math-delim [ ]" "math-delim [ ]"
+	Item "Insert { }" "math-delim { }" "math-delim { }"
+	Separator
+	Item "Insert matrix" "math-matrix 2 2" "math-matrix 2 2"
+	Item "Insert cases" "math-insert \cases" "math-insert \cases"
+	Item "Add row" "tabular-feature append-row" "tabular-feature append-row"
+	Item "Add column" "tabular-feature append-column" "tabular-feature append-column"
+	Item "Delete row" "tabular-feature delete-row" "tabular-feature delete-row"
+	Item "Delete column" "tabular-feature delete-column" "tabular-feature delete-column"
 End
 
 Toolbar "minibuffer" "Command Buffer"
Index: src/ToolbarBackend.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/ToolbarBackend.C,v
retrieving revision 1.26
diff -u -p -r1.26 ToolbarBackend.C
--- src/ToolbarBackend.C	8 Nov 2004 10:54:27 -0000	1.26
+++ src/ToolbarBackend.C	31 Jan 2006 16:33:59 -0000
@@ -103,7 +103,11 @@ void ToolbarBackend::read(LyXLex & lex)
 
 				FuncRequest func =
 					lyxaction.lookupFunc(func_arg);
-				add(tb, func, tooltip);
+				lex.next(true);
+				string const icon = lex.getString();
+				lyxerr[Debug::PARSER] << "icon: " << icon << endl;
+
+				add(tb, func, icon, tooltip);
 			}
 			break;
 
@@ -204,37 +208,39 @@ void ToolbarBackend::readToolbars(LyXLex
 
 
 void ToolbarBackend::add(Toolbar & tb,
-			 FuncRequest const & func, string const & tooltip)
+			 FuncRequest const & func, string const & icon, string const & tooltip)
 {
-	tb.items.push_back(make_pair(func, tooltip));
-	tb.items.back().first.origin = FuncRequest::UI;
+	Item item;
+	item.action = func;
+	item.action.origin = FuncRequest::UI;
+	item.icon = icon;
+	item.tooltip = tooltip;
+	tb.items.push_back(item);
 }
 
 
-string const ToolbarBackend::getIcon(FuncRequest const & f)
+string const ToolbarBackend::getIcon(FuncRequest const & f, std::string const & icon)
 {
 	using lyx::frontend::find_xpm;
 
 	string fullname;
 
 	if (f.action == LFUN_INSERT_MATH) {
-		if (!f.argument.empty())
+		if (!f.argument.empty()) 
 			fullname = find_xpm(f.argument.substr(1));
 	} else if (f.action == LFUN_MATH_DELIM) {
 		fullname = find_xpm(f.argument);
-	} else {
+	} else fullname = LibFileSearch("images", icon, "xpm");
+
+	// if icon cannot be found, fall back on the action name
+	if (fullname.empty()) {
 		string const name = lyxaction.getActionName(f.action);
 		string xpm_name(name);
 
-		if (!f.argument.empty())
+		if (!f.argument.empty()) {
 			xpm_name = subst(name + ' ' + f.argument, ' ', '_');
-
-		fullname = LibFileSearch("images", xpm_name, "xpm");
-
-		if (fullname.empty()) {
-			// try without the argument
-			fullname = LibFileSearch("images", name, "xpm");
 		}
+		fullname = LibFileSearch("images", xpm_name, "xpm");
 	}
 
 	if (!fullname.empty()) {
Index: src/ToolbarBackend.h
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/ToolbarBackend.h,v
retrieving revision 1.12
diff -u -p -r1.12 ToolbarBackend.h
--- src/ToolbarBackend.h	21 Apr 2004 00:19:26 -0000	1.12
+++ src/ToolbarBackend.h	31 Jan 2006 16:33:59 -0000
@@ -34,8 +34,12 @@ public:
 		LAYOUTS = -1,
 	};
 
-	/// action, tooltip
-	typedef std::pair<FuncRequest, std::string> Item;
+	/// action, tooltip, icon
+	struct Item {
+		FuncRequest action;
+		std::string tooltip;
+		std::string icon;
+	};
 
 	/// the toolbar items
 	typedef std::vector<Item> Items;
@@ -86,11 +90,13 @@ public:
 	void readToolbars(LyXLex &);
 
 	/// return a full path of an XPM for the given action
-	static std::string const getIcon(FuncRequest const &);
+	static std::string const getIcon(FuncRequest const &,
+					 std::string const & icon = std::string());
 
 private:
-	/// add the given lfun with tooltip if relevant
+	/// add the given lfun with icon and with tooltip if relevant
 	void add(Toolbar & tb, FuncRequest const &,
+		 std::string const & icon = std::string(),
 		 std::string const & tooltip = std::string());
 
 	/// all the toolbars
Index: src/frontends/Toolbars.h
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/frontends/Toolbars.h,v
retrieving revision 1.2
diff -u -p -r1.2 Toolbars.h
--- src/frontends/Toolbars.h	9 Nov 2004 12:40:33 -0000	1.2
+++ src/frontends/Toolbars.h	31 Jan 2006 16:33:59 -0000
@@ -51,6 +51,7 @@ public:
 	virtual ~Toolbar() {}
 	/// Add a button to the bar.
 	virtual void add(FuncRequest const & func,
+			 std::string const & icon,
 			 std::string const & tooltip) = 0;
 
 	/** Hide the bar.
Index: src/frontends/qt2/QLToolbar.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/frontends/qt2/QLToolbar.C,v
retrieving revision 1.17
diff -u -p -r1.17 QLToolbar.C
--- src/frontends/qt2/QLToolbar.C	9 Nov 2004 12:40:33 -0000	1.17
+++ src/frontends/qt2/QLToolbar.C	31 Jan 2006 16:33:59 -0000
@@ -174,11 +174,11 @@ QLToolbar::QLToolbar(ToolbarBackend::Too
 	ToolbarBackend::item_iterator it = tbb.items.begin();
 	ToolbarBackend::item_iterator end = tbb.items.end();
 	for (; it != end; ++it)
-		add(it->first, it->second);
+		add(it->action, it->icon, it->tooltip);
 }
 
 
-void QLToolbar::add(FuncRequest const & func, string const & tooltip)
+void QLToolbar::add(FuncRequest const & func, string const & icon, string const & tooltip)
 {
 	switch (func.action) {
 	case ToolbarBackend::SEPARATOR:
@@ -194,7 +194,7 @@ void QLToolbar::add(FuncRequest const & 
 	default: {
 		if (owner_.getLyXFunc().getStatus(func).unknown())
 			break;
-		QPixmap p = QPixmap(toolbarbackend.getIcon(func).c_str());
+		QPixmap p = QPixmap(toolbarbackend.getIcon(func,icon).c_str());
 		QToolButton * button =
 			new QToolButton(p, toqstr(tooltip), "",
 			this, SLOT(clicked()), toolbar_);
Index: src/frontends/qt2/QLToolbar.h
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/frontends/qt2/QLToolbar.h,v
retrieving revision 1.8
diff -u -p -r1.8 QLToolbar.h
--- src/frontends/qt2/QLToolbar.h	19 May 2004 15:11:33 -0000	1.8
+++ src/frontends/qt2/QLToolbar.h	31 Jan 2006 16:33:59 -0000
@@ -61,7 +61,7 @@ class QLToolbar : public QObject, public
 	Q_OBJECT
 public:
 	QLToolbar(ToolbarBackend::Toolbar const &, LyXView &);
-	void add(FuncRequest const & func, std::string const & tooltip);
+	void add(FuncRequest const & func, std::string const & icon, std::string const & tooltip);
 	void hide(bool);
 	void show(bool);
 	void update();

Reply via email to