# ------------------------------------------------------------------------------------ # # LABELBOOK And FLATIMAGEBOOK Widgets wxPython IMPLEMENTATION # # Original C++ Code From Eran, embedded in the FlatMenu source code # # # License: wxWidgets license # # # Python Code By: # # Andrea Gavana, @ 03 Nov 2006 # Latest Revision: 22 Jan 2013, 21.00 GMT # # # For All Kind Of Problems, Requests Of Enhancements And Bug Reports, # Please Write To Me At: # # andrea.gavana@gmail.com # andrea.gavana@maerskoil.com # # Or, Obviously, To The wxPython Mailing List!!! # # TODO: # LabelBook - Support IMB_SHOW_ONLY_IMAGES # LabelBook - An option to only draw the border between the controls and the pages so # the background colour can flow into the window background # # Tags: phoenix-port, unittest, documented, py3-port # # # End Of Comments # ------------------------------------------------------------------------------------ # """LabelBook and FlatImageBook are owner-drawn notebooks. Description =========== :class:`LabelBook` and :class:`FlatImageBook` are quasi-full implementations of the :class:`Notebook`, and designed to be a drop-in replacement for :class:`Notebook`. The API functions are similar so one can expect the function to behave in the same way. :class:`LabelBook` and :class:`FlatImageBook` share their appearance with :class:`Toolbook` and :class:`Listbook`, while having more options for custom drawings, label positioning, mouse pointing and so on. Moreover, they retain also some visual characteristics of the Outlook address book. Some features: - They are generic controls; - Supports for left, right, top (:class:`FlatImageBook` only), bottom (:class:`FlatImageBook` only) book styles; - Possibility to draw images only, text only or both (:class:`FlatImageBook` only); - Support for a "pin-button", that allows the user to shrink/expand the book tab area; - Shadows behind tabs (:class:`LabelBook` only); - Gradient shading of the tab area (:class:`LabelBook` only); - Web-like mouse pointing on tabs style (:class:`LabelBook` only); - Many customizable colours (tab area, active tab text, tab borders, active tab, highlight) :class:`LabelBook` only. And much more. See the demo for a quasi-complete review of all the functionalities of :class:`LabelBook` and :class:`FlatImageBook`. Usage ===== Usage example:: import wx import wx.lib.agw.labelbook as LB class MyFrame(wx.Frame): def __init__(self, parent: None | wx.Window) -> None: wx.Frame.__init__(self, parent, -1, "LabelBook Demo") # Possible values for Tab placement are INB_TOP, INB_BOTTOM, INB_RIGHT, INB_LEFT notebook = LB.LabelBook( self, -1, agw_style=LB.INB_FIT_LABELTEXT | LB.INB_LEFT | LB.INB_DRAW_SHADOW | LB.INB_GRADIENT_BACKGROUND, ) pane1 = wx.Panel(notebook) pane2 = wx.Panel(notebook) imagelist = wx.ImageList(32, 32) imagelist.Add(wx.Bitmap("my_bitmap.png", wx.BITMAP_TYPE_PNG)) notebook.AssignImageList(imagelist) notebook.AddPage(pane1, "Tab1", True, 0) notebook.AddPage(pane2, "Tab2", False, 0) # our normal wxApp-derived class, as usual app = wx.App(False) frame = MyFrame(None) app.SetTopWindow(frame) frame.Show() app.MainLoop() Supported Platforms =================== :class:`LabelBook` and :class:`FlatImageBook` have been tested on the following platforms: * Windows (Windows XP); * Linux Ubuntu (Dapper 6.06) Window Styles ============= This class supports the following window styles: =========================== =========== ================================================ Window Styles Hex Value Description =========================== =========== ================================================ ``INB_BOTTOM`` 0x1 Place labels below the page area. Available only for :class:`FlatImageBook`. ``INB_LEFT`` 0x2 Place labels on the left side. Available only for :class:`FlatImageBook`. ``INB_RIGHT`` 0x4 Place labels on the right side. ``INB_TOP`` 0x8 Place labels above the page area. ``INB_BORDER`` 0x10 Draws a border around :class:`LabelBook` or :class:`FlatImageBook`. ``INB_SHOW_ONLY_TEXT`` 0x20 Shows only text labels and no images. Available only for :class:`LabelBook`. ``INB_SHOW_ONLY_IMAGES`` 0x40 Shows only tab images and no label texts. Available only for :class:`LabelBook`. ``INB_FIT_BUTTON`` 0x80 Displays a pin button to show/hide the book control. ``INB_DRAW_SHADOW`` 0x100 Draw shadows below the book tabs. Available only for :class:`LabelBook`. ``INB_USE_PIN_BUTTON`` 0x200 Displays a pin button to show/hide the book control. ``INB_GRADIENT_BACKGROUND`` 0x400 Draws a gradient shading on the tabs background. Available only for :class:`LabelBook`. ``INB_WEB_HILITE`` 0x800 On mouse hovering, tabs behave like html hyperlinks. Available only for :class:`LabelBook`. ``INB_NO_RESIZE`` 0x1000 Don't allow resizing of the tab area. ``INB_FIT_LABELTEXT`` 0x2000 Will fit the tab area to the longest text (or text+image if you have images) in all the tabs. ``INB_BOLD_TAB_SELECTION`` 0x4000 Show the selected tab text using a bold font. =========================== =========== ================================================ Events Processing ================= This class processes the following events: =================================== ==================================================== Event Name Description =================================== ==================================================== ``EVT_IMAGENOTEBOOK_PAGE_CHANGED`` Notify client objects when the active page in :class:`FlatImageBook` or :class:`LabelBook` has changed. ``EVT_IMAGENOTEBOOK_PAGE_CHANGING`` Notify client objects when the active page in :class:`FlatImageBook` or :class:`LabelBook` is about to change. ``EVT_IMAGENOTEBOOK_PAGE_CLOSED`` Notify client objects when a page in :class:`FlatImageBook` or :class:`LabelBook` has been closed. ``EVT_IMAGENOTEBOOK_PAGE_CLOSING`` Notify client objects when a page in :class:`FlatImageBook` or :class:`LabelBook` is closing. =================================== ==================================================== TODOs ===== - :class:`LabelBook`: support ``IMB_SHOW_ONLY_IMAGES``; - :class:`LabelBook`: an option to only draw the border between the controls and the pages so the background colour can flow into the window background. License And Version =================== :class:`LabelBook` and :class:`FlatImageBook` are distributed under the wxPython license. Latest Revision: Andrea Gavana @ 22 Jan 2013, 21.00 GMT Version 0.6. """ from __future__ import annotations __version__ = "0.6" # ------------------------------------------------------------------------------------ # # Beginning Of IMAGENOTEBOOK wxPython Code # ------------------------------------------------------------------------------------ # from enum import IntFlag import wx from DisplayCAL.lib.agw.artmanager import ArtManager, DCSaver from DisplayCAL.lib.agw.fmresources import ( BOTTOM_SHADOW, BOTTOM_SHADOW_FULL, IMG_NONE, IMG_OVER_EW_BORDER, IMG_OVER_IMG, IMG_OVER_PIN, INB_ACTIVE_TAB_COLOUR, INB_ACTIVE_TEXT_COLOUR, INB_HILITE_TAB_COLOUR, INB_PIN_HOVER, INB_PIN_NONE, INB_PIN_PRESSED, INB_TAB_AREA_BACKGROUND_COLOUR, INB_TABS_BORDER_COLOUR, INB_TEXT_COLOUR, PIN_DOWN_XPM, PIN_LEFT_XPM, RIGHT_SHADOW, ) class ImageBookStyle(IntFlag): """This class that defines the styles for the LabelBook and FlatImageBook.""" INB_BOTTOM = 1 """Place labels below the page area. Available only for :class:`FlatImageBook`. """ INB_LEFT = 2 """Place labels on the left side. Available only for :class:`FlatImageBook`. """ INB_RIGHT = 4 """Place labels on the right side.""" INB_TOP = 8 """Place labels above the page area.""" INB_BORDER = 16 """Draw a border around :class:`LabelBook` or :class:`FlatImageBook`.""" INB_SHOW_ONLY_TEXT = 32 """Show only text labels and no images. Available only for :class:`LabelBook`. """ INB_SHOW_ONLY_IMAGES = 64 """Show only tab images and no label texts. Available only for :class:`LabelBook`. """ INB_FIT_BUTTON = 128 """Display a pin button to show/hide the book control.""" INB_DRAW_SHADOW = 256 """Draw shadows below the book tabs. Available only for :class:`LabelBook`. """ INB_USE_PIN_BUTTON = 512 """Display a pin button to show/hide the book control.""" INB_GRADIENT_BACKGROUND = 1024 """Draw a gradient shading on the tabs background. Available only for :class:`LabelBook`. """ INB_WEB_HILITE = 2048 """On mouse hovering, tabs behave like html hyperlinks. Available only for :class:`LabelBook`. """ INB_NO_RESIZE = 4096 """Don't allow resizing of the tab area.""" INB_FIT_LABELTEXT = 8192 """Fit the tab area to the longest text (or text+image).""" INB_BOLD_TAB_SELECTION = 16384 """Show the selected tab text using a bold font.""" wxEVT_IMAGENOTEBOOK_PAGE_CHANGED: int = wx.wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGED # noqa: N816 wxEVT_IMAGENOTEBOOK_PAGE_CHANGING: int = wx.wxEVT_COMMAND_NOTEBOOK_PAGE_CHANGING # noqa: N816 wxEVT_IMAGENOTEBOOK_PAGE_CLOSING: int = wx.NewEventType() # noqa: N816 wxEVT_IMAGENOTEBOOK_PAGE_CLOSED: int = wx.NewEventType() # noqa: N816 # ------------------------------------------------------------------------------------ # # ImageNotebookEvent # ------------------------------------------------------------------------------------ # EVT_IMAGENOTEBOOK_PAGE_CHANGED: wx.PyEventBinder = wx.EVT_NOTEBOOK_PAGE_CHANGED """Notify clients when the active page in FlatImageBook or LabelBook changes.""" EVT_IMAGENOTEBOOK_PAGE_CHANGING: wx.PyEventBinder = wx.EVT_NOTEBOOK_PAGE_CHANGING """Notify clients when the active page changes.""" EVT_IMAGENOTEBOOK_PAGE_CLOSING = wx.PyEventBinder(wxEVT_IMAGENOTEBOOK_PAGE_CLOSING, 1) """Notify clients when a page in :class:`FlatImageBook` or :class:`LabelBook` closes.""" EVT_IMAGENOTEBOOK_PAGE_CLOSED = wx.PyEventBinder(wxEVT_IMAGENOTEBOOK_PAGE_CLOSED, 1) """Notify clients when a page in FlatImageBook or LabelBook is closed.""" # ------------------------------------------------------------------------------------ # # Class ImageNotebookEvent # ------------------------------------------------------------------------------------ # class ImageNotebookEvent(wx.PyCommandEvent): """These events are sent when page changes or closures are mapped in the parent.""" def __init__( self, event_type: int, event_id: int = 1, sel: int = -1, old_sel: int = -1 ) -> None: """Construct the default class. Args: event_type (int): the event type; event_id (int): the event identifier; sel (int): the current selection; old_sel (int): the old selection. """ wx.PyCommandEvent.__init__(self, event_type, event_id) self._eventType: int = event_type self._sel: int = sel self._oldsel: int = old_sel self._allowed = True def SetSelection(self, s: int) -> None: # noqa: N802 """Set the event selection. Args: s (int): an integer specifying the new selection. """ self._sel = s def SetOldSelection(self, s: int) -> None: # noqa: N802 """Set the event old selection. Args: s (int): an integer specifying the old selection. """ self._oldsel = s def GetSelection(self) -> int: # noqa: N802 """Return the event selection.""" return self._sel def GetOldSelection(self) -> int: # noqa: N802 """Return the old event selection.""" return self._oldsel def Veto(self) -> None: # noqa: N802 """Prevent the change announced by this event from happening. Note: It is in general a good idea to notify the user about the reasons for vetoing the change because otherwise the applications behaviour (which just refuses to do what the user wants) might be quite surprising. """ self._allowed = False def Allow(self) -> None: # noqa: N802 """Explicitly allow the event to be processed. This is the opposite of :meth:`~ImageNotebookEvent.Veto`. For most events it is not necessary to call this method as the events are allowed anyhow but some are forbidden by default (this will be mentioned in the corresponding event description). """ self._allowed = True def IsAllowed(self) -> bool: # noqa: N802 """Return ``True`` if allowed, ``False`` if vetoed.""" return self._allowed # ------------------------------------------------------------------------------------ # # Class ImageInfo # ------------------------------------------------------------------------------------ # class ImageInfo: """This class stores tab info (caption, image, etc.) for :class:`LabelBook`.""" def __init__( self, str_caption: str = "", image_index: int = -1, enabled: bool = True ) -> None: """Construct the default class. Args: str_caption (str): The tab caption. image_index (int): the tab image index based on the assigned (set) :class:`wx.ImageList` (if any). enabled (bool): sets the tab as enabled or disabled. """ self._pos = wx.Point() self._size = wx.Size() self._strCaption: str = str_caption self._ImageIndex: int = image_index self._captionRect = wx.Rect() self._bEnabled: bool = enabled def SetCaption(self, value: str) -> None: # noqa: N802 """Set the tab caption. Args: value (str): the new tab caption. """ self._strCaption = value def GetCaption(self) -> str: # noqa: N802 """Return the tab caption.""" return self._strCaption def SetPosition(self, value: wx.Point) -> None: # noqa: N802 """Set the tab position. Args: value (wx.Point): the new tab position, an instance of :class:`wx.Point`. """ self._pos: wx.Point = value def GetPosition(self) -> wx.Point: # noqa: N802 """Return the tab position.""" return self._pos def SetSize(self, value: wx.Size) -> None: # noqa: N802 """Set the tab size. Args: value (wx.Size): the new tab size, an instance of :class:`wx.Size`. """ self._size: wx.Size = value def GetSize(self) -> wx.Size: # noqa: N802 """Return the tab size.""" return self._size def SetImageIndex(self, value: int) -> None: # noqa: N802 """Set the tab image index. Args: value (int): an index into the image list.. """ self._ImageIndex = value def GetImageIndex(self) -> int: # noqa: N802 """Return the tab image index.""" return self._ImageIndex def SetTextRect(self, rect: wx.Rect) -> None: # noqa: N802 """Set the client rectangle available for the tab text. Args: rect (wx.Rect): the tab text client rectangle, an instance of :class:`wx.Rect`. """ self._captionRect: wx.Rect = rect def GetTextRect(self) -> wx.Rect: # noqa: N802 """Return the client rectangle available for the tab text.""" return self._captionRect def GetEnabled(self) -> bool: # noqa: N802 """Return whether the tab is enabled or not.""" return self._bEnabled def EnableTab(self, enabled: bool) -> None: # noqa: N802 """Set the tab enabled or disabled. Args: enabled (bool): ``True`` to enable a tab, ``False`` to disable it. """ self._bEnabled = enabled # ------------------------------------------------------------------------------------ # # Class ImageContainerBase # ------------------------------------------------------------------------------------ # class ImageContainerBase(wx.Panel): """Base class for :class:`FlatImageBook` image container.""" def __init__( self, parent: wx.Window, id: int = wx.ID_ANY, # noqa: A002 pos: wx.Point = wx.DefaultPosition, size: wx.Size = wx.DefaultSize, style: int = 0, agw_style: int = 0, name: str = "ImageContainerBase", ) -> None: """Construct the default class. Args: parent (wx.Window): parent window. Must not be ``None``; id (int): window identifier. A value of -1 indicates a default value; pos (wx.Point): the control position. A value of (-1, -1) indicates a default position, chosen by either the windowing system or wxPython, depending on platform; size (wx.Size): the control size. A value of (-1, -1) indicates a default size, chosen by either the windowing system or wxPython, depending on platform; style (int): the underlying :class:`Panel` window style; agw_style (int): the AGW-specific window style. This can be a combination of the following bits: =========================== =========== ================================ Window Styles Hex Value Description =========================== =========== ================================ ``INB_BOTTOM`` 0x1 Place labels below the page area. Available only for :class:`FlatImageBook`. ``INB_LEFT`` 0x2 Place labels on the left side. Available only for :class:`FlatImageBook`. ``INB_RIGHT`` 0x4 Place labels on the right side. ``INB_TOP`` 0x8 Place labels above the page area. ``INB_BORDER`` 0x10 Draws a border around :class:`LabelBook` or :class:`FlatImageBook`. ``INB_SHOW_ONLY_TEXT`` 0x20 Shows only text labels and no images. Available only for :class:`LabelBook`. ``INB_SHOW_ONLY_IMAGES`` 0x40 Shows only tab images and no label texts. Available only for :class:`LabelBook`. ``INB_FIT_BUTTON`` 0x80 Displays a pin button to show/hide the book control. ``INB_DRAW_SHADOW`` 0x100 Draw shadows below the book tabs. Available only for :class:`LabelBook`. ``INB_USE_PIN_BUTTON`` 0x200 Displays a pin button to show/hide the book control. ``INB_GRADIENT_BACKGROUND`` 0x400 Draws a gradient shading on the tabs background. Available only for :class:`LabelBook`. ``INB_WEB_HILITE`` 0x800 On mouse hovering, tabs behave like html hyperlinks. Available only for :class:`LabelBook`. ``INB_NO_RESIZE`` 0x1000 Don't allow resizing of the tab area. ``INB_FIT_LABELTEXT`` 0x2000 Will fit the tab area to the longest text (or text+image if you have images) in all the tabs. ``INB_BOLD_TAB_SELECTION`` 0x4000 Show the selected tab text using a bold font. =========================== =========== ================================ name (str): the window name. """ self._nIndex = -1 self._nImgSize = 16 self._ImageList = None self._nHoveredImgIdx = -1 self._bCollapsed = False self._tabAreaSize: wx.Size = wx.Size(-1, -1) self._nPinButtonStatus: int = INB_PIN_NONE self._pagesInfoVec: list[ImageInfo] = [] self._pinBtnRect: wx.Rect = wx.Rect() wx.Panel.__init__( self, parent, id, pos, size, style | wx.NO_BORDER | wx.NO_FULL_REPAINT_ON_RESIZE, name, ) def HasAGWFlag(self, flag: int) -> bool: # noqa: N802 """Test for existence of flag in the style. Args: flag (int): a window style. This can be a combination of the following bits: =========================== =========== ================================ Window Styles Hex Value Description =========================== =========== ================================ ``INB_BOTTOM`` 0x1 Place labels below the page area. Available only for :class:`FlatImageBook`. ``INB_LEFT`` 0x2 Place labels on the left side. Available only for :class:`FlatImageBook`. ``INB_RIGHT`` 0x4 Place labels on the right side. ``INB_TOP`` 0x8 Place labels above the page area. ``INB_BORDER`` 0x10 Draws a border around :class:`LabelBook` or :class:`FlatImageBook`. ``INB_SHOW_ONLY_TEXT`` 0x20 Shows only text labels and no images. Available only for :class:`LabelBook`. ``INB_SHOW_ONLY_IMAGES`` 0x40 Shows only tab images and no label texts. Available only for :class:`LabelBook`. ``INB_FIT_BUTTON`` 0x80 Displays a pin button to show/hide the book control. ``INB_DRAW_SHADOW`` 0x100 Draw shadows below the book tabs. Available only for :class:`LabelBook`. ``INB_USE_PIN_BUTTON`` 0x200 Displays a pin button to show/hide the book control. ``INB_GRADIENT_BACKGROUND`` 0x400 Draws a gradient shading on the tabs background. Available only for :class:`LabelBook`. ``INB_WEB_HILITE`` 0x800 On mouse hovering, tabs behave like html hyperlinks. Available only for :class:`LabelBook`. ``INB_NO_RESIZE`` 0x1000 Don't allow resizing of the tab area. ``INB_FIT_LABELTEXT`` 0x2000 Will fit the tab area to the longest text (or text+image if you have images) in all the tabs. ``INB_BOLD_TAB_SELECTION`` 0x4000 Show the selected tab text using a bold font. =========================== =========== ================================ """ style: int = self.GetParent().GetAGWWindowStyleFlag() res: bool = ((style & flag and [True]) or [False])[0] return res def ClearFlag(self, flag: int) -> None: # noqa: N802 """Remove flag from the style. Args: flag (int): a window style flag. See: :meth:`~ImageContainerBase.HasAGWFlag` for a list of possible window style flags. """ parent: wx.Window = self.GetParent() agw_style: int = parent.GetAGWWindowStyleFlag() agw_style &= ~flag parent.SetAGWWindowStyleFlag(agw_style) def AssignImageList(self, imglist: wx.ImageList) -> None: # noqa: N802 """Assign an image list to the :class:`wx.ImageContainerBase`. Args: imglist (wx.ImageList): an instance of :class:`wx.ImageList`. """ if imglist and imglist.GetImageCount() != 0: self._nImgSize: int = imglist.GetBitmap(0).GetHeight() self._ImageList: None | wx.ImageList = imglist parent: wx.Window = self.GetParent() agw_style: int = parent.GetAGWWindowStyleFlag() parent.SetAGWWindowStyleFlag(agw_style) def GetImageList(self) -> None | wx.ImageList: # noqa: N802 """Return the image list for :class:`wx.ImageContainerBase`.""" return self._ImageList def GetImageSize(self) -> int: # noqa: N802 """Return the image size in the :class:`wx.ImageContainerBase` image list.""" return self._nImgSize def FixTextSize(self, dc: wx.DC, text: str, max_width: int) -> None | str: # noqa: N802 """Fix the text, to fit `maxWidth` value. If the text length exceeds `maxWidth` value this function truncates it and appends two dots at the end. ("Long Long Long Text" might become "Long Long..."). Args: dc (wx.DC): an instance of :class:`wx.DC`; text (str): the text to fix/truncate; max_width (int): the maximum allowed width for the text, in pixels. """ return ArtManager.Get().TruncateText(dc, text, max_width) def CanDoBottomStyle(self) -> bool: # noqa: N802 """Allow the parent to examine the children type. Some implementation (such as :class:`LabelBook`), does not support top/bottom images, only left/right. """ return False def AddPage(self, caption: str, selected: bool = False, image_id: int = -1) -> None: # noqa: N802 """Add a page to the container. Args: caption (str): specifies the text for the new tab; selected (bool): specifies whether the page should be selected; image_id (int): specifies the optional image index for the new tab. """ self._pagesInfoVec.append(ImageInfo(caption, image_id)) if selected or len(self._pagesInfoVec) == 1: self._nIndex: int = len(self._pagesInfoVec) - 1 self.Refresh() def InsertPage( # noqa: N802 self, page_id: int, caption: str, selected: bool = False, image_id: int = -1 ) -> None: """Insert a page into the container at the specified position. Args: page_id (int): specifies the position for the new tab; caption (str): specifies the text for the new tab; selected (bool): specifies whether the page should be selected; image_id (int): specifies the optional image index for the new tab. """ self._pagesInfoVec.insert(page_id, ImageInfo(caption, image_id)) if selected or len(self._pagesInfoVec) == 1: self._nIndex = len(self._pagesInfoVec) - 1 self.Refresh() def SetPageImage(self, page: int, image_id: int) -> None: # noqa: N802 """Set the image for the given page. Args: page (int): The index of the tab; image_id (int): Specifies the optional image index for the tab. """ image_info: ImageInfo = self._pagesInfoVec[page] image_info.SetImageIndex(image_id) def SetPageText(self, page: int, text: str) -> None: # noqa: N802 """Set the tab caption for the given page. Args: page (int): the index of the tab; text (str): the new tab caption. """ image_info: ImageInfo = self._pagesInfoVec[page] image_info.SetCaption(text) def GetPageImage(self, page: int) -> int: # noqa: N802 """Return the image index for the given page. Args: page (int): the index of the tab. """ image_info: ImageInfo = self._pagesInfoVec[page] return image_info.GetImageIndex() def GetPageText(self, page: int) -> str: # noqa: N802 """Return the tab caption for the given page. Args: page (int): the index of the tab. """ image_info: ImageInfo = self._pagesInfoVec[page] return image_info.GetCaption() def GetEnabled(self, page: int) -> bool: # noqa: N802 """Return whether a tab is enabled or not. Args: page (int): an integer specifying the page index. """ if page >= len(self._pagesInfoVec): return True # Adding a page - enabled by default image_info: ImageInfo = self._pagesInfoVec[page] return image_info.GetEnabled() def EnableTab(self, page: int, enabled: bool = True) -> None: # noqa: N802 """Enable or disable a tab. Args: page (int): an integer specifying the page index; enabled (bool): ``True`` to enable a tab, ``False`` to disable it. """ if page >= len(self._pagesInfoVec): return image_info: ImageInfo = self._pagesInfoVec[page] image_info.EnableTab(enabled) def ClearAll(self) -> None: # noqa: N802 """Delete all the pages in the container.""" self._pagesInfoVec = [] self._nIndex = wx.NOT_FOUND def DoDeletePage(self, page: int) -> None: # noqa: N802 """Delete the page. Args: page (int): the index of the tab. """ # Remove the page from the vector book: wx.Window = self.GetParent() self._pagesInfoVec.pop(page) if self._nIndex >= page: self._nIndex = self._nIndex - 1 # The delete page was the last first on the array, # but the book still has more pages, so we set the # active page to be the first one (0) if self._nIndex < 0 and len(self._pagesInfoVec) > 0: self._nIndex = 0 # Refresh the tabs if self._nIndex >= 0 and isinstance(book, FlatBookBase): book._bForceSelection = True book.SetSelection(self._nIndex) book._bForceSelection = False if not self._pagesInfoVec: # Erase the page container drawings dc = wx.ClientDC(self) dc.Clear() def OnSize(self, event: wx.SizeEvent) -> None: # noqa: N802 """Handle the wx.EVT_SIZE event for wx.ImageContainerBase. Args: event (wx.SizeEvent): a :class:`wx.SizeEvent` event to be processed. """ self.Refresh() # Call on paint event.Skip() def OnEraseBackground(self, event: wx.EraseEvent) -> None: # noqa: N802 """Handle the wx.EVT_ERASE_BACKGROUND event for wx.ImageContainerBase. Args: event (wx.EraseEvent): a :class:`EraseEvent` event to be processed. Note: This method is intentionally empty to reduce flicker. """ def HitTest(self, pt: wx.Point) -> tuple[int, int]: # noqa: N802 """Return the index of the tab at the specified position or NOT_FOUND if None. plus the flag style of :meth:`~ImageContainerBase.HitTest`. Args: pt (wx.Point): an instance of :class:`wx.Point`, to test for hits. Returns: The index of the tab at the specified position plus the hit test flag, which can be one of the following bits: ====================== ======= ========================================= HitTest Flags Value Description ====================== ======= ========================================= ``IMG_OVER_IMG`` 0 The mouse is over the tab icon ``IMG_OVER_PIN`` 1 The mouse is over the pin button ``IMG_OVER_EW_BORDER`` 2 The mouse is over the east-west book border ``IMG_NONE`` 3 Nowhere ====================== ======= ========================================= """ style: int = self.GetParent().GetAGWWindowStyleFlag() if style & ImageBookStyle.INB_USE_PIN_BUTTON and self._pinBtnRect.Contains(pt): return -1, IMG_OVER_PIN for i in range(len(self._pagesInfoVec)): if self._pagesInfoVec[i].GetPosition() == wx.Point(-1, -1): break # For Web Hover style, we test the TextRect if not self.HasAGWFlag(ImageBookStyle.INB_WEB_HILITE): button_rect = wx.Rect( self._pagesInfoVec[i].GetPosition(), self._pagesInfoVec[i].GetSize() ) else: button_rect: wx.Rect = self._pagesInfoVec[i].GetTextRect() if button_rect.Contains(pt): return i, IMG_OVER_IMG if self.PointOnSash(pt): return -1, IMG_OVER_EW_BORDER return -1, IMG_NONE def PointOnSash(self, pt: wx.Point) -> bool: # noqa: N802 """Test whether pt is located on the sash. Args: pt (wx.Point): an instance of :class:`wx.Point`, to test for hits. """ # Check if we are on a the sash border clt_rect: wx.Rect = self.GetClientRect() if self.HasAGWFlag(ImageBookStyle.INB_LEFT) or self.HasAGWFlag( ImageBookStyle.INB_TOP ): if pt.x > clt_rect.x + clt_rect.width - 4: return True elif pt.x < 4: return True return False def OnMouseLeftDown(self, event: wx.MouseEvent) -> None: # noqa: N802 """Handle the ``wx.EVT_LEFT_DOWN`` event for :class:`wx.ImageContainerBase`. Args: event (wx.MouseEvent): a :class:`MouseEvent` event to be processed. """ event.Skip() # Support for collapse/expand style: int = self.GetParent().GetAGWWindowStyleFlag() if style & ImageBookStyle.INB_USE_PIN_BUTTON and self._pinBtnRect.Contains( event.GetPosition() ): self._nPinButtonStatus = INB_PIN_PRESSED dc = wx.ClientDC(self) self.DrawPin(dc, self._pinBtnRect, not self._bCollapsed) return # In case panel is collapsed, there is nothing to check if self._bCollapsed: return tab_id, where = self.HitTest(event.GetPosition()) if where == IMG_OVER_IMG: self._nHoveredImgIdx = -1 if tab_id == -1: return self.GetParent().SetSelection(tab_id) def OnMouseLeaveWindow(self, event: wx.MouseEvent) -> None: # noqa: N802 """Handle the ``wx.EVT_LEAVE_WINDOW`` event for :class:`wx.ImageContainerBase`. Args: event (wx.MouseEvent): a :class:`MouseEvent` event to be processed. """ b_repaint: bool = self._nHoveredImgIdx != -1 self._nHoveredImgIdx = -1 # Make sure the pin button status is NONE in case we were in pin button style style: int = self.GetParent().GetAGWWindowStyleFlag() if style & ImageBookStyle.INB_USE_PIN_BUTTON: self._nPinButtonStatus = INB_PIN_NONE dc = wx.ClientDC(self) self.DrawPin(dc, self._pinBtnRect, not self._bCollapsed) # Restore cursor self.SetCursor(wx.Cursor(wx.CURSOR_ARROW)) if b_repaint: self.Refresh() def OnMouseLeftUp(self, event: wx.MouseEvent) -> None: # noqa: N802 """Handle the ``wx.EVT_LEFT_UP`` event for :class:`wx.ImageContainerBase`. Args: event (wx.MouseEvent): a :class:`MouseEvent` event to be processed. """ style: int = self.GetParent().GetAGWWindowStyleFlag() if not style & ImageBookStyle.INB_USE_PIN_BUTTON: return b_is_label_container: bool = not self.CanDoBottomStyle() if not self._pinBtnRect.Contains(event.GetPosition()): return self._nPinButtonStatus = INB_PIN_NONE self._bCollapsed: bool = not self._bCollapsed if self._bCollapsed: # Save the current tab area width self._tabAreaSize = self.GetSize() if b_is_label_container: self.SetSizeHints(20, self._tabAreaSize[1]) elif style & ImageBookStyle.INB_BOTTOM or style & ImageBookStyle.INB_TOP: self.SetSizeHints(self._tabAreaSize[0], 20) else: self.SetSizeHints(20, self._tabAreaSize[1]) elif b_is_label_container: self.SetSizeHints(self._tabAreaSize[0], -1) # Restore the tab area size elif style & ImageBookStyle.INB_BOTTOM or style & ImageBookStyle.INB_TOP: self.SetSizeHints(-1, self._tabAreaSize[1]) else: self.SetSizeHints(self._tabAreaSize[0], -1) self.GetParent().GetSizer().Layout() self.Refresh() return def OnMouseMove(self, event: wx.MouseEvent) -> None: # noqa: N802 """Handle the ``wx.EVT_MOTION`` event for :class:`wx.ImageContainerBase`. Args: event (wx.MouseEvent): a :class:`MouseEvent` event to be processed. """ style: int = self.GetParent().GetAGWWindowStyleFlag() if style & ImageBookStyle.INB_USE_PIN_BUTTON and ( not self._pinBtnRect.Contains(event.GetPosition()) and self._nPinButtonStatus == INB_PIN_PRESSED ): # Check to see if we are in the pin button rect self._nPinButtonStatus = INB_PIN_NONE dc = wx.ClientDC(self) self.DrawPin(dc, self._pinBtnRect, not self._bCollapsed) image_id, _ = self.HitTest(event.GetPosition()) # Allow hovering unless over current tab or tab is disabled self._nHoveredImgIdx = -1 if ( image_id < len(self._pagesInfoVec) and self.GetEnabled(image_id) and image_id != self._nIndex ): self._nHoveredImgIdx: int = image_id if not self._bCollapsed: if self._nHoveredImgIdx >= 0 and self.HasAGWFlag( ImageBookStyle.INB_WEB_HILITE ): # Change the cursor to be Hand if we have the Web hover style set self.SetCursor(wx.Cursor(wx.CURSOR_HAND)) elif not self.PointOnSash(event.GetPosition()): # Restore the cursor if we are not currently hovering the sash self.SetCursor(wx.Cursor(wx.CURSOR_ARROW)) self.Refresh() def DrawPin(self, dc: wx.DC, rect: wx.Rect, down_pin: bool) -> None: # noqa: N802 """Draw a pin button, that allows collapsing of the image panel. Args: dc (wx.DC): an instance of :class:`wx.DC`; rect (wx.Rect): the pin button client rectangle; down_pin (bool): ``True`` if the pin button is facing downwards, ``False`` if it is facing leftwards. """ # Set the bitmap according to the button status pin_bmp = wx.Bitmap(PIN_DOWN_XPM) if down_pin else wx.Bitmap(PIN_LEFT_XPM) xx: int = rect.x + 2 if self._nPinButtonStatus in [INB_PIN_HOVER, INB_PIN_NONE]: dc.SetBrush(wx.TRANSPARENT_BRUSH) dc.SetPen(wx.BLACK_PEN) dc.DrawRectangle(xx, rect.y, 16, 16) # Draw upper and left border with grey colour dc.SetPen(wx.WHITE_PEN) dc.DrawLine(xx, rect.y, xx + 16, rect.y) dc.DrawLine(xx, rect.y, xx, rect.y + 16) elif self._nPinButtonStatus == INB_PIN_PRESSED: dc.SetBrush(wx.TRANSPARENT_BRUSH) dc.SetPen(wx.LIGHT_GREY_PEN) dc.DrawRectangle(xx, rect.y, 16, 16) # Draw upper and left border with grey colour dc.SetPen(wx.BLACK_PEN) dc.DrawLine(xx, rect.y, xx + 16, rect.y) dc.DrawLine(xx, rect.y, xx, rect.y + 16) # Set the masking pin_bmp.SetMask(wx.Mask(pin_bmp, wx.WHITE)) # Draw the new bitmap dc.DrawBitmap(pin_bmp, xx, rect.y, True) # Save the pin rect self._pinBtnRect = rect # ------------------------------------------------------------------------------------ # # Class ImageContainer # ------------------------------------------------------------------------------------ # class ImageContainer(ImageContainerBase): """Base class for :class:`FlatImageBook` image container.""" def __init__( self, parent: wx.Window, id: int = wx.ID_ANY, # noqa: A002 pos: wx.Point = wx.DefaultPosition, size: wx.Size = wx.DefaultSize, style: int = 0, agw_style: int = 0, name: str = "ImageContainer", ) -> None: """Construct the default class. Args: parent (wx.Window): parent window. Must not be ``None``; id (int): window identifier. A value of -1 indicates a default value; pos (wx.Point): the control position. A value of (-1, -1) indicates a default position, chosen by either the windowing system or wxPython, depending on platform; size (wx.Size): the control size. A value of (-1, -1) indicates a default size, chosen by either the windowing system or wxPython, depending on platform; style (int): the underlying :class:`Panel` window style; agw_style (int): the AGW-specific window style. This can be a combination of the following bits: =========================== =========== ================================ Window Styles Hex Value Description =========================== =========== ================================ ``INB_BOTTOM`` 0x1 Place labels below the page area. Available only for :class:`FlatImageBook`. ``INB_LEFT`` 0x2 Place labels on the left side. Available only for :class:`FlatImageBook`. ``INB_RIGHT`` 0x4 Place labels on the right side. ``INB_TOP`` 0x8 Place labels above the page area. ``INB_BORDER`` 0x10 Draws a border around :class:`LabelBook` or :class:`FlatImageBook`. ``INB_SHOW_ONLY_TEXT`` 0x20 Shows only text labels and no images. Available only for :class:`LabelBook`. ``INB_SHOW_ONLY_IMAGES`` 0x40 Shows only tab images and no label texts. Available only for :class:`LabelBook`. ``INB_FIT_BUTTON`` 0x80 Displays a pin button to show/hide the book control. ``INB_DRAW_SHADOW`` 0x100 Draw shadows below the book tabs. Available only for :class:`LabelBook`. ``INB_USE_PIN_BUTTON`` 0x200 Displays a pin button to show/hide the book control. ``INB_GRADIENT_BACKGROUND`` 0x400 Draws a gradient shading on the tabs background. Available only for :class:`LabelBook`. ``INB_WEB_HILITE`` 0x800 On mouse hovering, tabs behave like html hyperlinks. Available only for :class:`LabelBook`. ``INB_NO_RESIZE`` 0x1000 Don't allow resizing of the tab area. ``INB_FIT_LABELTEXT`` 0x2000 Will fit the tab area to the longest text (or text+image if you have images) in all the tabs. ``INB_BOLD_TAB_SELECTION`` 0x4000 Show the selected tab text using a bold font. =========================== =========== ================================ name (str): the window name. """ ImageContainerBase.__init__(self, parent, id, pos, size, style, agw_style, name) self.Bind(wx.EVT_PAINT, self.OnPaint) self.Bind(wx.EVT_SIZE, self.OnSize) self.Bind(wx.EVT_LEFT_DOWN, self.OnMouseLeftDown) self.Bind(wx.EVT_LEFT_UP, self.OnMouseLeftUp) self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground) self.Bind(wx.EVT_MOTION, self.OnMouseMove) self.Bind(wx.EVT_LEAVE_WINDOW, self.OnMouseLeaveWindow) self.padding = 4 # Define padding as a class attribute def OnSize(self, event: wx.SizeEvent) -> None: # noqa: N802 """Handle the ``wx.EVT_SIZE`` event for :class:`wx.ImageContainer`. Args: event (wx.SizeEvent): a :class:`wx.SizeEvent` event to be processed. """ ImageContainerBase.OnSize(self, event) event.Skip() def OnMouseLeftDown(self, event: wx.MouseEvent) -> None: # noqa: N802 """Handle the ``wx.EVT_LEFT_DOWN`` event for :class:`wx.ImageContainer`. Args: event (wx.MouseEvent): a :class:`MouseEvent` event to be processed. """ ImageContainerBase.OnMouseLeftDown(self, event) event.Skip() def OnMouseLeftUp(self, event: wx.MouseEvent) -> None: # noqa: N802 """Handle the ``wx.EVT_LEFT_UP`` event for :class:`wx.ImageContainer`. Args: event (wx.MouseEvent): a :class:`MouseEvent` event to be processed. """ ImageContainerBase.OnMouseLeftUp(self, event) event.Skip() def OnEraseBackground(self, event: wx.EraseEvent) -> None: # noqa: N802 """Handle the ``wx.EVT_ERASE_BACKGROUND`` event for :class:`wx.ImageContainer`. Args: event (wx.EraseEvent): a :class:`EraseEvent` event to be processed. """ ImageContainerBase.OnEraseBackground(self, event) def OnMouseMove(self, event: wx.MouseEvent) -> None: # noqa: N802 """Handle the ``wx.EVT_MOTION`` event for :class:`wx.ImageContainer`. Args: event (wx.MouseEvent): a :class:`MouseEvent` event to be processed. """ ImageContainerBase.OnMouseMove(self, event) event.Skip() def OnMouseLeaveWindow(self, event: wx.MouseEvent) -> None: # noqa: N802 """Handle the ``wx.EVT_LEAVE_WINDOW`` event for :class:`wx.ImageContainer`. Args: event (wx.MouseEvent): a :class:`MouseEvent` event to be processed. """ ImageContainerBase.OnMouseLeaveWindow(self, event) event.Skip() def CanDoBottomStyle(self) -> bool: # noqa: N802 """Allow the parent to examine the children type. Some implementation (such as :class:`LabelBook`), does not support top/bottom images, only left/right. """ return True def OnPaint(self, event: wx.PaintEvent) -> None: # noqa: N802 """Handle the ``wx.EVT_PAINT`` event for :class:`wx.ImageContainer`. Args: event (wx.PaintEvent): a :class:`PaintEvent` event to be processed. """ dc = wx.BufferedPaintDC(self) style: int = ImageBookStyle(self.GetParent().GetAGWWindowStyleFlag()) self._draw_background(dc, style) self._draw_pin_button(dc, style) self._draw_buttons(dc, style) def _draw_background(self, dc: wx.DC, style: ImageBookStyle) -> None: size: wx.Size = self.GetSize() back_brush: wx.Brush = wx.WHITE_BRUSH border_pen: wx.Pen = ( wx.Pen(wx.SystemSettings.GetColour(wx.SYS_COLOUR_3DSHADOW)) if style & ImageBookStyle.INB_BORDER else wx.TRANSPARENT_PEN ) dc.SetBrush(back_brush) dc.SetPen(border_pen) dc.DrawRectangle(0, 0, size.width, size.height) def _draw_pin_button(self, dc: wx.DC, style: ImageBookStyle) -> None: if not (style & ImageBookStyle.INB_USE_PIN_BUTTON): return client_rect: wx.Rect = self.GetClientRect() pin_rect = wx.Rect(client_rect.x + client_rect.width - 20, 2, 20, 20) self.DrawPin(dc, pin_rect, not self._bCollapsed) if self._bCollapsed: return def _draw_buttons(self, dc: wx.DC, style: ImageBookStyle) -> None: size: wx.Size = self.GetSize() b_use_pin: ImageBookStyle = style & ImageBookStyle.INB_USE_PIN_BUTTON b_use_y_coord: ImageBookStyle = style & ( ImageBookStyle.INB_RIGHT | ImageBookStyle.INB_LEFT ) client_size: int = size.height if b_use_y_coord else size.width pin_btn_size: int = 20 if b_use_pin else 0 normal_font: wx.Font = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT) bold_font: wx.Font = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT) bold_font.SetWeight(wx.FONTWEIGHT_BOLD) padding = 4 text_padding_left = 2 img_top_padding = 10 pos: int = 0 if style & ImageBookStyle.INB_BORDER else 1 if b_use_pin and (style & (ImageBookStyle.INB_TOP | ImageBookStyle.INB_BOTTOM)): pos += 20 for i, page_info in enumerate(self._pagesInfoVec): if ( pos + self._get_button_width(dc, page_info, style, pin_btn_size) > client_size ): break button_rect: wx.Rect = self._get_button_rect(pos, size, style) self._draw_button( dc, i, button_rect, style, normal_font, bold_font, padding, text_padding_left, img_top_padding, ) pos += self._get_button_width(dc, page_info, style, pin_btn_size) self._update_non_visible_buttons(pos) def _get_button_width( self, dc: wx.DC, page_info: ImageInfo, style: ImageBookStyle, pin_button_size: int, ) -> int: rect_width: int = self._nImgSize * 2 if ( style & ImageBookStyle.INB_FIT_BUTTON and not (style & (ImageBookStyle.INB_LEFT | ImageBookStyle.INB_RIGHT)) and page_info.GetCaption() and not (style & ImageBookStyle.INB_SHOW_ONLY_IMAGES) ): text_width, _ = dc.GetTextExtent(page_info.GetCaption()) rect_width = max(rect_width, text_width + self.padding * 2) if rect_width % 2 != 0: rect_width += 1 return rect_width + pin_button_size def _get_button_rect( self, pos: int, size: wx.Size, style: ImageBookStyle ) -> wx.Rect: rect_width: int = self._nImgSize * 2 rect_height: int = self._nImgSize * 2 if style & (ImageBookStyle.INB_RIGHT | ImageBookStyle.INB_LEFT): rect_width -= 2 else: rect_height -= 2 if style & (ImageBookStyle.INB_RIGHT | ImageBookStyle.INB_LEFT): return wx.Rect(1, pos, rect_width, rect_height) return wx.Rect(pos, 1, rect_width, rect_height) def _draw_button( self, dc: wx.DC, index: int, button_rect: wx.Rect, style: ImageBookStyle, normal_font: wx.Font, bold_font: wx.Font, padding: int, text_padding_left: int, image_top_padding: int, ) -> None: if self._nIndex == index: self._draw_selected_button(dc, button_rect, style) if self._nHoveredImgIdx == index: self._draw_hovered_button(dc, button_rect, style) self._draw_button_content( dc, index, button_rect, style, normal_font, bold_font, padding, text_padding_left, image_top_padding, ) def _draw_selected_button( self, dc: wx.DC, button_rect: wx.Rect, style: ImageBookStyle ) -> None: pen_colour: wx.Colour = wx.SystemSettings.GetColour(wx.SYS_COLOUR_ACTIVECAPTION) brush_colour: wx.Colour = ArtManager.Get().LightColour( wx.SystemSettings.GetColour(wx.SYS_COLOUR_ACTIVECAPTION), 75 ) dc.SetPen(wx.Pen(pen_colour)) dc.SetBrush(wx.Brush(brush_colour)) if style & ImageBookStyle.INB_BORDER: button_rect = self._adjust_button_rect(button_rect, style) dc.DrawRectangle(button_rect) def _draw_hovered_button( self, dc: wx.DC, button_rect: wx.Rect, style: ImageBookStyle ) -> None: pen_colour: wx.Colour = wx.SystemSettings.GetColour(wx.SYS_COLOUR_ACTIVECAPTION) brush_colour: wx.Colour = ArtManager.Get().LightColour( wx.SystemSettings.GetColour(wx.SYS_COLOUR_ACTIVECAPTION), 90 ) dc.SetPen(wx.Pen(pen_colour)) dc.SetBrush(wx.Brush(brush_colour)) if style & ImageBookStyle.INB_BORDER: button_rect = self._adjust_button_rect(button_rect, style) dc.DrawRectangle(button_rect) def _adjust_button_rect( self, button_rect: wx.Rect, style: ImageBookStyle ) -> wx.Rect: if style & (ImageBookStyle.INB_TOP | ImageBookStyle.INB_BOTTOM): return wx.Rect( button_rect.x + 1, button_rect.y, button_rect.width - 1, button_rect.height, ) return wx.Rect( button_rect.x, button_rect.y + 1, button_rect.width, button_rect.height - 1 ) def _draw_button_content( self, dc: wx.DC, index: int, button_rect: wx.Rect, style: ImageBookStyle, normal_font: wx.Font, bold_font: wx.Font, padding: int, text_padding_left: int, image_top_padding: int, ) -> None: page_info: ImageInfo = self._pagesInfoVec[index] dc.SetFont( normal_font if not ( style & ImageBookStyle.INB_BOLD_TAB_SELECTION and self._nIndex == index ) else bold_font ) if not ( style & ImageBookStyle.INB_SHOW_ONLY_TEXT and style & ImageBookStyle.INB_SHOW_ONLY_IMAGES ): if ( style & ImageBookStyle.INB_SHOW_ONLY_TEXT and style & ImageBookStyle.INB_SHOW_ONLY_IMAGES ): style ^= ImageBookStyle.INB_SHOW_ONLY_TEXT style ^= ImageBookStyle.INB_SHOW_ONLY_IMAGES self.GetParent().SetAGWWindowStyleFlag(style) if ( not style & ImageBookStyle.INB_SHOW_ONLY_TEXT and page_info.GetImageIndex() != -1 and self._ImageList is not None ): img_x_coord, img_y_coord = self._get_image_coords( button_rect, style, padding, image_top_padding ) self._ImageList.Draw( page_info.GetImageIndex(), dc, img_x_coord, img_y_coord, wx.IMAGELIST_DRAW_TRANSPARENT, True, ) if ( not style & ImageBookStyle.INB_SHOW_ONLY_IMAGES and page_info.GetCaption() ): fixed_text = self._get_fixed_text( dc, page_info.GetCaption(), style, padding ) text_offset_x, text_offset_y = self._get_text_coords( dc, button_rect, style, padding, text_padding_left, image_top_padding, index, ) dc.SetTextForeground( wx.SystemSettings.GetColour(wx.SYS_COLOUR_WINDOWTEXT) ) dc.DrawText(fixed_text, text_offset_x, text_offset_y) page_info.SetPosition(button_rect.GetPosition()) page_info.SetSize(button_rect.GetSize()) def _get_image_coords( self, button_rect: wx.Rect, style: ImageBookStyle, padding: int, image_top_padding: int, ) -> tuple[int, int]: if style & (ImageBookStyle.INB_RIGHT | ImageBookStyle.INB_LEFT): img_x_coord: int = self._nImgSize // 2 img_y_coord: int = button_rect.y + ( self._nImgSize // 2 if style & ImageBookStyle.INB_SHOW_ONLY_IMAGES else image_top_padding ) else: img_x_coord = ( button_rect.x + (button_rect.width // 2) - (self._nImgSize // 2) ) img_y_coord = ( self._nImgSize // 2 if style & ImageBookStyle.INB_SHOW_ONLY_IMAGES else image_top_padding ) return img_x_coord, img_y_coord def _get_fixed_text( self, dc: wx.DC, caption: str, style: ImageBookStyle, padding: int ) -> None | str: if not ( style & ImageBookStyle.INB_FIT_BUTTON or style & (ImageBookStyle.INB_LEFT | ImageBookStyle.INB_RIGHT) ): return self.FixTextSize(dc, caption, self._nImgSize * 2 - 4) return caption def _get_text_coords( self, dc: wx.DC, button_rect: wx.Rect, style: ImageBookStyle, padding: int, text_padding_left: int, image_top_padding: int, index: int, ) -> tuple[int, int]: text_width, text_height = dc.GetTextExtent( self._get_fixed_text( dc, self._pagesInfoVec[index].GetCaption(), style, padding ) ) if style & (ImageBookStyle.INB_RIGHT | ImageBookStyle.INB_LEFT): text_offset_x: int = (button_rect.width - text_width) // 2 text_offset_y: int = button_rect.y + ( image_top_padding + 3 if not style & ImageBookStyle.INB_SHOW_ONLY_TEXT else (self._nImgSize * 2 - text_height) // 2 ) else: text_offset_x = ( (button_rect.width - text_width) // 2 + button_rect.x + text_padding_left ) text_offset_y = ( image_top_padding + 3 if not style & ImageBookStyle.INB_SHOW_ONLY_TEXT else (self._nImgSize * 2 - text_height) // 2 ) return text_offset_x, text_offset_y def _update_non_visible_buttons(self, pos: int) -> None: for ii in range(pos // (self._nImgSize * 2), len(self._pagesInfoVec)): self._pagesInfoVec[ii].SetPosition(wx.Point(-1, -1)) # ------------------------------------------------------------------------------------ # # Class LabelContainer # ------------------------------------------------------------------------------------ # class LabelContainer(ImageContainerBase): """Base class for :class:`LabelBook`.""" num_padding = 6 def __init__( self, parent: wx.Window, id: int = wx.ID_ANY, # noqa: A002 pos: wx.Point = wx.DefaultPosition, size: wx.Size = wx.DefaultSize, style: int = 0, agw_style: int = 0, name: str = "LabelContainer", ) -> None: """Construct the default class. Args: parent (wx.Window): parent window. Must not be ``None``; id (int): window identifier. A value of -1 indicates a default value; pos (wx.Point): the control position. A value of (-1, -1) indicates a default position, chosen by either the windowing system or wxPython, depending on platform; size (wx.Size): the control size. A value of (-1, -1) indicates a default size, chosen by either the windowing system or wxPython, depending on platform; style (int): the underlying :class:`Panel` window style; agw_style (int): the AGW-specific window style. This can be a combination of the following bits: =========================== =========== ================================ Window Styles Hex Value Description =========================== =========== ================================ ``INB_BOTTOM`` 0x1 Place labels below the page area. Available only for :class:`FlatImageBook`. ``INB_LEFT`` 0x2 Place labels on the left side. Available only for :class:`FlatImageBook`. ``INB_RIGHT`` 0x4 Place labels on the right side. ``INB_TOP`` 0x8 Place labels above the page area. ``INB_BORDER`` 0x10 Draws a border around :class:`LabelBook` or :class:`FlatImageBook`. ``INB_SHOW_ONLY_TEXT`` 0x20 Shows only text labels and no images. Available only for :class:`LabelBook`. ``INB_SHOW_ONLY_IMAGES`` 0x40 Shows only tab images and no label texts. Available only for :class:`LabelBook`. ``INB_FIT_BUTTON`` 0x80 Displays a pin button to show/hide the book control. ``INB_DRAW_SHADOW`` 0x100 Draw shadows below the book tabs. Available only for :class:`LabelBook`. ``INB_USE_PIN_BUTTON`` 0x200 Displays a pin button to show/hide the book control. ``INB_GRADIENT_BACKGROUND`` 0x400 Draws a gradient shading on the tabs background. Available only for :class:`LabelBook`. ``INB_WEB_HILITE`` 0x800 On mouse hovering, tabs behave like html hyperlinks. Available only for :class:`LabelBook`. ``INB_NO_RESIZE`` 0x1000 Don't allow resizing of the tab area. ``INB_FIT_LABELTEXT`` 0x2000 Will fit the tab area to the longest text (or text+image if you have images) in all the tabs. ``INB_BOLD_TAB_SELECTION`` 0x4000 Show the selected tab text using a bold font. =========================== =========== ================================ name (str): the window name. """ ImageContainerBase.__init__(self, parent, id, pos, size, style, agw_style, name) self._nTabAreaWidth = 100 self._oldCursor: wx.Cursor = wx.NullCursor self._coloursMap: dict[int, wx.Colour] = {} self._skin: wx.Bitmap = wx.NullBitmap self._sashRect = wx.Rect() self.Bind(wx.EVT_PAINT, self.OnPaint) self.Bind(wx.EVT_SIZE, self.OnSize) self.Bind(wx.EVT_LEFT_DOWN, self.OnMouseLeftDown) self.Bind(wx.EVT_LEFT_UP, self.OnMouseLeftUp) self.Bind(wx.EVT_MOTION, self.OnMouseMove) self.Bind(wx.EVT_LEAVE_WINDOW, self.OnMouseLeaveWindow) self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground) self._ImageList: None | wx.ImageList = None self._initialize_image_list() def _initialize_image_list(self) -> None: # Initialize the image list with default values or from a source self._ImageList = wx.ImageList(16, 16) # Example initialization def OnSize(self, event: wx.SizeEvent) -> None: # noqa: N802 """Handle the ``wx.EVT_SIZE`` event for :class:`LabelContainer`. Args: event (wx.SizeEvent): a :class:`wx.SizeEvent` event to be processed. """ ImageContainerBase.OnSize(self, event) event.Skip() def OnEraseBackground(self, event: wx.EraseEvent) -> None: # noqa: N802 """Handle the ``wx.EVT_ERASE_BACKGROUND`` event for :class:`LabelContainer`. Args: event (wx.EraseEvent): a :class:`EraseEvent` event to be processed. """ ImageContainerBase.OnEraseBackground(self, event) def GetTabAreaWidth(self) -> int: # noqa: N802 """Return the width of the tab area.""" return self._nTabAreaWidth def SetTabAreaWidth(self, width: int) -> None: # noqa: N802 """Set the width of the tab area. Args: width (int): the width of the tab area, in pixels. """ self._nTabAreaWidth: int = width self.SetSizeHints(width, -1) def CanDoBottomStyle(self) -> bool: # noqa: N802 """Allow the parent to examine the children type. Some implementation (such as :class:`LabelBook`), does not support top/bottom images, only left/right. """ return False def SetBackgroundBitmap(self, bmp: wx.Bitmap) -> None: # noqa: N802 """Set the background bitmap for the control. Args: bmp (wx.Bitmap): a valid :class:`wx.Bitmap` object. """ self._skin = bmp def OnPaint(self, event: wx.PaintEvent) -> None: # noqa: N802 """Handle the ``wx.EVT_PAINT`` event for :class:`LabelContainer`. Args: event (wx.PaintEvent): a :class:`PaintEvent` event to be processed. """ style: int = self.GetParent().GetAGWWindowStyleFlag() # In case user set both flags, we override them to display both # INB_SHOW_ONLY_TEXT and INB_SHOW_ONLY_IMAGES if ( style & ImageBookStyle.INB_SHOW_ONLY_TEXT and style & ImageBookStyle.INB_SHOW_ONLY_IMAGES ): style ^= ImageBookStyle.INB_SHOW_ONLY_TEXT style ^= ImageBookStyle.INB_SHOW_ONLY_IMAGES self.GetParent().SetAGWWindowStyleFlag(style) dc = wx.BufferedPaintDC(self) self._draw_background(dc) self._draw_tabs(dc) self._draw_pin_button(dc) def _draw_background(self, dc: wx.DC) -> None: """Draw the background of the control. Args: dc (wx.DC): an instance of :class:`wx.DC`. """ size: wx.Size = self.GetSize() back_brush = wx.Brush(self._coloursMap[INB_TAB_AREA_BACKGROUND_COLOUR]) border_pen: wx.Pen = ( wx.Pen(self._coloursMap[INB_TABS_BORDER_COLOUR]) if self.HasAGWFlag(ImageBookStyle.INB_BORDER) else wx.TRANSPARENT_PEN ) dc.SetBrush(back_brush) dc.SetPen(border_pen) dc.DrawRectangle(wx.Rect(0, 0, size.x, size.y)) if ( self.HasAGWFlag(ImageBookStyle.INB_GRADIENT_BACKGROUND) and not self._skin.IsOk() ): self._draw_gradient_background(dc, size) def _draw_gradient_background(self, dc: wx.DC, size: wx.Size) -> None: """Draw a gradient background. Args: dc (wx.DC): an instance of :class:`wx.DC`. size (wx.Size): the size of the control. """ start_colour: wx.Colour = self._coloursMap[INB_TAB_AREA_BACKGROUND_COLOUR] end_colour: wx.Colour = ArtManager.Get().LightColour(start_colour, 50) ArtManager.Get().PaintStraightGradientBox( dc, wx.Rect(0, 0, size.x // 2, size.y), start_colour, end_colour, False ) ArtManager.Get().PaintStraightGradientBox( dc, wx.Rect(size.x // 2, 0, size.x // 2, size.y), end_colour, start_colour, False, ) def _draw_tabs(self, dc: wx.DC) -> None: """Draw the tabs. Args: dc (wx.DC): an instance of :class:`wx.DC`. """ size: wx.Size = self.GetSize() posy = 20 count = 0 for i in range(len(self._pagesInfoVec)): count += 1 rect_width: int = self._nTabAreaWidth rect_height: int = self._calculate_tab_height(i) if posy + rect_height > size.GetHeight(): break button_rect = wx.Rect(0, posy, rect_width, rect_height) self._draw_tab( dc, button_rect, i, selected=self._nIndex == i, hover=self._nHoveredImgIdx == i, ) posy += rect_height self._update_non_visible_tabs(count) def _calculate_tab_height(self, index: int) -> int: """Calculate the height of a tab. Args: index (int): the index of the tab. Returns: int: the height of the tab. """ if not self.HasAGWFlag(ImageBookStyle.INB_SHOW_ONLY_TEXT): return self._nImgSize * 2 font: wx.Font = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT) font.SetPointSize( int(font.GetPointSize() * self.GetParent().GetFontSizeMultiple()) ) if self.GetParent().GetFontBold() or ( self.HasAGWFlag(ImageBookStyle.INB_BOLD_TAB_SELECTION) and self._nIndex == index ): font.SetWeight(wx.FONTWEIGHT_BOLD) dc = wx.ClientDC(self) dc.SetFont(font) w, h = dc.GetTextExtent(self._pagesInfoVec[index].GetCaption()) return h * 2 def _draw_tab( self, dc: wx.DC, rect: wx.Rect, index: int, selected: bool, hover: bool, ) -> None: """Draw a single tab. Args: dc (wx.DC): an instance of :class:`wx.DC`. rect (wx.Rect): the tab client rectangle. index (int): the index of the tab. selected (bool): ``True`` if the tab is selected, ``False`` otherwise. hover (bool): ``True`` if the tab is being hovered with the mouse, ``False`` otherwise. """ orientation_left: bool = self.HasAGWFlag( ImageBookStyle.INB_LEFT ) or self.HasAGWFlag(ImageBookStyle.INB_TOP) text: str = self._pagesInfoVec[index].GetCaption() bmp: wx.Bitmap = self._get_tab_bitmap(index) text_rect: wx.Rect = self._calculate_text_rect(dc, rect, text, bmp) image_rect: wx.Rect = self._calculate_image_rect(dc, rect, bmp) image_info: ImageInfo = self._pagesInfoVec[ index ] # Assuming ImageInfo is a part of _pagesInfoVec self._draw_tab_background(dc, rect, selected, orientation_left) self._draw_tab_text(dc, text_rect, text, selected, image_info) self._draw_tab_image(dc, image_rect, bmp) self._draw_tab_shadow(dc, rect, selected, index, hover) self._draw_tab_hover_effect( dc, rect, text_rect, text, selected, hover, image_info ) self._pagesInfoVec[index].SetPosition(rect.GetPosition()) self._pagesInfoVec[index].SetSize(rect.GetSize()) def _get_tab_bitmap(self, index: int) -> wx.Bitmap: """Get the bitmap for a tab. Args: index (int): the index of the tab. Returns: wx.Bitmap: the bitmap for the tab. """ if self._ImageList is None or self._pagesInfoVec[index].GetImageIndex() == -1: return wx.NullBitmap return self._ImageList.GetBitmap(self._pagesInfoVec[index].GetImageIndex()) def _update_non_visible_tabs(self, count: int) -> None: """Update the visibility of tabs that do not fit on the screen. Args: count (int): the number of tabs that fit on the screen. """ for ii in range(count, len(self._pagesInfoVec)): self._pagesInfoVec[ii].SetPosition(wx.Point(-1, -1)) def _draw_pin_button(self, dc: wx.DC) -> None: """Draw the pin button if it is enabled and the control is not collapsed. Args: dc (wx.DC): an instance of :class:`wx.DC`. """ if self.HasAGWFlag(ImageBookStyle.INB_USE_PIN_BUTTON) and not self._bCollapsed: client_rect: wx.Rect = self.GetClientRect() pin_rect = wx.Rect( client_rect.GetX() + client_rect.GetWidth() - 20, 2, 20, 20 ) self.DrawPin(dc, pin_rect, not self._bCollapsed) def DrawBackgroundBitmap(self, dc: wx.DC) -> None: # noqa: N802 """Draw a bitmap as the background of the control. Args: dc (wx.DC): an instance of :class:`wx.DC`. """ client_rect: wx.Rect = self.GetClientRect() width: int = client_rect.GetWidth() height: int = client_rect.GetHeight() xstep: int = self._skin.GetWidth() ystep: int = self._skin.GetHeight() bmp_rect = wx.Rect(0, 0, xstep, ystep) if bmp_rect != client_rect: self._draw_tiled_background(dc, width, height, xstep, ystep) else: dc.DrawBitmap(self._skin, 0, 0) def _draw_tiled_background( self, dc: wx.DC, width: int, height: int, xstep: int, ystep: int ) -> None: """Draw a tiled background. Args: dc (wx.DC): an instance of :class:`wx.DC`. width (int): the width of the control. height (int): the height of the control. xstep (int): the width of the background bitmap. ystep (int): the height of the background bitmap. """ mem_dc = wx.MemoryDC() bmp = wx.Bitmap(width, height) mem_dc.SelectObject(bmp) covered_y = 0 covered_x = 0 while covered_y < height: while covered_x < width: mem_dc.DrawBitmap(self._skin, covered_x, covered_y, True) covered_x += xstep covered_x = 0 covered_y += ystep mem_dc.SelectObject(wx.NullBitmap) dc.DrawBitmap(bmp, 0, 0) def OnMouseLeftUp(self, event: wx.MouseEvent) -> None: # noqa: N802 """Handle the ``wx.EVT_LEFT_UP`` event for :class:`LabelContainer`. Args: event (wx.MouseEvent): a :class:`MouseEvent` event to be processed. """ if self.HasAGWFlag(ImageBookStyle.INB_NO_RESIZE): ImageContainerBase.OnMouseLeftUp(self, event) return if self.HasCapture(): self.ReleaseMouse() if not self._sashRect.IsEmpty(): self._handle_sash_release(event) return self._sashRect = wx.Rect() if self._oldCursor.IsOk(): self.SetCursor(self._oldCursor) self._oldCursor = wx.NullCursor ImageContainerBase.OnMouseLeftUp(self, event) def _handle_sash_release(self, event: wx.MouseEvent) -> None: """Handle the release of the sash. Args: event (wx.MouseEvent): a :class:`MouseEvent` event to be processed. """ ArtManager.Get().DrawDragSash(self._sashRect) size_event = wx.SizeEvent(self.GetSize()) self.Resize(size_event) self._sashRect = wx.Rect() def Resize(self, event: wx.SizeEvent) -> None: # noqa: N802 """Actually resize the tab area. Args: event (wx.SizeEvent): an instance of :class:`wx.SizeEvent`. """ self._tabAreaSize = self.GetSize() new_width: int = self._tabAreaSize.width event_size: wx.Size = event.GetSize() if self.HasAGWFlag(ImageBookStyle.INB_BOTTOM) or self.HasAGWFlag( ImageBookStyle.INB_RIGHT ): new_width -= event_size.width else: new_width = event_size.width new_width = max(new_width, 100) # Don't allow width to be lower than that self.SetSizeHints(new_width, self._tabAreaSize.height) self.GetParent().Freeze() self.GetParent().GetSizer().Layout() self.GetParent().Thaw() def OnMouseMove(self, event: wx.MouseEvent) -> None: # noqa: N802 """Handle the ``wx.EVT_MOTION`` event for :class:`LabelContainer`. Args: event (wx.MouseEvent): a :class:`MouseEvent` event to be processed. """ if self.HasAGWFlag(ImageBookStyle.INB_NO_RESIZE): ImageContainerBase.OnMouseMove(self, event) return if not self._sashRect.IsEmpty(): self._handle_sash_drag(event) elif event.LeftIsDown(): self._handle_sash_start(event) else: self._handle_cursor_change(event) def _handle_sash_drag(self, event: wx.MouseEvent) -> None: """Handle the dragging of the sash. Args: event (wx.MouseEvent): a :class:`MouseEvent` event to be processed. """ ArtManager.Get().DrawDragSash(self._sashRect) client_rect: wx.Rect = self.GetClientRect() pt: wx.Point = self.ClientToScreen(wx.Point(event.GetX(), 0)) self._sashRect = wx.Rect(pt, wx.Size(4, client_rect.height)) ArtManager.Get().DrawDragSash(self._sashRect) def _handle_sash_start(self, event: wx.MouseEvent) -> None: """Handle the start of sash dragging. Args: event (wx.MouseEvent): a :class:`MouseEvent` event to be processed. """ image_id, where = self.HitTest(event.GetPosition()) if where != IMG_OVER_EW_BORDER or self._bCollapsed: return if not self._sashRect.IsEmpty(): ArtManager.Get().DrawDragSash(self._sashRect) else: self.CaptureMouse() self._oldCursor = self.GetCursor() self.SetCursor(wx.Cursor(wx.CURSOR_SIZEWE)) client_rect: wx.Rect = self.GetClientRect() pt: wx.Point = self.ClientToScreen(wx.Point(event.GetX(), 0)) self._sashRect = wx.Rect(pt, wx.Size(4, client_rect.height)) ArtManager.Get().DrawDragSash(self._sashRect) def _handle_cursor_change(self, event: wx.MouseEvent) -> None: """Handle the change of cursor when not dragging the sash. Args: event (wx.MouseEvent): a :class:`MouseEvent` event to be processed. """ if self.PointOnSash(event.GetPosition()): self._oldCursor = self.GetCursor() self.SetCursor(wx.Cursor(wx.CURSOR_SIZEWE)) elif self._oldCursor.IsOk(): self.SetCursor(self._oldCursor) self._oldCursor = wx.NullCursor self._sashRect = wx.Rect() ImageContainerBase.OnMouseMove(self, event) def OnMouseLeftDown(self, event: wx.MouseEvent) -> None: # noqa: N802 """Handle the ``wx.EVT_LEFT_DOWN`` event for :class:`LabelContainer`. Args: event (wx.MouseEvent): a :class:`MouseEvent` event to be processed. """ if self.HasAGWFlag(ImageBookStyle.INB_NO_RESIZE): ImageContainerBase.OnMouseLeftDown(self, event) return _, where = self.HitTest(event.GetPosition()) if where == IMG_OVER_EW_BORDER and not self._bCollapsed: if not self._sashRect.IsEmpty(): ArtManager.Get().DrawDragSash(self._sashRect) else: self.CaptureMouse() self._oldCursor = self.GetCursor() self.SetCursor(wx.Cursor(wx.CURSOR_SIZEWE)) client_rect: wx.Rect = self.GetClientRect() pt: wx.Point = self.ClientToScreen(wx.Point(event.GetX(), 0)) self._sashRect = wx.Rect(pt, wx.Size(4, client_rect.height)) ArtManager.Get().DrawDragSash(self._sashRect) else: ImageContainerBase.OnMouseLeftDown(self, event) def OnMouseLeaveWindow(self, event: wx.MouseEvent) -> None: # noqa: N802 """Handle the ``wx.EVT_LEAVE_WINDOW`` event for :class:`LabelContainer`. Args: event (wx.MouseEvent): a :class:`MouseEvent` event to be processed. """ if self.HasAGWFlag(ImageBookStyle.INB_NO_RESIZE): ImageContainerBase.OnMouseLeaveWindow(self, event) return if not self.HasCapture(): ImageContainerBase.OnMouseLeaveWindow(self, event) def DrawRegularHover(self, dc: wx.DC, rect: wx.Rect, image_info: ImageInfo) -> None: # noqa: N802 """Draw a rounded rectangle around the current tab. Args: dc (wx.DC): an instance of :class:`wx.DC`; rect (wx.Rect): the current tab client rectangle. image_info (ImageInfo): an instance of :class:`ImageInfo`. """ dc.SetBrush(wx.TRANSPARENT_BRUSH) dc.SetPen(wx.Pen(wx.WHITE)) if self.HasAGWFlag(ImageBookStyle.INB_RIGHT) or self.HasAGWFlag( ImageBookStyle.INB_TOP ): dc.DrawLine(rect.x + 1, rect.y, rect.x + rect.width, rect.y) dc.DrawLine( rect.x + rect.width, rect.y, rect.x + rect.width, rect.y + rect.height ) dc.SetPen(wx.Pen(self._coloursMap[INB_TABS_BORDER_COLOUR])) dc.DrawLine( rect.x + rect.width, rect.y + rect.height, rect.x, rect.y + rect.height ) else: dc.DrawLine(rect.x, rect.y, rect.x + rect.width - 1, rect.y) dc.DrawLine(rect.x, rect.y, rect.x, rect.y + rect.height) dc.SetPen(wx.Pen(self._coloursMap[INB_TABS_BORDER_COLOUR])) dc.DrawLine( rect.x, rect.y + rect.height, rect.x + rect.width, rect.y + rect.height ) def DrawWebHover( # noqa: N802 self, dc: wx.DC, caption: None | str, x_coord: int, y_coord: int, selected: bool, image_info: ImageInfo, ) -> None: """Draw a web style hover effect (set cursor to hand & underline text). Args: dc (wx.DC): an instance of :class:`wx.DC`; caption (str): the tab caption text; x_coord (int): the x position of the tab caption; y_coord (int): the y position of the tab caption; selected (bool): ``True`` if the tab is selected, ``False`` otherwise. image_info (ImageInfo): an instance of :class:`ImageInfo`. """ under_lined_font: wx.Font = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT) under_lined_font.SetPointSize( int( under_lined_font.GetPointSize() * self.GetParent().GetFontSizeMultiple() ) ) if self.GetParent().GetFontBold() or ( self.HasAGWFlag(ImageBookStyle.INB_BOLD_TAB_SELECTION) and selected ): under_lined_font.SetWeight(wx.FONTWEIGHT_BOLD) under_lined_font.SetUnderlined(True) dc.SetFont(under_lined_font) dc.DrawText(caption, x_coord, y_coord) def SetColour(self, which: int, colour: wx.Colour) -> None: # noqa: N802 """Set a colour for a parameter. Args: which (int): can be one of the following parameters: ================================== ======= ============================= Colour Key Value Description ================================== ======= ============================= ``INB_TAB_AREA_BACKGROUND_COLOUR`` 100 The tab area background colour ``INB_ACTIVE_TAB_COLOUR`` 101 The active tab background colour ``INB_TABS_BORDER_COLOUR`` 102 The tabs border colour ``INB_TEXT_COLOUR`` 103 The tab caption text colour ``INB_ACTIVE_TEXT_COLOUR`` 104 The active tab caption text colour ``INB_HILITE_TAB_COLOUR`` 105 The tab caption highlight text colour ================================== ======= ============================= colour (wx.Colour): a valid :class:`wx.Colour` object. """ self._coloursMap[which] = colour def GetColour(self, which: int) -> wx.Colour: # noqa: N802 """Return a colour for a parameter. Args: which (int): the colour key. See: :meth:`~LabelContainer.SetColour` for a list of valid colour keys. """ return self._coloursMap.get(which, wx.Colour()) def InitializeColours(self) -> None: # noqa: N802 """Initialize the colours map to be used for this control.""" self._coloursMap.update( { INB_TAB_AREA_BACKGROUND_COLOUR: ArtManager.Get().LightColour( ArtManager.Get().FrameColour(), 50 ) } ) self._coloursMap.update( {INB_ACTIVE_TAB_COLOUR: ArtManager.Get().GetMenuFaceColour()} ) self._coloursMap.update( { INB_TABS_BORDER_COLOUR: wx.SystemSettings.GetColour( wx.SYS_COLOUR_3DSHADOW ) } ) self._coloursMap.update({INB_HILITE_TAB_COLOUR: wx.Colour("LIGHT BLUE")}) self._coloursMap.update({INB_TEXT_COLOUR: wx.WHITE}) self._coloursMap.update({INB_ACTIVE_TEXT_COLOUR: wx.BLACK}) if not ArtManager.Get().IsDark( self._coloursMap[INB_TAB_AREA_BACKGROUND_COLOUR] ) and not ArtManager.Get().IsDark(self._coloursMap[INB_TEXT_COLOUR]): self._coloursMap[INB_TEXT_COLOUR] = ArtManager.Get().DarkColour( self._coloursMap[INB_TEXT_COLOUR], 100 ) def DrawLabel( # noqa: N802 self, dc: wx.DC, rect: wx.Rect, text: str, bmp: wx.Bitmap, image_info: ImageInfo, orientation_left: bool, image_id: int, selected: bool, hover: bool, ) -> None: """Draw a label using the specified dc. Args: dc (wx.DC): an instance of :class:`wx.DC`; rect (wx.Rect): the text client rectangle; text (str): the actual text string; bmp (wx.Bitmap): a bitmap to be drawn next to the text; image_info (ImageInfo): an instance of :class:`ImageInfo`; orientation_left (bool): ``True`` if the book has the ``INB_RIGHT`` or ``INB_LEFT`` style set; image_id (int): the tab image index; selected (bool): ``True`` if the tab is selected, ``False`` otherwise; hover (bool): ``True`` if the tab is being hovered with the mouse, ``False`` otherwise. """ dcsaver = DCSaver(dc) n_padding = 6 if orientation_left: rect.x += n_padding rect.width -= n_padding else: rect.width -= n_padding text_rect: wx.Rect = self._calculate_text_rect(dc, rect, text, bmp) image_rect: wx.Rect = self._calculate_image_rect(dc, rect, bmp) self._draw_tab_background(dc, rect, selected, orientation_left) self._draw_tab_text(dc, text_rect, text, selected, image_info) self._draw_tab_image(dc, image_rect, bmp) self._draw_tab_shadow(dc, rect, selected, image_id, hover) self._draw_tab_hover_effect( dc, rect, text_rect, text, selected, hover, image_info ) image_info.SetPosition(rect.GetPosition()) image_info.SetSize(rect.GetSize()) del dcsaver def _calculate_text_rect( self, dc: wx.DC, rect: wx.Rect, text: str, bmp: wx.Bitmap ) -> wx.Rect: """Calculate the text bounding rectangle. Args: dc (wx.DC): an instance of :class:`wx.DC`. rect (wx.Rect): the tab client rectangle. text (str): the tab caption text. bmp (wx.Bitmap): the tab bitmap. Returns: wx.Rect: the text bounding rectangle. """ n_padding = 6 font: wx.Font = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT) font.SetPointSize( int(font.GetPointSize() * self.GetParent().GetFontSizeMultiple()) ) if self.GetParent().GetFontBold() or ( self.HasAGWFlag(ImageBookStyle.INB_BOLD_TAB_SELECTION) and self._nIndex == self._nHoveredImgIdx ): font.SetWeight(wx.FONTWEIGHT_BOLD) dc.SetFont(font) w, h = dc.GetTextExtent(text) text_rect = wx.Rect( rect.x + n_padding, rect.y + (rect.height - h) // 2, rect.width - 2 * n_padding, h, ) if bmp.IsOk() and not self.HasAGWFlag(ImageBookStyle.INB_SHOW_ONLY_TEXT): text_rect.x += bmp.GetWidth() + n_padding text_rect.width -= bmp.GetWidth() + n_padding # Truncate text if needed caption = ArtManager.Get().TruncateText(dc, text, text_rect.width) if caption is not None: text_rect.width = dc.GetTextExtent(caption)[0] return text_rect def _calculate_image_rect( self, dc: wx.DC, rect: wx.Rect, bmp: wx.Bitmap ) -> wx.Rect: """Calculate the image bounding rectangle. Args: dc (wx.DC): an instance of :class:`wx.DC`. rect (wx.Rect): the tab client rectangle. bmp (wx.Bitmap): the tab bitmap. Returns: wx.Rect: the image bounding rectangle. """ if bmp.IsOk() and not self.HasAGWFlag(ImageBookStyle.INB_SHOW_ONLY_TEXT): return wx.Rect( rect.x + self.num_padding, rect.y + (rect.height - bmp.GetHeight()) // 2, bmp.GetWidth(), bmp.GetHeight(), ) return wx.Rect() def _draw_tab_background( self, dc: wx.DC, rect: wx.Rect, selected: bool, orientation_left: bool ) -> None: """Draw the tab background. Args: dc (wx.DC): an instance of :class:`wx.DC`. rect (wx.Rect): the tab client rectangle. selected (bool): ``True`` if the tab is selected, ``False`` otherwise. orientation_left (bool): ``True`` if the book has the ``INB_RIGHT`` or ``INB_LEFT`` style set. """ if not selected: return dc.SetBrush(wx.Brush(self._coloursMap[INB_ACTIVE_TAB_COLOUR])) dc.SetPen( wx.Pen(self._coloursMap[INB_TABS_BORDER_COLOUR]) if self.HasAGWFlag(ImageBookStyle.INB_BORDER) else wx.Pen(self._coloursMap[INB_ACTIVE_TAB_COLOUR]) ) label_rect = wx.Rect(*rect) if orientation_left: label_rect.width += 3 else: label_rect.width += 3 label_rect.x -= 3 dc.DrawRoundedRectangle(label_rect, 3) if not orientation_left and self.HasAGWFlag(ImageBookStyle.INB_DRAW_SHADOW): dc.SetPen(wx.BLACK_PEN) dc.DrawPoint( label_rect.x + label_rect.width - 1, label_rect.y + label_rect.height - 1, ) def _draw_tab_text( self, dc: wx.DC, text_rect: wx.Rect, text: str, selected: bool, image_info: ImageInfo, ) -> None: """Draw the tab text. Args: dc (wx.DC): an instance of :class:`wx.DC`. text_rect (wx.Rect): the text bounding rectangle. text (str): the tab caption text. selected (bool): ``True`` if the tab is selected, ``False`` otherwise. image_info (ImageInfo): an instance of :class:`ImageInfo`. """ if not text: return dc.SetTextForeground( self._coloursMap[INB_ACTIVE_TEXT_COLOUR] if selected else self._coloursMap[INB_TEXT_COLOUR] ) dc.DrawText(text, text_rect.x, text_rect.y) image_info.SetTextRect(text_rect) def _draw_tab_image(self, dc: wx.DC, image_rect: wx.Rect, bmp: wx.Bitmap) -> None: """Draw the tab image. Args: dc (wx.DC): an instance of :class:`wx.DC`. image_rect (wx.Rect): the image bounding rectangle. bmp (wx.Bitmap): the tab bitmap. """ if bmp.IsOk() and not self.HasAGWFlag(ImageBookStyle.INB_SHOW_ONLY_TEXT): dc.DrawBitmap(bmp, image_rect.x, image_rect.y, True) def _draw_tab_shadow( self, dc: wx.DC, rect: wx.Rect, selected: bool, image_id: int, hover: bool ) -> None: """Draw the tab shadow. Args: dc (wx.DC): an instance of :class:`wx.DC`. rect (wx.Rect): the tab client rectangle. selected (bool): ``True`` if the tab is selected, ``False`` otherwise. image_id (int): the tab image index. hover (bool): ``True`` if the tab is being hovered with the mouse, ``False`` otherwise. """ if not self.HasAGWFlag(ImageBookStyle.INB_DRAW_SHADOW) or not selected: return sstyle: int = ( BOTTOM_SHADOW if self.HasAGWFlag(ImageBookStyle.INB_LEFT) else BOTTOM_SHADOW_FULL | RIGHT_SHADOW ) if ( self.HasAGWFlag(ImageBookStyle.INB_WEB_HILITE) or image_id + 1 != self._nHoveredImgIdx ): ArtManager.Get().DrawBitmapShadow(dc, rect, sstyle) def _draw_tab_hover_effect( self, dc: wx.DC, rect: wx.Rect, text_rect: wx.Rect, text: str, selected: bool, hover: bool, image_info: ImageInfo, ) -> None: """Draw the tab hover effect. Args: dc (wx.DC): an instance of :class:`wx.DC`. rect (wx.Rect): the tab client rectangle. text_rect (wx.Rect): the text bounding rectangle. text (str): the tab caption text. selected (bool): ``True`` if the tab is selected, ``False`` otherwise. hover (bool): ``True`` if the tab is being hovered with the mouse, ``False`` otherwise. image_info (ImageInfo): an instance of :class:`ImageInfo`. """ if not hover: return if self.HasAGWFlag(ImageBookStyle.INB_WEB_HILITE) and text: self.DrawWebHover(dc, text, text_rect.x, text_rect.y, selected, image_info) else: self.DrawRegularHover(dc, rect, image_info) # ------------------------------------------------------------------------------------ # # Class FlatBookBase # ------------------------------------------------------------------------------------ # class FlatBookBase(wx.Panel): """Base class for :class:`LabelBook` and :class:`FlatImageBook` containers.""" def __init__( self, parent: wx.Window, id: int = wx.ID_ANY, # noqa: A002 pos: wx.Point = wx.DefaultPosition, size: wx.Size = wx.DefaultSize, style: int = 0, agw_style: int = 0, name: str = "FlatBookBase", ) -> None: """Construct the default class. Args: parent (wx.Window): parent window. Must not be ``None``; id (int): window identifier. A value of -1 indicates a default value; pos (wx.Point): the control position. A value of (-1, -1) indicates a default position, chosen by either the windowing system or wxPython, depending on platform; size (wx.Size): the control size. A value of (-1, -1) indicates a default size, chosen by either the windowing system or wxPython, depending on platform; style (int): the underlying :class:`Panel` window style; agw_style (int): the AGW-specific window style. This can be a combination of the following bits: =========================== =========== ================================ Window Styles Hex Value Description =========================== =========== ================================ ``INB_BOTTOM`` 0x1 Place labels below the page area. Available only for :class:`FlatImageBook`. ``INB_LEFT`` 0x2 Place labels on the left side. Available only for :class:`FlatImageBook`. ``INB_RIGHT`` 0x4 Place labels on the right side. ``INB_TOP`` 0x8 Place labels above the page area. ``INB_BORDER`` 0x10 Draws a border around :class:`LabelBook` or :class:`FlatImageBook`. ``INB_SHOW_ONLY_TEXT`` 0x20 Shows only text labels and no images. Available only for :class:`LabelBook`. ``INB_SHOW_ONLY_IMAGES`` 0x40 Shows only tab images and no label texts. Available only for :class:`LabelBook`. ``INB_FIT_BUTTON`` 0x80 Displays a pin button to show/hide the book control. ``INB_DRAW_SHADOW`` 0x100 Draw shadows below the book tabs. Available only for :class:`LabelBook`. ``INB_USE_PIN_BUTTON`` 0x200 Displays a pin button to show/hide the book control. ``INB_GRADIENT_BACKGROUND`` 0x400 Draws a gradient shading on the tabs background. Available only for :class:`LabelBook`. ``INB_WEB_HILITE`` 0x800 On mouse hovering, tabs behave like html hyperlinks. Available only for :class:`LabelBook`. ``INB_NO_RESIZE`` 0x1000 Don't allow resizing of the tab area. ``INB_FIT_LABELTEXT`` 0x2000 Will fit the tab area to the longest text (or text+image if you have images) in all the tabs. ``INB_BOLD_TAB_SELECTION`` 0x4000 Show the selected tab text using a bold font. =========================== =========== ================================ name (str): the window name. """ self._pages = None self._bInitializing = True self._bForceSelection = False self._windows: list[wx.Window] = [] self._fontSizeMultiple = 1.0 self._fontBold = False style |= wx.TAB_TRAVERSAL self._agw_style: int = agw_style wx.Panel.__init__(self, parent, id, pos, size, style, name) self._bInitializing = False self.Bind(wx.EVT_NAVIGATION_KEY, self.OnNavigationKey) self.Bind(wx.EVT_MOUSE_CAPTURE_LOST, lambda evt: True) def CreateImageContainer(self) -> ImageContainerBase: # noqa: N802 """Create the image container class for :class:`FlatBookBase`.""" return ImageContainer(self, wx.ID_ANY, agw_style=self.GetAGWWindowStyleFlag()) def SetAGWWindowStyleFlag(self, agw_style: int) -> None: # noqa: N802 """Set the window style. Args: agw_style (int): can be a combination of the following bits: =========================== =========== ================================ Window Styles Hex Value Description =========================== =========== ================================ ``INB_BOTTOM`` 0x1 Place labels below the page area. Available only for :class:`FlatImageBook`. ``INB_LEFT`` 0x2 Place labels on the left side. Available only for :class:`FlatImageBook`. ``INB_RIGHT`` 0x4 Place labels on the right side. ``INB_TOP`` 0x8 Place labels above the page area. ``INB_BORDER`` 0x10 Draws a border around :class:`LabelBook` or :class:`FlatImageBook`. ``INB_SHOW_ONLY_TEXT`` 0x20 Shows only text labels and no images. Available only for :class:`LabelBook`. ``INB_SHOW_ONLY_IMAGES`` 0x40 Shows only tab images and no label texts. Available only for :class:`LabelBook`. ``INB_FIT_BUTTON`` 0x80 Displays a pin button to show/hide the book control. ``INB_DRAW_SHADOW`` 0x100 Draw shadows below the book tabs. Available only for :class:`LabelBook`. ``INB_USE_PIN_BUTTON`` 0x200 Displays a pin button to show/hide the book control. ``INB_GRADIENT_BACKGROUND`` 0x400 Draws a gradient shading on the tabs background. Available only for :class:`LabelBook`. ``INB_WEB_HILITE`` 0x800 On mouse hovering, tabs behave like html hyperlinks. Available only for :class:`LabelBook`. ``INB_NO_RESIZE`` 0x1000 Don't allow resizing of the tab area. ``INB_FIT_LABELTEXT`` 0x2000 Will fit the tab area to the longest text (or text+image if you have images) in all the tabs. ``INB_BOLD_TAB_SELECTION`` 0x4000 Show the selected tab text using a bold font. =========================== =========== ================================ """ self._agw_style = agw_style # Check that we are not in initialization process if self._bInitializing: return if not self._pages: return # Detach the windows attached to the sizer if self.GetSelection() >= 0: self._mainSizer.Detach(self._windows[self.GetSelection()]) self._mainSizer.Detach(self._pages) if ( isinstance(self, LabelBook) or agw_style & ImageBookStyle.INB_LEFT or agw_style & ImageBookStyle.INB_RIGHT ): self._mainSizer = wx.BoxSizer(wx.HORIZONTAL) else: self._mainSizer = wx.BoxSizer(wx.VERTICAL) self.SetSizer(self._mainSizer) # Add the tab container and the separator self._mainSizer.Add(self._pages, 0, wx.EXPAND) if isinstance(self, FlatImageBook): if ( agw_style & ImageBookStyle.INB_LEFT or agw_style & ImageBookStyle.INB_RIGHT ): self._pages.SetSizeHints(self._pages._nImgSize * 2, -1) else: self._pages.SetSizeHints(-1, self._pages._nImgSize * 2) # Attach the windows back to the sizer to the sizer if self.GetSelection() >= 0: self.DoSetSelection(self._windows[self.GetSelection()]) if agw_style & ImageBookStyle.INB_FIT_LABELTEXT: self.ResizeTabArea() self._mainSizer.Layout() dummy = wx.SizeEvent(self.GetSize()) wx.PostEvent(self, dummy) self._pages.Refresh() def GetAGWWindowStyleFlag(self) -> int: # noqa: N802 """Return the :class:`FlatBookBase` window style. See: :meth:`~FlatBookBase.SetAGWWindowStyleFlag` for a list of possible window style flags. """ return self._agw_style def HasAGWFlag(self, flag: int) -> bool: # noqa: N802 """Return whether a flag is present in the :class:`FlatBookBase` style. Args: flag (int): one of the possible :class:`FlatBookBase` window styles. See: :meth:`~FlatBookBase.SetAGWWindowStyleFlag` for a list of possible window style flags. """ agw_style: int = self.GetAGWWindowStyleFlag() res: bool = ((agw_style & flag and [True]) or [False])[0] return res def AddPage( # noqa: N802 self, page: wx.Window, text: str, select: bool = False, image_id: int = -1 ) -> None: """Add a page to the book. Args: page (wx.Window): specifies the new page; text (str): specifies the text for the new page; select (bool): specifies whether the page should be selected; image_id (int): specifies the optional image index for the new page. Note: The call to this function generates the page changing events. """ if not page: return if self._pages is None: raise ValueError("ImageContainer is not initialized") page.Reparent(self) self._windows.append(page) if select or len(self._windows) == 1: self.SetSelection(len(self._windows) - 1) else: page.Hide() self._pages.AddPage(text, select, image_id) self.ResizeTabArea() self.Refresh() def InsertPage( # noqa: N802 self, page_id: int, page: wx.Window, text: str, select: bool = False, image_id: int = -1, ) -> None: """Insert a page into the book at the specified position. Args: page_id (int): specifies the position for the new page; page (wx.Window): specifies the new page; text (str): specifies the text for the new page; select (bool): specifies whether the page should be selected; image_id (int): specifies the optional image index for the new page. Note: The call to this function generates the page changing events. """ if not page: return if self._pages is None: raise ValueError("LabelContainer is not initialized") page.Reparent(self) self._windows.insert(page_id, page) if select or len(self._windows) == 1: self.SetSelection(page_id) else: page.Hide() self._pages.InsertPage(page_id, text, select, image_id) self.ResizeTabArea() self.Refresh() def DeletePage(self, page: int) -> None | bool: # noqa: N802 """Delete the specified page, and the associated window. Args: page (int): an integer specifying the page to be deleted. Note: The call to this function generates the page changing events. """ if page >= len(self._windows) or page < 0: return None # Fire a closing event event = ImageNotebookEvent(wxEVT_IMAGENOTEBOOK_PAGE_CLOSING, self.GetId()) event.SetSelection(page) event.SetEventObject(self) self.GetEventHandler().ProcessEvent(event) # The event handler allows it? if not event.IsAllowed(): return False self.Freeze() # Delete the requested page page_removed: wx.Window = self._windows[page] # If the page is the current window, remove it from the sizer as well if page == self.GetSelection(): self._mainSizer.Detach(page_removed) # Remove it from the array as well self._windows.pop(page) # Now we can destroy it in wxWidgets use Destroy instead of delete page_removed.Destroy() self._mainSizer.Layout() if self._pages: self._pages.DoDeletePage(page) self.ResizeTabArea() self.Thaw() # Fire a closed event closed_event = ImageNotebookEvent(wxEVT_IMAGENOTEBOOK_PAGE_CLOSED, self.GetId()) closed_event.SetSelection(page) closed_event.SetEventObject(self) self.GetEventHandler().ProcessEvent(closed_event) return True def RemovePage(self, page: int) -> bool: # noqa: N802 """Delete the specified page, without deleting the associated window. Args: page (int): an integer specifying the page to be removed. Note: The call to this function generates the page changing events. """ if page >= len(self._windows): return False # Fire a closing event event = ImageNotebookEvent(wxEVT_IMAGENOTEBOOK_PAGE_CLOSING, self.GetId()) event.SetSelection(page) event.SetEventObject(self) self.GetEventHandler().ProcessEvent(event) # The event handler allows it? if not event.IsAllowed(): return False self.Freeze() # Remove the requested page page_removed: wx.Window = self._windows[page] # If the page is the current window, remove it from the sizer as well if page == self.GetSelection(): self._mainSizer.Detach(page_removed) # Remove it from the array as well self._windows.pop(page) self._mainSizer.Layout() self.ResizeTabArea() self.Thaw() if self._pages is not None: self._pages.DoDeletePage(page) # Fire a closed event closed_event = ImageNotebookEvent(wxEVT_IMAGENOTEBOOK_PAGE_CLOSED, self.GetId()) closed_event.SetSelection(page) closed_event.SetEventObject(self) self.GetEventHandler().ProcessEvent(closed_event) return True def ResizeTabArea(self) -> None: # noqa: N802 """Resize the tab area if ``INB_FIT_LABELTEXT`` style is set.""" if self._pages is None: return agw_style: int = self.GetAGWWindowStyleFlag() if agw_style & ImageBookStyle.INB_FIT_LABELTEXT == 0: return if isinstance(self._pages, LabelContainer): self._pages.SetTabAreaWidth(self._calculate_max_tab_width()) self._pages.Refresh() def _calculate_max_tab_width(self) -> int: """Calculate the maximum width needed for the tab area.""" if self._pages is None: return 100 dc = wx.MemoryDC() dc.SelectObject(wx.Bitmap(1, 1)) font: wx.Font = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT) font.SetPointSize(int(font.GetPointSize() * self._fontSizeMultiple)) if self.GetFontBold() or self.HasAGWFlag(ImageBookStyle.INB_BOLD_TAB_SELECTION): font.SetWeight(wx.FONTWEIGHT_BOLD) dc.SetFont(font) max_width = 0 for page in range(self.GetPageCount()): caption: str = self._pages.GetPageText(page) w, h = dc.GetTextExtent(caption) max_width: int = max(max_width, w) max_width += 24 # TODO this is 6*4 6 is nPadding from drawlabel if not self.HasAGWFlag(ImageBookStyle.INB_SHOW_ONLY_TEXT): max_width += self._pages._nImgSize * 2 return max(max_width, 100) def DeleteAllPages(self) -> None: # noqa: N802 """Delete all the pages in the book.""" if not self._windows: return self.Freeze() for win in self._windows: win.Destroy() self._windows = [] self.Thaw() if self._pages: self._pages.ClearAll() self._pages.Refresh() def SetSelection(self, page: int) -> None: # noqa: N802 """Change selection to the page given by page. Args: page (int): an integer specifying the page to be selected. Note: The call to this function generates the page changing events. """ if self._pages is None: raise ValueError("Pages container is not initialized") if page >= len(self._windows): return if not self.GetEnabled(page): return if page == self.GetSelection() and not self._bForceSelection: return old_selection: int = self.GetSelection() # Generate an event that indicates that an image is about to be selected event = ImageNotebookEvent(wxEVT_IMAGENOTEBOOK_PAGE_CHANGING, self.GetId()) event.SetSelection(page) event.SetOldSelection(old_selection) event.SetEventObject(self) self.GetEventHandler().ProcessEvent(event) # The event handler allows it? if not event.IsAllowed() and not self._bForceSelection: return self.DoSetSelection(self._windows[page]) # Now we can update the new selection self._pages._nIndex = page # Refresh calls the OnPaint of this class self._pages.Refresh() # Generate an event that indicates that an image was selected event_changed = ImageNotebookEvent( wxEVT_IMAGENOTEBOOK_PAGE_CHANGED, self.GetId() ) event_changed.SetEventObject(self) event_changed.SetOldSelection(old_selection) event_changed.SetSelection(page) self.GetEventHandler().ProcessEvent(event_changed) def AssignImageList(self, imglist: wx.ImageList) -> None: # noqa: N802 """Assign an image list to the control. Args: imglist (wx.ImageList): an instance of :class:`wx.ImageList`. """ if self._pages is None: raise ValueError("ImageContainer is not initialized") self._pages.AssignImageList(imglist) # Force change self.SetAGWWindowStyleFlag(self.GetAGWWindowStyleFlag()) def GetSelection(self) -> int: # noqa: N802 """Return the current selection. Returns: int: The current selection. """ if self._pages: return self._pages._nIndex return -1 def DoSetSelection(self, window: wx.Window) -> None: # noqa: N802 """Select the window by the provided pointer. Args: window (wx.Window): an instance of :class:`wx.Window`. """ cur_sel: int = self.GetSelection() agw_style: int = self.GetAGWWindowStyleFlag() # Replace the window in the sizer self.Freeze() # Check if a new selection was made b_insert_first: int = ( agw_style & ImageBookStyle.INB_BOTTOM or agw_style & ImageBookStyle.INB_RIGHT ) if cur_sel >= 0: # Remove the window from the main sizer self._mainSizer.Detach(self._windows[cur_sel]) self._windows[cur_sel].Hide() if b_insert_first: self._mainSizer.Insert(0, window, 1, wx.EXPAND) else: self._mainSizer.Add(window, 1, wx.EXPAND) window.Show() self._mainSizer.Layout() self.Thaw() def GetImageList(self) -> None | wx.ImageList: # noqa: N802 """Return the associated image list.""" if self._pages is None: return None return self._pages.GetImageList() def GetPageCount(self) -> int: # noqa: N802 """Return the number of pages in the book. Returns: int: The number of pages in the book. """ return len(self._windows) def GetFontBold(self) -> bool: # noqa: N802 """Get the font bold status. Returns: bool: ``True`` if the page captions are bold, ``False`` otherwise. """ return self._fontBold def SetFontBold(self, bold: bool) -> None: # noqa: N802 """Set whether the page captions are bold or not. Args: bold (bool): ``True`` or ``False``. """ self._fontBold: bool = bold def GetFontSizeMultiple(self) -> float: # noqa: N802 """Get the font size multiple for the page captions.""" return self._fontSizeMultiple def SetFontSizeMultiple(self, multiple: float) -> None: # noqa: N802 """Set the font size multiple for the page captions. Args: multiple (float): The multiple to be applied to the system font to get the our font size. """ self._fontSizeMultiple: float = multiple def SetPageImage(self, page: int, image_id: int) -> None: # noqa: N802 """Set the image index for the given page. Args: page (int): an integer specifying the page index; image_id (int): an index into the image list. """ if self._pages is None: raise ValueError("ImageContainer is not initialized") self._pages.SetPageImage(page, image_id) self._pages.Refresh() def SetPageText(self, page: int, text: str) -> None: # noqa: N802 """Set the text for the given page. Args: page (int): an integer specifying the page index; text (str): the new tab label. """ if self._pages is None: raise ValueError("Pages container is not initialized") self._pages.SetPageText(page, text) self._pages.Refresh() def GetPageText(self, page: int) -> str: # noqa: N802 """Return the text for the given page. Args: page (int): an integer specifying the page index. """ if self._pages is None: raise ValueError("Pages container is not initialized") return self._pages.GetPageText(page) def GetPageImage(self, page: int) -> int: # noqa: N802 """Return the image index for the given page. Args: page (int): an integer specifying the page index. """ if self._pages is None: raise ValueError("ImageContainer is not initialized") return self._pages.GetPageImage(page) def GetEnabled(self, page: int) -> bool: # noqa: N802 """Return whether a tab is enabled or not. Args: page (int): an integer specifying the page index. """ if self._pages is None: raise ValueError("Pages container is not initialized") return self._pages.GetEnabled(page) def EnableTab(self, page: int, enabled: bool = True) -> None: # noqa: N802 """Enable or disable a tab. Args: page (int): an integer specifying the page index; enabled (bool): ``True`` to enable a tab, ``False`` to disable it. """ if page >= len(self._windows): return if self._pages is None: raise ValueError("Pages container is not initialized") self._windows[page].Enable(enabled) self._pages.EnableTab(page, enabled) def GetPage(self, page: int) -> None | wx.Window: # noqa: N802 """Return the window at the given page position. Args: page (int): an integer specifying the page to be returned. """ if page >= len(self._windows): return None return self._windows[page] def GetCurrentPage(self) -> None | wx.Window: # noqa: N802 """Return the currently selected notebook page or ``None``.""" selection: int = self.GetSelection() if selection < 0: return None return self.GetPage(selection) def OnNavigationKey(self, event: wx.NavigationKeyEvent) -> None: # noqa: N802 """Handle the ``wx.EVT_NAVIGATION_KEY`` event for :class:`FlatBookBase`. Args: event (wx.NavigationKeyEvent): a :class:`NavigationKeyEvent` event to be processed. """ if event.IsWindowChange(): if self.GetPageCount() == 0: return # change pages self.AdvanceSelection(event.GetDirection()) else: event.Skip() def AdvanceSelection(self, forward: bool = True) -> None: # noqa: N802 """Cycle through the tabs. Args: forward (bool): if ``True``, the selection is advanced in ascending order (to the right), otherwise the selection is advanced in descending order. Note: The call to this function generates the page changing events. """ n_sel: int = self.GetSelection() if n_sel < 0: return n_max: int = self.GetPageCount() - 1 if forward: new_selection: int = ((n_sel == n_max and [0]) or [n_sel + 1])[0] else: new_selection = ((n_sel == 0 and [n_max]) or [n_sel - 1])[0] self.SetSelection(new_selection) def ChangeSelection(self, page: int) -> None | int: # noqa: N802 """Change the selection for the given page, returning the previous selection. Args: page (int): an integer specifying the page to be selected. Note: The call to this function does not generate the page changing events. """ if page < 0 or page >= self.GetPageCount(): return None old_page: int = self.GetSelection() if page >= 0 and page < len(self._windows): self.DoSetSelection(self._windows[page]) # Pass the window object return old_page @property def PageText(self) -> str: # noqa: N802 """Return the text for the current page. Returns: str: The text for the current page. """ page: int = self.GetSelection() if page >= 0: return self.GetPageText(page) return "" @PageText.setter def PageText(self, text: str) -> None: # noqa: N802 """Set the text for the current page. Args: text (str): The new tab label. """ page: int = self.GetSelection() if page >= 0: self.SetPageText(page, text) @property def PageImage(self) -> int: # noqa: N802 """Return the image index for the current page. Returns: int: The image index for the current page. """ return self.GetPageImage(self.GetSelection()) @PageImage.setter def PageImage(self, value: int) -> None: # noqa: N802 self.SetPageImage(self.GetSelection(), value) @property def Page(self) -> None | wx.Window: # noqa: N802 """Return the currently selected notebook page or ``None``.""" return self.GetCurrentPage() @property def CurrentPage(self) -> None | wx.Window: # noqa: N802 """Return the currently selected notebook page or ``None``.""" return self.GetCurrentPage() @property def PageCount(self) -> int: # noqa: N802 """Return the number of pages in the book. Returns: int: The number of pages in the book. """ return self.GetPageCount() @property def Selection(self) -> int: # noqa: N802 """Return the current selection. Returns: int: The current selection. """ return self.GetSelection() @Selection.setter def Selection(self, page: int) -> None: # noqa: N802 """Change selection to the page given by page. Args: page (int): an integer specifying the page to be selected. Note: Setting this property generates the page changing events. """ self.SetSelection(page) # ---------------------------------------------------------------------------- # # Class FlatImageBook # ---------------------------------------------------------------------------- # class FlatImageBook(FlatBookBase): """Default implementation of the image book. It is like a :class:`Notebook`, except that images are used to control the different pages. This container is usually used for configuration dialogs etc. Note: Currently, this control works properly for images of size 32x32 and bigger. """ def __init__( self, parent: wx.Window, id: int = wx.ID_ANY, # noqa: A002 pos: wx.Point = wx.DefaultPosition, size: wx.Size = wx.DefaultSize, style: int = 0, agw_style: int = 0, name: str = "FlatImageBook", ) -> None: """Construct the default class. Args: parent (wx.Window): parent window. Must not be ``None``; id (int): window identifier. A value of -1 indicates a default value; pos (wx.Point): the control position. A value of (-1, -1) indicates a default position, chosen by either the windowing system or wxPython, depending on platform; size (wx.Size): the control size. A value of (-1, -1) indicates a default size, chosen by either the windowing system or wxPython, depending on platform; style (int): the underlying :class:`Panel` window style; agw_style (int): the AGW-specific window style. This can be a combination of the following bits: =========================== =========== ================================ Window Styles Hex Value Description =========================== =========== ================================ ``INB_BOTTOM`` 0x1 Place labels below the page area. Available only for :class:`FlatImageBook`. ``INB_LEFT`` 0x2 Place labels on the left side. Available only for :class:`FlatImageBook`. ``INB_RIGHT`` 0x4 Place labels on the right side. ``INB_TOP`` 0x8 Place labels above the page area. ``INB_BORDER`` 0x10 Draws a border around :class:`LabelBook` or :class:`FlatImageBook`. ``INB_SHOW_ONLY_TEXT`` 0x20 Shows only text labels and no images. Available only for :class:`LabelBook`. ``INB_SHOW_ONLY_IMAGES`` 0x40 Shows only tab images and no label texts. Available only for :class:`LabelBook`. ``INB_FIT_BUTTON`` 0x80 Displays a pin button to show/hide the book control. ``INB_DRAW_SHADOW`` 0x100 Draw shadows below the book tabs. Available only for :class:`LabelBook`. ``INB_USE_PIN_BUTTON`` 0x200 Displays a pin button to show/hide the book control. ``INB_GRADIENT_BACKGROUND`` 0x400 Draws a gradient shading on the tabs background. Available only for :class:`LabelBook`. ``INB_WEB_HILITE`` 0x800 On mouse hovering, tabs behave like html hyperlinks. Available only for :class:`LabelBook`. ``INB_NO_RESIZE`` 0x1000 Don't allow resizing of the tab area. ``INB_FIT_LABELTEXT`` 0x2000 Will fit the tab area to the longest text (or text+image if you have images) in all the tabs. ``INB_BOLD_TAB_SELECTION`` 0x4000 Show the selected tab text using a bold font. =========================== =========== ================================ name (str): the window name. """ FlatBookBase.__init__(self, parent, id, pos, size, style, agw_style, name) self._pages: ImageContainer = self.CreateImageContainer() if self._pages is None: raise ValueError("Failed to create ImageContainer") if agw_style & ImageBookStyle.INB_LEFT or agw_style & ImageBookStyle.INB_RIGHT: self._mainSizer = wx.BoxSizer(wx.HORIZONTAL) else: self._mainSizer = wx.BoxSizer(wx.VERTICAL) self.SetSizer(self._mainSizer) # Add the tab container to the sizer self._mainSizer.Add(self._pages, 0, wx.EXPAND) if agw_style & ImageBookStyle.INB_LEFT or agw_style & ImageBookStyle.INB_RIGHT: self._pages.SetSizeHints(self._pages.GetImageSize() * 2, -1) else: self._pages.SetSizeHints(-1, self._pages.GetImageSize() * 2) self._mainSizer.Layout() def CreateImageContainer(self) -> ImageContainer: # noqa: N802 """Create the image container class for :class:`FlatImageBook`.""" return ImageContainer(self, wx.ID_ANY, agw_style=self.GetAGWWindowStyleFlag()) # ---------------------------------------------------------------------------- # # Class LabelBook # ---------------------------------------------------------------------------- # class LabelBook(FlatBookBase): """An implementation of a notebook control. Except that instead of having tabs to show labels, it labels to the right or left (arranged horizontally). """ def __init__( self, parent: wx.Window, id: int = wx.ID_ANY, # noqa: A002 pos: wx.Point = wx.DefaultPosition, size: wx.Size = wx.DefaultSize, style: int = 0, agw_style: int = 0, name: str = "LabelBook", ) -> None: """Construct the default class. Args: parent (wx.Window): parent window. Must not be ``None``; id (int): window identifier. A value of -1 indicates a default value; pos (wx.Point): the control position. A value of (-1, -1) indicates a default position, chosen by either the windowing system or wxPython, depending on platform; size (wx.Size): the control size. A value of (-1, -1) indicates a default size, chosen by either the windowing system or wxPython, depending on platform; style (int): the underlying :class:`Panel` window style; agw_style (int): the AGW-specific window style. This can be a combination of the following bits: =========================== =========== ================================ Window Styles Hex Value Description =========================== =========== ================================ ``INB_BOTTOM`` 0x1 Place labels below the page area. Available only for :class:`FlatImageBook`. ``INB_LEFT`` 0x2 Place labels on the left side. Available only for :class:`FlatImageBook`. ``INB_RIGHT`` 0x4 Place labels on the right side. ``INB_TOP`` 0x8 Place labels above the page area. ``INB_BORDER`` 0x10 Draws a border around :class:`LabelBook` or :class:`FlatImageBook`. ``INB_SHOW_ONLY_TEXT`` 0x20 Shows only text labels and no images. Available only for :class:`LabelBook`. ``INB_SHOW_ONLY_IMAGES`` 0x40 Shows only tab images and no label texts. Available only for :class:`LabelBook`. ``INB_FIT_BUTTON`` 0x80 Displays a pin button to show/hide the book control. ``INB_DRAW_SHADOW`` 0x100 Draw shadows below the book tabs. Available only for :class:`LabelBook`. ``INB_USE_PIN_BUTTON`` 0x200 Displays a pin button to show/hide the book control. ``INB_GRADIENT_BACKGROUND`` 0x400 Draws a gradient shading on the tabs background. Available only for :class:`LabelBook`. ``INB_WEB_HILITE`` 0x800 On mouse hovering, tabs behave like html hyperlinks. Available only for :class:`LabelBook`. ``INB_NO_RESIZE`` 0x1000 Don't allow resizing of the tab area. ``INB_FIT_LABELTEXT`` 0x2000 Will fit the tab area to the longest text (or text+image if you have images) in all the tabs. ``INB_BOLD_TAB_SELECTION`` 0x4000 Show the selected tab text using a bold font. =========================== =========== ================================ name (str): the window name. """ FlatBookBase.__init__(self, parent, id, pos, size, style, agw_style, name) self._pages: LabelContainer = self.CreateImageContainer() print(f"self._pages initialized: {self._pages}") if self._pages is None: raise ValueError("Failed to create LabelContainer") # Label book specific initialization self._mainSizer = wx.BoxSizer(wx.HORIZONTAL) self.SetSizer(self._mainSizer) # Add the tab container to the sizer self._mainSizer.Add(self._pages, 0, wx.EXPAND) self._pages.SetSizeHints(self._pages.GetTabAreaWidth(), -1) # Initialize the colours maps self._pages.InitializeColours() self.Bind(wx.EVT_SIZE, self.OnSize) def CreateImageContainer(self) -> LabelContainer: # noqa: N802 """Create the image container (LabelContainer) class for :class:`LabelBook`.""" return LabelContainer(self, wx.ID_ANY, agw_style=self.GetAGWWindowStyleFlag()) def SetColour(self, which: int, colour: wx.Colour) -> None: # noqa: N802 """Set the colour for the specified parameter. Args: which (int): the colour key; colour (wx.Colour): a valid :class:`wx.Colour` instance. See: :meth:`LabelContainer.SetColour() ` for a list of valid colour keys. """ self._pages.SetColour(which, colour) def GetColour(self, which: int) -> wx.Colour: # noqa: N802 """Return the colour for the specified parameter. Args: which (int): the colour key. See: :meth:`LabelContainer.SetColour() ` for a list of valid colour keys. """ return self._pages.GetColour(which) def OnSize(self, event: wx.SizeEvent) -> None: # noqa: N802 """Handle the ``wx.EVT_SIZE`` event for :class:`LabelBook`. Args: event (wx.SizeEvent): a :class:`wx.SizeEvent` event to be processed. """ self._pages.Refresh() event.Skip()