File: pager.py

package info (click to toggle)
python-pyface 8.0.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 13,944 kB
  • sloc: python: 54,107; makefile: 82
file content (101 lines) | stat: -rw-r--r-- 2,959 bytes parent folder | download
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
# (C) Copyright 2005-2023 Enthought, Inc., Austin, TX
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in LICENSE.txt and may be redistributed only under
# the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
#
# Thanks for using Enthought open source!

""" A pager contains a set of pages, but only shows one at a time. """


import wx
from wx.lib.scrolledpanel import ScrolledPanel as wxScrolledPanel


class Pager(wxScrolledPanel):
    """ A pager contains a set of pages, but only shows one at a time. """

    def __init__(self, parent, wxid, **kw):
        """ Creates a new pager. """

        # Base-class constructor.
        wxScrolledPanel.__init__(self, parent, wxid, **kw)
        self.SetupScrolling()

        # The pages in the pager!
        self._pages = {}  # { str name : wx.Window page }

        # The page that is currently displayed.
        self._current_page = None

        # Create the widget!
        self._create_widget()

        return

    # ------------------------------------------------------------------------
    # 'Pager' interface.
    # ------------------------------------------------------------------------

    def add_page(self, name, page):
        """ Adds a page with the specified name. """

        self._pages[name] = page

        # Make the pager panel big enought ot hold the biggest page.
        #
        # fixme: I have a feeling this needs some testing!
        sw, sh = self.GetSize()
        pw, ph = page.GetSize()
        self.SetSize((max(sw, pw), max(sh, ph)))

        # All pages are added as hidden.  Use 'show_page' to make a page
        # visible.
        page.Show(False)

        return page

    def show_page(self, name):
        """ Shows the page with the specified name. """

        # Hide the current page (if one is displayed).
        if self._current_page is not None:
            self._hide_page(self._current_page)

        # Show the specified page.
        page = self._show_page(self._pages[name])

        # Resize the panel to match the sizer's minimal size.
        self._sizer.Fit(self)

        return page

    # ------------------------------------------------------------------------
    # Private interface.
    # ------------------------------------------------------------------------

    def _create_widget(self):
        """ Creates the widget. """

        self._sizer = sizer = wx.BoxSizer(wx.VERTICAL)
        self.SetSizer(sizer)
        self.SetAutoLayout(True)

    def _hide_page(self, page):
        """ Hides the specified page. """

        page.Show(False)
        self._sizer.Remove(page)

    def _show_page(self, page):
        """ Shows the specified page. """

        page.Show(True)
        self._sizer.Add(page, 1, wx.EXPAND)

        self._current_page = page

        return page