| Top |
Before using CTK+, you need to initialize it; initialization connects to the
window system display, and parses some standard command line arguments. The
ctk_init() macro initializes CTK+. ctk_init() exits the application if errors
occur; to avoid this, use ctk_init_check(). ctk_init_check() allows you to
recover from a failed CTK+ initialization - you might start up your
application in text mode instead.
Like all GUI toolkits, CTK+ uses an event-driven programming model. When the user is doing nothing, CTK+ sits in the “main loop” and waits for input. If the user performs some action - say, a mouse click - then the main loop “wakes up” and delivers an event to CTK+. CTK+ forwards the event to one or more widgets.
When widgets receive an event, they frequently emit one or more
“signals”. Signals notify your program that "something
interesting happened" by invoking functions you’ve connected to the signal
with g_signal_connect(). Functions connected to a signal are often termed
“callbacks”.
When your callbacks are invoked, you would typically take some action - for example, when an Open button is clicked you might display a CtkFileChooserDialog. After a callback finishes, CTK+ will return to the main loop and await more user input.
main() function for a CTK+ application1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
int main (int argc, char **argv) { CtkWidget *mainwin; // Initialize i18n support with bindtextdomain(), etc. // ... // Initialize the widget set ctk_init (&argc, &argv); // Create the main window mainwin = ctk_window_new (CTK_WINDOW_TOPLEVEL); // Set up our GUI elements // ... // Show the application window ctk_widget_show_all (mainwin); // Enter the main event loop, and wait for user interaction ctk_main (); // The user lost interest return 0; } |
It’s OK to use the GLib main loop directly instead of ctk_main(), though it
involves slightly more typing. See GMainLoop in the GLib documentation.
void
ctk_disable_setlocale (void);
Prevents ctk_init(), ctk_init_check(), ctk_init_with_args() and
ctk_parse_args() from automatically
calling setlocale (LC_ALL, ""). You would
want to use this function if you wanted to set the locale for
your program to something other than the user’s locale, or if
you wanted to set different values for different locale categories.
Most programs should not need to call this function.
PangoLanguage *
ctk_get_default_language (void);
Returns the PangoLanguage for the default language currently in effect. (Note that this can change over the life of an application.) The default language is derived from the current locale. It determines, for example, whether CTK+ uses the right-to-left or left-to-right text direction.
This function is equivalent to pango_language_get_default().
See that function for details.
CtkTextDirection
ctk_get_locale_direction (void);
Get the direction of the current locale. This is the expected reading direction for text and UI.
This function depends on the current locale being set with
setlocale() and will default to setting the CTK_TEXT_DIR_LTR
direction otherwise. CTK_TEXT_DIR_NONE will never be returned.
CTK+ sets the default text direction according to the locale
during ctk_init(), and you should normally use
ctk_widget_get_direction() or ctk_widget_get_default_direction()
to obtain the current direcion.
This function is only needed rare cases when the locale is changed after CTK+ has already been initialized. In this case, you can use it to update the default text direction as follows:
1 2 3 |
setlocale (LC_ALL, new_locale); direction = ctk_get_locale_direction (); ctk_widget_set_default_direction (direction); |
Since: 3.12
gboolean ctk_parse_args (int *argc,char ***argv);
Parses command line arguments, and initializes global
attributes of CTK+, but does not actually open a connection
to a display. (See cdk_display_open(), cdk_get_display_arg_name())
Any arguments used by CTK+ or CDK are removed from the array and
argc
and argv
are updated accordingly.
There is no need to call this function explicitly if you are using
ctk_init(), or ctk_init_check().
Note that many aspects of CTK+ require a display connection to function, so this way of initializing CTK+ is really only useful for specialized use cases.
void ctk_init (int *argc,char ***argv);
Call this function before using any other CTK+ functions in your GUI applications. It will initialize everything needed to operate the toolkit and parses some standard command line options.
Although you are expected to pass the argc
, argv
parameters from main() to
this function, it is possible to pass NULL if argv
is not available or
commandline handling is not required.
argc
and argv
are adjusted accordingly so your own code will
never see those standard arguments.
Note that there are some alternative ways to initialize CTK+:
if you are calling ctk_parse_args(), ctk_init_check(),
ctk_init_with_args() or g_option_context_parse() with
the option group returned by ctk_get_option_group(),
you don’t have to call ctk_init().
And if you are using CtkApplication, you don't have to call any of the initialization functions either; the “startup” handler does it for you.
This function will terminate your program if it was unable to
initialize the windowing system for some reason. If you want
your program to fall back to a textual interface you want to
call ctk_init_check() instead.
Since 2.18, CTK+ calls signal (SIGPIPE, SIG_IGN)
during initialization, to ignore SIGPIPE signals, since these are
almost never wanted in graphical applications. If you do need to
handle SIGPIPE for some reason, reset the handler after ctk_init(),
but notice that other libraries (e.g. libdbus or gvfs) might do
similar things.
argc |
Address of the |
[inout] |
argv |
Address of the
|
[array length=argc][inout][allow-none] |
gboolean ctk_init_check (int *argc,char ***argv);
This function does the same work as ctk_init() with only a single
change: It does not terminate the program if the commandline
arguments couldn’t be parsed or the windowing system can’t be
initialized. Instead it returns FALSE on failure.
This way the application can fall back to some other means of communication with the user - for example a curses or command line interface.
Note that calling any CTK function or instantiating any CTK type after
this function returns FALSE results in undefined behavior.
argc |
Address of the |
[inout] |
argv |
Address of the
|
[array length=argc][inout][allow-none] |
gboolean ctk_init_with_args (gint *argc,gchar ***argv,const gchar *parameter_string,const GOptionEntry *entries,const gchar *translation_domain,GError **error);
This function does the same work as ctk_init_check().
Additionally, it allows you to add your own commandline options,
and it automatically generates nicely formatted
--help output. Note that your program will
be terminated after writing out the help output.
argc |
Address of the |
[inout] |
argv |
Address of the
|
[array length=argc][inout][allow-none] |
parameter_string |
a string which is displayed in
the first line of |
[allow-none] |
entries |
a |
[array zero-terminated=1] |
translation_domain |
a translation domain to use for translating
the |
[nullable] |
error |
a return location for errors |
TRUE if the commandline arguments (if any) were valid and
if the windowing system has been successfully initialized,
FALSE otherwise
Since: 2.6
GOptionGroup *
ctk_get_option_group (gboolean open_default_display);
Returns a GOptionGroup for the commandline arguments recognized by CTK+ and CDK.
You should add this group to your GOptionContext
with g_option_context_add_group(), if you are using
g_option_context_parse() to parse your commandline arguments.
open_default_display |
whether to open the default display when parsing the commandline arguments |
Since: 2.6
gboolean
ctk_events_pending (void);
Checks if any events are pending.
This can be used to update the UI and invoke timeouts etc. while doing some time intensive computation.
void
ctk_main (void);
Runs the main loop until ctk_main_quit() is called.
You can nest calls to ctk_main(). In that case ctk_main_quit()
will make the innermost invocation of the main loop return.
void
ctk_main_quit (void);
Makes the innermost invocation of the main loop return when it regains control.
gboolean
ctk_main_iteration (void);
Runs a single iteration of the mainloop.
If no events are waiting to be processed CTK+ will block
until the next event is noticed. If you don’t want to block
look at ctk_main_iteration_do() or check if any events are
pending with ctk_events_pending() first.
gboolean
ctk_main_iteration_do (gboolean blocking);
Runs a single iteration of the mainloop.
If no events are available either return or block depending on
the value of blocking
.
void
ctk_main_do_event (CdkEvent *event);
Processes a single CDK event.
This is public only to allow filtering of events between CDK and CTK+. You will not usually need to call this function directly.
While you should not call this function directly, you might want to know how exactly events are handled. So here is what this function does with the event:
Compress enter/leave notify events. If the event passed build an enter/leave pair together with the next event (peeked from CDK), both events are thrown away. This is to avoid a backlog of (de-)highlighting widgets crossed by the pointer.
Find the widget which got the event. If the widget can’t be determined the event is thrown away unless it belongs to a INCR transaction.
Then the event is pushed onto a stack so you can query the currently
handled event with ctk_get_current_event().
The event is sent to a widget. If a grab is active all events for widgets that are not in the contained in the grab widget are sent to the latter with a few exceptions:
Deletion and destruction events are still sent to the event widget for obvious reasons.
Events which directly relate to the visual representation of the event widget.
Leave events are delivered to the event widget if there was an enter event delivered to it before without the paired leave event.
Drag events are not redirected because it is unclear what the semantics
of that would be.
Another point of interest might be that all key events are first passed
through the key snooper functions if there are any. Read the description
of ctk_key_snooper_install() if you need this feature.
After finishing the delivery the event is popped from the event stack.
void (*CtkModuleInitFunc) (gint *argc,gchar ***argv);
Each CTK+ module must have a function ctk_module_init() with this prototype.
This function is called after loading the module.
void
(*CtkModuleDisplayInitFunc) (CdkDisplay *display);
A multihead-aware CTK+ module may have a ctk_module_display_init() function
with this prototype. CTK+ calls this function for each opened display.
Since: 2.2
gboolean
ctk_true (void);
All this function does it to return TRUE.
This can be useful for example if you want to inhibit the deletion of a window. Of course you should not do this as the user expects a reaction from clicking the close icon of the window...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
#include <ctk/ctk.h> int main (int argc, char **argv) { CtkWidget *win, *but; const char *text = "Close yourself. I mean it!"; ctk_init (&argc, &argv); win = ctk_window_new (CTK_WINDOW_TOPLEVEL); g_signal_connect (win, "delete-event", G_CALLBACK (ctk_true), NULL); g_signal_connect (win, "destroy", G_CALLBACK (ctk_main_quit), NULL); but = ctk_button_new_with_label (text); g_signal_connect_swapped (but, "clicked", G_CALLBACK (ctk_object_destroy), win); ctk_container_add (CTK_CONTAINER (win), but); ctk_widget_show_all (win); ctk_main (); return 0; } |
gboolean
ctk_false (void);
Analogical to ctk_true(), this function does nothing
but always returns FALSE.
void
ctk_grab_add (CtkWidget *widget);
Makes widget
the current grabbed widget.
This means that interaction with other widgets in the same application is blocked and mouse as well as keyboard events are delivered to this widget.
If widget
is not sensitive, it is not set as the current
grabbed widget and this function does nothing.
[method]
CtkWidget *
ctk_grab_get_current (void);
Queries the current grab of the default window group.
void
ctk_grab_remove (CtkWidget *widget);
Removes the grab from the given widget.
You have to pair calls to ctk_grab_add() and ctk_grab_remove().
If widget
does not have the grab, this function does nothing.
[method]
void ctk_device_grab_add (CtkWidget *widget,CdkDevice *device,gboolean block_others);
Adds a CTK+ grab on device
, so all the events on device
and its
associated pointer or keyboard (if any) are delivered to widget
.
If the block_others
parameter is TRUE, any other devices will be
unable to interact with widget
during the grab.
Since: 3.0
void ctk_device_grab_remove (CtkWidget *widget,CdkDevice *device);
Removes a device grab from the given widget.
You have to pair calls to ctk_device_grab_add() and
ctk_device_grab_remove().
Since: 3.0
guint ctk_key_snooper_install (CtkKeySnoopFunc snooper,gpointer func_data);
ctk_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 be handled by widgets.
Installs a key snooper function, which will get called on all key events before delivering them normally.
[skip]
gint (*CtkKeySnoopFunc) (CtkWidget *grab_widget,CdkEventKey *event,gpointer func_data);
Key snooper functions are called before normal event delivery. They can be used to implement custom key event handling.
grab_widget |
the widget to which the event will be delivered |
|
event |
the key event |
|
func_data |
data supplied to |
[closure] |
void
ctk_key_snooper_remove (guint snooper_handler_id);
ctk_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 be handled by widgets.
Removes the key snooper function with the given id.
CdkEvent *
ctk_get_current_event (void);
Obtains a copy of the event currently being processed by CTK+.
For example, if you are handling a “clicked” signal, the current event will be the CdkEventButton that triggered the ::clicked signal.
a copy of the current event, or
NULL if there is no current event. The returned event must be
freed with cdk_event_free().
[transfer full][nullable]
guint32
ctk_get_current_event_time (void);
If there is a current event and it has a timestamp,
return that timestamp, otherwise return CDK_CURRENT_TIME.
gboolean
ctk_get_current_event_state (CdkModifierType *state);
If there is a current event and it has a state field, place
that state field in state
and return TRUE, otherwise return
FALSE.
CdkDevice *
ctk_get_current_event_device (void);
If there is a current event and it has a device, return that
device, otherwise return NULL.
CtkWidget *
ctk_get_event_widget (CdkEvent *event);
If event
is NULL or the event was not associated with any widget,
returns NULL, otherwise returns the widget that received the event
originally.
void ctk_propagate_event (CtkWidget *widget,CdkEvent *event);
Sends an event to a widget, propagating the event to parent widgets if the event remains unhandled.
Events received by CTK+ from CDK normally begin in ctk_main_do_event().
Depending on the type of event, existence of modal dialogs, grabs, etc.,
the event may be propagated; if so, this function is used.
ctk_propagate_event() calls ctk_widget_event() on each widget it
decides to send the event to. So ctk_widget_event() is the lowest-level
function; it simply emits the “event” and possibly an
event-specific signal on a widget. ctk_propagate_event() is a bit
higher-level, and ctk_main_do_event() is the highest level.
All that said, you most likely don’t want to use any of these
functions; synthesizing events is rarely needed. There are almost
certainly better ways to achieve your goals. For example, use
cdk_window_invalidate_rect() or ctk_widget_queue_draw() instead
of making up expose events.
#define CTK_PRIORITY_RESIZE (G_PRIORITY_HIGH_IDLE + 10)
Use this priority for functionality related to size allocation.
It is used internally by CTK+ to compute the sizes of widgets.
This priority is higher than CDK_PRIORITY_REDRAW to avoid
resizing a widget which was just redrawn.