Improve menu accelerator keys

This commit is contained in:
Markus Gans 2015-11-12 01:33:16 +01:00
parent df83751be2
commit 6fd2e7c650
20 changed files with 500 additions and 325 deletions

View File

@ -1,3 +1,6 @@
2015-11-12 Markus Gans <guru.mail@muenster.de>
* Improve menu accelerator keys
2015-11-08 Markus Gans <guru.mail@muenster.de>
* Activate the previous window in case if the menu is open and
the mouse click has activated no window.

View File

@ -286,7 +286,7 @@ void FApplication::processKeyboardEvent()
if ( key != NEED_MORE_DATA )
{
if ( key == 0x0c ) // Ctrl-L (redraw the screen)
if ( key == fc::Fckey_l ) // Ctrl-L (redraw the screen)
redraw();
if ( key == fc::Fkey_mouse )

View File

@ -124,7 +124,7 @@ class FApplication : public FWidget
bool KeyPressed();
ssize_t readKey();
void processKeyboardEvent();
void getX11ButtonState (int button);
void getX11ButtonState (int);
bool parseX11Mouse();
bool parseSGRMouse();
bool parseUrxvtMouse();

View File

@ -51,7 +51,7 @@ FButton::FButton (const FString& txt, FWidget* parent)
//----------------------------------------------------------------------
FButton::~FButton() // destructor
{
delAccelerator (this);
delAccelerator();
delAllTimer();
}
@ -103,16 +103,16 @@ void FButton::setHotkeyAccelerator()
{
if ( isalpha(hotkey) || isdigit(hotkey) )
{
addAccelerator (tolower(hotkey), this);
addAccelerator (toupper(hotkey), this);
addAccelerator (tolower(hotkey));
addAccelerator (toupper(hotkey));
// Meta + hotkey
addAccelerator (0x20000e0+tolower(hotkey), this);
addAccelerator (fc::Fmkey_meta + tolower(hotkey));
}
else
addAccelerator (getHotkey(), this);
addAccelerator (getHotkey());
}
else
delAccelerator(this);
delAccelerator();
}
//----------------------------------------------------------------------
@ -120,7 +120,7 @@ inline void FButton::detectHotkey()
{
if ( isEnabled() )
{
delAccelerator (this);
delAccelerator();
setHotkeyAccelerator();
}
}
@ -499,7 +499,7 @@ bool FButton::setEnable (bool on)
else
{
flags &= ~ACTIVE;
delAccelerator (this);
delAccelerator();
}
updateButtonColor();
return on;

View File

@ -179,16 +179,16 @@ void FButtonGroup::setHotkeyAccelerator()
{
if ( isalpha(hotkey) || isdigit(hotkey) )
{
addAccelerator (tolower(hotkey), this);
addAccelerator (toupper(hotkey), this);
addAccelerator (tolower(hotkey));
addAccelerator (toupper(hotkey));
// Meta + hotkey
addAccelerator (0x20000e0 + tolower(hotkey), this);
addAccelerator (fc::Fmkey_meta + tolower(hotkey));
}
else
addAccelerator (getHotkey(), this);
addAccelerator (getHotkey());
}
else
delAccelerator(this);
delAccelerator();
}
//----------------------------------------------------------------------
@ -528,7 +528,7 @@ bool FButtonGroup::setEnable (bool on)
else
{
flags &= ~ACTIVE;
delAccelerator (this);
delAccelerator();
}
return on;
}
@ -549,7 +549,7 @@ void FButtonGroup::setText (const FString& txt)
text = txt;
if ( isEnabled() )
{
delAccelerator (this);
delAccelerator();
setHotkeyAccelerator();
}
}

View File

@ -171,10 +171,38 @@ class fc
// keyboard - single keys
enum keys
{
Fkey_erase = 0x0000008,
Fkey_tab = 0x0000009,
Fkey_return = 0x000000d,
Fkey_escape = 0x000001b,
Fckey_at = 0x0000000, // control-@
Fckey_a = 0x0000001, // control-a
Fckey_b = 0x0000002, // control-b
Fckey_c = 0x0000003, // control-c
Fckey_d = 0x0000004, // control-d
Fckey_e = 0x0000005, // control-e
Fckey_f = 0x0000006, // control-f
Fckey_g = 0x0000007, // control-g
Fkey_erase = 0x0000008, // control-h
Fkey_tab = 0x0000009, // control-i
Fckey_j = 0x000000a, // control-j
Fckey_h = 0x000000b, // control-k
Fckey_l = 0x000000c, // control-l
Fkey_return = 0x000000d, // control-m
Fckey_n = 0x000000e, // control-n
Fckey_o = 0x000000f, // control-o
Fckey_p = 0x0000010, // control-p
Fckey_q = 0x0000011, // control-q
Fckey_r = 0x0000012, // control-r
Fckey_s = 0x0000013, // control-s
Fckey_t = 0x0000014, // control-t
Fckey_u = 0x0000015, // control-u
Fckey_v = 0x0000016, // control-v
Fckey_w = 0x0000017, // control-w
Fckey_x = 0x0000018, // control-x
Fckey_y = 0x0000019, // control-y
Fckey_z = 0x000001a, // control-t
Fkey_escape = 0x000001b, // control-[
Fckey_backslash = 0x000001c, // control-'\'
Fckey_right_square_bracket = 0x000001d, // control-]
Fckey_caret = 0x000001e, // control-^
Fckey_underscore = 0x000001f, // control-_
Fkey_space = 0x0000020,
Fkey_backspace = 0x1000100,
Fkey_catab = 0x1000101,
@ -438,6 +466,7 @@ class fc
Fkey_mouse = 0x2000020, // xterm mouse
Fkey_extended_mouse = 0x2000021, // SGR extended mouse
Fkey_urxvt_mouse = 0x2000022, // urxvt mouse extension
Fmkey_meta = 0x20000e0, // meta key offset
Fmkey_space = 0x2000100, // M-' '
Fmkey_bang = 0x2000101, // M-!
Fmkey_quotes = 0x2000102, // M-"

View File

@ -415,10 +415,37 @@ keyname;
static keyname FkeyName[] =
{
{ fc::Fkey_erase , "Backspace" },
{ fc::Fkey_tab , "Tab" },
{ fc::Fkey_return , "Return" },
{ fc::Fkey_escape , "Esc" },
{ fc::Fckey_at , "Ctrl+@" },
{ fc::Fckey_a , "Ctrl+a" },
{ fc::Fckey_b , "Ctrl+b" },
{ fc::Fckey_c , "Ctrl+c" },
{ fc::Fckey_d , "Ctrl+d" },
{ fc::Fckey_e , "Ctrl+e" },
{ fc::Fckey_f , "Ctrl+f" },
{ fc::Fckey_g , "Ctrl+g" },
{ fc::Fkey_erase , "Backspace" }, // Ctrl+h
{ fc::Fkey_tab , "Tab" }, // Ctrl+i
{ fc::Fckey_h , "Ctrl+k" },
{ fc::Fckey_l , "Ctrl+l" },
{ fc::Fkey_return , "Return" }, // Ctrl+m
{ fc::Fckey_n , "Ctrl+n" },
{ fc::Fckey_o , "Ctrl+o" },
{ fc::Fckey_p , "Ctrl+p" },
{ fc::Fckey_q , "Ctrl+q" },
{ fc::Fckey_r , "Ctrl+r" },
{ fc::Fckey_s , "Ctrl+s" },
{ fc::Fckey_t , "Ctrl+t" },
{ fc::Fckey_u , "Ctrl+u" },
{ fc::Fckey_v , "Ctrl+v" },
{ fc::Fckey_w , "Ctrl+w" },
{ fc::Fckey_x , "Ctrl+x" },
{ fc::Fckey_y , "Ctrl+y" },
{ fc::Fckey_z , "Ctrl+t" },
{ fc::Fkey_escape , "Esc" }, // Ctrl+[
{ fc::Fckey_backslash , "Ctrl+\\" },
{ fc::Fckey_right_square_bracket, "Ctrl+]" },
{ fc::Fckey_caret , "Ctrl+^" },
{ fc::Fckey_underscore , "Ctrl+_" },
{ fc::Fkey_space , "Space" },
{ fc::Fkey_backspace , "Backspace" },
{ fc::Fkey_catab , "Clear-All-Tabs" },

View File

@ -47,7 +47,7 @@ FLabel::FLabel (const FString& txt, FWidget* parent)
//----------------------------------------------------------------------
FLabel::~FLabel() // destructor
{
delAccelerator (this);
delAccelerator();
}
@ -118,16 +118,16 @@ void FLabel::setHotkeyAccelerator()
{
if ( isalpha(hotkey) || isdigit(hotkey) )
{
addAccelerator (tolower(hotkey), this);
addAccelerator (toupper(hotkey), this);
addAccelerator (tolower(hotkey));
addAccelerator (toupper(hotkey));
// Meta + hotkey
addAccelerator (0x20000e0 + tolower(hotkey), this);
addAccelerator (fc::Fmkey_meta + tolower(hotkey));
}
else
addAccelerator (getHotkey(), this);
addAccelerator (getHotkey());
}
else
delAccelerator(this);
delAccelerator();
}
//----------------------------------------------------------------------
@ -391,7 +391,7 @@ void FLabel::onAccel (FAccelEvent* ev)
void FLabel::cb_accel_widget_destroyed (FWidget*, void*)
{
accel_widget = 0;
delAccelerator (this);
delAccelerator();
}
//----------------------------------------------------------------------
@ -449,7 +449,7 @@ bool FLabel::setEnable (bool on)
else
{
flags &= ~ACTIVE;
delAccelerator (this);
delAccelerator();
}
return on;
}
@ -472,7 +472,7 @@ void FLabel::setText (const FString& txt)
if ( isEnabled() )
{
delAccelerator (this);
delAccelerator();
setHotkeyAccelerator();
}
}

View File

@ -47,7 +47,7 @@ void FMenuBar::init()
setGeometry (1, 1, getColumnNumber(), 1, false);
getRootWidget()->setTopPadding(1, true);
setMenuBar(this);
addAccelerator (fc::Fkey_f10, this);
addAccelerator (fc::Fkey_f10);
foregroundColor = wc.menu_active_fg;
backgroundColor = wc.menu_active_bg;
window_object = true;
@ -208,7 +208,7 @@ bool FMenuBar::hotkeyMenu (FKeyEvent*& ev)
int hotkey = (*iter)->getHotkey();
int key = ev->key();
if ( 0x20000e0+tolower(hotkey) == key )
if ( fc::Fmkey_meta + tolower(hotkey) == key )
{
FMenuItem* sel_item = getSelectedItem();

View File

@ -22,7 +22,7 @@ FMenuItem::FMenuItem (FWidget* parent)
, checked(false)
, text_length(0)
, hotkey(0)
//, accel_key(0)
, accel_key(0)
, menu(0)
, super_menu(0)
{
@ -38,7 +38,7 @@ FMenuItem::FMenuItem (FString& txt, FWidget* parent)
, checked(false)
, text_length(0)
, hotkey(0)
//, accel_key(0)
, accel_key(0)
, menu(0)
, super_menu(0)
{
@ -54,7 +54,7 @@ FMenuItem::FMenuItem (const std::string& txt, FWidget* parent)
, checked(false)
, text_length(0)
, hotkey(0)
//, accel_key(0)
, accel_key(0)
, menu(0)
, super_menu(0)
{
@ -70,13 +70,59 @@ FMenuItem::FMenuItem (const char* txt, FWidget* parent)
, checked(false)
, text_length(0)
, hotkey(0)
//, accel_key(0)
, accel_key(0)
, menu(0)
, super_menu(0)
{
init (parent);
}
//----------------------------------------------------------------------
FMenuItem::FMenuItem (int k, FString& txt, FWidget* parent)
: FWidget(parent)
, text(txt)
, selected(false)
, separator(false)
, checked(false)
, text_length(0)
, hotkey(0)
, accel_key(k)
, menu(0)
, super_menu(0)
{
init (parent);
}
//----------------------------------------------------------------------
FMenuItem::FMenuItem (int k, const std::string& txt, FWidget* parent)
: FWidget(parent)
, text(txt)
, selected(false)
, separator(false)
, checked(false)
, text_length(0)
, hotkey(0)
, accel_key(k)
, menu(0)
, super_menu(0)
{
init (parent);
}
//----------------------------------------------------------------------
FMenuItem::FMenuItem (int k, const char* txt, FWidget* parent)
: FWidget(parent)
, text(txt)
, selected(false)
, separator(false)
, checked(false)
, text_length(0)
, hotkey(0)
, accel_key(k)
, menu(0)
, super_menu(0)
{
init (parent);
}
//----------------------------------------------------------------------
FMenuItem::~FMenuItem() // destructor
{ }
@ -88,14 +134,21 @@ void FMenuItem::init (FWidget* parent)
{
text_length = text.getLength();
hotkey = hotKey();
if ( hotkey )
text_length--;
setGeometry (1,1,int(text_length+2),1, false);
if ( parent )
{
FMenuList* menu_list;
setSuperMenu (parent);
FMenuList* menu_list = dynamic_cast<FMenuList*>(parent);
if ( accel_key )
addAccelerator (accel_key);
menu_list = dynamic_cast<FMenuList*>(parent);
if ( menu_list )
menu_list->insert(this);
@ -106,7 +159,8 @@ void FMenuItem::init (FWidget* parent)
menubar_ptr->menu_dimension();
// Meta + hotkey
menubar_ptr->addAccelerator (0x20000e0+tolower(hotkey), this);
if ( hotkey )
menubar_ptr->addAccelerator (fc::Fmkey_meta + tolower(hotkey), this);
this->addCallback
(
@ -125,8 +179,6 @@ void FMenuItem::init (FWidget* parent)
if ( menu_ptr )
menu_ptr->menu_dimension();
//addAccelerator (accel_key, this);
this->addCallback
(
"activate",
@ -204,6 +256,64 @@ void FMenuItem::processClicked()
}
// public methods of FMenuItem
//----------------------------------------------------------------------
void FMenuItem::addAccelerator (int key, FWidget* obj)
{
FWidget* super = super_menu;
while ( super && strncmp ( super->getClassName()
, const_cast<char*>("FMenu"), 5) == 0 )
{
super = super->parentWidget();
}
if ( super )
{
FWidget* window = FWindow::getWindowWidget(super);
accelerator accel = { key, obj };
if ( ! window )
window = getRootWidget();
if ( window && window->accelerator_list )
window->accelerator_list->push_back(accel);
}
}
//----------------------------------------------------------------------
void FMenuItem::delAccelerator (FWidget* obj)
{
FWidget* super = super_menu;
while ( super && strncmp ( super->getClassName()
, const_cast<char*>("FMenu"), 5) == 0 )
{
super = super->parentWidget();
}
if ( super )
{
FWidget* window = FWindow::getWindowWidget(super);
if ( ! window )
window = getRootWidget();
if ( window
&& window->accelerator_list
&& ! window->accelerator_list->empty() )
{
FWidget::Accelerators::iterator iter;
iter = window->accelerator_list->begin();
while ( iter != window->accelerator_list->end() )
{
if ( iter->object == obj )
iter = window->accelerator_list->erase(iter);
else
++iter;
}
}
}
}
//----------------------------------------------------------------------
void FMenuItem::onKeyPress (FKeyEvent* ev)
{
@ -377,6 +487,8 @@ void FMenuItem::onAccel (FAccelEvent* ev)
ev->accept();
}
}
else
processClicked();
}
}
@ -418,7 +530,7 @@ bool FMenuItem::setEnable (bool on)
if ( super && isMenuBar(super) )
{
// Meta + hotkey
super->addAccelerator (0x20000e0+tolower(hotkey), this);
super->addAccelerator (fc::Fmkey_meta + tolower(hotkey), this);
}
}
else
@ -509,24 +621,24 @@ void FMenuItem::unsetSelected()
//----------------------------------------------------------------------
void FMenuItem::openMenu()
{
FMenu* menu;
FMenu* submenu;
FMenu* open_menu;
if ( hasMenu() )
{
menu = getMenu();
submenu = getMenu();
if ( ! menu->isVisible() )
if ( ! submenu->isVisible() )
{
open_menu = static_cast<FMenu*>(getOpenMenu());
if ( open_menu && open_menu != menu )
if ( open_menu && open_menu != submenu )
open_menu->hide();
setOpenMenu(menu);
setOpenMenu(submenu);
menu->setVisible();
menu->show();
menu->raiseWindow(menu);
menu->redraw();
submenu->setVisible();
submenu->show();
submenu->raiseWindow(submenu);
submenu->redraw();
updateTerminal();
flush_out();
}

View File

@ -49,7 +49,7 @@ class FMenuItem : public FWidget
bool checked;
uInt text_length;
int hotkey;
//int accel_key;
int accel_key;
FMenu* menu;
FWidget* super_menu;
@ -71,8 +71,17 @@ class FMenuItem : public FWidget
FMenuItem (FString&, FWidget* = 0);
FMenuItem (const std::string&, FWidget* = 0);
FMenuItem (const char*, FWidget* = 0);
FMenuItem (int, FString&, FWidget* = 0);
FMenuItem (int, const std::string&, FWidget* = 0);
FMenuItem (int, const char*, FWidget* = 0);
virtual ~FMenuItem();
// make every addAccelerator from FWidget available
using FWidget::addAccelerator;
void addAccelerator (int, FWidget*);
// make every delAccelerator from FWidget available
using FWidget::delAccelerator;
void delAccelerator (FWidget*);
void onKeyPress (FKeyEvent*);
void onMouseDown (FMouseEvent*);
void onMouseUp (FMouseEvent*);

View File

@ -97,8 +97,6 @@ void FMenuList::remove (FMenuItem* i)
{
std::vector<FMenuItem*>::iterator iter;
//delAccelerator (i);
if ( itemlist.empty() )
return;

View File

@ -100,8 +100,8 @@ inline FMenuItem* FMenuList::getSelectedItem() const
{ return selectedItem; }
//----------------------------------------------------------------------
inline void FMenuList::setSelectedItem (FMenuItem* item)
{ selectedItem = item; }
inline void FMenuList::setSelectedItem (FMenuItem* menuitem)
{ selectedItem = menuitem; }
//----------------------------------------------------------------------
inline bool FMenuList::hasSelectedItem() const

View File

@ -81,8 +81,8 @@ class FObject
void delChild (FObject*);
int numOfChildren() const;
static void getCurrentTime (timeval&);
int addTimer (int interval);
bool delTimer (int id);
int addTimer (int);
bool delTimer (int);
bool delAllTimer();
virtual bool event (FEvent*);

View File

@ -62,7 +62,7 @@ FStatusKey::~FStatusKey() // destructor
{
if ( statusbar() )
statusbar()->remove(this);
delAccelerator(this);
delAccelerator();
}

View File

@ -72,13 +72,13 @@ class FString
mutable char* c_string;
private:
inline void initLength (uInt);
inline void _replace (const wchar_t*);
inline void _insert (uInt, uInt, const wchar_t*);
inline void _remove (uInt, uInt);
inline char* wc_to_c_str (const wchar_t* s) const;
inline wchar_t* c_to_wc_str (const char* s) const;
inline wchar_t* extractToken (wchar_t**, const wchar_t*, const wchar_t*);
void initLength (uInt);
void _replace (const wchar_t*);
void _insert (uInt, uInt, const wchar_t*);
void _remove (uInt, uInt);
char* wc_to_c_str (const wchar_t*) const;
wchar_t* c_to_wc_str (const char*) const;
wchar_t* extractToken (wchar_t**, const wchar_t*, const wchar_t*);
public:
FString (); // constructor

View File

@ -56,7 +56,7 @@ FToggleButton::FToggleButton ( const FString& txt, FWidget* parent )
//----------------------------------------------------------------------
FToggleButton::~FToggleButton() // destructor
{
delAccelerator(this);
delAccelerator();
if ( group() )
group()->remove(this);
if ( hasFocus() )
@ -137,16 +137,16 @@ void FToggleButton::setHotkeyAccelerator()
{
if ( isalpha(hotkey) || isdigit(hotkey) )
{
addAccelerator (tolower(hotkey), this);
addAccelerator (toupper(hotkey), this);
addAccelerator (tolower(hotkey));
addAccelerator (toupper(hotkey));
// Meta + hotkey
addAccelerator (0x20000e0 + tolower(hotkey), this);
addAccelerator (fc::Fmkey_meta + tolower(hotkey));
}
else
addAccelerator (getHotkey(), this);
addAccelerator (getHotkey());
}
else
delAccelerator(this);
delAccelerator();
}
//----------------------------------------------------------------------
@ -406,7 +406,7 @@ bool FToggleButton::setEnable(bool on)
else
{
flags &= ~ACTIVE;
delAccelerator (this);
delAccelerator();
foregroundColor = wc.toggle_button_inactive_fg;
backgroundColor = wc.toggle_button_inactive_bg;
}
@ -625,7 +625,7 @@ void FToggleButton::setText (FString txt)
setWidth(button_width + int(text.getLength()));
if ( isEnabled() )
{
delAccelerator (this);
delAccelerator();
setHotkeyAccelerator();
}
}

View File

@ -1072,20 +1072,6 @@ void FWidget::emitCallback (FString emit_signal)
}
}
//----------------------------------------------------------------------
void FWidget::addAccelerator (int key)
{
FWidget* window = FWindow::getWindowWidget(this);
accelerator accel = { key, this };
if ( ! window )
window = getRootWidget();
if ( window == statusbar || window == menubar )
window = FWindow::getWindowWidget(parentWidget());
if ( window && window->accelerator_list )
window->accelerator_list->push_back(accel);
}
//----------------------------------------------------------------------
void FWidget::addAccelerator (int key, FWidget* obj)
{

View File

@ -358,9 +358,10 @@ class FWidget : public FObject, public FTerm
void delCallback (FWidget*);
void emitCallback (FString);
void addAccelerator (int key);
void addAccelerator (int, FWidget*);
void delAccelerator (FWidget*);
void addAccelerator (int);
virtual void addAccelerator (int, FWidget*);
void delAccelerator ();
virtual void delAccelerator (FWidget*);
virtual void redraw();
virtual void resize();
@ -434,7 +435,7 @@ class FWidget : public FObject, public FTerm
virtual void setGeometry (const FRect&, bool = true);
virtual void setGeometry (int, int, int, int, bool = true);
virtual void move (const FPoint&);
virtual void move (int x, int y);
virtual void move (int, int);
int getFlags() const;
bool setCursor();
@ -509,6 +510,14 @@ inline void FWidget::clearStatusbarMessage()
inline FString FWidget::getStatusbarMessage()
{ return statusbar_message; }
//----------------------------------------------------------------------
inline void FWidget::addAccelerator (int key)
{ addAccelerator (key, this); }
//----------------------------------------------------------------------
inline void FWidget::delAccelerator()
{ delAccelerator(this); }
//----------------------------------------------------------------------
inline bool FWidget::setVisible()
{ return visible = true; }

View File

@ -210,10 +210,12 @@ MyDialog::MyDialog (FWidget* parent)
// "File" menu items
FMenuItem* Open = new FMenuItem ("&Open...", File);
Open->addAccelerator (fc::Fckey_o); // Ctrl + O
Open->setStatusbarMessage ("Locate and open a text file");
FMenuItem* Line1 = new FMenuItem (File);
Line1->setSeparator();
FMenuItem* Quit = new FMenuItem ("&Quit", File);
Quit->addAccelerator (fc::Fmkey_x); // Meta/Alt + X
Quit->setStatusbarMessage ("Exit the program");
// "Edit" menu items
@ -223,11 +225,11 @@ MyDialog::MyDialog (FWidget* parent)
Redo->setDisable();
FMenuItem* Line2 = new FMenuItem (Edit);
Line2->setSeparator();
FMenuItem* Cut = new FMenuItem("Cu&t", Edit);
FMenuItem* Cut = new FMenuItem (fc::Fckey_x, "Cu&t", Edit);
Cut->setStatusbarMessage ("Remove the input text and put it in the clipboard");
FMenuItem* Copy = new FMenuItem("&Copy", Edit);
FMenuItem* Copy = new FMenuItem (fc::Fckey_c, "&Copy", Edit);
Copy->setStatusbarMessage ("Copy the input text into the clipboad");
FMenuItem* Paste = new FMenuItem("&Paste", Edit);
FMenuItem* Paste = new FMenuItem (fc::Fckey_v, "&Paste", Edit);
Paste->setStatusbarMessage ("Insert text form clipboard");
FMenuItem* Clear = new FMenuItem ("C&lear", Edit);
Clear->setStatusbarMessage ("Delete input text");