implemented hotkey support in new GUI code

svn-id: r4488
This commit is contained in:
Max Horn 2002-07-08 00:29:47 +00:00
parent c9b1d393b8
commit 28852f1497
5 changed files with 42 additions and 23 deletions

View file

@ -18,6 +18,8 @@
* $Header$
*/
#include <ctype.h>
#include "stdafx.h"
#include "dialog.h"
#include "widget.h"
@ -44,6 +46,25 @@ void Dialog::handleClick(int x, int y, int button)
w->handleClick(button);
}
void Dialog::handleKey(char key, int modifiers)
{
// ESC closes all dialogs by default
if (key == 27)
close();
// Hotkey handling
Widget *w = _firstWidget;
key = toupper(key);
while (w) {
ButtonWidget *b = dynamic_cast<ButtonWidget *>(w);
if (b && key == toupper(b->_hotkey)) {
b->handleClick(1);
break;
}
w = w->_next;
}
}
void Dialog::handleMouseMoved(int x, int y, int button)
{
Widget *w = findWidget(x - _x, y - _y);
@ -96,10 +117,9 @@ void Dialog::addResText(int x, int y, int w, int h, int resID)
new StaticTextWidget(this, x, y, w, h, str);
}
void Dialog::addButton(int x, int y, int w, int h, char hotkey, const char *label, uint32 cmd)
void Dialog::addButton(int x, int y, int w, int h, const char *label, uint32 cmd, char hotkey)
{
new ButtonWidget(this, x, y, w, h, label, cmd);
// TODO - handle hotkey
new ButtonWidget(this, x, y, w, h, label, cmd, hotkey);
}
#pragma mark -
@ -120,11 +140,11 @@ SaveLoadDialog::SaveLoadDialog(NewGui *gui)
// addResText(10, 7, 240, 16, 2);
// addResText(10, 7, 240, 16, 3);
addButton(200, 20, 54, 16, 'S', RES_STRING(4), kSaveCmd); // Save
addButton(200, 40, 54, 16, 'L', RES_STRING(5), kLoadCmd); // Load
addButton(200, 60, 54, 16, 'P', RES_STRING(6), kPlayCmd); // Play
addButton(200, 80, 54, 16, 'O', CUSTOM_STRING(17), kOptionsCmd); // Options
addButton(200, 100, 54, 16, 'Q', RES_STRING(8), kQuitCmd); // Quit
addButton(200, 20, 54, 16, RES_STRING(4), kSaveCmd, 'S'); // Save
addButton(200, 40, 54, 16, RES_STRING(5), kLoadCmd, 'L'); // Load
addButton(200, 60, 54, 16, RES_STRING(6), kPlayCmd, 'P'); // Play
addButton(200, 80, 54, 16, CUSTOM_STRING(17), kOptionsCmd, 'O'); // Options
addButton(200, 100, 54, 16, RES_STRING(8), kQuitCmd, 'Q'); // Quit
// FIXME - test
new CheckboxWidget(this, 50, 20, 100, 16, "Toggle me", 0);
@ -164,11 +184,11 @@ enum {
OptionsDialog::OptionsDialog(NewGui *gui)
: Dialog (gui, 50, 80, 210, 60)
{
addButton( 10, 10, 40, 15, 'S', CUSTOM_STRING(5), kSoundCmd); // Sound
addButton( 80, 10, 40, 15, 'K', CUSTOM_STRING(6), kKeysCmd); // Keys
addButton(150, 10, 40, 15, 'A', CUSTOM_STRING(7), kAboutCmd); // About
addButton( 10, 35, 40, 15, 'M', CUSTOM_STRING(18), kMiscCmd); // Misc
addButton(150, 35, 40, 15, 'C', CUSTOM_STRING(23), kCloseCmd); // Close dialog - FIXME
addButton( 10, 10, 40, 15, CUSTOM_STRING(5), kSoundCmd, 'S'); // Sound
addButton( 80, 10, 40, 15, CUSTOM_STRING(6), kKeysCmd, 'K'); // Keys
addButton(150, 10, 40, 15, CUSTOM_STRING(7), kAboutCmd, 'A'); // About
addButton( 10, 35, 40, 15, CUSTOM_STRING(18), kMiscCmd, 'M'); // Misc
addButton(150, 35, 40, 15, CUSTOM_STRING(23), kCloseCmd, 'C'); // Close dialog - FIXME
}
void OptionsDialog::handleCommand(uint32 cmd)

View file

@ -52,8 +52,7 @@ public:
//virtual void handleIdle(); // Called periodically
virtual void handleClick(int x, int y, int button);
virtual void handleKey(char key, int modifiers) // modifiers = alt/shift/ctrl etc.
{ if (key == 27) close(); }
virtual void handleKey(char key, int modifiers); // modifiers = alt/shift/ctrl etc.
virtual void handleMouseMoved(int x, int y, int button);
virtual void handleCommand(uint32 cmd);
@ -64,7 +63,7 @@ protected:
void close();
void addResText(int x, int y, int w, int h, int resID);
void addButton(int x, int y, int w, int h, char hotkey, const char *label, uint32 cmd);
void addButton(int x, int y, int w, int h, const char *label, uint32 cmd, char hotkey);
};
class SaveLoadDialog : public Dialog {

View file

@ -90,8 +90,8 @@ void StaticTextWidget::drawWidget(bool hilite)
#pragma mark -
ButtonWidget::ButtonWidget(Dialog *boss, int x, int y, int w, int h, const char *label, uint32 cmd)
: StaticTextWidget(boss, x, y, w, h, label), _cmd(cmd), _hotkey(0)
ButtonWidget::ButtonWidget(Dialog *boss, int x, int y, int w, int h, const char *label, uint32 cmd, uint8 hotkey)
: StaticTextWidget(boss, x, y, w, h, label), _cmd(cmd), _hotkey(hotkey)
{
_flags = WIDGET_ENABLED | WIDGET_BORDER /* | WIDGET_CLEARBG */ ;
}
@ -118,8 +118,8 @@ static uint32 checked_img[8] = {
0x00000000,
};
CheckboxWidget::CheckboxWidget(Dialog *boss, int x, int y, int w, int h, const char *label, uint32 cmd)
: ButtonWidget(boss, x, y, w, h, label, cmd), _state(false)
CheckboxWidget::CheckboxWidget(Dialog *boss, int x, int y, int w, int h, const char *label, uint32 cmd, uint8 hotkey)
: ButtonWidget(boss, x, y, w, h, label, cmd, hotkey), _state(false)
{
_flags = WIDGET_ENABLED;
}

View file

@ -78,11 +78,12 @@ protected:
/* ButtonWidget */
class ButtonWidget : public StaticTextWidget {
friend class Dialog;
protected:
uint32 _cmd;
uint8 _hotkey;
public:
ButtonWidget(Dialog *boss, int x, int y, int w, int h, const char *label, uint32 cmd);
ButtonWidget(Dialog *boss, int x, int y, int w, int h, const char *label, uint32 cmd = 0, uint8 hotkey = 0);
void setCmd(uint32 cmd) { _cmd = cmd; }
uint32 getCmd() { return _cmd; }
@ -96,7 +97,7 @@ class CheckboxWidget : public ButtonWidget {
protected:
bool _state;
public:
CheckboxWidget(Dialog *boss, int x, int y, int w, int h, const char *label, uint32 cmd);
CheckboxWidget(Dialog *boss, int x, int y, int w, int h, const char *label, uint32 cmd = 0, uint8 hotkey = 0);
void setState(bool state) { _state = state; }
bool getState() { return _state; }

View file

@ -31,7 +31,6 @@
/*
* TODO list
* - add more widgets
* - implement hotkeys
* - add "close" widget to all dialogs (with a flag to turn it off) ?
* - make dialogs "moveable" ?
* - implement the missing / incomplete dialogs