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