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 #include <wx/arrimpl.cpp>
36 #include <wx/stattext.h>
38 // ----------------------------------------------------------------------------
39 // event tables and other macros for wxWindows
40 // ----------------------------------------------------------------------------
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
46 // IDs for the controls and the menu commands
52 // it is important for the id corresponding to the "About" command to have
53 // this standard value as otherwise it won't be handled properly under Mac
54 // (where it is special and put into the "Apple" menu)
55 Minimal_About = wxID_ABOUT,
62 * wxbTHREAD_EVENT declaration, used by csprint
64 BEGIN_DECLARE_EVENT_TYPES()
65 DECLARE_EVENT_TYPE(wxbTHREAD_EVENT, 1)
66 END_DECLARE_EVENT_TYPES()
68 DEFINE_EVENT_TYPE(wxbTHREAD_EVENT)
70 // the event tables connect the wxWindows events with the functions (event
71 // handlers) which process them. It can be also done at run-time, but for the
72 // simple menu events like this the static method is much simpler.
73 BEGIN_EVENT_TABLE(wxbMainFrame, wxFrame)
74 EVT_MENU(Minimal_Quit, wxbMainFrame::OnQuit)
75 EVT_MENU(Minimal_About, wxbMainFrame::OnAbout)
76 EVT_TEXT_ENTER(TypeText, wxbMainFrame::OnEnter)
77 EVT_BUTTON(SendButton, wxbMainFrame::OnEnter)
78 EVT_CUSTOM(wxbTHREAD_EVENT, Thread, wxbMainFrame::OnPrint)
81 // ----------------------------------------------------------------------------
83 // ----------------------------------------------------------------------------
86 * wxbThreadEvent constructor
88 wxbThreadEvent::wxbThreadEvent(int id): wxEvent(id, wxbTHREAD_EVENT) {
93 * wxbThreadEvent destructor
95 wxbThreadEvent::~wxbThreadEvent()
97 if (m_eventObject != NULL) {
103 * wxbThreadEvent copy constructor
105 wxbThreadEvent::wxbThreadEvent(const wxbThreadEvent& te)
107 this->m_eventType = te.m_eventType;
108 this->m_id = te.m_id;
109 if (te.m_eventObject != NULL) {
110 this->m_eventObject = new wxbPrintObject(*((wxbPrintObject*)te.m_eventObject));
113 this->m_eventObject = NULL;
115 this->m_skipped = te.m_skipped;
116 this->m_timeStamp = te.m_timeStamp;
120 * Must be implemented (abstract in wxEvent)
122 wxEvent* wxbThreadEvent::Clone() const
124 return new wxbThreadEvent(*this);
128 * Gets the wxbPrintObject attached to this event, containing data sent by director
130 wxbPrintObject* wxbThreadEvent::GetEventPrintObject()
132 return (wxbPrintObject*)m_eventObject;
136 * Sets the wxbPrintObject attached to this event
138 void wxbThreadEvent::SetEventPrintObject(wxbPrintObject* object)
140 m_eventObject = (wxObject*)object;
143 // ----------------------------------------------------------------------------
145 // ----------------------------------------------------------------------------
147 wxbMainFrame *wxbMainFrame::frame = NULL;
150 * Singleton constructor
152 wxbMainFrame* wxbMainFrame::CreateInstance(const wxString& title, const wxPoint& pos, const wxSize& size, long style)
154 frame = new wxbMainFrame(title, pos, size, style);
159 * Returns singleton instance
161 wxbMainFrame* wxbMainFrame::GetInstance()
169 wxbMainFrame::~wxbMainFrame()
171 if (ct != NULL) { // && (!ct->IsRunning())
177 * Private constructor
179 wxbMainFrame::wxbMainFrame(const wxString& title, const wxPoint& pos, const wxSize& size, long style)
180 : wxFrame(NULL, -1, title, pos, size, style)
184 // set the frame icon
185 SetIcon(wxIcon(wxwin16x16_xpm));
189 wxMenu *menuFile = new wxMenu;
191 // the "About" item should be in the help menu
192 wxMenu *helpMenu = new wxMenu;
193 helpMenu->Append(Minimal_About, _T("&About...\tF1"), _T("Show about dialog"));
195 menuFile->Append(Minimal_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));
197 // now append the freshly created menu to the menu bar...
198 wxMenuBar *menuBar = new wxMenuBar();
199 menuBar->Append(menuFile, _T("&File"));
200 menuBar->Append(helpMenu, _T("&Help"));
202 // ... and attach this menu bar to the frame
204 #endif // wxUSE_MENUS
207 SetStatusText(wxString("Welcome to bacula wx-console ") << VERSION << " (" << BDATE << ")!\n");
209 wxPanel* global = new wxPanel(this, -1);
211 notebook = new wxNotebook(global, -1);
215 wxPanel* consolePanel = new wxPanel(notebook, -1);
216 notebook->AddPage(consolePanel, "Console");
218 consoleCtrl = new wxTextCtrl(consolePanel,-1,"",wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE | wxTE_READONLY | wxTE_RICH);
219 consoleCtrl->SetDefaultStyle(wxTextAttr(*wxBLACK, wxNullColour, wxFont(10, wxMODERN, wxNORMAL, wxNORMAL)));
221 wxFlexGridSizer *consoleSizer = new wxFlexGridSizer(2, 1, 0, 0);
222 consoleSizer->AddGrowableCol(0);
223 consoleSizer->AddGrowableRow(0);
225 typeCtrl = new wxTextCtrl(consolePanel,TypeText,"",wxDefaultPosition,wxSize(200,20), wxTE_PROCESS_ENTER);
226 sendButton = new wxButton(consolePanel, SendButton, "Send");
228 wxFlexGridSizer *typeSizer = new wxFlexGridSizer(1, 3, 0, 0);
229 typeSizer->AddGrowableCol(1);
230 typeSizer->AddGrowableRow(0);
232 typeSizer->Add(new wxStaticText(consolePanel, -1, "Command: "), 0, wxALIGN_CENTER | wxALL, 0);
233 typeSizer->Add(typeCtrl, 1, wxEXPAND | wxALL, 0);
234 typeSizer->Add(sendButton, 1, wxEXPAND | wxLEFT, 5);
236 consoleSizer->Add(consoleCtrl, 1, wxEXPAND | wxALL, 0);
237 consoleSizer->Add(typeSizer, 0, wxEXPAND | wxALL, 0);
239 consolePanel->SetAutoLayout( TRUE );
240 consolePanel->SetSizer( consoleSizer );
241 consoleSizer->SetSizeHints( consolePanel );
243 // Creates the list of panels which are included in notebook, and that need to receive director information
245 panels = new wxbPanel* [2];
246 panels[0] = new wxbRestorePanel(notebook);
249 for (int i = 0; panels[i] != NULL; i++) {
250 notebook->AddPage(panels[i], panels[i]->GetTitle());
253 wxBoxSizer* globalSizer = new wxBoxSizer(wxHORIZONTAL);
255 globalSizer->Add(new wxNotebookSizer(notebook), 1, wxEXPAND, 0);
257 global->SetSizer( globalSizer );
258 globalSizer->SetSizeHints( global );
260 wxBoxSizer* sizer = new wxBoxSizer(wxHORIZONTAL);
262 sizer->Add(global, 1, wxEXPAND | wxALL, 0);
265 sizer->SetSizeHints( this );
267 EnableConsole(false);
269 lockedbyconsole = false;
275 * Starts the thread interacting with the director
277 void wxbMainFrame::StartConsoleThread()
283 promptparser = new wxbPromptParser();
285 ct = new console_thread();
288 SetStatusText("Connecting to the director...");
291 /* Register a new wxbDataParser */
292 void wxbMainFrame::Register(wxbDataParser* dp) {
296 /* Unregister a wxbDataParser */
297 void wxbMainFrame::Unregister(wxbDataParser* dp) {
299 if ((index = parsers.Index(dp)) != wxNOT_FOUND) {
300 parsers.RemoveAt(index);
303 Print("Failed to unregister a data parser !", CS_DEBUG);
309 void wxbMainFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
311 // TRUE is to force the frame to close
315 void wxbMainFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
318 msg.Printf( _T("Welcome to Bacula wx-console.\nWritten by Nicolas Boichat <nicolas@boichat.ch>\n(C) 2004 Kern Sibbald and John Walker\n"));
320 wxMessageBox(msg, _T("About Bacula wx-console"), wxOK | wxICON_INFORMATION, this);
323 void wxbMainFrame::OnEnter(wxCommandEvent& WXUNUSED(event))
325 lockedbyconsole = true;
327 wxString str = typeCtrl->GetValue() + "\n";
332 * Called when data is arriving from director
334 void wxbMainFrame::OnPrint(wxbThreadEvent& event) {
335 wxbPrintObject* po = event.GetEventPrintObject();
337 Print(po->str, po->status);
341 * Prints data received from director to the console, and forwards it to the panels
343 void wxbMainFrame::Print(wxString str, int status)
345 if (lockedbyconsole) {
346 EnableConsole(false);
349 if (status == CS_TERMINATED) {
350 consoleCtrl->AppendText(consoleBuffer);
352 SetStatusText("Console thread terminated.");
358 if (status == CS_CONNECTED) {
359 SetStatusText("Connected to the director.");
363 if (status == CS_DISCONNECTED) {
364 consoleCtrl->AppendText(consoleBuffer);
366 SetStatusText("Disconnected of the director.");
371 // CS_DEBUG is often sent by panels,
372 // and resend it to them would sometimes cause infinite loops
374 /* One promptcaught is normal, so we must have two true Print values to be
375 * sure that the prompt has effectively been caught.
377 int promptcaught = -1;
379 if (status != CS_DEBUG) {
380 for (unsigned int i = 0; i < parsers.GetCount(); i++) {
381 promptcaught += parsers[i]->Print(str, status) ? 1 : 0;
384 if ((status == CS_PROMPT) && (promptcaught < 1) && (promptparser->isPrompt())) {
385 Print("Unexpected question has been received.\n", CS_DEBUG);
386 // Print(wxString("(") << promptparser->getIntroString() << "/-/" << promptparser->getQuestionString() << ")\n", CS_DEBUG);
389 if (promptparser->getIntroString() != "") {
390 message << promptparser->getIntroString() << "\n";
392 message << promptparser->getQuestionString();
394 if (promptparser->getChoices()) {
395 wxString *choices = new wxString[promptparser->getChoices()->GetCount()];
396 int *numbers = new int[promptparser->getChoices()->GetCount()];
399 for (unsigned int i = 0; i < promptparser->getChoices()->GetCount(); i++) {
400 if ((*promptparser->getChoices())[i] != "") {
401 choices[n] = (*promptparser->getChoices())[i];
407 int res = ::wxGetSingleChoiceIndex(message,
408 "wx-console: unexpected director's question.", n, choices, this);
413 Send(wxString() << numbers[res] << "\n");
417 Send(::wxGetTextFromUser(message,
418 "wx-console: unexpected director's question.", "", this) + "\n");
423 if (status == CS_END) {
424 if (lockedbyconsole) {
426 lockedbyconsole = false;
431 if (status == CS_DEBUG) {
432 consoleCtrl->AppendText(consoleBuffer);
434 consoleCtrl->SetDefaultStyle(wxTextAttr(wxColour(0, 128, 0)));
437 consoleCtrl->SetDefaultStyle(wxTextAttr(*wxBLACK));
439 consoleBuffer << str;
440 if (status == CS_PROMPT) {
441 if (lockedbyconsole) {
444 consoleBuffer << "<P>";
447 if ((status == CS_END) || (status == CS_PROMPT) || (str.Find("\n") > -1)) {
448 consoleCtrl->AppendText(consoleBuffer);
451 consoleCtrl->ScrollLines(3);
454 // consoleCtrl->ShowPosition(consoleCtrl->GetLastPosition());
456 /*if (status != CS_DEBUG) {
457 consoleCtrl->AppendText("@");
459 //consoleCtrl->SetInsertionPointEnd();
461 /* if ((consoleCtrl->GetNumberOfLines()-1) > nlines) {
462 nlines = consoleCtrl->GetNumberOfLines()-1;
465 if (status == CS_END) {
466 consoleCtrl->ShowPosition(nlines);
471 * Sends data to the director
473 void wxbMainFrame::Send(wxString str)
476 ct->Write((const char*)str);
477 typeCtrl->SetValue("");
478 consoleCtrl->SetDefaultStyle(wxTextAttr(*wxRED));
479 consoleCtrl->AppendText(str);
480 consoleCtrl->ScrollLines(3);
483 /* if ((consoleCtrl->GetNumberOfLines()-1) > nlines) {
484 nlines = consoleCtrl->GetNumberOfLines()-1;
487 consoleCtrl->ShowPosition(nlines);*/
491 void wxbMainFrame::EnablePanels() {
492 for (int i = 0; panels[i] != NULL; i++) {
493 panels[i]->EnablePanel(true);
498 /* Disable panels, except the one passed as parameter */
499 void wxbMainFrame::DisablePanels(void* except) {
500 for (int i = 0; panels[i] != NULL; i++) {
501 if (panels[i] != except) {
502 panels[i]->EnablePanel(false);
505 panels[i]->EnablePanel(true);
508 if (this != except) {
509 EnableConsole(false);
513 /* Enable or disable console typing */
514 void wxbMainFrame::EnableConsole(bool enable) {
515 typeCtrl->Enable(enable);
517 typeCtrl->SetFocus();
522 * Used by csprint, which is called by console thread.
524 * In GTK and perhaps X11, only the main thread is allowed to interact with
525 * graphical components, so by firing an event, the main loop will call OnPrint.
527 * Calling OnPrint directly from console thread produces "unexpected async replies".
529 void firePrintEvent(wxString str, int status)
531 wxbPrintObject* po = new wxbPrintObject(str, status);
533 wxbThreadEvent evt(Thread);
534 evt.SetEventPrintObject(po);
536 wxbMainFrame::GetInstance()->AddPendingEvent(evt);
539 //wxString csBuffer; /* Temporary buffer for receiving data from console thread */
542 * Called by console thread, this function forwards data line by line and end
543 * signals to the GUI.
545 void csprint(const char* str, int status)
548 firePrintEvent(wxString(str), status);
551 firePrintEvent("", status);