-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
wxHelpers.cpp
489 lines (401 loc) · 14.4 KB
/
wxHelpers.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/* PCSX2 - PS2 Emulator for PCs
* Copyright (C) 2002-2010 PCSX2 Dev Team
*
* PCSX2 is free software: you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* PCSX2 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with PCSX2.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include "PrecompiledHeader.h"
#include "wxGuiTools.h"
#include "pxStaticText.h"
#include "Threading.h"
#include "IniInterface.h"
#ifdef __LINUX__
// I do not know if it is a GCC issue or a bug in wxwidget !
// Anyway the code isn't used (m_hasContextHelp always false) so I remove
// it for linux. The 'vtable' issue appears with the include of
// <wx/cshelp.h> -- Gregory
#define GCC_4_6_LTO_COMPILATION_ISSUE
#endif
#ifndef GCC_4_6_LTO_COMPILATION_ISSUE
#include <wx/cshelp.h>
#endif
#include <wx/tooltip.h>
#include <wx/spinctrl.h>
using namespace pxSizerFlags;
pxDialogCreationFlags pxDialogFlags()
{
return pxDialogCreationFlags().CloseBox().Caption().Vertical();
}
// --------------------------------------------------------------------------------------
// BaseDeletableObject Implementation
// --------------------------------------------------------------------------------------
// This code probably deserves a better home. It's general purpose non-GUI code (the single
// wxApp/Gui dependency is in wxGuiTools.cpp for now).
//
bool BaseDeletableObject::MarkForDeletion()
{
return !_InterlockedExchange( &m_IsBeingDeleted, true );
}
void BaseDeletableObject::DeleteSelf()
{
if( MarkForDeletion() )
DoDeletion();
}
BaseDeletableObject::BaseDeletableObject()
{
#ifdef _MSC_VER
// Bleh, this fails because _CrtIsValidHeapPointer calls HeapValidate on the
// pointer, but the pointer is a virtual base class, so it's not a valid block. >_<
//pxAssertDev( _CrtIsValidHeapPointer( this ), "BaseDeletableObject types cannot be created on the stack or as temporaries!" );
#endif
m_IsBeingDeleted = false;
}
BaseDeletableObject::~BaseDeletableObject() throw()
{
AffinityAssert_AllowFrom_MainUI();
}
// --------------------------------------------------------------------------------------
// Creates a text control which is right-justified and has it's minimum width configured to suit
// the number of digits requested.
wxTextCtrl* CreateNumericalTextCtrl( wxWindow* parent, int digits, long flags )
{
wxTextCtrl* ctrl = new wxTextCtrl( parent, wxID_ANY );
ctrl->SetWindowStyleFlag( flags );
pxFitToDigits( ctrl, digits );
return ctrl;
}
void pxFitToDigits( wxWindow* win, int digits )
{
int ex;
win->GetTextExtent( wxString( L'0', digits+1 ), &ex, NULL );
win->SetMinSize( wxSize( ex+10, wxDefaultCoord ) ); // +10 for text control borders/insets and junk.
}
void pxFitToDigits( wxSpinCtrl* win, int digits )
{
// HACK!! The better way would be to create a pxSpinCtrl class that extends wxSpinCtrl and thus
// have access to wxSpinButton::DoGetBestSize(). But since I don't want to do that, we'll just
// make/fake it with a value it's pretty common to Win32/GTK/Mac:
static const int MagicSpinnerSize = 18;
int ex;
win->GetTextExtent( wxString( L'0', digits+1 ), &ex, NULL );
win->SetMinSize( wxSize( ex+10+MagicSpinnerSize, wxDefaultCoord ) ); // +10 for text control borders/insets and junk.
}
bool pxDialogExists( const wxString& name )
{
return wxFindWindowByName( name ) != NULL;
}
// =====================================================================================================
// wxDialogWithHelpers Class Implementations
// =====================================================================================================
DEFINE_EVENT_TYPE( pxEvt_OnDialogCreated )
IMPLEMENT_DYNAMIC_CLASS(wxDialogWithHelpers, wxDialog)
wxDialogWithHelpers::wxDialogWithHelpers()
{
m_hasContextHelp = false;
m_extraButtonSizer = NULL;
Init( pxDialogFlags() );
}
wxDialogWithHelpers::wxDialogWithHelpers( wxWindow* parent, const wxString& title, const pxDialogCreationFlags& cflags )
: wxDialog( parent, wxID_ANY, title, wxDefaultPosition, wxDefaultSize, cflags.GetWxWindowFlags() )
{
m_hasContextHelp = cflags.hasContextHelp;
if( (int)cflags.BoxSizerOrient != 0 )
{
SetSizer( new wxBoxSizer( cflags.BoxSizerOrient ) );
*this += StdPadding;
}
Init( cflags );
SetMinSize( cflags.MinimumSize );
}
wxDialogWithHelpers::~wxDialogWithHelpers() throw()
{
}
void wxDialogWithHelpers::Init( const pxDialogCreationFlags& cflags )
{
// This fixes it so that the dialogs show up in the task bar in Vista:
// (otherwise they go stupid iconized mode if the user minimizes them)
if( cflags.hasMinimizeBox )
SetExtraStyle(GetExtraStyle() & ~wxTOPLEVEL_EX_DIALOG);
m_extraButtonSizer = NULL;
#ifndef GCC_4_6_LTO_COMPILATION_ISSUE
if( m_hasContextHelp )
delete wxHelpProvider::Set( new wxSimpleHelpProvider() );
#endif
// GTK/Linux Note: currently the Close (X) button doesn't appear to work in dialogs. Docs
// indicate that it should, so I presume the problem is in wxWidgets and that (hopefully!)
// an updated version will fix it later. I tried to fix it using a manual Connect but it
// didn't do any good. (problem could also be my Co-Linux / x-window manager)
Connect( pxEvt_OnDialogCreated, wxCommandEventHandler (wxDialogWithHelpers::OnDialogCreated) );
Connect( wxID_OK, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (wxDialogWithHelpers::OnOkCancel) );
Connect( wxID_CANCEL, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (wxDialogWithHelpers::OnOkCancel) );
Connect( wxEVT_CLOSE_WINDOW, wxCloseEventHandler (wxDialogWithHelpers::OnCloseWindow) );
wxCommandEvent createEvent( pxEvt_OnDialogCreated );
createEvent.SetId( GetId() );
AddPendingEvent( createEvent );
}
void wxDialogWithHelpers::OnDialogCreated( wxCommandEvent& evt )
{
evt.Skip();
if( (evt.GetId() == GetId()) && !GetDialogName().IsEmpty() )
SetName( L"Dialog:" + GetDialogName() );
}
wxString wxDialogWithHelpers::GetDialogName() const
{
return wxEmptyString;
}
void wxDialogWithHelpers::DoAutoCenter()
{
// Smart positioning logic! If our parent window is larger than our window by some
// good amount, then we center on that. If not, center relative to the screen. This
// avoids the popup automatically eclipsing the parent window (which happens in PCSX2
// a lot since the main window is small).
bool centerfail = true;
if( wxWindow* parent = GetParent() )
{
const wxSize parentSize( parent->GetSize() );
if( (parentSize.x > ((int)GetSize().x * 1.5)) || (parentSize.y > ((int)GetSize().y * 1.5)) )
{
CenterOnParent();
centerfail = false;
}
}
if( centerfail ) CenterOnScreen();
}
void wxDialogWithHelpers::SmartCenterFit()
{
Fit();
const wxString dlgName( GetDialogName() );
if( dlgName.IsEmpty() )
{
DoAutoCenter(); return;
}
if( wxConfigBase* cfg = wxConfigBase::Get( false ) )
{
wxRect screenRect( GetScreenRect() );
IniLoader loader( cfg );
ScopedIniGroup group( loader, L"DialogPositions" );
cfg->SetRecordDefaults( false );
if( GetWindowStyle() & wxRESIZE_BORDER )
{
wxSize size;
loader.Entry( dlgName + L"_Size", size, screenRect.GetSize() );
SetSize( size );
}
if( !cfg->Exists( dlgName + L"_Pos" ) )
DoAutoCenter();
else
{
wxPoint pos;
loader.Entry( dlgName + L"_Pos", pos, screenRect.GetPosition() );
SetPosition( pos );
}
cfg->SetRecordDefaults( true );
}
}
// Overrides wxDialog behavior to include automatic Fit() and CenterOnParent/Screen. The centering
// is based on a heuristic the centers against the parent window if the parent window is at least
// 75% larger than the fitted dialog.
int wxDialogWithHelpers::ShowModal()
{
SmartCenterFit();
m_CreatedRect = GetScreenRect();
return wxDialog::ShowModal();
}
// Overrides wxDialog behavior to include automatic Fit() and CenterOnParent/Screen. The centering
// is based on a heuristic the centers against the parent window if the parent window is at least
// 75% larger than the fitted dialog.
bool wxDialogWithHelpers::Show( bool show )
{
if( show )
{
SmartCenterFit();
m_CreatedRect = GetScreenRect();
}
return wxDialog::Show( show );
}
wxStaticText& wxDialogWithHelpers::Label( const wxString& label )
{
return *new wxStaticText( this, wxID_ANY, label, wxDefaultPosition, wxDefaultSize, wxALIGN_CENTER_VERTICAL );
}
pxStaticText& wxDialogWithHelpers::Text( const wxString& label )
{
return *new pxStaticText( this, label );
}
pxStaticText& wxDialogWithHelpers::Heading( const wxString& label )
{
return *new pxStaticHeading( this, label );
}
bool wxDialogWithHelpers::Destroy()
{
// Save the dialog position if the dialog is named...
// FIXME : This doesn't get called if the app is exited by alt-f4'ing the main app window.
// ... not sure how to fix that yet. I could register a list of open windows into wxAppWithHelpers
// that systematically get closed. Seems like work, maybe later. --air
if( wxConfigBase* cfg = IsIconized() ? NULL : wxConfigBase::Get( false ) )
{
const wxString dlgName( GetDialogName() );
const wxRect screenRect( GetScreenRect() );
if( !dlgName.IsEmpty() && ( m_CreatedRect != screenRect) )
{
wxPoint pos( screenRect.GetPosition() );
IniSaver saver( cfg );
ScopedIniGroup group( saver, L"DialogPositions" );
if( GetWindowStyle() & wxRESIZE_BORDER )
{
wxSize size( screenRect.GetSize() );
saver.Entry( dlgName + L"_Size", size, screenRect.GetSize() );
}
saver.Entry( dlgName + L"_Pos", pos, screenRect.GetPosition() );
}
}
return _parent::Destroy();
}
void wxDialogWithHelpers::OnCloseWindow( wxCloseEvent& evt )
{
if( !IsModal() ) Destroy();
evt.Skip();
}
void wxDialogWithHelpers::OnOkCancel( wxCommandEvent& evt )
{
Close();
evt.Skip();
}
void wxDialogWithHelpers::AddOkCancel( wxSizer &sizer, bool hasApply )
{
wxStdDialogButtonSizer& s_buttons( *new wxStdDialogButtonSizer() );
s_buttons.AddButton( new wxButton( this, wxID_OK ) );
s_buttons.AddButton( new wxButton( this, wxID_CANCEL ) );
if( hasApply )
s_buttons.AddButton( new wxButton( this, wxID_APPLY ) );
m_extraButtonSizer = new wxBoxSizer( wxHORIZONTAL );
#ifndef GCC_4_6_LTO_COMPILATION_ISSUE
// Add the context-sensitive help button on the caption for the platforms
// which support it (currently MSW only)
if( m_hasContextHelp )
{
SetExtraStyle( wxDIALOG_EX_CONTEXTHELP );
#ifndef __WXMSW__
*m_extraButtonSizer += new wxContextHelpButton(this) | StdButton();
#endif
}
#endif
// create a sizer to hold the help and ok/cancel buttons, for platforms
// that need a custom help icon. [fixme: help icon prolly better off somewhere else]
wxFlexGridSizer& flex( *new wxFlexGridSizer( 2 ) );
flex.AddGrowableCol( 0, 1 );
flex.AddGrowableCol( 1, 15 );
flex += m_extraButtonSizer | pxAlignLeft;
flex += s_buttons | (pxExpand & pxCenter);
sizer += flex | StdExpand();
s_buttons.Realize();
}
void wxDialogWithHelpers::AddOkCancel( wxSizer *sizer, bool hasApply )
{
if( sizer == NULL ) sizer = GetSizer();
pxAssert( sizer );
AddOkCancel( *sizer, hasApply );
}
wxDialogWithHelpers& wxDialogWithHelpers::SetMinWidth( int newWidth )
{
SetMinSize( wxSize( newWidth, GetMinHeight() ) );
if( wxSizer* sizer = GetSizer() )
sizer->SetMinSize( wxSize( newWidth, sizer->GetMinSize().GetHeight() ) );
return *this;
}
wxDialogWithHelpers& wxDialogWithHelpers::SetMinHeight( int newHeight )
{
SetMinSize( wxSize( GetMinWidth(), newHeight ) );
if( wxSizer* sizer = GetSizer() )
sizer->SetMinSize( wxSize( sizer->GetMinSize().GetWidth(), newHeight ) );
return *this;
}
int wxDialogWithHelpers::GetCharHeight() const
{
return pxGetCharHeight( this, 1 );
}
// --------------------------------------------------------------------------------------
// wxPanelWithHelpers Implementations
// --------------------------------------------------------------------------------------
IMPLEMENT_DYNAMIC_CLASS(wxPanelWithHelpers, wxPanel)
void wxPanelWithHelpers::Init()
{
}
// Creates a Static Box container for this panel. the static box sizer becomes the default
// sizer for this panel. If the panel already has a sizer set, then that sizer will be
// transfered to the new StaticBoxSizer (and will be the first item in it's list, retaining
// consistent and expected layout)
wxPanelWithHelpers* wxPanelWithHelpers::AddFrame( const wxString& label, wxOrientation orient )
{
wxSizer* oldSizer = GetSizer();
SetSizer( new wxStaticBoxSizer( orient, this, label ), false );
Init();
if( oldSizer )
*this += oldSizer | pxExpand;
return this;
}
wxStaticText& wxPanelWithHelpers::Label( const wxString& label )
{
return *new wxStaticText( this, wxID_ANY, label );
}
pxStaticText& wxPanelWithHelpers::Text( const wxString& label )
{
return *new pxStaticText( this, label );
}
pxStaticText& wxPanelWithHelpers::Heading( const wxString& label )
{
return *new pxStaticHeading( this, label );
}
wxPanelWithHelpers::wxPanelWithHelpers( wxWindow* parent, wxOrientation orient, const wxString& staticBoxLabel )
: wxPanel( parent )
{
SetSizer( new wxStaticBoxSizer( orient, this, staticBoxLabel ) );
Init();
}
wxPanelWithHelpers::wxPanelWithHelpers( wxWindow* parent, wxOrientation orient )
: wxPanel( parent )
{
SetSizer( new wxBoxSizer( orient ) );
Init();
}
wxPanelWithHelpers::wxPanelWithHelpers( wxWindow* parent )
: wxPanel( parent )
{
Init();
}
wxPanelWithHelpers::wxPanelWithHelpers( wxWindow* parent, const wxPoint& pos, const wxSize& size )
: wxPanel( parent, wxID_ANY, pos, size )
{
Init();
}
wxPanelWithHelpers& wxPanelWithHelpers::SetMinWidth( int newWidth )
{
SetMinSize( wxSize( newWidth, GetMinHeight() ) );
if( wxSizer* sizer = GetSizer() )
sizer->SetMinSize( wxSize( newWidth, sizer->GetMinSize().GetHeight() ) );
return *this;
}
int pxGetCharHeight( const wxWindow* wind, int rows )
{
if( !wind ) return 0;
wxClientDC dc(wx_const_cast(wxWindow*, wind));
dc.SetFont( wind->GetFont() );
#ifdef __LINUX__
// It seems there is a bad detection of the size of the font (non standard dpi ???). Font are cut in top or bottom.
// Add a correction factor to leave enough room. Visualy 1.7 seems fine but feel free to tune it -- Gregory
return (dc.GetCharHeight() * 1.7 + 1 ) * rows;
#else
return (dc.GetCharHeight() + 1 ) * rows;
#endif
}
int pxGetCharHeight( const wxWindow& wind, int rows )
{
return pxGetCharHeight( &wind, rows );
}