No idea, to be honest. I'm not opening files, I'm just printing direct to the document (treating it like an EditorGadget) with append commands, whatever Scintilla's default may be.
I set initial text with SETTEXT, then use APPENDTEXT after that (seems to require a SET to work, but I have an idea on how to get around that, I will try out later)
If I completely remove any kind of newline inserts from the code it seems to function properly, with the exception of search/replace, which still requires a +1 offset, I suspect in that case because its locating the gap in front of the match instead (docs say it will match either the word or the gap in front of it, something to do with multi-byte characters idk... )
gotta get some sleep before work, but here is a scratchpad of test code I've slowly built up. There are a lot of potential red herrings as I have unfinished ideas in the code, empty procedures, commented out stuff, etc. Apologies, haven't coded in ~5 years or so. But this should compile and highlight the problem at the very least.
Code: Select all
;// Enumerate some Constants for object handles
Enumeration Window
#MainWindow
EndEnumeration
;// Scintilla specific
Enumeration
#SCINT_VISIBLE
#SCINT_SCRATCHPAD
EndEnumeration
;// Structures
Structure ColorTable
name.s
style.i
EndStructure
;// Make a list to hold our map for translating text styling markup
;// to style indexes
NewList Color_Table.ColorTable()
Procedure BuildColorTable(name.s, style.i)
Shared Color_Table() ;// give access to the translation table
ForEach Color_Table() ;// iterate through the list
If Color_Table()\name = name.s
ProcedureReturn #False ;// If the entry already exists, break out of the loop and end procedure
EndIf
Next
AddElement(Color_Table()) ;// Add a new entry to the color table
Color_Table()\name = name.s ;// assign color name (red, blue, whatever)
Color_Table()\style = style.i ;// assign the style index number used for Scintilla's styles
ProcedureReturn #True ;// after iterating entire list without error, end procedure.
EndProcedure
;// Define some useful shit like a procedure
;// to automatically feed a string to a Scintilla
;// gadget without having to type all this crap
Procedure SciPrint(Text.s) ;// Prints text to a new line when added
;// originally this was simply Text.s = Chr(10) + Text.s
CRLF.s = Chr(10)
*NewLine = UTF8(CRLF.s)
ScintillaSendMessage(0, #SCI_APPENDTEXT, Len(CRLF.s), *NewLine)
Text.s = Text.s
*Text=UTF8(Text.s)
ScintillaSendMessage(0, #SCI_APPENDTEXT, Len(Text.s), *Text)
FreeMemory(*Text)
ScintillaSendMessage(0, #SCI_DOCUMENTEND)
EndProcedure
Procedure SciPrintStyled(Text.s)
EndProcedure
If OpenWindow(0, 0, 0, 640, 480, "ScintillaGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
If InitScintilla()
ContainerGadget(10,10,10,310, 70,#PB_Container_Flat)
ScintillaGadget(#SCINT_VISIBLE, -2, -2, 310, 70, 0)
;// Make a second Scintilla Gadget
ScintillaGadget(#SCINT_SCRATCHPAD, 0, 0, 100, 50, 0)
;// Hide the scratchpad
HideGadget(#SCINT_SCRATCHPAD, #True)
CloseGadgetList()
;//==========================================
;// Set all Styles to a default state
ScintillaSendMessage(#SCINT_VISIBLE, #SCI_STYLESETBACK, 32, RGB(0,0,0))
ScintillaSendMessage(#SCINT_VISIBLE, #SCI_STYLECLEARALL)
;// Output set to red color
ScintillaSendMessage(#SCINT_VISIBLE, #SCI_STYLESETFORE, 0, RGB(255, 0, 0))
ScintillaSendMessage(#SCINT_VISIBLE, #SCI_STYLESETFORE, 1, RGB(255, 0, 255))
ScintillaSendMessage(#SCINT_VISIBLE, #SCI_STYLESETFORE, 2, RGB(255, 255, 255))
;// Build Color Table
BuildColorTable("red", 0)
BuildColorTable("hotpink", 1)
BuildColorTable("white", 2)
;//==========================================
;// Set some styles up for the scratchpad
ScintillaSendMessage(#SCINT_SCRATCHPAD, #SCI_STYLESETFORE, 0, RGB(137, 137, 200))
;// Turn on Word Wrap.
ScintillaSendMessage(#SCINT_VISIBLE, #SCI_SETWRAPMODE, #SC_WRAP_WORD)
;// Set Margin to 0px
ScintillaSendMessage(#SCINT_VISIBLE, #SCI_SETMARGINWIDTHN, 1, 0)
;// Set Caret to Invisible
ScintillaSendMessage(#SCINT_VISIBLE, #SCI_SETCARETWIDTH, 1)
ScintillaSendMessage(#SCINT_VISIBLE, #SCI_SETCARETFORE, RGB(0, 255, 0))
; Set the initial text to the ScintillaGadget
*Text=UTF8("This is a simple ScintillaGadget with text...")
ScintillaSendMessage(#SCINT_VISIBLE, #SCI_SETTEXT, 0, *Text)
FreeMemory(*Text) ; The buffer made by UTF8() has to be freed, to avoid memory leak
; Adding a second line of text with linebreak before
Text$ = Chr(10) + Chr(10) + "Second line"
*Text=UTF8(Text$)
ScintillaSendMessage(#SCINT_VISIBLE, #SCI_APPENDTEXT, Len(Text$), *Text)
FreeMemory(*Text)
;//Test Scrolling
SciPrint("Hey This is a test")
SciPrint("And some more stuff added to the screen")
SciPrint("")
SciPrint("The line before this was a blank line. I inserted a character return")
SciPrint("But this line should print directly below the line above it with no gap at all")
SciPos = ScintillaSendMessage(#SCINT_VISIBLE, #SCI_GETCURRENTPOS, 0, 0)
SciPrint(Str(SciPos))
SciPos2 = ScintillaSendMessage(#SCINT_VISIBLE, #SCI_GETCURRENTPOS, 0, 0) ;// + 1
SciPrint(Str(SciPos2))
SciPos3 = ScintillaSendMessage(#SCINT_VISIBLE, #SCI_GETCURRENTPOS, 0, 0) ;// + 1
SciPrint(Str(SciPos3))
ScintillaSendMessage(#SCINT_VISIBLE, #SCI_STARTSTYLING, SciPos2, 0)
ScintillaSendMessage(#SCINT_VISIBLE, #SCI_SETSTYLING, Len(Str(SciPos2)), 1)
ScintillaSendMessage(#SCINT_VISIBLE, #SCI_STARTSTYLING, SciPos3, 0)
ScintillaSendMessage(#SCINT_VISIBLE, #SCI_SETSTYLING, Len(Str(SciPos3)), 2)
DbgOut$ = "*SciPos2 is " + Len(Str(SciPos2)) + " characters long."
Debug DbgOut$
;// Here we are going to test sending text to the scratchpad, then reading
;// it back as styled, using the default style settings. Then we will try
;// and copy and paste it over to the visible control
*Text = UTF8("This line should appear styled when it is copied over to the visible Scintilla control.")
ScintillaSendMessage(#SCINT_SCRATCHPAD, #SCI_APPENDTEXT, Len(Str(*Text)), *Text)
FreeMemory(*Text)
LineCount.i = ScintillaSendMessage(#SCINT_VISIBLE, #SCI_GETLINECOUNT)
Debug "Raw Line Count: " + Str(LineCount)
WrapCount.i = ScintillaSendMessage(#SCINT_VISIBLE, #SCI_WRAPCOUNT, 7)
Debug "Wrapped Line Count: " + Str(WrapCount)
;ScintillaSendMessage(#SCINT_VISIBLE, #SCI_SETCURRENTPOS, 0, 0)
;ScintillaSendMessage(#SCINT_VISIBLE, #SCI_SETANCHOR, 0, 0)
ScintillaSendMessage(#SCINT_VISIBLE, #SCI_DOCUMENTEND)
SciPrint("<color=white>Adding a new line")
;// Set search start to entire document
ScintillaSendMessage(#SCINT_VISIBLE, #SCI_SETTARGETSTART, 0, 0)
;// Set the search end to the end of the document
;ScintillaSendMessage(#SCINT_VISIBLE, #SCI_DOCUMENTEND)
SelEndPos = ScintillaSendMessage(#SCINT_VISIBLE, #SCI_GETCURRENTPOS)
ScintillaSendMessage(#SCINT_VISIBLE, #SCI_SETTARGETEND, SelEndPos)
Debug "Document Start Position: " + Str( ScintillaSendMessage(#SCINT_VISIBLE, #SCI_GETTARGETSTART) )
Debug "Document End Position: " + Str( ScintillaSendMessage(#SCINT_VISIBLE, #SCI_GETTARGETEND) )
;// It took me forever to figure this out, some of this API is retarded
;// the following code will establish a search term, and then search
;// inside the Scintilla control for instances of that term.
;// When the instance is found, the position within the document is
;// returned. Length must be set by TARGET search term.
TextSearch$ = "<color=" + Color_Table()\name
*txtSearch = UTF8(TextSearch$) ;// Test String to search for
MatchLocation = ScintillaSendMessage(#SCINT_VISIBLE, #SCI_SEARCHINTARGET, Len(TextSearch$), *txtSearch)
MatchEnd = MatchLocation + Len(TextSearch$)
Debug ~"Search Term \" " + TextSearch$ + ~" \"" + ", located at: " + MatchLocation
ScintillaSendMessage(#SCINT_VISIBLE, #SCI_SETTARGETSTART, MatchLocation)
ScintillaSendMessage(#SCINT_VISIBLE, #SCI_SETTARGETEND, MatchEnd)
EmptyStr$ = ""
*EmptyStr = UTF8(EmptyStr$)
;// Unless you add a +1 to SCI_TARGETSTART or SCI_TARGETEND
;// a > symbol is still left from the color tag.
ScintillaSendMessage(#SCINT_VISIBLE, #SCI_REPLACETARGET, -1, *EmptyStr)
Debug "Start Location: " + MatchLocation
Debug "End Location: " + MatchEnd
FreeMemory(*txtSearch) ;// Free the memory allocated to the pointer
FreeMemory(*EmptyStr)
EndIf
Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf