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 // ----------------------------------------------------------------------------
37 // event tables and other macros for wxWindows
38 // ----------------------------------------------------------------------------
40 // ----------------------------------------------------------------------------
42 // ----------------------------------------------------------------------------
44 // IDs for the controls and the menu commands
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,
59 * wxbTHREAD_EVENT declaration, used by csprint
61 BEGIN_DECLARE_EVENT_TYPES()
62 DECLARE_EVENT_TYPE(wxbTHREAD_EVENT, 1)
63 END_DECLARE_EVENT_TYPES()
65 DEFINE_EVENT_TYPE(wxbTHREAD_EVENT)
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)
77 // ----------------------------------------------------------------------------
79 // ----------------------------------------------------------------------------
82 * wxbThreadEvent constructor
84 wxbThreadEvent::wxbThreadEvent(int id): wxEvent(id, wxbTHREAD_EVENT) {
89 * wxbThreadEvent destructor
91 wxbThreadEvent::~wxbThreadEvent()
93 if (m_eventObject != NULL) {
99 * wxbThreadEvent copy constructor
101 wxbThreadEvent::wxbThreadEvent(const wxbThreadEvent& te)
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));
109 this->m_eventObject = NULL;
111 this->m_skipped = te.m_skipped;
112 this->m_timeStamp = te.m_timeStamp;
116 * Must be implemented (abstract in wxEvent)
118 wxEvent* wxbThreadEvent::Clone() const
120 return new wxbThreadEvent(*this);
124 * Gets the wxbPrintObject attached to this event, containing data sent by director
126 wxbPrintObject* wxbThreadEvent::GetEventPrintObject()
128 return (wxbPrintObject*)m_eventObject;
132 * Sets the wxbPrintObject attached to this event
134 void wxbThreadEvent::SetEventPrintObject(wxbPrintObject* object)
136 m_eventObject = (wxObject*)object;
139 // ----------------------------------------------------------------------------
141 // ----------------------------------------------------------------------------
143 wxbMainFrame *wxbMainFrame::frame = NULL;
146 * Singleton constructor
148 wxbMainFrame* wxbMainFrame::CreateInstance(const wxString& title, const wxPoint& pos, const wxSize& size, long style)
150 frame = new wxbMainFrame(title, pos, size, style);
155 * Returns singleton instance
157 wxbMainFrame* wxbMainFrame::GetInstance()
165 wxbMainFrame::~wxbMainFrame()
167 if (ct != NULL) { // && (!ct->IsRunning())
173 * Private constructor
175 wxbMainFrame::wxbMainFrame(const wxString& title, const wxPoint& pos, const wxSize& size, long style)
176 : wxFrame(NULL, -1, title, pos, size, style)
180 // set the frame icon
181 SetIcon(wxIcon(wxwin16x16_xpm));
185 wxMenu *menuFile = new wxMenu;
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"));
191 menuFile->Append(Minimal_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));
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"));
198 // ... and attach this menu bar to the frame
200 #endif // wxUSE_MENUS
204 SetStatusText(wxString("Welcome to bacula wx-console ") << VERSION << " (" << BDATE << ")!\n");
205 #endif // wxUSE_STATUSBAR
207 wxPanel* global = new wxPanel(this, -1);
209 notebook = new wxNotebook(global, -1);
213 wxPanel* consolePanel = new wxPanel(notebook, -1);
214 notebook->AddPage(consolePanel, "Console");
216 consoleCtrl = new wxTextCtrl(consolePanel,-1,"",wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE | wxTE_READONLY | wxTE_RICH);
217 consoleCtrl->SetDefaultStyle(wxTextAttr(*wxBLACK, wxNullColour, wxFont(10, wxMODERN, wxNORMAL, wxNORMAL)));
219 typeCtrl = new wxTextCtrl(consolePanel,TypeText,"",wxDefaultPosition,wxSize(200,20), wxTE_PROCESS_ENTER);
221 wxFlexGridSizer *consoleSizer = new wxFlexGridSizer(2, 1, 0, 0);
222 consoleSizer->AddGrowableCol(0);
223 consoleSizer->AddGrowableRow(0);
225 consoleSizer->Add(consoleCtrl, 1, wxEXPAND | wxALL, 0);
226 consoleSizer->Add(typeCtrl, 0, wxEXPAND | wxALL, 0);
228 consolePanel->SetAutoLayout( TRUE );
229 consolePanel->SetSizer( consoleSizer );
230 consoleSizer->SetSizeHints( consolePanel );
232 // Creates the list of panels which are included in notebook, and that need to receive director information
234 panels = new wxbPanel* [2];
235 panels[0] = new wxbRestorePanel(notebook);
238 for (int i = 0; panels[i] != NULL; i++) {
239 notebook->AddPage(panels[i], panels[i]->GetTitle());
242 wxBoxSizer* globalSizer = new wxBoxSizer(wxHORIZONTAL);
244 globalSizer->Add(new wxNotebookSizer(notebook), 1, wxEXPAND, 0);
246 global->SetSizer( globalSizer );
247 globalSizer->SetSizeHints( global );
249 wxBoxSizer* sizer = new wxBoxSizer(wxHORIZONTAL);
251 sizer->Add(global, 1, wxEXPAND | wxALL, 0);
254 sizer->SetSizeHints( this );
256 EnableConsole(false);
258 lockedbyconsole = false;
262 * Starts the thread interacting with the director
264 void wxbMainFrame::StartConsoleThread()
270 promptparser = new wxbPromptParser();
272 ct = new console_thread();
275 SetStatusText("Connecting to the director...");
278 /* Register a new wxbDataParser */
279 void wxbMainFrame::Register(wxbDataParser* dp) {
283 /* Unregister a wxbDataParser */
284 void wxbMainFrame::Unregister(wxbDataParser* dp) {
286 if ((index = parsers.Index(dp)) != wxNOT_FOUND) {
287 parsers.RemoveAt(index);
290 Print("Failed to unregister a data parser !", CS_DEBUG);
296 void wxbMainFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
298 // TRUE is to force the frame to close
302 void wxbMainFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
305 msg.Printf( _T("Welcome to Bacula wx-console.\nWritten by Nicolas Boichat <nicolas@boichat.ch>\n(C) 2004 Kern Sibbald and John Walker\n"));
307 wxMessageBox(msg, _T("About Bacula wx-console"), wxOK | wxICON_INFORMATION, this);
310 void wxbMainFrame::OnEnter(wxCommandEvent& WXUNUSED(event))
312 lockedbyconsole = true;
314 wxString str = typeCtrl->GetValue() + "\n";
319 * Called when data is arriving from director
321 void wxbMainFrame::OnPrint(wxbThreadEvent& event) {
322 wxbPrintObject* po = event.GetEventPrintObject();
324 Print(po->str, po->status);
328 * Prints data received from director to the console, and forwards it to the panels
330 void wxbMainFrame::Print(wxString str, int status)
332 if (lockedbyconsole) {
333 EnableConsole(false);
336 if (status == CS_TERMINATED) {
337 SetStatusText("Console thread terminated.");
343 if (status == CS_CONNECTED) {
344 SetStatusText("Connected to the director.");
348 if (status == CS_DISCONNECTED) {
349 SetStatusText("Disconnected of the director.");
354 // CS_DEBUG is often sent by panels,
355 // and resend it to them would sometimes cause infinite loops
357 /* One promptcaught is normal, so we must have two true Print values to be
358 * sure that the prompt has effectively been caught.
360 int promptcaught = -1;
362 if (status != CS_DEBUG) {
363 for (unsigned int i = 0; i < parsers.GetCount(); i++) {
364 promptcaught += parsers[i]->Print(str, status) ? 1 : 0;
367 if ((status == CS_PROMPT) && (promptcaught < 1) && (promptparser->isPrompt())) {
368 Print("Unexpected question has been received.\n", CS_DEBUG);
369 // Print(wxString("(") << promptparser->getIntroString() << "/-/" << promptparser->getQuestionString() << ")\n", CS_DEBUG);
372 if (promptparser->getIntroString() != "") {
373 message << promptparser->getIntroString() << "\n";
375 message << promptparser->getQuestionString();
377 if (promptparser->getChoices()) {
378 wxString *choices = new wxString[promptparser->getChoices()->GetCount()];
379 int *numbers = new int[promptparser->getChoices()->GetCount()];
382 for (unsigned int i = 0; i < promptparser->getChoices()->GetCount(); i++) {
383 if ((*promptparser->getChoices())[i] != "") {
384 choices[n] = (*promptparser->getChoices())[i];
390 int res = ::wxGetSingleChoiceIndex(message,
391 "wx-console: unexpected director's question.", n, choices, this);
396 Send(wxString() << numbers[res] << "\n");
400 Send(::wxGetTextFromUser(message,
401 "wx-console: unexpected director's question.", "", this) + "\n");
406 if (status == CS_END) {
407 if (lockedbyconsole) {
409 lockedbyconsole = false;
414 if (status == CS_DEBUG) {
415 consoleCtrl->SetDefaultStyle(wxTextAttr(wxColour(0, 128, 0)));
418 consoleCtrl->SetDefaultStyle(wxTextAttr(*wxBLACK));
420 consoleCtrl->AppendText(str);
421 if (status == CS_PROMPT) {
422 if (lockedbyconsole) {
425 consoleCtrl->AppendText("<P>");
428 consoleCtrl->ScrollLines(3);
430 // consoleCtrl->ShowPosition(consoleCtrl->GetLastPosition());
432 /*if (status != CS_DEBUG) {
433 consoleCtrl->AppendText("@");
435 //consoleCtrl->SetInsertionPointEnd();
437 /* if ((consoleCtrl->GetNumberOfLines()-1) > nlines) {
438 nlines = consoleCtrl->GetNumberOfLines()-1;
441 if (status == CS_END) {
442 consoleCtrl->ShowPosition(nlines);
447 * Sends data to the director
449 void wxbMainFrame::Send(wxString str)
452 ct->Write((const char*)str);
453 typeCtrl->SetValue("");
454 consoleCtrl->SetDefaultStyle(wxTextAttr(*wxRED));
455 consoleCtrl->AppendText(str);
456 consoleCtrl->ScrollLines(3);
459 /* if ((consoleCtrl->GetNumberOfLines()-1) > nlines) {
460 nlines = consoleCtrl->GetNumberOfLines()-1;
463 consoleCtrl->ShowPosition(nlines);*/
467 void wxbMainFrame::EnablePanels() {
468 for (int i = 0; panels[i] != NULL; i++) {
469 panels[i]->EnablePanel(true);
474 /* Disable panels, except the one passed as parameter */
475 void wxbMainFrame::DisablePanels(void* except) {
476 for (int i = 0; panels[i] != NULL; i++) {
477 if (panels[i] != except) {
478 panels[i]->EnablePanel(false);
481 panels[i]->EnablePanel(true);
484 if (this != except) {
485 EnableConsole(false);
489 /* Enable or disable console typing */
490 void wxbMainFrame::EnableConsole(bool enable) {
491 typeCtrl->Enable(enable);
493 typeCtrl->SetFocus();
498 * Used by csprint, which is called by console thread.
500 * In GTK and perhaps X11, only the main thread is allowed to interact with
501 * graphical components, so by firing an event, the main loop will call OnPrint.
503 * Calling OnPrint directly from console thread produces "unexpected async replies".
505 void firePrintEvent(wxString str, int status)
507 wxbPrintObject* po = new wxbPrintObject(str, status);
509 wxbThreadEvent evt(Thread);
510 evt.SetEventPrintObject(po);
512 wxbMainFrame::GetInstance()->AddPendingEvent(evt);
515 //wxString csBuffer; /* Temporary buffer for receiving data from console thread */
518 * Called by console thread, this function forwards data line by line and end
519 * signals to the GUI.
521 void csprint(const char* str, int status)
524 firePrintEvent(wxString(str), status);
527 firePrintEvent("", status);