Debugging framework
[qemu-gui.git] / console.cpp
blob9e8716dee9e50b65efea8fa5c7826fc3b37f2c28
1 #include <wx/wx.h>
2 #include <wx/textctrl.h>
3 #include <wx/sizer.h>
4 #include "mainframe.h"
5 #include "qemu-ui.h"
6 #include "pipedprocess.h"
7 #include "socket.h"
8 #include "console.h"
9 #include "vm.h"
11 BEGIN_EVENT_TABLE(ConsolePanel, wxPanel)
12 EVT_COMMAND (wxID_ANY,EVT_PROCESS_INPUT,ConsolePanel::HandleConsoleInput)
13 EVT_COMMAND (wxID_ANY,EVT_PROCESS_OUTPUT,ConsolePanel::HandleConsoleOutput)
14 EVT_COMMAND (wxID_ANY,EVT_PROCESS_CREATED,ConsolePanel::HandleProcessCreation)
15 EVT_COMMAND (wxID_ANY,EVT_PROCESS_TERMINATED,ConsolePanel::HandleProcessTermination)
16 EVT_COMMAND (wxID_ANY,EVT_SOCKET_INPUT,ConsolePanel::HandleConsoleInput)
17 EVT_COMMAND (wxID_ANY,EVT_SOCKET_WRITE,ConsolePanel::HandleConsoleOutput)
18 END_EVENT_TABLE()
20 ConsolePanel::ConsolePanel(wxWindow *parent,QemuVM *vm) : wxPanel(parent){
21 this->vm = vm;
22 this->process = vm->GetProcess();
23 this->socket = vm->GetSocket();
25 if(process)
26 process->AddEventHandler(this);
28 if(socket)
29 socket->AddEventHandler(this);
31 console = new wxTextCtrl(this,-1,wxT(""),wxDefaultPosition,
32 wxDefaultSize, wxTE_MULTILINE|wxTE_READONLY|
33 wxHSCROLL|wxTE_LINEWRAP
36 console->SetFont(wxFont(10, wxFONTFAMILY_TELETYPE,wxFONTSTYLE_NORMAL,
37 wxFONTWEIGHT_NORMAL));
39 input = new InputTextCtrl(this);
41 sizer = new wxBoxSizer(wxVERTICAL);
42 sizer->Add(console, 1, wxEXPAND|wxADJUST_MINSIZE, 0);
43 sizer->Add(input, 0, wxEXPAND|wxADJUST_MINSIZE, 0);
44 SetAutoLayout(true);
45 SetSizer(sizer);
48 ConsolePanel::~ConsolePanel(){
51 void ConsolePanel::SetProcess(PipedProcess *process){
52 if(!this->process){
53 this->process = process;
54 process->AddEventHandler(this);
58 bool ConsolePanel::Notify(const wxString& cmd){
59 if(socket != NULL){
60 // will generate an event which is handlet
61 // by HandleConsoleOutput
62 socket->Write(cmd);
65 return true;
68 void ConsolePanel::HandleConsoleInput( wxCommandEvent &event )
70 AppendConsoleOutput(event.GetString());
73 void ConsolePanel::HandleConsoleOutput( wxCommandEvent &event )
75 AppendConsoleInput(event.GetString());
78 void ConsolePanel::HandleProcessCreation(wxCommandEvent& event){
79 console->Clear();
80 AppendConsoleInput(event.GetString());
83 void ConsolePanel::HandleProcessTermination(wxCommandEvent& event)
85 wxString msg;
86 msg << _("Qemu exited with");
87 msg << wxT(" ") << event.GetInt() << wxT(".");
88 AppendConsoleError(msg);
89 QemuGUI::GetMainFrame()->UpdateState();
92 void ConsolePanel::AppendConsoleInput(const wxString& cmd){
93 wxTextAttr attr;
94 attr.SetFont(wxFont(10, wxFONTFAMILY_TELETYPE,wxFONTSTYLE_NORMAL,
95 wxFONTWEIGHT_BOLD));
96 long start, end;
97 start = console->GetLastPosition();
98 console->AppendText(wxString::Format(wxT(">>%s\n"),cmd.c_str()));
99 end = console->GetLastPosition();
100 console->SetStyle(start, end, attr);
101 ScrollToEnd();
104 void ConsolePanel::AppendConsoleOutput(const wxString& cmd){
105 console->AppendText(cmd + wxT("\n"));
106 ScrollToEnd();
109 void ConsolePanel::AppendConsoleError(const wxString& cmd){
110 console->AppendText(cmd + wxT("\n"));
111 ScrollToEnd();
114 void ConsolePanel::ScrollToEnd(){
115 console->ShowPosition(console->GetLastPosition());
118 void ConsolePanel::SetState(bool state){
119 input->Enable(state);
122 void InputTextCtrl::OnKeyDown(wxKeyEvent& event){
123 switch(event.GetKeyCode()){
124 case WXK_RETURN:
125 case WXK_NUMPAD_ENTER:
126 const wxString& cmd = this->GetValue();
127 if(((ConsolePanel *)GetParent())->Notify(cmd))
128 this->Clear();
129 break;
132 event.Skip();
135 InputTextCtrl::InputTextCtrl(ConsolePanel *parent)
136 :wxTextCtrl(parent, wxID_ANY, wxT(""), wxDefaultPosition,
137 wxDefaultSize, wxTE_PROCESS_ENTER|wxTE_PROCESS_TAB){
141 BEGIN_EVENT_TABLE(InputTextCtrl, wxTextCtrl)
142 EVT_KEY_DOWN(InputTextCtrl::OnKeyDown)
143 END_EVENT_TABLE()