Developer forums (C::B DEVELOPMENT STRICTLY!) > Development
Events
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.
ollydbg:
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();
}
--- End code ---
Here, the "tab smart jump" was implemented, that is mostly the same place you can implement your feature. Right?
thomas:
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".
Navigation
[0] Message Index
[#] Next page
Go to full version