Page 1 of 1

GetSelectedText

Posted: Sat Feb 22, 2025 1:28 pm
by mk-soft

Code: Select all

;-TOP by mk-soft, v1.01.1, 22.02.2025

Procedure.s GetSelectedText(Gadget, Hidden_Chars = #False)
  Protected r1.s, *TextBuffer, StartTextIter.GtkTextIter, EndTextIter.GtkTextIter,*gchar
  
  *TextBuffer = gtk_text_view_get_buffer_(GadgetID(Gadget))
  If *TextBuffer
    gtk_text_buffer_get_selection_bounds_(*TextBuffer, @StartTextIter, @EndTextIter)
    *gchar = gtk_text_buffer_get_text_(*TextBuffer, @StartTextIter, @EndTextIter, Hidden_Chars)
    If *gchar
      r1 = PeekS(*gchar, -1, #PB_UTF8)
      g_free_(*gchar)
    EndIf
  EndIf
  ProcedureReturn r1
EndProcedure

CompilerIf #PB_Compiler_IsMainFile
  
  Procedure UpdateWindow()
    Protected dx, dy
    dx = WindowWidth(0)
    dy = WindowHeight(0) - StatusBarHeight(0) - MenuHeight()
    ; Resize Gadgets
    ResizeGadget(0, 0, 0, dx, dy)
  EndProcedure
  
  Procedure Main()
    Protected dx, dy
    
    #WinStyle = #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_MaximizeGadget | #PB_Window_MinimizeGadget
    
    If OpenWindow(0, #PB_Ignore, #PB_Ignore, 600, 400, "Test Window", #WinStyle)
      ; MenuBar
      CreateMenu(0, WindowID(0))
      MenuTitle("&File")
      MenuItem(99, "E&xit")
      MenuTitle("&Edit")
      MenuItem(1, "Selected Text")
      
      ; StatusBar
      CreateStatusBar(0, WindowID(0))
      AddStatusBarField(#PB_Ignore)
      
      ; Gadgets
      dx = WindowWidth(0)
      dy = WindowHeight(0) - StatusBarHeight(0) - MenuHeight()
      EditorGadget(0, 0, 0, dx, dy)
      ; Bind Events
      BindEvent(#PB_Event_SizeWindow, @UpdateWindow(), 0)
      
      ; Main Loop
      Repeat
        Select WaitWindowEvent()
          Case #PB_Event_CloseWindow
            Select EventWindow()
              Case 0
                Break
            EndSelect
            
          Case #PB_Event_Menu
            Select EventMenu()
              Case 1
                Debug GetSelectedText(0)
                
              Case 99
                PostEvent(#PB_Event_CloseWindow, 0, 0)
                
            EndSelect
            
          Case #PB_Event_Gadget
            Select EventGadget()
                
            EndSelect
            
        EndSelect
      ForEver
      
    EndIf
    
  EndProcedure : Main()
  
CompilerEndIf

Re: GetSelectedText

Posted: Sat Mar 08, 2025 11:16 pm
by Mijikai
Thank you that will come in handy.