~ubuntu-branches/ubuntu/lucid/graphviz/lucid-updates

« back to all changes in this revision

Viewing changes to cmd/smyrna/viewport.c

  • Committer: Bazaar Package Importer
  • Author(s): Bryce Harrington
  • Date: 2008-06-19 20:23:23 UTC
  • mfrom: (1.2.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20080619202323-ls23h96ntj9ny94m
Tags: 2.18-1ubuntu1
* Merge from debian unstable, remaining changes:
  - Build depend on liblualib50-dev instead of liblua5.1-0-dev.
  - Drop libttf-dev (libttf-dev is in universe) (LP: #174749).
  - Replace gs-common with ghostscript.
  - Build-depend on python-dev instead of python2.4-dev or python2.5-dev.
  - Mention the correct python version for the python bindings in the
    package description.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Id: viewport.c,v 1.4 2008/03/04 17:54:51 arif Exp $ $Revision: 1.4 $ */
 
2
/* vim:set shiftwidth=4 ts=8: */
 
3
 
 
4
/**********************************************************
 
5
*      This software is part of the graphviz package      *
 
6
*                http://www.graphviz.org/                 *
 
7
*                                                         *
 
8
*            Copyright (c) 1994-2004 AT&T Corp.           *
 
9
*                and is licensed under the                *
 
10
*            Common Public License, Version 1.0           *
 
11
*                      by AT&T Corp.                      *
 
12
*                                                         *
 
13
*        Information and Software Systems Research        *
 
14
*              AT&T Research, Florham Park NJ             *
 
15
**********************************************************/
 
16
 
 
17
#ifdef _WIN32
 
18
#include <windows.h>
 
19
#endif
 
20
#include "compat.h"
 
21
#include "viewport.h"
 
22
#include "draw.h"
 
23
#include "color.h"
 
24
#include <glade/glade.h>
 
25
#include "gui.h"
 
26
#include "string.h"
 
27
#include "topview.h"
 
28
#include "gltemplate.h"
 
29
#include "colorprocs.h"
 
30
 
 
31
 
 
32
#define countof( array ) ( sizeof( array )/sizeof( array[0] ) )
 
33
 
 
34
ViewInfo*       view;
 
35
/*these two global variables should be wrapped in somethign else*/
 
36
GtkMessageDialog*  Dlg;
 
37
int respond;
 
38
 
 
39
void clear_viewport(ViewInfo* view)
 
40
{
 
41
        int ind=0;
 
42
        /*free topview if there is one*/
 
43
        if(view->graphCount)
 
44
        {
 
45
                cleartopview(view->Topview);
 
46
                /*all cgraph graphs should be freed*/
 
47
                for (ind=0;ind < view->graphCount;ind ++)
 
48
                {
 
49
                        agclose(view->g[ind]);
 
50
                }
 
51
                /*frees itself*/
 
52
                free(view);
 
53
        }
 
54
}
 
55
void init_viewport(ViewInfo* view)
 
56
{
 
57
 
 
58
        //init graphs
 
59
        view->g=NULL;           //no graph, gl screen should check it
 
60
        view->graphCount=0;     //and disable interactivity if count is zero
 
61
 
 
62
 
 
63
        view->bdxLeft=0;
 
64
        view->bdxRight=500;
 
65
        view->bdyBottom=0;
 
66
        view->bdyTop=500;
 
67
        view->bdzBottom=0;
 
68
        view->bdzTop=0;
 
69
 
 
70
        view->borderColor.R=1;
 
71
        view->borderColor.G=0;
 
72
        view->borderColor.B=0;
 
73
        view->borderColor.A=1;
 
74
 
 
75
        view->bdVisible=1;              //show borders red
 
76
        
 
77
        view->gridSize=10;
 
78
        view->gridColor.R=0.5;
 
79
        view->gridColor.G=0.5;
 
80
        view->gridColor.B=0.5;
 
81
        view->gridColor.A=1;
 
82
        view->gridVisible=0;    //show grids in light gray
 
83
 
 
84
        //mouse mode=pan
 
85
        view->mouse.mouse_mode=0;
 
86
        //pen color
 
87
        view->penColor.R=0;
 
88
        view->penColor.G=0;
 
89
        view->penColor.B=0;
 
90
        view->penColor.A=1;
 
91
 
 
92
        view->fillColor.R=1;
 
93
        view->fillColor.G=0;
 
94
        view->fillColor.B=0;
 
95
        view->fillColor.A=1;
 
96
        //background color , default white
 
97
        view->bgColor.R=1;
 
98
        view->bgColor.G=1;
 
99
        view->bgColor.B=1;
 
100
        view->bgColor.A=1;
 
101
 
 
102
        //selected objets are drawn with this color
 
103
        view->selectColor.R=1;
 
104
        view->selectColor.G=0;
 
105
        view->selectColor.B=0;
 
106
        view->selectColor.A=1;
 
107
 
 
108
        //default line width;
 
109
        view->LineWidth=1;
 
110
 
 
111
        //default view settings , camera is not active
 
112
        view->GLDepth=1;        //should be set before GetFixedOGLPos(int x, int y,float kts) funtion is used!!!!
 
113
        view->panx=0;
 
114
        view->pany=0;
 
115
        view->panz=0;
 
116
 
 
117
        view->prevpanx=0;
 
118
        view->prevpany=0;
 
119
 
 
120
 
 
121
        view->zoom=-20;
 
122
        view->texture=1;
 
123
        view->FontSize=52;
 
124
 
 
125
        view->topviewusermode =TOP_VIEW_USER_NOVICE_MODE;       //for demo
 
126
        view->mg.active=0;
 
127
        view->mg.x=0;
 
128
        view->mg.y=0;
 
129
        view->mg.width=DEFAULT_MAGNIFIER_WIDTH;
 
130
        view->mg.height=DEFAULT_MAGNIFIER_HEIGHT;
 
131
        view->mg.kts=DEFAULT_MAGNIFIER_KTS;
 
132
        view->fmg.R=DEFAULT_FISHEYE_MAGNIFIER_RADIUS;
 
133
        view->fmg.active=0;
 
134
        view->mouse.mouse_down=0;
 
135
        view->activeGraph=-1;
 
136
        view->SignalBlock=0;
 
137
        view->Selection.Active=0;
 
138
        view->Selection.SelectionColor.R=0.5;
 
139
        view->Selection.SelectionColor.G=(float)0.2;
 
140
        view->Selection.SelectionColor.B=1;
 
141
        view->Selection.SelectionColor.A=1;
 
142
        view->Selection.Anti=0;
 
143
        view->Topview=malloc(sizeof(topview));
 
144
}
 
145
 
 
146
int add_graph_to_viewport_from_file (char* fileName)
 
147
{
 
148
        //returns 1 if successfull else 0
 
149
        Agraph_t* graph;
 
150
        graph=loadGraph(fileName);
 
151
        if(graph)
 
152
        {
 
153
                view->graphCount = view->graphCount + 1; 
 
154
                view->g= (Agraph_t**)realloc(view->g,sizeof(Agraph_t*)*view->graphCount);
 
155
                view->g[view->graphCount-1]=graph;
 
156
                view->activeGraph=view->graphCount-1;
 
157
                //GUI update , graph combo box on top-right should be updated
 
158
                refreshControls(view);
 
159
                return 1;
 
160
        }
 
161
        else
 
162
                return 0;
 
163
        
 
164
}
 
165
 
 
166
int add_new_graph_to_viewport()
 
167
{
 
168
        //returns graph index , otherwise -1
 
169
        Agraph_t* graph;
 
170
        graph=(Agraph_t*) malloc(sizeof(Agraph_t));
 
171
        if(graph)
 
172
        {
 
173
                view->graphCount = view->graphCount + 1; 
 
174
                view->g[view->graphCount-1]=graph;
 
175
                return (view->graphCount-1);
 
176
        }
 
177
        else
 
178
                return -1;
 
179
}
 
180
 
 
181
 
 
182
void refreshControls(ViewInfo* v)
 
183
{
 
184
 
 
185
        int i=0;
 
186
        GtkComboBox* widget;
 
187
        widget=get_SelectGraph();
 
188
        //load graph names to combobox
 
189
        for (i=0; i < v->graphCount; i ++)
 
190
        {
 
191
                gtk_combo_box_append_text(widget,((custom_graph_data*)(AGDATA(v->g[i])))->GraphFileName);
 
192
        }
 
193
        view->SignalBlock=1;    //HACK
 
194
        gtk_combo_box_set_active (widget,view->activeGraph);
 
195
        view->SignalBlock=0;
 
196
 
 
197
 
 
198
        //change button colors
 
199
        Color_Widget_bg ("gray",glade_xml_get_widget(xml, "btnDot"));   
 
200
        Color_Widget_bg ("gray",glade_xml_get_widget(xml, "btnNeato")); 
 
201
        Color_Widget_bg ("gray",glade_xml_get_widget(xml, "btnTwopi")); 
 
202
        Color_Widget_bg ("gray",glade_xml_get_widget(xml, "btnCirco")); 
 
203
        Color_Widget_bg ("gray",glade_xml_get_widget(xml, "btnFdp"));   
 
204
 
 
205
 
 
206
        switch( ((custom_graph_data*)(AGDATA(view->g[view->activeGraph])))->Engine )
 
207
        {
 
208
                case 0: 
 
209
                        Color_Widget_bg ("red",glade_xml_get_widget(xml, "btnDot"));    
 
210
                        break;
 
211
 
 
212
                case 1: 
 
213
                        Color_Widget_bg ("red",glade_xml_get_widget(xml, "btnNeato"));  
 
214
                        break;
 
215
 
 
216
                case 2: 
 
217
                        Color_Widget_bg ("red",glade_xml_get_widget(xml, "btnTwopi"));  
 
218
                        break;
 
219
 
 
220
                case 3: 
 
221
                        Color_Widget_bg ("red",glade_xml_get_widget(xml, "btnCirco"));  
 
222
                        break;
 
223
 
 
224
                case 4: 
 
225
                        Color_Widget_bg ("red",glade_xml_get_widget(xml, "btnFdp"));    
 
226
                        break;
 
227
 
 
228
 
 
229
        }
 
230
        glexpose();
 
231
 
 
232
                
 
233
 
 
234
 
 
235
 
 
236
}
 
237
 
 
238
 
 
239
void update_graph_params(Agraph_t* graph)       //adds gledit params 
 
240
{
 
241
 
 
242
 
 
243
        char tempString[100];
 
244
        agattr(graph,AGRAPH,"GraphFileName",((custom_graph_data*)(AGDATA(graph)))->GraphFileName);
 
245
        agattr(graph,AGRAPH,"GraphName",((custom_graph_data*)(AGDATA(graph)))->GraphName);
 
246
        sprintf( tempString, "%i", ((custom_graph_data*)(AGDATA(graph)))->AlwaysShow );
 
247
        agattr(graph,AGRAPH,"AlwaysShow",tempString);
 
248
        sprintf( tempString, "%i", ((custom_graph_data*)(AGDATA(graph)))->TopView);
 
249
                agattr(graph,AGRAPH,"TopView",tempString);
 
250
        sprintf( tempString, "%i", ((custom_graph_data*)(AGDATA(graph)))->Locked);
 
251
                agattr(graph,AGRAPH,"Locked",tempString);
 
252
        sprintf( tempString, "%i", ((custom_graph_data*)(AGDATA(graph)))->Engine);
 
253
                agattr(graph,AGRAPH,"Engine",tempString);
 
254
 
 
255
}
 
256
 
 
257
void load_graph_params(Agraph_t* graph) //run once right after loading graph
 
258
{
 
259
                //file may or may have not gl edit attributes
 
260
                //first defaults are set in loading function
 
261
                //here file is checked for previously saved gledit attributes
 
262
        if(agget(graph, "GraphName"))           //Graph Name
 
263
        {       
 
264
                ((custom_graph_data*)AGDATA(graph))->GraphName=(char*)malloc((strlen(agget(graph, "GraphName"))+1)*sizeof(char)); 
 
265
                        strcpy(((custom_graph_data*)AGDATA(graph))->GraphName,agget(graph, "GraphName")); 
 
266
        }
 
267
        if(agget(graph, "AlwaysShow"))          //Graph Name
 
268
                ((custom_graph_data*)AGDATA(graph))->AlwaysShow=atoi(agget(graph, "AlwaysShow"));
 
269
        else
 
270
                ((custom_graph_data*)AGDATA(graph))->AlwaysShow=0;
 
271
 
 
272
        if(agget(graph, "TopView"))             //Graph Name
 
273
                ((custom_graph_data*)AGDATA(graph))->TopView=atoi(agget(graph, "TopView"));
 
274
        else
 
275
                ((custom_graph_data*)AGDATA(graph))->TopView=0;
 
276
        if(agget(graph, "Locked"))              //Graph Name
 
277
                ((custom_graph_data*)AGDATA(graph))->Locked=atoi(agget(graph, "Locked"));
 
278
        else
 
279
                ((custom_graph_data*)AGDATA(graph))->Locked=0;
 
280
        if(agget(graph, "Engine"))              //Graph Name
 
281
                ((custom_graph_data*)AGDATA(graph))->Engine=atoi(agget(graph, "Engine"));
 
282
        else
 
283
                ((custom_graph_data*)AGDATA(graph))->Engine=0;  //DOT
 
284
 
 
285
        ((custom_graph_data*)AGDATA(graph))->Modified=0;        //not modified yet
 
286
        ((custom_graph_data*)AGDATA(graph))->selectedEdges='\0';
 
287
        ((custom_graph_data*)AGDATA(graph))->selectedNodes='\0';
 
288
        ((custom_graph_data*)AGDATA(graph))->selectedGraphs='\0';
 
289
 
 
290
        ((custom_graph_data*)AGDATA(graph))->selectedNodesCount=0;
 
291
        ((custom_graph_data*)AGDATA(graph))->selectedEdgesCount=0;
 
292
        ((custom_graph_data*)AGDATA(graph))->selectedGraphsCount=0;
 
293
 
 
294
        
 
295
        
 
296
/*              if(agget((void*)g, "xdotversion"))      //xdot exists
 
297
                        ((custom_graph_data*)AGDATA(g))->view->Topview=0; //need to check xdot version attribute
 
298
                else            //we dont know if it is view->Topview or simply a graph with no xdot, for testing i ll use view->Topview
 
299
                        ((custom_graph_data*)AGDATA(g))->view->Topview=1;  */
 
300
 
 
301
 
 
302
 
 
303
 
 
304
 
 
305
}
 
306
 
 
307
int save_graph()        //save without prompt
 
308
{
 
309
        //check if there is an active graph
 
310
        if(view->activeGraph > -1)
 
311
        {
 
312
                //check if active graph has a file name
 
313
                if (((custom_graph_data*)AGDATA(view->g[view->activeGraph]))->GraphFileName)
 
314
                {
 
315
                        return save_graph_with_file_name(view->g[view->activeGraph],((custom_graph_data*)AGDATA(view->g[view->activeGraph]))->GraphFileName);
 
316
                }
 
317
                else
 
318
                        return save_as_graph();
 
319
        }
 
320
        return 1;
 
321
 
 
322
}
 
323
int save_as_graph() //save with prompt
 
324
{
 
325
        //check if there is an active graph
 
326
        if(view->activeGraph > -1)
 
327
        {
 
328
                GtkWidget *dialog;
 
329
                dialog = gtk_file_chooser_dialog_new ("Save File",
 
330
                                      NULL,
 
331
                                      GTK_FILE_CHOOSER_ACTION_SAVE,
 
332
                                      GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
 
333
                                      GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
 
334
                                      NULL);
 
335
                gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog), TRUE);
 
336
                if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
 
337
                {
 
338
                        char *filename;
 
339
                        filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
 
340
                        save_graph_with_file_name(view->g[view->activeGraph],filename);
 
341
                        g_free (filename);
 
342
                        gtk_widget_destroy (dialog);
 
343
 
 
344
                        return 1;
 
345
                }
 
346
                else
 
347
                {
 
348
                        gtk_widget_destroy (dialog);
 
349
                        return 0;
 
350
                }
 
351
        }
 
352
        return 0;
 
353
}
 
354
int save_graph_with_file_name(Agraph_t* graph,char* fileName)   //saves graph with file name,if file name is NULL save as is ++
 
355
{
 
356
        //if file name is NULL save to graph's filename
 
357
        //else use file name, this implements save as..
 
358
        FILE* output_file;
 
359
        update_graph_params(graph);
 
360
        if(fileName)    
 
361
                output_file = fopen(fileName, "w");
 
362
        else
 
363
        {
 
364
                if  ( ((custom_graph_data*)(AGDATA(graph)))->GraphFileName )
 
365
                        output_file = fopen(((custom_graph_data*)(AGDATA(graph)))->GraphFileName, "w");
 
366
                else
 
367
                {
 
368
                        g_print("there is no file name to save! Programmer error\n");
 
369
                        return 0;
 
370
                }
 
371
        }
 
372
        if (output_file  == NULL)
 
373
        {
 
374
                g_print("Cannot create file \n");
 
375
                return 0;
 
376
        }
 
377
        else if (agwrite(graph,(void*) output_file))
 
378
        {
 
379
                g_print("%s sucessfully saved \n", fileName);
 
380
                        return 1;
 
381
        }
 
382
        return 0;
 
383
}
 
384
 
 
385
int create_xdot_for_graph(Agraph_t* graph,int keeppos)
 
386
{
 
387
        //0 failed , 1 successfull
 
388
        //save graph to __temp.dot
 
389
        //run dot/neato whatever to create the xdot version __temp.xdot
 
390
        //delete temp files
 
391
        //use this function to do layouts too   
 
392
        int r=0;
 
393
        FILE* output_file;
 
394
        update_graph_params(graph);
 
395
#ifdef _WIN32
 
396
        if(output_file = fopen("c:/__tempfile.dot", "w"))
 
397
#else
 
398
        if(output_file = fopen("/tmp/__tempfile.dot", "w"))
 
399
#endif
 
400
        {
 
401
                clear_graph_xdot(graph);
 
402
                agwrite(graph,(void*) output_file);             //save graph
 
403
                fclose(output_file);//close file desc
 
404
 
 
405
                if(keeppos==0)
 
406
                {
 
407
#ifdef _WIN32
 
408
                        if ( ((custom_graph_data*)AGDATA(graph))->Engine == 0)
 
409
                                system("\"C:/Program Files/Graphviz2.15/bin/dot.exe\" -Txdot -Kdot c:/__tempfile.dot -oc:/__tempfile.xdot");
 
410
                        if ( ((custom_graph_data*)AGDATA(graph))->Engine == 1)
 
411
                                system("\"C:/Program Files/Graphviz2.15/bin/dot.exe\" -Txdot -Kneato c:/__tempfile.dot -oc:/__tempfile.xdot");
 
412
                        if ( ((custom_graph_data*)AGDATA(graph))->Engine == 2)
 
413
                                system("\"C:/Program Files/Graphviz2.15/bin/dot.exe\" -Txdot -Ktwopi c:/__tempfile.dot -oc:/__tempfile.xdot");
 
414
                        if ( ((custom_graph_data*)AGDATA(graph))->Engine == 3)
 
415
                                system("\"C:/Program Files/Graphviz2.15/bin/dot.exe\" -Txdot -Kcirco c:/__tempfile.dot -oc:/__tempfile.xdot");
 
416
                        if ( ((custom_graph_data*)AGDATA(graph))->Engine == 4)
 
417
                                system("\"C:/Program Files/Graphviz2.15/bin/dot.exe\" -Txdot -Kfdp c:/__tempfile.dot -oc:/__tempfile.xdot");
 
418
#else
 
419
                        if ( ((custom_graph_data*)AGDATA(graph))->Engine == 0)
 
420
                                system("\"dot\" -Txdot -Kdot /tmp/__tempfile.dot -o/tmp/__tempfile.xdot");
 
421
                        if ( ((custom_graph_data*)AGDATA(graph))->Engine == 1)
 
422
                                system("\"dot\" -Txdot -Kneato /tmp/__tempfile.dot -o/tmp/__tempfile.xdot");
 
423
                        if ( ((custom_graph_data*)AGDATA(graph))->Engine == 2)
 
424
                                system("\"dot\" -Txdot -Ktwopi /tmp/__tempfile.dot -o/tmp/__tempfile.xdot");
 
425
                        if ( ((custom_graph_data*)AGDATA(graph))->Engine == 3)
 
426
                                system("\"dot\" -Txdot -Kcirco /tmp/__tempfile.dot -o/tmp/__tempfile.xdot");
 
427
                        if ( ((custom_graph_data*)AGDATA(graph))->Engine == 4)
 
428
                                system("\"dot\" -Txdot -Kfdp /tmp/__tempfile.dot -o/tmp/__tempfile.xdot");
 
429
#endif
 
430
                }
 
431
 
 
432
                //no position change
 
433
                else
 
434
#ifdef _WIN32
 
435
                                system("\"C:/Program Files/Graphviz2.15/bin/neato.exe\" -n2 -Txdot  c:/__tempfile.dot -oc:/__tempfile.xdot");
 
436
#else
 
437
                                system("\"neato\" -n2 -Txdot  /tmp/__tempfile.dot -o/tmp/__tempfile.xdot");
 
438
#endif
 
439
                        
 
440
                if(r)           //load the new graph and change file name
 
441
                {
 
442
                        clear_graph(graph);     //i am not sure about this, need to talk to North
 
443
                        return TRUE;    
 
444
 
 
445
                }
 
446
        }
 
447
        else
 
448
                return FALSE;
 
449
 
 
450
        return 0;
 
451
 
 
452
}
 
453
int do_graph_layout(Agraph_t* graph,int Engine,int keeppos) //changes the layout, all user relocations are reset unless keeppos is set to 1
 
454
{
 
455
/*      Agnode_t *v;
 
456
        Agedge_t *e;
 
457
        Agsym_t *attr;
 
458
        Dict_t *d;
 
459
        int cnt;*/
 
460
//      mydata *p;
 
461
        FILE* input_file;
 
462
        char* _filename=(char*)malloc((strlen(((custom_graph_data*)(AGDATA(view->g[view->activeGraph])))->GraphFileName)+1)*sizeof(char));
 
463
                strcpy(_filename,((custom_graph_data*)AGDATA(view->g[view->activeGraph]))->GraphFileName); 
 
464
 
 
465
        
 
466
        ((custom_graph_data*)AGDATA(view->g[view->activeGraph]))->Engine=Engine;
 
467
                create_xdot_for_graph(view->g[view->activeGraph],keeppos);
 
468
 
 
469
#ifdef _WIN32
 
470
        input_file = fopen("c:/__tempfile.xdot", "r");
 
471
#else
 
472
        input_file = fopen("/tmp/__tempfile.xdot", "r");
 
473
#endif
 
474
        clear_graph(view->g[view->activeGraph]);
 
475
        agclose(view->g[view->activeGraph]);
 
476
        if (input_file  == NULL)
 
477
                g_print("temp file Cannot open n");
 
478
        else if (view->g[view->activeGraph]= agread(input_file,NIL(Agdisc_t*)))
 
479
        {
 
480
                fclose(input_file);
 
481
                //attaching rec for graph fields
 
482
                attach_object_custom_data_to_graph(view->g[view->activeGraph]);
 
483
                //set real file name
 
484
                ((custom_graph_data*)AGDATA(view->g[view->activeGraph]))->GraphFileName=(char*)malloc((strlen(_filename)+1)*sizeof(char)); 
 
485
                load_graph_params(view->g[view->activeGraph]);  //init glparams
 
486
                strcpy(((custom_graph_data*)AGDATA(view->g[view->activeGraph]))->GraphFileName,_filename); 
 
487
                free(_filename);
 
488
                //set engine
 
489
                ((custom_graph_data*)AGDATA(view->g[view->activeGraph]))->Engine=Engine;
 
490
                ((custom_graph_data*)AGDATA(view->g[view->activeGraph]))->Modified=1;
 
491
                refreshControls(view);
 
492
                return 1;
 
493
        }
 
494
        else
 
495
        {
 
496
                return 0;
 
497
        }
 
498
 
 
499
        
 
500
        return 0;
 
501
 
 
502
}
 
503
 
 
504
void clear_graph(Agraph_t* graph)
 
505
//clears custom data binded
 
506
{
 
507
        
 
508
 
 
509
}
 
510
 
 
511
Agraph_t* loadGraph(char* filename)
 
512
{
 
513
        Agraph_t *g;
 
514
//      mydata *p;
 
515
        FILE* input_file;
 
516
        input_file = fopen(filename, "r");
 
517
        if (input_file  == NULL)
 
518
                g_print("Cannot open %s\n", filename);
 
519
        else if (g = agread(input_file,NIL(Agdisc_t*)))
 
520
        {
 
521
                attach_object_custom_data_to_graph(g);
 
522
                load_graph_params(g);
 
523
 
 
524
                if ( 
 
525
                        ( !agget(g, "xdotversion"))
 
526
                                 &&  
 
527
                        ( 
 
528
                                ( agget(g, "TopView")=="0")
 
529
                                || 
 
530
                                !agget(g, "TopView")
 
531
                                ) 
 
532
                        
 
533
                        )
 
534
                
 
535
                {
 
536
                create_xdot_for_graph(g,0);
 
537
                fclose(input_file);
 
538
#ifdef _WIN32
 
539
                input_file = fopen("c:/__tempfile.xdot", "r");
 
540
#else
 
541
                input_file = fopen("/tmp/__tempfile.xdot", "r");
 
542
#endif
 
543
                while(input_file== NULL)        //HACK!!!!
 
544
                {
 
545
                        input_file = fopen("c:/__tempfile.xdot", "r");
 
546
//                      g_print("Cannot open xdot  error %si\n",strerror(errno));
 
547
                                
 
548
                }
 
549
                g = agread(input_file,NIL(Agdisc_t*));
 
550
                g_print("xdot is being loaded\n");
 
551
                //attaching rec for graph fields
 
552
                attach_object_custom_data_to_graph(g);
 
553
                load_graph_params(g);
 
554
                //      fclose(input_file);
 
555
                }
 
556
                ((custom_graph_data*)AGDATA(g))->GraphFileName=(char*)malloc((strlen(filename)+1)*sizeof(char)); 
 
557
                //attaching rec for graph objects
 
558
                strcpy(((custom_graph_data*)AGDATA(g))->GraphFileName,filename); 
 
559
                /*if(strcasecmp(agget(g, "TopView"),"1")==0)
 
560
                {
 
561
                        if(
 
562
                                TopviewNodeCount > 0)
 
563
                        {
 
564
                                Dlg=gtk_message_dialog_new (NULL,
 
565
                                                                        GTK_DIALOG_MODAL,
 
566
                                          GTK_MESSAGE_WARNING,
 
567
                                                                        GTK_BUTTONS_OK,
 
568
                                                                        "For Performance issues , this program does not support multiple Topview graphs!");
 
569
                                respond=gtk_dialog_run (Dlg);
 
570
                                gtk_object_destroy (Dlg);
 
571
                                agclose(g);
 
572
                                return 0;
 
573
                        }
 
574
                        else
 
575
                                PrepareTopview(g);
 
576
                }*/
 
577
                preparetopview(g,view->Topview);
 
578
                return g;
 
579
        }
 
580
        else
 
581
        {
 
582
                return 0;
 
583
        }
 
584
        return 0;
 
585
}
 
586
int clear_object_xdot(void* obj)
 
587
{
 
588
        if (obj)
 
589
        {
 
590
                if(agattrsym(obj,"_draw_"))
 
591
                        agset(obj,"_draw_","");
 
592
                if(agattrsym(obj,"_ldraw_"))
 
593
                        agset(obj,"_ldraw_","");
 
594
                if(agattrsym(obj,"_hdraw_"))
 
595
                        agset(obj,"_hdraw_","");
 
596
                if(agattrsym(obj,"_tdraw_"))
 
597
                        agset(obj,"_tdraw_","");
 
598
                if(agattrsym(obj,"_hldraw_"))
 
599
                        agset(obj,"_hldraw_","");
 
600
                if(agattrsym(obj,"_tldraw_"))
 
601
                        agset(obj,"_tldraw_","");
 
602
                return 1;
 
603
        }
 
604
        return 0;
 
605
}
 
606
 
 
607
 
 
608
int clear_graph_xdot(Agraph_t* graph)   //clears all xdot  attributes, used especially before layout change
 
609
{
 
610
   Agnode_t *n;
 
611
        Agedge_t *e;
 
612
        Agraph_t *s;
 
613
 
 
614
        
 
615
        clear_object_xdot(graph);
 
616
        n = agfstnode(graph);
 
617
        
 
618
        for (s = agfstsubg(graph); s; s = agnxtsubg(s))
 
619
                clear_object_xdot(s);
 
620
 
 
621
        for (n = agfstnode(graph); n; n = agnxtnode(graph, n))
 
622
        {
 
623
                clear_object_xdot(n);
 
624
                for (e = agfstout(graph,n) ; e ; e = agnxtout (graph,e))
 
625
                {
 
626
                        clear_object_xdot(e);
 
627
                }
 
628
        }
 
629
        return 1;       
 
630
 
 
631
 
 
632
}
 
633
 
 
634
/*
 
635
 
 
636
Object Custom Data Functions
 
637
 
 
638
*/
 
639
 
 
640
 
 
641
int attach_object_custom_data_to_graph(Agraph_t* graph)
 
642
{
 
643
    Agnode_t *n;
 
644
        Agedge_t *e;
 
645
        Agraph_t *s;
 
646
 
 
647
        agbindrec(graph,"custom_graph_data",sizeof(custom_graph_data),TRUE);//graph custom data
 
648
        init_object_custom_data(graph,graph);   //attach to graph itself
 
649
 
 
650
        n = agfstnode(graph);
 
651
        
 
652
        for (s = agfstsubg(graph); s; s = agnxtsubg(s))
 
653
                init_object_custom_data(graph,s);       //attach to subgraph 
 
654
 
 
655
        for (n = agfstnode(graph); n; n = agnxtnode(graph, n))
 
656
        {
 
657
                init_object_custom_data(graph,n);       //attach to node
 
658
                for (e = agfstout(graph,n) ; e ; e = agnxtout (graph,e))
 
659
                {
 
660
                        init_object_custom_data(graph,e);       //attach to edge
 
661
                }
 
662
        }
 
663
        return 1;
 
664
        
 
665
}
 
666
 
 
667
 
 
668
int init_object_custom_data(Agraph_t* graph,void* obj)//creates a custom_object_data 
 
669
{
 
670
        agdelrec(graph, "custom_object_data");
 
671
    agbindrec(obj,"custom_object_data",sizeof(custom_object_data),TRUE);
 
672
        ((custom_object_data*)AGDATA(obj))->ID=0;
 
673
        ((custom_object_data*)AGDATA(obj))->ObjName=NULL;
 
674
        ((custom_object_data*)AGDATA(obj))->ObjType=AGTYPE(obj);
 
675
        ((custom_object_data*)AGDATA(obj))->Layer=-1;
 
676
        ((custom_object_data*)AGDATA(obj))->Visible=1;
 
677
        ((custom_object_data*)AGDATA(obj))->Locked=0;
 
678
        ((custom_object_data*)AGDATA(obj))->Highlighted=0;
 
679
        ((custom_object_data*)AGDATA(obj))->NumDataCount=0;
 
680
        ((custom_object_data*)AGDATA(obj))->NumData=NULL;
 
681
        ((custom_object_data*)AGDATA(obj))->StrDataCount=0;
 
682
        ((custom_object_data*)AGDATA(obj))->StrData=NULL; 
 
683
        return 1;
 
684
}
 
685
 
 
686
 
 
687
int clear_object_custom_data(void* obj) //frees memory allocated for cutom object data
 
688
{
 
689
        return ( (clear_string_data_from_object_custom_data(obj)) || (clear_numeric_data_from_object_custom_data(obj)));
 
690
}
 
691
 
 
692
int add_string_data_to_object_custom_data(void* obj,char* data)
 
693
{
 
694
        if ((obj != NULL) && (data !=NULL))
 
695
        {
 
696
                        ((custom_object_data*)AGDATA(obj))->StrData=realloc( ((custom_object_data*)AGDATA(obj))->StrData , sizeof(char*)*(((custom_object_data*)AGDATA(obj))->StrDataCount +1));
 
697
                        ((custom_object_data*)AGDATA(obj))->StrData[((custom_object_data*)AGDATA(obj))->StrDataCount]=malloc((strlen(data)+1) * sizeof(char));
 
698
                        strcpy(((custom_object_data*)AGDATA(obj))->StrData[((custom_object_data*)AGDATA(obj))->StrDataCount],data);
 
699
                        ((custom_object_data*)AGDATA(obj))->StrDataCount++;
 
700
                        return 1;
 
701
        }
 
702
                return 0;
 
703
}
 
704
 
 
705
int add_numeric_data_to_object_custom_data(void* obj,float data)
 
706
{
 
707
        if (obj != NULL)
 
708
        {
 
709
                ((custom_object_data*)AGDATA(obj))->NumData=realloc( ((custom_object_data*)AGDATA(obj))->StrData , sizeof(char*)*(((custom_object_data*)AGDATA(obj))->NumDataCount+1));
 
710
                ((custom_object_data*)AGDATA(obj))->NumData[((custom_object_data*)AGDATA(obj))->NumDataCount]=data;
 
711
                ((custom_object_data*)AGDATA(obj))->NumDataCount++;
 
712
                        return 1;
 
713
        }
 
714
                return 0;
 
715
}
 
716
 
 
717
int clear_string_data_from_object_custom_data(void* obj)
 
718
{
 
719
        if(obj != NULL)
 
720
        {
 
721
                int ind=0;
 
722
                for (ind=0; ind < ((custom_object_data*)AGDATA(obj))->StrDataCount;ind ++)
 
723
                {
 
724
                        free ( ((custom_object_data*)AGDATA(obj))->StrData[ind]);
 
725
                }
 
726
                free(((custom_object_data*)AGDATA(obj))->StrData);
 
727
                return 1;
 
728
        }
 
729
        return 0;       
 
730
}
 
731
 
 
732
int clear_numeric_data_from_object_custom_data(void* obj)
 
733
{
 
734
        if(obj != NULL)
 
735
        {
 
736
                free(((custom_object_data*)AGDATA(obj))->NumData);
 
737
                return 1;
 
738
        }
 
739
        return 0;       
 
740
}
 
741
void move_node(void* obj,float dx,float dy)
 
742
{
 
743
        char buf[512];
 
744
        char buf2[512];
 
745
        char* pch;
 
746
        int a=0;
 
747
        int i=0;
 
748
        if( (agget(obj,"pos")) && ( AGTYPE(obj)==AGNODE ))
 
749
        {
 
750
                //tokenize 
 
751
                strcpy(buf,agget(obj, "pos"));
 
752
 
 
753
                pch=strtok (buf,"," );
 
754
                while (pch != NULL)
 
755
                {
 
756
                        if(i==0)
 
757
                                a=sprintf(buf2+a,"%i,",atoi(pch)-(int)dx);
 
758
                        else
 
759
                                a=sprintf(buf2+a,"%i,",atoi(pch)-(int)dy);
 
760
                        pch=strtok (NULL,"," );
 
761
                        i++;
 
762
                }
 
763
                buf2[strlen(buf2)-1]='\0';
 
764
                agset(obj,"pos",buf2);
 
765
        }
 
766
}
 
767
 
 
768
void move_nodes(Agraph_t* g)    //move selected nodes 
 
769
{
 
770
        Agnode_t* obj;
 
771
 
 
772
        float dx,dy;
 
773
        xdot* bf;
 
774
        int i=0;
 
775
        dx=view->GLx-view->GLx2;
 
776
        dy=view->GLy-view->GLy2;
 
777
 
 
778
        if(((custom_graph_data*)AGDATA(view->g[view->activeGraph]))->TopView == 0)
 
779
        {
 
780
                for (i=0;i < ((custom_graph_data*)AGDATA(g))->selectedNodesCount;i++)
 
781
                {       
 
782
                        obj=((custom_graph_data*)AGDATA(g))->selectedNodes[i];
 
783
                        bf=parseXDot (agget(obj,"_draw_"));
 
784
                        agset(obj,"_draw_",move_xdot(obj,bf,(int)dx,(int)dy,0));
 
785
                        free(bf);
 
786
                        bf=parseXDot (agget(obj,"_ldraw_"));
 
787
                        agset(obj,"_ldraw_",move_xdot(obj,bf,(int)dx,(int)dy,0));
 
788
                        free(bf);
 
789
                        move_node(obj,dx,dy);
 
790
                        //iterate edges
 
791
                        /*for (e = agfstout(g,obj) ; e ; e = agnxtout (g,e))
 
792
                        {
 
793
                                bf=parseXDot (agget(e,"_tdraw_"));
 
794
                                agset(e,"_tdraw_",move_xdot(e,bf,(int)dx,(int)dy,0.00));
 
795
                                free(bf);
 
796
                                bf=parseXDot (agget(e,"_tldraw_"));
 
797
                                agset(e,"_tldraw_",move_xdot(e,bf,(int)dx,(int)dy,0.00));
 
798
                                free(bf);
 
799
                                bf=parseXDot (agget(e,"_draw_"));
 
800
                                agset(e,"_draw_",offset_spline(bf,(int)dx,(int)dy,0.00,0.00,0.00));
 
801
                                free(bf);
 
802
                                bf=parseXDot (agget(e,"_ldraw_"));
 
803
                                agset(e,"_ldraw_",offset_spline(bf,(int)dx,(int)dy,0.00,0.00,0.00));
 
804
                                free (bf);
 
805
                        }*/
 
806
        /*              for (e = agfstin(g,obj) ; e ; e = agnxtin (g,e))
 
807
                        {
 
808
                                free(bf);
 
809
                                bf=parseXDot (agget(e,"_hdraw_"));
 
810
                                agset(e,"_hdraw_",move_xdot(e,bf,(int)dx,(int)dy,0.00));
 
811
                                free(bf);
 
812
                                bf=parseXDot (agget(e,"_hldraw_"));
 
813
                                agset(e,"_hldraw_",move_xdot(e,bf,(int)dx,(int)dy,0.00));
 
814
                                free(bf);
 
815
                                bf=parseXDot (agget(e,"_draw_"));
 
816
                                agset(e,"_draw_",offset_spline(e,bf,(int)dx,(int)dy,0.00,0.00,0.00));
 
817
                                free(bf);
 
818
                                bf=parseXDot (agget(e,"_ldraw_"));
 
819
                                agset(e,"_ldraw_",offset_spline(e,bf,(int)dx,(int)dy,0.00,0.00,0.00));
 
820
                        }*/
 
821
                }
 
822
        }
 
823
}
 
824
 
 
825
 
 
826
 
 
827
 
 
828
int SetGdkColor(GdkColor* c,char* color)
 
829
{
 
830
        gvcolor_t cl;   
 
831
        if (color != '\0')
 
832
        {
 
833
                colorxlate(color, &cl, RGBA_DOUBLE);
 
834
                c->red=(int)(cl.u.RGBA[0]*65535.0);
 
835
                c->green=(int)(cl.u.RGBA[1]*65535.0);
 
836
                c->blue=(int)(cl.u.RGBA[2]*65535.0);
 
837
                return 1;
 
838
        }
 
839
        else
 
840
                return 0;
 
841
 
 
842
}
 
843
 
 
844
void glexpose()
 
845
{
 
846
        expose_event (view->drawing_area,NULL,NULL);
 
847
}
 
848