Developer forums (C::B DEVELOPMENT STRICTLY!) > CodeCompletion redesign

hook "tab" key

<< < (4/6) > >>

Loaden:

--- Quote from: MortenMacFly on March 26, 2010, 08:43:22 pm ---The same like this:

--- Code: ---Index: src/include/cbstyledtextctrl.h
+    switch (GetLexer())
+    {
+        case wxSCI_LEX_CPP:
+        {
+            if (style != wxSCI_C_STRING && style != wxSCI_C_CHARACTER)
+                return true;
+        }
+        case wxSCI_LEX_D:
+        {
+            if (style != wxSCI_D_STRING && style != wxSCI_D_CHARACTER)
+                return true;
+        }
+    }
+    return false;
+}

--- End code ---
???
:lol: :lol: :lol:

--- End quote ---
Haha, you're right! Thank you. :P

Jenna:
Why not this (and get rid of the if-statements) :


--- Code: ---Index: src/include/cbstyledtextctrl.h
+    switch (GetLexer())
+    {
+        case wxSCI_LEX_CPP:
+        {
+            return !(style == wxSCI_C_STRING || style == wxSCI_C_CHARACTER);
+        }
+        case wxSCI_LEX_D:
+        {
+            return !(style == wxSCI_D_STRING || style == wxSCI_D_CHARACTER);
+        }
+    }
+    return false;
+}
--- End code ---
?

Loaden:

--- Quote from: MortenMacFly on March 26, 2010, 08:39:09 pm ---
--- Quote from: Loaden on March 26, 2010, 08:36:00 am ---Here, I do not quite understand: Why did not directly break it, because at the end of the function, there implementation: 'event.Skip ();' ?

--- End quote ---
Yes, but there are computations in between which can be avoided. Namely event.GetKeyCode(); and the switch computation.

--- End quote ---
Sorry, I still do not quite understand.
If change the code like this:

--- Code: ---void cbStyledTextCtrl::OnKeyUp(wxKeyEvent& event)
{
    if (!AllowTabSmartJump())
    {
        event.Skip();
        return;
    }
    const int keyCode = event.GetKeyCode();
    switch (keyCode)
    {
...
    }

    event.Skip();
}
--- End code ---

We do not have access to ‘event.GetKeyCode()’ value and 'switch’ calculation, BUT, we need call 'AllowTabSmartJump' each time.
Here, the time occupied may be greater.

Loaden:

--- Quote from: jens on March 27, 2010, 12:36:04 am ---Why not this (and get rid of the if-statements) :


--- Code: ---Index: src/include/cbstyledtextctrl.h
+    switch (GetLexer())
+    {
+        case wxSCI_LEX_CPP:
+        {
+            return !(style == wxSCI_C_STRING || style == wxSCI_C_CHARACTER);
+        }
+        case wxSCI_LEX_D:
+        {
+            return !(style == wxSCI_D_STRING || style == wxSCI_D_CHARACTER);
+        }
+    }
+    return false;
+}
--- End code ---
?

--- End quote ---
cool! :D

Loaden:
Add an interface for external use :

--- Code: ---+        void SetTabSmartJump(bool enable = true) { m_tabSmartJump = enable; }
+
--- End code ---

And according to everyone's tips for perfect 'AllowTabSmartJump' function.

--- Code: ---Index: src/include/cbstyledtextctrl.h
===================================================================
--- src/include/cbstyledtextctrl.h (revision 6196)
+++ src/include/cbstyledtextctrl.h (working copy)
@@ -19,14 +19,20 @@
         cbStyledTextCtrl(wxWindow* pParent, int id, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize, long style = 0);
         virtual ~cbStyledTextCtrl();
         wxDateTime GetLastFocusTime() const {return m_lastFocusTime;}
+        void SetTabSmartJump(bool enable = true) { m_tabSmartJump = enable; }
+
     private:
         void OnContextMenu(wxContextMenuEvent& event);
         void OnKillFocus(wxFocusEvent& event);
         void OnGetFocus(wxFocusEvent& event);
         void OnGPM(wxMouseEvent& event);
+        void OnKeyDown(wxKeyEvent& event);
+        void OnKeyUp(wxKeyEvent& event);
+        bool AllowTabSmartJump();
 
         wxWindow* m_pParent;
         wxLongLong m_lastFocusTime;
+        bool m_tabSmartJump;
 
         DECLARE_EVENT_TABLE()
 };
Index: src/sdk/cbstyledtextctrl.cpp
===================================================================
--- src/sdk/cbstyledtextctrl.cpp (revision 6196)
+++ src/sdk/cbstyledtextctrl.cpp (working copy)
@@ -18,17 +18,23 @@
 #include "editorbase.h" // DisplayContextMenu
 #include "prep.h" // platform::gtk
 
+static const wxString s_leftBrace(_T("([{'\""));
+static const wxString s_rightBrace(_T(")]}'\""));
+
 BEGIN_EVENT_TABLE(cbStyledTextCtrl, wxScintilla)
     EVT_CONTEXT_MENU(cbStyledTextCtrl::OnContextMenu)
     EVT_KILL_FOCUS(cbStyledTextCtrl::OnKillFocus)
     EVT_MIDDLE_DOWN(cbStyledTextCtrl::OnGPM)
     EVT_SET_FOCUS(cbStyledTextCtrl::OnGetFocus)
+    EVT_KEY_DOWN(cbStyledTextCtrl::OnKeyDown)
+    EVT_KEY_UP(cbStyledTextCtrl::OnKeyUp)
 END_EVENT_TABLE()
 
 cbStyledTextCtrl::cbStyledTextCtrl(wxWindow* pParent, int id, const wxPoint& pos, const wxSize& size, long style)
     : wxScintilla(pParent, id, pos, size, style),
     m_pParent(pParent),
-    m_lastFocusTime(0L)
+    m_lastFocusTime(0L),
+    m_tabSmartJump(false)
 {
     //ctor
 }
@@ -103,3 +109,115 @@
         SetSelectionVoid(start, end);
     }
 } // end of OnGPM
+
+void cbStyledTextCtrl::OnKeyDown(wxKeyEvent& event)
+{
+    int keyCode = event.GetKeyCode();
+    switch (keyCode)
+    {
+        case WXK_TAB:
+        {
+            if (m_tabSmartJump)
+            {
+                if (s_rightBrace.Find(GetCharAt(GetCurrentPos())) == wxNOT_FOUND)
+                {
+                    m_tabSmartJump = false;
+                }
+                else
+                {
+                    CharRight();
+                    return;
+                }
+            }
+        }
+        break;
+
+        case WXK_BACK:
+        {
+            if (m_tabSmartJump)
+            {
+                const int pos = GetCurrentPos();
+                const int index = s_leftBrace.Find(GetCharAt(pos - 1));
+                if (index != wxNOT_FOUND && GetCharAt(pos) == s_rightBrace.GetChar(index))
+                {
+                    CharRight();
+                    DeleteBack();
+                }
+            }
+        }
+        break;
+
+        case WXK_ESCAPE:
+        {
+            if (m_tabSmartJump)
+            {
+                m_tabSmartJump = false;
+                CharLeft();
+                Tab();
+                return;
+            }
+        }
+        break;
+
+        case WXK_RETURN:
+        {
+            m_tabSmartJump = false;
+        }
+        break;
+    }
+
+    event.Skip();
+}
+
+void cbStyledTextCtrl::OnKeyUp(wxKeyEvent& event)
+{
+    const int keyCode = event.GetKeyCode();
+    switch (keyCode)
+    {
+        case _T('['):   // [ {
+        case _T('\''):  // ' "
+        case _T('9'):   // (
+        {
+            if (!AllowTabSmartJump()) break;
+
+            wxChar ch = keyCode;
+            if (event.ShiftDown())
+            {
+                if (keyCode == _T('\'')) ch = _T('"');
+                else if (keyCode == _T('9')) ch = _T('(');
+                else if (keyCode == _T('[')) ch = _T('{');
+            }
+
+            int index = s_leftBrace.Find(ch);
+            if (index != wxNOT_FOUND && GetCharAt(GetCurrentPos()) == s_rightBrace.GetChar(index))
+                m_tabSmartJump = true;
+            else if (keyCode == _T('\'')) // ' "
+                m_tabSmartJump = false;
+        }
+        break;
+
+        case _T(']'):   // ] }
+        case _T('0'):   // )
+        {
+            if (!AllowTabSmartJump()) break;
+            if (keyCode == _T('0') && !event.ShiftDown()) break;
+            m_tabSmartJump = false;
+        }
+        break;
+    }
+
+    event.Skip();
+}
+
+bool cbStyledTextCtrl::AllowTabSmartJump()
+{
+    int style = GetStyleAt(GetCurrentPos() - 2);
+    switch (GetLexer())
+    {
+        case wxSCI_LEX_CPP:
+            return !(style == wxSCI_C_STRING || style == wxSCI_C_CHARACTER);
+        case wxSCI_LEX_D:
+            return !(style == wxSCI_D_STRING || style == wxSCI_D_CHARACTER);
+    }
+    return false;
+}

--- End code ---

[attachment deleted by admin]

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version