Editorgadget - which line?

Just starting out? Need help? Post your questions and find answers here.
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Editorgadget - which line?

Post by davido »

How does one tell which is the current line in Editorgadget.

Other gadgets such as ListIcongadget uses getgadgetstate, but this does not appear to work with Editorgadget.

Any suggestions?
DE AA EB
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Re: Editorgadget - which line?

Post by rsts »

Windows only

Code: Select all

SendMessage_(GadgetID(Gadget),#EM_GETSEL,@cursorPos,@cursorPos)
Line may be obtained also.

There are examples if you search - maybe some are cross-platform.

cheers
Little John
Addict
Addict
Posts: 4791
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Editorgadget - which line?

Post by Little John »

Here is a complete demo code, also Windows only:

Code: Select all

; PB 5.11

EnableExplicit

Procedure.i Editor_CurrentLine (editID.i)
   Protected.i selStart, selEnd
   
   SendMessage_(editID, #EM_GETSEL, @selStart, @selEnd)
   ProcedureReturn SendMessage_(editID, #EM_LINEFROMCHAR, selStart, 0)
EndProcedure

#Edit = 0

Define.i i, editID, event

If OpenWindow(0, 0, 0, 322, 150, "EditorGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) = 0
   MessageRequester("Fatal error", "Can't open main window. Program terminated.")
   End
EndIf
EditorGadget(#Edit, 8, 8, 306, 133)
For i = 0 To 2
   AddGadgetItem(#Edit, i, "Line " + Str(i))
Next
editID = GadgetID(#Edit)

Debug "Current line (zero based): " + Str(Editor_CurrentLine(editID))

Repeat
   event = WaitWindowEvent()
   Select event
      Case #WM_LBUTTONUP
         If GetActiveGadget() = #Edit
            Debug "Current line (zero based): " + Str(Editor_CurrentLine(editID))
         EndIf
         
      Case #WM_KEYUP
         If GetActiveGadget() = #Edit
            Debug "Current line (zero based): " + Str(Editor_CurrentLine(editID))
         EndIf
   EndSelect
Until event = #PB_Event_CloseWindow
Last edited by Little John on Sat Mar 30, 2013 1:57 pm, edited 2 times in total.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4954
Joined: Sun Apr 12, 2009 6:27 am

Re: Editorgadget - which line?

Post by RASHAD »

Hi
Cross Platform
At any line just type "#" or any unusual character you choose and use it in the next code

Code: Select all

OpenWindow(0, 0, 0, 322, 150, "EditorGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
EditorGadget(0, 8, 8, 306, 133)
For a = 0 To 5
  AddGadgetItem(0, a, "Line "+Str(a))
Next
    

Repeat
  Select WaitWindowEvent()      
      Case #PB_Event_CloseWindow
            Quit = 1
       
     
      Case #PB_Event_Gadget
          Select EventGadget()
           Case 0
              Select EventType()
                  Case #PB_EventType_Change
                      For line = 0 To CountGadgetItems(0)
                          If FindString(GetGadgetItemText(0, Line), "#")     ;Use any strange character
                             SetGadgetItemText(0, Line,RemoveString(GetGadgetItemText(0, Line), "#"))
                             Debug "Line : "+Str(Line)
                             Break
                          EndIf
                      Next
              EndSelect
          EndSelect          
             
  EndSelect 

Until Quit = 1
End

Windows only
You get Row & Column

Code: Select all

Procedure Caretpos(Gadget)
  GetCaretPos_(p.POINT)
  Row = SendMessage_(GadgetID(0),#EM_LINEFROMCHAR,-1,0)
  Column = SendMessage_(GadgetID(0),#EM_CHARFROMPOS,0,@p) - SendMessage_(GadgetID(0),#EM_LINEINDEX,-1,0)
  SetWindowTitle(0,"Row : "+Str(Row + 1)+"  Column : "+Str(Column))
EndProcedure

OpenWindow(0,0,0,220,120,"",#PB_Window_SystemMenu | #PB_Window_ScreenCentered)
EditorGadget(0,10,10,200,100)

Repeat  
  Select WaitWindowEvent()

;     Case #PB_Event_Gadget    
;      Select EventGadget()      
;       Case 0
;           SetWindowTitle(0,"Row : "+Str(StringGadgetCursorY(0))+"  Column : "+Str(StringGadgetCursorX(0)))
;      EndSelect

    Case #WM_LBUTTONUP
          If GetActiveGadget() = 0
              Caretpos(0)
          EndIf
     
    Case #WM_KEYUP
          If GetActiveGadget() = 0
              Caretpos(0)
          EndIf
    
    Case #PB_Event_CloseWindow
          Quit = 1
          
  EndSelect
  
Until Quit = 1
Egypt my love
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: Editorgadget - which line?

Post by davido »

This is a great Forum. :D

Thanks rsts for your suggestion.
Thanks Little John for your demo.
Thanks RASHAD for you demos. Thanks for the 'lateral thinking' cross-platform method. Typical 'later thinking' — I'd never have thought of it — but so simple once shown!

Whilst you guys were doing this I had a nice lunch :) - Felt good!

Feel even better now!

Regards
DE AA EB
Little John
Addict
Addict
Posts: 4791
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Editorgadget - which line?

Post by Little John »

The following cross-platform code is based on Rashad's idea of utilising the 'change' event, but here is no need to type any special character.

Code: Select all

EnableExplicit

Procedure.s EqualText (s1$, s2$)
   ; return all equal characters on the left of s1$ and s2$
   Protected maxEqual, temp, equal
   Protected *p1.Character, *p2.Character
   
   maxEqual = Len(s1$)
   temp = Len(s2$)
   If maxEqual > temp
      maxEqual = temp
   EndIf
   
   *p1 = @s1$
   *p2 = @s2$
   equal = 0
   While equal < maxEqual And *p1\c = *p2\c
      equal + 1
      *p1 + SizeOf(character)
      *p2 + SizeOf(character)
   Wend
   
   ProcedureReturn Left(s1$, equal)
EndProcedure


#Edit = 0

Define.i i, event, curLine
Define oldContent$, curContent$, left$

If OpenWindow(0, 0, 0, 322, 150, "EditorGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) = 0
   MessageRequester("Fatal error", "Can't open main window. Program terminated.")
   End
EndIf
EditorGadget(#Edit, 8, 8, 306, 133)
For i = 0 To 2
   AddGadgetItem(#Edit, i, "Line " + Str(i))
Next
oldContent$ = GetGadgetText(#Edit)

Debug "Current line (zero based): " + Str(i)

Repeat
   event = WaitWindowEvent()
   Select event         
      Case #PB_Event_Gadget
         Select EventGadget()
            Case #Edit
               Select EventType()
                  Case #PB_EventType_Change
                     curContent$ = GetGadgetText(#Edit)
                     left$ = EqualText(oldContent$, curContent$)
                     curLine = CountString(left$, #LF$)
                     oldContent$ = curContent$
                     Debug "Current line : " + Str(curLine)
               EndSelect
         EndSelect         
   EndSelect
Until event = #PB_Event_CloseWindow
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4954
Joined: Sun Apr 12, 2009 6:27 am

Re: Editorgadget - which line?

Post by RASHAD »

Good one LJ
Another point of view
Except it has one drawback
What if I need to know the line no. without changing the content
Egypt my love
Little John
Addict
Addict
Posts: 4791
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Editorgadget - which line?

Post by Little John »

Hi Rashad,

yes, that's a drawback. I don't have a solution for it.
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Editorgadget - which line?

Post by IdeasVacuum »

A different approach. You can find the current cursor position in the Editor Gadget with

Code: Select all

iPosnY = WindowMouseY(#MyWin) - GadgetY(#MyEditor)
The current number of lines used in the editor is

Code: Select all

iLineCnt = CountGadgetItems(#MyEditor)
So, if you know the text height, you should now be able to determine which is the current line.........
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4954
Joined: Sun Apr 12, 2009 6:27 am

Re: Editorgadget - which line?

Post by RASHAD »

Hi IdeasVacuum
No can not do the job
What if you have more lines and scrollbar scrolled down
Egypt my love
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: Editorgadget - which line?

Post by davido »

Thanks for the continuing interest. :)

I like Little John's idea, pity about the drawback RASHAD found.

I wouldn't know how to do this and I don't know it it would work but may I suggest the following:

Could a keypress be sent automatically?
DE AA EB
Post Reply