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