Basic Univers

#SCF_ALL = 4

#PFM_NUMBERINGSTYLE= $2000
#PFM_NUMBERINGTAB= $4000
#PFM_NUMBERINGSTART= $8000

Enumeration
  #Editor_0
  #Editor_1
EndEnumeration


Global egPara.PARAFORMAT2
egPara\cbSize = SizeOf(PARAFORMAT2)
egPara\dwMask = #PFM_NUMBERING | #PFM_NUMBERINGSTART | #PFM_NUMBERINGSTYLE | #PFM_NUMBERINGTAB
egPara\wNumbering = 2       ; 2 = 1..2..3 ; 3 = a..b..c ; 4 = A..B..C ; 5 = i...ii...iii ; 6 = I...II...III
egPara\wNumberingStart = 1   ; First number to use
egPara\wNumberingStyle = $100 ; 0 = NUM) ; $100 = (NUM) ; $200 = NUM. ; $300 = NUM
egPara\wNumberingTab = 700    ; twips
 
Procedure ALineNumber(hWnd, msg, wParam, lParam)
  result = #PB_ProcessPureBasicEvents
  Shared previousLineCount
  Select msg
    Case #WM_COMMAND
      Select wParam>>16 &$FFFF
        Case #EN_CHANGE
         
          currentLineCount = SendMessage_(lParam, #EM_GETLINECOUNT , 0, 0)
          ; MessageRequester("",Str(currentLineCount))
          If currentLineCount <> previousLineCount
            currentLine = SendMessage_(lParam, #EM_LINEFROMCHAR, - 1, 0)
            ; MessageRequester("",Str(currentLine))
            currentPos = SendMessage_(lParam, #EM_GETSEL, @startPos, @endPos)
            ; --> Get current scroll postion
            ; --<  pour y revenir aprés réécriture des items
            SendMessage_(lParam, #EM_GETSCROLLPOS, 0, @scrollP.POINT)
            egPara\wNumberingStart = currentLine + 1
            ; --> Désactive la réaffichage de l'editorGadget
            ; --< pour éviter le scrolling lors de la réécriture des lignes
            SendMessage_(lParam, #WM_SETREDRAW, 0, 0)
            For a = currentLine - 1 To currentLineCount - 1
              egPara\wNumberingStart = a + 1
              line.s = GetGadgetItemText(GetActiveGadget(), a, 0)
              ; --> Line numbering only continues with valid item text
              ; --> so we'll add a temporary space character
              If line = ""
                line = " "
              EndIf
              SetGadgetItemText(GetActiveGadget(), a, line, 0)
              SendMessage_(lParam, #EM_SETPARAFORMAT, 0, egPara)
              ; --> On peut enlever le caractère espace temporaire
              If line = " "
                line = ""
                SetGadgetItemText(GetActiveGadget(), a, line, 0)
              EndIf
            Next a
           ; --> Put caret back To correct position
            SendMessage_(lParam, #EM_SETSCROLLPOS, 0, scrollP.POINT)
            SendMessage_(lParam, #EM_SETSEL, startPos, startPos)
           ; --> Turn redraw back on For EditorGadget
            SendMessage_(lParam, #WM_SETREDRAW, 1, 0)
           ; --> Redraw EditorGadget To show new line numbers
            InvalidateRect_(lParam, 0, 1)
          EndIf
          previousLineCount = SendMessage_(lParam, #EM_GETLINECOUNT , 0, 0)
      EndSelect
  EndSelect
  ProcedureReturn result
 EndProcedure
If OpenWindow(0, 0, 0, 800, 600, "EditorGadget with Line Numbers", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) And CreateGadgetList(WindowID(0))
  EditorGadget(#Editor_0, 0, 0, 400, 600)
  EditorGadget(#Editor_1, 400, 0, 400, 600)
  SendMessage_(GadgetID(#Editor_0), #EM_SETEVENTMASK, 0, #ENM_CHANGE)
  SendMessage_(GadgetID(#Editor_1), #EM_SETEVENTMASK, 0, #ENM_CHANGE)
  For l = 1 To 260
    AddGadgetItem(#Editor_0, l, "Line " + Str(l))
    AddGadgetItem(#Editor_1, l, "Line " + Str(l))
    SendMessage_(GadgetID(#Editor_0), #EM_SETPARAFORMAT, 0, egPara)
    SendMessage_(GadgetID(#Editor_1), #EM_SETPARAFORMAT, 0, egPara)
    egPara\wNumberingStart + 1
  Next
 
  SetWindowCallback(@ALineNumber())

  Repeat
    event = WaitWindowEvent()
  Until event = #PB_Event_CloseWindow
EndIf
End