-
Notifications
You must be signed in to change notification settings - Fork 2
/
hignotebooks.py
226 lines (171 loc) · 6.88 KB
/
hignotebooks.py
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2005-2006 Insecure.Com LLC.
# Copyright (C) 2007-2008 Adriano Monteiro Marques
#
# Author: Adriano Monteiro Marques <adriano@umitproject.org>
# Cleber Rodrigues <cleber.gnu@gmail.com>
# Francesco Piccinno <stack.box@gmail.com>
#
# This library is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# This library 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 Lesser General Public
# License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this library; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
import gtk
import gobject
from higwidgets.higspinner import HIGSpinner
from higwidgets.higboxes import HIGHBox
from higwidgets.higbuttons import HIGButton
class HIGEditableLabel(gtk.EventBox):
# called when label is changed .. if returns True new_value is setted
__gsignals__ = {'title-edited' : (gobject.SIGNAL_RUN_LAST,
gobject.TYPE_BOOLEAN,
(gobject.TYPE_STRING,
gobject.TYPE_STRING))}
def __init__(self, label=''):
gobject.GObject.__init__(self)
self.label = gtk.Label(label)
self.entry = gtk.Entry()
self.lock = False
box = gtk.HBox()
self.add(box)
box.pack_start(self.label, False, False, 0)
box.pack_start(self.entry, False, False, 0)
self.set_visible_window(False)
self.show_all()
self.entry.connect('activate', self.on_entry_activated)
self.entry.connect('focus-out-event', self.on_lost_focus)
self.connect('realize', self.on_realize_event)
self.connect('button-press-event', self.on_button_press_event)
def on_lost_focus(self, widget, event):
self.on_entry_activated(widget)
def on_entry_activated(self, widget):
# Muttex for focus
if self.lock:
return False
self.lock = True
old_text = self.label.get_text()
new_text = self.entry.get_text()
self.switch_mode(False)
# If returns True we can change the label
if self.emit('title-edited', old_text, new_text):
self.label.set_text(new_text)
self.lock = False
return False
def on_realize_event(self, widget):
self.entry.hide()
def on_button_press_event(self, widget, event):
if event.type == gtk.gdk._2BUTTON_PRESS:
self.switch_mode(True)
def switch_mode(self, editing):
"""Switches from editing (True) to label mode (False).
"""
if editing:
self.entry.set_text(self.label.get_text())
self.entry.grab_focus()
self.label.hide()
self.entry.show()
else:
self.entry.set_text('')
self.label.show()
self.entry.hide()
# Reallocate widget
self.set_size_request(-1, -1)
# Getters/setters for compatibility
def get_text(self):
return self.label.get_text()
def set_text(self, label):
self.label.set_text(label)
def get_label(self):
return self.label.get_label()
def set_label(self, label):
self.label.set_text(label)
gobject.type_register(HIGEditableLabel)
HIGAnimatedLabel = HIGEditableLabel
gtk.rc_parse_string("""
style "thinWidget" {
xthickness = 0
ythickness = 0
}
widget "*.tabNotebookButton" style "thinWidget"
""")
class HIGNotebook(gtk.Notebook):
def __init__(self, popup=True):
gtk.Notebook.__init__(self)
if popup:
self.popup_enable()
class HIGClosableTabLabel(HIGHBox):
__gsignals__ = { 'close-clicked' : (gobject.SIGNAL_RUN_LAST,
gobject.TYPE_NONE, ()) }
def __init__(self, label_text=""):
gobject.GObject.__init__(self)
#HIGHBox.__init__(self, spacing=4)
self.label_text = label_text
self.__create_widgets()
#self.propery_map = {"label_text" : self.label.get_label}
def __create_widgets(self):
self.label = HIGAnimatedLabel(self.label_text)
self.editing = False
self.image = gtk.image_new_from_stock(gtk.STOCK_CLOSE,
gtk.ICON_SIZE_MENU)
self.button = HIGButton()
self.button.set_relief(gtk.RELIEF_NONE)
self.button.set_focus_on_click(False)
self.button.add(self.image)
self.button.set_name('tabNotebookButton')
self.button.connect('clicked', self.__on_button_clicked)
self.button.connect('style-set', self.__on_button_style_set)
self.label.connect('button-press-event', self.on_button_press_event)
self.label.entry.connect('focus-out-event', self.on_entry_focus_out)
self.pack_start(self.label, False, False, 0)
self.pack_end(self.button, False, False, 0)
self.show_all()
self.switch_button_mode(False)
def on_entry_focus_out(self, widget, event):
self.switch_button_mode(False)
def on_button_press_event(self, widget, event):
if event.type == gtk.gdk._2BUTTON_PRESS:
self.switch_button_mode(True)
def switch_button_mode(self, mode):
"""Switch button from editing mode (True) to label mode (False)
"""
if mode:
self.image.set_from_stock(gtk.STOCK_APPLY, gtk.ICON_SIZE_MENU)
else:
self.image.set_from_stock(gtk.STOCK_CLOSE, gtk.ICON_SIZE_MENU)
self.editing = mode
parent = self.get_parent()
if parent:
parent.queue_draw()
def __on_button_clicked(self, widget):
if self.editing:
self.label.on_entry_activated(self.label.entry)
self.switch_button_mode(False)
else:
self.emit('close-clicked')
def __on_button_style_set(self, widget, prev_style):
w, h = gtk.icon_size_lookup_for_settings(self.image.get_settings(),
gtk.ICON_SIZE_MENU)
self.image.set_size_request(w, h)
def get_text(self):
return self.label.get_text()
def set_text(self, text):
self.label.set_text(text)
def get_label(self):
return self.label.get_label()
def set_label(self, label):
self.label.set_text(label)
def get_animated_label(self):
return self.label
gobject.type_register(HIGClosableTabLabel)
HIGAnimatedTabLabel = HIGClosableTabLabel