]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/wx-console/wxbconfigfileeditor.cpp
kes wx-console crashes because of differences between Bacula and wxWidgets
[bacula/bacula] / bacula / src / wx-console / wxbconfigfileeditor.cpp
1 /*
2  *
3  *    Configuration file editor
4  *
5  *    Nicolas Boichat, May 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 "wxbconfigfileeditor.h"
26 #include <wx/file.h>
27 #include <wx/filename.h>
28
29
30 enum
31 {
32    Save = 1,
33    Quit = 2
34 };
35
36 BEGIN_EVENT_TABLE(wxbConfigFileEditor, wxDialog)
37    EVT_BUTTON(Save, wxbConfigFileEditor::OnSave)
38    EVT_BUTTON(Quit, wxbConfigFileEditor::OnQuit)
39 #ifdef HAVE_WIN32
40    EVT_PAINT (      wxbConfigFileEditor::OnPaint)
41 #endif
42 END_EVENT_TABLE()
43
44 wxbConfigFileEditor::wxbConfigFileEditor(wxWindow* parent, wxString filename):
45       wxDialog(parent, -1, _("Config file editor"), wxDefaultPosition, wxSize(500, 300),
46                    wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER) {
47    this->filename = filename;
48    
49    wxString strbuf;
50
51    wxFileName filen(filename);
52    
53    if (!filen.FileExists()) {
54       strbuf << wxT("#\n");
55       strbuf << _("# Bacula wx-console Configuration File\n");
56       strbuf << wxT("#\n");
57       strbuf << wxT("\n");
58       strbuf << wxT("Director {\n");
59       strbuf << wxT("  Name = <hostname>-dir\n");
60       strbuf << wxT("  DIRport = 9101\n");
61       strbuf << wxT("  address = <hostname>\n");
62       strbuf << wxT("  Password = \"<dir_password>\"\n");
63       strbuf << wxT("}\n");
64    }
65    else {
66       wxFile file(filename);
67       char buffer[2049];
68       off_t len;
69       while ((len = file.Read(buffer, 2048)) > -1) {
70          buffer[len] = 0;
71          strbuf << wxString(buffer,wxConvUTF8);
72          if (file.Eof())
73             break;
74       }
75       file.Close();
76    }
77
78    textCtrl = new wxTextCtrl(this,-1,strbuf,wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE | wxTE_RICH2 | wxTE_DONTWRAP);
79    wxFont font(10, wxMODERN, wxNORMAL, wxNORMAL);
80 #if defined __WXGTK12__ && !defined __WXGTK20__ // Fix for "chinese" fonts under gtk+ 1.2
81    font.SetDefaultEncoding(wxFONTENCODING_ISO8859_1);
82 #endif
83    textCtrl->SetDefaultStyle(wxTextAttr(*wxBLACK, wxNullColour, font));
84    textCtrl->SetStyle(0, textCtrl->GetLastPosition(), wxTextAttr(*wxBLACK, wxNullColour, font));
85
86    wxFlexGridSizer *mainSizer = new wxFlexGridSizer(2, 1, 0, 0);
87    mainSizer->AddGrowableCol(0);
88    mainSizer->AddGrowableRow(0);
89    
90    wxBoxSizer *bottomsizer = new wxBoxSizer(wxHORIZONTAL);
91    bottomsizer->Add(new wxButton(this, Save, _("Save and close")), 0, wxALL, 10);
92    bottomsizer->Add(new wxButton(this, Quit, _("Close without saving")), 0, wxALL, 10);
93    
94    mainSizer->Add(textCtrl, 1, wxEXPAND);
95    mainSizer->Add(bottomsizer, 0, wxALIGN_CENTER_HORIZONTAL | wxALIGN_CENTER_VERTICAL);
96    
97    this->SetSizer(mainSizer);
98
99    firstpaint = true;
100 }
101
102 wxbConfigFileEditor::~wxbConfigFileEditor() {
103    
104 }
105
106 /* Kludge for Win32, so the text control is not completely selected. */
107 void wxbConfigFileEditor::OnPaint(wxPaintEvent& event) {
108    wxPaintDC dc(this);
109
110    if (firstpaint) {
111       firstpaint = false;
112       textCtrl->SetSelection(0, 0);
113    }
114 }
115
116 void wxbConfigFileEditor::OnSave(wxCommandEvent& event) {
117    wxFile file(filename, wxFile::write);
118    if (!file.IsOpened()) {
119       wxMessageBox(wxString::Format(_("Unable to write to %s\n"), filename.c_str()),
120                         _("Error while saving"),
121                         wxOK | wxICON_ERROR, this);
122       EndModal(wxCANCEL);
123       return;
124    }
125    
126    file.Write(textCtrl->GetValue(),wxConvLocal);
127    
128    file.Flush();
129    file.Close();
130    
131    EndModal(wxOK);
132 }
133
134 void wxbConfigFileEditor::OnQuit(wxCommandEvent& event) {
135    EndModal(wxCANCEL);
136 }