]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/wx-console/wxbmainframe.cpp
- wxbRestorePanel : improved restore appearance
[bacula/bacula] / bacula / src / wx-console / wxbmainframe.cpp
1 /*
2  *
3  *   Main frame
4  *
5  *    Nicolas Boichat, April 2004
6  *
7  */
8 /*
9    Copyright (C) 2004 Kern Sibbald and John Walker
10
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.
15
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.
20
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.
24  */
25
26 #include "wxbmainframe.h" // class's header file
27
28 #include "wxbrestorepanel.h"
29
30 #include "csprint.h"
31
32 #include "wxwin16x16.xpm"
33
34 #include <wx/arrimpl.cpp>
35
36 // ----------------------------------------------------------------------------
37 // event tables and other macros for wxWindows
38 // ----------------------------------------------------------------------------
39
40 // ----------------------------------------------------------------------------
41 // constants
42 // ----------------------------------------------------------------------------
43
44 // IDs for the controls and the menu commands
45 enum
46 {
47    // menu items
48    Minimal_Quit = 1,
49
50    // it is important for the id corresponding to the "About" command to have
51    // this standard value as otherwise it won't be handled properly under Mac
52    // (where it is special and put into the "Apple" menu)
53    Minimal_About = wxID_ABOUT,
54    TypeText = 2,
55    Thread = 3
56 };
57
58 /*
59  *   wxbTHREAD_EVENT declaration, used by csprint
60  */
61 BEGIN_DECLARE_EVENT_TYPES()
62    DECLARE_EVENT_TYPE(wxbTHREAD_EVENT,       1)
63 END_DECLARE_EVENT_TYPES()
64
65 DEFINE_EVENT_TYPE(wxbTHREAD_EVENT)
66
67 // the event tables connect the wxWindows events with the functions (event
68 // handlers) which process them. It can be also done at run-time, but for the
69 // simple menu events like this the static method is much simpler.
70 BEGIN_EVENT_TABLE(wxbMainFrame, wxFrame)
71    EVT_MENU(Minimal_Quit,  wxbMainFrame::OnQuit)
72    EVT_MENU(Minimal_About, wxbMainFrame::OnAbout)
73    EVT_TEXT_ENTER(TypeText, wxbMainFrame::OnEnter)
74    EVT_CUSTOM(wxbTHREAD_EVENT, Thread, wxbMainFrame::OnPrint)
75 END_EVENT_TABLE()
76
77 // ----------------------------------------------------------------------------
78 // wxbThreadEvent
79 // ----------------------------------------------------------------------------
80
81 /*
82  *  wxbThreadEvent constructor
83  */
84 wxbThreadEvent::wxbThreadEvent(int id): wxEvent(id, wxbTHREAD_EVENT) {
85    m_eventObject = NULL;
86 }
87
88 /*
89  *  wxbThreadEvent destructor
90  */
91 wxbThreadEvent::~wxbThreadEvent()
92 {
93    if (m_eventObject != NULL) {
94       delete m_eventObject;
95    }
96 }
97
98 /*
99  *  wxbThreadEvent copy constructor
100  */
101 wxbThreadEvent::wxbThreadEvent(const wxbThreadEvent& te)
102 {
103    this->m_eventType = te.m_eventType;
104    this->m_id = te.m_id;
105    if (te.m_eventObject != NULL) {
106       this->m_eventObject = new wxbPrintObject(*((wxbPrintObject*)te.m_eventObject));
107    }
108    else {
109       this->m_eventObject = NULL;
110    }
111    this->m_skipped = te.m_skipped;
112    this->m_timeStamp = te.m_timeStamp;
113 }
114
115 /*
116  *  Must be implemented (abstract in wxEvent)
117  */
118 wxEvent* wxbThreadEvent::Clone() const
119 {
120    return new wxbThreadEvent(*this);
121 }
122
123 /*
124  *  Gets the wxbPrintObject attached to this event, containing data sent by director
125  */
126 wxbPrintObject* wxbThreadEvent::GetEventPrintObject()
127 {
128    return (wxbPrintObject*)m_eventObject;
129 }
130
131 /*
132  *  Sets the wxbPrintObject attached to this event
133  */
134 void wxbThreadEvent::SetEventPrintObject(wxbPrintObject* object)
135 {
136    m_eventObject = (wxObject*)object;
137 }
138
139 // ----------------------------------------------------------------------------
140 // main frame
141 // ----------------------------------------------------------------------------
142
143 wxbMainFrame *wxbMainFrame::frame = NULL;
144
145 /*
146  *  Singleton constructor
147  */
148 wxbMainFrame* wxbMainFrame::CreateInstance(const wxString& title, const wxPoint& pos, const wxSize& size, long style)
149 {
150    frame = new wxbMainFrame(title, pos, size, style);
151    return frame;
152 }
153
154 /*
155  *  Returns singleton instance
156  */
157 wxbMainFrame* wxbMainFrame::GetInstance()
158 {
159    return frame;
160 }
161
162 /*
163  *  Private destructor
164  */
165 wxbMainFrame::~wxbMainFrame()
166 {
167    if ((ct != NULL) && (!ct->IsRunning())) {
168       ct->Delete();
169    }
170 }
171
172 /*
173  *  Private constructor
174  */
175 wxbMainFrame::wxbMainFrame(const wxString& title, const wxPoint& pos, const wxSize& size, long style)
176       : wxFrame(NULL, -1, title, pos, size, style)
177 {
178    ct = NULL;
179
180    // set the frame icon
181    SetIcon(wxIcon(wxwin16x16_xpm));
182
183 #if wxUSE_MENUS
184    // create a menu bar
185    wxMenu *menuFile = new wxMenu;
186
187    // the "About" item should be in the help menu
188    wxMenu *helpMenu = new wxMenu;
189    helpMenu->Append(Minimal_About, _T("&About...\tF1"), _T("Show about dialog"));
190
191    menuFile->Append(Minimal_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));
192
193    // now append the freshly created menu to the menu bar...
194    wxMenuBar *menuBar = new wxMenuBar();
195    menuBar->Append(menuFile, _T("&File"));
196    menuBar->Append(helpMenu, _T("&Help"));
197
198    // ... and attach this menu bar to the frame
199    SetMenuBar(menuBar);
200 #endif // wxUSE_MENUS
201
202 #if wxUSE_STATUSBAR
203    // create a status bar just for fun (by default with 1 pane only)
204    CreateStatusBar(2);
205    SetStatusText(_T("Welcome to Bacula wx-console!"));
206 #endif // wxUSE_STATUSBAR
207
208    wxPanel* global = new wxPanel(this, -1);
209
210    notebook = new wxNotebook(global, -1);
211
212    /* Console */
213
214    wxPanel* consolePanel = new wxPanel(notebook, -1);
215    notebook->AddPage(consolePanel, "Console");
216
217    consoleCtrl = new wxTextCtrl(consolePanel,-1,"",wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE | wxTE_READONLY | wxTE_RICH);
218    consoleCtrl->SetDefaultStyle(wxTextAttr(*wxBLACK, wxNullColour, wxFont(10, wxMODERN, wxNORMAL, wxNORMAL)));
219
220    typeCtrl = new wxTextCtrl(consolePanel,TypeText,"",wxDefaultPosition,wxSize(200,20), wxTE_PROCESS_ENTER);
221
222    wxFlexGridSizer *consoleSizer = new wxFlexGridSizer(2, 1, 0, 0);
223    consoleSizer->AddGrowableCol(0);
224    consoleSizer->AddGrowableRow(0);
225
226    consoleSizer->Add(consoleCtrl, 1, wxEXPAND | wxALL, 0);
227    consoleSizer->Add(typeCtrl, 0, wxEXPAND | wxALL, 0);
228
229    consolePanel->SetAutoLayout( TRUE );
230    consolePanel->SetSizer( consoleSizer );
231    consoleSizer->SetSizeHints( consolePanel );
232
233    // Creates the list of panels which are included in notebook, and that need to receive director information
234
235    panels = new wxbPanel* [2];
236    panels[0] = new wxbRestorePanel(notebook);
237    panels[1] = NULL;
238
239    for (int i = 0; panels[i] != NULL; i++) {
240       notebook->AddPage(panels[i], panels[i]->GetTitle());
241    }
242
243    wxBoxSizer* globalSizer = new wxBoxSizer(wxHORIZONTAL);
244
245    globalSizer->Add(new wxNotebookSizer(notebook), 1, wxEXPAND, 0);
246
247    global->SetSizer( globalSizer );
248    globalSizer->SetSizeHints( global );
249
250    wxBoxSizer* sizer = new wxBoxSizer(wxHORIZONTAL);
251
252    sizer->Add(global, 1, wxEXPAND | wxALL, 0);
253    SetAutoLayout(true);
254    SetSizer( sizer );
255    sizer->SetSizeHints( this );
256    this->SetSize(size);
257    EnableConsole(false);
258 }
259
260 /*
261  *  Starts the thread interacting with the director
262  */
263 void wxbMainFrame::StartConsoleThread()
264 {
265    if (ct != NULL) {
266       ct->Delete();
267    }
268    ct = new console_thread();
269    ct->Create();
270    ct->Run();
271 }
272
273 /* Register a new wxbDataParser */
274 void wxbMainFrame::Register(wxbDataParser* dp) {
275    parsers.Add(dp);
276 }
277    
278 /* Unregister a wxbDataParser */
279 void wxbMainFrame::Unregister(wxbDataParser* dp) {
280    int index;
281    if ((index = parsers.Index(dp)) != wxNOT_FOUND) {
282       parsers.RemoveAt(index);
283    }
284    else {
285       Print("Failed to unregister a data parser !", CS_DEBUG);
286    }
287 }
288
289 // event handlers
290
291 void wxbMainFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
292 {
293    // TRUE is to force the frame to close
294    Close(TRUE);
295 }
296
297 void wxbMainFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
298 {
299    wxString msg;
300    msg.Printf( _T("Welcome to Bacula wx-console.\nWritten by Nicolas Boichat <nicolas@boichat.ch>\n(C) 2004 Kern Sibbald and John Walker\n"));
301
302    wxMessageBox(msg, _T("About Bacula wx-console"), wxOK | wxICON_INFORMATION, this);
303 }
304
305 void wxbMainFrame::OnEnter(wxCommandEvent& WXUNUSED(event))
306 {
307    wxString str = typeCtrl->GetValue() + "\n";
308    Send(str);
309 }
310
311 /*
312  *  Called when data is arriving from director
313  */
314 void wxbMainFrame::OnPrint(wxbThreadEvent& event) {
315    wxbPrintObject* po = event.GetEventPrintObject();
316
317    Print(po->str, po->status);
318 }
319
320 /*
321  *  Prints data received from director to the console, and forwards it to the panels
322  */
323 void wxbMainFrame::Print(wxString str, int status)
324 {
325    if (status == CS_CONNECTED) {
326       EnablePanels();
327       return;
328    }
329    if (status == CS_DISCONNECTED) {
330       DisablePanels();
331       return;
332    }
333       
334    // CS_DEBUG is often sent by panels, 
335    // and resend it to them would sometimes cause an infinite loop
336    if (status != CS_DEBUG) {
337       for (unsigned int i = 0; i < parsers.GetCount(); i++) {
338          parsers[i]->Print(str, status);
339        }
340    }
341
342    if (status == CS_END) {
343       str = "#";
344    }
345
346    consoleCtrl->SetDefaultStyle(wxTextAttr(*wxBLACK));
347    (*consoleCtrl) << str;
348    consoleCtrl->SetInsertionPointEnd();
349 }
350
351 /*
352  *  Sends data to the director
353  */
354 void wxbMainFrame::Send(wxString str)
355 {
356    ct->Write((const char*)str);
357    typeCtrl->SetValue("");
358    consoleCtrl->SetDefaultStyle(wxTextAttr(*wxRED));
359    (*consoleCtrl) << str;
360 }
361
362 /* Enable panels */
363 void wxbMainFrame::EnablePanels() {
364    for (int i = 0; panels[i] != NULL; i++) {
365       panels[i]->EnablePanel(true);
366    }
367    EnableConsole(true);
368 }
369
370 /* Disable panels, except the one passed as parameter */
371 void wxbMainFrame::DisablePanels(void* except) {
372    for (int i = 0; panels[i] != NULL; i++) {
373       if (panels[i] != except) {
374          panels[i]->EnablePanel(false);
375       }
376       else {
377          panels[i]->EnablePanel(true);
378       }
379    }
380    if (this != except) {
381       EnableConsole(false);
382    }
383 }
384
385 /* Enable or disable console typing */
386 void wxbMainFrame::EnableConsole(bool enable) {
387    typeCtrl->Enable(enable);
388 }
389
390 /*
391  *  Used by csprint, which is called by console thread.
392  *
393  *  In GTK and perhaps X11, only the main thread is allowed to interact with
394  *  graphical components, so by firing an event, the main loop will call OnPrint.
395  *
396  *  Calling OnPrint directly from console thread produces "unexpected async replies".
397  */
398 void firePrintEvent(wxString str, int status)
399 {
400    wxbPrintObject* po = new wxbPrintObject(str, status);
401
402    wxbThreadEvent evt(Thread);
403    evt.SetEventPrintObject(po);
404
405    wxbMainFrame::GetInstance()->AddPendingEvent(evt);
406 }
407
408 wxString csBuffer; /* Temporary buffer for receiving data from console thread */
409
410 /*
411  *  Called by console thread, this function forwards data line by line and end
412  *  signals to the GUI.
413  */
414 void csprint(char* str, int status)
415 {
416    if (str != 0) {
417       int len = strlen(str);
418       bool allnewline = true;
419       for (int i = 0; i < len; i++) {
420       if (!(allnewline = (str[i] == '\n')))
421       break;
422       }
423
424       if (allnewline) {
425          firePrintEvent(csBuffer << "\n", CS_DATA);
426          csBuffer = "";
427          for (int i = 1; i < len; i++) {
428             firePrintEvent("\n", status);
429          }
430       }
431       else {
432          wxStringTokenizer tkz(str, "\n", 
433             wxTOKEN_RET_DELIMS | wxTOKEN_RET_EMPTY | wxTOKEN_RET_EMPTY_ALL);
434
435          while ( tkz.HasMoreTokens() ) {
436             csBuffer << tkz.GetNextToken();
437             if (csBuffer.Length() != 0) {
438                if ((csBuffer.GetChar(csBuffer.Length()-1) == '\n') ||
439                   (csBuffer.GetChar(csBuffer.Length()-1) == '\r')) {
440                   firePrintEvent(csBuffer, status);
441                   csBuffer = "";
442                }
443             }
444          }
445       }
446
447       if (csBuffer == "$ ") { // Restore console
448          firePrintEvent(csBuffer, status);
449          csBuffer = "";
450       }
451    }
452
453    if (status != CS_DATA) {
454       if (csBuffer.Length() != 0) {
455          firePrintEvent(csBuffer, CS_DATA);
456       }
457       csBuffer = "";
458       firePrintEvent("", status);
459    }
460 }
461