Permalink
Browse files

Completed macro saving

  • Loading branch information...
1 parent 1ef5859 commit 35f2b0f99ed06f29fc6e8837246f74432597ec2d Alexander Stigsen committed Jul 30, 2010
@@ -439,7 +439,7 @@ bool BundleItemEditorCtrl::LoadBundleItem(const wxString& bundleUri) {
case BUNDLE_MACRO:
{
PListArray cmdArray;
- itemDict.GetArray("commands", cmdArray);
+ if (!itemDict.GetArray("e_commands", cmdArray)) itemDict.GetArray("commands", cmdArray);
const wxString jsonSettings = cmdArray.GetJSON();
cxLOCKDOC_WRITE(m_doc)
doc.Insert(0, jsonSettings);
View
@@ -17,6 +17,7 @@
#include "Env.h"
#include "Execute.h"
+#include "Macro.h"
#include "images/tmBundle.xpm"
#include "images/tmCommand.xpm"
@@ -1034,7 +1035,7 @@ bool BundlePane::HasSelection() const {
return selItem.IsOk();
}
-void BundlePane::NewItem(BundleItemType type) {
+void BundlePane::NewItem(BundleItemType type, const eMacro* macro) {
const wxTreeItemId selItem = m_bundleTree->GetSelection();
if (!selItem.IsOk()) return; // Can't add item if no bundle
const BundleItemData* data = (BundleItemData*)m_bundleTree->GetItemData(selItem);
@@ -1055,6 +1056,7 @@ void BundlePane::NewItem(BundleItemType type) {
case BUNDLE_DRAGCMD: name = _("New DragCommand"); break;
case BUNDLE_PREF: name = _("New Preference"); break;
case BUNDLE_LANGUAGE: name = _("New Language"); break;
+ case BUNDLE_MACRO: name = _("New Macro"); break;
default: wxASSERT(false);
}
name = wxGetTextFromUser(_("Name of new item:"), _("New Item"), name, this);
@@ -1086,7 +1088,9 @@ void BundlePane::NewItem(BundleItemType type) {
break;
case BUNDLE_PREF:
case BUNDLE_LANGUAGE:
+ break;
case BUNDLE_MACRO:
+ if (macro) macro->SaveTo(itemDict);
break;
default:
wxASSERT(false);
View
@@ -32,14 +32,15 @@ class EditorFrame;
class ITmLoadBundles;
class wxDragImage;
class BundleItemData;
+class eMacro;
class BundlePane : public wxPanel {
public:
BundlePane(EditorFrame& parent, ITmLoadBundles* syntaxHandler);
void LoadBundles();
bool HasSelection() const;
- void NewItem(BundleItemType type);
+ void NewItem(BundleItemType type, const eMacro* macro=NULL);
private:
class SortTreeCtrl : public wxTreeCtrl {
View
@@ -2316,7 +2316,7 @@ unsigned int EditorCtrl::InsertNewline() {
void EditorCtrl::InsertChar(const wxChar& text) {
if (m_macro.IsRecording()) {
eMacroCmd& cmd = lastaction != ACTION_INSERT || m_macro.IsEmpty() || m_macro.Last().GetName() != wxT("InsertChars")
- ? m_macro.AddWithStrArg(wxT("InsertChars"), wxT("text"), wxT("")) : m_macro.Last();
+ ? m_macro.Add(wxT("InsertChars"), wxT("text"), wxT("")) : m_macro.Last();
cmd.ExtendString(0, text);
}
@@ -5220,7 +5220,7 @@ void EditorCtrl::ShowCompletionPopup(const wxArrayString& completions) {
void EditorCtrl::ReplaceCurrentWord(const wxString& word) {
if (m_macro.IsRecording()) {
- m_macro.AddWithStrArg(wxT("ReplaceCurrentWord"), wxT("text"), word);
+ m_macro.Add(wxT("ReplaceCurrentWord"), wxT("text"), word);
}
const interval iv = GetWordIv(GetPos());
View
@@ -1647,9 +1647,9 @@ void EditorFrame::ShowProjectPane(const wxString& project) {
}
void EditorFrame::SaveMacro() {
- if (!IsBundlePaneShownAndSelected()) return;
-
+ if (!IsBundlePaneShownAndSelected() || m_macro.IsEmpty()) return;
+ m_bundlePane->NewItem(BUNDLE_MACRO, &m_macro);
}
bool EditorFrame::IsBundlePaneShownAndSelected() const {
View
@@ -0,0 +1,30 @@
+#include "Macro.h"
+#include "plistHandler.h"
+
+void eMacroCmd::SaveTo(PListArray& arr) const {
+ PListDict dict = arr.InsertNewDict(arr.GetSize());
+
+ dict.wxSetString("command", m_cmd);
+
+ if (m_argNames.empty()) return;
+ PListArray args = dict.NewArray("arguments");
+ for (size_t i = 0; i < m_argNames.size(); ++i) {
+ const wxString& name = m_argNames[i];
+ const wxVariant& arg = m_args[i];
+ PListArray a = args.InsertNewArray(args.GetSize());
+
+ a.AddString(name);
+ if (arg.GetType() == wxT("bool")) a.AddBool(arg.GetBool());
+ else if (arg.GetType() == wxT("long")) a.AddInt(arg.GetLong());
+ else if (arg.GetType() == wxT("string")) a.AddString(arg.GetString());
+ else wxASSERT(false);
+ }
+}
+
+void eMacro::SaveTo(PListDict& dict) const {
+ PListArray cmdArray = dict.NewArray("e_commands");
+
+ for (boost::ptr_vector<eMacroCmd>::const_iterator p = m_cmds.begin(); p != m_cmds.end(); ++p) {
+ p->SaveTo(cmdArray);
+ }
+}
View
@@ -22,6 +22,10 @@
#include <boost/ptr_container/ptr_vector.hpp>
#include <vector>
+// Pre-declarations
+class PListDict;
+class PListArray;
+
class eMacroArg {
public:
eMacroArg(const wxString& name, bool value) : m_name(name), m_value(value) {};
@@ -77,34 +81,17 @@ class eMacroCmd {
value = str;
}
- void AddArg(bool value) {m_args.push_back(wxVariant(value));};
- void AddArg(int value) {m_args.push_back(wxVariant(value));};
- void AddArg(const wxChar* value) {m_args.push_back(wxVariant(value));};
- void AddArg(const wxString& value) {m_args.push_back(wxVariant(value));};
-
- void AddArg(const wxString& name, bool value) {m_argNames.push_back(name); m_args.push_back(wxVariant(value));};
- void AddArg(const wxString& name, int value) {m_argNames.push_back(name); m_args.push_back(wxVariant(value));};
- void AddArg(const wxString& name, const wxChar* value) {m_argNames.push_back(name); m_args.push_back(wxVariant(value));};
- void AddArg(const wxString& name, const wxString& value) {m_argNames.push_back(name); m_args.push_back(wxVariant(value));};
+ template<class T> void AddArg(T value) {m_args.push_back(wxVariant(value));};
+ template<class T> void AddArg(const wxString& name, T value) {m_argNames.push_back(name); m_args.push_back(wxVariant(value));};
template<class T> void SetArg(size_t ndx, const wxString& name, T value) {
m_argNames.resize(ndx+1);
m_args.resize(ndx+1);
m_argNames[ndx] = name;
m_args[ndx] = value;
};
- /*void SetArg(size_t ndx, const wxString& name, const wxChar* value) {
- m_argNames.resize(ndx+1);
- m_args.resize(ndx+1);
- m_argNames[ndx] = name;
- m_args[ndx] = value;
- };
- void SetArg(size_t ndx, const wxString& name, const wxString& value) {
- m_argNames.resize(ndx+1);
- m_args.resize(ndx+1);
- m_argNames[ndx] = name;
- m_args[ndx] = value;
- };*/
+
+ void SaveTo(PListArray& arr) const;
private:
wxString m_cmd;
@@ -145,22 +132,16 @@ class eMacro {
return m_cmds.back();
};
- eMacroCmd& AddWithStrArg(const wxString& cmd, const wxString& arg, const wxString& value) {
- wxLogDebug(wxT("Adding macro: %s"), cmd);
- m_cmds.push_back(new eMacroCmd(cmd));
- m_cmds.back().AddArg(arg, value);
- m_isModified = true;
- return m_cmds.back();
- };
-
- eMacroCmd& Add(const wxString& cmd, const wxString& arg, bool value) {
+ template<class T> eMacroCmd& Add(const wxString& cmd, const wxString& arg, T value) {
wxLogDebug(wxT("Adding macro: %s"), cmd);
m_cmds.push_back(new eMacroCmd(cmd));
m_cmds.back().AddArg(arg, value);
m_isModified = true;
return m_cmds.back();
};
+ void SaveTo(PListDict& dict) const;
+
private:
bool m_record;
View
@@ -54,6 +54,8 @@ MacroPane::MacroPane(EditorFrame& frame, wxWindow* parent, eMacro& macro)
m_cmdList = new wxListBox(this, ID_CMDLIST);
m_argsGrid = new wxGrid(this, ID_ARG_GRID);
+ m_buttonSave->SetToolTip(_("Save as Bundle Item\n(select in Bundle Editor)"));
+
UpdateButtons();
// Configure grid
@@ -205,7 +207,7 @@ void MacroPane::OnIdle(wxIdleEvent& WXUNUSED(evt)) {
}
}
- const bool canSave = m_parentFrame.IsBundlePaneShownAndSelected();
+ const bool canSave = m_parentFrame.IsBundlePaneShownAndSelected() && !m_macro.IsEmpty();
if (m_buttonSave->IsEnabled() != canSave) m_buttonSave->Enable(canSave);
if (m_macro.IsModified()) {
View
@@ -1638,6 +1638,10 @@
>
</File>
<File
+ RelativePath=".\Macro.cpp"
+ >
+ </File>
+ <File
RelativePath=".\MacroPane.cpp"
>
</File>
View
@@ -2738,6 +2738,44 @@ void PListDict::DeleteItem(const char* key) {
m_vDict.RemoveAt(ndx);
}
+void PListDict::SetBool(const char* key, bool value) {
+ wxASSERT(m_rPlist);
+ wxASSERT(key);
+
+ const int ndx = m_vDict.Find(pKey[key]);
+ if (ndx == -1) {
+ c4_Row rRef;
+ pKey(rRef) = key;
+ pRefType(rRef) = REF_BOOL;
+ pRef(rRef) = value ? 1 : 0;
+ m_vDict.Add(rRef);
+ }
+ else {
+ const c4_RowRef rItem = m_vDict[ndx];
+ wxASSERT(pRefType(rItem) == REF_BOOL);
+ pRef(rItem) = value ? 1 : 0;
+ }
+}
+
+void PListDict::SetInt(const char* key, int value) {
+ wxASSERT(m_rPlist);
+ wxASSERT(key);
+
+ const int ndx = m_vDict.Find(pKey[key]);
+ if (ndx == -1) {
+ c4_Row rRef;
+ pKey(rRef) = key;
+ pRefType(rRef) = REF_INTEGER;
+ pRef(rRef) = value;
+ m_vDict.Add(rRef);
+ }
+ else {
+ const c4_RowRef rItem = m_vDict[ndx];
+ wxASSERT(pRefType(rItem) == REF_INTEGER);
+ pRef(rItem) = value;
+ }
+}
+
void PListDict::SetString(const char* key, const char* text) {
wxASSERT(m_rPlist);
wxASSERT(key && text);
@@ -3000,6 +3038,31 @@ PListArray::PListArray(c4_View& array, c4_RowRef& plist)
m_vArrays = pArrays(*m_rPlist);
}
+bool PListArray::IsBool(unsigned int ndx) const {
+ wxASSERT(m_rPlist);
+ wxASSERT((int)ndx < m_vArray.GetSize());
+
+ const c4_RowRef rItem = m_vArray[ndx];
+ return pType(rItem) == REF_BOOL;
+}
+
+bool PListArray::IsInt(unsigned int ndx) const {
+ wxASSERT(m_rPlist);
+ wxASSERT((int)ndx < m_vArray.GetSize());
+
+ const c4_RowRef rItem = m_vArray[ndx];
+ return pType(rItem) == REF_INTEGER;
+}
+
+bool PListArray::IsString(unsigned int ndx) const {
+ wxASSERT(m_rPlist);
+ wxASSERT((int)ndx < m_vArray.GetSize());
+
+ const c4_RowRef rItem = m_vArray[ndx];
+ return pType(rItem) == REF_STRING;
+}
+
+
void PListArray::SetArray(c4_View& array, c4_RowRef& plist) {
m_vArray = array;
@@ -3011,6 +3074,28 @@ void PListArray::SetArray(c4_View& array, c4_RowRef& plist) {
m_vArrays = pArrays(*m_rPlist);
}
+bool PListArray::GetBool(unsigned int ndx) const {
+ wxASSERT(m_rPlist);
+ wxASSERT((int)ndx < m_vArray.GetSize());
+
+ const c4_RowRef rItem = m_vArray[ndx];
+ if (pType(rItem) != REF_BOOL) return false;
+
+ const int ref = pRef(rItem);
+ return ref != 0;
+}
+
+int PListArray::GetInt(unsigned int ndx) const {
+ wxASSERT(m_rPlist);
+ wxASSERT((int)ndx < m_vArray.GetSize());
+
+ const c4_RowRef rItem = m_vArray[ndx];
+ if (pType(rItem) != REF_INTEGER) return false;
+
+ const int ref = pRef(rItem);
+ return ref;
+}
+
const char* PListArray::GetString(unsigned int ndx) const {
wxASSERT(m_rPlist);
wxASSERT((int)ndx < m_vArray.GetSize());
@@ -3150,6 +3235,20 @@ void PListArray::InsertString(unsigned int ndx, const char* text) {
m_vArray.InsertAt(ndx, rRef);
}
+void PListArray::AddBool(bool value) {
+ c4_Row rRef;
+ pRefType(rRef) = REF_BOOL;
+ pRef(rRef) = value ? 1 : 0;
+ m_vArray.Add(rRef);
+}
+
+void PListArray::AddInt(int value) {
+ c4_Row rRef;
+ pRefType(rRef) = REF_INTEGER;
+ pRef(rRef) = value;
+ m_vArray.Add(rRef);
+}
+
void PListArray::AddString(const char* text) {
wxASSERT(text);
const int ref = m_vStrings.Add(pString[text]);
@@ -3160,6 +3259,10 @@ void PListArray::AddString(const char* text) {
m_vArray.Add(rRef);
}
+void PListArray::AddString(const wxString& text) {
+ AddString(text.ToUTF8());
+}
+
wxJSONValue PListArray::GetJSONArray() const {
wxJSONValue root;
View
@@ -242,6 +242,8 @@ class PListDict : public PListItem {
bool GetBool(const char* key) const;
void DeleteItem(const char* key);
+ void SetBool(const char* key, bool value);
+ void SetInt(const char* key, int value);
void SetString(const char* key, const char* text);
void wxSetString(const char* key, const wxString& text);
@@ -277,6 +279,13 @@ class PListArray : public PListItem {
void Clear();
unsigned int GetSize() const {return m_vArray.GetSize();};
+
+ bool IsBool(unsigned int ndx) const;
+ bool IsInt(unsigned int ndx) const;
+ bool IsString(unsigned int ndx) const;
+
+ bool GetBool(unsigned int ndx) const;
+ int GetInt(unsigned int ndx) const;
const char* GetString(unsigned int ndx) const;
wxString wxGetString(unsigned int ndx) const;
bool GetDict(unsigned int ndx, PListDict& dict) const;
@@ -286,7 +295,10 @@ class PListArray : public PListItem {
PListDict InsertNewDict(unsigned int ndx);
PListArray InsertNewArray(unsigned int ndx);
void InsertString(unsigned int ndx, const char* str);
+ void AddBool(bool value);
+ void AddInt(int value);
void AddString(const char* str);
+ void AddString(const wxString& text);
// Plist <-> JSON conversion (support functions)
wxString GetJSON() const;
Oops, something went wrong.

0 comments on commit 35f2b0f

Please sign in to comment.