| 1 | #!/usr/bin/env python |
|---|
| 2 | # -*- coding: utf-8 -*- |
|---|
| 3 | # |
|---|
| 4 | # Copyright (C) 2005-2006 Insecure.Com LLC. |
|---|
| 5 | # Copyright (C) 2007-2008 Adriano Monteiro Marques |
|---|
| 6 | # |
|---|
| 7 | # Author: Adriano Monteiro Marques <adriano@umitproject.org> |
|---|
| 8 | # Cleber Rodrigues <cleber.gnu@gmail.com> |
|---|
| 9 | # Francesco Piccinno <stack.box@gmail.com> |
|---|
| 10 | # |
|---|
| 11 | # This library is free software; you can redistribute it and/or modify |
|---|
| 12 | # it under the terms of the GNU Lesser General Public License as published |
|---|
| 13 | # by the Free Software Foundation; either version 2.1 of the License, or |
|---|
| 14 | # (at your option) any later version. |
|---|
| 15 | # |
|---|
| 16 | # This library is distributed in the hope that it will be useful, but |
|---|
| 17 | # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
|---|
| 18 | # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
|---|
| 19 | # License for more details. |
|---|
| 20 | # |
|---|
| 21 | # You should have received a copy of the GNU Lesser General Public License |
|---|
| 22 | # along with this library; if not, write to the Free Software Foundation, |
|---|
| 23 | # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|---|
| 24 | |
|---|
| 25 | import gtk |
|---|
| 26 | import gobject |
|---|
| 27 | |
|---|
| 28 | from higwidgets.higspinner import HIGSpinner |
|---|
| 29 | from higwidgets.higboxes import HIGHBox |
|---|
| 30 | from higwidgets.higbuttons import HIGButton |
|---|
| 31 | |
|---|
| 32 | class HIGEditableLabel(gtk.EventBox): |
|---|
| 33 | # called when label is changed .. if returns True new_value is setted |
|---|
| 34 | __gsignals__ = {'title-edited' : (gobject.SIGNAL_RUN_LAST, |
|---|
| 35 | gobject.TYPE_BOOLEAN, |
|---|
| 36 | (gobject.TYPE_STRING, |
|---|
| 37 | gobject.TYPE_STRING))} |
|---|
| 38 | |
|---|
| 39 | def __init__(self, label=''): |
|---|
| 40 | gobject.GObject.__init__(self) |
|---|
| 41 | |
|---|
| 42 | self.label = gtk.Label(label) |
|---|
| 43 | self.entry = gtk.Entry() |
|---|
| 44 | |
|---|
| 45 | self.lock = False |
|---|
| 46 | |
|---|
| 47 | box = gtk.HBox() |
|---|
| 48 | self.add(box) |
|---|
| 49 | |
|---|
| 50 | box.pack_start(self.label, False, False, 0) |
|---|
| 51 | box.pack_start(self.entry, False, False, 0) |
|---|
| 52 | |
|---|
| 53 | self.set_visible_window(False) |
|---|
| 54 | self.show_all() |
|---|
| 55 | |
|---|
| 56 | self.entry.connect('activate', self.on_entry_activated) |
|---|
| 57 | self.entry.connect('focus-out-event', self.on_lost_focus) |
|---|
| 58 | self.connect('realize', self.on_realize_event) |
|---|
| 59 | self.connect('button-press-event', self.on_button_press_event) |
|---|
| 60 | |
|---|
| 61 | def on_lost_focus(self, widget, event): |
|---|
| 62 | self.on_entry_activated(widget) |
|---|
| 63 | |
|---|
| 64 | def on_entry_activated(self, widget): |
|---|
| 65 | # Muttex for focus |
|---|
| 66 | if self.lock: |
|---|
| 67 | return False |
|---|
| 68 | |
|---|
| 69 | self.lock = True |
|---|
| 70 | |
|---|
| 71 | old_text = self.label.get_text() |
|---|
| 72 | new_text = self.entry.get_text() |
|---|
| 73 | |
|---|
| 74 | self.switch_mode(False) |
|---|
| 75 | |
|---|
| 76 | # If returns True we can change the label |
|---|
| 77 | if self.emit('title-edited', old_text, new_text): |
|---|
| 78 | self.label.set_text(new_text) |
|---|
| 79 | |
|---|
| 80 | self.lock = False |
|---|
| 81 | |
|---|
| 82 | return False |
|---|
| 83 | |
|---|
| 84 | def on_realize_event(self, widget): |
|---|
| 85 | self.entry.hide() |
|---|
| 86 | |
|---|
| 87 | def on_button_press_event(self, widget, event): |
|---|
| 88 | if event.type == gtk.gdk._2BUTTON_PRESS: |
|---|
| 89 | self.switch_mode(True) |
|---|
| 90 | |
|---|
| 91 | def switch_mode(self, editing): |
|---|
| 92 | """Switches from editing (True) to label mode (False). |
|---|
| 93 | """ |
|---|
| 94 | if editing: |
|---|
| 95 | self.entry.set_text(self.label.get_text()) |
|---|
| 96 | self.entry.grab_focus() |
|---|
| 97 | |
|---|
| 98 | self.label.hide() |
|---|
| 99 | self.entry.show() |
|---|
| 100 | else: |
|---|
| 101 | self.entry.set_text('') |
|---|
| 102 | |
|---|
| 103 | self.label.show() |
|---|
| 104 | self.entry.hide() |
|---|
| 105 | |
|---|
| 106 | # Reallocate widget |
|---|
| 107 | self.set_size_request(-1, -1) |
|---|
| 108 | |
|---|
| 109 | # Getters/setters for compatibility |
|---|
| 110 | |
|---|
| 111 | def get_text(self): |
|---|
| 112 | return self.label.get_text() |
|---|
| 113 | |
|---|
| 114 | def set_text(self, label): |
|---|
| 115 | self.label.set_text(label) |
|---|
| 116 | |
|---|
| 117 | def get_label(self): |
|---|
| 118 | return self.label.get_label() |
|---|
| 119 | |
|---|
| 120 | def set_label(self, label): |
|---|
| 121 | self.label.set_text(label) |
|---|
| 122 | |
|---|
| 123 | gobject.type_register(HIGEditableLabel) |
|---|
| 124 | HIGAnimatedLabel = HIGEditableLabel |
|---|
| 125 | |
|---|
| 126 | gtk.rc_parse_string(""" |
|---|
| 127 | style "thinWidget" { |
|---|
| 128 | xthickness = 0 |
|---|
| 129 | ythickness = 0 |
|---|
| 130 | } |
|---|
| 131 | widget "*.tabNotebookButton" style "thinWidget" |
|---|
| 132 | """) |
|---|
| 133 | |
|---|
| 134 | class HIGNotebook(gtk.Notebook): |
|---|
| 135 | def __init__(self): |
|---|
| 136 | gtk.Notebook.__init__(self) |
|---|
| 137 | self.popup_enable() |
|---|
| 138 | |
|---|
| 139 | class HIGClosableTabLabel(HIGHBox): |
|---|
| 140 | __gsignals__ = { 'close-clicked' : (gobject.SIGNAL_RUN_LAST, |
|---|
| 141 | gobject.TYPE_NONE, ()) } |
|---|
| 142 | |
|---|
| 143 | def __init__(self, label_text=""): |
|---|
| 144 | gobject.GObject.__init__(self) |
|---|
| 145 | #HIGHBox.__init__(self, spacing=4) |
|---|
| 146 | |
|---|
| 147 | self.label_text = label_text |
|---|
| 148 | self.__create_widgets() |
|---|
| 149 | |
|---|
| 150 | #self.propery_map = {"label_text" : self.label.get_label} |
|---|
| 151 | |
|---|
| 152 | def __create_widgets(self): |
|---|
| 153 | self.label = HIGAnimatedLabel(self.label_text) |
|---|
| 154 | |
|---|
| 155 | self.image = gtk.image_new_from_stock(gtk.STOCK_CLOSE, |
|---|
| 156 | gtk.ICON_SIZE_MENU) |
|---|
| 157 | self.button = HIGButton() |
|---|
| 158 | self.button.set_relief(gtk.RELIEF_NONE) |
|---|
| 159 | self.button.set_focus_on_click(False) |
|---|
| 160 | self.button.add(self.image) |
|---|
| 161 | self.button.set_name('tabNotebookButton') |
|---|
| 162 | |
|---|
| 163 | self.button.connect('clicked', self.__on_button_clicked) |
|---|
| 164 | self.button.connect('style-set', self.__on_button_style_set) |
|---|
| 165 | self.label.connect('button-press-event', self.on_button_press_event) |
|---|
| 166 | self.label.entry.connect('focus-out-event', self.on_entry_focus_out) |
|---|
| 167 | |
|---|
| 168 | self.pack_start(self.label, False, False, 0) |
|---|
| 169 | self.pack_end(self.button, False, False, 0) |
|---|
| 170 | |
|---|
| 171 | self.show_all() |
|---|
| 172 | self.switch_button_mode(False) |
|---|
| 173 | |
|---|
| 174 | def on_entry_focus_out(self, widget, event): |
|---|
| 175 | self.switch_button_mode(False) |
|---|
| 176 | |
|---|
| 177 | def on_button_press_event(self, widget, event): |
|---|
| 178 | if event.type == gtk.gdk._2BUTTON_PRESS: |
|---|
| 179 | self.switch_button_mode(True) |
|---|
| 180 | |
|---|
| 181 | def switch_button_mode(self, mode): |
|---|
| 182 | """Switch button from editing mode (True) to label mode (False) |
|---|
| 183 | """ |
|---|
| 184 | if mode: |
|---|
| 185 | self.image.set_from_stock(gtk.STOCK_APPLY, gtk.ICON_SIZE_MENU) |
|---|
| 186 | else: |
|---|
| 187 | self.image.set_from_stock(gtk.STOCK_CLOSE, gtk.ICON_SIZE_MENU) |
|---|
| 188 | |
|---|
| 189 | parent = self.get_parent() |
|---|
| 190 | |
|---|
| 191 | if parent: |
|---|
| 192 | parent.queue_draw() |
|---|
| 193 | |
|---|
| 194 | def __on_button_clicked(self, widget): |
|---|
| 195 | if mode: |
|---|
| 196 | self.label.on_entry_activated(self.label.entry) |
|---|
| 197 | self.switch_button_mode(False) |
|---|
| 198 | else: |
|---|
| 199 | self.emit('close-clicked') |
|---|
| 200 | |
|---|
| 201 | def __on_button_style_set(self, widget, prev_style): |
|---|
| 202 | w, h = gtk.icon_size_lookup_for_settings(widget.get_settings(), |
|---|
| 203 | gtk.ICON_SIZE_MENU) |
|---|
| 204 | widget.set_size_request(w + 2, h + 2) |
|---|
| 205 | |
|---|
| 206 | def get_text(self): |
|---|
| 207 | return self.label.get_text() |
|---|
| 208 | |
|---|
| 209 | def set_text(self, text): |
|---|
| 210 | self.label.set_text(text) |
|---|
| 211 | |
|---|
| 212 | def get_label(self): |
|---|
| 213 | return self.label.get_label() |
|---|
| 214 | |
|---|
| 215 | def set_label(self, label): |
|---|
| 216 | self.label.set_text(label) |
|---|
| 217 | |
|---|
| 218 | def get_animated_label(self): |
|---|
| 219 | return self.label |
|---|
| 220 | |
|---|
| 221 | gobject.type_register(HIGClosableTabLabel) |
|---|
| 222 | |
|---|
| 223 | HIGAnimatedTabLabel = HIGClosableTabLabel |
|---|