Page 1 of 2

Assigning RTF (styled) text to EditorGadget directly

Posted: Thu Oct 28, 2004 11:07 am
by mescalito
I am looking for a way to assign directly RTF (styled) Text to EditorGadget. In El_Choni's RichEdit lib there is a function StreamFileIn(FileName$, flags.l), which enables to assign a rtf file content to a EditorGadget. But i'm looking for a direct way.

For example, in VB 6.0 i would do this:

RichTextBox1.TextRTF = "{\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 }"

After starting the VB program in the RichTextBox1 (same like EditorGadget) would appear 'mOhm', but 'Ohm' like greek sign for electric resistance.

Any idea?

Thanks,

mescalito

Posted: Thu Oct 28, 2004 11:26 am
by El_Choni

Code: Select all

If OpenWindow(0, 100, 200, 195, 260, #PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_MaximizeGadget, "PureBasic Window")=0:End:EndIf
If CreateGadgetList(WindowID())=0:End:EndIf
EditorGadget(0, 0, 0, WindowWidth(), WindowHeight())
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
Regards,

Posted: Thu Oct 28, 2004 11:36 am
by mescalito
I didn't suppose it would be so easy!

Tanks!

Posted: Fri Feb 25, 2005 9:22 pm
by Flype
hello,

how to ouput the RTF content of the EditorGadget directly to the default installed printer
and with the minimum user-click as possible ?

something that look like the DOS command "wordpad.exe /p mydocument.RTF" but directly from the EditorGadget... :?:

Posted: Fri Feb 25, 2005 9:51 pm
by Flype
sorry i found how to do this here viewtopic.php?t=6679&highlight=rtf+printer

Posted: Fri Feb 25, 2005 10:15 pm
by Flype
so i found this really cool proc

Code: Select all

;############################################ 
;Content 
;EditorGadget print on a standard printer 
; 
;Author : Andreas 
;June 2003 
;extended by Wichtel and Joro 
; 
;all formatting in the editorgadget will be 
;printed 
;############################################ 
Global GadgetNr,GadgetNr_hWnd 
#Margin_left=80 
#Margin_right=81 
#Margin_top=82 
#Margin_bottom=83 

Structure textlength 
  flags.w 
  codepage.b 
EndStructure 
;####################################################################### 
;the 2 Procedures are based on El_Choni's example 
Procedure StreamFileIn_Callback(hFile, pbBuff, cb, pcb) 
ProcedureReturn ReadFile_(hFile, pbBuff, cb, pcb, 0)!1 
EndProcedure 

Procedure FileStreamIn(FileID.l, File.s, Gadget.l,Option.l) 
Protected File 
Protected FileID 
Protected Gadget 
Protected StreamData.EDITSTREAM 
If ReadFile(FileID, File) 
StreamData\dwCookie = UseFile(FileID) 
StreamData\dwError = #Null 
StreamData\pfnCallback = @StreamFileIn_Callback() 
SendMessage_(GadgetNr_hWnd, #EM_STREAMIN, Option, @StreamData) 
CloseFile(FileID) 
EndIf 
EndProcedure 
;####################################################################### 

;structure to set the documentproperties 
Structure DInfo1 
cbSize.l 
lpszDocname.l 
lpszOutput.l 
lpszDataType.l 
fwType.w 
EndStructure 


Procedure PrintWindow() 
WindowWidth = 200 
WindowHeight = 40 
If OpenWindow(10, 0, 0, WindowWidth, WindowHeight, #PB_Window_WindowCentered, "Printing") 
SetWindowPos_(WindowID(),#HWND_TOPMOST,0,0,0,0,#SWP_NOMOVE|#SWP_NOSIZE) 
CreateGadgetList(WindowID(10)) 
TextGadget(0, 10, 10, 180, 20,"printing page") 
EndIf 
EndProcedure 


Procedure EditorPrint(document.s) 
  Protected DC ,a 
  lppd.PRINTDLGAPI; structure for the Api printer selection 
  lppd\lStructsize=SizeOf(PRINTDLGAPI) 
  lppd\flags=#PD_ALLPAGES| #PD_HIDEPRINTTOFILE | #PD_NOSELECTION | #PD_RETURNDC |#PD_PRINTSETUP 
  PrintDlg_(lppd);  Printerselection API  
   DC=lppd\hdc; here the device context is coming back 
If DC<>-1 
  mydevmode.DEVMODE 
  *Pointer.DEVMODE 
  *Pointer=GlobalLock_(lppd\hdevmode) 
  paperlength=*Pointer\dmPaperlength 
  paperwidth=*Pointer\dmPaperwidth 
  ppmm.f=(*Pointer\dmYResolution/10)/2.54 ; pixel per millimeter 
  GlobalFree_(hdevmode) 
  cRect.RECT 
  FormatRange.FORMATRANGE 
  Docinfo.DInfo1 
  Docinfo\cbSize = SizeOf(DInfo1) 
  Docinfo\lpszDocname = @document.s 
  Docinfo\lpszOutput = #Null 
  Docinfo\lpszDataType = #Null 
  Docinfo\fwType = 0 
  PrintWindow() 
  SetGadgetText(0, "preparing for printing") 
  Result=StartDoc_(DC,Docinfo) 
  LastChar = 0 
  leftmargin=GetGadgetState(#Margin_left) 
  rightmargin=GetGadgetState(#Margin_right) 
  topmargin=GetGadgetState(#Margin_top) 
  bottommargin=GetGadgetState(#Margin_bottom) 
  ;ActivateGadget(GadgetNr); seems that it sometimes crashes without activating , you can try to leave it 
  long.textlength 
    long\flags=0 
    MaxLen=SendMessage_(GadgetID(GadgetNr),#EM_GETTEXTLENGTHEX ,@long,0) 
  
  OldMapMode = GetMapMode_(DC) 
  SetMapMode_(DC,#MM_TWIPS) 
  OffsetX = leftmargin*ppmm.f-GetDeviceCaps_(DC,#PHYSICALOFFSETX) 
  If OffsetX < GetDeviceCaps_(DC,#PHYSICALOFFSETX):OffsetX=GetDeviceCaps_(DC,#PHYSICALOFFSETX):EndIf 
  OffsetY=-topmargin*ppmm.f+GetDeviceCaps_(DC,#PHYSICALOFFSETY) 
  If OffsetY > -GetDeviceCaps_(DC,#PHYSICALOFFSETY): OffsetY = -GetDeviceCaps_(DC,#PHYSICALOFFSETY):EndIf 
  HorzRes=((paperwidth/10-rightmargin))*ppmm.f 
  If HorzRes > GetDeviceCaps_(DC,#HORZRES) :HorzRes=GetDeviceCaps_(DC,#HORZRES):EndIf 
  VertRes=-(paperlength/10)*ppmm.f+bottommargin*ppmm.f 
  If VertRes < -GetDeviceCaps_(DC,#VERTRES):VertRes=-GetDeviceCaps_(DC,#VERTRES):EndIf 
  SetRect_(cRect,OffsetX,OffsetY,HorzRes,VertRes) 
  DPtoLP_(DC,cRect,2) 
  SetMapMode_(DC,OldMapMode) 
  FormatRange\hdc = DC 
  FormatRange\hdcTarget = DC 
  FormatRange\rc\left = cRect\left 
  FormatRange\rc\top = cRect\top 
  FormatRange\rc\right = cRect\right 
  FormatRange\rc\bottom = cRect\bottom 
  FormatRange\rcPage\left = cRect\left 
  FormatRange\rcPage\top = cRect\top 
  FormatRange\rcPage\right = cRect\right 
  FormatRange\rcPage\bottom = cRect\bottom 
  a = 1 
  Repeat 
    SetGadgetText(0, "Printing page : "+ Str(a)) 
    Result=StartPage_(DC) 
    FormatRange\chrg\cpMax = -1 
    LastChar = SendMessage_(GadgetNr_hWnd,#EM_FORMATRANGE,#True,@FormatRange) 
    FormatRange\chrg\cpMin = LastChar 
    SendMessage_(GadgetNr_hWnd,#EM_DISPLAYBAND,0,cRect) 
    a + 1 
    EndPage_(DC) 
  Until LastChar >= MaxLen Or LastChar = -1 
  SetGadgetText(0, "Fertig") 
  CloseWindow(10) 
  EndDoc_(DC) 
  SendMessage_(GadgetNr_hWnd,#EM_FORMATRANGE,0,0) 
EndIf 
EndProcedure  

If OpenWindow(0, 10, 10, 640, 480, #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget, "PureBasic Window") 
  If CreateGadgetList(WindowID()) 
  GadgetNr_hWnd=EditorGadget(1, 0, 0, WindowWidth()-100,WindowHeight()) 
  GadgetNr=1 
  ButtonGadget(2, WindowWidth()-90,10,80,24,"Print") 
  ButtonGadget(3, WindowWidth()-90,40,80,24,"Open document") 
  TextGadget(10,WindowWidth()-100,95, 100, 20,"margin in mm") 

  SpinGadget(#Margin_left, WindowWidth()-90,150, 50, 20, 0, 100) 
  SetGadgetState(#Margin_left,20) 
  SetGadgetText(#Margin_left,"0") 
  TextGadget(11,WindowWidth()-90,125, 80, 20,"left") 

  SpinGadget(#Margin_right, WindowWidth()-90,200, 50, 20, 0, 100) 
  SetGadgetState(#Margin_right,20) 
  SetGadgetText(#Margin_right,"0") 
  TextGadget(12,WindowWidth()-90,175, 80, 20,"right") 

  SpinGadget(#Margin_top, WindowWidth()-90,250, 50, 20, 0, 100) 
  SetGadgetState(#Margin_top,20) 
  SetGadgetText(#Margin_top,"0") 
  TextGadget(13,WindowWidth()-90,225, 80, 20,"top") 

  SpinGadget(#Margin_bottom, WindowWidth()-90,300, 50, 20, 0, 100) 
  SetGadgetState(#Margin_bottom,20) 
  SetGadgetText(#Margin_bottom,"0") 
  TextGadget(14,WindowWidth()-90,275, 80, 20,"bottom") 
  EndIf 
  Repeat 
  EventID.l = WaitWindowEvent() 
    If EventID = #PB_Event_CloseWindow 
    Quit = 1 
    EndIf 
    If EventID = #PB_EventGadget 
    Select EventGadgetID() 
      Case 2 
      EditorPrint(DName$) 
      Case 3 
      DName$ = OpenFileRequester("Open File :","*.txt", "Text-Files ( *.txt) | *.txt | Rich Text Files  (*.rtf)|*.rtf", 0) 
      If UCase(GetExtensionPart(DName$)) = "RTF" 
      FileStreamIn(0,DName$, 1,#SF_RTF) 
      Else 
      FileStreamIn(0,DName$, 1,#SF_TEXT) 
      EndIf 
      Case #Margin_left 
          If spinc=2 
             spinc=0 
          Else 
            spin=GetGadgetState(#Margin_left) 
            spinc=spinc+1 
            SetGadgetText(#Margin_left,Str(spin)) 
            WindowEvent() 
          EndIf 
        Case #Margin_right 
          If spinc=2 
            spinc=0 
          Else 
            spin=GetGadgetState(#Margin_right) 
            spinc=spinc+1 
            SetGadgetText(#Margin_right,Str(spin)) 
            WindowEvent() 
          EndIf 
        Case #Margin_top 
          If spinc=2 
            spinc=0 
          Else 
            spin=GetGadgetState(#Margin_top) 
            spinc=spinc+1 
            SetGadgetText(#Margin_top,Str(spin)) 
            WindowEvent() 
          EndIf 
        Case #Margin_bottom 
          If spinc=2 
            spinc=0 
          Else 
            spin=GetGadgetState(#Margin_bottom) 
            spinc=spinc+1 
            SetGadgetText(#Margin_bottom,Str(spin)) 
            WindowEvent() 
          EndIf 
    EndSelect 
    EndIf 
  Until Quit = 1 
EndIf 
End
but how to not use the PrintDlg_() function, for invisible printing ?

Posted: Fri Feb 25, 2005 10:29 pm
by RJP Computing
You actually got that code to work?
I can't get it to work it prints like 3000 pages.

Re: Assigning RTF (styled) text to EditorGadget directly

Posted: Sat Apr 10, 2010 5:19 pm
by Armoured
OpenWindow(0, 200, 200, 400, 200,"Memory Put", #PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_MaximizeGadget)
CreateGadgetList(WindowID(0))
EditorGadget(0, 0, 0, WindowWidth(0), WindowHeight(0),#PB_Editor_ReadOnly)
SendMessage_(GadgetID(0), #EM_LIMITTEXT, -1, 0)

a.s="{\rtf1\ansi\ansicpg1252\deff0\deflang2057{\fonttbl{\f0\fswiss\fcharset0 Arial;}}{\colortbl ;\red255\green0\blue0;\red0\green0\blue0;}"
a=a+"{\*\generator Msftedit 5.41.15.1503;}\viewkind4\uc1\pard\f0\fs20 Hello, this is \cf1\b\fs32 RTF\cf2\b0\fs20 direct!\cf0\par}"
SetGadgetText(0, a)
Repeat:Until WaitWindowEvent()=#PB_Event_CloseWindow
When I compile this code with the "Unicode" option of the compiler the text doesn't have any style applicated
Is a bug of Pb 4.50 beta 2? :?:

Re: Assigning RTF (styled) text to EditorGadget directly

Posted: Sat Apr 10, 2010 5:28 pm
by ts-soft
{\rtf1\ansi\ansicpg1252
Your text define it as ansi and you use unicode, why should this work?

Re: Assigning RTF (styled) text to EditorGadget directly

Posted: Sat Apr 10, 2010 5:31 pm
by Armoured
ts-soft wrote:
{\rtf1\ansi\ansicpg1252
Your text define it as ansi and you use unicode, why should this work?
I can't have an EditorGadget in ansi inside a program in unicode?
PureBasic seems to convert all the strings in unicode, I think is better to leave the decision to the programmer on which string type want use.
Why don't add a command to change the format of a string between ansi and unicode?

Re: Assigning RTF (styled) text to EditorGadget directly

Posted: Sat Apr 10, 2010 7:20 pm
by RASHAD
Hi Armoured
Try the next code with unicode option enabled

Code: Select all

OpenWindow(0, 200, 200, 400, 200,"Memory Put", #PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_MaximizeGadget)
EditorGadget(0, 0, 0, WindowWidth(0), WindowHeight(0),#PB_Editor_ReadOnly)
SendMessage_(GadgetID(0), #EM_LIMITTEXT, -1, 0)

a.s="{\rtf1\ansi\ansicpg1252\deff0\deflang2057{\fonttbl{\f0\fswiss\fcharset0 Arial;}}{\colortbl ;\red255\green0\blue0;\red0\green0\blue0;}"
a.s=a.s+"{\*\generator Msftedit 5.41.15.1503;}\viewkind4\uc1\pard\f0\fs20 Hello, this is \cf1\b\fs32 RTF\cf2\b0\fs20 direct!\cf0\par}"
*MemoryBuffer = AllocateMemory(Len(a.s)+1)
PokeS(*MemoryBuffer, a.s, Len(a.s)+1,#PB_Ascii)
SetGadgetText(0,PeekS(*MemoryBuffer ,Len(a.s)+1,#PB_Unicode))
Repeat:Until WaitWindowEvent()=#PB_Event_CloseWindow 
Good Luck

Re: Assigning RTF (styled) text to EditorGadget directly

Posted: Sat Apr 10, 2010 7:28 pm
by ts-soft
easily modified version of RASHAD code:

Code: Select all

OpenWindow(0, 200, 200, 400, 200,"Memory Put", #PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_MaximizeGadget)
EditorGadget(0, 0, 0, WindowWidth(0), WindowHeight(0),#PB_Editor_ReadOnly)
SendMessage_(GadgetID(0), #EM_LIMITTEXT, -1, 0)

a.s="{\rtf1\ansi\ansicpg1252\deff0\deflang2057{\fonttbl{\f0\fswiss\fcharset0 Arial;}}{\colortbl ;\red255\green0\blue0;\red0\green0\blue0;}"
a.s=a.s+"{\*\generator Msftedit 5.41.15.1503;}\viewkind4\uc1\pard\f0\fs20 Hello, this is \cf1\b\fs32 RTF\cf2\b0\fs20 direct!\cf0\par}"
*MemoryBuffer = AllocateMemory(StringByteLength(a, #PB_Ascii) + 1)
PokeS(*MemoryBuffer, a.s, -1, #PB_Ascii)
SetGadgetText(0,PeekS(*MemoryBuffer))
Repeat:Until WaitWindowEvent()=#PB_Event_CloseWindow

works in ascii and unicode mode

greetings
Thomas

Re: Assigning RTF (styled) text to EditorGadget directly

Posted: Sat Apr 10, 2010 7:29 pm
by RASHAD
Thank you Thomas
You are very fast I can see

Re: Assigning RTF (styled) text to EditorGadget directly

Posted: Sat Apr 10, 2010 7:31 pm
by ts-soft
RASHAD wrote:Thank you Thomas
You are very fast I can see
i have edit the code to change the len to stringbytelength!

Re: Assigning RTF (styled) text to EditorGadget directly

Posted: Sat Apr 10, 2010 10:07 pm
by Armoured
ts-soft wrote:easily modified version of RASHAD code:

Code: Select all

OpenWindow(0, 200, 200, 400, 200,"Memory Put", #PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_MaximizeGadget)
EditorGadget(0, 0, 0, WindowWidth(0), WindowHeight(0),#PB_Editor_ReadOnly)
SendMessage_(GadgetID(0), #EM_LIMITTEXT, -1, 0)

a.s="{\rtf1\ansi\ansicpg1252\deff0\deflang2057{\fonttbl{\f0\fswiss\fcharset0 Arial;}}{\colortbl ;\red255\green0\blue0;\red0\green0\blue0;}"
a.s=a.s+"{\*\generator Msftedit 5.41.15.1503;}\viewkind4\uc1\pard\f0\fs20 Hello, this is \cf1\b\fs32 RTF\cf2\b0\fs20 direct!\cf0\par}"
*MemoryBuffer = AllocateMemory(StringByteLength(a, #PB_Ascii) + 1)
PokeS(*MemoryBuffer, a.s, -1, #PB_Ascii)
SetGadgetText(0,PeekS(*MemoryBuffer))
Repeat:Until WaitWindowEvent()=#PB_Event_CloseWindow

works in ascii and unicode mode

greetings
Thomas
This is my version:

Code: Select all

Procedure.s UnicodeToAscii(string.s)
  Protected ascii.s, *MemoryBuffer.l
  *MemoryBuffer = AllocateMemory(StringByteLength(string, #PB_Ascii) + 1)
  If (*MemoryBuffer)
    PokeS(*MemoryBuffer, string, -1, #PB_Ascii)
    ascii = PeekS(*MemoryBuffer)
    FreeMemory(*MemoryBuffer)
  EndIf
  ProcedureReturn ascii
EndProcedure

OpenWindow(0, 200, 200, 400, 200,"Memory Put", #PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_MaximizeGadget)
EditorGadget(0, 0, 0, WindowWidth(0), WindowHeight(0),#PB_Editor_ReadOnly)
SendMessage_(GadgetID(0), #EM_LIMITTEXT, -1, 0)

a.s="{\rtf1\ansi\ansicpg1252\deff0\deflang2057{\fonttbl{\f0\fswiss\fcharset0 Arial;}}{\colortbl ;\red255\green0\blue0;\red0\green0\blue0;}"
a.s=a.s+"{\*\generator Msftedit 5.41.15.1503;}\viewkind4\uc1\pard\f0\fs20 Hello, this is \cf1\b\fs32 RTF\cf2\b0\fs20 direct!\cf0\par}"
SetGadgetText(0,UnicodeToAscii(a))
Repeat:Until WaitWindowEvent()=#PB_Event_CloseWindow
8)
Changes:
1) Memory allocation and deallocation is more accurate for the use in a real program
2) The buffer and PokeS/PeekS conversion is inside a procedure for much comfort

But why not to have a basic command to make this? :?:

Thanks Rashad and Ts-soft :wink: