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 void Rename( const wxString& aOldKey, const wxString& aNewKey );
74
78 bool CanCloseWithoutPrompt( const wxString& aKey ) const;
79
80 int FindIndex( const wxString& aKey ) const;
81
85 int PreviewIndex() const;
86
87 const std::vector<ENTRY>& Entries() const { return m_entries; }
88
89private:
90 std::vector<ENTRY> m_entries;
91};
92
93
96class EDITOR_TABS_PANEL : public wxPanel
97{
98public:
99 EDITOR_TABS_PANEL( wxWindow* aParent, EDA_DRAW_PANEL_GAL* aSharedCanvas );
100 ~EDITOR_TABS_PANEL() override;
101
103 std::function<void( int )> onActivateTab;
104
106 std::function<bool( int )> onCloseTabRequested;
107
110
117 void ReleaseSharedCanvas();
118
123 void SetSuppressActivateOnClose( bool aSuppress ) { m_suppressActivateOnClose = aSuppress; }
124
125 int AddTab( const wxString& aKey, const wxString& aLabel, bool aAsPreview );
126 void CloseTab( int aIdx );
127 void CloseOthers( int aKeepIdx );
128 void CloseToRight( int aIdx );
129 void CloseAll();
130
134 void MarkModified( int aIdx, bool aModified );
135
139 void PromoteTab( int aIdx );
140
141 void SelectTab( int aIdx );
142 void AdvanceTab( bool aForward );
143 int GetActiveTab() const;
144 int FindTab( const wxString& aKey ) const;
145 void RefreshTabLabels();
146
150 void RenameTab( const wxString& aOldKey, const wxString& aNewKey, const wxString& aNewLabel );
151
152 const EDITOR_TABS_MODEL& Model() const { return m_model; }
153
159
160private:
161 void onPageChanged( wxAuiNotebookEvent& aEvent );
162 void onPageClose( wxAuiNotebookEvent& aEvent );
163 void onTabRightDown( wxAuiNotebookEvent& aEvent );
164 void onTabDClick( wxMouseEvent& aEvent );
165 void onContextMenu( wxCommandEvent& aEvent );
166
170 void closeTabInternal( int aIdx );
171
175 void activateTab( int aIdx );
176
180 void touchMru( const wxString& aKey );
181
185 void forgetMru( const wxString& aKey );
186
190 int indexOfWindow( wxWindow* aWindow ) const;
191
197
203 void bindTabDClick();
204
205 TAB_VISUAL_STATE visualStateForIndex( int aIdx ) const;
206
207 wxAuiNotebook* m_tabs = nullptr;
209
212 wxWindow* m_originalCanvasParent = nullptr;
213
214 wxBoxSizer* m_sizer = nullptr;
216
218 bool m_activating = false;
219
222
224 std::vector<wxWindow*> m_pageWindows;
225
227 std::vector<wxString> m_mru;
228
230};
231
232#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 Rename(const wxString &aOldKey, const wxString &aNewKey)
Re-key the entry for aOldKey to aNewKey, keeping its position and state.
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 RenameTab(const wxString &aOldKey, const wxString &aNewKey, const wxString &aNewLabel)
Re-key the tab aOldKey to aNewKey and relabel it to aNewLabel.
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 ...
EDITOR_TABS_MODEL & MutableModel()
Mutable access to the dirty/preview model for hosts that clear several tabs at once and repaint the s...
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...