home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / S12715.ZIP / BASE.C < prev    next >
C/C++ Source or Header  |  1990-09-27  |  10KB  |  266 lines

  1. /**************************************************************************\
  2.  *                                                                        *
  3.  * Base.c                                                                 *
  4.  *                                                                        *
  5.  * This application does the following:                                   *
  6.  *                                                                        *
  7.  *      1. Puts up the original dialog box without resizing and           *
  8.  *         repositioning it when it is called the first time.             *
  9.  *                                                                        *   
  10.  *         It also puts up a full screen dialog box the second time it    *
  11.  *         is called and thereafter.                                      * 
  12.  *                                                                        *
  13.  *      2. Allocates heap for the list box control in the dialog box the  *
  14.  *          first time it is called and reuse it for subsequent calls.    *
  15.  *                                                                        *
  16.  * Author: Petrus Wong                                                    *
  17.  *                                                                        *
  18.  * History: created 8-16-90                                               *
  19.  *                                                                        *
  20.  **************************************************************************/
  21. #define INCL_WIN
  22. #define INCL_GPI
  23.  
  24. #define INCL_SUB
  25. #define INCL_DOS
  26. #define INCL_BASE
  27. #define INCL_DOSERRORS
  28.  
  29.  
  30. #include <os2.h>
  31. #include "base.h"
  32.  
  33. MRESULT EXPENTRY ClientWndProc (HWND, USHORT, MPARAM, MPARAM) ;
  34. MRESULT EXPENTRY DlgProc (HWND, USHORT, MPARAM, MPARAM);
  35.  
  36. SEL   sel;
  37. HHEAP hHeap;
  38. HWND  hwndFrame, hwndClient;
  39. HWND hwndList ;
  40. static BOOL  fFirstCall = TRUE;         // Flags if the dialog box is 
  41.                                         // called the first time.
  42.  
  43. int main (void)
  44.      {
  45.      static CHAR  szClientClass [] = "Base" ;
  46.      static ULONG flFrameFlags = FCF_SIZEBORDER | FCF_MENU | FCF_SYSMENU |
  47.                                  FCF_TASKLIST | FCF_TITLEBAR |
  48.                                  FCF_MINMAX | FCF_SHELLPOSITION;
  49.  
  50.      HAB          hab ;
  51.      HMQ          hmq ;
  52.      QMSG         qmsg ;
  53.  
  54.  
  55.      hab = WinInitialize (0) ;
  56.      hmq = WinCreateMsgQueue (hab, 0) ;
  57.  
  58.      WinRegisterClass (
  59.                     hab,                // Anchor block handle.
  60.                     szClientClass,      // Name of class being registered.
  61.                     ClientWndProc,      // Window procedure for class.
  62.                     CS_SIZEREDRAW,      // Class style.
  63.                     0) ;                // Extra bytes to reserve.
  64.  
  65.      hwndFrame = WinCreateStdWindow (
  66.                     HWND_DESKTOP,       // Parent window handle.
  67.                     WS_VISIBLE,         // Style of frame window.
  68.                     &flFrameFlags,      // Pointer to control data.
  69.                     szClientClass,      // Client window class name.
  70.                     NULL,               // Title bar text.
  71.                     0L,                 // Style of client window.
  72.                     NULL,               // Module handle for resources.
  73.                     2,                  // ID of resources.
  74.                     &hwndClient) ;      // Pointer to client window handle.
  75.  
  76.  
  77.      while (WinGetMsg (hab, &qmsg, NULL, 0, 0))
  78.           WinDispatchMsg (hab, &qmsg) ;
  79.  
  80.      WinDestroyWindow(hwndList) ;
  81.      WinDestroyHeap(hHeap) ;
  82.      WinDestroyWindow (hwndFrame) ;
  83.      WinDestroyMsgQueue (hmq) ;
  84.      WinTerminate (hab) ;
  85.      return 0 ;
  86.      }
  87.  
  88. MRESULT EXPENTRY ClientWndProc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
  89.      {
  90.      HPS          hps;
  91.      RECTL        rect;
  92.  
  93.      switch (msg)
  94.           {
  95.           case WM_CREATE: {
  96.                register HWND   hwndDlg;
  97.                register USHORT wResult;
  98.  
  99.                /* Puts up the original dialog box for the first time. */
  100.                /*      No resizing and repositioning.                 */
  101.                if (!(hwndDlg = WinLoadDlg( HWND_DESKTOP, hwnd,
  102.                                            DlgProc, NULL, ID_DIALOG, NULL)))
  103.                   {
  104.                    /* Cannot create the dialog box, sound the alarm.  */
  105.                    DosBeep(375, 1000);
  106.                    DosBeep(375, 100);
  107.                    DosBeep(375, 1000);
  108.                   }
  109.                wResult = WinProcessDlg(hwndDlg) ;
  110.  
  111.                if (hwndDlg != NULL)
  112.                   WinDestroyWindow(hwndDlg) ;
  113.  
  114.                fFirstCall = FALSE ;              // Reset the flag to signal
  115.                                                  //  it has been called the
  116.                                                  //  first time.
  117.  
  118.                break ;
  119.                }
  120.  
  121.           case WM_PAINT:
  122.  
  123.                hps = WinBeginPaint (hwnd, NULL, NULL) ;
  124.                WinQueryWindowRect (hwnd, &rect);
  125.                WinFillRect (hps, &rect, CLR_WHITE);
  126.                WinEndPaint (hps) ;
  127.                return 0 ;
  128.  
  129.           case WM_COMMAND:
  130.                switch (COMMANDMSG(&msg)->cmd)
  131.                {
  132.                case IDM_BEEPHIGH: {
  133.                     register HWND   hwndDlg;
  134.                     register USHORT wResult;
  135.  
  136.                     if (!(hwndDlg = WinLoadDlg( HWND_DESKTOP, hwnd,
  137.                                            DlgProc, NULL, ID_DIALOG, NULL)))
  138.                       {
  139.                         /* Cannot create the dialog box, sound the alarm.  */
  140.                         DosBeep(375, 1000);
  141.                         DosBeep(375, 100);
  142.                         DosBeep(375, 1000);
  143.                       }
  144.                     wResult = WinProcessDlg(hwndDlg) ;
  145.  
  146.                     if (hwndDlg != NULL)
  147.                       WinDestroyWindow(hwndDlg) ;
  148.  
  149.                     return (wResult) ;
  150.                     }
  151.  
  152.                case IDM_BEEPLOW:
  153.                       DosBeep (400, 175);
  154.                       return 0;
  155.                }
  156.                break;
  157.  
  158.           case WM_DESTROY:
  159.              /*  DosExit (EXIT_PROCESS, 0); */
  160.                return 0;
  161.  
  162.           }
  163.      return WinDefWindowProc (hwnd, msg, mp1, mp2) ;
  164.      }
  165.  
  166. MRESULT EXPENTRY DlgProc (HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
  167.      {
  168.      static USHORT flag = 0;
  169.      HWND flaghwnd;
  170.  
  171.      switch (msg)
  172.           {
  173.           case WM_INITDLG: {
  174.                SWP swp;
  175.                LONG cx, cy ;
  176.  
  177.                if (!fFirstCall)     // Puts up a full screen sized dialog
  178.                                     // box.
  179.                      {
  180.                         /* Finds out the size and position of the list box.
  181.                          */
  182.                         WinQueryWindowPos(WinWindowFromID (hwnd, IDD_LIST),
  183.                                           &swp);
  184.  
  185.                         /* Finds out the size of the screen. */
  186.                         cx = WinQuerySysValue(HWND_DESKTOP, SV_CXSCREEN);
  187.                         cy = WinQuerySysValue(HWND_DESKTOP, SV_CYSCREEN);
  188.  
  189.                         /* We want to reuse the old list box so trash this
  190.                            one.                                            */
  191.                         WinDestroyWindow (WinWindowFromID (hwnd, IDD_LIST));
  192.  
  193.                         /* Revive the old list box.                        */
  194.                         WinSetOwner(hwndList, hwnd);
  195.                         WinSetParent(hwndList, hwnd, FALSE);
  196.  
  197.                         /* Position and resize the dialog box to that of
  198.                            the screen.
  199.                          */
  200.                         WinSetWindowPos(hwnd, HWND_TOP,
  201.                                         0, 0, cx, cy,
  202.                                         SWP_SIZE | SWP_MOVE | SWP_SHOW );
  203.  
  204.                         /* Position the list box item. */
  205.                         WinSetWindowPos(hwndList, HWND_TOP,
  206.                                         swp.x, swp.y, swp.cx, swp.cy,
  207.                                         SWP_MOVE | SWP_SHOW );
  208.  
  209.                         WinSendMsg(WinWindowFromID(hwnd, IDD_LIST),
  210.                                LM_INSERTITEM,
  211.                                MPFROMSHORT ((SHORT) LIT_END),
  212.                                MPFROMP ((PSZ) "DEF"));
  213.  
  214.                       }
  215.                   else {            // Shows the original sized dialog box.
  216.                     WinSendMsg(WinWindowFromID(hwnd, IDD_LIST),
  217.                                LM_INSERTITEM,
  218.                                MPFROMSHORT ((SHORT) LIT_END),
  219.                                MPFROMP ((PSZ) "ABC"));
  220.                        }
  221.                return 0 ;
  222.                }
  223.  
  224.           case WM_COMMAND:
  225.                switch (SHORT1FROMMP (mp1))
  226.                {
  227.                case IDD_OK:
  228.  
  229.                       hwndList = WinWindowFromID (hwnd, IDD_LIST);
  230.  
  231.                       /* Save the list box for future use, then destroy
  232.                          the dialog box.                                */
  233.                       WinSetParent(hwndList, HWND_OBJECT, FALSE) ;
  234.                       WinSetOwner(hwndList, NULL);
  235.                       WinDismissDlg(hwnd, TRUE);
  236.                       fFirstCall = FALSE ;
  237.                       return 0;
  238.                }
  239.                break ;
  240.  
  241.           case WM_CONTROLHEAP:
  242.               /* Take care of the heap allocation for the list box 
  243.                   ourselves. */
  244.               /* The same heap is reused, ie. only created the first time. */
  245.  
  246.               flaghwnd = HWNDFROMMP (mp2);
  247.               if ((flaghwnd == WinWindowFromID(hwnd, IDD_LIST)) &&
  248.                   (fFirstCall))
  249.               {
  250.                if (!DosAllocSeg ((60 * 1024), &sel, 0))
  251.                {
  252.                   hHeap = WinCreateHeap ( sel, (40 * 1024),
  253.                                                 0, 0, 128, 0);
  254.  
  255.                   if (hHeap)
  256.                   {
  257.                      return (hHeap);
  258.                   }
  259.                }
  260.                break;
  261.               }
  262.               break;
  263.           }
  264.      return WinDefDlgProc (hwnd, msg, mp1, mp2) ;
  265.      }
  266.