Page 1 of 1
Force vertical scrollbar to bottom on EditorGadget
Posted: Fri Apr 24, 2015 2:09 am
by dchisholm
Folks,
I am using the EditorGadget to display the progress of a lengthy processing job. It works great and the scroll bars are generated as I fill the window BUT once the last line scrolls off the screen the user can no longer see it without clicking on the scroll bar.
I always want the bottom line (the most recent event) to be visible on the screen and have the older stuff scroll off the top of the screen. Essentially I would like the vertical scroll bar to be always on the bottom (thus showing the last entry posted to the editor window). Is there an easy to do this?
I am coding in the windows platform only.
Thanks.
Re: Force vertical scrollbar to bottom on EditorGadget
Posted: Fri Apr 24, 2015 2:25 am
by Thunder93
Hi. If I haven't misunderstood, the below is what you want.
Code: Select all
If OpenWindow(0, 0, 0, 322, 150, "EditorGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
EditorGadget(0, 8, 8, 306, 133)
For a = 0 To 1000
AddGadgetItem(0, a, "Line "+Str(a))
SendMessage_(GadgetID(0), #EM_SETSEL, -1, -1)
Delay(1)
Next
Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
Re: Force vertical scrollbar to bottom on EditorGadget
Posted: Fri Apr 24, 2015 1:45 pm
by dchisholm
Thank you. That worked great!
Re: Force vertical scrollbar to bottom on EditorGadget
Posted: Sun May 31, 2015 9:20 pm
by DirkVW
This works great for only Windows. The MacOSX App I am developing does not allow "SendMessage_". Is there any in a mac application to always scroll to the bottom of an EditorGadget automatically?
Re: Force vertical scrollbar to bottom on EditorGadget
Posted: Mon Jun 01, 2015 7:17 am
by Shardik
Wilbert has already demonstrated
here how to scroll to the end of an EditorGadget on MacOS.
For your conveniance I have put together an example for MacOS which dynamically adds a line to an EditorGadget and always keeps the last line visible:
Code: Select all
EnableExplicit
#MaxLines = 20
Define LastLine.I
Procedure AddLine(EditorGadgetID.I, Line.I)
Protected Range.NSRange
AddGadgetItem(EditorGadgetID, -1, "Line " + Str(Line))
Range.NSRange\location = Len(GetGadgetText(EditorGadgetID))
CocoaMessage(0, GadgetID(EditorGadgetID), "scrollRangeToVisible:@", @Range)
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
This is a cross-platform example which works on Linux, MacOS and Windows:
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 *Adjustment.GtkAdjustment
*Adjustment.GtkAdjustment = gtk_scrolled_window_get_vadjustment_(gtk_widget_get_parent_(GadgetID(EditorGadgetID)))
*Adjustment\value = *Adjustment\upper
gtk_adjustment_value_changed_(*Adjustment)
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
Re: Force vertical scrollbar to bottom on EditorGadget
Posted: Tue Jun 25, 2019 9:33 pm
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
Re: Force vertical scrollbar to bottom on EditorGadget
Posted: Tue Apr 08, 2025 7:58 pm
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!?
Re: Force vertical scrollbar to bottom on EditorGadget
Posted: Wed Apr 09, 2025 11:19 am
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
Re: Force vertical scrollbar to bottom on EditorGadget
Posted: Fri Apr 11, 2025 7:16 pm
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
Re: Force vertical scrollbar to bottom on EditorGadget
Posted: Sat Apr 12, 2025 7:41 am
by rndrei
It doesn't work!
Code: Select all
*textbuffer=PokeS(*textbuffer,txt,-1)
Re: Force vertical scrollbar to bottom on EditorGadget
Posted: Mon Apr 14, 2025 8:58 pm
by Shardik
If you comment or delete the line
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.
Re: Force vertical scrollbar to bottom on EditorGadget
Posted: Tue Apr 15, 2025 7:35 am
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
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.
Re: Force vertical scrollbar to bottom on EditorGadget
Posted: Tue Apr 15, 2025 9:36 am
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
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