You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

712 lines
21 KiB

* KIWAY Milestone A): Make major modules into DLL/DSOs. ! The initial testing of this commit should be done using a Debug build so that all the wxASSERT()s are enabled. Also, be sure and keep enabled the USE_KIWAY_DLLs option. The tree won't likely build without it. Turning it off is senseless anyways. If you want stable code, go back to a prior version, the one tagged with "stable". * Relocate all functionality out of the wxApp derivative into more finely targeted purposes: a) DLL/DSO specific b) PROJECT specific c) EXE or process specific d) configuration file specific data e) configuration file manipulations functions. All of this functionality was blended into an extremely large wxApp derivative and that was incompatible with the desire to support multiple concurrently loaded DLL/DSO's ("KIFACE")s and multiple concurrently open projects. An amazing amount of organization come from simply sorting each bit of functionality into the proper box. * Switch to wxConfigBase from wxConfig everywhere except instantiation. * Add classes KIWAY, KIFACE, KIFACE_I, SEARCH_STACK, PGM_BASE, PGM_KICAD, PGM_SINGLE_TOP, * Remove "Return" prefix on many function names. * Remove obvious comments from CMakeLists.txt files, and from else() and endif()s. * Fix building boost for use in a DSO on linux. * Remove some of the assumptions in the CMakeLists.txt files that windows had to be the host platform when building windows binaries. * Reduce the number of wxStrings being constructed at program load time via static construction. * Pass wxConfigBase* to all SaveSettings() and LoadSettings() functions so that these functions are useful even when the wxConfigBase comes from another source, as is the case in the KICAD_MANAGER_FRAME. * Move the setting of the KIPRJMOD environment variable into class PROJECT, so that it can be moved into a project variable soon, and out of FP_LIB_TABLE. * Add the KIWAY_PLAYER which is associated with a particular PROJECT, and all its child wxFrames and wxDialogs now have a Kiway() member function which returns a KIWAY& that that window tree branch is in support of. This is like wxWindows DNA in that child windows get this member with proper value at time of construction. * Anticipate some of the needs for milestones B) and C) and make code adjustments now in an effort to reduce work in those milestones. * No testing has been done for python scripting, since milestone C) has that being largely reworked and re-thought-out.
12 years ago
* KIWAY Milestone A): Make major modules into DLL/DSOs. ! The initial testing of this commit should be done using a Debug build so that all the wxASSERT()s are enabled. Also, be sure and keep enabled the USE_KIWAY_DLLs option. The tree won't likely build without it. Turning it off is senseless anyways. If you want stable code, go back to a prior version, the one tagged with "stable". * Relocate all functionality out of the wxApp derivative into more finely targeted purposes: a) DLL/DSO specific b) PROJECT specific c) EXE or process specific d) configuration file specific data e) configuration file manipulations functions. All of this functionality was blended into an extremely large wxApp derivative and that was incompatible with the desire to support multiple concurrently loaded DLL/DSO's ("KIFACE")s and multiple concurrently open projects. An amazing amount of organization come from simply sorting each bit of functionality into the proper box. * Switch to wxConfigBase from wxConfig everywhere except instantiation. * Add classes KIWAY, KIFACE, KIFACE_I, SEARCH_STACK, PGM_BASE, PGM_KICAD, PGM_SINGLE_TOP, * Remove "Return" prefix on many function names. * Remove obvious comments from CMakeLists.txt files, and from else() and endif()s. * Fix building boost for use in a DSO on linux. * Remove some of the assumptions in the CMakeLists.txt files that windows had to be the host platform when building windows binaries. * Reduce the number of wxStrings being constructed at program load time via static construction. * Pass wxConfigBase* to all SaveSettings() and LoadSettings() functions so that these functions are useful even when the wxConfigBase comes from another source, as is the case in the KICAD_MANAGER_FRAME. * Move the setting of the KIPRJMOD environment variable into class PROJECT, so that it can be moved into a project variable soon, and out of FP_LIB_TABLE. * Add the KIWAY_PLAYER which is associated with a particular PROJECT, and all its child wxFrames and wxDialogs now have a Kiway() member function which returns a KIWAY& that that window tree branch is in support of. This is like wxWindows DNA in that child windows get this member with proper value at time of construction. * Anticipate some of the needs for milestones B) and C) and make code adjustments now in an effort to reduce work in those milestones. * No testing has been done for python scripting, since milestone C) has that being largely reworked and re-thought-out.
12 years ago
  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-2022 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. #include <dialog_shim.h>
  25. #include <ignore.h>
  26. #include <kiway_player.h>
  27. #include <kiway.h>
  28. #include <pgm_base.h>
  29. #include <tool/tool_manager.h>
  30. #include <kiplatform/ui.h>
  31. #include <wx/display.h>
  32. #include <wx/evtloop.h>
  33. #include <wx/app.h>
  34. #include <wx/event.h>
  35. #include <wx/grid.h>
  36. #include <wx/bmpbuttn.h>
  37. #include <wx/textctrl.h>
  38. #include <wx/stc/stc.h>
  39. #include <algorithm>
  40. /// Toggle a window's "enable" status to disabled, then enabled on destruction.
  41. class WDO_ENABLE_DISABLE
  42. {
  43. wxWindow* m_win;
  44. public:
  45. WDO_ENABLE_DISABLE( wxWindow* aWindow ) :
  46. m_win( aWindow )
  47. {
  48. if( m_win )
  49. m_win->Disable();
  50. }
  51. ~WDO_ENABLE_DISABLE()
  52. {
  53. if( m_win )
  54. {
  55. m_win->Enable();
  56. m_win->SetFocus(); // let's focus back on the parent window
  57. }
  58. }
  59. };
  60. BEGIN_EVENT_TABLE( DIALOG_SHIM, wxDialog )
  61. // If dialog has a grid and the grid has an active cell editor
  62. // Esc key closes cell editor, otherwise Esc key closes the dialog.
  63. EVT_GRID_EDITOR_SHOWN( DIALOG_SHIM::OnGridEditorShown )
  64. EVT_GRID_EDITOR_HIDDEN( DIALOG_SHIM::OnGridEditorHidden )
  65. EVT_CHAR_HOOK( DIALOG_SHIM::OnCharHook )
  66. END_EVENT_TABLE()
  67. DIALOG_SHIM::DIALOG_SHIM( wxWindow* aParent, wxWindowID id, const wxString& title,
  68. const wxPoint& pos, const wxSize& size, long style,
  69. const wxString& name ) :
  70. wxDialog( aParent, id, title, pos, size, style, name ),
  71. KIWAY_HOLDER( nullptr, KIWAY_HOLDER::DIALOG ),
  72. m_units( EDA_UNITS::MILLIMETRES ),
  73. m_useCalculatedSize( false ),
  74. m_firstPaintEvent( true ),
  75. m_initialFocusTarget( nullptr ),
  76. m_qmodal_loop( nullptr ),
  77. m_qmodal_showing( false ),
  78. m_qmodal_parent_disabler( nullptr ),
  79. m_parentFrame( nullptr )
  80. {
  81. KIWAY_HOLDER* kiwayHolder = nullptr;
  82. m_initialSize = size;
  83. if( aParent )
  84. {
  85. kiwayHolder = dynamic_cast<KIWAY_HOLDER*>( aParent );
  86. while( !kiwayHolder && aParent->GetParent() )
  87. {
  88. aParent = aParent->GetParent();
  89. kiwayHolder = dynamic_cast<KIWAY_HOLDER*>( aParent );
  90. }
  91. }
  92. // Inherit units from parent
  93. if( kiwayHolder && kiwayHolder->GetType() == KIWAY_HOLDER::FRAME )
  94. m_units = static_cast<EDA_BASE_FRAME*>( kiwayHolder )->GetUserUnits();
  95. else if( kiwayHolder && kiwayHolder->GetType() == KIWAY_HOLDER::DIALOG )
  96. m_units = static_cast<DIALOG_SHIM*>( kiwayHolder )->GetUserUnits();
  97. // Don't mouse-warp after a dialog run from the context menu
  98. if( kiwayHolder && kiwayHolder->GetType() == KIWAY_HOLDER::FRAME )
  99. {
  100. m_parentFrame = static_cast<EDA_BASE_FRAME*>( kiwayHolder );
  101. TOOL_MANAGER* toolMgr = m_parentFrame->GetToolManager();
  102. if( toolMgr && toolMgr->IsContextMenuActive() )
  103. toolMgr->VetoContextMenuMouseWarp();
  104. }
  105. // Set up the message bus
  106. if( kiwayHolder )
  107. SetKiway( this, &kiwayHolder->Kiway() );
  108. if( HasKiway() )
  109. Kiway().SetBlockingDialog( this );
  110. Bind( wxEVT_CLOSE_WINDOW, &DIALOG_SHIM::OnCloseWindow, this );
  111. Bind( wxEVT_BUTTON, &DIALOG_SHIM::OnButton, this );
  112. #ifdef __WINDOWS__
  113. // On Windows, the app top windows can be brought to the foreground (at least temporarily)
  114. // in certain circumstances such as when calling an external tool in Eeschema BOM generation.
  115. // So set the parent frame (if exists) to top window to avoid this annoying behavior.
  116. if( kiwayHolder && kiwayHolder->GetType() == KIWAY_HOLDER::FRAME )
  117. Pgm().App().SetTopWindow( (EDA_BASE_FRAME*) kiwayHolder );
  118. #endif
  119. Connect( wxEVT_PAINT, wxPaintEventHandler( DIALOG_SHIM::OnPaint ) );
  120. }
  121. DIALOG_SHIM::~DIALOG_SHIM()
  122. {
  123. // if the dialog is quasi-modal, this will end its event loop
  124. if( IsQuasiModal() )
  125. EndQuasiModal( wxID_CANCEL );
  126. if( HasKiway() )
  127. Kiway().SetBlockingDialog( nullptr );
  128. if( m_qmodal_parent_disabler )
  129. delete m_qmodal_parent_disabler; // usually NULL by now
  130. }
  131. void DIALOG_SHIM::finishDialogSettings()
  132. {
  133. // must be called from the constructor of derived classes,
  134. // when all widgets are initialized, and therefore their size fixed
  135. // SetSizeHints fixes the minimal size of sizers in the dialog
  136. // (SetSizeHints calls Fit(), so no need to call it)
  137. GetSizer()->SetSizeHints( this );
  138. }
  139. void DIALOG_SHIM::setSizeInDU( int x, int y )
  140. {
  141. wxSize sz( x, y );
  142. SetSize( ConvertDialogToPixels( sz ) );
  143. }
  144. int DIALOG_SHIM::horizPixelsFromDU( int x ) const
  145. {
  146. wxSize sz( x, 0 );
  147. return ConvertDialogToPixels( sz ).x;
  148. }
  149. int DIALOG_SHIM::vertPixelsFromDU( int y ) const
  150. {
  151. wxSize sz( 0, y );
  152. return ConvertDialogToPixels( sz ).y;
  153. }
  154. // our hashtable is an implementation secret, don't need or want it in a header file
  155. #include <hashtables.h>
  156. #include <typeinfo>
  157. static std::unordered_map<std::string, wxRect> class_map;
  158. void DIALOG_SHIM::SetPosition( const wxPoint& aNewPosition )
  159. {
  160. wxDialog::SetPosition( aNewPosition );
  161. // Now update the stored position:
  162. const char* hash_key;
  163. if( m_hash_key.size() )
  164. {
  165. // a special case like EDA_LIST_DIALOG, which has multiple uses.
  166. hash_key = m_hash_key.c_str();
  167. }
  168. else
  169. {
  170. hash_key = typeid(*this).name();
  171. }
  172. std::unordered_map<std::string, wxRect>::iterator it = class_map.find( hash_key );
  173. if( it == class_map.end() )
  174. return;
  175. wxRect rect = it->second;
  176. rect.SetPosition( aNewPosition );
  177. class_map[ hash_key ] = rect;
  178. }
  179. bool DIALOG_SHIM::Show( bool show )
  180. {
  181. bool ret;
  182. const char* hash_key;
  183. if( m_hash_key.size() )
  184. {
  185. // a special case like EDA_LIST_DIALOG, which has multiple uses.
  186. hash_key = m_hash_key.c_str();
  187. }
  188. else
  189. {
  190. hash_key = typeid(*this).name();
  191. }
  192. // Show or hide the window. If hiding, save current position and size.
  193. // If showing, use previous position and size.
  194. if( show )
  195. {
  196. #ifndef __WINDOWS__
  197. wxDialog::Raise(); // Needed on OS X and some other window managers (i.e. Unity)
  198. #endif
  199. ret = wxDialog::Show( show );
  200. // classname is key, returns a zeroed-out default wxRect if none existed before.
  201. wxRect savedDialogRect = class_map[ hash_key ];
  202. if( savedDialogRect.GetSize().x != 0 && savedDialogRect.GetSize().y != 0 )
  203. {
  204. if( m_useCalculatedSize )
  205. {
  206. SetSize( savedDialogRect.GetPosition().x, savedDialogRect.GetPosition().y,
  207. wxDialog::GetSize().x, wxDialog::GetSize().y, 0 );
  208. }
  209. else
  210. {
  211. SetSize( savedDialogRect.GetPosition().x, savedDialogRect.GetPosition().y,
  212. std::max( wxDialog::GetSize().x, savedDialogRect.GetSize().x ),
  213. std::max( wxDialog::GetSize().y, savedDialogRect.GetSize().y ),
  214. 0 );
  215. }
  216. }
  217. else if( m_initialSize != wxDefaultSize )
  218. SetSize( m_initialSize );
  219. // Be sure that the dialog appears in a visible area
  220. // (the dialog position might have been stored at the time when it was
  221. // shown on another display)
  222. if( wxDisplay::GetFromWindow( this ) == wxNOT_FOUND )
  223. Centre();
  224. }
  225. else
  226. {
  227. // Save the dialog's position & size before hiding, using classname as key
  228. class_map[ hash_key ] = wxRect( wxDialog::GetPosition(), wxDialog::GetSize() );
  229. #ifdef __WXMAC__
  230. if ( m_eventLoop )
  231. m_eventLoop->Exit( GetReturnCode() ); // Needed for APP-MODAL dlgs on OSX
  232. #endif
  233. ret = wxDialog::Show( show );
  234. }
  235. return ret;
  236. }
  237. void DIALOG_SHIM::resetSize()
  238. {
  239. const char* hash_key;
  240. if( m_hash_key.size() )
  241. {
  242. // a special case like EDA_LIST_DIALOG, which has multiple uses.
  243. hash_key = m_hash_key.c_str();
  244. }
  245. else
  246. {
  247. hash_key = typeid(*this).name();
  248. }
  249. std::unordered_map<std::string, wxRect>::iterator it = class_map.find( hash_key );
  250. if( it == class_map.end() )
  251. return;
  252. wxRect rect = it->second;
  253. rect.SetSize( wxSize( 0, 0 ) );
  254. class_map[ hash_key ] = rect;
  255. }
  256. bool DIALOG_SHIM::Enable( bool enable )
  257. {
  258. // so we can do logging of this state change:
  259. return wxDialog::Enable( enable );
  260. }
  261. // Recursive descent doing a SelectAll() in wxTextCtrls.
  262. // MacOS User Interface Guidelines state that when tabbing to a text control all its
  263. // text should be selected. Since wxWidgets fails to implement this, we do it here.
  264. static void selectAllInTextCtrls( wxWindowList& children )
  265. {
  266. for( wxWindow* child : children )
  267. {
  268. if( wxTextCtrl* childTextCtrl = dynamic_cast<wxTextCtrl*>( child ) )
  269. {
  270. // We don't currently run this on GTK because some window managers don't hide the
  271. // selection in non-active controls, and other window managers do the selection
  272. // automatically anyway.
  273. #if defined( __WXMAC__ ) || defined( __WXMSW__ )
  274. if( !childTextCtrl->GetStringSelection().IsEmpty() )
  275. {
  276. // Respect an existing selection
  277. }
  278. else
  279. {
  280. childTextCtrl->SelectAll();
  281. }
  282. #else
  283. ignore_unused( childTextCtrl );
  284. #endif
  285. }
  286. else if( wxStyledTextCtrl* scintilla = dynamic_cast<wxStyledTextCtrl*>( child ) )
  287. {
  288. if( !scintilla->GetSelectedText().IsEmpty() )
  289. {
  290. // Respect an existing selection
  291. }
  292. else
  293. {
  294. scintilla->SelectAll();
  295. }
  296. }
  297. #ifdef __WXMAC__
  298. // Temp hack for square (looking) buttons on OSX. Will likely be made redundant
  299. // by the image store....
  300. else if( dynamic_cast<wxBitmapButton*>( child ) != nullptr )
  301. {
  302. wxSize minSize( 29, 27 );
  303. wxRect rect = child->GetRect();
  304. child->ConvertDialogToPixels( minSize );
  305. rect.Inflate( std::max( 0, minSize.x - rect.GetWidth() ),
  306. std::max( 0, minSize.y - rect.GetHeight() ) );
  307. child->SetMinSize( rect.GetSize() );
  308. child->SetSize( rect );
  309. }
  310. #endif
  311. else
  312. {
  313. selectAllInTextCtrls( child->GetChildren() );
  314. }
  315. }
  316. }
  317. void DIALOG_SHIM::OnPaint( wxPaintEvent &event )
  318. {
  319. if( m_firstPaintEvent )
  320. {
  321. KIPLATFORM::UI::FixupCancelButtonCmdKeyCollision( this );
  322. selectAllInTextCtrls( GetChildren() );
  323. if( m_initialFocusTarget )
  324. KIPLATFORM::UI::ForceFocus( m_initialFocusTarget );
  325. else
  326. KIPLATFORM::UI::ForceFocus( this ); // Focus the dialog itself
  327. m_firstPaintEvent = false;
  328. }
  329. event.Skip();
  330. }
  331. void DIALOG_SHIM::OnModify()
  332. {
  333. if( !GetTitle().StartsWith( "*" ) )
  334. SetTitle( "*" + GetTitle() );
  335. }
  336. /*
  337. Quasi-Modal Mode Explained:
  338. The gtk calls in wxDialog::ShowModal() cause event routing problems if that
  339. modal dialog then tries to use KIWAY_PLAYER::ShowModal(). The latter shows up
  340. and mostly works but does not respond to the window decoration close button.
  341. There is no way to get around this without reversing the gtk calls temporarily.
  342. Quasi-Modal mode is our own almost modal mode which disables only the parent
  343. of the DIALOG_SHIM, leaving other frames operable and while staying captured in the
  344. nested event loop. This avoids the gtk calls and leaves event routing pure
  345. and sufficient to operate the KIWAY_PLAYER::ShowModal() properly. When using
  346. ShowQuasiModal() you have to use EndQuasiModal() in your dialogs and not
  347. EndModal(). There is also IsQuasiModal() but its value can only be true
  348. when the nested event loop is active. Do not mix the modal and quasi-modal
  349. functions. Use one set or the other.
  350. You might find this behavior preferable over a pure modal mode, and it was said
  351. that only the Mac has this natively, but now other platforms have something
  352. similar. You CAN use it anywhere for any dialog. But you MUST use it when
  353. you want to use KIWAY_PLAYER::ShowModal() from a dialog event.
  354. */
  355. int DIALOG_SHIM::ShowQuasiModal()
  356. {
  357. // This is an exception safe way to zero a pointer before returning.
  358. // Yes, even though DismissModal() clears this first normally, this is
  359. // here in case there's an exception before the dialog is dismissed.
  360. struct NULLER
  361. {
  362. void*& m_what;
  363. NULLER( void*& aPtr ) : m_what( aPtr ) {}
  364. ~NULLER() { m_what = nullptr; } // indeed, set it to NULL on destruction
  365. } clear_this( (void*&) m_qmodal_loop );
  366. // release the mouse if it's currently captured as the window having it
  367. // will be disabled when this dialog is shown -- but will still keep the
  368. // capture making it impossible to do anything in the modal dialog itself
  369. wxWindow* win = wxWindow::GetCapture();
  370. if( win )
  371. win->ReleaseMouse();
  372. // Get the optimal parent
  373. wxWindow* parent = GetParentForModalDialog( GetParent(), GetWindowStyle() );
  374. wxASSERT_MSG( !m_qmodal_parent_disabler, wxT( "Caller using ShowQuasiModal() twice on same "
  375. "window?" ) );
  376. // quasi-modal: disable only my "optimal" parent
  377. m_qmodal_parent_disabler = new WDO_ENABLE_DISABLE( parent );
  378. // Apple in its infinite wisdom will raise a disabled window before even passing
  379. // us the event, so we have no way to stop it. Instead, we must set an order on
  380. // the windows so that the quasi-modal will be pushed in front of the disabled
  381. // window when it is raised.
  382. KIPLATFORM::UI::ReparentQuasiModal( this );
  383. Show( true );
  384. m_qmodal_showing = true;
  385. WX_EVENT_LOOP event_loop;
  386. m_qmodal_loop = &event_loop;
  387. event_loop.Run();
  388. m_qmodal_showing = false;
  389. return GetReturnCode();
  390. }
  391. void DIALOG_SHIM::EndQuasiModal( int retCode )
  392. {
  393. // Hook up validator and transfer data from controls handling so quasi-modal dialogs
  394. // handle validation in the same way as other dialogs.
  395. if( ( retCode == wxID_OK ) && ( !Validate() || !TransferDataFromWindow() ) )
  396. return;
  397. SetReturnCode( retCode );
  398. if( !IsQuasiModal() )
  399. {
  400. wxFAIL_MSG( wxT( "Either DIALOG_SHIM::EndQuasiModal was called twice, or ShowQuasiModal"
  401. "wasn't called" ) );
  402. return;
  403. }
  404. if( m_qmodal_loop )
  405. {
  406. if( m_qmodal_loop->IsRunning() )
  407. m_qmodal_loop->Exit( 0 );
  408. else
  409. m_qmodal_loop->ScheduleExit( 0 );
  410. m_qmodal_loop = nullptr;
  411. }
  412. delete m_qmodal_parent_disabler;
  413. m_qmodal_parent_disabler = nullptr;
  414. Show( false );
  415. }
  416. void DIALOG_SHIM::OnCloseWindow( wxCloseEvent& aEvent )
  417. {
  418. if( IsQuasiModal() )
  419. {
  420. EndQuasiModal( wxID_CANCEL );
  421. return;
  422. }
  423. // This is mandatory to allow wxDialogBase::OnCloseWindow() to be called.
  424. aEvent.Skip();
  425. }
  426. void DIALOG_SHIM::OnButton( wxCommandEvent& aEvent )
  427. {
  428. const int id = aEvent.GetId();
  429. // If we are pressing a button to exit, we need to enable the escapeID
  430. // otherwise the dialog does not process cancel
  431. if( id == wxID_CANCEL )
  432. SetEscapeId( wxID_ANY );
  433. if( IsQuasiModal() )
  434. {
  435. if( id == GetAffirmativeId() )
  436. {
  437. EndQuasiModal( id );
  438. }
  439. else if( id == wxID_APPLY )
  440. {
  441. // Dialogs that provide Apply buttons should make sure data is valid before
  442. // allowing a transfer, as there is no other way to indicate failure
  443. // (i.e. the dialog can't refuse to close as it might with OK, because it
  444. // isn't closing anyway)
  445. if( Validate() )
  446. {
  447. ignore_unused( TransferDataFromWindow() );
  448. }
  449. }
  450. else if( id == GetEscapeId() || (id == wxID_CANCEL && GetEscapeId() == wxID_ANY) )
  451. {
  452. EndQuasiModal( wxID_CANCEL );
  453. }
  454. else // not a standard button
  455. {
  456. aEvent.Skip();
  457. }
  458. return;
  459. }
  460. // This is mandatory to allow wxDialogBase::OnButton() to be called.
  461. aEvent.Skip();
  462. }
  463. void DIALOG_SHIM::OnCharHook( wxKeyEvent& aEvt )
  464. {
  465. if( aEvt.GetKeyCode() == 'U' && aEvt.GetModifiers() == wxMOD_CONTROL )
  466. {
  467. if( m_parentFrame )
  468. {
  469. m_parentFrame->ToggleUserUnits();
  470. return;
  471. }
  472. }
  473. // shift-return (Mac default) or Ctrl-Return (GTK) for OK
  474. else if( aEvt.GetKeyCode() == WXK_RETURN && ( aEvt.ShiftDown() || aEvt.ControlDown() ) )
  475. {
  476. wxPostEvent( this, wxCommandEvent( wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK ) );
  477. return;
  478. }
  479. else if( aEvt.GetKeyCode() == WXK_TAB && !aEvt.ControlDown() )
  480. {
  481. wxWindow* currentWindow = wxWindow::FindFocus();
  482. int currentIdx = -1;
  483. int delta = aEvt.ShiftDown() ? -1 : 1;
  484. auto advance =
  485. [&]( int& idx )
  486. {
  487. // Wrap-around modulus
  488. int size = m_tabOrder.size();
  489. idx = ( ( idx + delta ) % size + size ) % size;
  490. };
  491. for( size_t i = 0; i < m_tabOrder.size(); ++i )
  492. {
  493. if( m_tabOrder[i] == currentWindow )
  494. {
  495. currentIdx = (int) i;
  496. break;
  497. }
  498. }
  499. if( currentIdx >= 0 )
  500. {
  501. advance( currentIdx );
  502. //todo: We don't currently have non-textentry dialog boxes but this will break if
  503. // we add them.
  504. #ifdef __APPLE__
  505. while( dynamic_cast<wxTextEntry*>( m_tabOrder[ currentIdx ] ) == nullptr )
  506. advance( currentIdx );
  507. #endif
  508. m_tabOrder[ currentIdx ]->SetFocus();
  509. return;
  510. }
  511. }
  512. aEvt.Skip();
  513. }
  514. void DIALOG_SHIM::OnGridEditorShown( wxGridEvent& event )
  515. {
  516. SetEscapeId( wxID_NONE );
  517. event.Skip();
  518. }
  519. void DIALOG_SHIM::OnGridEditorHidden( wxGridEvent& event )
  520. {
  521. SetEscapeId( wxID_ANY );
  522. event.Skip();
  523. }
  524. static void recursiveDescent( wxSizer* aSizer, std::map<int, wxString>& aLabels )
  525. {
  526. wxStdDialogButtonSizer* sdbSizer = dynamic_cast<wxStdDialogButtonSizer*>( aSizer );
  527. auto setupButton =
  528. [&]( wxButton* aButton )
  529. {
  530. if( aLabels.count( aButton->GetId() ) > 0 )
  531. {
  532. aButton->SetLabel( aLabels[ aButton->GetId() ] );
  533. }
  534. else
  535. {
  536. // wxWidgets has an uneven track record when the language is changed on
  537. // the fly so we set them even when they don't appear in the label map
  538. switch( aButton->GetId() )
  539. {
  540. case wxID_OK: aButton->SetLabel( _( "&OK" ) ); break;
  541. case wxID_CANCEL: aButton->SetLabel( _( "&Cancel" ) ); break;
  542. case wxID_YES: aButton->SetLabel( _( "&Yes" ) ); break;
  543. case wxID_NO: aButton->SetLabel( _( "&No" ) ); break;
  544. case wxID_APPLY: aButton->SetLabel( _( "&Apply" ) ); break;
  545. case wxID_SAVE: aButton->SetLabel( _( "&Save" ) ); break;
  546. case wxID_HELP: aButton->SetLabel( _( "&Help" ) ); break;
  547. case wxID_CONTEXT_HELP: aButton->SetLabel( _( "&Help" ) ); break;
  548. }
  549. }
  550. };
  551. if( sdbSizer )
  552. {
  553. if( sdbSizer->GetAffirmativeButton() )
  554. setupButton( sdbSizer->GetAffirmativeButton() );
  555. if( sdbSizer->GetApplyButton() )
  556. setupButton( sdbSizer->GetApplyButton() );
  557. if( sdbSizer->GetNegativeButton() )
  558. setupButton( sdbSizer->GetNegativeButton() );
  559. if( sdbSizer->GetCancelButton() )
  560. setupButton( sdbSizer->GetCancelButton() );
  561. if( sdbSizer->GetHelpButton() )
  562. setupButton( sdbSizer->GetHelpButton() );
  563. sdbSizer->Layout();
  564. if( sdbSizer->GetAffirmativeButton() )
  565. sdbSizer->GetAffirmativeButton()->SetDefault();
  566. }
  567. for( wxSizerItem* item : aSizer->GetChildren() )
  568. {
  569. if( item->GetSizer() )
  570. recursiveDescent( item->GetSizer(), aLabels );
  571. }
  572. }
  573. void DIALOG_SHIM::SetupStandardButtons( std::map<int, wxString> aLabels )
  574. {
  575. recursiveDescent( GetSizer(), aLabels );
  576. }