Metropoli BBS
VIEWER: mle3.pas MODE: TEXT (CP437)
{█▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀█}
{█                                                       █}
{█      Virtual Pascal Examples. Version 1.10            █}
{█        MLE with clipboard example                     █}
{█        The Art Of OS/2, Chapter 18, p. 294-300        █}
{█      ─────────────────────────────────────────────────█}
{█      Copyright (c) 1992-1996 by Larry Salomon, Jr.    █}
{█      VP/2 Version Copyright (C) 1996 fPrint UK Ltd    █}
{█                                                       █}
{▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀}

Program MLE3;

{$PMTYPE PM}
{$R *.RES}
{$D MLE Sample 3 - Copyright (c) 1994-1995 by Larry Salomon, Jr.}

Uses
  Os2Def, Os2Base, Os2PmApi, Strings, VPUtils;

{$IFDEF VPDEMO}
  {&Dynamic VP11Demo.Lib}
{$ENDIF}

Const
  RES_CLIENT     = 256;
  WND_MLE        = 257;

  M_SAMPLE       = 320;
  MI_IMPORTTEXT  = 321;
  MI_EXPORTTEXT  = 322;
  MI_EXIT        = 323;

   CLS_CLIENT     = 'MLE3SampleClass';

  MYM_SETFOCUS   = (WM_USER);

Type
  ptInstData = ^tInstData;
  tInstData = Record
    ulSzStruct  : ULONG;           { Size of the structure }
    habAnchor   : HAB;             { Anchor block handle }
    hwndFrame   : HWND;            { Frame window handle }
    hwndMle     : HWND;            { MLE window handle }
  end;

Procedure importText(hwndMle : HWND );
{--------------------------------------------------------------- }
{ This function imports text to the MLE.                         }
{                                                                }
{ Input:  hwndMle - MLE window handle                            }
{--------------------------------------------------------------- }
Var
  pfImport  : Text;
  achImpExp : String;
  iInsert   : IPT;

begin

   {------------------------------------------------------------ }
   { Set the import/export buffer and the format to }
   { MLFIE_NOTRANS.  Remember that, internally, a '\n' }
   { character is a LF only.  Only when it gets output to a }
   { 'text-mode' stream does it get converted to CR-LF. }
   {------------------------------------------------------------ }
   WinSendMsg(hwndMle,
              MLM_SETIMPORTEXPORT,
              MPFROMP(@achImpExp[1]),
              MPFROMLONG(sizeof(achImpExp)));
   WinSendMsg(hwndMle,
              MLM_FORMAT,
              MPFROMLONG(MLFIE_NOTRANS),
              0);

   FileMode := open_access_readonly;
   Assign( pfImport, 'MLE3.IMP' );
   {$I-}
   Reset( pfImport );
   {$I+}
   If IOResult <> 0 then
     begin
       WinAlarm(HWND_DESKTOP,WA_ERROR);
       exit;
     end;

   WinEnableWindowUpdate(hwndMle,FALSE);

   iInsert := 0;

   While not eof( pfImport ) do
     begin
       Readln( pfImport, achImpExp );
       achImpExp := achImpExp+#10;

       WinSendMsg(hwndMle,
                  MLM_IMPORT,
                  mpfromp(@iInsert),
                  mpfromlong(Length(achImpExp)) );
     end;

   WinEnableWindowUpdate(hwndMle,True);

   Close( pfImport );
end; { addText }

Procedure exportText( hwndMle : HWND );
{--------------------------------------------------------------- }
{ This function exports text from the MLE.                       }
{                                                                }
{ Input:  hwndMle - MLE window handle                            }
{--------------------------------------------------------------- }
Var
  pfExport          : Text;
  achImpExp         : Array[0..255] of Char;
  lNumLines         : LONG;
  lIndex            : LONG;
  iBegin            : IPT;
  lSzLine           : LONG;
begin

  {------------------------------------------------------------ }
  { Set the import/export buffer and the format to              }
  { MLFIE_NOTRANS.  Remember that, internally, a '\n'           }
  { character is a LF only.  Only when it gets output to a      }
  { 'text-mode' stream does it get converted to CR-LF.          }
  {------------------------------------------------------------ }
  WinSendMsg(hwndMle,
             MLM_SETIMPORTEXPORT,
             MPFROMP(@achImpExp),
             MPFROMLONG(sizeof(achImpExp)));
  WinSendMsg(hwndMle,
             MLM_FORMAT,
             MPFROMLONG(MLFIE_NOTRANS),
             0);

  FileMode := open_access_ReadWrite;
  Assign( pfExport, 'MLE3.EXP' );
  {$I-}
  ReWrite( pfExport );
  {$I+}
  If IOResult <> 0 then
    begin
      WinAlarm(HWND_DESKTOP,WA_ERROR);
      exit;
    end;

  WinEnableWindowUpdate(hwndMle,FALSE);

  lNumLines := LONGFROMMR(WinSendMsg(hwndMle,
                                     MLM_QUERYLINECOUNT,
                                     0,
                                     0));

  for lIndex :=0 to lNumLines-1 do
    begin
     iBegin := LONGFROMMR(WinSendMsg(hwndMle,
                                  MLM_CHARFROMLINE,
                                  MPFROMLONG(lIndex),
                                  0));
     lSzLine := LONGFROMMR(WinSendMsg(hwndMle,
                                   MLM_QUERYLINELENGTH,
                                   MPFROMLONG(iBegin),
                                   0));

     fillchar(achImpExp,sizeof(achImpExp),0);

     WinSendMsg(hwndMle,
                MLM_EXPORT,
                MPFROMP(@iBegin),
                MPFROMP(@lSzLine));

     Write( pfExport, strpas( achImpExp ));
  end;

  WinEnableWindowUpdate(hwndMle,TRUE);

  close(pfExport);
end; { ExportText }

Function ClientWndProc ( hwndWnd : HWND ;
                         ulMsg   : ULONG;
                         mpParm1 : MPARAM;
                         mpParm2 : MPARAM ) : mResult; cdecl;
{--------------------------------------------------------------- }
{ Client window procedure.                                       }
{                                                                }
{ Input, Output, Returns:  message-specific                      }
{--------------------------------------------------------------- }
Var
  pidData   : ptINSTDATA;
  bReadOnly : Bool;
  hwndMenu  : Hwnd;

begin
  ClientWndProc := ord(False);
  pidData := WinQueryWindowPtr(hwndWnd,0);

  Case ulMsg of
    WM_CREATE:
      begin
        {--------------------------------------------------------- }
        { Allocate memory for the instance data.                   }
        {--------------------------------------------------------- }
        New( pidData );
        if pidData = nil then
          begin
            WinAlarm(HWND_DESKTOP,WA_ERROR);
            ClientWndProc := ord(TRUE);
            Exit;
          end;

        {--------------------------------------------------------- }
        { Initialize the instance data. }
        {--------------------------------------------------------- }
        pidData^.ulSzStruct := sizeof( tINSTDATA );

        WinSetWindowPtr( hwndWnd, 0, pidData );

        pidData^.habAnchor := WinQueryAnchorBlock(hwndWnd);
        pidData^.hwndFrame := WinQueryWindow(hwndWnd,QW_PARENT);

        {--------------------------------------------------------- }
        { Create the MLE. }
        {--------------------------------------------------------- }
        pidData^.hwndMle := WinCreateWindow(hwndWnd,
                                            WC_MLE,
                                            '',
                                            WS_VISIBLE  OR
                                            MLS_HSCROLL  OR
                                            MLS_VSCROLL  OR
                                            MLS_BORDER,
                                            0,
                                            0,
                                            0,
                                            0,
                                            hwndWnd,
                                            HWND_TOP,
                                            WND_MLE,
                                            nil,
                                            nil);
        if (pidData^.hwndMle=NULLHANDLE) then
          begin
            WinAlarm(HWND_DESKTOP,WA_ERROR);
            Dispose(pidData);
            ClientWndProc := ord(TRUE);
            Exit;
          end;
      end; { WM_CREATE }

    WM_DESTROY:
      Dispose( pidData );

    WM_SIZE:
      {--------------------------------------------------------- }
      { Resize the MLE according to our new dimensions.          }
      {--------------------------------------------------------- }
      WinSetWindowPos(pidData^.hwndMle,
                      NULLHANDLE,
                      0,
                      0,
                      SHORT1FROMMP(mpParm2),
                      SHORT2FROMMP(mpParm2),
                      SWP_SIZE);

    WM_SETFOCUS:
      {--------------------------------------------------------- }
      { If we are getting the focus, post ourselves a message    }
      { to change it to the MLE.  We may not do it now,          }
      { because PM is still in the focus change processing,      }
      { meaning that any call to WinSetFocus() will get          }
      { overwritten when the focus change processing completes.  }
      {--------------------------------------------------------- }
      if SHORT1FROMMP(mpParm2) <> 0 then
        WinPostMsg(hwndWnd,MYM_SETFOCUS,0,0);

    MYM_SETFOCUS:
      WinSetFocus(HWND_DESKTOP,pidData^.hwndMle);

    WM_COMMAND:
      Case SHORT1FROMMP(mpParm1) of
        MI_IMPORTTEXT:
          importText(pidData^.hwndMle);

        MI_EXPORTTEXT:
          exportText(pidData^.hwndMle);

       MI_EXIT:
         WinPostMsg(hwndWnd,WM_CLOSE,0,0);

       else
         ClientWndProc := WinDefWindowProc(hwndWnd,ulMsg,mpParm1,mpParm2);
     end; { WM_COMMAND }
   else
     ClientWndProc := WinDefWindowProc(hwndWnd,ulMsg,mpParm1,mpParm2);
   end; { Case ulMsg }
end;

{--------------------------------------------------------------- }
{ Standard main function for PM applications.                    }
{--------------------------------------------------------------- }

Var
  habAnchor       : HAB;
  hmqQueue        : HMQ;
  ulCreate        : ULONG;
  hwndFrame       : HWND;
  hwndClient      : HWND;
  bLoop           : BOOL;
  qmMsg           : QMSG;

begin
  habAnchor := WinInitialize(0);
  hmqQueue := WinCreateMsgQueue(habAnchor, 0);

  WinRegisterClass(habAnchor,
                   CLS_CLIENT,
                   ClientWndProc,
                   CS_SIZEREDRAW,
                   Sizeof(Pointer));

  ulCreate := FCF_SYSMENU OR FCF_TITLEBAR OR FCF_MINMAX OR
              FCF_SIZEBORDER OR FCF_MENU OR FCF_SHELLPOSITION OR
              FCF_TASKLIST;

  hwndFrame := WinCreateStdWindow(HWND_DESKTOP,
                                  WS_VISIBLE,
                                  ulCreate,
                                  CLS_CLIENT,
                                  'MLE Sample 3',
                                  0,
                                  NULLHANDLE,
                                  RES_CLIENT,
                                  @hwndClient);

  if hwndFrame <> NULLHANDLE then
    begin
      bLoop := WinGetMsg(habAnchor, qmMsg, NULLHANDLE, 0, 0);
      while bLoop do
        begin
          WinDispatchMsg(habAnchor, qmMsg);
          bLoop := WinGetMsg(habAnchor, qmMsg, NULLHANDLE, 0, 0);
        end;
      WinDestroyWindow(hwndFrame);
    end;
  WinDestroyMsgQueue(hmqQueue);
  WinTerminate(habAnchor);
end.


[ RETURN TO DIRECTORY ]