| 1 |
# Copyright (C) 2008-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 |
import os |
| 28 |
|
| 29 |
from gi.repository import Gtk |
| 30 |
from gi.repository import GObject |
| 31 |
|
| 32 |
from xl import xdg |
| 33 |
from xlgui.widgets.notebook import NotebookPage |
| 34 |
from xlgui import guiutil |
| 35 |
import logging |
| 36 |
|
| 37 |
|
| 38 |
LOGGER = logging.getLogger(__name__) |
| 39 |
|
| 40 |
|
| 41 |
class Panel(GObject.Object): |
| 42 |
""" |
| 43 |
The base panel class. |
| 44 |
|
| 45 |
This class is abstract and should be subclassed. All subclasses |
| 46 |
should define a 'ui_info' and 'name' variables. |
| 47 |
""" |
| 48 |
|
| 49 |
ui_info = ('panel.ui', 'PanelWindow') |
| 50 |
|
| 51 |
def __init__(self, parent, name, label=None): |
| 52 |
""" |
| 53 |
Initializes the panel |
| 54 |
|
| 55 |
@param parent: the main window |
| 56 |
@type parent: Gtk.Window |
| 57 |
@param name: the name of the panel. should be unique. |
| 58 |
@param label: text of the label displayed to the user |
| 59 |
""" |
| 60 |
GObject.Object.__init__(self) |
| 61 |
self.name = name # panel id |
| 62 |
self.label = label # label to be displayed |
| 63 |
self.parent = parent |
| 64 |
|
| 65 |
# if the UI designer file starts with file:// use the full path minus |
| 66 |
# file://, otherwise check in the data directories |
| 67 |
ui_file = self.ui_info[0] |
| 68 |
if not os.path.isabs(ui_file): |
| 69 |
ui_file = xdg.get_data_path('ui', 'panel', ui_file) |
| 70 |
|
| 71 |
self.builder = guiutil.get_builder(ui_file) |
| 72 |
self._child = None |
| 73 |
|
| 74 |
def focus(self): |
| 75 |
""" |
| 76 |
Makes this panel grab the keyboard focus |
| 77 |
Subclasses can override this to give focus to a particular widget |
| 78 |
or perform another action. |
| 79 |
""" |
| 80 |
self._child.grab_focus() |
| 81 |
|
| 82 |
def get_panel(self): |
| 83 |
""" |
| 84 |
Returns a NotebookPage object that will be used as the panel |
| 85 |
|
| 86 |
:returns: NotebookPage object |
| 87 |
""" |
| 88 |
if not self._child: |
| 89 |
widget = self.builder.get_object(self.ui_info[1]) |
| 90 |
if isinstance(widget, Gtk.Window): |
| 91 |
# the old way, for pre 4.0.0-compatibility |
| 92 |
child = widget.get_child() |
| 93 |
if not self.label: |
| 94 |
self.label = widget.get_title() |
| 95 |
LOGGER.info( |
| 96 |
"Old style panel %s is creating unnecessary Gtk.Window.", self.label |
| 97 |
) |
| 98 |
widget.remove(child) |
| 99 |
widget.destroy() |
| 100 |
else: |
| 101 |
child = widget |
| 102 |
|
| 103 |
self._child = NotebookPage(child, self.label, 'panel-tab-context') |
| 104 |
|
| 105 |
return self._child |