Re: Editorgadget line numbers possible??
Posted: Tue Aug 15, 2006 8:59 pm
utopiomania wrote:Hi! Is there a simple way to add line numbers to the editorgadget? I'm working on a
simple html editor, and Javascript errors usually report a line number on errors, so..
Possible? Yes. Simple way? No.Editorgadget line numbers possible??
Here's an example I found for PB 3.94:
Code: Select all
#RICHEDIT = #WS_CHILD|#WS_VISIBLE|#WS_VSCROLL|#ES_MULTILINE|#ES_AUTOVSCROLL|#ES_NOHIDESEL
#WINDOW_PARAMETERS = #PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_SizeGadget|#PB_Window_ScreenCentered|#PB_Window_MaximizeGadget
Global RichEditID, hWindow, WindowWidth, WindowHeight
Global hLnFont, rc.RECT, FontHeight, buffer.s, OldRedProc
Procedure Error(message.s, fatal.b)
MemoryID = AllocateMemory(256)
FormatMessage_(#FORMAT_MESSAGE_FROM_SYSTEM, 0, GetLastError_(), 0, MenoryID, 256, 0)
MessageRequester("Error", message+Chr(10)+Chr(10)+PeekS(MemoryID), 0)
FreeMemory(0)
If fatal
End
EndIf
EndProcedure
Procedure DrawLineNumbers()
hDC = GetDC_(RichEditID)
OldObject = SelectObject_(hDC, hLnFont)
lineno = SendMessage_(RichEditID, #EM_GETFIRSTVISIBLELINE, 0, 0)+1
GetClientRect_(RichEditID, rc)
rc\right = 28
FillRect_(hDC, rc, GetStockObject_(#GRAY_BRUSH))
SetBkMode_(hDC, #TRANSPARENT)
SetTextColor_(hDC, $0FFFF)
lastline = WindowHeight/FontHeight+lineno
While lineno<lastline
buffer = Str(lineno)
DrawText_(hDC, @buffer, -1, rc, #DT_RIGHT)
rc\top+FontHeight
lineno+1
Wend
MoveToEx_(hDC, rc\right, 0, #Null)
LineTo_(hDC, rc\right, rc\bottom)
SelectObject_(hDC, OldObject)
ReleaseDC_(RichEditID, hDC)
EndProcedure
Procedure RedProc(hWnd, uMsg, wParam, lParam)
Select uMsg
Case #WM_PAINT
CallWindowProc_(OldRedProc, hWnd, uMsg, wParam, lParam)
DrawLineNumbers()
result = 0
Default
result = CallWindowProc_(OldRedProc, hWnd, uMsg, wParam, lParam)
EndSelect
ProcedureReturn result
EndProcedure
Procedure WndProc(hWnd, uMsg, wParam, lParam)
result = #PB_ProcessPureBasicEvents
Select uMsg
Case #WM_SIZE
If hWnd=hWindow And wParam<>#SIZE_MINIMIZED
WindowWidth = lParam&$ffff
WindowHeight = lParam>>16
MoveWindow_(RichEditID, 0, 0, WindowWidth, WindowHeight, 1)
EndIf
EndSelect
ProcedureReturn result
EndProcedure
hWindow = OpenWindow(0, 0, 0, 640, 480, #WINDOW_PARAMETERS, "RichEdit line numbers example")
If hWindow
SetClassLong_(hWindow, #GCL_HBRBACKGROUND, 0) ; remove resize flicker
If LoadLibrary_("RICHED20.DLL")
RichClass.s = "RichEdit20A"
ElseIf LoadLibrary_("RICHED32.DLL")
RichClass.s = "RichEdit"
Else
Error("RichEdit library not present.", 0)
End
EndIf
WindowWidth = WindowWidth()
WindowHeight = WindowHeight()
RichEditID = CreateWindowEx_(#WS_EX_CLIENTEDGE, RichClass, 0, #RICHEDIT, 0, 0, WindowWidth, WindowHeight, hWindow, 0, GetModuleHandle_(0), 0)
If RichEditID
SendMessage_(RichEditID, #EM_GETRECT, 0, rc)
rc\left+32
SendMessage_(RichEditID, #EM_SETRECT, 0, rc)
lfnt.LOGFONT
cf.CHARFORMAT
FontName.s = "Courier New"
lstrcpy_(@lfnt\lfFaceName[0], @FontName)
lstrcpy_(@cf\szFaceName[0], @FontName)
lfnt\lfHeight = -12
lfnt\lfWeight = 400
hLnFont = CreateFontIndirect_(lfnt)
hFont = CreateFontIndirect_(lfnt)
cf\cbSize = SizeOf(CHARFORMAT)
cf\dwMask = #CFM_FACE|#CFM_SIZE
cf\yHeight=(-lfnt\lfHeight)*15
SendMessage_(RichEditID, #EM_SETCHARFORMAT, #SCF_SELECTION, cf)
hDC = GetDC_(RichEditID)
OldObject = SelectObject_(hDC, hFont)
GetTextExtentPoint32_(hDC, @"WWWW", 4, siz.SIZE)
FontHeight = siz\cy
SelectObject_(hDC, OldObject)
ReleaseDC_(RichEditID, hDC)
OldRedProc = SetWindowLong_(RichEditID, #GWL_WNDPROC, @RedProc())
SetWindowCallback(@WndProc())
Repeat
EventID = WaitWindowEvent()
Until EventID = #PB_EventCloseWindow
Else
Error("Could not create the RichEdit control.", 0)
End
EndIf
Else
Error("Could not create the main window.", 0)
EndIf
End