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.

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