home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / quot210s.zip / src / qotd.c < prev    next >
C/C++ Source or Header  |  1998-06-07  |  11KB  |  350 lines

  1. /*
  2.  * qotd.c
  3.  *
  4.  * Main procedures for the Quoteriser's Quote-of-the-Day utility.
  5.  *
  6.  *      Created: 31st March, 1997
  7.  * Version 1.00: 9th April, 1997
  8.  * Version 2.00: 21st December, 1997
  9.  * Version 2.01: 8th January, 1997
  10.  *
  11.  * (C) 1997 Nicholas Paul Sheppard
  12.  *
  13.  * This file is distributed under the GNU General Public License. See the
  14.  * file copying.txt for details.
  15.  */
  16.  
  17. #define INCL_WIN
  18. #include <os2.h>
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include "qotd.h"
  22. #include "threads.h"
  23. #include "types.h"
  24. #include "general.h"
  25. #include "quotes.h"
  26. #include "authors.h"
  27. #include "pmutil.h"
  28.  
  29. HAB        hab;
  30. HWND        hwndFrame, hwndMain, hwndMenu, hwndVertScroll;
  31. char        szAppName[64];
  32. PROGSTATE    ps;
  33. PROFILE        prf;
  34.  
  35.  
  36. MRESULT EXPENTRY MainWinProc(HWND, ULONG, MPARAM, MPARAM);
  37. void GetWindowPosition(HWND);
  38. void SaveWindowPosition(HWND);
  39. int GetINISettings(HWND);
  40.  
  41. int main(void)
  42. {
  43.        HMQ    hmq;
  44.     QMSG    qmsg;
  45.     ULONG    flFrameFlags = FCF_TITLEBAR | FCF_SYSMENU | FCF_MENU | FCF_SIZEBORDER | FCF_MINMAX | FCF_TASKLIST | FCF_VERTSCROLL;
  46.     char    szMainClass[] = "QOTDMAIN";
  47.  
  48.     if ((hab = WinInitialize(0)) == 0)
  49.         return (1);
  50.     hmq = WinCreateMsgQueue(hab, 0);
  51.  
  52.     /* create the main window */
  53.     WinRegisterClass(hab, szMainClass, (PFNWP)MainWinProc, CS_SIZEREDRAW, 0); 
  54.     WinLoadString(hab, 0, IDS_APPNAME, sizeof(szAppName), szAppName);
  55.     hwndFrame = WinCreateStdWindow(HWND_DESKTOP, 0, &flFrameFlags, szMainClass,
  56.                     szAppName, 0L, NULLHANDLE, IDM_MAIN, &hwndMain);
  57.  
  58.     if (hwndFrame != NULLHANDLE) {
  59.         GetWindowPosition(hwndFrame);
  60.         hwndMenu = WinWindowFromID(hwndFrame, FID_MENU);
  61.         hwndVertScroll = WinWindowFromID(hwndFrame, FID_VERTSCROLL);
  62.  
  63.         /* initialise menu item enable states */
  64.         WinEnableMenuItem(hwndMenu, IDM_AGAIN, FALSE);
  65.  
  66.         /* disable scroll bar (painting will re-enable it later) */
  67.         WinEnableWindow(hwndVertScroll, FALSE);
  68.  
  69.         while (WinGetMsg(hab, &qmsg, (HWND)NULL, 0, 0))
  70.             WinDispatchMsg(hab, &qmsg);
  71.  
  72.         WinDestroyWindow(hwndFrame);
  73.     }
  74.  
  75.     WinDestroyMsgQueue(hmq);
  76.     WinTerminate(hab);
  77.     return (0);
  78. }
  79.  
  80.  
  81. MRESULT EXPENTRY MainWinProc(HWND hwnd, ULONG message, MPARAM mp1, MPARAM mp2)
  82. /*
  83.  * WIndow procedure for the main window.
  84.  */
  85. {
  86.     static THREADINFO    ti;
  87.     char            szString[200], szMessage[200], szTitle[20];
  88.     QDBINFO            qdbinfo;
  89.     ADBINFO            adbinfo;
  90.     RECTL            rectl;
  91.  
  92.     switch (message) {
  93.         case WM_CREATE:
  94.             if (!GetINISettings(hwnd))
  95.                 return ((MRESULT)TRUE);
  96.             ps.iMode = Q_SEARCHING;
  97.             ps.szAuthorCode[0] = '\0';
  98.             ps.szQuoteCode[0] = '\0';
  99.             ps.pqi = NULL;
  100.             ps.pai = NULL;
  101.             ps.pszText = ps.pszDesc = NULL;
  102.             ti.hwndCaller = hwnd;
  103.             ti.pps = &ps;
  104.             if (_beginthread((THREADPROC)ThreadPaint, NULL, THREADSTACK, (void *)(&ti)) == -1) {
  105.                 WinLoadString(hab, 0, IDS_THREADFAILED, sizeof(szMessage), szMessage);
  106.                 WinLoadString(hab, 0, IDS_ERROR, sizeof(szTitle), szTitle);
  107.                 WinMessageBox(HWND_DESKTOP, hwnd, szMessage, szTitle, 0, MB_ERROR);
  108.                 return ((MRESULT)TRUE);
  109.             }
  110.             return (FALSE);
  111.  
  112.         case QM_START:
  113.             WinSendMsg(hwndPaint, QM_TYPESET, NULL, NULL);
  114.             WinSendMsg(hwndPaint, QM_PAINT, NULL, NULL);
  115.             if (ps.szQuoteDB[0] == '\0') {
  116.                 WinLoadString(hab, 0, IDS_UNSET, sizeof(szMessage), szMessage);
  117.                 WinLoadString(hab, 0, IDS_ERROR, sizeof(szTitle), szTitle);
  118.                 WinMessageBox(HWND_DESKTOP, hwnd, szMessage, szTitle, 0, MB_ERROR);
  119.                 WinSendMsg(hwnd, WM_CLOSE, NULL, NULL);
  120.                 return (FALSE);
  121.             }
  122.             if (!QuoteDBStat(ps.szQuoteDB, &qdbinfo)) {
  123.                 WinLoadString(hab, 0, IDS_NODB, sizeof(szString), szString);
  124.                 sprintf(szMessage, szString, ps.szQuoteDB);
  125.                 WinLoadString(hab, 0, IDS_ERROR, sizeof(szTitle), szTitle);
  126.                 WinMessageBox(HWND_DESKTOP, hwnd, szMessage, szTitle, 0, MB_ERROR);
  127.                 WinSendMsg(hwnd, WM_CLOSE, NULL, NULL);
  128.                 return (FALSE);
  129.             }
  130.             if (!QuoteOpenDB(ps.szQuoteDB, &(ps.qdb), S_IREAD)) {
  131.                 WinLoadString(hab, 0, IDS_OPENFAILED, sizeof(szString), szString);
  132.                 sprintf(szMessage, szString, ps.szQuoteDB);
  133.                 WinLoadString(hab, 0, IDS_ERROR, sizeof(szTitle), szTitle);
  134.                 WinMessageBox(HWND_DESKTOP, hwnd, szMessage, szTitle, 0, MB_ERROR);
  135.                 WinSendMsg(hwnd, WM_CLOSE, NULL, NULL);
  136.                 return (FALSE);
  137.             }
  138.             if (ps.szAuthorDB[0] != '\0') {
  139.                 if (!AuthorDBStat(ps.szAuthorDB, &adbinfo)) {
  140.                     WinLoadString(hab, 0, IDS_NODB, sizeof(szString), szString);
  141.                     sprintf(szMessage, szString, ps.szAuthorDB);
  142.                     WinLoadString(hab, 0, IDS_ERROR, sizeof(szTitle), szTitle);
  143.                     WinMessageBox(HWND_DESKTOP, hwnd, szMessage, szTitle, 0, MB_ERROR);
  144.                     WinSendMsg(hwnd, WM_CLOSE, NULL, NULL);
  145.                     return (FALSE);
  146.                 }
  147.                 if (!AuthorOpenDB(ps.szAuthorDB, &(ps.adb), S_IREAD)) {
  148.                     WinLoadString(hab, 0, IDS_OPENFAILED, sizeof(szString), szString);
  149.                     sprintf(szMessage, szString, ps.szAuthorDB);
  150.                     WinLoadString(hab, 0, IDS_ERROR, sizeof(szTitle), szTitle);
  151.                     WinMessageBox(HWND_DESKTOP, hwnd, szMessage, szTitle, 0, MB_ERROR);
  152.                     WinSendMsg(hwnd, WM_CLOSE, NULL, NULL);
  153.                     return (FALSE);
  154.                 }
  155.             } else
  156.                 AuthorNullifyDB(&(ps.adb));
  157.             if (_beginthread((THREADPROC)ThreadFindRandomQuote, NULL, THREADSTACK, (void *)(&ti)) == -1) {
  158.                 WinLoadString(hab, 0, IDS_THREADFAILED, sizeof(szMessage), szMessage);
  159.                 WinLoadString(hab, 0, IDS_ERROR, sizeof(szTitle), szTitle);
  160.                 WinMessageBox(HWND_DESKTOP, hwnd, szMessage, szTitle, 0, MB_ERROR);
  161.                 WinSendMsg(hwnd, WM_CLOSE, NULL, NULL);
  162.                 return (FALSE);
  163.             }
  164.             return (FALSE);
  165.  
  166.         case WM_ERASEBACKGROUND:
  167.             return ((MRESULT)TRUE);
  168.  
  169.         case WM_PRESPARAMCHANGED:
  170.         case WM_SIZE:
  171.             WinSendMsg(hwndPaint, QM_TYPESET, NULL, NULL);
  172.             WinInvalidateRect(hwndMain, NULL, TRUE);
  173.             return (FALSE);
  174.  
  175.         case WM_PAINT:
  176.             WinSendMsg(hwndPaint, QM_PAINT, NULL, NULL);
  177.             return (FALSE);
  178.  
  179.         case WM_DESTROY:
  180.             /* free all of our memory */
  181.             QuoteFreeQuote(&(ps.pqi), &(ps.pszText));
  182.             AuthorFreeAuthor(&(ps.pai), &(ps.pszDesc));
  183.             if (ps.qdb.dbfInfo != NULL)
  184.                 QuoteCloseDB(&(ps.qdb));
  185.             if (ps.adb.dbfInfo != NULL)
  186.                 AuthorCloseDB(&(ps.adb));
  187.  
  188.             /* save the window position in QUOTER.INI */
  189.             SaveWindowPosition(hwndFrame);
  190.  
  191.             return (FALSE);
  192.  
  193.         case QM_FOUND:
  194.             if (PVOIDFROMMP(mp1) == NULL) {
  195.                 WinLoadString(hab, 0, IDS_EMPTYDB, sizeof(szMessage), szMessage);
  196.                 WinLoadString(hab, 0, IDS_ERROR, sizeof(szTitle), szTitle);
  197.                 WinMessageBox(HWND_DESKTOP, hwnd, szMessage, szTitle, 0, MB_ERROR);
  198.             } else {
  199.                 strcpy(ps.szQuoteCode, (char *)PVOIDFROMMP(mp1));
  200.                 QuoteGetQuote(&(ps.qdb), ps.szQuoteCode, &(ps.pqi), &(ps.pszText));
  201.                 if ((ps.adb.dbfInfo != NULL) && (ps.pqi->szAuthorCode[0] != '\0')) {
  202.                     strcpy(ps.szAuthorCode, ps.pqi->szAuthorCode);
  203.                     if (!AuthorGetAuthor(&(ps.adb), ps.szAuthorCode, &(ps.pai), &(ps.pszDesc)))
  204.                         ps.szAuthorCode[0] = '\0';
  205.                 }
  206.                 ps.iMode = Q_QUOTE;
  207.                 WinSendMsg(hwndPaint, QM_TYPESET, NULL, NULL);
  208.                 WinInvalidateRect(hwndMain, NULL, TRUE);
  209.                 WinEnableMenuItem(hwndMenu, IDM_AGAIN, TRUE);
  210.             }
  211.             return (FALSE);
  212.  
  213.         case WM_VSCROLL:
  214.             WinQueryWindowRect(hwndMain, &rectl);
  215.             WinScrollWindow(hwndMain, 0L, ScrollMsgHandler(hwndVertScroll, message, mp1, mp2, &rectl, 1), &rectl, NULL, NULLHANDLE, NULL, SW_INVALIDATERGN);
  216.             return (FALSE);
  217.  
  218.         case WM_COMMAND:
  219.             switch (SHORT1FROMMP(mp1)) {
  220.                 case IDM_EXIT:
  221.                     ps.iMode = Q_NONE;
  222.                     WinSendMsg(hwnd, WM_CLOSE, NULL, NULL);
  223.                     return (FALSE);
  224.  
  225.                 case IDM_COPY_TEXT:
  226.                     if ((ps.iMode == Q_QUOTE) || (ps.iMode == Q_AUTHOR))
  227.                         WinSendMsg(hwndPaint, QM_COPY, MPFROMSHORT(CF_TEXT), NULL);
  228.                     return (FALSE);
  229.  
  230.                 case IDM_COPY_METAFILE:
  231.                     if ((ps.iMode == Q_QUOTE) || (ps.iMode == Q_AUTHOR))
  232.                         WinSendMsg(hwndPaint, QM_COPY, MPFROMSHORT(CF_METAFILE), NULL);
  233.                     return (FALSE);
  234.  
  235.                 case IDM_AGAIN:
  236.                     ps.iMode = Q_SEARCHING;
  237.                     ps.szAuthorCode[0] = '\0';
  238.                     ps.szQuoteCode[0] = '\0';
  239.                     ti.hwndCaller = hwnd;
  240.                     ti.pps = &ps;
  241.                     WinEnableMenuItem(hwndMenu, IDM_AGAIN, FALSE);
  242.                     if (_beginthread((THREADPROC)ThreadFindRandomQuote, NULL, THREADSTACK, (void *)(&ti)) == -1) {
  243.                         WinLoadString(hab, 0, IDS_THREADFAILED, sizeof(szMessage), szMessage);
  244.                         WinLoadString(hab, 0, IDS_ERROR, sizeof(szTitle), szTitle);
  245.                         WinMessageBox(HWND_DESKTOP, hwnd, szMessage, szTitle, 0, MB_ERROR);
  246.                     }
  247.                     return (FALSE);
  248.             }
  249.  
  250.         default:
  251.             return (WinDefWindowProc(hwnd, message, mp1, mp2));
  252.     }
  253.  
  254.     return (FALSE);
  255. }
  256.  
  257.  
  258. void GetWindowPosition(HWND hwnd)
  259. /*
  260.  * Set the window position from the QUOTER.INI file.
  261.  */
  262. {
  263.     HINI        hiniProfile;
  264.     SWP        swpMain;
  265.     unsigned long    ulSize;
  266.  
  267.     hiniProfile = PrfOpenProfile(hab, "QUOTER.INI");
  268.  
  269.     ulSize = sizeof(SWP);
  270.     if (!PrfQueryProfileData(hiniProfile, "QofD", "WindowPos", &swpMain, &ulSize)) {
  271.         /* QUOTER.INI doesn't have a window size in it, so make up a default */
  272.         swpMain.hwndInsertBehind = HWND_TOP;
  273.         swpMain.fl = SWP_ACTIVATE | SWP_SIZE | SWP_MOVE;
  274.         swpMain.x = 20;
  275.         swpMain.y = 20;
  276.         swpMain.cx = 600;
  277.         swpMain.cy = 310;
  278.         WinQueryTaskSizePos(hab, 0, &swpMain);
  279.     }
  280.     WinSetWindowPos(hwnd, HWND_TOP, swpMain.x, swpMain.y, swpMain.cx, swpMain.cy,
  281.             SWP_MOVE | SWP_SIZE | SWP_ACTIVATE | SWP_SHOW);
  282.  
  283.     PrfCloseProfile(hiniProfile);
  284. }
  285.  
  286.  
  287. void SaveWindowPosition(HWND hwnd)
  288. /*
  289.  * Save the window position to the QUOTER.INI file.
  290.  */
  291. {
  292.     HINI    hiniProfile;
  293.     SWP    swpMain;
  294.  
  295.     hiniProfile = PrfOpenProfile(hab, "QUOTER.INI");
  296.  
  297.     WinQueryWindowPos(hwnd, &swpMain);
  298.     PrfWriteProfileData(hiniProfile, "QofD", "WindowPos", &swpMain, sizeof(SWP));
  299.  
  300.     PrfCloseProfile(hiniProfile);
  301. }
  302.  
  303.  
  304. int GetINISettings(HWND hwnd)
  305. /*
  306.  * Get the relevent settings from the QUOTER.INI file.
  307.  *
  308.  * Returns    - 1 if successful
  309.  *           0 on failure
  310.  */
  311. {
  312.     HINI        hiniProfile;
  313.     char        szMessage[200], szTitle[20];
  314.     FONTMETRICS    fm;
  315.     HPS        hps;
  316.     unsigned long    ulSize;
  317.  
  318.     if ((hiniProfile = PrfOpenProfile(hab, "QUOTER.INI")) == NULLHANDLE) {
  319.         WinLoadString(hab, 0, IDS_INIFAILED, sizeof(szMessage), szMessage);
  320.         WinLoadString(hab, 0, IDS_ERROR, sizeof(szTitle), szTitle);
  321.         WinMessageBox(HWND_DESKTOP, hwnd, szMessage, szTitle, 0, MB_ERROR);
  322.         return (0);
  323.     }
  324.  
  325.     PrfQueryProfileString(hiniProfile, "QofD", "QDB", "", ps.szQuoteDB, sizeof(ps.szQuoteDB));
  326.     PrfQueryProfileString(hiniProfile, "QofD", "ADB", "", ps.szAuthorDB, sizeof(ps.szAuthorDB));
  327.     dirnoext(ps.szQuoteDB);
  328.     dirnoext(ps.szAuthorDB);
  329.     ulSize = sizeof(FATTRS);
  330.     if (!PrfQueryProfileData(hiniProfile, "Quoter", "Font", &(prf.fattrs), &ulSize)) {
  331.         /* no font information, so make up a default */
  332.         hps = WinGetPS(hwndMain);
  333.         GpiQueryFontMetrics(hps, sizeof(FONTMETRICS), &fm);
  334.         prf.fattrs.usRecordLength = sizeof(FATTRS);
  335.         prf.fattrs.fsSelection = 0;
  336.         prf.fattrs.lMatch = 0;
  337.         strcpy(prf.fattrs.szFacename, fm.szFacename);
  338.         prf.fattrs.idRegistry = 0;
  339.         prf.fattrs.usCodePage = fm.usCodePage;
  340.         prf.fattrs.lMaxBaselineExt = fm.lMaxBaselineExt;
  341.         prf.fattrs.lAveCharWidth = fm.lAveCharWidth;
  342.         prf.fattrs.fsType = 0;
  343.         prf.fattrs.fsFontUse = 0;
  344.         WinReleasePS(hps);
  345.     }
  346.  
  347.     PrfCloseProfile(hiniProfile);
  348.     return (1);
  349. }
  350.