home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / windows / disolve.zip / WORDS.C < prev    next >
C/C++ Source or Header  |  1990-06-21  |  17KB  |  615 lines

  1. /*  words.c
  2.     words Application
  3.  
  4.     Copyright 1990 by William R. Ross
  5.  
  6.     12/26/89    start adding new "Fader" control
  7. */
  8.  
  9. #include <stdlib.h> /* for rand(), srand() */
  10. #include <sys\types.h> /* for ftime() */
  11. #include <sys\timeb.h> /* ditto */
  12.  
  13. #define NOMINMAX 
  14. #include "windows.h"
  15. #define WORDS_MAIN
  16. #include "words.h"
  17. #include "fader.h"
  18.  
  19. /* preface all functions w/FCN */
  20. #define FCN
  21.  
  22. char *szAppName = "Words"; /* also class name for dialog */
  23. char *szAbout = "About...";
  24. char szTempBuf[150];
  25.  
  26. char *Adjectives[] = {
  27.                      "timeless",
  28.                      "skillful",
  29.                      "blissful",
  30.                      "powerful",
  31.                      "fragrant",
  32.                      "young",
  33.                      "ancient",
  34.                      "wanton",
  35.                      "fervent",
  36.                      "torrid",
  37.                      "regretful",
  38.                      "plaintive",
  39.                      "soft",
  40.                      "sweet",
  41.                      "forgotten",
  42.                      "unwanted",
  43.                      "silent",
  44.                      "willing",
  45.                      "deliberate",
  46.                      "serene",
  47.                      "shrill",
  48.                      "exotic",
  49.                      "familiar",
  50.                      "new",
  51.                      "reluctant",
  52.                      "languid",
  53.                      "frozen",
  54.                      "harsh",
  55.                      "lone",
  56.                      "desperate",
  57.                      "soaring",
  58.                      "sinister",
  59.                      "gentle",
  60.                      "tranquil",
  61.                      "vicious",
  62.                      "loving",
  63.                      "grim",
  64.                      "pitiless",
  65.                      "savage",
  66.                      "austere",
  67.                      "stern",
  68.                      "still",
  69.                      "silent",
  70.                      "dark"
  71.                      };
  72.  
  73. #define NUMADJS sizeof(Adjectives) / sizeof (char *)
  74.  
  75.  
  76. char *Nouns[] = {
  77.                 "leaders",
  78.                 "followers",
  79.                 "winds",
  80.                 "crystals",
  81.                 "brothers",
  82.                 "sisters",
  83.                 "mothers",
  84.                 "pioneers",
  85.                 "heroes",
  86.                 "icons",
  87.                 "idols",
  88.                 "children",
  89.                 "intentions",
  90.                 "goddesses",
  91.                 "artisans",
  92.                 "flowers",
  93.                 "wisdoms",
  94.                 "rivals",
  95.                 "opponents",
  96.                 "words",
  97.                 "symbols",
  98.                 "heresies",
  99.                 "visions",
  100.                 "songs",
  101.                 "fools",
  102.                 "demons",
  103.                 "patterns",
  104.                 "illusions",
  105.                 "warriors"
  106.                  };
  107.  
  108. #define NUMNOUNS sizeof(Nouns) / sizeof (char *)
  109.  
  110. char *Verbs[] = {
  111.                 "balance",
  112.                 "sustain",
  113.                 "challenge",
  114.                 "arouse",
  115.                 "beguile",
  116.                 "tantalize",
  117.                 "seduce",
  118.                 "excite",
  119.                 "beckon",
  120.                 "shelter",
  121.                 "inspire",
  122.                 "annoy",
  123.                 "provoke",
  124.                 "torment",
  125.                 "welcome",
  126.                 "summon",
  127.                 "deprive",
  128.                 "ensnare",
  129.                 "entrap",
  130.                 "assert",
  131.                 "defend",
  132.                 "reveal",
  133.                 "conceal",
  134.                 "enrich",
  135.                 "improve",
  136.                 "demolish",
  137.                 "shield",
  138.                 "inform",
  139.                 "lure",
  140.                 "recall",
  141.                 "worship"
  142.                 };
  143.  
  144. #define NUMVERBS sizeof(Verbs) / sizeof (char *)
  145.  
  146. static HWND  hWnd;
  147. static HANDLE hInst;
  148. static int nLastControl;
  149.  
  150. #define NUMWORDS 50
  151. #define NUMCTLS 5
  152. #define ADJ 1
  153. #define NOUN 2
  154. #define VERB 3
  155.  
  156. static struct controls_tag {
  157.         HWND hCtl;
  158.         int nID;
  159.         BOOL bDone;
  160.         int  nType;
  161.         int nNumWords;
  162.         int nNumWordsDone;
  163.         BOOL bWordsDone [NUMWORDS];
  164.     } Controls [NUMCTLS];
  165.  
  166.  
  167. BOOL bNounsDone [NUMNOUNS];
  168. BOOL bVerbsDone [NUMVERBS];
  169. BOOL bAdjsDone [NUMADJS];
  170. int nNumNounsDone;
  171. int nNumVerbsDone;
  172. int nNumAdjsDone;
  173.  
  174. /* externs defined in KERNEL */
  175. WORD far PASCAL _lwrite ();
  176. WORD far PASCAL _lclose ();
  177.  
  178. /******************************************
  179. * WordsInit
  180. * function called when app loaded for the first time
  181. *
  182. *******************************************/
  183. FCN BOOL WordsInit( hInstance )
  184. HANDLE hInstance;
  185. {
  186.     PWNDCLASS   pClass;
  187.  
  188.     pClass = (PWNDCLASS)LocalAlloc( LPTR, sizeof(WNDCLASS) );
  189.  
  190.     /* this is the class for the main window */
  191.     pClass->hCursor        = LoadCursor( NULL, IDC_ARROW );
  192.     pClass->hIcon          = LoadIcon( hInstance, MAKEINTRESOURCE(HELLOICON) );
  193.     pClass->lpszMenuName   = (LPSTR)NULL;
  194.     pClass->lpszClassName  = (LPSTR)szAppName;
  195.     pClass->hbrBackground  = (HBRUSH)GetStockObject( WHITE_BRUSH );
  196.     pClass->hInstance      = hInstance;
  197.     pClass->style          = CS_HREDRAW | CS_VREDRAW;
  198.     pClass->lpfnWndProc    = WndProc;
  199.  
  200.     if (!RegisterClass( (LPWNDCLASS)pClass ) )
  201.         /* Initialization failed.
  202.          * Windows will automatically deallocate all allocated memory.
  203.          */
  204.         return FALSE;
  205.  
  206.     LocalFree( (HANDLE)pClass );
  207.     if (!FaderInit (hInstance))
  208.         return FALSE;
  209.     else
  210.         return TRUE;        /* Initialization succeeded */
  211. } /* end of WordsInit */
  212.  
  213. /**********************************************
  214. * main()
  215. *
  216. ***********************************************/
  217. FCN int PASCAL WinMain( hInstance, hPrevInstance, lpszCmdLine, cmdShow )
  218. HANDLE hInstance, hPrevInstance;
  219. LPSTR lpszCmdLine;
  220. int cmdShow;
  221. {
  222.     MSG   msg;
  223.     HMENU hMenu;
  224.     RECT rect;
  225.     OFSTRUCT Ofstruct;
  226.  
  227.     if (!hPrevInstance)
  228.         {
  229.         /* Call initialization procedure if this is the first instance */
  230.         if (!WordsInit( hInstance ))
  231.             {
  232.             MessageBox(NULL, (LPSTR)"Cannot register window class",
  233.                         (LPSTR)"Words", MB_OK);
  234.             return FALSE;
  235.             }
  236.         }
  237.  
  238.     /* Save instance handle for DialogBox */
  239.     hInst = hInstance;
  240.  
  241.     /* Bind callback function with module instance */
  242.     lpprocAbout = MakeProcInstance( (FARPROC)About, hInstance );
  243.  
  244.     hWnd = CreateDialog (hInst, szAppName, 0, NULL);
  245.     if (!hWnd)
  246.         {
  247.         MessageBox(NULL, (LPSTR) "CreateDialog returned NULL",
  248.                    (LPSTR) "Words", MB_OK);
  249.         return FALSE;
  250.         }
  251.     ShowWindow (hWnd, cmdShow);
  252.  
  253.     /* Insert "About..." into system menu */
  254.     hMenu = GetSystemMenu(hWnd, FALSE);
  255.     ChangeMenu(hMenu, 0, NULL, 999, MF_APPEND | MF_SEPARATOR);
  256.     ChangeMenu(hMenu, 0, (LPSTR)szAbout, IDSABOUT, MF_APPEND | MF_STRING);
  257.  
  258.     /* Polling messages from event queue */
  259.     while (GetMessage((LPMSG)&msg, NULL, 0, 0))
  260.         {
  261.         TranslateMessage((LPMSG)&msg);
  262.         DispatchMessage((LPMSG)&msg);
  263.         } /* while */
  264.  
  265.     return (int)msg.wParam;
  266. } /* end of WinMain */
  267.  
  268.  
  269. /***************************************************
  270. * WndProc ()
  271. * primary window procedure for app
  272. *
  273. ***************************************************/
  274. FCN long FAR PASCAL WndProc( hWnd, message, wParam, lParam )
  275. HWND hWnd;
  276. unsigned message;
  277. WORD wParam;
  278. LONG lParam;
  279. {
  280.     HWND hCtl;
  281.     WORD wResult;
  282.     int i;
  283.     static BOOL bFirst = TRUE;
  284.     static BOOL bSecond = FALSE;
  285.     static int nNext = 0;
  286.  
  287.     switch (message)
  288.     {
  289.     case WM_INITDIALOG:
  290.     case WM_CREATE:
  291.         if (!(wResult = SetTimer (hWnd, 1, 3200, NULL)))
  292.             {
  293.             MessageBox (hWnd, "Not enough timers.", (LPSTR)szAppName,
  294.                         MB_OK | MB_ICONEXCLAMATION);
  295.             exit (0);
  296.             }
  297.         InitControls (hWnd);
  298.         break;
  299.  
  300.     case WM_TIMER:
  301.         if (bFirst)
  302.             {
  303.             bFirst = FALSE;
  304.             KillTimer (hWnd, 1);
  305.             SendDlgItemMessage (hWnd, ADJ1, (WORD)WM_FADER,
  306.             (WORD)NULL, (long) (LPSTR) NULL);
  307.             SendDlgItemMessage (hWnd, NOUN1, (WORD)WM_FADER,
  308.             (WORD)NULL, (long) (LPSTR) NULL);
  309.             SendDlgItemMessage (hWnd, VERB1, (WORD)WM_FADER,
  310.             (WORD)NULL, (long) (LPSTR) NULL);
  311.             SendDlgItemMessage (hWnd, ADJ2, (WORD)WM_FADER,
  312.             (WORD)NULL, (long) (LPSTR) NULL);
  313.             SendDlgItemMessage (hWnd, NOUN2, (WORD)WM_FADER,
  314.             (WORD)NULL, (long) (LPSTR) NULL);
  315.             SetTimer (hWnd, 1, 1700, NULL);
  316.             bSecond = TRUE;
  317.             break;
  318.             }
  319.  
  320.         if (bSecond)
  321.             {
  322.             UpdateControl (hWnd, &Controls[nNext]);
  323.             nNext++;
  324.             if (nNext == NUMCTLS)
  325.                 {
  326.                 bSecond = FALSE;
  327.                 KillTimer (hWnd, 1);
  328.                 SetTimer (hWnd, 1, 3200, NULL);
  329.                 }
  330.             break;
  331.             }
  332.  
  333.         i = SelectControl ();
  334.         UpdateControl (hWnd, &Controls[i]);
  335.         break;
  336.  
  337.  
  338.     case WM_SYSCOMMAND:
  339.         switch (wParam)
  340.         {
  341.         case IDSABOUT:
  342.             DialogBox( hInst, MAKEINTRESOURCE(ABOUTBOX), hWnd, lpprocAbout );
  343.             break;
  344.  
  345.         default:
  346.             return DefWindowProc( hWnd, message, wParam, lParam );
  347.         }
  348.         break;
  349.  
  350.     case WM_DESTROY:
  351.         KillTimer (hWnd, NULL);
  352.         PostQuitMessage( 0 );
  353.         break;
  354.  
  355.     default:
  356.         return DefWindowProc( hWnd, message, wParam, lParam );
  357.         break;
  358.     }
  359.     return(0L);
  360. } /* end of WndProc */
  361.  
  362.  
  363. /***************************************************
  364. * About ()
  365. * window procedure for About dialog
  366. *
  367. ***************************************************/
  368. FCN BOOL FAR PASCAL About( hDlg, message, wParam, lParam )
  369. HWND hDlg;
  370. unsigned message;
  371. WORD wParam;
  372. LONG lParam;
  373. {
  374.     BOOL bDisplayed;
  375.     WORD wResult;
  376.  
  377.     if (message == WM_COMMAND)
  378.         {
  379.         KillTimer (hDlg, 1);
  380.         EndDialog( hDlg, TRUE );
  381.         return TRUE;
  382.         }
  383.     else if (message == WM_INITDIALOG)
  384.         {
  385.         bDisplayed = TRUE;
  386.  
  387.         if (!(wResult = SetTimer (hDlg, 1, 1000, NULL)))
  388.             MessageBox (hDlg, "About: Not enough timers.", (LPSTR)szAppName,
  389.                         MB_OK | MB_ICONEXCLAMATION);
  390.  
  391.         return TRUE;
  392.         }
  393.     else if (message == WM_TIMER)
  394.         {
  395.         if (bDisplayed)
  396.             {
  397.             KillTimer (hDlg, 1);
  398.             SetTimer (hDlg, 1, 1000, NULL);
  399.             SendDlgItemMessage (hDlg, ID_TITLE, (WORD)WM_FADER,
  400.                         (WORD)NULL, (long) NULL);
  401.             bDisplayed = FALSE;
  402.             }
  403.         else
  404.             {
  405.             KillTimer (hDlg, 1);
  406.             SetTimer (hDlg, 1, 3000, NULL);
  407.             SendDlgItemMessage (hDlg, ID_TITLE, (WORD)WM_FADER,
  408.                         (WORD)NULL, (long) (LPSTR) "Words by Ross");
  409.             bDisplayed = TRUE;
  410.             } /* else */
  411.         return TRUE;
  412.         } /* if wm_timer */
  413.     else return FALSE;
  414. } /* end of About */
  415.  
  416. /***************************************************
  417. * InitControls ()
  418. * Initialization for the Fader controls
  419. *
  420. ***************************************************/
  421. FCN void InitControls (HWND hDlg)
  422. {
  423.     int i,j;
  424.     struct timeb timebuffer;
  425.  
  426.     /* init random number generator */
  427.     ftime (&timebuffer);
  428.     srand ((unsigned) timebuffer.millitm);
  429.  
  430.     /* get handles for the controls and store */
  431.     Controls[0].hCtl = GetDlgItem ( hDlg, ADJ1 );
  432.     Controls[0].nID = ADJ1;
  433.     Controls[0].bDone = FALSE;
  434.     Controls[0].nType = ADJ;
  435.     Controls[0].nNumWordsDone = 0;
  436.     Controls[0].nNumWords = NUMADJS;
  437.  
  438.     Controls[1].hCtl = GetDlgItem ( hDlg, NOUN1 );
  439.     Controls[1].nID = NOUN1;
  440.     Controls[1].bDone = FALSE;
  441.     Controls[1].nType = NOUN;
  442.     Controls[1].nNumWordsDone = 0;
  443.     Controls[1].nNumWords = NUMNOUNS;
  444.  
  445.     Controls[2].hCtl = GetDlgItem (hDlg, VERB1 );
  446.     Controls[2].nID = VERB1;
  447.     Controls[2].bDone = FALSE;
  448.     Controls[2].nType = VERB;
  449.     Controls[2].nNumWordsDone = 0;
  450.     Controls[2].nNumWords = NUMVERBS;
  451.  
  452.     Controls[3].hCtl = GetDlgItem (hDlg, ADJ2 );
  453.     Controls[3].nID = ADJ2;
  454.     Controls[3].bDone = FALSE;
  455.     Controls[3].nType = ADJ;
  456.     Controls[3].nNumWordsDone = 0;
  457.     Controls[3].nNumWords = NUMADJS;
  458.  
  459.     Controls[4].hCtl = GetDlgItem (hDlg, NOUN2 );
  460.     Controls[4].nID = NOUN2;
  461.     Controls[4].bDone = FALSE;
  462.     Controls[4].nType = NOUN;
  463.     Controls[4].nNumWordsDone = 0;
  464.     Controls[4].nNumWords = NUMNOUNS;
  465.  
  466.     for (i = 0; i < NUMCTLS; i++)
  467.         {
  468.         for (j = 0; j < Controls[i].nNumWords; j++)
  469.             Controls[i].bWordsDone[j] = FALSE;
  470.  
  471.         UpdateControl (hDlg, &Controls[i]);
  472.         } /* for */
  473.  
  474.         nLastControl = -1;
  475.  
  476.     for (i = 0; i < NUMVERBS; i++)
  477.         bVerbsDone [i] = FALSE;
  478.  
  479.     for (i = 0; i < NUMNOUNS; i++)
  480.         bNounsDone [i] = FALSE;
  481.  
  482.     for (i = 0; i < NUMADJS; i++)
  483.         bAdjsDone [i] = FALSE;
  484.  
  485.     nNumNounsDone = 0;
  486.     nNumAdjsDone = 0;
  487.     nNumVerbsDone = 0;
  488.  
  489.  
  490. } /* end of InitControls */
  491.  
  492.  
  493. /***************************************************
  494. * UpdateControl ()
  495. * selects a word to display in a control
  496. *
  497. ***************************************************/
  498. FCN void UpdateControl (HWND hDlg, struct controls_tag *Control)
  499. {
  500.     int i, j;
  501.  
  502.     i = rand() % Control->nNumWords;
  503.  
  504.     switch (Control->nType)
  505.         {
  506.          case ADJ:
  507.             if (nNumAdjsDone == NUMADJS)
  508.                 {
  509.                 nNumAdjsDone = 0;
  510.                 for (j = 0; j < NUMADJS; j++)
  511.                     bAdjsDone[j] = FALSE;
  512.                 } /* if */
  513.  
  514.             while (bAdjsDone [i])
  515.                 {
  516.                 i++;
  517.                 if (i >= NUMADJS)
  518.                     i = 0;
  519.                 } /* while */
  520.  
  521.             bAdjsDone[i] = TRUE;
  522.             nNumAdjsDone++;
  523.             SendDlgItemMessage (hDlg, Control->nID, (WORD)WM_FADER,
  524.                         (WORD)NULL, (long) (LPSTR) Adjectives[i]);
  525.             break;
  526.  
  527.         case NOUN:
  528.             if (nNumNounsDone == NUMNOUNS)
  529.                 {
  530.                 nNumNounsDone = 0;
  531.                 for (j = 0; j < NUMNOUNS; j++)
  532.                     bNounsDone[j] = FALSE;
  533.                 } /* if */
  534.  
  535.             while (bNounsDone [i])
  536.                 {
  537.                 i++;
  538.                 if (i >= NUMNOUNS)
  539.                     i = 0;
  540.                 } /* while */
  541.  
  542.             bNounsDone[i] = TRUE;
  543.             nNumNounsDone++;
  544.             SendDlgItemMessage (hDlg, Control->nID, (WORD)WM_FADER,
  545.                         (WORD)NULL, (long) (LPSTR) Nouns[i]);
  546.             break;
  547.  
  548.         case VERB:
  549.             if (nNumVerbsDone == NUMVERBS)
  550.                 {
  551.                 nNumVerbsDone = 0;
  552.                 for (j = 0; j < NUMVERBS; j++)
  553.                     bVerbsDone[j] = FALSE;
  554.                 } /* if */
  555.  
  556.             while (bVerbsDone [i])
  557.                 {
  558.                 i++;
  559.                 if (i >= NUMVERBS)
  560.                     i = 0;
  561.                 } /* while */
  562.  
  563.             bVerbsDone[i] = TRUE;
  564.             nNumVerbsDone++;
  565.             SendDlgItemMessage (hDlg, Control->nID, (WORD)WM_FADER,
  566.                         (WORD)NULL, (long) (LPSTR) Verbs[i]);
  567.             break;
  568.  
  569.         default:
  570.             /* error */
  571.             break;
  572.  
  573.         } /* switch */
  574.  
  575. } /* end of UpdateControls */
  576.  
  577. /***************************************************
  578. * SelectControl ()
  579. * selects a control to update
  580. *
  581. ***************************************************/
  582. FCN int SelectControl (void)
  583. {
  584.     int i;
  585.  
  586.     if (Controls[0].bDone &&
  587.         Controls[1].bDone &&
  588.         Controls[2].bDone &&
  589.         Controls[3].bDone &&
  590.         Controls[4].bDone)
  591.         {
  592.         Controls[0].bDone = FALSE;
  593.         Controls[1].bDone = FALSE;
  594.         Controls[2].bDone = FALSE;
  595.         Controls[3].bDone = FALSE;
  596.         Controls[4].bDone = FALSE;
  597.         } /* if */
  598.  
  599.  
  600.     i = rand() % NUMCTLS;
  601.  
  602.     while  ((Controls[i].bDone) || (i == nLastControl))
  603.         {
  604.         i++;
  605.         if (i >= NUMCTLS)
  606.             i = 0;
  607.         } /* while */
  608.  
  609.     Controls[i].bDone = TRUE;
  610.     nLastControl = i;
  611.  
  612.     return i;
  613.  
  614. } /* end of SelectControl */
  615.