1 | #include <stdio.h> |
---|
2 | #include <stdlib.h> |
---|
3 | #include <string.h> |
---|
4 | #include <memory.h> |
---|
5 | // #include <malloc.h> |
---|
6 | #include <X11/X.h> |
---|
7 | #include <X11/Xlib.h> |
---|
8 | |
---|
9 | #include <aw_root.hxx> |
---|
10 | #include "aw_device.hxx" |
---|
11 | #include "aw_commn.hxx" |
---|
12 | #include <aw_click.hxx> |
---|
13 | |
---|
14 | using namespace AW; |
---|
15 | |
---|
16 | // ***************************************************************************************** |
---|
17 | // device_click |
---|
18 | // ***************************************************************************************** |
---|
19 | |
---|
20 | AW_device_click::AW_device_click(AW_common *commoni):AW_device(commoni) { |
---|
21 | } |
---|
22 | |
---|
23 | void AW_device_click::init(AW_pos mousex,AW_pos mousey, AW_pos max_distance_linei, AW_pos max_distance_texti, AW_pos radi, AW_bitset filteri) { |
---|
24 | AWUSE(radi); |
---|
25 | mouse_x = mousex; |
---|
26 | mouse_y = mousey; |
---|
27 | filter = filteri; |
---|
28 | max_distance_line = max_distance_linei*max_distance_linei; |
---|
29 | max_distance_text = max_distance_texti; |
---|
30 | memset((char *)&opt_line,0,sizeof(opt_line)); |
---|
31 | memset((char *)&opt_text,0,sizeof(opt_text)); |
---|
32 | opt_line.exists = AW_FALSE; |
---|
33 | opt_text.exists = AW_FALSE; |
---|
34 | } |
---|
35 | |
---|
36 | |
---|
37 | AW_DEVICE_TYPE AW_device_click::type(void) { |
---|
38 | return AW_DEVICE_CLICK; |
---|
39 | } |
---|
40 | |
---|
41 | |
---|
42 | /***********************************************************************************************************************/ |
---|
43 | /* line text zoomtext box *******************************************************************************************/ |
---|
44 | /***********************************************************************************************************************/ |
---|
45 | |
---|
46 | int AW_device_click::line(int gc, AW_pos x0, AW_pos y0, AW_pos x1, AW_pos y1, AW_bitset filteri, AW_CL clientdata1, AW_CL clientdata2) { |
---|
47 | AW_pos X0,Y0,X1,Y1; // Transformed pos |
---|
48 | AW_pos CX0,CY0,CX1,CY1; // Clipped line |
---|
49 | int drawflag; // is line visible on screen |
---|
50 | AW_pos lx, ly, dx, dy, h1, h2, distance, skalar = 0; |
---|
51 | AW_BOOL best_line = AW_FALSE; // is this line the best ? |
---|
52 | |
---|
53 | AWUSE(gc); |
---|
54 | if(!(filteri & filter)) return AW_FALSE; |
---|
55 | |
---|
56 | this->transform(x0,y0,X0,Y0); |
---|
57 | this->transform(x1,y1,X1,Y1); |
---|
58 | drawflag = this->clip(X0,Y0,X1,Y1,CX0,CY0,CX1,CY1); |
---|
59 | |
---|
60 | if (drawflag) { |
---|
61 | //stimmen die Kreise um die Punkte ? |
---|
62 | |
---|
63 | |
---|
64 | |
---|
65 | // distance to the second point of the line |
---|
66 | dx = mouse_x - X1; |
---|
67 | dy = mouse_y - Y1; |
---|
68 | distance = (dx*dx) + (dy*dy); |
---|
69 | if (distance < max_distance_line) { |
---|
70 | best_line = AW_TRUE; |
---|
71 | max_distance_line = distance; |
---|
72 | //add more comments |
---|
73 | skalar = 0.0; |
---|
74 | } |
---|
75 | |
---|
76 | // distance to the first point of the line |
---|
77 | dx = mouse_x - X0; |
---|
78 | dy = mouse_y - Y0; |
---|
79 | distance = (dx*dx) + (dy*dy); |
---|
80 | if (distance < max_distance_line) { |
---|
81 | best_line = AW_TRUE; |
---|
82 | max_distance_line = distance; |
---|
83 | //add more comments |
---|
84 | skalar = 1.0; |
---|
85 | } |
---|
86 | |
---|
87 | lx = X1 - X0; |
---|
88 | ly = Y1 - Y0; |
---|
89 | h2 = (lx*lx) + (ly*ly); |
---|
90 | |
---|
91 | // Punkt darf nicht in der Verlaengerung der Linie liegen |
---|
92 | if (h2 > 0.0000000001){ |
---|
93 | skalar = (dx*lx+dy*ly)/h2; |
---|
94 | if ( 0.0 <= skalar && skalar <= 1.0 ) { |
---|
95 | // berechne Trefferpunkt auf Linie |
---|
96 | // distance to the line |
---|
97 | h1 = dx*ly - dy*lx; |
---|
98 | distance = (h1*h1) / h2; |
---|
99 | if (distance < max_distance_line) { |
---|
100 | best_line = AW_TRUE; |
---|
101 | max_distance_line = distance; |
---|
102 | //add more comments |
---|
103 | } |
---|
104 | } |
---|
105 | } |
---|
106 | |
---|
107 | if(best_line == AW_TRUE) { |
---|
108 | aw_assert(x0 == x0); aw_assert(x1 == x1); // not NAN |
---|
109 | aw_assert(y0 == y0); aw_assert(y1 == y1); |
---|
110 | |
---|
111 | opt_line.x0 = x0; |
---|
112 | opt_line.y0 = y0; |
---|
113 | opt_line.x1 = x1; |
---|
114 | opt_line.y1 = y1; |
---|
115 | opt_line.height = distance; |
---|
116 | opt_line.length = skalar; |
---|
117 | opt_line.client_data1 = clientdata1; |
---|
118 | opt_line.client_data2 = clientdata2; |
---|
119 | opt_line.exists = AW_TRUE; |
---|
120 | } |
---|
121 | return AW_TRUE; |
---|
122 | } |
---|
123 | return AW_FALSE; |
---|
124 | } |
---|
125 | |
---|
126 | |
---|
127 | int AW_device_click::text(int gc, const char *str, AW_pos x, AW_pos y, AW_pos alignment, AW_bitset filteri, AW_CL clientdata1, AW_CL clientdata2,long opt_strlen) { |
---|
128 | if(filteri & filter) { |
---|
129 | AW_pos X0,Y0; // Transformed pos |
---|
130 | this->transform(x,y,X0,Y0); |
---|
131 | |
---|
132 | XFontStruct *xfs = &common->gcs[gc]->curfont; |
---|
133 | |
---|
134 | AW_pos Y1 = Y0+(AW_pos)(xfs->max_bounds.descent); |
---|
135 | Y0 = Y0-(AW_pos)(xfs->max_bounds.ascent); |
---|
136 | |
---|
137 | /***************** Fast check text against top bottom clip ***************************/ |
---|
138 | |
---|
139 | if (this->clip_rect.t == 0) { |
---|
140 | if (Y1 < this->clip_rect.t) return 0; |
---|
141 | } |
---|
142 | else { |
---|
143 | if (Y0 < this->clip_rect.t) return 0; |
---|
144 | } |
---|
145 | |
---|
146 | if (this->clip_rect.b == common->screen.b) { |
---|
147 | if (Y0 > this->clip_rect.b) return 0; |
---|
148 | } |
---|
149 | else { |
---|
150 | if (Y1 > this->clip_rect.b) return 0; |
---|
151 | } |
---|
152 | |
---|
153 | /***************** vertical check mouse against textsurrounding ***************************/ |
---|
154 | |
---|
155 | bool exact = true; |
---|
156 | double best_dist = 0; |
---|
157 | |
---|
158 | if (mouse_y > Y1) { // outside text |
---|
159 | if (mouse_y > Y1+max_distance_text) return 0; // too far above |
---|
160 | exact = false; |
---|
161 | best_dist = mouse_y - Y1; |
---|
162 | } |
---|
163 | else if (mouse_y < Y0) { |
---|
164 | if (mouse_y < Y0-max_distance_text) return 0; // too far below |
---|
165 | exact = false; |
---|
166 | best_dist = Y0 - mouse_y; |
---|
167 | } |
---|
168 | |
---|
169 | /***************** align text ***************************/ |
---|
170 | int len = opt_strlen ? opt_strlen : strlen(str); |
---|
171 | int text_width = (int)get_string_size(gc,str,len); |
---|
172 | |
---|
173 | X0 = common->x_alignment(X0,text_width,alignment); |
---|
174 | AW_pos X1 = X0+text_width; |
---|
175 | |
---|
176 | /**************** check against left right clipping areas *********/ |
---|
177 | if (X1 < this->clip_rect.l) return 0; |
---|
178 | if (X0 > this->clip_rect.r) return 0; |
---|
179 | |
---|
180 | if (mouse_x < X0) return 0; // left of text |
---|
181 | if (mouse_x > X1) return 0; // right of text |
---|
182 | |
---|
183 | max_distance_text = best_dist; // exact hit -> distance = 0; |
---|
184 | |
---|
185 | int position; |
---|
186 | if (xfs->max_bounds.width == xfs->min_bounds.width) { // monospaced font |
---|
187 | short letter_width = xfs->max_bounds.width; |
---|
188 | position = (int)((mouse_x-X0)/letter_width); |
---|
189 | if (position<0) position = 0; |
---|
190 | if (position>(len-1)) position = len-1; |
---|
191 | } |
---|
192 | else { // non-monospaced font |
---|
193 | AW_GC_Xm *gcm = AW_MAP_GC(gc); |
---|
194 | position = 0; |
---|
195 | int tmp_offset = 0; |
---|
196 | while (position<=len) { |
---|
197 | tmp_offset += gcm->width_of_chars[(unsigned char)str[position]]; |
---|
198 | if (mouse_x <= X0+tmp_offset) break; |
---|
199 | position++; |
---|
200 | } |
---|
201 | } |
---|
202 | |
---|
203 | AW_pos dist2center = Distance(Position(mouse_x, mouse_y), |
---|
204 | LineVector(X0, Y0, X0+text_width, Y1).centroid()); |
---|
205 | |
---|
206 | if (!opt_text.exists || // first candidate |
---|
207 | (!opt_text.exactHit && exact) || // previous candidate was no exact hit |
---|
208 | (dist2center<opt_text.dist2center)) // distance to text-center is smaller |
---|
209 | { |
---|
210 | opt_text.textArea = Rectangle(rtransform(LineVector(X0, Y0, X1, Y1))); |
---|
211 | opt_text.alignment = alignment; |
---|
212 | opt_text.rotation = 0; |
---|
213 | opt_text.distance = max_distance_text; |
---|
214 | opt_text.dist2center = dist2center; |
---|
215 | opt_text.cursor = position; |
---|
216 | opt_text.client_data1 = clientdata1; |
---|
217 | opt_text.client_data2 = clientdata2; |
---|
218 | opt_text.exists = AW_TRUE; |
---|
219 | opt_text.exactHit = exact; |
---|
220 | } |
---|
221 | } |
---|
222 | return 1; |
---|
223 | } |
---|
224 | |
---|
225 | |
---|
226 | void AW_device_click::get_clicked_line(class AW_clicked_line *ptr) { |
---|
227 | *ptr = opt_line; |
---|
228 | } |
---|
229 | |
---|
230 | |
---|
231 | void AW_device_click::get_clicked_text(class AW_clicked_text *ptr) { |
---|
232 | *ptr = opt_text; |
---|
233 | } |
---|
234 | |
---|
235 | double AW_clicked_line::distanceTo(const AW::Position& pos) { |
---|
236 | AW::LineVector cl(x0, y0, x1, y1); |
---|
237 | if (cl.length() == 0) { |
---|
238 | return AW::Distance(pos, cl.start()); |
---|
239 | } |
---|
240 | return AW::Distance(pos, cl); |
---|
241 | } |
---|
242 | |
---|
243 | bool AW_getBestClick(const AW::Position& click, AW_clicked_line *cl, AW_clicked_text *ct, AW_CL *cd1, AW_CL *cd2) { |
---|
244 | // detect the nearest item next to 'click' |
---|
245 | // and return that items callback params. |
---|
246 | // returns false, if nothing has been clicked |
---|
247 | |
---|
248 | AW_clicked_element *bestClick = 0; |
---|
249 | |
---|
250 | if (cl->exists) { |
---|
251 | if (ct->exists) { |
---|
252 | if (cl->distanceTo(click) < ct->distance) { |
---|
253 | bestClick = cl; |
---|
254 | } |
---|
255 | else { |
---|
256 | bestClick = ct; |
---|
257 | } |
---|
258 | } |
---|
259 | else { |
---|
260 | bestClick = cl; |
---|
261 | } |
---|
262 | } |
---|
263 | else if (ct->exists) { |
---|
264 | bestClick = ct; |
---|
265 | } |
---|
266 | |
---|
267 | if (bestClick) { |
---|
268 | *cd1 = bestClick->client_data1; |
---|
269 | *cd2 = bestClick->client_data2; |
---|
270 | } |
---|
271 | else { |
---|
272 | *cd1 = 0; |
---|
273 | *cd2 = 0; |
---|
274 | } |
---|
275 | |
---|
276 | return bestClick; |
---|
277 | } |
---|
278 | |
---|
279 | |
---|
280 | |
---|