EditorGadget scroll
Posted: Wed Jul 10, 2024 8:22 pm
As in EditorGadget to make the text scroll down automatically when you add the line AddGadgetItem(#EDITOR, number_editor,Output$)?
http://www.purebasic.com
https://www.purebasic.fr/english/
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
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
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
EndProcedureCode: Select all
gtk_adjustment_value_changed_(*Adjustment)Code: Select all
Range.NSRange\location = Len(GetGadgetText(0))
CocoaMessage(0, GadgetID(0), "scrollRangeToVisible:@", @Range)Works for merndrei wrote: Thu Jul 11, 2024 6:43 pm it is necessary for Linux, this code does not work, it gives an error on the lineerror: memory access invalidCode: Select all
gtk_adjustment_value_changed_(*Adjustment)
Deprecated since: 3.18
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()
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()
Code: Select all
SetGadgetItemColor(#MainTraceList,1,#PB_Gadget_BackColor, RGB(255,0,0),#PB_All)
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