wxRectTracker
RectTracker.h
Go to the documentation of this file.
00001 /* ****************************************************************************
00002  * RectTracker.h                                                              *
00003  *                                                                            *
00004  * (c) 2004-2012 - Rémi Peyronnet <remi+rphoto@via.ecp.fr>                    *
00005  *                                                                            *
00006  * wxEvtHandler adaptation from Troels K. (2011)                              *
00007  *                                                                            *
00008  * RectTracker was originally designed for RPhoto, but can be used separately *
00009  * It is a control similar to the CRectTracker of MS MFC.                     *
00010  *                                                                            *
00011  * Licence : wxWindows (based on L-GPL)                                       *
00012  *                                                                            *
00013  * ************************************************************************** */
00014 
00015 #ifndef __RECTTRACKER_H__
00016 #define __RECTTRACKER_H__
00017 
00018 #include <wx/wx.h>
00019 
00020 /// Handler position enum.
00021 enum RT_HANDLER
00022 {
00023    RT_HANDLER_OUTSIDE       = -1,
00024    RT_HANDLER_NONE          =  0,
00025    RT_HANDLER_TOP_MID       =  1,
00026    RT_HANDLER_MID_RIGHT     =  2,
00027    RT_HANDLER_BOTTOM_MID    =  4,
00028    RT_HANDLER_MID_LEFT      =  8,
00029    RT_HANDLER_TOP_LEFT      = RT_HANDLER_TOP_MID + RT_HANDLER_MID_LEFT,
00030    RT_HANDLER_TOP_RIGHT     = RT_HANDLER_TOP_MID + RT_HANDLER_MID_RIGHT,
00031    RT_HANDLER_BOTTOM_RIGHT  = RT_HANDLER_BOTTOM_MID + RT_HANDLER_MID_RIGHT,
00032    RT_HANDLER_BOTTOM_LEFT   = RT_HANDLER_BOTTOM_MID + RT_HANDLER_MID_LEFT
00033 };
00034 
00035 /// Current state of the wxRectTracker control. Currently only for internal use.
00036 enum RT_STATE
00037 {
00038    RT_STATE_NONE = 0,
00039    RT_STATE_DRAGGING = 1,
00040    RT_STATE_MOUSE_CAPTURED = 2,
00041    RT_STATE_DISABLED = 4,
00042    RT_STATE_FIRSTDRAG = 8
00043 };
00044 
00045 /// Mask to use with SetHandlerMask() to specify which handlers will be displayed.
00046 enum RT_MASK
00047 {
00048    /// Use this to hide handlers.
00049    RT_MASK_NONE         = 0x00,
00050    RT_MASK_TOP_LEFT     = 0x01,
00051    RT_MASK_TOP_MID      = 0x02,
00052    RT_MASK_TOP_RIGHT    = 0x04,
00053    RT_MASK_MID_RIGHT    = 0x08,
00054    RT_MASK_BOTTOM_RIGHT = 0x10,
00055    RT_MASK_BOTTOM_MID   = 0x20,
00056    RT_MASK_BOTTOM_LEFT  = 0x40,
00057    RT_MASK_MID_LEFT     = 0x80,
00058    /// Use this to show all handlers (default).
00059    RT_MASK_ALL          = 0xFF
00060 };
00061  
00062 /** wxRectTracker control
00063  * 
00064  * This control aims at providing same functionnalies as the MFC CRectTracker.
00065  * It is basically a selection rectangle with dragging capabilites, 
00066  *  to set its size and position.
00067  *
00068  * This control is a wxEvtHandler to not disturb Event chain and fix background issues
00069  * You must register it vist PushEventHandler and RemoveEventHandler
00070  *
00071  * Well nothing much to be said about this... or maybe :
00072  *  - The control defines two events EVT_TRACKER_CHANGED and EVT_TRACKER_CHANGING to be
00073  *     used in the parent hierarchy (for size and position display in a status bar, for instance)
00074  *  - The virtual function AdjustTrackerRect can be used to implement some 
00075  *     rules about the way the tracker should behave (typically, keeping a constant 
00076  *     width/height ratio, as in wxRectTrackerRatio)
00077  *  - You can set a max area with SetMaxRect()
00078  *  - You can get the current size/position with GetUnscrolledRect()
00079  *
00080  * To use this control, you have just to create it on the wxWindow you want ; it will register itself. 
00081  * Delete it to unregister it.
00082  *
00083  */ 
00084 class wxRectTracker : public wxEvtHandler
00085 {
00086     DECLARE_CLASS(wxRectTracker)
00087 public:
00088     /** wxRectTracker constructor
00089      *
00090      * @param wnd is the wxWindow accepting this widget
00091      * @param frame is the frame containing this widget for debugging purpose (will display its position in the statusbar)
00092      */
00093     wxRectTracker(wxWindow* wnd, wxFrame* frame = NULL);
00094     virtual ~wxRectTracker();
00095 
00096 public:
00097     /// Returns true if the provided coordinates are in the tracker. (Parent coo.)
00098     int HitTest(int x, int y) const;
00099     
00100     // Manipulation function -----------------------------------------------------
00101     /// Update the tracker position and size (usefull for initialisation)
00102     void Update();
00103     /// Get the coordinates of the area the tracker should not go beyond.
00104     wxRect GetMaxRect() const { return m_maxRect; };
00105     /// Set the maximum boundaries of the available space the tracker.
00106     void SetMaxRect(const wxRect& maxRect);
00107     /// Get the list of handlers to be displayed (See RT_MASK enum)
00108     int GetHandlerMask() const { return m_iHandlerMask; }
00109     /// Set which handlers will be displayed (See RT_MASK enum)
00110     void SetHandlerMask(int iMask = RT_MASK_ALL) { m_iHandlerMask = iMask; }
00111     /// Enable the Tracker
00112     void Enable();
00113     /// Disable the Tracker
00114     void Disable();
00115     /// Get the Status
00116     bool IsEnabled() const { return ((m_state & RT_STATE_DISABLED) == 0); };
00117     /// Get Appearance Status
00118     bool IsShown() const;
00119     /// Hide the Tracker
00120     void Hide();
00121 
00122     /// Get current position of the tracker
00123     wxRect GetTrackerRect() { return m_Rect; }
00124     /// Set a new position for the tracker
00125     void SetTrackerRect(const wxRect & rect) { m_Rect = rect; }
00126     
00127     // Scrolled Region Function --------------------------------------------------
00128     /** Get the current size and position of the tracker. 
00129      * If the tracker is on a wxScrolledWindow component, this is the absolute 
00130      * position (as if the control was not scrolled) 
00131      *
00132      * Deprecated with wxEvtHandler ; will be soon removed, use GetRect / SetRect instead.
00133      */
00134     wxRect GetUnscrolledRect() const;
00135     /// Set a new position for the tracker ; deprecated with wxEvtHandlet, do not use.
00136     virtual void SetUnscrolledRect(const wxRect& rect);
00137     /// Get the current position of the tracker, without taking in account any scroll area  ; deprecated with wxEvtHandlet, do not use.
00138     wxRect GetTrackerRect() const { return m_curRect; };
00139 
00140     /// Callback on Draw event
00141     virtual void OnDraw(wxDC*);
00142 
00143 protected:
00144     // Internals : Events
00145     DECLARE_EVENT_TABLE()
00146     // - Misc Events
00147     virtual void OnPaint(wxPaintEvent & event);
00148     virtual void OnKey(wxKeyEvent & event);
00149     virtual void OnMouseMotion(wxMouseEvent & event);
00150     virtual void OnMouseLeftDown(wxMouseEvent & event);
00151     virtual void OnMouseLeftUp(wxMouseEvent & event);
00152 
00153     // Helper Functions
00154     virtual void DrawRect(wxDC & dc, int x, int y, int w, int h);
00155     virtual void DrawRect(wxDC & dc, wxRect rect);
00156     virtual void DrawTracker(wxDC & dc, int x, int y, int w, int h);
00157     virtual void DrawTracker(wxDC & dc, const wxRect& rect);
00158     //void ForwardMouseEventToParent(wxMouseEvent & event);
00159 
00160 protected:
00161     // Behaviour Functions
00162     /** Adjust the behaviour of the tracker.
00163      * This virtual function allow you to specify special behaviour of the tracker
00164      *  as maintaining a constant ratio, keeping into a special area,...
00165      *
00166      * @param curRect [in, out] the asked position
00167      * @param handler [in] the handler used to ask this new position
00168      *
00169      * You get the new coordinates the user ask in curRect, with the handler he 
00170      * used. You can now process this information, and decide a new position that
00171      * you indicated as output in curRect (that is why this is not a const wxRect &).
00172      */
00173     virtual void AdjustTrackerRect(wxRect & curRect, int handler);
00174     /// Example of AdjustTrackerRect function
00175     void AdjustTrackerRectMax(wxRect & curRect, int handler);
00176 
00177 protected:
00178     int m_iHandlerWidth; /// Width of drawn handler
00179     int m_iHandlerMask;  /// Mask describing which handlers will be drawn
00180     int m_state;         /// Current state of the control (See RT_STATE)
00181 
00182     wxPoint m_leftClick; /// Coordinates of the last left clic
00183     wxPoint m_prevMove;  /// Coordinates of the previous move
00184     wxRect m_prevRect;   /// Coordinates of the previous calculated tracker
00185     wxRect m_curRect;    /// Coordinates of the current tracker
00186     wxRect m_Rect;       /// Coordinates of the current tracker
00187     wxCursor * m_cursorMove;
00188 
00189     /// maxRect : the tracker should not go beyond this rect (Parent Coo)
00190     wxRect m_maxRect;
00191     
00192     /// m_wnd : the window containing the widget
00193     wxWindow* m_wnd;
00194 
00195     /// m_frame : the frame containing the widget
00196     wxFrame* m_frame;
00197 };
00198 
00199 extern const wxEventType wxEVT_TRACKER_CHANGING;
00200 extern const wxEventType wxEVT_TRACKER_CHANGED;
00201 
00202 /// Event fired when the user has decided a new position for the tracker (dragging is finished)
00203 #define EVT_TRACKER_CHANGED(id, fn)\
00204  DECLARE_EVENT_TABLE_ENTRY(wxEVT_TRACKER_CHANGED, id,\
00205  wxID_ANY,\
00206  (wxObjectEventFunction) (wxEventFunction) (wxCommandEventFunction)& fn, NULL),
00207 
00208 /// Event fired when the user is being moving or resizing the tracker (dragging in process)
00209 #define EVT_TRACKER_CHANGING(id, fn)\
00210  DECLARE_EVENT_TABLE_ENTRY(wxEVT_TRACKER_CHANGING, id,\
00211  wxID_ANY,\
00212  (wxObjectEventFunction) (wxEventFunction) (wxCommandEventFunction)& fn, NULL),
00213 
00214 
00215 #endif // __RECTTRACKER_H__