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