1 /* $Id$Revision: */
2 /* vim:set shiftwidth=4 ts=8: */
3 
4 /*************************************************************************
5  * Copyright (c) 2011 AT&T Intellectual Property
6  * All rights reserved. This program and the accompanying materials
7  * are made available under the terms of the Eclipse Public License v1.0
8  * which accompanies this distribution, and is available at
9  * http://www.eclipse.org/legal/epl-v10.html
10  *
11  * Contributors: See CVS logs. Details at http://www.graphviz.org/
12  *************************************************************************/
13 
14 #include "topviewfuncs.h"
15 #include "topviewsettings.h"
16 #include "gui.h"
17 #include "colorprocs.h"
18 #include "viewport.h"
19 #include "memory.h"
20 
color_change_request(GtkWidget * widget,gpointer user_data)21 void color_change_request(GtkWidget * widget, gpointer user_data)
22 {
23     view->refresh.color=1;
24 }
size_change_request(GtkWidget * widget,gpointer user_data)25 void size_change_request(GtkWidget * widget, gpointer user_data)
26 {
27     view->refresh.nodesize=1;
28 }
29 
on_settingsOKBtn_clicked(GtkWidget * widget,gpointer user_data)30 void on_settingsOKBtn_clicked(GtkWidget * widget, gpointer user_data)
31 {
32     on_settingsApplyBtn_clicked(widget, user_data);
33     gtk_widget_hide(glade_xml_get_widget(xml, "dlgSettings"));
34 }
35 
on_settingsApplyBtn_clicked(GtkWidget * widget,gpointer user_data)36 void on_settingsApplyBtn_clicked(GtkWidget * widget, gpointer user_data)
37 {
38     update_graph_from_settings(view->g[view->activeGraph]);
39     set_viewport_settings_from_template(view, view->g[view->activeGraph]);
40 #if 0
41     update_topview(view->g[view->activeGraph], view->Topview, 0);
42     settvcolorinfo(view->g[view->activeGraph],view->Topview);
43     init_node_size(view->g[view->activeGraph]);
44 #endif
45     updateSmGraph(view->g[view->activeGraph],view->Topview);
46 }
on_dlgSettings_close(GtkWidget * widget,gpointer user_data)47 void on_dlgSettings_close (GtkWidget * widget, gpointer user_data)
48 {
49     gtk_widget_hide(glade_xml_get_widget(xml, "dlgSettings"));
50     /* printf ("setting window is being closed\n"); */
51 }
52 
on_settingsCancelBtn_clicked(GtkWidget * widget,gpointer user_data)53 void on_settingsCancelBtn_clicked(GtkWidget * widget, gpointer user_data)
54 {
55     gtk_widget_hide(glade_xml_get_widget(xml, "dlgSettings"));
56 }
copy_attr(Agraph_t * destG,char * attr,Agraph_t * g)57 void copy_attr(Agraph_t* destG,char* attr,Agraph_t* g)
58 {
59     agattr(g,AGRAPH,attr,agget(destG,attr));
60 }
61 
62 
set_color_button_widget(char * attribute,char * widget_name)63 static int set_color_button_widget(char *attribute, char *widget_name)
64 {
65     GdkColor color;
66     gvcolor_t cl;
67 
68     char *buf;
69     attribute=attribute +13;
70     buf = agget(view->g[view->activeGraph], attribute);
71     if ((!buf) || (strcmp(buf, "") == 0))
72     {
73 	buf = agget(view->systemGraphs.def_attrs, attribute);
74 	copy_attr(view->systemGraphs.def_attrs, attribute,view->g[view->activeGraph]);
75     }
76     if (buf) {
77 	colorxlate(buf, &cl, RGBA_DOUBLE);
78 	color.red = (int) (cl.u.RGBA[0] * 65535.0);
79 	color.green = (int) (cl.u.RGBA[1] * 65535.0);
80 	color.blue = (int) (cl.u.RGBA[2] * 65535.0);
81 	gtk_color_button_set_color((GtkColorButton *)
82 				   glade_xml_get_widget(xml, widget_name),
83 				   &color);
84 	return 1;
85 
86     }
87     return 0;
88 }
get_color_button_widget_to_attribute(char * attribute,char * widget_name,Agraph_t * g)89 static int get_color_button_widget_to_attribute(char *attribute,
90 						char *widget_name,
91 						Agraph_t * g)
92 {
93     GdkColor color;
94     char *buf = N_GNEW(256, char);
95     attribute=attribute +13;
96 
97     gtk_color_button_get_color((GtkColorButton *)
98 			       glade_xml_get_widget(xml, widget_name),
99 			       &color);
100     sprintf(buf, "#%02x%02x%02x",
101 	    (int) ((float) color.red / 65535.0 * 255.0),
102 	    (int) ((float) color.green / 65535.0 * 255.0),
103 	    (int) ((float) color.blue / 65535.0 * 255.0));
104     agattr(g, AGRAPH, attribute, buf);
105     free(buf);
106     return 1;
107 }
get_text_widget_to_attribute(char * attribute,char * widget_name,Agraph_t * g)108 static int get_text_widget_to_attribute(char *attribute, char *widget_name,
109 					Agraph_t * g)
110 {
111     attribute=attribute +9;
112 
113     if (strlen(attribute) > 512)
114 	return 0;
115     agattr(g, AGRAPH, attribute,
116 	    (char*)gtk_entry_get_text((GtkEntry *)
117 			       glade_xml_get_widget(xml, widget_name)));
118     return 1;
119 }
set_text_widget(char * attribute,char * widget_name)120 static int set_text_widget(char *attribute, char *widget_name)
121 {
122     char *buf;
123     attribute=attribute +9;
124 
125     buf = agget(view->g[view->activeGraph], attribute);
126     if ((!buf) || (strcmp(buf, "") == 0))
127     {
128 	buf = agget(view->systemGraphs.def_attrs, attribute);
129 	copy_attr(view->systemGraphs.def_attrs, attribute,view->g[view->activeGraph]);
130     }
131 
132     if (buf) {
133 	gtk_entry_set_text((GtkEntry *)
134 			   glade_xml_get_widget(xml, widget_name), buf);
135 
136 	return 1;
137     }
138     return 0;
139 }
set_checkbox_widget(char * attribute,char * widget_name)140 static int set_checkbox_widget(char *attribute, char *widget_name)
141 {
142     char *buf;
143     int value;
144     attribute=attribute +10;
145 
146     buf = agget(view->g[view->activeGraph], attribute);
147     if ((!buf) || (strcmp(buf, "") == 0))
148     {
149 	buf = agget(view->systemGraphs.def_attrs, attribute);
150 	copy_attr(view->systemGraphs.def_attrs, attribute,view->g[view->activeGraph]);
151     }
152 
153 
154     if (buf) {
155 	value = atoi(buf);
156 	gtk_toggle_button_set_active((GtkToggleButton *)
157 				     glade_xml_get_widget(xml,
158 							  widget_name),
159 				     value);
160 	return 1;
161     }
162     return 0;
163 
164 
165 }
166 
get_checkbox_widget_to_attribute(char * attribute,char * widget_name,Agraph_t * g)167 static int get_checkbox_widget_to_attribute(char *attribute,
168 					    char *widget_name,
169 					    Agraph_t * g)
170 {
171     int value;
172     char buf[100];
173     attribute=attribute +10;
174 
175     value = (int) gtk_toggle_button_get_active((GtkToggleButton *)
176 					       glade_xml_get_widget(xml,
177 								    widget_name));
178     sprintf(buf, "%d", value);
179     agattr(g, AGRAPH, attribute, buf);
180    return 1;
181 }
182 
set_spinbtn_widget(char * attribute,char * widget_name)183 static int set_spinbtn_widget(char *attribute, char *widget_name)
184 {
185     char *buf;
186     float value;
187     attribute=attribute +12;
188 
189     buf = agget(view->g[view->activeGraph], attribute);
190     if ((!buf) || (strcmp(buf, "") == 0))
191     {
192 	buf = agget(view->systemGraphs.def_attrs, attribute);
193 	copy_attr(view->systemGraphs.def_attrs, attribute,view->g[view->activeGraph]);
194     }
195     if (buf) {
196 	value = (float) atof(buf);
197 	gtk_spin_button_set_value((GtkSpinButton *)
198 				  glade_xml_get_widget(xml, widget_name),
199 				  value);
200 	return 1;
201     }
202     return 0;
203 }
get_spinbtn_widget_to_attribute(char * attribute,char * widget_name,Agraph_t * g)204 static int get_spinbtn_widget_to_attribute(char *attribute,
205 					   char *widget_name, Agraph_t * g)
206 {
207     float value;
208     char buf[25];
209     attribute=attribute +12;
210 
211     value = (float) gtk_spin_button_get_value((GtkSpinButton *)
212 					      glade_xml_get_widget(xml,
213 								   widget_name));
214     sprintf(buf, "%f", value);
215     //      agattr(
216     agattr(g, AGRAPH, attribute, buf);
217     return 1;
218 }
get_scalebtn_widget_to_attribute(char * attribute,char * widget_name,Agraph_t * g)219 static int get_scalebtn_widget_to_attribute(char *attribute,
220 					    char *widget_name,
221 					    Agraph_t * g)
222 {
223     float value;
224     char buf[25];
225     attribute=attribute +13;
226 
227     value = (float) gtk_range_get_value((GtkRange *)
228 					glade_xml_get_widget(xml,
229 							     widget_name));
230     sprintf(buf, "%f", value);
231     //      agattr(
232     agattr(g, AGRAPH, attribute, buf);
233     return 1;
234 }
235 
set_scalebtn_widget_to_attribute(char * attribute,char * widget_name)236 static int set_scalebtn_widget_to_attribute(char *attribute,
237 					    char *widget_name)
238 {
239     char *buf;
240     float value;
241     attribute=attribute +13;
242     buf = agget(view->g[view->activeGraph], attribute);
243 
244     if ((!buf) || (strcmp(buf, "") == 0))
245     {
246 	buf = agget(view->systemGraphs.def_attrs, attribute);
247 	copy_attr(view->systemGraphs.def_attrs, attribute,view->g[view->activeGraph]);
248 
249     }
250     if (buf) {
251 	value = (float) atof(buf);
252 	gtk_range_set_value((GtkRange *)
253 			    glade_xml_get_widget(xml, widget_name), value);
254 	return 1;
255     }
256     return 0;
257 }
258 
set_combobox_widget(char * attribute,char * widget_name)259 static int set_combobox_widget(char *attribute, char *widget_name)
260 {
261     char *buf;
262     int value;
263     attribute=attribute +9;
264     buf = agget(view->g[view->activeGraph], attribute);
265     if ((!buf) || (strcmp(buf, "") == 0))
266     {
267 	buf = agget(view->systemGraphs.def_attrs, attribute);
268 	copy_attr(view->systemGraphs.def_attrs, attribute,view->g[view->activeGraph]);
269     }
270     if (buf) {
271 	value = (int) atoi(buf);
272 	gtk_combo_box_set_active((GtkComboBox *)
273 				 glade_xml_get_widget(xml, widget_name),
274 				 (int) value);
275 
276 	return 1;
277     }
278 
279     return 0;
280 }
281 
get_combobox_widget_to_attribute(char * attribute,char * widget_name,Agraph_t * g)282 static int get_combobox_widget_to_attribute(char *attribute,
283 					    char *widget_name,
284 					    Agraph_t * g)
285 {
286     char buf[25];
287     int value;
288 
289     attribute=attribute +9;
290 
291     value = (int)
292 	gtk_combo_box_get_active((GtkComboBox *)
293 				 glade_xml_get_widget(xml, widget_name));
294 
295     sprintf(buf, "%d", value);
296     agattr(g, AGRAPH, attribute, buf);
297     /* printf ("%s %f \n",attribute,value); */
298     return 1;
299 
300 
301 }
load_settings_from_graph(Agraph_t * g)302 int load_settings_from_graph(Agraph_t * g)
303 {
304     Agsym_t* sym=NULL;
305     while ((sym = agnxtattr(view->systemGraphs.attrs_widgets,AGRAPH, sym))) {
306 	    if(strncmp (sym->name,"color_button",strlen("color_button"))==0)
307 		set_color_button_widget(sym->name, agget(view->systemGraphs.attrs_widgets,sym->name));
308 	    if(strncmp (sym->name,"check_box",strlen("check_box"))==0)
309 		set_checkbox_widget(sym->name, agget(view->systemGraphs.attrs_widgets,sym->name));
310     	    if(strncmp (sym->name,"text_box",strlen("text_box"))==0)
311 		set_text_widget(sym->name, agget(view->systemGraphs.attrs_widgets,sym->name));
312 	    if(strncmp (sym->name,"combobox",strlen("combobox"))==0)
313 		set_combobox_widget(sym->name, agget(view->systemGraphs.attrs_widgets,sym->name));
314 	    if(strncmp (sym->name,"spin_button",strlen("spin_button"))==0)
315 		set_spinbtn_widget(sym->name, agget(view->systemGraphs.attrs_widgets,sym->name));
316 	    if(strncmp (sym->name,"scale_button",strlen("scale_button"))==0)
317 		set_scalebtn_widget_to_attribute(sym->name, agget(view->systemGraphs.attrs_widgets,sym->name));
318 
319 	}
320     return 1;
321 }
322 
update_graph_from_settings(Agraph_t * g)323 int update_graph_from_settings(Agraph_t * g)
324 {
325     Agsym_t* sym=NULL;
326     while ((sym = agnxtattr(view->systemGraphs.attrs_widgets,AGRAPH, sym)))
327     {
328 	if(strncmp (sym->name,"color_button",strlen("color_button"))==0)
329 	    get_color_button_widget_to_attribute(sym->name, agget(view->systemGraphs.attrs_widgets,sym->name),g);
330 	if(strncmp (sym->name,"check_box",strlen("check_box"))==0)
331 	   get_checkbox_widget_to_attribute(sym->name, agget(view->systemGraphs.attrs_widgets,sym->name),g);
332 	if(strncmp (sym->name,"text_box",strlen("text_box"))==0)
333 	   get_text_widget_to_attribute(sym->name, agget(view->systemGraphs.attrs_widgets,sym->name),g);
334 	if(strncmp (sym->name,"combobox",strlen("combobox"))==0)
335 	   get_combobox_widget_to_attribute(sym->name, agget(view->systemGraphs.attrs_widgets,sym->name),g);
336 	if(strncmp (sym->name,"spin_button",strlen("spin_button"))==0)
337 	   get_spinbtn_widget_to_attribute(sym->name, agget(view->systemGraphs.attrs_widgets,sym->name),g);
338 	if(strncmp (sym->name,"scale_button",strlen("scale_button"))==0)
339 	   get_scalebtn_widget_to_attribute(sym->name, agget(view->systemGraphs.attrs_widgets,sym->name),g);
340     }
341 
342     return 1;
343 }
344 
show_settings_form()345 int show_settings_form()
346 {
347 
348     if (view->activeGraph >= 0) {
349 	load_settings_from_graph(view->g[view->activeGraph]);
350 	gtk_widget_hide(glade_xml_get_widget(xml, "dlgSettings"));
351 	gtk_widget_show(glade_xml_get_widget(xml, "dlgSettings"));
352 	gtk_window_set_keep_above((GtkWindow *)
353 				  glade_xml_get_widget(xml, "dlgSettings"),
354 				  1);
355     } else {
356 	GtkMessageDialog *dlg;
357 	dlg = (GtkMessageDialog *) gtk_message_dialog_new(NULL,
358 							  GTK_DIALOG_MODAL,
359 							  GTK_MESSAGE_QUESTION,
360 							  GTK_BUTTONS_OK,
361 							  "No active graph");
362 	gtk_dialog_run((GtkDialog *) dlg);
363 	gtk_widget_hide((GtkWidget *) dlg);
364     }
365     return 1;
366 }
367