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 
37 /* Code for Toggle */
38 /*****************************************************************************/
39 
mib_create_Toggle(mib_Widget * parent,char * name,char * label,int posx,int posy,int width,int height,int mib_fill)40 mib_Widget *mib_create_Toggle(mib_Widget *parent, char *name, char *label,
41 		int posx, int posy, int width, int height, int mib_fill)
42 {
43   mib_Widget *temp;
44   mib_Toggle *myres;
45   /*unsigned char *label_text;*/
46   XmString	label_text = NULL;
47   Arg     args[20];
48   int     n;
49 
50   /* create the new widget and add it to the tree */
51 
52   temp = mib_new_mib_Widget();
53   if (mib_fill == WDEFAULT)
54     mib_add_backward(temp, parent);
55   else
56     mib_add_mib_Widget(temp, parent);
57 
58   myres = (mib_Toggle *)malloc(sizeof(mib_Toggle));
59 
60   /* initialize public resources */
61 
62   if (mib_fill == WDEFAULT)
63   {
64     temp->name = (char *)malloc(strlen(name)+1);
65     strcpy(temp->name,name);
66   }
67   temp->mib_class = (char *)malloc(7);
68   sprintf(temp->mib_class,"Toggle");
69   temp->mib_class_num = MIB_TOGGLE;
70   temp->width = width;
71   temp->height = height;
72   temp->topOffset = posy;
73   temp->leftOffset = posx;
74   temp->bottomOffset = 0;
75   temp->rightOffset = 0;
76   temp->topAttachment = 1;
77   temp->leftAttachment = 1;
78   temp->bottomAttachment = 0;
79   temp->rightAttachment = 0;
80 
81   temp->mib_allowresize = 0;
82 
83   /* initialize private resources */
84 
85   temp->myres = (void *)myres;
86 
87   myres->isize = 0;
88   if (mib_fill == WDEFAULT)
89   {
90     myres->label = (char *)malloc(strlen(label)+1);
91     strcpy(myres->label,label);
92   }
93 
94   /* create Xt widget */
95 
96   n = 0;
97 
98   if (mib_fill == WDEFAULT)
99   {
100     label_text = XmStringCreateLtoR(label, XmSTRING_DEFAULT_CHARSET);
101 
102     XtSetArg (args[n], XmNlabelString, label_text); n++;
103     XtSetArg (args[n], XmNleftAttachment, XmATTACH_FORM); n++;
104     XtSetArg (args[n], XmNleftOffset, posx);n++;
105     XtSetArg (args[n], XmNtopAttachment, XmATTACH_FORM); n++;
106     XtSetArg (args[n], XmNtopOffset, posy);n++;
107 /*    XtSetArg (args[n], XmNwidth, width); n++;
108     XtSetArg (args[n], XmNheight, height); n++;*/
109   }
110 
111   /* Do not set the width or height, if zero.
112      The core widget will compute appropriately. */
113   if ( width ) {
114       XtSetArg (args[n], XmNwidth, width); n++;
115   }
116   if ( height ) {
117       XtSetArg (args[n], XmNheight, height); n++;
118   }
119   XtSetArg (args[n], XmNspacing, 4); n++;
120   XtSetArg (args[n], XmNhighlightThickness, 0); n++;
121   XtSetArg (args[n], XmNrubberPositioning, False); n++;
122   XtSetArg (args[n], XmNindicatorType, XmN_OF_MANY);
123 
124   temp->me = XtCreateManagedWidget(name, xmToggleButtonWidgetClass,
125                 temp->parent->me, args, n);
126 
127   if (mib_fill == WDEFAULT)
128   {
129     XmStringFree(label_text);
130   }
131 
132   if (mib_fill == WEDIT || mib_fill == WDEFAULT)
133   {
134     mib_apply_eventhandlers(temp->me, temp);
135   }
136 
137   return temp;
138 }
139 
mib_delete_Toggle(mib_Widget * this)140 void mib_delete_Toggle(mib_Widget *this)
141 {
142   mib_Toggle *temp = (mib_Toggle *)this->myres;
143 
144   free(temp->label);
145   free(temp);
146 }
147 
mib_save_Toggle(mib_Widget * this,FILE * fout)148 void mib_save_Toggle(mib_Widget *this, FILE *fout)
149 {
150   mib_Toggle *temp = (mib_Toggle *)this->myres;
151 
152   fprintf(fout,"label: \\\"%s\\\"\\n\\\n", temp->label);
153   fprintf(fout,"indicatorSize: %d\\n\\\n", temp->isize);
154 }
155 
mib_load_Toggle(mib_Widget * this,mib_Buffer * fin)156 int mib_load_Toggle(mib_Widget *this, mib_Buffer *fin)
157 {
158   mib_Toggle    *myres;
159   /*unsigned char *label_text;*/
160   XmString	label_text;
161   char          res[MI_MAXSTRLEN];
162   char          val[MI_MAXSTRLEN];
163   Arg           args[20];
164   int           n, got_line, vallen;
165 
166   myres = (mib_Toggle *)this->myres;
167 
168   got_line = mib_read_line(fin, res, val);
169   if (!got_line)
170     return 0;
171 
172   if (!strcmp(res,"label"))
173   {
174     vallen = strlen(val);
175     if (vallen < 2)
176       return 0;
177     val[vallen-1] = '\0';
178     myres->label = (char *)malloc(vallen-1);
179     sprintf(myres->label,"%s",&(val[1]));
180 
181     label_text = XmStringCreateLtoR(myres->label, XmSTRING_DEFAULT_CHARSET);
182 
183     n = 0;
184     XtSetArg (args[n], XmNlabelString, label_text); n++;
185 
186     /* Do not set the width or height, if zero.
187        The core widget will compute appropriately. */
188     if ( this->width ) {
189 	XtSetArg (args[n], XmNwidth, this->width); n++;
190     }
191     if ( this->height ) {
192 	XtSetArg (args[n], XmNheight, this->height); n++;
193     }
194     XtSetValues(this->me, args, n);
195     XmStringFree(label_text);
196 
197   }
198   else
199     return 0;
200 
201   got_line = mib_read_line(fin, res, val);
202   if (!got_line)
203     return 0;
204 
205   if (!strcmp(res, "indicatorSize"))
206   {
207     sscanf(val, "%d", &(myres->isize));
208     if (myres->isize)
209       XtVaSetValues(this->me, XmNindicatorSize, (Dimension)myres->isize,
210 		  XmNmarginBottom, 0, XmNmarginTop, 0, XmNmarginLeft, 0,
211 		  XmNmarginRight, 0, XmNspacing, 0, NULL);
212     got_line = mib_read_line(fin, res, val);
213     if (!got_line)
214       return 0;
215   }
216 
217   if (strcmp(res,"EndWidget"))
218     return 0;
219 
220   return 1;
221 }
222