Announcement

Collapse
No announcement yet.

Justify or Center Align dpWrapText

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

  • Justify or Center Align dpWrapText

    Hello,

    Is there a way to justify or center align a wrapped text using the DDOC dpWrapText function?

    Code:
    dpFont ihandle, %DDOC_FONTNORMAL + %DDOC_FONTITALIC, 8, %vbBlack, "Arial"
    zCourseDescription = slFN("CoursesCourseDescription")
    iLen% = Len(zCourseDescription)
    Do
        iPrinted% = dpWrapText(IHandle%, 0.5,5.1, 4.8, 7, .17, zCourseDescription)
    
           If iPrinted% = 0 Then Exit Do
    
              iLen% = iLen% - iPrinted%
                If iLen% < 1 Then
                   Exit Do
                Else            
                   zCourseDescription = Mid$(zCourseDescription, iPrinted%)
                End If
    Loop
    Any hint would be appreciated.

    Best regards.
    Fredrick Ughimi
    www.meganetsoft.com

  • #2
    I haven't used dpWrapText until now, but it looks like the rectangle could be placed at any position.
    It is easier for others to have a full program to test. This code will compile with ddoc and run.

    Code:
    #DIM ALL
    #INCLUDE "win32api.inc"
    #INCLUDE "ddoc_p32.inc"
    %vbBlack=&H0&:%vbRed=&HFF&:%vbGreen=&HFF00&:%vbYellow=&HFFFF&:%vbBlue=&HFF0000:%vbMagenta=&HFF00FF
    %vbCyan=&HFFFF00:%vbWhite=&HFFFFFF:%vbGrey=&HC0C0C0:%LIGHT_SHADE=&HE0E0E0
    
    FUNCTION PBMAIN () AS LONG
     LOCAL ihandle,endCode AS INTEGER
     LOCAL FontStyle,FontSize,DefaultColor,DefaultZoom,counter,charsprinted AS LONG
     LOCAL zTitle AS ASCIIZ * 128,zoutputfile AS ASCIIZ * 128,defaultFont AS ASCIIZ * 64,zText AS ASCIIZ * 300
     LOCAL LeftMargin,y,lineheight AS SINGLE
    '----------------------------------------------------------------------------------
     zTitle= "My Test"
     zOutputFile = "junk.ddc" 'handling manually by creating our own output file
     KILL zOutputFile         'must start with fresh output file if doing manually
     ERRCLEAR
    '----------------------------------------------------------------------------------
     DefaultZoom=%DDOC_ZOOM100+ %DDOC_VIEWBUILD  'ZOOMWIDTH 'ZOOMFIT ZOOM75  ZOOM100 ZOOM125  'add viewbuild so it shows!!
     ihandle = dpStartDoc(0,zTitle,zOutputfile,%DDOC_INCH,%DDOC_PAPER_LETTER,%DDOC_SYSTEM_DEFAULT,%DDOC_BIN_AUTO,DefaultZoom)
     SELECT CASE ihandle
       CASE -1: ? "no handles":EXIT FUNCTION
       CASE -2: ? "bad file"  :EXIT FUNCTION
       CASE -3: ? "file already exists":EXIT FUNCTION
     END SELECT
    
     FontStyle = %DDOC_FONTNORMAL '%DDOC_  FONTNORMAL,FONTBOLD,FONTITALIC,FONTUNDERLINE,FONTSTRIKEOUT,FONTBASELINE
     FontSize = 8
     DefaultColor = %Vbgreen
     DefaultFont = "courier" 'set font or color will not work!
     dpFont iHandle,  FontStyle ,FontSize,DefaultColor,DefaultFont
     zText = REPEAT$(2,"Now is the time for all good men to come to the aid of their countrymen.")
     LeftMargin = 0
     LineHeight = .17
     y = 0
    
     FOR counter = 1 TO 1:GOSUB PrintDetailLine:NEXT
     endCode% = %DDOC_END_VIEW + %DDOC_END_DELETE
     SLEEP 250 'may be needed in some cases with dpText
     dpEndDoc iHandle%, EndCode%
    
    EXIT FUNCTION
    
    
    PrintDetailLine:
      dpText iHandle%,LeftMargin, y,%ddoc_left,zText
      charsPrinted = dpWrapText(iHandle, 3, 1, 4, 4, lineheight, zText)
      y+= lineheight
    RETURN
    
    END FUNCTION
    https://www.tesla.com/roadster

    Comment


    • #3
      Hello Mike,

      Thanks for your response and your code.

      I want to be able to center align (not start from center) the text like dpText would do when the constant %DDOC_CENTER is used:

      Code:
      dpText IHandle%, 6.25, 1.8, %DDOC_CENTER, zCourseDescription
      except that zCourseDescription is quite long. Can't fit on a line. So I resorted to using dpWrapText() to be able to wrap the text to the next line.

      Not sure if one can center align with dpWrapText().

      Best regards.
      Fredrick Ughimi
      www.meganetsoft.com

      Comment


      • #4

        Click image for larger version  Name:	wordwrap1.png Views:	1 Size:	4.5 KB ID:	772088
        Code:
        #PBFORMS CREATED V2.01
        'WordWrap1.bas
        #COMPILE EXE
        #DIM ALL
        GLOBAL ghDlg AS DWORD
        #PBFORMS BEGIN INCLUDES
        #INCLUDE ONCE "WIN32API.INC"
        #PBFORMS END INCLUDES
        #PBFORMS BEGIN CONSTANTS
        %TEXTBOX1    = 1001
        %TEXTBOX2    = 1002
        %BUTTON1     = 1003
        #PBFORMS END CONSTANTS
        #PBFORMS DECLARATIONS
        '------------------------------------------------------------------------------
        FUNCTION WORDWRAP AS STRING
         LOCAL NumberOfLines AS LONG
         LOCAL s AS STRING
         CONTROL GET TEXT ghDlg,%TEXTBOX1 TO s 'get text from textbox1
         CONTROL SET TEXT ghDlg, %TEXTBOX2,s   'put text from textbox1 into textbox2
         CONTROL SEND ghDlg, %TEXTBOX2, %EM_GETLINECOUNT, 0,0 TO NumberOfLines 'get number of lines if needed
         CONTROL SEND ghDlg, %TEXTBOX2, %EM_FMTLINES, %TRUE, 0                 'add $CR+$CR+LF
         CONTROL GET TEXT ghDlg,%TEXTBOX2 TO s
         REPLACE $CR+$CR WITH $CR IN s
         ? s,,USING$("Lines #",NumberOfLines)
         FUNCTION = s
        END FUNCTION
        '------------------------------------------------------------------------------
        FUNCTION PBMAIN()
          ShowDIALOG1 %HWND_DESKTOP
        END FUNCTION
        '------------------------------------------------------------------------------
        CALLBACK FUNCTION MyCallBack()
        
         SELECT CASE AS LONG CB.MSG
        
          CASE %WM_SIZE
          CASE %WM_INITDIALOG
          CASE %WM_COMMAND
           SELECT CASE AS LONG CB.CTL
                CASE %TEXTBOX1
                CASE %TEXTBOX2
            CASE %BUTTON1
             IF CB.CTLMSG = %BN_CLICKED OR CB.CTLMSG = 1 THEN
              WORDWRAP
             END IF
           END SELECT
          CASE %WM_QUERYENDSESSION
          CASE %WM_SYSCOMMAND
             IF (CB.WPARAM AND &HFFF0) = %SC_CLOSE THEN
             END IF
          CASE %WM_CLOSE
          CASE %WM_DESTROY
           postquitmessage 0
           FUNCTION = 0
          END SELECT
        END FUNCTION
        '------------------------------------------------------------------------------
        FUNCTION ShowDIALOG1(BYVAL hParent AS DWORD) AS LONG
          LOCAL lRslt AS LONG
        #PBFORMS BEGIN DIALOG %IDD_DIALOG1->->
          LOCAL hDlg  AS DWORD
        
          DIALOG NEW hParent, "WordWrap1.bas", 372, 118, 443, 400, %WS_POPUP OR _
            %WS_BORDER OR %WS_THICKFRAME OR %WS_CAPTION OR %WS_SYSMENU OR _
            %WS_MINIMIZEBOX OR %WS_MAXIMIZEBOX OR %WS_CLIPSIBLINGS OR %WS_VISIBLE OR _
            %DS_MODALFRAME OR %DS_3DLOOK OR %DS_NOFAILCREATE OR %DS_SETFONT, _
            %WS_EX_CONTROLPARENT OR %WS_EX_LEFT OR %WS_EX_LTRREADING OR _
            %WS_EX_RIGHTSCROLLBAR, TO hDlg
          CONTROL ADD TEXTBOX, hDlg, %TEXTBOX1, "", 10, 15, 415, 12
          CONTROL ADD TEXTBOX, hDlg, %TEXTBOX2, "", 5, 53, 75, 335, %WS_CHILD _
            OR %WS_VISIBLE OR %ES_LEFT OR %ES_MULTILINE OR %ES_WANTRETURN, _
            %WS_EX_CLIENTEDGE OR %WS_EX_LEFT OR %WS_EX_LTRREADING OR _
            %WS_EX_RIGHTSCROLLBAR
          CONTROL ADD BUTTON,  hDlg, %BUTTON1, "Convert Textbox1 to Textbox2", 275, _
            50, 150, 15
        #PBFORMS END DIALOG
          ghDlg = hDlg
          'put here so PBFORMS shows textbox correctly
          CONTROL SET TEXT ghDlg, %TEXTBOX1,"Now is the time for all good men to come to the aid of their countrymen after " +_
                                            "the quick brown fox jumps over the lazy brown dogs back."
        
          DIALOG SHOW MODELESS hDlg, CALL MyCallBack TO lRslt
          LOCAL uMsg AS tagMsg
          WHILE GetMessage(uMsg, %NULL, 0, 0)
            IF IsDialogMessage(hDlg, uMsg) = 0 THEN
              TranslateMessage uMsg
              DispatchMessage uMsg
              END IF
          WEND
        #PBFORMS BEGIN CLEANUP %IDD_DIALOG1
        #PBFORMS END CLEANUP
          FUNCTION = lRslt
        END FUNCTION
        Text might be wrapped within a hidden or off screen multi-line textbox then use dpText instead of dpWrapText
        Last edited by Mike Doty; 4 May 2018, 02:06 AM.
        https://www.tesla.com/roadster

        Comment


        • #5
          Fredrick,
          Change function a bit and perhaps do this:
          Code:
          s$ = WordWrap(LongString$,NumberOfLines&)
          for x = 1 to NumberOfLines
            zText = PARSE$(s,$CRLF,x)
            dpText iHandle%,LeftMargin, y,%ddoc_left,zText
            y+= lineheight
           NEXT
          https://www.tesla.com/roadster

          Comment


          • #6
            Hello Mike,

            Thanks alot. Would take a look and get back to you.

            Best regards.
            Fredrick Ughimi
            www.meganetsoft.com

            Comment


            • #7
              Hello Mike,

              Is it possible to retrieve the text in a multiline Textbox one line at a time? That way one can use dpText on each line retrieved on the DDOC Report.
              That would really give one the flexibility to format each as one wants.

              Best regards.
              Fredrick Ughimi
              www.meganetsoft.com

              Comment


              • #8

                Is it possible to retrieve the text in a multiline Textbox one line at a time?
                Yes. Same as post #5.
                Code:
                s$ = WordWrap(LongString$,NumberOfLines&)'s$ all lines of multiline textbox with added $CRLF's
                FOR x = 1 to NumberOfLines
                 zText = PARSE$(s,$CRLF,x) 'individual lines of multiline textbox
                 dpText iHandle%,LeftMargin, y,%ddoc_left,zText 'print 1-line of multiline textbox
                 y+= lineheight 'position to next line
                NEXT
                https://www.tesla.com/roadster

                Comment


                • #9
                  Mike,

                  Great! Thanks a lot.
                  Fredrick Ughimi
                  www.meganetsoft.com

                  Comment


                  • #10
                    is it possible to retrieve the text in a multiline textbox one line at a time?
                    yes ==>

                    the em_getline message copies a line of text from an edit control and places it in a specified buffer. You can send this message to either an edit control or a rich edit control.
                    mcm

                    Comment


                    • #11
                      Thank you MCM.
                      Fredrick Ughimi
                      www.meganetsoft.com

                      Comment


                      • #12
                        Hello,

                        I tried this function (GetLineText) from Borje Hagsten on DDOC. Initially the result wasn't that consistent. Seems to be okay now.

                        Code:
                        '====================================================================
                        ' From Borje Hagsten: https://forum.powerbasic.com/forum/user-to-user-discussions/powerbasic-for-windows/2668-ddt-display-questions
                        FUNCTION GetLineText (BYVAL hEdit AS DWORD, BYVAL ln AS LONG) AS STRING
                        '--------------------------------------------------------------------
                          ' Get a line's text. hEdit is TextBox handle, ln is line number.
                          '------------------------------------------------------------------
                          LOCAL Buf AS STRING, lnStart AS LONG, lnLen AS LONG
                        
                          lnStart = SendMessage(hEdit, %EM_LINEINDEX, ln, 0)       'line start
                          lnLen   = SendMessage(hEdit, %EM_LINELENGTH, lnStart, 0) 'line length
                        
                          IF lnLen THEN
                             Buf = SPACE$(lnLen)
                             lnLen = SendMessage(hEdit, %EM_GETLINE, ln, STRPTR(Buf)) 'Get line
                             IF lnLen THEN FUNCTION = Buf
                          END IF
                        
                        END FUNCTION
                        
                        CONTROL HANDLE nCbHndl, %ID_FRMMARITIMECERTIFICATESRPTDLG_TXTCOURSEDESCRIPTION TO hCourseDescription
                        
                        dpText IHandle, 2.9,5.1, %DDOC_CENTER, "" + GetLineText(hCourseDescription, 0)
                        dpText IHandle, 2.9,5.3, %DDOC_CENTER, "" + GetLineText(hCourseDescription, 1)
                        dpText IHandle, 2.9,5.5, %DDOC_CENTER, "" + GetLineText(hCourseDescription, 2)
                        dpText IHandle, 2.9,5.7, %DDOC_CENTER, "" + GetLineText(hCourseDescription, 3)
                        Fredrick Ughimi
                        www.meganetsoft.com

                        Comment


                        • #13
                          What if the number of wrapped lines is not 4?
                          CONTROL SEND hDlg, %TEXTBOX, %EM_GETLINECOUNT, 0,0 TO NumberOfLines


                          Someone probably has a function to wordwrap a string into multiple strings knowing the width of the print area and your font (to eliminate the textbox control.)

                          I introduced a single line text box for the input string and a multiline textbox for the wordwrap see post #4.
                          https://www.tesla.com/roadster

                          Comment


                          • #14
                            Hello Mike,

                            Yeah. I was just testing to see how it comes out. My result is still not consistent. Sometimes I get right result and other times not.

                            I wonder why? Doing some more testing...
                            Fredrick Ughimi
                            www.meganetsoft.com

                            Comment

                            Working...
                            X