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.

463 lines
14 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
* 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-2018 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 <kiway_player.h>
  26. #include <wx/evtloop.h>
  27. #include <pgm_base.h>
  28. #include <eda_rect.h>
  29. #include <wx/display.h>
  30. /// Toggle a window's "enable" status to disabled, then enabled on destruction.
  31. class WDO_ENABLE_DISABLE
  32. {
  33. wxWindow* m_win;
  34. public:
  35. WDO_ENABLE_DISABLE( wxWindow* aWindow ) :
  36. m_win( aWindow )
  37. {
  38. if( m_win )
  39. m_win->Disable();
  40. }
  41. ~WDO_ENABLE_DISABLE()
  42. {
  43. if( m_win )
  44. {
  45. m_win->Enable();
  46. m_win->SetFocus(); // let's focus back on the parent window
  47. }
  48. }
  49. };
  50. DIALOG_SHIM::DIALOG_SHIM( wxWindow* aParent, wxWindowID id, const wxString& title,
  51. const wxPoint& pos, const wxSize& size, long style, const wxString& name ) :
  52. wxDialog( aParent, id, title, pos, size, style, name ),
  53. KIWAY_HOLDER( nullptr ),
  54. m_units( MILLIMETRES ),
  55. m_firstPaintEvent( true ),
  56. m_initialFocusTarget( nullptr ),
  57. m_qmodal_loop( nullptr ),
  58. m_qmodal_showing( false ),
  59. m_qmodal_parent_disabler( nullptr )
  60. {
  61. KIWAY_HOLDER* kiwayHolder = nullptr;
  62. if( aParent )
  63. {
  64. kiwayHolder = dynamic_cast<KIWAY_HOLDER*>( aParent );
  65. while( !kiwayHolder && aParent->GetParent() )
  66. {
  67. aParent = aParent->GetParent();
  68. kiwayHolder = dynamic_cast<KIWAY_HOLDER*>( aParent );
  69. }
  70. }
  71. if( kiwayHolder )
  72. {
  73. // Inherit units from parent
  74. m_units = kiwayHolder->GetUserUnits();
  75. // Set up the message bus
  76. SetKiway( this, &kiwayHolder->Kiway() );
  77. }
  78. Bind( wxEVT_CLOSE_WINDOW, &DIALOG_SHIM::OnCloseWindow, this );
  79. Bind( wxEVT_BUTTON, &DIALOG_SHIM::OnButton, this );
  80. #ifdef __WINDOWS__
  81. // On Windows, the app top windows can be brought to the foreground
  82. // (at least temporary) in certain circumstances,
  83. // for instance when calling an external tool in Eeschema BOM generation.
  84. // So set the parent KIWAY_PLAYER kicad frame (if exists) to top window
  85. // to avoid this annoying behavior
  86. KIWAY_PLAYER* parent_kiwayplayer = dynamic_cast<KIWAY_PLAYER*>( aParent );
  87. if( parent_kiwayplayer )
  88. Pgm().App().SetTopWindow( parent_kiwayplayer );
  89. #endif
  90. Connect( wxEVT_PAINT, wxPaintEventHandler( DIALOG_SHIM::OnPaint ) );
  91. }
  92. DIALOG_SHIM::~DIALOG_SHIM()
  93. {
  94. // if the dialog is quasi-modal, this will end its event loop
  95. if( IsQuasiModal() )
  96. EndQuasiModal( wxID_CANCEL );
  97. if( m_qmodal_parent_disabler )
  98. delete m_qmodal_parent_disabler; // usually NULL by now
  99. }
  100. void DIALOG_SHIM::FinishDialogSettings()
  101. {
  102. // must be called from the constructor of derived classes,
  103. // when all widgets are initialized, and therefore their size fixed
  104. // SetSizeHints fixes the minimal size of sizers in the dialog
  105. // (SetSizeHints calls Fit(), so no need to call it)
  106. GetSizer()->SetSizeHints( this );
  107. // the default position, when calling the first time the dlg
  108. Center();
  109. }
  110. void DIALOG_SHIM::SetSizeInDU( int x, int y )
  111. {
  112. wxSize sz( x, y );
  113. SetSize( ConvertDialogToPixels( sz ) );
  114. }
  115. int DIALOG_SHIM::HorizPixelsFromDU( int x )
  116. {
  117. wxSize sz( x, 0 );
  118. return ConvertDialogToPixels( sz ).x;
  119. }
  120. int DIALOG_SHIM::VertPixelsFromDU( int y )
  121. {
  122. wxSize sz( 0, y );
  123. return ConvertDialogToPixels( sz ).y;
  124. }
  125. // our hashtable is an implementation secret, don't need or want it in a header file
  126. #include <hashtables.h>
  127. #include <base_struct.h> // EDA_RECT
  128. #include <typeinfo>
  129. static RECT_MAP class_map;
  130. bool DIALOG_SHIM::Show( bool show )
  131. {
  132. bool ret;
  133. const char* hash_key;
  134. if( m_hash_key.size() )
  135. {
  136. // a special case like EDA_LIST_DIALOG, which has multiple uses.
  137. hash_key = m_hash_key.c_str();
  138. }
  139. else
  140. {
  141. hash_key = typeid(*this).name();
  142. }
  143. // Show or hide the window. If hiding, save current position and size.
  144. // If showing, use previous position and size.
  145. if( show )
  146. {
  147. #ifndef __WINDOWS__
  148. wxDialog::Raise(); // Needed on OS X and some other window managers (i.e. Unity)
  149. #endif
  150. ret = wxDialog::Show( show );
  151. // classname is key, returns a zeroed out default EDA_RECT if none existed before.
  152. EDA_RECT savedDialogRect = class_map[ hash_key ];
  153. if( savedDialogRect.GetSize().x != 0 && savedDialogRect.GetSize().y != 0 )
  154. {
  155. SetSize( savedDialogRect.GetPosition().x,
  156. savedDialogRect.GetPosition().y,
  157. std::max( wxDialog::GetSize().x, savedDialogRect.GetSize().x ),
  158. std::max( wxDialog::GetSize().y, savedDialogRect.GetSize().y ),
  159. 0 );
  160. }
  161. // Be sure that the dialog appears in a visible area
  162. // (the dialog position might have been stored at the time when it was
  163. // shown on another display)
  164. if( wxDisplay::GetFromWindow( this ) == wxNOT_FOUND )
  165. Centre();
  166. }
  167. else
  168. {
  169. // Save the dialog's position & size before hiding, using classname as key
  170. class_map[ hash_key ] = EDA_RECT( wxDialog::GetPosition(), wxDialog::GetSize() );
  171. #ifdef __WXMAC__
  172. if ( m_eventLoop )
  173. m_eventLoop->Exit( GetReturnCode() ); // Needed for APP-MODAL dlgs on OSX
  174. #endif
  175. ret = wxDialog::Show( show );
  176. }
  177. return ret;
  178. }
  179. bool DIALOG_SHIM::Enable( bool enable )
  180. {
  181. // so we can do logging of this state change:
  182. #if defined(DEBUG)
  183. const char* type_id = typeid( *this ).name();
  184. printf( "wxDialog %s: %s\n", type_id, enable ? "enabled" : "disabled" );
  185. #endif
  186. return wxDialog::Enable( enable );
  187. }
  188. #ifdef __WXMAC__
  189. // Recursive descent doing a SelectAll() in wxTextCtrls.
  190. // MacOS User Interface Guidelines state that when tabbing to a text control all its
  191. // text should be selected. Since wxWidgets fails to implement this, we do it here.
  192. static void selectAllInTextCtrls( wxWindowList& children )
  193. {
  194. for( wxWindow* child : children )
  195. {
  196. wxTextCtrl* childTextCtrl = dynamic_cast<wxTextCtrl*>( child );
  197. if( childTextCtrl )
  198. {
  199. wxTextEntry* asTextEntry = dynamic_cast<wxTextEntry*>( childTextCtrl );
  200. // Respect an existing selection
  201. if( asTextEntry->GetStringSelection().IsEmpty() )
  202. asTextEntry->SelectAll();
  203. }
  204. else
  205. selectAllInTextCtrls( child->GetChildren() );
  206. }
  207. }
  208. #endif
  209. #ifdef __WXMAC__
  210. static void fixOSXCancelButtonIssue( wxWindow *aWindow )
  211. {
  212. // A ugly hack to fix an issue on OSX: cmd+c closes the dialog instead of
  213. // copying the text if a button with wxID_CANCEL is used in a
  214. // wxStdDialogButtonSizer created by wxFormBuilder: the label is &Cancel, and
  215. // this accelerator key has priority over the standard copy accelerator.
  216. // Note: problem also exists in other languages; for instance cmd+a closes
  217. // dialogs in German because the button is &Abbrechen.
  218. wxButton* button = dynamic_cast<wxButton*>( wxWindow::FindWindowById( wxID_CANCEL, aWindow ) );
  219. if( button )
  220. {
  221. static const wxString placeholder = wxT( "{amp}" );
  222. wxString buttonLabel = button->GetLabel();
  223. buttonLabel.Replace( wxT( "&&" ), placeholder );
  224. buttonLabel.Replace( wxT( "&" ), wxEmptyString );
  225. buttonLabel.Replace( placeholder, wxT( "&" ) );
  226. button->SetLabel( buttonLabel );
  227. }
  228. }
  229. #endif
  230. void DIALOG_SHIM::OnPaint( wxPaintEvent &event )
  231. {
  232. if( m_firstPaintEvent )
  233. {
  234. #ifdef __WXMAC__
  235. fixOSXCancelButtonIssue( this );
  236. selectAllInTextCtrls( GetChildren() );
  237. #endif
  238. if( m_initialFocusTarget )
  239. m_initialFocusTarget->SetFocus();
  240. else
  241. SetFocus(); // Focus the dialog itself
  242. m_firstPaintEvent = false;
  243. }
  244. event.Skip();
  245. }
  246. /*
  247. Quasi-Modal Mode Explained:
  248. The gtk calls in wxDialog::ShowModal() cause event routing problems if that
  249. modal dialog then tries to use KIWAY_PLAYER::ShowModal(). The latter shows up
  250. and mostly works but does not respond to the window decoration close button.
  251. There is no way to get around this without reversing the gtk calls temporarily.
  252. Quasi-Modal mode is our own almost modal mode which disables only the parent
  253. of the DIALOG_SHIM, leaving other frames operable and while staying captured in the
  254. nested event loop. This avoids the gtk calls and leaves event routing pure
  255. and sufficient to operate the KIWAY_PLAYER::ShowModal() properly. When using
  256. ShowQuasiModal() you have to use EndQuasiModal() in your dialogs and not
  257. EndModal(). There is also IsQuasiModal() but its value can only be true
  258. when the nested event loop is active. Do not mix the modal and quasi-modal
  259. functions. Use one set or the other.
  260. You might find this behavior preferable over a pure modal mode, and it was said
  261. that only the Mac has this natively, but now other platforms have something
  262. similar. You CAN use it anywhere for any dialog. But you MUST use it when
  263. you want to use KIWAY_PLAYER::ShowModal() from a dialog event.
  264. */
  265. int DIALOG_SHIM::ShowQuasiModal()
  266. {
  267. // This is an exception safe way to zero a pointer before returning.
  268. // Yes, even though DismissModal() clears this first normally, this is
  269. // here in case there's an exception before the dialog is dismissed.
  270. struct NULLER
  271. {
  272. void*& m_what;
  273. NULLER( void*& aPtr ) : m_what( aPtr ) {}
  274. ~NULLER() { m_what = 0; } // indeed, set it to NULL on destruction
  275. } clear_this( (void*&) m_qmodal_loop );
  276. // release the mouse if it's currently captured as the window having it
  277. // will be disabled when this dialog is shown -- but will still keep the
  278. // capture making it impossible to do anything in the modal dialog itself
  279. wxWindow* win = wxWindow::GetCapture();
  280. if( win )
  281. win->ReleaseMouse();
  282. // Get the optimal parent
  283. wxWindow* parent = GetParentForModalDialog( GetParent(), GetWindowStyle() );
  284. // Show the optimal parent
  285. DBG( if( parent ) printf( "%s: optimal parent: %s\n", __func__, typeid(*parent).name() );)
  286. wxASSERT_MSG( !m_qmodal_parent_disabler,
  287. wxT( "Caller using ShowQuasiModal() twice on same window?" ) );
  288. // quasi-modal: disable only my "optimal" parent
  289. m_qmodal_parent_disabler = new WDO_ENABLE_DISABLE( parent );
  290. #ifdef __WXMAC__
  291. // Apple in its infinite wisdom will raise a disabled window before even passing
  292. // us the event, so we have no way to stop it. Instead, we must set an order on
  293. // the windows so that the quasi-modal will be pushed in front of the disabled
  294. // window when it is raised.
  295. ReparentQuasiModal();
  296. #endif
  297. Show( true );
  298. m_qmodal_showing = true;
  299. WX_EVENT_LOOP event_loop;
  300. m_qmodal_loop = &event_loop;
  301. event_loop.Run();
  302. m_qmodal_showing = false;
  303. return GetReturnCode();
  304. }
  305. void DIALOG_SHIM::EndQuasiModal( int retCode )
  306. {
  307. // Hook up validator and transfer data from controls handling so quasi-modal dialogs
  308. // handle validation in the same way as other dialogs.
  309. if( ( retCode == wxID_OK ) && ( !Validate() || !TransferDataFromWindow() ) )
  310. return;
  311. SetReturnCode( retCode );
  312. if( !IsQuasiModal() )
  313. {
  314. wxFAIL_MSG( wxT( "either DIALOG_SHIM::EndQuasiModal called twice or ShowQuasiModal wasn't called" ) );
  315. return;
  316. }
  317. if( m_qmodal_loop )
  318. {
  319. if( m_qmodal_loop->IsRunning() )
  320. m_qmodal_loop->Exit( 0 );
  321. else
  322. m_qmodal_loop->ScheduleExit( 0 );
  323. m_qmodal_loop = NULL;
  324. }
  325. delete m_qmodal_parent_disabler;
  326. m_qmodal_parent_disabler = 0;
  327. Show( false );
  328. }
  329. void DIALOG_SHIM::OnCloseWindow( wxCloseEvent& aEvent )
  330. {
  331. if( IsQuasiModal() )
  332. {
  333. EndQuasiModal( wxID_CANCEL );
  334. return;
  335. }
  336. // This is mandatory to allow wxDialogBase::OnCloseWindow() to be called.
  337. aEvent.Skip();
  338. }
  339. void DIALOG_SHIM::OnButton( wxCommandEvent& aEvent )
  340. {
  341. if( IsQuasiModal() )
  342. {
  343. const int id = aEvent.GetId();
  344. if( id == GetAffirmativeId() )
  345. {
  346. EndQuasiModal( id );
  347. }
  348. else if( id == wxID_APPLY )
  349. {
  350. // Dialogs that provide Apply buttons should make sure data is valid before
  351. // allowing a transfer, as there is no other way to indicate failure
  352. // (i.e. the dialog can't refuse to close as it might with OK, because it
  353. // isn't closing anyway)
  354. if( Validate() )
  355. {
  356. bool success = TransferDataFromWindow();
  357. (void) success;
  358. }
  359. }
  360. else if( id == GetEscapeId() ||
  361. (id == wxID_CANCEL && GetEscapeId() == wxID_ANY) )
  362. {
  363. EndQuasiModal( wxID_CANCEL );
  364. }
  365. else // not a standard button
  366. {
  367. aEvent.Skip();
  368. }
  369. return;
  370. }
  371. // This is mandatory to allow wxDialogBase::OnButton() to be called.
  372. aEvent.Skip();
  373. }