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