topviewsettings.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*************************************************************************
  2. * Copyright (c) 2011 AT&T Intellectual Property
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution, and is available at
  6. * https://www.eclipse.org/legal/epl-v10.html
  7. *
  8. * Contributors: Details at https://graphviz.org
  9. *************************************************************************/
  10. #include "topviewfuncs.h"
  11. #include "topviewsettings.h"
  12. #include "gui.h"
  13. #include <common/colorprocs.h>
  14. #include <stdint.h>
  15. #include <util/startswith.h>
  16. #include "viewport.h"
  17. void size_change_request(GtkWidget *widget, void *user_data) {
  18. (void)widget;
  19. (void)user_data;
  20. }
  21. void on_settingsOKBtn_clicked(GtkWidget *widget, void *user_data) {
  22. on_settingsApplyBtn_clicked(widget, user_data);
  23. gtk_widget_hide(glade_xml_get_widget(xml, "dlgSettings"));
  24. }
  25. void on_settingsApplyBtn_clicked(GtkWidget *widget, void *user_data) {
  26. (void)widget;
  27. (void)user_data;
  28. update_graph_from_settings(view->g[view->activeGraph]);
  29. set_viewport_settings_from_template(view, view->g[view->activeGraph]);
  30. updateSmGraph(view->g[view->activeGraph],view->Topview);
  31. }
  32. void on_dlgSettings_close(GtkWidget *widget, void *user_data) {
  33. (void)widget;
  34. (void)user_data;
  35. gtk_widget_hide(glade_xml_get_widget(xml, "dlgSettings"));
  36. }
  37. void on_settingsCancelBtn_clicked(GtkWidget *widget, void *user_data) {
  38. (void)widget;
  39. (void)user_data;
  40. gtk_widget_hide(glade_xml_get_widget(xml, "dlgSettings"));
  41. }
  42. static void copy_attr(Agraph_t *destG, char *attrib, Agraph_t *g)
  43. {
  44. agattr(g, AGRAPH, attrib, agget(destG, attrib));
  45. }
  46. static void set_color_button_widget(char *attrib, char *widget_name) {
  47. GdkColor color;
  48. gvcolor_t cl;
  49. const char *buf = agget(view->g[view->activeGraph], attrib);
  50. if ((!buf) || (strcmp(buf, "") == 0))
  51. {
  52. buf = agget(view->systemGraphs.def_attrs, attrib);
  53. copy_attr(view->systemGraphs.def_attrs, attrib, view->g[view->activeGraph]);
  54. }
  55. if (buf) {
  56. colorxlate(buf, &cl, RGBA_DOUBLE);
  57. color.red = (uint16_t)(cl.u.RGBA[0] * 65535.0);
  58. color.green = (uint16_t)(cl.u.RGBA[1] * 65535.0);
  59. color.blue = (uint16_t)(cl.u.RGBA[2] * 65535.0);
  60. gtk_color_button_set_color((GtkColorButton *)
  61. glade_xml_get_widget(xml, widget_name),
  62. &color);
  63. }
  64. }
  65. static void get_color_button_widget_to_attribute(char *attrib,
  66. char *widget_name,
  67. Agraph_t * g)
  68. {
  69. GdkColor color;
  70. char buf[256];
  71. gtk_color_button_get_color((GtkColorButton *)
  72. glade_xml_get_widget(xml, widget_name),
  73. &color);
  74. snprintf(buf, sizeof(buf), "#%02x%02x%02x",
  75. (int) ((float) color.red / 65535.0 * 255.0),
  76. (int) ((float) color.green / 65535.0 * 255.0),
  77. (int) ((float) color.blue / 65535.0 * 255.0));
  78. agattr(g, AGRAPH, attrib, buf);
  79. }
  80. static void get_text_widget_to_attribute(char *attrib, char *widget_name,
  81. Agraph_t * g)
  82. {
  83. if (strlen(attrib) > 512)
  84. return;
  85. agattr(g, AGRAPH, attrib, gtk_entry_get_text((GtkEntry*)
  86. glade_xml_get_widget(xml, widget_name)));
  87. }
  88. static void set_text_widget(char *attrib, char *widget_name) {
  89. char *buf;
  90. buf = agget(view->g[view->activeGraph], attrib);
  91. if ((!buf) || (strcmp(buf, "") == 0))
  92. {
  93. buf = agget(view->systemGraphs.def_attrs, attrib);
  94. copy_attr(view->systemGraphs.def_attrs, attrib, view->g[view->activeGraph]);
  95. }
  96. if (buf) {
  97. gtk_entry_set_text((GtkEntry *)
  98. glade_xml_get_widget(xml, widget_name), buf);
  99. }
  100. }
  101. static void set_checkbox_widget(char *attrib, char *widget_name) {
  102. char *buf;
  103. int value;
  104. buf = agget(view->g[view->activeGraph], attrib);
  105. if ((!buf) || (strcmp(buf, "") == 0))
  106. {
  107. buf = agget(view->systemGraphs.def_attrs, attrib);
  108. copy_attr(view->systemGraphs.def_attrs, attrib, view->g[view->activeGraph]);
  109. }
  110. if (buf) {
  111. value = atoi(buf);
  112. gtk_toggle_button_set_active((GtkToggleButton *)
  113. glade_xml_get_widget(xml,
  114. widget_name),
  115. value);
  116. }
  117. }
  118. static void get_checkbox_widget_to_attribute(char *attrib, char *widget_name,
  119. Agraph_t * g)
  120. {
  121. int value;
  122. char buf[100];
  123. value = (int) gtk_toggle_button_get_active((GtkToggleButton *)
  124. glade_xml_get_widget(xml,
  125. widget_name));
  126. snprintf(buf, sizeof(buf), "%d", value);
  127. agattr(g, AGRAPH, attrib, buf);
  128. }
  129. static void set_spinbtn_widget(char *attrib, char *widget_name) {
  130. char *buf;
  131. float value;
  132. buf = agget(view->g[view->activeGraph], attrib);
  133. if ((!buf) || (strcmp(buf, "") == 0))
  134. {
  135. buf = agget(view->systemGraphs.def_attrs, attrib);
  136. copy_attr(view->systemGraphs.def_attrs, attrib, view->g[view->activeGraph]);
  137. }
  138. if (buf) {
  139. value = (float) atof(buf);
  140. gtk_spin_button_set_value((GtkSpinButton *)
  141. glade_xml_get_widget(xml, widget_name),
  142. value);
  143. }
  144. }
  145. static void get_spinbtn_widget_to_attribute(char *attrib,
  146. char *widget_name, Agraph_t * g)
  147. {
  148. float value;
  149. char buf[25];
  150. value = (float) gtk_spin_button_get_value((GtkSpinButton *)
  151. glade_xml_get_widget(xml,
  152. widget_name));
  153. snprintf(buf, sizeof(buf), "%f", value);
  154. agattr(g, AGRAPH, attrib, buf);
  155. }
  156. static void get_scalebtn_widget_to_attribute(char *attrib, char *widget_name,
  157. Agraph_t * g)
  158. {
  159. float value;
  160. char buf[25];
  161. value = (float) gtk_range_get_value((GtkRange *)
  162. glade_xml_get_widget(xml,
  163. widget_name));
  164. snprintf(buf, sizeof(buf), "%f", value);
  165. agattr(g, AGRAPH, attrib, buf);
  166. }
  167. static void set_scalebtn_widget_to_attribute(char *attrib, char *widget_name) {
  168. char *buf;
  169. float value;
  170. buf = agget(view->g[view->activeGraph], attrib);
  171. if ((!buf) || (strcmp(buf, "") == 0))
  172. {
  173. buf = agget(view->systemGraphs.def_attrs, attrib);
  174. copy_attr(view->systemGraphs.def_attrs, attrib, view->g[view->activeGraph]);
  175. }
  176. if (buf) {
  177. value = (float) atof(buf);
  178. gtk_range_set_value((GtkRange *)
  179. glade_xml_get_widget(xml, widget_name), value);
  180. }
  181. }
  182. static void set_combobox_widget(char *attrib, char *widget_name) {
  183. char *buf;
  184. int value;
  185. buf = agget(view->g[view->activeGraph], attrib);
  186. if ((!buf) || (strcmp(buf, "") == 0))
  187. {
  188. buf = agget(view->systemGraphs.def_attrs, attrib);
  189. copy_attr(view->systemGraphs.def_attrs, attrib, view->g[view->activeGraph]);
  190. }
  191. if (buf) {
  192. value = atoi(buf);
  193. gtk_combo_box_set_active((GtkComboBox *)
  194. glade_xml_get_widget(xml, widget_name), value);
  195. }
  196. }
  197. static void get_combobox_widget_to_attribute(char *attrib, char *widget_name,
  198. Agraph_t * g)
  199. {
  200. char buf[25];
  201. int value;
  202. value = (int)
  203. gtk_combo_box_get_active((GtkComboBox *)
  204. glade_xml_get_widget(xml, widget_name));
  205. snprintf(buf, sizeof(buf), "%d", value);
  206. agattr(g, AGRAPH, attrib, buf);
  207. }
  208. void load_settings_from_graph(void) {
  209. Agsym_t *sym = NULL;
  210. while ((sym = agnxtattr(view->systemGraphs.attrs_widgets, AGRAPH, sym))) {
  211. if (startswith(sym->name, "color_button"))
  212. set_color_button_widget(
  213. sym->name + strlen("color_button="),
  214. agget(view->systemGraphs.attrs_widgets, sym->name));
  215. if (startswith(sym->name, "check_box"))
  216. set_checkbox_widget(sym->name + strlen("check_box="),
  217. agget(view->systemGraphs.attrs_widgets, sym->name));
  218. if (startswith(sym->name, "text_box"))
  219. set_text_widget(sym->name + strlen("text_box="),
  220. agget(view->systemGraphs.attrs_widgets, sym->name));
  221. if (startswith(sym->name, "combobox"))
  222. set_combobox_widget(sym->name + strlen("combobox="),
  223. agget(view->systemGraphs.attrs_widgets, sym->name));
  224. if (startswith(sym->name, "spin_button"))
  225. set_spinbtn_widget(sym->name + strlen("spin_button="),
  226. agget(view->systemGraphs.attrs_widgets, sym->name));
  227. if (startswith(sym->name, "scale_button"))
  228. set_scalebtn_widget_to_attribute(
  229. sym->name + strlen("scale_button="),
  230. agget(view->systemGraphs.attrs_widgets, sym->name));
  231. }
  232. }
  233. void update_graph_from_settings(Agraph_t *g) {
  234. Agsym_t *sym = NULL;
  235. while ((sym = agnxtattr(view->systemGraphs.attrs_widgets, AGRAPH, sym))) {
  236. if (startswith(sym->name, "color_button"))
  237. get_color_button_widget_to_attribute(
  238. sym->name + strlen("color_button="),
  239. agget(view->systemGraphs.attrs_widgets, sym->name), g);
  240. if (startswith(sym->name, "check_box"))
  241. get_checkbox_widget_to_attribute(
  242. sym->name + strlen("check_box="),
  243. agget(view->systemGraphs.attrs_widgets, sym->name), g);
  244. if (startswith(sym->name, "text_box"))
  245. get_text_widget_to_attribute(
  246. sym->name + strlen("text_box="),
  247. agget(view->systemGraphs.attrs_widgets, sym->name), g);
  248. if (startswith(sym->name, "combobox"))
  249. get_combobox_widget_to_attribute(
  250. sym->name + strlen("combobox="),
  251. agget(view->systemGraphs.attrs_widgets, sym->name), g);
  252. if (startswith(sym->name, "spin_button"))
  253. get_spinbtn_widget_to_attribute(
  254. sym->name + strlen("spin_button="),
  255. agget(view->systemGraphs.attrs_widgets, sym->name), g);
  256. if (startswith(sym->name, "scale_button"))
  257. get_scalebtn_widget_to_attribute(
  258. sym->name + strlen("scale_button="),
  259. agget(view->systemGraphs.attrs_widgets, sym->name), g);
  260. }
  261. }
  262. void show_settings_form(void) {
  263. if (view->activeGraph >= 0) {
  264. load_settings_from_graph();
  265. gtk_widget_hide(glade_xml_get_widget(xml, "dlgSettings"));
  266. gtk_widget_show(glade_xml_get_widget(xml, "dlgSettings"));
  267. gtk_window_set_keep_above((GtkWindow *)
  268. glade_xml_get_widget(xml, "dlgSettings"),
  269. 1);
  270. } else {
  271. void *dlg = gtk_message_dialog_new(NULL,
  272. GTK_DIALOG_MODAL,
  273. GTK_MESSAGE_QUESTION,
  274. GTK_BUTTONS_OK,
  275. "No active graph");
  276. gtk_dialog_run(dlg);
  277. gtk_widget_hide(dlg);
  278. }
  279. }