Force vertical scrollbar to bottom on 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: Force vertical scrollbar to bottom on EditorGadget

Post by Shardik »

I had to modify my multi-platform example for Linux because it doesn't work with GTK3. The modified version works in Linux with both GTK2 and GTK3:

Code: Select all

EnableExplicit

#MaxLines = 20

Define LastLine.I

Procedure AddLine(EditorGadgetID.I, Line.I)
  AddGadgetItem(EditorGadgetID, -1, "Line " + Str(Line))
  
  CompilerSelect #PB_Compiler_OS
    CompilerCase #PB_OS_Linux
      Protected EndMark.I
      Protected TextBuffer.I
      Protected EndIter.GtkTextIter

      TextBuffer = gtk_text_view_get_buffer_(GadgetID(EditorGadgetID))
      gtk_text_buffer_get_end_iter_(TextBuffer, @EndIter)
      EndMark = gtk_text_buffer_create_mark_(TextBuffer, "end_mark",
        @EndIter, #False)
      gtk_text_view_scroll_mark_onscreen_(GadgetID(EditorGadgetID), EndMark)
    CompilerCase #PB_OS_MacOS
      Protected Range.NSRange

      Range.NSRange\location = Len(GetGadgetText(EditorGadgetID))
      CocoaMessage(0, GadgetID(EditorGadgetID), "scrollRangeToVisible:@", @Range)
    CompilerCase #PB_OS_Windows
      SendMessage_(GadgetID(EditorGadgetID), #EM_SETSEL, -1, -1) 
  CompilerEndSelect
EndProcedure

OpenWindow(0, 270, 100, 220, 150, "EditorGadget")
EditorGadget(0, 10, 10, 200, 130)
AddWindowTimer(0, 0, 500)

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Break
    Case #PB_Event_Timer
      If LastLine < #MaxLines
        LastLine + 1
        AddLine(0, LastLine)
      Else
        RemoveWindowTimer(0, 0)
      EndIf
  EndSelect
ForEver
User avatar
rndrei
Enthusiast
Enthusiast
Posts: 151
Joined: Thu Dec 28, 2023 9:04 pm

Re: Force vertical scrollbar to bottom on EditorGadget

Post by rndrei »

Shardik wrote: Tue Jun 25, 2019 9:33 pm I had to modify my multi-platform example for Linux because it doesn't work with GTK3. The modified version works in Linux with both GTK2 and GTK3:

Code: Select all

EnableExplicit

#MaxLines = 20

Define LastLine.I

Procedure AddLine(EditorGadgetID.I, Line.I)
  AddGadgetItem(EditorGadgetID, -1, "Line " + Str(Line))
  
  CompilerSelect #PB_Compiler_OS
    CompilerCase #PB_OS_Linux
      Protected EndMark.I
      Protected TextBuffer.I
      Protected EndIter.GtkTextIter

      TextBuffer = gtk_text_view_get_buffer_(GadgetID(EditorGadgetID))
      gtk_text_buffer_get_end_iter_(TextBuffer, @EndIter)
      EndMark = gtk_text_buffer_create_mark_(TextBuffer, "end_mark",
        @EndIter, #False)
      gtk_text_view_scroll_mark_onscreen_(GadgetID(EditorGadgetID), EndMark)
    CompilerCase #PB_OS_MacOS
      Protected Range.NSRange

      Range.NSRange\location = Len(GetGadgetText(EditorGadgetID))
      CocoaMessage(0, GadgetID(EditorGadgetID), "scrollRangeToVisible:@", @Range)
    CompilerCase #PB_OS_Windows
      SendMessage_(GadgetID(EditorGadgetID), #EM_SETSEL, -1, -1) 
  CompilerEndSelect
EndProcedure

OpenWindow(0, 270, 100, 220, 150, "EditorGadget")
EditorGadget(0, 10, 10, 200, 130)
AddWindowTimer(0, 0, 500)

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Break
    Case #PB_Event_Timer
      If LastLine < #MaxLines
        LastLine + 1
        AddLine(0, LastLine)
      Else
        RemoveWindowTimer(0, 0)
      EndIf
  EndSelect
ForEver
Tell me, if I use, ResizeGadget (), then this stops working under Linux!?
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Force vertical scrollbar to bottom on EditorGadget

Post by Shardik »

rndrei wrote: Tue Apr 08, 2025 7:58 pm Tell me, if I use, ResizeGadget (), then this stops working under Linux!?
Do you have an example code where it doesn't work after ResizeGadget()? I have taken my multi-platform example, removed all non-Linux stuff and resize the EditorGadget after the 10th displayed line. It works like a charm with GTK2 and GTK3 (tested successfully in Linux Mint 21.3 'Virginia' x64 with Cinnamon and PB 6.20)!

Code: Select all

EnableExplicit

#MaxLines = 20

Define LastLine.I

Procedure AddLine(EditorGadgetID.I, Line.I)
  AddGadgetItem(EditorGadgetID, -1, "Line " + Str(Line))
  
  Protected EndMark.I
  Protected TextBuffer.I
  Protected EndIter.GtkTextIter
  
  TextBuffer = gtk_text_view_get_buffer_(GadgetID(EditorGadgetID))
  gtk_text_buffer_get_end_iter_(TextBuffer, @EndIter)
  EndMark = gtk_text_buffer_create_mark_(TextBuffer, "end_mark",
    @EndIter, #False)
  gtk_text_view_scroll_mark_onscreen_(GadgetID(EditorGadgetID), EndMark)

  If Line = 10
    ResizeGadget(EditorGadgetID, 30, 30, 160, 90) 
  EndIf
EndProcedure

OpenWindow(0, 270, 100, 220, 150, "EditorGadget")
EditorGadget(0, 10, 10, 200, 130)
AddWindowTimer(0, 0, 700)

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Break
    Case #PB_Event_Timer
      If LastLine < #MaxLines
        LastLine + 1
        AddLine(0, LastLine)
      Else
        RemoveWindowTimer(0, 0)
      EndIf
  EndSelect
ForEver
User avatar
rndrei
Enthusiast
Enthusiast
Posts: 151
Joined: Thu Dec 28, 2023 9:04 pm

Re: Force vertical scrollbar to bottom on EditorGadget

Post by rndrei »

But how to make the text not from Editrorgadget, but from the line Define txt.s?

Code: Select all

 TextBuffer = gtk_text_view_get_buffer_(GadgetID(gadget))
Here is a redone code, who does not work correctly:

Code: Select all

EnableExplicit

#MaxLines = 50

Define LastLine.I
Define txt.s="123000000000000000000000000000000000000000000000"+#LF$+
             "456000000000000000000000000000000000000000000000"+#LF$+
             "789000000000000000000000000000000000000000000000"+#LF$+
             "456000000000000000000000000000000000000000000000"+#LF$+
             "457000000000000000000000000000000000000000000000"+#LF$+
             "458000000000000000000000000000000000000000000000"+#LF$+
             "459000000000000000000000000000000000000000000000"+#LF$+
             "460000000000000000000000000000000000000000000000"+#LF$
             


Procedure AddLine(EditorGadgetID.I, Line.s)
  AddGadgetItem(EditorGadgetID, -1, line)
  
  Protected EndMark.I
  Protected TextBuffer.I
  Protected EndIter.GtkTextIter
  
  TextBuffer = gtk_text_view_get_buffer_(GadgetID(EditorGadgetID))
  gtk_text_buffer_get_end_iter_(TextBuffer, @EndIter)
  EndMark = gtk_text_buffer_create_mark_(TextBuffer, "end_mark",
    @EndIter, #False)
  gtk_text_view_scroll_mark_onscreen_(GadgetID(EditorGadgetID), EndMark)

EndProcedure

OpenWindow(0, 270, 100, 220, 150, "EditorGadget")
EditorGadget(0, 10, 10, 200, 130)
AddWindowTimer(0, 0, 700)

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Break
    Case #PB_Event_Timer
      If LastLine < #MaxLines
        LastLine + 1

          AddLine(0, txt)
          txt=txt+txt
  
      Else
        RemoveWindowTimer(0, 0)
      EndIf
  EndSelect
ForEver
User avatar
rndrei
Enthusiast
Enthusiast
Posts: 151
Joined: Thu Dec 28, 2023 9:04 pm

Re: Force vertical scrollbar to bottom on EditorGadget

Post by rndrei »

It doesn't work!

Code: Select all

  *textbuffer=PokeS(*textbuffer,txt,-1)
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Force vertical scrollbar to bottom on EditorGadget

Post by Shardik »

If you comment or delete the line

Code: Select all

          txt=txt+txt
in your code example it will work like a charm! The problem is caused by doubling the size of the string variable txt 50 times in succession (#MaxLines = 50). Take a look into this thread where Fred explained:
Fred wrote:The editor gadget is not meant to have many item added as it gets the whole buffer and add it at the end at every call, that's why it gets slower the more your add it to it.
User avatar
rndrei
Enthusiast
Enthusiast
Posts: 151
Joined: Thu Dec 28, 2023 9:04 pm

Re: Force vertical scrollbar to bottom on EditorGadget

Post by rndrei »

This is not a comment, I did so.To show that the function does not work when adding a large piece of text!
Shardik wrote: Mon Apr 14, 2025 8:58 pm If you comment or delete the line

Code: Select all

          txt=txt+txt
in your code example it will work like a charm! The problem is caused by doubling the size of the string variable txt 50 times in succession (#MaxLines = 50). Take a look into this thread where Fred explained:
Fred wrote:The editor gadget is not meant to have many item added as it gets the whole buffer and add it at the end at every call, that's why it gets slower the more your add it to it.
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Force vertical scrollbar to bottom on EditorGadget

Post by Shardik »

rndrei wrote: Tue Apr 15, 2025 7:35 am To show that the function does not work when adding a large piece of text!
With

Code: Select all

txt=txt+txt
in a loop you are simply blowing the capacity of the EditorGadget. When setting #MaxLines = 23, your txt string contains already more than 3 GB of characters!

See for yourself:

Code: Select all

#MaxLines = 23

Define txt.s="123000000000000000000000000000000000000000000000"+#LF$+
             "456000000000000000000000000000000000000000000000"+#LF$+
             "789000000000000000000000000000000000000000000000"+#LF$+
             "456000000000000000000000000000000000000000000000"+#LF$+
             "457000000000000000000000000000000000000000000000"+#LF$+
             "458000000000000000000000000000000000000000000000"+#LF$+
             "459000000000000000000000000000000000000000000000"+#LF$+
             "460000000000000000000000000000000000000000000000"+#LF$

Debug "Iteration: " + RSet(Str(LastLine), 2, "0") +
  ", Text length: " + FormatNumber(Len(txt) / 1024 / 1024, 4, ".", ",") + " MB"

While LastLine < #MaxLines
  LastLine + 1
  txt=txt+txt
  Debug "Iteration: " + RSet(Str(LastLine), 2, "0") +
    ", Text length: " + FormatNumber(Len(txt) / 1024 / 1024, 4, ".", ",") + " MB"
Wend
Post Reply