Linux get_selection_text

Just starting out? Need help? Post your questions and find answers here.
User avatar
Piero
Addict
Addict
Posts: 1040
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Linux get_selection_text

Post by Piero »

Does this work on Linux?
Should I allocate/free memory for a procedure like this?
Thanks

Code: Select all

ImportC ""
   gtk_text_buffer_get_selection_content(*buffer.GtkTextBuffer)
EndImport

Procedure.s get_selection_text(gadget)
   Protected b = gtk_text_buffer_get_selection_content(gtk_text_view_get_buffer_(GadgetID(gadget))
   ProcedureReturn PeekS(b, -1, #PB_UTF8)
EndProcedure

OpenWindow(0, 270, 100, 200, 130, "EditorGadget")
EditorGadget(0, 10, 10, 180, 80, #PB_Editor_WordWrap)
SetGadgetText(0, "The quick brown fox jumps over the lazy dog.")

StartIter.GtkTextIter ; select all
EndIter.GtkTextIter
TextBuffer = gtk_text_view_get_buffer_(GadgetID(0))
gtk_text_buffer_get_start_iter_(TextBuffer, @StartIter)
gtk_text_buffer_get_end_iter_(TextBuffer, @EndIter)
gtk_text_buffer_select_range_(TextBuffer, @StartIter, @EndIter)

Debug get_selection_text(0)

; Repeat:Select WaitWindowEvent():Case #PB_Event_CloseWindow:Break:EndSelect:ForEver
User avatar
mk-soft
Always Here
Always Here
Posts: 6315
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Linux get_selection_text

Post by mk-soft »

gtk_text_buffer_get_selection_content(*buffer.GtkTextBuffer) is deprecated !

You always have to look for the GTK3 documentation.
gtk_text_buffer_get_text:
The caller of the method takes ownership of the returned data, and is responsible for freeing it.

Code: Select all

Procedure.s get_selection_text(gadget, hidden_char = #False)
  Protected r1.s, *textBuffer, startIter.GtkTextIter, endIter.GtkTextIter,*gchar
  
  *textBuffer = gtk_text_view_get_buffer_(GadgetID(gadget))
  If *textBuffer
    gtk_text_buffer_get_selection_bounds_(*textBuffer, @startIter, @endIter)
    *gchar = gtk_text_buffer_get_text_(*textBuffer, @startIter, @endIter, hidden_char)
    If *gchar
      r1 = PeekS(*gchar, -1, #PB_UTF8)
      g_free_(*gchar)
    EndIf
  EndIf
  ProcedureReturn r1
EndProcedure
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Piero
Addict
Addict
Posts: 1040
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: Linux get_selection_text

Post by Piero »

mk-soft wrote: Sat Feb 22, 2025 12:56 pmYou always have to look for the GTK3 documentation.
What can I say, if not…
THANKS MK-SOFT!

PS: Does your procedure need an ImportC? It seems_not_!

PPS: As you saw, I cannot test on Linux now, but, probably soon, I will be able to test even on Windows (from my Mac; I'm cleaning up my SSD to upgrade MacOS and install VMs :mrgreen:)

PPPS: I already have all the old, pre-OSX Mac OSs (ooh… the good old times…); hope they will work in Sequoia… :lol:
Post Reply