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