From 07e32bdecc9f4730a2ce2405d2e8457610160a52 Mon Sep 17 00:00:00 2001 From: Daniel Francis Date: Mon, 26 Dec 2011 19:19:04 +0000 Subject: Consoles --- diff --git a/activity.py b/activity.py index ae1488f..5dc5703 100644 --- a/activity.py +++ b/activity.py @@ -33,10 +33,13 @@ import gtk import sugar from sugar import mime + from sugar.graphics import iconentry from sugar.graphics.toolbutton import ToolButton +from sugar.graphics.radiotoolbutton import RadioToolButton from sugar.graphics.toggletoolbutton import ToggleToolButton from sugar.graphics.toolbarbox import ToolbarBox +from sugar.graphics.tray import VTray from sugar.activity.widgets import EditToolbar, StopButton, \ ActivityToolbarButton, ToolbarButton from sugar.datastore import datastore @@ -45,12 +48,11 @@ from sugar.activity import activity from pep8_check import PEP8_Check import options from editor import Editor -from python_console import PythonConsole +import consoles import file_choosers file_choosers.langsmanager = options.LANGUAGE_MANAGER file_choosers.langs = options.LANGUAGES - class JAMEdit(activity.Activity): def __init__(self, handle): @@ -69,12 +71,13 @@ class JAMEdit(activity.Activity): scroll.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) scroll.add(self.editor) - scroll.show_all() vbox = gtk.VBox() vpaned = gtk.VPaned() - vpaned.pack1(scroll) vbox.pack_start(vpaned, True, True, 0) + vpaned.show() + vpaned.pack1(scroll, resize=True) + scroll.show_all() self.set_canvas(vbox) @@ -114,12 +117,41 @@ class JAMEdit(activity.Activity): self.pep8_bar.add(self.pep8_bar.label) vbox.pack_end(self.pep8_bar, False, True, 0) - self.python_console = PythonConsole() - self.python_console.show() - vpaned.pack2(self.python_console) + tray = VTray() + self.test_notebook = gtk.Notebook() + self.test_notebook.set_show_tabs(False) + terminal = consoles.Terminal() + terminal.show() + terminal_item = RadioToolButton() + terminal_item.set_named_icon("console-terminal") + terminal_item.set_tooltip("Terminal") + terminal_item.connect("toggled", self.page_changed, 0) + terminal_item.show() + self.test_notebook.append_page(terminal, None) + tray.add_item(terminal_item) + python_console = consoles.PythonConsole() + python_console.show() + python_item = RadioToolButton() + python_item.set_named_icon("console-python") + python_item.set_tooltip("Python Console") + python_item.set_group(terminal_item) + python_item.connect("toggled", self.page_changed, 1) + python_item.show() + self.test_notebook.append_page(python_console) + tray.add_item(python_item) + hbox = gtk.HBox() + hbox.pack_start(tray, False, True, 0) + hbox.pack_start(self.test_notebook) + self.test_notebook.show() + vpaned.pack2(hbox, resize=True) vpaned.show_all() + tray.show() + hbox.show() vbox.show_all() + def page_changed(self, widget, index): + self.test_notebook.set_current_page(index) + def change_style(self, widget, style): self.editor.set_style(style) diff --git a/consoles.py b/consoles.py new file mode 100644 index 0000000..0bb6322 --- /dev/null +++ b/consoles.py @@ -0,0 +1,246 @@ +#!/usr/bin/env python +# -*- coding: UTF-8 -*- + +# consoles.py by/por: +# Agustin Zubiaga +# Daniel Francis +# +# 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 3 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., 51 Franklin Street, Fifth Floor, Boston, +# MA 02110-1301, USA. + +from signal import SIGTERM +import os + +import pango +import gtk +import vte + +from sugar.graphics.toolbutton import ToolButton +from sugar.graphics.colorbutton import ColorToolButton +from sugar.graphics.tray import VTray + +class Console(gtk.HBox): + def __init__(self): + gtk.HBox.__init__(self) + self.terminal = vte.Terminal() + self.bgcolor = gtk.gdk.color_parse("#000000") + self.fgcolor = gtk.gdk.color_parse("#FFFFFF") + self.scrollbar = gtk.VScrollbar(self.terminal.get_adjustment()) + self.terminal.show() + self.scrollbar.show() + self.pack_start(self.terminal, True, True, 0) + self.pack_end(self.scrollbar, False, True, 0) + self.terminal.grab_focus() + + def set_font(self, font): + self.terminal.set_font(font) + + def set_bgcolor(self, color): + self.terminal.set_colors(self.fgcolor, color, []) + self.bgcolor = color + + def set_fgcolor(self, color): + self.terminal.set_colors(color, self.bgcolor, []) + self.fgcolor = color + + def copy(self): + if self.terminal.get_has_selection(): + self.terminal.copy_clipboard() + + def paste(self): + self.terminal.paste_clipboard() + + def stop(self): + try: + os.kill(self.process_id, SIGTERM) + self._clear_console() + except Exception, err: + print err + + def _clear_console(self): + self.terminal.grab_focus() + self.terminal.feed("\x1B[H\x1B[J\x1B[0;39m") + + def run_command(self, command=None, args=None): + self.process_id = self.terminal.fork_command(command=command, argv=args) + +class Terminal(gtk.HBox): + def __copy_cb(self, button): + self.console.copy() + + def __paste_cb(self, button): + self.console.paste() + + def _fgcolor_cb(self, button, pspec): + newcolor = button.get_color() + self.console.set_fgcolor(newcolor) + + def _bgcolor_cb(self, button, pspec): + newcolor = button.get_color() + self.console.set_bgcolor(newcolor) + + def update_font(self, widget, font_selection): + font = pango.FontDescription(font_selection.get_font_name()) + self.console.set_font(font) + + def reset(self, widget): + self.console.stop() + self.console.run_command() + + def __init__(self): + gtk.HBox.__init__(self) + self.toolbar = VTray() + + copy = ToolButton("edit-copy") + copy.set_tooltip('Copy') + copy.connect("clicked", self.__copy_cb) + copy.show() + self.toolbar.add_item(copy, -1) + paste = ToolButton("edit-paste") + paste.set_tooltip("Paste") + paste.connect("clicked", self.__paste_cb) + paste.show() + self.toolbar.add_item(paste, -1) + + fgcolor = ColorToolButton() + fgcolor.get_child()._title = "Font Color" + self.toolbar.add_item(fgcolor, -1) + fgcolor.connect('notify::color', self._fgcolor_cb) + fgcolor.show_all() + bgcolor = ColorToolButton() + bgcolor.get_child()._title = "Background Color" + bgcolor.connect('notify::color', self._bgcolor_cb) + self.toolbar.add_item(bgcolor, -1) + bgcolor.show_all() + + self.console = Console() + self.console.set_bgcolor(gtk.gdk.color_parse("#FFFFFF")) + self.console.set_fgcolor(gtk.gdk.color_parse("#000000")) + bgcolor.set_color(gtk.gdk.color_parse("#FFFFFF")) + fgcolor.set_color(gtk.gdk.color_parse("#000000")) + self.console.show() + + font = ToolButton("format-text") + font.set_tooltip("Console Font") + fontselection = gtk.FontSelection() + fontselection.get_family_list().get_selection().connect("changed", self.update_font, fontselection) + fontselection.get_face_list().get_selection().connect("changed", self.update_font, fontselection) + fontselection.get_size_entry().connect("changed", self.update_font, fontselection) + fontselection.get_size_entry().connect("activate", self.update_font, fontselection) + fontselection.show() + font.props.palette.set_content(fontselection) + fontselection.set_font_name("Monospace Regular 10") + font.show() + self.toolbar.add_item(font, -1) + + reset = ToolButton("view-refresh") + reset.set_tooltip("Reset Console") + reset.connect("clicked", self.reset) + reset.show() + self.toolbar.add_item(reset, -1) + self.toolbar.show() + self.pack_start(self.toolbar, False, True, 0) + + self.console.run_command() + self.pack_start(self.console) + +class PythonConsole(gtk.HBox): + def __copy_cb(self, button): + self.console.copy() + + def __paste_cb(self, button): + self.console.paste() + + def _fgcolor_cb(self, button, pspec): + newcolor = button.get_color() + self.console.set_fgcolor(newcolor) + + def _bgcolor_cb(self, button, pspec): + newcolor = button.get_color() + self.console.set_bgcolor(newcolor) + + def update_font(self, widget, font_selection): + font = pango.FontDescription(font_selection.get_font_name()) + self.console.set_font(font) + + def reset(self, widget): + self.console.stop() + self.console.run_command("python") + + def __init__(self): + gtk.HBox.__init__(self) + self.toolbar = VTray() + + copy = ToolButton("edit-copy") + copy.set_tooltip('Copy') + copy.connect("clicked", self.__copy_cb) + copy.show() + self.toolbar.add_item(copy, -1) + paste = ToolButton("edit-paste") + paste.set_tooltip("Paste") + paste.connect("clicked", self.__paste_cb) + paste.show() + self.toolbar.add_item(paste, -1) + + fgcolor = ColorToolButton() + fgcolor.get_child()._title = "Font Color" + self.toolbar.add_item(fgcolor, -1) + fgcolor.connect('notify::color', self._fgcolor_cb) + fgcolor.show_all() + bgcolor = ColorToolButton() + bgcolor.get_child()._title = "Background Color" + bgcolor.connect('notify::color', self._bgcolor_cb) + self.toolbar.add_item(bgcolor, -1) + bgcolor.show_all() + + self.console = Console() + self.console.set_bgcolor(gtk.gdk.color_parse("#FFFFFF")) + self.console.set_fgcolor(gtk.gdk.color_parse("#000000")) + bgcolor.set_color(gtk.gdk.color_parse("#FFFFFF")) + fgcolor.set_color(gtk.gdk.color_parse("#000000")) + self.console.show() + + font = ToolButton("format-text") + font.set_tooltip("Console Font") + fontselection = gtk.FontSelection() + fontselection.get_family_list().get_selection().connect("changed", self.update_font, fontselection) + fontselection.get_face_list().get_selection().connect("changed", self.update_font, fontselection) + fontselection.get_size_entry().connect("changed", self.update_font, fontselection) + fontselection.get_size_entry().connect("activate", self.update_font, fontselection) + fontselection.show() + font.props.palette.set_content(fontselection) + fontselection.set_font_name("Monospace Regular 10") + font.show() + self.toolbar.add_item(font, -1) + + reset = ToolButton("view-refresh") + reset.set_tooltip("Reset Console") + reset.connect("clicked", self.reset) + reset.show() + self.toolbar.add_item(reset, -1) + self.toolbar.show() + self.pack_start(self.toolbar, False, True, 0) + + self.console.run_command("python") + self.pack_start(self.console) + +if __name__ == "__main__": + w = gtk.Window() + term = Terminal() + term.show() + w.add(term) + w.show() + w.set_resizable(True) + gtk.main() diff --git a/editor.py b/editor.py index d9345fc..a397c28 100644 --- a/editor.py +++ b/editor.py @@ -35,6 +35,7 @@ from sugar.graphics.combobox import ComboBox from sugar.graphics.toolcombobox import ToolComboBox LANGUAGE_MANAGER = gtksourceview2.language_manager_get_default() +LANGUAGES = LANGUAGE_MANAGER.get_language_ids() class Editor(gtksourceview2.View): __gsignals__ = {"pep8-aviable": (gobject.SIGNAL_RUN_LAST, diff --git a/icons/console-python.svg b/icons/console-python.svg new file mode 100644 index 0000000..753aa9c --- /dev/null +++ b/icons/console-python.svg @@ -0,0 +1,36 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/icons/console-terminal.svg b/icons/console-terminal.svg new file mode 100644 index 0000000..12d3fa1 --- /dev/null +++ b/icons/console-terminal.svg @@ -0,0 +1,11 @@ + + +]> + + + + + + + -- cgit v0.9.1