home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-386-Vol-2of3.iso / b / bricon20.zip / bricons / buttons.c < prev    next >
C/C++ Source or Header  |  1992-07-30  |  10KB  |  447 lines

  1. static char* sccs_buttons_c = "%W%%G%";
  2.  
  3. #include "gen.h"
  4. #include "widgets.h"
  5.  
  6.  
  7. extern void    CreateSubButton PROTO((Info*, int, int, char*));
  8. extern    void    CreateMainMenuButton PROTO((Info*, int, int, char*, char*));
  9. static    void    CreateDefaultSubButton PROTO((int, char*, int));
  10. extern  void    Quit PROTO((Widget,XtPointer,XtPointer)),
  11.         SingleAction PROTO((Widget,XtPointer,XtPointer)),
  12.         MultiAction PROTO((Widget,XtPointer,XtPointer)),
  13.                 ReadALine PROTO((FILE*,char **)),
  14.                 Source PROTO((Widget,XtPointer,XtPointer));
  15. extern  void    set_popup PROTO((Widget,XEvent *,String *,Cardinal *));
  16. extern    void    delay PROTO((Widget,XEvent *,String *,Cardinal *));
  17.  
  18. static  void    SetButton PROTO((int,char *,int));
  19. static  void    SetSubMenuButton PROTO((int,char *,int));
  20. static    void    CreatePopupShell PROTO((int));
  21. extern    void    CreateSubMenuButton PROTO((int, char *, int, char*, int));
  22. extern    void    DefaultButtons PROTO((Info *,int));
  23.  
  24. #include "trans.h"
  25.  
  26.  
  27. extern void
  28. CreateSubButton(data, button_no, is_icon, label)
  29. Info    *data;
  30. int        button_no;
  31. int        is_icon;
  32. char    *label;
  33. {
  34.     char    w_buffer[80];
  35.     char    ptrans[250];
  36.  
  37.     /* 
  38.         create translations for the button that 
  39.         pops up the sub menu
  40.     */
  41.     (void) sprintf(ptrans, pb_Trans, button_no, popup_menu_name);
  42.  
  43.     /* create main menu button that will popup sub menu */
  44.     (void) sprintf(w_buffer, "action%d", button_no);
  45.     action[button_no] = XtVaCreateWidget(
  46.             w_buffer,
  47.             commandWidgetClass,
  48.             box,
  49.             NULL);
  50.  
  51.     XtVaSetValues(action[button_no],
  52.             XtNtranslations,
  53.             XtParseTranslationTable(ptrans),
  54.             NULL);
  55.  
  56.     SetButton(is_icon, label, button_no);
  57.     CreatePopupShell(button_no);
  58. }
  59.  
  60.  
  61. extern void
  62. CreateMainMenuButton(data, button_no, is_icon, label, command)
  63. Info    *data;
  64. int    button_no;
  65. int    is_icon;
  66. char    *label;
  67. char    *command;
  68. {
  69.         char    w_buffer[80];
  70.  
  71.         (void) sprintf(w_buffer, "action%d", button_no);
  72.  
  73.         action[button_no] = XtVaCreateWidget(
  74.                 w_buffer,          /* widget name */    
  75.                 commandWidgetClass,  /* widget class */
  76.                 box,                /* parent widget */
  77.                 NULL);       /* terminate varargs list */
  78.  
  79.         SetButton(is_icon, label, button_no);
  80.  
  81.         /* add callback for this button */
  82.         if(data->sensitive)
  83.         {
  84.             if((is_icon == TEXT) || (is_icon == ICON))
  85.                 XtAddCallback(action[button_no],XtNcallback, SingleAction, command);
  86.             else
  87.                 XtAddCallback(action[button_no],XtNcallback,
  88.                     MultiAction,command);
  89.         } 
  90.         else
  91.         {
  92.             if((is_icon == TEXT) || (is_icon == ICON))
  93.                 XtAddCallback(action[button_no],XtNcallback,
  94.                     MultiAction,command);
  95.             else
  96.                 XtAddCallback(action[button_no],XtNcallback,
  97.                     SingleAction,command);
  98.         }
  99. }
  100.  
  101. static    void
  102. SetButton(is_icon,icon,button_num)
  103. int    is_icon;
  104. char    *icon;
  105. int    button_num;
  106. {
  107.  
  108.     /* if button title is a string then */
  109.     if((is_icon == TEXT) || (is_icon == TEXTPLUS))
  110.     {
  111.         XtVaSetValues(
  112.             action[button_num],
  113.             XtNwidth,
  114.             W_WIDTH,
  115.             XtNheight,
  116.             W_HEIGHT,
  117.             XtNlabel,
  118.             icon,    /* button title */
  119.             NULL);
  120.     }
  121.     /* if button title is a bitmap then */
  122.     if((is_icon == ICON) || (is_icon == ICONPLUS))    
  123.     {
  124.         XtVaSetValues(
  125.             action[button_num],
  126.             XtNwidth,
  127.             W_WIDTH,
  128.             XtNheight,
  129.             W_HEIGHT,
  130.             XtVaTypedArg,
  131.             XtNbitmap,
  132.             XtRString,
  133.             icon,    /* button title (bitmap) */
  134.             strlen(icon) +1,
  135.             NULL);
  136.     }
  137. }
  138.  
  139. static    void
  140. SetSubMenuButton(is_icon,icon,button_num)
  141. int    is_icon;
  142. char    *icon;
  143. int    button_num;
  144. {
  145.  
  146.     /* if button title is a string then */
  147.     if((is_icon == TEXT) || (is_icon == TEXTPLUS))
  148.     {
  149.         XtVaSetValues(
  150.             sub_action[button_num],
  151.             XtNwidth,
  152.             W_WIDTH,
  153.             XtNheight,
  154.             W_HEIGHT,
  155.             XtNlabel,
  156.             icon,    /* button title */
  157.             NULL);
  158.     }
  159.     /* if button title is a bitmap then */
  160.     if((is_icon == ICON) || (is_icon == ICONPLUS))    
  161.     {
  162.         XtVaSetValues(
  163.             sub_action[button_num],
  164.             XtNwidth,
  165.             W_WIDTH,
  166.             XtNheight,
  167.             W_HEIGHT,
  168.             XtVaTypedArg,
  169.             XtNbitmap,
  170.             XtRString,
  171.             icon,    /* button title (bitmap) */
  172.             strlen(icon) +1,
  173.             NULL);
  174.     }
  175.     if(button_num > 0)
  176.         XtVaSetValues(sub_action[button_num], XtNfromVert,
  177.                 sub_action[button_num - 1],NULL);
  178. }
  179.  
  180. /*
  181.     The function PositionButtons places each button next to the 
  182.     previous button one until the number of buttons equals the 
  183.     number of columns.  Then a new row is started.
  184. */
  185. extern void
  186. PositionButtons(i,columns)
  187. int    i;
  188. int    columns;
  189. {
  190.     int    button_no;
  191.     int    position = 0;
  192.  
  193.     button_no = 1;
  194.     while((button_no < columns) && (button_no <= i))
  195.     {
  196.         XtVaSetValues(action[button_no],
  197.             XtNfromHoriz,action[button_no - 1], NULL);
  198.         button_no++;
  199.     }
  200.     position = columns;
  201.     while(button_no <= i)
  202.     {
  203.         if(position == columns)
  204.         {
  205.             XtVaSetValues(action[button_no],
  206.                 XtNfromVert,action[button_no - columns],NULL);
  207.             position = 0;
  208.         }
  209.         else
  210.         {
  211.             XtVaSetValues(action[button_no],
  212.                 XtNfromVert,action[button_no - columns],NULL);
  213.             XtVaSetValues(action[button_no],
  214.                 XtNfromHoriz,action[button_no -1],NULL);
  215.         }
  216.         position++;
  217.         button_no++;
  218.     }
  219. }
  220.  
  221. /*
  222.     Once all the buttons have been created then make sure they are 
  223.     managed.
  224. */
  225. extern void
  226. ManageButtons()
  227. {
  228.     WidgetList      w_list;
  229.     Cardinal        num_childern, i;
  230.  
  231.     XtVaGetValues(box,
  232.         XtNchildren,
  233.         &w_list,
  234.         XtNnumChildren,
  235.         &num_childern,
  236.         NULL);
  237.  
  238.     for(i = 0; i < num_childern; i++)
  239.         XtManageChild(w_list[i]);
  240. }
  241.  
  242. static    void
  243. CreatePopupShell(shell_no)
  244. int    shell_no;
  245. {
  246.  
  247.     popup_shell = XtCreatePopupShell(
  248.             "popup_menu",
  249.             overrideShellWidgetClass,
  250.             action[shell_no],
  251.             NULL,
  252.             0);
  253.  
  254.     XtVaSetValues(popup_shell,
  255.             XtNtranslations,
  256.             XtParseTranslationTable(pm_Trans),
  257.             NULL);
  258.  
  259.     popup_layout = XtCreateManagedWidget(
  260.             "popup_layout",
  261.             formWidgetClass,
  262.             popup_shell,
  263.             NULL,
  264.             0);
  265. }
  266.  
  267. static    void
  268. CreateDefaultSubButton(key_word,button_name,button_num)
  269. int     key_word;
  270. char    *button_name;
  271. int     button_num;
  272. {
  273.         char    w_buffer[80];
  274.  
  275.         (void) sprintf(w_buffer,"sub_action%d",button_num);
  276.         sub_action[button_num] = XtVaCreateManagedWidget(
  277.                         w_buffer,
  278.                         menuButtonWidgetClass,
  279.                         popup_layout,
  280.                         NULL);
  281.  
  282.         SetSubMenuButton(key_word,button_name,button_num); 
  283. }
  284.  
  285. extern    void
  286. CreateSubMenuButton(key_word, button_name, button_num, command, sensitive)
  287. int    key_word;
  288. char    *button_name;
  289. int    button_num;
  290. char    *command;
  291. int    sensitive;
  292. {
  293.     char    w_buffer[80];
  294.  
  295.     (void) sprintf(w_buffer,"sub_action%d", button_num);
  296.     sub_action[button_num] = XtVaCreateManagedWidget(
  297.             w_buffer,
  298.             menuButtonWidgetClass,
  299.             popup_layout,
  300.             NULL);
  301.  
  302.     SetSubMenuButton(key_word, button_name, button_num); 
  303.  
  304.     if(sensitive)
  305.     {
  306.         if((key_word == TEXT) || (key_word == ICON))
  307.             XtAddCallback(sub_action[button_num],
  308.             XtNcallback,SingleAction,command);
  309.         else
  310.             XtAddCallback(sub_action[button_num],
  311.             XtNcallback,MultiAction,command);
  312.     }
  313.     else
  314.     {
  315.         if((key_word == TEXT) || (key_word == ICON))
  316.             XtAddCallback(sub_action[button_num],
  317.             XtNcallback,MultiAction,command);
  318.         else
  319.             XtAddCallback(sub_action[button_num],
  320.             XtNcallback,SingleAction,command);
  321.     }
  322.  
  323.     XtVaSetValues(sub_action[button_num],
  324.             XtNtranslations,
  325.             XtParseTranslationTable(cb_Trans),
  326.             NULL);
  327. }
  328.  
  329. extern    void
  330. set_popup(w,event,params,num_params)
  331. Widget    w;
  332. XEvent    *event;    /* unused */
  333. String    *params;
  334. Cardinal    *num_params;
  335. {
  336.     /*
  337.         position the menu, whose name was passed as the first and
  338.         only parameter at position (20,5) relative to the widget w.
  339.     */
  340.     Widget    a_shell;
  341.     Position    x,y;
  342.  
  343.     if(*num_params != 1)
  344.         XtError("Wrong parameter count passed to set_popup()");
  345.     
  346.     /* get the internal Widget id of the named shell */
  347.     a_shell = XtNameToWidget(toplevel,params[0]);
  348.     if(a_shell == NULL)
  349.         XtError("FATAL ERROR: Wrong shell name passed to set_popup()\n\
  350.             please check icon file is correct");
  351.     XtTranslateCoords(w,(Position) 5,(Position) 5, &x,&y);
  352.     XtVaSetValues(a_shell,XtNx,x,XtNy,y,NULL);
  353. }
  354.  
  355. extern    void
  356. delay(w,event,params,num_params)
  357. Widget  w;
  358. XEvent  *event; /* unused */
  359. String  *params;
  360. Cardinal        *num_params;
  361. {
  362.     (void) sleep(1);
  363. }
  364.  
  365. extern    void
  366. DefaultButtons(data,last_button)
  367. Info    *data;
  368. int    last_button;
  369. {
  370.     int    string_length;
  371.     char    w_buffer[80];
  372.     char    ptrans[250];
  373.     char    *edit_file, *h_file;
  374.  
  375.     /* create default sub menu which will contain source, edit & quit */
  376.     /* buttons.  Default menu button is called Manage  */
  377.  
  378.     /* create main menu button */
  379.     (void) sprintf(ptrans,pb_Trans,last_button,popup_menu_name);
  380.  
  381.     (void) sprintf(w_buffer,"action%d",last_button);
  382.     action[last_button] = XtVaCreateWidget(
  383.             w_buffer,
  384.             commandWidgetClass,
  385.             box,
  386.             NULL);
  387.  
  388.     XtVaSetValues(action[last_button],XtNtranslations,
  389.             XtParseTranslationTable(ptrans),NULL);
  390.  
  391.     SetButton(TEXT,"Edit\nIcons",last_button);
  392.  
  393.     /* create popup shell */
  394.     CreatePopupShell(last_button);
  395.  
  396.     CreateDefaultSubButton(TEXT,"Source",0);
  397.  
  398.     XtAddCallback(sub_action[0],XtNcallback,Source,data);
  399.  
  400.     XtVaSetValues(sub_action[0],XtNtranslations,
  401.                 XtParseTranslationTable(cb_Trans),NULL);
  402.  
  403.     CreateDefaultSubButton(TEXT,"Edit",1);
  404.  
  405.     string_length = strlen(data->editor);
  406.     string_length += strlen(data->icon_file);
  407.     if((edit_file = malloc(string_length + 1)) == NULL)
  408.     {
  409.         (void) fprintf(stderr,"ERROR: out of memory for string edit_file.  In function CreateButtons.\n");
  410.         exit(1);
  411.     }
  412.     strcpy(edit_file,data->editor);
  413.     strcat(edit_file,data->icon_file);
  414.  
  415.     XtAddCallback(sub_action[1],XtNcallback,SingleAction,edit_file);
  416.  
  417.     XtVaSetValues(sub_action[1],XtNtranslations,
  418.                 XtParseTranslationTable(cb_Trans),NULL);
  419.  
  420.     CreateDefaultSubButton(TEXT,"Help",2);
  421.  
  422.     string_length = 0;
  423.     string_length = strlen(FORMATTER);
  424.     string_length += strlen(HELP_VIEWER);
  425.     string_length += strlen(data->help_file);
  426.     if((h_file = malloc(string_length +1)) == NULL)
  427.     {
  428.         (void) fprintf(stderr,"ERROR: out of memory for string edit_file.  In function CreateButtons.\n");
  429.         exit(1);
  430.     }
  431.     strcpy(h_file,FORMATTER);
  432.     strcat(h_file,data->help_file);
  433.     strcat(h_file,HELP_VIEWER);
  434.     XtAddCallback(sub_action[2],XtNcallback,SingleAction,h_file);
  435.  
  436.     XtVaSetValues(sub_action[2],XtNtranslations,
  437.                 XtParseTranslationTable(cb_Trans),NULL);
  438.  
  439.     CreateDefaultSubButton(TEXT,"Quit",3);
  440.  
  441.     XtAddCallback(sub_action[3],XtNcallback,Quit,NULL);
  442.  
  443.     XtVaSetValues(sub_action[3],XtNtranslations,
  444.                 XtParseTranslationTable(cb_Trans),NULL);
  445. }
  446.  
  447.