Set RTF content to EditorGadget with Unicode Option ON

Share your advanced PureBasic knowledge/code with the community.
zapman*
Enthusiast
Enthusiast
Posts: 115
Joined: Wed Jun 02, 2004 10:17 pm
Location: New Caledonia (South Pacific)
Contact:

Set RTF content to EditorGadget with Unicode Option ON

Post 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.
Don't try - DO it !
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Nice tip.

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

Thanks.
I may look like a mule, but I'm not a complete ass.
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

Thanks!! 8)
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
Post Reply