KiCad PCB EDA Suite
Loading...
Searching...
No Matches
kicad_tab_art.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
21
22#include <utility>
23
24#include <wx/dc.h>
25#include <wx/pen.h>
26
27
28TAB_VISUAL_STATE ResolveTabVisualState( bool aPreview, bool aModified )
29{
30 TAB_VISUAL_STATE state;
31
32 // An edit promotes a preview to a permanent tab, so modified wins.
33 if( aModified )
34 {
35 state.bold = true;
36 state.showAsterisk = true;
37 }
38 else if( aPreview )
39 {
40 state.italic = true;
41 }
42
43 return state;
44}
45
46
48 m_stateFn( std::move( aProvider ) )
49{
50 // Sizing receives no per-tab state, so measure with the widest font (bold) and no tab can clip.
51 wxFont measuring = m_measuringFont;
52 measuring.SetWeight( wxFONTWEIGHT_BOLD );
53 m_measuringFont = measuring;
54}
55
56
58{
59 return new KICAD_TAB_ART( m_stateFn );
60}
61
62
63TAB_VISUAL_STATE KICAD_TAB_ART::stateFor( wxWindow* aPageWindow ) const
64{
65 if( m_stateFn )
66 return m_stateFn( aPageWindow );
67
68 return TAB_VISUAL_STATE{};
69}
70
71
72wxFont KICAD_TAB_ART::decoratedFont( const TAB_VISUAL_STATE& aState, bool aActive ) const
73{
74 wxFont font = aActive ? m_selectedFont : m_normalFont;
75 font.SetStyle( aState.italic ? wxFONTSTYLE_ITALIC : wxFONTSTYLE_NORMAL );
76 font.SetWeight( aState.bold ? wxFONTWEIGHT_BOLD : wxFONTWEIGHT_NORMAL );
77
78 return font;
79}
80
81
82#if wxCHECK_VERSION( 3, 3, 0 )
83
84int KICAD_TAB_ART::DrawPageTab( wxDC& aDc, wxWindow* aWnd, wxAuiNotebookPage& aPage,
85 const wxRect& aRect )
86{
87 const TAB_VISUAL_STATE st = stateFor( aPage.window );
88
89 // The base writes the laid-out rect back into aPage, so decorate the real page in place and
90 // restore its caption afterwards rather than measuring a throwaway copy.
91 const wxString savedCaption = aPage.caption;
92
93 if( st.showAsterisk )
94 aPage.caption = wxT( "*" ) + aPage.caption;
95
96 const wxFont decorated = decoratedFont( st, aPage.active );
97
98 const wxFont savedNormal = m_normalFont;
99 const wxFont savedSelected = m_selectedFont;
100
101 m_normalFont = decorated;
102 m_selectedFont = decorated;
103
104 const int xExtent = wxAuiDefaultTabArt::DrawPageTab( aDc, aWnd, aPage, aRect );
105
106 m_normalFont = savedNormal;
107 m_selectedFont = savedSelected;
108 aPage.caption = savedCaption;
109
110 return xExtent;
111}
112
113
114wxSize KICAD_TAB_ART::GetPageTabSize( wxReadOnlyDC& aDc, wxWindow* aWnd,
115 const wxAuiNotebookPage& aPage, int* aXExtent )
116{
117 // Always reserve the leading "*" width since a modified tab prepends one when drawn.
118 wxAuiNotebookPage reserved = aPage;
119 reserved.caption = wxT( "*" ) + aPage.caption;
120
121 return wxAuiDefaultTabArt::GetPageTabSize( aDc, aWnd, reserved, aXExtent );
122}
123
124#else
125
126void KICAD_TAB_ART::DrawTab( wxDC& aDc, wxWindow* aWnd, const wxAuiNotebookPage& aPage,
127 const wxRect& aInRect, int aCloseButtonState, wxRect* aOutTabRect,
128 wxRect* aOutButtonRect, int* aXExtent )
129{
130 const TAB_VISUAL_STATE st = stateFor( aPage.window );
131
132 // Decorate a local copy so the notebook's stored caption stays unmutated.
133 wxAuiNotebookPage local = aPage;
134
135 if( st.showAsterisk )
136 local.caption = wxT( "*" ) + local.caption;
137
138 const wxFont decorated = decoratedFont( st, aPage.active );
139
140 const wxFont savedNormal = m_normalFont;
141 const wxFont savedSelected = m_selectedFont;
142
143 m_normalFont = decorated;
144 m_selectedFont = decorated;
145
146 wxAuiDefaultTabArt::DrawTab( aDc, aWnd, local, aInRect, aCloseButtonState, aOutTabRect,
147 aOutButtonRect, aXExtent );
148
149 m_normalFont = savedNormal;
150 m_selectedFont = savedSelected;
151}
152
153
154wxSize KICAD_TAB_ART::GetTabSize( wxDC& aDc, wxWindow* aWnd, const wxString& aCaption,
155 const wxBitmapBundle& aBitmap, bool aActive,
156 int aCloseButtonState, int* aXExtent )
157{
158 // Always reserve the leading "*" width since a modified tab prepends one when drawn.
159 const wxString reserved = wxT( "*" ) + aCaption;
160
161 return wxAuiDefaultTabArt::GetTabSize( aDc, aWnd, reserved, aBitmap, aActive, aCloseButtonState,
162 aXExtent );
163}
164
165#endif
wxSize GetTabSize(wxDC &aDc, wxWindow *aWnd, const wxString &aCaption, const wxBitmapBundle &aBitmap, bool aActive, int aCloseButtonState, int *aXExtent) override
KICAD_TAB_ART(STATE_FN aProvider)
wxAuiTabArt * Clone() override
STATE_FN m_stateFn
std::function< TAB_VISUAL_STATE(wxWindow *)> STATE_FN
Returns the current visual state for a page window.
void DrawTab(wxDC &aDc, wxWindow *aWnd, const wxAuiNotebookPage &aPage, const wxRect &aInRect, int aCloseButtonState, wxRect *aOutTabRect, wxRect *aOutButtonRect, int *aXExtent) override
wxFont decoratedFont(const TAB_VISUAL_STATE &aState, bool aActive) const
The base caption font for a tab, styled italic for preview and bold for modified.
TAB_VISUAL_STATE stateFor(wxWindow *aPageWindow) const
Visual state for a page, or a default if no provider is set.
TAB_VISUAL_STATE ResolveTabVisualState(bool aPreview, bool aModified)
Resolve a tab's decorations from its document state flags.
STL namespace.
Visual decorations derived from document state: preview is italic, modified is bold with a leading as...