home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / mletst.zip / mletest.C < prev    next >
Text File  |  1993-05-24  |  13KB  |  350 lines

  1. //----------------------------------------------------------------------------
  2. //
  3. // MLE sample - words and music by Dan Lee
  4. //
  5. //----------------------------------------------------------------------------
  6. //
  7. // Yet another sample program, this one plays with a Multiline Entry Field.
  8. //
  9. // Actions -
  10. //
  11. //    SEARCH: Searches for a string (limited in this sample to 10 chars) in
  12. //       in the MLE.  When the first menu item is selected, a simple dialog
  13. //       box is called to get the search string.  Then the MLE_SEARCH struct
  14. //       is initialized with the string, and then sent to the MLE with the
  15. //       message MLM_SEARCH.  The MLE responds by hilighting the string (if
  16. //       it finds it).  The way the MLE_SEARCH structure is initialized,
  17. //       searches always start from the beginning of the MLE.
  18. //
  19. //    FONTS: Uses WinFontDlg to get a new font, then sets the MLE font by
  20. //       sending the MLM_SETFONT message.  The FONTDLG struct is kept globally
  21. //       and initialized in the WM_CREATE step rather than each time the menu
  22. //       item is selected.  That way changes made to the struct by WinFontDlg
  23. //       are still current the next time the ChangeFont menu item is selected.
  24. //
  25. //    EXPORT: Sets up and uses the MLM_EXPORT message to put the MLE text into
  26. //       a buffer.  I put the MLE format stuff here (rather than right after
  27. //       the MLE is created) to emphasize how critical the text format is in
  28. //       the MLE.  For example, if you use the message MLM_QUERYTEXTLENGTH
  29. //       rather than MLM_QUERYFORMATTEXTLENGTH, the number you get back may
  30. //       NOT reflect the cr-lf chars correctly.  This has bitten a lot of
  31. //       developers trying to use that number to determine how much space to
  32. //       allocate for the export buffer.
  33. //
  34. //       Once the buffer was successfully exported, I put it into a message
  35. //       box to display it (I know - lazy, lazy) rather than increasing the
  36. //       complexity of this sample by putting it into a container or drawing
  37. //       it onto the client space.  I may add a WinFileDlg call (later!) to
  38. //       write the buffer to disk.
  39. //
  40. //----------------------------------------------------------------------------
  41. //
  42. // If I've made any horrible errors in here, or if you find bugs or have
  43. // suggestions, feel free to contact me at 72360,3611.
  44. //
  45. //----------------------------------------------------------------------------
  46.  
  47. #define  INCL_WINFRAMEMGR
  48. #define  INCL_WINMLE
  49. #define  INCL_WINPOINTERS
  50. #define  INCL_WINSTDDLGS
  51. #define  INCL_WINWINDOWMGR
  52.  
  53. #include <os2.h>
  54. #include <stdio.h>
  55. #include <stdlib.h>
  56. #include <string.h>
  57. #include "mletest.h"
  58.  
  59.  
  60. //-------Function prototypes--------------------------------------------------
  61. INT main( VOID );
  62. MRESULT EXPENTRY ClientProc( HWND, ULONG, MPARAM, MPARAM );
  63. MRESULT EXPENTRY GetSearchString( HWND, ULONG, MPARAM, MPARAM );
  64. VOID InitializeFontDialog( VOID );
  65. //----------------------------------------------------------------------------
  66.  
  67.  
  68. //-------Global variables-----------------------------------------------------
  69. FONTDLG fd;
  70. CHAR    szCurrentFont[ FACESIZE ];
  71. CHAR    szClass[] = "MLEClass";
  72. HAB     hab;
  73. HWND    hwndFrame, hwndClient, hwndMLE;
  74. //----------------------------------------------------------------------------
  75.  
  76.  
  77. INT main( VOID )
  78. {
  79.    HMQ   hmq;
  80.    QMSG  qmsg;
  81.    ULONG flFrame = FCF_TASKLIST | FCF_TITLEBAR   | FCF_SYSMENU |
  82.                    FCF_MINMAX   | FCF_SIZEBORDER | FCF_MENU    |
  83.                    FCF_SHELLPOSITION;
  84.  
  85.    hab = WinInitialize( 0 );
  86.    hmq = WinCreateMsgQueue( hab, 0 );
  87.  
  88.    WinRegisterClass( hab, szClass, ClientProc, CS_SIZEREDRAW, 0 );
  89.  
  90.    hwndFrame = WinCreateStdWindow( HWND_DESKTOP,
  91.                                    WS_VISIBLE,
  92.                                    &flFrame,
  93.                                    szClass,
  94.                                    "MLE Starter Code",
  95.                                    0,
  96.                                    NULLHANDLE,
  97.                                    ID_RESOURCES,
  98.                                    &hwndClient );
  99.  
  100.    while( WinGetMsg( hab, &qmsg, (HWND) NULL, 0, 0 ))
  101.        WinDispatchMsg( hab, &qmsg );
  102.  
  103.    WinDestroyWindow( hwndFrame );
  104.    WinDestroyMsgQueue( hmq );
  105.    WinTerminate( hab );
  106.  
  107.    return 0;
  108. }
  109.  
  110. MRESULT EXPENTRY ClientProc( HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2 )
  111. {
  112.    RECTL rcl;
  113.    PCREATESTRUCT pCreateStruct;
  114.    SHORT x, y;
  115.  
  116.    switch( msg )
  117.    {
  118.       case WM_CREATE:
  119.  
  120.          //-------------------------------------------------------------------
  121.          // make an MLE
  122.          //-------------------------------------------------------------------
  123.          hwndMLE =
  124.             WinCreateWindow(
  125.                         hwnd,
  126.                         WC_MLE,
  127.                         (PSZ) NULL,
  128.                         MLS_BORDER | MLS_WORDWRAP | MLS_VSCROLL |
  129.                            WS_VISIBLE | WS_TABSTOP,
  130.                         0, 0, 0, 0,      //we'll size this in WM_SIZE
  131.                         hwnd,
  132.                         HWND_TOP,
  133.                         ID_MLE,
  134.                         (PVOID) NULL,
  135.                         (PVOID) NULL );
  136.  
  137.          //-------------------------------------------------------------------
  138.          //set up font dialog now instead of each time it's called...
  139.          //  ... that way changes made to it by user will stay current
  140.          //-------------------------------------------------------------------
  141.          InitializeFontDialog();
  142.  
  143.          WinSetFocus( HWND_DESKTOP, hwndMLE );
  144.  
  145.          return( (MRESULT) 0 );
  146.  
  147.       case WM_SIZE:
  148.          x = (SHORT) SHORT1FROMMP( mp2 );
  149.          y = (SHORT) SHORT2FROMMP( mp2 );
  150.  
  151.          //-------------------------------------------------------------------
  152.          // The MLE will cover the entire client area
  153.          //-------------------------------------------------------------------
  154.          WinSetWindowPos( hwndMLE, HWND_TOP,
  155.                           0, 0, x, y,
  156.                           SWP_SIZE | SWP_MOVE | SWP_SHOW );
  157.  
  158.          break;
  159.  
  160.       case WM_COMMAND:
  161.          switch( SHORT1FROMMP( mp1 ))
  162.          {
  163.             //----------------------------------------------------------------
  164.             // Search for string
  165.             //----------------------------------------------------------------
  166.             case IDM_SEARCH:
  167.             {
  168.                MLE_SEARCHDATA mlesd;
  169.                CHAR szSearchString[ 10 ];
  170.                BOOL bSearch;
  171.  
  172.                bSearch = WinDlgBox( HWND_DESKTOP, hwnd,
  173.                                     GetSearchString,
  174.                                     (HMODULE) NULL,
  175.                                     IDDLG_GETSRCHSTRING,
  176.                                     &szSearchString );
  177.  
  178.                if( bSearch )
  179.                {
  180.                   mlesd.pchFind = szSearchString;
  181.                   mlesd.pchReplace = NULL;         // No find and replace...
  182.                   mlesd.cchFind = 0;               // ...options in this...
  183.                   mlesd.cchReplace = 0;            // ...brain-dead sample.
  184.                   mlesd.iptStart = 0;              // start at beginning
  185.                   mlesd.iptStop = 0xffffffff;      // search entire MLE
  186.  
  187.                   WinSendMsg( hwndMLE, MLM_SEARCH,
  188.                               MPFROMLONG( MLFSEARCH_SELECTMATCH ),
  189.                               &mlesd );
  190.  
  191.                }
  192.  
  193.                break;
  194.             }
  195.  
  196.             //----------------------------------------------------------------
  197.             // Change font
  198.             //----------------------------------------------------------------
  199.             case IDM_CHANGEFONT:
  200.             {
  201.                fd.hpsScreen = WinGetPS( hwnd );
  202.                WinFontDlg( HWND_DESKTOP, hwnd, &fd );
  203.                WinReleasePS( fd.hpsScreen );
  204.  
  205.                WinSendMsg( hwndMLE, MLM_SETFONT, &fd.fAttrs, NULL );
  206.  
  207.                break;
  208.             }
  209.  
  210.             //----------------------------------------------------------------
  211.             // Export to buffer
  212.             //----------------------------------------------------------------
  213.             case IDM_EXPORTTEXT:
  214.             {
  215.                ULONG ulLenMLE, ulOffsetBegin=0;
  216.                PVOID pvBuffer;
  217.  
  218.                WinSendMsg( hwndMLE, MLM_FORMAT, MLFIE_CFTEXT, NULL );
  219.  
  220.                ulLenMLE = (ULONG) WinSendMsg( hwndMLE,
  221.                                               MLM_QUERYFORMATTEXTLENGTH,
  222.                                               0, (MPARAM) 0xffffffff );
  223.  
  224.                if( ulLenMLE > 0 )
  225.                   DosAllocMem( &pvBuffer, ulLenMLE, PAG_READ | PAG_WRITE | PAG_COMMIT );
  226.  
  227.                WinSendMsg( hwndMLE, MLM_SETIMPORTEXPORT,
  228.                            MPFROMP( (PBYTE) pvBuffer ),
  229.                            MPFROMLONG( ulLenMLE ));
  230.  
  231.                WinSendMsg( hwndMLE, MLM_EXPORT,
  232.                            MPFROMP( &ulOffsetBegin ), MPFROMLONG( &ulLenMLE ));
  233.  
  234.                WinMessageBox( HWND_DESKTOP, HWND_DESKTOP,
  235.                               (PSZ) pvBuffer,
  236.                               "First few lines of export buffer:",
  237.                               0L, MB_OK );
  238.  
  239.                break;
  240.             }
  241.  
  242.             //----------------------------------------------------------------
  243.             // Good-bye
  244.             //----------------------------------------------------------------
  245.             case IDM_EXIT:
  246.                WinSendMsg( hwnd, WM_CLOSE, NULL, NULL );
  247.                break;
  248.  
  249.          }
  250.  
  251.          break;
  252.  
  253.    }
  254.    return WinDefWindowProc( hwnd, msg, mp1, mp2 );
  255. }
  256.  
  257.  
  258. //---------------------------------------------------------------------------
  259. // Initialize the fontdlg structure for WinFontDlg
  260. //---------------------------------------------------------------------------
  261. VOID InitializeFontDialog( VOID )
  262. {
  263.    memset( &fd, 0, sizeof( FONTDLG ));
  264.  
  265.    szCurrentFont[0] = '\0';
  266.    fd.cbSize = sizeof( FONTDLG );
  267.  
  268.    fd.fl = FNTS_CENTER | FNTS_INITFROMFATTRS;
  269.  
  270.    strcpy( fd.fAttrs.szFacename, "System Proportional" );
  271.  
  272.    fd.fAttrs.fsType = 0;
  273.    fd.fAttrs.usRecordLength = sizeof( FATTRS );
  274.    fd.fAttrs.fsSelection = 0;
  275.    fd.fAttrs.lMatch = 0;
  276.    fd.fAttrs.idRegistry = 0;
  277.    fd.fAttrs.usCodePage = 850;
  278.    fd.fAttrs.lMaxBaselineExt = 13;
  279.    fd.fAttrs.lAveCharWidth = 8;
  280.    fd.fAttrs.fsFontUse = FATTR_FONTUSE_NOMIX;
  281.  
  282.    fd.fl = FNTS_CENTER | FNTS_INITFROMFATTRS;
  283.    fd.pszFamilyname = szCurrentFont;
  284.    fd.usFamilyBufLen = FACESIZE;
  285.    fd.clrFore = CLR_NEUTRAL;
  286.    fd.clrBack = CLR_DEFAULT;
  287.  
  288.    return;
  289. }
  290.  
  291.  
  292. //---------------------------------------------------------------------------
  293. // Dialog box to get search string
  294. //---------------------------------------------------------------------------
  295. MRESULT EXPENTRY GetSearchString( HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2 )
  296. {
  297.    static PSZ pszSearch;
  298.  
  299.    switch (msg)
  300.    {
  301.       case  WM_INITDLG :
  302.  
  303.          //------------------------------------------------------------------
  304.          // Get search string buffer passed in pCreateParams field
  305.          //------------------------------------------------------------------
  306.          pszSearch = (PSZ) PVOIDFROMMP( mp2 );
  307.  
  308.          //------------------------------------------------------------------
  309.          // 10 character search string
  310.          //------------------------------------------------------------------
  311.          WinSendDlgItemMsg( hwnd, IDD_SEARCHSTRING, EM_SETTEXTLIMIT,
  312.                             MPFROM2SHORT( 11, 0 ),       //+1 for terminator
  313.                             NULL );
  314.  
  315.          WinSetFocus( HWND_DESKTOP,
  316.                       WinWindowFromID( hwnd, IDD_SEARCHSTRING ));
  317.  
  318.          return (MPARAM) TRUE;
  319.  
  320.       case WM_COMMAND :
  321.  
  322.          switch( SHORT1FROMMP( mp1 ))
  323.          {
  324.             //---------------------------------------------------------------
  325.             // Okay - dialog box returns TRUE
  326.             //---------------------------------------------------------------
  327.             case IDD_BTN_OK:
  328.  
  329.                WinQueryDlgItemText( hwnd, IDD_SEARCHSTRING,
  330.                                     11,
  331.                                     pszSearch );
  332.  
  333.                WinDismissDlg( hwnd, TRUE );
  334.                return;
  335.  
  336.             //---------------------------------------------------------------
  337.             // Cancel - dialog box returns FALSE
  338.             //---------------------------------------------------------------
  339.             case IDD_BTN_CANCEL :
  340.  
  341.                WinDismissDlg( hwnd, FALSE );
  342.                return;
  343.          }
  344.  
  345.    }
  346.    return  WinDefDlgProc(hwnd, msg, mp1, mp2);
  347. }
  348.  
  349.  
  350.