5 * Nicolas Boichat, April 2004
9 Copyright (C) 2004 Kern Sibbald and John Walker
11 This program is free software; you can redistribute it and/or
12 modify it under the terms of the GNU General Public License
13 as published by the Free Software Foundation; either version 2
14 of the License, or (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 #include "wxbmainframe.h" // class's header file
28 #include "wxbrestorepanel.h"
32 #include "wxwin16x16.xpm"
34 // ----------------------------------------------------------------------------
35 // event tables and other macros for wxWindows
36 // ----------------------------------------------------------------------------
38 // ----------------------------------------------------------------------------
40 // ----------------------------------------------------------------------------
42 // IDs for the controls and the menu commands
48 // it is important for the id corresponding to the "About" command to have
49 // this standard value as otherwise it won't be handled properly under Mac
50 // (where it is special and put into the "Apple" menu)
51 Minimal_About = wxID_ABOUT,
57 * wxbTHREAD_EVENT declaration, used by csprint
59 BEGIN_DECLARE_EVENT_TYPES()
60 DECLARE_EVENT_TYPE(wxbTHREAD_EVENT, 1)
61 END_DECLARE_EVENT_TYPES()
63 DEFINE_EVENT_TYPE(wxbTHREAD_EVENT)
65 // the event tables connect the wxWindows events with the functions (event
66 // handlers) which process them. It can be also done at run-time, but for the
67 // simple menu events like this the static method is much simpler.
68 BEGIN_EVENT_TABLE(wxbMainFrame, wxFrame)
69 EVT_MENU(Minimal_Quit, wxbMainFrame::OnQuit)
70 EVT_MENU(Minimal_About, wxbMainFrame::OnAbout)
71 EVT_TEXT_ENTER(TypeText, wxbMainFrame::OnEnter)
72 EVT_CUSTOM(wxbTHREAD_EVENT, Thread, wxbMainFrame::OnPrint)
75 // ----------------------------------------------------------------------------
77 // ----------------------------------------------------------------------------
80 * wxbThreadEvent constructor
82 wxbThreadEvent::wxbThreadEvent(int id): wxEvent(id, wxbTHREAD_EVENT) {
87 * wxbThreadEvent destructor
89 wxbThreadEvent::~wxbThreadEvent()
91 if (m_eventObject != NULL) {
97 * wxbThreadEvent copy constructor
99 wxbThreadEvent::wxbThreadEvent(const wxbThreadEvent& te)
101 this->m_eventType = te.m_eventType;
102 this->m_id = te.m_id;
103 if (te.m_eventObject != NULL) {
104 this->m_eventObject = new wxbPrintObject(*((wxbPrintObject*)te.m_eventObject));
107 this->m_eventObject = NULL;
109 this->m_skipped = te.m_skipped;
110 this->m_timeStamp = te.m_timeStamp;
114 * Must be implemented (abstract in wxEvent)
116 wxEvent* wxbThreadEvent::Clone() const
118 return new wxbThreadEvent(*this);
122 * Gets the wxbPrintObject attached to this event, containing data sent by director
124 wxbPrintObject* wxbThreadEvent::GetEventPrintObject()
126 return (wxbPrintObject*)m_eventObject;
130 * Sets the wxbPrintObject attached to this event
132 void wxbThreadEvent::SetEventPrintObject(wxbPrintObject* object)
134 m_eventObject = (wxObject*)object;
137 // ----------------------------------------------------------------------------
139 // ----------------------------------------------------------------------------
141 wxbMainFrame *wxbMainFrame::frame = NULL;
144 * Singleton constructor
146 wxbMainFrame* wxbMainFrame::CreateInstance(const wxString& title, const wxPoint& pos, const wxSize& size, long style)
148 frame = new wxbMainFrame(title, pos, size, style);
153 * Returns singleton instance
155 wxbMainFrame* wxbMainFrame::GetInstance()
163 wxbMainFrame::~wxbMainFrame()
169 * Private constructor
171 wxbMainFrame::wxbMainFrame(const wxString& title, const wxPoint& pos, const wxSize& size, long style)
172 : wxFrame(NULL, -1, title, pos, size, style)
176 // set the frame icon
177 SetIcon(wxIcon(wxwin16x16_xpm));
181 wxMenu *menuFile = new wxMenu;
183 // the "About" item should be in the help menu
184 wxMenu *helpMenu = new wxMenu;
185 helpMenu->Append(Minimal_About, _T("&About...\tF1"), _T("Show about dialog"));
187 menuFile->Append(Minimal_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));
189 // now append the freshly created menu to the menu bar...
190 wxMenuBar *menuBar = new wxMenuBar();
191 menuBar->Append(menuFile, _T("&File"));
192 menuBar->Append(helpMenu, _T("&Help"));
194 // ... and attach this menu bar to the frame
196 #endif // wxUSE_MENUS
199 // create a status bar just for fun (by default with 1 pane only)
201 SetStatusText(_T("Welcome to Bacula wx-GUI!"));
202 #endif // wxUSE_STATUSBAR
204 wxPanel* global = new wxPanel(this, -1);
206 notebook = new wxNotebook(global, -1);
210 wxPanel* consolePanel = new wxPanel(notebook, -1);
211 notebook->AddPage(consolePanel, "Console");
213 consoleCtrl = new wxTextCtrl(consolePanel,-1,"",wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE | wxTE_READONLY | wxTE_RICH);
214 consoleCtrl->SetDefaultStyle(wxTextAttr(*wxBLACK, wxNullColour, wxFont(10, wxMODERN, wxNORMAL, wxNORMAL)));
216 typeCtrl = new wxTextCtrl(consolePanel,TypeText,"",wxDefaultPosition,wxSize(200,20), wxTE_PROCESS_ENTER);
218 wxFlexGridSizer *consoleSizer = new wxFlexGridSizer(2, 1, 0, 0);
219 consoleSizer->AddGrowableCol(0);
220 consoleSizer->AddGrowableRow(0);
222 consoleSizer->Add(consoleCtrl, 1, wxEXPAND | wxALL, 0);
223 consoleSizer->Add(typeCtrl, 0, wxEXPAND | wxALL, 0);
225 consolePanel->SetAutoLayout( TRUE );
226 consolePanel->SetSizer( consoleSizer );
227 consoleSizer->SetSizeHints( consolePanel );
229 // Creates the list of panels which are included in notebook, and that need to receive director information
231 panels = new (wxbPanel*)[2];
232 panels[0] = new wxbRestorePanel(notebook);
235 for (int i = 0; panels[i] != NULL; i++) {
236 notebook->AddPage(panels[i], panels[i]->GetTitle());
239 wxBoxSizer* globalSizer = new wxBoxSizer(wxHORIZONTAL);
241 globalSizer->Add(new wxNotebookSizer(notebook), 1, wxEXPAND, 0);
243 global->SetSizer( globalSizer );
244 globalSizer->SetSizeHints( global );
246 wxBoxSizer* sizer = new wxBoxSizer(wxHORIZONTAL);
248 sizer->Add(global, 1, wxEXPAND | wxALL, 0);
251 //sizer->SetSizeHints( this );
255 * Starts the thread interacting with the director
257 void wxbMainFrame::StartConsoleThread()
262 ct = new console_thread();
269 void wxbMainFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
271 // TRUE is to force the frame to close
275 void wxbMainFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
278 msg.Printf( _T("Welcome to Bacula wx-GUI.\n (c) 2004 Nicolas Boichat <nicolas@boichat.ch>\n")
279 _T("Version : %s"), wxVERSION_STRING);
281 wxMessageBox(msg, _T("About Bacula-wx-GUI"), wxOK | wxICON_INFORMATION, this);
284 void wxbMainFrame::OnEnter(wxCommandEvent& WXUNUSED(event))
286 wxString str = typeCtrl->GetValue() + "\n";
291 * Called when data is arriving from director
293 void wxbMainFrame::OnPrint(wxbThreadEvent& event) {
294 wxbPrintObject* po = event.GetEventPrintObject();
296 Print(po->str, po->status);
300 * Prints data received from director to the console, and forwards it to the panels
302 void wxbMainFrame::Print(wxString str, int status)
304 // CS_DEBUG is often sent by panels, so resend it to them would cause an infinite loop
305 if (status != CS_DEBUG) {
306 for (int i = 0; panels[i] != NULL; i++) {
307 panels[i]->Print(str, status);
311 if (status == CS_END) {
315 consoleCtrl->SetDefaultStyle(wxTextAttr(*wxBLACK));
316 (*consoleCtrl) << str;
317 consoleCtrl->SetInsertionPointEnd();
321 * Sends data to the director
323 void wxbMainFrame::Send(wxString str)
325 ct->Write((const char*)str);
326 typeCtrl->SetValue("");
327 consoleCtrl->SetDefaultStyle(wxTextAttr(*wxRED));
328 (*consoleCtrl) << str;
332 * Used by csprint, which is called by console thread.
334 * In GTK and perhaps X11, only the main thread is allowed to interact with
335 * graphical components, so by firing an event, the main loop will call OnPrint.
337 * Calling OnPrint directly from console thread produces "unexpected async replies".
339 void firePrintEvent(wxString str, int status)
341 wxbPrintObject* po = new wxbPrintObject(str, status);
343 wxbThreadEvent evt(Thread);
344 evt.SetEventPrintObject(po);
346 wxbMainFrame::GetInstance()->AddPendingEvent(evt);
349 wxString csBuffer; /* Temporary buffer for receiving data from console thread */
352 * Called by console thread, this function forwards data line by line and end
353 * signals to the GUI.
355 void csprint(char* str, int status)
358 int len = strlen(str);
359 bool allnewline = true;
360 for (int i = 0; i < len; i++) {
361 if (!(allnewline = (str[i] == '\n')))
366 firePrintEvent(csBuffer << "\n", CS_DATA);
368 for (int i = 1; i < len; i++) {
369 firePrintEvent("\n", status);
373 wxStringTokenizer tkz(str, "\n", wxTOKEN_RET_DELIMS | wxTOKEN_RET_EMPTY | wxTOKEN_RET_EMPTY_ALL);
375 while ( tkz.HasMoreTokens() ) {
376 csBuffer << tkz.GetNextToken();
377 if (csBuffer.Length() != 0) {
378 if ((csBuffer.GetChar(csBuffer.Length()-1) == '\n') ||
379 (csBuffer.GetChar(csBuffer.Length()-1) == '\r')) {
380 firePrintEvent(csBuffer, status);
387 if (csBuffer == "$ ") { // Restore console
388 firePrintEvent(csBuffer, status);
393 if (status == CS_END) {
394 if (csBuffer.Length() != 0) {
395 firePrintEvent(csBuffer, CS_DATA);
398 firePrintEvent("", CS_END);