-
Notifications
You must be signed in to change notification settings - Fork 5
/
lea_gwin-embedded_texts.adb
132 lines (119 loc) · 4.44 KB
/
lea_gwin-embedded_texts.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
with LEA_Common.Syntax;
with LEA_GWin.MDI_Child;
with HAT;
with Zip, UnZip.Streams, Zip_Streams;
with GWindows.Base,
GWindows.Message_Boxes;
with Ada.Command_Line;
package body LEA_GWin.Embedded_Texts is
procedure Show_Embedded
(Main_Window : in out MDI_Main.MDI_Main_Type;
File_Name : String;
Short_Name : String;
Is_Help : Boolean)
is
use LEA_Common, LEA_Common.Syntax, LEA_GWin.MDI_Child,
HAT,
GWindows.Message_Boxes, Zip_Streams;
lea_exe : constant String := Ada.Command_Line.Command_Name;
zi : Zip.Zip_Info;
mem_stream_unpacked : aliased Memory_Zipstream;
unpacked : HAT.VString;
already_open : Boolean := False;
New_ID : constant ID_Type :=
(file_name => Null_GString_Unbounded, -- No file until first "Save".
short_name => G2GU (S2G (Short_Name)));
--
procedure Check_Duplicate_Embedded_Doc
(Any_Window : GWindows.Base.Pointer_To_Base_Window_Class)
is
begin
if already_open then
-- Duplicate already found.
return;
end if;
if Any_Window.all in MDI_Child_Type'Class then
declare
one_child : MDI_Child_Type renames MDI_Child_Type (Any_Window.all);
begin
if Is_Help then
already_open := one_child.editor.document_kind = help_main;
else
already_open := Equivalent (one_child.ID, New_ID);
end if;
if already_open then
one_child.Focus;
end if;
end;
end if;
end Check_Duplicate_Embedded_Doc;
New_Window : MDI_Child_Access;
procedure Err_Msg (reason : GString) is
begin
Message_Box
(Main_Window,
"Embedded file",
"Could not unpack file from " & S2G (lea_exe) & NL & NL & "Reason: " & reason);
end Err_Msg;
begin
-- We want only one copy of the help of any sample document displayed.
GWindows.Base.Enumerate_Children
(Main_Window.MDI_Client_Window.all,
Check_Duplicate_Embedded_Doc'Unrestricted_Access);
if already_open then
return;
end if;
Zip.Load (zi, lea_exe);
UnZip.Streams.Extract (mem_stream_unpacked, zi, File_Name);
Get (mem_stream_unpacked, unpacked);
if not Is_Help then
unpacked :=
"-- **************************************************************" & ASCII.LF &
"-- ** This sample is not saved anywhere on your file system. **" & ASCII.LF &
"-- ** Don't forget to save it, especially if you modify it! **" & ASCII.LF &
"-- **************************************************************" & ASCII.LF &
ASCII.LF &
unpacked;
end if;
declare
unpacked_str : constant String := HAT.To_String (unpacked); -- visible to dbg
begin
New_Window := new MDI_Child_Type;
if Is_Help then
New_Window.editor.document_kind := help_main;
else
Main_Window.Close_Initial_Document;
end if;
Main_Window.User_maximize_restore := False;
New_Window.Create_LEA_MDI_Child (Main_Window, New_ID);
New_Window.editor.Load_Text (contents => unpacked_str);
if Is_Help then
New_Window.editor.Set_Read_Only (True);
else
New_Window.editor.syntax_kind := Ada_syntax;
New_Window.editor.Set_Scintilla_Syntax;
end if;
end;
-- This is just to set the MRUs in the new window's menu:
Main_Window.Update_Common_Menus;
--
New_Window.Finish_subwindow_opening;
New_Window.editor.Focus;
exception
when Zip.Archive_corrupted =>
Err_Msg ("embedded archive is damaged or absent.");
when Zip.Entry_name_not_found =>
-- This happens when LEA's maintainer forgot to run `sample_catalogue.exe`,
-- then replace `_lea_data.zip` with `_lea_data_new.zip`.
Err_Msg ("resource """ & S2G (File_Name) & """ is missing in embedded archive.");
end Show_Embedded;
procedure Show_Help (Main_Window : in out MDI_Main.MDI_Main_Type) is
begin
Show_Embedded (Main_Window, "lea_help.txt", "Help", Is_Help => True);
end Show_Help;
procedure Show_Sample (Main_Window : in out MDI_Main.MDI_Main_Type; Dir, Sample_Name : String) is
begin
Show_Embedded
(Main_Window, "hac_samples/" & Dir & '/' & Sample_Name, Sample_Name, Is_Help => False);
end Show_Sample;
end LEA_GWin.Embedded_Texts;