Jumping to chr position in an EditorGadget

Just starting out? Need help? Post your questions and find answers here.
User avatar
marcoagpinto
Addict
Addict
Posts: 939
Joined: Sun Mar 10, 2013 3:01 pm
Location: Portugal
Contact:

Jumping to chr position in an EditorGadget

Post by marcoagpinto »

Hello!

How do I do it for Linux and Mac?

Code: Select all

Procedure move_to_text_position(t)
  
  CompilerIf #PB_Compiler_OS=#PB_OS_Windows
    Debug "Windows"
    SendMessage_(GadgetID(#EDITOR_GADGET_WINDOW_ADD_EDIT_WORD), #EM_SETSEL, t, t)
    SetActiveGadget(#EDITOR_GADGET_WINDOW_ADD_EDIT_WORD)
  CompilerEndIf
  CompilerIf #PB_Compiler_OS=#PB_OS_Linux
    Debug "Linux"
    MessageRequester("Warning!","Doesn't work in Linux yet.",#PB_MessageRequester_Warning)
  CompilerEndIf
  CompilerIf #PB_Compiler_OS=#PB_OS_MacOS
    Debug "Mac"
    MessageRequester("Warning!","Doesn't work in Mac yet.",#PB_MessageRequester_Warning)
  CompilerEndIf               
  
EndProcedure

Thanks!
mestnyi
Addict
Addict
Posts: 995
Joined: Mon Nov 25, 2013 6:41 am

Re: Jumping to chr position in an EditorGadget

Post by mestnyi »

How do I do it for Linux and Mac?

Code: Select all

DeclareModule Caret
  Declare SetPos(Gadget, Position)
EndDeclareModule

Module Caret
  Procedure SetPos(Gadget, Position)
    ;     SetActiveGadget(Gadget)
    
    CompilerSelect #PB_Compiler_OS
      CompilerCase #PB_OS_Linux
        If GadgetType( Gadget) = #PB_GadgetType_Editor
          Protected PositionIter.GtkTextIter
          Protected TextBuffer = gtk_text_view_get_buffer_(GadgetID(Gadget))
          gtk_text_buffer_get_iter_at_offset_(TextBuffer, @PositionIter, Position)
          gtk_text_buffer_place_cursor_(TextBuffer, PositionIter)
        Else
          gtk_editable_set_position_( GadgetID(Gadget), Position )
        EndIf
        
      CompilerCase #PB_OS_MacOS
        Protected Range.NSRange : Range\location = Position
        CocoaMessage(0, GadgetID(Gadget), "setSelectedRange:@", @Range)
        CocoaMessage(0, GadgetID(Gadget), "scrollRangeToVisible:@", @Range)
        
      CompilerCase #PB_OS_Windows
        SendMessage_(GadgetID(Gadget), #EM_SETSEL, Position, Position) 
        
    CompilerEndSelect
  EndProcedure
EndModule

Macro SetCaretPos(_gadget_, _position_)
  Caret::SetPos(_gadget_, _position_)
EndMacro

User avatar
marcoagpinto
Addict
Addict
Posts: 939
Joined: Sun Mar 10, 2013 3:01 pm
Location: Portugal
Contact:

Re: Jumping to chr position in an EditorGadget

Post by marcoagpinto »

What is wrong with this?

It doesn't work in Ubuntu.

Code: Select all

; Place the cursor position in a text gadget
;
; Help from the PB forum user: mestnyi - https://www.purebasic.fr/english/viewtopic.php?f=13&t=72277
;
; V1.0 - 13/FEB/2019

  Procedure SetPos(Gadget, Position)
   
    CompilerSelect #PB_Compiler_OS
      CompilerCase #PB_OS_Linux
        If GadgetType( Gadget) = #PB_GadgetType_Editor
          Protected PositionIter.GtkTextIter
          Protected TextBuffer = gtk_text_view_get_buffer_(GadgetID(Gadget))
          gtk_text_buffer_get_iter_at_offset_(TextBuffer, @PositionIter, Position)
          gtk_text_buffer_place_cursor_(TextBuffer, PositionIter)
        Else
          gtk_editable_set_position_( GadgetID(Gadget), Position )
        EndIf
       
      CompilerCase #PB_OS_MacOS
        Protected Range.NSRange : Range\location = Position
        CocoaMessage(0, GadgetID(Gadget), "setSelectedRange:@", @Range)
        CocoaMessage(0, GadgetID(Gadget), "scrollRangeToVisible:@", @Range)
       
      CompilerCase #PB_OS_Windows
        SendMessage_(GadgetID(Gadget), #EM_SETSEL, Position, Position)
       
    CompilerEndSelect
    
  EndProcedure
mestnyi
Addict
Addict
Posts: 995
Joined: Mon Nov 25, 2013 6:41 am

Re: Jumping to chr position in an EditorGadget

Post by mestnyi »

Do you make him active?

Code: Select all

; Place the cursor position in a text gadget
;
; Help from the PB forum user: mestnyi - https://www.purebasic.fr/english/viewtopic.php?f=13&t=72277
;
; V1.0 - 13/FEB/2019

  Procedure SetPos(Gadget, Position)
    CompilerSelect #PB_Compiler_OS
      CompilerCase #PB_OS_Linux
        If GadgetType( Gadget) = #PB_GadgetType_Editor
          Protected PositionIter.GtkTextIter
          Protected TextBuffer = gtk_text_view_get_buffer_(GadgetID(Gadget))
          gtk_text_buffer_get_iter_at_offset_(TextBuffer, @PositionIter, Position)
          gtk_text_buffer_place_cursor_(TextBuffer, PositionIter)
        Else
          gtk_editable_set_position_( GadgetID(Gadget), Position )
        EndIf
       
      CompilerCase #PB_OS_MacOS
        Protected Range.NSRange : Range\location = Position
        CocoaMessage(0, GadgetID(Gadget), "setSelectedRange:@", @Range)
        CocoaMessage(0, GadgetID(Gadget), "scrollRangeToVisible:@", @Range)
       
      CompilerCase #PB_OS_Windows
        SendMessage_(GadgetID(Gadget), #EM_SETSEL, Position, Position)
       
    CompilerEndSelect
    
  EndProcedure
  
  
  If OpenWindow(0, 0, 0, 322, 150, "EditorGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    EditorGadget(0, 8, 8, 306, 133)
    For a = 0 To 5
      AddGadgetItem(0, a, "Line "+Str(a))
    Next
    
    SetActiveGadget(0)
    SetPos(0, 5)
    
    Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
  EndIf
User avatar
marcoagpinto
Addict
Addict
Posts: 939
Joined: Sun Mar 10, 2013 3:01 pm
Location: Portugal
Contact:

Re: Jumping to chr position in an EditorGadget

Post by marcoagpinto »

@mestnyi

Yes, I call it from inside my tool.

It works with Windows, but I also ran it in Ubuntu 18.04 and it doesn't do anything, the position in the EditorGadget doesn't change.
mestnyi
Addict
Addict
Posts: 995
Joined: Mon Nov 25, 2013 6:41 am

Re: Jumping to chr position in an EditorGadget

Post by mestnyi »

purebasic 570 x64
This code just checked for debian and it works.
I don't have ubuntu.
Let somebody check.
User avatar
Sicro
Enthusiast
Enthusiast
Posts: 538
Joined: Wed Jun 25, 2014 5:25 pm
Location: Germany
Contact:

Re: Jumping to chr position in an EditorGadget

Post by Sicro »

In the CodeArchiv is also a code for Linux:
SetGet_EditorGadgetCursorPosition[Lin].pbi

In this code, I pass the address of the iter-position-variable to the function "gtk_text_buffer_place_cursor_()".
Image
Why OpenSource should have a license :: PB-CodeArchiv-Rebirth :: Pleasant-Dark (syntax color scheme) :: RegEx-Engine (compiles RegExes to NFA/DFA)
Manjaro Xfce x64 (Main system) :: Windows 10 Home (VirtualBox) :: Newest PureBasic version
User avatar
marcoagpinto
Addict
Addict
Posts: 939
Joined: Sun Mar 10, 2013 3:01 pm
Location: Portugal
Contact:

Re: Jumping to chr position in an EditorGadget

Post by marcoagpinto »

Guys,

Thank you for all the help, but it doesn't work both.

Let me place a screenshot of Proofing Tool GUI on Windows 10:
Image

As you can see, flag "J" is at chr position 6928.

When I double-click on the ListIconGadget or in the DISPLAY button, on Windows it jumps to position 6928 (see the bottom EditorGadget "SFX J Y 25").

On Ubuntu both of the code you placed on the forum does nothing :oops:

Maybe it is trying to jump to a line and not to a chr position?

What could be wrong?

Thank you for all the help!
mestnyi
Addict
Addict
Posts: 995
Joined: Mon Nov 25, 2013 6:41 am

Re: Jumping to chr position in an EditorGadget

Post by mestnyi »

It is clear that your project does not work, but you did not say if my example works as it is for you?
Maybe in your project that does not allow him to work.
User avatar
marcoagpinto
Addict
Addict
Posts: 939
Joined: Sun Mar 10, 2013 3:01 pm
Location: Portugal
Contact:

Re: Jumping to chr position in an EditorGadget

Post by marcoagpinto »

mestnyi wrote:It is clear that your project does not work, but you did not say if my example works as it is for you?
Maybe in your project that does not allow him to work.
I found out why it is not working.

Your code does move the cursor to the position, but if the position is outside the current viewable EditorGadget, the gadget is not moved to the cursor position.

The EditorGadget remains static, while in Windows it automatically scrolls to the cursor position.

Is there a command to do this in Linux?

Thanks!
User avatar
marcoagpinto
Addict
Addict
Posts: 939
Joined: Sun Mar 10, 2013 3:01 pm
Location: Portugal
Contact:

Re: Jumping to chr position in an EditorGadget

Post by marcoagpinto »

Guys?!

Any update on this?

The Linux version seems to place the cursor at the position but doesn't refresh the editor gadget to move to that position.

If we have an editor gadget with huge amounts of text, it moves the cursor but doesn't show the gadget at the new position.
mestnyi
Addict
Addict
Posts: 995
Joined: Mon Nov 25, 2013 6:41 am

Re: Jumping to chr position in an EditorGadget

Post by mestnyi »

hold my friend viewtopic.php?f=15&t=29840

Code: Select all

; Place the cursor position in a text gadget
;
; Help from the PB forum user: mestnyi - https://www.purebasic.fr/english/viewtopic.php?f=13&t=72277
;
; V1.0 - 13/FEB/2019
Procedure ScrollToEnd(gadget)                          ;scrolls text window to last line
  Protected end_mark,*buffer, end_iter.GtkTextIter
  *buffer=gtk_text_view_get_buffer_(GadgetID(gadget))
  gtk_text_buffer_get_end_iter_(*buffer,@end_iter)
  end_mark=gtk_text_buffer_create_mark_(*buffer,"end_mark",@end_iter,#False)
  gtk_text_view_scroll_mark_onscreen_(GadgetID(gadget),end_mark)
EndProcedure

Procedure SetCursorPos(Id.l,pos.l)
    Protected mypointertoiteration.GtkTextIter, *buffer
    *buffer = gtk_text_view_get_buffer_(GadgetID(Id))
    gtk_text_buffer_get_iter_at_offset_(*buffer,@mypointertoiteration,pos)
    gtk_text_buffer_place_cursor_(*buffer,@mypointertoiteration)
EndProcedure


Procedure.l GetCursorPos(Id.l)
    Protected mypointertoiteration.GtkTextIter, *buffer,cursor.l
    *buffer = gtk_text_view_get_buffer_(GadgetID(Id))
    cursor = gtk_text_buffer_get_insert_(*buffer)
    gtk_text_buffer_get_iter_at_mark_(*buffer, @mypointertoiteration, cursor)
    ProcedureReturn gtk_text_iter_get_offset_(@mypointertoiteration)
  EndProcedure
  
Procedure SetPos(Gadget, Position)
  CompilerSelect #PB_Compiler_OS
    CompilerCase #PB_OS_Linux
      If GadgetType( Gadget) = #PB_GadgetType_Editor
        Protected PositionIter.GtkTextIter
        Protected mark, *iter.GtkTextIter
        Protected *buffer = gtk_text_view_get_buffer_(GadgetID(Gadget))
        gtk_text_buffer_get_iter_at_offset_(*buffer, @PositionIter, Position)
        gtk_text_buffer_place_cursor_(*buffer, PositionIter)
        mark=gtk_text_buffer_create_mark_(*buffer,"cursor", PositionIter, #False);//create a mark in the buffer to scroll to
        gtk_text_view_scroll_to_mark_(GadgetID(Gadget),mark,0,#False,0,0) ; //scroll to the mark
        ; gtk_text_view_scroll_to_iter_(GadgetID(gadget), PositionIter, 0,#False,0,0)

      Else
        gtk_editable_set_position_( GadgetID(Gadget), Position )
      EndIf
      
      CompilerCase #PB_OS_MacOS
        Protected Range.NSRange : Range\location = Position
        CocoaMessage(0, GadgetID(Gadget), "setSelectedRange:@", @Range)
        CocoaMessage(0, GadgetID(Gadget), "scrollRangeToVisible:@", @Range)
       
      CompilerCase #PB_OS_Windows
        SendMessage_(GadgetID(Gadget), #EM_SETSEL, Position, Position)
       
    CompilerEndSelect
   
  EndProcedure
 
 
  If OpenWindow(0, 0, 0, 322, 150, "EditorGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    EditorGadget(0, 8, 8, 306, 133)
    For a = 0 To 15
      AddGadgetItem(0, a, "Line "+Str(a))
    Next
   
    SetActiveGadget(0)
    SetPos(0, 52)
   
    Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
  EndIf
User avatar
marcoagpinto
Addict
Addict
Posts: 939
Joined: Sun Mar 10, 2013 3:01 pm
Location: Portugal
Contact:

Re: Jumping to chr position in an EditorGadget

Post by marcoagpinto »

I have just tried it on Ubuntu 18.04.3 LTS and it worked!

Thank you very much!
Post Reply