Move to a position in an EditGadget

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Move to a position in an EditGadget

Post by Shardik »

marcoagpinto wrote:I posted in the forum asking for help but the reply I got only works in Windows.

http://www.purebasic.fr/english/viewtop ... 13&t=56193
Sorry, but that's not quite true. Danilo posted in that thread a procedure Editor_SetCursorPos() for MacOS X and in a later posting I posted a cross-platform example for Linux, MacOS X and Windows which lets you select a text in a ListIconGadget, find that text in an EditorGadget and select that text. If these code examples didn't measure up to your expectations, why did you post
marcoagpinto wrote:Thank you guys for all the help :)
in your last posting of that thread and didn't ask further questions?
Danilo wrote:- A completely cross-platform way to obtain an EditorGadget's selection start, end, range, set the selection, insert into the selection, activating word wrap, ...
If you follow the link inside your linked posting, you will find out that a lot of procedures in Erich's code are still not implemented (especially for MacOS X, but also some for Linux). So it's still some work left for these routines to become "completely cross-platform"... :wink:


@Marco,
as a new try to help you I have put together a simple cross-platform example for positioning the cursor. All words of the text in the EditorGadget are displayed in a ListIconGadget together with their respective offset and you may select a word in the ListIconGadget by double-clicking it or by selecting it and clicking on the button "Set cursor". Upon this action the cursor will be positioned in front of the chosen word in the EditorGadget.

On MacOS X there is one thing which will not work: you won't see the cursor if setting it to position 0. All other positions will work. If selecting text (only one character is sufficient) the cursor will even be shown on position 0 on MacOS X!

Code: Select all

Enumeration 
  #ListIcon
  #Button
  #Editor
EndEnumeration

Procedure SetCursor(EditorGadgetID.I, Position.I)
  SetActiveGadget(#Editor)

  CompilerSelect #PB_Compiler_OS
    CompilerCase #PB_OS_Linux
      Protected PositionIter.GtkTextIter
      Protected TextBuffer.I

      TextBuffer = gtk_text_view_get_buffer_(GadgetID(EditorGadgetID))
      gtk_text_buffer_get_iter_at_offset_(TextBuffer, @PositionIter, Position)
      gtk_text_buffer_place_cursor_(TextBuffer, PositionIter)
    CompilerCase #PB_OS_MacOS
      Protected Range.NSRange
      Range\location = Position
      CocoaMessage(0, GadgetID(EditorGadgetID), "setSelectedRange:@", @Range)
      CocoaMessage(0, GadgetID(EditorGadgetID), "scrollRangeToVisible:@", @Range)
    CompilerCase #PB_OS_Windows
      SendMessage_(GadgetID(EditorGadgetID), #EM_SETSEL, Position, Position) 
  CompilerEndSelect
EndProcedure

OpenWindow(0, 270, 100, 200, 230, "Set cursor")
ListIconGadget(#ListIcon, 10, 10, WindowWidth(0) - 20, 90, "Word", 80,
  #PB_ListIcon_FullRowSelect)
AddGadgetColumn(#ListIcon, 1, "Position",
  GadgetWidth(#ListIcon) - GetGadgetItemAttribute(#ListIcon, 0,
  #PB_ListIcon_ColumnWidth, 0) - 24)
ButtonGadget(#Button, 40, GadgetY(#ListIcon) + GadgetHeight(#ListIcon) + 12, 120,
  25, "Set cursor")
EditorGadget(#Editor, 10, GadgetY(#Button) + GadgetHeight(#Button) + 10,
  WindowWidth(0) - 20,
  WindowHeight(0) - GadgetY(#Button) - GadgetHeight(#Button) - 20,
  #PB_Editor_ReadOnly | #PB_Editor_WordWrap)

; ----- Enable word wrap for Linux (workaround for Linux bug in PB 5.21)
CompilerIf #PB_Compiler_OS = #PB_OS_Linux
  gtk_text_view_set_wrap_mode_(GadgetID(#Editor), #GTK_WRAP_WORD)
CompilerEndIf

SetGadgetText(#Editor, "The quick brown fox jumps over the lazy dog.")

AddGadgetItem(#ListIcon, -1, "The" + #LF$ + "0")
AddGadgetItem(#ListIcon, -1, "quick" + #LF$ + "4")
AddGadgetItem(#ListIcon, -1, "brown" + #LF$ + "10")
AddGadgetItem(#ListIcon, -1, "fox" + #LF$ + "16")
AddGadgetItem(#ListIcon, -1, "jumps" + #LF$ + "20")
AddGadgetItem(#ListIcon, -1, "over" + #LF$ + "26")
AddGadgetItem(#ListIcon, -1, "the" + #LF$ + "31")
AddGadgetItem(#ListIcon, -1, "lazy" + #LF$ + "35")
AddGadgetItem(#ListIcon, -1, "dog" + #LF$ + "40")

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Break
    Case #PB_Event_Gadget
      Select EventGadget()
        Case #ListIcon
          If EventType() = #PB_EventType_LeftDoubleClick
            SetCursor(#Editor, Val(GetGadgetItemText(#ListIcon,
              GetGadgetState(#ListIcon), 1)))
          EndIf
        Case #Button
          If GetGadgetState(#ListIcon) >= 0
            SetCursor(#Editor, Val(GetGadgetItemText(#ListIcon,
              GetGadgetState(#ListIcon), 1)))
          EndIf
      EndSelect
  EndSelect
ForEver