home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / sun / volume1 / contool2.2 / part01 / alert.c next >
Encoding:
C/C++ Source or Header  |  1989-10-26  |  3.0 KB  |  115 lines

  1. /************************************************************************/
  2. /*                                    */
  3. /*    window_error    Display an error message in a small window and    */
  4. /*            await confirmation before returning.        */
  5. /*                                    */
  6. /************************************************************************/
  7.  
  8. #include    <suntool/sunview.h>
  9. #include    <suntool/panel.h>
  10.  
  11. #include    "contool.h"
  12.  
  13. #define        charwidth_of(f)        ((f)->pf_char['0'].pc_adv.x)
  14. #define        charheight_of(f)    ((f)->pf_defaultsize.y)
  15.  
  16. PUBLIC    Pixrect    *better_button_image();
  17.  
  18. EXPORT    Frame    confirmer = NULL;
  19. PRIVATE    Panel    panel = NULL;
  20.  
  21. PRIVATE    struct    pixfont    *pf = NULL;
  22.  
  23. /************************************************************************/
  24. PRIVATE    confirmed(item, event)
  25.  
  26. Panel_item    item;
  27. Event    *event;
  28.  
  29. {
  30.     if (event_is_up(event))
  31.        window_return(0);
  32. }
  33.  
  34. /************************************************************************/
  35. EXPORT    window_error(s0, s1, s2, s3, s4, s5, s6, s7, s8, s9)
  36.  
  37. char    *s0;
  38. char    *s1;
  39. char    *s2;
  40. char    *s3;
  41. char    *s4;
  42. char    *s5;
  43. char    *s6;
  44. char    *s7;
  45. char    *s8;
  46. char    *s9;
  47.  
  48. {    char    buf[1024], message[1024], **p, *b, *m;
  49.     int    i, j, k, left, top, width, lines;
  50.     Rect    *r;
  51.     Pixrect    *image;
  52.     Panel_item    *item, button;
  53.  
  54.     if (pf == NULL)
  55.        pf = pf_open(BOLD_FONT);
  56.     if (confirmer == NULL) {
  57.        confirmer = window_create(0, FRAME,
  58.                         FRAME_NO_CONFIRM, TRUE,
  59.                         FRAME_SHOW_LABEL, FALSE,
  60.                      0);
  61.        panel = window_create(confirmer, PANEL,
  62.                     PANEL_BACKGROUND_PROC, confirmed,
  63.                      0);
  64.        }
  65.  
  66.     sprintf(message, s0, s1, s2, s3, s4, s5, s6, s7, s8, s9);
  67.     for (m = message, b = buf, i = 0, lines = 1; *m; m++, b++, i++)
  68.        if (*m == '\t') {
  69.           for (j = i % 8; j < 8; j++, i++)
  70.              *b++ = ' ';
  71.           i--, b--;
  72.           }
  73.        else if ((*b = *m) == '\n') {
  74.           i = -1;
  75.           lines++;
  76.           }
  77.     *b = '\0';
  78.  
  79.     item = (Panel_item *) malloc(lines * sizeof(Panel_item));
  80.     for (b = m = buf, i = 4, width = 0, k = 0; k < lines; b++)
  81.        if (*b == '\n' || *b == '\0') {
  82.           *b = '\0';
  83.           item[k++] = panel_create_item(panel, PANEL_MESSAGE,
  84.                                PANEL_LABEL_STRING, m,
  85.                                PANEL_LABEL_FONT, pf,
  86.                                PANEL_ITEM_X, 4,
  87.                                PANEL_ITEM_Y, i,
  88.                             0);
  89.           width = (width < (j = charwidth_of(pf) * strlen(m)))? j : width;
  90.           i += charheight_of(pf) + 4;
  91.           m = b + 1;
  92.           }
  93.     window_fit_width(panel);
  94.     image = better_button_image(panel, "OK", 12, 0, pf);
  95.     button = panel_create_item(panel, PANEL_BUTTON,
  96.                       PANEL_LABEL_IMAGE, image,
  97.                       PANEL_ITEM_X, (width - image->pr_width) / 2 + 4,
  98.                       PANEL_ITEM_Y, i,
  99.                       PANEL_NOTIFY_PROC, confirmed,
  100.                    0);
  101.     window_fit(panel);
  102.     window_fit(confirmer);
  103.     r = (Rect *) window_get(confirmer, WIN_SCREEN_RECT);
  104.     left = (r->r_width - (int) window_get(confirmer, WIN_WIDTH)) / 2;
  105.     top = (r->r_height - (int) window_get(confirmer, WIN_HEIGHT)) / 2;
  106.     if (left < 0) left = 0;
  107.     if (top < 0) top = 0;
  108.     window_set(confirmer, WIN_X, left, WIN_Y, top, 0);
  109.     window_loop(confirmer);
  110.     panel_destroy_item(button);
  111.     for (i = 0; i < lines; i++)
  112.        panel_destroy_item(item[i]);
  113.     free(item);
  114. }
  115.