1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
|
/* Copyright (C) 2005 MySQL AB
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
#include "MGShellView.h"
/**
* @file MGShellView.cc
* @brief
*/
MGShellView::MGShellView()
{
_shell= 0;
_prompt_visible= false;
set_wrap_mode(Gtk::WRAP_WORD);
signal_key_press_event().connect(sigc::mem_fun(*this,&MGShellView::key_press_event), false);
}
void MGShellView::on_insert_at_cursor(const Glib::ustring& str)
{
g_message("INSERT");
}
bool MGShellView::key_press_event(GdkEventKey* event)
{
if (event->keyval == GDK_Return)
{
Glib::ustring command= get_buffer()->get_slice(_prompt_end->get_iter(), get_buffer()->end());
Gtk::TextView::on_key_press_event(event);
_prompt_start.clear();
_prompt_end.clear();
_prompt_visible= false;
idle();
if (_shell && !command.empty())
{
_shell->perform_command(command);
}
return true;
}
else if (event->keyval == GDK_BackSpace)
{
if (get_buffer()->get_insert()->get_iter().get_offset() <= _prompt_end->get_iter().get_offset())
return true;
}
else if (event->keyval == GDK_Home)
{
if (_prompt_end)
get_buffer()->move_mark(get_buffer()->get_insert(), _prompt_end->get_iter());
return true;
}
else
{
if (get_buffer()->get_insert()->get_iter().get_offset() <= _prompt_end->get_iter().get_offset()
&& event->string && *event->string && event->state == 0)
get_buffer()->move_mark(get_buffer()->get_insert(), get_buffer()->end());
}
return Gtk::TextView::on_key_press_event(event);
}
bool MGShellView::idle()
{
if (!_prompt_visible)
{
if (_shell)
{
Glib::RefPtr<Gtk::TextBuffer> buffer= get_buffer();
if (buffer->end().get_line_offset() > 0)
buffer->insert(buffer->end(), "\n");
_prompt_start= buffer->create_mark(buffer->end());
buffer->insert(buffer->end(), _shell->get_prompt());
_prompt_end= buffer->create_mark(buffer->end());
}
_prompt_visible= true;
}
return false;
}
void MGShellView::out_text(const Glib::ustring &str)
{
if (_prompt_visible)
{
get_buffer()->erase(_prompt_start->get_iter(), _prompt_end->get_iter());
Glib::signal_idle().connect(sigc::mem_fun(*this,&MGShellView::idle));
_prompt_visible= false;
}
get_buffer()->insert(get_buffer()->end(), str);
}
void MGShellView::set_shell(MGShellInterface *shell)
{
_shell= shell;
Glib::signal_idle().connect(sigc::mem_fun(*this,&MGShellView::idle));
}
|