1 /* Copyright (C) 1992-1998 The Geometry Center
2 * Copyright (C) 1998-2000 Stuart Levy, Tamara Munzner, Mark Phillips
3 *
4 * This file is part of Geomview.
5 *
6 * Geomview is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published
8 * by the Free Software Foundation; either version 2, or (at your option)
9 * any later version.
10 *
11 * Geomview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with Geomview; see the file COPYING. If not, write
18 * to the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139,
19 * USA, or visit http://www.gnu.org.
20 */
21
22 #if HAVE_CONFIG_H
23 # include "config.h"
24 #endif
25
26 #if 0
27 static char copyright[] = "Copyright (C) 1992-1998 The Geometry Center\n\
28 Copyright (C) 1998-2000 Stuart Levy, Tamara Munzner, Mark Phillips";
29 #endif
30
31 #include "mibload.h"
32 #include "mibwidgets.h"
33
34 extern Display *dpy;
35 extern GC mib_gc;
36 static int delhandler; /* delay adding event handler until menu
37 is actually created */
38
39 /* Code for Menu */
40 /*****************************************************************************/
41
mib_create_Menu(mib_Widget * parent,char * name,char * label,int posx,int posy,int width,int height,int mib_fill)42 mib_Widget *mib_create_Menu(mib_Widget *parent, char *name, char *label,
43 int posx, int posy, int width, int height, int mib_fill)
44 {
45 mib_Widget *temp;
46 mib_Menu *myres;
47 Widget wtemp;
48 char label_temp[50];
49 Arg args[20];
50 int n;
51
52 /* create the new widget and add it to the tree */
53
54 temp = mib_new_mib_Widget();
55 if (mib_fill == WDEFAULT)
56 mib_add_backward(temp, parent);
57 else
58 mib_add_mib_Widget(temp, parent);
59
60 myres = (mib_Menu *)malloc(sizeof(mib_Menu));
61
62 /* initialize public resources */
63
64 if (mib_fill == WDEFAULT)
65 {
66 temp->name = (char *)malloc(strlen(name)+1);
67 strcpy(temp->name,name);
68 }
69 temp->mib_class = (char *)malloc(8);
70 sprintf(temp->mib_class,"Menu");
71 temp->mib_class_num = MIB_MENU;
72 temp->width = width;
73 temp->height = height;
74 temp->topOffset = posy;
75 temp->leftOffset = posx;
76 temp->bottomOffset = 0;
77 temp->rightOffset = 0;
78 temp->topAttachment = 1;
79 temp->leftAttachment = 1;
80 temp->bottomAttachment = 0;
81 temp->rightAttachment = 0;
82
83 temp->mib_allowresize = 0;
84
85 /* initialize private resources */
86
87 temp->myres = (void *)myres;
88 myres->numitems = 0;
89
90 /* create Xt widget */
91
92 n = 0;
93
94 if (mib_fill == WDEFAULT)
95 {
96 XtSetArg (args[n], XmNleftAttachment, XmATTACH_FORM); n++;
97 XtSetArg (args[n], XmNleftOffset, posx);n++;
98 XtSetArg (args[n], XmNtopAttachment, XmATTACH_FORM); n++;
99 XtSetArg (args[n], XmNtopOffset, posy);n++;
100 }
101 XtSetArg (args[n], XmNrubberPositioning, False); n++;
102
103 temp->me = XtCreateManagedWidget(name, xmRowColumnWidgetClass,
104 temp->parent->me, args, n);
105
106 n = 0;
107
108 delhandler = 1;
109 if (mib_fill == WDEFAULT)
110 {
111 delhandler = 0;
112 myres->numitems = 3;
113 myres->my_menu = (MenuItem *)malloc(sizeof(MenuItem) * (myres->numitems+1));
114 myres->items = (Widget *)malloc(sizeof(Widget) * myres->numitems);
115 sprintf(label_temp, "Item 1");
116 myres->my_menu[0].label = (char *)malloc(strlen(label_temp)+1);
117 strcpy(myres->my_menu[0].label,label_temp);
118 myres->my_menu[0].class = &xmPushButtonGadgetClass;
119 myres->my_menu[0].mnemonic = '1';
120 myres->my_menu[0].accelerator = NULL;
121 myres->my_menu[0].accel_text = NULL;
122 myres->my_menu[0].accel_text = NULL;
123 myres->my_menu[0].callback = NULL;
124 myres->my_menu[0].callback_data = NULL;
125 myres->my_menu[0].subitems = (struct _menu_item *)1;
126
127 sprintf(label_temp, "Item 2");
128 myres->my_menu[1].label = (char *)malloc(strlen(label_temp)+1);
129 strcpy(myres->my_menu[1].label,label_temp);
130 myres->my_menu[1].class = &xmPushButtonGadgetClass;
131 myres->my_menu[1].mnemonic = '2';
132 myres->my_menu[1].accelerator = NULL;
133 myres->my_menu[1].accel_text = NULL;
134 myres->my_menu[1].accel_text = NULL;
135 myres->my_menu[1].callback = NULL;
136 myres->my_menu[1].callback_data = NULL;
137 myres->my_menu[1].subitems = (struct _menu_item *)1;
138
139 sprintf(label_temp, "Item 3");
140 myres->my_menu[2].label = (char *)malloc(strlen(label_temp)+1);
141 strcpy(myres->my_menu[2].label,label_temp);
142 myres->my_menu[2].class = &xmPushButtonGadgetClass;
143 myres->my_menu[2].mnemonic = '2';
144 myres->my_menu[2].accelerator = NULL;
145 myres->my_menu[2].accel_text = NULL;
146 myres->my_menu[2].accel_text = NULL;
147 myres->my_menu[2].callback = NULL;
148 myres->my_menu[2].callback_data = NULL;
149 myres->my_menu[2].subitems = (struct _menu_item *)1;
150
151 myres->my_menu[3].label = NULL;
152
153 wtemp = BuildMenu(temp->me, XmMENU_OPTION, "", 'M', myres->my_menu);
154 XtManageChild(wtemp);
155 }
156
157 if (mib_fill == WEDIT || mib_fill == WDEFAULT)
158 {
159
160 /* default menu */
161
162 if (!delhandler)
163 {
164 mib_apply_eventhandlers(wtemp, temp);
165 mib_apply_eventhandlers(temp->me, temp);
166 }
167 }
168
169 if (mib_fill == WEMPTY)
170 delhandler = 0;
171
172 return temp;
173 }
174
mib_delete_Menu(mib_Widget * this)175 void mib_delete_Menu(mib_Widget *this)
176 {
177 mib_Menu *temp = (mib_Menu *)this->myres;
178
179 free(temp);
180 }
181
mib_save_Menu(mib_Widget * this,FILE * fout)182 void mib_save_Menu(mib_Widget *this, FILE *fout)
183 {
184 mib_Menu *temp = (mib_Menu *)this->myres;
185 int count;
186
187 fprintf(fout, "numitems: %d\\n\\\n", temp->numitems);
188
189 for (count=0; count < temp->numitems; count++)
190 {
191 fprintf(fout, "item: \\\"%s\\\"\\n\\\n", temp->my_menu[count].label);
192 }
193
194 }
195
mib_load_Menu(mib_Widget * this,mib_Buffer * fin)196 int mib_load_Menu(mib_Widget *this, mib_Buffer *fin)
197 {
198 char res[MI_MAXSTRLEN];
199 char val[MI_MAXSTRLEN];
200 int count, vallen;
201 Widget wtemp;
202 mib_Menu *myres = (mib_Menu *)this->myres;
203
204
205 if (!mib_read_line(fin, res, val))
206 return 0;
207
208 if (!strcmp(res, "numitems"))
209 {
210 myres->numitems = 0;
211 sscanf(val, "%d", &(myres->numitems));
212 if (!(myres->numitems))
213 return 0;
214
215 myres->my_menu = (MenuItem *)malloc(sizeof(MenuItem) * (myres->numitems+1));
216 myres->items = (Widget *)malloc(sizeof(Widget) * myres->numitems);
217 for (count = 0; count < myres->numitems; count++)
218 {
219 if (!mib_read_line(fin, res, val))
220 return 0;
221 vallen = strlen(val);
222 if (vallen < 2)
223 return 0;
224 val[vallen-1] = '\0';
225 myres->my_menu[count].label = (char *)malloc(vallen-1);
226 sprintf(myres->my_menu[count].label, "%s", &(val[1]));
227 myres->my_menu[count].class = &xmPushButtonGadgetClass;
228 myres->my_menu[count].mnemonic = '\0';
229 myres->my_menu[count].accelerator = NULL;
230 myres->my_menu[count].accel_text = NULL;
231 myres->my_menu[count].accel_text = NULL;
232 myres->my_menu[count].callback = NULL;
233 myres->my_menu[count].callback_data = NULL;
234 myres->my_menu[count].subitems = (struct _menu_item *)1;
235 }
236 myres->my_menu[myres->numitems].label = NULL;
237
238 wtemp = BuildMenu(this->me, XmMENU_OPTION, "", '\0', myres->my_menu);
239 XtManageChild(wtemp);
240
241 if (delhandler)
242 {
243 mib_apply_eventhandlers(wtemp, this);
244 mib_apply_eventhandlers(this->me, this);
245 }
246 else
247 this->me = wtemp;
248
249 for (count = 0; count < myres->numitems; count++)
250 {
251 /* this hack extracts the widget for each item in the menu
252 for possible use by the programmer */
253 myres->items[count] = (Widget)(myres->my_menu[count].subitems);
254 }
255
256 }
257 else
258 return 0;
259
260 if (!mib_read_line(fin, res, val))
261 return 0;
262 if (strcmp(res,"EndWidget"))
263 return 0;
264
265 return 1;
266 }
267