-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathada_gui-gnoga-gui-window.adb
708 lines (584 loc) · 20.3 KB
/
ada_gui-gnoga-gui-window.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
-- Ada_GUI implementation based on Gnoga. Adapted 2021
-- --
-- GNOGA - The GNU Omnificent GUI for Ada --
-- --
-- G N O G A . G U I . W I N D O 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.Fixed;
with Ada.Strings.Maps;
with Ada.Strings.Unbounded;
with Ada.Calendar.Formatting;
with Ada.Unchecked_Deallocation;
with Ada.Exceptions;
with Ada_GUI.Gnoga.Client_Storage;
with Ada_GUI.Gnoga.Server.Connection;
pragma Elaborate (Ada_GUI.Gnoga.Server.Connection);
with Ada_GUI.Gnoga.Gui.Element;
with Ada_GUI.Gnoga.Gui.View;
package body Ada_GUI.Gnoga.Gui.Window is
--------------
-- Finalize --
--------------
overriding
procedure Finalize (Object : in out Window_Type) is
P : Gnoga.Pointer_to_Connection_Data_Class :=
Object.Connection_Data;
procedure Free_Data is
new Ada.Unchecked_Deallocation
(Gnoga.Connection_Data_Type'Class,
Gnoga.Pointer_to_Connection_Data_Class);
begin
if Object.View /= null and Object.View_Is_Dynamic then
Object.View.Free;
Object.View := null;
else
Object.View := null;
end if;
Gnoga.Gui.Base_Type (Object).Finalize;
if Object.Free_Connection_Data then
Free_Data (P);
Object.Connection_Data (null);
Log ("Connection_Data freed " & Object.Connection_ID'Image);
end if;
exception
when E : others =>
Log ("Error finalizing Window - " & Object.ID);
Log (Ada.Exceptions.Exception_Information (E));
end Finalize;
------------
-- Attach --
------------
overriding procedure Attach
(Window : in out Window_Type;
Connection_ID : in Gnoga.Connection_ID;
ID : in String := "window";
ID_Type : in Gnoga.ID_Enumeration := Gnoga.Script)
is
begin
if ID_Type = Gnoga.DOM_ID then
raise Invalid_ID_Type;
end if;
Gnoga.Gui.Attach
(Object => Gnoga.Gui.Base_Type (Window),
Connection_ID => Connection_ID,
ID => ID,
ID_Type => ID_Type);
Window.DOM_Document.Attach (Connection_ID, ID, ID_Type);
Window.Location.Attach
(Connection_ID => Connection_ID,
ID => Window.jQuery & ".prop ('location')",
ID_Type => Gnoga.Script);
Window.Bind_Event (Event => "resize",
Message => "");
end Attach;
procedure Attach
(Window : in out Window_Type;
Parent : in out Window_Type'Class;
ID : in String;
ID_Type : in Gnoga.ID_Enumeration := Gnoga.Script)
is
CID : constant String := Gnoga.Server.Connection.Execute_Script
(Parent.Connection_ID,
Script_Accessor (ID, ID_Type) & ".gnoga['Connection_ID']");
begin
if ID_Type = Gnoga.DOM_ID then
raise Invalid_ID_Type;
end if;
Attach (Window, Gnoga.Connection_ID'Value (CID));
exception
when E : others =>
Log ("Unable to find gnoga['Connection_ID'] on " & ID & " eval returned : " & CID);
Log (Ada.Exceptions.Exception_Information (E));
raise Not_A_Gnoga_Window;
end Attach;
--------------
-- Reattach --
--------------
procedure Reattach (Window : in out Window_Type;
Parent : in out Window_Type'Class)
is
CID : constant String := Gnoga.Server.Connection.Execute_Script
(Parent.Connection_ID,
Script_Accessor (Window.ID, Window.ID_Type) & ".gnoga['Connection_ID']");
begin
Connection_ID (Object => Window, Value => Gnoga.Connection_ID'Value (CID));
end Reattach;
---------------------------
-- Disable_Auto_Set_View --
---------------------------
procedure Disable_Auto_Set_View (Window : in out Window_Type;
Value : in Boolean := True)
is
begin
Window.Auto_Set_View := not Value;
end Disable_Auto_Set_View;
--------------
-- Set_View --
--------------
procedure Set_View (Window : in out Window_Type;
Object : in out Gnoga.Gui.Base_Type'Class;
Place : in Boolean := True)
is
use Gnoga.Gui.Element;
begin
if not (Object in Element_Type'Class) then
raise Invalid_ID_Type with "Not in Element_Type'Class";
end if;
if Place then
Element_Type (Object).Place_Inside_Top_Of
(Window.Document.Body_Element.all);
end if;
Window.View := Object'Unchecked_Access;
Window.View_Is_Dynamic := Object.Dynamic;
Element_Type (Object).Box_Sizing (Border_Box);
Element_Type (Object).Position (Gnoga.Gui.Element.Fixed);
Element_Type (Object).Display ("block");
Element_Type (Object).Left (0);
Element_Type (Object).Top (0);
Element_Type (Object).Box_Height (Window.Height);
Element_Type (Object).Box_Width (Window.Width);
end Set_View;
--------------
-- Get_View --
--------------
function Get_View (Window : Window_Type)
return Gnoga.Gui.Pointer_To_Base_Class
is
begin
return Window.View;
end Get_View;
-----------------
-- Remove_View --
-----------------
procedure Remove_View (Window : in out Window_Type) is
begin
Window.View := null;
end Remove_View;
--------------
-- Document --
--------------
function Document (Window : Window_Type)
return Gnoga.Gui.Document.Document_Access
is
begin
return Window.DOM_Document'Unrestricted_Access;
end Document;
--------------
-- Location --
--------------
function Location (Window : Window_Type)
return Gnoga.Gui.Location.Location_Access
is
begin
return Window.Location'Unrestricted_Access;
end Location;
------------------------
-- Browser_Status_Bar --
------------------------
procedure Browser_Status_Bar (Window : in out Window_Type;
Value : in String)
is
begin
Window.Property ("status", Value);
end Browser_Status_Bar;
function Browser_Status_Bar (Window : Window_Type) return String
is
begin
return Window.Property ("status");
end Browser_Status_Bar;
----------
-- Name --
----------
procedure Name (Window : in out Window_Type; Value : in String) is
begin
Window.Property ("name", Value);
end Name;
----------
-- Name --
----------
function Name (Window : Window_Type) return String is
begin
return Window.Property ("name");
end Name;
------------------
-- Inner_Height --
------------------
procedure Inner_Height (Window : in out Window_Type; Value : in Integer) is
begin
Window.Property ("innerHeight", Value);
end Inner_Height;
------------------
-- Inner_Height --
------------------
function Inner_Height (Window : Window_Type) return Integer is
begin
return Window.Property ("innerHeight");
end Inner_Height;
-----------------
-- Inner_Width --
-----------------
procedure Inner_Width (Window : in out Window_Type; Value : in Integer) is
begin
Window.Property ("innerWidth", Value);
end Inner_Width;
-----------------
-- Inner_Width --
-----------------
function Inner_Width (Window : Window_Type) return Integer is
begin
return Window.Property ("innerWidth");
end Inner_Width;
------------------
-- Outer_Height --
------------------
procedure Outer_Height (Window : in out Window_Type; Value : in Integer) is
begin
Window.Property ("outerHeight", Value);
end Outer_Height;
------------------
-- Outer_Height --
------------------
function Outer_Height (Window : Window_Type) return Integer is
begin
return Window.Property ("outerHeight");
end Outer_Height;
-----------------
-- Outer_Width --
-----------------
procedure Outer_Width (Window : in out Window_Type; Value : in Integer) is
begin
Window.Property ("outerWidth", Value);
end Outer_Width;
-----------------
-- Outer_Width --
-----------------
function Outer_Width (Window : Window_Type) return Integer is
begin
return Window.Property ("outerWidth");
end Outer_Width;
--------------
-- X_Offset --
--------------
function X_Offset (Window : Window_Type) return Integer is
begin
return Window.Property ("pageXOffset");
end X_Offset;
--------------
-- Y_Offset --
--------------
function Y_Offset (Window : Window_Type) return Integer is
begin
return Window.Property ("pageYOffset");
end Y_Offset;
---------
-- Top --
---------
function Top (Window : Window_Type) return Integer is
begin
return Window.Property ("screenY");
end Top;
----------
-- Left --
----------
function Left (Window : Window_Type) return Integer is
begin
return Window.Property ("screenX");
end Left;
--------------------
-- Form_Parameter --
--------------------
function Form_Parameter (Window : Window_Type; Name : String)
return String
is
begin
return Gnoga.Server.Connection.Form_Parameter (Window.Connection_ID,
Name);
end Form_Parameter;
----------------------
-- Gnoga_Session_ID --
----------------------
function Gnoga_Session_ID (Window : Window_Type; Name : String := "gid")
return String
is
use Gnoga.Client_Storage;
function Generate_Session_ID return String;
-- Create a new unique string to identify sessions
function Generate_Session_ID return String is
use Ada.Strings.Fixed;
use Ada.Strings;
use Ada.Calendar.Formatting;
Now : constant Ada.Calendar.Time := Ada.Calendar.Clock;
begin
return Trim (Year (Now)'Img, Side => Both)
& Trim (Month (Now)'Img, Side => Both)
& Trim (Day (Now)'Img, Side => Both)
& Trim (Hour (Now)'Img, Side => Both)
& Trim (Minute (Now)'Img, Side => Both)
& Trim (Second (Now)'Img, Side => Both)
& Translate (Trim (Sub_Second (Now)'Img, Side => Both),
Ada.Strings.Maps.To_Mapping (".", "0"));
end Generate_Session_ID;
S : Session_Storage_Type := Session_Storage (Window);
Gid : constant String := S.Get (Name);
begin
if Gid = "null" then
declare
New_Session : constant String := Generate_Session_ID;
begin
S.Set (Name, New_Session);
return New_Session;
end;
else
return Gid;
end if;
end Gnoga_Session_ID;
---------------------
-- Connection_Data --
---------------------
procedure Connection_Data
(Window : in out Window_Type;
Data : access Gnoga.Connection_Data_Type'Class;
Dynamic : in Boolean := True)
is
begin
if Data = null then
Gnoga.Server.Connection.Connection_Data
(ID => Window.Connection_ID, Data => null);
Window.Free_Connection_Data := False;
else
Gnoga.Server.Connection.Connection_Data
(ID => Window.Connection_ID, Data => Data.all'Unrestricted_Access);
Window.Free_Connection_Data := Dynamic;
end if;
end Connection_Data;
------------
-- Launch --
------------
procedure Launch (Window : in out Window_Type;
Parent : in out Window_Type'Class;
URL : in String;
Width : in Integer := -1;
Height : in Integer := -1;
Left : in Integer := -1;
Top : in Integer := -1;
Location : in Boolean := False;
Menu : in Boolean := False;
Status : in Boolean := False;
Tool_Bar : in Boolean := False;
Title : in Boolean := False)
is
GID : constant String := Gnoga.Server.Connection.New_GID;
function Params return String;
function Params return String is
use Ada.Strings.Unbounded;
P : Unbounded_String;
C : Boolean := False;
procedure Add_Param (S : String; V : String);
procedure Add_Param (S : String; V : Boolean);
procedure Add_Param (S : String; V : String) is
begin
if C then
P := P & ", ";
end if;
P := P & To_Unbounded_String (S) & "=" & To_Unbounded_String (V);
C := True;
end Add_Param;
procedure Add_Param (S : String; V : Boolean) is
begin
if V then
Add_Param (S, "yes");
else
Add_Param (S, "no");
end if;
end Add_Param;
begin
if Width > -1 then
Add_Param ("width", Width'Img);
end if;
if Height > -1 then
Add_Param ("height", Height'Img);
end if;
if Top > -1 then
Add_Param ("top", Top'Img);
end if;
if Left > -1 then
Add_Param ("left", Left'Img);
end if;
Add_Param ("menubar", Menu);
Add_Param ("status", Status);
Add_Param ("toolbar", Tool_Bar);
Add_Param ("menubar", Menu);
Add_Param ("titlebar", Title);
Add_Param ("location", Location);
return To_String (P);
end Params;
begin
Window.Create_With_Script
(Connection_ID => Parent.Connection_ID,
ID => GID,
Script => "gnoga['" & GID & "']=window.open ('" & URL &
"', '" & GID & "', '" & Params & "')",
ID_Type => Gnoga.Gnoga_ID);
delay 0.25;
-- Needed for Firefox or there is a race condition with the value of
-- gnoga['" & GID & "']. Popups are nasty, they should be avoided.
if
Gnoga.Server.Connection.Execute_Script
(Parent.Connection_ID, "gnoga['" & GID & "']") = "undefined"
then
raise Popup_Blocked;
end if;
Window.DOM_Document.Attach
(Parent.Connection_ID, GID, Gnoga.Gnoga_ID);
Window.Location.Attach
(Connection_ID => Parent.Connection_ID,
ID => Window.jQuery & ".prop ('location')",
ID_Type => Gnoga.Script);
end Launch;
-----------
-- Alert --
-----------
procedure Alert (Window : in out Window_Type; Message : String) is
begin
Window.Execute ("alert ('" & Escape_Quotes (Message) & "');");
end Alert;
---------
-- Log --
---------
procedure Log (Window : in out Window_Type; Message : String) is
begin
Window.Execute ("console.log ('" & Escape_Quotes (Message) & "');");
end Log;
-----------
-- Error --
-----------
procedure Error (Window : in out Window_Type; Message : String) is
begin
Window.Execute ("console.error ('" & Escape_Quotes (Message) & "');");
end Error;
-----------
-- Close --
-----------
procedure Close (Window : in out Window_Type) is
begin
Window.Execute ("close();");
end Close;
-----------
-- Print --
-----------
procedure Print (Window : in out Window_Type) is
begin
Window.Execute ("print();");
end Print;
---------------
-- Resize_By --
---------------
procedure Resize_By
(Window : in out Window_Type;
Width, Height : Integer)
is
begin
Window.Execute ("resizeBy(" & Width'Img & "," & Height'Img & ");");
end Resize_By;
---------------
-- Resize_To --
---------------
procedure Resize_To
(Window : in out Window_Type;
Width, Height : Integer)
is
begin
Window.Execute ("resizeTo(" & Width'Img & "," & Height'Img & ");");
end Resize_To;
-------------
-- Move_By --
-------------
procedure Move_By (Window : in out Window_Type; X, Y : Integer) is
begin
Window.Execute ("moveBy(" & X'Img & "," & Y'Img & ");");
end Move_By;
-------------
-- Move_To --
-------------
procedure Move_To (Window : in out Window_Type; X, Y : Integer) is
begin
Window.Execute ("moveTo(" & X'Img & "," & Y'Img & ");");
end Move_To;
---------------
-- Scroll_By --
---------------
procedure Scroll_By (Window : in out Window_Type; X, Y : Integer) is
begin
Window.Execute ("scrollBy(" & X'Img & "," & Y'Img & ");");
end Scroll_By;
---------------
-- Scroll_To --
---------------
procedure Scroll_To (Window : in out Window_Type; X, Y : Integer) is
begin
Window.Execute ("scrollTo(" & X'Img & "," & Y'Img & ");");
end Scroll_To;
----------------------
-- Close_Connection --
----------------------
procedure Close_Connection (Window : in out Window_Type) is
begin
Gnoga.Server.Connection.Close (Window.Connection_ID);
end Close_Connection;
---------------
-- On_Resize --
---------------
procedure On_Resize (Window : in out Window_Type) is
use Gnoga.Gui.Element;
begin
if Window.View /= null then
Element_Access (Window.View).Box_Height (Window.Height);
Element_Access (Window.View).Box_Width (Window.Width);
end if;
end On_Resize;
--------------------
-- On_Child_Added --
--------------------
overriding
procedure On_Child_Added (Object : in out Window_Type;
Child : in out Gnoga.Gui.Base_Type'Class)
is
begin
if Child in Gnoga.Gui.View.View_Base_Type'Class and Object.Auto_Set_View
then
if Gnoga.Gui.View.View_Base_Type (Child).Auto_Place then
Object.Set_View (Child);
end if;
end if;
end On_Child_Added;
end Ada_GUI.Gnoga.Gui.Window;