home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / quot210s.zip / src / settings.c < prev    next >
C/C++ Source or Header  |  1998-10-28  |  12KB  |  339 lines

  1. /*
  2.  * settings.c
  3.  *
  4.  * Window procedures for settings dialogues and functions for managing
  5.  * Quoteriser settings.
  6.  *
  7.  *      Created: 16th July, 1997 (split from quoter.c)
  8.  * Version 2.00: 19th December, 1997
  9.  *
  10.  * (C) 1997 Nicholas Paul Sheppard
  11.  *
  12.  * This file is distributed under the GNU General Public License. See the
  13.  * file copying.txt for details.
  14.  */
  15.  
  16. #define INCL_WIN
  17. #include <os2.h>
  18. #include <string.h>
  19. #include "quoter.h"
  20. #include "general.h"
  21. #include "threads.h"
  22. #include "types.h"
  23.  
  24. PROFILE    prf;
  25.  
  26. /* internal function prototypes */
  27. char *GetDefaultDocPath(char *);
  28.  
  29.  
  30. MRESULT EXPENTRY SettingsBox(HWND hwnd, ULONG message, MPARAM mp1, MPARAM mp2)
  31. /*
  32.  * Window procedure for the settings box. Used to set preferences for using the
  33.  * Quoteriser.
  34.  */
  35. {
  36.     char    szMessage[CCHMAXPATH], szTitle[20], szFamilyName[FACESIZE];
  37.     FONTDLG    fd;
  38.  
  39.     switch (message) {
  40.         case WM_INITDLG:
  41.             WinSendDlgItemMsg(hwnd, IDC_SET_EDITOR, EM_SETTEXTLIMIT, MPFROMSHORT(CCHMAXPATH), 0);
  42.             WinSendDlgItemMsg(hwnd, IDC_SET_EDPARAMS, EM_SETTEXTLIMIT, MPFROMSHORT(CCHMAXPATH), 0);
  43.             WinSendDlgItemMsg(hwnd, IDC_SET_DATAPATH, EM_SETTEXTLIMIT, MPFROMSHORT(CCHMAXPATH), 0);
  44.             WinSendDlgItemMsg(hwnd, IDC_SET_DOCPATH, EM_SETTEXTLIMIT, MPFROMSHORT(CCHMAXPATH), 0);
  45.             WinSendDlgItemMsg(hwnd, IDC_SET_BROWSER, EM_SETTEXTLIMIT, MPFROMSHORT(CCHMAXPATH), 0);
  46.             WinSendDlgItemMsg(hwnd, IDC_SET_BROWSERPARAMS, EM_SETTEXTLIMIT, MPFROMSHORT(CCHMAXPATH), 0);
  47.             WinSetDlgItemText(hwnd, IDC_SET_EDITOR, prf.szEditor);
  48.             WinSetDlgItemText(hwnd, IDC_SET_EDPARAMS, prf.szEditorParams);
  49.             WinSetDlgItemText(hwnd, IDC_SET_DATAPATH, prf.szDataPath);
  50.             WinSetDlgItemText(hwnd, IDC_SET_DOCPATH, prf.szDocPath);
  51.             WinSetDlgItemText(hwnd, IDC_SET_BROWSER, prf.szBrowser);
  52.             WinSetDlgItemText(hwnd, IDC_SET_BROWSERPARAMS, prf.szBrowserParams);
  53.             return (FALSE);
  54.  
  55.         case WM_COMMAND:
  56.             switch (SHORT1FROMMP(mp1)) {
  57.                 case IDC_SET_FONT:
  58.                     szFamilyName[0] = '\0';
  59.                     WinLoadString(hab, 0, IDS_SAMPLE, sizeof(szMessage), szMessage);
  60.                     WinLoadString(hab, 0, IDS_TITLE_FONT, sizeof(szTitle), szTitle);
  61.                     memset(&fd, 0, sizeof(FONTDLG));
  62.                     fd.cbSize = sizeof(FONTDLG);
  63.                     fd.pszTitle = szTitle;
  64.                     fd.pszPreview = szMessage;
  65.                     fd.pszFamilyname = szFamilyName;
  66.                     fd.usFamilyBufLen = sizeof(szMessage);
  67.                     fd.fl = FNTS_CENTER | FNTS_INITFROMFATTRS | FNTS_NOSYNTHESIZEDFONTS | FNTS_RESETBUTTON;
  68.                     fd.clrFore = CLR_BLACK;
  69.                     fd.clrBack = CLR_WHITE;
  70.                     memcpy(&(fd.fAttrs), &(prf.fattrs), sizeof(FATTRS));
  71.                     WinFontDlg(HWND_DESKTOP, hwnd, &fd);
  72.                     if (fd.lReturn == DID_OK) {
  73.                         memcpy(&(prf.fattrs), &(fd.fAttrs), sizeof(FATTRS));
  74.                         WinSendMsg(hwndPaint, QM_TYPESET, NULL, NULL);
  75.                         WinInvalidateRect(hwndMain, NULL, TRUE);
  76.                     }
  77.                     return (FALSE);
  78.  
  79.                 case IDC_SET_OKAY:
  80.                     WinQueryDlgItemText(hwnd, IDC_SET_EDITOR, sizeof(prf.szEditor), prf.szEditor);
  81.                     WinQueryDlgItemText(hwnd, IDC_SET_EDPARAMS, sizeof(prf.szEditorParams), prf.szEditorParams);
  82.                     WinQueryDlgItemText(hwnd, IDC_SET_DATAPATH, sizeof(prf.szDataPath), prf.szDataPath);
  83.                     WinQueryDlgItemText(hwnd, IDC_SET_DOCPATH, sizeof(prf.szDocPath), prf.szDocPath);
  84.                     WinQueryDlgItemText(hwnd, IDC_SET_BROWSER, sizeof(prf.szBrowser), prf.szBrowser);
  85.                     WinQueryDlgItemText(hwnd, IDC_SET_BROWSERPARAMS, sizeof(prf.szBrowserParams), prf.szBrowserParams);
  86.                     SetINISettings();
  87.                     WinDismissDlg(hwnd, TRUE);
  88.                     return (FALSE);
  89.  
  90.                 case IDC_SET_DEFAULT:
  91.                     WinLoadString(hab, 0, IDS_DEFAULT, sizeof(szMessage), szMessage);
  92.                     WinLoadString(hab, 0, IDS_CONFIRM, sizeof(szTitle), szTitle);
  93.                     if (WinMessageBox(HWND_DESKTOP, hwnd, szMessage, szTitle, 0, MB_YESNO) == MBID_YES) {
  94.                         WinSetDlgItemText(hwnd, IDC_SET_EDITOR, "E.EXE");
  95.                         WinSetDlgItemText(hwnd, IDC_SET_EDPARAMS, "%");
  96.                         WinSetDlgItemText(hwnd, IDC_SET_DATAPATH, "");
  97.                         WinSetDlgItemText(hwnd, IDC_SET_DOCPATH, GetDefaultDocPath(szMessage));
  98.                         WinSetDlgItemText(hwnd, IDC_SET_BROWSER, "EXPLORE.EXE");
  99.                         WinSetDlgItemText(hwnd, IDC_SET_BROWSERPARAMS, "%");
  100.                     }
  101.                     return (FALSE);
  102.  
  103.                 case IDC_SET_CANCEL:
  104.                     WinDismissDlg(hwnd, TRUE);
  105.                     return (FALSE);
  106.             }
  107.  
  108.         default:
  109.             return (WinDefDlgProc(hwnd, message, mp1, mp2));
  110.     }
  111.  
  112.     return (FALSE);
  113. }
  114.  
  115.  
  116. MRESULT EXPENTRY QuoteOfTheDayBox(HWND hwnd, ULONG message, MPARAM mp1, MPARAM mp2)
  117. /*
  118.  * Window procedure for modifying the settings used by the Quote-of-the-Day
  119.  * program.
  120.  */
  121. {
  122.     HINI        hini;
  123.     FILEDLG        fd;
  124.     char        szString[CCHMAXPATH];
  125.  
  126.     switch (message) {
  127.         case WM_INITDLG:
  128.             hini = PrfOpenProfile(hab, "QUOTER.INI");
  129.             PrfQueryProfileString(hini, "QofD", "QDB", "", szString, sizeof(szString));
  130.             WinSendDlgItemMsg(hwnd, IDC_QOTD_QDB, EM_SETTEXTLIMIT, MPFROMSHORT(CCHMAXPATH), 0);
  131.             WinSetDlgItemText(hwnd, IDC_QOTD_QDB, szString);
  132.             WinSendDlgItemMsg(hwnd, IDC_QOTD_ADB, EM_SETTEXTLIMIT, MPFROMSHORT(CCHMAXPATH), 0);
  133.             PrfQueryProfileString(hini, "QofD", "ADB", "", szString, sizeof(szString));
  134.             WinSetDlgItemText(hwnd, IDC_QOTD_ADB, szString);
  135.             PrfCloseProfile(hini);
  136.             return (FALSE);
  137.  
  138.         case WM_COMMAND:
  139.             switch (SHORT1FROMMP(mp1)) {
  140.                 case IDC_QOTD_QDB_BROWSE:
  141.                     memset(&fd, 0, sizeof(FILEDLG));
  142.                     fd.cbSize = sizeof(FILEDLG);
  143.                     fd.fl = FDS_OPEN_DIALOG | FDS_CENTER;
  144.                     dircat(strcpy(fd.szFullFile, prf.szDataPath), szQuoteExt);
  145.                     WinLoadString(hab, 0, IDS_TITLE_BROWSE, sizeof(szString), szString);
  146.                     fd.pszTitle = szString;
  147.                     WinFileDlg(HWND_DESKTOP, hwnd, &fd);
  148.                     if (fd.lReturn == DID_OK)
  149.                         WinSetDlgItemText(hwnd, IDC_QOTD_QDB, fd.szFullFile);
  150.                     return (FALSE);
  151.  
  152.                 case IDC_QOTD_ADB_BROWSE:
  153.                     memset(&fd, 0, sizeof(FILEDLG));
  154.                     fd.cbSize = sizeof(FILEDLG);
  155.                     fd.fl = FDS_OPEN_DIALOG | FDS_CENTER;
  156.                     dircat(strcpy(fd.szFullFile, prf.szDataPath), szAuthorExt);
  157.                     WinLoadString(hab, 0, IDS_TITLE_BROWSE, sizeof(szString), szString);
  158.                     fd.pszTitle = szString;
  159.                     WinFileDlg(HWND_DESKTOP, hwnd, &fd);
  160.                     if (fd.lReturn == DID_OK)
  161.                         WinSetDlgItemText(hwnd, IDC_QOTD_ADB, fd.szFullFile);
  162.                     return (FALSE);
  163.  
  164.                 case IDC_QOTD_OKAY:
  165.                     hini = PrfOpenProfile(hab, "QUOTER.INI");
  166.                     WinQueryDlgItemText(hwnd, IDC_QOTD_QDB, sizeof(szString), szString);
  167.                     PrfWriteProfileString(hini, "QofD", "QDB", szString);
  168.                     WinQueryDlgItemText(hwnd, IDC_QOTD_ADB, sizeof(szString), szString);
  169.                     PrfWriteProfileString(hini, "QofD", "ADB", szString);
  170.                     PrfCloseProfile(hini);
  171.                     WinDismissDlg(hwnd, TRUE);
  172.                     return (FALSE);
  173.  
  174.                 case IDC_QOTD_CANCEL:
  175.                     WinDismissDlg(hwnd, TRUE);
  176.                     return (FALSE);
  177.             }
  178.  
  179.         default:
  180.             return (WinDefDlgProc(hwnd, message, mp1, mp2));
  181.     }
  182.  
  183.     return (FALSE);
  184. }
  185.  
  186.  
  187. void GetINISettings(void)
  188. /*
  189.  * Get the relevent settings from the QUOTER.INI file.
  190.  */
  191. {
  192.     HINI        hiniProfile;
  193.     HPS        hps;
  194.     FONTMETRICS    fm;
  195.     unsigned long    ulSize;
  196.     char        szString[CCHMAXPATH];
  197.  
  198.     hiniProfile = PrfOpenProfile(hab, "QUOTER.INI");
  199.  
  200.     PrfQueryProfileString(hiniProfile, "Quoter", "Editor", "E.EXE", prf.szEditor, sizeof(prf.szEditor));
  201.     PrfQueryProfileString(hiniProfile, "Quoter", "EditorParams", "%", prf.szEditorParams, sizeof(prf.szEditorParams));
  202.     PrfQueryProfileString(hiniProfile, "Quoter", "DataPath", "", prf.szDataPath, sizeof(prf.szDataPath));
  203.     PrfQueryProfileString(hiniProfile, "Quoter", "DocPath", GetDefaultDocPath(szString), prf.szDocPath, sizeof(prf.szDocPath));
  204.     PrfQueryProfileString(hiniProfile, "Quoter", "QuoteDB1", "", prf.szQuoteDB1, sizeof(prf.szQuoteDB1));
  205.     PrfQueryProfileString(hiniProfile, "Quoter", "AuthorDB1", "", prf.szAuthorDB1, sizeof(prf.szAuthorDB1));
  206.     PrfQueryProfileString(hiniProfile, "Quoter", "Browser", "EXPLORE.EXE", prf.szBrowser, sizeof(prf.szBrowser));
  207.     PrfQueryProfileString(hiniProfile, "Quoter", "BrowserParams", "%", prf.szBrowserParams, sizeof(prf.szBrowserParams));
  208.     ulSize = sizeof(FATTRS);
  209.     if (!PrfQueryProfileData(hiniProfile, "Quoter", "Font", &(prf.fattrs), &ulSize)) {
  210.         /* no font information, so make up a default */
  211.         hps = WinGetPS(hwndMain);
  212.         GpiQueryFontMetrics(hps, sizeof(FONTMETRICS), &fm);
  213.         prf.fattrs.usRecordLength = sizeof(FATTRS);
  214.         prf.fattrs.fsSelection = 0;
  215.         prf.fattrs.lMatch = 0;
  216.         strcpy(prf.fattrs.szFacename, fm.szFacename);
  217.         prf.fattrs.idRegistry = 0;
  218.         prf.fattrs.usCodePage = fm.usCodePage;
  219.         prf.fattrs.lMaxBaselineExt = fm.lMaxBaselineExt;
  220.         prf.fattrs.lAveCharWidth = fm.lAveCharWidth;
  221.         prf.fattrs.fsType = 0;
  222.         prf.fattrs.fsFontUse = 0;
  223.         WinReleasePS(hps);
  224.     }
  225.  
  226.     PrfCloseProfile(hiniProfile);
  227. }
  228.  
  229.  
  230. void SetINISettings(void)
  231. /*
  232.  * Set the relevent settings in the QUOTER.INI file.
  233.  */
  234. {
  235.     HINI    hiniProfile;
  236.  
  237.     hiniProfile = PrfOpenProfile(hab, "QUOTER.INI");
  238.  
  239.     PrfWriteProfileString(hiniProfile, "Quoter", "Editor", prf.szEditor);
  240.     PrfWriteProfileString(hiniProfile, "Quoter", "EditorParams", prf.szEditorParams);
  241.     PrfWriteProfileString(hiniProfile, "Quoter", "DataPath", prf.szDataPath);
  242.     PrfWriteProfileString(hiniProfile, "Quoter", "DocPath", prf.szDocPath);
  243.     PrfWriteProfileString(hiniProfile, "Quoter", "Browser", prf.szBrowser);
  244.     PrfWriteProfileString(hiniProfile, "Quoter", "BrowserParams", prf.szBrowserParams);
  245.     PrfWriteProfileData(hiniProfile, "Quoter", "Font", &(prf.fattrs), sizeof(FATTRS));
  246.  
  247.     PrfCloseProfile(hiniProfile);
  248. }
  249.  
  250.  
  251. void SetUsedDB(char *pszQuoteDB1, char *pszAuthorDB1)
  252. /*
  253.  * Save the last used databases to the QUOTER.INI file.
  254.  *
  255.  * char *pszQuoteDB1    - the last used quote database
  256.  * char *pszAuthorDB1    - the last used author database
  257.  */
  258. {
  259.     HINI    hiniProfile;
  260.  
  261.     hiniProfile = PrfOpenProfile(hab, "QUOTER.INI");
  262.  
  263.     if (pszQuoteDB1 != NULL)
  264.         PrfWriteProfileString(hiniProfile, "Quoter", "QuoteDB1", pszQuoteDB1);
  265.     if (pszAuthorDB1 != NULL)
  266.         PrfWriteProfileString(hiniProfile, "Quoter", "AuthorDB1", pszAuthorDB1);
  267.  
  268.     PrfCloseProfile(hiniProfile);
  269. }
  270.  
  271.  
  272. void GetWindowPosition(HWND hwnd)
  273. /*
  274.  * Set the window position from the QUOTER.INI file.
  275.  */
  276. {
  277.     HINI        hiniProfile;
  278.     SWP        swpMain;
  279.     unsigned long    ulSize;
  280.  
  281.     hiniProfile = PrfOpenProfile(hab, "QUOTER.INI");
  282.  
  283.     ulSize = sizeof(SWP);
  284.     if (!PrfQueryProfileData(hiniProfile, "Quoter", "WindowPos", &swpMain, &ulSize)) {
  285.         /* QUOTER.INI doesn't have a window size in it, so make up a default */
  286.         swpMain.hwndInsertBehind = HWND_TOP;
  287.         swpMain.fl = SWP_ACTIVATE | SWP_SIZE | SWP_MOVE;
  288.         swpMain.x = 20;
  289.         swpMain.y = 20;
  290.         swpMain.cx = 600;
  291.         swpMain.cy = 310;
  292.         WinQueryTaskSizePos(hab, 0, &swpMain);
  293.     }
  294.     WinSetWindowPos(hwnd, HWND_TOP, swpMain.x, swpMain.y, swpMain.cx, swpMain.cy,
  295.             SWP_MOVE | SWP_SIZE | SWP_ACTIVATE | SWP_SHOW);
  296.  
  297.     PrfCloseProfile(hiniProfile);
  298. }
  299.  
  300.  
  301. void SaveWindowPosition(HWND hwnd)
  302. /*
  303.  * Save the window position to the QUOTER.INI file.
  304.  */
  305. {
  306.     HINI    hiniProfile;
  307.     SWP    swpMain;
  308.  
  309.     hiniProfile = PrfOpenProfile(hab, "QUOTER.INI");
  310.  
  311.     WinQueryWindowPos(hwnd, &swpMain);
  312.     PrfWriteProfileData(hiniProfile, "Quoter", "WindowPos", &swpMain, sizeof(SWP));
  313.  
  314.     PrfCloseProfile(hiniProfile);
  315. }
  316.  
  317.  
  318. char *GetDefaultDocPath(char *pszDocPath)
  319. /*
  320.  * Compile the default path to the documentation, i.e. the 'doc' directory in
  321.  * the parent directory of the current directory. I might have used getcwd()
  322.  * for this, but EMX puts forward slashes in its getcwd(), so...
  323.  *
  324.  * char *pszDocPath    - variable to receive the default path
  325.  *
  326.  * Returns        - pszDocPath
  327.  */
  328. {
  329.     unsigned long    ulDrive, ul;
  330.     char        szDrive[4];
  331.  
  332.     DosQueryCurrentDisk(&ulDrive, &ul);
  333.     DosQueryCurrentDir(ulDrive, pszDocPath, &ul);
  334.     sprintf(szDrive, "%c:\\", 'A' + (int)ulDrive - 1);
  335.     dirsimp(dircat(strpre(pszDocPath, szDrive), "..\\DOC"));
  336.  
  337.     return (pszDocPath);
  338. }
  339.