EditorGadget with Linenumbers

Just starting out? Need help? Post your questions and find answers here.
Sparkie
PureBatMan Forever
PureBatMan Forever
Posts: 2307
Joined: Tue Feb 10, 2004 3:07 am
Location: Ohio, USA

Post by Sparkie »

You're welcome Henrik, and thank you for all of your help and input as well :)

Unless someone spots a bug in the code, I think this is about as far as I will go with this. Everything else is just cosmetics as well as enabling Save, Save As, and checking for valid clipboard content.

The main purpose was to add line numbers and I think we reached that goal, ...and then some ;)

Code: Select all

; ************************************************************* 
; Title:          Sparkies EditorGadget with line numbers" 
; Author:         Spakie 
; Start Date:     December 24, 2004 9:50 AM 
; Version 0.27B:  December 29, 2004 2:10 PM 
; License:        Free to use, optimize, and modify at will :) 
; ************************************************************* 

; ********************************************************
; --> Start Constants
; ********************************************************
#SES_EMULATESYSEDIT = 1 
#SCF_ALL = 4 
#PFM_NUMBERINGSTART = $8000 
#PFM_NUMBERINGSTYLE = $2000 
#PFM_NUMBERINGTAB = $4000 
; ********************************************************
; <-- End Constants
; ********************************************************

; ********************************************************
; --> Start Enumerations
; ********************************************************
Enumeration 
  #MainWin 
EndEnumeration 
Enumeration 
  #StatusBar 
  #MainMenu 
  #EditorPopup 
EndEnumeration 
Enumeration 
  #LineNumbers 
  #Editor 
  #Lines_Conatiner 
EndEnumeration 
; ********************************************************
; <-- End Enumerations
; ********************************************************

; ********************************************************
; --> Start Structures
; ********************************************************
; --> CHARRANGE structure for 'Select All' popup menu command 
editSel.CHARRANGE 
; --> CHARFORMAT structure for text formatting 
egFormat.CHARFORMAT 
egFormat\cbSize = SizeOf(CHARFORMAT) 
; ********************************************************
; <-- End Structures
; ********************************************************

; ********************************************************
; --> Start Main window callback procedure
; ********************************************************
Procedure.l myWindowCallback(hwnd, msg, wparam, lparam) 
  Shared previousItems 
  result = #PB_ProcessPureBasicEvents 
  Select msg 
    Case #WM_COMMAND 
      If lparam = GadgetID(#Editor) 
        Select wparam >>16&$FFFF 
          Case #EN_VSCROLL 
            ; --> Keep linenumbers in sync with EditorGadget scrolling up or down (clicking scroll buttons) 
            SendMessage_(GadgetID(#Editor), #EM_GETSCROLLPOS, 0, @egOne.POINT) 
            ; --> Keep numbers from scrolling left 
            egOne\x = 0 
            SendMessage_(GadgetID(#LineNumbers), #EM_SETSCROLLPOS, 0, egOne) 
          Case #EN_UPDATE 
            ; --> Keep linenumbers in sync with EditorGadget scrolling up or down (dragging scroll thumb) 
            SendMessage_(GadgetID(#Editor), #EM_GETSCROLLPOS, 0, @egOne.POINT) 
            ; --> Keep numbers from scrolling left 
            egOne\x = 0 
            SendMessage_(GadgetID(#LineNumbers), #EM_SETSCROLLPOS, 0, egOne) 
          Case #EN_CHANGE 
            ; --> Keep linenumbers in sync with EditorGadget adding or removing items 
            currentLine = SendMessage_(GadgetID(#Editor), #EM_LINEFROMCHAR, -1, 0)+1 
            lnItems = CountGadgetItems(#LineNumbers) 
            egItems = CountGadgetItems(#Editor) 
            ; --> Add neeeded number of items in Linenumbers
            If egItems > lnItems
              For i = lnItems+1 To egItems 
                AddGadgetItem(#LineNumbers, i, RSet(Str(i), 4, "0")) 
              Next i 
            EndIf 
            ; --> Remove un-neeeded number of items in Linenumbers
            If egItems < lnItems
              For i = lnItems To egItems Step -1
                RemoveGadgetItem(#LineNumbers, i)
              Next i 
              ; --> Remove the last CR/LF left behind by RemoveGadgetItem 
              ; --> Readonly off for linenumbers 
              SendMessage_(GadgetID(#LineNumbers), #EM_SETREADONLY, 0, 0) 
              SendMessage_(GadgetID(#LineNumbers), #WM_KEYDOWN, #VK_BACK, 0) 
              SendMessage_(GadgetID(#LineNumbers), #WM_KEYUP, #VK_BACK, 0) 
              ; --> Readonly for linenumbers 
              SendMessage_(GadgetID(#LineNumbers), #EM_SETREADONLY, 1, 0) 
            EndIf 
            ; --> Keep linenumbers in sync with EditorGadget scrolling up or down (dragging scroll thumb) 
            SendMessage_(GadgetID(#Editor), #EM_GETSCROLLPOS, 0, @egOne.POINT) 
            ; --> Keep numbers from scrolling left 
            egOne\x = 0 
            SendMessage_(GadgetID(#LineNumbers), #EM_SETSCROLLPOS, 0, egOne) 
        EndSelect 
      EndIf 
  EndSelect 
  ProcedureReturn result 
EndProcedure 
; ********************************************************
; <-- End Main window callback procedure
; ********************************************************

; ********************************************************
; --> Start open file procedure 
; ********************************************************
Procedure openTheFile() 
  oFile$ = OpenFileRequester("Select a File", "c:\", "Text file (.txt) | *.txt", 0) 
  If FileSize(oFile$) > 0 And FileSize(oFile$) < 65535 
    If ReadFile(0, oFile$) 
      ClearGadgetItemList(#LineNumbers) 
      ClearGadgetItemList(#Editor) 
      While Eof(0) = 0 
        egText$ + ReadString() + Chr(13) 
      Wend 
      AddGadgetItem(#Editor, -1, egText$) 
      CloseFile(0) 
    Else 
      MessageRequester("Error", "Could not open file: " + oFile$, #MB_ICONERROR) 
    EndIf 
  Else 
    MessageRequester("Error", "File not found: " + oFile$, #MB_ICONERROR) 
  EndIf 
EndProcedure 
; ********************************************************
; <-- End open file procedure 
; ********************************************************

; ********************************************************
; --> Start main window , menu, and gadgets 
; ********************************************************
If OpenWindow(#MainWin, 0, 0, 700, 500, #PB_Window_SystemMenu | #PB_Window_ScreenCentered, "EditorGadget w/Line Numbers") And CreateGadgetList(WindowID(0)) 
  SetWindowCallback(@myWindowCallback()) 
  CreateStatusBar(#StatusBar, WindowID(#MainWin))
  ; ********************************************************
  ; --> Start Main menu 
  ; ********************************************************
  CreateMenu(#MainMenu, WindowID(#MainWin)) 
  MenuTitle("&File") 
  MenuItem(101, "&Open..." + Chr(9 ) + "Ctrl+O") 
  MenuItem(102, "&Save") 
  MenuItem(103, "S&ave as") 
  MenuItem(104, "&Quit" + Chr(9) + "Ctrl+Q") 
  MenuTitle("&Edit") 
  MenuItem(111, "&Undo" + Chr(9 ) + "Ctrl+z") 
  MenuBar() 
  MenuItem(112, "&Cut" + Chr(9 ) + "Ctrl+X") 
  MenuItem(113, "C&opy" + Chr(9 ) + "Ctrl+C") 
  MenuItem(114, "&Paste" + Chr(9 ) + "Ctrl+V") 
  MenuBar() 
  MenuItem(115, "Select &All" + Chr(9 ) + "Ctrl+A") 
  MenuTitle("&Options")
  OpenSubMenu("&Editor")
  MenuItem(121, "&Font...")
  MenuItem(122, "&Background Color...")
  CloseSubMenu()
  OpenSubMenu("&Numbers")
  MenuItem(123, "&Font Color...")
  MenuItem(124, "&Background Color...")
  CloseSubMenu()
  ; --> Disable 'save' and 'save as' for now 
  DisableMenuItem(102, 1) 
  DisableMenuItem(103, 1) 
  ; ********************************************************
  ; <-- End Main menu 
  ; ********************************************************
  
  ; ********************************************************
  ; --> Start keyboard shortcuts
  ; ********************************************************
  AddKeyboardShortcut(#MainWin, #PB_Shortcut_Control | #PB_Shortcut_O, 101) 
  ; disabled AddKeyboardShortcut(#MainWin, #PB_Shortcut_Control | #PB_Shortcut_S, 102) 
  ; disabled AddKeyboardShortcut(#MainWin, #PB_Shortcut_Control | #PB_Shortcut_A, 103) 
  AddKeyboardShortcut(#MainWin, #PB_Shortcut_Control | #PB_Shortcut_Q, 104) 
  ; ********************************************************
  ; <-- End keyboard shortcuts
  ; ********************************************************
  
  ; ********************************************************
  ; --> Start EditorGadget for Linenumbers
  ; ********************************************************
  ; --> Container is narrower than Linenumbers to hide scrollbar
  ContainerGadget(#Lines_Conatiner, 0, 0, 60, 460) 
  EditorGadget(#LineNumbers, 3, 3, 85, 454) 
  AddGadgetItem(#LineNumbers, -1, "0001") 
  ; --> Set background color for numbers 
  SendMessage_(GadgetID(#LineNumbers), #EM_SETBKGNDCOLOR, 0, RGB(248, 248, 220)) 
  ; --> Readonly for linenumbers 
  SendMessage_(GadgetID(#LineNumbers), #EM_SETREADONLY, 1, 0) 
  CloseGadgetList() 
  ; ********************************************************
  ; <-- End EditorGadget for Linenumbers
  ; ********************************************************
  
  ; ********************************************************
  ; --> Start EditorGadget for Editor
  ; ********************************************************
  EditorGadget(#Editor, 60, 3, 637, 454) 
  ; --> Set left margin for #Editor
  SendMessage_(GadgetID(#Editor), #EM_SETMARGINS, #EC_LEFTMARGIN, 5)
  ; --> Draft mode forces ASCII text at all times
  SendMessage_(GadgetID(#Editor), #EM_SETEDITSTYLE , #SES_EMULATESYSEDIT, #SES_EMULATESYSEDIT) 
  ; --> Set Editor to 64K text limit 
  SendMessage_(GadgetID(#Editor), #EM_SETLIMITTEXT, 0, 0) 
  ; --> Set background color for Editorgadget 
  SendMessage_(GadgetID(#Editor), #EM_SETBKGNDCOLOR, 0, RGB(228, 228, 200)) 
  ; --> Set #EN_UPDATE, #EN_CHANGE and #EN_SCROLL event catching for EditorGadget 
  SendMessage_(GadgetID(#Editor), #EM_SETEVENTMASK, 0, #ENM_UPDATE | #ENM_CHANGE | #ENM_SCROLL | #ENM_KEYEVENTS) 
  ; ********************************************************
  ; <-- End EditorGadget for Editor
  ; ********************************************************
  
  ; *******************************************************
  ; --> Start filling CHARFORMAT structure 
  ; ********************************************************
  ; --> Set our formatting mask to change font, size, and color
  egFormat\dwMask =  #CFM_SIZE | #CFM_COLOR | #CFM_FACE
  ; --> I'll use 12pt Arial (yHeight is twips 1/1440 of an inch | 1/20 of printer point) 
  egFormat\yHeight = 12*20 
  fontName$ = "Arial"
  PokeS(@egFormat\szFaceName, fontName$)
  ; --> Send info to both EditorGadgets 
  egFormat\crTextColor = RGB(0, 80, 0) 
  SendMessage_(GadgetID(#LineNumbers), #EM_SETCHARFORMAT, #SCF_ALL, @egFormat) 
  egFormat\crTextColor = RGB(0, 0, 80) 
  SendMessage_(GadgetID(#Editor), #EM_SETCHARFORMAT, #SCF_ALL, @egFormat) 
  ; *******************************************************
  ; <-- End filling CHARFORMAT structure 
  ; ********************************************************
  
  ; ********************************************************
  ; --> Start popup menu for EditorGadget 
  ; ********************************************************
  CreatePopupMenu(#EditorPopup) 
  MenuItem(201, "&Undo") 
  MenuBar() 
  MenuItem(202, "&Cut") 
  MenuItem(203, "C&opy") 
  MenuItem(204, "&Paste") 
  MenuBar() 
  MenuItem(205, "Select &All") 
  ; ********************************************************
  ; <-- End popup menu for EditorGadget 
  ; ********************************************************
  
  ; ********************************************************
  ; --> Start Main loop 
  ; ********************************************************
  Repeat 
    event = WaitWindowEvent() 
    ; --> Keep focus out of linenumbers 
    If EventGadgetID() = #LineNumbers 
      ActivateGadget(#Editor) 
    EndIf 
    Select event 
      Case #WM_RBUTTONDOWN 
        xPos = WindowMouseX()-5
        yPos = WindowMouseY()-5
        ; ********************************************************
        ; --> Start display popup menu for EditorGadget 
        ; ********************************************************
        ; --> Make sure mouse is over Editor befor displaying popup menu
        If xPos >= GadgetX(#Editor)+2 And xPos <= (GadgetX(#Editor)+ GadgetWidth(#Editor)-4)
          If yPos >= GadgetY(#Editor)+2 And yPos <= (GadgetY(#Editor)+ GadgetHeight(#Editor)-4)
            If Len(GetGadgetText(#Editor)) < 1 
              ; --> If no text found in Editor, disable all menu items except 'Paste'
              DisableMenuItem(201,1) 
              DisableMenuItem(202,1) 
              DisableMenuItem(203,1) 
              DisableMenuItem(205,1) 
              DisableMenuItem(206,1) 
            Else 
              ; --> otherwise, enable all menu items
              DisableMenuItem(201,0) 
              DisableMenuItem(202,0) 
              DisableMenuItem(203,0) 
              DisableMenuItem(205,0) 
              DisableMenuItem(206,0) 
            EndIf 
            DisplayPopupMenu(#EditorPopup, WindowID()) 
          EndIf 
        EndIf
        ; ********************************************************
        ; <-- End display popup menu for EditorGadget 
        ; ********************************************************
        
        ; ********************************************************
        ; --> Start handling menu events
        ; ******************************************************** 
      Case #PB_EventMenu 
        Select EventMenuID() 
          ; --> Main menu items 101 - 199 
          Case 101 
            ; --> File > Open
            openTheFile() 
          Case 102 
            ; --> File > Save  
            Debug "Save disabled" 
          Case 103 
            ; --> File > Save As
            Debug "Save as disabled" 
          Case 104 
            ; --> File > Quit
            CloseWindow(#MainWin) 
          Case 111 
            ; --> Edit > Undo
            SendMessage_(GadgetID(#Editor), #WM_UNDO, 0, 0) 
          Case 112 
            ; --> Edit > Cut
            SendMessage_(GadgetID(#Editor), #WM_CUT, 0, 0) 
          Case 113 
            ; --> Edit > Copy
            SendMessage_(GadgetID(#Editor), #WM_COPY, 0, 0) 
            SendMessage_(GadgetID(#Editor), #EM_SETSEL, -1, 0) 
            ActivateGadget(#Editor) 
          Case 114 
            ; --> Edit > Paste
            ; >>>>>> Still need to check for proper format do disable paste command as necessary <<<<<< 
            SendMessage_(GadgetID(#Editor), #WM_PASTE, 0, 0) 
          Case 115 
            ; --> Edit > Select All
            editSel\cpMin = 0 
            editSel\cpMax = -1 
            SendMessage_(GadgetID(#Editor), #EM_EXSETSEL, 0, @editSel) 
          Case 121
            ; --> Options > Editor > Font
            If fontSelector = #True
              FontRequester(SelectedFontName(), SelectedFontSize(), #PB_FontRequester_Effects)
            Else
              If FontRequester("Arial", 11, #PB_FontRequester_Effects) > 0
                fontSelector = #True
              EndIf
            EndIf
            fontName$ = SelectedFontName()
            egFormat\crTextColor = SelectedFontColor()
            If SelectedFontSize() > 14
              MessageRequester("Attention", "Defaulting to max font size of 14", #MB_ICONINFORMATION)
              egFormat\yHeight = 14*20
            EndIf
            PokeS(@egFormat\szFaceName, fontName$)
            egFormat\dwMask =  #CFM_SIZE | #CFM_COLOR | #CFM_FACE
            SendMessage_(GadgetID(#Editor), #EM_SETCHARFORMAT, #SCF_ALL, @egFormat) 
            egFormat\dwMask =  #CFM_SIZE | #CFM_FACE
            SendMessage_(GadgetID(#LineNumbers), #EM_SETCHARFORMAT, #SCF_ALL, @egFormat) 
          Case 122
            ; --> Options > Editor > Background Color
            egBackColor = ColorRequester()
            If egBackColor > -1
              SendMessage_(GadgetID(#Editor), #EM_SETBKGNDCOLOR, 0, egBackColor)
            EndIf
          Case 123
            ; --> Options > Numbers > Font Color
            lnTextColor = ColorRequester()
            egFormat\crTextColor = lnTextColor
            If lnTextColor > -1
              egFormat\dwMask =  #CFM_COLOR
              SendMessage_(GadgetID(#LineNumbers), #EM_SETCHARFORMAT, #SCF_ALL, @egFormat) 
            EndIf
          Case 124
            ; --> Options > Numbers > Background Color
            egBackColor = ColorRequester()
            If egBackColor > -1
              SendMessage_(GadgetID(#LineNumbers), #EM_SETBKGNDCOLOR, 0, egBackColor)
            EndIf
          ; --> Popup menu items 201 - 299 
          Case 201 
            ; --> Undo
            SendMessage_(GadgetID(#Editor), #WM_UNDO, 0, 0) 
          Case 202 
            ; --> Cut
            SendMessage_(GadgetID(#Editor), #WM_CUT, 0, 0) 
          Case 203 
            ; --> Copy
            SendMessage_(GadgetID(#Editor), #WM_COPY, 0, 0) 
            SendMessage_(GadgetID(#Editor), #EM_SETSEL, -1, 0); 
            ActivateGadget(#Editor) 
          Case 204 
            ; --> Paste
            ; >>>>>> Still need to check for proper format do disable paste command as necessary <<<<<< 
            SendMessage_(GadgetID(#Editor), #WM_PASTE, 0, 0) 
          Case 205 
            ; --> Select All
            editSel\cpMin = 0 
            editSel\cpMax = -1 
            SendMessage_(GadgetID(#Editor), #EM_EXSETSEL, 0, @editSel) 
        EndSelect 
        ; ********************************************************
        ; <-- End handling menu events
        ; ******************************************************** 
    EndSelect 
  Until event = #PB_Event_CloseWindow  
EndIf 
; ********************************************************
; <-- End Main loop 
; ********************************************************
End
What goes around comes around.

PB 5.21 LTS (x86) - Windows 8.1
Henrik
Enthusiast
Enthusiast
Posts: 404
Joined: Sat Apr 26, 2003 5:08 pm
Location: Denmark

Post by Henrik »

The main purpose was to add line numbers and I think we reached that goal, ...and then some icon_wink.gif
Indeed, indeed :D
And Thanks for your effort Sparkie :D
Best Regrads
Henrik.
sverson
Enthusiast
Enthusiast
Posts: 286
Joined: Sun Jul 04, 2004 12:15 pm
Location: Germany

RichEdit LineNumbering stops counting at 255

Post by sverson »

RichEdit LineNumbering stops counting at 255

Hi Sparkie - great work your last post :D

Can you remember one of your first examples:

Code: Select all

;/ http://forums.purebasic.com/english/viewtopic.php?t=13467
;/ Posted: Fri Dec 24, 2004 4:47 pm - Sparkie

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

Structure myPARAFORMAT2
  cbSize.l
  dwMask.l
  wNumbering.w
  wEffects.w
  dxStartIndent.l
  dxRightIndent.l
  dxOffset.l
  wAlignment.w
  cTabCount.w
  rgxTabs.l[#MAX_TAB_STOPS]
  dySpaceBefore.l
  dySpaceAfter.l
  dyLineSpacing.l
  sStyle.w
  bLineSpacingRule.b
  bOutlineLevel.b
  wShadingWeight.w
  wShadingStyle.w
  wNumberingStart.w
  wNumberingStyle.w
  wNumberingTab.w
  wBorderSpace.w
  wBorderWidth.w
  wBorders.w
EndStructure


egPara.myPARAFORMAT2
egPara\cbSize = SizeOf(myPARAFORMAT2)
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 = $300 ; 0 = NUM) ; $100 = (NUM) ; $300 = NUM
egPara\wNumberingTab = 450    ; twips


If OpenWindow(0, 0, 0, 300, 150, #PB_Window_SystemMenu | #PB_Window_ScreenCentered, "EditorGadget with Line Numbers") And CreateGadgetList(WindowID(0))
  EditorGadget (0, 10, 10, 280, 130)
  SendMessage_(GadgetID(0), #EM_SETPARAFORMAT, 0, egPara)
  For l = 0 To 300 ;/ <- modified
    AddGadgetItem(0, l, "Line " + Str(l))
  Next
  Repeat
    Event = WaitWindowEvent()
  Until Event = #PB_Event_CloseWindow
EndIf
End
The line numbers stop counting at 255 :? :!:

Du you - or does anybody - know the reason?

:wink: sverson

P.S. maybe the answer is somewhere in the thread - but i didn't find it.
Sparkie
PureBatMan Forever
PureBatMan Forever
Posts: 2307
Joined: Tue Feb 10, 2004 3:07 am
Location: Ohio, USA

Post by Sparkie »

Hi sverson :)

I didn't fiqure out the max 255 linenumbering until you brought this back up. 8) Try changing line 41

Code: Select all

egPara\wNumberingStyle = $300
to

Code: Select all

egPara\wNumberingStyle = $400
What goes around comes around.

PB 5.21 LTS (x86) - Windows 8.1
sverson
Enthusiast
Enthusiast
Posts: 286
Joined: Sun Jul 04, 2004 12:15 pm
Location: Germany

Post by sverson »

Hi Sparkie :)

$400 leads to nirwana -> no linenumbering/slow display

Code: Select all

egPara\wNumberingStart = 100
lets the linenumbering stop at 354 :?

**edit**
Image
**end edit**

Perhaps 255 lines is the MCBL (Microsoft Coder Brain Limit) :D
... :arrow: that's why MS just sells betas ...

:wink: sverson
Sparkie
PureBatMan Forever
PureBatMan Forever
Posts: 2307
Joined: Tue Feb 10, 2004 3:07 am
Location: Ohio, USA

Post by Sparkie »

Sorry about that sverson. Weak coffee today ;)
This code works for line numbers > 255 but if you add or remove lines, the numbering sequence is lost. :( You may be right about the MCBL factor :lol:

Code: Select all

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

Structure myPARAFORMAT2 
  cbSize.l 
  dwMask.l 
  wNumbering.w 
  wEffects.w 
  dxStartIndent.l 
  dxRightIndent.l 
  dxOffset.l 
  wAlignment.w 
  cTabCount.w 
  rgxTabs.l[#MAX_TAB_STOPS] 
  dySpaceBefore.l 
  dySpaceAfter.l 
  dyLineSpacing.l 
  sStyle.w 
  bLineSpacingRule.b 
  bOutlineLevel.b 
  wShadingWeight.w 
  wShadingStyle.w 
  wNumberingStart.w 
  wNumberingStyle.w 
  wNumberingTab.w 
  wBorderSpace.w 
  wBorderWidth.w 
  wBorders.w 
EndStructure 

egPara.myPARAFORMAT2 
egPara\cbSize = SizeOf(myPARAFORMAT2) 
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 = $300 ; 0 = NUM) ; $100 = (NUM) ; $300 = NUM 
egPara\wNumberingTab = 450    ; twips 

If OpenWindow(0, 0, 0, 300, 150, #PB_Window_SystemMenu | #PB_Window_ScreenCentered, "EditorGadget with Line Numbers") And CreateGadgetList(WindowID(0)) 
  EditorGadget (0, 10, 10, 280, 130) 
  For l = 1 To 300 ;/ <- modified 
    AddGadgetItem(0, l, "Line " + Str(l)) 
    SendMessage_(GadgetID(0), #EM_SETPARAFORMAT, 0, egPara) 
    egPara\wNumberingStart +1
  Next 
  Repeat 
    event = WaitWindowEvent() 
  Until event = #PB_Event_CloseWindow 
EndIf 
End
What goes around comes around.

PB 5.21 LTS (x86) - Windows 8.1
sverson
Enthusiast
Enthusiast
Posts: 286
Joined: Sun Jul 04, 2004 12:15 pm
Location: Germany

Post by sverson »

Thx Sparkie!

It works :) as long as noone touches a key - well - i will print red labels:
!!!PLEASE REMOVE YOUR KEYBOARD BEFORE USING THIS SOFTWARE !!!

OK the MCBL makes us writing our own linenumbering to get stable results...

:wink: sverson
Sparkie
PureBatMan Forever
PureBatMan Forever
Posts: 2307
Joined: Tue Feb 10, 2004 3:07 am
Location: Ohio, USA

Post by Sparkie »

I get the feeling that in order to use the MS version line numbering, you'll have to rewrite each line after adding or removing a line anywhere other than at the end. :(

Seems easier to just use your method of removing keyboard :lol:
What goes around comes around.

PB 5.21 LTS (x86) - Windows 8.1
Sparkie
PureBatMan Forever
PureBatMan Forever
Posts: 2307
Joined: Tue Feb 10, 2004 3:07 am
Location: Ohio, USA

Post by Sparkie »

Taking my own advice
Sparkie wrote:I get the feeling that in order to use the MS version line numbering, you'll have to rewrite each line after adding or removing a line anywhere other than at the end.
here's what I came up with. WinME or greater required.

Code: Select all

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

; --> CHARFORMAT structure for text formatting 
egFormat.CHARFORMAT 
egFormat\cbSize = SizeOf(CHARFORMAT) 
; --> Set our formatting mask to change font, size, and color 
egFormat\dwMask =  #CFM_SIZE | #CFM_COLOR | #CFM_FACE 
  ; --> I'll use 12pt Arial (yHeight is twips 1/1440 of an inch | 1/20 of printer point) 
egFormat\yHeight = 12*20 
fontName$ = "Arial" 
PokeS(@egFormat\szFaceName, fontName$) 
egFormat\crTextColor = RGB(0, 50, 0) 

Enumeration
  #Editor_0
EndEnumeration

Structure myPARAFORMAT2 
  cbSize.l 
  dwMask.l 
  wNumbering.w 
  wEffects.w 
  dxStartIndent.l 
  dxRightIndent.l 
  dxOffset.l 
  wAlignment.w 
  cTabCount.w 
  rgxTabs.l[#MAX_TAB_STOPS] 
  dySpaceBefore.l 
  dySpaceAfter.l 
  dyLineSpacing.l 
  sStyle.w 
  bLineSpacingRule.b 
  bOutlineLevel.b 
  wShadingWeight.w 
  wShadingStyle.w 
  wNumberingStart.w 
  wNumberingStyle.w 
  wNumberingTab.w 
  wBorderSpace.w 
  wBorderWidth.w 
  wBorders.w 
EndStructure 

egPara.myPARAFORMAT2 
egPara\cbSize = SizeOf(myPARAFORMAT2) 
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 = $300 ; 0 = NUM) ; $100 = (NUM) ; $300 = NUM 
egPara\wNumberingTab = 700    ; twips 

Global egPara
Procedure myWindowCallback(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)
          If currentLineCount <> previousLineCount
            currentLine = SendMessage_(lParam, #EM_LINEFROMCHAR, -1, 0)
            currentPos = SendMessage_(lParam, #EM_GETSEL, @startPos, @endPos)
            ; --> Get current scroll postion so we can come back here after re-writing itmes
            SendMessage_(lParam, #EM_GETSCROLLPOS, 0, @scrollP.POINT)
            egPara\wNumberingStart = currentLine+1
            ; --> Turn redraw off for EditorGadget to prevent scrolling when re-writing items
            SendMessage_(lParam, #WM_SETREDRAW, 0, 0)
            For a = currentLine-1 To currentLineCount -1
              egPara\wNumberingStart = a + 1
              t$ = GetGadgetItemText(#Editor_0, a, 0)
              ; --> Line numbering only continues with valid item text
              ; --> so we'll add a temporary space character
              If t$ = ""
                t$ = " "
              EndIf
              SetGadgetItemText(#Editor_0, a, t$, 0)
              SendMessage_(lParam, #EM_SETPARAFORMAT, 0, egPara)
              ; --> We can now remove the temporay space character
              If t$ = " "
                t$ = ""
                SetGadgetItemText(#Editor_0, a, t$, 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, 600, 450, #PB_Window_SystemMenu | #PB_Window_ScreenCentered, "EditorGadget with Line Numbers") And CreateGadgetList(WindowID(0)) 
  EditorGadget (#Editor_0, 0, 0, 600, 450) 
  SendMessage_(GadgetID(#Editor_0), #EM_SETCHARFORMAT, #SCF_ALL, @egFormat) 
  SendMessage_(GadgetID(#Editor_0), #EM_SETEVENTMASK, 0, #ENM_CHANGE) 
  For l = 1 To 300
    AddGadgetItem(#Editor_0, l, "Line " + Str(l)) 
    SendMessage_(GadgetID(#Editor_0), #EM_SETPARAFORMAT, 0, egPara) 
    egPara\wNumberingStart +1 
  Next 
  SetWindowCallback(@myWindowCallback())
  Repeat 
    event = WaitWindowEvent() 
  Until event = #PB_Event_CloseWindow 
EndIf 
End
What goes around comes around.

PB 5.21 LTS (x86) - Windows 8.1
sverson
Enthusiast
Enthusiast
Posts: 286
Joined: Sun Jul 04, 2004 12:15 pm
Location: Germany

Post by sverson »

8) wow - that looks great!

But that's what i ment. We (in this case you) have to write code that should be built in.

And still there are some cases to handle.
If you copy&paste lines the line numbers will be copied as well...

I suppose this thing is just ment to count some (<255) paragraphs...
It's not worth spending so much time on it.

I took a look at the PB-IDE source - there is a second RichEdit used for the line numbers - like you did in your 12/04 example.

:arrow: or just switch to Scintilla used in jaPBe

***edit***

Code: Select all

#Margin_LineNumbers   = 0
#SCI_GETDIRECTPOINTER = 2185
#SCI_SETMARGINWIDTHN  = 2242

If OpenWindow(0, 0, 0, 320, 240, #PB_Window_ScreenCentered | #PB_Window_SystemMenu,"Scintilla") ;/ SciLexer.dll demo
  DLL_SciEdit       = LoadLibrary_("SciLexer.dll")  
  hInstance         = GetModuleHandle_(0) 
  hwndScintilla     = CreateWindowEx_(#WS_EX_CLIENTEDGE, "Scintilla",0,#WS_CHILD|#WS_VISIBLE,5,5,310,230,WindowID(),0,hInstance,0) 
  SciEdi_WndData    = SendMessage_(hwndScintilla,#SCI_GETDIRECTPOINTER,0,0)
  SciEdi_MsgPointer = GetProcAddress_(DLL_SciEdit,"Scintilla_DirectFunction")
  CallFunctionFast(SciEdi_MsgPointer,SciEdi_WndData,#SCI_SETMARGINWIDTHN,#Margin_LineNumbers,30)
  Repeat 
    EventID.l = WaitWindowEvent() 
  Until EventID = #PB_EventCloseWindow 
  DestroyWindow_(hwndScintilla)
  FreeLibrary_(DLL_SciEdit)
EndIf 

End
***end edit***

This little code does it's job :D
Image

:wink: sverson
RJP Computing
Enthusiast
Enthusiast
Posts: 202
Joined: Sun Apr 27, 2003 4:44 am
Location: Michigan, USA
Contact:

Post by RJP Computing »

If you are going to use Scintilla I suggest looking at the great include that GPI has put in the includes of jaPBe. It is all the functions wrapped nicely for you.
-Ryan
RJP Computing

Ubuntu 8.10/WinXP, AMD Athlon 64 3000+, 1000MB RAM, AC 97 Audio, nVidia GeForce 7600GT 512MB
User avatar
fsw
Addict
Addict
Posts: 1603
Joined: Tue Apr 29, 2003 9:18 pm
Location: North by Northwest

Post by fsw »

If you are not interested in multi platform (windows/linux/osx) because you use Windows only, than you should look into 'RaEdit.dll' (comes with RadASM or look into Ketilo's SimED project, there are the sources for it).

RaEdit is coded in pure asm (60Kb instead of 260Kb), is way faster than scintilla, can load MB files without problems and is a RichEdit replacement.
This said: SendMessage(...) commands for RichEdit work for RaEdit too.
It has folding, numbers, highliting, a splitter ( you can see/edit your code on 2 different places), bookmarks etc.

Also to consider: AFAIK scintilla can only be loaded once, if you have multiple files to edit you have to switch the document on the scintilla control. With RaEdit you just load another RaEdit control - no back and forth switching of documents necessary. This way you can do a MDI editor - it works, I've done that (not in pb though).
sverson
Enthusiast
Enthusiast
Posts: 286
Joined: Sun Jul 04, 2004 12:15 pm
Location: Germany

Post by sverson »

RJP Computing wrote:If you are going to use Scintilla I suggest looking at the great include that GPI has put in the includes of jaPBe. It is all the functions wrapped nicely for you.
Thx RJP i allready use this great tool.
In the example above i didn't use the SCI_... commands to show the raw code.

Code: Select all

If OpenWindow(0, 0, 0, 320, 240, #PB_Window_ScreenCentered | #PB_Window_SystemMenu ,"Scintilla") 
  InitSciEdit()
  Editor = SciEditGadget(WindowID(),5,5,310,230) 
  SCI_UseGadget(Editor)
  SCI_SetMarginWidthN(#Margin_LineNumbers,30)
  Repeat 
    EventID.l = WaitWindowEvent() 
  Until EventID = #PB_EventCloseWindow 
  SCI_FreeGadget(Editor)
  ExitSciEdit()
EndIf 

End
This code does the same using scintilla.pbi

fsw wrote:It has folding, numbers, highliting, ...
All features of scintilla!
fsw wrote:AFAIK scintilla can only be loaded once, if you have multiple files to edit you have to switch the document on the scintilla control.
Take a look at jaPBe you can open a lot of files at the same time.

;-) sverson
RJP Computing
Enthusiast
Enthusiast
Posts: 202
Joined: Sun Apr 27, 2003 4:44 am
Location: Michigan, USA
Contact:

Post by RJP Computing »

fsw wrote:If you are not interested in multi platform (windows/linux/osx) because you use Windows only, than you should look into 'RaEdit.dll' (comes with RadASM or look into Ketilo's SimED project, there are the sources for it).

RaEdit is coded in pure asm (60Kb instead of 260Kb), is way faster than scintilla, can load MB files without problems and is a RichEdit replacement.
This said: SendMessage(...) commands for RichEdit work for RaEdit too.
It has folding, numbers, highliting, a splitter ( you can see/edit your code on 2 different places), bookmarks etc.
Can you tell me how to add the control to a window? Also where is the help for this? I really want to try this out, just don't know how to use it. Please Help.

*EDIT*
I found it in the SimED page.
-Ryan
RJP Computing

Ubuntu 8.10/WinXP, AMD Athlon 64 3000+, 1000MB RAM, AC 97 Audio, nVidia GeForce 7600GT 512MB
User avatar
fsw
Addict
Addict
Posts: 1603
Joined: Tue Apr 29, 2003 9:18 pm
Location: North by Northwest

Post by fsw »

sverson wrote:
fsw wrote:It has folding, numbers, highliting, ...
All features of scintilla!
fsw wrote:AFAIK scintilla can only be loaded once, if you have multiple files to edit you have to switch the document on the scintilla control.
Take a look at jaPBe you can open a lot of files at the same time.

;-) sverson
Sure you can, but you have only ONE scintilla control and have to switch the documents back and forth. You can never look at 2 documents at the same time, this said you can't do a MDI Editor out of scintilla.

Example: if I have a lot of files open with the normal PB-Editor and switch back and forth on the TABs (panel) while coding (big files...), it happens sometimes that there is the wrong file visible to the belonging PanelItem.

Don't know about jaPBe though (don't use it, and haven't looked at the sources for years...) but I assume that the bigger the documents, the editor needs more and more time to switch from one document to the other, and it could be that the same hick-up happens as with the PB-Editor.

Anyhow, scintilla is the only multi-platform editor-control I know, so if you do multi-platform than use it.
But for Windows only, I use RaEdit.
Last edited by fsw on Fri Feb 25, 2005 5:01 pm, edited 1 time in total.
Post Reply