Get selected text CPP
-
HI!
I am trying to select text using c++, i used to work with c# and i used the following:
int length = (int)Win32.SendMessage(PluginBase.GetCurrentScintilla(), SciMsg.SCI_GETSELTEXT, 0, 0);
IntPtr ptrToText = Marshal.AllocHGlobal(length + 1);
Win32.SendMessage(PluginBase.GetCurrentScintilla(), SciMsg.SCI_GETSELTEXT, length, ptrToText);
string textAnsi = Marshal.PtrToStringAnsi(ptrToText);the first part using c++ will look like:
int length = SendMessage(nppData._scintillaMainHandle, SCI_GETSELTEXT, 0, 0 );But I don’t know how to “translate” the rest… is there an easy way to get/set the current selected text using c++?
Thanks in advance.
-
is there an easy way to get/set the current selected text using c++?
A good reference for calling Scintilla functions and working with the results is … drum roll … the Notepad++ source code itself.
For what you’re asking about, maybe see HERE, which shows an example of getting the selected text and then setting it to something else.
This specific example is from Notepad++'s Edit menu > Convert Case to > … commands.
Those commands get the selected text from Scintilla, convert the case in local C++ buffers, and then hand the data back to Scintilla by setting the text. -
HI!
Thanks for your response, it was pretty interesting to check the source code of n++ :O, currently I am trying the following:int selectionEnd = SendMessage(nppData._scintillaMainHandle, SCI_GETSELECTIONEND, 0, 0); int selectionStart = SendMessage(nppData._scintillaMainHandle, SCI_GETSELECTIONSTART, 0, 0); char* selection; if ((selectionEnd > selectionStart)) { selection = new char [selectionEnd - selectionStart +1]; SendMessage(nppData._scintillaMainHandle,SCI_GETSELTEXT,0, reinterpret_cast<LPARAM>(selection)); }
I think is enough to obtain the current selected test, but also I need the last part:
string textAnsi = Marshal.PtrToStringAnsi(ptrToText);
I want to edit the current selected text as a string.
Thanks! -
@NanyaRuriros said in Get selected text CPP:
I want to edit the current selected text as a string
Well…go ahead with that. :-)
So, from your code you have the string in a “C-style” string, which is a memory buffer that has a zero-byte at the end. You can manipulate that buffer.
But I sense that isn’t what you’re looking for. As a C# programmer, you want everything “easy”. Well, in the C/C++ world, things are often not so easy. :-)
You could certainly get your data into astd::string
or astd::wstring
, to gain more “easiness”. :-)I notice that your buffer is a
char *
one.
Depending upon what you’re doing, that may be fine.
But it certainly precludes any powerful Unicode handling of the data you are retrieving.
The example code I pointed you to earlier is more appropriate for that kind (Unicode) of handling. -
Thank you :)