home *** CD-ROM | disk | FTP | other *** search
/ Better Windows, Better Works / CDRM93801.bin / winutil / util42 / settings.c < prev    next >
C/C++ Source or Header  |  1991-11-19  |  19KB  |  466 lines

  1. /*
  2.  *  Settings.c
  3.  *      Source for Pundit (V 2.0)
  4.  *      Settings dialog box, etc.
  5.  *
  6.  *  Author:         Jeff Bienstadt
  7.  *
  8.  *  Environment:
  9.  *
  10.  *      Run-Time:   Microsoft Windows 3.0
  11.  *
  12.  *      Compilers/Tools:
  13.  *                  Microsoft C 6.0
  14.  *                  Microsoft Windows SDK 3.0
  15.  *
  16.  */
  17.  
  18. #include    <windows.h>     // All the Windows goodies
  19. #include    <stdio.h>       // for sscanf()
  20. #include    <string.h>      // for strlen(), etc.
  21. #include    "pundit.h"      // constants for Pundit
  22. #include    "extras.h"      // for WriteProfileInt(), etc.
  23.  
  24. static void     HandleColorScroll(WORD, LONG);
  25. static void     InitSettings(HWND);
  26. static void     NewBackground(void);
  27. static void     SaveSettings(HWND);
  28. static void     SaveSizeAndPosition(RECT *);
  29.  
  30. extern HWND     Pundit;         // a global Window Handle (for main window)
  31. extern HDC      hdc;            // a global Display Context Handle
  32. extern HANDLE   hOrgBrush;      // a global Brush handle
  33. extern SETTINGS settings;       // the Settings storage structure
  34. extern char     szAppName[],    // The Application Name
  35.                 szProfName[];   // The name of the Profile file
  36. extern int      category,       // what category are we using now?
  37.                 hide_mode;      // when we go away, do we hide or do we iconize?
  38.  
  39. static HWND     hInScroll,      // Handle for IN scrollbar
  40.                 hOutScroll,     // Handle for OUT scrollbar
  41.                 hRScroll,       // Handle for RED scrollbar
  42.                 hGScroll,       // Handle for GREEN scrollbar
  43.                 hBScroll;       // Handle for BLUE scrollbar
  44.  
  45. static short    fRed,
  46.                 fGreen,
  47.                 fBlue,
  48.                 bRed,
  49.                 bGreen,
  50.                 bBlue,
  51.                 foreground;     // processing foreground or background color?
  52.  
  53. static SETTINGS s;              // a temporary values for settings
  54.  
  55. static char     text[15];       // a string to write text into
  56.  
  57.  
  58. BOOL FAR PASCAL Settings(hDlg, message, wParam, lParam)
  59. HWND        hDlg;       // Window Handle for our Dialog Box
  60. unsigned    message;    // The Window's Message
  61. WORD        wParam;     // The WORD parameter value
  62. LONG        lParam;     // The LONG parameter value
  63. {
  64.     HWND    hsb;        // Handle for a Horizontal Scroll Bar
  65.     char    temp[10];   // string to write "numbers" into
  66.  
  67.     switch (message) {      // check for messages...
  68.     case    WM_INITDIALOG:      // If the Dialog is just starting...
  69.         PostMessage(Pundit, PM_PAUSE, 0, 0L);   // shut down the timer
  70.         InitSettings(hDlg);     // Initialize the Settings Dialog
  71.         return TRUE;
  72.  
  73.     case    WM_PAINT:               // if dialog is painted
  74.         PostMessage(hDlg, PM_DLGPAINT, 0, 0L);
  75.         return FALSE;
  76.  
  77.     case    PM_DLGPAINT:
  78.         SetTextColor(hdc, RGB(fRed, fGreen, fBlue));
  79.         SetBkColor(hdc, RGB(bRed, bGreen, bBlue));
  80.  
  81.         TextOut(hdc, 0, 0, (LPSTR)text, strlen(text));
  82.         return TRUE;
  83.  
  84.     case    WM_HSCROLL:         // We got a Horizontal Scroll Bar Message...
  85.         if (hsb = HIWORD(lParam)) { // if it's not zero
  86.             if (hsb == hRScroll || hsb == hGScroll || hsb == hBScroll)
  87.                 HandleColorScroll(wParam, lParam);  // do the colors
  88.             else if (hsb == hInScroll) { // If it's from the IN Scroll Bar
  89.                 switch (wParam) {       // find out what happened...
  90.                 case    SB_PAGEUP:          // User did a PAGE UP
  91.                     settings.insecs -= 4;   // take off 4 seconds and...
  92.                 case    SB_LINEUP:          // User did a LINE UP
  93.                         // take off 1 second or set to 1, whichever is greater
  94.                     settings.insecs = max(1, settings.insecs-1);
  95.                     break;
  96.                 case    SB_PAGEDOWN:        // User did a PAGE DOWN
  97.                     settings.insecs += 4;   // add 4 seconds and...
  98.                 case    SB_LINEDOWN:        // User did a LINE DOWN
  99.                         // add 1 second or set to 1 minute, whichever is lesser
  100.                     settings.insecs = min(MAX_IN, settings.insecs+1);
  101.                     break;
  102.                 case    SB_TOP:             // User went to the TOP
  103.                     settings.insecs = 1;    // set to 1 second
  104.                     break;
  105.                 case    SB_BOTTOM:          // User went the to BOTTOM
  106.                     settings.insecs = MAX_IN;   // set to 1 minute
  107.                     break;
  108.                 case    SB_THUMBPOSITION:   // User moved...
  109.                 case    SB_THUMBTRACK:      // or is moving the THUMB BUTTON
  110.                         // Windows tells us how far...
  111.                     settings.insecs = LOWORD(lParam);
  112.                 default:
  113.                     break;
  114.                 }
  115.                 // Put the new value into a string
  116.                 wsprintf(temp, "%4d", settings.insecs);
  117.                 // Display it in the box next to the Scroll Bar
  118.                 SetDlgItemText(hDlg, IDC_INSECS, temp);
  119.                 // Set the new Scroll Bar position
  120.                 SetScrollPos(hsb, SB_CTL, settings.insecs, TRUE);
  121.             }
  122.             else if (hsb == hOutScroll) {   // If it's from the OUT Scroll Bar
  123.                 switch (wParam) {       // find out what happened...
  124.                 case    SB_PAGEUP:          // User did a PAGE UP
  125.                     settings.outsecs -= 559;   // take off 10 min and...
  126.                 case    SB_LINEUP:          // User did a LINE UP
  127.                         // take off 1 second or set to 1, whichever is greater
  128.                     settings.outsecs = max(1, settings.outsecs-1);
  129.                     break;
  130.                 case    SB_PAGEDOWN:        // User did a PAGE DOWN
  131.                     settings.outsecs += 559;   // add 10 min and...
  132.                 case    SB_LINEDOWN:        // User did a LINE DOWN
  133.                         // add 1 second or set to 1 hour, whichever is lesser
  134.                     settings.outsecs = min(MAX_OUT, settings.outsecs+1);
  135.                     break;
  136.                 case    SB_TOP:             // User went to the TOP
  137.                     settings.outsecs = 1;    // set to 1 second
  138.                     break;
  139.                 case    SB_BOTTOM:          // User went the to BOTTOM
  140.                     settings.outsecs = MAX_IN;   // set to 1 hour
  141.                     break;
  142.                 case    SB_THUMBPOSITION:   // User moved...
  143.                 case    SB_THUMBTRACK:      // or is moving the THUMB BUTTON
  144.                         // Windows tells us how far...
  145.                     settings.outsecs = LOWORD(lParam);
  146.                 default:
  147.                     break;
  148.                 }
  149.                 // Put the new value into a string
  150.                 wsprintf(temp, "%4d", settings.outsecs);
  151.                 // Display it in the box next to the Scroll Bar
  152.                 SetDlgItemText(hDlg, IDC_OUTSECS, temp);
  153.                 // Set the new Scroll Bar position
  154.                 SetScrollPos(hsb, SB_CTL, settings.outsecs, TRUE);
  155.             }
  156.         }
  157.         return TRUE;
  158.  
  159.     case    WM_COMMAND:         // One of the Buttons got pressed
  160.         switch (wParam) {           // which one?
  161.         case    IDC_FORE:
  162.         case    IDC_BACK:
  163.             foreground = (wParam == IDC_FORE) ? TRUE : FALSE;
  164.             SendMessage(GetDlgItem(hDlg, IDC_FORE), BM_SETCHECK,
  165.                         foreground, 0L);
  166.             SendMessage(GetDlgItem(hDlg, IDC_BACK), BM_SETCHECK,
  167.                         !foreground, 0L);
  168.             SetScrollPos(hRScroll, SB_CTL,
  169.                         (foreground ? fRed : bRed), TRUE);
  170.             SetScrollPos(hGScroll, SB_CTL,
  171.                         (foreground ? fGreen : bGreen), TRUE);
  172.             SetScrollPos(hBScroll, SB_CTL,
  173.                         (foreground ? fBlue : bBlue), TRUE);
  174.             break;
  175.  
  176.         case    IDC_SAVE:           // Save Settings
  177.             SaveSettings(hDlg);     // Save the Settings
  178.             break;
  179.  
  180.         case    IDCANCEL:           // CANCEL
  181.             settings = s;           // Restore setting to what they were
  182.  
  183.         case    IDOK:               // OK (or fall-thru from CANCEL)
  184.             if (wParam == IDOK) {
  185.                 settings.fore = RGB(fRed, fGreen, fBlue);
  186.                 settings.back = RGB(bRed, bGreen, bBlue);
  187.                 NewBackground();
  188.                 InvalidateRect(Pundit, NULL, TRUE);
  189.             }
  190.             ReleaseDC(hDlg, hdc);
  191.             EndDialog(hDlg, TRUE);  // we're done
  192.             return TRUE;
  193.  
  194.         default:
  195.             break;
  196.         }
  197.         break;
  198.     }
  199.     return FALSE;       // Say that we did NOT process the message
  200. }
  201.  
  202.  
  203. //  Retrieve WIN.INI settings for Pundit
  204. void FetchSettings(void)
  205. {
  206.     BYTE    r,
  207.             g,
  208.             b;
  209.     char    hold[80],
  210.             tmp[80];
  211.  
  212.     //  Here we get each of the 8 settings values
  213.     //  from PUNDIT.INI
  214.     settings.insecs =  GetPrivateProfileInt((LPSTR)szAppName, (LPSTR)"InSecs",
  215.                                     settings.insecs, (LPSTR)szProfName);
  216.     settings.outsecs = GetPrivateProfileInt((LPSTR)szAppName, (LPSTR)"OutSecs",
  217.                                     settings.outsecs, (LPSTR)szProfName);
  218.     settings.cat =     GetPrivateProfileInt((LPSTR)szAppName, (LPSTR)"Category",
  219.                                     settings.cat, (LPSTR)szProfName);
  220.     settings.hide =    GetPrivateProfileInt((LPSTR)szAppName, (LPSTR)"Hide",
  221.                                     settings.hide, (LPSTR)szProfName);
  222.     settings.x =       GetPrivateProfileInt((LPSTR)szAppName, (LPSTR)"X",
  223.                                     settings.x, (LPSTR)szProfName);
  224.     settings.y =       GetPrivateProfileInt((LPSTR)szAppName, (LPSTR)"Y",
  225.                                     settings.y, (LPSTR)szProfName);
  226.     settings.width =   GetPrivateProfileInt((LPSTR)szAppName, (LPSTR)"Width",
  227.                                     settings.width, (LPSTR)szProfName);
  228.     settings.height =  GetPrivateProfileInt((LPSTR)szAppName, (LPSTR)"Height",
  229.                                     settings.height, (LPSTR)szProfName);
  230.  
  231.     wsprintf(hold, "%d %d %d", GetRValue(settings.back),
  232.                                GetGValue(settings.back),
  233.                                GetBValue(settings.back));
  234.     GetPrivateProfileString((LPSTR)szAppName, (LPSTR)"Back", (LPSTR)hold,
  235.                             (LPSTR)tmp, 80, (LPSTR)szProfName);
  236.     sscanf(tmp, "%d %d %d", &r, &g, &b);
  237.     settings.back = RGB(r, g, b);
  238.  
  239.     wsprintf(hold, "%d %d %d", GetRValue(settings.fore),
  240.                                GetGValue(settings.fore),
  241.                                GetBValue(settings.fore));
  242.     GetPrivateProfileString((LPSTR)szAppName, (LPSTR)"Fore", (LPSTR)hold,
  243.                             (LPSTR)tmp, 80, (LPSTR)szProfName);
  244.     sscanf(tmp, "%d %d %d", &r, &g, &b);
  245.     settings.fore = RGB(r, g, b);
  246.  
  247.     if (settings.cat < IDM_QUOTES || settings.cat > IDM_RANDOM)
  248.         settings.cat = 1;
  249.     category = settings.cat;
  250.     hide_mode = SW_HIDE;
  251.     if (!settings.hide)
  252.         hide_mode = SW_MINIMIZE;
  253.     settings.hide = hide_mode;
  254. }
  255.  
  256.  
  257. //  Save the Settings (from Settings Dialog)
  258. static void SaveSettings(hDlg)
  259. HWND        hDlg;
  260. {
  261.     RECT    rect;               // boundaries of entire Window
  262.  
  263.     char    hold[80];
  264.  
  265.     //  Save the colors
  266.     settings.fore = RGB(fRed, fGreen, fBlue);
  267.     wsprintf(hold, "%d %d %d", GetRValue(settings.back),
  268.                                GetGValue(settings.back),
  269.                                GetBValue(settings.back));
  270.     WritePrivateProfileString((LPSTR)szAppName, (LPSTR)"Back", (LPSTR)hold,
  271.                               (LPSTR)szProfName);
  272.     settings.back = RGB(bRed, bGreen, bBlue);
  273.     wsprintf(hold, "%d %d %d", GetRValue(settings.back),
  274.                                GetGValue(settings.back),
  275.                                GetBValue(settings.back));
  276.     WritePrivateProfileString((LPSTR)szAppName, (LPSTR)"Back", (LPSTR)hold,
  277.                               (LPSTR)szProfName);
  278.  
  279.     //  write the IN seconds
  280.     WritePrivateProfileInt((LPSTR)szAppName, (LPSTR)"InSecs",
  281.                               settings.insecs,
  282.                               (LPSTR)szProfName);
  283.  
  284.     //  write the OUT seconds
  285.     WritePrivateProfileInt((LPSTR)szAppName, (LPSTR)"OutSecs",
  286.                               settings.outsecs,
  287.                               (LPSTR)szProfName);
  288.  
  289.     wsprintf(hold, "%d %d %d", GetRValue(settings.back),
  290.                                GetGValue(settings.back),
  291.                                GetBValue(settings.back));
  292.     WritePrivateProfileString((LPSTR)szAppName, (LPSTR)"Back", (LPSTR)hold,
  293.                               (LPSTR)szProfName);
  294.  
  295.     wsprintf(hold, "%d %d %d", GetRValue(settings.fore),
  296.                                GetGValue(settings.fore),
  297.                                GetBValue(settings.fore));
  298.     WritePrivateProfileString((LPSTR)szAppName, (LPSTR)"Fore", (LPSTR)hold,
  299.                               (LPSTR)szProfName);
  300.  
  301.  
  302.     // Ask Windows for size and position of entire window
  303.     GetWindowRect(Pundit, (LPRECT)&rect);
  304.  
  305.     // find out if size or position has changed
  306.     if ((settings.x      != rect.left) ||
  307.         (settings.y      != rect.top) ||
  308.         (settings.width  != rect.right - rect.left) ||
  309.         (settings.height != rect.bottom - rect.top))
  310.     {
  311.         //  Ask user if we should save position and size
  312.         if (MessageBox(hDlg,
  313.                        (LPSTR)"Save Current Window Position and Size?",
  314.                        (LPSTR)"Saving",
  315.                        MB_ICONQUESTION | MB_YESNO) == IDYES)
  316.         {
  317.             SaveSizeAndPosition(&rect);   // yes, save them
  318.         }
  319.     }
  320. }
  321.  
  322.  
  323. static void SaveSizeAndPosition(rect)
  324. RECT    *rect;
  325. {
  326.     settings.x      = rect->left;               // X position
  327.     settings.y      = rect->top;                // Y position
  328.     settings.width  = rect->right - rect->left; // Width
  329.     settings.height = rect->bottom - rect->top; // Height
  330.  
  331.     //  write the X position
  332.     WritePrivateProfileInt((LPSTR)szAppName, (LPSTR)"X", settings.x,
  333.                               (LPSTR)szProfName);
  334.  
  335.     //  write the Y position
  336.     WritePrivateProfileInt((LPSTR)szAppName, (LPSTR)"Y",
  337.                               settings.y,
  338.                               (LPSTR)szProfName);
  339.  
  340.     //  write the Width
  341.     WritePrivateProfileInt((LPSTR)szAppName, (LPSTR)"Width",
  342.                               settings.width,
  343.                               (LPSTR)szProfName);
  344.  
  345.     //  write the Height
  346.     WritePrivateProfileInt((LPSTR)szAppName, (LPSTR)"Height",
  347.                               settings.height,
  348.                               (LPSTR)szProfName);
  349. }
  350.  
  351.  
  352. //  Initialize Settings Dialog info
  353. static void InitSettings(hDlg)
  354. HWND    hDlg;
  355. {
  356.     HWND    hwnd;
  357.     char    temp[15];
  358.  
  359.     s = settings;       // stash the current settings (in case of CANCEL)
  360.  
  361.     //  Write the current IN seconds in the box next to the Scroll Bar
  362.     wsprintf(temp, "%4d", settings.insecs);
  363.     SetDlgItemText(hDlg, IDC_INSECS, temp);
  364.  
  365.     //  Write the current OUT seconds in the box next to the Scroll Bar
  366.     wsprintf(temp, "%4d", settings.outsecs);
  367.     SetDlgItemText(hDlg, IDC_OUTSECS, temp);
  368.  
  369.     //  Get handles to each of the 5 Scroll Bar Controls
  370.     hInScroll  = GetDlgItem(hDlg, IDC_INSCROLL);
  371.     hOutScroll = GetDlgItem(hDlg, IDC_OUTSCROLL);
  372.     hRScroll   = GetDlgItem(hDlg, IDC_RSCROLL);
  373.     hGScroll   = GetDlgItem(hDlg, IDC_GSCROLL);
  374.     hBScroll   = GetDlgItem(hDlg, IDC_BSCROLL);
  375.  
  376.     //  Separate the color values for Foreground and Background
  377.     fRed    = GetRValue(settings.fore);
  378.     fGreen  = GetGValue(settings.fore);
  379.     fBlue   = GetBValue(settings.fore);
  380.     bRed    = GetRValue(settings.back);
  381.     bGreen  = GetGValue(settings.back);
  382.     bBlue   = GetBValue(settings.back);
  383.  
  384.     //  Set the Scroll Bar Ranges
  385.     SetScrollRange(hInScroll,  SB_CTL, 1, MAX_IN,  TRUE);
  386.     SetScrollRange(hOutScroll, SB_CTL, 1, MAX_OUT, TRUE);
  387.     SetScrollRange(hRScroll,   SB_CTL, 0, 255,     TRUE);
  388.     SetScrollRange(hGScroll,   SB_CTL, 0, 255,     TRUE);
  389.     SetScrollRange(hBScroll,   SB_CTL, 0, 255,     TRUE);
  390.  
  391.     //  Set the Scroll Bar Positions
  392.     SetScrollPos(hInScroll,    SB_CTL, settings.insecs,                TRUE);
  393.     SetScrollPos(hOutScroll,   SB_CTL, settings.outsecs,               TRUE);
  394.     SetScrollPos(hRScroll,     SB_CTL, (foreground ? fRed   : bRed),   TRUE);
  395.     SetScrollPos(hGScroll,     SB_CTL, (foreground ? fGreen : bGreen), TRUE);
  396.     SetScrollPos(hBScroll,     SB_CTL, (foreground ? fBlue  : bBlue),  TRUE);
  397.  
  398.     hdc = GetDC(hwnd = GetDlgItem(hDlg, IDC_DEMO));
  399.     GetDlgItemText(hDlg, IDC_DEMO, (LPSTR)text, sizeof(text) - 1);
  400.     SetClassWord(hwnd, GCW_HBRBACKGROUND, hOrgBrush);
  401.  
  402.     SetBkMode(hdc, OPAQUE); // has to be OPAQUE for background color to show
  403.  
  404.     SendMessage(GetDlgItem(hDlg, IDC_FORE), BM_SETCHECK, foreground, 0L);
  405.     SendMessage(GetDlgItem(hDlg, IDC_BACK), BM_SETCHECK, !foreground, 0L);
  406. }
  407.  
  408.  
  409. //  Process the Scroll Bars for Color
  410. void HandleColorScroll(wParam, lParam)
  411. WORD    wParam;
  412. LONG    lParam;
  413. {
  414.     HWND    hsb;
  415.     short   *color;
  416.  
  417.     hsb = HIWORD(lParam);
  418.     if (hsb == hRScroll)
  419.         color = (foreground ? &fRed : &bRed);
  420.     if (hsb == hGScroll)
  421.         color = (foreground ? &fGreen : &bGreen);
  422.     if (hsb == hBScroll)
  423.         color = (foreground ? &fBlue : &bBlue);
  424.  
  425.     switch (wParam) {
  426.     case    SB_PAGEDOWN:
  427.         *color += 15;       // fall through
  428.     case    SB_LINEDOWN:
  429.         *color = min(255, *color + 1);
  430.         break;
  431.     case    SB_PAGEUP:
  432.         *color -= 15;       // fall through
  433.     case    SB_LINEUP:
  434.         *color = max(0, *color - 1);
  435.         break;
  436.     case    SB_TOP:
  437.         *color = 0;
  438.         break;
  439.     case    SB_BOTTOM:
  440.         *color = 255;
  441.         break;
  442.     case    SB_THUMBPOSITION:
  443.     case    SB_THUMBTRACK:
  444.         *color = LOWORD(lParam);
  445.     default:
  446.         break;
  447.     }
  448.     SetScrollPos(hsb, SB_CTL, *color, TRUE);
  449.     SetTextColor(hdc, RGB(fRed, fGreen, fBlue));
  450.     SetBkColor(hdc, RGB(bRed, bGreen, bBlue));
  451.  
  452.     TextOut(hdc, 0, 0, (LPSTR)text, strlen(text));
  453. }
  454.  
  455.  
  456. //  set up background color for main window
  457. static void NewBackground()
  458. {
  459.     HDC     hdc = GetDC(Pundit);
  460.  
  461.     DeleteObject(SelectObject(hdc, hOrgBrush = CreateSolidBrush(settings.back)));
  462.     SetClassWord(Pundit, GCW_HBRBACKGROUND, hOrgBrush);
  463.     ReleaseDC(Pundit, hdc);
  464. }
  465.  
  466.