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 	 * Determines if keyboard layouts for both right-to-left and left-to-right
282 	 * languages are in use.
283 	 *
284 	 * Return: %TRUE if there are layouts in both directions, %FALSE otherwise
285 	 *
286 	 * Since: 2.12
287 	 */
288 	public bool haveBidiLayouts()
289 	{
290 		return gdk_keymap_have_bidi_layouts(gdkKeymap) != 0;
291 	}
292 
293 	/**
294 	 * Looks up the keyval mapped to a keycode/group/level triplet.
295 	 * If no keyval is bound to @key, returns 0. For normal user input,
296 	 * you want to use gdk_keymap_translate_keyboard_state() instead of
297 	 * this function, since the effective group/level may not be
298 	 * the same as the current keyboard state.
299 	 *
300 	 * Params:
301 	 *     key = a #GdkKeymapKey with keycode, group, and level initialized
302 	 *
303 	 * Return: a keyval, or 0 if none was mapped to the given @key
304 	 */
305 	public uint lookupKey(GdkKeymapKey* key)
306 	{
307 		return gdk_keymap_lookup_key(gdkKeymap, key);
308 	}
309 
310 	/**
311 	 * Maps the virtual modifiers (i.e. Super, Hyper and Meta) which
312 	 * are set in @state to their non-virtual counterparts (i.e. Mod2,
313 	 * Mod3,...) and set the corresponding bits in @state.
314 	 *
315 	 * This function is useful when matching key events against
316 	 * accelerators.
317 	 *
318 	 * Params:
319 	 *     state = pointer to the modifier state to map
320 	 *
321 	 * Return: %TRUE if no virtual modifiers were mapped to the
322 	 *     same non-virtual modifier. Note that %FALSE is also returned
323 	 *     if a virtual modifier is mapped to a non-virtual modifier that
324 	 *     was already set in @state.
325 	 *
326 	 * Since: 2.20
327 	 */
328 	public bool mapVirtualModifiers(ref GdkModifierType state)
329 	{
330 		return gdk_keymap_map_virtual_modifiers(gdkKeymap, &state) != 0;
331 	}
332 
333 	/**
334 	 * Translates the contents of a #GdkEventKey into a keyval, effective
335 	 * group, and level. Modifiers that affected the translation and
336 	 * are thus unavailable for application use are returned in
337 	 * @consumed_modifiers.
338 	 * See [Groups][key-group-explanation] for an explanation of
339 	 * groups and levels. The @effective_group is the group that was
340 	 * actually used for the translation; some keys such as Enter are not
341 	 * affected by the active keyboard group. The @level is derived from
342 	 * @state. For convenience, #GdkEventKey already contains the translated
343 	 * keyval, so this function isn’t as useful as you might think.
344 	 *
345 	 * @consumed_modifiers gives modifiers that should be masked outfrom @state
346 	 * when comparing this key press to a hot key. For instance, on a US keyboard,
347 	 * the `plus` symbol is shifted, so when comparing a key press to a
348 	 * `<Control>plus` accelerator `<Shift>` should be masked out.
349 	 *
350 	 * |[<!-- language="C" -->
351 	 * // We want to ignore irrelevant modifiers like ScrollLock
352 	 * #define ALL_ACCELS_MASK (GDK_CONTROL_MASK | GDK_SHIFT_MASK | GDK_MOD1_MASK)
353 	 * gdk_keymap_translate_keyboard_state (keymap, event->hardware_keycode,
354 	 * event->state, event->group,
355 	 * &keyval, NULL, NULL, &consumed);
356 	 * if (keyval == GDK_PLUS &&
357 	 * (event->state & ~consumed & ALL_ACCELS_MASK) == GDK_CONTROL_MASK)
358 	 * // Control was pressed
359 	 * ]|
360 	 *
361 	 * An older interpretation @consumed_modifiers was that it contained
362 	 * all modifiers that might affect the translation of the key;
363 	 * this allowed accelerators to be stored with irrelevant consumed
364 	 * modifiers, by doing:
365 	 * |[<!-- language="C" -->
366 	 * // XXX Don’t do this XXX
367 	 * if (keyval == accel_keyval &&
368 	 * (event->state & ~consumed & ALL_ACCELS_MASK) == (accel_mods & ~consumed))
369 	 * // Accelerator was pressed
370 	 * ]|
371 	 *
372 	 * However, this did not work if multi-modifier combinations were
373 	 * used in the keymap, since, for instance, `<Control>` would be
374 	 * masked out even if only `<Control><Alt>` was used in the keymap.
375 	 * To support this usage as well as well as possible, all single
376 	 * modifier combinations that could affect the key for any combination
377 	 * of modifiers will be returned in @consumed_modifiers; multi-modifier
378 	 * combinations are returned only when actually found in @state. When
379 	 * you store accelerators, you should always store them with consumed
380 	 * modifiers removed. Store `<Control>plus`, not `<Control><Shift>plus`,
381 	 *
382 	 * Params:
383 	 *     hardwareKeycode = a keycode
384 	 *     state = a modifier state
385 	 *     group = active keyboard group
386 	 *     keyval = return location for keyval, or %NULL
387 	 *     effectiveGroup = return location for effective
388 	 *         group, or %NULL
389 	 *     level = return location for level, or %NULL
390 	 *     consumedModifiers = return location for modifiers
391 	 *         that were used to determine the group or level, or %NULL
392 	 *
393 	 * Return: %TRUE if there was a keyval bound to the keycode/state/group
394 	 */
395 	public bool translateKeyboardState(uint hardwareKeycode, GdkModifierType state, int group, out uint keyval, out int effectiveGroup, out int level, out GdkModifierType consumedModifiers)
396 	{
397 		return gdk_keymap_translate_keyboard_state(gdkKeymap, hardwareKeycode, state, group, &keyval, &effectiveGroup, &level, &consumedModifiers) != 0;
398 	}
399 
400 	int[string] connectedSignals;
401 
402 	void delegate(Keymap)[] onDirectionChangedListeners;
403 	/**
404 	 * The ::direction-changed signal gets emitted when the direction of
405 	 * the keymap changes.
406 	 *
407 	 * Since: 2.0
408 	 */
409 	void addOnDirectionChanged(void delegate(Keymap) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
410 	{
411 		if ( "direction-changed" !in connectedSignals )
412 		{
413 			Signals.connectData(
414 				this,
415 				"direction-changed",
416 				cast(GCallback)&callBackDirectionChanged,
417 				cast(void*)this,
418 				null,
419 				connectFlags);
420 			connectedSignals["direction-changed"] = 1;
421 		}
422 		onDirectionChangedListeners ~= dlg;
423 	}
424 	extern(C) static void callBackDirectionChanged(GdkKeymap* keymapStruct, Keymap _keymap)
425 	{
426 		foreach ( void delegate(Keymap) dlg; _keymap.onDirectionChangedListeners )
427 		{
428 			dlg(_keymap);
429 		}
430 	}
431 
432 	void delegate(Keymap)[] onKeysChangedListeners;
433 	/**
434 	 * The ::keys-changed signal is emitted when the mapping represented by
435 	 * @keymap changes.
436 	 *
437 	 * Since: 2.2
438 	 */
439 	void addOnKeysChanged(void delegate(Keymap) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
440 	{
441 		if ( "keys-changed" !in connectedSignals )
442 		{
443 			Signals.connectData(
444 				this,
445 				"keys-changed",
446 				cast(GCallback)&callBackKeysChanged,
447 				cast(void*)this,
448 				null,
449 				connectFlags);
450 			connectedSignals["keys-changed"] = 1;
451 		}
452 		onKeysChangedListeners ~= dlg;
453 	}
454 	extern(C) static void callBackKeysChanged(GdkKeymap* keymapStruct, Keymap _keymap)
455 	{
456 		foreach ( void delegate(Keymap) dlg; _keymap.onKeysChangedListeners )
457 		{
458 			dlg(_keymap);
459 		}
460 	}
461 
462 	void delegate(Keymap)[] onStateChangedListeners;
463 	/**
464 	 * The ::state-changed signal is emitted when the state of the
465 	 * keyboard changes, e.g when Caps Lock is turned on or off.
466 	 * See gdk_keymap_get_caps_lock_state().
467 	 *
468 	 * Since: 2.16
469 	 */
470 	void addOnStateChanged(void delegate(Keymap) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
471 	{
472 		if ( "state-changed" !in connectedSignals )
473 		{
474 			Signals.connectData(
475 				this,
476 				"state-changed",
477 				cast(GCallback)&callBackStateChanged,
478 				cast(void*)this,
479 				null,
480 				connectFlags);
481 			connectedSignals["state-changed"] = 1;
482 		}
483 		onStateChangedListeners ~= dlg;
484 	}
485 	extern(C) static void callBackStateChanged(GdkKeymap* keymapStruct, Keymap _keymap)
486 	{
487 		foreach ( void delegate(Keymap) dlg; _keymap.onStateChangedListeners )
488 		{
489 			dlg(_keymap);
490 		}
491 	}
492 
493 	/**
494 	 * Obtains the upper- and lower-case versions of the keyval @symbol.
495 	 * Examples of keyvals are #GDK_KEY_a, #GDK_KEY_Enter, #GDK_KEY_F1, etc.
496 	 *
497 	 * Params:
498 	 *     symbol = a keyval
499 	 *     lower = return location for lowercase version of @symbol
500 	 *     upper = return location for uppercase version of @symbol
501 	 */
502 	public static void keyvalConvertCase(uint symbol, out uint lower, out uint upper)
503 	{
504 		gdk_keyval_convert_case(symbol, &lower, &upper);
505 	}
506 
507 	/**
508 	 * Converts a key name to a key value.
509 	 *
510 	 * The names are the same as those in the
511 	 * `gdk/gdkkeysyms.h` header file
512 	 * but without the leading “GDK_KEY_”.
513 	 *
514 	 * Params:
515 	 *     keyvalName = a key name
516 	 *
517 	 * Return: the corresponding key value, or %GDK_KEY_VoidSymbol
518 	 *     if the key name is not a valid key
519 	 */
520 	public static uint keyvalFromName(string keyvalName)
521 	{
522 		return gdk_keyval_from_name(Str.toStringz(keyvalName));
523 	}
524 
525 	/**
526 	 * Returns %TRUE if the given key value is in lower case.
527 	 *
528 	 * Params:
529 	 *     keyval = a key value.
530 	 *
531 	 * Return: %TRUE if @keyval is in lower case, or if @keyval is not
532 	 *     subject to case conversion.
533 	 */
534 	public static bool keyvalIsLower(uint keyval)
535 	{
536 		return gdk_keyval_is_lower(keyval) != 0;
537 	}
538 
539 	/**
540 	 * Returns %TRUE if the given key value is in upper case.
541 	 *
542 	 * Params:
543 	 *     keyval = a key value.
544 	 *
545 	 * Return: %TRUE if @keyval is in upper case, or if @keyval is not subject to
546 	 *     case conversion.
547 	 */
548 	public static bool keyvalIsUpper(uint keyval)
549 	{
550 		return gdk_keyval_is_upper(keyval) != 0;
551 	}
552 
553 	/**
554 	 * Converts a key value into a symbolic name.
555 	 *
556 	 * The names are the same as those in the
557 	 * `gdk/gdkkeysyms.h` header file
558 	 * but without the leading “GDK_KEY_”.
559 	 *
560 	 * Params:
561 	 *     keyval = a key value
562 	 *
563 	 * Return: a string containing the name
564 	 *     of the key, or %NULL if @keyval is not a valid key. The string
565 	 *     should not be modified.
566 	 */
567 	public static string keyvalName(uint keyval)
568 	{
569 		return Str.toString(gdk_keyval_name(keyval));
570 	}
571 
572 	/**
573 	 * Converts a key value to lower case, if applicable.
574 	 *
575 	 * Params:
576 	 *     keyval = a key value.
577 	 *
578 	 * Return: the lower case form of @keyval, or @keyval itself if it is already
579 	 *     in lower case or it is not subject to case conversion.
580 	 */
581 	public static uint keyvalToLower(uint keyval)
582 	{
583 		return gdk_keyval_to_lower(keyval);
584 	}
585 
586 	/**
587 	 * Convert from a GDK key symbol to the corresponding ISO10646 (Unicode)
588 	 * character.
589 	 *
590 	 * Params:
591 	 *     keyval = a GDK key symbol
592 	 *
593 	 * Return: the corresponding unicode character, or 0 if there
594 	 *     is no corresponding character.
595 	 */
596 	public static uint keyvalToUnicode(uint keyval)
597 	{
598 		return gdk_keyval_to_unicode(keyval);
599 	}
600 
601 	/**
602 	 * Converts a key value to upper case, if applicable.
603 	 *
604 	 * Params:
605 	 *     keyval = a key value.
606 	 *
607 	 * Return: the upper case form of @keyval, or @keyval itself if it is already
608 	 *     in upper case or it is not subject to case conversion.
609 	 */
610 	public static uint keyvalToUpper(uint keyval)
611 	{
612 		return gdk_keyval_to_upper(keyval);
613 	}
614 
615 	/**
616 	 * Convert from a ISO10646 character to a key symbol.
617 	 *
618 	 * Params:
619 	 *     wc = a ISO10646 encoded character
620 	 *
621 	 * Return: the corresponding GDK key symbol, if one exists.
622 	 *     or, if there is no corresponding symbol,
623 	 *     wc | 0x01000000
624 	 */
625 	public static uint unicodeToKeyval(uint wc)
626 	{
627 		return gdk_unicode_to_keyval(wc);
628 	}
629 }