Page 1 of 1

Set RTF content to EditorGadget with Unicode Option ON

Posted: Mon Oct 08, 2007 11:17 pm
by zapman*
When you want to fill an EditorGadget with an RTF text, it is very usefull to use the SetGadgetText instruction.

The following code

Code: Select all

If OpenWindow(0, 100, 200, 195, 260, "PureBasic Window",#PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_MaximizeGadget)=0:End:EndIf 
If CreateGadgetList(WindowID(0))=0:End:EndIf 
EditorGadget(0, 0, 0, WindowWidth(0), WindowHeight(0)) 
RichTextBox1.s = "{\rtf1\ansi\ansicpg1252\deff0\deftab720{\fonttbl{\f0\fswiss MS Sans Serif;}{\f1\froman\fcharset2 Symbol;}{\f2\fswiss Arial;}{\f3\froman\fprq2\fcharset2 Symbol;}{\f4\fswiss Arial;}{\f5\fswiss\fprq2 Arial;}{\f6\fswiss\fprq2 MS Sans Serif;}{\f7\froman\fprq2 Times New Roman;}{\f8\fswiss MS Sans Serif;}{\f9\fswiss\fprq2 System;}}{\colortbl\red0\green0\blue0;}\deflang1031\pard\plain\f5\fs18 m\plain\f3\fs20 W\plain\f2\fs17\par }" 
SetGadgetText(0, RichTextBox1) 
Repeat:Until WaitWindowEvent()=#PB_Event_CloseWindow 
Will work well with PB 4.0 if you've set the source file encodage to plain text.

But it won't if you've set it to "Unicode"

Here is a procedure which will fix the problem:

Code: Select all

Procedure SetGadgetTextRE(gadget, Text$) 
  *buffer = AllocateMemory(Len(Text$)+1)
  PokeS(*buffer,Text$,#PB_Any,#PB_Ascii)
  txt.SETTEXTEX 
  txt\flags = 0 
  txt\codepage = #CP_ACP 
  SendMessage_(GadgetID(gadget), #EM_SETTEXTEX, @txt, *buffer) 
  FreeMemory(*buffer)
EndProcedure 
Just replace into the first code
SetGadgetText(0, RichTextBox1)
by
SetGadgetTextRE(0, RichTextBox1)
and all is OK.

Posted: Mon Oct 08, 2007 11:20 pm
by srod
Nice tip.

I'd overlooked that problem when setting rtf content with this method. 8)

Thanks.

Posted: Tue Oct 09, 2007 1:10 am
by Psychophanta
Thanks!! 8)