1 /* 2 * This file is part of gtkD. 3 * 4 * gtkD is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU Lesser General Public License 6 * as published by the Free Software Foundation; either version 3 7 * of the License, or (at your option) any later version, with 8 * some exceptions, please read the COPYING file. 9 * 10 * gtkD is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU Lesser General Public License for more details. 14 * 15 * You should have received a copy of the GNU Lesser General Public License 16 * along with gtkD; if not, write to the Free Software 17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA 18 */ 19 20 // generated automatically - do not change 21 // find conversion definition on APILookup.txt 22 // implement new conversion functionalities on the wrap.utils pakage 23 24 25 module gdk.Keymap; 26 27 private import gdk.Display; 28 private import glib.Str; 29 private import gobject.ObjectG; 30 private import gobject.Signals; 31 private import gtkc.gdk; 32 public import gtkc.gdktypes; 33 private import std.algorithm; 34 35 36 /** 37 * A #GdkKeymap defines the translation from keyboard state 38 * (including a hardware key, a modifier mask, and active keyboard group) 39 * to a keyval. This translation has two phases. The first phase is 40 * to determine the effective keyboard group and level for the keyboard 41 * state; the second phase is to look up the keycode/group/level triplet 42 * in the keymap and see what keyval it corresponds to. 43 */ 44 public class Keymap : ObjectG 45 { 46 /** the main Gtk struct */ 47 protected GdkKeymap* gdkKeymap; 48 49 /** Get the main Gtk struct */ 50 public GdkKeymap* getKeymapStruct(bool transferOwnership = false) 51 { 52 if (transferOwnership) 53 ownedRef = false; 54 return gdkKeymap; 55 } 56 57 /** the main Gtk struct as a void* */ 58 protected override void* getStruct() 59 { 60 return cast(void*)gdkKeymap; 61 } 62 63 protected override void setStruct(GObject* obj) 64 { 65 gdkKeymap = cast(GdkKeymap*)obj; 66 super.setStruct(obj); 67 } 68 69 /** 70 * Sets our main struct and passes it to the parent class. 71 */ 72 public this (GdkKeymap* gdkKeymap, bool ownedRef = false) 73 { 74 this.gdkKeymap = gdkKeymap; 75 super(cast(GObject*)gdkKeymap, ownedRef); 76 } 77 78 79 /** */ 80 public static GType getType() 81 { 82 return gdk_keymap_get_type(); 83 } 84 85 /** 86 * Returns the #GdkKeymap attached to the default display. 87 * 88 * Returns: the #GdkKeymap attached to the default display. 89 */ 90 public static Keymap getDefault() 91 { 92 auto p = gdk_keymap_get_default(); 93 94 if(p is null) 95 { 96 return null; 97 } 98 99 return ObjectG.getDObject!(Keymap)(cast(GdkKeymap*) p); 100 } 101 102 /** 103 * Returns the #GdkKeymap attached to @display. 104 * 105 * Params: 106 * display = the #GdkDisplay. 107 * 108 * Returns: the #GdkKeymap attached to @display. 109 * 110 * Since: 2.2 111 */ 112 public static Keymap getForDisplay(Display display) 113 { 114 auto p = gdk_keymap_get_for_display((display is null) ? null : display.getDisplayStruct()); 115 116 if(p is null) 117 { 118 return null; 119 } 120 121 return ObjectG.getDObject!(Keymap)(cast(GdkKeymap*) p); 122 } 123 124 /** 125 * Maps the non-virtual modifiers (i.e Mod2, Mod3, ...) which are set 126 * in @state to the virtual modifiers (i.e. Super, Hyper and Meta) and 127 * set the corresponding bits in @state. 128 * 129 * GDK already does this before delivering key events, but for 130 * compatibility reasons, it only sets the first virtual modifier 131 * it finds, whereas this function sets all matching virtual modifiers. 132 * 133 * This function is useful when matching key events against 134 * accelerators. 135 * 136 * Params: 137 * state = pointer to the modifier mask to change 138 * 139 * Since: 2.20 140 */ 141 public void addVirtualModifiers(ref GdkModifierType state) 142 { 143 gdk_keymap_add_virtual_modifiers(gdkKeymap, &state); 144 } 145 146 /** 147 * Returns whether the Caps Lock modifer is locked. 148 * 149 * Returns: %TRUE if Caps Lock is on 150 * 151 * Since: 2.16 152 */ 153 public bool getCapsLockState() 154 { 155 return gdk_keymap_get_caps_lock_state(gdkKeymap) != 0; 156 } 157 158 /** 159 * Returns the direction of effective layout of the keymap. 160 * 161 * Returns: %PANGO_DIRECTION_LTR or %PANGO_DIRECTION_RTL 162 * if it can determine the direction. %PANGO_DIRECTION_NEUTRAL 163 * otherwise. 164 */ 165 public PangoDirection getDirection() 166 { 167 return gdk_keymap_get_direction(gdkKeymap); 168 } 169 170 /** 171 * Returns the keyvals bound to @hardware_keycode. 172 * The Nth #GdkKeymapKey in @keys is bound to the Nth 173 * keyval in @keyvals. Free the returned arrays with g_free(). 174 * When a keycode is pressed by the user, the keyval from 175 * this list of entries is selected by considering the effective 176 * keyboard group and level. See gdk_keymap_translate_keyboard_state(). 177 * 178 * Params: 179 * hardwareKeycode = a keycode 180 * keys = return 181 * location for array of #GdkKeymapKey, or %NULL 182 * keyvals = return 183 * location for array of keyvals, or %NULL 184 * nEntries = length of @keys and @keyvals 185 * 186 * Returns: %TRUE if there were any entries 187 */ 188 public bool getEntriesForKeycode(uint hardwareKeycode, out GdkKeymapKey[] keys, out uint[] keyvals) 189 { 190 GdkKeymapKey* outkeys = null; 191 uint* outkeyvals = null; 192 int nEntries; 193 194 auto p = gdk_keymap_get_entries_for_keycode(gdkKeymap, hardwareKeycode, &outkeys, &outkeyvals, &nEntries) != 0; 195 196 keys = outkeys[0 .. nEntries]; 197 keyvals = outkeyvals[0 .. nEntries]; 198 199 return p; 200 } 201 202 /** 203 * Obtains a list of keycode/group/level combinations that will 204 * generate @keyval. Groups and levels are two kinds of keyboard mode; 205 * in general, the level determines whether the top or bottom symbol 206 * on a key is used, and the group determines whether the left or 207 * right symbol is used. On US keyboards, the shift key changes the 208 * keyboard level, and there are no groups. A group switch key might 209 * convert a keyboard between Hebrew to English modes, for example. 210 * #GdkEventKey contains a %group field that indicates the active 211 * keyboard group. The level is computed from the modifier mask. 212 * The returned array should be freed 213 * with g_free(). 214 * 215 * Params: 216 * keyval = a keyval, such as %GDK_KEY_a, %GDK_KEY_Up, %GDK_KEY_Return, etc. 217 * keys = return location 218 * for an array of #GdkKeymapKey 219 * nKeys = return location for number of elements in returned array 220 * 221 * Returns: %TRUE if keys were found and returned 222 */ 223 public bool getEntriesForKeyval(uint keyval, out GdkKeymapKey[] keys) 224 { 225 GdkKeymapKey* outkeys = null; 226 int nKeys; 227 228 auto p = gdk_keymap_get_entries_for_keyval(gdkKeymap, keyval, &outkeys, &nKeys) != 0; 229 230 keys = outkeys[0 .. nKeys]; 231 232 return p; 233 } 234 235 /** 236 * Returns the modifier mask the @keymap’s windowing system backend 237 * uses for a particular purpose. 238 * 239 * Note that this function always returns real hardware modifiers, not 240 * virtual ones (e.g. it will return #GDK_MOD1_MASK rather than 241 * #GDK_META_MASK if the backend maps MOD1 to META), so there are use 242 * cases where the return value of this function has to be transformed 243 * by gdk_keymap_add_virtual_modifiers() in order to contain the 244 * expected result. 245 * 246 * Params: 247 * intent = the use case for the modifier mask 248 * 249 * Returns: the modifier mask used for @intent. 250 * 251 * Since: 3.4 252 */ 253 public GdkModifierType getModifierMask(GdkModifierIntent intent) 254 { 255 return gdk_keymap_get_modifier_mask(gdkKeymap, intent); 256 } 257 258 /** 259 * Returns the current modifier state. 260 * 261 * Returns: the current modifier state. 262 * 263 * Since: 3.4 264 */ 265 public uint getModifierState() 266 { 267 return gdk_keymap_get_modifier_state(gdkKeymap); 268 } 269 270 /** 271 * Returns whether the Num Lock modifer is locked. 272 * 273 * Returns: %TRUE if Num Lock is on 274 * 275 * Since: 3.0 276 */ 277 public bool getNumLockState() 278 { 279 return gdk_keymap_get_num_lock_state(gdkKeymap) != 0; 280 } 281 282 /** 283 * Returns whether the Scroll Lock modifer is locked. 284 * 285 * Returns: %TRUE if Scroll Lock is on 286 * 287 * Since: 3.18 288 */ 289 public bool getScrollLockState() 290 { 291 return gdk_keymap_get_scroll_lock_state(gdkKeymap) != 0; 292 } 293 294 /** 295 * Determines if keyboard layouts for both right-to-left and left-to-right 296 * languages are in use. 297 * 298 * Returns: %TRUE if there are layouts in both directions, %FALSE otherwise 299 * 300 * Since: 2.12 301 */ 302 public bool haveBidiLayouts() 303 { 304 return gdk_keymap_have_bidi_layouts(gdkKeymap) != 0; 305 } 306 307 /** 308 * Looks up the keyval mapped to a keycode/group/level triplet. 309 * If no keyval is bound to @key, returns 0. For normal user input, 310 * you want to use gdk_keymap_translate_keyboard_state() instead of 311 * this function, since the effective group/level may not be 312 * the same as the current keyboard state. 313 * 314 * Params: 315 * key = a #GdkKeymapKey with keycode, group, and level initialized 316 * 317 * Returns: a keyval, or 0 if none was mapped to the given @key 318 */ 319 public uint lookupKey(GdkKeymapKey* key) 320 { 321 return gdk_keymap_lookup_key(gdkKeymap, key); 322 } 323 324 /** 325 * Maps the virtual modifiers (i.e. Super, Hyper and Meta) which 326 * are set in @state to their non-virtual counterparts (i.e. Mod2, 327 * Mod3,...) and set the corresponding bits in @state. 328 * 329 * This function is useful when matching key events against 330 * accelerators. 331 * 332 * Params: 333 * state = pointer to the modifier state to map 334 * 335 * Returns: %FALSE if two virtual modifiers were mapped to the 336 * same non-virtual modifier. Note that %FALSE is also returned 337 * if a virtual modifier is mapped to a non-virtual modifier that 338 * was already set in @state. 339 * 340 * Since: 2.20 341 */ 342 public bool mapVirtualModifiers(ref GdkModifierType state) 343 { 344 return gdk_keymap_map_virtual_modifiers(gdkKeymap, &state) != 0; 345 } 346 347 /** 348 * Translates the contents of a #GdkEventKey into a keyval, effective 349 * group, and level. Modifiers that affected the translation and 350 * are thus unavailable for application use are returned in 351 * @consumed_modifiers. 352 * See [Groups][key-group-explanation] for an explanation of 353 * groups and levels. The @effective_group is the group that was 354 * actually used for the translation; some keys such as Enter are not 355 * affected by the active keyboard group. The @level is derived from 356 * @state. For convenience, #GdkEventKey already contains the translated 357 * keyval, so this function isn’t as useful as you might think. 358 * 359 * @consumed_modifiers gives modifiers that should be masked outfrom @state 360 * when comparing this key press to a hot key. For instance, on a US keyboard, 361 * the `plus` symbol is shifted, so when comparing a key press to a 362 * `<Control>plus` accelerator `<Shift>` should be masked out. 363 * 364 * |[<!-- language="C" --> 365 * // We want to ignore irrelevant modifiers like ScrollLock 366 * #define ALL_ACCELS_MASK (GDK_CONTROL_MASK | GDK_SHIFT_MASK | GDK_MOD1_MASK) 367 * gdk_keymap_translate_keyboard_state (keymap, event->hardware_keycode, 368 * event->state, event->group, 369 * &keyval, NULL, NULL, &consumed); 370 * if (keyval == GDK_PLUS && 371 * (event->state & ~consumed & ALL_ACCELS_MASK) == GDK_CONTROL_MASK) 372 * // Control was pressed 373 * ]| 374 * 375 * An older interpretation @consumed_modifiers was that it contained 376 * all modifiers that might affect the translation of the key; 377 * this allowed accelerators to be stored with irrelevant consumed 378 * modifiers, by doing: 379 * |[<!-- language="C" --> 380 * // XXX Don’t do this XXX 381 * if (keyval == accel_keyval && 382 * (event->state & ~consumed & ALL_ACCELS_MASK) == (accel_mods & ~consumed)) 383 * // Accelerator was pressed 384 * ]| 385 * 386 * However, this did not work if multi-modifier combinations were 387 * used in the keymap, since, for instance, `<Control>` would be 388 * masked out even if only `<Control><Alt>` was used in the keymap. 389 * To support this usage as well as well as possible, all single 390 * modifier combinations that could affect the key for any combination 391 * of modifiers will be returned in @consumed_modifiers; multi-modifier 392 * combinations are returned only when actually found in @state. When 393 * you store accelerators, you should always store them with consumed 394 * modifiers removed. Store `<Control>plus`, not `<Control><Shift>plus`, 395 * 396 * Params: 397 * hardwareKeycode = a keycode 398 * state = a modifier state 399 * group = active keyboard group 400 * keyval = return location for keyval, or %NULL 401 * effectiveGroup = return location for effective 402 * group, or %NULL 403 * level = return location for level, or %NULL 404 * consumedModifiers = return location for modifiers 405 * that were used to determine the group or level, or %NULL 406 * 407 * Returns: %TRUE if there was a keyval bound to the keycode/state/group 408 */ 409 public bool translateKeyboardState(uint hardwareKeycode, GdkModifierType state, int group, out uint keyval, out int effectiveGroup, out int level, out GdkModifierType consumedModifiers) 410 { 411 return gdk_keymap_translate_keyboard_state(gdkKeymap, hardwareKeycode, state, group, &keyval, &effectiveGroup, &level, &consumedModifiers) != 0; 412 } 413 414 protected class OnDirectionChangedDelegateWrapper 415 { 416 void delegate(Keymap) dlg; 417 gulong handlerId; 418 419 this(void delegate(Keymap) dlg) 420 { 421 this.dlg = dlg; 422 onDirectionChangedListeners ~= this; 423 } 424 425 void remove(OnDirectionChangedDelegateWrapper source) 426 { 427 foreach(index, wrapper; onDirectionChangedListeners) 428 { 429 if (wrapper.handlerId == source.handlerId) 430 { 431 onDirectionChangedListeners[index] = null; 432 onDirectionChangedListeners = std.algorithm.remove(onDirectionChangedListeners, index); 433 break; 434 } 435 } 436 } 437 } 438 OnDirectionChangedDelegateWrapper[] onDirectionChangedListeners; 439 440 /** 441 * The ::direction-changed signal gets emitted when the direction of 442 * the keymap changes. 443 * 444 * Since: 2.0 445 */ 446 gulong addOnDirectionChanged(void delegate(Keymap) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 447 { 448 auto wrapper = new OnDirectionChangedDelegateWrapper(dlg); 449 wrapper.handlerId = Signals.connectData( 450 this, 451 "direction-changed", 452 cast(GCallback)&callBackDirectionChanged, 453 cast(void*)wrapper, 454 cast(GClosureNotify)&callBackDirectionChangedDestroy, 455 connectFlags); 456 return wrapper.handlerId; 457 } 458 459 extern(C) static void callBackDirectionChanged(GdkKeymap* keymapStruct, OnDirectionChangedDelegateWrapper wrapper) 460 { 461 wrapper.dlg(wrapper.outer); 462 } 463 464 extern(C) static void callBackDirectionChangedDestroy(OnDirectionChangedDelegateWrapper wrapper, GClosure* closure) 465 { 466 wrapper.remove(wrapper); 467 } 468 469 protected class OnKeysChangedDelegateWrapper 470 { 471 void delegate(Keymap) dlg; 472 gulong handlerId; 473 474 this(void delegate(Keymap) dlg) 475 { 476 this.dlg = dlg; 477 onKeysChangedListeners ~= this; 478 } 479 480 void remove(OnKeysChangedDelegateWrapper source) 481 { 482 foreach(index, wrapper; onKeysChangedListeners) 483 { 484 if (wrapper.handlerId == source.handlerId) 485 { 486 onKeysChangedListeners[index] = null; 487 onKeysChangedListeners = std.algorithm.remove(onKeysChangedListeners, index); 488 break; 489 } 490 } 491 } 492 } 493 OnKeysChangedDelegateWrapper[] onKeysChangedListeners; 494 495 /** 496 * The ::keys-changed signal is emitted when the mapping represented by 497 * @keymap changes. 498 * 499 * Since: 2.2 500 */ 501 gulong addOnKeysChanged(void delegate(Keymap) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 502 { 503 auto wrapper = new OnKeysChangedDelegateWrapper(dlg); 504 wrapper.handlerId = Signals.connectData( 505 this, 506 "keys-changed", 507 cast(GCallback)&callBackKeysChanged, 508 cast(void*)wrapper, 509 cast(GClosureNotify)&callBackKeysChangedDestroy, 510 connectFlags); 511 return wrapper.handlerId; 512 } 513 514 extern(C) static void callBackKeysChanged(GdkKeymap* keymapStruct, OnKeysChangedDelegateWrapper wrapper) 515 { 516 wrapper.dlg(wrapper.outer); 517 } 518 519 extern(C) static void callBackKeysChangedDestroy(OnKeysChangedDelegateWrapper wrapper, GClosure* closure) 520 { 521 wrapper.remove(wrapper); 522 } 523 524 protected class OnStateChangedDelegateWrapper 525 { 526 void delegate(Keymap) dlg; 527 gulong handlerId; 528 529 this(void delegate(Keymap) dlg) 530 { 531 this.dlg = dlg; 532 onStateChangedListeners ~= this; 533 } 534 535 void remove(OnStateChangedDelegateWrapper source) 536 { 537 foreach(index, wrapper; onStateChangedListeners) 538 { 539 if (wrapper.handlerId == source.handlerId) 540 { 541 onStateChangedListeners[index] = null; 542 onStateChangedListeners = std.algorithm.remove(onStateChangedListeners, index); 543 break; 544 } 545 } 546 } 547 } 548 OnStateChangedDelegateWrapper[] onStateChangedListeners; 549 550 /** 551 * The ::state-changed signal is emitted when the state of the 552 * keyboard changes, e.g when Caps Lock is turned on or off. 553 * See gdk_keymap_get_caps_lock_state(). 554 * 555 * Since: 2.16 556 */ 557 gulong addOnStateChanged(void delegate(Keymap) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 558 { 559 auto wrapper = new OnStateChangedDelegateWrapper(dlg); 560 wrapper.handlerId = Signals.connectData( 561 this, 562 "state-changed", 563 cast(GCallback)&callBackStateChanged, 564 cast(void*)wrapper, 565 cast(GClosureNotify)&callBackStateChangedDestroy, 566 connectFlags); 567 return wrapper.handlerId; 568 } 569 570 extern(C) static void callBackStateChanged(GdkKeymap* keymapStruct, OnStateChangedDelegateWrapper wrapper) 571 { 572 wrapper.dlg(wrapper.outer); 573 } 574 575 extern(C) static void callBackStateChangedDestroy(OnStateChangedDelegateWrapper wrapper, GClosure* closure) 576 { 577 wrapper.remove(wrapper); 578 } 579 580 /** 581 * Obtains the upper- and lower-case versions of the keyval @symbol. 582 * Examples of keyvals are #GDK_KEY_a, #GDK_KEY_Enter, #GDK_KEY_F1, etc. 583 * 584 * Params: 585 * symbol = a keyval 586 * lower = return location for lowercase version of @symbol 587 * upper = return location for uppercase version of @symbol 588 */ 589 public static void keyvalConvertCase(uint symbol, out uint lower, out uint upper) 590 { 591 gdk_keyval_convert_case(symbol, &lower, &upper); 592 } 593 594 /** 595 * Converts a key name to a key value. 596 * 597 * The names are the same as those in the 598 * `gdk/gdkkeysyms.h` header file 599 * but without the leading “GDK_KEY_”. 600 * 601 * Params: 602 * keyvalName = a key name 603 * 604 * Returns: the corresponding key value, or %GDK_KEY_VoidSymbol 605 * if the key name is not a valid key 606 */ 607 public static uint keyvalFromName(string keyvalName) 608 { 609 return gdk_keyval_from_name(Str.toStringz(keyvalName)); 610 } 611 612 /** 613 * Returns %TRUE if the given key value is in lower case. 614 * 615 * Params: 616 * keyval = a key value. 617 * 618 * Returns: %TRUE if @keyval is in lower case, or if @keyval is not 619 * subject to case conversion. 620 */ 621 public static bool keyvalIsLower(uint keyval) 622 { 623 return gdk_keyval_is_lower(keyval) != 0; 624 } 625 626 /** 627 * Returns %TRUE if the given key value is in upper case. 628 * 629 * Params: 630 * keyval = a key value. 631 * 632 * Returns: %TRUE if @keyval is in upper case, or if @keyval is not subject to 633 * case conversion. 634 */ 635 public static bool keyvalIsUpper(uint keyval) 636 { 637 return gdk_keyval_is_upper(keyval) != 0; 638 } 639 640 /** 641 * Converts a key value into a symbolic name. 642 * 643 * The names are the same as those in the 644 * `gdk/gdkkeysyms.h` header file 645 * but without the leading “GDK_KEY_”. 646 * 647 * Params: 648 * keyval = a key value 649 * 650 * Returns: a string containing the name 651 * of the key, or %NULL if @keyval is not a valid key. The string 652 * should not be modified. 653 */ 654 public static string keyvalName(uint keyval) 655 { 656 return Str.toString(gdk_keyval_name(keyval)); 657 } 658 659 /** 660 * Converts a key value to lower case, if applicable. 661 * 662 * Params: 663 * keyval = a key value. 664 * 665 * Returns: the lower case form of @keyval, or @keyval itself if it is already 666 * in lower case or it is not subject to case conversion. 667 */ 668 public static uint keyvalToLower(uint keyval) 669 { 670 return gdk_keyval_to_lower(keyval); 671 } 672 673 /** 674 * Convert from a GDK key symbol to the corresponding ISO10646 (Unicode) 675 * character. 676 * 677 * Params: 678 * keyval = a GDK key symbol 679 * 680 * Returns: the corresponding unicode character, or 0 if there 681 * is no corresponding character. 682 */ 683 public static uint keyvalToUnicode(uint keyval) 684 { 685 return gdk_keyval_to_unicode(keyval); 686 } 687 688 /** 689 * Converts a key value to upper case, if applicable. 690 * 691 * Params: 692 * keyval = a key value. 693 * 694 * Returns: the upper case form of @keyval, or @keyval itself if it is already 695 * in upper case or it is not subject to case conversion. 696 */ 697 public static uint keyvalToUpper(uint keyval) 698 { 699 return gdk_keyval_to_upper(keyval); 700 } 701 702 /** 703 * Convert from a ISO10646 character to a key symbol. 704 * 705 * Params: 706 * wc = a ISO10646 encoded character 707 * 708 * Returns: the corresponding GDK key symbol, if one exists. 709 * or, if there is no corresponding symbol, 710 * wc | 0x01000000 711 */ 712 public static uint unicodeToKeyval(uint wc) 713 { 714 return gdk_unicode_to_keyval(wc); 715 } 716 }