KiCad PCB EDA Suite
Loading...
Searching...
No Matches
dialog_shim.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 (C) 2012 SoftPLC Corporation, Dick Hollenbeck <[email protected]>
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21#ifndef DIALOG_SHIM_
22#define DIALOG_SHIM_
23
24#include <kicommon.h>
25#include <eda_units.h>
26#include <kiway_holder.h>
27#include <wx/dialog.h>
28#include <map>
29#include <optional>
30#include <set>
31#include <vector>
32#include <wx/variant.h>
33#include <core/raii.h>
34
35class EDA_BASE_FRAME;
36class UNIT_BINDER;
37
38class wxGridEvent;
39class wxGUIEventLoop;
40class wxInitDialogEvent;
41class wxSpinEvent;
42class wxSpinDoubleEvent;
43class wxStyledTextEvent;
44class wxPropertyGridEvent;
45class wxDataViewEvent;
46
47
60KICOMMON_API wxRect ClampRectToDisplay( const wxRect& aRect, const wxRect& aClientArea );
61
62
80class KICOMMON_API DIALOG_SHIM : public wxDialog, public KIWAY_HOLDER
81{
82public:
83 DIALOG_SHIM( wxWindow* aParent, wxWindowID id, const wxString& title,
84 const wxPoint& pos = wxDefaultPosition,
85 const wxSize& size = wxDefaultSize,
86 long style = wxDEFAULT_FRAME_STYLE | wxRESIZE_BORDER,
87 const wxString& name = wxDialogNameStr );
88
90
95 void SetInitialFocus( wxWindow* aWindow )
96 {
97 m_initialFocusTarget = aWindow;
98 }
99
100 int ShowModal() override;
101
102 int ShowQuasiModal(); // disable only the parent window, otherwise modal.
103
104 void EndQuasiModal( int retCode ); // End quasi-modal mode
105
106 bool IsQuasiModal() const { return m_qmodal_showing; }
107
108 // A quasi-modal dialog disables its parent window. Sadly this disabling is more extreme
109 // than wxWidgets' normal modal dialog disabling, and prevents things like hotkey Cut/Copy/
110 // Paste from working in search controls in standard file dialogs. So when we put up a modal
111 // dialog in front of a quasi-modal, we suspend the quasi-modal dialog parent window
112 // disabling, causing us to fall back to the normal modal dialog parent window disabling.
113 void PrepareForModalSubDialog();
114 void CleanupAfterModalSubDialog();
115
116 bool Show( bool show ) override;
117
118 bool Enable( bool enable ) override;
119
120 void OnPaint( wxPaintEvent &event );
121
122 void OnModify();
123 void ClearModify();
124
129 void SetPosition( const wxPoint& aNewPosition );
130
132 {
133 return m_units;
134 }
135
136 void SelectAllInTextCtrls( wxWindowList& children );
137
138 void SetupStandardButtons( std::map<int, wxString> aLabels = {} );
139
140 static bool IsCtrl( int aChar, const wxKeyEvent& e )
141 {
142 return e.GetKeyCode() == aChar && e.ControlDown() && !e.AltDown() &&
143 !e.ShiftDown() && !e.MetaDown();
144 }
145
146 static bool IsShiftCtrl( int aChar, const wxKeyEvent& e )
147 {
148 return e.GetKeyCode() == aChar && e.ControlDown() && !e.AltDown() &&
149 e.ShiftDown() && !e.MetaDown();
150 }
151
157 void LoadControlState();
158
163 void ReloadSashPositions();
164
171 void SaveControlState( const std::optional<wxPoint>& aPosition = std::nullopt );
172
177 void OptOut( wxWindow* aWindow );
178
186 void ExcludeFromControlUndoRedo( wxWindow* aWindow );
187
194 void RegisterUnitBinder( UNIT_BINDER* aUnitBinder, wxWindow* aWindow );
195
205 void UnregisterUnitBinder( UNIT_BINDER* aUnitBinder );
206
219 void EndDialogShim( int aReturnCode );
220
221protected:
232 void finishDialogSettings();
233
243 void clampToWorkArea();
244
250 void setSizeInDU( int x, int y );
251
256 int horizPixelsFromDU( int x ) const;
257
262 int vertPixelsFromDU( int y ) const;
263
271 void resetSize();
272
273 virtual void OnCharHook( wxKeyEvent& aEvt );
274
282 virtual void TearDownQuasiModal() {}
283
291 void recordControlChange( wxWindow* aCtrl );
292
293private:
298 void OnCloseWindow( wxCloseEvent& aEvent );
299 void OnActivate( wxActivateEvent& aEvent );
300
305 void forceInitialFocus();
306
307 void OnSize( wxSizeEvent& aEvent );
308 void OnMove( wxMoveEvent& aEvent );
309
314 void OnButton( wxCommandEvent& aEvent );
315
316 void onChildSetFocus( wxFocusEvent& aEvent );
317
318 void onInitDialog( wxInitDialogEvent& aEvent );
319
329 void focusParentCanvas( bool aDeferUntilFrameActive = false );
330
331 std::string generateKey( const wxWindow* aWin ) const;
332
333 void registerUndoRedoHandlers( wxWindowList& aChildren );
334
340 void flushPendingControlChanges();
341
342 void onCommandEvent( wxCommandEvent& aEvent );
343 void onSpinEvent( wxSpinEvent& aEvent );
344 void onSpinDoubleEvent( wxSpinDoubleEvent& aEvent );
345 void onStyledTextChanged( wxStyledTextEvent& aEvent );
346 void onGridCellChanged( wxGridEvent& aEvent );
347 void onPropertyGridChanged( wxPropertyGridEvent& aEvent );
348 void onDataViewListChanged( wxDataViewEvent& aEvent );
349 void doUndo();
350 void doRedo();
351 wxVariant getControlValue( wxWindow* aCtrl );
352 void setControlValue( wxWindow* aCtrl, const wxVariant& aValue );
353
355
356protected:
357 EDA_UNITS m_units; // userUnits for display and parsing
358 std::string m_hash_key; // optional custom key for persistence
359
360 // The following disables the storing of a user size. It is used primarily for dialogs
361 // with conditional content which don't need user sizing.
363
364 // On MacOS (at least) SetFocus() calls made in the constructor will fail because a
365 // window that isn't yet visible will return false to AcceptsFocus(). So we must delay
366 // the initial-focus SetFocus() call to the first paint event.
370
371 wxGUIEventLoop* m_qmodal_loop; // points to nested event_loop, NULL means not qmodal
372 // and dismissed
375
377
378 std::vector<wxWindow*> m_tabOrder;
379
380 // The size asked by the caller, used the first time the dialog is created
384
385 // Used to support first-esc-cancels-edit logic
386 std::map<wxWindow*, wxString> m_beforeEditValues;
387 std::map<wxWindow*, UNIT_BINDER*> m_unitBinders;
388
390 {
391 wxWindow* ctrl;
392 wxVariant before;
393 wxVariant after;
394 };
395
396 std::vector<UNDO_STEP> m_undoStack;
397 std::vector<UNDO_STEP> m_redoStack;
398 std::map<wxWindow*, wxVariant> m_currentValues;
399 std::set<wxWindow*> m_controlsWithPendingChanges;
400 std::set<wxWindow*> m_noControlUndoRedo;
403};
404
405#endif // DIALOG_SHIM_
const char * name
static bool IsShiftCtrl(int aChar, const wxKeyEvent &e)
bool m_handlingUndoRedo
std::set< wxWindow * > m_noControlUndoRedo
std::vector< wxWindow * > m_tabOrder
bool m_qmodal_showing
virtual void TearDownQuasiModal()
Override this method to perform dialog tear down actions not suitable for object dtor.
std::set< wxWindow * > m_controlsWithPendingChanges
static bool IsCtrl(int aChar, const wxKeyEvent &e)
std::vector< UNDO_STEP > m_redoStack
wxGUIEventLoop * m_qmodal_loop
EDA_UNITS m_units
void SetInitialFocus(wxWindow *aWindow)
Sets the window (usually a wxTextCtrl) that should be focused when the dialog is shown.
Definition dialog_shim.h:95
WINDOW_DISABLER * m_qmodal_parent_disabler
std::string m_hash_key
bool m_firstPaintEvent
bool m_userResized
std::map< wxWindow *, wxString > m_beforeEditValues
bool IsQuasiModal() const
bool m_useCalculatedSize
std::map< wxWindow *, UNIT_BINDER * > m_unitBinders
bool m_childReleased
wxWindow * m_initialFocusTarget
DIALOG_SHIM(wxWindow *aParent, wxWindowID id, const wxString &title, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, long style=wxDEFAULT_FRAME_STYLE|wxRESIZE_BORDER, const wxString &name=wxDialogNameStr)
bool m_userPositioned
std::map< wxWindow *, wxVariant > m_currentValues
wxSize m_initialSize
EDA_BASE_FRAME * m_parentFrame
std::vector< UNDO_STEP > m_undoStack
EDA_UNITS GetUserUnits() const
The base frame for deriving all KiCad main window classes.
KIWAY_HOLDER(KIWAY *aKiway, HOLDER_TYPE aType)
Temporarily disable a window, and then re-enable on destruction.
Definition raii.h:89
KICOMMON_API wxRect ClampRectToDisplay(const wxRect &aRect, const wxRect &aClientArea)
Constrain a window rectangle so it fits entirely within a display's client area.
EDA_UNITS
Definition eda_units.h:45
#define KICOMMON_API
Definition kicommon.h:27