Announcement

Collapse
No announcement yet.

Newbie RTF Commands

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Newbie RTF Commands

    Since I will admit I am somewhat a 'Newbie' to RTF commands (my only previous experiences were VB (which handled it for me), and PB (which has some Excellent examples for RTF. I thought I would ask some basic questions that searching I can not find if or if not I misconcept how RTF works.

    So far, all examples given (including my own) use "Streamin" (which is basically pointers to the data to pass to how its shown, or I think), but its all like you have a complete document and just showing. (Like a file Open, format, show)

    What I am wondering is if I can "append" to a file already shown? or if I have to get all the text, and then reformat with the RTF commands to properly show? (AKA..if using a RichEdit, to show, do you have to get text, reformat with the unseen RTF commands? and then repost with what you added?, or can you just append what you need to the "Text part" along with the unseen RTF commands?")

    It sounds weird to me because it is like working with webpages (although its all text, the "engine" behind the scenes recognizes certain "flags is you will" and does something with it, and only shows the text part as you meant it)

    Anyways, excluding my own confusion, is this possible?
    • Original RTF codes to set things up
    • Text
    • End RTF codes


    versus
    • Original RTF codes to set things up
    • Text
    • Text --- Append as necessary
    • End RTF codes


    or do I have to[*] Original RTF codes to set things up[*] Text[*] Text --- Append as necessary[*] End RTF codes[/list]
    then get the text and
    • Original RTF codes to set things up
    • Text --- Original now becomes original plus what I added?
    • End RTF codes


    does any of this make sense?
    Engineer's Motto: If it aint broke take it apart and fix it

    "If at 1st you don't succeed... call it version 1.0"

    "Half of Programming is coding"....."The other 90% is DEBUGGING"

    "Document my code????" .... "WHYYY??? do you think they call it CODE? "

  • #2
    or do I have to[*] Original RTF codes to set things up[*] Text[*] Text --- Append as necessary[*] End RTF codes[/list]then get the text and
    Original RTF codes to set things up
    Text --- Original now becomes original plus what I added?
    End RTF codes

    does any of this make sense?
    Yes

    You would not be able to add because the original text should be bracketed
    {.....} and as far as I know anything added would be ignored.

    However, I always do it the way you have shown above.
    Bob

    Comment


    • #3
      So far, all examples given (including my own) use "Streamin" (which is basically pointers to the data to pass to how its shown, or I think), but its all like you have a complete document and just showing. (Like a file Open, format, show)

      What I am wondering is if I can "append" to a file already shown? or if I have to get all the text, and then reformat with the RTF commands to properly show? (AKA..if using a RichEdit, to show, do you have to get text, reformat with the unseen RTF commands? and then repost with what you added?, or can you just append what you need to the "Text part" along with the unseen RTF commands?")
      The streaming functions can be used with valid RTF from a string (whether it came from an RTF file, another richedit control, a database, or you built it yourself).

      Code:
      SUB rt_AppendTextRTF(BYVAL dwRT AS DWORD, zText AS STRING)
      
          LOCAL tStream AS EDITSTREAM
      
          rt_zText = zText
          rt_lPos = 1
      
          tStream.pfnCallback = CODEPTR(rt_StreamStringIn)
      
          SendMessage dwRT, %EM_STREAMIN, %SF_RTF OR %SFF_SELECTION, VARPTR(tStream)
      
      END SUB
      The above routine inserts RTF text at the current selection. If you have selected the end of the current richedit, the RTF text will be appended.

      rt_zText is a global string variable to hold the RTF text being inserted. rt_lPos is a global long variable holding the current position of the transfer; set to 1 to begin.

      Code:
      FUNCTION rt_StreamStringIn(BYVAL dwCookie AS DWORD, BYVAL pbBuffer AS BYTE PTR, BYVAL lBufferLen AS LONG, BYREF lBytes AS LONG) AS LONG
      
          ' copy smaller of: remaining text or buffer length
      
          lBytes = MIN&(lBufferLen, LEN(rt_zText) - (rt_lPos - 1))
      
          ' if text to process,
          '    copy to richedit buffer from position in text
          '    set new text position
      
          IF lBytes > 0 THEN
              POKE$ pbBuffer, MID$(rt_zText, rt_lPos, lBytes)
              rt_lPos = rt_lPos + lBytes
          END IF
      
      END FUNCTION
      The above StreamIn callback copies the number of bytes in the richedit control buffer can accept (or remaining bytes to be inserting, if fewer). After copying from the current string position to the richedit buffer, it increments rt_lPos (current position in string) by the number of bytes copied.

      May the stream be with you.

      Ron

      Comment


      • #4
        I stand corrected (I will now have to change my code)

        Many thanks

        Bob

        Comment


        • #5
          You can add /replace text in a RichEdit control by pasting from the clipboard and also by using the EM_REPLACESEL message...
          '
          Code:
          #Include "WIN32API.INC"
          #Include "RICHEDIT.INC"
          '------------------/Includes
           
          Function REStreamInCB(ByVal dwCookie As Dword, ByVal pbBuff As Byte Ptr, _
                                ByVal cb As Long, ByRef pcb As Long) As Long
           Local psBuffer As String Ptr
            psBuffer = dwCookie                                                       ' **
            pcb = Min(Len(@psBuffer), cb) 
            If pcb > 0 Then
              Poke$ pbBuff, Left$(@psBuffer, pcb)
              @psBuffer = Mid$(@psBuffer, pcb + 1)
            End If
          End Function
          '------------------/RESTReamInCB
           
          Function REStreamIn(ByVal hEd As Dword, ByVal sBuffer As String, ByVal StrmFmt As Long) As Long
           Local ES As EDITSTREAM
            ES.dwCookie    = VarPtr(sBuffer)                                          ' ** 
            ES.pfnCallback = CodePtr(REStreamInCB)
           
           Function = SendMessage(hEd, %EM_STREAMIN, StrmFmt, VarPtr(ES))             ' %SF_RTF / %SF_TEXT
          End Function                                                                ' OR %SFF_SELECTION
          '------------------/REStreamIn
           
          CallBack Function DIALOGProc()
           Local RichText As String 
           Static RTFHeader As String
           
            Select Case CbMsg
              Case %WM_INITDIALOG
                Dialog Post CbHndl, %WM_USER + 1000, 0, 0
           
              Case %WM_USER + 1000, 0, 0                                              ' RTF header starts with
                RTFHeader = "{"+ _                                                    ' Opening brace
                  "\rtf1\ansi\ansicpg1252\deff0\deflang1033"+ _                       ' Version and Char set
                  "{\fonttbl"+ _                                                      ' Font table
                  "{\f0\froman\fcharset0\fprq2\fttruetype Times New Roman;}"+ _
                  "{\f1\fswiss\fcharset0\fprq2\fttruetype Arial;}"+ _
                  "{\f2\fnil\fprq2\fcharset2 Wingdings;}}"+ _
                  "{\colortbl"+ _                                                     ' Colour table
                  "\red0\green0\blue0;"+ _                                            ' black  \cf0
                  "\red255\green0\blue0;}"                                            ' red    \cf1
           
                RichText = _
                  " Rather than streaming the contents in (eg from a file) - or pasting from the "+ _
                  "clipboard - you can use ""CONTROL SET TEXT"" to fill a RichEdit control. \par "+ _
                  "You can also use ""%EM_REPLACESEL"" to insert / append new text. \par\par "+ _
                  "Set the insertion point or select text to be replaced and click either button below. "+ _
                  "\fs24 \f2 \cf1 J"
           
                RichText = RTFHeader + RichText + "}"                                 ' NB Closing brace
                Control Set Text CbHndl, 101, RichText                                ' load marked up text
           
              Case %WM_NCACTIVATE
                Static hWndSaveFocus As Dword
                If Isfalse CbWparam Then
                  hWndSaveFocus = GetFocus()
                Elseif hWndSaveFocus Then
                  SetFocus(hWndSaveFocus)
                  hWndSaveFocus = 0
                End If
           
              Case %WM_COMMAND
                Select Case As Long CbCtl
                  Case 110
                    If CbCtlMsg = %BN_CLICKED Then 
                      'RichText = "{ 'Streamed In' }"
                      RichText = RTFHeader + " \cf1 \f1  'Streamed In' " + "}"
                      ' need RTHeader if changing font, colour etc - otherwise inserted text takes on
                      ' character formatting of the insertion point / text being replaced.
                      REStreamIn GetDlgItem(CbHndl, 101), RichText, %SF_RTF OR %SFF_SELECTION 
                    End If
                  Case 120
                    If CbCtlMsg = %BN_CLICKED Then 
                      RichText = RTFHeader + " \cf1 \f1  'EM_Replaced' " + "}"
                      Control Send CbHndl, 101, %EM_REPLACESEL, 0, StrPtr(RichText)
                    End If
                End Select
           
            End Select
          End Function
          '------------------/DialogProc
           
          Function PbMain()
           Local lRslt As Long, hDlg As Dword 
           LoadLibrary("RICHED32.DLL")
           
            Dialog New 0, "RichEdit insert / replace test", , , 300, 220, %WS_SYSMENU, %WS_EX_TOPMOST To hDlg
            Control Add "RichEdit", hDlg, 101, "", 10, 10, 275, 110, _
              %WS_CHILD Or %WS_VISIBLE Or %ES_MULTILINE Or %ES_WANTRETURN Or _ 
              %WS_VSCROLL Or %ES_DISABLENOSCROLL, %WS_EX_RIGHTSCROLLBAR
            Control Add Button, hDlg, 110, "Stream In", 55, 175, 50, 15
            Control Add Button, hDlg, 120, "ReplaceSel", 145, 175, 50, 15
           
            Dialog Show Modal hDlg, Call DIALOGProc
           
          End Function
          '------------------/PbMain
          '
          Rgds, Dave

          Comment


          • #6
            Dave when you mentioned
            RichText = _
            " Rather than streaming the contents in (eg from a file) - or pasting from the "+ _
            "clipboard - you can use ""CONTROL SET TEXT"" to fill a RichEdit control. \par "+ _
            "You can also use ""%EM_REPLACESEL"" to insert / append new text. \par\par "+ _
            "Set the insertion point or select text to be replaced and click either button below. "+ _
            "\fs24 \f2 \cf1 J"
            Kinda fell into place the way I have been doing it the last year or 2 (never had time to research how it all works, but as long as it worked was good for me)

            Now that I am back to correcting "Mistakes of my past" (stuff I did not learn beyond "Just make it work", or stuff I misunderstood, but it worked, and now I know I made mistakes)...

            My 1st attempts (using Richedit) I first tried "CONTROL SET TEXT" and I will admit at some point "CONTROL SET TEXT" would show all the text formatted (including \line, \fs24 and etc, but not show as a linefeed, font = 24 sort of thing which I believe was just because I had a mistake somewhere)

            I will have to go back and work up a test example, but I suspect my suspicions are true
            1. Taken as text, all is either text or formatting commands, escapes sequences, or what have you, and only what you post it to will either understand to parse and format, or show as it truly is (its a string...whether its css, xml, rtf, some other format
            2. Richedit understands these formatting commands and shows just the text
            3. If I sent to notepad, it shows all the string (Formatting commands and all) because it was not built to parse the string
            4. Streamin vs "Control Set Text" time differences really would only be due to using pointers vs strings (not sure I phrased that right, but you know what I meant)

            Either way, if properly split apart, appended, edited etc, when its all put back together, it should show the same way (whether streamin, or "Control Set Text") just because "Behind the scenes" the richedit knows what to do with what you sent it.

            Somewhere in the past, I posted some source code to speed things up if reading large files, strings etc...but for the sake of learning the basics of "WHY" it worked, I am back to basics and do not care about speed since in this case I am not doing much more than 1 sentence to 1 paragraph.

            up to this point, I think I get the basics of RTF commands (Still learning I have some concepts to re-think) but at least a sandbox concept.

            I guess the next "Baby Step" would be how to include a picture in the midst of it (forgive my example, but maybe someone would know what
            I am trying)

            Code:
            <Rtf Formatting><Text><Picture><Text><end of Rtf formatting>
            I am sure somewhere in the forums there are simple examples, but I just do not know enough terminology on this idea to even do a correct search without all the examples being streamin and text only. (although I can understand why)
            Engineer's Motto: If it aint broke take it apart and fix it

            "If at 1st you don't succeed... call it version 1.0"

            "Half of Programming is coding"....."The other 90% is DEBUGGING"

            "Document my code????" .... "WHYYY??? do you think they call it CODE? "

            Comment


            • #7
              Cliff,
              I guess the next "Baby Step" would be how to include a picture in the midst of it...
              Not such a baby step , that will entail the use of OLE/COM interfacing.

              Börje's 'AboutBox' and 'RichEdit with OLE' code samples on http://www.reonis.com/POFFS/ are about as 'simple' as you will find (but beware, even with those, Börje warns that dizziness and nausea might ensue from studying their inner workings :sick.

              <Rtf Formatting><Text><Picture><Text><end of Rtf formatting>
              Using a text editor to look at a WordPad file that has an embedded picture, shows that manual formatting of data to be inserted by code into a RichEdit control, probably isn't practical. The <Picture> is just too large a block of data to be manageable.

              BTW. Shannon Larratt once made a pretty good fist of explaining the workings of RTF markup here.. http://www.bmeworld.com/shannon/basic/rtfhelp.html
              - I've made a local copy of his web page (against the day that the link no longer works) as I refer to it often. It covers the basic character formatting, colour etc. that you need to quickly throw together some text for display in a RichEdit control.

              More detail on version 1.0 is available from http://latex2rtf.sourceforge.net/RTF-Spec-1.0.txt and more recent specs here.. www.wotsit.org.

              Good luck.
              Rgds, Dave

              Comment


              • #8
                Thank you Dave, your links help verify a thought I had in the past and worked on, but could not complete, (well partially complete), and had to give up due to other ideas that took precedence at the time, but I would like to go back and finish a concept I started.

                The links show what I figured, was its not as simple as <img></img> sort of idea, but more on the "Break the img into binary or hex values and stream them in" sort of like some examples I see using DATA and a bunch of hex codes (but I may be off there cause I have not researched that either yet)

                well off to research deeper and see if I will get dizzy? or if I can make sense of code I do not use often so I got a bit of a wall to climb :coffee3:
                Engineer's Motto: If it aint broke take it apart and fix it

                "If at 1st you don't succeed... call it version 1.0"

                "Half of Programming is coding"....."The other 90% is DEBUGGING"

                "Document my code????" .... "WHYYY??? do you think they call it CODE? "

                Comment

                Working...
                X