gnoga_2.1.2_5f127c56/src/gnoga-gui-view.adb

  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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
------------------------------------------------------------------------------
--                                                                          --
--                   GNOGA - The GNU Omnificent GUI for Ada                 --
--                                                                          --
--                       G N O G A . G U I . V I E W                        --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
--                                                                          --
--                     Copyright (C) 2014 David Botton                      --
--                                                                          --
--  This library is free software;  you can redistribute it and/or modify   --
--  it under terms of the  GNU General Public License  as published by the  --
--  Free Software  Foundation;  either version 3,  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.                    --
--                                                                          --
--  As a special exception under Section 7 of GPL version 3, you are        --
--  granted additional permissions described in the GCC Runtime Library     --
--  Exception, version 3.1, as published by the Free Software Foundation.   --
--                                                                          --
--  You should have received a copy of the GNU General Public License and   --
--  a copy of the GCC Runtime Library Exception along with this program;    --
--  see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see   --
--  <http://www.gnu.org/licenses/>.                                         --
--                                                                          --
--  As a special exception, if other files instantiate generics from this   --
--  unit, or you link this unit with other files to produce an executable,  --
--  this  unit  does not  by itself cause  the resulting executable to be   --
--  covered by the GNU General Public License. This exception does not      --
--  however invalidate any other reasons why the executable file might be   --
--  covered by the  GNU Public License.                                     --
--                                                                          --
--  For more information please go to http://www.gnoga.com                  --
------------------------------------------------------------------------------

with Ada.Strings.Wide_Wide_Maps.Wide_Wide_Constants;

with Gnoga.Gui.Document;
with Gnoga.Gui.Element.Common;

with Gnoga.Server.Connection;
with Gnoga.Server.Template_Parser.Simple;

package body Gnoga.Gui.View is

   --------------
   -- Finalize --
   --------------

   overriding procedure Finalize (Object : in out View_Base_Type) is
   begin
      if not Gnoga.Server.Connection.Shutting_Down then
         for i in Object.Child_Array.First_Index .. Object.Child_Array.Last_Index loop
            if Object.Child_Array.Element (i).Dynamic then
               Object.Child_Array.Element (i).Free;
            end if;
         end loop;
      end if;

      Gnoga.Gui.Element.Element_Type (Object).Finalize;
   end Finalize;

   ------------
   -- Create --
   ------------

   procedure Create
     (View   : in out View_Type;
      Parent : in out Gnoga.Gui.Base.Base_Type'Class;
      ID     : in     String := "")
   is
   begin
      View.Create_From_HTML (Parent, "<div />", ID);
   end Create;

   --------------------
   -- On_Child_Added --
   --------------------

   overriding procedure On_Child_Added
     (View  : in out View_Base_Type;
      Child : in out Gnoga.Gui.Base.Base_Type'Class)
   is
      use Gnoga.Gui.Element;
   begin
      if Child in Element_Type'Class then
         if Element_Type (Child).Auto_Place then
            Element_Type (Child).Place_Inside_Bottom_Of (View);
         end if;
      end if;

      if Child.Dynamic then
         View.Child_Array.Append (Child'Unchecked_Access);
      end if;
   end On_Child_Added;

   ------------------
   --  Fill_Parent --
   ------------------

   procedure Fill_Parent (View : in out View_Base_Type) is
      use Gnoga.Gui.Element;
   begin
      View.Position (Absolute);
      View.Box_Height ("100%");
      View.Box_Width ("100%");
      if View.Height = 0 then
         View.Position (Relative);
      end if;
   end Fill_Parent;

   --------------
   -- Put_Line --
   --------------

   procedure Put_Line
     (View    : in out View_Base_Type;
      Message : in     String;
      Class   : in     String := "";
      ID      : in     String := "")
   is
      D : Gnoga.Gui.Element.Common.DIV_Type;
   begin
      D.Create (View, Message, ID);
      if Class /= "" then
         D.Class_Name (Class);
      end if;
   end Put_Line;

   ---------
   -- Put --
   ---------

   procedure Put
     (View    : in out View_Base_Type;
      Message : in     String;
      Class   : in     String := "";
      ID      : in     String := "")
   is
      S : Gnoga.Gui.Element.Common.Span_Type;
   begin
      S.Create (View, Message, ID);
      if Class /= "" then
         S.Class_Name (Class);
      end if;
   end Put;

   --------------
   -- Put_HTML --
   --------------

   procedure Put_HTML
     (View  : in out View_Base_Type;
      HTML  : in     String;
      Class : in     String := "";
      ID    : in     String := "")
   is
      D : Gnoga.Gui.Element.Element_Type;
   begin
      D.Create_From_HTML (View, Escape_Quotes (HTML), ID);
      if Class /= "" then
         D.Class_Name (Class);
      end if;
   end Put_HTML;

   --------------
   -- New_Line --
   --------------

   procedure New_Line (View : in out View_Base_Type) is
   begin
      View.Put_HTML ("<br />");
   end New_Line;

   ---------------------
   -- Horizontal_Rule --
   ---------------------

   procedure Horizontal_Rule (View : in out View_Base_Type) is
   begin
      View.Put_HTML ("<hr />");
   end Horizontal_Rule;

   ---------------
   -- Load_File --
   ---------------

   procedure Load_File
     (View      : in out View_Base_Type;
      File_Name : in     String;
      Class     : in     String := "";
      ID        : in     String := "")
   is
      S : constant String := Gnoga.Server.Template_Parser.Simple.Load_View (File_Name);
   begin
      View.Put_Line (S, Class, ID);
   end Load_File;

   ---------------
   -- Load_HTML --
   ---------------

   procedure Load_HTML
     (View      : in out View_Base_Type;
      File_Name : in     String;
      Class     : in     String := "";
      ID        : in     String := "")
   is
      use Ada.Strings.Wide_Wide_Maps.Wide_Wide_Constants;

      S : constant String  := Gnoga.Server.Template_Parser.Simple.Load_View (File_Name);
      B : constant Natural := Index (Source => S, Pattern => "<body", Mapping => Lower_Case_Map);
      T : constant Natural := Index (Source => S, Pattern => ">", From => B);
      E : constant Natural := Index (Source => S, Pattern => "</body", Mapping => Lower_Case_Map);
   begin
      if B > 0 and E > 0 then
         View.Put_HTML (S.Slice (T + 1, E - 1), Class, ID);
      end if;
   end Load_HTML;

   --------------
   -- Load_CSS --
   --------------

   procedure Load_CSS
     (View : in out View_Base_Type;
      URL  : in     String)
   is
      Document : Gnoga.Gui.Document.Document_Type;
   begin
      Document.Attach (View.Connection_ID);
      Document.Head_Element.jQuery_Execute ("append ('<link rel='stylesheet' href='" & Escape_Quotes (URL) & "' />')");
   end Load_CSS;

   -------------------
   -- Load_CSS_File --
   -------------------

   procedure Load_CSS_File
     (View      : in out View_Base_Type;
      File_Name : in     String)
   is
      S : constant String := Gnoga.Server.Template_Parser.Simple.Load_View (File_Name);

      Document : Gnoga.Gui.Document.Document_Type;
   begin
      Document.Attach (View.Connection_ID);
      Document.Head_Element.jQuery_Execute ("append ('<style>" & Escape_Quotes (S) & "</style>'");
   end Load_CSS_File;

   -----------------
   -- Add_Element --
   -----------------

   procedure Add_Element
     (View    : in out View_Base_Type;
      Name    : in     String;
      Element :        Gnoga.Gui.Element.Pointer_To_Element_Class)
   is
   begin
      View.Element_Map.Include (Key => Name, New_Item => Element);
   end Add_Element;

   -----------------
   -- New_Element --
   -----------------

   function New_Element
     (View    : access View_Base_Type;
      Name    : String;
      Element : Gnoga.Gui.Element.Pointer_To_Element_Class)
      return Gnoga.Gui.Element.Pointer_To_Element_Class
   is
   begin
      View.Add_Element (Name, Element);
      Element.Dynamic;
      return Element.all'Unrestricted_Access;
   end New_Element;

   ---------
   -- Add --
   ---------

   function Add
     (View    : access View_Base_Type;
      Element : access Gnoga.Gui.Element.Element_Type'Class)
      return Gnoga.Gui.Element.Pointer_To_Element_Class
   is
      pragma Unreferenced (View);
   begin
      Element.Dynamic;
      return Element.all'Unrestricted_Access;
   end Add;

   -------------
   -- Element --
   -------------

   function Element
     (View : View_Base_Type;
      Name : String)
      return Gnoga.Gui.Element.Pointer_To_Element_Class
   is
   begin
      if View.Element_Map.Contains (Name) then
         return View.Element_Map.Element (Name);
      else
         return null;
      end if;
   end Element;

   -------------------
   -- Element_Names --
   -------------------

   function Element_Names
     (View : View_Base_Type)
      return Gnoga.Types.Data_Array_Type
   is
      Names : Gnoga.Types.Data_Array_Type;

      procedure Add_Name (C : in Gnoga.Gui.Element.Element_Type_Maps.Cursor);

      procedure Add_Name (C : in Gnoga.Gui.Element.Element_Type_Maps.Cursor) is
      begin
         Names.Append (String'(Gnoga.Gui.Element.Element_Type_Maps.Key (C)));
      end Add_Name;
   begin
      View.Element_Map.Iterate (Add_Name'Access);

      return Names;
   end Element_Names;

end Gnoga.Gui.View;