]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/wx-console/wxbconfigpanel.cpp
80c039a489717263c3901aa420de323a9343f433
[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    Copyright (C) 2004 Kern Sibbald and John Walker
11
12    This program is free software; you can redistribute it and/or
13    modify it under the terms of the GNU General Public License
14    as published by the Free Software Foundation; either version 2
15    of the License, or (at your option) any later version.
16
17    This program is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20    GNU General Public License for more details.
21
22    You should have received a copy of the GNU General Public License
23    along with this program; if not, write to the Free Software
24    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25  */
26
27 #include "wxbconfigpanel.h"
28
29 #include <wx/arrimpl.cpp>
30
31 WX_DEFINE_OBJARRAY(wxbConfig);
32
33 /* Create a new config parameter */
34 wxbConfigParam::wxbConfigParam(wxString title, wxWindowID id, wxbConfigType type, wxString value) {
35    this->title = title;
36    this->id = id;
37    this->type = type;
38    this->value = value;
39    this->values = NULL;
40    this->nvalues = 0;
41    this->choicectrl = NULL;
42    this->textctrl = NULL;
43    this->statictext = NULL;
44 }
45
46 wxbConfigParam::wxbConfigParam(wxString title, wxWindowID id, wxbConfigType type, int n, wxString* values) {
47    this->title = title;
48    this->id = id;
49    this->type = type;
50    this->values = new wxString[n];
51    for (int i = 0; i < n; i++) {
52       this->values[i] = values[i];
53    }
54    this->nvalues = n;
55    this->choicectrl = NULL;
56    this->textctrl = NULL;
57    this->statictext = NULL;
58 }
59
60 wxbConfigParam::~wxbConfigParam() {
61    if (values) delete[] values;
62    if (choicectrl) delete choicectrl;
63    if (textctrl) delete textctrl;
64    if (statictext) delete statictext;
65 }
66   
67 void wxbConfigParam::AddControl(wxWindow* parent, wxSizer* sizer) {
68    sizer->Add(new wxStaticText(parent, -1, title + wxT(": "), wxDefaultPosition, wxDefaultSize, wxALIGN_RIGHT), 0, wxALIGN_CENTER_VERTICAL);
69    switch (type) {
70    case text:
71       statictext = new wxStaticText(parent, -1, value, wxDefaultPosition, wxDefaultSize, wxALIGN_LEFT);
72       sizer->Add(statictext, 1, wxEXPAND | wxADJUST_MINSIZE);
73       break;
74    case modifiableText:
75       textctrl = new wxTextCtrl(parent, id, value, wxDefaultPosition, wxDefaultSize);
76       sizer->Add(textctrl, 1, wxEXPAND | wxADJUST_MINSIZE);
77       break;
78    case choice:
79 #if defined __WXGTK20__ /* Choices are taller under GTK+-2.0 */
80       wxSize size = wxSize(150, 25);
81 #else
82       wxSize size = wxSize(150, 20);
83 #endif
84       choicectrl = new wxChoice(parent, id, wxDefaultPosition, size, nvalues, values);
85       sizer->Add(choicectrl, 1, wxEXPAND);
86       break;
87    }
88 }
89
90 wxString wxbConfigParam::GetTitle() {
91    return title;
92 }
93
94 wxString wxbConfigParam::GetValue() {
95    switch (type) {
96    case text:
97       return (statictext != NULL) ? statictext->GetLabel() : wxString(wxT(""));
98       break;
99    case modifiableText:
100       return (textctrl != NULL) ? textctrl->GetValue() : wxString(wxT(""));      
101       break;
102    case choice:
103       return (choicectrl != NULL) ? choicectrl->GetStringSelection() : wxString(wxT(""));
104       break;      
105    }
106    return wxT("");
107 }
108
109 void wxbConfigParam::SetValue(wxString str) {
110    switch (type) {
111    case text:
112       if (statictext)
113          statictext->SetLabel(str);
114       break;
115    case modifiableText:
116       if (textctrl)
117          textctrl->SetValue(str);      
118       break;
119    case choice:
120       if (choicectrl) {
121          int k;
122          for (k = 0; k < choicectrl->GetCount(); k++) {
123             if (str == choicectrl->GetString(k)) {
124                choicectrl->SetSelection(k);
125                break;
126             }
127          }
128          if (k == choicectrl->GetCount()) { // Should never happen
129             choicectrl->Append(str);
130             choicectrl->SetSelection(k);
131          }
132       }
133       break;      
134    }
135 }
136
137 int wxbConfigParam::GetIndex() {
138    if (choicectrl) {
139       return choicectrl->GetSelection();
140    }
141    return -1;
142 }
143
144 void wxbConfigParam::SetIndex(int ind) {
145    if (choicectrl) {
146       choicectrl->SetSelection(ind);
147    }
148 }
149
150 void wxbConfigParam::Clear() {
151    if (choicectrl) {
152       choicectrl->Clear();
153    }
154 }
155
156 void wxbConfigParam::Add(wxString value) {
157    if (choicectrl) {
158       choicectrl->Append(value);
159    }
160 }
161
162 /* Creates a new config panel, config must be allocated with new */
163 wxbConfigPanel::wxbConfigPanel(wxWindow* parent, wxbConfig* config, wxString title,
164       wxWindowID ok, wxWindowID cancel, wxWindowID apply): wxPanel(parent) {
165
166    this->config = config;
167
168    wxBoxSizer* mainSizer = new wxBoxSizer(wxVERTICAL);
169    
170    mainSizer->Add(
171       new wxStaticText(this, -1, title, wxDefaultPosition, wxDefaultSize, wxALIGN_CENTER),
172             0, wxALIGN_CENTER_HORIZONTAL | wxALL, 5);
173
174    wxFlexGridSizer* cfgSizer = new wxFlexGridSizer(config->GetCount()+1, 2, 8, 5);
175    unsigned int i;
176    for (i = 0; i < config->GetCount(); i++) {
177       (*config)[i].AddControl(this, cfgSizer);
178    }
179    mainSizer->Add(cfgSizer, 1, wxALIGN_CENTER_HORIZONTAL | wxALIGN_CENTER_VERTICAL | wxALL, 5);
180    
181    wxBoxSizer* restoreBottomSizer = new wxBoxSizer(wxHORIZONTAL);
182    
183    cfgOk = new wxButton(this, ok, wxT("OK"), wxDefaultPosition, wxSize(70, 25));
184    restoreBottomSizer->Add(cfgOk, 1, wxALIGN_CENTER_VERTICAL | wxRIGHT, 10);
185
186    if (apply != -1) {
187       cfgApply = new wxButton(this, apply, wxT("Apply"), wxDefaultPosition, wxSize(70, 25));
188       restoreBottomSizer->Add(cfgApply, 1, wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT, 10);
189    }
190    else {
191       cfgApply = NULL;
192    }
193
194    cfgCancel = new wxButton(this, cancel, wxT("Cancel"), wxDefaultPosition, wxSize(70, 25));
195    restoreBottomSizer->Add(cfgCancel, 1, wxALIGN_CENTER_VERTICAL | wxLEFT, 10);
196    
197    mainSizer->Add(restoreBottomSizer, 0, wxALIGN_CENTER_HORIZONTAL | wxALIGN_CENTER_VERTICAL | wxALL, 5);
198    
199    SetSizer(mainSizer);
200    mainSizer->SetSizeHints(this);
201    
202    last = 0;
203 }
204
205 wxbConfigPanel::~wxbConfigPanel() {
206    delete config;
207 }
208    
209 void wxbConfigPanel::SetRowString(const wxChar* title, wxString value) {
210    int i;
211    if ((i = FindRow(title)) > -1) {
212       (*config)[i].SetValue(value);
213    }
214 }
215
216 wxString wxbConfigPanel::GetRowString(const wxChar* title) {
217    int i;
218    if ((i = FindRow(title)) > -1) {
219       return (*config)[i].GetValue();
220    }
221    return wxT("");
222 }
223
224 void wxbConfigPanel::SetRowSelection(const wxChar* title, int ind) {
225    int i;
226    if ((i = FindRow(title)) > -1) {
227       (*config)[i].SetIndex(ind);
228    }
229 }
230
231 int wxbConfigPanel::GetRowSelection(const wxChar* title) {
232    int i;
233    if ((i = FindRow(title)) > -1) {
234       return (*config)[i].GetIndex();
235    }
236    return -1;
237 }
238
239 void wxbConfigPanel::ClearRowChoices(const wxChar* title) {
240    int i;
241    if ((i = FindRow(title)) > -1) {
242       (*config)[i].Clear();
243    }  
244 }
245
246 void wxbConfigPanel::AddRowChoice(const wxChar* title, wxString value) {
247    int i;
248    if ((i = FindRow(title)) > -1) {
249       (*config)[i].Add(value);
250    }  
251 }
252
253 int wxbConfigPanel::FindRow(const wxChar* title) {
254    unsigned int i;
255    
256    for (i = last; i < config->GetCount(); i++) {
257       if ((*config)[i].GetTitle() == title) {
258          last = i;
259          return i;
260       }
261    }
262    
263    for (i = 0; i < last; i++) {
264       if ((*config)[i].GetTitle() == title) {
265          last = i;
266          return i;
267       }
268    }
269    
270    last = 0;
271    return -1;
272 }
273
274 void wxbConfigPanel::EnableApply(bool enable) {
275    cfgOk->Enable(!enable);
276    if (cfgApply) cfgApply->Enable(enable);
277 }