KiCad PCB EDA Suite
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 <dick@softplc.com>
5  * Copyright (C) 2012-2019 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, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
25 #ifndef DIALOG_SHIM_
26 #define DIALOG_SHIM_
27 
28 #include <wx/dialog.h>
29 #include <hashtables.h>
30 #include <kiway_player.h>
31 class wxGridEvent;
32 
33 
34 
36 {
37  WINDOW_THAWER( wxWindow* aWindow )
38  {
39  m_window = aWindow;
40  m_freezeCount = 0;
41 
42  while( m_window->IsFrozen() )
43  {
44  m_window->Thaw();
45  m_freezeCount++;
46  }
47  }
48 
50  {
51  while( m_freezeCount > 0 )
52  {
53  m_window->Freeze();
54  m_freezeCount--;
55  }
56  }
57 
58 protected:
59  wxWindow* m_window;
61 };
62 
63 
64 class WDO_ENABLE_DISABLE;
65 class WX_EVENT_LOOP;
66 
67 // These macros are for DIALOG_SHIM only, NOT for KIWAY_PLAYER. KIWAY_PLAYER
68 // has its own support for quasi modal and its platform specific issues are different
69 // than for a wxDialog.
70  #define SHOWQUASIMODAL ShowQuasiModal
71  #define ENDQUASIMODAL EndQuasiModal
72 
73 
84 class DIALOG_SHIM : public wxDialog, public KIWAY_HOLDER
85 {
90  void OnCloseWindow( wxCloseEvent& aEvent );
91 
96  void OnButton( wxCommandEvent& aEvent );
97 
98  void OnCharHook( wxKeyEvent& aEvt );
99 
100 public:
101 
102  DIALOG_SHIM( wxWindow* aParent, wxWindowID id, const wxString& title,
103  const wxPoint& pos = wxDefaultPosition,
104  const wxSize& size = wxDefaultSize,
105  long style = wxDEFAULT_FRAME_STYLE|wxRESIZE_BORDER,
106  const wxString& name = wxDialogNameStr
107  );
108 
109  ~DIALOG_SHIM();
110 
115  void SetInitialFocus( wxWindow* aWindow )
116  {
117  m_initialFocusTarget = aWindow;
118  }
119 
120  int ShowQuasiModal(); // disable only the parent window, otherwise modal.
121 
122  void EndQuasiModal( int retCode ); // End quasi-modal mode
123 
124  bool IsQuasiModal() { return m_qmodal_showing; }
125 
126  bool Show( bool show ) override;
127 
128  bool Enable( bool enable ) override;
129 
130  void OnPaint( wxPaintEvent &event );
131 
132  EDA_UNITS_T GetUserUnits() const { return m_units; }
133 
134  static bool IsCtrl( int aChar, const wxKeyEvent& e )
135  {
136  return e.GetKeyCode() == aChar && e.ControlDown() && !e.AltDown() &&
137  !e.ShiftDown() && !e.MetaDown();
138  }
139 
140  static bool IsShiftCtrl( int aChar, const wxKeyEvent& e )
141  {
142  return e.GetKeyCode() == aChar && e.ControlDown() && !e.AltDown() &&
143  e.ShiftDown() && !e.MetaDown();
144  }
145 
146 protected:
147 
163  void FinishDialogSettings();
164 
170  void SetSizeInDU( int x, int y );
171 
176  int HorizPixelsFromDU( int x );
177 
182  int VertPixelsFromDU( int y );
183 
191  void ResetSize();
192 
193  EDA_UNITS_T m_units; // userUnits for display and parsing
194  std::string m_hash_key; // alternate for class_map when classname re-used
195 
196  // On MacOS (at least) SetFocus() calls made in the constructor will fail because a
197  // window that isn't yet visible will return false to AcceptsFocus(). So we must delay
198  // the initial-focus SetFocus() call to the first paint event.
201 
202  WX_EVENT_LOOP* m_qmodal_loop; // points to nested event_loop, NULL means not qmodal
203  // and dismissed
206 
207  std::vector<wxWindow*> m_tabOrder;
208 
209 private:
210  void OnGridEditorShown( wxGridEvent& event );
211  void OnGridEditorHidden( wxGridEvent& event );
212 
213  DECLARE_EVENT_TABLE()
214 };
215 
216 #endif // DIALOG_SHIM_
bool m_qmodal_showing
Definition: dialog_shim.h:204
Class KIWAY_HOLDER is a mix in class which holds the location of a wxWindow's KIWAY.
Definition: kiway_holder.h:39
#define WX_EVENT_LOOP
Definition: kiway_player.h:41
void OnButton(wxCommandEvent &aEvent)
Properly handle the default button events when in the quasimodal mode when not calling EndQuasiModal ...
std::string m_hash_key
Definition: dialog_shim.h:194
std::vector< wxWindow * > m_tabOrder
Definition: dialog_shim.h:207
void ResetSize()
Clear the existing dialog size and position.
void FinishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
wxWindow * m_initialFocusTarget
Definition: dialog_shim.h:200
Dialog helper object to sit in the inheritance tree between wxDialog and any class written by wxFormB...
Definition: dialog_shim.h:84
void OnGridEditorShown(wxGridEvent &event)
void OnGridEditorHidden(wxGridEvent &event)
void SetInitialFocus(wxWindow *aWindow)
Sets the window (usually a wxTextCtrl) that should be focused when the dialog is shown.
Definition: dialog_shim.h:115
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)
Definition: dialog_shim.cpp:68
bool m_firstPaintEvent
Definition: dialog_shim.h:199
WX_EVENT_LOOP * m_qmodal_loop
Definition: dialog_shim.h:202
void OnCloseWindow(wxCloseEvent &aEvent)
Properly handle the wxCloseEvent when in the quasimodal mode when not calling EndQuasiModal which is ...
WINDOW_THAWER(wxWindow *aWindow)
Definition: dialog_shim.h:37
int ShowQuasiModal()
void SetSizeInDU(int x, int y)
Set the dialog to the given dimensions in "dialog units".
bool IsQuasiModal()
Definition: dialog_shim.h:124
void OnPaint(wxPaintEvent &event)
wxWindow * m_window
Definition: dialog_shim.h:59
bool Show(bool show) override
int VertPixelsFromDU(int y)
Convert an integer number of dialog units to pixels, vertically.
WDO_ENABLE_DISABLE * m_qmodal_parent_disabler
Definition: dialog_shim.h:205
void OnCharHook(wxKeyEvent &aEvt)
void EndQuasiModal(int retCode)
EDA_UNITS_T GetUserUnits() const
Definition: dialog_shim.h:132
const char * name
Definition: DXF_plotter.cpp:61
static bool IsShiftCtrl(int aChar, const wxKeyEvent &e)
Definition: dialog_shim.h:140
Toggle a window's "enable" status to disabled, then enabled on destruction.
Definition: dialog_shim.cpp:35
int HorizPixelsFromDU(int x)
Convert an integer number of dialog units to pixels, horizontally.
bool Enable(bool enable) override
EDA_UNITS_T m_units
Definition: dialog_shim.h:193
static bool IsCtrl(int aChar, const wxKeyEvent &e)
Definition: dialog_shim.h:134
EDA_UNITS_T
Definition: common.h:133