OpenASIP  2.0
KeyboardShortcutDialog.cc
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2009 Tampere University.
3 
4  This file is part of TTA-Based Codesign Environment (TCE).
5 
6  Permission is hereby granted, free of charge, to any person obtaining a
7  copy of this software and associated documentation files (the "Software"),
8  to deal in the Software without restriction, including without limitation
9  the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  and/or sell copies of the Software, and to permit persons to whom the
11  Software is furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in
14  all copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  DEALINGS IN THE SOFTWARE.
23  */
24 /**
25  * @file KeyboardShortcutDialog.cc
26  *
27  * Definition of KeyboardShortcutDialog class.
28  *
29  * @author Veli-Pekka Jääskeläinen 2004 (vjaaskel-no.spam-cs.tut.fi)
30  * @note rating: red
31  */
32 
33 #include <string>
34 
36 #include "KeyboardShortcut.hh"
37 #include "WxConversion.hh"
38 #include "Conversion.hh"
39 #include "Application.hh"
40 
41 using std::string;
42 
43 
44 BEGIN_EVENT_TABLE(KeyboardShortcutDialog, wxDialog)
48 
49 
50 /**
51  * The constructor.
52  *
53  * @param parent Parent window of the dialog.
54  * @param shortcut Shortcut to modify.
55  */
57  wxWindow* parent, KeyboardShortcut* shortcut):
58  wxDialog(parent, -1, _T("Edit keyboard shortcut"), wxDefaultPosition),
59  shortcut_(shortcut) {
60 
61  assert(shortcut != NULL);
62 
63  wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL);
64  wxPanel* panel = new wxPanel(this, -1);
65  createContents(panel, true, true);
66  sizer->Add(panel);
67  SetSizer(sizer);
68  sizer->Fit(this);
69  FindWindow(wxID_OK)->Enable(false);
70  panel->SetFocus();
71 }
72 
73 
74 /**
75  * The Destructor.
76  */
78 }
79 
80 
81 /**
82  * Transfers data from dialog attributes to dialog widgets.
83  *
84  * @return True, if the data was succesfully transfered, false othwerwise.
85  */
86 bool
88 
89  string keyName = "";
90 
91  // set the name of the key
92  if (shortcut_->key() > 32 && shortcut_->key() < 127) {
93  // character key
94  keyName = Conversion::toString(shortcut_->key());
95  } else if (shortcut_->key() == 127) {
96  // delete key
97  keyName = "DEL";
98  } else if (shortcut_->fKey() != 0) {
99  // function key
100  keyName = "F"+Conversion::toString(shortcut_->fKey());
101  }
102 
103  wxString key = WxConversion::toWxString(keyName);
104 
105  // prepend key modifiers
106  if (shortcut_->alt()) {
107  key.Prepend(_T("ALT - "));
108  }
109  if (shortcut_->ctrl()) {
110  key.Prepend(_T("CTRL - "));
111  }
112 
113  shortcutField_->SetLabel(key);
114 
115  return true;
116 }
117 
118 
119 
120 /**
121  * Sets a new shortcut for the command.
122  *
123  * @param event Keyevent of the new shortcut.
124  */
125 void
127 
128  int keycode = event.GetKeyCode();
129 
130  PRINT_VAR(keycode);
131  // check that key code is valid
132  if (!((keycode >= int('0') && keycode <= int('9')) ||
133  (keycode >= int('A') && keycode <= int('Z')) ||
134  (keycode == 127) ||
135  (keycode >= WXK_F1 && keycode <= WXK_F12))) {
136 
137  return;
138  }
139 
140  // Check that character key shortcut has at least control
141  // or alt modifier.
142  if (keycode < 256 &&
143  !(event.AltDown() || event.ControlDown())) {
144  return;
145  }
146 
147  shortcut_->setAlt(event.AltDown());
148  shortcut_->setCtrl(event.ControlDown());
149 
150  if (keycode < 256) {
151  // character key shortcut
152  shortcut_->setFKey(0);
153  shortcut_->setKey(keycode);
154  } else if(keycode >= WXK_F1 && keycode <= WXK_F12) {
155  // function key shortcut
156  shortcut_->setKey(0);
157  shortcut_->setFKey(keycode - WXK_F1 + 1);
158  } else {
159  // invalid shortcut
160  assert(false);
161  }
162 
163  FindWindow(wxID_OK)->Enable(true);
165 }
166 
167 
168 /**
169  * Creates the dialog contents.
170  *
171  * This function was initially generated by wxDesigner.
172  * @return Main sizer of the created contents.
173  * @param parent The dialog window.
174  * @param call_fit If true, fits the contents inside the dialog.
175  * @param set_sizer If true, sets the main sizer as dialog contents.
176  */
177 wxSizer*
179  wxWindow *parent, bool call_fit, bool set_sizer) {
180 
181  wxBoxSizer *item0 = new wxBoxSizer( wxVERTICAL );
182  wxStaticText *item1 = new wxStaticText( parent, -1,
183  wxT("Redefine the shortcut by pressing\n"
184  "keys for the new key combination."),
185  wxDefaultPosition, wxDefaultSize, wxALIGN_CENTRE );
186  item0->Add( item1, 0, wxALIGN_CENTER|wxALL, 10 );
187  wxStaticBox *item3 = new wxStaticBox( parent, -1, wxT("Shortcut:") );
188  wxStaticBoxSizer *item2 = new wxStaticBoxSizer( item3, wxHORIZONTAL );
189 
191  new wxStaticText(parent, ID_SHORTCUT, wxT(""), wxDefaultPosition,
192  wxDefaultSize, 0);
193  item2->Add(shortcutField_, 0, wxALIGN_CENTER|wxALL, 5 );
194  item0->Add( item2, 0, wxGROW|wxALL, 5 );
195  wxStaticLine *item5 =
196  new wxStaticLine(parent, -1, wxDefaultPosition, wxSize(20,-1),
197  wxLI_HORIZONTAL );
198  item0->Add( item5, 0, wxGROW|wxALL, 5 );
199  wxBoxSizer *item6 = new wxBoxSizer( wxHORIZONTAL );
200  wxButton *item7 =
201  new wxButton(parent, wxID_OK, wxT("OK"), wxDefaultPosition,
202  wxDefaultSize, 0);
203  item6->Add( item7, 0, wxALIGN_CENTER|wxALL, 5 );
204  wxButton *item8 =
205  new wxButton(parent, wxID_CANCEL, wxT("Cancel"), wxDefaultPosition,
206  wxDefaultSize, 0);
207  item6->Add( item8, 0, wxALIGN_CENTER|wxALL, 5 );
208  item0->Add( item6, 0, wxALIGN_CENTER|wxALL, 5 );
209 
210  if (set_sizer) {
211  parent->SetAutoLayout( TRUE );
212  parent->SetSizer( item0 );
213  if (call_fit)
214  {
215  item0->Fit( parent );
216  item0->SetSizeHints( parent );
217  }
218  }
219 
220  return item0;
221 }
WxConversion::toWxString
static wxString toWxString(const std::string &source)
PRINT_VAR
#define PRINT_VAR(VARIABLE__)
Definition: Application.hh:118
KeyboardShortcut::setKey
void setKey(char key)
Definition: KeyboardShortcut.cc:222
KeyboardShortcutDialog::shortcutField_
wxStaticText * shortcutField_
shortcut text field widget
Definition: KeyboardShortcutDialog.hh:57
KeyboardShortcut::key
char key() const
Definition: KeyboardShortcut.cc:190
FindWindow
Definition: FindWindow.hh:49
KeyboardShortcutDialog::TransferDataToWindow
virtual bool TransferDataToWindow()
Definition: KeyboardShortcutDialog.cc:87
Conversion::toString
static std::string toString(const T &source)
assert
#define assert(condition)
Definition: Application.hh:86
KeyboardShortcut.hh
KeyboardShortcutDialog
Definition: KeyboardShortcutDialog.hh:44
Conversion.hh
KeyboardShortcutDialog::createContents
wxSizer * createContents(wxWindow *parent, bool callFit, bool set_sizer)
Definition: KeyboardShortcutDialog.cc:178
Application.hh
KeyboardShortcutDialog::onCharEvent
void onCharEvent(wxKeyEvent &event)
Definition: KeyboardShortcutDialog.cc:126
TRUE
const string TRUE
Value used for true in attribute and element values.
Definition: GUIOptionsSerializer.cc:65
KeyboardShortcut::setFKey
void setFKey(int fKey)
Definition: KeyboardShortcut.cc:233
KeyboardShortcut
Definition: KeyboardShortcut.hh:50
KeyboardShortcut::fKey
int fKey() const
Definition: KeyboardShortcut.cc:149
KeyboardShortcutDialog.hh
KeyboardShortcutDialog::ID_SHORTCUT
@ ID_SHORTCUT
Definition: KeyboardShortcutDialog.hh:61
WxConversion.hh
KeyboardShortcut::setAlt
void setAlt(bool alt)
Definition: KeyboardShortcut.cc:212
KeyboardShortcutDialog::~KeyboardShortcutDialog
virtual ~KeyboardShortcutDialog()
Definition: KeyboardShortcutDialog.cc:77
KeyboardShortcut::setCtrl
void setCtrl(bool ctrl)
Definition: KeyboardShortcut.cc:201
KeyboardShortcutDialog::shortcut_
KeyboardShortcut * shortcut_
modified shortcut
Definition: KeyboardShortcutDialog.hh:55
KeyboardShortcut::ctrl
bool ctrl() const
Definition: KeyboardShortcut.cc:165
KeyboardShortcut::alt
bool alt() const
Definition: KeyboardShortcut.cc:177
END_EVENT_TABLE
END_EVENT_TABLE() using namespace IDF