File: button_press.c

package info (click to toggle)
garlic 1.6-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 4,516 kB
  • sloc: ansic: 52,465; makefile: 2,254
file content (286 lines) | stat: -rw-r--r-- 8,766 bytes parent folder | download | duplicates (5)
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
/* Copyright (C) 2000-2002 Damir Zucic */

/*=============================================================================

				button_press.c

Purpose:
	Handle ButtonPress events (mouse buttons).

Input:
	(1) Pointer to MolComplexS structure, with macromol. complexes.
	(2) Number of macromolecular complexes.
	(3) Pointer to RuntimeS structure.
	(4) Pointer to ConfigS structure, with configuration data.
	(5) Pointer to GUIS structure, with GUI data.
	(6) Pointer to NearestAtomS structure.
	(7) The number of pixels in the main window free area.
	(8) Pointer to refreshI.
	(9) Pointer to XButtonEvent structure.

Output:
	(1) If the  click_modeI is equal to zero and the click occured in
	    the main window, the interatomic distances and angles will be
	    calculated.  If the click_modeI is zero and the click occured
	    in the control window,  it will cause translation,  rotation,
	    slab change or fading change.  If the click_modeI is equal to
	    one, the click will choose the bond for editing.
	(2) Return value.

Return value:
	(1) Positive on success.
	(2) Zero if event is ignored.
	(3) Negative on failure.

========includes:============================================================*/

#include <stdio.h>

#include <string.h>
#include <math.h>

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xos.h>
#include <X11/Xatom.h>

#include "defines.h"
#include "typedefs.h"

/*======function prototypes:=================================================*/

int		ControlClick_ (MolComplexS *, int,
			       RuntimeS *, ConfigS *, GUIS *,
			       NearestAtomS *, size_t, unsigned int *,
			       XButtonEvent *);
int		PickBond_ (MolComplexS *, int,
			   RuntimeS *, ConfigS *, GUIS *,
			   NearestAtomS *, size_t, unsigned int *,
			   NearestAtomS *);

/*======handle ButtonPress events:===========================================*/

int ButtonPress_ (MolComplexS *mol_complexSP, int mol_complexesN,
		  RuntimeS *runtimeSP, ConfigS *configSP, GUIS *guiSP,
		  NearestAtomS *nearest_atomSP, size_t pixelsN,
		  unsigned int *refreshIP,
		  XButtonEvent *button_eventSP)
{
int			screen_x, screen_y, height;
size_t			pixelI;
NearestAtomS		*curr_pixelSP;
int			n;
AtomS			*atomSP;
RawAtomS		*raw_atomSP;
static int		atoms_pickedN;
char			stringA[STRINGSIZE];
static char		label0A[SHORTSTRINGSIZE];
static char		label1A[SHORTSTRINGSIZE];
static char		label2A[SHORTSTRINGSIZE];
static char		label3A[SHORTSTRINGSIZE];
static double		x0, y0, z0, x1, y1, z1, x2, y2, z2, x3, y3, z3;
double			delta_x, delta_y, delta_z, distance_squared, distance;
char			formatA[100];
double			dx10, dy10, dz10, dx32, dy32, dz32;
double			abs10, abs32, scalar_product;
double			cos_angle, angle;

/* Check the main window drawing mode index: */
if (guiSP->main_window_modeI != 0) return 0;

/* If the event came from child window, call the corresponding function: */
if (button_eventSP->subwindow != None)
	{
	/** Control window: **/
	if (button_eventSP->subwindow == guiSP->control_winS.ID)
		{
		return ControlClick_ (mol_complexSP, mol_complexesN,
				      runtimeSP, configSP, guiSP,
				      nearest_atomSP, pixelsN, refreshIP,
				      button_eventSP);
		}
	}

/*------If the event came from the main window:------------------------------*/

/* Prepare and check the pixel index: */
screen_x = button_eventSP->x;
screen_y = button_eventSP->y;
pixelI = guiSP->main_win_free_area_width * screen_y + screen_x;
if (pixelI >= pixelsN) return -1;

/* Pointer to the current pixel: */
curr_pixelSP = nearest_atomSP + pixelI;

/* Chech last_refreshI - data associated with this pixel may be obsolete: */
if (curr_pixelSP->last_refreshI != *refreshIP) return 0;

/* Check the edit mode index; if it is equal to one, the */
/* click should be used  to choose the bond for editing: */
if (runtimeSP->click_modeI == 1)
	{
	n = PickBond_ (mol_complexSP, mol_complexesN,
		       runtimeSP, configSP, guiSP,
		       nearest_atomSP, pixelsN, refreshIP,
		       curr_pixelSP);
	if (n > 0) return 1;
	else return -2;
	}

/* If this point is reached, the atom was picked successfully: */
atoms_pickedN++;
if (atoms_pickedN > 4) atoms_pickedN = 4;

/* Pointer to the current atom: */
atomSP = (mol_complexSP + curr_pixelSP->mol_complexI)->atomSP +
	  curr_pixelSP->atomI;
raw_atomSP = &atomSP->raw_atomS;

/* Store the coordinates of the current atom: */
x0 = raw_atomSP->x[0];
y0 = raw_atomSP->y;
z0 = raw_atomSP->z[0];

/* Prepare the label: */
sprintf (stringA, "%s %d %s",
	 raw_atomSP->pure_residue_nameA,
	 raw_atomSP->residue_sequenceI,
	 raw_atomSP->pure_atom_nameA);
strncpy (label0A, stringA, SHORTSTRINGSIZE - 1);
label0A[SHORTSTRINGSIZE - 1] = '\0';

/* If coordinates are not available for at least two atoms, */
/* copy coordinates,  copy label,  set one flag and return: */
if (atoms_pickedN < 2)
	{
	x1 = x0;
	y1 = y0;
	z1 = z0;
	strncpy (label1A, label0A, SHORTSTRINGSIZE - 1);
	label1A[SHORTSTRINGSIZE - 1] = '\0';
	return 2;
	}

/* Prepare text background color: */
XSetForeground (guiSP->displaySP, guiSP->theGCA[1],
		guiSP->main_winS.bg_colorID);

/* Prepare text color: */
XSetForeground (guiSP->displaySP, guiSP->theGCA[0],
		guiSP->main_winS.fg_colorID);

/* Refresh text background: */
height = guiSP->main_winS.font_height + 4;
screen_y = guiSP->main_win_free_area_height - height;
XFillRectangle (guiSP->displaySP, guiSP->main_winS.ID, guiSP->theGCA[1],
		0, screen_y, guiSP->main_win_free_area_width, height);

/* Calculate distance to the atom which was previously clicked: */
delta_x = x0 - x1;
delta_y = y0 - y1;
delta_z = z0 - z1;
distance_squared = delta_x * delta_x + delta_y * delta_y + delta_z * delta_z;
distance = sqrt (distance_squared);

/* Write distance to the output window. Note that */
/* this  string is  not drawn  to hidden  pixmap! */
strcpy (formatA, "dist. = %.2f A [%s]-->[%s]");
sprintf (stringA, formatA, distance, label1A, label0A);
screen_x = 2;
screen_y = guiSP->main_win_free_area_height -
	   guiSP->main_winS.fontSP->descent - 2;
XDrawString (guiSP->displaySP,
	     guiSP->main_winS.ID,
	     guiSP->theGCA[0],
	     screen_x, screen_y,
	     stringA, strlen (stringA));

/* Calculate the angle, if coordinates for four atoms are available: */
if (atoms_pickedN >= 4)
	{
	/** Prepare text coordinates: **/
	height = guiSP->main_winS.font_height + 4;
	screen_y = guiSP->main_win_free_area_height - 2 * height;

	/** Refresh text background: **/
	XFillRectangle (guiSP->displaySP, guiSP->main_winS.ID,
			guiSP->theGCA[1],
			0, screen_y, guiSP->main_win_free_area_width, height);

	/** Calculate absolute values and scalar product: **/
	dx10 = x0 - x1;
	dy10 = y0 - y1;
	dz10 = z0 - z1;
	abs10 = sqrt (dx10 * dx10 + dy10 * dy10 + dz10 * dz10);
	dx32 = x2 - x3;
	dy32 = y2 - y3;
	dz32 = z2 - z3;
	abs32 = sqrt (dx32 * dx32 + dy32 * dy32 + dz32 * dz32);
	scalar_product = dx10 * dx32 + dy10 * dy32 + dz10 * dz32;

	/** Calculate and write the angle, if atoms were properly chosen: **/
	if ((abs10 != 0.0) && (abs32 != 0.0))
		{
		/*** Calculate the angle. Remember that arc cosine ***/
		/*** is very sensitive  to floating point  errors: ***/
		cos_angle = scalar_product / (abs10 * abs32);
		if (cos_angle <= -1.0) angle = 180.0;
		else if (cos_angle >= 1.0) angle = 0.0;
		else angle = RAD_TO_DEG * acos (cos_angle);

		/*** Write angle to the output window. Note that ***/
		/*** this string is not drawn  to hidden pixmap! ***/
		strcpy (formatA,
			"angle = %.2f deg [%s]-->[%s], [%s]-->[%s]");
		sprintf (stringA, formatA,
			 angle, label3A, label2A, label1A, label0A);
		screen_x = 2;
		screen_y = guiSP->main_win_free_area_height - height -
			   guiSP->main_winS.fontSP->descent - 2;
		XDrawString (guiSP->displaySP,
			     guiSP->main_winS.ID,
			     guiSP->theGCA[0],
			     screen_x, screen_y,
			     stringA, strlen (stringA));
		}

	/** On failure,  inform user  that  at least  four **/
	/** atoms should be picked to calculate the angle: **/
	else
		{
		screen_x = 2;
		screen_y = guiSP->main_win_free_area_height - height -
			   guiSP->main_winS.fontSP->descent - 2;
		strcpy (stringA, "The same atom picked twice!");
		XDrawString (guiSP->displaySP,
			     guiSP->main_winS.ID,
			     guiSP->theGCA[0],
			     screen_x, screen_y,
			     stringA, strlen (stringA));
		}
	}

/* Copy the coordinates and labels: */
x3 = x2;
y3 = y2;
z3 = z2;
strncpy (label3A, label2A, SHORTSTRINGSIZE - 1);
label3A[SHORTSTRINGSIZE - 1] = '\0';
x2 = x1;
y2 = y1;
z2 = z1;
strncpy (label2A, label1A, SHORTSTRINGSIZE - 1);
label2A[SHORTSTRINGSIZE - 1] = '\0';
x1 = x0;
y1 = y0;
z1 = z0;
strncpy (label1A, label0A, SHORTSTRINGSIZE - 1);
label1A[SHORTSTRINGSIZE - 1] = '\0';

/* Return positive value: */
return 3;
}

/*===========================================================================*/