>>>>> "Angus" == Angus Leeming <[EMAIL PROTECTED]> writes:

Angus> Rather than try and do this yourself, why not generate a
Angus> QKeySequence from the binding and then invoke:

Actually, the right thing to do would be to have
kb_keymap::findbinding return a list of kb_sequence and expose only
that to the frontends. This is more reasonable than this business of
converting back and forth between strings and keys.

However, since we are supposedly in freeze time, I came up with the
attached kludgy but simple patch. It adds a method to kb_keymap that
returns the first 1-key binding to a FuncRequest. This is much easier
than writing the complete recursive version, but I can try to do that
if people prefer.

Bennett, could you try it and tell me whether it fixes the problems in
LyX/Mac? Note that this will not display the emacs-like two-key
bindings, but this is a limitation of Qt/Mac, as far as I can see.

Comments welcome.

JMarc

Index: src/ChangeLog
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/ChangeLog,v
retrieving revision 1.1927
diff -u -p -r1.1927 ChangeLog
--- src/ChangeLog	23 Jun 2004 14:04:07 -0000	1.1927
+++ src/ChangeLog	24 Jun 2004 10:52:00 -0000
@@ -1,3 +1,8 @@
+2004-06-24  Jean-Marc Lasgouttes  <[EMAIL PROTECTED]>
+
+	* kbmap.C (find1keybinding): new method, only used by LyX/Mac with
+	Qt frontend
+
 2004-06-10  Jean-Marc Lasgouttes  <[EMAIL PROTECTED]>
 
 	* lyxfunc.C (getStatus): if lyx_gui::getStatus disables the
Index: src/kbmap.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/kbmap.C,v
retrieving revision 1.53
diff -u -p -r1.53 kbmap.C
--- src/kbmap.C	29 Apr 2004 15:25:56 -0000	1.53
+++ src/kbmap.C	24 Jun 2004 10:52:00 -0000
@@ -304,3 +304,17 @@ string const kb_keymap::findbinding(Func
 
 	return res;
 }
+
+
+std::pair<LyXKeySym const *, key_modifier::state>
+kb_keymap::find1keybinding(FuncRequest const & func) const
+{
+	Table::const_iterator end = table.end();
+	for (Table::const_iterator cit = table.begin();
+	    cit != end; ++cit) {
+		if (!cit->table.get() && cit->func == func) 
+			return std::make_pair(cit->code.get(), cit->mod.first);
+	}
+
+	return std::make_pair<LyXKeySym const *, key_modifier::state>(0, key_modifier::none);
+}	
Index: src/kbmap.h
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/kbmap.h,v
retrieving revision 1.33
diff -u -p -r1.33 kbmap.h
--- src/kbmap.h	26 Mar 2004 15:12:34 -0000	1.33
+++ src/kbmap.h	24 Jun 2004 10:52:00 -0000
@@ -60,6 +60,14 @@ public:
 				 std::string const & prefix = std::string()) const;
 
 	/**
+	 *  Given an action, find the first 1-key binding (if it exists).
+	 *  The LyXKeySym pointer is 0 is no key is found.
+	 *  [only used by the Qt/Mac frontend]
+	 */
+	std::pair<LyXKeySym const *, key_modifier::state>
+	find1keybinding(FuncRequest const & func) const;
+
+	/**
 	 * Returns a string of the given keysym, with modifiers.
 	 * @param key the key as a keysym
 	 * @param mod the modifiers
Index: src/frontends/qt2/ChangeLog
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/frontends/qt2/ChangeLog,v
retrieving revision 1.696
diff -u -p -r1.696 ChangeLog
--- src/frontends/qt2/ChangeLog	23 Jun 2004 14:04:09 -0000	1.696
+++ src/frontends/qt2/ChangeLog	24 Jun 2004 10:52:00 -0000
@@ -1,3 +1,11 @@
+2004-06-24  Jean-Marc Lasgouttes  <[EMAIL PROTECTED]>
+
+	* QLyXKeySym.C (key): accept an optional modifier as argument
+	(print): use it.
+
+	* QLPopupMenu.C (getLabel): do not add the binding here anymore
+	(populate): changes to make bindings work on Qt/Mac. 
+
 2004-06-09  Jean-Marc Lasgouttes  <[EMAIL PROTECTED]>
 
 	* lyx_gui.C (getStatus): under Mac OS X, disable the
Index: src/frontends/qt2/QLPopupMenu.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/frontends/qt2/QLPopupMenu.C,v
retrieving revision 1.31
diff -u -p -r1.31 QLPopupMenu.C
--- src/frontends/qt2/QLPopupMenu.C	20 May 2004 09:36:27 -0000	1.31
+++ src/frontends/qt2/QLPopupMenu.C	24 Jun 2004 10:52:00 -0000
@@ -22,6 +22,13 @@
 
 #include "support/lstrings.h"
 
+#ifdef Q_WS_MACX
+#include "kbmap.h"
+#include "QLyXKeySym.h"
+#include <qaccel.h>
+extern boost::scoped_ptr<kb_keymap> toplevel_keymap;
+#endif
+
 using std::distance;
 using std::make_pair;
 using std::string;
@@ -46,13 +53,6 @@ string const getLabel(MenuItem const & m
 			label.insert(pos, 1, '&');
 	}
 
-	if (mi.kind() == MenuItem::Command) {
-		string const binding(mi.binding());
-		if (!binding.empty()) {
-			label += '\t' + binding;
-		}
-	}
-
 	return label;
 }
 
@@ -107,7 +107,30 @@ void QLPopupMenu::populate(Menu * menu)
 				funcs_.insert(funcs_.end(), m->func());
 			int const index = distance(funcs_.begin(), fit);
 
-			insertItem(toqstr(getLabel(*m)), index);
+			QString label = toqstr(getLabel(*m));
+#ifdef Q_WS_MACX
+			/* There are two constraints on Qt/Mac: (1)
+			   the bindings require a unicode string to be
+			   represented meaningfully and std::string
+			   does not work (2) only 1-key bindings can
+			   be represented in menus.
+			     
+			   This is why the unpleasant hack bellow is
+			   needed (JMarc)
+			*/
+			pair<LyXKeySym const *, key_modifier::state>
+				binding = toplevel_keymap->find1keybinding(m->func());
+			if (binding.first) {
+				int const key = static_cast<QLyXKeySym const *>(binding.first)->key(binding.second);
+				label += '\t' + QAccel::keyToString(key);
+			}
+#else
+			string const binding(m->binding());
+			if (!binding.empty()) {
+				label += '\t' + toqstr(binding);
+			}
+#endif
+			insertItem(label, index);
 			setItemEnabled(index, status.enabled());
 			setItemChecked(index, status.onoff(true));
 		}
Index: src/frontends/qt2/QLyXKeySym.C
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/frontends/qt2/QLyXKeySym.C,v
retrieving revision 1.31
diff -u -p -r1.31 QLyXKeySym.C
--- src/frontends/qt2/QLyXKeySym.C	20 May 2004 09:36:27 -0000	1.31
+++ src/frontends/qt2/QLyXKeySym.C	24 Jun 2004 10:52:00 -0000
@@ -169,11 +169,10 @@ char QLyXKeySym::getISOEncoded(string co
 }
 
 
-string const QLyXKeySym::print(key_modifier::state mod) const
+int QLyXKeySym::key(key_modifier::state mod) const
 {
 	int tmpkey = key_;
 
-
 	if (mod & key_modifier::shift)
 		tmpkey += Qt::SHIFT;
 	if (mod & key_modifier::ctrl)
@@ -181,7 +180,13 @@ string const QLyXKeySym::print(key_modif
 	if (mod & key_modifier::alt)
 		tmpkey += Qt::ALT;
 
-	return fromqstr(QAccel::keyToString(tmpkey));
+	return tmpkey;
+}
+
+
+string const QLyXKeySym::print(key_modifier::state mod) const
+{
+	return fromqstr(QAccel::keyToString(key(mod)));
 }
 
 
Index: src/frontends/qt2/QLyXKeySym.h
===================================================================
RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/frontends/qt2/QLyXKeySym.h,v
retrieving revision 1.17
diff -u -p -r1.17 QLyXKeySym.h
--- src/frontends/qt2/QLyXKeySym.h	26 Mar 2004 15:12:35 -0000	1.17
+++ src/frontends/qt2/QLyXKeySym.h	24 Jun 2004 10:52:00 -0000
@@ -59,9 +59,8 @@ public:
 	virtual std::string const print(key_modifier::state mod) const;
 
 	///
-	int key() const {
-		return key_;
-	}
+	int key(key_modifier::state mod = key_modifier::none) const;
+
 private:
 	/// the Qt sym value
 	int key_;

Reply via email to