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 gtk.AccelGroup;
26 
27 private import gdk.Display;
28 private import glib.ConstructionException;
29 private import glib.ListSG;
30 private import glib.Str;
31 private import gobject.Closure;
32 private import gobject.ObjectG;
33 private import gobject.Signals;
34 private import gtk.c.functions;
35 public  import gtk.c.types;
36 public  import gtkc.gtktypes;
37 private import std.algorithm;
38 
39 
40 /**
41  * A #GtkAccelGroup represents a group of keyboard accelerators,
42  * typically attached to a toplevel #GtkWindow (with
43  * gtk_window_add_accel_group()). Usually you won’t need to create a
44  * #GtkAccelGroup directly; instead, when using #GtkUIManager, GTK+
45  * automatically sets up the accelerators for your menus in the ui
46  * manager’s #GtkAccelGroup.
47  * 
48  * Note that “accelerators” are different from
49  * “mnemonics”. Accelerators are shortcuts for
50  * activating a menu item; they appear alongside the menu item they’re a
51  * shortcut for. For example “Ctrl+Q” might appear alongside the “Quit”
52  * menu item. Mnemonics are shortcuts for GUI elements such as text
53  * entries or buttons; they appear as underlined characters. See
54  * gtk_label_new_with_mnemonic(). Menu items can have both accelerators
55  * and mnemonics, of course.
56  */
57 public class AccelGroup : ObjectG
58 {
59 	/** the main Gtk struct */
60 	protected GtkAccelGroup* gtkAccelGroup;
61 
62 	/** Get the main Gtk struct */
63 	public GtkAccelGroup* getAccelGroupStruct(bool transferOwnership = false)
64 	{
65 		if (transferOwnership)
66 			ownedRef = false;
67 		return gtkAccelGroup;
68 	}
69 
70 	/** the main Gtk struct as a void* */
71 	protected override void* getStruct()
72 	{
73 		return cast(void*)gtkAccelGroup;
74 	}
75 
76 	/**
77 	 * Sets our main struct and passes it to the parent class.
78 	 */
79 	public this (GtkAccelGroup* gtkAccelGroup, bool ownedRef = false)
80 	{
81 		this.gtkAccelGroup = gtkAccelGroup;
82 		super(cast(GObject*)gtkAccelGroup, ownedRef);
83 	}
84 
85 
86 	/** */
87 	public static GType getType()
88 	{
89 		return gtk_accel_group_get_type();
90 	}
91 
92 	/**
93 	 * Creates a new #GtkAccelGroup.
94 	 *
95 	 * Returns: a new #GtkAccelGroup object
96 	 *
97 	 * Throws: ConstructionException GTK+ fails to create the object.
98 	 */
99 	public this()
100 	{
101 		auto p = gtk_accel_group_new();
102 
103 		if(p is null)
104 		{
105 			throw new ConstructionException("null returned by new");
106 		}
107 
108 		this(cast(GtkAccelGroup*) p, true);
109 	}
110 
111 	/**
112 	 * Finds the #GtkAccelGroup to which @closure is connected;
113 	 * see gtk_accel_group_connect().
114 	 *
115 	 * Params:
116 	 *     closure = a #GClosure
117 	 *
118 	 * Returns: the #GtkAccelGroup to which @closure
119 	 *     is connected, or %NULL
120 	 */
121 	public static AccelGroup fromAccelClosure(Closure closure)
122 	{
123 		auto p = gtk_accel_group_from_accel_closure((closure is null) ? null : closure.getClosureStruct());
124 
125 		if(p is null)
126 		{
127 			return null;
128 		}
129 
130 		return ObjectG.getDObject!(AccelGroup)(cast(GtkAccelGroup*) p);
131 	}
132 
133 	/**
134 	 * Finds the first accelerator in @accel_group that matches
135 	 * @accel_key and @accel_mods, and activates it.
136 	 *
137 	 * Params:
138 	 *     accelQuark = the quark for the accelerator name
139 	 *     acceleratable = the #GObject, usually a #GtkWindow, on which
140 	 *         to activate the accelerator
141 	 *     accelKey = accelerator keyval from a key event
142 	 *     accelMods = keyboard state mask from a key event
143 	 *
144 	 * Returns: %TRUE if an accelerator was activated and handled
145 	 *     this keypress
146 	 */
147 	public bool activate(GQuark accelQuark, ObjectG acceleratable, uint accelKey, GdkModifierType accelMods)
148 	{
149 		return gtk_accel_group_activate(gtkAccelGroup, accelQuark, (acceleratable is null) ? null : acceleratable.getObjectGStruct(), accelKey, accelMods) != 0;
150 	}
151 
152 	/**
153 	 * Installs an accelerator in this group. When @accel_group is being
154 	 * activated in response to a call to gtk_accel_groups_activate(),
155 	 * @closure will be invoked if the @accel_key and @accel_mods from
156 	 * gtk_accel_groups_activate() match those of this connection.
157 	 *
158 	 * The signature used for the @closure is that of #GtkAccelGroupActivate.
159 	 *
160 	 * Note that, due to implementation details, a single closure can
161 	 * only be connected to one accelerator group.
162 	 *
163 	 * Params:
164 	 *     accelKey = key value of the accelerator
165 	 *     accelMods = modifier combination of the accelerator
166 	 *     accelFlags = a flag mask to configure this accelerator
167 	 *     closure = closure to be executed upon accelerator activation
168 	 */
169 	public void connect(uint accelKey, GdkModifierType accelMods, GtkAccelFlags accelFlags, Closure closure)
170 	{
171 		gtk_accel_group_connect(gtkAccelGroup, accelKey, accelMods, accelFlags, (closure is null) ? null : closure.getClosureStruct());
172 	}
173 
174 	/**
175 	 * Installs an accelerator in this group, using an accelerator path
176 	 * to look up the appropriate key and modifiers (see
177 	 * gtk_accel_map_add_entry()). When @accel_group is being activated
178 	 * in response to a call to gtk_accel_groups_activate(), @closure will
179 	 * be invoked if the @accel_key and @accel_mods from
180 	 * gtk_accel_groups_activate() match the key and modifiers for the path.
181 	 *
182 	 * The signature used for the @closure is that of #GtkAccelGroupActivate.
183 	 *
184 	 * Note that @accel_path string will be stored in a #GQuark. Therefore,
185 	 * if you pass a static string, you can save some memory by interning it
186 	 * first with g_intern_static_string().
187 	 *
188 	 * Params:
189 	 *     accelPath = path used for determining key and modifiers
190 	 *     closure = closure to be executed upon accelerator activation
191 	 */
192 	public void connectByPath(string accelPath, Closure closure)
193 	{
194 		gtk_accel_group_connect_by_path(gtkAccelGroup, Str.toStringz(accelPath), (closure is null) ? null : closure.getClosureStruct());
195 	}
196 
197 	/**
198 	 * Removes an accelerator previously installed through
199 	 * gtk_accel_group_connect().
200 	 *
201 	 * Since 2.20 @closure can be %NULL.
202 	 *
203 	 * Params:
204 	 *     closure = the closure to remove from this accelerator
205 	 *         group, or %NULL to remove all closures
206 	 *
207 	 * Returns: %TRUE if the closure was found and got disconnected
208 	 */
209 	public bool disconnect(Closure closure)
210 	{
211 		return gtk_accel_group_disconnect(gtkAccelGroup, (closure is null) ? null : closure.getClosureStruct()) != 0;
212 	}
213 
214 	/**
215 	 * Removes an accelerator previously installed through
216 	 * gtk_accel_group_connect().
217 	 *
218 	 * Params:
219 	 *     accelKey = key value of the accelerator
220 	 *     accelMods = modifier combination of the accelerator
221 	 *
222 	 * Returns: %TRUE if there was an accelerator which could be
223 	 *     removed, %FALSE otherwise
224 	 */
225 	public bool disconnectKey(uint accelKey, GdkModifierType accelMods)
226 	{
227 		return gtk_accel_group_disconnect_key(gtkAccelGroup, accelKey, accelMods) != 0;
228 	}
229 
230 	/**
231 	 * Finds the first entry in an accelerator group for which
232 	 * @find_func returns %TRUE and returns its #GtkAccelKey.
233 	 *
234 	 * Params:
235 	 *     findFunc = a function to filter the entries
236 	 *         of @accel_group with
237 	 *     data = data to pass to @find_func
238 	 *
239 	 * Returns: the key of the first entry passing
240 	 *     @find_func. The key is owned by GTK+ and must not be freed.
241 	 */
242 	public GtkAccelKey* find(GtkAccelGroupFindFunc findFunc, void* data)
243 	{
244 		return gtk_accel_group_find(gtkAccelGroup, findFunc, data);
245 	}
246 
247 	/**
248 	 * Locks are added and removed using gtk_accel_group_lock() and
249 	 * gtk_accel_group_unlock().
250 	 *
251 	 * Returns: %TRUE if there are 1 or more locks on the @accel_group,
252 	 *     %FALSE otherwise.
253 	 *
254 	 * Since: 2.14
255 	 */
256 	public bool getIsLocked()
257 	{
258 		return gtk_accel_group_get_is_locked(gtkAccelGroup) != 0;
259 	}
260 
261 	/**
262 	 * Gets a #GdkModifierType representing the mask for this
263 	 * @accel_group. For example, #GDK_CONTROL_MASK, #GDK_SHIFT_MASK, etc.
264 	 *
265 	 * Returns: the modifier mask for this accel group.
266 	 *
267 	 * Since: 2.14
268 	 */
269 	public GdkModifierType getModifierMask()
270 	{
271 		return gtk_accel_group_get_modifier_mask(gtkAccelGroup);
272 	}
273 
274 	/**
275 	 * Locks the given accelerator group.
276 	 *
277 	 * Locking an acelerator group prevents the accelerators contained
278 	 * within it to be changed during runtime. Refer to
279 	 * gtk_accel_map_change_entry() about runtime accelerator changes.
280 	 *
281 	 * If called more than once, @accel_group remains locked until
282 	 * gtk_accel_group_unlock() has been called an equivalent number
283 	 * of times.
284 	 */
285 	public void lock()
286 	{
287 		gtk_accel_group_lock(gtkAccelGroup);
288 	}
289 
290 	/**
291 	 * Queries an accelerator group for all entries matching @accel_key
292 	 * and @accel_mods.
293 	 *
294 	 * Params:
295 	 *     accelKey = key value of the accelerator
296 	 *     accelMods = modifier combination of the accelerator
297 	 *
298 	 * Returns: an array of
299 	 *     @n_entries #GtkAccelGroupEntry elements, or %NULL. The array
300 	 *     is owned by GTK+ and must not be freed.
301 	 */
302 	public GtkAccelGroupEntry[] query(uint accelKey, GdkModifierType accelMods)
303 	{
304 		uint nEntries;
305 
306 		auto p = gtk_accel_group_query(gtkAccelGroup, accelKey, accelMods, &nEntries);
307 
308 		return p[0 .. nEntries];
309 	}
310 
311 	/**
312 	 * Undoes the last call to gtk_accel_group_lock() on this @accel_group.
313 	 */
314 	public void unlock()
315 	{
316 		gtk_accel_group_unlock(gtkAccelGroup);
317 	}
318 
319 	/**
320 	 * The accel-activate signal is an implementation detail of
321 	 * #GtkAccelGroup and not meant to be used by applications.
322 	 *
323 	 * Params:
324 	 *     acceleratable = the object on which the accelerator was activated
325 	 *     keyval = the accelerator keyval
326 	 *     modifier = the modifier combination of the accelerator
327 	 *
328 	 * Returns: %TRUE if the accelerator was activated
329 	 */
330 	gulong addOnAccelActivate(bool delegate(ObjectG, uint, GdkModifierType, AccelGroup) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
331 	{
332 		return Signals.connect(this, "accel-activate", dlg, connectFlags ^ ConnectFlags.SWAPPED);
333 	}
334 
335 	/**
336 	 * The accel-changed signal is emitted when an entry
337 	 * is added to or removed from the accel group.
338 	 *
339 	 * Widgets like #GtkAccelLabel which display an associated
340 	 * accelerator should connect to this signal, and rebuild
341 	 * their visual representation if the @accel_closure is theirs.
342 	 *
343 	 * Params:
344 	 *     keyval = the accelerator keyval
345 	 *     modifier = the modifier combination of the accelerator
346 	 *     accelClosure = the #GClosure of the accelerator
347 	 */
348 	gulong addOnAccelChanged(void delegate(uint, GdkModifierType, Closure, AccelGroup) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
349 	{
350 		return Signals.connect(this, "accel-changed", dlg, connectFlags ^ ConnectFlags.SWAPPED);
351 	}
352 
353 	/**
354 	 * Finds the first accelerator in any #GtkAccelGroup attached
355 	 * to @object that matches @accel_key and @accel_mods, and
356 	 * activates that accelerator.
357 	 *
358 	 * Params:
359 	 *     object = the #GObject, usually a #GtkWindow, on which
360 	 *         to activate the accelerator
361 	 *     accelKey = accelerator keyval from a key event
362 	 *     accelMods = keyboard state mask from a key event
363 	 *
364 	 * Returns: %TRUE if an accelerator was activated and handled
365 	 *     this keypress
366 	 */
367 	public static bool accelGroupsActivate(ObjectG object, uint accelKey, GdkModifierType accelMods)
368 	{
369 		return gtk_accel_groups_activate((object is null) ? null : object.getObjectGStruct(), accelKey, accelMods) != 0;
370 	}
371 
372 	/**
373 	 * Gets a list of all accel groups which are attached to @object.
374 	 *
375 	 * Params:
376 	 *     object = a #GObject, usually a #GtkWindow
377 	 *
378 	 * Returns: a list of
379 	 *     all accel groups which are attached to @object
380 	 */
381 	public static ListSG accelGroupsFromObject(ObjectG object)
382 	{
383 		auto p = gtk_accel_groups_from_object((object is null) ? null : object.getObjectGStruct());
384 
385 		if(p is null)
386 		{
387 			return null;
388 		}
389 
390 		return new ListSG(cast(GSList*) p);
391 	}
392 
393 	/**
394 	 * Gets the modifier mask.
395 	 *
396 	 * The modifier mask determines which modifiers are considered significant
397 	 * for keyboard accelerators. See gtk_accelerator_set_default_mod_mask().
398 	 *
399 	 * Returns: the default accelerator modifier mask
400 	 */
401 	public static GdkModifierType acceleratorGetDefaultModMask()
402 	{
403 		return gtk_accelerator_get_default_mod_mask();
404 	}
405 
406 	/**
407 	 * Converts an accelerator keyval and modifier mask into a string
408 	 * which can be used to represent the accelerator to the user.
409 	 *
410 	 * Params:
411 	 *     acceleratorKey = accelerator keyval
412 	 *     acceleratorMods = accelerator modifier mask
413 	 *
414 	 * Returns: a newly-allocated string representing the accelerator.
415 	 *
416 	 * Since: 2.6
417 	 */
418 	public static string acceleratorGetLabel(uint acceleratorKey, GdkModifierType acceleratorMods)
419 	{
420 		auto retStr = gtk_accelerator_get_label(acceleratorKey, acceleratorMods);
421 
422 		scope(exit) Str.freeString(retStr);
423 		return Str.toString(retStr);
424 	}
425 
426 	/**
427 	 * Converts an accelerator keyval and modifier mask
428 	 * into a (possibly translated) string that can be displayed to
429 	 * a user, similarly to gtk_accelerator_get_label(), but handling
430 	 * keycodes.
431 	 *
432 	 * This is only useful for system-level components, applications
433 	 * should use gtk_accelerator_parse() instead.
434 	 *
435 	 * Params:
436 	 *     display = a #GdkDisplay or %NULL to use the default display
437 	 *     acceleratorKey = accelerator keyval
438 	 *     keycode = accelerator keycode
439 	 *     acceleratorMods = accelerator modifier mask
440 	 *
441 	 * Returns: a newly-allocated string representing the accelerator.
442 	 *
443 	 * Since: 3.4
444 	 */
445 	public static string acceleratorGetLabelWithKeycode(Display display, uint acceleratorKey, uint keycode, GdkModifierType acceleratorMods)
446 	{
447 		auto retStr = gtk_accelerator_get_label_with_keycode((display is null) ? null : display.getDisplayStruct(), acceleratorKey, keycode, acceleratorMods);
448 
449 		scope(exit) Str.freeString(retStr);
450 		return Str.toString(retStr);
451 	}
452 
453 	/**
454 	 * Converts an accelerator keyval and modifier mask into a string
455 	 * parseable by gtk_accelerator_parse(). For example, if you pass in
456 	 * #GDK_KEY_q and #GDK_CONTROL_MASK, this function returns “<Control>q”.
457 	 *
458 	 * If you need to display accelerators in the user interface,
459 	 * see gtk_accelerator_get_label().
460 	 *
461 	 * Params:
462 	 *     acceleratorKey = accelerator keyval
463 	 *     acceleratorMods = accelerator modifier mask
464 	 *
465 	 * Returns: a newly-allocated accelerator name
466 	 */
467 	public static string acceleratorName(uint acceleratorKey, GdkModifierType acceleratorMods)
468 	{
469 		auto retStr = gtk_accelerator_name(acceleratorKey, acceleratorMods);
470 
471 		scope(exit) Str.freeString(retStr);
472 		return Str.toString(retStr);
473 	}
474 
475 	/**
476 	 * Converts an accelerator keyval and modifier mask
477 	 * into a string parseable by gtk_accelerator_parse_with_keycode(),
478 	 * similarly to gtk_accelerator_name() but handling keycodes.
479 	 * This is only useful for system-level components, applications
480 	 * should use gtk_accelerator_parse() instead.
481 	 *
482 	 * Params:
483 	 *     display = a #GdkDisplay or %NULL to use the default display
484 	 *     acceleratorKey = accelerator keyval
485 	 *     keycode = accelerator keycode
486 	 *     acceleratorMods = accelerator modifier mask
487 	 *
488 	 * Returns: a newly allocated accelerator name.
489 	 *
490 	 * Since: 3.4
491 	 */
492 	public static string acceleratorNameWithKeycode(Display display, uint acceleratorKey, uint keycode, GdkModifierType acceleratorMods)
493 	{
494 		auto retStr = gtk_accelerator_name_with_keycode((display is null) ? null : display.getDisplayStruct(), acceleratorKey, keycode, acceleratorMods);
495 
496 		scope(exit) Str.freeString(retStr);
497 		return Str.toString(retStr);
498 	}
499 
500 	/**
501 	 * Parses a string representing an accelerator. The format looks like
502 	 * “<Control>a” or “<Shift><Alt>F1” or “<Release>z” (the last one is
503 	 * for key release).
504 	 *
505 	 * The parser is fairly liberal and allows lower or upper case, and also
506 	 * abbreviations such as “<Ctl>” and “<Ctrl>”. Key names are parsed using
507 	 * gdk_keyval_from_name(). For character keys the name is not the symbol,
508 	 * but the lowercase name, e.g. one would use “<Ctrl>minus” instead of
509 	 * “<Ctrl>-”.
510 	 *
511 	 * If the parse fails, @accelerator_key and @accelerator_mods will
512 	 * be set to 0 (zero).
513 	 *
514 	 * Params:
515 	 *     accelerator = string representing an accelerator
516 	 *     acceleratorKey = return location for accelerator
517 	 *         keyval, or %NULL
518 	 *     acceleratorMods = return location for accelerator
519 	 *         modifier mask, %NULL
520 	 */
521 	public static void acceleratorParse(string accelerator, out uint acceleratorKey, out GdkModifierType acceleratorMods)
522 	{
523 		gtk_accelerator_parse(Str.toStringz(accelerator), &acceleratorKey, &acceleratorMods);
524 	}
525 
526 	/**
527 	 * Parses a string representing an accelerator, similarly to
528 	 * gtk_accelerator_parse() but handles keycodes as well. This is only
529 	 * useful for system-level components, applications should use
530 	 * gtk_accelerator_parse() instead.
531 	 *
532 	 * If @accelerator_codes is given and the result stored in it is non-%NULL,
533 	 * the result must be freed with g_free().
534 	 *
535 	 * If a keycode is present in the accelerator and no @accelerator_codes
536 	 * is given, the parse will fail.
537 	 *
538 	 * If the parse fails, @accelerator_key, @accelerator_mods and
539 	 * @accelerator_codes will be set to 0 (zero).
540 	 *
541 	 * Params:
542 	 *     accelerator = string representing an accelerator
543 	 *     acceleratorKey = return location for accelerator
544 	 *         keyval, or %NULL
545 	 *     acceleratorCodes = return location for accelerator keycodes, or %NULL
546 	 *     acceleratorMods = return location for accelerator
547 	 *         modifier mask, %NULL
548 	 *
549 	 * Since: 3.4
550 	 */
551 	public static void acceleratorParseWithKeycode(string accelerator, out uint acceleratorKey, out uint[] acceleratorCodes, out GdkModifierType acceleratorMods)
552 	{
553 		uint* outacceleratorCodes = null;
554 
555 		gtk_accelerator_parse_with_keycode(Str.toStringz(accelerator), &acceleratorKey, &outacceleratorCodes, &acceleratorMods);
556 
557 		acceleratorCodes = outacceleratorCodes[0 .. getArrayLength(outacceleratorCodes)];
558 	}
559 
560 	/**
561 	 * Sets the modifiers that will be considered significant for keyboard
562 	 * accelerators. The default mod mask depends on the GDK backend in use,
563 	 * but will typically include #GDK_CONTROL_MASK | #GDK_SHIFT_MASK |
564 	 * #GDK_MOD1_MASK | #GDK_SUPER_MASK | #GDK_HYPER_MASK | #GDK_META_MASK.
565 	 * In other words, Control, Shift, Alt, Super, Hyper and Meta. Other
566 	 * modifiers will by default be ignored by #GtkAccelGroup.
567 	 *
568 	 * You must include at least the three modifiers Control, Shift
569 	 * and Alt in any value you pass to this function.
570 	 *
571 	 * The default mod mask should be changed on application startup,
572 	 * before using any accelerator groups.
573 	 *
574 	 * Params:
575 	 *     defaultModMask = accelerator modifier mask
576 	 */
577 	public static void acceleratorSetDefaultModMask(GdkModifierType defaultModMask)
578 	{
579 		gtk_accelerator_set_default_mod_mask(defaultModMask);
580 	}
581 
582 	/**
583 	 * Determines whether a given keyval and modifier mask constitute
584 	 * a valid keyboard accelerator. For example, the #GDK_KEY_a keyval
585 	 * plus #GDK_CONTROL_MASK is valid - this is a “Ctrl+a” accelerator.
586 	 * But, you can't, for instance, use the #GDK_KEY_Control_L keyval
587 	 * as an accelerator.
588 	 *
589 	 * Params:
590 	 *     keyval = a GDK keyval
591 	 *     modifiers = modifier mask
592 	 *
593 	 * Returns: %TRUE if the accelerator is valid
594 	 */
595 	public static bool acceleratorValid(uint keyval, GdkModifierType modifiers)
596 	{
597 		return gtk_accelerator_valid(keyval, modifiers) != 0;
598 	}
599 }