home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / sun / volume1 / contool2.2 / part03 / dialog.c < prev   
Encoding:
C/C++ Source or Header  |  1989-10-26  |  10.8 KB  |  342 lines

  1. /************************************************************************/
  2. /*    Copyright 1988, 1989 by Chuck Musciano and Harris Corporation    */
  3. /*                                    */
  4. /*    Permission to use, copy, modify, and distribute this software    */
  5. /*    and its documentation for any purpose and without fee is    */
  6. /*    hereby granted, provided that the above copyright notice    */
  7. /*    appear in all copies and that both that copyright notice and    */
  8. /*    this permission notice appear in supporting documentation, and    */
  9. /*    that the name of Chuck Musciano and Harris Corporation not be    */
  10. /*    used in advertising or publicity pertaining to distribution    */
  11. /*    of the software without specific, written prior permission.    */
  12. /*    Chuck Musciano and Harris Corporation make no representations    */
  13. /*    about the suitability of this software for any purpose.  It is    */
  14. /*    provided "as is" without express or implied warranty.  This     */
  15. /*    software may not be sold without the prior explicit permission    */
  16. /*    of Harris Corporation.                        */
  17. /************************************************************************/
  18.  
  19. #include    <stdio.h>
  20.  
  21. #include    <sys/file.h>
  22.  
  23. #include    <suntool/sunview.h>
  24. #include    <suntool/panel.h>
  25.  
  26. #include    "contool.h"
  27. #include    "entry.h"
  28.  
  29. PUBLIC    Pixrect    *better_button_image();
  30.  
  31. EXPORT    Pixfont    *bold, *regular;
  32. EXPORT    Panel    other;
  33.  
  34. PRIVATE    Frame    dialog = NULL;
  35. PRIVATE    Panel    items;
  36. PRIVATE    Panel_item    file_name;
  37.  
  38. /************************************************************************/
  39. /*    Support routines for edit buttons                */
  40. /************************************************************************/
  41.  
  42. /************************************************************************/
  43. PRIVATE    f_ptr    update_filters()
  44.  
  45. {    int    i, curr, limit, errors = FALSE;
  46.     char    *result, *start, *end;
  47.     f_ptr    f, new_filters = NULL;
  48.     
  49.     for (i = 0; i < entries; i++)
  50.        panel_set(entry[i].handle, PANEL_VALUE, 0, 0);
  51.     for (i = 0; i < entries; i++)
  52.        if (entry[i].created) {
  53.           start = (char *) panel_get(entry[i].start, PANEL_VALUE);
  54.           if (panel_get(entry[i].lines, PANEL_VALUE)) {
  55.              if (*start == '\0') {
  56.                 window_error("Filter error:\n   Filter %d is missing its starting pattern", i + 1);
  57.                 panel_set(entry[i].handle, PANEL_VALUE, 1, 0);
  58.                 panel_set(items, PANEL_CARET_ITEM, entry[i].start, 0);
  59.                 errors = TRUE;
  60.                 continue;
  61.                 }
  62.              end = (char *) panel_get(entry[i].end, PANEL_VALUE);
  63.              if (*end == '\0') {
  64.                 window_error("Filter error:\n   Filter %d is missing its ending pattern", i + 1);
  65.                 panel_set(entry[i].handle, PANEL_VALUE, 1, 0);
  66.                 panel_set(items, PANEL_CARET_ITEM, entry[i].end, 0);
  67.                 errors = TRUE;
  68.                 continue;
  69.                 }
  70.              }
  71.           else {
  72.              end = NULL;
  73.              if (*start == '\0') {
  74.                 window_error("Filter error:\n   Filter %d has no pattern specified", i + 1);
  75.                 panel_set(entry[i].handle, PANEL_VALUE, 1, 0);
  76.                 panel_set(items, PANEL_CARET_ITEM, entry[i].start, 0);
  77.                 errors = TRUE;
  78.                 continue;
  79.                 }
  80.              }
  81.           if (result = compile_exp(NULL, start, end)) {
  82.              window_error("Error in filter %d:\n   %s", i + 1, result);
  83.              panel_set(entry[i].handle, PANEL_VALUE, 1, 0);
  84.              panel_set(items, PANEL_CARET_ITEM, entry[i].start, 0);
  85.              errors = TRUE;
  86.              continue;
  87.              }
  88.           }
  89.     if (errors)
  90.        return(NULL);
  91.     for (i = 0, f = next_filter(&new_filters, &curr, &limit); i < MAX_ENTRIES; i++)
  92.        if (entry[i].created) {
  93.           f->save  = (int) panel_get(entry[i].save,  PANEL_VALUE);
  94.           f->beep  = (int) panel_get(entry[i].beep,  PANEL_VALUE);
  95.           f->open  = (int) panel_get(entry[i].open,  PANEL_VALUE);
  96.           f->flash = (int) panel_get(entry[i].flash, PANEL_VALUE);
  97.           f->stamp = (int) panel_get(entry[i].stamp, PANEL_VALUE);
  98.           f->start = strsave(panel_get(entry[i].start, PANEL_VALUE));
  99.           f->end   = panel_get(entry[i].lines, PANEL_VALUE)? strsave(panel_get(entry[i].end, PANEL_VALUE)) : NULL;
  100.           f->valid = TRUE;
  101.           compile_exp(f, f->start, f->end);
  102.           f = next_filter(&new_filters, &curr, &limit);
  103.           }
  104.     return(new_filters);
  105. }
  106.  
  107. /************************************************************************/
  108. /*    Edit button action routines                    */
  109. /************************************************************************/
  110.  
  111. /************************************************************************/
  112. PRIVATE    load_filters()
  113.  
  114. {    char    *path, *result;
  115.     f_ptr    new_filters, f;
  116.  
  117.     path = (char *) panel_get(file_name, PANEL_VALUE);
  118.     if (access(path, R_OK) == 0) {
  119.        if (new_filters = read_filters(path, window_error)) {
  120.           for (f = new_filters; f->start; f++)
  121.              if (result = compile_exp(f, f->start, f->end)) {
  122.                 window_error("Bad filter encountered in %s:\n   %s", path, result);
  123.                 f->valid = FALSE;
  124.                 }
  125.           update_edit_dialog(new_filters);
  126.           free_filters(new_filters);
  127.           }
  128.        }
  129.     else
  130.        window_error("Cannot read filter file %s", path);
  131. }
  132.  
  133. /************************************************************************/
  134. PRIVATE    save_filters()
  135.  
  136. {    char    *path;
  137.     f_ptr    filters;
  138.  
  139.     if (filters = update_filters()) {
  140.        path = (char *) panel_get(file_name, PANEL_VALUE);
  141.        write_filters(filters, path, window_error);
  142.        }
  143.     else
  144.        window_error("Filters have not been saved");
  145. }
  146.  
  147. /************************************************************************/
  148. PRIVATE    cut_filter()
  149.  
  150. {    int    i, j;
  151.  
  152.     clear_clipboard();
  153.     for (i = 0; i < entries; i++)
  154.        if (panel_get(entry[i].handle, PANEL_VALUE)) {
  155.           add_to_clipboard(entry + i);
  156.           delete_entry(entry + i);
  157.           }
  158.        else if (cb_size > 0)
  159.           move_entry(entry, i, i - cb_size);
  160.     entries -= cb_size;
  161.     panel_update_scrolling_size(items);
  162.     panel_paint(items, PANEL_NO_CLEAR);
  163. }
  164.  
  165. /************************************************************************/
  166. PRIVATE    copy_filter()
  167.  
  168. {    int    i;
  169.     f_ptr    f;
  170.  
  171.     clear_clipboard();
  172.     for (i = cb_size = 0; i < entries; i++)
  173.        if (panel_get(entry[i].handle, PANEL_VALUE))
  174.           add_to_clipboard(entry + i);
  175. }
  176.  
  177. /************************************************************************/
  178. PRIVATE    paste_filter()
  179.  
  180. {    int    i, j, count;
  181.  
  182.     for (i = 0; i < entries; i++)
  183.        if (panel_get(entry[i].handle, PANEL_VALUE))
  184.           break;
  185.     for (j = entries - 1, count = cb_size? cb_size : 1; j >= i; j--)
  186.        move_entry(entry, j, j + count);
  187.     if (cb_size == 0) {
  188.        update_entry(items, entry, i, NULL);
  189.        panel_set(items, PANEL_CARET_ITEM, entry[i].start, 0);
  190.        entries++;
  191.        }
  192.     else {
  193.        for (j = 0; j < cb_size; j++)
  194.           update_entry(items, entry, i + j, clipboard + j);
  195.        entries += cb_size;
  196.        }
  197.     panel_update_scrolling_size(items);
  198.     panel_paint(items, PANEL_NO_CLEAR);
  199. }
  200.  
  201. /************************************************************************/
  202. PRIVATE    done_edit()
  203.  
  204. {    f_ptr    new_filters;
  205.  
  206.     if (new_filters = update_filters()) {
  207.        free_filters(filters);
  208.        filters = new_filters;
  209.        curr_filter = NULL;
  210.        install_defaults();
  211.        strcpy(filter_path, panel_get(file_name, PANEL_VALUE));
  212.        window_set(dialog, WIN_SHOW, FALSE, 0);
  213.        }
  214. }
  215.  
  216. /************************************************************************/
  217. PRIVATE    reset_edit()
  218.  
  219. {
  220.     update_edit_dialog(filters);
  221.     update_defaults();
  222. }
  223.  
  224. /************************************************************************/
  225. PRIVATE    quit_edit()
  226.  
  227. {
  228.     window_set(dialog, WIN_SHOW, FALSE, 0);
  229. }
  230.  
  231. /************************************************************************/
  232. /*    External entry points to this module                */
  233. /************************************************************************/
  234.  
  235. /************************************************************************/
  236. EXPORT    update_edit_dialog(filters)
  237.  
  238. f_ptr    filters;
  239.  
  240. {    register    int    i;
  241.     register    e_ptr    e;
  242.     register    f_ptr    f;
  243.  
  244.     for (i = 0, f = filters; f->start; i++, e++, f++) {
  245.        create_entry(items, entry, i);
  246.        update_entry(items, entry, i, f);
  247.        }
  248.     for (entries = i, e = entry + i; i < MAX_ENTRIES; i++, e++)
  249.        delete_entry(e);
  250.     panel_update_scrolling_size(items);
  251.     panel_paint(items, PANEL_CLEAR);
  252. }
  253.  
  254. /************************************************************************/
  255. EXPORT    Frame    create_dialog_box(base)
  256.  
  257. Frame    base;
  258.  
  259. {    Panel    control;
  260.     Panel_item    c, p, d;
  261.     int    i;
  262.  
  263.     if (dialog)
  264.        return;
  265.     bold = pf_open(BOLD_FONT);
  266.     regular = pf_open(REGULAR_FONT);
  267.     dialog = window_create(base, FRAME,
  268.                   FRAME_LABEL, "<< Contool Filter Editor >>",
  269.                   FRAME_SHOW_LABEL, TRUE,
  270.                   FRAME_NO_CONFIRM, TRUE,
  271.                   FRAME_DONE_PROC, done_edit,
  272.                    0);
  273.     control = window_create(dialog, PANEL,
  274.                    WIN_WIDTH, ITEM_WIDTH,
  275.                 0);
  276.     panel_create_item(control, PANEL_BUTTON,
  277.                  PANEL_LABEL_IMAGE, better_button_image(control, "Load", 12, 4, bold),
  278.                  PANEL_NOTIFY_PROC, load_filters,
  279.               0);
  280.     panel_create_item(control, PANEL_BUTTON,
  281.                  PANEL_LABEL_IMAGE, better_button_image(control, "Save", 12, 3, bold),
  282.                  PANEL_NOTIFY_PROC, save_filters,
  283.               0);
  284.     c = panel_create_item(control, PANEL_BUTTON,
  285.                  PANEL_LABEL_IMAGE, better_button_image(control, "Cut", 12, 4, bold),
  286.                  PANEL_NOTIFY_PROC, cut_filter,
  287.               0);
  288.     p = panel_create_item(control, PANEL_BUTTON,
  289.                  PANEL_LABEL_IMAGE, better_button_image(control, "Paste", 12, 3, bold),
  290.                  PANEL_NOTIFY_PROC, paste_filter,
  291.               0);
  292.     d = panel_create_item(control, PANEL_BUTTON,
  293.                  PANEL_LABEL_IMAGE, better_button_image(control, "Done", 12, 4, bold),
  294.                  PANEL_NOTIFY_PROC, done_edit,
  295.               0);
  296.     file_name = panel_create_item(control, PANEL_TEXT,
  297.                      PANEL_ITEM_Y, 35,
  298.                      PANEL_LABEL_STRING, "File:",
  299.                      PANEL_LABEL_FONT, bold,
  300.                      PANEL_VALUE_STORED_LENGTH, 256,
  301.                      PANEL_VALUE_DISPLAY_LENGTH, 24,
  302.                      PANEL_VALUE_FONT, regular,
  303.                      PANEL_VALUE, filter_path,
  304.                       0);
  305.     panel_create_item(control, PANEL_BUTTON,
  306.                  PANEL_ITEM_Y, 31,
  307.                  PANEL_LABEL_IMAGE, better_button_image(control, "Copy", 12, 4, bold),
  308.                  PANEL_NOTIFY_PROC, copy_filter,
  309.                  PANEL_ITEM_X, panel_get(c, PANEL_ITEM_X),
  310.               0);
  311.     panel_create_item(control, PANEL_BUTTON,
  312.                  PANEL_ITEM_Y, 31,
  313.                  PANEL_LABEL_IMAGE, better_button_image(control, "Reset", 12, 4, bold),
  314.                  PANEL_NOTIFY_PROC, reset_edit,
  315.                  PANEL_ITEM_X, panel_get(p, PANEL_ITEM_X),
  316.               0);
  317.     panel_create_item(control, PANEL_BUTTON,
  318.                  PANEL_ITEM_Y, 31,
  319.                  PANEL_LABEL_IMAGE, better_button_image(control, "Cancel", 12, 4, bold),
  320.                  PANEL_NOTIFY_PROC, quit_edit,
  321.                  PANEL_ITEM_X, panel_get(d, PANEL_ITEM_X),
  322.               0);
  323.     window_fit_height(control);
  324.     items = window_create(dialog, PANEL,
  325.                  WIN_X, 0,
  326.                  WIN_BELOW, control,
  327.                  WIN_WIDTH, ITEM_WIDTH,
  328.                  WIN_HEIGHT, ROW_HEIGHT * 10 + ROW_MARGIN * 11,
  329.                  WIN_VERTICAL_SCROLLBAR, scrollbar_create(0),
  330.                   0);
  331.     other = window_create(dialog, PANEL,
  332.                  WIN_X, 0,
  333.                  WIN_BELOW, items,
  334.                  WIN_WIDTH, ITEM_WIDTH,
  335.                  WIN_HEIGHT, ROW_HEIGHT + ROW_MARGIN * 2,
  336.                   0);
  337.     window_fit(dialog);
  338.     for (i = 0; i < MAX_ENTRIES; i++)
  339.        entry[i].created = FALSE;
  340.     return(dialog);
  341. }
  342.