Move scrollbar to top of EditorGadget

Just starting out? Need help? Post your questions and find answers here.
User avatar
Columbo
Enthusiast
Enthusiast
Posts: 303
Joined: Wed Sep 10, 2014 7:17 am
Location: Ontario Canada
Contact:

Move scrollbar to top of EditorGadget

Post by Columbo »

In my program I have a EditGadget which is populated by a record from a database. When reading the contents of the current record in the EditorGadget, if you use the scrollbar to scroll down to the end of the content and then move to the next record, the scrollbar is at the bottom of the EditorGadget forcing you to scroll back up in order to read the beginning of the next record. Is there any way to programmatically move the scrollbar to the top of each new record?
http://www.oldtimeradiotoday.com - Listen to or download classic old time radio broadcasts.
User avatar
RSBasic
Moderator
Moderator
Posts: 1218
Joined: Thu Dec 31, 2009 11:05 pm
Location: Gernsbach (Germany)
Contact:

Re: Move scrollbar to top of EditorGadget

Post by RSBasic »

For Windows:

Code: Select all

SendMessage_(GadgetID(#Gadget), #EM_SCROLL, #SB_TOP, 0)
Image
Image
User avatar
Columbo
Enthusiast
Enthusiast
Posts: 303
Joined: Wed Sep 10, 2014 7:17 am
Location: Ontario Canada
Contact:

Re: Move scrollbar to top of EditorGadget

Post by Columbo »

Works perfectly. Thank you very much RSBasic.
http://www.oldtimeradiotoday.com - Listen to or download classic old time radio broadcasts.
User avatar
Borstensohn
User
User
Posts: 11
Joined: Sun Jun 05, 2005 9:45 pm

Re: Move scrollbar to top of EditorGadget

Post by Borstensohn »

I have a similar question, and that is: How can the scrollbar in the EditorGadget scroll down automatically under Linux?

To explain more detailed: When a text from a file is loaded into the EditorGadget, I’d love the gadget’s scrollbar to scroll down, without the need for the user to push the PgDn (page down) key.

The code below works with the ListViewGadget, but not with the EditorGadget. Has anybody an idea, how this could be solved? Remember: Under Linux, not Windows. Thanks in advance.

Code: Select all


EnableExplicit

Enumeration
	#wndFenster
	#edtEditor
	#lstListe
EndEnumeration

Global.i hoehe, zeilen, listenzeilen

If OpenWindow(#wndFenster, 0, 0, 400, 400, "Rollbalken soll ans Ende rollen, will aber nicht.", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) 
	EditorGadget(#edtEditor, 20, 20, 360, 120, #PB_Editor_WordWrap)
	SetGadgetText(#edtEditor, "Dies" + #CRLF$ + "ist" + #CRLF$ + "ein" + #CRLF$ + "sehr" + #CRLF$ + "hoher" + #CRLF$ + "Text," + #CRLF$ + "wenngleich" + #CRLF$ + "auch" + #CRLF$ + "kein" + #CRLF$ + "hochgeistiger.")
;Alternative:
;	AddGadgetItem(#edtEditor, 0, "Dies")
;	AddGadgetItem(#edtEditor, 1, "ist")
;	AddGadgetItem(#edtEditor, 2, "ein")
;	AddGadgetItem(#edtEditor, 3, "sehr")
;	AddGadgetItem(#edtEditor, 4, "hoher")
;	AddGadgetItem(#edtEditor, 5, "Text")
;	AddGadgetItem(#edtEditor, 6, "wenngleich")
;	AddGadgetItem(#edtEditor, 7, "auch")
;	AddGadgetItem(#edtEditor, 8, "kein")
;	AddGadgetItem(#edtEditor, 8, "hochgeistiger")
	SetActiveGadget(#edtEditor)
	SetGadgetState(#edtEditor, zeilen-1)
	
	ListViewGadget(#lstListe, 20, 160, 360, 120)
	AddGadgetItem(#lstListe, 0, "Dies")
	AddGadgetItem(#lstListe, 1, "ist")
	AddGadgetItem(#lstListe, 2, "ein")
	AddGadgetItem(#lstListe, 3, "sehr")
	AddGadgetItem(#lstListe, 4, "hoher")
	AddGadgetItem(#lstListe, 5, "Text")
	AddGadgetItem(#lstListe, 6, "wenngleich")
	AddGadgetItem(#lstListe, 7, "auch")
	AddGadgetItem(#lstListe, 8, "kein")
	AddGadgetItem(#lstListe, 8, "hochgeistiger")
	listenzeilen = CountGadgetItems(#lstListe)
	SetGadgetState(#lstListe, listenzeilen-1)

	Repeat
		Select WaitWindowEvent(20) 
			Case  #PB_Event_CloseWindow 
				End 
		EndSelect
	ForEver
EndIf
ON ERROR GOTO BED
User avatar
Shardik
Addict
Addict
Posts: 1989
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Move scrollbar to top of EditorGadget

Post by Shardik »

Have you already taken a look into my list of cross-platform examples?
User avatar
Borstensohn
User
User
Posts: 11
Joined: Sun Jun 05, 2005 9:45 pm

Re: Move scrollbar to top of EditorGadget

Post by Borstensohn »

Thanks, Shardik, your example Set cursor is the most interesting for me. I changed the editor’s content like this (see below), so that a scrollbar appears:

Code: Select all

SetGadgetText(#Editor, "The" + #LF$ + "quick" + #LF$ + "brown" + #LF$ + "fox" + #LF$ + "jumps" + #LF$ + "over" + #LF$ + "the" + #LF$ + "lazy" + #LF$ + "dog.")
And the cursor jumps to the desired position (let’s say to the last word), but the scrollbar does not move down.
ON ERROR GOTO BED
#NULL
Addict
Addict
Posts: 1440
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: Move scrollbar to top of EditorGadget

Post by #NULL »

Try the code from
EditorGadget:
- Add line and keep it visible
User avatar
Borstensohn
User
User
Posts: 11
Joined: Sun Jun 05, 2005 9:45 pm

Re: Move scrollbar to top of EditorGadget

Post by Borstensohn »

Thanks, #NULL, but this example simulates adding of text lines. When a real user adds text, the scrollbar moves accordingly, so that’s not my point actually. What I am looking for is: After loading text into the Gadget editor, the scrollbar should scroll to the end of the text, without user interaction. So, when a text is loaded, the user should see the last text line, so that she or he can keep on writing, without the need to manually scroll down to the last sentence/line.

But my wish seems to be quite unusual, because in the meantime I tested several text editors, also one word processing program, and all of them always started with the scroll bar at the top position. I found no way in the options to change that. So I'll take that as standard behavior.
ON ERROR GOTO BED
#NULL
Addict
Addict
Posts: 1440
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: Move scrollbar to top of EditorGadget

Post by #NULL »

I have this one (possibly from Shardik or from somewhere else in the forum):

Code: Select all

  Procedure scrollToEnd(gadget)
    ; use subsystem gtk2, otherwise might not work with horizontal scrollbar (last line not visible and it scrolls the the right too)
    CompilerIf #PB_Compiler_OS = #PB_OS_Windows
      SendMessage_(GadgetID(gadget), #EM_SETSEL, -2, -1)
    CompilerElse
      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)
    CompilerEndIf
  EndProcedure
IdeasVacuum
Always Here
Always Here
Posts: 6425
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Move scrollbar to top of EditorGadget

Post by IdeasVacuum »

In Windows, you can do something like this:

Code: Select all

Define.i iScrollMin, iScrollMax                                                    
                                                                                   
If GetWindowLongPtr_(GadgetID(#Editor), #GWL_STYLE) & #WS_VSCROLL              
                                                                                   
         GetScrollRange_(GadgetID(#Editor), #SB_VERT, @iScrollMin, @iScrollMax)
           SetScrollPos_(GadgetID(#Editor), #SB_VERT, iScrollMax, #True)       
EndIf                                                                              
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
Borstensohn
User
User
Posts: 11
Joined: Sun Jun 05, 2005 9:45 pm

Re: Move scrollbar to top of EditorGadget

Post by Borstensohn »

Does not work for me, @IdeasVacuum. I tried with GTK3 and GTK2 (compiler options > library subsystem > gtk2).
Maybe I’ll try under Windows, but not today, I think.

Thanks for your help to all of you, guys, much appreciated!

Oh, by the way: I have PureBasic 5.71 LTS (x64).
ON ERROR GOTO BED
#NULL
Addict
Addict
Posts: 1440
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: Move scrollbar to top of EditorGadget

Post by #NULL »

Works here, both with gtk2 and gtk3:

Code: Select all

EnableExplicit

Procedure scrollToEnd(gadget)
  ; use subsystem gtk2, otherwise might not work with horizontal scrollbar (last line not visible and it scrolls the the right too)
  CompilerIf #PB_Compiler_OS = #PB_OS_Windows
    SendMessage_(GadgetID(gadget), #EM_SETSEL, -2, -1)
  CompilerElse
    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)
  CompilerEndIf
EndProcedure

Enumeration
   #wndFenster
   #edtEditor
   #lstListe
EndEnumeration

Global.i hoehe, zeilen, listenzeilen

If OpenWindow(#wndFenster, 0, 0, 400, 400, "Rollbalken soll ans Ende rollen, will aber nicht.", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
   EditorGadget(#edtEditor, 20, 20, 360, 120);, #PB_Editor_WordWrap)
   SetGadgetText(#edtEditor, "Dies" + #CRLF$ + "ist" + #CRLF$ + "ein" + #CRLF$ + "sehr" + #CRLF$ + "hoher" + #CRLF$ + "Text," + #CRLF$ + "wenngleich" + #CRLF$ + "auch" + #CRLF$ + "kein" + #CRLF$ + "hochgeistiger.")
;Alternative:
;   AddGadgetItem(#edtEditor, 0, "Dies")
;   AddGadgetItem(#edtEditor, 1, "ist")
;   AddGadgetItem(#edtEditor, 2, "ein")
;   AddGadgetItem(#edtEditor, 3, "sehr")
;   AddGadgetItem(#edtEditor, 4, "hoher")
;   AddGadgetItem(#edtEditor, 5, "Text")
;   AddGadgetItem(#edtEditor, 6, "wenngleich")
;   AddGadgetItem(#edtEditor, 7, "auch")
;   AddGadgetItem(#edtEditor, 8, "kein")
;   AddGadgetItem(#edtEditor, 8, "hochgeistiger")
   SetActiveGadget(#edtEditor)
   ;SetGadgetState(#edtEditor, zeilen-1)
   
   scrollToEnd(#edtEditor)

   
   ListViewGadget(#lstListe, 20, 160, 360, 120)
   AddGadgetItem(#lstListe, 0, "Dies")
   AddGadgetItem(#lstListe, 1, "ist")
   AddGadgetItem(#lstListe, 2, "ein")
   AddGadgetItem(#lstListe, 3, "sehr")
   AddGadgetItem(#lstListe, 4, "hoher")
   AddGadgetItem(#lstListe, 5, "Text")
   AddGadgetItem(#lstListe, 6, "wenngleich")
   AddGadgetItem(#lstListe, 7, "auch")
   AddGadgetItem(#lstListe, 8, "kein")
   AddGadgetItem(#lstListe, 8, "hochgeistiger")
   listenzeilen = CountGadgetItems(#lstListe)
   SetGadgetState(#lstListe, listenzeilen-1)

   Repeat
      Select WaitWindowEvent(20)
         Case  #PB_Event_CloseWindow
            End
      EndSelect
   ForEver
EndIf
Only if the last line is longer then it will also scroll to the right unfortunately. I avoid this in some of my codes by adding empty dummy lines after the actual content, and removing them before adding new content. But then there is a bug on Windows that needed additional handling. Goes like this (just copied from my code, doesn't compile like that):

Code: Select all

  Procedure messageToEditorLog(message.s)
    Shared editorLog, messages(), messagesMutex
    Protected NewList lines.s(), txt.s, i
    
    ; remove last dummy lines (" ")
    
    CompilerIf #PB_Compiler_OS = #PB_OS_Windows
      
      ; workaround for : https://www.purebasic.fr/english/viewtopic.php?f=4&t=54963
      
      ; get text, split into lines
      txt = GetGadgetText(editorLog)
      For i=1 To CountString(txt, #CRLF$) + 1
        AddElement(lines())
        lines() = StringField(txt, i, #CRLF$)
      Next
      ; remove last " " lines
      While LastElement(lines()) And lines() = " "
        DeleteElement(lines())
      Wend
      ; put back to gadget
      txt = ""
      ForEach lines()
        If Len(txt) > 0
          txt + #CRLF$
        EndIf
        txt + lines()
      Next
      SetGadgetText(editorLog, txt)
      
    CompilerElse
      
      ; works in linux
      
      ; remove last " " items
      While GetGadgetItemText(editorLog, CountGadgetItems(editorLog) - 1) = " "
        RemoveGadgetItem(editorLog, CountGadgetItems(editorLog) - 1)
      Wend
    CompilerEndIf
    
    ; add actual message
    AddGadgetItem(editorLog, -1, message)
    
    ; add dummy " " lines for end scroll
    For i = 1 To 2
      AddGadgetItem(editorLog, -1, " ")
    Next
    
    scrollToEnd(editorLog)
  EndProcedure
User avatar
Borstensohn
User
User
Posts: 11
Joined: Sun Jun 05, 2005 9:45 pm

Re: Move scrollbar to top of EditorGadget

Post by Borstensohn »

Wow, fantastic, #NULL, it works like a charm, thank you so much!
And the code of your procedure separates the beginners (like me) from the geniuses (like you). :)
ON ERROR GOTO BED
User avatar
Borstensohn
User
User
Posts: 11
Joined: Sun Jun 05, 2005 9:45 pm

Re: Move scrollbar to top of EditorGadget

Post by Borstensohn »

Well, on large texts it only works correctly with compiler option Library Subsystem: gtk2, otherwise (default GTK 3) the scrollbar stops after 24 lines or so. But that’s okay so far, I can use GTK 2.
ON ERROR GOTO BED
firace
Addict
Addict
Posts: 902
Joined: Wed Nov 09, 2011 8:58 am

Re: Move scrollbar to top of EditorGadget

Post by firace »

#NULL wrote: Only if the last line is longer then it will also scroll to the right unfortunately.
I had the same issue and solved it with the following:

Code: Select all

 SendMessage_(GadgetID(gadget), #WM_VSCROLL, #SB_BOTTOM, #Null)
Instead of

Code: Select all

   SendMessage_(GadgetID(gadget), #EM_SETSEL, -2, -1)
Post Reply