home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / mnth0108.zip / Bell / quotes.c next >
Text File  |  1993-06-16  |  13KB  |  290 lines

  1. /* Program demonstrates use of window words, sending menu messages to 
  2.     submenus, use of stringtables
  3.     Version 1.0 for OS/2 2.1
  4. */
  5.  
  6. #define INCL_WIN    /* This includes the window manager function prototypes, */
  7.                     /* constants, etc. */
  8. #include <os2.h>    /* This is the main OS/2 definitions include file */
  9. #include "malloc.h"
  10. #include <string.h>
  11.  
  12. #include "quotes.h"
  13.  
  14. /* Prototype for the applications window procedure */
  15. MRESULT EXPENTRY QuotesWndProc(HWND hwnd, ULONG usMessage, MPARAM mp1, MPARAM mp2);
  16. MRESULT EXPENTRY QuotesChildWndProc(HWND hwnd, ULONG usMessage, MPARAM mp1, MPARAM mp2);
  17.  
  18. /* We need handles for both the frame window and the client window */
  19. HWND hwndParentFrame, hwndParentClient;
  20. HWND hwndC1Frame, hwndC2Frame, hwndC1Client, hwndC2Client;
  21.  
  22. /* Multiple windows can be maintained by one wndproc, so it's a class, rather
  23. than an object. The class is registered by name with the system. This 
  24. "application" has two types of windows (main and two children) so it requires
  25. two window classes */
  26. char szClassName[] = "Quotes";
  27. char szChildClassName[] = "QuotesChild";
  28.  
  29. void main(int argc, char **argv)
  30. {
  31.     HAB hab;        /* Handle to an 'anchor block' */
  32.     HMQ hmq;        /* Handle to the input message queue */
  33.     QMSG qmsg;        /* Queue message structure to hold the incoming message */
  34.     ULONG ulFrameFlags;   /* Frame creation flags */
  35.  
  36.     /* First, register with PM to get services. This returns a handle to an anchor block */
  37.     hab = WinInitialize(0);
  38.  
  39.     /* Next, create a message queue */
  40.     hmq = WinCreateMsgQueue(hab, 0);
  41.  
  42.     /* Now register a window class for the application parent window */
  43.     if(!WinRegisterClass(hab,
  44.         (PCH)szClassName,
  45.         (PFNWP)QuotesWndProc,    /* Pointer to winproc function */
  46.         CS_SIZEREDRAW,   /* Flags to force redraw when resized, etc. */
  47.         sizeof(char *)))                 /* Number of bytes in 'window words' */
  48.     DosExit(EXIT_PROCESS, 1);
  49.  
  50.     /* Now register a window class for the application child windows */
  51.     if(!WinRegisterClass(hab,
  52.         (PCH)szChildClassName,
  53.         (PFNWP)QuotesChildWndProc,    /* Pointer to winproc function */
  54.         CS_SIZEREDRAW,   /* Flags to force redraw when resized, etc. */
  55.         sizeof(PSTRCLR)))                /* Number of bytes in 'window words' */
  56.     DosExit(EXIT_PROCESS, 1);
  57.  
  58.     /* We want a regular window, but without an accelerator table */
  59.     ulFrameFlags = FCF_STANDARD & ~FCF_ACCELTABLE;
  60.  
  61.     /* Now go ahead and create the window */
  62.     /* This function call returns the frame window handle and also sets the client window handle */
  63.     hwndParentFrame = WinCreateStdWindow(HWND_DESKTOP,    /* Parent window */
  64.             WS_VISIBLE,             /* Window style visible */
  65.             &ulFrameFlags,              /* pointer to frame flags */
  66.             (PCH)szClassName,            /* Registered class name */
  67.             "Quotable Quotes",                /* Text for title bar */
  68.             WS_VISIBLE,             /* Client window style */
  69.             (HMODULE)NULL,            /* Pointer to resource module */
  70.             ID_QUOTES,                    /* Resource ID within module */
  71.             (HWND *)&hwndParentClient);        /* Pointer to client window handle */
  72.  
  73.     if(hwndParentFrame == 0) {
  74.         WinAlarm(HWND_DESKTOP, WA_ERROR);
  75.         DosExit(EXIT_PROCESS, 1);
  76.     }
  77.  
  78.     /* Now loop around processing events. This is called the 'event loop' */
  79.     /* WinGetMsg returns FALSE when it gets the WM_QUIT message */
  80.     while(WinGetMsg(hab, &qmsg, 0, 0, 0))    /* Get a message */
  81.        WinDispatchMsg(hab, &qmsg);        /* Dispatch it to the window */
  82.  
  83.     /* Lastly, clean up */
  84.     WinDestroyWindow(hwndParentFrame);
  85.     WinDestroyMsgQueue(hmq);
  86.     WinTerminate(hab);
  87. }
  88.  
  89. /* The client window procedure. Gets called with message components as parameters */
  90. MRESULT EXPENTRY QuotesWndProc(HWND hwnd, ULONG usMessage, MPARAM mp1, MPARAM mp2)
  91. {
  92.     HPS hps;        /* Handle to a presentation space. This is where a PM program 'draws' */
  93.     RECTL rc;       /* A rectangle structure, used to store the window coordinates */
  94.     ULONG ulFrameFlags = FCF_STANDARD & ~FCF_MENU & ~FCF_ICON & ~FCF_ACCELTABLE;    /* Frame creation flags */
  95.     HWND hwndActive;
  96.  
  97.     switch(usMessage) {
  98.  
  99.         case WM_CREATE:     /* process this message by returning FALSE. This lets the */
  100.  
  101.             hwndC1Frame = WinCreateStdWindow(hwnd,
  102.                         WS_VISIBLE,
  103.                         &ulFrameFlags,
  104.                         (PCH)szChildClassName,
  105.                         " Child 1",
  106.                         WS_VISIBLE,
  107.                         (HMODULE)NULL,
  108.                         0,
  109.                         (HWND *)&hwndC1Client);
  110.  
  111.             if(hwndC1Frame == 0) {
  112.                 WinAlarm(HWND_DESKTOP, WA_ERROR);
  113.                 DosExit(EXIT_PROCESS, 1);
  114.             }
  115.  
  116.             hwndC2Frame = WinCreateStdWindow(hwnd,
  117.                         WS_VISIBLE,
  118.                         &ulFrameFlags,
  119.                         (PCH)szChildClassName,
  120.                         " Child 2",
  121.                         WS_VISIBLE,
  122.                         (HMODULE)NULL,
  123.                         0,
  124.                         (HWND *)&hwndC2Client);
  125.  
  126.             if(hwndC2Frame == 0) {
  127.                 WinAlarm(HWND_DESKTOP, WA_ERROR);
  128.                 WinAlarm(HWND_DESKTOP, WA_ERROR);
  129.                 DosExit(EXIT_PROCESS, 1);
  130.             }
  131.  
  132.             WinSetWindowPtr(hwnd,0, "This is the background window");
  133.  
  134.             /* We can't size the children yet, since this window itself has */
  135.             /* no size. So we'll post a message to 'remind' ourselves */
  136.             /* to do it as soon as the window is visible */
  137.             WinPostMsg(hwnd, UM_INITSIZE, 0L, 0L);
  138.  
  139.             return (MRESULT)FALSE;  /* system continue creating the window */
  140.             break;
  141.  
  142.         case UM_INITSIZE:
  143.  
  144.                 /* Now's our chance to size the children */
  145.                 WinQueryWindowRect(hwnd, &rc);
  146.  
  147.                 WinSetWindowPos(hwndC1Frame,
  148.                         HWND_TOP,
  149.                         rc.xRight / 16,
  150.                         rc.yTop / 8,
  151.                         rc.xRight * 3 / 4,
  152.                         rc.yTop * 3 / 4,
  153.                         SWP_SIZE | SWP_MOVE);
  154.  
  155.                 WinSetWindowPos(hwndC2Frame,
  156.                         HWND_TOP,
  157.                         rc.xRight/8,
  158.                         rc.yTop/16,
  159.                         rc.xRight * 3 / 4,
  160.                         rc.yTop * 3 / 4,
  161.                         SWP_SIZE | SWP_MOVE);
  162.                 break;
  163.  
  164.         case WM_COMMAND:
  165.             /* Got a menu choice. If it applies to this window, do it */
  166.             /* Otherwise, pass it down to the active window (if there is one) */
  167.  
  168.             switch (SHORT1FROMMP(mp1)) {
  169.                 case IDM_EXIT:
  170.                     WinPostMsg(hwnd, WM_QUIT, mp1, mp2);
  171.                     break;
  172.                  default: 
  173.                     if ((hwndActive=WinQueryActiveWindow(hwnd)) != 0L)
  174.                         WinSendMsg(hwndActive, WM_COMMAND, mp1, mp2);
  175.                     break;
  176.             }
  177.             break;
  178.  
  179.         case WM_ERASEBACKGROUND:    /* Let the frame window procedure redraw the background */
  180.             return (MRESULT)TRUE;   /* in SYSCLR_WINDOW (usually white) */
  181.             break;
  182.  
  183.         case WM_PAINT:        /* The 'guts' of the application */
  184.             hps = WinBeginPaint(hwnd, 0, NULL);   /* Get a presentation space */
  185.             WinQueryWindowRect(hwnd, &rc);         /* Get the window dimensions */
  186.             /* Draw the message, in rectangle rc, coloured CLR_NEUTRAL (black) on
  187.             CLR_BACKGROUND (white), centered, over-writing the entire rectangle */
  188.             WinDrawText(hps, -1, WinQueryWindowPtr(hwnd, QWL_USER), &rc, CLR_NEUTRAL, CLR_BACKGROUND,
  189.                         DT_CENTER | DT_VCENTER | DT_ERASERECT);
  190.             WinEndPaint(hps);        /* Release the presentation space */
  191.             break;
  192.  
  193.         case WM_DESTROY:
  194. /*            WinDestroyWindow(hwndC1Frame);
  195.             WinDestroyWindow(hwndC2Frame); */
  196.             return (MRESULT) 0;
  197.             break;
  198.  
  199.         case WM_CLOSE:    /* User chose CLOSE on system menu or double-clicked */
  200.             WinPostMsg(hwnd, WM_QUIT, 0, 0);    /* So send back WM_QUIT, causing */
  201.             break;            /* WinGetMsg to return FALSE and exit the event loop */
  202.  
  203.         default:        /* Let the system handle messages we don't */
  204.             return WinDefWindowProc(hwnd, usMessage, mp1, mp2);
  205.             break;
  206.     }
  207.     return 0L;
  208. }
  209.  
  210. /* The child window procedure. Gets called with message components as parameters */
  211. MRESULT EXPENTRY QuotesChildWndProc(HWND hwnd, ULONG usMessage, MPARAM mp1, MPARAM mp2)
  212. {
  213.     HPS hps;        /* Handle to a presentation space. This is where a PM program 'draws' */
  214.     RECTL rc;        /* A rectangle structure, used to store the window coordinates */
  215.     PSTRCLR p;      /* A pointer to a STRINGCOLOR structure */
  216.     HAB hab;        /* Required to load a string from a stringtable resource */
  217.  
  218.     /* Extract a pointer to the STRCLR structure from the window */
  219.     p = PSCFROMHWND(hwnd);
  220.  
  221.     switch(usMessage) {
  222.  
  223.         case WM_CREATE:
  224.             /* Allocate a STRCLR structure and initialize it */
  225.             p = (PSTRCLR) malloc(sizeof(STRCLR));
  226.             WinSetWindowPtr(hwnd,0,(PVOID)p);
  227.             p->ulFColor = CLR_NEUTRAL;
  228.             p->ulBColor = CLR_BACKGROUND;
  229.             strcpy(p->quote, "A suitable quote goes here");
  230.             return (MRESULT)FALSE;  /* Let the system continue creating the window */
  231.             break;
  232.  
  233.         case WM_DESTROY:
  234.             free(p);
  235.             return (MRESULT) 0;
  236.  
  237.         case WM_COMMAND:
  238.             hab = WinQueryAnchorBlock(hwnd);
  239.             switch(SHORT1FROMMP(mp1)) {
  240.                 case IDM_TWAIN:
  241.                     WinLoadString(hab, (HMODULE)0, IDS_TWAIN, MAXSTR, p->quote);
  242.                     break;
  243.                 case IDM_IBSEN:
  244.                     WinLoadString(hab, (HMODULE)0, IDS_IBSEN, MAXSTR, p->quote);
  245.                     break;
  246.                 case IDM_GATES:
  247.                     WinLoadString(hab, (HMODULE)0, IDS_GATES, MAXSTR, p->quote);
  248.                     break;
  249.                 case IDMF_RED: p->ulFColor = CLR_RED; break;
  250.                 case IDMF_GREEN: p->ulFColor = CLR_GREEN; break;
  251.                 case IDMF_BLUE: p->ulFColor = CLR_BLUE; break;
  252.                 case IDMB_RED: p->ulBColor = CLR_RED; break;
  253.                 case IDMB_GREEN: p->ulBColor = CLR_GREEN; break;
  254.                 case IDMB_BLUE: p->ulBColor = CLR_BLUE; break;
  255.             }
  256.  
  257.             /* After changing the internal representation of the window, get */
  258.             /* the system to repaint the window by invalidating it */
  259.             WinInvalidateRect(hwnd, NULL, 0);
  260.             return (MRESULT)0;
  261.             break;
  262.  
  263.         case WM_ERASEBACKGROUND:    /*Don't let the frame window procedure redraw the background */
  264.             WinInvalidateRect(hwnd, NULL, FALSE);
  265.             return (MRESULT)FALSE;   /* in SYSCLR_WINDOW (usually white) */
  266.             break;
  267.  
  268.         case WM_PAINT:          /* The 'guts' of the application */
  269.                                 /* Repaints the window based on the internal structure */
  270.             hps = WinBeginPaint(hwnd, 0, &rc);   /* Get a presentation space */
  271.             WinQueryWindowRect(hwnd, &rc);         /* Get the window dimensions */
  272.  
  273.             /* Draw the message, in rectangle rc, coloured CLR_NEUTRAL (black) on */
  274.             /* CLR_BACKGROUND (white), centered, over-writing the entire rectangle */
  275.             WinDrawText(hps, -1, p->quote, &rc, p->ulFColor, p->ulBColor,
  276.                         DT_CENTER | DT_VCENTER | DT_ERASERECT);
  277.             WinEndPaint(hps);        /* Release the presentation space */
  278.             break;
  279.  
  280.         case WM_CLOSE:    /* User chose CLOSE on system menu or double-clicked */
  281.             WinDestroyWindow(WinQueryWindow(hwnd, QW_PARENT));
  282.             break;            /* WinGetMsg to return FALSE and exit the event loop */
  283.  
  284.         default:        /* Let the system handle messages we don't */
  285.             return WinDefWindowProc(hwnd, usMessage, mp1, mp2);
  286.             break;
  287.     }
  288.     return 0L;
  289. }
  290.