From ba1cef419eb30acf7fda6754ba9e682cdea28341 Mon Sep 17 00:00:00 2001 From: Jed Barber Date: Mon, 19 Sep 2016 20:48:56 +1000 Subject: Restructured the save/save as functions a bit --- src/adapad.adb | 148 ++++++++++++++++++++++++++++++--------------------------- 1 file changed, 79 insertions(+), 69 deletions(-) diff --git a/src/adapad.adb b/src/adapad.adb index f7561dc..413306f 100644 --- a/src/adapad.adb +++ b/src/adapad.adb @@ -33,7 +33,9 @@ function AdaPad return Integer is procedure Set_Title (Editor : access Editor_Window); - function Cancel_Save_Discard return Choice; + 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); @@ -49,42 +51,6 @@ function AdaPad return Integer is - type Save_As_Callback is new Editor_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 - - New_Filename : String := File_Chooser - ("Save File As?", "*", To_String (Filename)); - - begin - if New_Filename /= "" then - Save_File (This.Buffer, New_Filename); - end if; - end Call; - - - - - type Save_Callback is new Editor_Callback with null record; - Save_CB : aliased Save_Callback; - - overriding procedure Call - (This : in Save_Callback; - Item : in out Widget'Class) is - begin - if Filename = "" then - Save_As_CB.Call (Item); - else - Save_File (This.Buffer, To_String (Filename)); - end if; - end Call; - - - - type New_Callback is new Editor_Callback with null record; New_CB : aliased New_Callback; @@ -92,13 +58,7 @@ function AdaPad return Integer is (This : in New_Callback; Item : in out Widget'Class) is begin - case Cancel_Save_Discard is - when First => return; - when Second => - Save_CB.Call (Item); - if Changed then return; end if; - when Third => null; - end case; + if not Safe_To_Discard (This.Buffer) then return; end if; Filename := To_Unbounded_String (0); This.Buffer.Set_Selection (0, This.Buffer.Length); This.Buffer.Remove_Selected_Text; @@ -116,13 +76,7 @@ function AdaPad return Integer is (This : in Open_Callback; Item : in out Widget'Class) is begin - case Cancel_Save_Discard is - when First => return; - when Second => - Save_CB.Call (Item); - if Changed then return; end if; - when Third => null; - end case; + if not Safe_To_Discard (This.Buffer) then return; end if; declare New_Filename : String := File_Chooser ("Open File?", "*", To_String (Filename)); begin @@ -135,6 +89,32 @@ function AdaPad return Integer is + type Save_Callback is new Editor_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); + end Call; + + + + + type Save_As_Callback is new Editor_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); + end Call; + + + + type Quit_Callback is new Editor_Callback with null record; Quit_CB : aliased Quit_Callback; @@ -142,13 +122,7 @@ function AdaPad return Integer is (This : in Quit_Callback; Item : in out Widget'Class) is begin - case Cancel_Save_Discard is - when First => return; - when Second => - Save_CB.Call (Item); - if Changed then return; end if; - when Third => null; - end case; + if not Safe_To_Discard (This.Buffer) then return; end if; This.Editor.Hide; end Call; @@ -290,31 +264,67 @@ function AdaPad return Integer is 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 Cancel_Save_Discard - return Choice is + function Safe_To_Discard + (Buffer : access Text_Buffer) + return Boolean is + + User_Response : Choice; + begin - if not Changed then - return Third; + if not Changed then return True; end if; + User_Response := 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 First => + return False; + when Second => + Do_Save (Buffer); + return not Changed; + when Third => + return True; + end case; + end Safe_To_Discard; + + + + + procedure Do_Save + (Buffer : access Text_Buffer) is + begin + if Filename = "" then + Do_Save_As (Buffer); else - return Three_Way_Choice - ("The current file has not been saved." & Character'Val (10) & - "Would you like to save it now?", - "Cancel", "Save", "Discard"); + Save_File (Buffer, To_String (Filename)); + end if; + end Do_Save; + + + + + procedure Do_Save_As + (Buffer : access Text_Buffer) is + + New_Filename : String := File_Chooser + ("Save File As?", "*", To_String (Filename)); + + begin + if New_Filename /= "" then + Save_File (Buffer, New_Filename); end if; - end Cancel_Save_Discard; + end Do_Save_As; -- cgit