Author Topic: Events  (Read 19418 times)

Offline Alpha

  • Developer
  • Lives here!
  • *****
  • Posts: 1513
Events
« on: November 05, 2011, 04:42:56 pm »
I have been trying to add a function to Code::Blocks that will retrieve the currently selected text when the user presses a key (for example, 'a').  Inside cbEditor::CreateEditor(), I have tried wxEVT_SCI_CHARADDED, however, the function is called after the charecter is inserted, so the selected text will already have been deleted.  I also tried wxEVT_SCI_KEY and EVT_KEY_DOWN, but they were never called (should they have been, and I incorrectly implemented it?).

Could someone kindly let me know if sdk\cbEditor.cpp is even the correct file to be working in, and point me to the correct event to use (and maybe give an example code snippit in case I am doing this completely wrong)?

Thank you.

Offline Alpha

  • Developer
  • Lives here!
  • *****
  • Posts: 1513
Re: Events
« Reply #1 on: November 08, 2011, 01:00:11 am »
Hmm... I guess I did not exactly give any indication of what I am trying to do.

In Code::Blocks, a feature I would like to use is an extension to brace completion: if a section of code is selected when one of the following is keys is pressed:
( ) [ ] < > "
the selected text will be surrounded by (instead of being replaced by) the corresponding symbols.  When { or } are pressed, the line(s) the selected text is (are) on will be indented, and braces added on the lines before and after the indented section.

This did not seem to be extremely difficult to do (but still useful, at least to me), however, as I mentioned above, I am having the difficulty of registering the correct event.  If someone could enlighten me, I am sure that I could complete this modification (and I would be very grateful, as I have spent multiple hours reading event documentation to no avail).

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: Events
« Reply #2 on: November 08, 2011, 01:41:17 am »
Why don't you do it as a menu command? And an advanced version, showing a dialog to enter a value.
For example I want to be able to wrap strings in wxT("...."), _("...."),
so if I type in the dialog wxT, or _ the plugin will wrap it automatically.

But I guess, that you need to interrupt the event before wxScintilla has processed it, not after.
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5916
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: Events
« Reply #3 on: November 08, 2011, 01:59:11 am »
You can have a look at the file "sdk\cbstyledtextctrl.cpp"
Especially the function:
Code
void cbStyledTextCtrl::OnKeyDown(wxKeyEvent& event)
{
    switch (event.GetKeyCode())
    {
        case WXK_TAB:
        {
            if (m_tabSmartJump && !(event.ControlDown() || event.ShiftDown() || event.AltDown()))
            {
                if (!AutoCompActive() && m_bracePosition != wxSCI_INVALID_POSITION)
                {
                    m_lastPosition = GetCurrentPos();
                    GotoPos(m_bracePosition);

                    // Need judge if it's the final brace
                    HighlightRightBrace();
                    if (!m_tabSmartJump && CallTipActive())
                        CallTipCancel();
                    return;
                }
            }
        }
        break;

        case WXK_BACK:
        {
            if (m_tabSmartJump)
            {
                if (!(event.ControlDown() || event.ShiftDown() || event.AltDown()))
                {
                    const int pos = GetCurrentPos();
                    const int index = s_leftBrace.Find((wxChar)GetCharAt(pos - 1));
                    if (index != wxNOT_FOUND && (wxChar)GetCharAt(pos) == s_rightBrace.GetChar(index))
                    {
                        CharRight();
                        DeleteBack();
                    }
                }
                else if (m_lastPosition != wxSCI_INVALID_POSITION && event.ControlDown())
                {
                    GotoPos(m_lastPosition);
                    m_lastPosition = wxSCI_INVALID_POSITION;
                    return;
                }
            }
        }
        break;

        case WXK_RETURN:
        case WXK_ESCAPE:
        {
            if (m_tabSmartJump)
                m_tabSmartJump = false;
        }
        break;
    }

    event.Skip();
}
Here, the "tab smart jump" was implemented, that is mostly the same place you can implement your feature. Right?
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: Events
« Reply #4 on: November 08, 2011, 09:22:18 am »
This is a feature I've wished I had had many times in the past. Actually it's something I still wish I had, about 20 times per day :)

One just needs to be very careful that it works the way people expect, and that's where it gets complicated.

Most people will expect that if they have a text selected and press a key such as {, then the selection will be erased and replaced with a single character. That's how it works in pretty much every editor, anywhere.

vi probably has a key for such a thing, and if vi doesn't, then emacs probably does (they have a keys for every crap, just I can't remember any of them). Might be worthwile to look there, and use that combination, so it's "somewhat standards compliant".
« Last Edit: November 08, 2011, 09:26:03 am by thomas »
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

Offline Pecan

  • Plugin developer
  • Lives here!
  • ****
  • Posts: 2778
Re: Events
« Reply #5 on: November 08, 2011, 02:30:13 pm »
There's a lot of code here that might help you intercept/insert keys and characters.

KeyMacs
http://forums.codeblocks.org/index.php/topic,9980.msg70445.html#msg70445

Offline Alpha

  • Developer
  • Lives here!
  • *****
  • Posts: 1513
Re: Events
« Reply #6 on: November 08, 2011, 11:26:54 pm »
Thank you everyone for your responses :).

... And an advanced version, showing a dialog to enter a value.
For example I want to be able to wrap strings in wxT("...."), _("...."),
so if I type in the dialog wxT, or _ the plugin will wrap it automatically.
This sounds like an additional feature that would be quite useful.  Would you suggest I try to add this dialog to the core application, or as a plugin?

You can have a look at the file "sdk\cbstyledtextctrl.cpp"
...
Here, the "tab smart jump" was implemented, that is mostly the same place you can implement your feature. Right?
Yes, I think this is exactly what I was looking for.

Most people will expect that if they have a text selected and press a key such as {, then the selection will be erased and replaced with a single character. That's how it works in pretty much every editor, anywhere.

vi probably has a key for such a thing, and if vi doesn't, then emacs probably does (they have a keys for every crap, just I can't remember any of them). Might be worthwile to look there, and use that combination, so it's "somewhat standards compliant".
I know that I would find it most efficient to have this bound to the actual key, however others (I assume) would prefer it as a shortcut, or not at all.  It is in my plan to have some sort of setting to choose between these - and I will do some reading on vi and emacs to find out what their key combination(s) is (are).

There's a lot of code here that might help you intercept/insert keys and characters.

KeyMacs
http://forums.codeblocks.org/index.php/topic,9980.msg70445.html#msg70445
Thanks, I will take a look at that.

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: Events
« Reply #7 on: November 08, 2011, 11:40:05 pm »
This sounds like an additional feature that would be quite useful.  Would you suggest I try to add this dialog to the core application, or as a plugin?
Your priority should be this:
0. script <--biggest priority here
1. plugin
2. core
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline smallB

  • Almost regular
  • **
  • Posts: 193
Re: Events
« Reply #8 on: November 09, 2011, 09:54:25 am »
@Alpha I think it's great idea. Something like Alt + Bracket_Type would disambiguate situation.
« Last Edit: November 09, 2011, 12:00:49 pm by smallB »

Offline Alpha

  • Developer
  • Lives here!
  • *****
  • Posts: 1513
Re: Events
« Reply #9 on: November 10, 2011, 12:35:58 am »
Alt + Bracket_Type sounds like another good option; I will try it as well.

This sounds like an additional feature that would be quite useful.  Would you suggest I try to add this dialog to the core application, or as a plugin?
Your priority should be this:
0. script <--biggest priority here
1. plugin
2. core
OK; I probably will not work on this until I have completed the original modification though.

As a note, I do not know if I will have any time to work on this before the weekend (although, I am feeling a bit of pressure now that I know a bunch of other people like it :)).

Offline daniloz

  • Regular
  • ***
  • Posts: 268
Re: Events
« Reply #10 on: November 10, 2011, 08:08:31 am »
Alt + Bracket_Type sounds like another good option; I will try it as well.

Please make it configurable, since I have a swiss german keyboard and the "[" and "{" brackets are obtained via Alt_Gr + ü/ä, so I'm not sure if Alt + Alt_Gr + ü/ä would work. I'm afraid not...

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7255
Re: Events
« Reply #11 on: November 10, 2011, 10:46:15 am »
Most likely not and the same is for many other languages I guess.
German layout uses AltGr+8  for "[" and AltGr+7 for "{".

Offline smallB

  • Almost regular
  • **
  • Posts: 193
Re: Events
« Reply #12 on: November 10, 2011, 12:08:37 pm »
Yes, that definitely should be configurable. But "full configurability" should be a part of every feature not just this one. I as I user should be able to configure IDE (look and behavior) in a way that will suits me best.
That's why I so much love C++ - almost unconstrained freedom!

Offline Ceniza

  • Developer
  • Lives here!
  • *****
  • Posts: 1441
    • CenizaSOFT
Re: Events
« Reply #13 on: November 10, 2011, 12:12:33 pm »
Visual Assist X has something like this, and it can be quite handy. It also allows you to comment/uncomment by selecting something and typing either / or *. Make it a setting, so there's no need to also press an extra key.

Offline smallB

  • Almost regular
  • **
  • Posts: 193
Re: Events
« Reply #14 on: November 10, 2011, 12:53:29 pm »
Cannot wait to use it! :shock: