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