1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
|
/* This file is part of the KDE libraries
Copyright (C) 1997 Mark Donohoe <donohoe@kde.org>
Copyright (C) 1997-2000 Nicolas Hadacek <hadacek@kde.org>
Copyright (C) 1998 Matthias Ettrich <ettrich@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
*/
#ifndef _KACCEL_H
#define _KACCEL_H
#include <qmap.h>
#include <qstring.h>
#include <qaccel.h>
#include <kstdaccel.h>
class QPopupMenu;
class KConfig;
class KConfigBase;
class QObject;
class QWidget;
class KAccelPrivate;
class KKey {
protected:
uint m_keyCombQt;
public:
KKey() { m_keyCombQt = 0; }
KKey( const KKey& k ) { m_keyCombQt = k.m_keyCombQt; }
KKey( uint keyCombQt ) { m_keyCombQt = keyCombQt; }
KKey( const XEvent * );
KKey( const QKeyEvent * );
KKey( const QString& );
KKey& operator =( KKey k ) { m_keyCombQt = k.m_keyCombQt; return *this; }
KKey& operator =( uint keyCombQt ) { m_keyCombQt = keyCombQt; return *this; }
uint key() const { return m_keyCombQt; }
uint sym() const { return m_keyCombQt & 0xffff; }
uint mod() const { return m_keyCombQt & ~0xffff; }
uint state() const { return mod() >> 18; }
QString toString();
};
/**
* Accelerator information, similar to an action.
*
* It is used internally by @ref KAccel.
* @internal
*/
struct KKeyEntry {
public:
int aCurrentKeyCode;
int aDefaultKeyCode; // For keyboards with no meta key
//int aDefaultKeyCode4; // For keyboards with meta key (4 modifiers)
int aConfigKeyCode;
bool bConfigurable;
bool bEnabled;
int aAccelId;
const QObject *receiver;
const char *member;
QString descr;
int menuId;
QPopupMenu *menu;
void operator=(const KKeyEntry& e);
KKeyEntry();
KKeyEntry(const KKeyEntry& e);
};
typedef QMap<QString, KKeyEntry> KKeyEntryMap;
typedef QMap<int, QString> KKeyMapOrder;
/**
* Handle keyboard accelerators.
*
* Allow an user to configure
* key bindings through application configuration files or through the
* @ref KKeyChooser GUI.
*
* A @ref KAccel contains a list of accelerator items. Each accelerator item
* consists of an action name and a keyboard code combined with modifiers
* (Shift, Ctrl and Alt.)
*
* For example, "Ctrl+P" could be a shortcut for printing a document. The key
* codes are listed in ckey.h. "Print" could be the action name for printing.
* The action name identifies the key binding in configuration files and the
* @ref KKeyChooser GUI.
*
* When pressed, an accelerator key calls the slot to which it has been
* connected. Accelerator items can be connected so that a key will activate
* two different slots.
*
* A KAccel object handles key events sent to its parent widget and to all
* children of this parent widget.
*
* Key binding reconfiguration during run time can be prevented by specifying
* that an accelerator item is not configurable when it is inserted. A special
* group of non-configurable key bindings are known as the
* standard accelerators.
*
* The standard accelerators appear repeatedly in applications for
* standard document actions such as printing and saving. Convenience methods are
* available to insert and connect these accelerators which are configurable on
* a desktop-wide basis.
*
* It is possible for a user to choose to have no key associated with
* an action.
*
* The translated first argument for @ref insertItem() is used only
* in the configuration dialog.
*<pre>
* KAccel *a = new KAccel( myWindow );
* // Insert an action "Scroll Up" which is associated with the "Up" key:
* a->insertItem( i18n("Scroll up"), "Scroll Up", "Up" );
* // Insert an action "Scroll Down" which is not associated with any key:
* a->insertItem( i18n("Scroll down"), "Scroll Down", 0);
* a->connectItem( "Scroll up", myWindow, SLOT( scrollUp() ) );
* // a->insertStdItem( KStdAccel::Print ); //not necessary, since it
* // is done automatially with the
* // connect below!
* a->connectItem(KStdAccel::Print, myWindow, SLOT( printDoc() ) );
*
* a->readSettings();
*</pre>
*
* If a shortcut has a menu entry as well, you could insert them like
* this. The example is again the @ref KStdAccel::Print from above.
*
* <pre>
* int id;
* id = popup->insertItem("&Print",this, SLOT(printDoc()));
* a->changeMenuAccel(popup, id, KStdAccel::Print );
* </pre>
*
* If you want a somewhat "exotic" name for your standard print action, like
* id = popup->insertItem(i18n("Print &Document"),this, SLOT(printDoc()));
* it might be a good idea to insert the standard action before as
* a->insertStdItem( KStdAccel::Print, i18n("Print Document") )
* as well, so that the user can easily find the corresponding function.
*
* This technique works for other actions as well. Your "scroll up" function
* in a menu could be done with
*
* <pre>
* id = popup->insertItem(i18n"Scroll &up",this, SLOT(scrollUp()));
* a->changeMenuAccel(popup, id, "Scroll Up" );
* </pre>
*
* Please keep the order right: First insert all functions in the
* acceleratior, then call a -> @ref readSettings() and @em then build your
* menu structure.
*
* @short Configurable key binding support.
* @version $Id: kaccel.h,v 1.55 2001/06/19 18:59:17 ellis Exp $
*/
class KAccel : public QAccel
{
Q_OBJECT
public:
/**
* Create a KAccel object with a parent widget and a name.
*/
KAccel( QWidget * parent, const char *name = 0 );
/**
* Remove all accelerator items.
*/
void clear();
/**
* Connect an accelerator item to a slot/signal in another object.
*
* Arguments:
*
* @param action The accelerator item action name.
* @param receiver The object to receive a signal.
* @param member A slot or signal in the receiver.
* @param activate Indicates whether the accelerator item should be
* enabled immediately.
*/
void connectItem( const QString& action,
const QObject* receiver, const char *member,
bool activate = true );
/**
* Same as the preceding @ref connectItem(), but used for standard
* accelerators.
*
* If the standard accelerator was not inserted so far, it
* will be inserted automatically.
*/
void connectItem( KStdAccel::StdAccel accel,
const QObject* receiver, const char *member,
bool activate = true );
/**
* Retrieve the number of accelerator items.
*/
uint count() const;
/**
* Retrieve the key code of the accelerator item with the action name
* @p action, or zero if either the action name cannot be
* found or the current key is set to no key.
*/
int currentKey( const QString& action ) const;
/**
* Retrieve the description of the accelerator item with the
* action name @p action, or @ref QString::null if the action name cannot
* be found. Useful for menus.
*/
QString description( const QString& action ) const;
void setDescription(const QString &action, const QString &description);
/**
* Retrieve the default key code of the accelerator item with
* the action name
* @p action, or zero if the action name cannot be found.
*/
int defaultKey( const QString& action) const;
/**
* Disconnect an accelerator item from a function in another object.
*/
void disconnectItem( const QString& action,
const QObject* receiver, const char *member );
/**
* Rerieve the identifier of the accelerator item with the keycode @p key,
* or @ref QString::null if the item cannot be found.
*/
QString findKey( int key ) const;
/**
* Insert an accelerator item.
*
* If an action already exists the old association and connections
* will be removed.
*
* @param descr The localized name of the action, useful in
* menus or the keybinding editor.
* @param action The internal accelerator item action name. It
* is supposed to be the same for all languages.
* @param defaultKeyCode A key code to be used as the default
* for the action. Set it to 0 for no default key (It still
* may be configured later.)
* @param configurable Indicates whether a user can configure
* the key binding using the @ref KKeyChooser GUI and whether the
* key will be written back to configuration files when
* @ref writeSettings() is called.
* @return @p true if successful.
*/
bool insertItem( const QString& descr, const QString& action,
int defaultKeyCode, bool configurable = true );
bool insertItem( const QString& descr, const QString& action,
KKey defaultKeyCode3,
KKey defaultKeyCode4,
bool configurable = true );
/**
* Insert an accelerator item.
*
* If an action already exists the old association and connections
* will be removed.
*
* @param descr The localized name of the action, useful in
* menus or the keybinding editor.
* @param action The internal accelerator item action name. It
* is supposed to be the same for all languages.
* @param defaultKeyCode A key code to be used as the default
* for the action. Set it to 0 for no default key (It still
* may be configured later.)
* @param id Menu index of menu items associated with this action.
* @param qmenu Menu containing items associated with this action.
* @param configurable Indicates whether a user can configure
* the key binding using the @ref KKeyChooser GUI and whether the
* key will be written back to configuration files when
* @ref writeSettings() is called.
* @return @p true if successful.
*
*/
bool insertItem( const QString& descr, const QString& action,
int defaultKeyCode, int id, QPopupMenu *qmenu,
bool configurable = true );
bool insertItem( const QString& descr, const QString& action,
KKey defaultKeyCode3,
KKey defaultKeyCode4,
int id, QPopupMenu *qmenu,
bool configurable = true );
/**
* Insert an accelerator item.
*
* If an action already exists the old association and connections
* will be removed.
*
* @param descr The localized name of the action, useful in
* menus or the keybinding editor.
* @param action The internal accelerator item action name. It
* is supposed to be the same for all languages.
* @param defaultKeyCode A key plus a combination of Shift, Ctrl
* and Alt to be used as the default for the action.
* @param id Menu index of menu items associated with this action.
* @param qmenu Menu containing items associated with this action.
* @param configurable Indicates whether a user can configure
* the key binding using the @ref KKeyChooser GUI and whether the
* key will be written back to configuration files when
* @ref writeSettings() is called.
* @return @p true if successful.
*
*/
bool insertItem( const QString& descr, const QString& action,
const QString& defaultKeyCode,
bool configurable = true );
/**
* Insert an accelerator item.
*
* If an action already exists the old association and connections
* will be removed..
*
* @param descr The localized name of the action, useful in
* menus or the keybinding editor.
* @param action The internal accelerator item action name. It
* is supposed to be the same for all languages.
* @param defaultKeyCode A key plus a combination of Shift, Ctrl
* and Alt to be used as the default for the action.
* @param configurable Indicates whether a user can configure
* the key binding using the @ref KKeyChooser GUI and whether the
* key will be written back to configuration files when
* @ref writeSettings() is called.
* @return @p true if successful.
*
*/
bool insertItem( const QString& descr, const QString& action,
const QString& defaultKeyCode,
int id, QPopupMenu *qmenu, bool configurable = true );
/**
* Insert a standard accelerator item.
*
* If an action already exists the old association and connections
* will be removed.
* param id One of the following: @p Open,
* @p New, @p Close, @p Save, @p Print, @p Quit, @p Cut, @p Copy, @p Paste, @p Undo, @p Redo,
* @p Find, @p Replace, @p Insert, @p Home, @p End, @p Prior, @p Next, or @pHelp.
* param descr You can optionally also assign a description to
* the standard item which may be used a in a popup menu.
*/
bool insertStdItem( KStdAccel::StdAccel id, const QString& descr = QString::null );
/**
* Convenience function form of @ref insertItem()
* without the need to specify a localized
* function name for the user.
*
* This is useful if the accelerator
* is used internally only, without appearing in a menu or a
* keybinding editor.
*/
bool insertItem( const QString& action, int defaultKeyCode,
bool configurable = true );
/**
* Convenience function for of @ref insertItem() without the need
* to specify a localized
* function name for the user.
*
* This is useful if the accelerator
* is only used internally, without appearing in a menu or a
* keybinding editor.
*/
bool insertItem( const QString& action, int defaultKeyCode,
int id, QPopupMenu *qmenu,
bool configurable = true );
/**
* Remove the accelerator item with the action name action.
*/
void removeItem( const QString& action );
/**
* Shortcuts should be visible in the menu
* structure of an application.
*
* Use this function for that
* purpose. Note that the action must have been inserted
* before!
*/
void changeMenuAccel ( QPopupMenu *menu, int id,
const QString& action );
/**
* Same as @ref changeMenuAccel() but for standard accelerators.
*/
void changeMenuAccel ( QPopupMenu *menu, int id,
KStdAccel::StdAccel accel );
/**
* Set the dictionary of accelerator action names and @ref KKeyEntry
* objects to @p nKeyDict.
*
* Note that only a shallow copy is made so
* that items will be lost when the @ref KKeyEntry objects are deleted.
*/
bool setKeyDict( const KKeyEntryMap& nKeyDict );
/**
* Retrieve the dictionary of accelerator action names and
* @ref KKeyEntry
* objects. Note that only a shallow copy is returned so that
* items will be lost when the @ref KKeyEntry objects are deleted.
*/
KKeyEntryMap keyDict() const;
// Hack: to be replaced after the 2.2beta testing phase. -- ellis
const KKeyMapOrder& keyInsertOrder() const;
KKeyMapOrder& keyInsertOrder();
/**
* Read all key associations from @p config, or (if @p config
* is zero) from the application's configuration file
* @ref KGlobal::config().
*
* The group in which the configuration is stored can be
* set with @ref setConfigGroup().
*/
void readSettings(KConfig* config = 0);
/// @internal
static void readKeyMap( KKeyEntryMap &aKeyMap, const QString &group,
KConfigBase *config = 0 );
/**
* Write the current configurable associations to @p config,
* or (if @p config is zero) to the application's
* configuration file.
*/
void writeSettings(KConfig* config = 0) const;
/**
* More flexible version of @see writeSettings. You can specify your
* own key map. ( writeSettings calls this function internally )
* @internal
**/
static void writeKeyMap( const KKeyEntryMap &aKeyMap, const QString& group,
KConfig *config = 0, bool global = false );
/**
* Set the group in the configuration file in which the
* accelerator settings are stored.
*
* By default, this is "Keys".
*/
void setConfigGroup( const QString& group );
/**
* Retrieve the name of the group in which accelerator
* settings are stored.
**/
QString configGroup() const;
/**
* If @p global is true, KAccel writes to the global
* configuration file, instead of the application configuration file.
**/
void setConfigGlobal( bool global );
/**
* Will KAccel write to the global configuration file (instead of
* the application configuration file)?
**/
bool configGlobal() const;
/**
* Enable all accelerators if activate is true, or disable it if
* activate is false.
*
* Individual keys can also be enabled or disabled.
*/
void setEnabled( bool activate );
/**
* Are accelerators enabled?
**/
bool isEnabled() const;
/**
* Enable or disable an accelerator item.
*
* @param action The accelerator item action name.
* @param activate Specifies whether the item should be enabled or
* disabled.
*/
void setItemEnabled( const QString& action, bool activate );
/**
* Check whether a specific accelerator, @p action, is enabled.
**/
bool isItemEnabled( const QString& action ) const;
/**
* Returns @p true if keyentry can be modified.
*/
bool configurable( const QString &action ) const;
/**
* Change the keycode for an accelerator.
*/
bool updateItem( const QString &action, int keyCode);
/**
* Remove the keycode for an accelerator.
**/
void clearItem(const QString &action);
/**
* Clear any pointers to a menu.
**/
void removeDeletedMenu(QPopupMenu *menu);
// When bUseFourModifierKeys is on (setting: Global|Keyboard|Use Four Modifier Keys = true | false)
// calls to insertItem will set the current key to aDefaultKeyCode4.
static bool useFourModifierKeys();
static void useFourModifierKeys( bool b );
static bool qtSupportsMetaKey();
/**
* Retrieve the key code corresponding to the string @p sKey or
* zero if the string is not recognized.
*
* The string must be something like "Shift+A",
* "F1+Ctrl+Alt" or "Backspace" for example. That is, the string
* must consist of a key name plus a combination of
* the modifiers Shift, Ctrl and Alt.
*
* N.B.: @p sKey must @em not be @ref i18n()'d!
*/
static int stringToKey( const QString& sKey );
/**
* Retrieve a string corresponding to the key code @p keyCode,
* which is empty if
* @p keyCode is not recognized or zero.
*/
static QString keyToString( int keyCode, bool i18_n = FALSE );
// X11-Related Functions
// I want to move these functions out of KAccel and into their own
// class ASAP.
// Naming Proceedure:
// -CodeX the index of the physical key pressed (keyboard dependent)
// -Sym- key symbol. Either unicode (like 'A') or special key (like delete)
// -Mod- contains bits for modifier flags
// -X Formatted for/by the X sever
// -Qt Formatted for/by Qt
// keyQt Qt shortcut key value containing both Qt Sym and Qt Mod.
// keyEvent- An X or Qt key event
// Example:
// keyCodeXToKeyQt() converts the X11 key code & mod into a Qt shortcut key
enum ModKeysIndex {
ModShiftIndex, ModCapsLockIndex, ModCtrlIndex, ModAltIndex,
ModNumLockIndex, ModModeSwitchIndex, ModMetaIndex, ModScrollLockIndex,
MOD_KEYS
};
static void readModifierMapping();
static uint stringToKey( const QString& keyStr, uchar *pKeyCodeX, uint *pKeySymX, uint *pKeyModX );
static uint keyCodeXToKeySymX( uchar keyCodeX, uint keyModX );
static void keyEventXToKeyX( const XEvent *pEvent, uchar *pKeyCodeX, uint *pKeySymX, uint *pKeyModX );
static uint keyEventXToKeyQt( const XEvent *pEvent );
static int keySymXIndex( uint keySym );
static void keySymXMods( uint keySym, uint *pKeyModQt, uint *pKeyModX );
static uint keyCodeXToKeyQt( uchar keyCodeX, uint keyModX );
static uint keySymXToKeyQt( uint keySymX, uint keyModX );
static void keyQtToKeyX( uint keyCombQt, uchar *pKeyCodeX, uint *pKeySymX, uint *pKeyModX );
static uint keyEventQtToKeyQt( const QKeyEvent* );
static QString keyCodeXToString( uchar keyCodeX, uint keyModX, bool bi18n );
static QString keySymXToString( uint keySymX, uint keyModX, bool bi18n );
// Return the keyModX containing just the bit set for the given modifier.
static uint keyModXShift(); // ShiftMask
static uint keyModXLock(); // LockMask
static uint keyModXCtrl(); // ControlMask
static uint keyModXAlt(); // Normally Mod1Mask
static uint keyModXNumLock(); // Normally Mod2Mask
static uint keyModXModeSwitch(); // Normally Mod3Mask
static uint keyModXMeta(); // Normally Mod4Mask
static uint keyModXScrollLock(); // Normally Mod5Mask
// Return the keyMod mask containing the bits set for the modifiers
// which may be used in accelerator shortcuts.
static uint accelModMaskQt(); // Normally Qt::SHIFT | Qt::CTRL | Qt::ALT | (Qt::ALT<<1)
static uint accelModMaskX(); // Normally ShiftMask | ControlMask | Mod1Mask | Mod3Mask
// Returns true if X has the Meta key assigned to a modifier bit
static bool keyboardHasMetaKey();
signals:
void keycodeChanged();
protected:
int aAvailableId;
KKeyEntryMap aKeyMap;
bool bEnabled;
bool bGlobal;
QString aGroup;
private:
KAccelPrivate *d;
};
#endif
|