I have a solution with API.
Code: Select all
;-TOP
; *****************************************************************************
; AddTextRTF by mk-soft, v1.03, 25.03.2018
CompilerIf #PB_Compiler_Version < 550
Procedure UTF8(Text.s)
Protected *mem = AllocateMemory(StringByteLength(Text, #PB_UTF8) + 1)
If *mem
PokeS(*mem, Text, -1, #PB_UTF8)
EndIf
ProcedureReturn *mem
EndProcedure
CompilerEndIf
Procedure AddTextRTF(Gadget, Text.s , NewLine=#True)
If Left(Text, 5) <> "{\rtf "
Text = "{\rtf " + Text + "}"
EndIf
CompilerIf #PB_Compiler_Unicode
Protected hEdit = GadgetID(Gadget)
Protected ndx = GetWindowTextLength_(hEdit)
Protected *szBuffer = UTF8(Text)
Protected hFocus = GetFocus_()
If ndx And NewLine
AddGadgetItem(Gadget, -1, "")
EndIf
SetFocus_(hEdit);
SendMessage_(hEdit, #EM_SETSEL, ndx, ndx)
SendMessage_(hEdit, #EM_REPLACESEL, 0, *szBuffer)
SetFocus_(hFocus);
FreeMemory(*szBuffer)
CompilerElse
AddGadgetItem(Gadget, -1 , Text)
CompilerEndIf
EndProcedure
; *****************************************************************************
;// Defining Structures to hold Font and Color data
;// that will be used to build the reference Tables
;// and RTF Header we will be using.
;//
Structure Color_RGB ;//Hold RGB Color values in ColorTable.
r.i
g.i
b.i
EndStructure
Structure ColorTable ;// Main Structure for List of Colors.
name.s
color.Color_RGB
colorID.i
EndStructure
Structure FontTable ;// Main Structure for List of Fonts.
name.s
fontID.i
EndStructure
;// Creating Linked Lists to hold Color and Font data so we can
;// change the look of text on the fly, during Run Time.
NewList Color_Table.ColorTable()
NewList Font_Table.FontTable()
;// Variables to hold generated ID numbers for use in an RTF
;// Header that will be generated later on.
;//
Global ColorIndex.i
Global FontIndex.i
Global rtf_header.s
;// This Procedure assembles the Color Table from input passed to
;// it during Runtime. Each entry in the Linked List has a color
;// name attached to it, as well as the HEX (*BGR) value of that
;// color, and a generated ID number which corresponds to it's
;// ID within the RTF header when it is generated later.
;//
Procedure Build_Color_Table(name.s, r.i, g.i, b.i)
Shared Color_Table()
ForEach Color_Table()
If Color_Table()\name = name
ProcedureReturn #False
EndIf
Next
AddElement(Color_Table())
Color_Table()\name = name
Color_Table()\color\r = r
Color_Table()\color\g = g
Color_Table()\color\b = b
Color_Table()\colorID = 0
ProcedureReturn #True
EndProcedure
;// This Procedure will parse all text sent to the text box.
;// Its job is to parse each line for Color and Font tags
;// which will determine what the text looks like. It will
;// build the proper RTF header and convert the formating
;// tags into RTF Formating Syntax.
;//
Procedure.s Format(input.s)
Protected header.s, footer.s, ColorIndex.i
Shared Color_Table()
;// Build our initial RTF Header
SelectElement(Color_Table(), 0)
header = "{\rtf {\colortbl;" + "\red" +Str(Color_Table()\color\r)+"\green"+Str(Color_Table()\color\g)+"\blue"+Str(Color_Table()\color\b)+";"
;// Copy current input string to a temporary working variable.
;// Make sure the index is reset to 1
ColorIndex = 1
;// For some reason the default text color will not apply to the next line, if we post a line previously, whose only text is contained within color tags.
;// so we add a call for the default text color to each new line.
input = "\cf1" + input
;// Repeat this task for every color in the table. Here we are
;// searching the input string for any occurence of a color tag
;// and matching it to the name of the color, within the Table.
;// Then we assign an Index ID which will also serve as its RTF
;// \cf ID. If it works properly they should all match up
;// to the proper colors. This will be used to include color
;// formats in the header ONLY for colors present in the input
;// string; saves time and memory.
ResetList(Color_Table())
ForEach Color_Table()
If FindString(input, "<color=" +Color_Table()\name +">",1) <> 0
;// Increment and assign an index number.
ColorIndex +1
Color_Table()\colorID = ColorIndex
;// Add color to the Color Table.
header = header + "\red"+Str(Color_Table()\color\r)+"\green"+Str(Color_Table()\color\g)+"\blue"+Str(Color_Table()\color\b)+";"
EndIf
Next
;// Now we repeat the same task above, with a slightly different
;// command. We will replace the color/font/styling tags with
;// the proper RTF Syntax commands.
ResetList(Color_Table())
ForEach Color_Table()
input = ReplaceString(input, "<color=" + Color_Table()\name + ">", "\cf" +Str(Color_Table()\colorID))
Next
;// Look for closing tags to restore the default color, font, and styling.
input = ReplaceString(input, "</color>", "\cf1 ")
header = header + "}"
footer = "}"
ProcedureReturn header + input + footer
EndProcedure
;// Create the Color Table. Colors are stored as RGB
;// and will be parsed by name.
;//
Build_Color_Table("DefaultText", 119, 136, 153)
Build_Color_Table("RoomTitle", 255, 255, 255)
Build_Color_Table("RoomList", 255, 0, 0)
Build_Color_Table("RoomNPC", 43, 225, 0)
Build_Color_Table("RoomExits", 0, 116, 107)
Build_Color_Table("blue", 54, 0, 237)
Build_Color_Table("brightgreen", 0, 238, 0)
Build_Color_Table("brown", 153, 102, 0)
Build_Color_Table("darkred", 203, 32, 38)
Build_Color_Table("forestgreen", 34, 139, 34)
Build_Color_Table("gold", 254, 204, 0)
Build_Color_Table("green", 0, 204, 0)
Build_Color_Table("grey", 204, 204, 204)
Build_Color_Table("lightblue", 24, 116, 205)
Build_Color_Table("orange", 255, 140, 0)
Build_Color_Table("red", 255, 0, 0)
Build_Color_Table("slateblue", 0, 127, 255)
Build_Color_Table("teal", 0, 128, 128)
Build_Color_Table("white", 255, 255, 255)
Build_Color_Table("yellow", 255, 255, 0)
;///////////////////////////////
;///////////////////////////////
;Test Code Below
;///////////////////////////////
;///////////////////////////////
OpenWindow(0, 0, 0, 600, 400, "", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
EditorGadget(0, 0, 0, 600, 400);, #PB_Editor_ReadOnly)
SetGadgetColor(0, #PB_Gadget_BackColor, $000000)
SendMessage_(GadgetID(0), #EM_SETTARGETDEVICE, #Null, 0)
; AddGadgetItem(0, -1, Format("<color=RoomTitle>[Kirkhill, Town Center]</color>"))
; AddGadgetItem(0, -1, Format("You are standing in the center of Kirkhill. A large Japanese Maple stands before you ominously, as if beckoning for you to climb it. Beneath the tree, children are playing a game of tag, filling the air with their lighthearted laughter. In the distance you can see the peak of Tarn Kolag which marks the territorial boundary between The Darkan Lordship and The Wastelands."))
; AddGadgetItem(0, -1, Format("<color=RoomList>You also see:</color> A modest sized bronze statue of Eric and Neco Darkan."))
; AddGadgetItem(0, -1, Format("<color=RoomNPC>Also here:</color> A wandering Street Merchant, Ovelia the Blacksmith, a pet dog."))
; AddGadgetItem(0, -1, Format("<color=RoomExits>Obvious Exits:</color> Northeast, North, East, South, Southwest, West."))
AddTextRTF(0, Format("<color=RoomTitle>[Kirkhill, Town Center]</color>"))
AddTextRTF(0, Format("You are standing in the center of Kirkhill. A large Japanese Maple stands before you ominously, as if beckoning for you to climb it. Beneath the tree, children are playing a game of tag, filling the air with their lighthearted laughter. In the distance you can see the peak of Tarn Kolag which marks the territorial boundary between The Darkan Lordship and The Wastelands."))
AddTextRTF(0, Format("<color=RoomList>You also see:</color> A modest sized bronze statue of Eric and Neco Darkan."))
AddTextRTF(0, Format("<color=RoomNPC>Also here:</color> A wandering Street Merchant, Ovelia the Blacksmith, a pet dog."))
AddTextRTF(0, Format("<color=RoomExits>Obvious Exits:</color> Northeast, North, East, South, Southwest, West."))
AddTextRTF(0, "Hello World")
Repeat
event = WaitWindowEvent(20)
Until event = #PB_Event_CloseWindow
I think Purebasic internally converts the text to RTF and thus your control codes get lost