PageRenderTime 27ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/graphviz-cmake/cmd/smyrna/gui/gui.c

https://bitbucket.org/akristmann/custom_build
C | 492 lines | 378 code | 77 blank | 37 comment | 39 complexity | afdcd815799299d47e4d31d0e9a194ed MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, EPL-1.0, CPL-1.0, BSD-3-Clause, LGPL-2.1
  1. /* $Id: gui.c,v 1.32 2011/01/25 16:30:47 ellson Exp $ $Revision: 1.32 $ */
  2. /* vim:set shiftwidth=4 ts=8: */
  3. /*************************************************************************
  4. * Copyright (c) 2011 AT&T Intellectual Property
  5. * All rights reserved. This program and the accompanying materials
  6. * are made available under the terms of the Eclipse Public License v1.0
  7. * which accompanies this distribution, and is available at
  8. * http://www.eclipse.org/legal/epl-v10.html
  9. *
  10. * Contributors: See CVS logs. Details at http://www.graphviz.org/
  11. *************************************************************************/
  12. #include <stdio.h>
  13. #include "compat.h"
  14. #include <stdlib.h>
  15. #include "gui.h"
  16. /* #include "abstring.h" */
  17. #include <glade/glade.h>
  18. #include <gdk/gdkkeysyms.h>
  19. #include <gdk/gdk.h>
  20. #include "viewport.h"
  21. #include "memory.h"
  22. static char guibuffer[BUFSIZ]; //general purpose buffer
  23. #ifdef WIN32
  24. extern int strcasecmp(const char *s1, const char *s2);
  25. extern int strncasecmp(const char *s1, const char *s2, unsigned int n);
  26. #endif
  27. GdkWindow *window1;
  28. GtkWidget *statusbar1;
  29. GladeXML *xml; //global libglade vars
  30. GtkWidget *gladewidget;
  31. //1 subgraph 2 node 3 edge
  32. int frmObjectTypeIndex;
  33. Agraph_t *frmObjectg;
  34. GtkComboBox *cbSelectGraph; //combo at top left
  35. GtkWidget *AttrWidgets[MAXIMUM_WIDGET_COUNT];
  36. GtkWidget *AttrLabels[MAXIMUM_WIDGET_COUNT];
  37. int attr_widgets_modified[MAXIMUM_WIDGET_COUNT];
  38. int widgetcounter; //number of attributes counted dynamically, might be removed in the future
  39. attribute attr[MAXIMUM_WIDGET_COUNT];
  40. //loads object properties form and does some fixes
  41. //call this function only ones
  42. void create_object_properties(void)
  43. {
  44. #ifdef UNUSED
  45. char *data0 = "TEST0";
  46. char *data1 = "TEST1";
  47. char *data2 = "TEST2";
  48. char *data3 = "TEST3";
  49. char *data4 = "TEST4";
  50. #endif
  51. GladeXML *xml;
  52. GtkWidget *widget;
  53. xml = glade_xml_new(smyrnaGlade, NULL, NULL);
  54. widget = glade_xml_get_widget(xml, "listPoints");
  55. gtk_clist_set_column_title((GtkCList *) widget, 0, "Def");
  56. gtk_clist_set_column_title((GtkCList *) widget, 1, "x");
  57. gtk_clist_set_column_title((GtkCList *) widget, 2, "y");
  58. gtk_clist_set_column_title((GtkCList *) widget, 3, "z");
  59. gtk_clist_column_titles_show((GtkCList *) widget);
  60. gtk_widget_show(widget);
  61. widget = glade_xml_get_widget(xml, "win");
  62. gtk_widget_show(widget);
  63. }
  64. //call this after create_object_properties()
  65. void object_properties_node_init(void)
  66. {
  67. }
  68. void object_properties_edge_init(void) //customize window for Edges
  69. {
  70. }
  71. void object_properties_cluster_init(void) //customize window for Cluster
  72. {
  73. }
  74. void object_properties_graph_init(void) //customize window for Graph , this shows the graph default values
  75. {
  76. }
  77. GtkComboBox *get_SelectGraph(void)
  78. {
  79. GtkComboBox *cb;
  80. if (!cbSelectGraph) {
  81. cb = (GtkComboBox *) gtk_combo_box_new_text();
  82. gtk_widget_show((GtkWidget *) cb);
  83. gtk_box_pack_start(GTK_BOX(gladewidget), (GtkWidget *) cb, FALSE,
  84. FALSE, 0);
  85. gtk_layout_put((GtkLayout *) gladewidget, (GtkWidget *) cb, 780,
  86. 3);
  87. //signal
  88. return cb;
  89. } else
  90. return cbSelectGraph;
  91. }
  92. void Color_Widget(char *colorstring, GtkWidget * widget)
  93. {
  94. GtkRcStyle *rc_style;
  95. GdkColor color;
  96. gdk_color_parse (colorstring, &color);
  97. rc_style = gtk_rc_style_new ();
  98. /* Set foreground (fg) color in normal state to red */
  99. rc_style->fg[GTK_STATE_NORMAL] = color;
  100. rc_style->color_flags[GTK_STATE_NORMAL] |= GTK_RC_FG;
  101. gtk_widget_modify_style (widget, rc_style);
  102. gtk_rc_style_unref (rc_style);
  103. }
  104. void Color_Widget_bg(char *colorstring, GtkWidget * widget)
  105. {
  106. GdkColor color;
  107. gdk_color_parse(colorstring, &color);
  108. gtk_widget_modify_bg(widget, GTK_STATE_NORMAL, &color);
  109. gtk_widget_modify_base(widget, GTK_STATE_NORMAL, &color);
  110. }
  111. #ifdef UNUSED
  112. void Color_Widget_fg(int r, int g, int b, GtkWidget * widget)
  113. {
  114. GdkColor color;
  115. gdk_color_parse("red", &color);
  116. gtk_widget_modify_fg(widget, GTK_STATE_NORMAL, &color);
  117. }
  118. #endif
  119. void load_graph_properties(Agraph_t * graph)
  120. {
  121. //dlgOpenGraph , GtkDialog
  122. gtk_entry_set_text((GtkEntry *)
  123. glade_xml_get_widget(xml, "entryGraphFileName"),
  124. view->Topview->Graphdata.GraphFileName);
  125. }
  126. int update_graph_properties(Agraph_t * graph) //updates graph from gui
  127. {
  128. FILE *file;
  129. int respond = 0;
  130. //check if file is changed
  131. if (strcasecmp
  132. (gtk_entry_get_text
  133. ((GtkEntry *) glade_xml_get_widget(xml, "entryGraphFileName")),
  134. view->Topview->Graphdata.GraphFileName) != 0) {
  135. if ((file = fopen(gtk_entry_get_text((GtkEntry *)
  136. glade_xml_get_widget(xml,
  137. "entryGraphFileName")),
  138. "r"))) {
  139. fclose(file);
  140. Dlg = (GtkMessageDialog *) gtk_message_dialog_new(NULL,
  141. GTK_DIALOG_MODAL,
  142. GTK_MESSAGE_QUESTION,
  143. GTK_BUTTONS_YES_NO,
  144. "File name you have entered already exists\n,this will cause overwriting on existing file.\nAre you sure?");
  145. respond = gtk_dialog_run((GtkDialog *) Dlg);
  146. gtk_object_destroy((GtkObject *) Dlg);
  147. if (respond == GTK_RESPONSE_NO)
  148. return 0;
  149. }
  150. //now check if filename is legal, try to open it to write
  151. if ((file = fopen(gtk_entry_get_text((GtkEntry *)
  152. glade_xml_get_widget(xml,
  153. "entryGraphFileName")),
  154. "w")))
  155. fclose(file);
  156. else {
  157. Dlg = (GtkMessageDialog *) gtk_message_dialog_new(NULL,
  158. GTK_DIALOG_MODAL,
  159. GTK_MESSAGE_WARNING,
  160. GTK_BUTTONS_OK,
  161. "File name is invalid or I/O error!");
  162. respond = gtk_dialog_run((GtkDialog *) Dlg);
  163. gtk_object_destroy((GtkObject *) Dlg);
  164. GTK_DIALOG(Dlg);
  165. return 0;
  166. }
  167. }
  168. //if it comes so far graph deserves new values
  169. view->Topview->Graphdata.GraphFileName =
  170. (char *) gtk_entry_get_text((GtkEntry *)
  171. glade_xml_get_widget(xml,
  172. "entryGraphFileName"));
  173. return 1;
  174. }
  175. char *get_attribute_string_value_from_widget(attribute * att)
  176. {
  177. GdkColor color;
  178. switch (att->Type) {
  179. case 'F':
  180. sprintf(guibuffer, "%f",
  181. gtk_spin_button_get_value((GtkSpinButton *) att->
  182. attrWidget));
  183. return guibuffer;
  184. break;
  185. case 'C':
  186. gtk_color_button_get_color((GtkColorButton *) att->attrWidget,
  187. &color);
  188. sprintf(guibuffer, "#%x%x%x", color.red / 255, color.green / 255,
  189. color.blue / 255);
  190. return guibuffer;
  191. break;
  192. default:
  193. strcpy(guibuffer,
  194. gtk_entry_get_text((GtkEntry *) att->attrWidget));
  195. return guibuffer;
  196. }
  197. }
  198. #if UNUSED
  199. void change_selected_graph_attributes(Agraph_t * g, char *attrname,
  200. char *attrvalue)
  201. {
  202. agattr(g, AGRAPH, attrname, "");
  203. agset(view->g[view->activeGraph], attrname, attrvalue);
  204. }
  205. void change_selected_node_attributes(Agraph_t * g, char *attrname,
  206. char *attrvalue)
  207. {
  208. int ind = 0;
  209. Agsym_t* ap = agattr(g, AGNODE, attrname, NULL);
  210. if (!ap)
  211. ap = agattr(g, AGNODE, attrname, "");
  212. for (ind = 0; ind < view->Topview->Nodecount; ind++) {
  213. if (view->Topview->Nodes[ind].data.Selected == 1)
  214. agxset(view->Topview->Nodes[ind].Node, ap, attrvalue);
  215. }
  216. }
  217. void change_selected_edge_attributes(Agraph_t * g, char *attrname,
  218. char *attrvalue)
  219. {
  220. int ind = 0;
  221. Agsym_t* ap = agattr(g, AGEDGE, attrname, NULL);
  222. if (!ap)
  223. ap = agattr(g, AGEDGE, attrname, "");
  224. for (ind = 0; ind < view->Topview->Edgecount; ind++) {
  225. if (view->Topview->Edges[ind].data.Selected == 1)
  226. agxset(view->Topview->Edges[ind].Edge, ap, attrvalue);
  227. }
  228. }
  229. #endif
  230. void load_attributes(void)
  231. {
  232. FILE *file;
  233. char line[BUFSIZ];
  234. char *ss;
  235. char *pch;
  236. int ind = 0;
  237. int attrcount = 0;
  238. static char *smyrna_attrs;
  239. if (!smyrna_attrs) {
  240. smyrna_attrs = smyrnaPath("attrs.txt");
  241. }
  242. //loads attributes from a text file
  243. file = fopen(smyrna_attrs, "r");
  244. if (file != NULL) {
  245. while (fgets(line, sizeof line, file) != NULL) {
  246. pch = strtok(line, ",");
  247. ind = 0;
  248. while (pch != NULL) {
  249. ss = strdup(pch);
  250. // ABRemove(&ss,'\"');
  251. // ABRemove(&ss,' ');
  252. pch = strtok(NULL, ",");
  253. switch (ind) {
  254. case 0:
  255. attr[attrcount].Type = ss[0];
  256. break;
  257. case 1:
  258. attr[attrcount].Name = strdup(ss);
  259. break;
  260. case 2:
  261. attr[attrcount].Default = strdup(ss);
  262. break;
  263. case 3:
  264. if (strstr(ss, "ANY_ELEMENT")) {
  265. attr[attrcount].ApplyTo[GVE_GRAPH] = 1;
  266. attr[attrcount].ApplyTo[GVE_CLUSTER] = 1;
  267. attr[attrcount].ApplyTo[GVE_NODE] = 1;
  268. attr[attrcount].ApplyTo[GVE_EDGE] = 1;
  269. } else {
  270. attr[attrcount].ApplyTo[GVE_GRAPH] =
  271. strstr(ss, "GRAPH") ? 1 : 0;
  272. attr[attrcount].ApplyTo[GVE_CLUSTER] =
  273. strstr(ss, "CLUSTER") ? 1 : 0;
  274. attr[attrcount].ApplyTo[GVE_NODE] =
  275. strstr(ss, "NODE") ? 1 : 0;
  276. attr[attrcount].ApplyTo[GVE_EDGE] =
  277. strstr(ss, "EDGE") ? 1 : 0;
  278. }
  279. break;
  280. case 4:
  281. if (strstr(ss, "ALL_ENGINES")) {
  282. attr[attrcount].Engine[GVK_DOT] = 1;
  283. attr[attrcount].Engine[GVK_NEATO] = 1;
  284. attr[attrcount].Engine[GVK_TWOPI] = 1;
  285. attr[attrcount].Engine[GVK_CIRCO] = 1;
  286. attr[attrcount].Engine[GVK_FDP] = 1;
  287. } else {
  288. attr[attrcount].Engine[GVK_DOT] =
  289. strstr(ss, "DOT") ? 1 : 0;
  290. attr[attrcount].Engine[GVK_NEATO] =
  291. strstr(ss, "NEATO") ? 1 : 0;
  292. attr[attrcount].Engine[GVK_TWOPI] =
  293. strstr(ss, "TWOPI") ? 1 : 0;
  294. attr[attrcount].Engine[GVK_CIRCO] =
  295. strstr(ss, "CIRCO") ? 1 : 0;
  296. attr[attrcount].Engine[GVK_FDP] =
  297. strstr(ss, "FDP") ? 1 : 0;
  298. }
  299. break;
  300. default:
  301. attr[attrcount].ComboValues =
  302. RALLOC(attr[attrcount].ComboValuesCount,
  303. attr[attrcount].ComboValues, char *);
  304. attr[attrcount].ComboValues[attr[attrcount].
  305. ComboValuesCount] =
  306. strdup(ss);
  307. attr[attrcount].ComboValuesCount++;
  308. break;
  309. }
  310. ind++;
  311. }
  312. attrcount++;
  313. }
  314. fclose (file);
  315. }
  316. }
  317. void show_gui_warning(char *str)
  318. {
  319. Dlg = (GtkMessageDialog *) gtk_message_dialog_new(NULL,
  320. GTK_DIALOG_MODAL,
  321. GTK_MESSAGE_WARNING,
  322. GTK_BUTTONS_OK, str);
  323. respond = gtk_dialog_run((GtkDialog *) Dlg);
  324. gtk_object_destroy((GtkObject *) Dlg);
  325. }
  326. /*
  327. Generic Open File dialog, if a file is selected and return value is 1, else 0
  328. file name is copied to char* filename,which should be allocated before using the function
  329. */
  330. int openfiledlg(int filtercnt, char **filters, agxbuf * xbuf)
  331. {
  332. GtkWidget *dialog;
  333. GtkFileFilter *filter;
  334. int id, rv;
  335. filter = gtk_file_filter_new();
  336. if (filtercnt >= 1) {
  337. for (id = 0; id < filtercnt; id++) {
  338. gtk_file_filter_add_pattern(filter, filters[id]);
  339. }
  340. }
  341. dialog = gtk_file_chooser_dialog_new("Open File",
  342. NULL,
  343. GTK_FILE_CHOOSER_ACTION_OPEN,
  344. GTK_STOCK_CANCEL,
  345. GTK_RESPONSE_CANCEL,
  346. GTK_STOCK_OPEN,
  347. GTK_RESPONSE_ACCEPT, NULL);
  348. if (filtercnt >= 1)
  349. gtk_file_chooser_set_filter((GtkFileChooser *) dialog, filter);
  350. if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
  351. agxbput(xbuf,
  352. gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog)));
  353. rv = 1;
  354. } else
  355. rv = 0;
  356. gtk_widget_destroy(dialog);
  357. return rv;
  358. }
  359. int savefiledlg(int filtercnt, char **filters, agxbuf * xbuf)
  360. {
  361. GtkWidget *dialog;
  362. GtkFileFilter *filter;
  363. int id, rv;
  364. filter = gtk_file_filter_new();
  365. if (filtercnt >= 1) {
  366. for (id = 0; id < filtercnt; id++) {
  367. gtk_file_filter_add_pattern(filter, filters[id]);
  368. }
  369. }
  370. dialog = gtk_file_chooser_dialog_new("Save File",
  371. NULL,
  372. GTK_FILE_CHOOSER_ACTION_OPEN,
  373. GTK_STOCK_CANCEL,
  374. GTK_RESPONSE_CANCEL,
  375. GTK_STOCK_OPEN,
  376. GTK_RESPONSE_ACCEPT, NULL);
  377. if (filtercnt >= 1)
  378. gtk_file_chooser_set_filter((GtkFileChooser *) dialog, filter);
  379. if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
  380. agxbput(xbuf,
  381. gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog)));
  382. rv = 1;
  383. } else
  384. rv = 0;
  385. gtk_widget_destroy(dialog);
  386. return rv;
  387. }
  388. /*
  389. this function is designed to return a GtkTextView object's text in agxbuf
  390. send an initialized agxbuf and a GtkTextView object
  391. null termination is taken care by agxbuf
  392. */
  393. void get_gtktextview_text(GtkTextView * w, agxbuf * xbuf)
  394. {
  395. int charcnt;
  396. GtkTextBuffer *gtkbuf;
  397. GtkTextIter startit;
  398. GtkTextIter endit;
  399. gtkbuf = gtk_text_view_get_buffer(w);
  400. charcnt = gtk_text_buffer_get_char_count(gtkbuf);
  401. gtk_text_buffer_get_start_iter(gtkbuf, &startit);
  402. gtk_text_buffer_get_end_iter(gtkbuf, &endit);
  403. agxbput(xbuf, gtk_text_buffer_get_text(gtkbuf, &startit, &endit, 0));
  404. }
  405. void append_textview(GtkTextView * textv, const char *s, size_t bytes)
  406. {
  407. GtkTextIter endit;
  408. GtkTextBuffer *gtkbuf;
  409. /*get text view buffer */
  410. gtkbuf = gtk_text_view_get_buffer(textv);
  411. /*set iterator to the end of the buffer */
  412. gtk_text_buffer_get_end_iter(gtkbuf, &endit);
  413. /* insert buf to the end */
  414. gtk_text_buffer_insert(gtkbuf, &endit, s, (gint)bytes);
  415. }