Index

Package: Main

Description

package Glib.Main is
It is possible to create new instances of GMainLoop recursively. This is often used in GTK+ applications when showing modal dialog boxes. Note that event sources are associated with a particular GMainContext, and will be checked and dispatched for all main loops associated with that GMainContext. Creating new sources types ========================== One of the unusual features of the GTK+ main loop functionality is that new types of event source can be created and used in addition to the builtin type of event source. A new event source type is used for handling GDK events. New source types basically interact with with the main context in two ways. Their prepare function in GSourceFuncs can set a timeout to determine the maximum amount of time that the main loop will sleep before checking the source again. In addition, or as well, the source can add file descriptors to the set that the main context checks using g_source_add_poll(). Ada === Some of these features duplicate Ada builtin tasking support, but the latter might be more complex to use in the context of a graphical application, since most of the time the windowing system doesn't support multi-threaded applications. </description>

Binding from C File version glib 2.10.2

<testgtk>create_sources.adb</testgtk>

Packages

Generic_Sources (generic)

Types

G_Main_Context

type G_Main_Context is new Glib.C_Proxy;
This type represents a set of sources to handled in the main loop. Basically, this represents a main loop. There might be several main loops running at the same time, although gtk+ itself has only one, identified as the default main context.

G_Source

type G_Source is new Glib.C_Proxy;
This type represents an event source that can be monitored by the main loop. There are various internal types of such sources, that can be configured by setting appropriate callbacks (this is not yet doable in GtkAda). See Idle_Source_New and Timeout_Source_New.

G_Source_Id

type G_Source_Id is new Guint;
The ID of a source within the context to which it is attached.

G_Source_Func

type G_Source_Func is access function return Boolean;

Source_Prepare_Func

type Source_Prepare_Func is access
     function (Source : G_Source; Timeout : access Gint) return Gboolean;

Source_Check_Func

type Source_Check_Func is access
     function (Source : G_Source) return Gboolean;

G_Source_Func_User_Data

type G_Source_Func_User_Data is access
     function (User_Data : System.Address) return Gboolean;

Source_Dispatch_Func

type Source_Dispatch_Func is access
     function (Source   : G_Source;
               Callback : G_Source_Func_User_Data;
               Data     : System.Address) return Gboolean;

Source_Finalize_Func

type Source_Finalize_Func is access procedure (Source : G_Source);

G_Source_Type

type G_Source_Type is private;

G_Priority

type G_Priority is new Gint;

Constants & Global variables

No_Source_Id (G_Source_Id)

No_Source_Id : constant G_Source_Id;

Null_Source_Type (G_Source_Type)

Null_Source_Type : constant G_Source_Type;

Priority_High (G_Priority)

Priority_High         : constant G_Priority := -100;

Priority_Default (G_Priority)

Priority_Default      : constant G_Priority := 0;

Priority_High_Idle (G_Priority)

Priority_High_Idle    : constant G_Priority := 100;

Priority_Default_Idle (G_Priority)

Priority_Default_Idle : constant G_Priority := 200;

Priority_Low (G_Priority)

Priority_Low          : constant G_Priority := 300;
Priority_High and Priority_Low are not used within glib or gtk+. The priority for all graphical events is Priority_Default. gtk+ uses Priority_High_Idle+10 for resizing operations, and Priority_High_Idle+20 for redrawing operations, to ensure that resizing occurs before redrawing and avoid redrawing twice.

Subprograms & Entries

Main_Context_New

function Main_Context_New return G_Main_Context;
Create a new context

Main_Context_Ref

procedure Main_Context_Ref   
(Context: G_Main_Context);

Main_Context_Unref

procedure Main_Context_Unref 
(Context: G_Main_Context);
Increase or decreate the reference counting for Context. When this reaches 0, the memory is freed.

Main_Context_Default

function Main_Context_Default return G_Main_Context;
Returns the default main context. This is the main context used for main loop functions when a main loop is not explicitly specified.

Wakeup

procedure Wakeup 
(Context: G_Main_Context);
If context is currently waiting in a poll(), interrupt the poll(), and continue the iteration process.

Acquire

function Acquire 
(Context: G_Main_Context) return Boolean;
Tries to become the owner of the specified context. If some other thread is the owner of the context, returns FALSE immediately. Ownership is properly recursive: the owner can require ownership again and will release ownership when Release() is called as many times as Acquire(). You must be the owner of a context before you can call Prepare(), Query(), Check(), Dispatch().

Release

procedure Release 
(Context: G_Main_Context);
Releases ownership of a context previously acquired by this thread with Acquire(). If the context was acquired multiple times, the only release ownership when Release() is called as many times as it was acquired.

Is_Owner

function Is_Owner 
(Context: G_Main_Context) return Boolean;
Determines whether this thread holds the (recursive) ownership of this context. This is useful to know before waiting on another thread that may be blocking to get ownership of context.

Dispatch

procedure Dispatch 
(Context: G_Main_Context);
Dispatches all pending sources.

Depth

function Depth return Integer;
The main loop recursion level in the current thread. It returns 0 when called from the toplevel.

Default_Dispatch

function Default_Dispatch 
(Source: G_Source;
Cb: G_Source_Func_User_Data;
Data: System.Address) return Gboolean;

G_Source_Type_New

function G_Source_Type_New 
(Prepare: Source_Prepare_Func;
Check: Source_Check_Func;
Dispatch: Source_Dispatch_Func := Default_Dispatch'Access;
Finalize: Source_Finalize_Func := null) return G_Source_Type;
Create a new type of sources. This function is specific to GtkAda. The returned value is never freed. Most of the time, you do not need to create a new source type, or even call Source_New. Most things can be implemented through the careful use of Idle and Timeout callbacks. However, creating a new source type allows for cleaner code, by sharing the common part of the handling. For idle sources, the prepare and check functions always return TRUE to indicate that the source is always ready to be processed. The prepare function also returns a timeout value of 0 to ensure that the poll() call doesn't block (since that would be time wasted which could have been spent running the idle function). For timeout sources, the prepare and check functions both return TRUE if the timeout interval has expired. The prepare function also returns a timeout value to ensure that the poll() call doesn't block too long and miss the next timeout. For file descriptor sources, the prepare function typically returns FALSE, since it must wait until poll() has been called before it knows whether any events need to be processed. It sets the returned timeout to -1 to indicate that it doesn't mind how long the poll() call blocks. In the check function, it tests the results of the poll() call to see if the required condition has been met, and returns TRUE if so.

Source_New

function Source_New 
(Source_Type: G_Source_Type;
User_Data: System.Address) return G_Source;
Creates a new GSource structure. The source will not initially be associated with any GMainContext and must be added to one with Attach() before it will be executed.

Get_User_Data

function Get_User_Data 
(Source: G_Source) return System.Address;
Return the user data passed to Source_New. This only applies to sources created through that function, and returns undefined results (or even segfaults) otherwise

Source_Ref

procedure Source_Ref   
(Source: G_Source);

Source_Unref

procedure Source_Unref 
(Source: G_Source);
Increase or decrease the reference counting for Source. When this reaches 0, the Source is destroyed

Source_Destroy

procedure Source_Destroy 
(Source: G_Source);
Removes the source from its context, and mark it as destroyed (the memory is not reclaimed while the reference counting doesn't reach 0). Source cannot be added to another context.

Attach

function Attach 
(Source: G_Source;
Context: G_Main_Context := null) return G_Source_Id;
Add Source to Context. The Source will be executed within that context. If context is null, the source is added to the default context. Returns the Id of the source within Context.

Remove

function Remove 
(Id: G_Source_Id) return Boolean;

Remove

procedure Remove 
(Id: G_Source_Id);
Removes the source with the given id from the default main context. The id of. Return True if the source was found and removed

Set_Priority

procedure Set_Priority 
(Source: G_Source;
Priority: G_Priority);

Get_Priority

function  Get_Priority 
(Source: G_Source) return G_Priority;
Sets the priority of a source. While the main loop is being run, a source will be dispatched if it is ready to be dispatched and no sources at a higher (numerically smaller) priority are ready to be dispatched.

Set_Can_Recurse

procedure Set_Can_Recurse 
(Source: G_Source;
Can_Recurse: Boolean);

Get_Can_Recurse

function  Get_Can_Recurse 
(Source: G_Source) return Boolean;
Sets whether a source can be called recursively. If can_recurse is TRUE, then while the source is being dispatched then this source will be processed normally. Otherwise, all processing of this source is blocked until the dispatch function returns.

Get_Id

function Get_Id 
(Source: G_Source) return G_Source_Id;
Returns the numeric ID for a particular source. The ID of a source is positive integer which is unique within a particular main loop context. The reverse mapping from ID to source is done by Find_Source_By_Id

Find_Source_By_Id

function Find_Source_By_Id 
(Id: G_Source_Id;
Context: G_Main_Context := null) return G_Source;
Find a source given a context and its Id.

Get_Context

function Get_Context 
(Source: G_Source) return G_Main_Context;
Gets the context with which the source is associated. Calling this function on a destroyed source is an error. The returned value is Null for sources that haven't been attached yet

Idle_Source_New

function Idle_Source_New return G_Source;
Return a newly allocated idle G_Source. Such a source is polled whenever the main loop is not processing events with a higher priority. This source must be attached to a main context before it will be executed.

Timeout_Source_New

function Timeout_Source_New 
(Interval: Guint) return G_Source;
Return a newly allocated idle G_Source. Such a source is called at regular intervals. Internval is in milliseconds.

Idle_Add

function Idle_Add 
(Func: G_Source_Func) return G_Source_Id;
Adds a function to be called whenever there are no higher priority events pending in the default main loop. This function is given the priority Priority_Default_Idle. If the function returns False, it is automatically removed from the list of event sources and will not be called again. This function returns the Id of the event source. See Find_Source_By_Id. This is implemented by using Idle_Source_New internally.

Timeout_Add

function Timeout_Add 
(Interval: Guint;
Func: G_Source_Func) return G_Source_Id;
Create a new function to be called periodically until it returns False. Note that timeout functions may be delayed, due to the processing of other event sources. Thus they should not be relied on for precise timing. After each call to the timeout function, the time of the next timeout is recalculated based on the current time and the given interval (it does not try to 'catch up' time lost in delays).