02.04.2014 Views

GtkAda Tutorial - SIGAda

GtkAda Tutorial - SIGAda

GtkAda Tutorial - SIGAda

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

*WN$GD7XWRULDO<br />

Arnaud Charlet<br />

charlet@act-europe.fr


*WN$GD $UFKLWHFWXUH<br />

$SSOLFDWLRQ<br />

*WN$GD<br />

*WN<br />

*GN<br />

*OLE<br />

;OLE:LQ


*WN$GD ² *HWWLQJ6WDUWHG<br />

✔ A very simple program<br />

'HIDXOWLQLWV<br />

'LVSOD\LW<br />

with Gtk.Main, Gtk.Window;<br />

with Gtk.Main, Gtk.Window;<br />

*WN$GDW\SHV<br />

DQGURXWLQHV<br />

procedure Simple is<br />

procedure Simple is<br />

Window : Gtk.Window.Gtk_Window;<br />

Window Gtk.Window.Gtk_Window; &UHDWHD<br />

begin<br />

begin<br />

ZLQGRZ<br />

Gtk.Main.Init;<br />

Gtk.Main.Init;<br />

Gtk.Window.Gtk_New (Window);<br />

Gtk.Window.Gtk_New (Window);<br />

Gtk.Window.Show (Window);<br />

Gtk.Window.Show (Window);<br />

Gtk.Main.Main;<br />

Gtk.Main.Main;<br />

end Simple;<br />

end Simple;<br />

0DLQSURFHVVLQJORRS<br />

– Create a 200x200 pixel window and has no way of<br />

exiting<br />

– Build with gnatmake simple ‘gtkada-config‘


+HOOR:RUOGZLWK*WN$GD<br />

✔See hello.adb and hello_package<br />

✔Compiling Hello World<br />

– Gnatmake hello ‘gtkada-config‘<br />

• Compiler flags<br />

• Linker flags


6LJQDOVDQG&DOOEDFNV<br />

✔<strong>GtkAda</strong>: an event driven toolkit<br />

– Signal: message broadcasted<br />

– Handling via Callbacks or Handlers<br />

• Many handlers per signal<br />

• Handlers are ordered<br />

package Handlers is new Gtk.Handlers.Callback<br />

package Handlers is new Gtk.Handlers.Callback<br />

(Widget_Type => Gtk_Widget_Record);<br />

(Widget_Type => Gtk_Widget_Record);<br />

package Return_Handlers is new Gtk.Handlers.Return_Callback<br />

package Return_Handlers is new Gtk.Handlers.Return_Callback<br />

(Widget_Type => Gtk_Widget_Record,<br />

(Widget_Type => Gtk_Widget_Record,<br />

Return_Type => Boolean);<br />

Return_Type => Boolean);


6LJQDOVDQG&DOOEDFNV<br />

procedure Hello_Callback (Widget : access Gtk_Widget_Record’Class);<br />

procedure Hello_Callback (Widget : access Gtk_Widget_Record’Class);<br />

function Delete_Event<br />

function Delete_Event<br />

(Widget : access Gtk_Widget_Record’Class;<br />

(Widget : access Gtk_Widget_Record’Class;<br />

Event : Gdk_Event) return Boolean;<br />

Event : Gdk_Event) return Boolean;<br />

Return_Handlers.Connect<br />

Return_Handlers.Connect<br />

(Window, "delete_event",<br />

(Window, "delete_event",<br />

Return_Handlers.To_Marshaller (Delete_Event’Access));<br />

Return_Handlers.To_Marshaller (Delete_Event’Access));<br />

Handlers.Connect<br />

Handlers.Connect<br />

(Window, "destroy", Handlers.To_Marshaller (Destroy’Access));<br />

(Window, "destroy", Handlers.To_Marshaller (Destroy’Access));


*WN$GD (YHQWV<br />

✔Special kind of signal<br />

✔Correspond to X events<br />

– “button_press_event”<br />

procedure Button_Press_Handler<br />

procedure Button_Press_Handler<br />

(Widget : access Gtk_Widget_Record’Class;<br />

(Widget : access Gtk_Widget_Record’Class;<br />

Event : Gdk.Event.Gdk_Event;<br />

Event : Gdk.Event.Gdk_Event;<br />

User_Data : ...);<br />

User_Data : ...);<br />

Event_Cb.Connect<br />

Event_Cb.Connect<br />

(Button, -- The object to connect to the handler<br />

(Button, -- The object to connect to the handler<br />

"button_press_event", -- The name of the signal<br />

"button_press_event", -- The name of the signal<br />

Event_Cb.To_Marshaller (Button_Press_Handler’Access),<br />

Event_Cb.To_Marshaller (Button_Press_Handler’Access),<br />

-- The signal handler<br />

-- The signal handler<br />

Data => ...);<br />

Data => ...);


*WN$GD² 0RYLQJ2Q<br />

✔ More on Signal Handlers<br />

– Handler type defined<br />

– Connect procedures and functions<br />

• More than one handler may be connected<br />

– Connect_Object also provided<br />

• No User_Data<br />

• Emitting object substituted<br />

✔Emit_By_Name<br />

– Used when creating new widgets


0RUHRQ6LJQDO+DQGOHUV<br />

✔Disconnect<br />

procedure Disconnect<br />

procedure Disconnect<br />

(Object : access Gtk.Object.Gtk_Object_Record’Class;<br />

(Object : access Gtk.Object.Gtk_Object_Record’Class;<br />

Id : in Handler_Id);<br />

Id : in Handler_Id);<br />

Disconnect the handler identified by the given Handler_Id.<br />

Disconnect the handler identified by the given Handler_Id.<br />

✔Emit_Stop_By_Name<br />

procedure Disconnect<br />

procedure Disconnect<br />

(Object : access Gtk.Object.Gtk_Object_Record’Class;<br />

(Object : access Gtk.Object.Gtk_Object_Record’Class;<br />

Id : in Handler_Id);<br />

Id : in Handler_Id);<br />

Disconnect the handler identified by the given Handler_Id.<br />

Disconnect the handler identified by the given Handler_Id.<br />

✔Handler_Block/Unblock/Destroy<br />

procedure Handler_Block<br />

procedure Handler_Block<br />

(Obj : access Gtk.Object.Gtk_Object_Record’Class;<br />

(Obj : access Gtk.Object.Gtk_Object_Record’Class;<br />

Id : in Handler_Id);<br />

Id : in Handler_Id);<br />

Block temporarily the signal. For each call to this procedure,<br />

Block temporarily the signal. For each call to this procedure,<br />

a call to Handler_Unblock is needed to unblock the signal.<br />

a call to Handler_Unblock is needed to unblock the signal.


$QXSJUDGHG+HOOR:RUOG<br />

✔See hello2.adb and hello2_package<br />

✔Packing widgets


3DFNLQJ:LGJHWV<br />

✔ Theory of packing boxes<br />

– Invisible containers<br />

– Horizontal or Vertical<br />

– Gtk_New_Hbox / Gtk_New_Vbox<br />

✔Details of boxes<br />

– Boxes provide lots of options<br />

– Basically 5 cases


3DFNLQJ:LGJHWV<br />

✔Theory of packing boxes<br />

procedure Pack_Start<br />

procedure Pack_Start<br />

(In_Box : access Gtk_Box_Record;<br />

(In_Box : access Gtk_Box_Record;<br />

Child : access Gtk.Widget.Gtk_Widget_Record’Class;<br />

Child : access Gtk.Widget.Gtk_Widget_Record’Class;<br />

Expand : in Boolean := True;<br />

Expand : in Boolean := True;<br />

Fill : in Boolean := True;<br />

Fill : in Boolean := True;<br />

Padding : in Gint := 0);<br />

Padding : in Gint := 0);


3DFNLQJ:LGJHWV<br />

✔Difference between spacing and padding<br />

– Spacing between objects<br />

– Padding around objects


3DFNLQJ8VLQJ7DEOHV<br />

✔Container with any number of children<br />

– Each Child is put in a cell<br />

– Cells can have same or different sizes<br />

procedure Gtk_New<br />

procedure Gtk_New<br />

(Table : out Gtk_Table;<br />

(Table : out Gtk_Table;<br />

Rows : in Guint;<br />

Rows : in Guint;<br />

Columns : in Guint;<br />

Columns : in Guint;<br />

Homogeneous : in Boolean);<br />

Homogeneous : in Boolean);<br />

The width allocated to the table is divided into Columns columns,<br />

The width allocated to the table is divided into Columns columns,<br />

which all have the same width if Homogeneous is True.<br />

which all have the same width if Homogeneous is True.<br />

If Homogeneous is False, the width will be calculated with the<br />

If Homogeneous is False, the width will be calculated with the<br />

children contained in the table.<br />

children contained in the table.<br />

Same behavior for the rows.<br />

Same behavior for the rows.


3DFNLQJ8VLQJ7DEOHV<br />

procedure Attach<br />

procedure Attach<br />

(Table : access Gtk_Table_Record;<br />

(Table : access Gtk_Table_Record;<br />

Child : access Gtk.Widget.Gtk_Widget_Record’Class;<br />

Child : access Gtk.Widget.Gtk_Widget_Record’Class;<br />

Left_Attach : in Guint;<br />

Left_Attach : in Guint;<br />

Right_Attach : in Guint;<br />

Right_Attach : in Guint;<br />

Top_Attach : in Guint;<br />

Top_Attach : in Guint;<br />

Bottom_Attach : in Guint;<br />

Bottom_Attach : in Guint;<br />

Xoptions : in Gtk_Attach_Options := Expand or Fill;<br />

Xoptions : in Gtk_Attach_Options := Expand or Fill;<br />

Yoptions : in Gtk_Attach_Options := Expand or Fill;<br />

Yoptions : in Gtk_Attach_Options := Expand or Fill;<br />

Xpadding : in Guint := 0;<br />

Xpadding : in Guint := 0;<br />

Ypadding : in Guint := 0);<br />

Ypadding : in Guint := 0);<br />

procedure Set_Row_Spacing<br />

procedure Set_Row_Spacing<br />

(Table : access Gtk_Table_Record;<br />

(Table : access Gtk_Table_Record;<br />

Row : in Guint;<br />

Row : in Guint;<br />

Spacing : in Guint);<br />

Spacing : in Guint);


3DFNLQJ8VLQJ7DEOHV<br />

✔Attach Options<br />

– Fill: the widget will expand to use all the room<br />

available<br />

– Shrink: the widget will shrink the table<br />

– Expand: Cause the taqble to expand to use up<br />

any remaining space in the window


:LGJHW2YHUYLHZ<br />

✔ General steps to creating a widget:<br />

– Gtk_New – one of various functions to create a<br />

new widget<br />

– Connect all signals and events we wish to use to<br />

the appropriate handlers<br />

– Set the attributes of the widget<br />

– Pack the widget into a container using the<br />

appropriate call such as Gtk.Container.Add or<br />

Gtk.Box.Pack_Start<br />

– Gtk.Widget.Show the widget


:LGJHW+LHUDUFK\


:LGJHWV:LWKRXW:LQGRZV<br />

✔ The following widgets do not have an associated<br />

window. To capture events, use the Event_Box<br />

– Gtk_Alignment, Gtk_Arrow, Gtk_Bin, Gtk_Box, Gtk_Image,<br />

Gtk_Item, Gtk_Label, Gtk_Pixmap, Gtk_Scrolled_Window,<br />

Gtk_Separator, Gtk_Table, Gtk_Aspect_Frame, Gtk_Frame,<br />

Gtk_Vbox, Gtk_Hbox, Gtk_Vseparator, Gtk_Hseparator


*WN$GD²:LGJHWV'HWDLOV<br />

✔ The Button Widget<br />

– Normal Buttons<br />

– Toggle Buttons<br />

– Check Buttons<br />

– Radio Buttons<br />

✔Adjustments<br />

✔Range Widget<br />

– Scrollbar<br />

– Scale


6HWWLQJ:LGJHW$WWULEXWHV<br />

✔Name<br />

✔Style<br />

✔Position<br />

✔Size<br />

✔Accelerators<br />

✔…


7LPHRXWV,GOH)XQFWLRQV<br />

✔Timeout<br />

Function Timeout_Add<br />

Function Timeout_Add<br />

(Interval : in Guint32;<br />

(Interval : in Guint32;<br />

Func : Timeout_Callback)<br />

Func : Timeout_Callback)<br />

return Timeout_Handler_Id;<br />

return Timeout_Handler_Id;<br />

procedure Timeout_Remove (Id : in Timeout_Handler_Id);<br />

procedure Timeout_Remove (Id : in Timeout_Handler_Id);<br />

✔Idle Function<br />

function Idle_Add<br />

function Idle_Add<br />

(Cb : in Idle_Callback;<br />

(Cb : in Idle_Callback;<br />

Priority : in Idle_Priority := Priority_Default_Idle)<br />

Priority : in Idle_Priority := Priority_Default_Idle)<br />

return Idle_Handler_Id;<br />

return Idle_Handler_Id;<br />

procedure Idle_Remove (Id : in Idle_Handler_Id);<br />

procedure Idle_Remove (Id : in Idle_Handler_Id);


:ULWLQJ


*WN$GD² *8,%XLOGHU<br />

✔Overview<br />

✔Two types of Widgets<br />

– Visuals<br />

– Containers<br />

✔Easy to change properties at any time:<br />

– Signals and callbacks<br />

– Position<br />

– Widget specific data


*WN$GD² *8,([DPSOHV<br />

✔EditorEditor<br />

✔Open Open Dialog

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!