]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/wx-console/wxbmainframe.cpp
Fix conio.h problem on Solaris
[bacula/bacula] / bacula / src / wx-console / wxbmainframe.cpp
1 /*
2  *
3  *   Main frame
4  *
5  *    Nicolas Boichat, July 2004
6  *
7  *    Version $Id$
8  */
9 /*
10    Copyright (C) 2004 Kern Sibbald and John Walker
11
12    This program is free software; you can redistribute it and/or
13    modify it under the terms of the GNU General Public License
14    as published by the Free Software Foundation; either version 2
15    of the License, or (at your option) any later version.
16
17    This program is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20    GNU General Public License for more details.
21
22    You should have received a copy of the GNU General Public License
23    along with this program; if not, write to the Free Software
24    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25  */
26
27 #include "wxbmainframe.h" // class's header file
28
29 #include "wxbrestorepanel.h"
30
31 #include "wxbconfigfileeditor.h"
32
33 #include "csprint.h"
34
35 #include "wxwin16x16.xpm"
36
37 #include <wx/arrimpl.cpp>
38
39 #include <wx/stattext.h>
40 #include <wx/statline.h>
41 #include <wx/config.h>
42
43 #include <wx/filename.h>
44
45 #undef Yield /* MinGW defines Yield */
46
47 // ----------------------------------------------------------------------------
48 // event tables and other macros for wxWindows
49 // ----------------------------------------------------------------------------
50
51 // ----------------------------------------------------------------------------
52 // constants
53 // ----------------------------------------------------------------------------
54
55 // IDs for the controls and the menu commands
56 enum
57 {
58    // menu items
59    Minimal_Quit = 1,
60
61    // it is important for the id corresponding to the "About" command to have
62    // this standard value as otherwise it won't be handled properly under Mac
63    // (where it is special and put into the "Apple" menu)
64    Minimal_About = wxID_ABOUT,
65    
66    ChangeConfigFile = 2,
67    EditConfigFile = 3,
68    MenuConnect = 4,
69    MenuDisconnect = 5,
70    TypeText = 6,
71    SendButton = 7,
72    Thread = 8
73 };
74
75 /*
76  *   wxbTHREAD_EVENT declaration, used by csprint
77  */
78 BEGIN_DECLARE_EVENT_TYPES()
79    DECLARE_EVENT_TYPE(wxbTHREAD_EVENT,       1)
80 END_DECLARE_EVENT_TYPES()
81
82 DEFINE_EVENT_TYPE(wxbTHREAD_EVENT)
83
84 typedef void (wxEvtHandler::*wxThreadEventFunction)(wxbThreadEvent&);
85
86 #define EVT_THREAD_EVENT(id, fn) \
87     DECLARE_EVENT_TABLE_ENTRY( \
88         wxbTHREAD_EVENT, id, wxID_ANY, \
89         (wxObjectEventFunction)(wxEventFunction)(wxThreadEventFunction)&fn, \
90         (wxObject *) NULL \
91     ),
92
93 // the event tables connect the wxWindows events with the functions (event
94 // handlers) which process them. It can be also done at run-time, but for the
95 // simple menu events like this the static method is much simpler.
96 BEGIN_EVENT_TABLE(wxbMainFrame, wxFrame)
97    EVT_MENU(Minimal_Quit,  wxbMainFrame::OnQuit)
98    EVT_MENU(Minimal_About, wxbMainFrame::OnAbout)
99    EVT_MENU(ChangeConfigFile, wxbMainFrame::OnChangeConfig)
100    EVT_MENU(EditConfigFile, wxbMainFrame::OnEditConfig)
101    EVT_MENU(MenuConnect, wxbMainFrame::OnConnect)
102    EVT_MENU(MenuDisconnect, wxbMainFrame::OnDisconnect)
103    EVT_TEXT_ENTER(TypeText, wxbMainFrame::OnEnter)
104    EVT_THREAD_EVENT(Thread, wxbMainFrame::OnPrint)
105    EVT_BUTTON(SendButton, wxbMainFrame::OnEnter)
106 END_EVENT_TABLE()
107
108 // ----------------------------------------------------------------------------
109 // wxbThreadEvent
110 // ----------------------------------------------------------------------------
111
112 /*
113  *  wxbThreadEvent constructor
114  */
115 wxbThreadEvent::wxbThreadEvent(int id): wxEvent(id, wxbTHREAD_EVENT) {
116    m_eventObject = NULL;
117 }
118
119 /*
120  *  wxbThreadEvent destructor
121  */
122 wxbThreadEvent::~wxbThreadEvent()
123 {
124    if (m_eventObject != NULL) {
125       delete m_eventObject;
126    }
127 }
128
129 /*
130  *  wxbThreadEvent copy constructor
131  */
132 wxbThreadEvent::wxbThreadEvent(const wxbThreadEvent& te)
133 {
134    this->m_eventType = te.m_eventType;
135    this->m_id = te.m_id;
136    if (te.m_eventObject != NULL) {
137       this->m_eventObject = new wxbPrintObject(*((wxbPrintObject*)te.m_eventObject));
138    }
139    else {
140       this->m_eventObject = NULL;
141    }
142    this->m_skipped = te.m_skipped;
143    this->m_timeStamp = te.m_timeStamp;
144 }
145
146 /*
147  *  Must be implemented (abstract in wxEvent)
148  */
149 wxEvent* wxbThreadEvent::Clone() const
150 {
151    return new wxbThreadEvent(*this);
152 }
153
154 /*
155  *  Gets the wxbPrintObject attached to this event, containing data sent by director
156  */
157 wxbPrintObject* wxbThreadEvent::GetEventPrintObject()
158 {
159    return (wxbPrintObject*)m_eventObject;
160 }
161
162 /*
163  *  Sets the wxbPrintObject attached to this event
164  */
165 void wxbThreadEvent::SetEventPrintObject(wxbPrintObject* object)
166 {
167    m_eventObject = (wxObject*)object;
168 }
169
170 // ----------------------------------------------------------------------------
171 // main frame
172 // ----------------------------------------------------------------------------
173
174 wxbMainFrame *wxbMainFrame::frame = NULL;
175
176 /*
177  *  Singleton constructor
178  */
179 wxbMainFrame* wxbMainFrame::CreateInstance(const wxString& title, const wxPoint& pos, const wxSize& size, long style)
180 {
181    frame = new wxbMainFrame(title, pos, size, style);
182    return frame;
183 }
184
185 /*
186  *  Returns singleton instance
187  */
188 wxbMainFrame* wxbMainFrame::GetInstance()
189 {
190    return frame;
191 }
192
193 /*
194  *  Private destructor
195  */
196 wxbMainFrame::~wxbMainFrame()
197 {
198    if (ct != NULL) { // && (!ct->IsRunning())
199       ct->Delete();
200    }
201    frame = NULL;
202 }
203
204 /*
205  *  Private constructor
206  */
207 wxbMainFrame::wxbMainFrame(const wxString& title, const wxPoint& pos, const wxSize& size, long style)
208       : wxFrame(NULL, -1, title, pos, size, style)
209 {
210    ct = NULL;
211    
212    promptparser = NULL;
213
214    // set the frame icon
215    SetIcon(wxIcon(wxwin16x16_xpm));
216
217 #if wxUSE_MENUS
218    // create a menu bar
219    menuFile = new wxMenu;
220
221    // the "About" item should be in the help menu
222    wxMenu *helpMenu = new wxMenu;
223    helpMenu->Append(Minimal_About, _T("&About...\tF1"), _T("Show about dialog"));
224
225    menuFile->Append(MenuConnect, _T("Connect"), _T("Connect to the director"));
226    menuFile->Append(MenuDisconnect, _T("Disconnect"), _T("Disconnect of the director"));
227    menuFile->AppendSeparator();
228    menuFile->Append(ChangeConfigFile, _T("Change of configuration file"), _T("Change your default configuration file"));
229    menuFile->Append(EditConfigFile, _T("Edit your configuration file"), _T("Edit your configuration file"));
230    menuFile->AppendSeparator();
231    menuFile->Append(Minimal_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));
232
233    // now append the freshly created menu to the menu bar...
234    wxMenuBar *menuBar = new wxMenuBar();
235    menuBar->Append(menuFile, _T("&File"));
236    menuBar->Append(helpMenu, _T("&Help"));
237
238    // ... and attach this menu bar to the frame
239    SetMenuBar(menuBar);
240 #endif // wxUSE_MENUS
241
242    CreateStatusBar(1);
243    SetStatusText(wxString("Welcome to bacula wx-console ") << VERSION << " (" << BDATE << ")!\n");
244
245    wxPanel* global = new wxPanel(this, -1);
246
247    notebook = new wxNotebook(global, -1);
248
249    /* Console */
250
251    wxPanel* consolePanel = new wxPanel(notebook, -1);
252    notebook->AddPage(consolePanel, "Console");
253
254    consoleCtrl = new wxTextCtrl(consolePanel,-1,"",wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE | wxTE_READONLY | wxTE_RICH);
255    wxFont font(10, wxMODERN, wxNORMAL, wxNORMAL);
256 #if defined __WXGTK12__ && !defined __WXGTK20__ // Fix for "chinese" fonts under gtk+ 1.2
257    font.SetDefaultEncoding(wxFONTENCODING_ISO8859_1);
258    consoleCtrl->SetDefaultStyle(wxTextAttr(*wxBLACK, wxNullColour, font));
259    Print("Warning : Unicode is disabled because you are using wxWidgets for GTK+ 1.2.\n", CS_DEBUG);
260 #else
261    consoleCtrl->SetDefaultStyle(wxTextAttr(*wxBLACK, wxNullColour, font));
262 #endif
263
264    helpCtrl = new wxStaticText(consolePanel, -1, "Type your command below:");
265
266    wxFlexGridSizer *consoleSizer = new wxFlexGridSizer(4, 1, 0, 0);
267    consoleSizer->AddGrowableCol(0);
268    consoleSizer->AddGrowableRow(0);
269
270    typeCtrl = new wxbHistoryTextCtrl(helpCtrl, consolePanel,TypeText,"",wxDefaultPosition,wxSize(200,20));
271    sendButton = new wxButton(consolePanel, SendButton, "Send");
272    
273    wxFlexGridSizer *typeSizer = new wxFlexGridSizer(1, 2, 0, 0);
274    typeSizer->AddGrowableCol(0);
275    typeSizer->AddGrowableRow(0);
276
277    //typeSizer->Add(new wxStaticText(consolePanel, -1, "Command: "), 0, wxALIGN_CENTER | wxALL, 0);
278    typeSizer->Add(typeCtrl, 1, wxEXPAND | wxALL, 0);
279    typeSizer->Add(sendButton, 1, wxEXPAND | wxLEFT, 5);
280
281    consoleSizer->Add(consoleCtrl, 1, wxEXPAND | wxALL, 0);
282    consoleSizer->Add(new wxStaticLine(consolePanel, -1), 0, wxEXPAND | wxALL, 0);
283    consoleSizer->Add(helpCtrl, 1, wxEXPAND | wxALL, 2);
284    consoleSizer->Add(typeSizer, 0, wxEXPAND | wxALL, 2);
285
286    consolePanel->SetAutoLayout( TRUE );
287    consolePanel->SetSizer( consoleSizer );
288    consoleSizer->SetSizeHints( consolePanel );
289
290    // Creates the list of panels which are included in notebook, and that need to receive director information
291
292    panels = new wxbPanel* [2];
293    panels[0] = new wxbRestorePanel(notebook);
294    panels[1] = NULL;
295
296    for (int i = 0; panels[i] != NULL; i++) {
297       notebook->AddPage(panels[i], panels[i]->GetTitle());
298    }
299
300    wxBoxSizer* globalSizer = new wxBoxSizer(wxHORIZONTAL);
301
302    globalSizer->Add(new wxNotebookSizer(notebook), 1, wxEXPAND, 0);
303
304    global->SetSizer( globalSizer );
305    globalSizer->SetSizeHints( global );
306
307    wxBoxSizer* sizer = new wxBoxSizer(wxHORIZONTAL);
308
309    sizer->Add(global, 1, wxEXPAND | wxALL, 0);
310    SetAutoLayout(true);
311    SetSizer( sizer );
312    sizer->SetSizeHints( this );
313    this->SetSize(size);
314    EnableConsole(false);
315    
316    lockedbyconsole = false;
317    
318    consoleBuffer = "";
319    
320    configfile = "";
321 }
322
323 /*
324  *  Starts the thread interacting with the director
325  *  If config is not empty, uses this config file.
326  */
327 void wxbMainFrame::StartConsoleThread(const wxString& config) {
328    menuFile->Enable(MenuConnect, false);
329    menuFile->Enable(MenuDisconnect, false);
330    menuFile->Enable(ChangeConfigFile, false);
331    menuFile->Enable(EditConfigFile, false);
332
333    if (ct != NULL) {
334       ct->Delete();
335       ct = NULL;
336       wxTheApp->Yield();
337    }
338    if (promptparser == NULL) {
339       promptparser = new wxbPromptParser();      
340    }
341    
342    if (config == "") {   
343       if ((wxTheApp->argc == 3) && (wxString(wxTheApp->argv[1]) == "-c")) {
344          configfile = wxTheApp->argv[2];
345       }
346       else {
347          wxConfig::Set(new wxConfig("wx-console", "bacula"));
348          if (!wxConfig::Get()->Read("/ConfigFile", &configfile)) {
349 #ifdef HAVE_MACOSX
350             wxFileName filename(::wxGetHomeDir());
351             filename.MakeAbsolute();
352             configfile = filename.GetLongPath();
353             if (configfile.Last() != '/')
354                configfile += '/';
355             configfile += "Library/Preferences/org.bacula.wxconsole.conf";
356 #else
357             wxFileName filename(::wxGetCwd(), "wx-console.conf");
358             filename.MakeAbsolute();
359             configfile = filename.GetLongPath();
360 #ifdef HAVE_WIN32
361             configfile.Replace("\\", "/");
362 #endif //HAVE_WIN32
363 #endif //HAVE_MACOSX
364             wxConfig::Get()->Write("/ConfigFile", configfile);
365             if (wxTheApp->argc > 1) {
366                Print("Error while parsing command line arguments, using defaults.\n", CS_DEBUG);
367                Print("Usage: wx-console [-c configfile]\n", CS_DEBUG);
368             }
369    
370             int answer = wxMessageBox(
371                               wxString("It seems that it is the first time you run wx-console.\n") <<
372                                  "This file (" << configfile << ") has been choosen as default configuration file.\n" << 
373                                  "Do you want to edit it? (if you click No you will have to select another file)",
374                               "First run",
375                               wxYES_NO | wxICON_QUESTION, this);
376             if (answer == wxYES) {
377                wxbConfigFileEditor(this, configfile).ShowModal();
378             }
379          }
380       }
381    }
382    else {
383       configfile = config;
384    }
385    
386    wxString err = console_thread::LoadConfig(configfile);
387    
388    while (err != "") {
389       int answer = wxMessageBox(
390                         wxString("Unable to read ") << configfile << "\n" << 
391                            err << "\nDo you want to choose another one? (Press no to edit this file)",
392                         "Unable to read configuration file",
393                         wxYES_NO | wxCANCEL | wxICON_ERROR, this);
394       if (answer == wxNO) {
395          wxbConfigFileEditor(this, configfile).ShowModal();
396          err = console_thread::LoadConfig(configfile);
397       }
398       else if (answer == wxCANCEL) {
399          frame = NULL;
400          Close(true);
401          return;
402       }
403       else { // (answer == wxYES)
404          configfile = wxFileSelector("Please choose a configuration file to use");
405          if ( !configfile.empty() ) {
406             err = console_thread::LoadConfig(configfile);
407          }
408          else {
409             frame = NULL;
410             Close(true);
411             return;
412          }
413       }
414       
415       if ((err == "") && (config == "")) {
416          answer = wxMessageBox(
417                            "This configuration file has been successfully read, use it as default?",
418                            "Configuration file read successfully",
419                            wxYES_NO | wxICON_QUESTION, this);
420          if (answer == wxYES) {
421               wxConfigBase::Get()->Write("/ConfigFile", configfile);
422          }
423          break;
424       }
425    }
426    
427    csprint(wxString("Using this configuration file: ") << configfile << "\n", CS_DEBUG);
428    
429    ct = new console_thread();
430    ct->Create();
431    ct->Run();
432    SetStatusText("Connecting to the director...");
433 }
434
435 /* Register a new wxbDataParser */
436 void wxbMainFrame::Register(wxbDataParser* dp) {
437    parsers.Add(dp);
438 }
439    
440 /* Unregister a wxbDataParser */
441 void wxbMainFrame::Unregister(wxbDataParser* dp) {
442    int index;
443    if ((index = parsers.Index(dp)) != wxNOT_FOUND) {
444       parsers.RemoveAt(index);
445    }
446    else {
447       Print("Failed to unregister a data parser !", CS_DEBUG);
448    }
449 }
450
451 // event handlers
452
453 void wxbMainFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
454 {
455    Print("Quitting.\n", CS_DEBUG);
456    if (ct != NULL) {
457       ct->Delete();
458       ct = NULL;
459       wxTheApp->Yield();
460    }
461    console_thread::FreeLib();
462    frame = NULL;
463    wxTheApp->Yield();
464    Close(TRUE);
465 }
466
467 void wxbMainFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
468 {
469    wxString msg;
470    msg.Printf( _T("Welcome to Bacula wx-console.\nWritten by Nicolas Boichat <nicolas@boichat.ch>\n(C) 2004 Kern Sibbald and John Walker\n"));
471
472    wxMessageBox(msg, _T("About Bacula wx-console"), wxOK | wxICON_INFORMATION, this);
473 }
474
475 void wxbMainFrame::OnChangeConfig(wxCommandEvent& event) {
476    wxString oriconfigfile;
477    wxConfig::Get()->Read("/ConfigFile", &oriconfigfile);
478    wxString configfile = wxFileSelector("Please choose your default configuration file");
479    if ( !configfile.empty() ) {
480       if (oriconfigfile != configfile) {
481          int answer = wxMessageBox(
482                            "Use this configuration file as default?",
483                            "Configuration file",
484                            wxYES_NO | wxICON_QUESTION, this);
485          if (answer == wxYES) {
486               wxConfigBase::Get()->Write("/ConfigFile", configfile);
487               wxConfigBase::Get()->Flush();
488               StartConsoleThread("");
489               return;
490          }
491       }
492    
493       StartConsoleThread(configfile);
494    }
495 }
496
497 void wxbMainFrame::OnEditConfig(wxCommandEvent& event) {
498    wxString configfile;
499    wxConfig::Get()->Read("/ConfigFile", &configfile);
500    int stat = wxbConfigFileEditor(this, configfile).ShowModal();
501    if (stat == wxOK) {
502       StartConsoleThread(configfile);
503    }
504 }
505
506 void wxbMainFrame::OnConnect(wxCommandEvent& event) {
507    StartConsoleThread(configfile);
508 }
509
510 void wxbMainFrame::OnDisconnect(wxCommandEvent& event) {
511    if (ct != NULL) {
512       ct->Delete();
513       ct = NULL;
514    }
515 }
516
517 void wxbMainFrame::OnEnter(wxCommandEvent& WXUNUSED(event))
518 {
519    lockedbyconsole = true;
520    DisablePanels();
521    typeCtrl->HistoryAdd(typeCtrl->GetValue());
522    wxString str = typeCtrl->GetValue() + "\n";
523    Send(str);
524 }
525
526 /*
527  *  Called when data is arriving from director
528  */
529 void wxbMainFrame::OnPrint(wxbThreadEvent& event) {
530    wxbPrintObject* po = event.GetEventPrintObject();
531
532    Print(po->str, po->status);
533 }
534
535 /*
536  *  Prints data received from director to the console, and forwards it to the panels
537  */
538 void wxbMainFrame::Print(wxString str, int status)
539 {
540    if (lockedbyconsole) {
541       EnableConsole(false);
542    }
543    
544    if (status == CS_TERMINATED) {
545       consoleCtrl->AppendText(consoleBuffer);
546       consoleBuffer = "";
547       SetStatusText("Console thread terminated.");
548       consoleCtrl->ScrollLines(3);
549       ct = NULL;
550       DisablePanels();
551       int answer = wxMessageBox("Connection to the director lost. Quit program?", "Connection lost",
552                         wxYES_NO | wxICON_EXCLAMATION, this);
553       if (answer == wxYES) {
554          frame = NULL;
555          Close(true);
556       }
557       menuFile->Enable(MenuConnect, true);
558       menuFile->SetLabel(MenuConnect, "Connect");
559       menuFile->SetHelpString(MenuConnect, "Connect to the director");
560       menuFile->Enable(MenuDisconnect, false);
561       menuFile->Enable(ChangeConfigFile, true);
562       menuFile->Enable(EditConfigFile, true);
563       return;
564    }
565    
566    if (status == CS_CONNECTED) {
567       SetStatusText("Connected to the director.");
568       typeCtrl->ClearCommandList();
569       wxbDataTokenizer* dt = wxbUtils::WaitForEnd(".help", true);
570       int i, j;
571       wxString str;
572       for (i = 0; i < (int)dt->GetCount(); i++) {
573          str = (*dt)[i];
574          str.RemoveLast();
575          if ((j = str.Find(' ')) > -1) {
576             typeCtrl->AddCommand(str.Mid(0, j), str.Mid(j+1));
577          }
578       }
579       EnablePanels();
580       menuFile->Enable(MenuConnect, true);
581       menuFile->SetLabel(MenuConnect, "Reconnect");
582       menuFile->SetHelpString(MenuConnect, "Reconnect to the director");
583       menuFile->Enable(MenuDisconnect, true);
584       menuFile->Enable(ChangeConfigFile, true);
585       menuFile->Enable(EditConfigFile, true);
586       return;
587    }
588    if (status == CS_DISCONNECTED) {
589       consoleCtrl->AppendText(consoleBuffer);
590       consoleBuffer = "";
591       consoleCtrl->ScrollLines(3);
592       SetStatusText("Disconnected of the director.");
593       DisablePanels();
594       return;
595    }
596       
597    // CS_DEBUG is often sent by panels, 
598    // and resend it to them would sometimes cause infinite loops
599    
600    /* One promptcaught is normal, so we must have two true Print values to be
601     * sure that the prompt has effectively been caught.
602     */
603    int promptcaught = -1;
604    
605    if (status != CS_DEBUG) {
606       for (unsigned int i = 0; i < parsers.GetCount(); i++) {
607          promptcaught += parsers[i]->Print(str, status) ? 1 : 0;
608       }
609          
610       if ((status == CS_PROMPT) && (promptcaught < 1) && (promptparser->isPrompt())) {
611          Print("Unexpected question has been received.\n", CS_DEBUG);
612 //         Print(wxString("(") << promptparser->getIntroString() << "/-/" << promptparser->getQuestionString() << ")\n", CS_DEBUG);
613          
614          wxString message;
615          if (promptparser->getIntroString() != "") {
616             message << promptparser->getIntroString() << "\n";
617          }
618          message << promptparser->getQuestionString();
619          
620          if (promptparser->getChoices()) {
621             wxString *choices = new wxString[promptparser->getChoices()->GetCount()];
622             int *numbers = new int[promptparser->getChoices()->GetCount()];
623             int n = 0;
624             
625             for (unsigned int i = 0; i < promptparser->getChoices()->GetCount(); i++) {
626                if ((*promptparser->getChoices())[i] != "") {
627                   choices[n] = (*promptparser->getChoices())[i];
628                   numbers[n] = i;
629                   n++;
630                }
631             }
632             
633             int res = ::wxGetSingleChoiceIndex(message,
634                "wx-console: unexpected director's question.", n, choices, this);
635             if (res == -1) {
636                Send("\n");
637             }
638             else {
639                if (promptparser->isNumericalChoice()) {
640                   Send(wxString() << numbers[res] << "\n");
641                }
642                else {
643                   Send(wxString() << choices[res] << "\n");
644                }
645             }
646          }
647          else {
648             Send(::wxGetTextFromUser(message,
649                "wx-console: unexpected director's question.", "", this) + "\n");
650          }
651       }
652    }
653       
654    if (status == CS_END) {
655       if (lockedbyconsole) {
656          EnablePanels();
657          lockedbyconsole = false;
658       }
659       str = "#";
660    }
661
662    if (status == CS_DEBUG) {
663       consoleCtrl->AppendText(consoleBuffer);
664       consoleBuffer = "";
665       consoleCtrl->ScrollLines(3);
666       consoleCtrl->SetDefaultStyle(wxTextAttr(wxColour(0, 128, 0)));
667    }
668    else {
669       consoleCtrl->SetDefaultStyle(wxTextAttr(*wxBLACK));
670    }
671    consoleBuffer << str;
672    if (status == CS_PROMPT) {
673       if (lockedbyconsole) {
674          EnableConsole(true);
675       }
676       //consoleBuffer << "<P>";
677    }
678    
679    if ((status == CS_END) || (status == CS_PROMPT) || (str.Find("\n") > -1)) {
680       consoleCtrl->AppendText(consoleBuffer);
681       consoleBuffer = "";
682    
683       consoleCtrl->ScrollLines(3);
684    }
685    
686 //   consoleCtrl->ShowPosition(consoleCtrl->GetLastPosition());
687    
688    /*if (status != CS_DEBUG) {
689       consoleCtrl->AppendText("@");
690    }*/
691    //consoleCtrl->SetInsertionPointEnd();
692    
693 /*   if ((consoleCtrl->GetNumberOfLines()-1) > nlines) {
694       nlines = consoleCtrl->GetNumberOfLines()-1;
695    }
696    
697    if (status == CS_END) {
698       consoleCtrl->ShowPosition(nlines);
699    }*/
700 }
701
702 /*
703  *  Sends data to the director
704  */
705 void wxbMainFrame::Send(wxString str)
706 {
707    if (ct != NULL) {
708       ct->Write((const char*)str);
709       typeCtrl->SetValue("");
710       consoleCtrl->SetDefaultStyle(wxTextAttr(*wxRED));
711       consoleCtrl->AppendText(str);
712       consoleCtrl->ScrollLines(3);
713    }
714    
715 /*   if ((consoleCtrl->GetNumberOfLines()-1) > nlines) {
716       nlines = consoleCtrl->GetNumberOfLines()-1;
717    }
718    
719    consoleCtrl->ShowPosition(nlines);*/
720 }
721
722 /* Enable panels */
723 void wxbMainFrame::EnablePanels() {
724    for (int i = 0; panels[i] != NULL; i++) {
725       panels[i]->EnablePanel(true);
726    }
727    EnableConsole(true);
728 }
729
730 /* Disable panels, except the one passed as parameter */
731 void wxbMainFrame::DisablePanels(void* except) {
732    for (int i = 0; panels[i] != NULL; i++) {
733       if (panels[i] != except) {
734          panels[i]->EnablePanel(false);
735       }
736       else {
737          panels[i]->EnablePanel(true);
738       }
739    }
740    if (this != except) {
741       EnableConsole(false);
742    }
743 }
744
745 /* Enable or disable console typing */
746 void wxbMainFrame::EnableConsole(bool enable) {
747    typeCtrl->Enable(enable);
748    sendButton->Enable(enable);
749    if (enable) {
750       typeCtrl->SetFocus();
751    }
752 }
753
754 /*
755  *  Used by csprint, which is called by console thread.
756  *
757  *  In GTK and perhaps X11, only the main thread is allowed to interact with
758  *  graphical components, so by firing an event, the main loop will call OnPrint.
759  *
760  *  Calling OnPrint directly from console thread produces "unexpected async replies".
761  */
762 void firePrintEvent(wxString str, int status)
763 {
764    wxbPrintObject* po = new wxbPrintObject(str, status);
765
766    wxbThreadEvent evt(Thread);
767    evt.SetEventPrintObject(po);
768    
769    if (wxbMainFrame::GetInstance()) {
770       wxbMainFrame::GetInstance()->AddPendingEvent(evt);
771    }
772 }
773
774 //wxString csBuffer; /* Temporary buffer for receiving data from console thread */
775
776 /*
777  *  Called by console thread, this function forwards data line by line and end
778  *  signals to the GUI.
779  */
780 void csprint(const char* str, int status)
781 {
782    if (str != 0) {
783       firePrintEvent(wxString(str), status);
784    }
785    else {
786       firePrintEvent("", status);
787    }
788 }