home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / os2sdk / os2sdk12 / mdi / app.c next >
Encoding:
C/C++ Source or Header  |  1990-06-06  |  8.3 KB  |  308 lines

  1. /***************************************************************************\
  2. * app.c - MDI Sample application
  3. *
  4. * Created by Microsoft Corporation, 1989
  5. *
  6. \***************************************************************************/
  7.  
  8. #define INCL_WINSYS
  9. #define INCL_WINCOMMON
  10. #define INCL_WINMESSAGEMGR
  11. #define INCL_WINPOINTERS
  12. #define INCL_WININPUT
  13. #define INCL_WINMENUS
  14. #define INCL_WINFRAMEMGR
  15. #define INCL_WINWINDOWMGR
  16. #define INCL_WINRECTANGLES
  17. #define INCL_WINHEAP
  18. #define INCL_WINSCROLLBARS
  19. #define INCL_GPIPRIMITIVES
  20.  
  21. #include <os2.h>
  22. #include "app.h"
  23. #include "appdata.h"
  24. #include "mdi.h"
  25. #include "mdidata.h"
  26.  
  27.  
  28. /*
  29.     Function prototypes
  30. */
  31. BOOL AppInit(VOID);
  32. BOOL MDIInit(VOID);
  33. VOID AppTerminate(VOID);
  34. VOID MDITerminate(VOID);
  35. BOOL AppNewDocument(USHORT, PSZ);
  36. VOID TrackSplitbars(HWND, USHORT, SHORT, SHORT);
  37. VOID MDIDesktopSize(HWND, MPARAM, MPARAM);
  38. VOID MDIDesktopSetFocus(HWND, MPARAM); 
  39. VOID MDIDesktopActivateDoc(SHORT idMenuitem);
  40. BOOL AppNewDocument(USHORT, PSZ);
  41. NPDOC MDINewDocument(USHORT fsStyle, PSZ pszClassName);
  42. VOID MDISetInitialDocPos(HWND hwndNewFrame);
  43.  
  44. VOID AddToWindowMenu(NPDOC);
  45.  
  46.  
  47.  
  48. int cdecl main(void)
  49. {
  50.     QMSG qmsg;
  51.     /*
  52.      * Initialize the application globals
  53.      * and create the main window.
  54.      */
  55.     if (AppInit() == FALSE) {
  56.         WinAlarm(HWND_DESKTOP, WA_ERROR);
  57.         return(0);
  58.     }
  59.  
  60.     /*
  61.      * Initialize the MDI globals etc..
  62.      */
  63.     if (MDIInit() == FALSE) {
  64.         WinAlarm(HWND_DESKTOP, WA_ERROR);
  65.         WinAlarm(HWND_DESKTOP, WA_ERROR);
  66.         return(0);
  67.     }
  68.  
  69.     /*
  70.      * Create an initial, untitled document.
  71.      */
  72.     AppNewDocument(DS_HORZSPLITBAR | DS_VERTSPLITBAR, szDocClass);
  73.  
  74.     while (WinGetMsg(NULL, (PQMSG)&qmsg, NULL, 0, 0)) {
  75.         WinDispatchMsg(NULL, (PQMSG)&qmsg);
  76.     }
  77.  
  78.     /*
  79.      * Do the clean-up of the MDI code.
  80.      */
  81.     MDITerminate();
  82.  
  83.     /*
  84.      * Do the clean-up of the Application.
  85.      */
  86.     AppTerminate();
  87.  
  88.     DosExit(EXIT_PROCESS, 0);
  89. }
  90.  
  91.  
  92. MRESULT EXPENTRY MDIWndProc(HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
  93. {
  94.     HPS hps;
  95.     RECTL rclPaint, rclWindow;
  96.     POINTL ptlPatternRef;
  97.  
  98.     switch (msg) {
  99.  
  100.     case WM_PAINT:
  101.         hps = WinBeginPaint(hwnd, (HPS)NULL, &rclPaint);
  102.  
  103.         /*
  104.          * Set the pattern to be at the top-left
  105.          * since we're top-left aligning the bits.
  106.          */
  107.         WinQueryWindowRect(hwnd, (PRECTL)&rclWindow);
  108.         ptlPatternRef.x = rclWindow.xLeft;
  109.         ptlPatternRef.y = rclWindow.yTop;
  110.         GpiSetPatternRefPoint(hps, &ptlPatternRef);
  111.  
  112.         WinFillRect(hps, &rclPaint, SYSCLR_APPWORKSPACE);
  113.  
  114.         WinEndPaint(hps);
  115.         break;
  116.  
  117. #if 0
  118.     case WM_SIZE:
  119.  
  120.         /* HACK -- only reposition the windows if it is not going to or coming
  121.         from a minimized position, it would be better to what
  122.         WM_WINDOWPOSCHANGED and pay attention to the fs fields of the SWP
  123.         structure */
  124.  
  125.         if ( SHORT1FROMMP(mp1) && SHORT2FROMMP(mp1) &&
  126.              SHORT1FROMMP(mp2) && SHORT2FROMMP(mp2) ) {
  127.             MDIDesktopSize ( hwnd, mp1, mp2 );
  128.         }
  129.     break;
  130.  
  131. #else
  132.     case WM_SIZE:
  133.         MDIDesktopSize ( hwnd, mp1, mp2 );
  134.         break;
  135. #endif
  136.  
  137.     case WM_SETFOCUS:
  138.         MDIDesktopSetFocus(hwnd, mp2);
  139.         break;
  140.  
  141.     case WM_COMMAND:
  142.         switch (SHORT1FROMMP(mp1)) {
  143.  
  144.         /*
  145.          * Pass these accelerators onto the active document's
  146.          * frame so it can process it.
  147.          *
  148.          * These are the CMD_ values from the document system
  149.          * menu.
  150.          */
  151.         case CMD_DOCRESTORE:
  152.             WinSendMsg(hwndActiveDoc, WM_SYSCOMMAND, (MPARAM)SC_RESTORE, mp2);
  153.             break;
  154.  
  155.         case CMD_DOCNEXT:
  156.             WinSendMsg(hwndActiveDoc, WM_SYSCOMMAND, (MPARAM)SC_NEXT, mp2);
  157.             break;
  158.  
  159.         case CMD_DOCMINIMIZE:
  160.             WinSendMsg(hwndActiveDoc, WM_SYSCOMMAND, (MPARAM)SC_MINIMIZE, mp2);
  161.             break;
  162.  
  163.         case CMD_DOCCLOSE:
  164.             WinSendMsg(hwndActiveDoc, WM_SYSCOMMAND, (MPARAM)SC_CLOSE, mp2);
  165.             break;
  166.  
  167.         case CMD_DOCSPLIT:
  168.             /*
  169.              * Call TrackSplitbars() with -1 for xMouse to tell
  170.              * it to reposition the pointer to where the
  171.              * splitbars currently are.
  172.              */
  173.             WinSetPointer(HWND_DESKTOP, hptrHVSplit);
  174.             TrackSplitbars(WinWindowFromID(hwndActiveDoc, FID_CLIENT),
  175.                     SPS_VERT | SPS_HORZ, -1, -1);
  176.             WinSetPointer(HWND_DESKTOP, hptrArrow);
  177.             break;
  178.  
  179.         case CMD_NEW:
  180.             if (AppNewDocument(DS_HORZSPLITBAR | DS_VERTSPLITBAR, szDocClass) == FALSE)
  181.                 WinAlarm(HWND_DESKTOP, WA_ERROR);
  182.             break;
  183.  
  184.         case CMD_CLOSE:
  185.             /*
  186.              * Close the active document.
  187.              */
  188.             if (hwndActiveDoc)
  189.                 WinSendMsg(hwndActiveDoc, WM_CLOSE, 0L, 0L);
  190.             break;
  191.  
  192.     case CMD_ABOUT:
  193.         /*
  194.          * Put up the About... dialog box
  195.          */
  196.         WinDlgBox(HWND_DESKTOP, hwnd, AboutDlgProc, NULL, IDD_ABOUT, NULL);
  197.         break;
  198.     
  199.         case CMD_EXIT:
  200.             WinPostMsg(hwnd, WM_QUIT, 0L, 0L);
  201.             break;
  202.  
  203.         case CMD_ARRANGETILED:
  204.             ArrangeWindows(AWP_TILED);
  205.             break;
  206.  
  207.         case CMD_ARRANGECASCADED:
  208.             ArrangeWindows(AWP_CASCADED);
  209.             break;
  210.  
  211.         default:
  212.             /*
  213.              * The means a window title was selected from
  214.              * the window menu.  Have the MDI code activate
  215.              * the correct window based on the menuitem ID.
  216.              *
  217.              * WARNING: Be sure to keep you applications
  218.              * menuitem IDs < CMD_WINDOWITEMS.
  219.              */
  220.  
  221.             /* MULTIPLEMENU */
  222.             /*  Also in here we need to pass document unique WM_COMMAND
  223.                 messages on down to the document's client procs */
  224.  
  225.             if (SHORT1FROMMP(mp1) >= CMD_WINDOWITEMS)
  226.                 MDIDesktopActivateDoc(SHORT1FROMMP(mp1));
  227.             break;
  228.         }
  229.         break;
  230.  
  231.     default:
  232.         return(WinDefWindowProc(hwnd, msg, mp1, mp2));
  233.         break;
  234.     }
  235.  
  236.     return (0L);
  237. }
  238.  
  239.  
  240. BOOL AppNewDocument(USHORT fsStyle, PSZ pszClassName)
  241. {
  242.     register NPDOC npdocNew;
  243.     HWND hwndFrame, hwndClient;
  244.     HWND hwndHScroll, hwndVScroll;
  245.  
  246.     npdocNew = MDINewDocument(fsStyle, pszClassName);
  247.  
  248.     npdocNew->clrBackground = clrNext++;
  249.     if (clrNext > CLR_PALEGRAY)
  250.         clrNext = CLR_BACKGROUND;
  251.  
  252.     hwndFrame = npdocNew->hwndFrame;
  253.     hwndClient = WinWindowFromID(hwndFrame, FID_CLIENT);
  254.  
  255.     /*
  256.      * Setup the scrollbars.
  257.      */
  258.     hwndHScroll = WinWindowFromID(hwndFrame, FID_HORZSCROLL);
  259.     WinSendMsg(hwndHScroll, SBM_SETSCROLLBAR, MPFROMSHORT(0),
  260.             MPFROM2SHORT(0, 600));
  261.     hwndHScroll = WinWindowFromID(hwndFrame, ID_HORZSCROLL2);
  262.     WinSendMsg(hwndHScroll, SBM_SETSCROLLBAR, MPFROMSHORT(0),
  263.             MPFROM2SHORT(0, 600));
  264.  
  265.     hwndVScroll = WinWindowFromID(hwndFrame, FID_VERTSCROLL);
  266.     WinSendMsg(hwndVScroll, SBM_SETSCROLLBAR, MPFROMSHORT(0),
  267.             MPFROM2SHORT(0, 600));
  268.     hwndVScroll = WinWindowFromID(hwndFrame, ID_VERTSCROLL2);
  269.     WinSendMsg(hwndVScroll, SBM_SETSCROLLBAR, MPFROMSHORT(0),
  270.             MPFROM2SHORT(0, 600));
  271.  
  272.     /*
  273.      * Set the focus the client so the new window will be
  274.      * active when we show it.
  275.      */
  276.     WinSetFocus(HWND_DESKTOP, hwndClient);
  277.  
  278.     AddToWindowMenu(npdocNew);    /* Moved here from end of
  279.                                   MdiNewDocument routine so that the doc has
  280.                                   been activated, and therefore the main
  281.                                   window has a menu before attempting to add
  282.                                   the doc to the main window's menu */
  283.  
  284.  
  285.     /*
  286.      * Set the initial position of the frame window and make it visible.
  287.      */
  288.     MDISetInitialDocPos(hwndFrame);
  289.  
  290.     return (TRUE);
  291. }
  292.  
  293. MRESULT EXPENTRY AboutDlgProc(HWND hDlg, USHORT msg, MPARAM mp1, MPARAM mp2)
  294. /*
  295.     About... dialog procedure
  296. */
  297. {
  298.     switch(msg) {
  299.     case WM_COMMAND:
  300.         switch(COMMANDMSG(&msg)->cmd) {
  301.         case DID_OK: WinDismissDlg(hDlg, TRUE); break;
  302.         default: break;
  303.         }
  304.     default: return WinDefDlgProc(hDlg, msg, mp1, mp2);
  305.     }
  306.     return FALSE;
  307. }
  308.