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