EditorGadGet

Just starting out? Need help? Post your questions and find answers here.
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: EditorGadGet

Post by Shardik »

spacebuddy wrote:is there any way programmatically to select all the text in an EditorGadget?
This is a cross-platform example for selecting and deselecting all the text in an EditorGadget which I tested successfully on
- MacOS 10.6.8 x86
- Ubuntu 12.04 x64 with KDE and Unity
- Windows 7 x64 SP1

Code: Select all

EnableExplicit

Procedure SelectAll(EditorGadgetID.I, SelectAll.I)
  CompilerSelect #PB_Compiler_OS
     CompilerCase #PB_OS_Linux
       Protected TextBuffer.I
       Protected StartIter.GtkTextIter
       Protected EndIter.GtkTextIter

       TextBuffer = gtk_text_view_get_buffer_(GadgetID(EditorGadgetID))
       gtk_text_buffer_get_start_iter_(TextBuffer, @StartIter)
       gtk_text_buffer_get_end_iter_(TextBuffer, @EndIter)

       If SelectAll
         gtk_text_buffer_select_range_(TextBuffer, @StartIter, @EndIter)
       Else
         gtk_text_buffer_select_range_(TextBuffer, @StartIter, @StartIter)
       EndIf
     CompilerCase #PB_OS_MacOS
       Protected Range.NSRange

       If SelectAll
         CocoaMessage(0, GadgetID(EditorGadgetID), "selectAll:", 0)
       Else
         Range\length = 0
         CocoaMessage(0, GadgetID(EditorGadgetID), "setSelectedRange:@", @Range)
       EndIf
     CompilerCase #PB_OS_Windows
       If SelectAll
         SendMessage_(GadgetID(EditorGadgetID), #EM_SETSEL, 0, -1)
       Else
         SendMessage_(GadgetID(EditorGadgetID), #EM_SETSEL, 0, 0)
       EndIf
  CompilerEndSelect
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.")
ButtonGadget(1, 5, 100, 95, 25, "Select all")
ButtonGadget(2, 100, 100, 95, 25, "Deselect all")

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Break
    Case #PB_Event_Gadget
      Select EventGadget()
        Case 1
          SelectAll(0, #True)
        Case 2
          SelectAll(0, #False)
      EndSelect
  EndSelect
ForEver