Platforms to show: All Mac Windows Linux Cross-Platform
/DynaPDF/StyledText Editor
Function:
Required plugins for this example: MBS DynaPDF Plugin
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /DynaPDF/StyledText Editor
This example is the version from Mon, 16th Jan 2022.
Function:
Required plugins for this example: MBS DynaPDF Plugin
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /DynaPDF/StyledText Editor
This example is the version from Mon, 16th Jan 2022.
Project "StyledText Editor.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
End Class
Class Window1 Inherits Window
Const testRTF = "{\rtf1\ansi\ansicpg1252{\fonttbl{\f0\fnil Helvetica;}{\f1\fnil Arial;}{\f2\fnil Times;}{\f3\fnil Comic Sans MS;}}{\colortbl\red0\green0\blue0;\red217\green11\blue0;\red46\green111\blue253;\red102\green177\blue50;}\uc0 \ql\f0\fs36 Hello World\par\par \cf1 red\cf0 \cf2 blue\cf0 \cf3 green\par \cf0 \par \fs28 14 point\fs24 \par \fs20 10 point\par \fs36 \par \b bold\b0 , \i italic\i0 , \ul underline\ul0 \par \par \f1 Arial \b bold\b0 \i italic\par \i0\f2 Times \b bold\b0 \i italic\par \i0\f3 Comic Sans \b bold\b0 italic\par }"
Control Editor Inherits TextArea
ControlInstance Editor Inherits TextArea
EventHandler Sub SelChange()
NoEvents = true
boldButton.Value = me.SelBold
ItalicButton.Value = me.SelItalic
underlineButton.Value = me.SelUnderline
dim n as string = me.SelTextFont
if n = "" then
PopupFont.ListIndex = -1
else
// schrift suchen und eintrag setzen
dim u as integer = PopupFont.ListCount-1
for i as integer = 0 to u
if PopupFont.List(i) = n then
PopupFont.ListIndex = i
exit
end if
next
end if
ColorPickerButton1.value = me.SelTextColor
Select case me.SelAlignment
case me.AlignRight
RightButton.Value = true
LeftButton.Value = false
CenterButton.Value = false
case me.AlignCenter
RightButton.Value = false
LeftButton.Value = false
CenterButton.Value = true
else
RightButton.Value = false
LeftButton.Value = true
CenterButton.Value = false
end Select
NoEvents = false
RedrawTimer.Mode = timer.ModeSingle
End EventHandler
End Control
Control BoldButton Inherits BevelButton
ControlInstance BoldButton Inherits BevelButton
EventHandler Sub Action()
Editor.SelBold = me.Value
End EventHandler
End Control
Control ItalicButton Inherits BevelButton
ControlInstance ItalicButton Inherits BevelButton
EventHandler Sub Action()
Editor.SelItalic = me.Value
End EventHandler
End Control
Control underlineButton Inherits BevelButton
ControlInstance underlineButton Inherits BevelButton
EventHandler Sub Action()
Editor.SelUnderline = me.Value
End EventHandler
End Control
Control SaveButton Inherits PushButton
ControlInstance SaveButton Inherits PushButton
EventHandler Sub Action()
dim f as FolderItem = GetSaveFolderItem(FileTypes1.Rtf, "test.rtf")
if f = nil then Return
dim b as BinaryStream = BinaryStream.Create(f)
b.Write editor.StyledText.RTFData
End EventHandler
End Control
Control OpenButton Inherits PushButton
ControlInstance OpenButton Inherits PushButton
EventHandler Sub Action()
dim f as FolderItem = GetopenFolderItem(FileTypes1.Rtf)
if f = nil then Return
dim b as BinaryStream = BinaryStream.open(f)
editor.StyledText.RTFData = b.Read(B.Length)
End EventHandler
End Control
Control PopupFont Inherits PopupMenu
ControlInstance PopupFont Inherits PopupMenu
EventHandler Sub Change()
if NoEvents then Return // nicht setzen, wenn wir selber setzen
editor.SelTextFont = me.Text
End EventHandler
EventHandler Sub Open()
dim u as integer = FontCount-1
for i as integer = 0 to u
dim n as string = font(i)
PopupFont.AddRow n
next
End EventHandler
End Control
Control ColorPickerButton1 Inherits ColorPickerButton
ControlInstance ColorPickerButton1 Inherits ColorPickerButton
EventHandler Sub ColorChanged()
editor.SelTextColor = me.value
End EventHandler
End Control
Control LeftButton Inherits BevelButton
ControlInstance LeftButton Inherits BevelButton
EventHandler Sub Action()
Editor.SelAlignment = editor.AlignLeft
RightButton.Value = false
CenterButton.Value = false
End EventHandler
End Control
Control CenterButton Inherits BevelButton
ControlInstance CenterButton Inherits BevelButton
EventHandler Sub Action()
Editor.SelAlignment = editor.AlignCenter
LeftButton.Value = false
RightButton.Value = false
End EventHandler
End Control
Control RightButton Inherits BevelButton
ControlInstance RightButton Inherits BevelButton
EventHandler Sub Action()
Editor.SelAlignment = editor.AlignRight
LeftButton.Value = false
CenterButton.Value = false
End EventHandler
End Control
Control CreateButton Inherits PushButton
ControlInstance CreateButton Inherits PushButton
EventHandler Sub Action()
dim f as FolderItem = GetSaveFolderItem(FileTypes1.pdf, "test.pdf")
if f = nil then Return
dim pdf as new MyDynapdfMBS
pdf.SetLicenseKey "Starter" // For this example you can use a Starter, Lite, Pro or Enterprise License
call pdf.CreateNewPDF(f)
// add page
call pdf.append
call pdf.SetFont("Helvetica")
// start horizontal 110 from left
// start veritical 100 from top
// width is page width minus 130 pixels left and 50 right
// height is page height minus 100 on bottom and 250 on top
call pdf.settextrect 110, pdf.getpageHeight -100, pdf.getpageWidth-110-50, pdf.getpageHeight-100-100
call pdf.writeStyledText(pdf.ktaLeft, window1.Editor.StyledText)
call pdf.EndPage
call pdf.closefile
f.Launch
End EventHandler
End Control
Control Preview Inherits Canvas
ControlInstance Preview Inherits Canvas
EventHandler Sub Paint(g As Graphics, areas() As REALbasic.Rect)
if pic <> nil then
g.DrawPicture pic, 0, 0, g.Width, g.Height, 0, 0, pic.Width, pic.Height
end if
End EventHandler
End Control
Control RedrawTimer Inherits Timer
ControlInstance RedrawTimer Inherits Timer
EventHandler Sub Action()
UpdatePreview
End EventHandler
End Control
EventHandler Sub Open()
editor.StyledText.RTFData = testRTF
UpdatePreview
If XojoVersion >= 2018.03 And IsDarkMode Then
MsgBox "Please be aware that in dark mode the white text may be hard to see on white paper."
End If
End EventHandler
EventHandler Sub Resized()
RedrawTimer.Mode = timer.ModeSingle
End EventHandler
EventHandler Sub Resizing()
RedrawTimer.Mode = timer.ModeSingle
End EventHandler
Sub UpdatePreview()
dim pdf as new MyDynapdfMBS
pdf.SetLicenseKey "Pro" // For this example you can use a Starter, Lite, Pro or Enterprise License
call pdf.CreateNewPDF(nil)
// add page
call pdf.append
call pdf.SetFont("Helvetica")
call pdf.SetPageWidth(preview.Width)
call pdf.SetPageHeight(preview.Height)
call pdf.SetColorSpace(pdf.kcsDeviceRGB)
call pdf.SetFillColor(0) // black
call pdf.settextrect 5, pdf.getpageHeight-5, pdf.getpageWidth-10, pdf.getpageHeight-10
call pdf.writeStyledText(pdf.ktaLeft, window1.Editor.StyledText)
call pdf.EndPage
pic = pdf.RenderPagePicture(1, preview.Width * 2, preview.Height * 2)
preview.Invalidate
End Sub
Property Private NoEvents As Boolean
Property pic As Picture
End Class
MenuBar MainMenuBar
MenuItem FileMenu = "&File"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem EditSeparator1 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "#App.kEditClear"
MenuItem EditSeparator2 = "-"
MenuItem EditSelectAll = "Select &All"
End MenuBar
FileTypes1
Filetype application/rtf
Filetype application/pdf
End FileTypes1
Class ColorPickerButton Inherits BevelButton
ComputedProperty value As color
Sub Set()
mvalue = value
update
End Set
Sub Get()
return mvalue
End Get
End ComputedProperty
Event ColorChanged()
End Event
EventHandler Sub Action()
dim c as color = mvalue
if SelectColor(c, "Farbe?") then
me.value = c // farbe merken und icon generieren
RaiseEvent ColorChanged
end if
End EventHandler
Sub update()
dim p as new Picture(16, 16)
dim g as Graphics = p.Graphics
g.ForeColor = mvalue
g.FillOval 0, 0, 16, 16
me.Icon = p
End Sub
Property Private mvalue As color
End Class
Class MyDynapdfMBS Inherits DynapdfMBS
EventHandler Function Error(ErrorCode as integer, ErrorMessage as string, ErrorType as integer) As integer
// output all messages on the console:
System.DebugLog str(ErrorCode)+": "+ErrorMessage
// and display dialog:
Dim d as New MessageDialog //declare the MessageDialog object
Dim b as MessageDialogButton //for handling the result
d.icon=MessageDialog.GraphicCaution //display warning icon
d.ActionButton.Caption="Continue"
d.CancelButton.Visible=True //show the Cancel button
// a warning or an error?
if BitAnd(ErrorType, me.kE_WARNING) = me.kE_WARNING then
// if user decided to ignore, we'll ignore
if IgnoreWarnings then Return 0
d.Message="A warning occurred while processing your PDF code."
// we add a third button to display all warnings
d.AlternateActionButton.Caption = "Ignore warnings"
d.AlternateActionButton.Visible = true
else
d.Message="An error occurred while processing your PDF code."
end if
d.Explanation = str(ErrorCode)+": "+ErrorMessage
b=d.ShowModal //display the dialog
Select Case b //determine which button was pressed.
Case d.ActionButton
Return 0 // ignore
Case d.AlternateActionButton
IgnoreWarnings = true
Return 0 // ignore
Case d.CancelButton
Return -1 // stop
End select
End EventHandler
EventHandler Function PageBreak(LastPosX as double, LastPosY as double, PageBreak as boolean) As integer
return 1 // cut
End EventHandler
Property IgnoreWarnings As Boolean
Property template As integer
End Class
End Project
The items on this page are in the following plugins: MBS DynaPDF Plugin.
