More work on the notebook widget. Ellipsize labels.

This commit is contained in:
Marco Pesenti Gritti 2006-04-21 15:27:20 -04:00
parent b15ce38850
commit 73e7360bab
7 changed files with 169 additions and 232 deletions

View File

@ -15,99 +15,127 @@ import geckoembed
class Activity(dbus.service.Object): class Activity(dbus.service.Object):
def __init__(self): def __init__(self):
pass pass
def name_owner_changed(self, service_name, old_service_name, new_service_name): def name_owner_changed(self, service_name, old_service_name, new_service_name):
#print "in name_owner_changed: svc=%s oldsvc=%s newsvc=%s"%(service_name, old_service_name, new_service_name) #print "in name_owner_changed: svc=%s oldsvc=%s newsvc=%s"%(service_name, old_service_name, new_service_name)
if service_name == "com.redhat.Sugar.Shell" and new_service_name == "": if service_name == "com.redhat.Sugar.Shell" and new_service_name == "":
self.activity_on_disconnected_from_shell() self.activity_on_disconnected_from_shell()
#elif service_name == "com.redhat.Sugar.Shell" and old_service_name == "": #elif service_name == "com.redhat.Sugar.Shell" and old_service_name == "":
# self.activity_on_shell_reappeared() # self.activity_on_shell_reappeared()
def activity_connect_to_shell(self): def activity_connect_to_shell(self):
self.__bus = dbus.SessionBus() self.__bus = dbus.SessionBus()
self.__bus.add_signal_receiver(self.name_owner_changed, dbus_interface = "org.freedesktop.DBus", signal_name = "NameOwnerChanged") self.__bus.add_signal_receiver(self.name_owner_changed, dbus_interface = "org.freedesktop.DBus", signal_name = "NameOwnerChanged")
self.__activity_container_object = self.__bus.get_object("com.redhat.Sugar.Shell", \ self.__activity_container_object = self.__bus.get_object("com.redhat.Sugar.Shell", \
"/com/redhat/Sugar/Shell/ActivityContainer") "/com/redhat/Sugar/Shell/ActivityContainer")
self.__activity_container = dbus.Interface(self.__activity_container_object, \ self.__activity_container = dbus.Interface(self.__activity_container_object, \
"com.redhat.Sugar.Shell.ActivityContainer") "com.redhat.Sugar.Shell.ActivityContainer")
self.__activity_id = self.__activity_container.add_activity("") self.__activity_id = self.__activity_container.add_activity("")
self.__object_path = "/com/redhat/Sugar/Shell/Activities/%d"%self.__activity_id self.__object_path = "/com/redhat/Sugar/Shell/Activities/%d"%self.__activity_id
print "object_path = %s"%self.__object_path print "object_path = %s"%self.__object_path
self.__activity_object = dbus.Interface(self.__bus.get_object("com.redhat.Sugar.Shell", self.__object_path), \ self.__activity_object = dbus.Interface(self.__bus.get_object("com.redhat.Sugar.Shell", self.__object_path), \
"com.redhat.Sugar.Shell.ActivityHost") "com.redhat.Sugar.Shell.ActivityHost")
self.__window_id = self.__activity_object.get_host_xembed_id() self.__window_id = self.__activity_object.get_host_xembed_id()
print "XEMBED window_id = %d"%self.__window_id print "XEMBED window_id = %d"%self.__window_id
self.__plug = gtk.Plug(self.__window_id) self.__plug = gtk.Plug(self.__window_id)
# Now let the Activity register a peer service so the Shell can poke it # Now let the Activity register a peer service so the Shell can poke it
self.__peer_service_name = "com.redhat.Sugar.Activity%d"%self.__activity_id self.__peer_service_name = "com.redhat.Sugar.Activity%d"%self.__activity_id
self.__peer_object_name = "/com/redhat/Sugar/Activity/%d"%self.__activity_id self.__peer_object_name = "/com/redhat/Sugar/Activity/%d"%self.__activity_id
self.__service = dbus.service.BusName(self.__peer_service_name, bus=self.__bus) self.__service = dbus.service.BusName(self.__peer_service_name, bus=self.__bus)
dbus.service.Object.__init__(self, self.__service, self.__peer_object_name) dbus.service.Object.__init__(self, self.__service, self.__peer_object_name)
self.__activity_object.set_peer_service_name(self.__peer_service_name, self.__peer_object_name) self.__activity_object.set_peer_service_name(self.__peer_service_name, self.__peer_object_name)
self.activity_on_connected_to_shell() self.activity_on_connected_to_shell()
def activity_get_gtk_plug(self): def activity_get_gtk_plug(self):
return self.__plug return self.__plug
def activity_set_tab_text(self, text): def activity_set_tab_text(self, text):
self.__activity_object.set_tab_text(text) self.__activity_object.set_tab_text(text)
@dbus.service.method("com.redhat.Sugar.Activity", \ @dbus.service.method("com.redhat.Sugar.Activity", \
in_signature="", \ in_signature="", \
out_signature="") out_signature="")
def lost_focus(self): def lost_focus(self):
self.activity_on_lost_focus() self.activity_on_lost_focus()
@dbus.service.method("com.redhat.Sugar.Activity", \ @dbus.service.method("com.redhat.Sugar.Activity", \
in_signature="", \ in_signature="", \
out_signature="") out_signature="")
def got_focus(self): def got_focus(self):
self.activity_on_got_focus() self.activity_on_got_focus()
@dbus.service.method("com.redhat.Sugar.Activity", \ @dbus.service.method("com.redhat.Sugar.Activity", \
in_signature="", \ in_signature="", \
out_signature="") out_signature="")
def close_from_user(self): def close_from_user(self):
self.activity_on_close_from_user() self.activity_on_close_from_user()
def activity_get_id(self): def activity_get_id(self):
return self.__activity_id return self.__activity_id
# pure virtual methods
def activity_on_connected_to_shell(self): def __shutdown_reply_cb(self):
print "act %d: you need to override activity_on_connected_to_shell"%self.activity_get_id() print "in __reply_cb"
def activity_on_disconnected_from_shell(self): self.__plug.destroy()
print "act %d: you need to override activity_on_disconnected_to_shell"%self.activity_get_id() self.__plug = None
def activity_on_close_from_user(self): self.__bus = None
print "act %d: you need to override activity_on_close_from_user"%self.activity_get_id() self.__activity_container_object = None
self.__activity_container = None
self.__activity_object = None
self.__service = None
def activity_on_lost_focus(self): self.__bus.remove_signal_receiver(self.name_owner_changed, dbus_interface = "org.freedesktop.DBus", signal_name = "NameOwnerChanged")
print "act %d: you need to override activity_on_lost_focus"%self.activity_get_id()
def activity_on_got_focus(self): self.activity_on_disconnected_from_shell()
print "act %d: you need to override activity_on_got_focus"%self.activity_get_id()
del self
def __shutdown_error_cb(self, error):
print "in __error_cb"
def activity_shutdown(self):
self.__activity_object.shutdown(reply_handler = self.__shutdown_reply_cb, error_handler = self.__shutdown_error_cb)
# pure virtual methods
def activity_on_connected_to_shell(self):
print "act %d: you need to override activity_on_connected_to_shell"%self.activity_get_id()
def activity_on_disconnected_from_shell(self):
print "act %d: you need to override activity_on_disconnected_from_shell"%self.activity_get_id()
def activity_on_close_from_user(self):
print "act %d: you need to override activity_on_close_from_user"%self.activity_get_id()
def activity_on_lost_focus(self):
print "act %d: you need to override activity_on_lost_focus"%self.activity_get_id()
def activity_on_got_focus(self):
print "act %d: you need to override activity_on_got_focus"%self.activity_get_id()
def my_exit(): def my_exit():
sys.exit(0) sys.exit(0)
def deferred_exit(): def deferred_exit():
gobject.timeout_add(0, my_exit) gobject.timeout_add(0, my_exit)
################################################################################ ################################################################################
@ -269,6 +297,9 @@ class BrowserActivity(Activity):
def __title_cb(self, embed): def __title_cb(self, embed):
self.activity_set_tab_text(embed.get_title()) self.activity_set_tab_text(embed.get_title())
def activity_on_close_from_user(self):
self.activity_shutdown()
class WebActivity(Activity): class WebActivity(Activity):
def __init__(self): def __init__(self):
Activity.__init__(self) Activity.__init__(self)

View File

@ -1,5 +1,3 @@
# -*- tab-width: 4; indent-tabs-mode: t -*-
import presence import presence
import avahi import avahi
@ -16,13 +14,6 @@ class Buddy(object):
self._host = host self._host = host
self._address = address self._address = address
self._port = port self._port = port
self._chat = None
def set_chat(self, chat):
self._chat = chat
def chat(self):
return self._chat
def nick(self): def nick(self):
return self._nick return self._nick
@ -75,12 +66,6 @@ class BuddyList(object):
return buddy return buddy
return None return None
def find_buddy_by_address(self, address):
for buddy in self._buddies.keys():
if buddy.address() == address:
return buddy
return None
def _notify_listeners(self, action, buddy): def _notify_listeners(self, action, buddy):
for listener in self._listeners: for listener in self._listeners:
listener(action, buddy) listener(action, buddy)

View File

@ -30,6 +30,31 @@
<property name="row_spacing">0</property> <property name="row_spacing">0</property>
<property name="column_spacing">0</property> <property name="column_spacing">0</property>
<child>
<widget class="GtkTextView" id="chatView">
<property name="visible">True</property>
<property name="editable">True</property>
<property name="overwrite">False</property>
<property name="accepts_tab">True</property>
<property name="justification">GTK_JUSTIFY_LEFT</property>
<property name="wrap_mode">GTK_WRAP_NONE</property>
<property name="cursor_visible">True</property>
<property name="pixels_above_lines">0</property>
<property name="pixels_below_lines">0</property>
<property name="pixels_inside_wrap">0</property>
<property name="left_margin">0</property>
<property name="right_margin">0</property>
<property name="indent">0</property>
<property name="text" translatable="yes"></property>
</widget>
<packing>
<property name="left_attach">0</property>
<property name="right_attach">1</property>
<property name="top_attach">0</property>
<property name="bottom_attach">1</property>
</packing>
</child>
<child> <child>
<widget class="GtkEntry" id="entry"> <widget class="GtkEntry" id="entry">
<property name="visible">True</property> <property name="visible">True</property>
@ -86,70 +111,6 @@
<property name="y_options">fill</property> <property name="y_options">fill</property>
</packing> </packing>
</child> </child>
<child>
<widget class="GtkVBox" id="vbox1">
<property name="visible">True</property>
<property name="homogeneous">False</property>
<property name="spacing">0</property>
<child>
<widget class="GtkLabel" id="chatLabel">
<property name="visible">True</property>
<property name="label" translatable="yes"></property>
<property name="use_underline">False</property>
<property name="use_markup">False</property>
<property name="justify">GTK_JUSTIFY_LEFT</property>
<property name="wrap">False</property>
<property name="selectable">False</property>
<property name="xalign">0.5</property>
<property name="yalign">0.5</property>
<property name="xpad">0</property>
<property name="ypad">0</property>
<property name="ellipsize">PANGO_ELLIPSIZE_NONE</property>
<property name="width_chars">-1</property>
<property name="single_line_mode">False</property>
<property name="angle">0</property>
</widget>
<packing>
<property name="padding">0</property>
<property name="expand">False</property>
<property name="fill">False</property>
</packing>
</child>
<child>
<widget class="GtkTextView" id="chatView">
<property name="visible">True</property>
<property name="editable">False</property>
<property name="overwrite">False</property>
<property name="accepts_tab">True</property>
<property name="justification">GTK_JUSTIFY_LEFT</property>
<property name="wrap_mode">GTK_WRAP_NONE</property>
<property name="cursor_visible">True</property>
<property name="pixels_above_lines">0</property>
<property name="pixels_below_lines">0</property>
<property name="pixels_inside_wrap">0</property>
<property name="left_margin">0</property>
<property name="right_margin">0</property>
<property name="indent">0</property>
<property name="text" translatable="yes"></property>
</widget>
<packing>
<property name="padding">0</property>
<property name="expand">True</property>
<property name="fill">True</property>
</packing>
</child>
</widget>
<packing>
<property name="left_attach">0</property>
<property name="right_attach">1</property>
<property name="top_attach">0</property>
<property name="bottom_attach">1</property>
<property name="x_options">fill</property>
</packing>
</child>
</widget> </widget>
</child> </child>
</widget> </widget>

View File

@ -1,5 +1,4 @@
#!/usr/bin/python -t #!/usr/bin/python -t
# -*- tab-width: 4; indent-tabs-mode: t -*-
import os, sys, pwd import os, sys, pwd
sys.path.append(os.getcwd()) sys.path.append(os.getcwd())
@ -13,46 +12,9 @@ import BuddyList
glade_dir = os.getcwd() glade_dir = os.getcwd()
class Chat(object):
def __init__(self, view, label):
self._buffer = gtk.TextBuffer()
self._view = view
self._label = label
def activate(self, label):
self._view.set_buffer(self._buffer)
self._label.set_text(label)
def recv_message(self, buddy, msg):
aniter = self._buffer.get_end_iter()
self._buffer.insert(aniter, buddy.nick() + ": " + msg + "\n")
class GroupChat(Chat):
def __init__(self, parent, view, label):
Chat.__init__(self, view, label)
self._parent = parent
self._gc_controller = network.GroupChatController('224.0.0.221', 6666, self._recv_group_message)
self._gc_controller.start()
self._label_prefix = "Cha"
def activate(self):
Chat.activate(self, "Group Chat")
def send_message(self, text):
if len(text) > 0:
self._gc_controller.send_msg(text)
def _recv_group_message(self, msg):
buddy = self._parent.find_buddy_by_address(msg['addr'])
if buddy:
self.recv_message(buddy, msg['data'])
class ChatApp(SimpleGladeApp): class ChatApp(SimpleGladeApp):
def __init__(self, glade_file="chat.glade", root="mainWindow", domain=None, **kwargs): def __init__(self, glade_file="chat.glade", root="mainWindow", domain=None, **kwargs):
self._pannounce = presence.PresenceAnnounce() self._pannounce = presence.PresenceAnnounce()
self._buddy_list = BuddyList.BuddyList() self._buddy_list = BuddyList.BuddyList()
self._buddy_list.add_buddy_listener(self._on_buddy_presence_event) self._buddy_list.add_buddy_listener(self._on_buddy_presence_event)
@ -79,57 +41,43 @@ class ChatApp(SimpleGladeApp):
print "Selected %s" % name print "Selected %s" % name
def _on_buddyList_buddy_double_clicked(self, widget, *args): def _on_buddyList_buddy_double_clicked(self, widget, *args):
""" Select the chat for this buddy or group """
(model, aniter) = widget.get_selection().get_selected() (model, aniter) = widget.get_selection().get_selected()
chat = None name = self.treemodel.get(aniter,0)
buddy = self.treemodel.get_value(aniter, 1) print "Double-clicked %s" % name
if not buddy:
chat = self._group_chat
else:
chat = buddy.chat()
if chat:
chat.activate()
else:
# start a new chat with them
pass
def _on_buddy_presence_event(self, action, buddy): def _on_buddy_presence_event(self, action, buddy):
if action == BuddyList.ACTION_BUDDY_ADDED: if action == BuddyList.ACTION_BUDDY_ADDED:
aniter = self.treemodel.append(None) aniter = self.treemodel.insert_after(None,None)
self.treemodel.set(aniter, 0, buddy.nick(), 1, buddy) self.treemodel.set(aniter, 0, buddy.nick())
elif action == BuddyList.ACCTION_BUDDY_REMOVED: elif action == BuddyList.ACCTION_BUDDY_REMOVED:
aniter = self.treemodel.get_iter(buddy.nick()) aniter = self.treemodel.get_iter(buddy.nick())
if aniter: if aniter:
self.treemodel.remove(iter) self.treemodel.remove(iter)
def find_buddy_by_address(self, address):
return self._buddy_list.find_buddy_by_address(address)
def _on_main_window_delete(self, widget, *args): def _on_main_window_delete(self, widget, *args):
self.quit() self.quit()
def _get_current_chat(self): def _recv_group_message(self, msg):
selection = self.buddyListView.get_selection() aniter = self._group_chat_buffer.get_end_iter()
(model, aniter) = selection.get_selected() self._group_chat_buffer.insert(aniter, msg['data'] + "\n")
buddy = model.get_value(aniter, 1) # print "Message: %s" % msg['data']
if not buddy:
return self._group_chat
return buddy.chat()
def _send_chat_message(self, widget, *args): def _send_group_message(self, widget, *args):
chat = self._get_current_chat()
text = widget.get_text() text = widget.get_text()
chat.send_message(text) if len(text) > 0:
self._gc_controller.send_msg(text)
widget.set_text("") widget.set_text("")
def new(self): def new(self):
self.treemodel = gtk.TreeStore(gobject.TYPE_STRING, gobject.TYPE_PYOBJECT) self._group_chat_buffer = gtk.TextBuffer()
self.chatView.set_buffer(self._group_chat_buffer)
self.treemodel = gtk.TreeStore(gobject.TYPE_STRING)
self.buddyListView.set_model(self.treemodel) self.buddyListView.set_model(self.treemodel)
self.buddyListView.connect("cursor-changed", self._on_buddyList_buddy_selected) self.buddyListView.connect("cursor-changed", self._on_buddyList_buddy_selected)
self.buddyListView.connect("row-activated", self._on_buddyList_buddy_double_clicked) self.buddyListView.connect("row-activated", self._on_buddyList_buddy_double_clicked)
self.mainWindow.connect("delete-event", self._on_main_window_delete) self.mainWindow.connect("delete-event", self._on_main_window_delete)
self.entry.connect("activate", self._send_chat_message) self.entry.connect("activate", self._send_group_message)
renderer = gtk.CellRendererText() renderer = gtk.CellRendererText()
column = gtk.TreeViewColumn("", renderer, text=0) column = gtk.TreeViewColumn("", renderer, text=0)
@ -138,14 +86,12 @@ class ChatApp(SimpleGladeApp):
column.set_expand(True); column.set_expand(True);
self.buddyListView.append_column(column) self.buddyListView.append_column(column)
self._group_chat = GroupChat(self, self.chatView, self.chatLabel)
aniter = self.treemodel.append(None)
self.treemodel.set(aniter, 0, "Group", 1, None)
self._group_chat.activate()
self._pannounce.register_service(self._realname, 6666, presence.OLPC_CHAT_SERVICE, self._pannounce.register_service(self._realname, 6666, presence.OLPC_CHAT_SERVICE,
name = self._nick, realname = self._realname) name = self._nick, realname = self._realname)
self._gc_controller = network.GroupChatController('224.0.0.221', 6666, self._recv_group_message)
self._gc_controller.start()
def cleanup(self): def cleanup(self):
pass pass

View File

@ -1,5 +1,3 @@
# -*- tab-width: 4; indent-tabs-mode: t -*-
import socket import socket
import threading import threading
import traceback import traceback

View File

@ -1,5 +1,3 @@
# -*- tab-width: 4; indent-tabs-mode: t -*-
import avahi, dbus, dbus.glib import avahi, dbus, dbus.glib
OLPC_CHAT_SERVICE = "_olpc_chat._udp" OLPC_CHAT_SERVICE = "_olpc_chat._udp"

View File

@ -10,7 +10,7 @@ import gobject
import pygtk import pygtk
pygtk.require('2.0') pygtk.require('2.0')
import gtk import gtk
import pango
activity_counter = 0 activity_counter = 0
@ -34,31 +34,42 @@ class ActivityHost(dbus.service.Object):
self.socket.set_data("sugar-activity", self) self.socket.set_data("sugar-activity", self)
self.socket.show() self.socket.show()
hbox = gtk.HBox(); hbox = gtk.HBox(False, 4);
self.tab_activity_image = gtk.Image() self.tab_activity_image = gtk.Image()
self.tab_activity_image.set_from_stock(gtk.STOCK_CONVERT, gtk.ICON_SIZE_MENU) self.tab_activity_image.set_from_stock(gtk.STOCK_CONVERT, gtk.ICON_SIZE_MENU)
self.tab_activity_image.show() hbox.pack_start(self.tab_activity_image)
self.tab_activity_image.show()
label_hbox = gtk.HBox(False, 4);
label_hbox.connect("style-set", self.__tab_label_style_set_cb)
hbox.pack_start(label_hbox)
self.tab_label = gtk.Label(self.activity_name) self.tab_label = gtk.Label(self.activity_name)
self.tab_label.set_ellipsize(pango.ELLIPSIZE_END)
self.tab_label.set_single_line_mode(True)
self.tab_label.set_alignment(0.0, 0.5)
self.tab_label.set_padding(0, 0)
self.tab_label.show() self.tab_label.show()
self.tab_close_button = gtk.Button()
settings = self.tab_close_button.get_settings()
[w, h] = gtk.icon_size_lookup_for_settings(settings, gtk.ICON_SIZE_MENU)
self.tab_close_button.set_size_request(w + 2, h + 2)
close_image = gtk.Image() close_image = gtk.Image()
close_image.set_from_stock (gtk.STOCK_CLOSE, gtk.ICON_SIZE_MENU) close_image.set_from_stock (gtk.STOCK_CLOSE, gtk.ICON_SIZE_MENU)
close_image.show() close_image.show()
self.tab_close_button = gtk.Button()
rcstyle = gtk.RcStyle();
rcstyle.xthickness = rcstyle.ythickness = 0;
self.tab_close_button.modify_style (rcstyle);
self.tab_close_button.add(close_image) self.tab_close_button.add(close_image)
self.tab_close_button.set_relief(gtk.RELIEF_NONE) self.tab_close_button.set_relief(gtk.RELIEF_NONE)
self.tab_close_button.set_focus_on_click(gtk.FALSE) self.tab_close_button.set_focus_on_click(gtk.FALSE)
self.tab_close_button.show() self.tab_close_button.show()
self.tab_close_button.connect("clicked", self.tab_close_button_clicked) self.tab_close_button.connect("clicked", self.tab_close_button_clicked)
hbox.set_spacing(4) label_hbox.pack_start(self.tab_label)
hbox.pack_start(self.tab_activity_image) label_hbox.pack_start(self.tab_close_button, False, False, 0)
hbox.pack_start(self.tab_label) label_hbox.show()
hbox.pack_start(self.tab_close_button)
hbox.show() hbox.show()
notebook = self.activity_container.notebook notebook = self.activity_container.notebook
@ -117,6 +128,13 @@ class ActivityHost(dbus.service.Object):
def get_object_path(self): def get_object_path(self):
return self.dbus_object_name return self.dbus_object_name
def __tab_label_style_set_cb(self, widget, previous_style):
context = widget.get_pango_context()
metrics = context.get_metrics(widget.style.font_desc, context.get_language())
char_width = metrics.get_approximate_digit_width()
[w, h] = gtk.icon_size_lookup_for_settings(widget.get_settings(), gtk.ICON_SIZE_MENU)
widget.set_size_request(15 * pango.PIXELS(char_width) + 2 * w, -1);
self.tab_close_button.set_size_request (w + 5, h + 2)
class ActivityContainer(dbus.service.Object): class ActivityContainer(dbus.service.Object):