/usr/share/pyshared/pychess/widgets/ToggleComboBox.py is in pychess 0.10.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | import pygtk
pygtk.require("2.0")
import gtk
from gobject import *
from pychess.System.Log import log
from pychess.Utils.IconLoader import load_icon
class ToggleComboBox (gtk.ToggleButton):
__gsignals__ = {'changed' : (SIGNAL_RUN_FIRST, TYPE_NONE, (TYPE_INT,))}
def __init__ (self):
gtk.ToggleButton.__init__(self)
self.set_relief(gtk.RELIEF_NONE)
self.label = label = gtk.Label()
label.set_alignment(0, 0.5)
self.hbox = hbox = gtk.HBox()
self.image = gtk.Image()
hbox.pack_start(self.image, False, False)
hbox.pack_start(label)
arrow = gtk.Arrow (gtk.ARROW_DOWN, gtk.SHADOW_OUT);
hbox.pack_start(arrow, False, False)
self.add(hbox)
self.show_all()
self.connect("button_press_event", self.button_press)
self.connect("key_press_event", self.key_press)
self.connect("scroll_event", self.scroll_event)
self.menu = menu = gtk.Menu()
deactivate = lambda w: self.set_active(False)
menu.connect("deactivate", deactivate)
menu.attach_to_widget(self, None)
self.markup = "", ""
self._active = -1
self._items = []
def _get_active(self):
return self._active
def _set_active(self, active):
if type(active) != int:
raise TypeError
if active == self._active: return
if active >= len(self._items):
log.warn("Tried to set combobox to %d, but it has only got %d items"
% (active, len(self._items)))
return
oldactive = self._active
# take care the case when last used engine was uninstalled
self._active = (active < len(self._items) and [active] or [1])[0]
self.emit("changed", oldactive)
text, icon = self._items[self._active]
self.label.set_markup (self.markup[0] + text + self.markup[1])
if icon != None:
self.hbox.set_spacing(6)
self.image.set_from_pixbuf(icon)
else:
self.hbox.set_spacing(0)
self.image.clear()
active = property(_get_active, _set_active)
def setMarkup(self, start, end):
self.markup = (start, end)
text = self._items[self.active][0]
self.label.set_markup (self.markup[0] + text + self.markup[1])
def getMarkup(self):
return self.markup
def addItem (self, text, stock=None):
if stock == None:
item = gtk.MenuItem(text)
else:
item = gtk.MenuItem()
label = gtk.Label(text)
label.props.xalign = 0
if type(stock) == str:
stock = load_icon(12, stock)
image = gtk.Image()
image.set_from_pixbuf(stock)
hbox = gtk.HBox()
hbox.set_spacing(6)
hbox.pack_start(image, expand=False, fill=False)
hbox.add(label)
item.add(hbox)
hbox.show_all()
item.connect("activate", self.menu_item_activate, len(self._items))
self.menu.append(item)
self._items += [(text, stock)]
item.show()
if self.active < 0: self.active = 0
def menuPos (self, menu):
x, y = self.window.get_origin()
x += self.get_allocation().x
y += self.get_allocation().y + self.get_allocation().height
return (x,y,False)
def scroll_event (self, widget, event):
if event.direction == gtk.gdk.SCROLL_UP:
if self.active > 0:
self.active -= 1
else:
if self.active < len(self._items)-1:
self.active += 1
def button_press (self, widget, event):
width = self.allocation.width
self.menu.set_size_request(-1,-1)
ownWidth = self.menu.size_request()[0]
self.menu.set_size_request(max(width,ownWidth),-1)
self.set_active(True)
self.menu.popup(None,None, self.menuPos, 1, event.time)
from gtk.gdk import keyval_from_name
keys = map(keyval_from_name,("space", "KP_Space", "Return", "KP_Enter"))
def key_press (self, widget, event):
if not event.keyval in self.keys: return
self.set_active(True)
self.menu.popup(None,None, self.menuPos, 1, event.time)
return True
def menu_item_activate (self, widget, index):
self.active = index
|