"""
L{RibbonAUIArtProvider} is responsible for drawing all the components of the ribbon
interface using an AUI-compatible appearance.
Description
===========
This allows a ribbon bar to have a pluggable look-and-feel, while retaining the same
underlying behaviour. As a single art provider is used for all ribbon components, a
ribbon bar usually has a consistent (though unique) appearance.
By default, a L{RibbonBar} uses an instance of a class called `RibbonDefaultArtProvider`,
which resolves to `RibbonAUIArtProvider`, `RibbonMSWArtProvider`, or `RibbonOSXArtProvider`
- whichever is most appropriate to the current platform. These art providers are all
slightly configurable with regard to colours and fonts, but for larger modifications,
you can derive from one of these classes, or write a completely new art provider class.
Call L{RibbonBar.SetArtProvider} to change the art provider being used.
See Also
========
L{RibbonBar}
"""
import wx
from math import cos
from math import pi as M_PI
from art_msw import RibbonMSWArtProvider
from art_internal import RibbonHSLColour, RibbonShiftLuminance, RibbonInterpolateColour
import bar as BAR, panel as PANEL
from art import *
if wx.Platform == "__WXMAC__":
import Carbon.Appearance
[docs]def FontFromFont(original):
newFont = wx.Font(original.GetPointSize(), original.GetFamily(),
original.GetStyle(), original.GetWeight(), original.GetUnderlined(),
original.GetFaceName(), original.GetEncoding())
return newFont
[docs]class RibbonAUIArtProvider(RibbonMSWArtProvider):
[docs] def __init__(self):
RibbonMSWArtProvider.__init__(self)
if wx.Platform == "__WXMAC__":
if hasattr(wx, 'MacThemeColour'):
base_colour = wx.MacThemeColour(Carbon.Appearance.kThemeBrushToolbarBackground)
else:
brush = wx.Brush(wx.BLACK)
brush.MacSetTheme(Carbon.Appearance.kThemeBrushToolbarBackground)
base_colour = brush.GetColour()
else:
base_colour = wx.SystemSettings.GetColour(wx.SYS_COLOUR_3DFACE)
self.SetColourScheme(base_colour, wx.SystemSettings.GetColour(wx.SYS_COLOUR_HIGHLIGHT),
wx.SystemSettings.GetColour(wx.SYS_COLOUR_HIGHLIGHTTEXT))
self._tab_active_label_font = FontFromFont(self._tab_label_font)
self._tab_active_label_font.SetWeight(wx.FONTWEIGHT_BOLD)
self._page_border_left = 1
self._page_border_right = 1
self._page_border_top = 1
self._page_border_bottom = 2
self._tab_separation_size = 0
self._gallery_bitmap_padding_left_size = 3
self._gallery_bitmap_padding_right_size = 3
self._gallery_bitmap_padding_top_size = 3
self._gallery_bitmap_padding_bottom_size = 3
[docs] def Clone(self):
"""
Create a new art provider which is a clone of this one.
"""
copy = RibbonAUIArtProvider()
self.CloneTo(copy)
copy._tab_ctrl_background_colour = self._tab_ctrl_background_colour
copy._tab_ctrl_background_gradient_colour = self._tab_ctrl_background_gradient_colour
copy._panel_label_background_colour = self._panel_label_background_colour
copy._panel_label_background_gradient_colour = self._panel_label_background_gradient_colour
copy._panel_hover_label_background_colour = self._panel_hover_label_background_colour
copy._panel_hover_label_background_gradient_colour = self._panel_hover_label_background_gradient_colour
copy._background_brush = self._background_brush
copy._tab_active_top_background_brush = self._tab_active_top_background_brush
copy._tab_hover_background_brush = self._tab_hover_background_brush
copy._button_bar_hover_background_brush = self._button_bar_hover_background_brush
copy._button_bar_active_background_brush = self._button_bar_active_background_brush
copy._gallery_button_active_background_brush = self._gallery_button_active_background_brush
copy._gallery_button_hover_background_brush = self._gallery_button_hover_background_brush
copy._gallery_button_disabled_background_brush = self._gallery_button_disabled_background_brush
copy._toolbar_hover_borden_pen = self._toolbar_hover_borden_pen
copy._tool_hover_background_brush = self._tool_hover_background_brush
copy._tool_active_background_brush = self._tool_active_background_brush
return copy
[docs] def SetFont(self, id, font):
"""
Set the value of a certain font setting to the value.
can be one of the font values of `RibbonArtSetting`.
:param `id`: the font id;
:param `font`: MISSING DESCRIPTION.
"""
RibbonMSWArtProvider.SetFont(self, id, font)
if id == RIBBON_ART_TAB_LABEL_FONT:
self._tab_active_label_font = FontFromFont(self._tab_label_font)
self._tab_active_label_font.SetWeight(wx.FONTWEIGHT_BOLD)
[docs] def GetColour(self, id):
"""
Get the value of a certain colour setting.
can be one of the colour values of `RibbonArtSetting`.
:param `id`: the colour id.
"""
if id in [RIBBON_ART_PAGE_BACKGROUND_COLOUR, RIBBON_ART_PAGE_BACKGROUND_GRADIENT_COLOUR]:
return self._background_brush.GetColour()
elif id == RIBBON_ART_TAB_CTRL_BACKGROUND_COLOUR:
return self._tab_ctrl_background_colour
elif id == RIBBON_ART_TAB_CTRL_BACKGROUND_GRADIENT_COLOUR:
return self._tab_ctrl_background_gradient_colour
elif id in [RIBBON_ART_TAB_ACTIVE_BACKGROUND_TOP_COLOUR, RIBBON_ART_TAB_ACTIVE_BACKGROUND_TOP_GRADIENT_COLOUR]:
return self._tab_active_top_background_brush.GetColour()
elif id in [RIBBON_ART_TAB_HOVER_BACKGROUND_COLOUR, RIBBON_ART_TAB_HOVER_BACKGROUND_GRADIENT_COLOUR]:
return self._tab_hover_background_brush.GetColour()
elif id == RIBBON_ART_PANEL_LABEL_BACKGROUND_COLOUR:
return self._panel_label_background_colour
elif id == RIBBON_ART_PANEL_LABEL_BACKGROUND_GRADIENT_COLOUR:
return self._panel_label_background_gradient_colour
elif id == RIBBON_ART_PANEL_HOVER_LABEL_BACKGROUND_COLOUR:
return self._panel_hover_label_background_colour
elif id == RIBBON_ART_PANEL_HOVER_LABEL_BACKGROUND_GRADIENT_COLOUR:
return self._panel_hover_label_background_gradient_colour
elif id in [RIBBON_ART_BUTTON_BAR_HOVER_BACKGROUND_COLOUR, RIBBON_ART_BUTTON_BAR_HOVER_BACKGROUND_GRADIENT_COLOUR]:
return self._button_bar_hover_background_brush.GetColour()
elif id in [RIBBON_ART_GALLERY_BUTTON_HOVER_BACKGROUND_COLOUR, RIBBON_ART_GALLERY_BUTTON_HOVER_BACKGROUND_GRADIENT_COLOUR]:
return self._gallery_button_hover_background_brush.GetColour()
elif id in [RIBBON_ART_GALLERY_BUTTON_ACTIVE_BACKGROUND_COLOUR, RIBBON_ART_GALLERY_BUTTON_ACTIVE_BACKGROUND_GRADIENT_COLOUR]:
return self._gallery_button_active_background_brush.GetColour()
elif id in [RIBBON_ART_GALLERY_BUTTON_DISABLED_BACKGROUND_COLOUR, RIBBON_ART_GALLERY_BUTTON_DISABLED_BACKGROUND_GRADIENT_COLOUR]:
return self._gallery_button_disabled_background_brush.GetColour()
else:
return RibbonMSWArtProvider.GetColour(self, id)
[docs] def SetColour(self, id, colour):
"""
Set the value of a certain colour setting to the value.
can be one of the colour values of `RibbonArtSetting`, though not all colour
settings will have an affect on every art provider.
:param `id`: the colour id;
:param `colour`: MISSING DESCRIPTION.
:see: L{SetColourScheme}
"""
if id in [RIBBON_ART_PAGE_BACKGROUND_COLOUR, RIBBON_ART_PAGE_BACKGROUND_GRADIENT_COLOUR]:
self._background_brush.SetColour(colour)
elif id == RIBBON_ART_TAB_CTRL_BACKGROUND_COLOUR:
self._tab_ctrl_background_colour = colour
elif id == RIBBON_ART_TAB_CTRL_BACKGROUND_GRADIENT_COLOUR:
self._tab_ctrl_background_gradient_colour = colour
elif id in [RIBBON_ART_TAB_ACTIVE_BACKGROUND_TOP_COLOUR, RIBBON_ART_TAB_ACTIVE_BACKGROUND_TOP_GRADIENT_COLOUR]:
self._tab_active_top_background_brush.SetColour(colour)
elif id in [RIBBON_ART_TAB_HOVER_BACKGROUND_COLOUR, RIBBON_ART_TAB_HOVER_BACKGROUND_GRADIENT_COLOUR]:
self._tab_hover_background_brush.SetColour(colour)
elif id == RIBBON_ART_PANEL_LABEL_BACKGROUND_COLOUR:
self._panel_label_background_colour = colour
elif id == RIBBON_ART_PANEL_LABEL_BACKGROUND_GRADIENT_COLOUR:
self._panel_label_background_gradient_colour = colour
elif id in [RIBBON_ART_BUTTON_BAR_HOVER_BACKGROUND_COLOUR, RIBBON_ART_BUTTON_BAR_HOVER_BACKGROUND_GRADIENT_COLOUR]:
self._button_bar_hover_background_brush.SetColour(colour)
elif id in [RIBBON_ART_GALLERY_BUTTON_HOVER_BACKGROUND_COLOUR, RIBBON_ART_GALLERY_BUTTON_HOVER_BACKGROUND_GRADIENT_COLOUR]:
self._gallery_button_hover_background_brush.SetColour(colour)
elif id in [RIBBON_ART_GALLERY_BUTTON_ACTIVE_BACKGROUND_COLOUR, RIBBON_ART_GALLERY_BUTTON_ACTIVE_BACKGROUND_GRADIENT_COLOUR]:
self._gallery_button_active_background_brush.SetColour(colour)
elif id in [RIBBON_ART_GALLERY_BUTTON_DISABLED_BACKGROUND_COLOUR, RIBBON_ART_GALLERY_BUTTON_DISABLED_BACKGROUND_GRADIENT_COLOUR]:
self._gallery_button_disabled_background_brush.SetColour(colour)
else:
RibbonMSWArtProvider.SetColour(self, id, colour)
[docs] def SetColourScheme(self, primary, secondary, tertiary):
"""
Set all applicable colour settings from a few base colours.
Uses any or all of the three given colours to create a colour scheme, and then
sets all colour settings which are relevant to the art provider using that
scheme. Note that some art providers may not use the tertiary colour for
anything, and some may not use the secondary colour either.
:param `primary`: MISSING DESCRIPTION;
:param `secondary`: MISSING DESCRIPTION;
:param `tertiary`: MISSING DESCRIPTION.
:see: L{SetColour}, L{RibbonMSWArtProvider.GetColourScheme}
"""
primary_hsl = RibbonHSLColour(primary)
secondary_hsl = RibbonHSLColour(secondary)
tertiary_hsl = RibbonHSLColour(tertiary)
# Map primary & secondary luminance from [0, 1] to [0.15, 0.85]
primary_hsl.luminance = cos(primary_hsl.luminance * M_PI) * -0.35 + 0.5
secondary_hsl.luminance = cos(secondary_hsl.luminance * M_PI) * -0.35 + 0.5
# TODO: Remove next line once this provider stops piggybacking MSW
RibbonMSWArtProvider.SetColourScheme(self, primary, secondary, tertiary)
self._tab_ctrl_background_colour = RibbonShiftLuminance(primary_hsl, 0.9).ToRGB()
self._tab_ctrl_background_gradient_colour = RibbonShiftLuminance(primary_hsl, 1.7).ToRGB()
self._tab_border_pen = wx.Pen(RibbonShiftLuminance(primary_hsl, 0.75).ToRGB())
self._tab_label_colour = RibbonShiftLuminance(primary_hsl, 0.1).ToRGB()
self._tab_hover_background_top_colour = primary_hsl.ToRGB()
self._tab_hover_background_top_gradient_colour = RibbonShiftLuminance(primary_hsl, 1.6).ToRGB()
self._tab_hover_background_brush = wx.Brush(self._tab_hover_background_top_colour)
self._tab_active_background_colour = self._tab_ctrl_background_gradient_colour
self._tab_active_background_gradient_colour = primary_hsl.ToRGB()
self._tab_active_top_background_brush = wx.Brush(self._tab_active_background_colour)
self._panel_label_colour = self._tab_label_colour
self._panel_minimised_label_colour = self._panel_label_colour
self._panel_hover_label_colour = tertiary_hsl.ToRGB()
self._page_border_pen = self._tab_border_pen
self._panel_border_pen = self._tab_border_pen
self._background_brush = wx.Brush(primary_hsl.ToRGB())
self._page_hover_background_colour = RibbonShiftLuminance(primary_hsl, 1.5).ToRGB()
self._page_hover_background_gradient_colour = RibbonShiftLuminance(primary_hsl, 0.9).ToRGB()
self._panel_label_background_colour = RibbonShiftLuminance(primary_hsl, 0.85).ToRGB()
self._panel_label_background_gradient_colour = RibbonShiftLuminance(primary_hsl, 0.97).ToRGB()
self._panel_hover_label_background_gradient_colour = secondary_hsl.ToRGB()
self._panel_hover_label_background_colour = secondary_hsl.Lighter(0.2).ToRGB()
self._button_bar_hover_border_pen = wx.Pen(secondary_hsl.ToRGB())
self._button_bar_hover_background_brush = wx.Brush(RibbonShiftLuminance(secondary_hsl, 1.7).ToRGB())
self._button_bar_active_background_brush = wx.Brush(RibbonShiftLuminance(secondary_hsl, 1.4).ToRGB())
self._button_bar_label_colour = self._tab_label_colour
self._gallery_border_pen = self._tab_border_pen
self._gallery_item_border_pen = self._button_bar_hover_border_pen
self._gallery_hover_background_brush = wx.Brush(RibbonShiftLuminance(primary_hsl, 1.2).ToRGB())
self._gallery_button_background_colour = self._page_hover_background_colour
self._gallery_button_background_gradient_colour = self._page_hover_background_gradient_colour
self._gallery_button_hover_background_brush = self._button_bar_hover_background_brush
self._gallery_button_active_background_brush = self._button_bar_active_background_brush
self._gallery_button_disabled_background_brush = wx.Brush(primary_hsl.Desaturated(0.15).ToRGB())
self.SetColour(RIBBON_ART_GALLERY_BUTTON_FACE_COLOUR, RibbonShiftLuminance(primary_hsl, 0.1).ToRGB())
self.SetColour(RIBBON_ART_GALLERY_BUTTON_DISABLED_FACE_COLOUR, wx.Colour(128, 128, 128))
self.SetColour(RIBBON_ART_GALLERY_BUTTON_ACTIVE_FACE_COLOUR, RibbonShiftLuminance(secondary_hsl, 0.1).ToRGB())
self.SetColour(RIBBON_ART_GALLERY_BUTTON_HOVER_FACE_COLOUR, RibbonShiftLuminance(secondary_hsl, 0.1).ToRGB())
self._toolbar_border_pen = self._tab_border_pen
self.SetColour(RIBBON_ART_TOOLBAR_FACE_COLOUR, RibbonShiftLuminance(primary_hsl, 0.1).ToRGB())
self._tool_background_colour = self._page_hover_background_colour
self._tool_background_gradient_colour = self._page_hover_background_gradient_colour
self._toolbar_hover_borden_pen = self._button_bar_hover_border_pen
self._tool_hover_background_brush = self._button_bar_hover_background_brush
self._tool_active_background_brush = self._button_bar_active_background_brush
[docs] def DrawTabCtrlBackground(self, dc, wnd, rect):
"""
Draw the background of the tab region of a ribbon bar.
:param `dc`: The device context to draw onto;
:param `wnd`: The window which is being drawn onto;
:param `rect`: The rectangle within which to draw.
"""
gradient_rect = wx.Rect(*rect)
gradient_rect.height -= 1
dc.GradientFillLinear(gradient_rect, self._tab_ctrl_background_colour, self._tab_ctrl_background_gradient_colour, wx.SOUTH)
dc.SetPen(self._tab_border_pen)
dc.DrawLine(rect.x, rect.GetBottom(), rect.GetRight()+1, rect.GetBottom())
[docs] def GetTabCtrlHeight(self, dc, wnd, pages):
"""
Calculate the height (in pixels) of the tab region of a ribbon bar.
Note that as the tab region can contain scroll buttons, the height should be
greater than or equal to the minimum height for a tab scroll button.
:param `dc`: A device context to use when one is required for size calculations;
:param `wnd`: The window onto which the tabs will eventually be drawn;
:param `pages`: The tabs which will acquire the returned height.
"""
text_height = 0
icon_height = 0
if len(pages) <= 1 and (self._flags & RIBBON_BAR_ALWAYS_SHOW_TABS) == 0:
# To preserve space, a single tab need not be displayed. We still need
# one pixel of border though.
return 1
if self._flags & RIBBON_BAR_SHOW_PAGE_LABELS:
dc.SetFont(self._tab_active_label_font)
text_height = dc.GetTextExtent("ABCDEFXj")[1]
if self._flags & RIBBON_BAR_SHOW_PAGE_ICONS:
for info in pages:
if info.page.GetIcon().IsOk():
icon_height = max(icon_height, info.page.GetIcon().GetHeight())
return max(text_height, icon_height) + 10
[docs] def DrawTab(self, dc, wnd, tab):
"""
Draw a single tab in the tab region of a ribbon bar.
:param `dc`: The device context to draw onto;
:param `wnd`: The window which is being drawn onto (not the L{RibbonPage}
associated with the tab being drawn);
:param `tab`: The rectangle within which to draw, and also the tab label,
icon, and state (active and/or hovered). The drawing rectangle will be
entirely within a rectangle on the same device context previously painted
with L{DrawTabCtrlBackground}. The rectangle's width will be at least the
minimum value returned by L{GetBarTabWidth}, and height will be the value
returned by L{GetTabCtrlHeight}.
"""
if tab.rect.height <= 1:
return
dc.SetFont(self._tab_label_font)
dc.SetPen(wx.TRANSPARENT_PEN)
if tab.active or tab.hovered:
if tab.active:
dc.SetFont(self._tab_active_label_font)
dc.SetBrush(self._background_brush)
dc.DrawRectangle(tab.rect.x, tab.rect.y + tab.rect.height - 1, tab.rect.width - 1, 1)
grad_rect = wx.Rect(*tab.rect)
grad_rect.height -= 4
grad_rect.width -= 1
grad_rect.height /= 2
grad_rect.y = grad_rect.y + tab.rect.height - grad_rect.height - 1
dc.SetBrush(self._tab_active_top_background_brush)
dc.DrawRectangle(tab.rect.x, tab.rect.y + 3, tab.rect.width - 1, grad_rect.y - tab.rect.y - 3)
dc.GradientFillLinear(grad_rect, self._tab_active_background_colour, self._tab_active_background_gradient_colour, wx.SOUTH)
else:
btm_rect = wx.Rect(*tab.rect)
btm_rect.height -= 4
btm_rect.width -= 1
btm_rect.height /= 2
btm_rect.y = btm_rect.y + tab.rect.height - btm_rect.height - 1
dc.SetBrush(self._tab_hover_background_brush)
dc.DrawRectangle(btm_rect.x, btm_rect.y, btm_rect.width, btm_rect.height)
grad_rect = wx.Rect(*tab.rect)
grad_rect.width -= 1
grad_rect.y += 3
grad_rect.height = btm_rect.y - grad_rect.y
dc.GradientFillLinear(grad_rect, self._tab_hover_background_top_colour, self._tab_hover_background_top_gradient_colour, wx.SOUTH)
border_points = [wx.Point() for i in xrange(5)]
border_points[0] = wx.Point(0, 3)
border_points[1] = wx.Point(1, 2)
border_points[2] = wx.Point(tab.rect.width - 3, 2)
border_points[3] = wx.Point(tab.rect.width - 1, 4)
border_points[4] = wx.Point(tab.rect.width - 1, tab.rect.height - 1)
dc.SetPen(self._tab_border_pen)
dc.DrawLines(border_points, tab.rect.x, tab.rect.y)
old_clip = dc.GetClippingRect()
is_first_tab = False
bar = tab.page.GetParent()
icon = wx.NullBitmap
if isinstance(bar, BAR.RibbonBar) and bar.GetPage(0) == tab.page:
is_first_tab = True
if self._flags & RIBBON_BAR_SHOW_PAGE_ICONS:
icon = tab.page.GetIcon()
if self._flags & RIBBON_BAR_SHOW_PAGE_LABELS == 0:
if icon.IsOk():
x = tab.rect.x + (tab.rect.width - icon.GetWidth()) / 2
dc.DrawBitmap(icon, x, tab.rect.y + 1 + (tab.rect.height - 1 - icon.GetHeight()) / 2, True)
if self._flags & RIBBON_BAR_SHOW_PAGE_LABELS:
label = tab.page.GetLabel()
if label.strip():
dc.SetTextForeground(self._tab_label_colour)
dc.SetBackgroundMode(wx.TRANSPARENT)
offset = 0
if icon.IsOk():
offset += icon.GetWidth() + 2
text_width, text_height = dc.GetTextExtent(label)
x = (tab.rect.width - 2 - text_width - offset) / 2
if x > 8:
x = 8
elif x < 1:
x = 1
width = tab.rect.width - x - 2
x += tab.rect.x + offset
y = tab.rect.y + (tab.rect.height - text_height) / 2
if icon.IsOk():
dc.DrawBitmap(icon, x - offset, tab.rect.y + (tab.rect.height - icon.GetHeight()) / 2, True)
dc.SetClippingRegion(x, tab.rect.y, width, tab.rect.height)
dc.DrawText(label, x, y)
# Draw the left hand edge of the tab only for the first tab (subsequent
# tabs use the right edge of the prior tab as their left edge). As this is
# outside the rectangle for the tab, only draw it if the leftmost part of
# the tab is within the clip rectangle (the clip region has to be cleared
# to draw outside the tab).
if is_first_tab and old_clip.x <= tab.rect.x and tab.rect.x < old_clip.x + old_clip.width:
dc.DestroyClippingRegion()
dc.DrawLine(tab.rect.x - 1, tab.rect.y + 4, tab.rect.x - 1, tab.rect.y + tab.rect.height - 1)
[docs] def GetBarTabWidth(self, dc, wnd, label, bitmap, ideal=None, small_begin_need_separator=None,
small_must_have_separator=None, minimum=None):
"""
Calculate the ideal and minimum width (in pixels) of a tab in a ribbon bar.
:param `dc`: A device context to use when one is required for size calculations;
:param `wnd`: The window onto which the tab will eventually be drawn;
:param `label`: The tab's label (or an empty string if it has none);
:param `bitmap`: The tab's icon (or `wx.NullBitmap` if it has none);
:param `ideal`: The ideal width (in pixels) of the tab;
:param `small_begin_need_separator`: A size less than the size, at which a tab
separator should begin to be drawn (i.e. drawn, but still fairly transparent);
:param `small_must_have_separator`: A size less than the size, at which a tab
separator must be drawn (i.e. drawn at full opacity);
:param `minimum`: A size less than the size, and greater than or equal to zero,
which is the minimum pixel width for the tab.
"""
width = mini = 0
if self._flags & RIBBON_BAR_SHOW_PAGE_LABELS and label.strip():
dc.SetFont(self._tab_active_label_font)
width += dc.GetTextExtent(label)[0]
mini += min(30, width) # enough for a few chars
if bitmap.IsOk():
# gap between label and bitmap
width += 4
mini += 2
if self._flags & RIBBON_BAR_SHOW_PAGE_ICONS and bitmap.IsOk():
width += bitmap.GetWidth()
mini += bitmap.GetWidth()
ideal = width + 16
small_begin_need_separator = mini
small_must_have_separator = mini
minimum = mini
return ideal, small_begin_need_separator, small_must_have_separator, minimum
[docs] def DrawTabSeparator(self, dc, wnd, rect, visibility):
"""
Draw a separator between two tabs in a ribbon bar.
:param `dc`: The device context to draw onto;
:param `wnd`: The window which is being drawn onto;
:param `rect`: The rectangle within which to draw, which will be entirely
within a rectangle on the same device context previously painted with
L{DrawTabCtrlBackground};
:param `visibility`: The opacity with which to draw the separator. Values
are in the range [0, 1], with 0 being totally transparent, and 1 being totally
opaque.
"""
# No explicit separators between tabs
pass
[docs] def DrawPageBackground(self, dc, wnd, rect):
"""
Draw the background of a ribbon page.
:param `dc`: The device context to draw onto;
:param `wnd`: The window which is being drawn onto (which is commonly the
L{RibbonPage} whose background is being drawn, but doesn't have to be);
:param `rect`: The rectangle within which to draw.
:see: L{RibbonMSWArtProvider.GetPageBackgroundRedrawArea}
"""
dc.SetPen(wx.TRANSPARENT_PEN)
dc.SetBrush(self._background_brush)
dc.DrawRectangle(rect.x + 1, rect.y, rect.width - 2, rect.height - 1)
dc.SetPen(self._page_border_pen)
dc.DrawLine(rect.x, rect.y, rect.x, rect.y + rect.height)
dc.DrawLine(rect.GetRight(), rect.y, rect.GetRight(), rect.y +rect.height)
dc.DrawLine(rect.x, rect.GetBottom(), rect.GetRight()+1, rect.GetBottom())
[docs] def GetPanelSize(self, dc, wnd, client_size, client_offset=None):
"""
Calculate the size of a panel for a given client size.
This should increment the given size by enough to fit the panel label and other
chrome.
:param `dc`: A device context to use if one is required for size calculations;
:param `wnd`: The ribbon panel in question;
:param `client_size`: The client size;
:param `client_offset`: The offset where the client rectangle begins within
the panel (may be ``None``).
:see: L{GetPanelClientSize}
"""
dc.SetFont(self._panel_label_font)
label_size = wx.Size(*dc.GetTextExtent(wnd.GetLabel()))
label_height = label_size.GetHeight() + 5
if self._flags & RIBBON_BAR_FLOW_VERTICAL:
client_size.IncBy(4, label_height + 6)
if client_offset is not None:
client_offset = wx.Point(2, label_height + 3)
else:
client_size.IncBy(6, label_height + 4)
if client_offset is not None:
client_offset = wx.Point(3, label_height + 2)
return client_size
[docs] def GetPanelClientSize(self, dc, wnd, size, client_offset=None):
"""
Calculate the client size of a panel for a given overall size.
This should act as the inverse to L{GetPanelSize}, and decrement the given size
by enough to fit the panel label and other chrome.
:param `dc`: A device context to use if one is required for size calculations;
:param `wnd`: The ribbon panel in question;
:param `size`: The overall size to calculate client size for;
:param `client_offset`: The offset where the returned client size begins within
the given (may be ``None``).
:see: L{GetPanelSize}
"""
dc.SetFont(self._panel_label_font)
label_size = wx.Size(*dc.GetTextExtent(wnd.GetLabel()))
label_height = label_size.GetHeight() + 5
if self._flags & RIBBON_BAR_FLOW_VERTICAL:
size.DecBy(4, label_height + 6)
if client_offset is not None:
client_offset = wx.Point(2, label_height + 3)
else:
size.DecBy(6, label_height + 4)
if client_offset is not None:
client_offset = wx.Point(3, label_height + 2)
if size.x < 0:
size.x = 0
if size.y < 0:
size.y = 0
return size, client_offset
[docs] def DrawPanelBackground(self, dc, wnd, rect):
"""
Draw the background and chrome for a ribbon panel.
This should draw the border, background, label, and any other items of a panel
which are outside the client area of a panel. Note that when a panel is
minimised, this function is not called - only L{DrawMinimisedPanel} is called,
so a background should be explicitly painted by that if required.
:param `dc`: The device context to draw onto;
:param `wnd`: The window which is being drawn onto, which is always the panel
whose background and chrome is being drawn. The panel label and other panel
attributes can be obtained by querying this;
:param `rect`: The rectangle within which to draw.
"""
dc.SetPen(wx.TRANSPARENT_PEN)
dc.SetBrush(self._background_brush)
dc.DrawRectangle(rect.x, rect.y, rect.width, rect.height)
true_rect = wx.Rect(*rect)
true_rect = self.RemovePanelPadding(true_rect)
dc.SetPen(self._panel_border_pen)
dc.SetBrush(wx.TRANSPARENT_BRUSH)
dc.DrawRectangle(true_rect.x, true_rect.y, true_rect.width, true_rect.height)
true_rect.x += 1
true_rect.width -= 2
true_rect.y += 1
dc.SetFont(self._panel_label_font)
label_size = wx.Size(*dc.GetTextExtent(wnd.GetLabel()))
label_height = label_size.GetHeight() + 5
label_rect = wx.Rect(*true_rect)
label_rect.height = label_height - 1
dc.DrawLine(label_rect.x, label_rect.y + label_rect.height, label_rect.x + label_rect.width, label_rect.y + label_rect.height)
label_bg_colour = self._panel_label_background_colour
label_bg_grad_colour = self._panel_label_background_gradient_colour
if wnd.IsHovered():
label_bg_colour = self._panel_hover_label_background_colour
label_bg_grad_colour = self._panel_hover_label_background_gradient_colour
dc.SetTextForeground(self._panel_hover_label_colour)
else:
dc.SetTextForeground(self._panel_label_colour)
if wx.Platform == "__WXMAC__":
dc.GradientFillLinear(label_rect, label_bg_grad_colour, label_bg_colour, wx.SOUTH)
else:
dc.GradientFillLinear(label_rect, label_bg_colour, label_bg_grad_colour, wx.SOUTH)
dc.SetFont(self._panel_label_font)
dc.DrawText(wnd.GetLabel(), label_rect.x + 3, label_rect.y + 2)
if wnd.IsHovered():
gradient_rect = wx.Rect(*true_rect)
gradient_rect.y += label_rect.height + 1
gradient_rect.height = true_rect.height - label_rect.height - 3
if wx.Platform == "__WXMAC__":
colour = self._page_hover_background_gradient_colour
gradient = self._page_hover_background_colour
else:
colour = self._page_hover_background_colour
gradient = self._page_hover_background_gradient_colour
dc.GradientFillLinear(gradient_rect, colour, gradient, wx.SOUTH)
[docs] def DrawMinimisedPanel(self, dc, wnd, rect, bitmap):
"""
Draw a minimised ribbon panel.
:param `dc`: The device context to draw onto;
:param `wnd`: The window which is being drawn onto, which is always the panel
which is minimised. The panel label can be obtained from this window. The
minimised icon obtained from querying the window may not be the size requested
by L{RibbonMSWArtProvider.GetMinimisedPanelMinimumSize} - the argument contains the icon in the
requested size;
:param `rect`: The rectangle within which to draw. The size of the rectangle
will be at least the size returned by L{RibbonMSWArtProvider.GetMinimisedPanelMinimumSize};
:param `bitmap`: A copy of the panel's minimised bitmap rescaled to the size
returned by L{RibbonMSWArtProvider.GetMinimisedPanelMinimumSize}.
"""
dc.SetPen(wx.TRANSPARENT_PEN)
dc.SetBrush(self._background_brush)
dc.DrawRectangle(rect.x, rect.y, rect.width, rect.height)
true_rect = wx.Rect(*rect)
true_rect = self.RemovePanelPadding(true_rect)
dc.SetPen(self._panel_border_pen)
dc.SetBrush(wx.TRANSPARENT_BRUSH)
dc.DrawRectangle(true_rect.x, true_rect.y, true_rect.width, true_rect.height)
true_rect.Deflate(1, 1)
if wnd.IsHovered() or wnd.GetExpandedPanel():
colour = self._page_hover_background_colour
gradient = self._page_hover_background_gradient_colour
if (wx.Platform == "__WXMAC__" and not wnd.GetExpandedPanel()) or \
(wx.Platform != "__WXMAC__" and wnd.GetExpandedPanel()):
temp = colour
colour = gradient
gradient = temp
dc.GradientFillLinear(true_rect, colour, gradient, wx.SOUTH)
preview = self.DrawMinimisedPanelCommon(dc, wnd, true_rect)
dc.SetPen(self._panel_border_pen)
dc.SetBrush(wx.TRANSPARENT_BRUSH)
dc.DrawRectangle(preview.x, preview.y, preview.width, preview.height)
preview.Deflate(1, 1)
preview_caption_rect = wx.Rect(*preview)
preview_caption_rect.height = 7
preview.y += preview_caption_rect.height
preview.height -= preview_caption_rect.height
if wx.Platform == "__WXMAC__":
dc.GradientFillLinear(preview_caption_rect, self._panel_hover_label_background_gradient_colour,
self._panel_hover_label_background_colour, wx.SOUTH)
dc.GradientFillLinear(preview, self._page_hover_background_gradient_colour,
self._page_hover_background_colour, wx.SOUTH)
else:
dc.GradientFillLinear(preview_caption_rect, self._panel_hover_label_background_colour,
self._panel_hover_label_background_gradient_colour, wx.SOUTH)
dc.GradientFillLinear(preview, self._page_hover_background_colour,
self._page_hover_background_gradient_colour, wx.SOUTH)
if bitmap.IsOk():
dc.DrawBitmap(bitmap, preview.x + (preview.width - bitmap.GetWidth()) / 2,
preview.y + (preview.height - bitmap.GetHeight()) / 2, True)
[docs] def DrawPartialPanelBackground(self, dc, wnd, rect):
dc.SetPen(wx.TRANSPARENT_PEN)
dc.SetBrush(self._background_brush)
dc.DrawRectangle(rect.x, rect.y, rect.width, rect.height)
offset = wx.Point(*wnd.GetPosition())
parent = wnd.GetParent()
panel = None
while 1:
panel = parent
if isinstance(panel, PANEL.RibbonPanel):
if not panel.IsHovered():
return
break
offset += parent.GetPosition()
parent = panel.GetParent()
if panel is None:
return
background = wx.Rect(0, 0, *panel.GetSize())
background = self.RemovePanelPadding(background)
background.x += 1
background.width -= 2
dc.SetFont(self._panel_label_font)
caption_height = dc.GetTextExtent(panel.GetLabel())[1] + 7
background.y += caption_height - 1
background.height -= caption_height
paint_rect = wx.Rect(*rect)
paint_rect.x += offset.x
paint_rect.y += offset.y
if wx.Platform == "__WXMAC__":
bg_grad_clr = self._page_hover_background_colour
bg_clr = self._page_hover_background_gradient_colour
else:
bg_clr = self._page_hover_background_colour
bg_grad_clr = self._page_hover_background_gradient_colour
paint_rect.Intersect(background)
if not paint_rect.IsEmpty():
starting_colour = RibbonInterpolateColour(bg_clr, bg_grad_clr, paint_rect.y, background.y, background.y + background.height)
ending_colour = RibbonInterpolateColour(bg_clr, bg_grad_clr, paint_rect.y + paint_rect.height, background.y, background.y + background.height)
paint_rect.x -= offset.x
paint_rect.y -= offset.y
dc.GradientFillLinear(paint_rect, starting_colour, ending_colour, wx.SOUTH)
[docs] def DrawGalleryBackground(self, dc, wnd, rect):
"""
Draw the background and chrome for a L{RibbonGallery} control.
This should draw the border, brackground, scroll buttons, extension button, and
any other UI elements which are not attached to a specific gallery item.
:param `dc`: The device context to draw onto;
:param `wnd`: The window which is being drawn onto, which is always the gallery
whose background and chrome is being drawn. Attributes used during drawing like
the gallery hover state and individual button states can be queried from this
parameter by L{RibbonGallery.IsHovered}, L{RibbonGallery.GetExtensionButtonState},
L{RibbonGallery.GetUpButtonState}, and L{RibbonGallery.GetDownButtonState};
:param `rect`: The rectangle within which to draw. This rectangle is the entire
area of the gallery control, not just the client rectangle.
"""
self.DrawPartialPanelBackground(dc, wnd, rect)
if wnd.IsHovered():
dc.SetPen(wx.TRANSPARENT_PEN)
dc.SetBrush(self._gallery_hover_background_brush)
if self._flags & RIBBON_BAR_FLOW_VERTICAL:
dc.DrawRectangle(rect.x + 1, rect.y + 1, rect.width - 2, rect.height - 16)
else:
dc.DrawRectangle(rect.x + 1, rect.y + 1, rect.width - 16, rect.height - 2)
dc.SetPen(self._gallery_border_pen)
dc.SetBrush(wx.TRANSPARENT_BRUSH)
dc.DrawRectangle(rect.x, rect.y, rect.width, rect.height)
self.DrawGalleryBackgroundCommon(dc, wnd, rect)
[docs] def DrawGalleryItemBackground(self, dc, wnd, rect, item):
"""
Draw the background of a single item in a L{RibbonGallery} control.
This is painted on top of a gallery background, and behind the items bitmap.
Unlike L{DrawButtonBarButton} and L{DrawTool}, it is not expected to draw the
item bitmap - that is done by the gallery control itself.
:param `dc`: The device context to draw onto;
:param `wnd`: The window which is being drawn onto, which is always the gallery
which contains the item being drawn;
:param `rect`: The rectangle within which to draw. The size of this rectangle
will be the size of the item's bitmap, expanded by gallery item padding values
(``RIBBON_ART_GALLERY_BITMAP_PADDING_LEFT_SIZE``, ``RIBBON_ART_GALLERY_BITMAP_PADDING_RIGHT_SIZE``,
``RIBBON_ART_GALLERY_BITMAP_PADDING_TOP_SIZE``, and ``RIBBON_ART_GALLERY_BITMAP_PADDING_BOTTOM_SIZE``).
The drawing rectangle will be entirely within a rectangle on the same device
context previously painted with L{DrawGalleryBackground};
:param `item`: The item whose background is being painted. Typically the
background will vary if the item is hovered, active, or selected;
L{RibbonGallery.GetSelection}, L{RibbonGallery.GetActiveItem}, and
L{RibbonGallery.GetHoveredItem} can be called to test if the given item is in one of these states.
"""
if wnd.GetHoveredItem() != item and wnd.GetActiveItem() != item and wnd.GetSelection() != item:
return
dc.SetPen(self._gallery_item_border_pen)
if wnd.GetActiveItem() == item or wnd.GetSelection() == item:
dc.SetBrush(self._gallery_button_active_background_brush)
else:
dc.SetBrush(self._gallery_button_hover_background_brush)
dc.DrawRectangle(rect.x, rect.y, rect.width, rect.height)