1 //////////////////////////////////////////////////////////////////////////
2 //
3 // pgAdmin III - PostgreSQL Tools
4 //
5 // Copyright (C) 2002 - 2016, The pgAdmin Development Team
6 // This software is released under the PostgreSQL Licence
7 //
8 // dlgManageMacros.cpp - Manage macros
9 //
10 //////////////////////////////////////////////////////////////////////////
11
12 // App headers
13 #include "pgAdmin3.h"
14
15 #include "dlg/dlgManageMacros.h"
16 #include "db/pgConn.h"
17 #include "schema/pgServer.h"
18 #include "utils/sysLogger.h"
19 #include "ctl/ctlSQLBox.h"
20 #include "utils/macros.h"
21
22 #include <wx/imaglist.h>
23
24 //pointer to controls
25 #define lstKeys CTRL_LISTVIEW("lstKeys")
26 #define txtName CTRL_TEXT("txtName")
27 #define txtSqlBox CTRL_SQLBOX("txtSqlBox")
28 #define btnClear CTRL_BUTTON("btnClear")
29 #define btnSave CTRL_BUTTON("btnSave")
30
BEGIN_EVENT_TABLE(dlgManageMacros,DialogWithHelp)31 BEGIN_EVENT_TABLE(dlgManageMacros, DialogWithHelp)
32 EVT_LIST_ITEM_SELECTED (XRCID("lstKeys"), dlgManageMacros::OnKeySelect)
33 EVT_BUTTON (wxID_OK, dlgManageMacros::OnOK)
34 EVT_BUTTON (wxID_CANCEL, dlgManageMacros::OnCancel)
35 EVT_BUTTON (XRCID("btnClear"), dlgManageMacros::OnClear)
36 EVT_BUTTON (XRCID("btnSave"), dlgManageMacros::OnSave)
37 EVT_TEXT (XRCID("txtName"), dlgManageMacros::OnNameChange)
38 EVT_STC_CHANGE (XRCID("txtSqlBox"), dlgManageMacros::OnQueryChange)
39 END_EVENT_TABLE()
40
41 dlgManageMacros::dlgManageMacros(wxWindow *parent, frmMain *form, queryMacroList *macros) :
42 DialogWithHelp(form)
43 {
44 SetFont(settings->GetSystemFont());
45 LoadResource(parent, wxT("dlgManageMacros"));
46 RestorePosition();
47
48 this->macros = macros;
49
50 // Setup list of keys
51 lstKeys->CreateColumns(NULL, _("Key"), _("Name"), 40);
52
53 lstKeys->Hide();
54 size_t i;
55 int num = 0;
56 for (i = 1; i < 13; i++)
57 {
58 wxString key;
59 key.Printf(wxT("Alt-F%d"), (int)i);
60 AddKeyToList(num++, key);
61 }
62 for (i = 1; i < 11; i++)
63 {
64 wxString key;
65 key.Printf(wxT("Ctrl-%d"), (int)i % 10); // in order of keys 1,2,...,8,9,0
66 AddKeyToList(num++, key);
67 }
68 lstKeys->Show();
69
70 // Initialy no key is selected, so disable editor keys
71 btnClear->Disable();
72 btnSave->Disable();
73
74 // Clear Markers
75 anythingChanged = false;
76 thisMacroChanged = false;
77
78 txtSqlBox->SetModEventMask(wxSTC_MOD_INSERTTEXT | wxSTC_MOD_DELETETEXT);
79 }
80
AddKeyToList(int position,const wxString & key)81 void dlgManageMacros::AddKeyToList(int position, const wxString &key)
82 {
83 long tmp = lstKeys->InsertItem(position, key);
84 queryMacroItem *item = macros->FindMacro(key);
85 if (item != NULL)
86 lstKeys->SetItem(tmp, 1, item->GetName());
87 }
88
ManageMacros()89 int dlgManageMacros::ManageMacros()
90 {
91 int r = ShowModal();
92 if (r == wxID_OK)
93 {
94 return 1;
95 }
96 else
97 {
98 if (anythingChanged)
99 return -1;
100 else
101 return 0;
102 }
103 }
104
~dlgManageMacros()105 dlgManageMacros::~dlgManageMacros()
106 {
107 SavePosition();
108 }
109
OnOK(wxCommandEvent & ev)110 void dlgManageMacros::OnOK(wxCommandEvent &ev)
111 {
112 if (thisMacroChanged)
113 SetMacro(true);
114 EndModal(wxID_OK);
115 }
116
OnCancel(wxCommandEvent & ev)117 void dlgManageMacros::OnCancel(wxCommandEvent &ev)
118 {
119 EndModal(wxID_CANCEL);
120 }
121
DeleteMacro(int listItem)122 void dlgManageMacros::DeleteMacro(int listItem)
123 {
124 wxString key;
125 key = lstKeys->GetItemText(listItem);
126
127 if (macros->DelMacro(key))
128 {
129 anythingChanged = true;
130 lstKeys->SetItem(listItem, 1, wxT(""));
131 txtName->ChangeValue(wxT(""));
132 txtSqlBox->SetText(wxT(""));
133 thisMacroChanged = false;
134 btnSave->Disable();
135 btnClear->Disable();
136 }
137 }
138
OnClear(wxCommandEvent & ev)139 void dlgManageMacros::OnClear(wxCommandEvent &ev)
140 {
141 int item;
142 item = lstKeys->GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
143
144 if (item == -1)
145 return;
146
147 DeleteMacro(item);
148 }
149
OnSave(wxCommandEvent & ev)150 void dlgManageMacros::OnSave(wxCommandEvent &ev)
151 {
152 if (!thisMacroChanged)
153 return;
154 SetMacro(false);
155 }
156
SetMacro(bool silent)157 void dlgManageMacros::SetMacro(bool silent)
158 {
159 int item;
160 wxString key, Name, query;
161
162 item = lstKeys->GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
163 if (item == -1)
164 return;
165
166 key = lstKeys->GetItemText(item);
167 Name = txtName->GetValue().Trim();
168 query = txtSqlBox->GetText().Trim();
169
170 if (Name.IsEmpty() && query.IsEmpty())
171 {
172 DeleteMacro(item);
173 }
174 else if (Name.IsEmpty() || query.IsEmpty())
175 {
176 if (!silent)
177 wxMessageBox(_("You must specify a query and a name for the macro"), _("Save macro"), wxICON_EXCLAMATION | wxOK);
178 return;
179 }
180 else
181 {
182 macros->AddOrUpdateMacro(key, Name, query);
183 anythingChanged = true;
184 thisMacroChanged = false;
185 lstKeys->SetItem(item, 1, Name);
186 btnClear->Enable();
187 btnSave->Disable();
188 }
189 }
190
OnKeySelect(wxListEvent & ev)191 void dlgManageMacros::OnKeySelect(wxListEvent &ev)
192 {
193 wxString key;
194 key = ev.GetText();
195
196 queryMacroItem *item = macros->FindMacro(key);
197 if (item != NULL)
198 {
199 txtName->ChangeValue(item->GetName());
200 txtSqlBox->SetText(item->GetQuery());
201 btnClear->Enable();
202 btnSave->Disable();
203 }
204 else
205 {
206 txtName->ChangeValue(wxT(""));
207 txtSqlBox->SetText(wxT(""));
208 btnClear->Disable();
209 btnSave->Disable();
210 }
211 thisMacroChanged = false;
212 }
213
OnNameChange(wxCommandEvent & ev)214 void dlgManageMacros::OnNameChange(wxCommandEvent &ev)
215 {
216 thisMacroChanged = true;
217 btnSave->Enable();
218 }
219
OnQueryChange(wxStyledTextEvent & ev)220 void dlgManageMacros::OnQueryChange(wxStyledTextEvent &ev)
221 {
222 thisMacroChanged = true;
223 btnSave->Enable();
224 }
225
GetHelpPage() const226 wxString dlgManageMacros::GetHelpPage() const
227 {
228 return wxT("macros");
229 }
230