From 8cb506a7f6ba79983b1327e29233f4fb25a4a694 Mon Sep 17 00:00:00 2001 From: Jed Barber Date: Tue, 20 Sep 2016 00:31:44 +1000 Subject: Removed some unnecessary complexity for a text editor --- src/adapad.adb | 143 +++++++++++++++++++++++++-------------------------------- 1 file changed, 62 insertions(+), 81 deletions(-) diff --git a/src/adapad.adb b/src/adapad.adb index f3c2d36..48ca844 100644 --- a/src/adapad.adb +++ b/src/adapad.adb @@ -21,67 +21,62 @@ with Ada.Text_IO; function AdaPad return Integer is - Pad : aliased Editor_Window := Create (0, 0, 640, 400, "(Untitled)"); - Buff : aliased Text_Buffer := Create; - - -- these globals make me feel dirty - -- like they should be in the buffer class or something - Changed : Boolean := False; - Filename : Unbounded_String := To_Unbounded_String (0); + -- forward declarations of helper functions + procedure Set_Title; + function Safe_To_Discard return Boolean; + procedure Do_Save; + procedure Do_Save_As; + procedure Load_File (Name : in String); + procedure Save_File (Name : in String); - procedure Set_Title (Editor : access Editor_Window); - function Safe_To_Discard (Buffer : access Text_Buffer) return Boolean; - procedure Do_Save (Buffer : access Text_Buffer); - procedure Do_Save_As (Buffer : access Text_Buffer); - procedure Load_File (Buffer : access Text_Buffer; Name : in String); - procedure Save_File (Buffer : access Text_Buffer; Name : in String); + -- global state of the text editor + Editor : aliased Editor_Window := Create (0, 0, 640, 400, "(Untitled)"); + Buffer : aliased Text_Buffer := Create; + Changed : Boolean := False; + Filename : Unbounded_String := To_Unbounded_String (0); - type Editor_Callback is abstract new Widget_Callback with - record - Editor : access Editor_Window := Pad'Access; - Buffer : access Text_Buffer := Buff'Access; - end record; + -- callbacks for the menu - type New_Callback is new Editor_Callback with null record; + type New_Callback is new Widget_Callback with null record; New_CB : aliased New_Callback; overriding procedure Call (This : in New_Callback; Item : in out Widget'Class) is begin - if not Safe_To_Discard (This.Buffer) then return; end if; + if not Safe_To_Discard then return; end if; Filename := To_Unbounded_String (0); - This.Buffer.Set_Selection (0, This.Buffer.Length); - This.Buffer.Remove_Selected_Text; + Buffer.Set_Selection (0, Buffer.Length); + Buffer.Remove_Selected_Text; Changed := False; - This.Buffer.Call_Modify_Callbacks; + Buffer.Call_Modify_Callbacks; end Call; - type Open_Callback is new Editor_Callback with null record; + type Open_Callback is new Widget_Callback with null record; Open_CB : aliased Open_Callback; overriding procedure Call (This : in Open_Callback; Item : in out Widget'Class) is begin - if not Safe_To_Discard (This.Buffer) then return; end if; + if not Safe_To_Discard then return; end if; declare New_Filename : String := File_Chooser ("Open File?", "*", To_String (Filename)); begin if New_Filename /= "" then - Load_File (This.Buffer, New_Filename); + Load_File (New_Filename); end if; end; end Call; @@ -89,125 +84,125 @@ function AdaPad return Integer is - type Save_Callback is new Editor_Callback with null record; + type Save_Callback is new Widget_Callback with null record; Save_CB : aliased Save_Callback; overriding procedure Call (This : in Save_Callback; Item : in out Widget'Class) is begin - Do_Save (This.Buffer); + Do_Save; end Call; - type Save_As_Callback is new Editor_Callback with null record; + type Save_As_Callback is new Widget_Callback with null record; Save_As_CB : aliased Save_As_Callback; overriding procedure Call (This : in Save_As_Callback; Item : in out Widget'Class) is begin - Do_Save_As (This.Buffer); + Do_Save_As; end Call; - type Quit_Callback is new Editor_Callback with null record; + type Quit_Callback is new Widget_Callback with null record; Quit_CB : aliased Quit_Callback; overriding procedure Call (This : in Quit_Callback; Item : in out Widget'Class) is begin - if not Safe_To_Discard (This.Buffer) then return; end if; - This.Editor.Hide; + if not Safe_To_Discard then return; end if; + Editor.Hide; end Call; - type Undo_Callback is new Editor_Callback with null record; + type Undo_Callback is new Widget_Callback with null record; Undo_CB : aliased Undo_Callback; overriding procedure Call (This : in Undo_Callback; Item : in out Widget'Class) is begin - This.Editor.Undo; + Editor.Undo; end Call; - type Cut_Callback is new Editor_Callback with null record; + type Cut_Callback is new Widget_Callback with null record; Cut_CB : aliased Cut_Callback; overriding procedure Call (This : in Cut_Callback; Item : in out Widget'Class) is begin - This.Editor.Cut; + Editor.Cut; end Call; - type Copy_Callback is new Editor_Callback with null record; + type Copy_Callback is new Widget_Callback with null record; Copy_CB : aliased Copy_Callback; overriding procedure Call (This : in Copy_Callback; Item : in out Widget'Class) is begin - This.Editor.Copy; + Editor.Copy; end Call; - type Paste_Callback is new Editor_Callback with null record; + type Paste_Callback is new Widget_Callback with null record; Paste_CB : aliased Paste_Callback; overriding procedure Call (This : in Paste_Callback; Item : in out Widget'Class) is begin - This.Editor.Paste; + Editor.Paste; end Call; - type Delete_Callback is new Editor_Callback with null record; + type Delete_Callback is new Widget_Callback with null record; Delete_CB : aliased Delete_Callback; overriding procedure Call (This : in Delete_Callback; Item : in out Widget'Class) is begin - This.Editor.Delete; + Editor.Delete; end Call; - type Select_All_Callback is new Editor_Callback with null record; + type Select_All_Callback is new Widget_Callback with null record; Select_All_CB : aliased Select_All_Callback; overriding procedure Call (This : in Select_All_Callback; Item : in out Widget'Class) is begin - This.Buffer.Set_Selection (0, This.Buffer.Length); + Buffer.Set_Selection (0, Buffer.Length); end Call; - type Find_Callback is new Editor_Callback with null record; + type Find_Callback is new Widget_Callback with null record; Find_CB : aliased Find_Callback; overriding procedure Call @@ -220,7 +215,7 @@ function AdaPad return Integer is - type Replace_Callback is new Editor_Callback with null record; + type Replace_Callback is new Widget_Callback with null record; Replace_CB : aliased Replace_Callback; overriding procedure Call @@ -233,7 +228,7 @@ function AdaPad return Integer is - type About_Callback is new Editor_Callback with null record; + type About_Callback is new Widget_Callback with null record; About_CB : aliased About_Callback; overriding procedure Call @@ -246,10 +241,9 @@ function AdaPad return Integer is - type Mod_Callback is new Modify_Callback with - record - Editor : access Editor_Window := Pad'Access; - end record; + -- callbacks for the text buffer + + type Mod_Callback is new Modify_Callback with null record; Mod_CB : aliased Mod_Callback; overriding procedure Call @@ -262,17 +256,16 @@ function AdaPad return Integer is if Action = Insert or Action = Delete then Changed := True; end if; - Set_Title (This.Editor); + Set_Title; end Call; - procedure Set_Title - (Editor : access Editor_Window) is + -- helper functions + procedure Set_Title is Title : Unbounded_String := To_Unbounded_String (0); - begin if Changed then Append (Title, "*"); @@ -288,12 +281,8 @@ function AdaPad return Integer is - function Safe_To_Discard - (Buffer : access Text_Buffer) - return Boolean is - + function Safe_To_Discard return Boolean is User_Response : Choice; - begin if not Changed then return True; end if; User_Response := Three_Way_Choice @@ -304,7 +293,7 @@ function AdaPad return Integer is when First => return False; when Second => - Do_Save (Buffer); + Do_Save; return not Changed; when Third => return True; @@ -314,37 +303,31 @@ function AdaPad return Integer is - procedure Do_Save - (Buffer : access Text_Buffer) is + procedure Do_Save is begin if Filename = "" then - Do_Save_As (Buffer); + Do_Save_As; else - Save_File (Buffer, To_String (Filename)); + Save_File (To_String (Filename)); end if; end Do_Save; - procedure Do_Save_As - (Buffer : access Text_Buffer) is - + procedure Do_Save_As is New_Filename : String := File_Chooser ("Save File As?", "*", To_String (Filename)); - begin if New_Filename /= "" then - Save_File (Buffer, New_Filename); + Save_File (New_Filename); end if; end Do_Save_As; - procedure Load_File - (Buffer : access Text_Buffer; - Name : in String) is + procedure Load_File (Name : in String) is begin Buffer.Load_File (Name); Filename := To_Unbounded_String (Name); @@ -358,9 +341,7 @@ function AdaPad return Integer is - procedure Save_File - (Buffer : access Text_Buffer; - Name : in String) is + procedure Save_File (Name : in String) is begin Buffer.Save_File (Name); Filename := To_Unbounded_String (Name); @@ -376,7 +357,7 @@ begin declare - Bar : Menu_Cursor := Pad.Get_Menu; + Bar : Menu_Cursor := Editor.Get_Menu; begin Bar.Add (Text => "&File", Flags => Flag_Submenu); Bar.Add ("File/&New", New_CB'Access, Mod_Ctrl + 'n'); @@ -402,9 +383,9 @@ begin end; - Buff.Add_Modify_Callback (Mod_CB'Access); - Pad.Set_Buffer (Buff); - Pad.Show; + Buffer.Add_Modify_Callback (Mod_CB'Access); + Editor.Set_Buffer (Buffer); + Editor.Show; return FLTK.Run; -- cgit