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  * Conversion parameters:
26  * inFile  = gtk3-General.html
27  * outPack = gtk
28  * outFile = Main
29  * strct   = 
30  * realStrct=
31  * ctorStrct=
32  * clss    = Main
33  * interf  = 
34  * class Code: Yes
35  * interface Code: No
36  * template for:
37  * extend  = 
38  * implements:
39  * prefixes:
40  * 	- gtk_main_
41  * 	- gtk_
42  * omit structs:
43  * omit prefixes:
44  * 	- gtk_true
45  * 	- gtk_false
46  * omit code:
47  * omit signals:
48  * imports:
49  * 	- glib.Str
50  * 	- glib.ErrorG
51  * 	- glib.GException
52  * 	- glib.OptionGroup
53  * 	- gdk.Device
54  * 	- gdk.Event
55  * 	- gdk.Threads
56  * 	- gthread.Thread
57  * 	- gtk.Widget
58  * 	- pango.PgLanguage
59  * structWrap:
60  * 	- GOptionGroup* -> OptionGroup
61  * 	- GdkDevice* -> Device
62  * 	- GdkEvent* -> Event
63  * 	- GtkWidget* -> Widget
64  * 	- PangoLanguage* -> PgLanguage
65  * module aliases:
66  * local aliases:
67  * 	- main -> run
68  * overrides:
69  */
70 
71 module gtk.Main;
72 
73 public  import gtkc.gtktypes;
74 
75 private import gtkc.gtk;
76 private import glib.ConstructionException;
77 private import gobject.ObjectG;
78 
79 
80 private import glib.Str;
81 private import glib.ErrorG;
82 private import glib.GException;
83 private import glib.OptionGroup;
84 private import gdk.Device;
85 private import gdk.Event;
86 private import gdk.Threads;
87 private import gthread.Thread;
88 private import gtk.Widget;
89 private import pango.PgLanguage;
90 
91 
92 
93 
94 /**
95  * Before using GTK+, you need to initialize it; initialization connects to the
96  * window system display, and parses some standard command line arguments. The
97  * gtk_init() macro initializes GTK+. gtk_init() exits the application if errors
98  * occur; to avoid this, use gtk_init_check(). gtk_init_check() allows you to
99  * recover from a failed GTK+ initialization - you might start up your
100  * application in text mode instead.
101  *
102  * Like all GUI toolkits, GTK+ uses an event-driven programming model. When the
103  * user is doing nothing, GTK+ sits in the main loop and
104  * waits for input. If the user performs some action - say, a mouse click - then
105  * the main loop "wakes up" and delivers an event to GTK+. GTK+ forwards the
106  * event to one or more widgets.
107  *
108  * When widgets receive an event, they frequently emit one or more
109  * signals. Signals notify your program that "something
110  * interesting happened" by invoking functions you've connected to the signal
111  * with g_signal_connect(). Functions connected to a signal are often termed
112  * callbacks.
113  *
114  * When your callbacks are invoked, you would typically take some action - for
115  * example, when an Open button is clicked you might display a
116  * GtkFileChooserDialog. After a callback finishes, GTK+ will return to the
117  * main loop and await more user input.
118  *
119  * $(DDOC_COMMENT example)
120  *
121  * It's OK to use the GLib main loop directly instead of gtk_main(), though it
122  * involves slightly more typing. See GMainLoop in the GLib documentation.
123  */
124 public class Main
125 {
126 	
127 	/**
128 	 * This initiates GtkD to supports multi threaded programs.
129 	 * read full documantation at http://gtk.org/faq/#AEN482
130 	 * from the FAQ:
131 	 * "There is a single global lock that you must acquire with
132 	 * gdk_threads_enter() before making any GDK calls,
133 	 * and release with gdk_threads_leave() afterwards throughout your code."
134 	 * This is to be used on any call to GDK not executed from the main thread.
135 	 */
136 	public static void initMultiThread(string[] args)
137 	{
138 		threadsInit();
139 		init(args);
140 	}
141 	
142 	/**
143 	 */
144 	
145 	/**
146 	 * Prevents gtk_init(), gtk_init_check(), gtk_init_with_args() and
147 	 * gtk_parse_args() from automatically
148 	 * calling setlocale (LC_ALL, ""). You would
149 	 * want to use this function if you wanted to set the locale for
150 	 * your program to something other than the user's locale, or if
151 	 * you wanted to set different values for different locale categories.
152 	 * Most programs should not need to call this function.
153 	 */
154 	public static void disableSetlocale()
155 	{
156 		// void gtk_disable_setlocale (void);
157 		gtk_disable_setlocale();
158 	}
159 	
160 	/**
161 	 * Returns the PangoLanguage for the default language currently in
162 	 * effect. (Note that this can change over the life of an
163 	 * application.) The default language is derived from the current
164 	 * locale. It determines, for example, whether GTK+ uses the
165 	 * right-to-left or left-to-right text direction.
166 	 * This function is equivalent to pango_language_get_default().
167 	 * See that function for details.
168 	 * Returns: the default language as a PangoLanguage, must not be freed
169 	 */
170 	public static PgLanguage getDefaultLanguage()
171 	{
172 		// PangoLanguage * gtk_get_default_language (void);
173 		auto p = gtk_get_default_language();
174 		
175 		if(p is null)
176 		{
177 			return null;
178 		}
179 		
180 		return ObjectG.getDObject!(PgLanguage)(cast(PangoLanguage*) p);
181 	}
182 	
183 	/**
184 	 * Parses command line arguments, and initializes global
185 	 * attributes of GTK+, but does not actually open a connection
186 	 * to a display. (See gdk_display_open(), gdk_get_display_arg_name())
187 	 * Any arguments used by GTK+ or GDK are removed from the array and
188 	 * argc and argv are updated accordingly.
189 	 * There is no need to call this function explicitely if you are using
190 	 * gtk_init(), or gtk_init_check().
191 	 * Params:
192 	 * argv = a pointer to the array of
193 	 * command line arguments. [array length=argc][inout]
194 	 * Returns: TRUE if initialization succeeded, otherwise FALSE
195 	 */
196 	public static int parseArgs(ref string[] argv)
197 	{
198 		// gboolean gtk_parse_args (int *argc,  char ***argv);
199 		char** outargv = Str.toStringzArray(argv);
200 		int argc = cast(int) argv.length;
201 		
202 		auto p = gtk_parse_args(&argc, &outargv);
203 		
204 		argv = null;
205 		foreach ( cstr; outargv[0 .. argc] )
206 		{
207 			argv ~= Str.toString(cstr);
208 		}
209 		return p;
210 	}
211 	
212 	/**
213 	 * Call this function before using any other GTK+ functions in your GUI
214 	 * applications. It will initialize everything needed to operate the
215 	 * toolkit and parses some standard command line options.
216 	 * Although you are expected to pass the argc, argv parameters from main() to
217 	 * this function, it is possible to pass NULL if argv is not available or
218 	 * commandline handling is not required.
219 	 * argc and argv are adjusted accordingly so your own code will
220 	 * never see those standard arguments.
221 	 * Since 2.18, GTK+ calls signal (SIGPIPE, SIG_IGN)
222 	 * during initialization, to ignore SIGPIPE signals, since these are
223 	 * almost never wanted in graphical applications. If you do need to
224 	 * handle SIGPIPE for some reason, reset the handler after gtk_init(),
225 	 * but notice that other libraries (e.g. libdbus or gvfs) might do
226 	 * similar things.
227 	 * Params:
228 	 * argv = Address of the
229 	 * argv parameter of main(), or NULL. Any options
230 	 * understood by GTK+ are stripped before return. [array length=argc][inout][allow-none]
231 	 */
232 	public static void init(ref string[] argv)
233 	{
234 		// void gtk_init (int *argc,  char ***argv);
235 		char** outargv = Str.toStringzArray(argv);
236 		int argc = cast(int) argv.length;
237 		
238 		gtk_init(&argc, &outargv);
239 		
240 		argv = null;
241 		foreach ( cstr; outargv[0 .. argc] )
242 		{
243 			argv ~= Str.toString(cstr);
244 		}
245 	}
246 	
247 	/**
248 	 * This function does the same work as gtk_init() with only a single
249 	 * change: It does not terminate the program if the windowing system
250 	 * can't be initialized. Instead it returns FALSE on failure.
251 	 * This way the application can fall back to some other means of
252 	 * communication with the user - for example a curses or command line
253 	 * interface.
254 	 * Params:
255 	 * argv = Address of the
256 	 * argv parameter of main(), or NULL. Any options
257 	 * understood by GTK+ are stripped before return. [array length=argc][inout][allow-none]
258 	 * Returns: TRUE if the windowing system has been successfully initialized, FALSE otherwise
259 	 */
260 	public static int initCheck(ref string[] argv)
261 	{
262 		// gboolean gtk_init_check (int *argc,  char ***argv);
263 		char** outargv = Str.toStringzArray(argv);
264 		int argc = cast(int) argv.length;
265 		
266 		auto p = gtk_init_check(&argc, &outargv);
267 		
268 		argv = null;
269 		foreach ( cstr; outargv[0 .. argc] )
270 		{
271 			argv ~= Str.toString(cstr);
272 		}
273 		return p;
274 	}
275 	
276 	/**
277 	 * This function does the same work as gtk_init_check().
278 	 * Additionally, it allows you to add your own commandline options,
279 	 * and it automatically generates nicely formatted
280 	 * --help output. Note that your program will
281 	 * be terminated after writing out the help output.
282 	 * Since 2.6
283 	 * Params:
284 	 * argv = Address of the
285 	 * argv parameter of main(), or NULL. Any options
286 	 * understood by GTK+ are stripped before return. [array length=argc][inout][allow-none]
287 	 * parameterString = a string which is displayed in
288 	 * the first line of --help output, after
289 	 * programname [OPTION...]. [allow-none]
290 	 * entries = a NULL-terminated array
291 	 * of GOptionEntrys describing the options of your program. [array zero-terminated=1]
292 	 * translationDomain = a translation domain to use for translating
293 	 * the --help output for the options in entries
294 	 * and the parameter_string with gettext(), or NULL
295 	 * Returns: TRUE if the windowing system has been successfully initialized, FALSE otherwise
296 	 * Throws: GException on failure.
297 	 */
298 	public static int initWithArgs(ref string[] argv, string parameterString, GOptionEntry[] entries, string translationDomain)
299 	{
300 		// gboolean gtk_init_with_args (gint *argc,  gchar ***argv,  const gchar *parameter_string,  const GOptionEntry *entries,  const gchar *translation_domain,  GError **error);
301 		char** outargv = Str.toStringzArray(argv);
302 		int argc = cast(int) argv.length;
303 		GError* err = null;
304 		
305 		auto p = gtk_init_with_args(&argc, &outargv, Str.toStringz(parameterString), entries.ptr, Str.toStringz(translationDomain), &err);
306 		
307 		if (err !is null)
308 		{
309 			throw new GException( new ErrorG(err) );
310 		}
311 		
312 		argv = null;
313 		foreach ( cstr; outargv[0 .. argc] )
314 		{
315 			argv ~= Str.toString(cstr);
316 		}
317 		return p;
318 	}
319 	
320 	/**
321 	 * Returns a GOptionGroup for the commandline arguments recognized
322 	 * by GTK+ and GDK.
323 	 * You should add this group to your GOptionContext
324 	 * with g_option_context_add_group(), if you are using
325 	 * g_option_context_parse() to parse your commandline arguments.
326 	 * Since 2.6
327 	 * Params:
328 	 * openDefaultDisplay = whether to open the default display
329 	 * when parsing the commandline arguments
330 	 * Returns: a GOptionGroup for the commandline arguments recognized by GTK+
331 	 */
332 	public static OptionGroup getOptionGroup(int openDefaultDisplay)
333 	{
334 		// GOptionGroup * gtk_get_option_group (gboolean open_default_display);
335 		auto p = gtk_get_option_group(openDefaultDisplay);
336 		
337 		if(p is null)
338 		{
339 			return null;
340 		}
341 		
342 		return ObjectG.getDObject!(OptionGroup)(cast(GOptionGroup*) p);
343 	}
344 	
345 	/**
346 	 * Checks if any events are pending.
347 	 * This can be used to update the UI and invoke timeouts etc.
348 	 * while doing some time intensive computation.
349 	 * $(DDOC_COMMENT example)
350 	 * Returns: TRUE if any events are pending, FALSE otherwise
351 	 */
352 	public static int eventsPending()
353 	{
354 		// gboolean gtk_events_pending (void);
355 		return gtk_events_pending();
356 	}
357 	
358 	/**
359 	 * Runs the main loop until gtk_main_quit() is called.
360 	 * You can nest calls to gtk_main(). In that case gtk_main_quit()
361 	 * will make the innermost invocation of the main loop return.
362 	 */
363 	public static void run()
364 	{
365 		// void gtk_main (void);
366 		gtk_main();
367 	}
368 	
369 	/**
370 	 * Asks for the current nesting level of the main loop.
371 	 * Returns: the nesting level of the current invocation of the main loop
372 	 */
373 	public static uint level()
374 	{
375 		// guint gtk_main_level (void);
376 		return gtk_main_level();
377 	}
378 	
379 	/**
380 	 * Makes the innermost invocation of the main loop return
381 	 * when it regains control.
382 	 */
383 	public static void quit()
384 	{
385 		// void gtk_main_quit (void);
386 		gtk_main_quit();
387 	}
388 	
389 	/**
390 	 * Runs a single iteration of the mainloop.
391 	 * If no events are waiting to be processed GTK+ will block
392 	 * until the next event is noticed. If you don't want to block
393 	 * look at gtk_main_iteration_do() or check if any events are
394 	 * pending with gtk_events_pending() first.
395 	 * Returns: TRUE if gtk_main_quit() has been called for the innermost mainloop
396 	 */
397 	public static int iteration()
398 	{
399 		// gboolean gtk_main_iteration (void);
400 		return gtk_main_iteration();
401 	}
402 	
403 	/**
404 	 * Runs a single iteration of the mainloop.
405 	 * If no events are available either return or block depending on
406 	 * the value of blocking.
407 	 * Params:
408 	 * blocking = TRUE if you want GTK+ to block if no events are pending
409 	 * Returns: TRUE if gtk_main_quit() has been called for the innermost mainloop
410 	 */
411 	public static int iterationDo(int blocking)
412 	{
413 		// gboolean gtk_main_iteration_do (gboolean blocking);
414 		return gtk_main_iteration_do(blocking);
415 	}
416 	
417 	/**
418 	 * Processes a single GDK event.
419 	 * This is public only to allow filtering of events between GDK and GTK+.
420 	 * You will not usually need to call this function directly.
421 	 * While you should not call this function directly, you might want to
422 	 * know how exactly events are handled. So here is what this function
423 	 * Params:
424 	 * event = An event to process (normally passed by GDK)
425 	 */
426 	public static void doEvent(Event event)
427 	{
428 		// void gtk_main_do_event (GdkEvent *event);
429 		gtk_main_do_event((event is null) ? null : event.getEventStruct());
430 	}
431 	
432 	/**
433 	 * Makes widget the current grabbed widget.
434 	 * This means that interaction with other widgets in the same
435 	 * application is blocked and mouse as well as keyboard events
436 	 * are delivered to this widget.
437 	 * If widget is not sensitive, it is not set as the current
438 	 * grabbed widget and this function does nothing.
439 	 * Params:
440 	 * widget = The widget that grabs keyboard and pointer events
441 	 */
442 	public static void grabAdd(Widget widget)
443 	{
444 		// void gtk_grab_add (GtkWidget *widget);
445 		gtk_grab_add((widget is null) ? null : widget.getWidgetStruct());
446 	}
447 	
448 	/**
449 	 * Queries the current grab of the default window group.
450 	 * Returns: The widget which currently has the grab or NULL if no grab is active. [transfer none]
451 	 */
452 	public static Widget grabGetCurrent()
453 	{
454 		// GtkWidget * gtk_grab_get_current (void);
455 		auto p = gtk_grab_get_current();
456 		
457 		if(p is null)
458 		{
459 			return null;
460 		}
461 		
462 		return ObjectG.getDObject!(Widget)(cast(GtkWidget*) p);
463 	}
464 	
465 	/**
466 	 * Removes the grab from the given widget.
467 	 * You have to pair calls to gtk_grab_add() and gtk_grab_remove().
468 	 * If widget does not have the grab, this function does nothing.
469 	 * Params:
470 	 * widget = The widget which gives up the grab
471 	 */
472 	public static void grabRemove(Widget widget)
473 	{
474 		// void gtk_grab_remove (GtkWidget *widget);
475 		gtk_grab_remove((widget is null) ? null : widget.getWidgetStruct());
476 	}
477 	
478 	/**
479 	 * Adds a GTK+ grab on device, so all the events on device and its
480 	 * associated pointer or keyboard (if any) are delivered to widget.
481 	 * If the block_others parameter is TRUE, any other devices will be
482 	 * unable to interact with widget during the grab.
483 	 * Params:
484 	 * widget = a GtkWidget
485 	 * device = a GdkDevice to grab on.
486 	 * blockOthers = TRUE to prevent other devices to interact with widget.
487 	 * Since 3.0
488 	 */
489 	public static void deviceGrabAdd(Widget widget, Device device, int blockOthers)
490 	{
491 		// void gtk_device_grab_add (GtkWidget *widget,  GdkDevice *device,  gboolean block_others);
492 		gtk_device_grab_add((widget is null) ? null : widget.getWidgetStruct(), (device is null) ? null : device.getDeviceStruct(), blockOthers);
493 	}
494 	
495 	/**
496 	 * Removes a device grab from the given widget.
497 	 * You have to pair calls to gtk_device_grab_add() and
498 	 * gtk_device_grab_remove().
499 	 * Params:
500 	 * widget = a GtkWidget
501 	 * device = a GdkDevice
502 	 * Since 3.0
503 	 */
504 	public static void deviceGrabRemove(Widget widget, Device device)
505 	{
506 		// void gtk_device_grab_remove (GtkWidget *widget,  GdkDevice *device);
507 		gtk_device_grab_remove((widget is null) ? null : widget.getWidgetStruct(), (device is null) ? null : device.getDeviceStruct());
508 	}
509 	
510 	/**
511 	 * Warning
512 	 * gtk_key_snooper_install has been deprecated since version 3.4 and should not be used in newly-written code. Key snooping should not be done. Events should
513 	 *  be handled by widgets.
514 	 * Installs a key snooper function, which will get called on all
515 	 * key events before delivering them normally.
516 	 * Params:
517 	 * snooper = a GtkKeySnoopFunc
518 	 * funcData = data to pass to snooper
519 	 * Returns: a unique id for this key snooper for use with gtk_key_snooper_remove().
520 	 */
521 	public static uint keySnooperInstall(GtkKeySnoopFunc snooper, void* funcData)
522 	{
523 		// guint gtk_key_snooper_install (GtkKeySnoopFunc snooper,  gpointer func_data);
524 		return gtk_key_snooper_install(snooper, funcData);
525 	}
526 	
527 	/**
528 	 * Warning
529 	 * gtk_key_snooper_remove has been deprecated since version 3.4 and should not be used in newly-written code. Key snooping should not be done. Events should
530 	 *  be handled by widgets.
531 	 * Removes the key snooper function with the given id.
532 	 * Params:
533 	 * snooperHandlerId = Identifies the key snooper to remove
534 	 */
535 	public static void keySnooperRemove(uint snooperHandlerId)
536 	{
537 		// void gtk_key_snooper_remove (guint snooper_handler_id);
538 		gtk_key_snooper_remove(snooperHandlerId);
539 	}
540 	
541 	/**
542 	 * Obtains a copy of the event currently being processed by GTK+.
543 	 * For example, if you are handling a "clicked" signal,
544 	 * the current event will be the GdkEventButton that triggered
545 	 * the ::clicked signal.
546 	 * Returns: a copy of the current event, or NULL if there is no current event. The returned event must be freed with gdk_event_free(). [transfer full]
547 	 */
548 	public static Event getCurrentEvent()
549 	{
550 		// GdkEvent * gtk_get_current_event (void);
551 		auto p = gtk_get_current_event();
552 		
553 		if(p is null)
554 		{
555 			return null;
556 		}
557 		
558 		return ObjectG.getDObject!(Event)(cast(GdkEvent*) p);
559 	}
560 	
561 	/**
562 	 * If there is a current event and it has a timestamp,
563 	 * return that timestamp, otherwise return GDK_CURRENT_TIME.
564 	 * Returns: the timestamp from the current event, or GDK_CURRENT_TIME.
565 	 */
566 	public static uint getCurrentEventTime()
567 	{
568 		// guint32 gtk_get_current_event_time (void);
569 		return gtk_get_current_event_time();
570 	}
571 	
572 	/**
573 	 * If there is a current event and it has a state field, place
574 	 * that state field in state and return TRUE, otherwise return
575 	 * FALSE.
576 	 * Params:
577 	 * state = a location to store the state of the current event. [out]
578 	 * Returns: TRUE if there was a current event and it had a state field
579 	 */
580 	public static int getCurrentEventState(out GdkModifierType state)
581 	{
582 		// gboolean gtk_get_current_event_state (GdkModifierType *state);
583 		return gtk_get_current_event_state(&state);
584 	}
585 	
586 	/**
587 	 * If there is a current event and it has a device, return that
588 	 * device, otherwise return NULL.
589 	 * Returns: a GdkDevice, or NULL. [transfer none]
590 	 */
591 	public static Device getCurrentEventDevice()
592 	{
593 		// GdkDevice * gtk_get_current_event_device (void);
594 		auto p = gtk_get_current_event_device();
595 		
596 		if(p is null)
597 		{
598 			return null;
599 		}
600 		
601 		return ObjectG.getDObject!(Device)(cast(GdkDevice*) p);
602 	}
603 	
604 	/**
605 	 * If event is NULL or the event was not associated with any widget,
606 	 * returns NULL, otherwise returns the widget that received the event
607 	 * originally.
608 	 * Params:
609 	 * event = a GdkEvent
610 	 * Returns: the widget that originally received event, or NULL. [transfer none]
611 	 */
612 	public static Widget getEventWidget(Event event)
613 	{
614 		// GtkWidget * gtk_get_event_widget (GdkEvent *event);
615 		auto p = gtk_get_event_widget((event is null) ? null : event.getEventStruct());
616 		
617 		if(p is null)
618 		{
619 			return null;
620 		}
621 		
622 		return ObjectG.getDObject!(Widget)(cast(GtkWidget*) p);
623 	}
624 	
625 	/**
626 	 * Sends an event to a widget, propagating the event to parent widgets
627 	 * if the event remains unhandled.
628 	 * Events received by GTK+ from GDK normally begin in gtk_main_do_event().
629 	 * Depending on the type of event, existence of modal dialogs, grabs, etc.,
630 	 * the event may be propagated; if so, this function is used.
631 	 * gtk_propagate_event() calls gtk_widget_event() on each widget it
632 	 * decides to send the event to. So gtk_widget_event() is the lowest-level
633 	 * function; it simply emits the "event" and possibly an
634 	 * event-specific signal on a widget. gtk_propagate_event() is a bit
635 	 * higher-level, and gtk_main_do_event() is the highest level.
636 	 * All that said, you most likely don't want to use any of these
637 	 * functions; synthesizing events is rarely needed. There are almost
638 	 * certainly better ways to achieve your goals. For example, use
639 	 * gdk_window_invalidate_rect() or gtk_widget_queue_draw() instead
640 	 * of making up expose events.
641 	 * Params:
642 	 * widget = a GtkWidget
643 	 * event = an event
644 	 */
645 	public static void propagateEvent(Widget widget, Event event)
646 	{
647 		// void gtk_propagate_event (GtkWidget *widget,  GdkEvent *event);
648 		gtk_propagate_event((widget is null) ? null : widget.getWidgetStruct(), (event is null) ? null : event.getEventStruct());
649 	}
650 }