File: create_gtkada_builder.adb

package info (click to toggle)
libgtkada 2.24.4dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 12,208 kB
  • ctags: 1,676
  • sloc: ada: 119,686; ansic: 4,719; sh: 3,003; makefile: 690; xml: 338; perl: 70
file content (269 lines) | stat: -rw-r--r-- 9,078 bytes parent folder | download
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
-----------------------------------------------------------------------
--          GtkAda - Ada95 binding for the Gimp Toolkit              --
--                                                                   --
--                 Copyright (C) 2011-2013, AdaCore                  --
--                                                                   --
-- This library is free software; you can redistribute it and/or     --
-- modify it under the terms of the GNU General Public               --
-- License as published by the Free Software Foundation; either      --
-- version 2 of the License, or (at your option) any later version.  --
--                                                                   --
-- This library is distributed in the hope that it will be useful,   --
-- but WITHOUT ANY WARRANTY; without even the implied warranty of    --
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU --
-- General Public License for more details.                          --
--                                                                   --
-- You should have received a copy of the GNU General Public         --
-- License along with this library; if not, write to the             --
-- Free Software Foundation, Inc., 59 Temple Place - Suite 330,      --
-- Boston, MA 02111-1307, USA.                                       --
--                                                                   --
-----------------------------------------------------------------------

--  with System;               use System;
--  with System.Address_Image;
with Ada.Exceptions;
with Ada.Text_IO;          use Ada.Text_IO;
--  with Interfaces.C.Strings; use Interfaces.C.Strings;

with Glib;                 use Glib;
with Glib.Error;           use Glib.Error;
with Glib.Object;          use Glib.Object;

with Gtk.Box;              use Gtk.Box;
with Gtk.Button;           use Gtk.Button;
with Gtk.GEntry;           use Gtk.GEntry;
with Gtk.Frame;            use Gtk.Frame;

with Gtk.Text_Buffer;      use Gtk.Text_Buffer;
with Gtk.Text_View;        use Gtk.Text_View;
with Gtk.Widget;

with Common;               use Common;

with Gtkada.Builder;       use Gtkada.Builder;

package body Create_Gtkada_Builder is

   Default_Filename : constant String := "gtkbuilder_example.xml";
   --  This is the file from which we'll read our UI description.

   procedure On_Button_Clicked (Button : access Gtk_Button_Record'Class);
   --  Callback for a button click

   procedure On_Btn_Concatenate_Clicked
     (Builder : access Gtkada_Builder_Record'Class);
   procedure On_Btn_Console_Greeting_Clicked
     (Builder : access Gtkada_Builder_Record'Class);
   function On_Window1_Delete_Event
     (Builder : access Gtkada_Builder_Record'Class) return Boolean;
   procedure On_Window1_Destroy
     (Builder : access Gtkada_Builder_Record'Class);

   procedure On_Print_To_Console (Object : access GObject_Record'Class);
   --  Callbacks referenced by our XML UI definition.  These match the
   --  items in the Callback_Function_Name enumeration.

   procedure Add_Custom_Widget (Box : Gtk_Hbox);
   --  Add custom widgets from the file "gtkbuilder_custom_widget.xml"
   --  to the given hbox

   -----------------------
   -- Add_Custom_Widget --
   -----------------------

   procedure Add_Custom_Widget (Box : Gtk_Hbox) is
      Builder : Gtkada_Builder;
      Error   : GError;
   begin
      --  Create a builder
      Gtk_New (Builder);

      --  Load the custom widget from the XML description
      Error := Add_From_File (Builder, "gtkbuilder_custom_widget.xml");
      if Error /= null then
         Put_Line ("Error [Create_Builder.Add_Custom_Widget]: "
                   & Get_Message (Error));
         Error_Free (Error);
         return;
      end if;

      --  Now get the widget...
      declare
         Custom_Widget : constant Gtk.Widget.Gtk_Widget :=
           Get_Widget (Builder, "custom");
      begin
         --  ... And add it to our Box
         Pack_Start (Box, Custom_Widget);
         Gtk.Widget.Show_All (Custom_Widget);
      end;
   end Add_Custom_Widget;

   -------------------------
   -- On_Print_To_Console --
   -------------------------

   procedure On_Print_To_Console (Object : access GObject_Record'Class) is
      Term1 : constant Gtk_Entry := Gtk_Entry (Object);
   begin
      Put_Line ("String 1 is: " & Get_Text (Term1));
   end On_Print_To_Console;

   -----------------------
   -- On_Button_Clicked --
   -----------------------

   procedure On_Button_Clicked (Button : access Gtk_Button_Record'Class) is
      pragma Unreferenced (Button);

      Builder : Gtkada_Builder;
      Error   : GError;
   begin
      --  Create a new Gtkada_Builder object
      Gtk_New (Builder);

      --  Read in our XML file
      Error := Add_From_File (Builder, Default_Filename);

      if Error /= null then
         Put_Line ("Error [Create_Builder.On_Button_Clicked]: "
                   & Get_Message (Error));
         Error_Free (Error);
      end if;

      --  Do the necessary connections

      Register_Handler
        (Builder      => Builder,
         Handler_Name => "on_btn_concatenate_clicked",
         Handler      => On_Btn_Concatenate_Clicked'Access);

      Register_Handler
        (Builder      => Builder,
         Handler_Name => "on_btn_console_greeting_clicked",
         Handler      => On_Btn_Console_Greeting_Clicked'Access);

      Register_Handler
        (Builder      => Builder,
         Handler_Name => "on_window1_delete_event",
         Handler      => On_Window1_Delete_Event'Access);

      Register_Handler
        (Builder      => Builder,
         Handler_Name => "on_window1_destroy",
         Handler      => On_Window1_Destroy'Access);

      Register_Handler
        (Builder      => Builder,
         Handler_Name => "on_print_to_console",
         Handler      => On_Print_To_Console'Access);

      Do_Connect (Builder);

      --  Add a custom widget 3 times to the placeholder hbox
      Add_Custom_Widget (Gtk_Hbox (Get_Widget (Builder, "placeholder_hbox")));
      Add_Custom_Widget (Gtk_Hbox (Get_Widget (Builder, "placeholder_hbox")));
      Add_Custom_Widget (Gtk_Hbox (Get_Widget (Builder, "placeholder_hbox")));

      --  Find our main window, then display it and all of its children.
      Gtk.Widget.Show_All (Get_Widget (Builder, "window1"));
   end On_Button_Clicked;

   --------------------------------
   -- On_Btn_Concatenate_Clicked --
   --------------------------------

   procedure On_Btn_Concatenate_Clicked
     (Builder : access Gtkada_Builder_Record'Class)
   is
      Buffer  : constant Gtk_Text_Buffer := Get_Buffer
        (Gtk.Text_View.Gtk_Text_View (Get_Widget (Builder, "textField")));
   begin
      Put_Line ("On_Btn_Concatenate_Clicked");

      Insert_At_Cursor
        (Buffer,
         "Concatenated: " &
         Get_Text (Gtk.GEntry.Gtk_Entry (Get_Widget (Builder, "term1"))) &
         Get_Text (Gtk.GEntry.Gtk_Entry (Get_Widget (Builder, "term2"))) &
         ASCII.LF);
   exception
      when Event : others =>
         Put_Line ("Error: " & Ada.Exceptions.Exception_Information (Event));
   end On_Btn_Concatenate_Clicked;

   -------------------------------------
   -- On_Btn_Console_Greeting_Clicked --
   -------------------------------------

   procedure On_Btn_Console_Greeting_Clicked
     (Builder : access Gtkada_Builder_Record'Class)
   is
      pragma Unreferenced (Builder);
   begin
      Put_Line ("On_Btn_Console_Greeting_Clicked says: HELLO!!!");
   end On_Btn_Console_Greeting_Clicked;

   -----------------------------
   -- On_Window1_Delete_Event --
   -----------------------------

   function On_Window1_Delete_Event
     (Builder : access Gtkada_Builder_Record'Class)
      return Boolean
   is
      pragma Unreferenced (Builder);
   begin
      Put_Line ("On_Window1_Delete_Event");

      --  Stop unconditionally.
      return False;
   end On_Window1_Delete_Event;

   ------------------------
   -- On_Window1_Destroy --
   ------------------------

   procedure On_Window1_Destroy
     (Builder : access Gtkada_Builder_Record'Class)
   is
   begin
      --  We actually don't do much here, since within testgtk, we're not
      --  the main window.
      Put_Line ("On_Window1_Destroy");

      --  Free memory associated to the builder
      Unref (Builder);
   end On_Window1_Destroy;

   ----------
   -- Help --
   ----------

   function Help return String is
   begin
      return "";
   end Help;

   ---------
   -- Run --
   ---------

   procedure Run (Frame : access Gtk.Frame.Gtk_Frame_Record'Class) is
      Box1    : Gtk_Box;
      Button1 : Gtk_Button;
   begin
      Set_Label (Frame, "Builder");

      Gtk_New_Vbox (Box1, False, 0);
      Add (Frame, Box1);

      Gtk_New (Button1, "Invoke Builder with file " & Default_Filename);
      Button_Handler.Connect (Button1, "clicked", On_Button_Clicked'Access);
      Pack_Start
        (Box1, Button1, Expand => False, Fill => False, Padding => 10);

      Show_All (Frame);
   end Run;

end Create_Gtkada_Builder;