You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
125 lines
4.1 KiB
125 lines
4.1 KiB
#!/usr/bin/env python |
|
# -*- coding: utf-8 -*- |
|
|
|
# Mitter, a multiple-interface client for microblogging services. |
|
# Copyright (C) 2007-2010 Mitter Contributors |
|
# |
|
# 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, see <http://www.gnu.org/licenses/>. |
|
|
|
import gtk |
|
import logging |
|
|
|
_log = logging.getLogger('ui.pygtk.updatebox') |
|
|
|
class UpdateBox(gtk.VBox): |
|
"""Custom update box widget.""" |
|
def __init__(self, avatar, spell_check=True): |
|
super(UpdateBox, self).__init__(False, 0) |
|
|
|
self._reply_to = None |
|
|
|
self._text = gtk.TextView() |
|
self._text.set_property('wrap-mode', gtk.WRAP_WORD) |
|
|
|
self.avatar = gtk.Image() |
|
self.avatar.set_from_pixbuf(avatar) |
|
|
|
status_bar = gtk.HBox(False, 0) |
|
self._update_status = gtk.Label() |
|
|
|
close_button = gtk.Button(stock=gtk.STOCK_CLOSE) |
|
close_button.set_relief(gtk.RELIEF_NONE) |
|
close_button.connect('clicked', self.hide) |
|
|
|
status_bar = gtk.HBox(False, 0) |
|
status_bar.pack_start(self._update_status, expand=True, fill=True, |
|
padding=0) |
|
status_bar.pack_start(close_button, expand=False, fill=False, |
|
padding=0) |
|
|
|
update_bar = gtk.HBox(False, 3) |
|
update_bar.pack_start(self._text, expand=True, fill=True, |
|
padding=0) |
|
update_bar.pack_start(self.avatar, expand=False, fill=False, |
|
padding=0) |
|
|
|
self.pack_start(status_bar, expand=False, fill=True, padding=0) |
|
self.pack_start(update_bar, expand=False, fill=True, padding=0) |
|
|
|
# Spell checking the update box |
|
if spell_check: |
|
try: |
|
import gtkspell |
|
import locale |
|
language = locale.getlocale()[0] |
|
self.spell_check = gtkspell.Spell(self._update_text, language) |
|
_log.debug('Spell checking turned on with language: %s' \ |
|
% (language)) |
|
except: |
|
_log.debug('Error initializing spell checking: ' \ |
|
'spell checking disabled') |
|
|
|
def show(self, reply_to=None): |
|
"""Show the update box and all related widgets.""" |
|
super(UpdateBox, self).show() |
|
self._reply_to = reply_to |
|
self.show_all() |
|
_log.debug('show') |
|
return |
|
|
|
def hide(self, caller=None): |
|
"""Hide the update box and related widgets.""" |
|
super(UpdateBox, self).hide() |
|
self.text = '' |
|
self.hide_all() |
|
return |
|
|
|
def _count_chars(self, text_buffer): |
|
"""Count the number of chars in the edit field and update the |
|
label that shows the available space.""" |
|
count = len(self.text) |
|
|
|
if self._reply_to: |
|
suffix = _('(replying to %s)') % ( |
|
self._reply_to) |
|
else: |
|
suffix = '' |
|
|
|
# TODO: gettext to properly use "characters"/"character" |
|
text = N_('%d character %s', '%d characters %s', count) % (count, |
|
suffix) |
|
self._update_status.set_text(text) |
|
return True |
|
|
|
@property |
|
def text(self): |
|
"""Return the text in the update box.""" |
|
text_buffer = self._text.get_buffer() |
|
|
|
start = text_buffer.get_start_iter() |
|
end = text_buffer.get_end_iter() |
|
|
|
return text_buffer.get_text(start, end, include_hidden_chars=False) |
|
|
|
@text.setter |
|
def text(self, text): |
|
"""Set the textarea text.""" |
|
self._text.get_buffer().set_text(text) |
|
return |
|
|
|
def _set_pixbuf(self, pixbuf): |
|
"""Update the avatar pixbuf.""" |
|
self.avatar.set_from_pixbuf(pixbuf) |
|
# pixbuf can only be set, not get. |
|
pixbuf = property(None, _set_pixbuf) |