News:

Accounts with zero posts and zero activity during the last months will be deleted periodically to fight SPAM!

Main Menu

Events

Started by Alpha, November 05, 2011, 04:42:56 PM

Previous topic - Next topic

Alpha

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.

Alpha

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).

oBFusCATed

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!]

ollydbg

You can have a look at the file "sdk\cbstyledtextctrl.cpp"
Especially the function:
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.

thomas

#4
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".
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

Pecan

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

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

Alpha

Thank you everyone for your responses :).

Quote from: oBFusCATed on November 08, 2011, 01:41:17 AM
... 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?

Quote from: ollydbg on November 08, 2011, 01:59:11 AM
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.

Quote from: thomas on November 08, 2011, 09:22:18 AM
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).

Quote from: Pecan 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.next.codeblocks.org/index.php/topic,9980.msg70445.html#msg70445
Thanks, I will take a look at that.

oBFusCATed

Quote from: Alpha on November 08, 2011, 11:26:54 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!]

smallB

#8
@Alpha I think it's great idea. Something like Alt + Bracket_Type would disambiguate situation.

Alpha

Alt + Bracket_Type sounds like another good option; I will try it as well.

Quote from: oBFusCATed on November 08, 2011, 11:40:05 PM
Quote from: Alpha on November 08, 2011, 11:26:54 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
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 :)).

daniloz

Quote from: Alpha on November 10, 2011, 12:35:58 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...

Jenna

Most likely not and the same is for many other languages I guess.
German layout uses AltGr+8  for "[" and AltGr+7 for "{".

smallB

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!

Ceniza

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.

smallB

Cannot wait to use it! :shock: