]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/wx-console/wxbconfigpanel.cpp
Some Win32 fixes
[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), 0, 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, wxSize(150, 20), 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 != NULL) ? statictext->GetLabel() : wxString("");
92       break;
93    case modifiableText:
94       return (textctrl != NULL) ? textctrl->GetValue() : wxString("");      
95       break;
96    case choice:
97       return (choicectrl != NULL) ? choicectrl->GetStringSelection() : wxString("");
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, wxString title,
158       wxWindowID ok, wxWindowID cancel, wxWindowID apply): wxPanel(parent) {
159
160    this->config = config;
161
162    wxBoxSizer* mainSizer = new wxBoxSizer(wxVERTICAL);
163    
164    mainSizer->Add(
165       new wxStaticText(this, -1, title, wxDefaultPosition, wxDefaultSize, wxALIGN_CENTER),
166             0, wxALIGN_CENTER_HORIZONTAL | wxALL, 5);
167
168    wxFlexGridSizer* cfgSizer = new wxFlexGridSizer(config->GetCount()+1, 2, 8, 5);
169    unsigned int i;
170    for (i = 0; i < config->GetCount(); i++) {
171       (*config)[i].AddControl(this, cfgSizer);
172    }
173    mainSizer->Add(cfgSizer, 1, wxALIGN_CENTER_HORIZONTAL | wxALIGN_CENTER_VERTICAL | wxALL, 5);
174    
175    wxBoxSizer* restoreBottomSizer = new wxBoxSizer(wxHORIZONTAL);
176    
177    cfgOk = new wxButton(this, ok, "OK", wxDefaultPosition, wxSize(70, 25));
178    restoreBottomSizer->Add(cfgOk, 1, wxALIGN_CENTER_VERTICAL | wxRIGHT, 10);
179
180    if (apply != -1) {
181       cfgApply = new wxButton(this, apply, "Apply", wxDefaultPosition, wxSize(70, 25));
182       restoreBottomSizer->Add(cfgApply, 1, wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT, 10);
183    }
184    else {
185       cfgApply = NULL;
186    }
187
188    cfgCancel = new wxButton(this, cancel, "Cancel", wxDefaultPosition, wxSize(70, 25));
189    restoreBottomSizer->Add(cfgCancel, 1, wxALIGN_CENTER_VERTICAL | wxLEFT, 10);
190    
191    mainSizer->Add(restoreBottomSizer, 0, wxALIGN_CENTER_HORIZONTAL | wxALIGN_CENTER_VERTICAL | wxALL, 5);
192    
193    SetSizer(mainSizer);
194    mainSizer->SetSizeHints(this);
195    
196    last = 0;
197 }
198
199 wxbConfigPanel::~wxbConfigPanel() {
200    delete config;
201 }
202    
203 void wxbConfigPanel::SetRowString(const char* title, wxString value) {
204    int i;
205    if ((i = FindRow(title)) > -1) {
206       (*config)[i].SetValue(value);
207    }
208 }
209
210 wxString wxbConfigPanel::GetRowString(const char* title) {
211    int i;
212    if ((i = FindRow(title)) > -1) {
213       return (*config)[i].GetValue();
214    }
215    return "";
216 }
217
218 void wxbConfigPanel::SetRowSelection(const char* title, int ind) {
219    int i;
220    if ((i = FindRow(title)) > -1) {
221       (*config)[i].SetIndex(ind);
222    }
223 }
224
225 int wxbConfigPanel::GetRowSelection(const char* title) {
226    int i;
227    if ((i = FindRow(title)) > -1) {
228       return (*config)[i].GetIndex();
229    }
230    return -1;
231 }
232
233 void wxbConfigPanel::ClearRowChoices(const char* title) {
234    int i;
235    if ((i = FindRow(title)) > -1) {
236       (*config)[i].Clear();
237    }  
238 }
239
240 void wxbConfigPanel::AddRowChoice(const char* title, wxString value) {
241    int i;
242    if ((i = FindRow(title)) > -1) {
243       (*config)[i].Add(value);
244    }  
245 }
246
247 int wxbConfigPanel::FindRow(const char* title) {
248    unsigned int i;
249    
250    for (i = last; i < config->GetCount(); i++) {
251       if ((*config)[i].GetTitle() == title) {
252          last = i;
253          return i;
254       }
255    }
256    
257    for (i = 0; i < last; i++) {
258       if ((*config)[i].GetTitle() == title) {
259          last = i;
260          return i;
261       }
262    }
263    
264    last = 0;
265    return -1;
266 }
267
268 void wxbConfigPanel::EnableApply(bool enable) {
269    cfgOk->Enable(!enable);
270    if (cfgApply) cfgApply->Enable(enable);
271 }