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