keybinder.h

Go to the documentation of this file.
00001 
00002 // Name:        keybinder.h
00003 // Purpose:     Classes for binding keypresses to commands.
00004 // Author:      Aleksandras Gluchovas
00005 // Modified by: Francesco Montorsi (6/1/2004)
00006 // Created:     2000/02/10
00007 // RCS-ID:      $Id: keybinder.h 443 2007-03-01 19:15:57Z frm $
00008 // Copyright:   (c) Aleksandras Gluchovas and (c) Francesco Montorsi
00009 // Licence:     wxWidgets licence
00011 
00012 
00013 #ifndef __KEYBINDER_G__
00014 #define __KEYBINDER_G__
00015 
00016 
00017 // required includes
00018 #include "wx/keybinderdef.h"
00019 #include "wx/panel.h"
00020 #include "wx/dialog.h"
00021 #include "wx/listbox.h"
00022 #include "wx/treectrl.h"
00023 #include "wx/menu.h"
00024 #include "wx/button.h"
00025 #include "wx/stattext.h"
00026 #include "wx/textctrl.h"
00027 #include "wx/combobox.h"
00028 #include "wx/app.h"
00029 
00030 
00031 // The maximum number of shortcuts associated with each wxCmd.
00032 #define wxCMD_MAX_SHORTCUTS             3
00033 
00034 #ifndef wxID_INVALID
00035 
00036     // A value used to represent an invalid id...
00037     #define wxID_INVALID                -1
00038 #endif
00039 
00040 // define the following to true to enable lots of debug messages
00041 #define wxKEYBINDER_DEBUG_MSG            0
00042 #if wxKEYBINDER_DEBUG_MSG
00043     #define wxKBLogDebug    wxLogDebug
00044 #else
00045     inline void wxKBLogDebug(const wxChar *WXUNUSED(fmt), ...) { /* do nothing */ }
00046 #endif
00047 
00048 
00049 // defined later...
00050 class wxConfigBase;
00051 class wxKeyBinder;
00052 
00053 
00054 
00065 class WXDLLIMPEXP_KEYBINDER wxKeyBind
00066 {
00067 protected:
00068 
00070     int m_nFlags;
00071 
00073     int m_nKeyCode;
00074 
00075 public:
00076 
00077     wxKeyBind() {
00078         m_nKeyCode = m_nFlags = -1;
00079     }
00080 
00081     wxKeyBind(int flags, int keycode) {
00082         Set(flags, keycode);
00083     }
00084 
00085     wxKeyBind(const wxKeyBind &tocopy) {
00086         DeepCopy(tocopy);
00087     }
00088 
00089     wxKeyBind(const wxString &key) {
00090         m_nFlags = StringToKeyModifier(key);
00091         m_nKeyCode = StringToKeyCode(key.AfterLast('+').AfterLast('-'));
00092     }
00093 
00094     virtual void DeepCopy(const wxKeyBind &p) {
00095         m_nFlags = p.m_nFlags;
00096         m_nKeyCode = p.m_nKeyCode;
00097     }
00098 
00099     virtual ~wxKeyBind() {}
00100 
00101 
00102 
00103 public:
00104 
00106     void Set(int flags, int keycode) {
00107         m_nFlags = flags;
00108         m_nKeyCode = keycode;
00109     }
00110 
00115     void Set(const wxKeyBind &key) {
00116         m_nFlags = key.m_nFlags;
00117         m_nKeyCode = key.m_nKeyCode;
00118     }
00119 
00121     bool MatchKey(const wxKeyEvent &key) const;
00122 
00124     bool Match(const wxKeyBind &key) const {
00125         if (m_nFlags == key.m_nFlags && m_nKeyCode == key.m_nKeyCode)
00126             return TRUE;
00127         return FALSE;
00128     }
00129 
00130 
00131 
00132     // Getters
00133     // ------------------
00134 
00135     int GetKeyCode() const {
00136         return m_nKeyCode;
00137     }
00138 
00139     int GetModifiers() const {
00140         return m_nFlags;
00141     }
00142 
00143     // Returns the string which describes this key combination.
00144     wxString GetStr() const {
00145         return KeyModifierToString(m_nFlags) +
00146             KeyCodeToString(m_nKeyCode);
00147     }
00148 
00149     wxAcceleratorEntry GetAccelerator(int cmdId) const {
00150         return wxAcceleratorEntry(GetModifiers(), GetKeyCode(), cmdId);
00151     }
00152 
00153 
00154 public:     // static utilities
00155 
00156     static wxString NumpadKeyCodeToString(int keyCode);
00157     static wxString KeyCodeToString(int keyCode);
00158     static wxString KeyModifierToString(int keyModifier);
00159 
00160     static int StringToKeyCode(const wxString &keyName);
00161     static int StringToKeyModifier(const wxString &keyModifier);
00162 
00163     static int GetKeyModifier(wxKeyEvent &event);
00164     static wxString GetKeyStrokeString(wxKeyEvent &event);
00165 };
00166 
00167 
00168 
00169 
00171 #define wxCMD_MAX_TYPES             16
00172 
00175 #define wxCMD_CONFIG_PREFIX         wxT("bind")
00176 
00177 
00187 class WXDLLIMPEXP_KEYBINDER wxCmd
00188 {
00189     // wxKeyBinder must be allowed to call #Exec()
00190     friend class wxKeyBinder;
00191 
00192 protected:
00193 
00195     wxKeyBind m_keyShortcut[wxCMD_MAX_SHORTCUTS];
00196     int m_nShortcuts;       
00197 
00199     wxString m_strName;
00200 
00202     wxString m_strDescription;
00203 
00205     int m_nId;
00206 
00207 protected:      // static
00208 
00211     typedef wxCmd *(*wxCmdCreationFnc)(int id);
00212 
00214     typedef struct {
00215         int type;
00216         wxCmdCreationFnc cmdCreateFnc;
00217     } wxCmdType;
00218 
00220     static wxCmdType m_arrCmdType[wxCMD_MAX_TYPES];
00221 
00223     static int m_nCmdTypes;
00224 
00225 
00226 public:     // static
00227 
00230     static wxCmd *CreateNew(int type, int id);
00231 
00233     static void AddCmdType(int type, wxCmdCreationFnc fnc);
00234 
00237     static wxCmdType *FindCmdType(int type);
00238 
00239 public:
00240 
00241     wxCmd(const wxKeyBind &first, int id,
00242         const wxString &name = wxEmptyString,
00243         const wxString &desc = wxEmptyString) {
00244         m_strName = name;
00245         m_strDescription = desc;
00246         m_nShortcuts = 1;
00247         m_keyShortcut[0] = first;
00248         m_nId = id;
00249     }
00250 
00251     wxCmd(int id = wxID_INVALID,
00252         const wxString &name = wxEmptyString,
00253         const wxString &desc = wxEmptyString) {
00254         m_strName = name;
00255         m_strDescription = desc;
00256         m_nId = id;
00257         m_nShortcuts = 0;
00258     }
00259 
00260     virtual void DeepCopy(const wxCmd *cmd) {
00261         m_strName = cmd->m_strName;
00262         m_strDescription = cmd->m_strDescription;
00263         m_nId = cmd->m_nId;
00264         m_nShortcuts = cmd->m_nShortcuts;
00265 
00266         for (int i=0; i < m_nShortcuts; i++)
00267             m_keyShortcut[i].DeepCopy(cmd->m_keyShortcut[i]);
00268     }
00269 
00270     virtual wxCmd *Clone() const = 0;
00271 
00272     // Destructor
00273     virtual ~wxCmd() {}
00274 
00275 
00276 public:
00277 
00278     // Add/Remove functions
00279     // ----------------------------
00280 
00282     void AddShortcut(const wxKeyBind &key) {
00283         if (m_nShortcuts >= wxCMD_MAX_SHORTCUTS) return;
00284         m_keyShortcut[m_nShortcuts++] = key;
00285         Update();
00286     }
00287 
00289     void AddShortcut(int flags, int keycode) {
00290         if (m_nShortcuts >= wxCMD_MAX_SHORTCUTS) return;
00291         wxKeyBind key(flags, keycode);
00292         AddShortcut(key);
00293         // update is called by the previous call
00294     }
00295 
00298     void AddShortcut(const wxString &key) {
00299         if (m_nShortcuts >= wxCMD_MAX_SHORTCUTS) return;
00300         if (key.IsEmpty()) return;
00301         m_keyShortcut[m_nShortcuts++] = wxKeyBind(key);
00302         Update();
00303     }
00304 
00306     void RemoveShortcut(int n) {
00307         for (int i=n; i < m_nShortcuts; i++)    // shift array left
00308             m_keyShortcut[i] = m_keyShortcut[i+1];
00309         m_nShortcuts--;
00310         Update();
00311     }
00312 
00314     void RemoveAllShortcuts() {
00315         m_nShortcuts=0;
00316         Update();
00317     }
00318 
00319 
00320 
00321     // Miscellaneous
00322     // ---------------------
00323 
00326     bool MatchKey(const wxKeyEvent &ev) const {
00327         for (int i=0; i < m_nShortcuts; i++)
00328             if (m_keyShortcut[i].MatchKey(ev))
00329                 return TRUE;
00330         return FALSE;
00331     }
00332 
00339     bool IsBindTo(const wxKeyBind &key, int *n = NULL) const {
00340         for (int i=0; i < m_nShortcuts; i++) {
00341             if (m_keyShortcut[i].Match(key)) {
00342                 if (n) *n = i;
00343                 return TRUE;
00344             }
00345         }
00346         return FALSE;
00347     }
00348 
00353     bool Save(wxConfigBase *p, const wxString &key = wxEmptyString, bool bCleanOld = FALSE) const;
00354 
00358     bool Load(wxConfigBase *p, const wxString &key = wxEmptyString);
00359 
00360 
00361 
00362 
00363     // Getters
00364     // ---------------------
00365 
00366     wxKeyBind *GetShortcut(int n)                { return &m_keyShortcut[n]; }
00367     const wxKeyBind *GetShortcut(int n) const    { return &m_keyShortcut[n]; }
00368 
00369     wxAcceleratorEntry GetAccelerator(int n) const {
00370         return GetShortcut(n)->GetAccelerator(m_nId);
00371     }
00372 
00373     int GetId() const {
00374         return m_nId;
00375     }
00376 
00377     int GetShortcutCount() const {
00378         return m_nShortcuts;
00379     }
00380 
00381     wxString GetDescription() const {
00382         return m_strDescription;
00383     }
00384 
00385     wxString GetName() const {
00386         return m_strName;
00387     }
00388 
00389     wxArrayString GetShortcutsList() const {
00390         wxArrayString arr;
00391         for (int i=0; i < m_nShortcuts; i++)
00392             arr.Add(m_keyShortcut[i].GetStr());
00393         return arr;
00394     }
00395 
00397     virtual int GetType() const = 0;
00398 
00399 
00400 protected:
00401 
00404     virtual void Update() {}
00405 
00411     virtual void Exec(wxObject *obj, wxEvtHandler *client) = 0;
00412 };
00413 
00414 
00415 
00416 
00421 class WXDLLIMPEXP_KEYBINDER wxCmdArray
00422 {
00423     wxArrayPtrVoid m_arr;
00424 
00425 public:
00426     wxCmdArray() {}
00427     wxCmdArray(const wxCmdArray &tocopy) { DeepCopy(tocopy); }
00428     virtual ~wxCmdArray() { Clear(); }
00429 
00430     void DeepCopy(const wxCmdArray &arr) {
00431         Clear();
00432         for (int i=0; i < arr.GetCount(); i++)
00433             Add(arr.Item(i)->Clone());
00434     }
00435 
00436     wxCmdArray &operator=(const wxCmdArray &tocopy) {
00437         DeepCopy(tocopy);
00438         return *this;
00439     }
00440 
00441     void Add(wxCmd *p)          { m_arr.Add(p); }
00442     void Remove(int n);
00443     void Clear();
00444 
00445     int GetCount() const        { return m_arr.GetCount(); }
00446     wxCmd *Item(int n) const    { return (wxCmd *)m_arr.Item(n); }
00447 };
00448 
00449 
00450 
00451 
00459 class WXDLLIMPEXP_KEYBINDER wxBinderEvtHandler : public wxEvtHandler
00460 {
00462     wxKeyBinder *m_pBinder;
00463 
00466     wxWindow *m_pTarget;
00467 
00468 protected:
00469 
00471     void OnChar(wxKeyEvent &event);
00472 
00473 public:
00474 
00477     wxBinderEvtHandler(wxKeyBinder *p, wxWindow *tg)
00478         : m_pBinder(p), m_pTarget(tg) { m_pTarget->PushEventHandler(this); }
00479 
00482     virtual ~wxBinderEvtHandler()
00483         { m_pTarget->RemoveEventHandler(this); }
00484 
00485 
00487     bool IsAttachedTo(wxWindow *p)
00488         { return p == m_pTarget; }
00489 
00491     wxKeyBinder *GetBinder() const
00492         { return m_pBinder; }
00493 
00495     wxWindow *GetTargetWnd() const
00496         { return m_pTarget; }
00497 
00498 private:
00499     DECLARE_CLASS(wxBinderEvtHandler)
00500     DECLARE_EVENT_TABLE()
00501 };
00502 
00503 
00504 
00505 
00506 // Define the wxADD_KEYBINDER_SUPPORT: you should use it inside a protected
00507 // block of the windows which want to use the wxKeyBinder facilities
00508 #ifdef __WXMSW__
00509 
00510     // This is a virtual function used in wxMSW which can be used to allow
00511     // or disable the message preprocessing and thus the preprocessing of
00512     // keyshortcuts; to add keybinder support, the preprocessing must be
00513     // disabled: wxKeyBinder will care of checking the keypresses for
00514     // eventual hotkeys...
00515     #define wxADD_KEYBINDER_SUPPORT()                                       \
00516         virtual bool MSWShouldPreProcessMessage(WXMSG*) { return FALSE; }
00517 #else
00518 
00519     // the other ports doesn't use MSWShouldPreProcessMessage...
00520     #define wxADD_KEYBINDER_SUPPORT()           /* expand to nothing */
00521 
00522 #endif
00523 
00524 
00531 class WXDLLIMPEXP_KEYBINDER wxBinderApp : public wxApp
00532 {
00533     wxKeyBinder *m_pGlobalBinder;
00534     wxEvtHandler *m_pGlobalHdl;
00535 
00536 public:
00537     wxBinderApp() { m_pGlobalHdl = NULL; m_pGlobalBinder = NULL; }
00538     virtual ~wxBinderApp() {}
00539 
00540 
00542     int FilterEvent(wxEvent &ev);
00543 
00546     static bool IsChildOf(wxWindow *parent, wxWindow *child);
00547 
00550     static wxWindow *GetTopLevelParent(wxWindow *wnd);
00551 
00552 
00553 public:     // accessors
00554 
00555     void SetGlobalBinder(wxKeyBinder *p)
00556         { m_pGlobalBinder = p; }
00557     void SetGlobalHandler(wxEvtHandler *p)
00558         { m_pGlobalHdl = p; }
00559 
00560     wxKeyBinder *GetGlobalBinder() const
00561         { return m_pGlobalBinder; }
00562     wxEvtHandler *GetGlobalHandler() const
00563         { return m_pGlobalHdl ; }
00564 };
00565 
00566 
00567 
00585 class WXDLLIMPEXP_KEYBINDER wxKeyBinder : public wxObject
00586 {
00587 protected:
00588 
00590     wxCmdArray m_arrCmd;
00591 
00599     //wxArrayPtrVoid m_arrAttachedWnd;
00600     //wxWindowList m_lstAttachedWnd;        // I don't like wxList...
00601 
00603     wxArrayPtrVoid m_arrHandlers;
00604 
00605 protected:
00606 
00607 
00609     int FindCmd(int id) const {
00610         for (int i=0; i < (int)m_arrCmd.GetCount(); i++)
00611             if (id == m_arrCmd.Item(i)->GetId())
00612                 return i;
00613         return -1;
00614     }
00615 
00618     int FindCmdBindTo(const wxKeyBind &key, int *n = NULL) const {
00619         for (int i=0; i < (int)m_arrCmd.GetCount(); i++)
00620             if (m_arrCmd.Item(i)->IsBindTo(key, n))
00621                 return i;
00622         return -1;
00623     }
00624 
00627     int FindMatchingCmd(const wxKeyEvent &key) const {
00628         for (int i=0; i < (int)m_arrCmd.GetCount(); i++)
00629             if (m_arrCmd.Item(i)->MatchKey(key))
00630                 return i;
00631         return -1;
00632     }
00633 
00634 
00635 public:
00636 
00637     wxKeyBinder() {}
00638     wxKeyBinder(const wxKeyBinder &tocopy) { DeepCopy(tocopy); }
00639     virtual ~wxKeyBinder() { DetachAll(); }
00640 
00641 
00642 
00643 public:     // miscellaneous
00644 
00646     void OnChar(wxKeyEvent &event, wxEvtHandler *next);
00647 
00649     void DeepCopy(const wxKeyBinder &p) {
00650         m_arrCmd.DeepCopy(p.m_arrCmd);
00651 
00652         // NEVER COPY THE ARRAY OF THE ATTACHED WINDOWs:
00653         // WE ARE NOT ATTACHED TO THE WINDOWS OF THE GIVEN BINDER !!
00654         // m_arrAttachedWnd = p->m_arrAttachedWnd;
00655     }
00656 
00657     wxKeyBinder &operator=(const wxKeyBinder &tocopy) {
00658         DeepCopy(tocopy);
00659         return *this;
00660     }
00661 
00663     void Reset() {
00664         m_arrCmd.Clear();
00665     }
00666 
00668     void UpdateAllCmd() {
00669         if (m_arrHandlers.GetCount() == 0)
00670             return;     // we are not attached to any window... we can skip
00671                         // this update...
00672         for (int i=0; i < (int)m_arrCmd.GetCount(); i++)
00673             m_arrCmd.Item(i)->Update();
00674     }
00675 
00678     void Enable(bool bEnable = TRUE) {
00679         for (int i=0; i < (int)m_arrHandlers.GetCount(); i++)
00680             ((wxBinderEvtHandler*)m_arrHandlers.Item(i))->SetEvtHandlerEnabled(bEnable);
00681     }
00682 
00684     void Attach(wxWindow *p);
00685 
00688     void AttachRecursively(wxWindow *p);
00689 
00691     void Detach(wxWindow *p);
00692 
00694     void DetachAll();
00695 
00697     bool IsAttachedTo(wxWindow *p) const
00698         { return FindHandlerIdxFor(p) != wxNOT_FOUND; }
00699 
00701     int FindHandlerIdxFor(wxWindow *p) const;
00702 
00704     wxBinderEvtHandler *FindHandlerFor(wxWindow *p) const;
00705 
00708     void ImportMenuBarCmd(wxMenuBar *p);
00709 
00714     bool Save(wxConfigBase *p, const wxString &key = wxEmptyString, bool bCleanOld = FALSE) const;
00715 
00717     bool Load(wxConfigBase *p, const wxString &key = wxEmptyString);
00718 
00719 
00720 
00721     // Add functions
00722     // -------------------
00723 
00724     void AddCmd(wxCmd *p) {
00725         m_arrCmd.Add(p);
00726     }
00727 
00728     void AddShortcut(int id, const wxString &key) {
00729         wxCmd *p = GetCmd(id);
00730         if (p) p->AddShortcut(key);
00731     }
00732 
00733     void AddShortcut(int id, const wxKeyBind &key) {
00734         wxCmd *p = GetCmd(id);
00735         if (p) p->AddShortcut(key);
00736     }
00737 
00738 
00739 
00740     // Getters
00741     // -------------------
00742 
00743     int GetCmdCount() const {
00744         return m_arrCmd.GetCount();
00745     }
00746 
00747     int GetAttachedWndCount() const {
00748         return m_arrHandlers.GetCount();
00749     }
00750 
00751     wxWindow *GetWindow(int n) const {
00752         return ((wxBinderEvtHandler*)m_arrHandlers.Item(n))->GetTargetWnd();
00753     }
00754 
00755     wxCmd *GetMatchingCmd(const wxKeyEvent &key) const {
00756         int i = FindMatchingCmd(key);
00757         if (i != -1)
00758             return m_arrCmd.Item(i);
00759         return NULL;
00760     }
00761 
00762     wxCmd *GetCmd(int id) const {
00763         int i = FindCmd(id);
00764         if (i != -1)
00765             return m_arrCmd.Item(i);
00766         return NULL;
00767     }
00768 
00769     wxCmd *GetCmdBindTo(const wxString &key, int *n = NULL) const {
00770         int i = FindCmdBindTo(wxKeyBind(key), n);
00771         if (i != -1)
00772             return m_arrCmd.Item(i);
00773         return NULL;
00774     }
00775 
00776     wxKeyBind *GetShortcut(int id, int n) const {
00777         wxCmd *p = GetCmd(id);
00778         if (p) return p->GetShortcut(n);
00779         return NULL;
00780     }
00781 
00782     wxString GetShortcutStr(int id, int n) const {
00783         wxKeyBind *p = GetShortcut(id, n);
00784         if (p) return p->GetStr();
00785         return wxEmptyString;
00786     }
00787 
00788     wxArrayString GetShortcutsList(int id) const {
00789         wxCmd *p = GetCmd(id);
00790         if (p) return p->GetShortcutsList();
00791         return wxArrayString();
00792     }
00793 
00794     wxArrayPtrVoid *GetHandlersArr()        { return &m_arrHandlers; }
00795 
00796     wxCmdArray *GetArray()                  { return &m_arrCmd; }
00797     const wxCmdArray *GetArray() const      { return &m_arrCmd; }
00798 
00799 
00800 private:
00801     DECLARE_CLASS(wxKeyBinder)
00802 };
00803 
00804 
00805 
00811 class WXDLLIMPEXP_KEYBINDER wxKeyProfile : public wxKeyBinder
00812 {
00813 protected:
00814 
00816     wxString m_strName;
00817 
00819     wxString m_strDescription;
00820 
00821 public:
00822     wxKeyProfile(const wxString &name = wxEmptyString,
00823                 const wxString &desc = wxEmptyString)
00824         : m_strName(name), m_strDescription(desc) {}
00825 
00826     wxKeyProfile(const wxKeyProfile &tocopy)
00827         { DeepCopy(tocopy); }
00828 
00829     virtual ~wxKeyProfile() {}
00830 
00831 
00832     void DeepCopy(const wxKeyProfile &p) {
00833         wxKeyBinder::DeepCopy(p);
00834         m_strName = p.m_strName;
00835         m_strDescription = p.m_strDescription;
00836     }
00837 
00838     wxKeyProfile &operator=(const wxKeyProfile &tocopy) {
00839         DeepCopy(tocopy);
00840         return *this;
00841     }
00842 
00843 public:     // miscellaneous
00844 
00845     void SetName(const wxString &name) { m_strName=name; }
00846     void SetDesc(const wxString &str)  { m_strDescription=str; }
00847 
00848     wxString GetName() const        { return m_strName; }
00849     wxString GetDesc() const        { return m_strDescription; }
00850 
00851     bool Save(wxConfigBase *p, const wxString &key = wxEmptyString, bool bCleanOld = FALSE) const;
00852     bool Load(wxConfigBase *p, const wxString &key = wxEmptyString);
00853 
00854 
00855 private:
00856     DECLARE_CLASS(wxKeyProfile)
00857 };
00858 
00859 
00860 
00861 
00864 #define wxKEYPROFILE_CONFIG_PREFIX          wxT("keyprof")
00865 
00866 
00881 class WXDLLIMPEXP_KEYBINDER wxKeyProfileArray
00882 {
00886     wxArrayPtrVoid m_arr;
00887 
00889     int m_nSelected;
00890 
00891 public:
00892     wxKeyProfileArray() { m_nSelected=-1; }
00893     wxKeyProfileArray(const wxKeyProfileArray &tocopy) { DeepCopy(tocopy); }
00894     virtual ~wxKeyProfileArray() { Cleanup(); }
00895 
00896 
00901 
00902     int GetCount() const            { return m_arr.GetCount(); }
00903     int GetSelProfileIdx() const    { return m_nSelected; }
00904     wxKeyProfile *Item(int n)       { return (wxKeyProfile*)m_arr.Item(n); }
00905     wxKeyProfile *GetSelProfile()   { wxASSERT(m_nSelected >= 0 && m_nSelected < GetCount()); return Item(m_nSelected); }
00906     void Add(wxKeyProfile *p)       { m_arr.Add(p); }
00907     void Clear()                    { m_arr.Clear(); }
00908     void Remove(wxKeyProfile *p)    { m_arr.Remove(p); }
00909     void SetSelProfile(int n)       { wxASSERT(n < GetCount()); m_nSelected = n; }
00910     bool IsEmpty() const            { return m_arr.IsEmpty(); }
00911 
00912     const wxKeyProfile *Item(int n) const           { return (wxKeyProfile*)m_arr.Item(n); }
00913     const wxKeyProfile *GetSelProfile() const       { return Item(m_nSelected); }
00914     void RemoveAt(size_t i, size_t count = 1)       { m_arr.RemoveAt(i, count); }
00915     void Insert(wxKeyProfile *p, int n)             { m_arr.Insert(p, n); }
00916 
00918 
00919 
00921     void DeepCopy(const wxKeyProfileArray &p) {
00922         Cleanup();
00923         for (int i=0; i < p.GetCount(); i++)
00924             Add(new wxKeyProfile(*p.Item(i)));
00925         m_nSelected = p.m_nSelected;
00926     }
00927 
00928     wxKeyProfileArray &operator=(const wxKeyProfileArray &tocopy) {
00929         DeepCopy(tocopy);
00930         return *this;
00931     }
00932 
00936     void Cleanup() {
00937         for (int i=0; i < GetCount(); i++)
00938             delete Item(i);
00939         Clear();
00940     }
00941 
00946     void AttachAllTo(wxWindow *w) {
00947         for (int i=0; i<GetCount(); i++)
00948             Item(i)->Attach(w);
00949     }
00950 
00952     void EnableAll(bool bEnable = TRUE) {
00953         for (int i=0; i<GetCount(); i++)
00954             Item(i)->Enable(bEnable);
00955     }
00956 
00958     void DetachAllFrom(wxWindow *w) {
00959         for (int i=0; i<GetCount(); i++)
00960             Item(i)->Detach(w);
00961     }
00962 
00964     void DetachAll() {
00965         for (int i=0; i<GetCount(); i++)
00966             Item(i)->DetachAll();
00967     }
00968 
00970     void UpdateAllCmd() {
00971         for (int i=0; i<GetCount(); i++)
00972             Item(i)->UpdateAllCmd();
00973     }
00974 
00979     bool Save(wxConfigBase *p, const wxString &key = wxEmptyString, bool bCleanOld = FALSE) const;
00980 
00984     bool Load(wxConfigBase *p, const wxString &key = wxEmptyString);
00985 };
00986 
00987 
00988 
00989 
00990 
00991 
01001 class WXDLLIMPEXP_KEYBINDER wxKeyMonitorTextCtrl : public wxTextCtrl
01002 {
01003 public:
01004     wxKeyMonitorTextCtrl(
01005         wxWindow* parent,
01006         wxWindowID id,
01007         const wxString& value = wxEmptyString,
01008         const wxPoint& pos = wxDefaultPosition,
01009         const wxSize& size = wxDefaultSize,
01010         long style = wxTE_PROCESS_ENTER,
01011         const wxValidator& validator = wxDefaultValidator,
01012         const wxString& name = wxTextCtrlNameStr) :
01013         wxTextCtrl(parent, id, value, pos, size, style, validator, name) {}
01014 
01015     virtual ~wxKeyMonitorTextCtrl() {}
01016 
01017 public:
01018 
01021     void OnKey(wxKeyEvent &);
01022 
01024     bool IsValidKeyComb() const {
01025         return !GetValue().IsEmpty() && GetValue().Last() != '+';
01026     }
01027 
01028 private:
01029     DECLARE_CLASS(wxKeyMonitorTextCtrl)
01030     DECLARE_EVENT_TABLE()
01031 };
01032 
01033 
01034 
01035 
01036 
01037 
01038 // IDs used by wxKeyConfigPanel
01039 #define wxKEYBINDER_BASEID                  30000       // start from this value
01040 
01041 #define wxKEYBINDER_COMMANDS_BOX_ID         wxKEYBINDER_BASEID+1
01042 #define wxKEYBINDER_BINDINGS_BOX_ID         wxKEYBINDER_BASEID+2
01043 #define wxKEYBINDER_KEY_FIELD_ID            wxKEYBINDER_BASEID+3
01044 #define wxKEYBINDER_ASSIGN_KEY_ID           wxKEYBINDER_BASEID+4
01045 #define wxKEYBINDER_REMOVE_KEY_ID           wxKEYBINDER_BASEID+5
01046 #define wxKEYBINDER_REMOVEALL_KEY_ID        wxKEYBINDER_BASEID+6
01047 #define wxKEYBINDER_KEYPROFILES_ID          wxKEYBINDER_BASEID+7
01048 #define wxKEYBINDER_CATEGORIES_ID           wxKEYBINDER_BASEID+8
01049 #define wxKEYBINDER_ADD_PROFILEBTN_ID       wxKEYBINDER_BASEID+9
01050 #define wxKEYBINDER_REMOVE_PROFILEBTN_ID    wxKEYBINDER_BASEID+10
01051 
01052 
01053 #define wxKEYBINDER_SELECTED_POSTFIX        wxT(" (selected)")
01054 
01055 
01059 
01064 #define wxKEYBINDER_USE_TREECTRL                1
01065 
01070 #define wxKEYBINDER_USE_LISTBOX                 2
01071 
01074 #define wxKEYBINDER_SHOW_APPLYBUTTON            4
01075 
01078 #define wxKEYBINDER_SHOW_ADDREMOVE_PROFILE      8
01079 
01082 #define wxKEYBINDER_ENABLE_PROFILE_EDITING      16
01083 
01085 #define wxKEYBINDER_DEFAULT_STYLE                                               \
01086         (wxKEYBINDER_SHOW_APPLYBUTTON|wxKEYBINDER_USE_TREECTRL|                 \
01087          wxKEYBINDER_SHOW_ADDREMOVE_PROFILE|wxKEYBINDER_ENABLE_PROFILE_EDITING| \
01088          wxTAB_TRAVERSAL)   /* wxTAB_TRAVERSAL is the default style for a wxPanel */
01089 
01091 
01092 
01147 class WXDLLIMPEXP_KEYBINDER wxKeyConfigPanel : public wxPanel
01148 {
01149 public:
01150 
01152     wxKeyConfigPanel(wxWindow* parent,
01153                      wxWindowID id = -1,
01154                      const wxPoint& pos = wxDefaultPosition,
01155                      const wxSize& size = wxDefaultSize,
01156                      long style = wxKEYBINDER_DEFAULT_STYLE,
01157                      const wxString& name = wxT("wxKeyConfigPanel"))
01158     {
01159         Create(parent, id, pos, size, style, name);
01160     }
01161 
01162     bool Create(wxWindow* parent,
01163                 wxWindowID id = -1,
01164                 const wxPoint& pos = wxDefaultPosition,
01165                 const wxSize& size = wxDefaultSize,
01166                 long style = wxKEYBINDER_DEFAULT_STYLE,
01167                 const wxString& name = wxT("wxKeyConfigPanel"));
01168 
01169     virtual ~wxKeyConfigPanel();
01170 
01171 
01172 public:     // import commands (to call BEFORE ShowModal):
01173             // they affect the tree control only
01174 
01181     virtual void ImportMenuBarCmd(wxMenuBar *menuitems,
01182                                 const wxString &rootname = wxT("Menu bar"));
01183 
01190     virtual void ImportKeyProfileCmd(const wxKeyProfile &toimport,
01191                                 const wxString &rootname = wxT("Commands"));
01192 
01193 
01194 public:     // keyprofile utilities (to call BEFORE ShowModal):
01195             // they affect the keybindings only
01196 
01202     virtual void AddProfile(const wxKeyProfile &p);
01203 
01208     virtual void AddProfiles(const wxKeyProfileArray &arr);
01209 
01211     virtual void SetSelProfile(int n);
01212 
01213 
01214 public:     // output-access utilities (to call AFTER ShowModal)
01215 
01217     wxKeyProfile *GetProfile(int n) const
01218         { wxASSERT(m_pKeyProfiles); return (wxKeyProfile *)m_pKeyProfiles->GetClientData(n); }
01219 
01222     wxKeyProfile *GetSelProfile() const
01223         { int n=GetSelProfileIdx(); return (n >= 0 ? GetProfile(n) : NULL); }
01224 
01232     int GetSelProfileIdx() const
01233         { wxASSERT(m_pKeyProfiles); return m_nCurrentProf; }
01234 
01243     wxKeyProfileArray GetProfiles() const;
01244 
01245 
01246 public:     // miscellaneous
01247 
01251     virtual void ApplyChanges();
01252 
01256     void EnableKeyProfiles(bool bEnable = TRUE);
01257 
01260     void DisableKeyProfiles()
01261         { EnableKeyProfiles(FALSE); }
01262 
01263 
01264 protected:      // event handlers
01265 
01267     void OnIdle(wxIdleEvent &event);
01268 
01269     void OnTreeCommandSelected(wxTreeEvent &event);
01270     void OnListCommandSelected(wxCommandEvent &event);
01271 
01272     void OnBindingSelected(wxCommandEvent &event);
01273     void OnProfileSelected(wxCommandEvent &event);
01274     void OnCategorySelected(wxCommandEvent &event);
01275 
01276     void OnProfileEditing(wxCommandEvent &event);
01277     void OnApplyChanges(wxCommandEvent &event);
01278     void OnAssignKey(wxCommandEvent &event);
01279     void OnRemoveKey(wxCommandEvent &event);
01280     void OnRemoveAllKey(wxCommandEvent &event);
01281     void OnAddProfile(wxCommandEvent &event);
01282     void OnRemoveProfile(wxCommandEvent &event);
01283 
01285     void OnKeyPressed(wxCommandEvent &event);
01286 
01287 
01288 protected:      // build functions; these ones can be overridden to
01289                 // customize wxKeyConfigPanel appearances
01290 
01291     virtual void BuildCtrls();
01292     virtual wxSizer *BuildColumn1();
01293     virtual wxSizer *BuildColumn2();
01294     virtual wxSizer *BuildMain(wxSizer *, wxSizer *, bool);
01295 
01296 
01297 protected:      // utilities
01298 
01299     virtual void UpdateButtons();
01300     virtual void UpdateDesc();
01301     virtual void FillInBindings();
01302     virtual void Reset();
01303     virtual void AddRootIfMissing(const wxString &rootname);
01304 
01305 
01307     wxTreeItemId GetSelCmdId() const;
01308 
01310     wxControl *GetMainCtrl() const;
01311 
01314     bool IsUsingTreeCtrl() const
01315         { return HasFlag(wxKEYBINDER_USE_TREECTRL); }
01316 
01318     bool IsSelectedValidCmd() const;
01319 
01321     wxString GetSelCmdStr() const;
01322 
01324     wxCmd *GetSelCmd() const;
01325 
01328     virtual void ShowSizer(wxSizer *toshow, bool show);
01329 
01330 
01331 protected:      // members
01332 
01335     bool m_bEnableKeyProfiles;
01336 
01338     wxCmd *m_pCurrCmd;
01339 
01342     wxKeyProfile m_kBinder;
01343 
01345     int m_nCurrentProf;
01346 
01352     bool m_bProfileHasBeenModified;
01353 
01354 protected:      // the subwindows of this dialog
01355 
01357     wxKeyMonitorTextCtrl *m_pKeyField;
01358 
01359     wxButton *m_pAssignBtn;
01360     wxButton *m_pRemoveBtn;
01361     wxButton *m_pRemoveAllBtn;
01362 
01363     // used when wxKEYBINDER_USE_TREECTRL is in the build flags
01364     wxTreeCtrl *m_pCommandsTree;
01365 
01366     // used when wxKEYBINDER_USE_LISTBOX is in the build flags
01367     wxComboBox *m_pCategories;
01368     wxListBox *m_pCommandsList;
01369 
01370     wxListBox *m_pBindings;
01371     wxComboBox *m_pKeyProfiles;
01372     wxSizer *m_pKeyProfilesSizer;
01373 
01374 #ifdef __WXGTK__
01375     // on wxGTK wxStaticText does not implement line wrapping
01376     // (which is required !!) so we will use a wxTextCtrl (on wxGTK)
01377     wxTextCtrl  *m_pDescLabel;
01378 #else
01379     wxStaticText *m_pDescLabel;
01380 #endif
01381     wxStaticText *m_pCurrCmdField;
01382 
01383 private:
01384     DECLARE_CLASS(wxKeyConfigPanel)
01385     DECLARE_EVENT_TABLE()
01386 };
01387 
01388 
01389 
01390 #endif // __KEYBINDER_G__
01391 

Generated on Sat Mar 10 18:30:36 2007 for Keybinder by  doxygen 1.4.7