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