/[pcsx2_0.9.7]/trunk/common/include/Utilities/wxGuiTools.h
ViewVC logotype

Annotation of /trunk/common/include/Utilities/wxGuiTools.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 31 - (hide annotations) (download)
Tue Sep 7 03:24:11 2010 UTC (9 years, 11 months ago) by william
File MIME type: text/plain
File size: 18136 byte(s)
committing r3113 initial commit again...
1 william 31 /* PCSX2 - PS2 Emulator for PCs
2     * Copyright (C) 2002-2010 PCSX2 Dev Team
3     *
4     * PCSX2 is free software: you can redistribute it and/or modify it under the terms
5     * of the GNU Lesser General Public License as published by the Free Software Found-
6     * ation, either version 3 of the License, or (at your option) any later version.
7     *
8     * PCSX2 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9     * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
10     * PURPOSE. See the GNU General Public License for more details.
11     *
12     * You should have received a copy of the GNU General Public License along with PCSX2.
13     * If not, see <http://www.gnu.org/licenses/>.
14     */
15    
16     #pragma once
17    
18     #if wxUSE_GUI
19    
20     #include "Dependencies.h"
21     #include "ScopedPtr.h"
22     #include <stack>
23    
24     #include <wx/wx.h>
25    
26     class pxStaticText;
27     class pxStaticHeading;
28     class pxCheckBox;
29     class wxSpinCtrl;
30    
31     #define wxSF wxSizerFlags()
32    
33     // --------------------------------------------------------------------------------------
34     // pxAlignment / pxStretchType
35     // --------------------------------------------------------------------------------------
36     // These are full blown class types instead of enumerations because wxSizerFlags has an
37     // implicit conversion from integer (silly design flaw creating more work for me!)
38     //
39     struct pxAlignmentType
40     {
41     enum
42     {
43     Centre,
44     Center = Centre,
45     Middle,
46     Left,
47     Right,
48     Top,
49     Bottom
50     };
51    
52     int intval;
53    
54     wxSizerFlags Apply( wxSizerFlags flags=wxSizerFlags() ) const;
55    
56     wxSizerFlags operator& ( const wxSizerFlags& _flgs ) const
57     {
58     return Apply( _flgs );
59     }
60    
61     wxSizerFlags Border( int dir, int padding ) const
62     {
63     return Apply().Border( dir, padding );
64     }
65    
66     wxSizerFlags Proportion( int prop ) const
67     {
68     return Apply().Proportion( intval );
69     }
70    
71     operator wxSizerFlags() const
72     {
73     return Apply();
74     }
75     };
76    
77     struct pxStretchType
78     {
79     enum
80     {
81     Shrink,
82     Expand,
83     Shaped,
84     ReserveHidden,
85     FixedMinimum
86     };
87    
88     int intval;
89    
90     wxSizerFlags Apply( wxSizerFlags flags=wxSizerFlags() ) const;
91    
92     wxSizerFlags operator& ( const wxSizerFlags& _flgs ) const
93     {
94     return Apply( _flgs );
95     }
96    
97     wxSizerFlags Border( int dir, int padding ) const
98     {
99     return Apply().Border( dir, padding );
100     }
101    
102     wxSizerFlags Proportion( int prop ) const
103     {
104     return Apply().Proportion( intval );
105     }
106    
107     operator wxSizerFlags() const
108     {
109     return Apply();
110     }
111     };
112    
113     static __forceinline wxSizerFlags pxProportion( int prop )
114     {
115     return wxSizerFlags( prop );
116     }
117    
118     static __forceinline wxSizerFlags pxBorder( int dir, int pad )
119     {
120     return wxSizerFlags().Border( dir, pad );
121     }
122    
123     class pxStretchSpacer
124     {
125     public:
126     int proportion;
127    
128     pxStretchSpacer( int prop=0 )
129     {
130     proportion = prop;
131     }
132     };
133    
134     extern const pxAlignmentType
135     pxCentre, // Horizontal centered alignment
136     pxCenter,
137     pxMiddle, // vertical centered alignment
138    
139     pxAlignLeft,
140     pxAlignRight,
141     pxAlignTop,
142     pxAlignBottom;
143    
144     extern const pxStretchType
145     pxShrink,
146     pxExpand,
147     pxShaped,
148     pxReserveHidden,
149     pxFixedMinimum;
150    
151     // --------------------------------------------------------------------------------------
152     // pxWindowAndFlags
153     // --------------------------------------------------------------------------------------
154     // This struct is a go-between for combining windows and sizer flags in "neat" fashion.
155     // To create the struct, use the | operator, like so:
156     //
157     // myPanel | wxSizerFlags().Expand()
158     //
159     // Implementation Note: This struct is a template as it allows us to use a special
160     // version of the += operator that retains the type information of the window, in case
161     // the window implements its own += overloads (one example is pxStaticText). Without the
162     // template, the type of the window would only be known as "wxWindow" when it's added to the
163     // sizer, and would thus fail to invoke the correct operator overload.
164     //
165     template< typename WinType >
166     struct pxWindowAndFlags
167     {
168     WinType* window;
169     wxSizerFlags flags;
170     };
171    
172    
173     extern wxSizerFlags operator& ( const wxSizerFlags& _flgs, const wxSizerFlags& _flgs2 );
174    
175     template< typename WinType >
176     pxWindowAndFlags<WinType> operator | ( WinType* _win, const wxSizerFlags& _flgs )
177     {
178     pxWindowAndFlags<WinType> result = { _win, _flgs };
179     return result;
180     }
181    
182     template< typename WinType >
183     pxWindowAndFlags<WinType> operator | ( WinType& _win, const wxSizerFlags& _flgs )
184     {
185     pxWindowAndFlags<WinType> result = { &_win, _flgs };
186     return result;
187     }
188    
189     // --------------------------------------------------------------------------------------
190     // wxSizer Operator += .. a wxSizer.Add() Substitute
191     // --------------------------------------------------------------------------------------
192     // This set of operators is the *recommended* method for adding windows to sizers, not just
193     // because it's a lot prettier but also because it allows controls like pxStaticText to over-
194     // ride default sizerflags behavior.
195     //
196     // += operator works on either sizers, wxDialogs or wxPanels. In the latter case, the window
197     // is added to the dialog/panel's toplevel sizer (wxPanel.GetSizer() is used). If the panel
198     // has no sizer set via SetSizer(), an assertion is generated.
199     //
200    
201     extern void operator+=( wxSizer& target, wxWindow* src );
202     extern void operator+=( wxSizer& target, wxSizer* src );
203     extern void operator+=( wxSizer& target, wxWindow& src );
204     extern void operator+=( wxSizer& target, wxSizer& src );
205    
206     extern void operator+=( wxSizer* target, wxWindow& src );
207     extern void operator+=( wxSizer* target, wxSizer& src );
208    
209     extern void operator+=( wxSizer& target, int spacer );
210     extern void operator+=( wxWindow& target, int spacer );
211     extern void operator+=( wxSizer& target, const pxStretchSpacer& spacer );
212     extern void operator+=( wxWindow& target, const pxStretchSpacer& spacer );
213    
214     // ----------------------------------------------------------------------------
215     // Important: This template is needed in order to retain window type information and
216     // invoke the proper overloaded version of += (which is used by pxStaticText and other
217     // classes to perform special actions when added to sizers).
218     template< typename WinType >
219     void operator+=( wxWindow& target, WinType* src )
220     {
221     if( !pxAssert( target.GetSizer() != NULL ) ) return;
222     *target.GetSizer() += src;
223     }
224    
225     template< typename WinType >
226     void operator+=( wxWindow& target, WinType& src )
227     {
228     if( !pxAssert( target.GetSizer() != NULL ) ) return;
229     *target.GetSizer() += src;
230     }
231    
232     template< typename WinType >
233     void operator+=( wxWindow& target, const pxWindowAndFlags<WinType>& src )
234     {
235     if( !pxAssert( target.GetSizer() != NULL ) ) return;
236     *target.GetSizer() += src;
237     }
238    
239     template< typename WinType >
240     void operator+=( wxSizer& target, const pxWindowAndFlags<WinType>& src )
241     {
242     target.Add( src.window, src.flags );
243     }
244    
245     // ----------------------------------------------------------------------------
246     // Pointer Versions! (note that C++ requires one of the two operator params be a
247     // "poper" object type (non-pointer), so that's why some of these are missing.
248    
249     template< typename WinType >
250     void operator+=( wxWindow* target, WinType& src )
251     {
252     if( !pxAssert( target != NULL ) ) return;
253     if( !pxAssert( target->GetSizer() != NULL ) ) return;
254     *target->GetSizer() += src;
255     }
256    
257     template< typename WinType >
258     void operator+=( wxWindow* target, const pxWindowAndFlags<WinType>& src )
259     {
260     if( !pxAssert( target != NULL ) ) return;
261     if( !pxAssert( target->GetSizer() != NULL ) ) return;
262     *target->GetSizer() += src;
263     }
264    
265     template< typename WinType >
266     void operator+=( wxSizer* target, const pxWindowAndFlags<WinType>& src )
267     {
268     if( !pxAssert( target != NULL ) ) return;
269     target->Add( src.window, src.flags );
270     }
271    
272     // ----------------------------------------------------------------------------
273     // wxGuiTools.h
274     //
275     // This file is meant to contain utility classes for users of the wxWidgets library.
276     // All classes in this file are dependent on wxBase and wxCore libraries! Meaning
277     // you will have to use wxCore header files and link against wxCore (GUI) to build
278     // them. For tools which require only wxBase, see wxBaseTools.h
279     // ----------------------------------------------------------------------------
280    
281     namespace pxSizerFlags
282     {
283     static const int StdPadding = 5;
284    
285     extern wxSizerFlags StdSpace();
286     extern wxSizerFlags StdCenter();
287     extern wxSizerFlags StdExpand();
288     extern wxSizerFlags TopLevelBox();
289     extern wxSizerFlags SubGroup();
290     extern wxSizerFlags StdButton();
291     extern wxSizerFlags Checkbox();
292     };
293    
294     BEGIN_DECLARE_EVENT_TYPES()
295     // Added to the event queue by pxDialogWithHelpers
296     DECLARE_EVENT_TYPE( pxEvt_OnDialogCreated, -1 )
297     END_DECLARE_EVENT_TYPES()
298    
299     // --------------------------------------------------------------------------------------
300     // wxDialogWithHelpers
301     // --------------------------------------------------------------------------------------
302     class wxDialogWithHelpers : public wxDialog
303     {
304     DECLARE_DYNAMIC_CLASS_NO_COPY(wxDialogWithHelpers)
305    
306     protected:
307     bool m_hasContextHelp;
308     int m_idealWidth;
309     wxBoxSizer* m_extraButtonSizer;
310    
311     public:
312     wxDialogWithHelpers();
313     wxDialogWithHelpers(wxWindow* parent, const wxString& title, bool hasContextHelp=false, bool resizable=false );
314     wxDialogWithHelpers(wxWindow* parent, const wxString& title, wxOrientation orient);
315     virtual ~wxDialogWithHelpers() throw();
316    
317     void Init();
318     void AddOkCancel( wxSizer& sizer, bool hasApply=false );
319     void AddOkCancel( wxSizer* sizer=NULL, bool hasApply=false );
320    
321     virtual void SmartCenterFit();
322     virtual int ShowModal();
323     virtual bool Show( bool show=true );
324    
325     // Must return the same thing as GetNameStatic; a name ideal for use in uniquely
326     // identifying dialogs. (this version is the 'instance' version, which is called
327     // by BaseConfigurationDialog to assign the wxWidgets dialog name, and for saving
328     // screenshots to disk)
329     virtual wxString GetDialogName() const;
330    
331     virtual pxStaticText* Text( const wxString& label );
332     virtual pxStaticHeading* Heading( const wxString& label );
333    
334     virtual wxDialogWithHelpers& SetIdealWidth( int newWidth ) { m_idealWidth = newWidth; return *this; }
335    
336     int GetIdealWidth() const { return m_idealWidth; }
337     bool HasIdealWidth() const { return m_idealWidth != wxDefaultCoord; }
338    
339     protected:
340     void OnDialogCreated( wxCommandEvent& evt );
341     void OnOkCancel(wxCommandEvent& evt);
342     void OnCloseWindow(wxCloseEvent& event);
343     };
344    
345     // --------------------------------------------------------------------------------------
346     // wxPanelWithHelpers
347     // --------------------------------------------------------------------------------------
348     // Overview of Helpers provided by this class:
349     // * Simpler constructors that have wxID, position, and size parameters removed (We never
350     // use them in pcsx2)
351     //
352     // * Automatic 'primary box sizer' creation, assigned via SetSizer() -- use GetSizer()
353     // to retrieve it, or use the "*this += window;" syntax to add windows directly to it.
354     //
355     // * Built-in support for StaticBoxes (aka groupboxes). Create one at construction with
356     // a wxString label, or add one "after the fact" using AddFrame.
357     //
358     // * Propagates IdealWidth settings from parenting wxPanelWithHelpers classes, and auto-
359     // matically adjusts the width based on the sizer type (groupsizers get truncated to
360     // account for borders).
361     //
362     class wxPanelWithHelpers : public wxPanel
363     {
364     DECLARE_DYNAMIC_CLASS_NO_COPY(wxPanelWithHelpers)
365    
366     protected:
367     int m_idealWidth;
368    
369     public:
370     wxPanelWithHelpers( wxWindow* parent, wxOrientation orient, const wxString& staticBoxLabel );
371     wxPanelWithHelpers( wxWindow* parent, wxOrientation orient );
372     wxPanelWithHelpers( wxWindow* parent, const wxPoint& pos, const wxSize& size=wxDefaultSize );
373     explicit wxPanelWithHelpers( wxWindow* parent=NULL );
374    
375     wxPanelWithHelpers* AddFrame( const wxString& label, wxOrientation orient=wxVERTICAL );
376    
377     pxStaticText* Text( const wxString& label );
378     pxStaticHeading* Heading( const wxString& label );
379    
380     // TODO : Propagate to children?
381     wxPanelWithHelpers& SetIdealWidth( int width ) { m_idealWidth = width; return *this; }
382     int GetIdealWidth() const { return m_idealWidth; }
383     bool HasIdealWidth() const { return m_idealWidth != wxDefaultCoord; }
384    
385     protected:
386     void Init();
387     };
388    
389    
390     // --------------------------------------------------------------------------------------
391     // pxTextWrapperBase
392     // --------------------------------------------------------------------------------------
393     // this class is used to wrap the text on word boundary: wrapping is done by calling
394     // OnStartLine() and OnOutputLine() functions. This class by itself can be used as a
395     // line counting tool, but produces no formatted text output.
396     //
397     // [class "borrowed" from wxWidgets private code, made public, and renamed to avoid possible
398     // conflicts with future editions of wxWidgets which might make it public. Why this isn't
399     // publicly available already in wxBase I'll never know-- air]
400     //
401     class pxTextWrapperBase
402     {
403     protected:
404     bool m_eol;
405     int m_linecount;
406    
407     public:
408     virtual ~pxTextWrapperBase() throw() { }
409    
410     pxTextWrapperBase()
411     {
412     m_eol = false;
413     m_linecount = 0;
414     }
415    
416     // win is used for getting the font, text is the text to wrap, width is the
417     // max line width or -1 to disable wrapping
418     pxTextWrapperBase& Wrap( const wxWindow& win, const wxString& text, int widthMax );
419    
420     int GetLineCount() const
421     {
422     return m_linecount;
423     }
424    
425     protected:
426     // line may be empty
427     virtual void OnOutputLine(const wxString& line) { }
428    
429     // called at the start of every new line (except the very first one)
430     virtual void OnNewLine() { }
431    
432     void DoOutputLine(const wxString& line);
433     bool IsStartOfNewLine();
434     };
435    
436     // --------------------------------------------------------------------------------------
437     // pxTextWrapper
438     // --------------------------------------------------------------------------------------
439     // This class extends pxTextWrapperBase and adds the ability to retrieve the formatted
440     // result of word wrapping.
441     //
442     class pxTextWrapper : public pxTextWrapperBase
443     {
444     typedef pxTextWrapperBase _parent;
445    
446     protected:
447     wxString m_text;
448    
449     public:
450     pxTextWrapper() : pxTextWrapperBase() { }
451     virtual ~pxTextWrapper() throw() { }
452    
453     const wxString& GetResult() const
454     {
455     return m_text;
456     }
457    
458     pxTextWrapper& Wrap( const wxWindow& win, const wxString& text, int widthMax );
459    
460     protected:
461     virtual void OnOutputLine(const wxString& line);
462     virtual void OnNewLine();
463     };
464    
465     // --------------------------------------------------------------------------------------
466     // pxWindowTextWriter
467     // --------------------------------------------------------------------------------------
468     class pxWindowTextWriter
469     {
470     protected:
471     wxDC& m_dc;
472    
473     wxAlignment m_align;
474     wxPoint m_curpos;
475     int m_leading;
476    
477     virtual void _DoWriteLn( const wxChar* msg );
478     void _DoWrite( const wxChar* msg );
479    
480     public:
481     pxWindowTextWriter( wxDC& dc )
482     : m_dc( dc )
483     {
484     m_curpos = wxPoint();
485     m_align = wxALIGN_CENTER;
486     m_leading = 2;
487    
488     OnFontChanged();
489     }
490    
491     virtual ~pxWindowTextWriter() throw()
492     {
493    
494     }
495    
496     virtual void OnFontChanged();
497    
498     pxWindowTextWriter& WriteLn();
499     pxWindowTextWriter& WriteLn( const wxChar* fmt, ... );
500     pxWindowTextWriter& SetFont( const wxFont& font );
501     pxWindowTextWriter& Align( const wxAlignment& align );
502    
503     pxWindowTextWriter& SetLeading( int lead )
504     {
505     m_leading = lead;
506     return *this;
507     }
508    
509     pxWindowTextWriter& SetWeight( int weight );
510     pxWindowTextWriter& SetStyle( int style );
511     pxWindowTextWriter& Normal();
512    
513     pxWindowTextWriter& Bold()
514     {
515     return SetWeight(wxBOLD);
516     }
517    
518     pxWindowTextWriter& Italic()
519     {
520     return SetStyle(wxITALIC);
521     }
522    
523     pxWindowTextWriter& SetPos( const wxPoint& pos );
524     pxWindowTextWriter& MovePos( const wxSize& delta );
525    
526     pxWindowTextWriter& SetPos( int xpos, int ypos )
527     {
528     return SetPos( wxPoint(xpos,ypos) );
529     }
530    
531     pxWindowTextWriter& MovePos( int xdelta, int ydelta )
532     {
533     return MovePos( wxSize(xdelta, ydelta) );
534     }
535    
536     pxWindowTextWriter& SetY( int ypos );
537     pxWindowTextWriter& MoveY( int ydelta );
538     };
539    
540     // --------------------------------------------------------------------------------------
541     // MoreStockCursors
542     // --------------------------------------------------------------------------------------
543     // Because (inexplicably) the ArrowWait cursor isn't in wxWidgets stock list.
544     //
545     class MoreStockCursors
546     {
547     protected:
548     ScopedPtr<wxCursor> m_arrowWait;
549    
550     public:
551     MoreStockCursors() { }
552     virtual ~MoreStockCursors() throw() { }
553     const wxCursor& GetArrowWait();
554     };
555    
556     enum BusyCursorType
557     {
558     Cursor_NotBusy,
559     Cursor_KindaBusy,
560     Cursor_ReallyBusy,
561     };
562    
563     extern MoreStockCursors StockCursors;
564    
565     // --------------------------------------------------------------------------------------
566     // ScopedBusyCursor
567     // --------------------------------------------------------------------------------------
568     // ... because wxWidgets wxBusyCursor doesn't really do proper nesting (doesn't let me
569     // override a partially-busy cursor with a really busy one)
570    
571     class ScopedBusyCursor
572     {
573     protected:
574     static std::stack<BusyCursorType> m_cursorStack;
575     static BusyCursorType m_defBusyType;
576    
577     public:
578     ScopedBusyCursor( BusyCursorType busytype );
579     virtual ~ScopedBusyCursor() throw();
580    
581     static void SetDefault( BusyCursorType busytype );
582     static void SetManualBusyCursor( BusyCursorType busytype );
583     };
584    
585     // --------------------------------------------------------------------------------------
586     // pxFitToDigits
587     // --------------------------------------------------------------------------------------
588     // Fits a given text or spinner control to the number of digits requested, since by default
589     // they're usually way over-sized.
590    
591     extern void pxFitToDigits( wxWindow* win, int digits );
592     extern void pxFitToDigits( wxSpinCtrl* win, int digits );
593     extern wxTextCtrl* CreateNumericalTextCtrl( wxWindow* parent, int digits );
594    
595    
596     //////////////////////////////////////////////////////////////////////////////////////////////
597    
598     extern bool pxDialogExists( const wxString& name );
599     extern bool pxIsValidWindowPosition( const wxWindow& window, const wxPoint& windowPos );
600     extern wxRect wxGetDisplayArea();
601    
602     extern wxString pxFormatToolTipText( wxWindow* wind, const wxString& src );
603     extern void pxSetToolTip( wxWindow* wind, const wxString& src );
604     extern void pxSetToolTip( wxWindow& wind, const wxString& src );
605     extern wxFont pxGetFixedFont( int ptsize=8, int weight=wxNORMAL );
606    
607    
608     #endif

  ViewVC Help
Powered by ViewVC 1.1.22