File: init_windows.c

package info (click to toggle)
quickplot 0.4.3-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 352 kB
  • ctags: 276
  • sloc: ansic: 4,124; makefile: 124; sh: 12
file content (437 lines) | stat: -rw-r--r-- 13,581 bytes parent folder | download | duplicates (2)
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
/***************************************************************
LanceMan's quickplot --- a fast interactive 2D plotter

Copyright (C) 1998, 1999  Lance Arsenault

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; version 2
of the License.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

Or look at http://www.gnu.org/copyleft/gpl.html .

**********************************************************************/
/* $Id: init_windows.c,v 1.6.1.2 1999/02/27 04:21:59 lance Exp $ */
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>

#include "xwin.h"
#include "data.h"

#include <X11/Intrinsic.h>
#include <X11/Shell.h>
#include <X11/StringDefs.h>
#include <X11/Xaw/Box.h>
#include <X11/Xaw/Command.h>
#include <X11/Xaw/Simple.h>
#include <X11/Xaw/Viewport.h>
#include <X11/Xaw/Dialog.h>

#include <X11/Xaw/AsciiText.h>
#include <X11/Xaw/Form.h>
#include <X11/cursorfont.h>


#define WIN_TITLE_SIZE 200
#define ICON_TITLE_SIZE 40
#define DUMMY_STRING "                                                                                   "

extern int write_pixmap(PlotXwins *win, Plot *plot);
extern int get_window_size(Display *display, Window window, 
				 unsigned int *width, 
				 unsigned int *height);
extern unsigned long GetNamedColor(Display *dpy, Colormap cmap, char *name);
extern void draw_plot(PlotXwins *win,Plot *plot);
extern void SetIconImage(Widget w, Boolean flag);

static struct
{
  PlotXwins *win;
  Plot *plot;
} CallBackData;

#if(0)
static Pixel get_pix_color(PlotXwins *win, char *color)
{
  Pixel pix;

  pix = GetNamedColor(win->display,
		      DefaultColormap(win->display,win->screen_number),
		      color);
  return pix;
}
#endif

static int Get_XColor(PlotXwins *win, char *color, XColor *xcolor)
{
  if(XParseColor(win->display,DefaultColormap(win->display,win->screen_number),
		 color, xcolor) == 0 ||
     XAllocColor(win->display,DefaultColormap(win->display,win->screen_number),
		 xcolor) == 0)
    {
      /* fprintf(stderr,"quickplot Info: Can't get xcolor \"%s\" in %s line %d.\n",
	    __FILE__,__LINE__);*/
      return 0;
    }
  return 1;
  
}

static void Get_BackgroundColor(PlotXwins *win, Widget widget, char *color_return)
{
  Pixel pix;
  XtVaGetValues(widget, XtNbackground, &pix, NULL);
  if(pix == BlackPixel(win->display, win->screen_number))
      sprintf(color_return,"white");
  else
      sprintf(color_return,"black");
}


static void quit_callback(Widget w, XtPointer appdata, XtPointer calldata)
{
  exit(0);
}

static void redraw_callback(Widget w, XtPointer appdata, XtPointer calldata)
{
  draw_plot(CallBackData.win,CallBackData.plot);
}

static void save_callback(Widget w, XtPointer appdata, XtPointer calldata)
{
  write_pixmap(CallBackData.win,CallBackData.plot);
}


static void handle_wm_message(Widget w, XtPointer client, Boolean *dispatch)
{
  if(XtIsRealized(((PlotXwins *)client)->popWig))
    XtUnrealizeWidget(((PlotXwins *)client)->popWig);
}

int check_popWig(PlotXwins *win, Plot *plot)
{
  int i;
  Atom xa_wm_protocol, protocol[1];

  if(XtIsRealized(win->popWig))
    return 0;
  else
    {
      XtRealizeWidget(win->popWig);
      for(i=0;i<plot->num_plots;i++)
        XtVaSetValues(win->valueWig[i],XtNforeground, plot->line_color[i],NULL);
      xa_wm_protocol = XInternAtom(XtDisplay(win->popWig), "WM_PROTOCOLS",False);
      protocol[0] = XInternAtom(XtDisplay(win->popWig), "WM_DELETE_WINDOW",False);
      XSetWMProtocols(XtDisplay(win->popWig), XtWindow(win->popWig),
		      protocol, XtNumber(protocol));
      XtAddEventHandler(win->popWig, NoEventMask, True,
			(XtEventHandler) handle_wm_message,
			(XtPointer) win );
      return 1;
    }
}

static void mode_callback(Widget w, XtPointer appdata, XtPointer calldata)
{
  if((CallBackData.win)->mode & VALUE_MODE)
    {
      XtVaSetValues(w,XtNlabel, "Non-Interpolated ",NULL);
      (CallBackData.win)->mode = EDIT_MODE;
    }
  else
    {
      XtVaSetValues(w,XtNlabel, "  Interpolated  ",NULL);
      (CallBackData.win)->mode = VALUE_MODE;
    }
}

void init_windows(int argc, char **argv, PlotXwins *win, Plot *plot)
{
  int i;
  unsigned int  width, height;
  char win_title[WIN_TITLE_SIZE + 1], icon_title[ICON_TITLE_SIZE];
  Cursor gumby_cursor;
  XColor xgreen, xbg;
  Widget boxWig,viewportWig;
  char str5[6];
  char *fallback_resources[6];
  char str0[] = {"quickplot*reverseVideo: true"};
  char str1[] = {"quickplot*plot.width:          500"};
  char str2[] = {"quickplot*plot.height:          500"};
  char str3[] = {"quickplotvalues.width:          600"};
  char str4[] = {"quickplotvalues.height:          100"};

  fallback_resources[0] = str0;
  fallback_resources[1] = str1;
  fallback_resources[2] = str2;
  fallback_resources[3] = str3;
  fallback_resources[4] = str4;
  fallback_resources[5] = NULL;

  CallBackData.plot = plot;
  CallBackData.win = win;

  if(plot->infiles[0] == NULL)
    {
      sprintf(win_title,"LANCEMAN'S QuickPlot");
      sprintf(icon_title,"QuickPlot");
    }
  else
    {
      win_title[0] = '\0';
      icon_title[0] = '\0';
      strncat(win_title,"QuickPlot file =",WIN_TITLE_SIZE - strlen(win_title));
      for(i=0;plot->infiles[i] != NULL;i++)
	{	 
	  strncat(win_title," ",WIN_TITLE_SIZE - strlen(win_title));
	  strncat(win_title,plot->infiles[i],WIN_TITLE_SIZE - 
		  strlen(win_title));
	  if(i>0) strncat(icon_title," ",ICON_TITLE_SIZE - strlen(icon_title));
	  strncat(icon_title,plot->infiles[i],ICON_TITLE_SIZE - 
		  strlen(icon_title));
	}
    }
  /* Open in then close the display just to get the
   * width and height of the root window.
   * Is there a better way?
   */
  if((win->display = XOpenDisplay((char *)NULL)) == (Display*) NULL)
    {
      fprintf(stderr,"quickplot ERROR: Can't open display\n");
      exit(1);
    }
  get_window_size(win->display, DefaultRootWindow(win->display), 
		  &width, &height);
  XCloseDisplay(win->display);

  XtToolkitInitialize();
  win->context = XtCreateApplicationContext();

  /* setup plot and value window size */
  width = (width > 1000)?600:2*width/3;
  height = (height > 900)?500:height/2;

  sprintf(&(fallback_resources[1][22]),"%d",width);
  sprintf(&(fallback_resources[2][23]),"%d",height);
  sprintf(&(fallback_resources[3][23]),"%d",width);
  sprintf(&(fallback_resources[4][24]),"%d",height/3);

  /* for(i=0;i<5;i++)
     printf("fallback_resources[%d] = <%s>\n",i,fallback_resources[i]); */

  XtAppSetFallbackResources(win->context,fallback_resources);

  if((win->display = XtOpenDisplay(win->context,"", "quickplot",
				       "Quickplot",
                          NULL, 0, &argc, argv)) == (Display*) NULL)
    {
      fprintf(stderr,"quickplot ERROR: Can't open display\n");
      exit(1);
    }
 
  win->screen_number = DefaultScreen(win->display);

  win->toplevelWig = XtVaAppCreateShell("quickplot", "QuickPlot",
					applicationShellWidgetClass,
					win->display,
					XtNborderWidth,0,
					XtNtitle, win_title,
					XtNiconName, icon_title,
					NULL);
  SetIconImage(win->toplevelWig, True);

  win->managerWig = XtVaCreateManagedWidget("manager", formWidgetClass,
					    win->toplevelWig,
					    XtNdefaultDistance, 0,
					    XtNborderWidth,0 ,
					    NULL);

  win->menuWig = XtVaCreateManagedWidget( "menu" , formWidgetClass,
					  win->managerWig,
					  XtNborderWidth,0 ,
					  XtNleft, XtChainLeft,
					  XtNright, XtChainLeft,
					  XtNbottom, XtChainTop,
					  XtNtop, XtChainTop,
					  NULL);

  gumby_cursor = XCreateFontCursor(win->display,XC_gumby);

  win->quitWig = XtVaCreateManagedWidget( "quit", commandWidgetClass,
					  win->menuWig,
					  XtNlabel, "Quit",
					  XtNleft, XtChainLeft,
					  XtNright, XtChainLeft,
					  XtNcursor, gumby_cursor,
 /* why is gumby_green not working */ /*  XtNpointerColor, gumby_green(win),*/
					  NULL);

  win->modeWig = XtVaCreateManagedWidget( "mode", commandWidgetClass,
					  win->menuWig,
					  XtNfromHoriz, win->quitWig,
					  XtNlabel, "  Interpolated  ",
					  XtNleft, XtChainLeft,
					  XtNright, XtChainLeft,
					  NULL);

  win->redrawWig = XtVaCreateManagedWidget( "redraw", commandWidgetClass,
					  win->menuWig,
					  XtNfromHoriz, win->modeWig,
					  XtNlabel, "Redraw",
					  XtNleft, XtChainLeft,
					  XtNright, XtChainLeft,
					  NULL);

  win->saveButtonWig = XtVaCreateManagedWidget( "savebutton", commandWidgetClass,
					  win->menuWig,
					  XtNfromHoriz, win->redrawWig,
					  XtNlabel, " Save XPM ",
					  XtNleft, XtChainLeft,
					  XtNright, XtChainLeft,
					  NULL);

  win->saveInputWig = XtVaCreateManagedWidget("savefilename",asciiTextWidgetClass,
					      win->menuWig,
					      XtNstring, "qp0.xpm",
					      XtNleft, XtChainLeft,
					      XtNright, XtChainRight,
      					      XtNresizable, True,
					      XtNfromHoriz, win->saveButtonWig,
					      XtNresize, XawtextResizeWidth,
					      XtNdisplayCaret, True,
					      XtNeditType, XawtextEdit,
					      NULL);
  win->savefile_count = 0;

  win->plotWig = XtVaCreateManagedWidget("plot",simpleWidgetClass,
					 win->managerWig,
					 XtNfromVert, win->menuWig,
					 XtNright, XtChainRight,
					 XtNtop, XtChainTop,
					 XtNleft, XtChainLeft,
					 XtNbottom, XtChainBottom,
					 XtNborderWidth,2,
					 
					 NULL);

  XtRealizeWidget(win->toplevelWig);

  /***** get BLACK AND WHITE background and foreground Colors ******/
  /*** this is in case the -/+rv option inverse video is given *********/
  XtVaGetValues(win->plotWig, XtNbackground, &(win->BWbackground_pix), NULL);
  if(win->BWbackground_pix == (win->BWforeground_pix =
			       BlackPixel(win->display, win->screen_number)))
     win->BWforeground_pix = WhitePixel(win->display, win->screen_number);
  
#if(1)
  /* make quit cursor gumby green */
  if(Get_XColor(win,"green", &xgreen))
    {
      Get_BackgroundColor(win,win->quitWig,str5);
      if(Get_XColor(win,str5, &xbg))
	XRecolorCursor(win->display, gumby_cursor, &xbg ,&xgreen);
    }
#else /* this dosen't work on my machine */
  XtVaSetValues(win->modeWig,XtNpointerColorBackground, get_pix_color(win,"green"));
#endif

  win->mode = VALUE_MODE; /* default mouse mode */
  /* if there is a phase plot or one of the 
   * function plots does not have the same x-values 
   * then disable the editing mode.
   */
  for(i=0;i<plot->num_plots;i++)
    if((plot->plot_opt[i] & PHASE_PLOT) ||
       (plot->list[i][X] != plot->list[0][X]))
      break;
  if(i == plot->num_plots) /* no phase plots */
    {
      XtAddCallback(win->modeWig, XtNcallback, mode_callback,NULL);
    }
  else
    XtVaSetValues(win->modeWig,XtNsensitive,False,NULL);

  XtAddCallback( win->quitWig, XtNcallback, quit_callback, NULL);
  XtAddCallback( win->redrawWig, XtNcallback, redraw_callback, NULL);
  XtAddCallback( win->saveButtonWig, XtNcallback, save_callback, NULL);

  /*  XtVaSetValues(win->redrawWig,XtNsensitive,False,NULL);*/

  XSelectInput(win->display,win->plotwin = XtWindow(win->plotWig),
	       ExposureMask | ButtonPressMask | StructureNotifyMask |
               PointerMotionMask | PointerMotionHintMask | ButtonReleaseMask);

  /* get a font */
  if(NULL == (win->font_struct=XLoadQueryFont(win->display,"fixed")))
    {
      fprintf(stderr,"quickplot ERROR: opening font failed.\n");
      XtCloseDisplay(win->display);
      exit(1);
    }

  win->gc = DefaultGC(win->display,win->screen_number);
  XSetFont(win->display,win->gc,win->font_struct->fid);

  /********************* make the quickplot values windows *********************/

  strncat(win_title," -- Values Window",WIN_TITLE_SIZE - strlen(win_title));
  win->popWig = XtVaAppCreateShell("quickplotvalues" , "QuickPlotValues",
				   applicationShellWidgetClass,win->display,
				   XtNtitle, win_title,
				   XtNiconName, "Values",
				   NULL);

  SetIconImage(win->popWig, True);

  viewportWig = XtVaCreateManagedWidget("valueveiwport", viewportWidgetClass,
					win->popWig,
					XtNallowHoriz, True,
					XtNallowVert, True,
					NULL);
  boxWig = XtVaCreateManagedWidget("valuebox", formWidgetClass, viewportWig, NULL);

  assert(NULL != (win->valueWig = (Widget *) malloc(plot->num_plots*sizeof(Widget))));
 win->valueWig[0] = XtVaCreateManagedWidget("value",asciiTextWidgetClass,
					    boxWig,
					    XtNstring,DUMMY_STRING,
					    XtNborderWidth,0,
					    XtNleft, XtChainLeft,
					    XtNright, XtChainLeft,
					    XtNbottom, XtChainTop,
					    XtNtop, XtChainTop,
					    XtNresizable, True,
					    XtNresize, XawtextResizeWidth,
					    XtNdisplayCaret, False,
					    NULL);

  for(i=1;i<plot->num_plots;i++)
    win->valueWig[i] = XtVaCreateManagedWidget("value",asciiTextWidgetClass,
					       boxWig,
					       XtNstring,DUMMY_STRING,
					       XtNborderWidth,0,
					       XtNleft, XtChainLeft,
					       XtNright, XtChainLeft,
					       XtNbottom, XtChainTop,
					       XtNtop, XtChainTop,
					       XtNresizable, True,
					       XtNfromVert, win->valueWig[i-1],   
					       XtNresize, XawtextResizeWidth,
					       XtNdisplayCaret, False,
					       NULL);
  win->popflag = 0;

}