sgpemv2/src/graphical_terminal_io.cc

139 lines
3.8 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/main.h>
#include <gtkmm/scrolledwindow.h>
#include <gtkmm/textview.h>
#include <glibmm/ustring.h>
#include <algorithm>
#include <iostream>
#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);
Box& mainbox = *manage(new VBox());
add(mainbox);
ScrolledWindow& txout_scroll = *manage(new ScrolledWindow());
_text_output.set_editable(false);
_text_output.modify_font(Pango::FontDescription("monospace"));
txout_scroll.set_policy(POLICY_AUTOMATIC, POLICY_AUTOMATIC);
txout_scroll.add(_text_output);
mainbox.pack_start(txout_scroll);
Box& cmdbox = *manage(new HBox());
mainbox.pack_end(cmdbox, false, false);
// Maybe we should connect explicitly TextBuffer.signal_changed
// to the TextView redraw function (see Gtk::Widget for that)??
_text_input.activate();
_text_input.set_activates_default();
cmdbox.pack_start(_text_input);
Button& btsend = *manage(new Gtk::Button(_("Send Command")));
btsend.signal_clicked().connect(sigc::mem_fun(*this, &GraphicalTerminalIO::onSend));
GTK_WIDGET_SET_FLAGS(btsend.gobj(), CAN_DEFAULT);
set_default(btsend);
cmdbox.pack_start(btsend, false, true);
_text_input.grab_focus();
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);
// Force the UI update queue to flush
// while(Gtk::Main::instance()->events_pending())
// Gtk::Main::instance()->iteration();
// i = _text_output.get_buffer()->end();
return buffer.size();
}
Glib::ustring
GraphicalTerminalIO::read_command()
{
// For next implementers: take a look to Gtk::EntryCompletion...
// ... maybe it's worth using (when and if we'll have a separate
// Interpreter class)
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;
}