]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/wx-console/wxbmainframe.cpp
Add wx-console directory
[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 // ----------------------------------------------------------------------------
35 // event tables and other macros for wxWindows
36 // ----------------------------------------------------------------------------
37
38 // ----------------------------------------------------------------------------
39 // constants
40 // ----------------------------------------------------------------------------
41
42 // IDs for the controls and the menu commands
43 enum
44 {
45    // menu items
46    Minimal_Quit = 1,
47
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,
52    TypeText = 2,
53    Thread = 3
54 };
55
56 /*
57  *   wxbTHREAD_EVENT declaration, used by csprint
58  */
59 BEGIN_DECLARE_EVENT_TYPES()
60    DECLARE_EVENT_TYPE(wxbTHREAD_EVENT,       1)
61 END_DECLARE_EVENT_TYPES()
62
63 DEFINE_EVENT_TYPE(wxbTHREAD_EVENT)
64
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)
73 END_EVENT_TABLE()
74
75 // ----------------------------------------------------------------------------
76 // wxbThreadEvent
77 // ----------------------------------------------------------------------------
78
79 /*
80  *  wxbThreadEvent constructor
81  */
82 wxbThreadEvent::wxbThreadEvent(int id): wxEvent(id, wxbTHREAD_EVENT) {
83    m_eventObject = NULL;
84 }
85
86 /*
87  *  wxbThreadEvent destructor
88  */
89 wxbThreadEvent::~wxbThreadEvent()
90 {
91    if (m_eventObject != NULL) {
92       delete m_eventObject;
93    }
94 }
95
96 /*
97  *  wxbThreadEvent copy constructor
98  */
99 wxbThreadEvent::wxbThreadEvent(const wxbThreadEvent& te)
100 {
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));
105    }
106    else {
107       this->m_eventObject = NULL;
108    }
109    this->m_skipped = te.m_skipped;
110    this->m_timeStamp = te.m_timeStamp;
111 }
112
113 /*
114  *  Must be implemented (abstract in wxEvent)
115  */
116 wxEvent* wxbThreadEvent::Clone() const
117 {
118    return new wxbThreadEvent(*this);
119 }
120
121 /*
122  *  Gets the wxbPrintObject attached to this event, containing data sent by director
123  */
124 wxbPrintObject* wxbThreadEvent::GetEventPrintObject()
125 {
126    return (wxbPrintObject*)m_eventObject;
127 }
128
129 /*
130  *  Sets the wxbPrintObject attached to this event
131  */
132 void wxbThreadEvent::SetEventPrintObject(wxbPrintObject* object)
133 {
134    m_eventObject = (wxObject*)object;
135 }
136
137 // ----------------------------------------------------------------------------
138 // main frame
139 // ----------------------------------------------------------------------------
140
141 wxbMainFrame *wxbMainFrame::frame = NULL;
142
143 /*
144  *  Singleton constructor
145  */
146 wxbMainFrame* wxbMainFrame::CreateInstance(const wxString& title, const wxPoint& pos, const wxSize& size, long style)
147 {
148    frame = new wxbMainFrame(title, pos, size, style);
149    return frame;
150 }
151
152 /*
153  *  Returns singleton instance
154  */
155 wxbMainFrame* wxbMainFrame::GetInstance()
156 {
157    return frame;
158 }
159
160 /*
161  *  Private destructor
162  */
163 wxbMainFrame::~wxbMainFrame()
164 {
165    ct->Delete();
166 }
167
168 /*
169  *  Private constructor
170  */
171 wxbMainFrame::wxbMainFrame(const wxString& title, const wxPoint& pos, const wxSize& size, long style)
172       : wxFrame(NULL, -1, title, pos, size, style)
173 {
174    ct = NULL;
175
176    // set the frame icon
177    SetIcon(wxIcon(wxwin16x16_xpm));
178
179 #if wxUSE_MENUS
180    // create a menu bar
181    wxMenu *menuFile = new wxMenu;
182
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"));
186
187    menuFile->Append(Minimal_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));
188
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"));
193
194    // ... and attach this menu bar to the frame
195    SetMenuBar(menuBar);
196 #endif // wxUSE_MENUS
197
198 #if wxUSE_STATUSBAR
199    // create a status bar just for fun (by default with 1 pane only)
200    CreateStatusBar(2);
201    SetStatusText(_T("Welcome to Bacula wx-GUI!"));
202 #endif // wxUSE_STATUSBAR
203
204    wxPanel* global = new wxPanel(this, -1);
205
206    notebook = new wxNotebook(global, -1);
207
208    /* Console */
209
210    wxPanel* consolePanel = new wxPanel(notebook, -1);
211    notebook->AddPage(consolePanel, "Console");
212
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)));
215
216    typeCtrl = new wxTextCtrl(consolePanel,TypeText,"",wxDefaultPosition,wxSize(200,20), wxTE_PROCESS_ENTER);
217
218    wxFlexGridSizer *consoleSizer = new wxFlexGridSizer(2, 1, 0, 0);
219    consoleSizer->AddGrowableCol(0);
220    consoleSizer->AddGrowableRow(0);
221
222    consoleSizer->Add(consoleCtrl, 1, wxEXPAND | wxALL, 0);
223    consoleSizer->Add(typeCtrl, 0, wxEXPAND | wxALL, 0);
224
225    consolePanel->SetAutoLayout( TRUE );
226    consolePanel->SetSizer( consoleSizer );
227    consoleSizer->SetSizeHints( consolePanel );
228
229    // Creates the list of panels which are included in notebook, and that need to receive director information
230
231    panels = new (wxbPanel*)[2];
232    panels[0] = new wxbRestorePanel(notebook);
233    panels[1] = NULL;
234
235    for (int i = 0; panels[i] != NULL; i++) {
236       notebook->AddPage(panels[i], panels[i]->GetTitle());
237    }
238
239    wxBoxSizer* globalSizer = new wxBoxSizer(wxHORIZONTAL);
240
241    globalSizer->Add(new wxNotebookSizer(notebook), 1, wxEXPAND, 0);
242
243    global->SetSizer( globalSizer );
244    globalSizer->SetSizeHints( global );
245
246    wxBoxSizer* sizer = new wxBoxSizer(wxHORIZONTAL);
247
248    sizer->Add(global, 1, wxEXPAND | wxALL, 0);
249    SetAutoLayout(true);
250    SetSizer( sizer );
251    //sizer->SetSizeHints( this );
252 }
253
254 /*
255  *  Starts the thread interacting with the director
256  */
257 void wxbMainFrame::StartConsoleThread()
258 {
259    if (ct != NULL) {
260       ct->Delete();
261    }
262    ct = new console_thread();
263    ct->Create();
264    ct->Run();
265 }
266
267 // event handlers
268
269 void wxbMainFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
270 {
271    // TRUE is to force the frame to close
272    Close(TRUE);
273 }
274
275 void wxbMainFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
276 {
277    wxString msg;
278    msg.Printf( _T("Welcome to Bacula wx-GUI.\n (c) 2004 Nicolas Boichat <nicolas@boichat.ch>\n")
279             _T("Version : %s"), wxVERSION_STRING);
280
281    wxMessageBox(msg, _T("About Bacula-wx-GUI"), wxOK | wxICON_INFORMATION, this);
282 }
283
284 void wxbMainFrame::OnEnter(wxCommandEvent& WXUNUSED(event))
285 {
286    wxString str = typeCtrl->GetValue() + "\n";
287    Send(str);
288 }
289
290 /*
291  *  Called when data is arriving from director
292  */
293 void wxbMainFrame::OnPrint(wxbThreadEvent& event) {
294    wxbPrintObject* po = event.GetEventPrintObject();
295
296    Print(po->str, po->status);
297 }
298
299 /*
300  *  Prints data received from director to the console, and forwards it to the panels
301  */
302 void wxbMainFrame::Print(wxString str, int status)
303 {
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);
308        }
309    }
310
311    if (status == CS_END) {
312       str = "#";
313    }
314
315    consoleCtrl->SetDefaultStyle(wxTextAttr(*wxBLACK));
316    (*consoleCtrl) << str;
317    consoleCtrl->SetInsertionPointEnd();
318 }
319
320 /*
321  *  Sends data to the director
322  */
323 void wxbMainFrame::Send(wxString str)
324 {
325    ct->Write((const char*)str);
326    typeCtrl->SetValue("");
327    consoleCtrl->SetDefaultStyle(wxTextAttr(*wxRED));
328    (*consoleCtrl) << str;
329 }
330
331 /*
332  *  Used by csprint, which is called by console thread.
333  *
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.
336  *
337  *  Calling OnPrint directly from console thread produces "unexpected async replies".
338  */
339 void firePrintEvent(wxString str, int status)
340 {
341    wxbPrintObject* po = new wxbPrintObject(str, status);
342
343    wxbThreadEvent evt(Thread);
344    evt.SetEventPrintObject(po);
345
346    wxbMainFrame::GetInstance()->AddPendingEvent(evt);
347 }
348
349 wxString csBuffer; /* Temporary buffer for receiving data from console thread */
350
351 /*
352  *  Called by console thread, this function forwards data line by line and end
353  *  signals to the GUI.
354  */
355 void csprint(char* str, int status)
356 {
357    if (str != 0) {
358       int len = strlen(str);
359       bool allnewline = true;
360       for (int i = 0; i < len; i++) {
361       if (!(allnewline = (str[i] == '\n')))
362       break;
363       }
364
365       if (allnewline) {
366          firePrintEvent(csBuffer << "\n", CS_DATA);
367          csBuffer = "";
368          for (int i = 1; i < len; i++) {
369             firePrintEvent("\n", status);
370          }
371       }
372       else {
373          wxStringTokenizer tkz(str, "\n", wxTOKEN_RET_DELIMS | wxTOKEN_RET_EMPTY | wxTOKEN_RET_EMPTY_ALL);
374
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);
381                   csBuffer = "";
382                }
383             }
384          }
385       }
386
387       if (csBuffer == "$ ") { // Restore console
388          firePrintEvent(csBuffer, status);
389          csBuffer = "";
390       }
391    }
392
393    if (status == CS_END) {
394       if (csBuffer.Length() != 0) {
395          firePrintEvent(csBuffer, CS_DATA);
396       }
397       csBuffer = "";
398       firePrintEvent("", CS_END);
399    }
400 }
401