home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / drdobbs / 1991 / 08 / dflat5 / msgbox.c < prev    next >
Text File  |  1991-06-27  |  4KB  |  166 lines

  1. /* ------------------ msgbox.c ------------------ */
  2.  
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <ctype.h>
  6. #include <dos.h>
  7. #include "dflat.h"
  8.  
  9. extern DBOX MsgBox;
  10.  
  11. static int ReturnValue;
  12.  
  13. int MessageBoxProc(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
  14. {
  15.     switch (msg)    {
  16.         case CREATE_WINDOW:
  17.             GetClass(wnd) = MESSAGEBOX;
  18.             ClearAttribute(wnd, CONTROLBOX);
  19.             break;
  20. #ifdef INCLUDE_DIALOG_BOXES
  21.         case KEYBOARD:
  22.             if (p1 == '\r' || p1 == ESC)
  23.                 ReturnValue = (int)p1;
  24.             break;
  25. #endif
  26.         default:
  27.             break;
  28.     }
  29.     return BaseWndProc(MESSAGEBOX, wnd, msg, p1, p2);
  30. }
  31.  
  32. int YesNoBoxProc(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
  33. {
  34.     switch (msg)    {
  35.         case CREATE_WINDOW:
  36.             GetClass(wnd) = MESSAGEBOX;
  37.             ClearAttribute(wnd, CONTROLBOX);
  38.             break;
  39.         case KEYBOARD:    {
  40.             int c = tolower((int)p1);
  41.             if (c == 'y')
  42.                 SendMessage(wnd, COMMAND, ID_OK, 0);
  43.             else if (c == 'n')
  44.                 SendMessage(wnd, COMMAND, ID_CANCEL, 0);
  45.             break;
  46.         }
  47. #ifndef INCLUDE_DIALOG_BOXES
  48.         case COMMAND:
  49.             if (p1 == ID_OK || p1 == ID_CANCEL)    {
  50.                 ReturnValue = (int)p1;
  51.                 return TRUE;
  52.             }
  53.             break;
  54. #endif
  55.         default:
  56.             break;
  57.     }
  58.     return BaseWndProc(MESSAGEBOX, wnd, msg, p1, p2);
  59. }
  60.  
  61. int ErrorBoxProc(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
  62. {
  63.     switch (msg)    {
  64.         case CREATE_WINDOW:
  65.             GetClass(wnd) = ERRORBOX;
  66.             ClearAttribute(wnd, MOVEABLE | CONTROLBOX);
  67.             break;
  68. #ifdef INCLUDE_DIALOG_BOXES
  69.         case KEYBOARD:
  70.             if (p1 == '\r' || p1 == ESC)
  71.                 ReturnValue = (int)p1;
  72.             break;
  73. #endif
  74.         default:
  75.             break;
  76.     }
  77.     return BaseWndProc(ERRORBOX, wnd, msg, p1, p2);
  78. }
  79.  
  80. int GenericMessage(char *ttl, char *msg, int buttonct,
  81.     int (*wndproc)(struct window *, enum messages, PARAM, PARAM),
  82.     char *b1, char *b2)
  83. {
  84. #ifdef INCLUDE_DIALOG_BOXES
  85.     int rtn;
  86.     MsgBox.dwnd.title = ttl;
  87.     MsgBox.ctl[0].dwnd.h = MsgHeight(msg);
  88.     MsgBox.ctl[0].dwnd.w = max(MsgWidth(msg),
  89.             buttonct*8 + buttonct + 2);
  90.     MsgBox.dwnd.h = MsgBox.ctl[0].dwnd.h+6;
  91.     MsgBox.dwnd.w = MsgBox.ctl[0].dwnd.w+4;
  92.     if (buttonct == 1)
  93.         MsgBox.ctl[1].dwnd.x = (MsgBox.dwnd.w - 10) / 2;
  94.     else    {
  95.         MsgBox.ctl[1].dwnd.x = (MsgBox.dwnd.w - 20) / 2;
  96.         MsgBox.ctl[2].dwnd.x = MsgBox.ctl[1].dwnd.x + 10;
  97.         MsgBox.ctl[2].class = BUTTON;
  98.     }
  99.     MsgBox.ctl[1].dwnd.y = MsgBox.dwnd.h - 4;
  100.     MsgBox.ctl[2].dwnd.y = MsgBox.dwnd.h - 4;
  101.     MsgBox.ctl[0].itext = msg;
  102.     MsgBox.ctl[1].itext = b1;
  103.     MsgBox.ctl[2].itext = b2;
  104.     MsgBox.ctl[1].isetting = ON;
  105.     MsgBox.ctl[2].isetting = ON;
  106.     rtn = DialogBox(NULLWND, &MsgBox, TRUE, wndproc);
  107.     MsgBox.ctl[2].class = 0;
  108.     return rtn;
  109. #else
  110.     WINDOW wnd;
  111.  
  112.     wnd = CreateWindow(TEXTBOX,ttl,
  113.             -1,-1,MsgHeight(msg)+3,MsgWidth(msg)+2,
  114.             NULL,NULL,
  115.             wndproc,
  116.             HASBORDER | SAVESELF);
  117.     SendMessage(wnd, SETTEXT, (PARAM) msg, 0);
  118.     SendMessage(wnd, SETFOCUS, TRUE, 0);
  119.     SendMessage(wnd, CAPTURE_KEYBOARD, 0, 0);
  120.     SendMessage(wnd, CAPTURE_MOUSE, 0, 0);
  121.     ReturnValue = 0;
  122.     while (ReturnValue == 0)
  123.         dispatch_message();
  124.     SendMessage(wnd, RELEASE_KEYBOARD, 0, 0);
  125.     SendMessage(wnd, RELEASE_MOUSE, 0, 0);
  126.     SendMessage(wnd, CLOSE_WINDOW, 0, 0);
  127.     return ReturnValue == ID_OK || ReturnValue == '\r';
  128. #endif
  129. }
  130.  
  131. WINDOW MomentaryMessage(char *msg)
  132. {
  133.     WINDOW wnd = CreateWindow(
  134.                     TEXTBOX,
  135.                     NULL,
  136.                     -1,-1,MsgHeight(msg)+2,MsgWidth(msg)+2,
  137.                     NULL,NULL,NULL,
  138.                     HASBORDER | VISIBLE | SAVESELF);
  139.     SendMessage(wnd, SETTEXT, (PARAM) msg, 0);
  140.     SendMessage(wnd, PAINT, 0, 0);
  141.     return wnd;
  142. }
  143.  
  144. int MsgHeight(char *msg)
  145. {
  146.     int h = 1;
  147.     while ((msg = strchr(msg, '\n')) != NULL)    {
  148.         h++;
  149.         msg++;
  150.     }
  151.     return min(h, SCREENHEIGHT-10);
  152. }
  153.  
  154. int MsgWidth(char *msg)
  155. {
  156.     int w = 0;
  157.     char *cp = msg;
  158.     while ((cp = strchr(msg, '\n')) != NULL)    {
  159.         w = max(w, (int) (cp-msg));
  160.         msg = cp+1;
  161.     }
  162.     return min(max(strlen(msg),w), SCREENWIDTH-10);
  163. }
  164.  
  165.  
  166.