Browse Source

Check for unsaved changes before closing a JSResourceEditor

rsredsq 10 years ago
parent
commit
9d7ea2fc76

+ 1 - 6
Script/AtomicEditor/ui/Shortcuts.ts

@@ -49,12 +49,7 @@ class Shortcuts extends Atomic.ScriptObject {
     }
 
     invokeFileClose() {
-
-        // pretty gross
-        var editor = EditorUI.getMainFrame().resourceframe.currentResourceEditor;
-        if (!editor) return;
-        editor.close(true);
-
+        this.invokeResourceFrameShortcut("close");
     }
 
     invokeFileSave() {

+ 1 - 38
Source/AtomicEditor/Editors/JSResourceEditor.cpp

@@ -38,7 +38,6 @@ JSResourceEditor ::JSResourceEditor(Context* context, const String &fullpath, UI
     autocomplete_(0),
     textDirty_(true),
     textDelta_(0.0f),
-    modified_(false),
     currentFindPos_(-1)
 {
 
@@ -270,19 +269,7 @@ bool JSResourceEditor::OnEvent(const TBWidgetEvent &ev)
     {
         if (ev.ref_id == TBIDC("close"))
         {
-            if (modified_)
-            {
-                TBMessageWindow *msg_win = new TBMessageWindow(container_->GetInternalWidget(), TBIDC("unsaved_jsmodifications_dialog"));
-                TBMessageWindowSettings settings(TB_MSG_OK_CANCEL, TBID(uint32(0)));
-                settings.dimmer = true;
-                settings.styling = true;
-                msg_win->Show("Unsaved Modifications", "There are unsaved modications.\nDo you wish to discard them and close?", &settings, 640, 360);
-            }
-            else
-            {
-                Close();
-            }
-
+            RequestClose();
         }
 
         if (ev.ref_id == TBIDC("find"))
@@ -318,24 +305,6 @@ bool JSResourceEditor::OnEvent(const TBWidgetEvent &ev)
         }
     }
 
-    if (ev.type == EVENT_TYPE_CLICK)
-    {
-        if (ev.target->GetID() == TBIDC("unsaved_jsmodifications_dialog"))
-        {
-            if (ev.ref_id == TBIDC("TBMessageWindow.ok"))
-            {
-                Close();
-            }
-            else
-            {
-                SetFocus();
-            }
-
-            return true;
-        }
-
-    }
-
     return false;
 }
 
@@ -525,12 +494,6 @@ void JSResourceEditor::GotoLineNumber(int lineNumber)
 
 }
 
-
-bool JSResourceEditor::HasUnsavedModifications()
-{
-    return modified_;
-}
-
 bool JSResourceEditor::ParseJavascriptToJSON(const char* source, String& json, bool loose)
 {
 

+ 1 - 5
Source/AtomicEditor/Editors/JSResourceEditor.h

@@ -18,7 +18,7 @@ using namespace tb;
 namespace AtomicEditor
 {
 
-class JSAutocomplete;
+class JSAutocomplete; 
 
 class JSResourceEditor: public ResourceEditor, public TBStyleEditTextChangeListener
 {
@@ -45,8 +45,6 @@ public:
 
     void SetFocus();
 
-    bool HasUnsavedModifications();
-
     bool Save();
 
 private:
@@ -64,8 +62,6 @@ private:
     float textDelta_;
     bool textDirty_;
 
-    bool modified_;
-
     int currentFindPos_;
 
 };

+ 44 - 5
Source/AtomicEditor/Editors/ResourceEditor.cpp

@@ -10,6 +10,8 @@
 #include <Atomic/IO/FileSystem.h>
 #include <Atomic/Resource/ResourceEvents.h>
 
+#include <TurboBadger/tb_message_window.h>
+
 #include "ResourceEditor.h"
 
 //#include "../UI/UIMainFrame.h"
@@ -32,17 +34,49 @@ public:
         button_->SetValue(value);
     }
 
+    bool RequestClose()
+    {
+        if (editor_->HasUnsavedModifications())
+        {
+            TBMessageWindow *msg_win = new TBMessageWindow(this, TBIDC("unsaved_modifications_dialog"));
+            TBMessageWindowSettings settings(TB_MSG_OK_CANCEL, TBID(uint32(0)));
+            settings.dimmer = true;
+            settings.styling = true;
+            msg_win->Show("Unsaved Modifications", "There are unsaved modications.\nDo you wish to discard them and close?", &settings, 640, 360);
+        }
+        else
+        {
+            editor_->Close();
+            return false;
+        }
+    }
+
     bool OnEvent(const TBWidgetEvent &ev)
     {
         if (ev.type == EVENT_TYPE_CLICK || ev.type == EVENT_TYPE_POINTER_DOWN)
         {
-            if (ev.target->GetID() == TBIDC("tabclose"))
+            if (ev.target->GetID() == TBIDC("unsaved_modifications_dialog"))
             {
-                container_->OnEvent(ev);
-                editor_->Close();
+                if (ev.ref_id == TBIDC("TBMessageWindow.ok"))
+                {
+                    container_->OnEvent(ev);
+                    editor_->Close();
+                }
+                else
+                {
+                    SetFocus(WIDGET_FOCUS_REASON_UNKNOWN);
+                }
                 return true;
             }
-            else
+            if (ev.target->GetID() == TBIDC("tabclose"))
+            {
+                if (RequestClose())
+                {
+                    container_->OnEvent(ev);
+                    return true;
+                }
+            }
+            else 
             {
                 TBWidgetEvent nevent = ev;
                 nevent.target = this;
@@ -56,7 +90,7 @@ public:
 
 ResourceEditor::ResourceEditor(Context* context, const String& fullpath, UITabContainer *container):
     Object(context), fullpath_(fullpath), container_(container),
-    editorTabLayout_(0), rootContentWidget_(0), button_(0)
+    editorTabLayout_(0), rootContentWidget_(0), button_(0), modified_(false)
 {
 
     String filename = GetFileNameAndExtension(fullpath_);
@@ -112,6 +146,11 @@ void ResourceEditor::HandleFileChanged(StringHash eventType, VariantMap& eventDa
     */
 }
 
+void ResourceEditor::RequestClose()
+{
+    editorTabLayout_->RequestClose();
+}
+
 void ResourceEditor::Close(bool navigateToAvailableResource)
 {
     // keep us alive through the close

+ 5 - 1
Source/AtomicEditor/Editors/ResourceEditor.h

@@ -32,7 +32,7 @@ public:
 
     UIButton* GetButton() { return button_; }
 
-    virtual bool HasUnsavedModifications() { return false; }
+    virtual bool HasUnsavedModifications() { return modified_; }
 
     virtual void SetFocus() {}
     virtual void Close(bool navigateToAvailableResource = true);
@@ -52,10 +52,14 @@ public:
 
     void InvokeShortcut(const String& shortcut);
 
+    void RequestClose();
+
 protected:
 
     String fullpath_;
 
+    bool modified_;
+
     EditorTabLayout* editorTabLayout_;
 
     SharedPtr<UITabContainer> container_;