~ubuntu-branches/ubuntu/hoary/scilab/hoary

« back to all changes in this revision

Viewing changes to xmetanet/metanet.c

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Werner
  • Date: 2005-01-09 22:58:21 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20050109225821-473xr8vhgugxxx5j
Tags: 3.0-12
changed configure.in to build scilab's own malloc.o, closes: #255869

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright INRIA */
2
 
#include <stdio.h>
3
 
#include <string.h>
4
 
#include <X11/cursorfont.h>
5
 
#include <X11/Intrinsic.h>
6
 
#include <X11/Shell.h>
7
 
#include <X11/StringDefs.h>
8
 
#include <X11/Xaw/Form.h>
9
 
#include <X11/Xaw/Label.h>
10
 
 
11
 
#include "defs.h"
12
 
#include "color.h"
13
 
#include "list.h"
14
 
#include "graph.h"
15
 
#include "graphics.h"
16
 
#include "menus.h"
17
 
#include "metadir.h"
18
 
 
19
 
#include "libCalCom.h"
20
 
#include "libCom.h"
21
 
 
22
 
#define TEMPS  1000    
23
 
 
24
 
#define max(a,b) ((a) > (b) ? (a) : (b))
25
 
 
26
 
/* Table of messages known by xmetanet */
27
 
 
28
 
static void quitter_appli_msgact();  
29
 
extern void GetMsg();
30
 
static void erreur_message_msgact(); 
31
 
 
32
 
static actions_messages tb_messages[]={
33
 
    {ID_GeCI,MSG_QUITTER_APPLI,NBP_QUITTER_APPLI,quitter_appli_msgact},
34
 
    {NULL,MSG_DISTRIB_LISTE_ELMNT,NBP_DISTRIB_LISTE_ELMNT,GetMsg},
35
 
    {NULL,NULL,0,erreur_message_msgact}};
36
 
 
37
 
static void clock_tic();   
38
 
static int find();
39
 
static void PrintUsage();
40
 
 
41
 
extern void CreateMenus();
42
 
extern void InitMetanet();
43
 
extern void MakeDraw();
44
 
extern void GetFonts();
45
 
extern XFontStruct *FontSelect();
46
 
 
47
 
char metanetName[MAXNAM];
48
 
char *Version = "3.0.3";
49
 
 
50
 
int isServeur;
51
 
int theWindow;
52
 
 
53
 
int metaWidth;
54
 
int metaHeight;
55
 
 
56
 
int drawHeight;
57
 
int drawWidth;
58
 
 
59
 
int arcW;
60
 
int arcH;
61
 
int nodeW;
62
 
int nodeDiam;
63
 
 
64
 
int arrowLength = ARROWLENGTH;
65
 
int arrowWidth = ARROWWIDTH;
66
 
int bezierDy = BEZIERDY;
67
 
double arcDx = ARCDX;
68
 
int arcDy = ARCDY;
69
 
 
70
 
int incX = INCX;
71
 
int incY = INCY;
72
 
 
73
 
int dpyWidth;
74
 
int dpyHeight;
75
 
 
76
 
typedef struct res {
77
 
    Pixel color[NUMCOLORS];
78
 
    Dimension draw_height;
79
 
    Dimension draw_width;
80
 
    String geometry;
81
 
} RES, *RESPTR;
82
 
 
83
 
static RES the_res;
84
 
 
85
 
static XtResource app_resources[] = {
86
 
    {"color0", "Color0", XtRPixel, sizeof(Pixel),
87
 
    XtOffset(RESPTR, color[0]), XtRString, (caddr_t) "black"},
88
 
    {"color1","Color1", XtRPixel, sizeof(Pixel),
89
 
    XtOffset(RESPTR, color[1]), XtRString, (caddr_t) "navyblue"},
90
 
    {"color2","Color2", XtRPixel, sizeof(Pixel),
91
 
    XtOffset(RESPTR, color[2]), XtRString, (caddr_t) "blue"},
92
 
    {"color3","Color3", XtRPixel, sizeof(Pixel),
93
 
    XtOffset(RESPTR, color[3]), XtRString, (caddr_t) "skyblue"},
94
 
    {"color4","Color4", XtRPixel, sizeof(Pixel),
95
 
    XtOffset(RESPTR, color[4]), XtRString, (caddr_t) "aquamarine"},
96
 
    {"color5","Color5", XtRPixel, sizeof(Pixel),
97
 
    XtOffset(RESPTR, color[5]), XtRString, (caddr_t) "forestgreen"},
98
 
    {"color6","Color6", XtRPixel, sizeof(Pixel),
99
 
    XtOffset(RESPTR, color[6]), XtRString, (caddr_t) "green"},
100
 
    {"color7","Color7", XtRPixel, sizeof(Pixel),
101
 
    XtOffset(RESPTR, color[7]), XtRString, (caddr_t) "lightcyan"},
102
 
    {"color8","Color8", XtRPixel, sizeof(Pixel),
103
 
    XtOffset(RESPTR, color[8]), XtRString, (caddr_t) "cyan"},
104
 
    {"color9","Color9", XtRPixel, sizeof(Pixel),
105
 
    XtOffset(RESPTR, color[9]), XtRString, (caddr_t) "orange"},
106
 
    {"color10","Color10", XtRPixel, sizeof(Pixel),
107
 
    XtOffset(RESPTR, color[10]), XtRString, (caddr_t) "red"},
108
 
    {"color11","Color11", XtRPixel, sizeof(Pixel),
109
 
    XtOffset(RESPTR, color[11]), XtRString, (caddr_t) "magenta"},
110
 
    {"color12","Color12", XtRPixel, sizeof(Pixel),
111
 
    XtOffset(RESPTR, color[12]), XtRString, (caddr_t) "violet"},
112
 
    {"color13","Color13", XtRPixel, sizeof(Pixel),
113
 
    XtOffset(RESPTR, color[13]), XtRString, (caddr_t) "yellow"},
114
 
    {"color14","Color14", XtRPixel, sizeof(Pixel),
115
 
    XtOffset(RESPTR, color[14]), XtRString, (caddr_t) "gold"},
116
 
    {"color15","Color15", XtRPixel, sizeof(Pixel),
117
 
    XtOffset(RESPTR, color[15]), XtRString, (caddr_t) "beige"},
118
 
    {"color16","Color16", XtRPixel, sizeof(Pixel),
119
 
    XtOffset(RESPTR, color[16]), XtRString, (caddr_t) "white"},
120
 
    {"drawHeight", "DrawHeight", XtRDimension, sizeof(Dimension),
121
 
       XtOffset(RESPTR, draw_height), XtRImmediate, (caddr_t) DRAWHEIGHT},
122
 
    {"drawWidth", "DrawWidth", XtRDimension, sizeof(Dimension),
123
 
       XtOffset(RESPTR, draw_width), XtRImmediate, (caddr_t) DRAWWIDTH},
124
 
    {"geometry", "Geometry", XtRString, sizeof(String),
125
 
       XtOffset(RESPTR, geometry), NULL,NULL},
126
 
};
127
 
 
128
 
Widget toplevel, frame, metanetMenu, drawViewport, metanetDraw;
129
 
 
130
 
int menuId = BEGIN;
131
 
G theG;
132
 
XtAppContext app_con;
133
 
 
134
 
void Destroyed(widget, closure, callData)
135
 
Widget widget;  
136
 
caddr_t closure;        
137
 
caddr_t callData;       
138
 
{
139
 
  exit(0);
140
 
}
141
 
 
142
 
void SetTitle(menu)
143
 
int menu;
144
 
{
145
 
  Arg args[2];
146
 
  int iargs;
147
 
  char title[2 * MAXNAM];
148
 
  char icon[2 * MAXNAM];
149
 
 
150
 
  switch (menu) {
151
 
  case BEGIN:
152
 
    if (isServeur) {
153
 
      sprintf(title,"%s    Window %d",metanetName,theWindow);
154
 
      sprintf(icon,"Net %d",theWindow);
155
 
    } else {
156
 
      sprintf(title,"%s",metanetName);
157
 
      strcpy(icon,"Metanet");
158
 
    }
159
 
    break;
160
 
  default:
161
 
    if (isServeur) {
162
 
      sprintf(title,"%s    Window %d    Graph name: %s",
163
 
              metanetName,theWindow,theGraph->name);
164
 
      sprintf(icon,"Net %d",theWindow);
165
 
    } else {
166
 
      sprintf(title,"%s    Graph name: %s",metanetName,theGraph->name);
167
 
      strcpy(icon,"Metanet");
168
 
    }
169
 
    break;
170
 
  }
171
 
 
172
 
  iargs = 0;
173
 
  XtSetArg(args[iargs], XtNtitle, title); iargs++;
174
 
  XtSetArg(args[iargs], XtNiconName, icon); iargs++;
175
 
  XtSetValues(toplevel,args,iargs);
176
 
}
177
 
 
178
 
static String fallback_resources[] = {NULL,NULL};
179
 
 
180
 
int main(argc, argv)
181
 
unsigned int argc;
182
 
char **argv;
183
 
{
184
 
  Arg args[20];
185
 
  int iargs;
186
 
  extern void ActionWhenDownMove3(); 
187
 
  extern void ActionWhenExpose(); 
188
 
  extern void ActionWhenLeave();
189
 
  extern void ActionWhenPress1(); 
190
 
  extern void ActionWhenPress3(); 
191
 
  extern void ActionWhenRelease3();
192
 
  extern void ActionWhenReturn();
193
 
  static XtActionsRec actionTable[] = {
194
 
    {"ActionWhenDownMove3", (XtActionProc) ActionWhenDownMove3},
195
 
    {"ActionWhenExpose", (XtActionProc) ActionWhenExpose},
196
 
    {"ActionWhenLeave", (XtActionProc) ActionWhenLeave},
197
 
    {"ActionWhenPress1", (XtActionProc) ActionWhenPress1},
198
 
    {"ActionWhenPress3", (XtActionProc) ActionWhenPress3},
199
 
    {"ActionWhenRelease3", (XtActionProc) ActionWhenRelease3},
200
 
    {"ActionWhenReturn", (XtActionProc) ActionWhenReturn}
201
 
  };
202
 
  GC gc, gc_clear, gc_xor;
203
 
  XGCValues gcv;
204
 
  Display *dpy;
205
 
  Drawable d;
206
 
  Pixel bg, fg;
207
 
  XFontStruct *fontstruct;
208
 
  XColor x_fg_color,x_bg_color;
209
 
  Widget look;
210
 
  int i;
211
 
  char *iniG;
212
 
  double iniS;
213
 
  float v;
214
 
  int igeci = -1;
215
 
  static char metanetgeom[200];
216
 
  XSetWindowAttributes attributes;
217
 
  int screen;
218
 
  unsigned int max_width,max_height;
219
 
  int geom_x, geom_y, dw, dh;
220
 
  XSizeHints sizehints;
221
 
 
222
 
  iniG = NULL;
223
 
 
224
 
  /* Is xmetanet called by Scilab? */
225
 
 
226
 
  igeci = find("-pipes",argc,argv);
227
 
  if (igeci != -1) {
228
 
    init_messages(tb_messages, atoi(argv[igeci+1]),atoi(argv[igeci+2]));
229
 
    isServeur = 1;
230
 
    for (i = igeci; i < argc - 3 ; i++) {
231
 
      if (argv[i+3] != NULL) argv[i] = argv[i+3];
232
 
      else argv[i] = NULL;
233
 
    }
234
 
    argc -= 3;
235
 
  }
236
 
  else isServeur = 0;
237
 
 
238
 
  /* Parse arguments when called by Scilab */
239
 
  dw = 0; dh = 0;
240
 
  if (isServeur) {
241
 
    sscanf(argv[2],"%d",&theWindow);
242
 
    iniG = argv[3];
243
 
    sscanf(argv[4],"%d",&dw);
244
 
    sscanf(argv[5],"%d",&dh);
245
 
  }
246
 
  else {
247
 
    theWindow = 0;
248
 
  }
249
 
 
250
 
  if (isServeur) {
251
 
    metaWidth = IMETAWIDTH;
252
 
    metaHeight = IMETAHEIGHT;
253
 
  } else {
254
 
    metaWidth = METAWIDTH;
255
 
    metaHeight = METAHEIGHT;
256
 
  }
257
 
 
258
 
/* Fallback ressources */
259
 
 
260
 
  sprintf(metanetgeom,"Metanet.geometry:+%d+%d",
261
 
          X + DX * theWindow,Y + DY * theWindow);
262
 
  fallback_resources[0]=metanetgeom;
263
 
 
264
 
/* Toplevel widget */
265
 
 
266
 
  toplevel = XtAppInitialize(&app_con, (String)"Metanet", NULL, 0, 
267
 
                             (int*)&argc, (String*)argv,
268
 
                             fallback_resources, NULL, 0);
269
 
 
270
 
/* Get ressources */
271
 
  XtGetApplicationResources(toplevel, &the_res, app_resources,
272
 
                            XtNumber(app_resources), NULL, 0);
273
 
 
274
 
  if (dw == 0 && dh == 0) {
275
 
    drawWidth = the_res.draw_width;
276
 
    drawHeight = the_res.draw_height;
277
 
  } else {
278
 
    drawWidth = dw;
279
 
    drawHeight = dh;
280
 
  }
281
 
/* Minimum 100 x 100 for Metanet draw */ 
282
 
  drawWidth = max(drawWidth,100);
283
 
  drawHeight = max(drawHeight,100);
284
 
  
285
 
  XParseGeometry(the_res.geometry,
286
 
                 &geom_x, &geom_y, &metaWidth, &metaHeight);
287
 
 
288
 
/* Parse xmetanet arguments */
289
 
 
290
 
  iniS = -1.0;
291
 
  if (!isServeur) {
292
 
    if (argc == 2) {
293
 
      if (!strcmp(argv[1],"-usage") || !strcmp(argv[1],"-help")) {
294
 
        PrintUsage();
295
 
        exit(0);
296
 
      }
297
 
      if (!strcmp(argv[1],"-version")) {
298
 
        printf("Version %s\n",Version);
299
 
        exit(0);
300
 
      }
301
 
    }
302
 
    if (argc % 2 == 1) iniG = 0;
303
 
    else iniG = argv[argc-1];
304
 
    for (i = 1; i < argc - 1; i++) {
305
 
      if (!strcmp(argv[i++],"-scale")) {
306
 
        if (sscanf(argv[i],"%g",&v) > 0)
307
 
          if (v > 0) iniS = (double)v;
308
 
      }
309
 
      else {
310
 
        fprintf(stderr,"Bad arguments\n");
311
 
        PrintUsage();
312
 
        exit(1);
313
 
      }
314
 
    }
315
 
  }
316
 
 
317
 
  if (isServeur) {
318
 
    XtAppAddTimeOut (app_con,
319
 
                     TEMPS,
320
 
                     (XtTimerCallbackProc) clock_tic,
321
 
                     (XtPointer)toplevel) ;
322
 
  }
323
 
  
324
 
  XtAppAddActions(app_con,actionTable,XtNumber(actionTable));
325
 
 
326
 
  sprintf(metanetName,"Metanet %s",Version);
327
 
 
328
 
  SetTitle(BEGIN);
329
 
 
330
 
  dpy = XtDisplay(toplevel);
331
 
  theG.dpy = dpy;
332
 
  screen = DefaultScreen(dpy);
333
 
 
334
 
/* Dimension of xmetanet window */
335
 
 
336
 
  dpyWidth = DisplayWidth(dpy,screen);
337
 
  dpyHeight = DisplayHeight(dpy,screen);
338
 
 
339
 
  max_width = (int)(dpyWidth * 0.8);
340
 
  max_height = (int)(dpyHeight * 0.8);
341
 
 
342
 
  if (max_width < metaWidth) metaWidth = max_width;
343
 
  if (max_height < metaHeight) metaHeight = max_height;
344
 
 
345
 
  iargs = 0;
346
 
  XtSetArg( args[iargs], XtNheight, metaHeight); iargs++;
347
 
  XtSetArg( args[iargs], XtNwidth, metaWidth); iargs++;
348
 
  XtSetArg( args[iargs], XtNx, X); iargs++;
349
 
  XtSetArg( args[iargs], XtNy, Y); iargs++;
350
 
  XtSetArg( args[iargs], XtNdefaultDistance, 0); iargs++;
351
 
  frame = XtCreateManagedWidget( "form", formWidgetClass, toplevel,
352
 
                                args, iargs);
353
 
  XtAddCallback(frame, XtNdestroyCallback, (XtCallbackProc)Destroyed, NULL );
354
 
 
355
 
  if ((fontstruct = XLoadQueryFont(dpy, METAFONT)) == NULL) {
356
 
    fprintf(stderr,"Font %s is unknown\n",METAFONT);
357
 
    fprintf(stderr,"I'll use font: fixed\n");
358
 
    if ((fontstruct = XLoadQueryFont(dpy, "fixed")) == NULL) {
359
 
      fprintf(stderr,"Unknown font: fixed\n");
360
 
      exit(1);
361
 
    }
362
 
  }
363
 
 
364
 
  theG.metafont = fontstruct;
365
 
  
366
 
  if ((fontstruct = XLoadQueryFont(dpy, HELPFONT)) == NULL) {
367
 
    fprintf(stderr,"Font %s is unknown\n",HELPFONT);
368
 
    fprintf(stderr,"I'll use font: fixed\n");
369
 
    if ((fontstruct = XLoadQueryFont(dpy, "fixed")) == NULL) {
370
 
      fprintf(stderr,"Unknown font: fixed\n");
371
 
      exit(1);
372
 
    }
373
 
  }
374
 
 
375
 
  theG.helpfont = fontstruct;
376
 
 
377
 
  CreateMenus();
378
 
  MakeDraw();
379
 
                                  
380
 
  XtRealizeWidget(toplevel);
381
 
 
382
 
  d = XtWindow(metanetDraw);
383
 
 
384
 
  attributes.backing_store = Always;
385
 
  XChangeWindowAttributes(dpy,d,CWBackingStore,&attributes);
386
 
 
387
 
/* Window Manager size hints */
388
 
  sizehints.flags = PMinSize | USPosition | PPosition;
389
 
  sizehints.x = X + DX * theWindow;
390
 
  sizehints.y = Y + DY * theWindow;
391
 
  sizehints.min_width = MINWIDTH;
392
 
  sizehints.min_height = MINHEIGHT;
393
 
  XSetNormalHints(dpy,XtWindow(toplevel),&sizehints);
394
 
 
395
 
  /* get color pixels */
396
 
  for (i = 0; i < NUMCOLORS - 2; i++) Colors[i+1] = the_res.color[i];
397
 
 
398
 
  /* get foreground and background pixels and colors */
399
 
  XtSetArg(args[0],XtNlabel,"");
400
 
  look = XtCreateWidget("look", labelWidgetClass, toplevel, args, 1);
401
 
 
402
 
  XtSetArg(args[0],XtNforeground, &x_fg_color.pixel);
403
 
  XtSetArg(args[1],XtNbackground, &x_bg_color.pixel);
404
 
  XtGetValues(look,args,2);
405
 
  fg = x_fg_color.pixel;
406
 
  bg = x_bg_color.pixel;
407
 
  Colors[0] = fg;
408
 
  Colors[NUMCOLORS-1] = bg;
409
 
 
410
 
  XtDestroyWidget(look);
411
 
 
412
 
  /* GC */
413
 
  GetFonts();
414
 
  fontstruct = FontSelect(DRAWFONTSIZE);
415
 
  gcv.font = fontstruct->fid;
416
 
 
417
 
  gcv.foreground = fg;
418
 
  gcv.background = bg;
419
 
  gcv.function = GXcopy; 
420
 
  gc = XCreateGC(dpy,d,
421
 
                 (GCFont | GCForeground | GCBackground | GCFunction),&gcv);
422
 
 
423
 
  /* GC Clear */
424
 
  gcv.foreground = bg;
425
 
  gcv.background = bg;
426
 
  gcv.function = GXcopy;
427
 
  gc_clear = XCreateGC(dpy,d,
428
 
                       (GCFont | GCForeground | GCBackground | GCFunction),
429
 
                       &gcv);
430
 
 
431
 
  /* GC Xor */
432
 
  gcv.foreground = fg ^ bg; 
433
 
  gcv.background = bg;
434
 
  gcv.function = GXxor;
435
 
  gcv.line_width = 1;
436
 
  gc_xor = XCreateGC(dpy,d,(GCForeground | GCBackground | GCFunction),&gcv);
437
 
  
438
 
  XDefineCursor(dpy,d,XCreateFontCursor(dpy,XC_draft_small));
439
 
  
440
 
  theG.d = d;
441
 
  theG.bg = bg;
442
 
  theG.fg = fg;
443
 
  theG.gc = gc;
444
 
  theG.gc_clear = gc_clear;
445
 
  theG.gc_xor = gc_xor;
446
 
  theG.drawfont = fontstruct;
447
 
  theG.shell = NULL;
448
 
 
449
 
  theColor = fg;
450
 
 
451
 
  InitMetanet(iniG,iniS);
452
 
 
453
 
  XtMapWidget(toplevel);
454
 
 
455
 
  XtAppMainLoop(app_con);
456
 
 
457
 
  exit(0);
458
 
}
459
 
 
460
 
static void clock_tic(client_data,id)
461
 
XtPointer client_data;
462
 
XtIntervalId id;
463
 
{
464
 
  scanner_messages();
465
 
  XtAppAddTimeOut (app_con,
466
 
                   TEMPS,
467
 
                   (XtTimerCallbackProc) clock_tic,
468
 
                   (XtPointer)toplevel);
469
 
}
470
 
 
471
 
static void erreur_message_msgact(message)
472
 
Message message;
473
 
{
474
 
  fprintf(stderr, "Message  recu incorrect !!!\n");
475
 
  envoyer_message_parametres_var(ID_GeCI, MSG_FIN_APPLI, NULL);
476
 
  exit(1);
477
 
}
478
 
 
479
 
static void quitter_appli_msgact(message)
480
 
Message message;
481
 
{  
482
 
  exit(0);
483
 
}
484
 
 
485
 
static int find(s,n,t)
486
 
char *s;
487
 
int n;
488
 
char **t;
489
 
{
490
 
  int i;
491
 
  for (i=0; i<n; i++)
492
 
    if (!strcmp(s,t[i])) return(i);
493
 
  return(-1);
494
 
}
495
 
 
496
 
static void PrintUsage()
497
 
{
498
 
  printf("Usage: xmetanet [OPTION]... GRAPH_FILE");
499
 
}
500
 
 
501
 
void MetanetQuit()
502
 
{
503
 
  XtUnmapWidget(toplevel);
504
 
  exit(0);
505
 
}
506
 
 
507
 
void GetMetanetGeometry(x,y,w,h)
508
 
int *x,*y,*w,*h;
509
 
{
510
 
  Window root,parent,*children;
511
 
  unsigned int nchildren;
512
 
  XWindowAttributes war;
513
 
  XQueryTree(theG.dpy,XtWindow(toplevel),&root,&parent,&children,&nchildren);
514
 
  if (parent != root) XGetWindowAttributes(theG.dpy,parent,&war);
515
 
  else XGetWindowAttributes(theG.dpy,XtWindow(toplevel),&war);
516
 
  *h = war.height; *w = war.width; *x = war.x; *y = war.y;
517
 
}