summaryrefslogtreecommitdiff
path: root/src/adapad.adb
blob: a28494bf6cf6e761590b2590c50fbaa2c517e7c2 (plain)
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


with FLTK;
with FLTK.Widgets;
with FLTK.Widgets.Menus;
with FLTK.Widgets.Groups.Windows;
with FLTK.Text_Buffers;
with FLTK.Popups;
with Windows.Editor;
with Windows.About;
with Windows.Find;
with Windows.Replace;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;


package body Adapad is


    package W  renames FLTK.Widgets;
    package WN renames FLTK.Widgets.Groups.Windows;
    package D  renames FLTK.Popups;




    --  global state of the text editor

    Editor   : Windows.Editor.Editor_Window   := Windows.Editor.Create (800, 500);
    Buffer   : FLTK.Text_Buffers.Text_Buffer  := FLTK.Text_Buffers.Create;
    About    : Windows.About.About_Window     := Windows.About.Create;
    Find     : Windows.Find.Find_Window       := Windows.Find.Create;
    Replace  : Windows.Replace.Replace_Window := Windows.Replace.Create;

    Changed  : Boolean          := False;
    Filename : Unbounded_String := To_Unbounded_String (0);




    --  main program interface

    procedure Show is
    begin
        Editor.Show;
    end Show;




    procedure Hide is
    begin
        About.Hide;
        Find.Hide;
        Replace.Hide;
        Editor.Hide;
    end Hide;




    --  callbacks for the menu

    overriding procedure Call
           (This : in     New_Callback;
            Item : in out W.Widget'Class) is
    begin
        if not Safe_To_Discard then return; end if;
        Filename := To_Unbounded_String (0);
        Buffer.Set_Selection (0, Buffer.Length);
        Buffer.Remove_Selected_Text;
        Changed := False;
        Buffer.Call_Modify_Callbacks;
    end Call;




    overriding procedure Call
           (This : in     Open_Callback;
            Item : in out W.Widget'Class) is
    begin
        if not Safe_To_Discard then return; end if;
        declare
            New_Filename : String := D.File_Chooser ("Open File?", "*", To_String (Filename));
        begin
            if New_Filename /= "" then
                Load_File (New_Filename);
            end if;
        end;
    end Call;




    overriding procedure Call
           (This : in     Save_Callback;
            Item : in out W.Widget'Class) is
    begin
        Do_Save;
    end Call;




    overriding procedure Call
           (This : in     Save_As_Callback;
            Item : in out W.Widget'Class) is
    begin
        Do_Save_As;
    end Call;




    overriding procedure Call
           (This : in     Quit_Callback;
            Item : in out W.Widget'Class) is
    begin
        if not Safe_To_Discard then return; end if;
        Hide;
    end Call;




    overriding procedure Call
           (This : in     Undo_Callback;
            Item : in out W.Widget'Class) is
    begin
        Editor.Undo;
    end Call;




    overriding procedure Call
           (This : in     Cut_Callback;
            Item : in out W.Widget'Class) is
    begin
        Editor.Cut;
    end Call;




    overriding procedure Call
           (This : in     Copy_Callback;
            Item : in out W.Widget'Class) is
    begin
        Editor.Copy;
    end Call;




    overriding procedure Call
           (This : in     Paste_Callback;
            Item : in out W.Widget'Class) is
    begin
        Editor.Paste;
    end Call;




    overriding procedure Call
           (This : in     Delete_Callback;
            Item : in out W.Widget'Class) is
    begin
        Editor.Delete;
    end Call;




    overriding procedure Call
           (This : in     Select_All_Callback;
            Item : in out W.Widget'Class) is
    begin
        Buffer.Set_Selection (0, Buffer.Length);
    end Call;




    overriding procedure Call
           (This : in     Find_Callback;
            Item : in out W.Widget'Class) is
    begin
        Centre (Find);
        Find.Show;
    end Call;




    overriding procedure Call
           (This : in     Replace_Callback;
            Item : in out W.Widget'Class) is
    begin
        Centre (Replace);
        Replace.Show;
    end Call;




    overriding procedure Call
           (This : in     About_Callback;
            Item : in out W.Widget'Class) is
    begin
        Centre (About);
        About.Show;
    end Call;




    --  callbacks for the text buffer

    overriding procedure Call
           (This         : in Mod_Callback;
            Action       : in FLTK.Text_Buffers.Modification;
            Place        : in FLTK.Text_Buffers.Position;
            Length       : in Natural;
            Deleted_Text : in String)
    is
        use type FLTK.Text_Buffers.Modification;
    begin
        if Action = FLTK.Text_Buffers.Insert or Action = FLTK.Text_Buffers.Delete then
            Changed := True;
        end if;
        Set_Title;
    end Call;




    --  callbacks for the find/replace windows

    overriding procedure Call
           (This       : in Do_Find_Callback;
            Item       : in String;
            Match_Case : in Boolean)
    is
        Current_Position, Found_At : Natural;
    begin
        Find.Hide;
        Current_Position := Editor.Get_Insert_Position;
        if Buffer.Search_Forward (Current_Position, Item, Found_At, Match_Case) then
            Buffer.Set_Selection (Found_At, Found_At + Item'Length);
            Editor.Set_Insert_Position (Found_At + Item'Length);
            Editor.Show_Insert_Position;
        else
            D.Alert ("No occurrences of '" & Item & "' found!");
        end if;
    end Call;




    overriding procedure Call
           (This                    : in Do_Replace_Callback;
            Item, Replace_With      : in String;
            Match_Case, Replace_All : in Boolean)
    is
        Current_Position, Found_At : Natural;
        Times_Replaced : Natural := 0;
    begin
        Replace.Hide;

        if Replace_All then
            Editor.Set_Insert_Position (0);
        end if;

        loop
            Current_Position := Editor.Get_Insert_Position;
            exit when not Buffer.Search_Forward (Current_Position, Item, Found_At, Match_Case);
            Buffer.Set_Selection (Found_At, Found_At + Item'Length);
            Buffer.Remove_Selected_Text;
            Buffer.Insert_Text (Found_At, Replace_With);
            Editor.Set_Insert_Position (Found_At + Replace_With'Length);
            Editor.Show_Insert_Position;
            Times_Replaced := Times_Replaced + 1;
            exit when not Replace_All and Times_Replaced > 0;
        end loop;

        if Times_Replaced > 0 then
            D.Message_Box ("Replaced " & Integer'Image (Times_Replaced) & " occurrences.");
        else
            D.Alert ("No occurrences of '" & Item & "' found!");
        end if;
    end Call;




    --  helper functions

    procedure Set_Title is
        Title : Unbounded_String := To_Unbounded_String (0);
    begin
        if Changed then
            Append (Title, "*");
        end if;
        if Filename = "" then
            Append (Title, "(Untitled)");
        else
            Append (Title, Filename);
        end if;
        Editor.Set_Label (To_String (Title));
    end Set_Title;




    function Safe_To_Discard
        return Boolean
    is
        User_Response : D.Choice;
    begin
        if not Changed then return True; end if;
        User_Response := D.Three_Way_Choice
               ("The current file has not been saved." & Character'Val (10) &
                "Would you like to save it now?",
                "Cancel", "Save", "Discard");
        case User_Response is
            when D.First =>
                return False;
            when D.Second =>
                Do_Save;
                return not Changed;
            when D.Third =>
                return True;
        end case;
    end Safe_To_Discard;




    procedure Do_Save is
    begin
        if Filename = "" then
            Do_Save_As;
        else
            Save_File (To_String (Filename));
        end if;
    end Do_Save;




    procedure Do_Save_As is
        New_Filename : String := D.File_Chooser
               ("Save File As?", "*", To_String (Filename));
    begin
        if New_Filename /= "" then
            Save_File (New_Filename);
        end if;
    end Do_Save_As;




    procedure Load_File
           (Name : in String) is
    begin
        Buffer.Load_File (Name);
        Filename := To_Unbounded_String (Name);
        Changed := False;
        Buffer.Call_Modify_Callbacks;
    exception
        when Storage_Error =>
            D.Alert ("Error reading from file " & Name);
    end Load_File;




    procedure Save_File
           (Name : in String) is
    begin
        Buffer.Save_File (Name);
        Filename := To_Unbounded_String (Name);
        Changed := False;
        Buffer.Call_Modify_Callbacks;
    exception
        when Storage_Error =>
            D.Alert ("Error writing to file " & Name);
    end Save_File;




    procedure Centre
           (Win : in out WN.Window'Class)
    is
        Middle_X : Integer := Editor.Get_X + Editor.Get_W / 2;
        Middle_Y : Integer := Editor.Get_Y + Editor.Get_H / 2;
    begin
        Win.Reposition
               (Middle_X - Win.Get_W / 2,
                Middle_Y - Win.Get_H / 2);
    end Centre;


begin


    declare
        use FLTK.Widgets.Menus;
        Bar : Menu_Cursor := Editor.Get_Menu;
    begin
        Bar.Add (Text => "&File",   Flags => Flag_Submenu);
        Bar.Add ("File/&New",          New_CB'Access,        Mod_Ctrl + 'n');
        Bar.Add ("File/&Open...",      Open_CB'Access,       Mod_Ctrl + 'o');
        Bar.Add ("File/&Save",         Save_CB'Access,       Mod_Ctrl + 's');
        Bar.Add ("File/Save &As...", Save_As_CB'Access, Mod_Shift + Mod_Ctrl + 's', Flag_Divider);
        Bar.Add ("File/&Quit",         Quit_CB'Access,       Mod_Ctrl + 'q');

        Bar.Add (Text => "&Edit",   Flags => Flag_Submenu);
        Bar.Add ("Edit/&Undo",         Undo_CB'Access,       Mod_Ctrl + 'z',    Flag_Divider);
        Bar.Add ("Edit/Cu&t",          Cut_CB'Access,        Mod_Ctrl + 'x');
        Bar.Add ("Edit/&Copy",         Copy_CB'Access,       Mod_Ctrl + 'c');
        Bar.Add ("Edit/&Paste",        Paste_CB'Access,      Mod_Ctrl + 'v');
        Bar.Add ("Edit/&Delete",       Delete_CB'Access,     No_Key,            Flag_Divider);
        Bar.Add ("Edit/Select &All",   Select_All_CB'Access, Mod_Ctrl + 'a');

        Bar.Add (Text => "&Search", Flags => Flag_Submenu);
        Bar.Add ("Search/&Find...",    Find_CB'Access,       Mod_Ctrl + 'f');
        Bar.Add ("Search/&Replace...", Replace_CB'Access,    Mod_Ctrl + 'h');

        Bar.Add (Text => "&Help",   Flags => Flag_Submenu);
        Bar.Add ("Help/&About",        About_CB'Access);
    end;


    Find.Set_Find_Callback (Do_Find_CB'Access);
    Replace.Set_Replace_Callback (Do_Replace_CB'Access);
    Buffer.Add_Modify_Callback (Mod_CB'Access);
    Editor.Set_Callback (Quit_CB'Access);


    Editor.Set_Buffer (Buffer);


end Adapad;