~ubuntu-branches/ubuntu/karmic/ibus/karmic-updates

« back to all changes in this revision

Viewing changes to ui/gtk/candidatepanel.py

  • Committer: Bazaar Package Importer
  • Author(s): LI Daobing
  • Date: 2009-06-13 11:39:05 UTC
  • mfrom: (1.1.3 upstream) (6.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20090613113905-0mfi5hhoz8w9y6k2
Tags: 1.1.0.20090612-1
* new upstream release.
* Fix "python-ibus should depends on iso-codes" (Closes: #532163)
* debian/libibus0.symbols: update symbols

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
# Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20
20
# Boston, MA  02111-1307  USA
21
21
 
 
22
import operator
22
23
import gtk
23
24
import gtk.gdk as gdk
24
25
import gobject
26
27
import ibus
27
28
from ibus._gtk import PangoAttrList
28
29
 
29
 
class Label(gtk.Label): pass
 
30
class EventBox(gtk.EventBox):
 
31
    def __init__(self):
 
32
        super(EventBox, self).__init__()
 
33
        self.connect("realize", self.__realize_cb)
 
34
 
 
35
    def __realize_cb(self, widget):
 
36
        widget.window.set_cursor(gdk.Cursor(gdk.HAND2))
 
37
 
 
38
gobject.type_register(EventBox, "IBusEventBox")
 
39
 
 
40
class Label(gtk.Label):
 
41
    pass
30
42
gobject.type_register(Label, "IBusPanelLabel")
31
43
 
32
44
class HSeparator(gtk.HBox):
33
 
    def __init__ (self):
34
 
        gtk.HBox.__init__ (self)
 
45
    def __init__(self):
 
46
        super(HSeparator, self).__init__()
35
47
        self.pack_start(gtk.HSeparator(), True, True, 4)
36
48
 
37
49
class VSeparator(gtk.VBox):
38
 
    def __init__ (self):
39
 
        gtk.VBox.__init__ (self)
 
50
    def __init__(self):
 
51
        super(VSeparator, self).__init__()
40
52
        self.pack_start(gtk.VSeparator(), True, True, 4)
41
53
 
42
54
class CandidateArea(gtk.HBox):
 
55
    __gsignals__ = {
 
56
        "candidate-clicked" : (
 
57
            gobject.SIGNAL_RUN_FIRST,
 
58
            gobject.TYPE_NONE,
 
59
            (gobject.TYPE_UINT, gobject.TYPE_UINT, gobject.TYPE_UINT )),
 
60
    }
 
61
 
43
62
    def __init__ (self, orientation):
44
63
        gtk.HBox.__init__ (self)
45
64
        self.__orientation = orientation
46
65
        self.__labels = []
 
66
        self.__candidates = []
47
67
        self.__create_ui()
48
68
 
49
69
    def __create_ui(self):
56
76
            self.pack_start(VSeparator(), False, False, 0)
57
77
            self.pack_start(self.__vbox2, True, True, 4)
58
78
 
59
 
        for i in xrange(1, 11):
60
 
            label1 = Label("%d." % (i % 10))
 
79
        for i in range(0, 16):
 
80
            label1 = Label("%c." % ("1234567890abcdef"[i]))
61
81
            label1.set_alignment(0.0, 0.5)
62
 
            label1.set_no_show_all(True)
 
82
            label1.show()
63
83
 
64
84
            label2 = Label()
65
85
            label2.set_alignment(0.0, 0.5)
66
 
            label2.set_no_show_all(True)
 
86
            label1.show()
67
87
 
68
88
            if self.__orientation == gtk.ORIENTATION_VERTICAL:
69
89
                label1.set_property("xpad", 8)
70
90
                label2.set_property("xpad", 8)
71
 
                self.__vbox1.pack_start(label1, False, False, 2)
72
 
                self.__vbox2.pack_start(label2, False, False, 2)
 
91
                ebox1 = EventBox()
 
92
                ebox1.set_no_show_all(True)
 
93
                ebox1.add(label1)
 
94
                ebox2 = EventBox()
 
95
                ebox2.set_no_show_all(True)
 
96
                ebox2.add(label2)
 
97
                self.__vbox1.pack_start(ebox1, False, False, 2)
 
98
                self.__vbox2.pack_start(ebox2, False, False, 2)
 
99
                self.__candidates.append((ebox1, ebox2))
73
100
            else:
74
101
                hbox = gtk.HBox()
 
102
                hbox.show()
75
103
                hbox.pack_start(label1, False, False, 1)
76
104
                hbox.pack_start(label2, False, False, 1)
77
 
                self.pack_start(hbox, False, False, 4)
 
105
                ebox = EventBox()
 
106
                ebox.set_no_show_all(True)
 
107
                ebox.add(hbox)
 
108
                self.pack_start(ebox, False, False, 4)
 
109
                self.__candidates.append((ebox,))
78
110
 
79
111
            self.__labels.append((label1, label2))
80
112
 
81
 
        self.__labels[0][0].show()
82
 
        self.__labels[0][1].show()
 
113
        cursor = gdk.Cursor(gdk.HAND1)
 
114
        for i, ws in enumerate(self.__candidates):
 
115
            for w in ws:
 
116
                w.connect("button-press-event", lambda w, e, i:self.emit("candidate-clicked", i, e.button, e.state), i)
 
117
 
 
118
    def set_labels(self, labels):
 
119
        if not labels:
 
120
            for i in xrange(0, 16):
 
121
                self.__labels[i][0].set_text("%c." % ("1234567890abcdef"[i]))
 
122
                self.__labels[i][0].set_property("attributes", None)
 
123
            return
 
124
 
 
125
        i = 0
 
126
        for text, attrs in labels:
 
127
            self.__labels[i][0].set_text(text)
 
128
            self.__labels[i][0].set_property("attributes", attrs)
 
129
            i += 1
 
130
            if i >= 16:
 
131
                break
83
132
 
84
133
    def set_candidates(self, candidates, focus_candidate = 0, show_cursor = True):
85
134
        assert len(candidates) <= len(self.__labels)
86
 
        i = 0
87
 
        for text, attrs in candidates:
 
135
        for i, (text, attrs) in enumerate(candidates):
88
136
            if i == focus_candidate and show_cursor:
89
137
                if attrs == None:
90
138
                    attrs = pango.AttrList()
97
145
                attrs.insert(attr)
98
146
 
99
147
            self.__labels[i][1].set_text(text)
 
148
            self.__labels[i][1].show()
100
149
            self.__labels[i][1].set_property("attributes", attrs)
101
 
            self.__labels[i][0].show()
102
 
            self.__labels[i][1].show()
103
 
 
104
 
            i += 1
105
 
 
106
 
        for label1, label2 in self.__labels[max(1, len(candidates)):]:
107
 
            label1.hide()
108
 
            label2.hide()
109
 
 
110
 
        if len(candidates) == 0:
111
 
            self.__labels[0][0].set_text("")
112
 
            self.__labels[0][1].set_text("")
113
 
        else:
114
 
            self.__labels[0][0].set_text("1.")
 
150
            for w in self.__candidates[i]:
 
151
                w.show()
 
152
 
 
153
        for w in reduce(operator.add, self.__candidates[len(candidates):]):
 
154
            w.hide()
115
155
 
116
156
class CandidatePanel(gtk.VBox):
117
157
    __gproperties__ = {
131
171
            gobject.SIGNAL_RUN_FIRST,
132
172
            gobject.TYPE_NONE,
133
173
            ()),
 
174
        "page-up" : (
 
175
            gobject.SIGNAL_RUN_FIRST,
 
176
            gobject.TYPE_NONE,
 
177
            ()),
 
178
        "page-down" : (
 
179
            gobject.SIGNAL_RUN_FIRST,
 
180
            gobject.TYPE_NONE,
 
181
            ()),
 
182
        "candidate-clicked" : (
 
183
            gobject.SIGNAL_RUN_FIRST,
 
184
            gobject.TYPE_NONE,
 
185
            (gobject.TYPE_UINT, gobject.TYPE_UINT, gobject.TYPE_UINT)),
134
186
    }
135
187
 
136
188
    def __init__ (self):
193
245
        # create candidates area
194
246
        self.__candidate_area = CandidateArea(self.__orientation)
195
247
        self.__candidate_area.set_no_show_all(True)
 
248
        self.__candidate_area.connect("candidate-clicked", lambda x, i, b, s: self.emit("candidate-clicked", i, b, s))
196
249
        self.update_lookup_table(self.__lookup_table, self.__lookup_table_visible)
197
250
 
198
251
        # create state label
201
254
 
202
255
        # create buttons
203
256
        self.__prev_button = gtk.Button()
204
 
        self.__prev_button.connect("clicked", lambda x: self.emit("cursor-up"))
 
257
        self.__prev_button.connect("clicked", lambda x: self.emit("page-up"))
205
258
        self.__prev_button.set_relief(gtk.RELIEF_NONE)
206
 
        self.__tooltips.set_tip(self.__prev_button, "Previous candidate")
 
259
        self.__tooltips.set_tip(self.__prev_button, "Previous page")
207
260
 
208
261
        self.__next_button = gtk.Button()
209
 
        self.__next_button.connect("clicked", lambda x: self.emit("cursor-down"))
 
262
        self.__next_button.connect("clicked", lambda x: self.emit("page-down"))
210
263
        self.__next_button.set_relief(gtk.RELIEF_NONE)
211
 
        self.__tooltips.set_tip(self.__next_button, "Next candidate")
 
264
        self.__tooltips.set_tip(self.__next_button, "Next page")
212
265
 
213
266
        self.__pack_all_widgets()
214
267
 
222
275
            image = gtk.Image()
223
276
            image.set_from_stock(gtk.STOCK_GO_DOWN, gtk.ICON_SIZE_MENU)
224
277
            self.__next_button.set_image(image)
 
278
 
225
279
            vbox = gtk.VBox()
226
280
            vbox.pack_start(self.__preedit_label, False, False, 0)
227
281
            vbox.pack_start(self.__aux_label, False, False, 0)
304
358
        self.__aux_attrs = attrs
305
359
        self.__aux_label.set_attributes(attrs)
306
360
 
 
361
    def __refresh_labels(self):
 
362
        labels = self.__lookup_table.get_labels()
 
363
        if labels:
 
364
            labels = map(lambda x: (x.text, PangoAttrList(x.attributes, x.text)), labels)
 
365
        else:
 
366
            labels = None
 
367
        self.__candidate_area.set_labels(labels)
 
368
 
 
369
 
307
370
    def __refresh_candidates(self):
308
371
        candidates = self.__lookup_table.get_candidates_in_current_page()
309
372
        candidates = map(lambda x: (x.text, PangoAttrList(x.attributes, x.text)), candidates)
323
386
 
324
387
        self.__lookup_table = lookup_table
325
388
        self.__refresh_candidates()
 
389
        self.__refresh_labels()
326
390
 
327
391
    def show_lookup_table(self):
328
392
        self.__lookup_table_visible = True