]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/wx-console/wxbconfigpanel.cpp
- wxbRestorePanel : implemented restore before=<Date> parameter.
[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 + ": ", wxDefaultPosition, wxDefaultSize, wxALIGN_RIGHT), 0, wxEXPAND | wxALIGN_CENTER_HORIZONTAL);
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       choicectrl = new wxChoice(parent, id, wxDefaultPosition, wxSize(150, 20), nvalues, values);
80       sizer->Add(choicectrl, 1, wxEXPAND);
81       break;
82    }
83 }
84
85 wxString wxbConfigParam::GetTitle() {
86    return title;
87 }
88
89 wxString wxbConfigParam::GetValue() {
90    switch (type) {
91    case text:
92       return (statictext != NULL) ? statictext->GetLabel() : wxString("");
93       break;
94    case modifiableText:
95       return (textctrl != NULL) ? textctrl->GetValue() : wxString("");      
96       break;
97    case choice:
98       return (choicectrl != NULL) ? choicectrl->GetStringSelection() : wxString("");
99       break;      
100    }
101    return "";
102 }
103
104 void wxbConfigParam::SetValue(wxString str) {
105    switch (type) {
106    case text:
107       if (statictext)
108          statictext->SetLabel(str);
109       break;
110    case modifiableText:
111       if (textctrl)
112          textctrl->SetValue(str);      
113       break;
114    case choice:
115       if (choicectrl) {
116          int k;
117          for (k = 0; k < choicectrl->GetCount(); k++) {
118             if (str == choicectrl->GetString(k)) {
119                choicectrl->SetSelection(k);
120                break;
121             }
122          }
123          if (k == choicectrl->GetCount()) { // Should never happen
124             choicectrl->Append(str);
125             choicectrl->SetSelection(k);
126          }
127       }
128       break;      
129    }
130 }
131
132 int wxbConfigParam::GetIndex() {
133    if (choicectrl) {
134       return choicectrl->GetSelection();
135    }
136    return -1;
137 }
138
139 void wxbConfigParam::SetIndex(int ind) {
140    if (choicectrl) {
141       choicectrl->SetSelection(ind);
142    }
143 }
144
145 void wxbConfigParam::Clear() {
146    if (choicectrl) {
147       choicectrl->Clear();
148    }
149 }
150
151 void wxbConfigParam::Add(wxString value) {
152    if (choicectrl) {
153       choicectrl->Append(value);
154    }
155 }
156
157 /* Creates a new config panel, config must be allocated with new */
158 wxbConfigPanel::wxbConfigPanel(wxWindow* parent, wxbConfig* config, wxString title,
159       wxWindowID ok, wxWindowID cancel, wxWindowID apply): wxPanel(parent) {
160
161    this->config = config;
162
163    wxBoxSizer* mainSizer = new wxBoxSizer(wxVERTICAL);
164    
165    mainSizer->Add(
166       new wxStaticText(this, -1, title, wxDefaultPosition, wxDefaultSize, wxALIGN_CENTER),
167             0, wxALIGN_CENTER_HORIZONTAL | wxALL, 5);
168
169    wxFlexGridSizer* cfgSizer = new wxFlexGridSizer(config->GetCount()+1, 2, 8, 5);
170    unsigned int i;
171    for (i = 0; i < config->GetCount(); i++) {
172       (*config)[i].AddControl(this, cfgSizer);
173    }
174    mainSizer->Add(cfgSizer, 1, wxALIGN_CENTER_HORIZONTAL | wxALIGN_CENTER_VERTICAL | wxALL, 5);
175    
176    wxBoxSizer* restoreBottomSizer = new wxBoxSizer(wxHORIZONTAL);
177    
178    cfgOk = new wxButton(this, ok, "OK", wxDefaultPosition, wxSize(70, 25));
179    restoreBottomSizer->Add(cfgOk, 1, wxALIGN_CENTER_VERTICAL | wxRIGHT, 10);
180
181    if (apply != -1) {
182       cfgApply = new wxButton(this, apply, "Apply", wxDefaultPosition, wxSize(70, 25));
183       restoreBottomSizer->Add(cfgApply, 1, wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT, 10);
184    }
185    else {
186       cfgApply = NULL;
187    }
188
189    cfgCancel = new wxButton(this, cancel, "Cancel", wxDefaultPosition, wxSize(70, 25));
190    restoreBottomSizer->Add(cfgCancel, 1, wxALIGN_CENTER_VERTICAL | wxLEFT, 10);
191    
192    mainSizer->Add(restoreBottomSizer, 0, wxALIGN_CENTER_HORIZONTAL | wxALIGN_CENTER_VERTICAL | wxALL, 5);
193    
194    SetSizer(mainSizer);
195    mainSizer->SetSizeHints(this);
196    
197    last = 0;
198 }
199
200 wxbConfigPanel::~wxbConfigPanel() {
201    delete config;
202 }
203    
204 void wxbConfigPanel::SetRowString(const char* title, wxString value) {
205    int i;
206    if ((i = FindRow(title)) > -1) {
207       (*config)[i].SetValue(value);
208    }
209 }
210
211 wxString wxbConfigPanel::GetRowString(const char* title) {
212    int i;
213    if ((i = FindRow(title)) > -1) {
214       return (*config)[i].GetValue();
215    }
216    return "";
217 }
218
219 void wxbConfigPanel::SetRowSelection(const char* title, int ind) {
220    int i;
221    if ((i = FindRow(title)) > -1) {
222       (*config)[i].SetIndex(ind);
223    }
224 }
225
226 int wxbConfigPanel::GetRowSelection(const char* title) {
227    int i;
228    if ((i = FindRow(title)) > -1) {
229       return (*config)[i].GetIndex();
230    }
231    return -1;
232 }
233
234 void wxbConfigPanel::ClearRowChoices(const char* title) {
235    int i;
236    if ((i = FindRow(title)) > -1) {
237       (*config)[i].Clear();
238    }  
239 }
240
241 void wxbConfigPanel::AddRowChoice(const char* title, wxString value) {
242    int i;
243    if ((i = FindRow(title)) > -1) {
244       (*config)[i].Add(value);
245    }  
246 }
247
248 int wxbConfigPanel::FindRow(const char* title) {
249    unsigned int i;
250    
251    for (i = last; i < config->GetCount(); i++) {
252       if ((*config)[i].GetTitle() == title) {
253          last = i;
254          return i;
255       }
256    }
257    
258    for (i = 0; i < last; i++) {
259       if ((*config)[i].GetTitle() == title) {
260          last = i;
261          return i;
262       }
263    }
264    
265    last = 0;
266    return -1;
267 }
268
269 void wxbConfigPanel::EnableApply(bool enable) {
270    cfgOk->Enable(!enable);
271    if (cfgApply) cfgApply->Enable(enable);
272 }