]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/wx-console/wxbconfigpanel.cpp
kes Simplify locking in the reservations system.
[bacula/bacula] / bacula / src / wx-console / wxbconfigpanel.cpp
1 /*
2  *
3  *   Config panel, used to specify parameters (for example clients, filesets... in restore)
4  *
5  *    Nicolas Boichat, April 2004
6  *
7  *    Version $Id$
8  */
9 /*
10    Bacula® - The Network Backup Solution
11
12    Copyright (C) 2004-2006 Free Software Foundation Europe e.V.
13
14    The main author of Bacula is Kern Sibbald, with contributions from
15    many others, a complete list can be found in the file AUTHORS.
16    This program is Free Software; you can redistribute it and/or
17    modify it under the terms of version two of the GNU General Public
18    License as published by the Free Software Foundation plus additions
19    that are listed in the file LICENSE.
20
21    This program is distributed in the hope that it will be useful, but
22    WITHOUT ANY WARRANTY; without even the implied warranty of
23    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24    General Public License for more details.
25
26    You should have received a copy of the GNU General Public License
27    along with this program; if not, write to the Free Software
28    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
29    02110-1301, USA.
30
31    Bacula® is a registered trademark of John Walker.
32    The licensor of Bacula is the Free Software Foundation Europe
33    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
34    Switzerland, email:ftf@fsfeurope.org.
35 */
36
37 /*  Windows debug builds set _DEBUG which is used by wxWidgets to select their
38  *  debug memory allocator.  Unfortunately it conflicts with Bacula's SmartAlloc.
39  * So we turn _DEBUG off since we aren't interested in things it enables.
40  */
41
42 #undef _DEBUG
43
44 #include "bacula.h"
45 #include "wxbconfigpanel.h"
46 #include <wx/arrimpl.cpp>
47
48
49 WX_DEFINE_OBJARRAY(wxbConfig);
50
51 /* Create a new config parameter */
52 wxbConfigParam::wxbConfigParam(wxString title, wxWindowID id, wxbConfigType type, wxString value) {
53    this->title = title;
54    this->id = id;
55    this->type = type;
56    this->value = value;
57    this->values = NULL;
58    this->nvalues = 0;
59    this->choicectrl = NULL;
60    this->textctrl = NULL;
61    this->statictext = NULL;
62 }
63
64 wxbConfigParam::wxbConfigParam(wxString title, wxWindowID id, wxbConfigType type, int n, wxString* values) {
65    this->title = title;
66    this->id = id;
67    this->type = type;
68    this->values = new wxString[n];
69    for (int i = 0; i < n; i++) {
70       this->values[i] = values[i];
71    }
72    this->nvalues = n;
73    this->choicectrl = NULL;
74    this->textctrl = NULL;
75    this->statictext = NULL;
76 }
77
78 wxbConfigParam::~wxbConfigParam() {
79    if (values) delete[] values;
80    if (choicectrl) delete choicectrl;
81    if (textctrl) delete textctrl;
82    if (statictext) delete statictext;
83 }
84   
85 void wxbConfigParam::AddControl(wxWindow* parent, wxSizer* sizer) {
86    sizer->Add(new wxStaticText(parent, -1, title + wxT(": "), wxDefaultPosition, wxDefaultSize, wxALIGN_RIGHT), 0, wxALIGN_CENTER_VERTICAL);
87    switch (type) {
88    case text:
89       statictext = new wxStaticText(parent, -1, value, wxDefaultPosition, wxDefaultSize, wxALIGN_LEFT);
90       sizer->Add(statictext, 1, wxEXPAND | wxADJUST_MINSIZE);
91       break;
92    case modifiableText:
93       textctrl = new wxTextCtrl(parent, id, value, wxDefaultPosition, wxDefaultSize);
94       sizer->Add(textctrl, 1, wxEXPAND | wxADJUST_MINSIZE);
95       break;
96    case choice:
97 #if defined __WXGTK20__ /* Choices are taller under GTK+-2.0 */
98       wxSize size = wxSize(150, 25);
99 #else
100       wxSize size = wxSize(150, 20);
101 #endif
102       choicectrl = new wxChoice(parent, id, wxDefaultPosition, size, nvalues, values);
103       sizer->Add(choicectrl, 1, wxEXPAND);
104       break;
105    }
106 }
107
108 wxString wxbConfigParam::GetTitle() {
109    return title;
110 }
111
112 wxString wxbConfigParam::GetValue() {
113    switch (type) {
114    case text:
115       return (statictext != NULL) ? statictext->GetLabel() : wxString(wxT(""));
116       break;
117    case modifiableText:
118       return (textctrl != NULL) ? textctrl->GetValue() : wxString(wxT(""));      
119       break;
120    case choice:
121       return (choicectrl != NULL) ? choicectrl->GetStringSelection() : wxString(wxT(""));
122       break;      
123    }
124    return wxT("");
125 }
126
127 void wxbConfigParam::SetValue(wxString str) {
128    switch (type) {
129    case text:
130       if (statictext)
131          statictext->SetLabel(str);
132       break;
133    case modifiableText:
134       if (textctrl)
135          textctrl->SetValue(str);      
136       break;
137    case choice:
138       if (choicectrl) {
139          int k;
140          for (k = 0; k < (int)choicectrl->GetCount(); k++) {
141             if (str == choicectrl->GetString(k)) {
142                choicectrl->SetSelection(k);
143                break;
144             }
145          }
146          if (k == (int)choicectrl->GetCount()) { // Should never happen
147             choicectrl->Append(str);
148             choicectrl->SetSelection(k);
149          }
150       }
151       break;      
152    }
153 }
154
155 int wxbConfigParam::GetIndex() {
156    if (choicectrl) {
157       return choicectrl->GetSelection();
158    }
159    return -1;
160 }
161
162 void wxbConfigParam::SetIndex(int ind) {
163    if (choicectrl) {
164       choicectrl->SetSelection(ind);
165    }
166 }
167
168 void wxbConfigParam::Clear() {
169    if (choicectrl) {
170       choicectrl->Clear();
171    }
172 }
173
174 void wxbConfigParam::Add(wxString value) {
175    if (choicectrl) {
176       choicectrl->Append(value);
177    }
178 }
179
180 /* Creates a new config panel, config must be allocated with new */
181 wxbConfigPanel::wxbConfigPanel(wxWindow* parent, wxbConfig* config, wxString title,
182       wxWindowID ok, wxWindowID cancel, wxWindowID apply): wxPanel(parent) {
183
184    this->config = config;
185
186    wxBoxSizer* mainSizer = new wxBoxSizer(wxVERTICAL);
187    
188    mainSizer->Add(
189       new wxStaticText(this, -1, title, wxDefaultPosition, wxDefaultSize, wxALIGN_CENTER),
190             0, wxALIGN_CENTER_HORIZONTAL | wxALL, 5);
191
192    wxFlexGridSizer* cfgSizer = new wxFlexGridSizer(config->GetCount()+1, 2, 8, 5);
193    unsigned int i;
194    for (i = 0; i < config->GetCount(); i++) {
195       (*config)[i].AddControl(this, cfgSizer);
196    }
197    mainSizer->Add(cfgSizer, 1, wxALIGN_CENTER_HORIZONTAL | wxALIGN_CENTER_VERTICAL | wxALL, 5);
198    
199    wxBoxSizer* restoreBottomSizer = new wxBoxSizer(wxHORIZONTAL);
200    
201    cfgOk = new wxButton(this, ok, _("OK"), wxDefaultPosition, wxSize(70, 25));
202    restoreBottomSizer->Add(cfgOk, 1, wxALIGN_CENTER_VERTICAL | wxRIGHT, 10);
203
204    if (apply != -1) {
205       cfgApply = new wxButton(this, apply, _("Apply"), wxDefaultPosition, wxSize(70, 25));
206       restoreBottomSizer->Add(cfgApply, 1, wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT, 10);
207    }
208    else {
209       cfgApply = NULL;
210    }
211
212    cfgCancel = new wxButton(this, cancel, _("Cancel"), wxDefaultPosition, wxSize(70, 25));
213    restoreBottomSizer->Add(cfgCancel, 1, wxALIGN_CENTER_VERTICAL | wxLEFT, 10);
214    
215    mainSizer->Add(restoreBottomSizer, 0, wxALIGN_CENTER_HORIZONTAL | wxALIGN_CENTER_VERTICAL | wxALL, 5);
216    
217    SetSizer(mainSizer);
218    mainSizer->SetSizeHints(this);
219    
220    last = 0;
221 }
222
223 wxbConfigPanel::~wxbConfigPanel() {
224    delete config;
225 }
226    
227 void wxbConfigPanel::SetRowString(const wxChar* title, wxString value) {
228    int i;
229    if ((i = FindRow(title)) > -1) {
230       (*config)[i].SetValue(value);
231    }
232 }
233
234 wxString wxbConfigPanel::GetRowString(const wxChar* title) {
235    int i;
236    if ((i = FindRow(title)) > -1) {
237       return (*config)[i].GetValue();
238    }
239    return wxT("");
240 }
241
242 void wxbConfigPanel::SetRowSelection(const wxChar* title, int ind) {
243    int i;
244    if ((i = FindRow(title)) > -1) {
245       (*config)[i].SetIndex(ind);
246    }
247 }
248
249 int wxbConfigPanel::GetRowSelection(const wxChar* title) {
250    int i;
251    if ((i = FindRow(title)) > -1) {
252       return (*config)[i].GetIndex();
253    }
254    return -1;
255 }
256
257 void wxbConfigPanel::ClearRowChoices(const wxChar* title) {
258    int i;
259    if ((i = FindRow(title)) > -1) {
260       (*config)[i].Clear();
261    }  
262 }
263
264 void wxbConfigPanel::AddRowChoice(const wxChar* title, wxString value) {
265    int i;
266    if ((i = FindRow(title)) > -1) {
267       (*config)[i].Add(value);
268    }  
269 }
270
271 int wxbConfigPanel::FindRow(const wxChar* title) {
272    unsigned int i;
273    
274    for (i = last; i < config->GetCount(); i++) {
275       if ((*config)[i].GetTitle() == title) {
276          last = i;
277          return i;
278       }
279    }
280    
281    for (i = 0; i < last; i++) {
282       if ((*config)[i].GetTitle() == title) {
283          last = i;
284          return i;
285       }
286    }
287    
288    last = 0;
289    return -1;
290 }
291
292 void wxbConfigPanel::EnableApply(bool enable) {
293    cfgOk->Enable(!enable);
294    if (cfgApply) cfgApply->Enable(enable);
295 }