Solved: Editor Gadget cursor position

Everything else that doesn't fall into one of the other PB categories.
k3pto
User
User
Posts: 87
Joined: Sat Jan 17, 2015 5:24 pm

Solved: Editor Gadget cursor position

Post by k3pto »

Is there a way to get the line number of the cursor position in an editor gadget?
Last edited by k3pto on Sun Mar 23, 2025 10:50 pm, edited 1 time in total.
User avatar
chi
Addict
Addict
Posts: 1087
Joined: Sat May 05, 2007 5:31 pm
Location: Austria

Re: Editor Gadget cursor position

Post by chi »

I don't know for which OS you need the code, but on Windows you do it like this:

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
Et cetera is my worst enemy
CodingBlind
New User
New User
Posts: 9
Joined: Wed Mar 22, 2023 5:39 pm

Re: Editor Gadget cursor position

Post by CodingBlind »

chi,

Many thanks for your reply to the original message and the code example. As I am using Windows, it was applicable to me.

In addition to the line number, I would like to be able to retrieve the current cursor column number. Is this possible? Also, using the SendMessage routines, is it possible to place the cursor at a specified line and column? this would be desirable after Find and/or Replace operations.

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.

I eagerly await assistance / comments.
Quin
Addict
Addict
Posts: 1133
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: Editor Gadget cursor position

Post by Quin »

CodingBlind 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.
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 down :)
User avatar
Piero
Addict
Addict
Posts: 923
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: Editor Gadget cursor position

Post by Piero »

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...
Risking the Wise Admins to ban me, TextMate.
That also means Ruby.
CodingBlind
New User
New User
Posts: 9
Joined: Wed Mar 22, 2023 5:39 pm

Re: Editor Gadget cursor position

Post by CodingBlind »

Quin,

Many thanks for your encouragement.
Whether or not I will undertake the project, depends on the answers to my questions. I would like my editor to be MDI.
As a serial / procedural programmer, I find the syntax of PB suits the way I think. Also, the IDE works well with JAWS and, likewise, controls are accessible right out of the box.
If you would care to contact me privately, I would welcome that. But, I shall not feel slighted if you choose not. My recollection from another discussion, is that you are many years younger than me.
User avatar
Piero
Addict
Addict
Posts: 923
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: Editor Gadget cursor position

Post by Piero »

I have a lot of positive things to say about PB, but that's not my duty; my Duty is to roast Fred. :mrgreen:
CodingBlind
New User
New User
Posts: 9
Joined: Wed Mar 22, 2023 5:39 pm

Re: Editor Gadget cursor position

Post by CodingBlind »

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.
User avatar
Mijikai
Addict
Addict
Posts: 1520
Joined: Sun Sep 11, 2016 2:17 pm

Re: Editor Gadget cursor position

Post by Mijikai »

Not a solution but i wanted to post this anyway :)

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()
BarryG
Addict
Addict
Posts: 4173
Joined: Thu Apr 18, 2019 8:17 am

Re: Editor Gadget cursor position

Post by BarryG »

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?
Yes, it's been answered here many times before. :) But for Windows:

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
User avatar
Piero
Addict
Addict
Posts: 923
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: Editor Gadget cursor position

Post by Piero »

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.
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"""…

I mentioned Ruby just as a way to customize your text editor; with textmate you can also easily use shell, python etc etc etc…

you can explore its bundles and enjoy tons of extremely good coding examples…
I wonder why it's becoming obsolete while (at least for me) the alternatives seem to be worse…

PS: in case, downgrade to older mac os…
Post Reply