sgpemv2/src/graphical_terminal_io.cc

121 lines
3.1 KiB
C++
Raw Normal View History

// src/graphicalterminalio.cc - Copyright 2005, 2006, University
// of Padova, dept. of Pure and Applied
// Mathematics
//
// This file is part of SGPEMv2.
//
// This 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.
//
// SGPEMv2 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 SGPEMv2; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#include "config.h"
#include "gettext.h"
#include "graphical_terminal_io.hh"
#include <glibmm/thread.h>
#include <gtkmm/button.h>
#include <gtkmm/textview.h>
#include <glibmm/ustring.h>
#include <algorithm>
#include <utility>
using namespace sgpem;
using namespace std;
using Glib::ustring;
using Glib::Thread;
GraphicalTerminalIO::GraphicalTerminalIO(TextSimulation* sim)
:_sim(sim)
{
using namespace Gtk;
set_title(_("Textual Simulation Log"));
set_default_size(500,300);
Gtk::Box* mainbox = manage(new VBox());
add(*mainbox);
_text_output.set_editable(false);
_text_output.modify_font(Pango::FontDescription("monospace"));
mainbox->pack_start(_text_output);
Gtk::Box* cmdbox = manage(new HBox());
mainbox->pack_end(*cmdbox, false, false);
cmdbox->pack_start(_text_input);
_send = manage(new Button(_("Send Command")));
_send->signal_clicked().connect(sigc::mem_fun(*this, &sgpem::GraphicalTerminalIO::onSend));
cmdbox->pack_start(*_send, false, true);
_text_input.grab_focus();
//this button is the default widget
//GTK_WIDGET_SET_FLAGS ( _send , GTK_CAN_DEFAULT);
//_send->grab_default();
show_all_children();
}
GraphicalTerminalIO::~GraphicalTerminalIO()
{}
void
GraphicalTerminalIO::onSend()
{
pair< pair<TextSimulation*, IOManager*>, const ustring > p(
pair<TextSimulation*, IOManager*>(_sim, this), _text_input.get_text());
Thread::create( sigc::bind(&TextSimulation::parse_command, p), true);
_text_input.set_text("");
_text_input.grab_focus();
}
uint
GraphicalTerminalIO::write_buffer(const Glib::ustring& buffer)
{
Gtk::TextBuffer::iterator i = _text_output.get_buffer()->end();
_text_output.get_buffer()->insert(i, buffer);
i = _text_output.get_buffer()->end();
//_text_output.scroll_to(i); // would need a mutex!
return buffer.size();
}
Glib::ustring
GraphicalTerminalIO::read_command()
{
using Glib::ustring;
static const ustring whitespaces = " \r\b\n\t\a";
// are there any other wspaces?
ustring command = _text_input.get_text();
// trimming:
uint f = command.find_first_not_of(whitespaces);
uint l = command.find_last_not_of(whitespaces);
if(f == ustring::npos)
return "";
return command.substr(f,l-f+1);
}
bool
GraphicalTerminalIO::is_full_duplex()
{
return true;
}