View2D - Initial commit of Pan-View Operator
[blender.git] / source / blender / editors / interface / view2d.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2008 Blender Foundation.
21  * All rights reserved.
22  * 
23  * Contributor(s): Blender Foundation, Joshua Leung
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 #include <math.h>
29
30 #include "MEM_guardedalloc.h"
31
32 #include "DNA_scene_types.h"
33 #include "DNA_screen_types.h"
34 #include "DNA_space_types.h"
35 #include "DNA_view2d_types.h"
36
37 #include "BKE_global.h"
38 #include "BKE_utildefines.h"
39
40 #include "WM_api.h"
41
42 #include "BIF_gl.h"
43
44 #include "UI_resources.h"
45 #include "UI_view2d.h"
46
47 /* *********************************************************************** */
48 /* Setup and Refresh Code */
49
50 /* Set view matrices to ortho for View2D drawing */
51 // XXX in past, this was not always the case!
52 void UI_view2d_ortho(const bContext *C, View2D *v2d)
53 {
54         wmOrtho2(C->window, v2d->cur.xmin, v2d->cur.xmax, v2d->cur.ymin, v2d->cur.ymax);
55 }
56
57 /* Adjust mask size in response to view size changes */
58 // XXX pre2.5 -> this used to be called  calc_scrollrcts()
59 void UI_view2d_update_size(View2D *v2d, int winx, int winy)
60 {
61         /* mask - view frame */
62         v2d->mask.xmin= v2d->mask.ymin= 0;
63         v2d->mask.xmax= winx;
64         v2d->mask.ymax= winy;
65         
66         /* scrollbars shrink mask area, but should be based off regionsize 
67          *      - they can only be on one edge of the region they define
68          */
69         if (v2d->scroll) {
70                 /* vertical scrollbar */
71                 if (v2d->scroll & L_SCROLL) {
72                         /* on left-hand edge of region */
73                         v2d->vert= v2d->mask;
74                         v2d->vert.xmax= SCROLLB;
75                         v2d->mask.xmin= SCROLLB;
76                 }
77                 else if (v2d->scroll & R_SCROLL) {
78                         /* on right-hand edge of region */
79                         v2d->vert= v2d->mask;
80                         v2d->vert.xmin= v2d->vert.xmax-SCROLLB;
81                         v2d->mask.xmax= v2d->vert.xmin;
82                 }
83                 
84                 /* horizontal scrollbar */
85                 if ((v2d->scroll & B_SCROLL) || (v2d->scroll & B_SCROLLO)) {
86                         /* on bottom edge of region (B_SCROLLO is outliner, the ohter is for standard) */
87                         v2d->hor= v2d->mask;
88                         v2d->hor.ymax= SCROLLH;
89                         v2d->mask.ymin= SCROLLH;
90                 }
91                 else if (v2d->scroll & T_SCROLL) {
92                         /* on upper edge of region */
93                         v2d->hor= v2d->mask;
94                         v2d->hor.ymin= v2d->hor.ymax-SCROLLH;
95                         v2d->mask.ymax= v2d->hor.ymin;
96                 }
97         }
98 }
99
100 /* Ensure View2D rects remain in a viable configuration 
101  *      - cur is not allowed to be: larger than max, smaller than min, or outside of tot
102  */
103 // XXX pre2.5 -> this used to be called  test_view2d()
104 // XXX FIXME - still need to go through this and figure out what it all parts of it do
105 void UI_view2d_enforce_status(View2D *v2d, int winx, int winy)
106 {
107         /* cur is not allowed to be larger than max, smaller than min, or outside of tot */
108         rctf *cur, *tot;
109         float dx, dy, temp, fac, zoom;
110         
111         /* correct winx for scrollbars */
112         if (v2d->scroll & L_SCROLL) winx-= SCROLLB;
113         if (v2d->scroll & B_SCROLL) winy-= SCROLLH;
114         if (v2d->scroll & B_SCROLLO) winy-= SCROLLH; /* B_SCROLL and B_SCROLLO are basically same thing */
115         
116         /* header completely closed window */
117         if (winy <= 0) return;
118         
119         /* get pointers */
120         cur= &v2d->cur;
121         tot= &v2d->tot;
122         
123         /* dx, dy are width and height of v2d->cur, respectively */
124         dx= cur->xmax - cur->xmin;
125         dy= cur->ymax - cur->ymin;
126         
127         /* keepzoom - restore old zoom */
128         if (v2d->keepzoom) {    
129                 /* keepzoom on x or y axis - reset size of current-viewable area to size of region (i.e. no zooming happened) */
130                 if (v2d->keepzoom & V2D_LOCKZOOM_Y)
131                         cur->ymax= cur->ymin + ((float)winy);
132                 if (v2d->keepzoom & V2D_LOCKZOOM_X)
133                         cur->xmax= cur->xmin + ((float)winx);
134                 
135                 /* calculate zoom-factor for x */
136                 zoom= ((float)winx)/dx;
137                 
138                 /* if zoom factor is excessive, normalise it and calculate new width */
139                 if ((zoom < v2d->minzoom) || (zoom > v2d->maxzoom)) {
140                         if (zoom < v2d->minzoom) fac= zoom / v2d->minzoom;
141                         else fac= zoom / v2d->maxzoom;
142                         
143                         dx *= fac;
144                         temp= 0.5f * (cur->xmax + cur->xmin);
145                         
146                         cur->xmin= temp - (0.5f * dx);
147                         cur->xmax= temp + (0.5f * dx);
148                 }
149                 
150                 /* calculate zoom-factor for y */
151                 zoom= ((float)winy)/dy;
152                 
153                 /* if zoom factor is excessive, normalise it and calculate new width */
154                 if ((zoom < v2d->minzoom) || (zoom > v2d->maxzoom)) {
155                         if (zoom < v2d->minzoom) fac= zoom / v2d->minzoom;
156                         else fac= zoom / v2d->maxzoom;
157                         
158                         dy *= fac;
159                         temp= 0.5f * (cur->ymax + cur->ymin);
160                         
161                         cur->ymin= temp - (0.5f * dy);
162                         cur->ymax= temp + (0.5f * dy);
163                 }
164         }
165         else {
166                 /* if extents of cur are below or above what's acceptable, interpolate extent to lie halfway */
167                 if (dx < v2d->min[0]) {
168                         dx= v2d->min[0];
169                         temp= 0.5f * (cur->xmax + cur->xmin);
170                         
171                         cur->xmin= temp - (0.5f * dx);
172                         cur->xmax= temp + (0.5f * dx);
173                 }
174                 else if (dx > v2d->max[0]) {
175                         dx= v2d->max[0];
176                         temp= 0.5f * (cur->xmax + cur->xmin);
177                         
178                         cur->xmin= temp - (0.5f * dx);
179                         cur->xmax= temp + (0.5f * dx);
180                 }
181                 
182                 if (dy < v2d->min[1]) {
183                         dy= v2d->min[1];
184                         temp= 0.5f * (cur->ymax + cur->ymin);
185                         
186                         cur->ymin= temp - (0.5f * dy);
187                         cur->ymax= temp + (0.5f * dy);
188                 }
189                 else if (dy > v2d->max[1]) {
190                         dy= v2d->max[1];
191                         temp= 0.5f * (cur->ymax + cur->ymin);
192                         cur->ymin= temp-0.5*dy;
193                         cur->ymax= temp+0.5*dy;
194                 }
195         }
196         
197         /* keep aspect - maintain aspect ratio */
198         if (v2d->keepaspect) {
199                 short do_x=0, do_y=0;
200                 
201                 /* when a window edge changes, the aspect ratio can't be used to
202                  * find which is the best new 'cur' rect. thats why it stores 'old' 
203                  */
204                 if (winx != v2d->oldwinx) do_x= 1;
205                 if (winy != v2d->oldwiny) do_y= 1;
206                 
207                 /* here dx is cur ratio, while dy is win ratio */
208                 dx= (cur->ymax - cur->ymin) / (cur->xmax - cur->xmin);
209                 dy= ((float)winy) / ((float)winx);
210                 
211                 /* both sizes change (area/region maximised)  */
212                 if (do_x == do_y) {
213                         if ((do_x==1) && (do_y==1)) {
214                                 if (ABS(winx - v2d->oldwinx) > ABS(winy - v2d->oldwiny)) do_y= 0;
215                                 else do_x= 0;
216                         }
217                         else if (dy > 1.0f) do_x= 0; 
218                         else do_x= 1;
219                 }
220                 
221                 if (do_x) {
222                         if ((v2d->keeptot == 2) && (winx < v2d->oldwinx)) {
223                                 /* This is a special hack for the outliner, to ensure that the 
224                                  * outliner contents will not eventually get pushed out of view
225                                  * when shrinking the view. 
226                                  */
227                                 cur->xmax -= cur->xmin;
228                                 cur->xmin= 0.0f;
229                         }
230                         else {
231                                 /* portrait window: correct for x */
232                                 dx= cur->ymax - cur->ymin;
233                                 temp= cur->xmax + cur->xmin;
234                                 
235                                 cur->xmin= (temp / 2.0f) - (0.5f * dx / dy);
236                                 cur->xmax= (temp / 2.0f) + (0.5f * dx / dy);
237                         }
238                 }
239                 else {
240                         dx= cur->xmax - cur->xmin;
241                         temp= cur->ymax + cur->ymin;
242                         
243                         cur->ymin= (temp / 2.0f) - (0.5f * dy * dx);
244                         cur->ymax= (temp / 2.0f) + (0.5f * dy * dx);
245                 }
246                 
247                 /* store region size for next time */
248                 v2d->oldwinx= winx; 
249                 v2d->oldwiny= winy;
250         }
251         
252         /* keeptot - make sure that size of cur doesn't exceed that of tot, otherwise, adjust! */
253         if (v2d->keeptot) {
254                 /* calculate extents of cur */
255                 dx= cur->xmax - cur->xmin;
256                 dy= cur->ymax - cur->ymin;
257                 
258                 /* cur is wider than tot? */
259                 if (dx > (tot->xmax - tot->xmin)) {
260                         if (v2d->keepzoom == 0) {
261                                 if (cur->xmin < tot->xmin) cur->xmin= tot->xmin;
262                                 if (cur->xmax > tot->xmax) cur->xmax= tot->xmax;
263                         }
264                         else {
265                                 /* maintaining zoom, so restore cur to tot size */
266                                 if (cur->xmax < tot->xmax) {
267                                         dx= tot->xmax - cur->xmax;
268                                         
269                                         cur->xmin+= dx;
270                                         cur->xmax+= dx;
271                                 }
272                                 else if (cur->xmin > tot->xmin) {
273                                         dx= cur->xmin - tot->xmin;
274                                         
275                                         cur->xmin-= dx;
276                                         cur->xmax-= dx;
277                                 }
278                         }
279                 }
280                 else {
281                         /* cur is smaller than tot, but cur cannot be outside of tot */
282                         if (cur->xmin < tot->xmin) {
283                                 dx= tot->xmin - cur->xmin;
284                                 
285                                 cur->xmin += dx;
286                                 cur->xmax += dx;
287                         }
288                         else if ((v2d->keeptot != 2) && (cur->xmax > tot->xmax)) {
289                                 /* NOTE: keeptot is 2, as keeptot!=0 makes sure it does get
290                                  *              too freely scrolled on x-axis, but keeptot=1 will result
291                                  *              in a snap-back when clicking on elements
292                                  */
293                                 dx= cur->xmax - tot->xmax;
294                                 cur->xmin -= dx;
295                                 cur->xmax -= dx;
296                         }
297                 }
298                 
299                 if (dy > (tot->ymax - tot->ymin)) {
300                         if (v2d->keepzoom==0) {
301                                 if (cur->ymin < tot->ymin) cur->ymin= tot->ymin;
302                                 if (cur->ymax > tot->ymax) cur->ymax= tot->ymax;
303                         }
304                         else {
305                                 if (cur->ymax < tot->ymax) {
306                                         dy= tot->ymax - cur->ymax;
307                                         cur->ymin+= dy;
308                                         cur->ymax+= dy;
309                                 }
310                                 else if (cur->ymin > tot->ymin) {
311                                         dy= cur->ymin - tot->ymin;
312                                         cur->ymin -= dy;
313                                         cur->ymax -= dy;
314                                 }
315                         }
316                 }
317                 else {
318                         if (cur->ymin < tot->ymin) {
319                                 dy= tot->ymin - cur->ymin;
320                                 cur->ymin += dy;
321                                 cur->ymax += dy;
322                         }
323                         else if (cur->ymax > tot->ymax) {
324                                 dy= cur->ymax - tot->ymax;
325                                 cur->ymin-= dy;
326                                 cur->ymax-= dy;
327                         }
328                 }
329         }
330 }
331
332 /* *********************************************************************** */
333 /* Gridlines */
334
335 /* minimum pixels per gridstep */
336 #define MINGRIDSTEP     35
337
338 /* View2DGrid is typedef'd in UI_view2d.h */
339 struct View2DGrid {
340         float dx, dy;                   /* stepsize (in pixels) between gridlines */
341         float startx, starty;   /* */
342         int powerx, powery;             /* step as power of 10 */
343 };
344
345 /* --------------- */
346
347 /* try to write step as a power of 10 */
348 static void step_to_grid(float *step, int *power, int unit)
349 {
350         const float loga= log10(*step);
351         float rem;
352         
353         *power= (int)(loga);
354         
355         rem= loga - (*power);
356         rem= pow(10.0, rem);
357         
358         if (loga < 0.0f) {
359                 if (rem < 0.2f) rem= 0.2f;
360                 else if(rem < 0.5f) rem= 0.5f;
361                 else rem= 1.0f;
362                 
363                 *step= rem * pow(10.0, (double)(*power));
364                 
365                 /* for frames, we want 1.0 frame intervals only */
366                 if (unit == V2D_UNIT_FRAMES) {
367                         rem = 1.0f;
368                         *step = 1.0f;
369                 }
370                 
371                 /* prevents printing 1.0 2.0 3.0 etc */
372                 if (rem == 1.0f) (*power)++;    
373         }
374         else {
375                 if (rem < 2.0f) rem= 2.0f;
376                 else if(rem < 5.0f) rem= 5.0f;
377                 else rem= 10.0f;
378                 
379                 *step= rem * pow(10.0, (double)(*power));
380                 
381                 (*power)++;
382                 /* prevents printing 1.0, 2.0, 3.0, etc. */
383                 if (rem == 10.0f) (*power)++;   
384         }
385 }
386
387 /* Intialise settings necessary for drawing gridlines in a 2d-view 
388  *      - Currently, will return pointer to View2DGrid struct that needs to 
389  *        be freed with UI_view2d_free_grid()
390  *      - Is used for scrollbar drawing too (for units drawing)  --> (XXX needs review)
391  *      
392  *      - unit  = V2D_UNIT_*  grid steps in seconds or frames 
393  *      - clamp = V2D_CLAMP_* only show whole-number intervals
394  *      - winx  = width of region we're drawing to
395  *      - winy  = height of region we're drawing into
396  */
397 View2DGrid *UI_view2d_calc_grid(const bContext *C, View2D *v2d, short unit, short clamp, int winx, int winy)
398 {
399         View2DGrid *grid;
400         float space, pixels, seconddiv;
401         int secondgrid;
402         
403         /* grid here is allocated... */
404         grid= MEM_callocN(sizeof(View2DGrid), "View2DGrid");
405         
406         /* rule: gridstep is minimal GRIDSTEP pixels */
407         if (unit == V2D_UNIT_FRAMES) {
408                 secondgrid= 0;
409                 seconddiv= 0.01f * FPS;
410         }
411         else {
412                 secondgrid= 1;
413                 seconddiv= 1.0f;
414         }
415         
416         space= v2d->cur.xmax - v2d->cur.xmin;
417         pixels= v2d->mask.xmax - v2d->mask.xmin;
418         
419         grid->dx= (MINGRIDSTEP * space) / (seconddiv * pixels);
420         step_to_grid(&grid->dx, &grid->powerx, unit);
421         grid->dx *= seconddiv;
422         
423         if (clamp == V2D_GRID_CLAMP) {
424                 if (grid->dx < 0.1f) grid->dx= 0.1f;
425                 grid->powerx-= 2;
426                 if (grid->powerx < -2) grid->powerx= -2;
427         }
428         
429         space= (v2d->cur.ymax - v2d->cur.ymin);
430         pixels= winy;
431         grid->dy= MINGRIDSTEP*space/pixels;
432         step_to_grid(&grid->dy, &grid->powery, unit);
433         
434         if (clamp == V2D_GRID_CLAMP) {
435                 if (grid->dy < 1.0f) grid->dy= 1.0f;
436                 if (grid->powery < 1) grid->powery= 1;
437         }
438         
439         grid->startx= seconddiv*(v2d->cur.xmin/seconddiv - fmod(v2d->cur.xmin/seconddiv, grid->dx/seconddiv));
440         if (v2d->cur.xmin < 0.0f) grid->startx-= grid->dx;
441         
442         grid->starty= (v2d->cur.ymin-fmod(v2d->cur.ymin, grid->dy));
443         if (v2d->cur.ymin < 0.0f) grid->starty-= grid->dy;
444
445         return grid;
446 }
447
448 /* Draw gridlines in the given 2d-region */
449 void UI_view2d_draw_grid(const bContext *C, View2D *v2d, View2DGrid *grid, int flag)
450 {
451         float vec1[2], vec2[2];
452         int a, step;
453         
454         /* vertical lines */
455         if (flag & V2D_VERTICAL_LINES) {
456                 /* initialise initial settings */
457                 vec1[0]= vec2[0]= grid->startx;
458                 vec1[1]= grid->starty;
459                 vec2[1]= v2d->cur.ymax;
460                 
461                 /* minor gridlines */
462                 step= (v2d->mask.xmax - v2d->mask.xmin + 1) / MINGRIDSTEP;
463                 
464                 UI_ThemeColor(TH_GRID);
465                 
466                 for (a=0; a<step; a++) {
467                         glBegin(GL_LINE_STRIP);
468                                 glVertex2fv(vec1); 
469                                 glVertex2fv(vec2);
470                         glEnd();
471                         
472                         vec2[0]= vec1[0]+= grid->dx;
473                 }
474                 
475                 /* major gridlines */
476                 vec2[0]= vec1[0]-= 0.5f*grid->dx;
477                 
478                 UI_ThemeColorShade(TH_GRID, 16);
479                 
480                 step++;
481                 for (a=0; a<=step; a++) {
482                         glBegin(GL_LINE_STRIP);
483                                 glVertex2fv(vec1); 
484                                 glVertex2fv(vec2);
485                         glEnd();
486                         
487                         vec2[0]= vec1[0]-= grid->dx;
488                 }
489         }
490         
491         /* horizontal lines */
492         if (flag & V2D_HORIZONTAL_LINES) {
493                 /* only major gridlines */
494                 vec1[0]= grid->startx;
495                 vec1[1]= vec2[1]= grid->starty;
496                 vec2[0]= v2d->cur.xmax;
497                 
498                 step= (v2d->mask.ymax - v2d->mask.ymax + 1) / MINGRIDSTEP;
499                 
500                 UI_ThemeColor(TH_GRID);
501                 for (a=0; a<=step; a++) {
502                         glBegin(GL_LINE_STRIP);
503                                 glVertex2fv(vec1); 
504                                 glVertex2fv(vec2);
505                         glEnd();
506                         
507                         vec2[1]= vec1[1]+= grid->dy;
508                 }
509                 
510                 vec2[1]= vec1[1]-= 0.5f*grid->dy;
511                 step++;
512         }
513         
514         /* Axes are drawn as darker lines */
515         UI_ThemeColorShade(TH_GRID, -50);
516         
517         /* horizontal axis */
518         if (flag & V2D_HORIZONTAL_AXIS) {
519                 vec1[0]= v2d->cur.xmin;
520                 vec2[0]= v2d->cur.xmax;
521                 vec1[1]= vec2[1]= 0.0f;
522                 
523                 glBegin(GL_LINE_STRIP);
524                         glVertex2fv(vec1);
525                         glVertex2fv(vec2);
526                 glEnd();
527         }
528         
529         /* vertical axis */
530         if (flag & V2D_VERTICAL_AXIS) {
531                 vec1[1]= v2d->cur.ymin;
532                 vec2[1]= v2d->cur.ymax;
533                 vec1[0]= vec2[0]= 0.0f;
534                 
535                 glBegin(GL_LINE_STRIP);
536                         glVertex2fv(vec1); 
537                         glVertex2fv(vec2);
538                 glEnd();
539         }
540 }
541
542 /* free temporary memory used for drawing grid */
543 void UI_view2d_free_grid(View2DGrid *grid)
544 {
545         MEM_freeN(grid);
546 }
547
548 /* *********************************************************************** */
549 /* Scrollbars */
550
551
552
553 /* *********************************************************************** */
554 /* Coordinate Conversions */
555
556 /* Convert from screen/region space to 2d-View space 
557  *      
558  *      - x,y                   = coordinates to convert
559  *      - viewx,viewy           = resultant coordinates
560  */
561 void UI_view2d_region_to_view(View2D *v2d, short x, short y, float *viewx, float *viewy)
562 {
563         float div, ofs;
564
565         if (viewx) {
566                 div= v2d->mask.xmax - v2d->mask.xmin;
567                 ofs= v2d->mask.xmin;
568                 
569                 *viewx= v2d->cur.xmin + (v2d->cur.xmax-v2d->cur.xmin) * (x - ofs) / div;
570         }
571
572         if (viewy) {
573                 div= v2d->mask.ymax - v2d->mask.ymin;
574                 ofs= v2d->mask.ymin;
575                 
576                 *viewy= v2d->cur.ymin + (v2d->cur.ymax - v2d->cur.ymin) * (y - ofs) / div;
577         }
578 }
579
580 /* Convert from 2d-View space to screen/region space
581  *      - Coordinates are clamped to lie within bounds of region
582  *
583  *      - x,y                           = coordinates to convert
584  *      - regionx,regiony       = resultant coordinates 
585  */
586 void UI_view2d_view_to_region(View2D *v2d, float x, float y, short *regionx, short *regiony)
587 {
588         /* set initial value in case coordinate lies outside of bounds */
589         if (regionx)
590                 *regionx= V2D_IS_CLIPPED;
591         if (regiony)
592                 *regiony= V2D_IS_CLIPPED;
593         
594         /* express given coordinates as proportional values */
595         x= (x - v2d->cur.xmin) / (v2d->cur.xmax - v2d->cur.xmin);
596         y= (x - v2d->cur.ymin) / (v2d->cur.ymax - v2d->cur.ymin);
597         
598         /* check if values are within bounds */
599         if ((x>=0.0f) && (x<=1.0f) && (y>=0.0f) && (y<=1.0f)) {
600                 if (regionx)
601                         *regionx= v2d->mask.xmin + x*(v2d->mask.xmax-v2d->mask.xmin);
602                 if (regiony)
603                         *regiony= v2d->mask.ymin + y*(v2d->mask.ymax-v2d->mask.ymin);
604         }
605 }
606
607 /* Convert from 2d-view space to screen/region space
608  *      - Coordinates are NOT clamped to lie within bounds of region
609  *
610  *      - x,y                           = coordinates to convert
611  *      - regionx,regiony       = resultant coordinates 
612  */
613 void UI_view2d_to_region_no_clip(View2D *v2d, float x, float y, short *regionx, short *regiony)
614 {
615         /* step 1: express given coordinates as proportional values */
616         x= (x - v2d->cur.xmin) / (v2d->cur.xmax - v2d->cur.xmin);
617         y= (x - v2d->cur.ymin) / (v2d->cur.ymax - v2d->cur.ymin);
618         
619         /* step 2: convert proportional distances to screen coordinates  */
620         x= v2d->mask.xmin + x*(v2d->mask.xmax - v2d->mask.xmin);
621         y= v2d->mask.ymin + y*(v2d->mask.ymax - v2d->mask.ymin);
622         
623         /* although we don't clamp to lie within region bounds, we must avoid exceeding size of shorts */
624         if (regionx) {
625                 if (x < -32760) *regionx= -32760;
626                 else if(x > 32760) *regionx= 32760;
627                 else *regionx= x;
628         }
629         if (regiony) {
630                 if (y < -32760) *regiony= -32760;
631                 else if(y > 32760) *regiony= 32760;
632                 else *regiony= y;
633         }
634 }
635
636 /* *********************************************************************** */
637 /* Utilities */
638
639 /* Calculate the scale per-axis of the drawing-area
640  *      - Is used to inverse correct drawing of icons, etc. that need to follow view 
641  *        but not be affected by scale
642  *
643  *      - x,y   = scale on each axis
644  */
645 void UI_view2d_getscale(View2D *v2d, float *x, float *y) 
646 {
647         if (x) *x = (v2d->mask.xmax - v2d->mask.xmin) / (v2d->cur.xmax - v2d->cur.xmin);
648         if (y) *y = (v2d->mask.ymax - v2d->mask.ymin) / (v2d->cur.ymax - v2d->cur.ymin);
649 }