addobject "MGCERichEdit.RichEdit", "redit", 2, 22, output.width - 4, output.height - 24
addobject "dialog", "cdlg"
addobject "CommandButton", "Bold", 2, 2, 20, 20
addobject "CommandButton", "Italic", 22, 2, 20, 20
addobject "CommandButton", "Uline", 42, 2, 20, 20
addobject "CommandButton", "Sout", 62, 2, 20, 20
addobject "CommandButton", "Save", 82, 2, 36, 20
addobject "CommandButton", "Load", 118, 2, 36, 20
addobject "CommandButton", "Find", 154, 2, 36, 20
addobject "Textbox", "Ftext", 190, 2, 80, 20

Bold.Caption = "&B"
Italic.Caption = "&I"
Uline.Caption = "&U"
Sout.Caption = "&S"
Save.Caption = "Sa&ve"
Load.Caption = "&Load"
Find.Caption = "&Find"

Bold.BackColor = output.BackColor
Italic.BackColor = output.BackColor
Uline.BackColor = output.BackColor
Sout.BackColor = output.BackColor
Save.BackColor = output.BackColor
Load.BackColor = output.BackColor
Find.BackColor = output.BackColor
Ftext.BorderStyle = 1

redit.Multiline = TRUE
redit.SelectionDisplay = TRUE
redit.Border = TRUE
redit.BackColor = 65535
redit.ForeColor = 0
redit.FontName = "Courier New"
redit.FontSize = 240
redit.Scrollbars = 2
redit.SetFocus
redit.Caption = "This is the Rich Edit control"
redit.SetStart = 12
redit.SelLength = 9
redit.SelectionFontBold = TRUE
redit.SetStart = 0
redit.SelLength = 0


sub Find_Click
   if Len( Trim( Ftext.Caption ) ) = 0 then exit sub
   i = redit.FindText( , , , Ftext.Caption )
   if i <> -1 then
      redit.SetStart = i
      redit.SelLength = Len( Ftext.Caption )
   end if
end sub

sub Bold_Click
   if redit.SelectionFontBold = FALSE then
      redit.SelectionFontBold = TRUE
      Bold.FontWeight = 700
      Bold.ForeColor = 255
   else
      redit.SelectionFontBold = FALSE
      Bold.FontWeight = 400
      Bold.ForeColor = 0
   end if
   redit.SetFocus
end sub

sub Italic_Click
   if redit.SelectionFontItalic = FALSE then
      redit.SelectionFontItalic = TRUE
      Italic.FontWeight = 700
      Italic.ForeColor = 255
   else
      redit.SelectionFontItalic = FALSE
      Italic.FontWeight = 400
      Italic.ForeColor = 0
   end if
   redit.SetFocus
end sub

sub Uline_Click
   if redit.SelectionFontUnderline = FALSE then
      redit.SelectionFontUnderline = TRUE
      Uline.FontWeight = 700
      Uline.ForeColor = 255
   else
      redit.SelectionFontUnderline = FALSE
      Uline.FontWeight = 400
      Uline.ForeColor = 0
   end if
   redit.SetFocus
end sub

sub Sout_Click
   if redit.SelectionFontStrikeout = FALSE then
      redit.SelectionFontStrikeout = TRUE
      Sout.FontWeight = 700
      Sout.ForeColor = 255
   else
      redit.SelectionFontStrikeout = FALSE
      Sout.FontWeight = 400
      Sout.ForeColor = 0
   end if
   redit.SetFocus
end sub

sub Save_Click
   cdlg.DialogTitle = "Save RTF File"
   cdlg.InitDir = "\"
   cdlg.Flags = 0
   cdlg.Filter = "RTF Files|*.rtf|All Files|*.*"
   cdlg.DefaultExt = "rtf"
   cdlg.Filename = "*.rtf"
   cdlg.ShowSave
   if InStr( 1, cdlg.Filename, "*" ) = 0 then
      on error resume next
      i = redit.Save( cdlg.Filename )
      if Err.number then
         MsgBox "An error occurred saving " & cdlg.Filename
      else
         MsgBox CStr( i ) & " bytes saved to " & cdlg.Filename
      end if
   end if
   redit.SetFocus
end sub

sub Load_Click
   cdlg.DialogTitle = "Load RTF File"
   cdlg.InitDir = "\"
   cdlg.Flags = 0
   cdlg.Filter = "RTF Files|*.rtf|All Files|*.*"
   cdlg.DefaultExt = "rtf"
   cdlg.Filename = "*.rtf"
   cdlg.ShowOpen
   if InStr( 1, cdlg.Filename, "*" ) = 0 then
      on error resume next
      i = redit.Load( cdlg.Filename )
      if Err.number then
         MsgBox "An error occurred loading " & cdlg.Filename
      else
         MsgBox CStr( i ) & " bytes loaded from " & cdlg.Filename
      end if
   end if 
   redit.SetFocus
end sub

sub redit_SelectionChange( pos, length, f )
   if f AND 1 then
      Bold.FontWeight = 700
      Bold.ForeColor = 255
   else
      Bold.FontWeight = 400
      Bold.ForeColor = 0
   end if
   if f AND 2 then
      Italic.FontWeight = 700
      Italic.ForeColor = 255
   else
      Italic.FontWeight = 400
      Italic.ForeColor = 0
   end if
   if f AND 4 then
      Uline.FontWeight = 700
      Uline.ForeColor = 255
   else
      Uline.FontWeight = 400
      Uline.ForeColor = 0
   end if
   if f AND 8 then
      Sout.FontWeight = 700
      Sout.ForeColor = 255
   else
      Sout.FontWeight = 400
      Sout.ForeColor = 0
   end if
end sub
