| 1 |
# Copyright (C) 2010 Adam Olsen |
| 2 |
# |
| 3 |
# This program is free software; you can redistribute it and/or modify |
| 4 |
# it under the terms of the GNU General Public License as published by |
| 5 |
# the Free Software Foundation; either version 2, or (at your option) |
| 6 |
# any later version. |
| 7 |
# |
| 8 |
# This program is distributed in the hope that it will be useful, |
| 9 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
# GNU General Public License for more details. |
| 12 |
# |
| 13 |
# You should have received a copy of the GNU General Public License |
| 14 |
# along with this program; if not, write to the Free Software |
| 15 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 16 |
# |
| 17 |
# |
| 18 |
# The developers of the Exaile media player hereby grant permission |
| 19 |
# for non-GPL compatible GStreamer and Exaile plugins to be used and |
| 20 |
# distributed together with GStreamer and Exaile. This permission is |
| 21 |
# above and beyond the permissions granted by the GPL license by which |
| 22 |
# Exaile is covered. If you modify this code, you may extend this |
| 23 |
# exception to your version of the code, but you are not obligated to |
| 24 |
# do so. If you do not wish to do so, delete this exception statement |
| 25 |
# from your version. |
| 26 |
|
| 27 |
from typing import Optional |
| 28 |
|
| 29 |
from gi.repository import ( |
| 30 |
Gdk, |
| 31 |
GdkPixbuf, |
| 32 |
GObject, |
| 33 |
Gtk, |
| 34 |
Pango, |
| 35 |
) |
| 36 |
|
| 37 |
from xl.nls import gettext as _ |
| 38 |
from xl import providers |
| 39 |
from xlgui import guiutil |
| 40 |
from xlgui.widgets import menu |
| 41 |
|
| 42 |
# Custom tab style; fixes some Adwaita ugliness |
| 43 |
TAB_CSS = Gtk.CssProvider() |
| 44 |
TAB_CSS.load_from_data( |
| 45 |
b''' |
| 46 |
/* Most themes don't handle vertical notebooks well, |
| 47 |
so we override everything. */ |
| 48 |
notebook.vertical tab { |
| 49 |
padding: 3px 4px 3px 4px; |
| 50 |
} |
| 51 |
notebook.vertical tab * { |
| 52 |
margin: 0; |
| 53 |
padding: 0; |
| 54 |
} |
| 55 |
notebook.vertical tab label { |
| 56 |
margin: 0 0 3px 0; |
| 57 |
} |
| 58 |
/* Remove gap between tabs */ |
| 59 |
header.top tab, header.bottom tab { |
| 60 |
margin-left: -1px; |
| 61 |
margin-right: -1px; |
| 62 |
} |
| 63 |
header.left tab, header.right tab { |
| 64 |
margin-top: -1px; |
| 65 |
margin-bottom: -1px; |
| 66 |
} |
| 67 |
''' |
| 68 |
) |
| 69 |
|
| 70 |
|
| 71 |
def apply_css(widget): |
| 72 |
sc = widget.get_style_context() |
| 73 |
sc.add_provider(TAB_CSS, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION) |
| 74 |
return sc |
| 75 |
|
| 76 |
|
| 77 |
class SmartNotebook(Gtk.Notebook): |
| 78 |
def __init__(self, vertical: bool = False): |
| 79 |
Gtk.Notebook.__init__(self) |
| 80 |
self.set_scrollable(True) |
| 81 |
self.connect('button-press-event', self.on_button_press) |
| 82 |
self.connect('popup-menu', self.on_popup_menu) |
| 83 |
self.connect('notify::tab-pos', self.__on_notify_tab_pos) |
| 84 |
self._add_tab_on_empty = True |
| 85 |
|
| 86 |
sc = apply_css(self) |
| 87 |
if vertical: |
| 88 |
sc.add_class('vertical') |
| 89 |
self.set_tab_pos(Gtk.PositionType.LEFT) |
| 90 |
|
| 91 |
def get_current_tab(self): |
| 92 |
current_page = self.get_current_page() |
| 93 |
if current_page == -1: |
| 94 |
return None |
| 95 |
return self.get_nth_page(current_page) |
| 96 |
|
| 97 |
def add_tab( |
| 98 |
self, |
| 99 |
tab: 'NotebookTab', |
| 100 |
page: 'NotebookPage', |
| 101 |
position: int = -1, |
| 102 |
switch: bool = True, |
| 103 |
) -> None: |
| 104 |
""" |
| 105 |
Add a tab to the notebook. |
| 106 |
|
| 107 |
:param tab: The tab to use |
| 108 |
:param page: The page to use |
| 109 |
:param position: Index to insert page at, or -1 for append |
| 110 |
:param switch: Switch focus to the new tab |
| 111 |
""" |
| 112 |
self.insert_page(page, tab, position=position) |
| 113 |
tab.notebook = self |
| 114 |
self.set_tab_reorderable(page, page.reorderable) |
| 115 |
tab_pos = self.get_tab_pos() |
| 116 |
if tab_pos in (Gtk.PositionType.TOP, Gtk.PositionType.BOTTOM): |
| 117 |
self.child_set_property(page, 'tab-expand', True) |
| 118 |
tab.adjust_label_width(tab_pos) |
| 119 |
if switch: |
| 120 |
self.set_current_page(self.page_num(page)) |
| 121 |
|
| 122 |
def add_default_tab(self): |
| 123 |
""" |
| 124 |
Action taken when a generic "new tab" option is triggered. |
| 125 |
Subclasses need to override this if they want new tab |
| 126 |
functionality to work automatically. |
| 127 |
|
| 128 |
:return: The NotebookTab created, or None |
| 129 |
""" |
| 130 |
pass |
| 131 |
|
| 132 |
def remove_page(self, page_num: int) -> None: |
| 133 |
""" |
| 134 |
Overrides Gtk.Notebook.remove_page |
| 135 |
""" |
| 136 |
if page_num == -1: |
| 137 |
page_num = self.get_n_pages() - 1 |
| 138 |
tab = self.get_tab_label(self.get_nth_page(page_num)) |
| 139 |
|
| 140 |
Gtk.Notebook.remove_page(self, page_num) |
| 141 |
tab.notebook = None |
| 142 |
|
| 143 |
if self._add_tab_on_empty and self.get_n_pages() == 0: |
| 144 |
self.add_default_tab() |
| 145 |
|
| 146 |
def remove_tab(self, tab: 'NotebookTab') -> None: |
| 147 |
""" |
| 148 |
Remove a specific NotebookTab from the notebook |
| 149 |
""" |
| 150 |
page_num = self.page_num(tab.page) |
| 151 |
if page_num >= 0: |
| 152 |
self.remove_page(page_num) |
| 153 |
|
| 154 |
def set_add_tab_on_empty(self, add_tab_on_empty: bool) -> None: |
| 155 |
""" |
| 156 |
If set True, the SmartNotebook will always maintain at |
| 157 |
least one tab in the notebook |
| 158 |
""" |
| 159 |
self._add_tab_on_empty = add_tab_on_empty |
| 160 |
|
| 161 |
def on_button_press(self, widget, event): |
| 162 |
if ( |
| 163 |
event.type == Gdk.EventType.BUTTON_PRESS |
| 164 |
and event.button == Gdk.BUTTON_MIDDLE |
| 165 |
): |
| 166 |
self.add_default_tab() |
| 167 |
|
| 168 |
def on_popup_menu(self, widget): |
| 169 |
page = self.get_current_tab() |
| 170 |
tab_label = self.get_tab_label(self.get_current_tab()) |
| 171 |
page.tab_menu.popup(None, None, guiutil.position_menu, tab_label, 0, 0) |
| 172 |
return True |
| 173 |
|
| 174 |
def __on_notify_tab_pos(self, _widget, _param): |
| 175 |
tab_pos = self.get_tab_pos() |
| 176 |
expand = tab_pos in (Gtk.PositionType.TOP, Gtk.PositionType.BOTTOM) |
| 177 |
for i in range(self.get_n_pages()): |
| 178 |
page = self.get_nth_page(i) |
| 179 |
self.child_set_property(page, 'tab-expand', expand) |
| 180 |
tab = self.get_tab_label(page) # type: NotebookTab |
| 181 |
tab.adjust_label_width(tab_pos) |
| 182 |
|
| 183 |
|
| 184 |
class NotebookTab(Gtk.EventBox): |
| 185 |
""" |
| 186 |
Class to represent a generic tab in a Gtk.Notebook. |
| 187 |
""" |
| 188 |
|
| 189 |
reorderable = True |
| 190 |
|
| 191 |
def __init__( |
| 192 |
self, notebook: SmartNotebook, page: 'NotebookPage', vertical: bool = False |
| 193 |
): |
| 194 |
""" |
| 195 |
:param notebook: The notebook this tab will belong to |
| 196 |
:param page: The page this tab will be associated with |
| 197 |
:param vertical: Whether the tab contents are to be laid out vertically |
| 198 |
""" |
| 199 |
Gtk.EventBox.__init__(self) |
| 200 |
self.set_visible_window(False) |
| 201 |
|
| 202 |
self.closable = True |
| 203 |
|
| 204 |
self.notebook = notebook |
| 205 |
self.page = page |
| 206 |
self.page.tab_menu.attach_to_widget(self.page, None) |
| 207 |
|
| 208 |
self.connect('button-press-event', self.on_button_press) |
| 209 |
|
| 210 |
self.vertical = vertical |
| 211 |
if vertical: |
| 212 |
box = Gtk.Box(spacing=2, orientation=Gtk.Orientation.VERTICAL) |
| 213 |
else: |
| 214 |
box = Gtk.Box(spacing=2) |
| 215 |
self.add(box) |
| 216 |
apply_css(box) |
| 217 |
|
| 218 |
self.icon = Gtk.Image() |
| 219 |
self.icon.set_no_show_all(True) |
| 220 |
apply_css(self.icon) |
| 221 |
|
| 222 |
self.label = Gtk.Label(label=self.page.get_page_name()) |
| 223 |
self.label.set_tooltip_text(self.page.get_page_name()) |
| 224 |
apply_css(self.label) |
| 225 |
|
| 226 |
if vertical: |
| 227 |
self.label.set_angle(90) |
| 228 |
self.label.props.valign = Gtk.Align.CENTER |
| 229 |
# Don't ellipsize but give a sane maximum length. |
| 230 |
self.label.set_max_width_chars(20) |
| 231 |
else: |
| 232 |
self.label.props.halign = Gtk.Align.CENTER |
| 233 |
self.label.set_ellipsize(Pango.EllipsizeMode.END) |
| 234 |
self.adjust_label_width(Gtk.PositionType.TOP) |
| 235 |
|
| 236 |
if self.can_rename(): |
| 237 |
self.entry = entry = Gtk.Entry() |
| 238 |
entry.set_width_chars(self.label.get_max_width_chars()) |
| 239 |
entry.set_text(self.label.get_text()) |
| 240 |
border = Gtk.Border.new() |
| 241 |
border.left = 1 |
| 242 |
border.right = 1 |
| 243 |
entry.connect('activate', self.on_entry_activate) |
| 244 |
entry.connect('focus-out-event', self.on_entry_focus_out_event) |
| 245 |
entry.connect('key-press-event', self.on_entry_key_press_event) |
| 246 |
entry.set_no_show_all(True) |
| 247 |
apply_css(entry) |
| 248 |
|
| 249 |
self.button = button = Gtk.Button() |
| 250 |
button.set_relief(Gtk.ReliefStyle.NONE) |
| 251 |
button.set_halign(Gtk.Align.CENTER) |
| 252 |
button.set_valign(Gtk.Align.CENTER) |
| 253 |
button.set_focus_on_click(False) |
| 254 |
button.set_tooltip_text(_("Close Tab")) |
| 255 |
button.add(Gtk.Image.new_from_icon_name('window-close', Gtk.IconSize.MENU)) |
| 256 |
button.connect('clicked', self.close) |
| 257 |
button.connect('button-press-event', self.on_button_press) |
| 258 |
apply_css(button) |
| 259 |
|
| 260 |
# pack the widgets in |
| 261 |
if vertical: |
| 262 |
box.pack_start(button, False, False, 0) |
| 263 |
box.pack_end(self.icon, False, False, 0) |
| 264 |
box.pack_end(self.label, True, True, 0) |
| 265 |
if self.can_rename(): |
| 266 |
box.pack_end(self.entry, True, True, 0) |
| 267 |
|
| 268 |
else: |
| 269 |
box.pack_start(self.icon, False, False, 0) |
| 270 |
box.pack_start(self.label, True, True, 0) |
| 271 |
if self.can_rename(): |
| 272 |
box.pack_start(self.entry, True, True, 0) |
| 273 |
box.pack_end(button, False, False, 0) |
| 274 |
|
| 275 |
page.set_tab(self) |
| 276 |
page.connect('name-changed', self.on_name_changed) |
| 277 |
|
| 278 |
box.show_all() |
| 279 |
|
| 280 |
def adjust_label_width(self, tab_pos: Gtk.PositionType) -> None: |
| 281 |
"""Change the label's minimum width according to tab position""" |
| 282 |
if self.vertical: |
| 283 |
# Vertical tabs don't care about tab position. |
| 284 |
return |
| 285 |
if tab_pos in (Gtk.PositionType.TOP, Gtk.PositionType.BOTTOM): |
| 286 |
# The number of characters here seems to be approximate. |
| 287 |
# 4 is enough for around 2 characters and an ellipsis. |
| 288 |
self.label.set_width_chars(4) |
| 289 |
else: |
| 290 |
self.label.set_width_chars(20) |
| 291 |
|
| 292 |
def set_icon(self, pixbuf: Optional[GdkPixbuf.Pixbuf]) -> None: |
| 293 |
""" |
| 294 |
Set the primary icon for the tab. |
| 295 |
|
| 296 |
:param pixbuf: The icon to use, or None to hide |
| 297 |
""" |
| 298 |
if pixbuf is None: |
| 299 |
self.icon.set_property("visible", False) |
| 300 |
else: |
| 301 |
self.icon.set_from_pixbuf(pixbuf) |
| 302 |
self.icon.set_property("visible", True) |
| 303 |
|
| 304 |
def set_closable(self, closable: bool) -> None: |
| 305 |
self.closable = closable |
| 306 |
self.button.set_sensitive(closable) |
| 307 |
|
| 308 |
def on_button_press(self, widget, event): |
| 309 |
""" |
| 310 |
Handles mouse button events on the tab. |
| 311 |
|
| 312 |
Typically triggers renaming, closing and menu. |
| 313 |
""" |
| 314 |
if ( |
| 315 |
event.button == Gdk.BUTTON_PRIMARY |
| 316 |
and event.type == Gdk.EventType._2BUTTON_PRESS |
| 317 |
): |
| 318 |
self.start_rename() |
| 319 |
elif event.button == Gdk.BUTTON_MIDDLE: |
| 320 |
self.close() |
| 321 |
elif event.triggers_context_menu(): |
| 322 |
self.page.tab_menu.popup(None, None, None, None, event.button, event.time) |
| 323 |
return True |
| 324 |
|
| 325 |
def on_entry_activate(self, entry): |
| 326 |
""" |
| 327 |
Handles end of editing and triggers the actual rename. |
| 328 |
""" |
| 329 |
self.entry.props.editing_canceled = False |
| 330 |
self.end_rename() |
| 331 |
|
| 332 |
def on_entry_focus_out_event(self, widget, event): |
| 333 |
""" |
| 334 |
Make defocusing the rename box equivalent to activating it. |
| 335 |
""" |
| 336 |
if not self.entry.props.editing_canceled: |
| 337 |
widget.activate() |
| 338 |
|
| 339 |
def on_entry_key_press_event(self, widget, event): |
| 340 |
""" |
| 341 |
Cancel rename if Escape is pressed |
| 342 |
""" |
| 343 |
if event.keyval == Gdk.KEY_Escape: |
| 344 |
self.entry.props.editing_canceled = True |
| 345 |
self.end_rename() |
| 346 |
return True |
| 347 |
|
| 348 |
def on_name_changed(self, *args): |
| 349 |
self.label.set_text(self.page.get_page_name()) |
| 350 |
|
| 351 |
def start_rename(self): |
| 352 |
""" |
| 353 |
Initiates the renaming of a tab, if the page supports this. |
| 354 |
""" |
| 355 |
if not self.can_rename(): |
| 356 |
return |
| 357 |
self.entry.set_text(self.page.get_page_name()) |
| 358 |
self.label.hide() |
| 359 |
self.button.hide() |
| 360 |
self.entry.show() |
| 361 |
self.entry.select_region(0, -1) |
| 362 |
self.entry.grab_focus() |
| 363 |
|
| 364 |
def end_rename(self, cancel=False): |
| 365 |
""" |
| 366 |
Finishes or cancels the renaming |
| 367 |
""" |
| 368 |
name = self.entry.get_text() |
| 369 |
if name.strip() != "" and not self.entry.props.editing_canceled: |
| 370 |
self.page.set_page_name(name) |
| 371 |
self.label.set_text(name) |
| 372 |
self.label.set_tooltip_text(name) |
| 373 |
|
| 374 |
self.entry.hide() |
| 375 |
self.label.show() |
| 376 |
self.button.show() |
| 377 |
|
| 378 |
self.entry.props.editing_canceled = False |
| 379 |
|
| 380 |
def can_rename(self): |
| 381 |
return hasattr(self.page, 'set_page_name') |
| 382 |
|
| 383 |
def close(self, *args): |
| 384 |
if self.closable and not self.page.emit('closing'): |
| 385 |
self.notebook.remove_page(self.notebook.page_num(self.page)) |
| 386 |
|
| 387 |
|
| 388 |
class NotebookPage(Gtk.Box): |
| 389 |
""" |
| 390 |
Base class representing a page. Should never be used directly. |
| 391 |
""" |
| 392 |
|
| 393 |
menu_provider_name = 'tab-context' # override this in subclasses |
| 394 |
reorderable = True |
| 395 |
__gsignals__ = { |
| 396 |
'name-changed': (GObject.SignalFlags.RUN_LAST, None, ()), |
| 397 |
'closing': (GObject.SignalFlags.RUN_LAST, GObject.TYPE_BOOLEAN, ()), |
| 398 |
} |
| 399 |
|
| 400 |
def __init__(self, child=None, page_name=None, menu_provider_name=None): |
| 401 |
Gtk.Box.__init__(self, orientation=Gtk.Orientation.VERTICAL) |
| 402 |
|
| 403 |
# sometimes you just want to create a page |
| 404 |
if menu_provider_name is not None: |
| 405 |
self.menu_provider_name = menu_provider_name |
| 406 |
|
| 407 |
self.tab = None |
| 408 |
self.tab_menu = menu.ProviderMenu(self.menu_provider_name, self) |
| 409 |
|
| 410 |
if child is not None: |
| 411 |
self.pack_start(child, True, True, 0) |
| 412 |
|
| 413 |
if page_name is not None: |
| 414 |
self.page_name = page_name |
| 415 |
|
| 416 |
def focus(self): |
| 417 |
""" |
| 418 |
Grabs focus for this page. Should be overridden in subclasses. |
| 419 |
""" |
| 420 |
self.grab_focus() |
| 421 |
|
| 422 |
def get_page_name(self): |
| 423 |
""" |
| 424 |
Returns the name of this tab. Should be overridden in subclasses. |
| 425 |
|
| 426 |
Subclasses can also implement set_page_name(self, name) to allow |
| 427 |
renaming, but this is not mandatory. |
| 428 |
""" |
| 429 |
if hasattr(self, 'page_name'): |
| 430 |
return self.page_name |
| 431 |
return "UNNAMED PAGE" |
| 432 |
|
| 433 |
def set_tab(self, tab): |
| 434 |
""" |
| 435 |
Set the tab that holds this page. This will be called directly |
| 436 |
from the tab itself when it is created, and should not be used |
| 437 |
outside of that. |
| 438 |
""" |
| 439 |
self.tab = tab |
| 440 |
|
| 441 |
def is_current_page(self): |
| 442 |
""" |
| 443 |
Returns True if this page is the currently-visible page in |
| 444 |
the Notebook. |
| 445 |
""" |
| 446 |
return self.tab.get_nth_page(self.tab.get_current_page()) == self |
| 447 |
|
| 448 |
def name_changed(self): |
| 449 |
self.emit('name-changed') |
| 450 |
|
| 451 |
|
| 452 |
class NotebookAction: |
| 453 |
""" |
| 454 |
A custom action to be placed to the left or right of tabs in a notebook |
| 455 |
""" |
| 456 |
|
| 457 |
name = None |
| 458 |
position = Gtk.PackType.END |
| 459 |
|
| 460 |
def __init__(self, notebook): |
| 461 |
self.notebook = notebook |
| 462 |
|
| 463 |
|
| 464 |
class NotebookActionService(providers.ProviderHandler): |
| 465 |
""" |
| 466 |
Provides interface for action widgets to be dynamically attached |
| 467 |
detached from notebooks. |
| 468 |
|
| 469 |
Actions are widgets placed to the left or right of tabs on a notebook. |
| 470 |
""" |
| 471 |
|
| 472 |
def __init__(self, notebook, servicename): |
| 473 |
""" |
| 474 |
:param notebook: Notebook to attach to |
| 475 |
:param servicename: Provider service name to use |
| 476 |
""" |
| 477 |
|
| 478 |
providers.ProviderHandler.__init__(self, servicename, notebook) |
| 479 |
|
| 480 |
self.notebook = notebook |
| 481 |
|
| 482 |
# Try to set up action widgets |
| 483 |
lw = Gtk.Box(spacing=3) |
| 484 |
lw.pack_end(Gtk.Separator.new(Gtk.Orientation.HORIZONTAL), False, False, 0) |
| 485 |
rw = Gtk.Box(spacing=3) |
| 486 |
lw.pack_start(Gtk.Separator.new(Gtk.Orientation.HORIZONTAL), False, False, 0) |
| 487 |
|
| 488 |
notebook.set_action_widget(lw, Gtk.PackType.START) |
| 489 |
notebook.set_action_widget(rw, Gtk.PackType.END) |
| 490 |
|
| 491 |
self.__actions = {} |
| 492 |
for provider in self.get_providers(): |
| 493 |
self.on_provider_added(provider) |
| 494 |
|
| 495 |
def on_provider_added(self, provider): |
| 496 |
""" |
| 497 |
Adds actions on provider addition |
| 498 |
""" |
| 499 |
try: |
| 500 |
actions_box = self.notebook.get_action_widget(provider.position) |
| 501 |
except AttributeError: |
| 502 |
pass |
| 503 |
else: |
| 504 |
self.__actions[provider.name] = provider(self.notebook) |
| 505 |
actions_box.pack_start(self.__actions[provider.name], False, False, 0) |
| 506 |
actions_box.show_all() |
| 507 |
|
| 508 |
def on_provider_removed(self, provider): |
| 509 |
""" |
| 510 |
Removes actions on provider removal |
| 511 |
""" |
| 512 |
try: |
| 513 |
actions_box = self.notebook.get_action_widget(provider.position) |
| 514 |
except AttributeError: |
| 515 |
pass |
| 516 |
else: |
| 517 |
action = self.__actions[provider.name] |
| 518 |
actions_box.remove(action) |
| 519 |
action.destroy() |
| 520 |
|
| 521 |
del self.__actions[provider.name] |