home *** CD-ROM | disk | FTP | other *** search
/ Adobe Graphics & Publishing SDK 1996 December / Adobe Graphics & Publishing SDK 1996 December.iso / pc / ps40sdk / examples / common / sources.c / windialogutils.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-29  |  11.5 KB  |  479 lines

  1. /*
  2.     File: WinDialogUtils.c
  3.  
  4.     Copyright (c) 1996, Adobe Systems Incorporated.
  5.     All rights reserved.
  6.  
  7.     C source file for MS-Windows specific dialog code
  8. */
  9.  
  10. #include "WinDialogUtils.h"
  11.  
  12. /*****************************************************************************/
  13.  
  14. void  ShowAbout (AboutRecordPtr aboutPtr, Handle hDllInstance, int32 resID)
  15. {
  16.     char message[256] = "";
  17.     char caption[60] = "";
  18.     PlatformData *platform;
  19.     HWND h = NULL;
  20.  
  21.     platform = (PlatformData *) (aboutPtr->platformData);
  22.  
  23.     h = (HWND)platform->hwnd;
  24.  
  25.     LoadString(hDllInstance, resID, message, 256);
  26.     LoadString(hDllInstance, resID+1, caption, 60);
  27.     MessageBox(h,message,caption,MB_APPLMODAL|MB_ICONINFORMATION|MB_OK);
  28. }
  29.  
  30. /*****************************************************************************/
  31.  
  32. short ShowVersionAlert (Handle hDllInstance,
  33.                         HWND hDlg,
  34.                         short alertID, 
  35.                         short stringID,
  36.                         Str255 versText1,
  37.                         Str255 versText2)
  38. {
  39.     short     result = 0;
  40.     Str255    ds = "";
  41.     char    message[256];
  42.     Ptr        p = 0;
  43.     Handle    h = 0;
  44.         
  45.     if (alertID)
  46.     {        
  47.         if (stringID > 0)
  48.         {
  49.             LoadString(hDllInstance, stringID, message, 256);
  50.             AppendString(ds, message, 0, (short)strlen(message));
  51.         }
  52.         
  53.         PIParamText(ds, NULL, versText1, versText2);
  54.         ds [ ++ds[0] ] = 0;
  55.         strcpy(message, &ds[1]);
  56.  
  57.         result = ( MessageBox(hDlg, message, NULL, 
  58.                    MB_OK | 
  59.                    MB_ICONEXCLAMATION));
  60.     }
  61.     return result;
  62. }
  63.  
  64. /*****************************************************************************/
  65.  
  66. short ShowAlertType (Handle hDllInstance,
  67.                      HWND hDlg,
  68.                      short alertID, 
  69.                      short stringID, 
  70.                      Str255 minText, 
  71.                      Str255 maxText,
  72.                      short alertType)
  73. {
  74.     short     result = 0;
  75.     Str255    ds = "";
  76.     char    message[256];
  77.     Ptr        p = 0;
  78.     long    size = 0;
  79.     Handle    h = 0;
  80.         
  81.     if (alertID)
  82.     {        
  83.         if (stringID > 0)
  84.         {
  85.             LoadString(hDllInstance, stringID, message, 256);
  86.             AppendString(ds, message, 0, (short)strlen(message));
  87.         }
  88.         
  89.         PIParamText(ds, NULL, minText, maxText);
  90.         ds [ ++ds[0] ] = 0;
  91.         strcpy(message, &ds[1]);
  92.  
  93.         switch (alertType)
  94.         { // will pop either caution or alert
  95.             case PIAlertStop:
  96.                 result = (MessageBox(hDlg, message, NULL, MB_OK |
  97.                                      MB_ICONSTOP));
  98.                 break;
  99.             default:
  100.             case PIAlertCaution:
  101.                 result = (MessageBox(hDlg, message, NULL, MB_OK |
  102.                                      MB_ICONEXCLAMATION));
  103.                 break;
  104.         }
  105.     }
  106.     return result;
  107. }
  108.  
  109. /*****************************************************************************/
  110.  
  111. /* Fixpoint function. */
  112.  
  113. Fixed FixRatio(short numer, short denom)
  114. {
  115.     if (denom == 0)
  116.     {
  117.         if (numer >=0)
  118.             return   LONG_MAX;
  119.  
  120.         else
  121.             return -(LONG_MAX);
  122.     }
  123.     else
  124.             return ((long)numer << 16) / denom;
  125. }
  126.  
  127. /*******************************************************************************/
  128.  
  129. /* Centers a dialog template 1/3 of the way down on the main screen */
  130.  
  131. void CenterDialog(HWND hDlg)
  132. {
  133.     HWND hParent;
  134.     int  nHeight;
  135.     int  nWidth;
  136.     int  nTitleBits;
  137.     RECT rcDialog;
  138.     RECT rcParent;
  139.     int  xOrigin;
  140.     int  yOrigin;
  141.     int  xScreen;
  142.     int  yScreen;
  143.  
  144.     if  ( ! (hParent = GetParent(hDlg)))
  145.         hParent = GetDesktopWindow();
  146.  
  147.     GetClientRect(hParent, &rcParent);
  148.     ClientToScreen(hParent, (LPPOINT)&rcParent.left);  // point(left,  top)
  149.     ClientToScreen(hParent, (LPPOINT)&rcParent.right); // point(right, bottom)
  150.  
  151.     // Center on Title: title bar has system menu, minimize,  maximize bitmaps
  152.     // Width of title bar bitmaps - assumes 3 of them and dialog has a sysmenu
  153.     nTitleBits = GetSystemMetrics(SM_CXSIZE);
  154.  
  155.     // If dialog has no sys menu compensate for odd# bitmaps by sub 1 bitwidth
  156.     if  ( ! (GetWindowLong(hDlg, GWL_STYLE) & WS_SYSMENU))
  157.         nTitleBits -= nTitleBits / 3;
  158.  
  159.     GetWindowRect(hDlg, &rcDialog);
  160.     nWidth  = rcDialog.right  - rcDialog.left;
  161.     nHeight = rcDialog.bottom - rcDialog.top;
  162.  
  163.     xOrigin = max(rcParent.right - rcParent.left - nWidth, 0) / 2
  164.             + rcParent.left - nTitleBits;
  165.     xScreen = GetSystemMetrics(SM_CXSCREEN);
  166.     if  (xOrigin + nWidth > xScreen)
  167.         xOrigin = max (0, xScreen - nWidth);
  168.  
  169.     yOrigin = max(rcParent.bottom - rcParent.top - nHeight, 0) / 3
  170.             + rcParent.top;
  171.     yScreen = GetSystemMetrics(SM_CYSCREEN);
  172.     if  (yOrigin + nHeight > yScreen)
  173.         yOrigin = max(0 , yScreen - nHeight);
  174.  
  175.     SetWindowPos(hDlg, NULL, xOrigin, yOrigin, nWidth, nHeight, SWP_NOZORDER);
  176. }
  177.  
  178. /************************************************************************/
  179. /* Utility function to return which item of a group of radio buttons is */
  180. /* checked.                                                                */
  181.  
  182. short GetRadioGroupState (HWND hDlg, short first, short last)
  183. {
  184.     short    item = 0;
  185.     while (first <= last)
  186.     {
  187.         if (IsDlgButtonChecked(hDlg, first))
  188.         {
  189.             item = first;
  190.             first = last;
  191.         }
  192.         first++;
  193.     }
  194.     return item;
  195. }
  196.  
  197. /**********************************************************/
  198. /* Sets the default item */
  199.  
  200. void SetDialogDefaultItem(HWND hDlg, short item)
  201. {
  202.     SendDlgItemMessage(hDlg, item, BM_SETSTYLE,
  203.                        (WPARAM)BS_DEFPUSHBUTTON,
  204.                        (LPARAM)TRUE);
  205.     SetFocus(GetDlgItem(hDlg, item));
  206. }
  207.  
  208. /**********************************************************/
  209. /* Selects a text item */
  210.  
  211. void SelectTextItem(HWND hDlg, short item)
  212. {
  213.     SendDlgItemMessage(hDlg, item, EM_SETSEL, 0, (LPARAM)-1);
  214.     SetFocus(GetDlgItem(hDlg, item));
  215. }
  216. /**********************************************************/
  217. /* Shows a dialog item */
  218.  
  219. void ShowDialogItem(HWND hDlg, short item)
  220. {
  221.     ShowWindow(GetDlgItem(hDlg, item),SW_SHOW);
  222. }
  223.  
  224. /**********************************************************/
  225. /* Hides a dialog item */
  226.  
  227. void HideDialogItem(HWND hDlg, short item)
  228. {
  229.     ShowWindow(GetDlgItem(hDlg, item),SW_HIDE);
  230. }
  231.  
  232. /**********************************************************/
  233. /* Hides or shows a dialog item */
  234.  
  235. void ShowHideItem(HWND hDlg, short item, BOOL state)
  236. {
  237.     if (state)
  238.         ShowWindow(GetDlgItem(hDlg, item), SW_SHOW);
  239.     else
  240.         ShowWindow(GetDlgItem(hDlg, item), SW_HIDE);
  241. }
  242.  
  243. /**********************************************************/
  244. /* Shows a dialog item */
  245.  
  246. void EnableControl(HWND hDlg, short item)
  247. {
  248.     EnableWindow(GetDlgItem(hDlg, item),TRUE);
  249. }
  250.  
  251. /**********************************************************/
  252. /* Hides a dialog item */
  253.  
  254. void DisableControl(HWND hDlg, short item)
  255. {
  256.     EnableWindow(GetDlgItem(hDlg, item),FALSE);
  257. }
  258.  
  259. /**********************************************************/
  260. /* Hides or shows a dialog item */
  261.  
  262. void EnableDisableControl(HWND hDlg, short item, BOOL state)
  263. {
  264.     EnableWindow(GetDlgItem(hDlg, item), state);
  265. }
  266.  
  267. /**********************************************************/
  268. /* Toggles and returns the value of a dialog item */
  269.  
  270. BOOL ToggleCheckBoxState(HWND hDlg, short item)
  271. {
  272.     BOOL    x;
  273.  
  274.     x = !GetCheckBoxState(hDlg, item);
  275.     SetCheckBoxState(hDlg, item, x);
  276.     return x;
  277. }
  278.  
  279. /*****************************************************************************/
  280. /* The following routine retrieves a pascal text string from a text field. */
  281.  
  282. void FetchText (HWND hDlg, short item, Str255 s)
  283. {
  284.     char c[256] = "";
  285.     
  286.     s [ (s[0] = 0)+1 ] = 0;
  287.     GetDlgItemText(hDlg, item, c, 255);
  288.     AppendString(s, c, 0, (short)strlen(c));
  289. }
  290.  
  291. /*****************************************************************************/
  292. /* The following routine stuffs a text into a text field. */
  293.  
  294. void StuffText (HWND dp, short item, Str255 s)
  295. {
  296.     if (s[0] > 254) s[0]--; // subtract one
  297.     s[ s[0]+1 ] = 0; // null terminate pascal string
  298.     SetDlgItemText(dp, item, &s[1]); // stuff string
  299. }
  300.  
  301. /**********************************************************/
  302. /* Retrieves a number from a dialog item */
  303.  
  304. short FetchNumber(HWND hDlg, 
  305.                 short item, 
  306.                 int32 min, 
  307.                 int32 max, 
  308.                 int32 *value)
  309. {
  310.     Str255        s = "";
  311.     Str255        minText = "";
  312.     Str255        maxText = "";
  313.     long        x = 0;
  314.     short        retn = noErr;
  315.  
  316.  
  317.     FetchText(hDlg, item, s);
  318.     if (!StringToNumber(s, &x))
  319.     {
  320.         x = 0;
  321.         retn = errNotANumber;
  322.     }
  323.     else if (x < min || x > max) retn = errOutOfRange;
  324.  
  325.     *value = x; // return something to work with
  326.     return retn;
  327. }
  328.  
  329. /*****************************************************************************/
  330.  
  331. /* Display corresponding alert for number */
  332.  
  333. void AlertNumber(HWND hDlg, 
  334.                 short item, 
  335.                 int32 min, 
  336.                 int32 max, 
  337.                 int32 *value,
  338.                 Handle hDllInstance, 
  339.                 short alertID, 
  340.                 short numberErr)
  341. {
  342.     Str255        s = "";
  343.     Str255        minText = "";
  344.     Str255        maxText = "";
  345.     long        x = *value;
  346.  
  347.     x = ((x < min) ? min : max);
  348.     *value = x;
  349.     StuffNumber(hDlg, item, x);
  350.  
  351.     ltoa (min, &minText[1], 10);
  352.     minText[0] = (char)strlen(&minText[1]);
  353.  
  354.     ltoa (max, &maxText[1], 10);
  355.     maxText[0] = (char)strlen(&maxText[1]);
  356.  
  357.     MessageBeep (MB_ICONEXCLAMATION);
  358.  
  359.     (void) ShowCaution (hDllInstance,
  360.                         hDlg,
  361.                         alertID,
  362.                           kBadNumberID,            // could use notANumber
  363.                         minText,               // to display a "Type a number"
  364.                         maxText);               // alert.
  365.  
  366.     SelectTextItem(hDlg, item);
  367.     SetFocus(GetDlgItem(hDlg, item));
  368. }
  369.  
  370. /*****************************************************************************/
  371. /* The following routine stuffs a double into a text field. */
  372.  
  373. void StuffDouble (HWND dp, short item, double value, short precision)
  374. {
  375.     Str255     s = "";
  376.     DoubleToString(value, s, precision);
  377.     StuffText(dp, item, s);
  378. }
  379.  
  380. /*****************************************************************************/
  381.  
  382. /* 
  383.    Here is the corresponding routine to retrieve the floating value from a text
  384.    field.  It will do range checking and validate that it has been
  385.    handed a number.
  386.    
  387.    It returns noErr if it gets a valid value. */
  388.    
  389. short FetchDouble (HWND hDlg,
  390.                     short item,
  391.                     double min,
  392.                     double max,
  393.                     double *value)
  394. {
  395.     Str255 s1 = "";
  396.     Str255 s2 = "";
  397.         
  398.     long x1 = 0;
  399.     long x2 = 0;
  400.     short precision = 0;
  401.     Boolean notAWholeNumber = false;
  402.     Boolean notADecimalNumber = false;
  403.     Boolean notANumber = false;
  404.     double x = 0;
  405.     short retn = noErr;
  406.     
  407.     FetchText(hDlg, item, s1);
  408.     
  409.     DivideAtDecimal(s1, s2);
  410.     
  411.     notAWholeNumber = !StringToNumber (s1, &x1);
  412.     
  413.     notADecimalNumber = !StringToNumber (s2, &x2);
  414.     
  415.     precision = s2[0]; //length
  416.     
  417.     notANumber = (notAWholeNumber && notADecimalNumber);
  418.     
  419.     x = (double)x1 + ((double)x2 / (double)power(10, s2[0]));
  420.     
  421.     if (notANumber)
  422.     {
  423.         x = 0;
  424.         retn = errNotANumber;
  425.     }
  426.     else if (x < min || x > max) retn = errOutOfRange;
  427.     
  428.     *value = x;
  429.     return retn;
  430. }
  431.  
  432.  
  433. /*****************************************************************************/
  434. /*
  435.      If it has not been handed a number, it brings up
  436.     an appropriate error dialog, inserts an appropriately pinned value,
  437.     and selects the item. */
  438.   
  439. void AlertDouble (HWND hDlg,
  440.                  short item,
  441.                  double min,
  442.                  double max,
  443.                  double *value,
  444.                  Handle hDllInstance,
  445.                  short alertID,
  446.                  short numberErr)
  447.     Str255 minText = "";
  448.     Str255 maxText = "";
  449.     short precision = 0;
  450.     double x = *value;
  451.     
  452.     // Figure out precision
  453.     FetchText(hDlg, item, minText);
  454.     DivideAtDecimal(minText, maxText);
  455.     precision = maxText[0]; // no more than number of digits in decimal
  456.     
  457.     x = (x < min ? min : max);
  458.     *value = x;        
  459.     StuffDouble (hDlg, item, x, precision);
  460.  
  461.  
  462.     DoubleToString (min, minText, precision);
  463.     DoubleToString (max, maxText, precision);
  464.     
  465.     MessageBeep (MB_ICONEXCLAMATION);
  466.  
  467.     (void) ShowCaution (hDllInstance,
  468.                         hDlg,
  469.                         alertID,
  470.                           kBadDoubleID, // could use numberErr==errNotANumber
  471.                         minText,      // to pop a "that's not a number"
  472.                         maxText);      // alert.    
  473.  
  474.     SelectTextItem (hDlg, item);
  475.     SetFocus(GetDlgItem(hDlg, item));
  476. }
  477.  
  478.