Page 1 of 2

EditorGadget scroll

Posted: Wed Jul 10, 2024 8:22 pm
by rndrei
As in EditorGadget to make the text scroll down automatically when you add the line AddGadgetItem(#EDITOR, number_editor,Output$)?

Re: EditorGadget scroll

Posted: Wed Jul 10, 2024 8:56 pm
by infratec

Re: EditorGadget scroll

Posted: Wed Jul 10, 2024 9:30 pm
by RASHAD
For Windows

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))
  Next
  SendMessage_(GadgetID(0), #EM_SETSEL, -1,-1)
  Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
Scroll per each line

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)
  Next  
  Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow

Re: EditorGadget scroll

Posted: Wed Jul 10, 2024 11:36 pm
by HeX0R
I use this usually:

Code: Select all

Procedure LogIT(Text.s, GadgetID = -1)
	If Text
		Text = FormatDate("%hh:%ii:%ss", Date()) + " " + Text
	EndIf
	If GadgetID <> -1
		AddGadgetItem(GadgetID, -1, Text)
		CompilerSelect #PB_Compiler_OS
			CompilerCase #PB_OS_Windows
				Select GadgetType(GadgetID)
					Case #PB_GadgetType_ListView
						SendMessage_(GadgetID(GadgetID), #LB_SETTOPINDEX, CountGadgetItems(GadgetID) - 1, #Null)
					Case #PB_GadgetType_ListIcon
						SendMessage_(GadgetID(GadgetID), #LVM_ENSUREVISIBLE, CountGadgetItems(GadgetID) - 1, #False)
					Case #PB_GadgetType_Editor
						SendMessage_(GadgetID(GadgetID), #EM_SCROLLCARET, #SB_BOTTOM, 0)
				EndSelect
			CompilerCase #PB_OS_Linux
				Protected *Adjustment.GtkAdjustment
				*Adjustment = gtk_scrolled_window_get_vadjustment_(gtk_widget_get_parent_(GadgetID(Gadget)))
				*Adjustment\value = *Adjustment\upper
				gtk_adjustment_value_changed_(*Adjustment)
		CompilerEndSelect 
	EndIf

EndProcedure

Re: EditorGadget scroll

Posted: Thu Jul 11, 2024 6:43 pm
by rndrei
it is necessary for Linux, this code does not work, it gives an error on the line

Code: Select all

gtk_adjustment_value_changed_(*Adjustment)
error: memory access invalid

Re: EditorGadget scroll

Posted: Thu Jul 11, 2024 11:24 pm
by Piero
Mac:

Code: Select all

Range.NSRange\location = Len(GetGadgetText(0))
CocoaMessage(0, GadgetID(0), "scrollRangeToVisible:@", @Range)

Re: EditorGadget scroll

Posted: Fri Jul 12, 2024 9:36 pm
by mk-soft
rndrei wrote: Thu Jul 11, 2024 6:43 pm it is necessary for Linux, this code does not work, it gives an error on the line

Code: Select all

gtk_adjustment_value_changed_(*Adjustment)
error: memory access invalid
Works for me

PB v6.11, Mint Linux LMDE 6

Re: EditorGadget scroll

Posted: Sat Jul 13, 2024 10:12 am
by Axolotl
Could it have something to do with the fact that the function depends on the version of the subsystem?

GTK Docu wrote this (?)
Deprecated since: 3.18

Re: EditorGadget scroll

Posted: Sat Jul 13, 2024 12:24 pm
by mk-soft
Linux not work ...

Code: Select all

ImportC ""
  gtk_adjustment_get_lower(*GtkAdjustment)
  gtk_adjustment_get_upper(*GtkAdjustment)
  gtk_adjustment_get_page_size(*GtkAdjustment) 
  gtk_adjustment_set_value(*GtkAdjustment, value)
EndImport


Procedure LogIT(Text.s, GadgetID = -1)
	If Text
		Text = FormatDate("%hh:%ii:%ss", Date()) + " " + Text
	EndIf
	If GadgetID <> -1
		AddGadgetItem(GadgetID, -1, Text)
		CompilerSelect #PB_Compiler_OS
			CompilerCase #PB_OS_Windows
				Select GadgetType(GadgetID)
					Case #PB_GadgetType_ListView
						SendMessage_(GadgetID(GadgetID), #LB_SETTOPINDEX, CountGadgetItems(GadgetID) - 1, #Null)
					Case #PB_GadgetType_ListIcon
						SendMessage_(GadgetID(GadgetID), #LVM_ENSUREVISIBLE, CountGadgetItems(GadgetID) - 1, #False)
					Case #PB_GadgetType_Editor
						SendMessage_(GadgetID(GadgetID), #EM_SCROLLCARET, #SB_BOTTOM, 0)
				EndSelect
			CompilerCase #PB_OS_Linux
			  Protected *Adjustment.GtkAdjustment, value.d, parent
			  parent = gtk_widget_get_parent_(GadgetID(GadgetID))
			  Debug Hex(parent)
			  *Adjustment = gtk_scrolled_window_get_vadjustment_(parent)
				value = gtk_adjustment_get_lower(*Adjustment)
				Debug value
				value = gtk_adjustment_get_page_size(*Adjustment)
				Debug value
				value = gtk_adjustment_get_upper(*Adjustment)
				Debug value
				gtk_adjustment_set_value(*Adjustment, value)
		CompilerEndSelect 
	EndIf

EndProcedure

;-TOP

#ProgramTitle = "Main Window"
#ProgramVersion = "v1.01.2"

Enumeration Windows
  #Main
EndEnumeration

Enumeration MenuBar
  #MainMenu
EndEnumeration

Enumeration MenuItems
  #MainMenuAbout
  #MainMenuExit
EndEnumeration

Enumeration Gadgets
  #MainList
EndEnumeration

Enumeration StatusBar
  #MainStatusBar
EndEnumeration

Procedure UpdateWindow()
  Protected dx, dy
  dx = WindowWidth(#Main)
  dy = WindowHeight(#Main) - StatusBarHeight(#MainStatusBar) - MenuHeight()
  ; Resize gadgets
  ResizeGadget(#MainList, 0, 0, dx, dy)
EndProcedure

Procedure Main()
  Protected dx, dy
  
  #MainStyle = #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_MaximizeGadget | #PB_Window_MinimizeGadget
  
  If OpenWindow(#Main, #PB_Ignore, #PB_Ignore, 800, 600, #ProgramTitle , #MainStyle)
    ; Menu
    CreateMenu(#MainMenu, WindowID(#Main))
    MenuTitle("&File")
    CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
      MenuItem(#PB_Menu_About, "")
    CompilerElse
      MenuItem(#MainMenuAbout, "About")
    CompilerEndIf
    ; Menu File Items
    
    CompilerIf Not #PB_Compiler_OS = #PB_OS_MacOS
      MenuBar()
      MenuItem(#MainMenuExit, "E&xit")
    CompilerEndIf
    
    ; StatusBar
    CreateStatusBar(#MainStatusBar, WindowID(#Main))
    AddStatusBarField(#PB_Ignore)
    
    ; Gadgets
    dx = WindowWidth(#Main)
    dy = WindowHeight(#Main) - StatusBarHeight(#MainStatusBar) - MenuHeight()
    ListViewGadget(#MainList, 0, 0, dx, dy)
    
    ; Bind Events
    BindEvent(#PB_Event_SizeWindow, @UpdateWindow(), #Main)
    
    For a = 0 To 1000
      AddGadgetItem(0, a, "Line "+Str(a))
    Next
    While WindowEvent() : Wend
    LogIT("Hello", #MainList)
    
    Repeat
      Select WaitWindowEvent()
        Case #PB_Event_CloseWindow
          Select EventWindow()
            Case #Main
              Break
              
          EndSelect
          
        Case #PB_Event_Menu
          Select EventMenu()
            CompilerIf #PB_Compiler_OS = #PB_OS_MacOS   
              Case #PB_Menu_About
                PostEvent(#PB_Event_Menu, #Main, #MainMenuAbout)
                
              Case #PB_Menu_Preferences
                
              Case #PB_Menu_Quit
                PostEvent(#PB_Event_CloseWindow, #Main, #Null)
                
            CompilerEndIf
              
            Case #MainMenuExit
              PostEvent(#PB_Event_CloseWindow, #Main, #Null)
              
            Case #MainMenuAbout
              MessageRequester("About", #ProgramTitle + #LF$ + #ProgramVersion, #PB_MessageRequester_Info)
              
          EndSelect
          
          
        Case #PB_Event_Gadget
          Select EventGadget()
            Case #MainList
              Select EventType()
                Case #PB_EventType_Change
                  ;
              EndSelect
              
          EndSelect
          
      EndSelect
    ForEver
    
  EndIf
  
EndProcedure : Main()

Re: EditorGadget scroll

Posted: Sat Jul 13, 2024 7:01 pm
by rndrei
Example, what's above, doesn't automatically scroll to the bottom of the Editor Gadget!?
GTK version 3.24.38

Re: EditorGadget scroll

Posted: Sun Jul 14, 2024 10:02 am
by mk-soft
I have searched for gtk c++ examples. It looks like you have to do a lot more.
Maybe you or I can find something in the examples and convert it to purebasic

Re: EditorGadget scroll

Posted: Sun Jul 14, 2024 11:06 am
by mk-soft
The function gtk_scrolled_window_get_vadjustment does not return any values, as it is probably set to automatic (which is correct)

Take a ListView as trace output.

ThreadSafe Trace ...

Code: Select all

;-TOP

CompilerIf Not #PB_Compiler_Thread
  CompilerError "Use Compiler Option ThreadSage!"
CompilerEndIf

Enumeration Windows
  #Main
EndEnumeration

Enumeration MenuBar
  #MainMenu
EndEnumeration

Enumeration MenuItems
  #MainMenuItemExit
EndEnumeration

Enumeration Gadgets
  #MainTraceList
EndEnumeration

Enumeration StatusBar
  #MainStatusBar
EndEnumeration

; ----

Enumeration CustomEvent #PB_Event_FirstCustomValue
  #MyEvent_Trace
EndEnumeration

; ---- String Helper ----
  
Procedure AllocateString(String.s) ; Result = Pointer
  Protected *mem.string = AllocateStructure(String)
  If *mem
    *mem\s = String
  EndIf
  ProcedureReturn *mem
EndProcedure

Procedure.s FreeString(*mem.string) ; Result String
  Protected r1.s
  If *mem
    r1 = *mem\s
    FreeStructure(*mem)
  EndIf
  ProcedureReturn r1
EndProcedure

; ----

Procedure DoEventTrace()
  Protected *data, cnt
  *data = EventData()
  If *data
    cnt = CountGadgetItems(#MainTraceList)
    AddGadgetItem(#MainTraceList, -1, FreeString(*data))
    SetGadgetState(#MainTraceList, cnt)
    SetGadgetState(#MainTraceList, -1)
    If cnt >= 1000
      RemoveGadgetItem(#MainTraceList, 0)
    EndIf
  EndIf
EndProcedure : BindEvent(#MyEvent_Trace, @DoEventTrace())

Procedure __Trace(Info.s, Modul.s, Proc.s, Line)
  Protected Text.s
  If Modul = ""
    Modul = "Main"
  EndIf
  Text = FormatDate("[%HH:%II:%SS] ", Date())
  Text + "[Module " + Modul + "; Proc " + Proc + "; Line " + Line + "] " + Info
  PostEvent(#MyEvent_Trace, 0, 0, 0, AllocateString(Text))  
EndProcedure

Macro Trace(Info, Modul = #PB_Compiler_Module, Proc = #PB_Compiler_Procedure, Line = #PB_Compiler_Line)
  __Trace(Info, Modul, Proc, Line)
EndMacro

; ----

Procedure UpdateWindow()
  Protected dx, dy
  dx = WindowWidth(#Main)
  dy = WindowHeight(#Main) - StatusBarHeight(#MainStatusBar) - MenuHeight()
  ; Resize gadgets
  ResizeGadget(#MainTraceList, 0, 0, dx, dy)
EndProcedure

Procedure Main()
  Protected dx, dy
  Protected ExitTime
  
  #MainStyle = #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_MaximizeGadget | #PB_Window_MinimizeGadget
  
  If OpenWindow(#Main, #PB_Ignore, #PB_Ignore, 800, 600, "Window" , #MainStyle)
    ; Menu
    CreateMenu(#MainMenu, WindowID(#Main))
    MenuTitle("&File")
    MenuItem(#MainMenuItemExit, "E&xit")
    
    ; StatusBar
    CreateStatusBar(#MainStatusBar, WindowID(#Main))
    AddStatusBarField(#PB_Ignore)
    
    ; Gadgets
    dx = WindowWidth(#Main)
    dy = WindowHeight(#Main) - StatusBarHeight(#MainStatusBar) - MenuHeight()
    ListViewGadget(#MainTraceList, 0, 0, dx, dy)
    
    ; Bind Events
    BindEvent(#PB_Event_SizeWindow, @UpdateWindow(), #Main)
    
    For i = 1 To 10
      Trace("Count " + i)
    Next
    
    Repeat
      Select WaitWindowEvent()
        Case #PB_Event_CloseWindow
          Select EventWindow()
            Case #Main
              Break
              
          EndSelect
          
        Case #PB_Event_Menu
          Select EventMenu()
            CompilerIf #PB_Compiler_OS = #PB_OS_MacOS   
              Case #PB_Menu_About
                
              Case #PB_Menu_Preferences
                
              Case #PB_Menu_Quit
                PostEvent(#PB_Event_CloseWindow, #Main, #Null)
                
            CompilerEndIf
            
          Case #MainMenuItemExit
            PostEvent(#PB_Event_CloseWindow, #Main, #Null)
            
          EndSelect
          
        Case #PB_Event_Gadget
          Select EventGadget()
              
          EndSelect
          
      EndSelect
    ForEver
    
    ;-- ExitProgram
    Trace("Exit Program (Wait 1 Seconds)")
    
    ExitTime = ElapsedMilliseconds()
    Repeat
      WaitWindowEvent(10)
      If ElapsedMilliseconds() - ExitTime >= 1000
        Break
      EndIf
    ForEver
    
  EndIf
  
EndProcedure : Main()

Re: EditorGadget scroll

Posted: Mon Jul 15, 2024 8:11 am
by rndrei
Yes, thank you works!

Re: EditorGadget scroll

Posted: Mon Jul 15, 2024 12:12 pm
by rndrei
Another question is how to set the background color?

Code: Select all

SetGadgetItemColor(#MainTraceList,1,#PB_Gadget_BackColor, RGB(255,0,0),#PB_All)
this does not work!

Re: EditorGadget scroll

Posted: Mon Jul 15, 2024 12:27 pm
by Axolotl
Hi,
the code line works as expected. What do you expect?
The background color of Line 1 has changed to red.

See the running code (borrowed from help)

Code: Select all

; Help Example (adapted to your line ....)   !! Working on Win11 and Ubuntu 22.04 !!
#MainTraceList = 0 

; Help Example (original) 

  If OpenWindow(0, 0, 0, 300, 300, "SetGadgetItemColor", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    ListIconGadget(0, 10, 10, 280, 280, "Column 0", 100)
    AddGadgetColumn(0, 1, "Column 1", 100)
    For i = 1 To 10
      AddGadgetItem(0, -1, "Text 1"+Chr(10)+"Text 2")
    Next

    SetGadgetItemColor(0, #PB_All, #PB_Gadget_FrontColor, $0000FF,  1)
    SetGadgetItemColor(0,  3, #PB_Gadget_BackColor,  $00FFFF, #PB_All)
    SetGadgetItemColor(0,  9, #PB_Gadget_BackColor,  $FFFF00,  1)

; Help Example (add your line ....) 
    SetGadgetItemColor(#MainTraceList, 1, #PB_Gadget_BackColor, RGB(255,0,0), #PB_All) 

    Repeat
    Until WaitWindowEvent() = #PB_Event_CloseWindow
  EndIf