~ssh-rdp/deskbar-applet/history-count

« back to all changes in this revision

Viewing changes to src/applet.py

  • Committer: rslinckx
  • Date: 2005-09-13 16:36:46 UTC
  • Revision ID: vcs-imports@canonical.com-20050913163646-gx8uamx1ea5564up
Initial revision

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import deskbar
 
2
import deskbar.config
 
3
import deskbar.handlers
 
4
import gnomeapplet
 
5
import gobject
 
6
import gtk
 
7
import gtk.gdk
 
8
import sexy
 
9
 
 
10
 
 
11
class DeskbarApplet:
 
12
        def __init__(self, applet):
 
13
                self.applet = applet
 
14
                self.gconf = deskbar.config.GConfBackend(applet.get_preferences_key())
 
15
                
 
16
                self.entry = None
 
17
                self.config_width_policy = "fixed"
 
18
                self.config_width = -1
 
19
                self.on_config_width()
 
20
                self.on_config_width_policy()
 
21
                self.gconf.notify_add("/width", self.on_config_width)
 
22
                self.gconf.notify_add("/width_policy", self.on_config_width_policy)
 
23
                
 
24
                self.construct_gui()
 
25
 
 
26
 
 
27
        def construct_gui(self):
 
28
                self.entry = sexy.IconEntry()
 
29
                self.entry.connect("activate",           self.on_entry_activate)
 
30
                self.entry.connect("button-press-event", self.on_entry_button_press)
 
31
                self.entry.connect("changed",            self.on_entry_changed)
 
32
                self.entry.connect("icon_clicked",       self.on_entry_icon_clicked)
 
33
                self.entry.connect("key-press-event",    self.on_entry_key_press)
 
34
                self.entry.set_size_request(self.config_width, self.entry.size_request()[1])
 
35
                self.entry.set_icon(deskbar.DESKBAR_IMAGE)
 
36
                
 
37
                self.construct_completions()
 
38
                
 
39
                width = self.gconf.get_int("/width", deskbar.DEFAULT_WIDTH)
 
40
                width_policy = self.gconf.get_string("/width_policy", "fixed")
 
41
                
 
42
                self.applet.set_flags(gtk.CAN_FOCUS)
 
43
                self.applet.add(self.entry)
 
44
                self.applet.set_applet_flags(gnomeapplet.EXPAND_MINOR)
 
45
                self.applet.connect("button-press-event", self.on_applet_button_press)
 
46
                self.applet.setup_menu_from_file (None, "Deskbar_Applet.xml", None,
 
47
                        [("About", self.on_about), ("New", self.on_new), ("Prefs", self.on_preferences)])
 
48
 
 
49
                self.applet.show_all()
 
50
                self.entry.grab_focus()
 
51
 
 
52
 
 
53
        def construct_completions(self):
 
54
                # description, handler_name, handler_arg, pixbuf
 
55
                self.completionModel = gtk.ListStore(
 
56
                        gobject.TYPE_STRING, gobject.TYPE_STRING, gobject.TYPE_STRING, gtk.gdk.Pixbuf)
 
57
                completion = gtk.EntryCompletion()
 
58
                completion.set_popup_set_width(False)
 
59
                completion.set_match_func(lambda completion, key, it: True)
 
60
                completion.set_model(self.completionModel)
 
61
                completion.connect("match-selected", self.on_completion_selected)
 
62
                
 
63
                self.entry.set_completion(completion)
 
64
                
 
65
                crp = gtk.CellRendererPixbuf()
 
66
                completion.pack_start(crp)
 
67
                completion.add_attribute(crp, "pixbuf", 3)
 
68
                
 
69
                crt = gtk.CellRendererText()
 
70
                completion.pack_start(crt)
 
71
                completion.add_attribute(crt, "markup", 0)
 
72
                completion.add_attribute(crt, "text", 0)
 
73
                completion.set_property("text-column", 0)
 
74
                completion.notify("text-column")
 
75
 
 
76
 
 
77
        def on_about(self, component, verb):
 
78
                import deskbar.about
 
79
                deskbar.about.show_about()
 
80
 
 
81
 
 
82
        def on_new(self, component, verb):
 
83
                import deskbar.preferences
 
84
                deskbar.preferences.show_preferences(self.gconf, True)
 
85
 
 
86
 
 
87
        def on_preferences(self, component, verb):
 
88
                import deskbar.preferences
 
89
                deskbar.preferences.show_preferences(self.gconf)
 
90
 
 
91
 
 
92
        def on_config_width(self):
 
93
                width = self.gconf.get_int("/width", deskbar.DEFAULT_WIDTH)
 
94
                
 
95
                if self.config_width == width:
 
96
                        return
 
97
                self.config_width = width
 
98
                
 
99
                if self.entry != None:
 
100
                        esr = self.entry.size_request()
 
101
                        self.entry.set_size_request(width, esr[1])
 
102
 
 
103
 
 
104
        def on_config_width_policy(self):
 
105
                wp = self.gconf.get_string("/width_policy", "fixed")
 
106
                
 
107
                if self.config_width_policy == wp:
 
108
                        return
 
109
                self.config_width_policy = wp
 
110
                
 
111
                if wp == "maximal":
 
112
                        self.applet.set_applet_flags(gnomeapplet.EXPAND_MINOR | gnomeapplet.EXPAND_MAJOR)
 
113
                else:
 
114
                        self.applet.set_applet_flags(gnomeapplet.EXPAND_MINOR)
 
115
 
 
116
 
 
117
        def on_completion_selected(self, completion, model, iterator):
 
118
                row = model[iterator]
 
119
                handler_name, handler_arg = row[1], row[2]
 
120
                
 
121
                # we wrap the set_text call in the gobject idle loop, otherwise
 
122
                # messing around with the completion and its iterator can
 
123
                # cause crashers.
 
124
                idle_callback = lambda entry: entry.set_text("")
 
125
                gobject.idle_add(idle_callback, self.entry)
 
126
                
 
127
                deskbar.handlers.handle_with_specific_handler(handler_arg, handler_name)
 
128
 
 
129
 
 
130
        def on_entry_activate(self, widget):
 
131
                t = widget.get_text()
 
132
                widget.set_text("")
 
133
                deskbar.handlers.handle(t)
 
134
 
 
135
 
 
136
        def on_entry_changed(self, widget):
 
137
                self.completionModel.clear()
 
138
                deskbar.handlers.add_to_completions(widget.get_text(), self.completionModel, self.entry)
 
139
 
 
140
 
 
141
        def on_entry_icon_clicked(self, widget, button):
 
142
                # Icon clicks are passed through to the applet, so that we can
 
143
                # get both Fitt's Law goodness, and an Applet context menu (to
 
144
                # remove the applet, for example).
 
145
                event = gtk.gdk.Event(gtk.gdk.BUTTON_PRESS)
 
146
                event.button = button
 
147
                self.applet.emit("button-press-event", event)
 
148
 
 
149
 
 
150
        def on_applet_button_press(self, widget, event):
 
151
                # Left-Mouse-Button should focus the GtkEntry widget (for Fitt's Law
 
152
                # - so that a click on applet border on edge of screen activates the
 
153
                # most important widget).
 
154
                if event.button == 1:
 
155
                        self.entry.select_region(0, -1)
 
156
                        self.entry.grab_focus()
 
157
                        return True
 
158
                
 
159
                return False
 
160
 
 
161
 
 
162
        def on_entry_button_press(self, widget, event):
 
163
                if self.applet != None:
 
164
                        self.applet.request_focus(long(event.time))
 
165
                return False
 
166
 
 
167
 
 
168
        def on_entry_key_press(self, entry, event):
 
169
                # bind Escape to clear the GtkEntry
 
170
                if event.keyval == gtk.keysyms.Escape:
 
171
                        entry.set_text("")
 
172
                
 
173
                return False