2006-01-27 18:57:53 +01:00
|
|
|
// src/graphicalterminalio.cc - Copyright 2005, 2006, University
|
2006-01-27 16:04:06 +01:00
|
|
|
// 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"
|
|
|
|
|
2006-02-09 20:43:24 +01:00
|
|
|
#include "graphical_terminal_io.hh"
|
2006-01-27 16:04:06 +01:00
|
|
|
|
2006-02-22 23:45:06 +01:00
|
|
|
#include <glibmm/thread.h>
|
2006-01-27 16:04:06 +01:00
|
|
|
#include <gtkmm/button.h>
|
2006-02-25 20:32:54 +01:00
|
|
|
#include <gtkmm/main.h>
|
|
|
|
#include <gtkmm/scrolledwindow.h>
|
2006-01-27 16:04:06 +01:00
|
|
|
#include <gtkmm/textview.h>
|
|
|
|
#include <glibmm/ustring.h>
|
|
|
|
|
2006-02-10 21:44:48 +01:00
|
|
|
#include <algorithm>
|
2006-02-25 20:32:54 +01:00
|
|
|
#include <iostream>
|
2006-02-22 23:45:06 +01:00
|
|
|
#include <utility>
|
2006-02-10 21:44:48 +01:00
|
|
|
|
2006-02-09 20:33:35 +01:00
|
|
|
using namespace sgpem;
|
2006-02-22 23:45:06 +01:00
|
|
|
using namespace std;
|
|
|
|
using Glib::ustring;
|
2006-01-27 16:04:06 +01:00
|
|
|
|
2006-02-22 23:45:06 +01:00
|
|
|
GraphicalTerminalIO::GraphicalTerminalIO(TextSimulation* sim)
|
2006-02-25 20:32:54 +01:00
|
|
|
: _sim(sim)
|
2006-02-10 21:44:48 +01:00
|
|
|
{
|
|
|
|
using namespace Gtk;
|
|
|
|
|
|
|
|
set_title(_("Textual Simulation Log"));
|
|
|
|
set_default_size(500,300);
|
|
|
|
|
2006-02-25 20:32:54 +01:00
|
|
|
Box& mainbox = *manage(new VBox());
|
|
|
|
add(mainbox);
|
2006-02-10 21:44:48 +01:00
|
|
|
|
2006-02-25 20:32:54 +01:00
|
|
|
ScrolledWindow& txout_scroll = *manage(new ScrolledWindow());
|
2006-02-10 21:44:48 +01:00
|
|
|
_text_output.set_editable(false);
|
|
|
|
_text_output.modify_font(Pango::FontDescription("monospace"));
|
2006-02-25 20:32:54 +01:00
|
|
|
txout_scroll.set_policy(POLICY_AUTOMATIC, POLICY_AUTOMATIC);
|
|
|
|
txout_scroll.add(_text_output);
|
|
|
|
mainbox.pack_start(txout_scroll);
|
2006-02-10 21:44:48 +01:00
|
|
|
|
2006-02-25 20:32:54 +01:00
|
|
|
Box& cmdbox = *manage(new HBox());
|
|
|
|
mainbox.pack_end(cmdbox, false, false);
|
2006-02-10 21:44:48 +01:00
|
|
|
|
2006-02-25 20:32:54 +01:00
|
|
|
// Maybe we should connect explicitly TextBuffer.signal_changed
|
|
|
|
// to the TextView redraw function (see Gtk::Widget for that)??
|
2006-02-10 21:44:48 +01:00
|
|
|
|
2006-02-25 20:32:54 +01:00
|
|
|
_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);
|
2006-02-10 21:44:48 +01:00
|
|
|
|
2006-02-22 23:45:06 +01:00
|
|
|
_text_input.grab_focus();
|
|
|
|
|
2006-02-10 21:44:48 +01:00
|
|
|
show_all_children();
|
|
|
|
}
|
2006-01-27 16:04:06 +01:00
|
|
|
|
|
|
|
GraphicalTerminalIO::~GraphicalTerminalIO()
|
2006-02-09 20:33:35 +01:00
|
|
|
{}
|
2006-01-27 16:04:06 +01:00
|
|
|
|
2006-02-22 23:45:06 +01:00
|
|
|
void
|
|
|
|
GraphicalTerminalIO::onSend()
|
|
|
|
{
|
2006-03-18 11:50:16 +01:00
|
|
|
using Glib::Thread;
|
2006-02-22 23:45:06 +01:00
|
|
|
|
2006-02-25 20:32:54 +01:00
|
|
|
pair<pair<TextSimulation*, IOManager*>, const ustring> p(
|
|
|
|
pair<TextSimulation*, IOManager*>(_sim, this),
|
2006-03-18 11:50:16 +01:00
|
|
|
read_command());
|
2006-02-22 23:45:06 +01:00
|
|
|
|
2006-02-25 20:32:54 +01:00
|
|
|
Thread::create(sigc::bind(&TextSimulation::parse_command, p), true);
|
2006-02-22 23:45:06 +01:00
|
|
|
}
|
|
|
|
|
2006-02-15 23:58:18 +01:00
|
|
|
uint
|
2006-01-27 16:04:06 +01:00
|
|
|
GraphicalTerminalIO::write_buffer(const Glib::ustring& buffer)
|
|
|
|
{
|
2006-03-18 11:50:16 +01:00
|
|
|
Glib::Mutex::Lock lock(_mtx);
|
|
|
|
|
|
|
|
Glib::RefPtr<Gtk::TextBuffer> txbuf = _text_output.get_buffer();
|
|
|
|
txbuf->insert_at_cursor(buffer);
|
2006-02-25 20:32:54 +01:00
|
|
|
|
|
|
|
// 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();
|
2006-01-27 16:04:06 +01:00
|
|
|
}
|
|
|
|
|
2006-02-10 21:44:48 +01:00
|
|
|
Glib::ustring
|
2006-02-15 23:58:18 +01:00
|
|
|
GraphicalTerminalIO::read_command()
|
2006-01-27 16:04:06 +01:00
|
|
|
{
|
2006-02-25 20:32:54 +01:00
|
|
|
// For next implementers: take a look to Gtk::EntryCompletion...
|
|
|
|
// ... maybe it's worth using (when and if we'll have a separate
|
|
|
|
// Interpreter class)
|
|
|
|
|
2006-02-10 21:44:48 +01:00
|
|
|
using Glib::ustring;
|
|
|
|
static const ustring whitespaces = " \r\b\n\t\a";
|
|
|
|
// are there any other wspaces?
|
|
|
|
|
|
|
|
ustring command = _text_input.get_text();
|
|
|
|
|
|
|
|
// trimming:
|
2006-02-15 23:58:18 +01:00
|
|
|
uint f = command.find_first_not_of(whitespaces);
|
|
|
|
uint l = command.find_last_not_of(whitespaces);
|
2006-02-10 21:44:48 +01:00
|
|
|
if(f == ustring::npos)
|
2006-02-17 23:19:25 +01:00
|
|
|
return "";
|
2006-03-18 11:50:16 +01:00
|
|
|
|
|
|
|
_text_input.set_text("");
|
|
|
|
_text_input.grab_focus();
|
2006-02-10 21:44:48 +01:00
|
|
|
|
2006-02-17 23:19:25 +01:00
|
|
|
return command.substr(f,l-f+1);
|
2006-01-27 16:04:06 +01:00
|
|
|
}
|
2006-02-17 23:19:25 +01:00
|
|
|
|
|
|
|
bool
|
|
|
|
GraphicalTerminalIO::is_full_duplex()
|
|
|
|
{
|
|
|
|
return true;
|
2006-02-19 23:36:24 +01:00
|
|
|
}
|