home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / sun / volume1 / tooltool2.1c / part06 / windows.c
Encoding:
C/C++ Source or Header  |  1989-06-06  |  14.3 KB  |  403 lines

  1. /************************************************************************/
  2. /*    Copyright 1988 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.        */
  15. /*                                    */
  16. /*    The sale of any product based wholely or in part upon the     */
  17. /*    technology provided by tooltool is strictly forbidden without    */
  18. /*    specific, prior written permission from Harris Corporation.    */
  19. /*    Tooltool technology includes, but is not limited to, the source    */
  20. /*    code, executable binary files, specification language, and    */
  21. /*    sample specification files.                    */
  22. /************************************************************************/
  23.  
  24.  
  25. #include    <ctype.h>
  26.  
  27. #include    "tooltool.h"
  28.  
  29. #include    <suntool/tty.h>
  30.  
  31. #define        TOOLTOOL_ICON        "tooltool.icon"
  32.  
  33. PRIVATE    short    icon_bits[] = {
  34. #include    TOOLTOOL_ICON
  35.                   };
  36. mpr_static(tt_default_icon_pr, 64, 64, 1, icon_bits);
  37.  
  38. EXPORT    Tty    tty = NULL;
  39.  
  40. PUBLIC    event_proc(),
  41.     notify_proc(),
  42.     background_proc(),
  43.     close_proc(),
  44.     tty_handler(),
  45.     tt_dialog_done();
  46.  
  47. /************************************************************************/
  48. /* This group of routines deals with laying out the tooltool windows    */
  49. /************************************************************************/
  50.  
  51. /************************************************************************/
  52. PRIVATE    gadget_rows(d)
  53.  
  54. d_ptr    d;
  55.  
  56. {    int    j, k, extra, count, row, height;
  57.     g_ptr    b, next, start;
  58.  
  59.     tt_build_images(d);
  60.     for (next = d->gadgets, row = 4, count = 0; next; row += height + 4) {
  61.        extra = (int) window_get(d->panel, WIN_WIDTH) - 4;
  62.        for (b = start = next, height = 0; b; b = b->next)
  63.           if (b->width + 4 > extra) { /* no room for this gadget */
  64.              next = b;
  65.              break;
  66.              }
  67.           else {
  68.              extra -= b->width + 4;
  69.              count++;
  70.              if (b->height > height)
  71.                 height = b->height;
  72.              }
  73.        if (b == NULL)
  74.           next = NULL;
  75.        if (next == start) {
  76.           next = start->next;
  77.           height = start->height;
  78.           count++;
  79.           }
  80.        if (!d->justified)
  81.           extra = 0;
  82.        for (b = start, j = 4, count--; b != next; b = b->next) {
  83.           if (d->g_align == ALIGN_TOP)
  84.              k = row;
  85.           else if (d->g_align == ALIGN_MIDDLE)
  86.              k = row + (height - b->height) / 2;
  87.           else
  88.              k = row + height - b->height;
  89.           tt_make_gadget(d, b, j, k);
  90.           if (count > 0) {
  91.              j += b->width + 4 + (extra / count);
  92.              extra -= extra / count--;
  93.              }
  94.           }
  95.        }
  96.     panel_fit_height(d->panel);
  97. }
  98.  
  99. /************************************************************************/
  100. PRIVATE    gadget_columns(d)
  101.  
  102. d_ptr    d;
  103.  
  104. {    int    j, k, extra, count, col, width;
  105.     g_ptr    b, next, start;
  106.  
  107.     tt_build_images(d);
  108.     for (next = d->gadgets, col = count = 0; next; col += width + 4) {
  109.        extra = (int) window_get(d->panel, WIN_HEIGHT) - 4;
  110.        for (b = start = next, width = 0; b; b = b->next)
  111.           if (b->height + 4 > extra) { /* no room for this gadget */
  112.              next = b;
  113.              break;
  114.              }
  115.           else {
  116.              extra -= b->height + 4;
  117.              count++;
  118.              if (b->width > width)
  119.                 width = b->width;
  120.              }
  121.        if (b == NULL)
  122.           next = NULL;
  123.        if (next == start) {
  124.           next = start->next;
  125.           width = start->width;
  126.           count++;
  127.           }
  128.        if (!d->justified)
  129.           extra = 0;
  130.        for (b = start, j = 4, count--; b != next; b = b->next) {
  131.           if (d->g_align == ALIGN_TOP)
  132.              k = 4 + col;
  133.           else if (d->g_align == ALIGN_MIDDLE)
  134.              k = 4 + col + (width - b->width) / 2;
  135.           else
  136.              k = 4 + col + width - b->width;
  137.           tt_make_gadget(d, b, k, j);
  138.           if (count > 0) {
  139.              j += b->height + 4 + (extra / count);
  140.              extra -= extra / count--;
  141.              }
  142.           }
  143.        }
  144.     panel_fit_width(d->panel);
  145. }
  146.  
  147. /************************************************************************/
  148. EXPORT    build_window(argc, argv)
  149.  
  150. int    argc;
  151. char    **argv;
  152.  
  153. {    int    i, j, w, h;
  154.     g_ptr    b, start, next;
  155.     char    *args[64];
  156.     struct    pixrect    *icon_pr;
  157.     Icon    ic;
  158.     Rect    *fr, *sr;
  159.     d_ptr    d;
  160.     static    char    *pos_hack[5];
  161.  
  162.     if (tt_icon != NULL)
  163.        icon_pr = tt_load_icon(tt_icon);
  164.     else
  165.        icon_pr = &tt_default_icon_pr;
  166.     ic = icon_create(ICON_IMAGE, icon_pr,
  167.              ICON_LABEL, "",
  168.              ICON_WIDTH, icon_pr->pr_size.x,
  169.              ICON_HEIGHT, icon_pr->pr_size.y,
  170.              0);
  171.     tt_base_window->frame = window_create(NULL, FRAME,
  172.                  FRAME_ARGC_PTR_ARGV, &argc, argv,
  173.                  FRAME_ICON, ic,
  174.                  WIN_CLIENT_DATA, tt_base_window,
  175.                   0);
  176.     if (tt_base_window->label)
  177.        window_set(tt_base_window->frame, FRAME_LABEL, tt_base_window->label, 0);
  178.     if (tt_base_window->win_x != -1)
  179.        window_set(tt_base_window->frame, WIN_X, tt_base_window->win_x, WIN_Y, tt_base_window->win_y, 0);
  180.  
  181.     args[0] = tt_program;
  182.     args[1] = POLLING_MAGIC_NUMBER;
  183.     args[2] = safe_malloc(10);
  184.     args[3] = safe_malloc(10);
  185.     sprintf(args[2], "%d", tt_base_window->columns / (tt_base_window->is_chars? 1 : charwidth_of(tt_a_font)));
  186.     sprintf(args[3], "%d", tt_base_window->rows / (tt_base_window->is_chars? 1 : charheight_of(tt_a_font)));
  187.     tokenize(tt_application, &i, args + 4, 60);
  188.     for (j = 1, i += 4; j < argc; j++)
  189.        args[i++] = argv[j];
  190.     args[i] = NULL;
  191.     if (tt_base_window->gadgets == NULL) {
  192.        if (*tt_application)
  193.           tty = window_create(tt_base_window->frame, TTY,
  194.                      tt_base_window->is_chars? WIN_ROWS : WIN_HEIGHT, tt_base_window->rows,
  195.                      tt_base_window->is_chars? WIN_COLUMNS : WIN_WIDTH, tt_base_window->columns,
  196.                      WIN_FONT, tt_a_font,
  197.                      TTY_QUIT_ON_CHILD_DEATH, TRUE,
  198.                      TTY_ARGV, args,
  199.                       0);
  200.        }
  201.     else if (tt_base_window->gadget_pos == G_TOP) {
  202.        tt_base_window->panel = window_create(tt_base_window->frame, PANEL,
  203.                        WIN_ROWS, 24,
  204.                        WIN_WIDTH, tt_base_window->is_chars? charwidth_of(tt_a_font) * tt_base_window->columns : tt_base_window->columns,
  205.                        WIN_FONT, tt_base_window->g_font,
  206.                     WIN_CLIENT_DATA, tt_base_window,
  207.                        PANEL_ACCEPT_KEYSTROKE, !tt_base_window->text_items_exist,
  208.                        PANEL_BACKGROUND_PROC, background_proc,
  209.                        PANEL_NOTIFY_PROC, notify_proc,
  210.                        PANEL_EVENT_PROC, event_proc,
  211.                     0);
  212.        gadget_rows(tt_base_window);
  213.        if (*tt_application)
  214.           tty = window_create(tt_base_window->frame, TTY,
  215.                      tt_base_window->is_chars? WIN_ROWS : WIN_HEIGHT, tt_base_window->rows,
  216.                      tt_base_window->is_chars? WIN_COLUMNS : WIN_WIDTH, tt_base_window->columns,
  217.                      WIN_BELOW, tt_base_window->panel,
  218.                      WIN_X, 0,
  219.                      WIN_FONT, tt_a_font,
  220.                      TTY_QUIT_ON_CHILD_DEATH, TRUE,
  221.                      TTY_ARGV, args,
  222.                       0);
  223.        }
  224.     else if (tt_base_window->gadget_pos == G_BOTTOM) {
  225.        if (*tt_application)
  226.           tty = window_create(tt_base_window->frame, TTY,
  227.                      tt_base_window->is_chars? WIN_ROWS : WIN_HEIGHT, tt_base_window->rows,
  228.                      tt_base_window->is_chars? WIN_COLUMNS : WIN_WIDTH, tt_base_window->columns,
  229.                      WIN_FONT, tt_a_font,
  230.                      TTY_QUIT_ON_CHILD_DEATH, TRUE,
  231.                      TTY_ARGV, args,
  232.                       0);
  233.        if (tty && tt_base_window->rows > 0 && tt_base_window->columns > 0)
  234.           tt_base_window->panel = window_create(tt_base_window->frame, PANEL,
  235.                                          WIN_BELOW, tty,
  236.                                          WIN_X, 0,
  237.                                       0);
  238.        else
  239.           tt_base_window->panel = window_create(tt_base_window->frame, PANEL,
  240.                                          WIN_X, 0,
  241.                                          WIN_Y, 0,
  242.                                       0);
  243.        window_set(tt_base_window->panel,
  244.                 WIN_ROWS, 24,
  245.                 WIN_WIDTH, tt_base_window->is_chars? charwidth_of(tt_a_font) * tt_base_window->columns : tt_base_window->columns,
  246.                 WIN_FONT, tt_base_window->g_font,
  247.              WIN_CLIENT_DATA, tt_base_window,
  248.                 PANEL_ACCEPT_KEYSTROKE, !tt_base_window->text_items_exist,
  249.                 PANEL_BACKGROUND_PROC, background_proc,
  250.                 PANEL_NOTIFY_PROC, notify_proc,
  251.                 PANEL_EVENT_PROC, event_proc,
  252.                  0);
  253.        gadget_rows(tt_base_window);
  254.        }
  255.     else if (tt_base_window->gadget_pos == G_LEFT)  {
  256.        tt_base_window->panel = window_create(tt_base_window->frame, PANEL,
  257.                        WIN_HEIGHT, tt_base_window->is_chars? charheight_of(tt_a_font) * tt_base_window->rows : tt_base_window->rows,
  258.                        WIN_COLUMNS, 80,
  259.                        WIN_FONT, tt_base_window->g_font,
  260.                     WIN_CLIENT_DATA, tt_base_window,
  261.                        PANEL_ACCEPT_KEYSTROKE, !tt_base_window->text_items_exist,
  262.                        PANEL_BACKGROUND_PROC, background_proc,
  263.                        PANEL_NOTIFY_PROC, notify_proc,
  264.                        PANEL_EVENT_PROC, event_proc,
  265.                     0);
  266.        gadget_columns(tt_base_window);
  267.        if (*tt_application)
  268.           tty = window_create(tt_base_window->frame, TTY,
  269.                      tt_base_window->is_chars? WIN_ROWS : WIN_HEIGHT, tt_base_window->rows,
  270.                      tt_base_window->is_chars? WIN_COLUMNS : WIN_WIDTH, tt_base_window->columns,
  271.                      WIN_RIGHT_OF, tt_base_window->panel,
  272.                      WIN_Y, 0,
  273.                      WIN_FONT, tt_a_font,
  274.                      TTY_QUIT_ON_CHILD_DEATH, TRUE,
  275.                      TTY_ARGV, args,
  276.                       0);
  277.        }
  278.     else if (tt_base_window->gadget_pos == G_RIGHT) {
  279.        if (*tt_application)
  280.           tty = window_create(tt_base_window->frame, TTY,
  281.                      tt_base_window->is_chars? WIN_ROWS : WIN_HEIGHT, tt_base_window->rows,
  282.                      tt_base_window->is_chars? WIN_COLUMNS : WIN_WIDTH, tt_base_window->columns,
  283.                      WIN_FONT, tt_a_font,
  284.                      TTY_QUIT_ON_CHILD_DEATH, TRUE,
  285.                      TTY_ARGV, args,
  286.                       0);
  287.        if (tty && tt_base_window->rows > 0 && tt_base_window->columns > 0)
  288.           tt_base_window->panel = window_create(tt_base_window->frame, PANEL,
  289.                                    WIN_RIGHT_OF, tty,
  290.                                    WIN_Y, 0,
  291.                                 0);
  292.        else
  293.           tt_base_window->panel = window_create(tt_base_window->frame, PANEL,
  294.                                    WIN_X, 0,
  295.                                    WIN_Y, 0,
  296.                                 0);
  297.        window_set(tt_base_window->panel,
  298.                     WIN_HEIGHT, tt_base_window->is_chars? charheight_of(tt_a_font) * tt_base_window->rows : tt_base_window->rows,
  299.                     WIN_COLUMNS, 80,
  300.                     WIN_FONT, tt_base_window->g_font,
  301.                  WIN_CLIENT_DATA, tt_base_window,
  302.                     PANEL_ACCEPT_KEYSTROKE, !tt_base_window->text_items_exist,
  303.                     PANEL_BACKGROUND_PROC, background_proc,
  304.                     PANEL_NOTIFY_PROC, notify_proc,
  305.                     PANEL_EVENT_PROC, event_proc,
  306.                  0);
  307.        gadget_columns(tt_base_window);
  308.        }
  309.     window_fit(tt_base_window->frame);
  310.  
  311.     fr = (Rect *) window_get(tt_base_window->frame, FRAME_OPEN_RECT);
  312.     sr = (Rect *) window_get(tt_base_window->frame, WIN_SCREEN_RECT);
  313.     if (fr->r_left + fr->r_width > sr->r_width)
  314.        window_set(tt_base_window->frame, WIN_X, max(sr->r_width - fr->r_width, 0), 0);
  315.     if (fr->r_top + fr->r_height > sr->r_height)
  316.        window_set(tt_base_window->frame, WIN_Y, max(sr->r_height - fr->r_height, 0), 0);
  317.  
  318.     fr = (Rect *) window_get(tt_base_window->frame, FRAME_CLOSED_RECT);
  319.     for (d = tt_base_window->next; d; d = d->next) {
  320.        if (d->g_align == NO_ALIGN)
  321.           d->g_align = ALIGN_TOP;
  322.        pos_hack[0] = "";
  323.        pos_hack[1] = "-WP";
  324.        pos_hack[2] = safe_malloc(7);
  325.        pos_hack[3] = safe_malloc(7);
  326.        pos_hack[4] = NULL;
  327.        sprintf(pos_hack[2], "%d", fr->r_left);
  328.        sprintf(pos_hack[3], "%d", fr->r_top);
  329.        d->frame = window_create(tt_base_window->frame, FRAME,
  330.                           FRAME_SHOW_LABEL, FALSE,
  331.                           FRAME_DONE_PROC, tt_dialog_done,
  332.                           FRAME_ARGS, 4, pos_hack,
  333.                           WIN_CLIENT_DATA, d,
  334.                        0);
  335.        if (d->label)
  336.           window_set(d->frame, FRAME_LABEL, d->label, FRAME_SHOW_LABEL, TRUE, 0);
  337.        d->panel = window_create(d->frame, PANEL,
  338.                           WIN_HEIGHT, d->is_chars? charwidth_of(d->g_font) * d->rows : d->rows,
  339.                           WIN_WIDTH, d->is_chars? charwidth_of(d->g_font) * d->columns : d->columns,
  340.                           WIN_FONT, d->g_font,
  341.                           WIN_CLIENT_DATA, d,
  342.                           PANEL_ACCEPT_KEYSTROKE, !d->text_items_exist,
  343.                           PANEL_BACKGROUND_PROC, background_proc,
  344.                           PANEL_NOTIFY_PROC, notify_proc,
  345.                           PANEL_EVENT_PROC, event_proc,
  346.                        0);
  347.        if (d->gadget_pos == G_TOP || d->gadget_pos == G_BOTTOM)
  348.           gadget_rows(d);
  349.        else
  350.           gadget_columns(d);
  351.        window_fit(d->panel);
  352.        window_fit(d->frame);
  353.        }
  354.  
  355.     notify_interpose_event_func(tt_base_window->frame, close_proc, NOTIFY_SAFE);
  356.     if (tt_base_window->panel)
  357.        notify_interpose_event_func(tt_base_window->panel, close_proc, NOTIFY_SAFE);
  358.  
  359.     if (tty) {
  360.        notify_interpose_event_func(tty, tty_handler, NOTIFY_SAFE);
  361.        tt_ttymenu = (Menu) window_get(tty, WIN_MENU);
  362.        }
  363.  
  364.     if (tty == NULL || tt_base_window->rows <= 0 || tt_base_window->columns <= 0) {
  365.        if (tty)
  366.           window_set(tty, WIN_SHOW, FALSE, 0);
  367.        if (tt_base_window->panel)
  368.           window_fit(tt_base_window->panel);
  369.        window_fit(tt_base_window->frame);
  370.        }
  371.  
  372.     fr = (Rect *) window_get(tt_base_window->frame, FRAME_OPEN_RECT);
  373.     window_set(tt_base_window->frame, WIN_X, 0, WIN_Y, 0, 0);
  374.     for (d = tt_base_window->next; d; d = d->next)
  375.        if (d->win_x != -1) {
  376.           window_set(d->frame, WIN_X, d->win_x, WIN_Y, d->win_y, 0);
  377.           w = (int) window_get(d->frame, WIN_WIDTH);
  378.           h = (int) window_get(d->frame, WIN_HEIGHT);
  379.           if (d->win_x + w > sr->r_width)
  380.              window_set(d->frame, WIN_X, max(sr->r_width - w, 0), 0);
  381.           if (d->win_y + h > sr->r_height)
  382.              window_set(d->frame, WIN_Y, max(sr->r_height - h, 0), 0);
  383.           }
  384.        else
  385.           window_set(d->frame,
  386.                    WIN_X, (sr->r_width - (int) window_get(d->frame, WIN_WIDTH)) / 2,
  387.                    WIN_Y, (sr->r_height - (int) window_get(d->frame, WIN_HEIGHT)) / 2,
  388.                    0);
  389.     window_set(tt_base_window->frame, WIN_X, fr->r_left, WIN_Y, fr->r_top, 0);
  390.  
  391.     init_function_fix(tt_base_window->frame);
  392.     if (tty)
  393.        init_function_fix(tty);
  394.     if (tt_base_window->panel)
  395.        init_function_fix(tt_base_window->panel);
  396.     for (d = tt_base_window->next; d; d = d->next) {
  397.        init_function_fix(d->frame);
  398.        init_function_fix(d->panel);
  399.        }
  400.  
  401.     tt_do_action(tt_initial_action);
  402. }
  403.