KiCad PCB EDA Suite
Loading...
Searching...
No Matches
editor_tabs_panel.h
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
20#ifndef EDITOR_TABS_PANEL_H
21#define EDITOR_TABS_PANEL_H
22
23#include <functional>
24#include <vector>
25
26#include <wx/panel.h>
27#include <wx/string.h>
28
30
32class wxAuiNotebook;
33class wxAuiNotebookEvent;
34class wxBoxSizer;
35class wxMouseEvent;
36
37
41{
42public:
43 struct ENTRY
44 {
45 wxString key;
46 bool preview = false;
47 bool modified = false;
48 };
49
53 int OpenDocument( const wxString& aKey, bool aAsPreview );
54
58 void CloseDocument( const wxString& aKey );
59
63 void MarkModified( const wxString& aKey, bool aModified );
64
68 void Promote( const wxString& aKey );
69
73 bool CanCloseWithoutPrompt( const wxString& aKey ) const;
74
75 int FindIndex( const wxString& aKey ) const;
76
80 int PreviewIndex() const;
81
82 const std::vector<ENTRY>& Entries() const { return m_entries; }
83
84private:
85 std::vector<ENTRY> m_entries;
86};
87
88
91class EDITOR_TABS_PANEL : public wxPanel
92{
93public:
94 EDITOR_TABS_PANEL( wxWindow* aParent, EDA_DRAW_PANEL_GAL* aSharedCanvas );
95 ~EDITOR_TABS_PANEL() override;
96
98 std::function<void( int )> onActivateTab;
99
101 std::function<bool( int )> onCloseTabRequested;
102
105
112 void ReleaseSharedCanvas();
113
118 void SetSuppressActivateOnClose( bool aSuppress ) { m_suppressActivateOnClose = aSuppress; }
119
120 int AddTab( const wxString& aKey, const wxString& aLabel, bool aAsPreview );
121 void CloseTab( int aIdx );
122 void CloseOthers( int aKeepIdx );
123 void CloseToRight( int aIdx );
124 void CloseAll();
125
129 void MarkModified( int aIdx, bool aModified );
130
134 void PromoteTab( int aIdx );
135
136 void SelectTab( int aIdx );
137 void AdvanceTab( bool aForward );
138 int GetActiveTab() const;
139 int FindTab( const wxString& aKey ) const;
140 void RefreshTabLabels();
141
142 const EDITOR_TABS_MODEL& Model() const { return m_model; }
143
144private:
145 void onPageChanged( wxAuiNotebookEvent& aEvent );
146 void onPageClose( wxAuiNotebookEvent& aEvent );
147 void onTabRightDown( wxAuiNotebookEvent& aEvent );
148 void onTabDClick( wxMouseEvent& aEvent );
149 void onContextMenu( wxCommandEvent& aEvent );
150
154 void closeTabInternal( int aIdx );
155
159 void activateTab( int aIdx );
160
164 void touchMru( const wxString& aKey );
165
169 void forgetMru( const wxString& aKey );
170
174 int indexOfWindow( wxWindow* aWindow ) const;
175
181
187 void bindTabDClick();
188
189 TAB_VISUAL_STATE visualStateForIndex( int aIdx ) const;
190
191 wxAuiNotebook* m_tabs = nullptr;
193
196 wxWindow* m_originalCanvasParent = nullptr;
197
198 wxBoxSizer* m_sizer = nullptr;
200
202 bool m_activating = false;
203
206
208 std::vector<wxWindow*> m_pageWindows;
209
211 std::vector<wxString> m_mru;
212
214};
215
216#endif // EDITOR_TABS_PANEL_H
GUI-free preview/dirty/close state machine.
const std::vector< ENTRY > & Entries() const
int PreviewIndex() const
Index of the current reusable preview tab, or -1 if none.
bool CanCloseWithoutPrompt(const wxString &aKey) const
True when the document has no unsaved edits and can be closed silently.
void Promote(const wxString &aKey)
Clear the preview flag so the tab becomes permanent and is no longer reused.
void MarkModified(const wxString &aKey, bool aModified)
Update the modified flag.
int FindIndex(const wxString &aKey) const
int OpenDocument(const wxString &aKey, bool aAsPreview)
Return the index to display the document at, reusing a preview slot or appending a new tab.
std::vector< ENTRY > m_entries
void CloseDocument(const wxString &aKey)
Drop the document with aKey, freeing the preview slot if it held it.
void closeTabInternal(int aIdx)
Close the tab at aIdx, prompting the host exactly once.
std::function< TAB_VISUAL_STATE(int)> onQueryVisualState
Host reports the visual state (modified/preview) for the tab at the given index.
void bindTabDClick()
(Re)bind the double-click handler to the notebook's current tab-strip control.
TAB_VISUAL_STATE visualStateForIndex(int aIdx) const
wxAuiNotebook * m_tabs
int indexOfWindow(wxWindow *aWindow) const
Map a notebook page window pointer to its current tab index, or -1.
void onPageChanged(wxAuiNotebookEvent &aEvent)
void CloseToRight(int aIdx)
void onContextMenu(wxCommandEvent &aEvent)
bool m_suppressActivateOnClose
When set, closeTabInternal selects the fallback page without firing onActivateTab.
void SetSuppressActivateOnClose(bool aSuppress)
When set, a close selects the fallback page visually but does not fire onActivateTab,...
void AdvanceTab(bool aForward)
std::vector< wxString > m_mru
Most-recently-used key order for Ctrl+Tab cycling; front is most recent.
void touchMru(const wxString &aKey)
Bump aKey to the front of the MRU order.
void onTabDClick(wxMouseEvent &aEvent)
void PromoteTab(int aIdx)
Convert a preview tab into a permanent one, dropping its italic styling.
std::function< void(int)> onActivateTab
Host swaps the active document context to the tab at the given index.
std::function< bool(int)> onCloseTabRequested
Host prompts as needed; return false to veto the close.
void MarkModified(int aIdx, bool aModified)
Mark the tab modified.
void onTabRightDown(wxAuiNotebookEvent &aEvent)
void updateTabStripHeight()
Clamp the notebook to its tab-strip height and re-Layout so its (unused) page area collapses and the ...
int AddTab(const wxString &aKey, const wxString &aLabel, bool aAsPreview)
void forgetMru(const wxString &aKey)
Remove aKey from the MRU order.
EDITOR_TABS_PANEL(wxWindow *aParent, EDA_DRAW_PANEL_GAL *aSharedCanvas)
EDA_DRAW_PANEL_GAL * m_sharedCanvas
void CloseOthers(int aKeepIdx)
void onPageClose(wxAuiNotebookEvent &aEvent)
std::vector< wxWindow * > m_pageWindows
Hidden per-tab key windows; index-aligned with the model entries.
void activateTab(int aIdx)
Activate the tab at aIdx without re-entering the change handler.
EDITOR_TABS_MODEL m_model
const EDITOR_TABS_MODEL & Model() const
void ReleaseSharedCanvas()
Hand the host-owned canvas back to its original parent.
wxWindow * m_originalCanvasParent
The canvas's parent before it was borrowed, reparented back on destruction so it is not freed as a ch...
bool m_activating
Guards activateTab() against re-entrancy from programmatic SetActivePage().
int FindTab(const wxString &aKey) const
Visual decorations derived from document state: preview is italic, modified is bold with a leading as...