Page 1 of 1

Editorgadget - which line?

Posted: Sat Mar 30, 2013 10:42 am
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?

Re: Editorgadget - which line?

Posted: Sat Mar 30, 2013 11:44 am
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

Re: Editorgadget - which line?

Posted: Sat Mar 30, 2013 12:16 pm
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

Re: Editorgadget - which line?

Posted: Sat Mar 30, 2013 12:48 pm
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

Re: Editorgadget - which line?

Posted: Sat Mar 30, 2013 2:40 pm
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

Re: Editorgadget - which line?

Posted: Sat Mar 30, 2013 3:18 pm
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

Re: Editorgadget - which line?

Posted: Sat Mar 30, 2013 3:30 pm
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

Re: Editorgadget - which line?

Posted: Sat Mar 30, 2013 5:16 pm
by Little John
Hi Rashad,

yes, that's a drawback. I don't have a solution for it.

Re: Editorgadget - which line?

Posted: Sat Mar 30, 2013 7:17 pm
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.........

Re: Editorgadget - which line?

Posted: Sat Mar 30, 2013 7:23 pm
by RASHAD
Hi IdeasVacuum
No can not do the job
What if you have more lines and scrollbar scrolled down

Re: Editorgadget - which line?

Posted: Sat Mar 30, 2013 9:08 pm
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?