sugar-toolkit-gtk3/sugar/chat/ChatEditor.py

96 lines
2.6 KiB
Python
Raw Normal View History

2006-06-15 22:51:30 +02:00
import pygtk
pygtk.require('2.0')
import gtk
from sugar.chat.sketchpad.SketchPad import SketchPad
import richtext
class ChatEditor(gtk.HBox):
2006-06-18 20:45:04 +02:00
TEXT_MODE = 0
SKETCH_MODE = 1
def __init__(self, chat, mode):
gtk.HBox.__init__(self, False, 6)
self._chat = chat
2006-06-18 20:45:04 +02:00
self._notebook = gtk.Notebook()
self._notebook.set_show_tabs(False)
self._notebook.set_show_border(False)
self._notebook.set_size_request(-1, 70)
2006-06-15 22:51:30 +02:00
chat_view_sw = gtk.ScrolledWindow()
chat_view_sw.set_shadow_type(gtk.SHADOW_IN)
chat_view_sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
self._text_view = richtext.RichTextView()
self._text_view.connect("key-press-event", self.__key_press_event_cb)
self._text_view.connect("button-press-event", self.__button_press_event_cb)
2006-06-15 22:51:30 +02:00
chat_view_sw.add(self._text_view)
self._text_view.show()
self._notebook.append_page(chat_view_sw)
2006-06-15 22:51:30 +02:00
chat_view_sw.show()
self._sketchpad = SketchPad()
self._notebook.append_page(self._sketchpad)
2006-06-15 22:51:30 +02:00
self._sketchpad.show()
self.pack_start(self._notebook)
self._notebook.show()
send_button = gtk.Button("Send")
send_button.set_size_request(60, -1)
send_button.connect('clicked', self.__send_button_clicked_cb)
self.pack_start(send_button, False, True)
send_button.show()
2006-06-18 20:45:04 +02:00
self.set_mode(mode)
2006-06-15 22:51:30 +02:00
def set_color(self, color):
self._sketchpad.set_color(color)
def get_buffer(self):
return self._text_view.get_buffer()
2006-06-18 20:45:04 +02:00
def set_mode(self, mode):
self._mode = mode
if self._mode == ChatEditor.SKETCH_MODE:
self._notebook.set_current_page(1)
2006-06-18 20:45:04 +02:00
elif self._mode == ChatEditor.TEXT_MODE:
self._notebook.set_current_page(0)
2006-06-15 22:51:30 +02:00
def __send_button_clicked_cb(self, button):
self._send()
def _send(self):
if self._mode == ChatEditor.SKETCH_MODE:
self._send_sketch()
elif self._mode == ChatEditor.TEXT_MODE:
self._send_text()
2006-06-15 22:51:30 +02:00
def _send_sketch(self):
self._chat.send_sketch(self._sketchpad.to_svg())
self._sketchpad.clear()
2006-06-15 22:51:30 +02:00
def _send_text(self):
buf = self._text_view.get_buffer()
text = buf.get_text(buf.get_start_iter(), buf.get_end_iter())
if len(text.strip()) > 0:
serializer = richtext.RichTextSerializer()
text = serializer.serialize(buf)
self._chat.send_text_message(text)
buf.set_text("")
buf.place_cursor(buf.get_start_iter())
def __key_press_event_cb(self, text_view, event):
if event.keyval == gtk.keysyms.Return:
self._send()
2006-06-15 22:51:30 +02:00
return True
def __button_press_event_cb(self, text_view, event):
# Need to explicitly get keyboard focus in the window
# because docks doesn't take it by default.
toplevel = text_view.get_toplevel()
toplevel.window.focus()