File: draw_zoom_box.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 (214 lines) | stat: -rw-r--r-- 6,807 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
/***************************************************************
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: draw_zoom_box.c,v 1.3 1999/01/15 02:19:14 lance Exp $ */
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include "data.h"
#include "xwin.h"

GC band_gc;


extern int get_window_size(Display *display, Window window, 
				 unsigned int *width, 
				 unsigned int *height);
/* setup gc for rubber bands */
void setup_rubber_bands(PlotXwins *win,Plot *plot)
{
  XGCValues gcvalues;

  gcvalues.background = 0UL;
  gcvalues.foreground = WhitePixel(win->display,win->screen_number);
  gcvalues.function = GXxor;

  band_gc = XCreateGC(win->display,win->plotwin,
		      GCFunction | GCForeground | GCBackground,
		      &gcvalues);
}

/* Blocks until it get an event */
/* returns 0 if Button 2 is pressed or released else returns 1 */
static int look_for_ButtonRelease(PlotXwins *win)
{
  XEvent event;

  XWindowEvent(win->display,win->plotwin,
	       PointerMotionMask | ButtonReleaseMask | ButtonPressMask,&event);
  switch(event.type)
    {
    case ButtonPress:
    case ButtonRelease:
      if(event.xbutton.button == Button2)
	return 0;
      return 1;
    default:
      return 1;
    }
}

/* returns 1 on failure */
int get_pointer_postion(PlotXwins *win, int *xi, int *yi)
{
  Window root_return, child_return;
  int root_x_return, root_y_return;
  unsigned int mask_return;

  if(XQueryPointer(win->display,win->plotwin,&root_return,&child_return,
                   &root_x_return, &root_y_return, xi,yi,&mask_return))
    return 0;
  return 1;
}


void draw_rubber_bands(PlotXwins *win, int *xout, int *yout, 
		       unsigned int *widthout, unsigned int *heightout)
{
  int xi,yi,x0,y0,x,y;
  unsigned int width0, width, height0, height;


  if(get_pointer_postion(win,&xi,&yi))
    {
      fprintf(stderr,"quickplot ERROR: can't get pointer position in file %s line %d\n",
	      __FILE__,__LINE__);
      return;
    }
  XDrawRectangle(win->display,win->plotwin,band_gc, xi, yi,
		 width=width0=0, height=height0=0);
  XFlush(win->display);
  x0 = x = xi; y0 = y = yi;
  while(look_for_ButtonRelease(win))
    {
      if(get_pointer_postion(win,&x,&y))
	{
	  fprintf(stderr,"quickplot ERROR: can't get pointer position: file %s line %d\n",
		  __FILE__,__LINE__ -3);
	  return;
	}
      if(x != x0 || y != y0)
	{
	  if(x > xi)
	    {
	      width = x - xi;
	      x = xi;
	    }
	  else
	    width = xi - x;
	  if(y > yi)
	    {
	      height = y - yi;
	      y = yi;
	    }
	  else
	    height = yi - y;
	  XDrawRectangle(win->display,win->plotwin,band_gc, x0, y0,width0, height0);
	  XDrawRectangle(win->display,win->plotwin,band_gc, x, y,width, height);
	  x0 = x; y0 = y;  width0 = width; height0 = height;
	  XFlush(win->display);
	}
    }
  XDrawRectangle(win->display,win->plotwin,band_gc, x0, y0,width0, height0);
  XFlush(win->display);
  *xout = x0; *yout = y0; *widthout = width0; *heightout = height0;
}

int zoom_plot(PlotXwins *win, Plot *plot, int boxx, int boxy,
		      unsigned int boxwidth, unsigned int boxheight)
{
  unsigned int width,height;
  int i,j;

  get_window_size(win->display,win->plotwin,&width,&height);

  if((boxx < 0 || boxx+boxwidth > width) && (boxy < 0 || boxy+boxheight > height))
    {
      plot->zoom_count = 0;
      return 1;
    }
  else if(boxx < 0 || boxy < 0 || boxx+boxwidth > width || boxy+boxheight > height)
    {
      plot->zoom_count -= (plot->zoom_count > 0)? 1:0;
      return 1;
    }
  else if(boxwidth == 0 || boxheight == 0)
    return 0;
  else
    {
      plot->zoom_count++;
      if(plot->zoom_count >= plot->num_zoom)
	{ 
	  /*** get more zoom variables */
	  plot->zoom_count = plot->num_zoom;
	  plot->num_zoom += DEFAULT_NUM_ZOOM;
	  assert((plot->z_scale=(double **)realloc(plot->z_scale,
			      sizeof(double *)*plot->num_zoom))!=NULL);
	  assert((plot->z_shift=(double **)realloc(plot->z_shift,
			      sizeof(double *)*plot->num_zoom))!=NULL);

	  assert((plot->start=(struct Data ****)realloc(plot->start,
                sizeof(struct Data ***)*plot->num_zoom))!=NULL);
	  assert((plot->end  =(struct Data ****)realloc(plot->end,
                sizeof(struct Data ***)*plot->num_zoom))!=NULL);
	  for(i=plot->zoom_count;i<plot->num_zoom;i++)
	    {
	      assert((plot->z_scale[i]=(double *)malloc(sizeof(double)*2))!=NULL);
	      assert((plot->z_shift[i]=(double *)malloc(sizeof(double)*2))!=NULL);

	      assert((plot->start[i]=(struct Data ***)malloc(
                     sizeof(struct Data **)*plot->num_plots))!=NULL);
	      assert((plot->end[i]  =(struct Data ***)malloc(
                     sizeof(struct Data **)*plot->num_plots))!=NULL);
	      for(j=0;j<plot->num_plots;j++)
		{
		  assert((plot->start[i][j]=(struct Data **)malloc(
			        sizeof(struct Data *)*2))!=NULL);
		  assert((plot->end[i][j]  =(struct Data **)malloc(
				sizeof(struct Data *)*2))!=NULL);
		}
	    }
	}
      plot->z_scale[plot->zoom_count][X] = 
	plot->z_scale[plot->zoom_count-1][X]*width/(boxwidth*(2.0-FRACTION_WINDOW_USE));
      plot->z_scale[plot->zoom_count][Y] = 
        plot->z_scale[plot->zoom_count-1][Y]*height/(boxheight*(2.0-FRACTION_WINDOW_USE));

      plot->z_shift[plot->zoom_count][X] = 
	(plot->z_scale[plot->zoom_count][X]/plot->z_scale[plot->zoom_count-1][X])*
	(plot->z_shift[plot->zoom_count-1][X]
	- boxx + (1.0 - FRACTION_WINDOW_USE)*boxwidth/2.0);

      plot->z_shift[plot->zoom_count][Y] =
	(plot->z_scale[plot->zoom_count][Y]/plot->z_scale[plot->zoom_count-1][Y])*
	(plot->z_shift[plot->zoom_count-1][Y]
	- boxy + (1.0 - FRACTION_WINDOW_USE)*boxheight/2.0);
      for(j=0;j<plot->num_plots;j++)
	{
	  plot->start[plot->zoom_count][j][X] = plot->start[plot->zoom_count - 1][j][X];
	  plot->start[plot->zoom_count][j][Y] = plot->start[plot->zoom_count - 1][j][Y];
	  plot->end[plot->zoom_count][j][X] = plot->end[plot->zoom_count - 1][j][X];
	  plot->end[plot->zoom_count][j][Y] = plot->end[plot->zoom_count - 1][j][Y];
	}
      return 1;
    }
}