home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / sizedlg.zip / SIZEDLG.C next >
C/C++ Source or Header  |  1993-09-03  |  8KB  |  224 lines

  1. /*
  2. *       Hello World
  3. *       Sample Application for OS/2 Presentation Manager
  4. */
  5.  
  6. /*---    Include files    ---*/
  7. #define INCL_WIN
  8. #define INCL_GPI
  9. #include <os2.h>
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include "sizedlg.h"
  13.  
  14. /*---       Defines       ---*/
  15. #define ID_TIMER 1
  16.  
  17. /*--- Function Prototypes ---*/
  18. void cdecl main(void);
  19. MRESULT EXPENTRY MyWindowProc(HWND, ULONG, MPARAM, MPARAM);
  20. MRESULT EXPENTRY SizeDlgProc(HWND, ULONG, MPARAM, MPARAM);
  21.  
  22. /*---  Global Variables  ---*/
  23. HAB hab;                        /* Anchor Block Handle */
  24.  
  25.  
  26. /*---   Main Procedure   ---*/
  27. void cdecl main()
  28.   {
  29.   HMQ hmq;                      /* Message Queue Handle */
  30.   HWND hwndClient;              /* Client Window Handle */
  31.   HWND hwndFrame;               /* Frame Window Handle */
  32.   QMSG msg;                     /* Message from window Queue */
  33.   ULONG flCreate;               /* Window Creation Flags */
  34.  
  35.   /* set up */
  36.   hab = WinInitialize(0);      /* Initialize Presentation Manager */
  37.   hmq = WinCreateMsgQueue (hab, 0); /* Create Message Queue */
  38.  
  39.   /* Register the window procedure  with the system */
  40.   WinRegisterClass(hab,
  41.         "MyWindow",             /* Arbitrary Class Name */
  42.         MyWindowProc,           /* Window Procedure */
  43.         CS_SIZEREDRAW,          /* Redraw when window is sized */
  44.         0);                     /* No window words */
  45.  
  46.   /* Now create the Window */
  47.   flCreate = FCF_TASKLIST |        /* Add Application to task list */
  48.              FCF_TITLEBAR |        /* Put a title bar on window */
  49.              FCF_SIZEBORDER |      /* Add a resizeable border */
  50.              FCF_MINMAX |          /* Add the Minimize / Maximize buttons */
  51.              FCF_MENU |            /* Add the Action-Bar Menu */
  52.              FCF_SYSMENU;          /* Give it a system menu */
  53.  
  54.   hwndFrame = WinCreateStdWindow(
  55.                 HWND_DESKTOP,       /* Handle of parent window */
  56.                 WS_VISIBLE,         /* frame window style  */
  57.                 &flCreate,          /* Frame window creation flags */
  58.                 "MyWindow",         /* Window Class Name */
  59.                 "Base PM Application", /* Text for the Title Bar */
  60.                 0L,                 /* Client window style */
  61.                 (HMODULE) 0,        /* Location of resources (in .exe) */
  62.                 ID_RESOURCE,        /* Frame window and menu resource ID */
  63.                 &hwndClient);       /* address of client window handle */
  64.  
  65.    WinSetWindowPos( hwndFrame,      /* Handle of window being set */
  66.                     HWND_TOP,       /* Z - Placement order (ON TOP) */
  67.                     100, 100,       /* Position of lower left corner */
  68.                     250, 200,       /* width and height */
  69.                     SWP_SIZE | SWP_MOVE |    /* flags telling what to do */
  70.                     SWP_ACTIVATE | SWP_SHOW);
  71.  
  72.   /* Start Processing Messages */
  73.   while (WinGetMsg(hab, &msg, (HWND)0, 0, 0))
  74.     {
  75.     WinDispatchMsg(hab, &msg);    /* send the message on its way */
  76.     }
  77.  
  78.   /* Clean up */
  79.   WinDestroyWindow( hwndFrame);         /* get rid of the frame window */
  80.   WinDestroyMsgQueue(hmq);              /* get rid of message queue */
  81.   WinTerminate(hab);                    /* Pull up anchor */
  82.   }
  83.  
  84. /*---  Window Procedure ---*/
  85. MRESULT EXPENTRY MyWindowProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  86.   {
  87.   HPS hps;                              /* Presentation Space Handle */
  88.   RECTL rcl;                            /* Rectangle Coordinates */
  89.   POINTL ptl;                           /* point coordinates */
  90.   USHORT i;
  91.   USHORT usRet;                          /* return value from Dialog Box */
  92.  
  93.   switch (msg)
  94.     {
  95.     /* Window needs painted */
  96.     case WM_PAINT:
  97.       hps = WinBeginPaint( hwnd, (HPS)0, &rcl );  /* get a PS to paint on */
  98.       WinFillRect(hps, &rcl, CLR_RED);
  99.       WinEndPaint(hps);                        /* release the PS */
  100.       break;
  101.  
  102.    case WM_COMMAND:
  103.      switch (SHORT1FROMMP(mp1))
  104.        {
  105.        case IDM_OPTIONS:
  106.           WinDlgBox(HWND_DESKTOP,       /* Parent Window */
  107.                hwnd,                           /* Owner Window  */
  108.                SizeDlgProc,                   /* Dialog Procedure */
  109.                (HMODULE)0,                     /* Where to find resource */
  110.                IDD_SIZEDLG,                /* Resource ID */
  111.                NULL);              /* Creation parameter */
  112.           return 0L;
  113.         }
  114.       break;
  115.  
  116.  
  117.     /* The system wants you to close */
  118.     case WM_CLOSE:
  119.       WinPostMsg(hwnd, WM_QUIT, 0L, 0L);     /* Shut yourself down */
  120.       break;
  121.  
  122.     /* All System Messages that we don't care about */
  123.     default:
  124.       return(WinDefWindowProc(hwnd, msg, mp1, mp2)); /* Let OS/2 handle them */
  125.     }
  126.   return 0L;
  127.   }
  128.  
  129.  
  130. // This is a magic number derived empirically for positioning an entry
  131. // field.  You would think that you could query an entry field window's
  132. // size, then set the size to the same values without changing it.  Not
  133. // so simple.  You must subtract this value first.  I imagine it has
  134. // something to do with the entry field having a margin.  The documentation
  135. // claims this is related to the font size, but my experiments have shown
  136. // otherwise.  This "Magic Number" works for OS/2 2.0 and 2.1 in CGA, EGA,
  137. // and various VGA resolutions.
  138. #define ENTRYFIELDBORDER 6
  139.  
  140. MRESULT EXPENTRY SizeDlgProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  141.   {
  142.   static BOOL minimized;
  143.  
  144.   switch (msg)
  145.     {
  146.     case WM_INITDLG:
  147.       minimized = FALSE;
  148.       return 0;
  149.  
  150.     // When the dialog box is resized, come here to resize the controls
  151.     // in the way that makes sense.  I grow the MLE in both x and y, grow
  152.     // the entry field only in x, and move the push button.
  153.     case WM_WINDOWPOSCHANGED:
  154.       {
  155.       PSWP pOldSize;
  156.       PSWP pNewSize;
  157.       SHORT dx, dy;
  158.       SWP ctrlpos;
  159.       int i;
  160.       BOOL change;
  161.  
  162.       pNewSize = (PSWP)mp1;
  163.       pOldSize = pNewSize + 1;
  164.       change = TRUE;
  165.       if (pNewSize->fl & SWP_MINIMIZE)
  166.         { // Don't shrink controls if minimizing. The size difference
  167.           // is so great that the controls won't come back to normal
  168.         minimized = TRUE;
  169.         change = FALSE;
  170.         }
  171.       if (pNewSize->fl & SWP_RESTORE)
  172.         { // Don't expand controls if restoring from minimized.
  173.           // (we never shrank them)
  174.         change = !minimized;
  175.         minimized = FALSE;
  176.         }
  177.       if ((pNewSize->fl & SWP_SIZE) && change)
  178.         {
  179.         WinEnableWindowUpdate(hwnd, FALSE);
  180.         dx = pNewSize->cx - pOldSize->cx;
  181.         dy = pNewSize->cy - pOldSize->cy;
  182.         WinQueryWindowPos(WinWindowFromID(hwnd, MLID_STDOUT), &ctrlpos);
  183.         ctrlpos.cx += dx;
  184.         ctrlpos.cy += dy;
  185.         WinSetWindowPos(WinWindowFromID(hwnd, MLID_STDOUT), (HWND)0,
  186.             0, 0, ctrlpos.cx, ctrlpos.cy, SWP_SIZE);
  187.         WinQueryWindowPos(WinWindowFromID(hwnd, EFID_STDIN), &ctrlpos);
  188.         ctrlpos.cx += dx - ENTRYFIELDBORDER;
  189.         ctrlpos.cy -= ENTRYFIELDBORDER;
  190.         WinSetWindowPos(WinWindowFromID(hwnd, EFID_STDIN), (HWND)0,
  191.           0, 0, ctrlpos.cx, ctrlpos.cy, SWP_SIZE);
  192.         WinQueryWindowPos(WinWindowFromID(hwnd, BID_ENTER), &ctrlpos);
  193.         ctrlpos.x += dx;
  194.         WinSetWindowPos(WinWindowFromID(hwnd, BID_ENTER), (HWND)0,
  195.           ctrlpos.x, ctrlpos.y, 0, 0, SWP_MOVE);
  196.         WinEnableWindowUpdate(hwnd, TRUE);
  197.         }
  198.       break;
  199.       }
  200.     // Process WM_QUERYTRACKINFO to limit the size of the dialog box.
  201.     // This prevents the user from shrinking me down to an un-useable
  202.     // size.  I picked the minimum size arbitrarily.
  203.     case WM_QUERYTRACKINFO:
  204.       {
  205.       MRESULT mr;
  206.       TRACKINFO *pti;
  207.       SWP boxpos;
  208.  
  209.       mr = WinDefDlgProc(hwnd, msg, mp1, mp2);
  210.       pti = (TRACKINFO *)mp2;
  211.       pti->ptlMinTrackSize.x = 260L;
  212.       pti->ptlMinTrackSize.y = 130L;
  213.       return mr;
  214.       }
  215.  
  216.  
  217.      /* Process a message from one of the Push Buttons */
  218.     case WM_COMMAND:
  219.       WinDismissDlg(hwnd, 0);   /* All done */
  220.       break;
  221.     }
  222.   return WinDefDlgProc(hwnd, msg, mp1, mp2); /* let OS/2 handle the rest */
  223.   }
  224.