File: metanet.c

package info (click to toggle)
scilab 2.6-4
  • links: PTS
  • area: non-free
  • in suites: woody
  • size: 54,632 kB
  • ctags: 40,267
  • sloc: ansic: 267,851; fortran: 166,549; sh: 10,005; makefile: 4,119; tcl: 1,070; cpp: 233; csh: 143; asm: 135; perl: 130; java: 39
file content (517 lines) | stat: -rw-r--r-- 13,327 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
/* Copyright INRIA */
#include <stdio.h>
#include <string.h>
#include <X11/cursorfont.h>
#include <X11/Intrinsic.h>
#include <X11/Shell.h>
#include <X11/StringDefs.h>
#include <X11/Xaw/Form.h>
#include <X11/Xaw/Label.h>

#include "defs.h"
#include "color.h"
#include "list.h"
#include "graph.h"
#include "graphics.h"
#include "menus.h"
#include "metadir.h"

#include "libCalCom.h"
#include "libCom.h"

#define TEMPS  1000    

#define max(a,b) ((a) > (b) ? (a) : (b))

/* Table of messages known by xmetanet */

static void quitter_appli_msgact();  
extern void GetMsg();
static void erreur_message_msgact(); 

static actions_messages tb_messages[]={
    {ID_GeCI,MSG_QUITTER_APPLI,NBP_QUITTER_APPLI,quitter_appli_msgact},
    {NULL,MSG_DISTRIB_LISTE_ELMNT,NBP_DISTRIB_LISTE_ELMNT,GetMsg},
    {NULL,NULL,0,erreur_message_msgact}};

static void clock_tic();   
static int find();
static void PrintUsage();

extern void CreateMenus();
extern void InitMetanet();
extern void MakeDraw();
extern void GetFonts();
extern XFontStruct *FontSelect();

char metanetName[MAXNAM];
char *Version = "3.0.3";

int isServeur;
int theWindow;

int metaWidth;
int metaHeight;

int drawHeight;
int drawWidth;

int arcW;
int arcH;
int nodeW;
int nodeDiam;

int arrowLength = ARROWLENGTH;
int arrowWidth = ARROWWIDTH;
int bezierDy = BEZIERDY;
double arcDx = ARCDX;
int arcDy = ARCDY;

int incX = INCX;
int incY = INCY;

int dpyWidth;
int dpyHeight;

typedef struct res {
    Pixel color[NUMCOLORS];
    Dimension draw_height;
    Dimension draw_width;
    String geometry;
} RES, *RESPTR;

static RES the_res;

static XtResource app_resources[] = {
    {"color0", "Color0", XtRPixel, sizeof(Pixel),
    XtOffset(RESPTR, color[0]), XtRString, (caddr_t) "black"},
    {"color1","Color1", XtRPixel, sizeof(Pixel),
    XtOffset(RESPTR, color[1]), XtRString, (caddr_t) "navyblue"},
    {"color2","Color2", XtRPixel, sizeof(Pixel),
    XtOffset(RESPTR, color[2]), XtRString, (caddr_t) "blue"},
    {"color3","Color3", XtRPixel, sizeof(Pixel),
    XtOffset(RESPTR, color[3]), XtRString, (caddr_t) "skyblue"},
    {"color4","Color4", XtRPixel, sizeof(Pixel),
    XtOffset(RESPTR, color[4]), XtRString, (caddr_t) "aquamarine"},
    {"color5","Color5", XtRPixel, sizeof(Pixel),
    XtOffset(RESPTR, color[5]), XtRString, (caddr_t) "forestgreen"},
    {"color6","Color6", XtRPixel, sizeof(Pixel),
    XtOffset(RESPTR, color[6]), XtRString, (caddr_t) "green"},
    {"color7","Color7", XtRPixel, sizeof(Pixel),
    XtOffset(RESPTR, color[7]), XtRString, (caddr_t) "lightcyan"},
    {"color8","Color8", XtRPixel, sizeof(Pixel),
    XtOffset(RESPTR, color[8]), XtRString, (caddr_t) "cyan"},
    {"color9","Color9", XtRPixel, sizeof(Pixel),
    XtOffset(RESPTR, color[9]), XtRString, (caddr_t) "orange"},
    {"color10","Color10", XtRPixel, sizeof(Pixel),
    XtOffset(RESPTR, color[10]), XtRString, (caddr_t) "red"},
    {"color11","Color11", XtRPixel, sizeof(Pixel),
    XtOffset(RESPTR, color[11]), XtRString, (caddr_t) "magenta"},
    {"color12","Color12", XtRPixel, sizeof(Pixel),
    XtOffset(RESPTR, color[12]), XtRString, (caddr_t) "violet"},
    {"color13","Color13", XtRPixel, sizeof(Pixel),
    XtOffset(RESPTR, color[13]), XtRString, (caddr_t) "yellow"},
    {"color14","Color14", XtRPixel, sizeof(Pixel),
    XtOffset(RESPTR, color[14]), XtRString, (caddr_t) "gold"},
    {"color15","Color15", XtRPixel, sizeof(Pixel),
    XtOffset(RESPTR, color[15]), XtRString, (caddr_t) "beige"},
    {"color16","Color16", XtRPixel, sizeof(Pixel),
    XtOffset(RESPTR, color[16]), XtRString, (caddr_t) "white"},
    {"drawHeight", "DrawHeight", XtRDimension, sizeof(Dimension),
       XtOffset(RESPTR, draw_height), XtRImmediate, (caddr_t) DRAWHEIGHT},
    {"drawWidth", "DrawWidth", XtRDimension, sizeof(Dimension),
       XtOffset(RESPTR, draw_width), XtRImmediate, (caddr_t) DRAWWIDTH},
    {"geometry", "Geometry", XtRString, sizeof(String),
       XtOffset(RESPTR, geometry), NULL,NULL},
};

Widget toplevel, frame, metanetMenu, drawViewport, metanetDraw;

int menuId = BEGIN;
G theG;
XtAppContext app_con;

void Destroyed(widget, closure, callData)
Widget widget;	
caddr_t closure;	
caddr_t callData;	
{
  exit(0);
}

void SetTitle(menu)
int menu;
{
  Arg args[2];
  int iargs;
  char title[2 * MAXNAM];
  char icon[2 * MAXNAM];

  switch (menu) {
  case BEGIN:
    if (isServeur) {
      sprintf(title,"%s    Window %d",metanetName,theWindow);
      sprintf(icon,"Net %d",theWindow);
    } else {
      sprintf(title,"%s",metanetName);
      strcpy(icon,"Metanet");
    }
    break;
  default:
    if (isServeur) {
      sprintf(title,"%s    Window %d    Graph name: %s",
	      metanetName,theWindow,theGraph->name);
      sprintf(icon,"Net %d",theWindow);
    } else {
      sprintf(title,"%s    Graph name: %s",metanetName,theGraph->name);
      strcpy(icon,"Metanet");
    }
    break;
  }

  iargs = 0;
  XtSetArg(args[iargs], XtNtitle, title); iargs++;
  XtSetArg(args[iargs], XtNiconName, icon); iargs++;
  XtSetValues(toplevel,args,iargs);
}

static String fallback_resources[] = {NULL,NULL};

int main(argc, argv)
unsigned int argc;
char **argv;
{
  Arg args[20];
  int iargs;
  extern void ActionWhenDownMove3(); 
  extern void ActionWhenExpose(); 
  extern void ActionWhenLeave();
  extern void ActionWhenPress1(); 
  extern void ActionWhenPress3(); 
  extern void ActionWhenRelease3();
  extern void ActionWhenReturn();
  static XtActionsRec actionTable[] = {
    {"ActionWhenDownMove3", (XtActionProc) ActionWhenDownMove3},
    {"ActionWhenExpose", (XtActionProc) ActionWhenExpose},
    {"ActionWhenLeave", (XtActionProc) ActionWhenLeave},
    {"ActionWhenPress1", (XtActionProc) ActionWhenPress1},
    {"ActionWhenPress3", (XtActionProc) ActionWhenPress3},
    {"ActionWhenRelease3", (XtActionProc) ActionWhenRelease3},
    {"ActionWhenReturn", (XtActionProc) ActionWhenReturn}
  };
  GC gc, gc_clear, gc_xor;
  XGCValues gcv;
  Display *dpy;
  Drawable d;
  Pixel bg, fg;
  XFontStruct *fontstruct;
  XColor x_fg_color,x_bg_color;
  Widget look;
  int i;
  char *iniG;
  double iniS;
  float v;
  int igeci = -1;
  static char metanetgeom[200];
  XSetWindowAttributes attributes;
  int screen;
  unsigned int max_width,max_height;
  int geom_x, geom_y, dw, dh;
  XSizeHints sizehints;

  iniG = NULL;

  /* Is xmetanet called by Scilab? */

  igeci = find("-pipes",argc,argv);
  if (igeci != -1) {
    init_messages(tb_messages, atoi(argv[igeci+1]),atoi(argv[igeci+2]));
    isServeur = 1;
    for (i = igeci; i < argc - 3 ; i++) {
      if (argv[i+3] != NULL) argv[i] = argv[i+3];
      else argv[i] = NULL;
    }
    argc -= 3;
  }
  else isServeur = 0;

  /* Parse arguments when called by Scilab */
  dw = 0; dh = 0;
  if (isServeur) {
    sscanf(argv[2],"%d",&theWindow);
    iniG = argv[3];
    sscanf(argv[4],"%d",&dw);
    sscanf(argv[5],"%d",&dh);
  }
  else {
    theWindow = 0;
  }

  if (isServeur) {
    metaWidth = IMETAWIDTH;
    metaHeight = IMETAHEIGHT;
  } else {
    metaWidth = METAWIDTH;
    metaHeight = METAHEIGHT;
  }

/* Fallback ressources */

  sprintf(metanetgeom,"Metanet.geometry:+%d+%d",
          X + DX * theWindow,Y + DY * theWindow);
  fallback_resources[0]=metanetgeom;

/* Toplevel widget */

  toplevel = XtAppInitialize(&app_con, (String)"Metanet", NULL, 0, 
			     (int*)&argc, (String*)argv,
			     fallback_resources, NULL, 0);

/* Get ressources */
  XtGetApplicationResources(toplevel, &the_res, app_resources,
			    XtNumber(app_resources), NULL, 0);

  if (dw == 0 && dh == 0) {
    drawWidth = the_res.draw_width;
    drawHeight = the_res.draw_height;
  } else {
    drawWidth = dw;
    drawHeight = dh;
  }
/* Minimum 100 x 100 for Metanet draw */ 
  drawWidth = max(drawWidth,100);
  drawHeight = max(drawHeight,100);
  
  XParseGeometry(the_res.geometry,
		 &geom_x, &geom_y, &metaWidth, &metaHeight);

/* Parse xmetanet arguments */

  iniS = -1.0;
  if (!isServeur) {
    if (argc == 2) {
      if (!strcmp(argv[1],"-usage") || !strcmp(argv[1],"-help")) {
	PrintUsage();
	exit(0);
      }
      if (!strcmp(argv[1],"-version")) {
	printf("Version %s\n",Version);
	exit(0);
      }
    }
    if (argc % 2 == 1) iniG = 0;
    else iniG = argv[argc-1];
    for (i = 1; i < argc - 1; i++) {
      if (!strcmp(argv[i++],"-scale")) {
	if (sscanf(argv[i],"%g",&v) > 0)
	  if (v > 0) iniS = (double)v;
      }
      else {
	fprintf(stderr,"Bad arguments\n");
	PrintUsage();
	exit(1);
      }
    }
  }

  if (isServeur) {
    XtAppAddTimeOut (app_con,
		     TEMPS,
		     (XtTimerCallbackProc) clock_tic,
		     (XtPointer)toplevel) ;
  }
  
  XtAppAddActions(app_con,actionTable,XtNumber(actionTable));

  sprintf(metanetName,"Metanet %s",Version);

  SetTitle(BEGIN);

  dpy = XtDisplay(toplevel);
  theG.dpy = dpy;
  screen = DefaultScreen(dpy);

/* Dimension of xmetanet window */

  dpyWidth = DisplayWidth(dpy,screen);
  dpyHeight = DisplayHeight(dpy,screen);

  max_width = (int)(dpyWidth * 0.8);
  max_height = (int)(dpyHeight * 0.8);

  if (max_width < metaWidth) metaWidth = max_width;
  if (max_height < metaHeight) metaHeight = max_height;

  iargs = 0;
  XtSetArg( args[iargs], XtNheight, metaHeight); iargs++;
  XtSetArg( args[iargs], XtNwidth, metaWidth); iargs++;
  XtSetArg( args[iargs], XtNx, X); iargs++;
  XtSetArg( args[iargs], XtNy, Y); iargs++;
  XtSetArg( args[iargs], XtNdefaultDistance, 0); iargs++;
  frame = XtCreateManagedWidget( "form", formWidgetClass, toplevel,
				args, iargs);
  XtAddCallback(frame, XtNdestroyCallback, (XtCallbackProc)Destroyed, NULL );

  if ((fontstruct = XLoadQueryFont(dpy, METAFONT)) == NULL) {
    fprintf(stderr,"Font %s is unknown\n",METAFONT);
    fprintf(stderr,"I'll use font: fixed\n");
    if ((fontstruct = XLoadQueryFont(dpy, "fixed")) == NULL) {
      fprintf(stderr,"Unknown font: fixed\n");
      exit(1);
    }
  }

  theG.metafont = fontstruct;
  
  if ((fontstruct = XLoadQueryFont(dpy, HELPFONT)) == NULL) {
    fprintf(stderr,"Font %s is unknown\n",HELPFONT);
    fprintf(stderr,"I'll use font: fixed\n");
    if ((fontstruct = XLoadQueryFont(dpy, "fixed")) == NULL) {
      fprintf(stderr,"Unknown font: fixed\n");
      exit(1);
    }
  }

  theG.helpfont = fontstruct;

  CreateMenus();
  MakeDraw();
				  
  XtRealizeWidget(toplevel);

  d = XtWindow(metanetDraw);

  attributes.backing_store = Always;
  XChangeWindowAttributes(dpy,d,CWBackingStore,&attributes);

/* Window Manager size hints */
  sizehints.flags = PMinSize | USPosition | PPosition;
  sizehints.x = X + DX * theWindow;
  sizehints.y = Y + DY * theWindow;
  sizehints.min_width = MINWIDTH;
  sizehints.min_height = MINHEIGHT;
  XSetNormalHints(dpy,XtWindow(toplevel),&sizehints);

  /* get color pixels */
  for (i = 0; i < NUMCOLORS - 2; i++) Colors[i+1] = the_res.color[i];

  /* get foreground and background pixels and colors */
  XtSetArg(args[0],XtNlabel,"");
  look = XtCreateWidget("look", labelWidgetClass, toplevel, args, 1);

  XtSetArg(args[0],XtNforeground, &x_fg_color.pixel);
  XtSetArg(args[1],XtNbackground, &x_bg_color.pixel);
  XtGetValues(look,args,2);
  fg = x_fg_color.pixel;
  bg = x_bg_color.pixel;
  Colors[0] = fg;
  Colors[NUMCOLORS-1] = bg;

  XtDestroyWidget(look);

  /* GC */
  GetFonts();
  fontstruct = FontSelect(DRAWFONTSIZE);
  gcv.font = fontstruct->fid;

  gcv.foreground = fg;
  gcv.background = bg;
  gcv.function = GXcopy; 
  gc = XCreateGC(dpy,d,
		 (GCFont | GCForeground | GCBackground | GCFunction),&gcv);

  /* GC Clear */
  gcv.foreground = bg;
  gcv.background = bg;
  gcv.function = GXcopy;
  gc_clear = XCreateGC(dpy,d,
		       (GCFont | GCForeground | GCBackground | GCFunction),
		       &gcv);

  /* GC Xor */
  gcv.foreground = fg ^ bg; 
  gcv.background = bg;
  gcv.function = GXxor;
  gcv.line_width = 1;
  gc_xor = XCreateGC(dpy,d,(GCForeground | GCBackground | GCFunction),&gcv);
  
  XDefineCursor(dpy,d,XCreateFontCursor(dpy,XC_draft_small));
  
  theG.d = d;
  theG.bg = bg;
  theG.fg = fg;
  theG.gc = gc;
  theG.gc_clear = gc_clear;
  theG.gc_xor = gc_xor;
  theG.drawfont = fontstruct;
  theG.shell = NULL;

  theColor = fg;

  InitMetanet(iniG,iniS);

  XtMapWidget(toplevel);

  XtAppMainLoop(app_con);

  exit(0);
}

static void clock_tic(client_data,id)
XtPointer client_data;
XtIntervalId id;
{
  scanner_messages();
  XtAppAddTimeOut (app_con,
		   TEMPS,
		   (XtTimerCallbackProc) clock_tic,
		   (XtPointer)toplevel);
}

static void erreur_message_msgact(message)
Message message;
{
  fprintf(stderr, "Message  recu incorrect !!!\n");
  envoyer_message_parametres_var(ID_GeCI, MSG_FIN_APPLI, NULL);
  exit(1);
}

static void quitter_appli_msgact(message)
Message message;
{  
  exit(0);
}

static int find(s,n,t)
char *s;
int n;
char **t;
{
  int i;
  for (i=0; i<n; i++)
    if (!strcmp(s,t[i])) return(i);
  return(-1);
}

static void PrintUsage()
{
  printf("Usage: xmetanet [OPTION]... GRAPH_FILE");
}

void MetanetQuit()
{
  XtUnmapWidget(toplevel);
  exit(0);
}

void GetMetanetGeometry(x,y,w,h)
int *x,*y,*w,*h;
{
  Window root,parent,*children;
  unsigned int nchildren;
  XWindowAttributes war;
  XQueryTree(theG.dpy,XtWindow(toplevel),&root,&parent,&children,&nchildren);
  if (parent != root) XGetWindowAttributes(theG.dpy,parent,&war);
  else XGetWindowAttributes(theG.dpy,XtWindow(toplevel),&war);
  *h = war.height; *w = war.width; *x = war.x; *y = war.y;
}