home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 24 / CD_ASCQ_24_0995.iso / vrac / dflt20.zip / MSGBOX.C < prev    next >
Text File  |  1994-12-24  |  6KB  |  213 lines

  1. /* ------------------ msgbox.c ------------------ */
  2.  
  3. #include "dflat.h"
  4.  
  5. extern DBOX MsgBox;
  6. extern DBOX InputBoxDB;
  7. WINDOW CancelWnd;
  8.  
  9. static int ReturnValue;
  10.  
  11. int MessageBoxProc(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
  12. {
  13.     switch (msg)    {
  14.         case CREATE_WINDOW:
  15.             GetClass(wnd) = MESSAGEBOX;
  16.             InitWindowColors(wnd);
  17.             ClearAttribute(wnd, CONTROLBOX);
  18.             break;
  19.         case KEYBOARD:
  20.             if (p1 == '\r' || p1 == ESC)
  21.                 ReturnValue = (int)p1;
  22.             break;
  23.         default:
  24.             break;
  25.     }
  26.     return BaseWndProc(MESSAGEBOX, wnd, msg, p1, p2);
  27. }
  28.  
  29. int YesNoBoxProc(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
  30. {
  31.     switch (msg)    {
  32.         case CREATE_WINDOW:
  33.             GetClass(wnd) = MESSAGEBOX;
  34.             InitWindowColors(wnd);
  35.             ClearAttribute(wnd, CONTROLBOX);
  36.             break;
  37.         case KEYBOARD:    {
  38.             int c = tolower((int)p1);
  39.             if (c == 'y')
  40.                 SendMessage(wnd, COMMAND, ID_OK, 0);
  41.             else if (c == 'n')
  42.                 SendMessage(wnd, COMMAND, ID_CANCEL, 0);
  43.             break;
  44.         }
  45.         default:
  46.             break;
  47.     }
  48.     return BaseWndProc(MESSAGEBOX, wnd, msg, p1, p2);
  49. }
  50.  
  51. int ErrorBoxProc(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
  52. {
  53.     switch (msg)    {
  54.         case CREATE_WINDOW:
  55.             GetClass(wnd) = ERRORBOX;
  56.             InitWindowColors(wnd);
  57.             break;
  58.         case KEYBOARD:
  59.             if (p1 == '\r' || p1 == ESC)
  60.                 ReturnValue = (int)p1;
  61.             break;
  62.         default:
  63.             break;
  64.     }
  65.     return BaseWndProc(ERRORBOX, wnd, msg, p1, p2);
  66. }
  67.  
  68. int CancelBoxProc(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
  69. {
  70.     switch (msg)    {
  71.         case CREATE_WINDOW:
  72.             CancelWnd = wnd;
  73.             SendMessage(wnd, CAPTURE_MOUSE, 0, 0);
  74.             SendMessage(wnd, CAPTURE_KEYBOARD, 0, 0);
  75.             break;
  76.         case COMMAND:
  77.             if ((int) p1 == ID_CANCEL && (int) p2 == 0)
  78.                 SendMessage(GetParent(wnd), msg, p1, p2);
  79.             return TRUE;
  80.         case CLOSE_WINDOW:
  81.             CancelWnd = NULL;
  82.             SendMessage(wnd, RELEASE_MOUSE, 0, 0);
  83.             SendMessage(wnd, RELEASE_KEYBOARD, 0, 0);
  84.             p1 = TRUE;
  85.             break;
  86.         default:
  87.             break;
  88.     }
  89.     return BaseWndProc(MESSAGEBOX, wnd, msg, p1, p2);
  90. }
  91.  
  92. void CloseCancelBox(void)
  93. {
  94.     if (CancelWnd != NULL)
  95.         SendMessage(CancelWnd, CLOSE_WINDOW, 0, 0);
  96. }
  97.  
  98. static char *InputText;
  99. static int TextLength;
  100.  
  101. int InputBoxProc(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
  102. {
  103.     int rtn;
  104.     switch (msg)    {
  105.         case CREATE_WINDOW:
  106.             rtn = DefaultWndProc(wnd, msg, p1, p2);
  107.             SendMessage(ControlWindow(&InputBoxDB,ID_INPUTTEXT),
  108.                         SETTEXTLENGTH, TextLength, 0);
  109.             SendMessage(ControlWindow(&InputBoxDB,ID_INPUTTEXT),
  110.                         ADDTEXT, (PARAM) InputText, 0);
  111.             return rtn;
  112.         case COMMAND:
  113.             if ((int) p1 == ID_OK && (int) p2 == 0)
  114.                 GetItemText(wnd, ID_INPUTTEXT,
  115.                             InputText, TextLength);
  116.             break;
  117.         default:
  118.             break;
  119.     }
  120.     return DefaultWndProc(wnd, msg, p1, p2);
  121. }
  122.  
  123. BOOL InputBox(WINDOW wnd,char *ttl,char *msg,char *text,int len,int wd)
  124. {
  125.     int ln = wd ? wd : len;
  126.     ln = min(SCREENWIDTH-8, ln);
  127.     InputText = text;
  128.     TextLength = len;
  129.     InputBoxDB.dwnd.title = ttl;
  130.     InputBoxDB.dwnd.w = 4 + max(20, max(ln, max(strlen(ttl), strlen(msg))));
  131.     InputBoxDB.ctl[1].dwnd.x = (InputBoxDB.dwnd.w-2-ln)/2;
  132.     InputBoxDB.ctl[0].dwnd.w = strlen(msg);
  133.     InputBoxDB.ctl[0].itext = msg;
  134.     InputBoxDB.ctl[1].itext = NULL;
  135.     InputBoxDB.ctl[1].dwnd.w = ln;
  136.     InputBoxDB.ctl[2].dwnd.x = (InputBoxDB.dwnd.w - 20) / 2;
  137.     InputBoxDB.ctl[3].dwnd.x = InputBoxDB.ctl[2].dwnd.x + 10;
  138.     InputBoxDB.ctl[2].isetting = ON;
  139.     InputBoxDB.ctl[3].isetting = ON;
  140.     return DialogBox(wnd, &InputBoxDB, TRUE, InputBoxProc);
  141. }
  142.  
  143. BOOL GenericMessage(WINDOW wnd,char *ttl,char *msg,int buttonct,
  144.       int (*wndproc)(struct window *,enum messages,PARAM,PARAM),
  145.       char *b1, char *b2, int c1, int c2, int isModal)
  146. {
  147.     BOOL rtn;
  148.     MsgBox.dwnd.title = ttl;
  149.     MsgBox.ctl[0].dwnd.h = MsgHeight(msg);
  150.     MsgBox.ctl[0].dwnd.w = max(max(MsgWidth(msg),
  151.             buttonct*8 + buttonct + 2), strlen(ttl)+2);
  152.     MsgBox.dwnd.h = MsgBox.ctl[0].dwnd.h+6;
  153.     MsgBox.dwnd.w = MsgBox.ctl[0].dwnd.w+4;
  154.     if (buttonct == 1)
  155.         MsgBox.ctl[1].dwnd.x = (MsgBox.dwnd.w - 10) / 2;
  156.     else    {
  157.         MsgBox.ctl[1].dwnd.x = (MsgBox.dwnd.w - 20) / 2;
  158.         MsgBox.ctl[2].dwnd.x = MsgBox.ctl[1].dwnd.x + 10;
  159.         MsgBox.ctl[2].Class = BUTTON;
  160.     }
  161.     MsgBox.ctl[1].dwnd.y = MsgBox.dwnd.h - 4;
  162.     MsgBox.ctl[2].dwnd.y = MsgBox.dwnd.h - 4;
  163.     MsgBox.ctl[0].itext = msg;
  164.     MsgBox.ctl[1].itext = b1;
  165.     MsgBox.ctl[2].itext = b2;
  166.     MsgBox.ctl[1].command = c1;
  167.     MsgBox.ctl[2].command = c2;
  168.     MsgBox.ctl[1].isetting = ON;
  169.     MsgBox.ctl[2].isetting = ON;
  170.     rtn = DialogBox(wnd, &MsgBox, isModal, wndproc);
  171.     MsgBox.ctl[2].Class = 0;
  172.     return rtn;
  173. }
  174.  
  175. WINDOW MomentaryMessage(char *msg)
  176. {
  177.     WINDOW wnd = CreateWindow(
  178.                     TEXTBOX,
  179.                     NULL,
  180.                     -1,-1,MsgHeight(msg)+2,MsgWidth(msg)+2,
  181.                     NULL,NULL,NULL,
  182.                     HASBORDER | SHADOW | SAVESELF);
  183.     SendMessage(wnd, SETTEXT, (PARAM) msg, 0);
  184.     if (cfg.mono == 0)    {
  185.         WindowClientColor(wnd, WHITE, GREEN);
  186.         WindowFrameColor(wnd, WHITE, GREEN);
  187.     }
  188.     SendMessage(wnd, SHOW_WINDOW, 0, 0);
  189.     return wnd;
  190. }
  191.  
  192. int MsgHeight(char *msg)
  193. {
  194.     int h = 1;
  195.     while ((msg = strchr(msg, '\n')) != NULL)    {
  196.         h++;
  197.         msg++;
  198.     }
  199.     return min(h, SCREENHEIGHT-10);
  200. }
  201.  
  202. int MsgWidth(char *msg)
  203. {
  204.     int w = 0;
  205.     char *cp = msg;
  206.     while ((cp = strchr(msg, '\n')) != NULL)    {
  207.         w = max(w, (int) (cp-msg));
  208.         msg = cp+1;
  209.     }
  210.     return min(max(strlen(msg),w), SCREENWIDTH-10);
  211. }
  212. 
  213.