Community
    • Login

    Get selected text CPP

    Scheduled Pinned Locked Moved Notepad++ & Plugin Development
    5 Posts 2 Posters 1.1k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • NanyaRurirosN
      NanyaRuriros
      last edited by

      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.

      Alan KilbornA 1 Reply Last reply Reply Quote 0
      • Alan KilbornA
        Alan Kilborn @NanyaRuriros
        last edited by Alan Kilborn

        @NanyaRuriros

        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.

        1 Reply Last reply Reply Quote 4
        • NanyaRurirosN
          NanyaRuriros
          last edited by

          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!

          Alan KilbornA 1 Reply Last reply Reply Quote 1
          • Alan KilbornA
            Alan Kilborn @NanyaRuriros
            last edited by Alan Kilborn

            @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 a std::string or a std::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.

            1 Reply Last reply Reply Quote 3
            • NanyaRurirosN
              NanyaRuriros
              last edited by

              Thank you :)

              1 Reply Last reply Reply Quote 1
              • First post
                Last post
              The Community of users of the Notepad++ text editor.
              Powered by NodeBB | Contributors