Solved: Editor Gadget cursor position
Posted: Fri Oct 25, 2024 5:32 pm
Is there a way to get the line number of the cursor position in an editor gadget?
http://www.purebasic.com
https://www.purebasic.fr/english/
Code: Select all
Procedure GetLineNr()
SendMessage_(GadgetID(0), #EM_GETSEL, @pos, 0)
ProcedureReturn SendMessage_(GadgetID(0), #EM_EXLINEFROMCHAR, 0, pos) + 1
EndProcedure
OpenWindow(0, 0, 0, 300, 200, "GetLineNr() EditorGadget", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
EditorGadget(0, 10, 10, 100, 100)
AddGadgetItem(0, -1, "edit...")
TextGadget(1, 120, 10, 100, 30, "")
AddWindowTimer(0, 123, 250)
Repeat
event = WaitWindowEvent()
Select event
Case #PB_Event_Timer
If EventTimer() = 123
SetGadgetText(1, "Line: " + GetLineNr())
EndIf
EndSelect
Until event = #PB_Event_CloseWindow
Fellow screen reader user here who wrote my own notepad clone in PB a couple years ago. Go for it! PB will not let you downCodingBlind wrote: Sat Mar 08, 2025 5:29 pm For many years, I have thought how cool it would be, to write one's own text editor. Certainly, there are many good text editors available, but the challenge of writing one customised for me...
From the discussions of string operations in PureBasic and my own experimentation, I had concluded PB may not be suitable for the task. However, seeing how quickly the SetEditGadgetText function loaded two of the world's greatest tomes, I have completely changed my mind.
Said texts were the Project Gutenberg texts of Gone With the Wind (208MS) and War And Peace (280MS). The texts were perfectly word-wrapped and my screen reading software did not repeat a line, while reading; this often happens in well regarded editors.
Risking the Wise Admins to ban me, TextMate.CodingBlind wrote: Sat Mar 08, 2025 5:29 pmCertainly, there are many good text editors available, but the challenge of writing one customised for me...
Code: Select all
EnableExplicit
;EditorLastChange()
;Gets the position of the first change since the last call to the same Editor Gadget!
;Dont take this code to serious just wipped it up for fun!
;Notes:
;- Only one gadget at a time is supported!
;- Bug when the same char is added multiple times at the same position (will report whole line size)
;- Looks for the first change!
;- Starts counting from 0
Procedure.i EditorLastChange(Gadget.i,*X.Integer,*Y.Integer);<- to reset set *X or *Y to #Null!
Static.s a,b
Static.i x,y
Protected.i sx,sy
Protected.Unicode *a,*b,*c
If *X = #Null Or *Y = #Null
x = 0
y = 0
b = #Null$
ProcedureReturn #Null
EndIf
a = GetGadgetText(Gadget)
If b And a <> b
*a = @a
*b = @b
While *a\u <> #Null And *b\u <> #Null
If *a\u = 10
*c = *a
sy + 1
EndIf
If *a\u <> *b\u
sx = (*a - *c) >> 1
Break
EndIf
*a + 2
*b + 2
If *a\u = #Null Or *b\u = #Null
If *c = 0
sx = StringByteLength(a) >> 1
Else
sx = (*a - *c) >> 1
EndIf
EndIf
If sx
sx - 1
EndIf
Wend
x = sx
y = sy
EndIf
b = a
*X\i = x
*Y\i = y
ProcedureReturn #Null
EndProcedure
Procedure.i Main()
Protected.i cx,cy
If OpenWindow(0,0,0,320,200,#Null$,#PB_Window_SystemMenu|#PB_Window_ScreenCentered|#PB_Window_MinimizeGadget)
If EditorGadget(0,0,0,WindowWidth(0),WindowHeight(0),#PB_Editor_WordWrap)
Repeat
Select WaitWindowEvent()
Case #PB_Event_Gadget
If EventGadget() = 0 And EventType() = #PB_EventType_Change
EditorLastChange(0,@cx,@cy)
Debug Str(cx) + " x " + Str(cy)
EndIf
Case #PB_Event_CloseWindow
Break
EndSelect
ForEver
EndIf
CloseWindow(0)
EndIf
ProcedureReturn #Null
EndProcedure
End Main()
Yes, it's been answered here many times before.k3pto wrote: Fri Oct 25, 2024 5:32 pmIs there a way to get the line number of the cursor position in an editor gadget?
Code: Select all
If OpenWindow(0, 0, 0, 322, 170, "EditorGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
EditorGadget(0, 8, 8, 306, 133)
TextGadget(1, 8, 147, 306, 20, "Move up/down lines with the cursor")
For a = 1 To 5
AddGadgetItem(0, a, "Line "+Str(a))
Next
SetActiveGadget(0)
Repeat
ev=WaitWindowEvent()
If ev=#WM_KEYDOWN And EventGadget()=0
SetGadgetText(1, "Cursor is on line "+Str(SendMessage_(GadgetID(0),#EM_LINEFROMCHAR,-1,0)+1))
EndIf
Until ev=#PB_Event_CloseWindow
EndIf
I'm so disappointed to hear you're comfortable being severely limited by windows; mac os may seem a gay multimedia thing, but it's like an """upgraded, powerful stable Linux"""…CodingBlind wrote: Sat Mar 08, 2025 9:28 pm Well Piero,
Even if I were interested, I won't be using Textmate. I'm strictly a Windows person; not necessarily because it's better.
As for Python, Perl, Ruby (on rails or not) I have no interest in them, on account of their dependence on indentation. Whilst indentation may be good programming practice, I like that PB does not require it. Also, I like that PB can properly indent code I wish to share with sighted people.