ac1ce45ab94301989e832ea3b2140b5003757fc7
[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 <string.h>
29 #include <math.h>
30
31 #include "MEM_guardedalloc.h"
32
33 #include "DNA_scene_types.h"
34 #include "DNA_screen_types.h"
35 #include "DNA_space_types.h"
36 #include "DNA_view2d_types.h"
37
38 #include "BKE_global.h"
39 #include "BKE_utildefines.h"
40
41 #include "WM_api.h"
42
43 #include "BIF_gl.h"
44 #include "BIF_glutil.h"
45
46 #include "ED_screen.h"
47
48 #include "UI_resources.h"
49 #include "UI_text.h"
50 #include "UI_view2d.h"
51
52 /* *********************************************************************** */
53 /* Refresh and Validation */
54
55 /* Adjust mask size in response to view size changes */
56 // XXX pre2.5 -> this used to be called  calc_scrollrcts()
57 void UI_view2d_update_size(View2D *v2d, int winx, int winy)
58 {
59         /* mask - view frame */
60         v2d->mask.xmin= v2d->mask.ymin= 0;
61         v2d->mask.xmax= winx;
62         v2d->mask.ymax= winy;
63         
64         /* scrollbars shrink mask area, but should be based off regionsize 
65          *      - they can only be on one edge of the region they define
66          */
67         if (v2d->scroll) {
68                 /* vertical scrollbar */
69                 if (v2d->scroll & L_SCROLL) {
70                         /* on left-hand edge of region */
71                         v2d->vert= v2d->mask;
72                         v2d->vert.xmax= SCROLLB;
73                         v2d->mask.xmin= SCROLLB;
74                 }
75                 else if (v2d->scroll & R_SCROLL) {
76                         /* on right-hand edge of region */
77                         v2d->vert= v2d->mask;
78                         v2d->vert.xmin= v2d->vert.xmax-SCROLLB;
79                         v2d->mask.xmax= v2d->vert.xmin;
80                 }
81                 
82                 /* horizontal scrollbar */
83                 if (v2d->scroll & (B_SCROLL|B_SCROLLO)) {
84                         /* on bottom edge of region (B_SCROLLO is outliner, the ohter is for standard) */
85                         v2d->hor= v2d->mask;
86                         v2d->hor.ymax= SCROLLH;
87                         v2d->mask.ymin= SCROLLH;
88                 }
89                 else if (v2d->scroll & T_SCROLL) {
90                         /* on upper edge of region */
91                         v2d->hor= v2d->mask;
92                         v2d->hor.ymin= v2d->hor.ymax-SCROLLH;
93                         v2d->mask.ymax= v2d->hor.ymin;
94                 }
95         }
96 }
97
98 /* Ensure View2D rects remain in a viable configuration 
99  *      - cur is not allowed to be: larger than max, smaller than min, or outside of tot
100  */
101 // XXX pre2.5 -> this used to be called  test_view2d()
102 // XXX FIXME - still need to go through this and figure out what it all parts of it do
103 void UI_view2d_enforce_status(View2D *v2d, int winx, int winy)
104 {
105         /* cur is not allowed to be larger than max, smaller than min, or outside of tot */
106         rctf *cur, *tot;
107         float dx, dy, temp, fac, zoom;
108         
109         /* correct winx for scrollbars */
110         if (v2d->scroll & L_SCROLL) winx-= SCROLLB;
111         if (v2d->scroll & (B_SCROLL|B_SCROLLO)) winy-= SCROLLH;
112         
113         /* header completely closed window */
114         if (winy <= 0) return;
115         
116         /* get pointers */
117         cur= &v2d->cur;
118         tot= &v2d->tot;
119         
120         /* dx, dy are width and height of v2d->cur, respectively */
121         dx= cur->xmax - cur->xmin;
122         dy= cur->ymax - cur->ymin;
123         
124         /* keepzoom - restore old zoom */
125         if (v2d->keepzoom) {    
126                 /* keepzoom on x or y axis - reset size of current-viewable area to size of region (i.e. no zooming happened) */
127                 if (v2d->keepzoom & V2D_LOCKZOOM_Y)
128                         cur->ymax= cur->ymin + ((float)winy);
129                 if (v2d->keepzoom & V2D_LOCKZOOM_X)
130                         cur->xmax= cur->xmin + ((float)winx);
131                 
132                 /* calculate zoom-factor for x */
133                 zoom= ((float)winx)/dx;
134                 
135                 /* if zoom factor is excessive, normalise it and calculate new width */
136                 if ((zoom < v2d->minzoom) || (zoom > v2d->maxzoom)) {
137                         if (zoom < v2d->minzoom) fac= zoom / v2d->minzoom;
138                         else fac= zoom / v2d->maxzoom;
139                         
140                         dx *= fac;
141                         temp= 0.5f * (cur->xmax + cur->xmin);
142                         
143                         cur->xmin= temp - (0.5f * dx);
144                         cur->xmax= temp + (0.5f * dx);
145                 }
146                 
147                 /* calculate zoom-factor for y */
148                 zoom= ((float)winy)/dy;
149                 
150                 /* if zoom factor is excessive, normalise it and calculate new width */
151                 if ((zoom < v2d->minzoom) || (zoom > v2d->maxzoom)) {
152                         if (zoom < v2d->minzoom) fac= zoom / v2d->minzoom;
153                         else fac= zoom / v2d->maxzoom;
154                         
155                         dy *= fac;
156                         temp= 0.5f * (cur->ymax + cur->ymin);
157                         
158                         cur->ymin= temp - (0.5f * dy);
159                         cur->ymax= temp + (0.5f * dy);
160                 }
161         }
162         else {
163                 /* if extents of cur are below or above what's acceptable, interpolate extent to lie halfway */
164                 if (dx < v2d->min[0]) {
165                         dx= v2d->min[0];
166                         temp= 0.5f * (cur->xmax + cur->xmin);
167                         
168                         cur->xmin= temp - (0.5f * dx);
169                         cur->xmax= temp + (0.5f * dx);
170                 }
171                 else if (dx > v2d->max[0]) {
172                         dx= v2d->max[0];
173                         temp= 0.5f * (cur->xmax + cur->xmin);
174                         
175                         cur->xmin= temp - (0.5f * dx);
176                         cur->xmax= temp + (0.5f * dx);
177                 }
178                 
179                 if (dy < v2d->min[1]) {
180                         dy= v2d->min[1];
181                         temp= 0.5f * (cur->ymax + cur->ymin);
182                         
183                         cur->ymin= temp - (0.5f * dy);
184                         cur->ymax= temp + (0.5f * dy);
185                 }
186                 else if (dy > v2d->max[1]) {
187                         dy= v2d->max[1];
188                         temp= 0.5f * (cur->ymax + cur->ymin);
189                         cur->ymin= temp-0.5*dy;
190                         cur->ymax= temp+0.5*dy;
191                 }
192         }
193         
194         /* keep aspect - maintain aspect ratio */
195         if (v2d->keepaspect) {
196                 short do_x=0, do_y=0;
197                 
198                 /* when a window edge changes, the aspect ratio can't be used to
199                  * find which is the best new 'cur' rect. thats why it stores 'old' 
200                  */
201                 if (winx != v2d->oldwinx) do_x= 1;
202                 if (winy != v2d->oldwiny) do_y= 1;
203                 
204                 /* here dx is cur ratio, while dy is win ratio */
205                 dx= (cur->ymax - cur->ymin) / (cur->xmax - cur->xmin);
206                 dy= ((float)winy) / ((float)winx);
207                 
208                 /* both sizes change (area/region maximised)  */
209                 if (do_x == do_y) {
210                         if ((do_x==1) && (do_y==1)) {
211                                 if (ABS(winx - v2d->oldwinx) > ABS(winy - v2d->oldwiny)) do_y= 0;
212                                 else do_x= 0;
213                         }
214                         else if (dy > 1.0f) do_x= 0; 
215                         else do_x= 1;
216                 }
217                 
218                 if (do_x) {
219                         if ((v2d->keeptot == 2) && (winx < v2d->oldwinx)) {
220                                 /* This is a special hack for the outliner, to ensure that the 
221                                  * outliner contents will not eventually get pushed out of view
222                                  * when shrinking the view. 
223                                  */
224                                 cur->xmax -= cur->xmin;
225                                 cur->xmin= 0.0f;
226                         }
227                         else {
228                                 /* portrait window: correct for x */
229                                 dx= cur->ymax - cur->ymin;
230                                 temp= cur->xmax + cur->xmin;
231                                 
232                                 cur->xmin= (temp / 2.0f) - (0.5f * dx / dy);
233                                 cur->xmax= (temp / 2.0f) + (0.5f * dx / dy);
234                         }
235                 }
236                 else {
237                         dx= cur->xmax - cur->xmin;
238                         temp= cur->ymax + cur->ymin;
239                         
240                         cur->ymin= (temp / 2.0f) - (0.5f * dy * dx);
241                         cur->ymax= (temp / 2.0f) + (0.5f * dy * dx);
242                 }
243                 
244                 /* store region size for next time */
245                 v2d->oldwinx= winx; 
246                 v2d->oldwiny= winy;
247         }
248         
249         /* keeptot - make sure that size of cur doesn't exceed that of tot, otherwise, adjust! */
250         if (v2d->keeptot) {
251                 /* calculate extents of cur */
252                 dx= cur->xmax - cur->xmin;
253                 dy= cur->ymax - cur->ymin;
254                 
255                 /* cur is wider than tot? */
256                 if (dx > (tot->xmax - tot->xmin)) {
257                         if (v2d->keepzoom == 0) {
258                                 if (cur->xmin < tot->xmin) cur->xmin= tot->xmin;
259                                 if (cur->xmax > tot->xmax) cur->xmax= tot->xmax;
260                         }
261                         else {
262                                 /* maintaining zoom, so restore cur to tot size */
263                                 if (cur->xmax < tot->xmax) {
264                                         dx= tot->xmax - cur->xmax;
265                                         
266                                         cur->xmin+= dx;
267                                         cur->xmax+= dx;
268                                 }
269                                 else if (cur->xmin > tot->xmin) {
270                                         dx= cur->xmin - tot->xmin;
271                                         
272                                         cur->xmin-= dx;
273                                         cur->xmax-= dx;
274                                 }
275                         }
276                 }
277                 else {
278                         /* cur is smaller than tot, but cur cannot be outside of tot */
279                         if (cur->xmin < tot->xmin) {
280                                 dx= tot->xmin - cur->xmin;
281                                 
282                                 cur->xmin += dx;
283                                 cur->xmax += dx;
284                         }
285                         else if ((v2d->keeptot != 2) && (cur->xmax > tot->xmax)) {
286                                 /* NOTE: keeptot is 2, as keeptot!=0 makes sure it does get
287                                  *              too freely scrolled on x-axis, but keeptot=1 will result
288                                  *              in a snap-back when clicking on elements
289                                  */
290                                 dx= cur->xmax - tot->xmax;
291                                 cur->xmin -= dx;
292                                 cur->xmax -= dx;
293                         }
294                 }
295                 
296                 if (dy > (tot->ymax - tot->ymin)) {
297                         if (v2d->keepzoom==0) {
298                                 if (cur->ymin < tot->ymin) cur->ymin= tot->ymin;
299                                 if (cur->ymax > tot->ymax) cur->ymax= tot->ymax;
300                         }
301                         else {
302                                 if (cur->ymax < tot->ymax) {
303                                         dy= tot->ymax - cur->ymax;
304                                         cur->ymin+= dy;
305                                         cur->ymax+= dy;
306                                 }
307                                 else if (cur->ymin > tot->ymin) {
308                                         dy= cur->ymin - tot->ymin;
309                                         cur->ymin -= dy;
310                                         cur->ymax -= dy;
311                                 }
312                         }
313                 }
314                 else {
315                         if (cur->ymin < tot->ymin) {
316                                 dy= tot->ymin - cur->ymin;
317                                 cur->ymin += dy;
318                                 cur->ymax += dy;
319                         }
320                         else if (cur->ymax > tot->ymax) {
321                                 dy= cur->ymax - tot->ymax;
322                                 cur->ymin-= dy;
323                                 cur->ymax-= dy;
324                         }
325                 }
326         }
327 }
328
329 /* *********************************************************************** */
330 /* View Matrix Setup */
331
332 /* Set view matrices to use 'cur' rect as viewing frame for View2D drawing 
333  *      - this assumes viewport/scissor been set for the region, taking scrollbars into account
334  */
335 void UI_view2d_view_ortho(const bContext *C, View2D *v2d)
336 {
337         /* set the matrix - pixel offsets (-0.375) for 1:1 correspondance are not applied, 
338          * as they were causing some unwanted offsets when drawing 
339          */
340         wmOrtho2(C->window, v2d->cur.xmin, v2d->cur.xmax, v2d->cur.ymin, v2d->cur.ymax);
341 }
342
343 /* Set view matrices to only use one axis of 'cur' only
344  *      - this assumes viewport/scissor been set for the region, taking scrollbars into account
345  *
346  *      - xaxis         = if non-zero, only use cur x-axis, otherwise use cur-yaxis (mostly this will be used for x)
347  */
348 void UI_view2d_view_orthospecial(const bContext *C, View2D *v2d, short xaxis)
349 {
350         ARegion *region= C->region;
351         int winx, winy;
352         
353         /* calculate extents of region */
354         winx= region->winrct.xmax - region->winrct.xmin;
355         winy= region->winrct.ymax - region->winrct.ymin;
356         
357         /* set the matrix - pixel offsets (-0.375) for 1:1 correspondance are not applied, 
358          * as they were causing some unwanted offsets when drawing 
359          */
360         if (xaxis)
361                 wmOrtho2(C->window, v2d->cur.xmin, v2d->cur.xmax, 0, winy);
362         else
363                 wmOrtho2(C->window, 0, winx, v2d->cur.ymin, v2d->cur.ymax);
364
365
366
367 /* Restore view matrices after drawing */
368 void UI_view2d_view_restore(const bContext *C)
369 {
370         ED_region_pixelspace(C, C->region);
371 }
372
373 /* *********************************************************************** */
374 /* Gridlines */
375
376 /* minimum pixels per gridstep */
377 #define MINGRIDSTEP     35
378
379 /* View2DGrid is typedef'd in UI_view2d.h */
380 struct View2DGrid {
381         float dx, dy;                   /* stepsize (in pixels) between gridlines */
382         float startx, starty;   /* initial coordinates to start drawing grid from */
383         int powerx, powery;             /* step as power of 10 */
384 };
385
386 /* --------------- */
387
388 /* try to write step as a power of 10 */
389 static void step_to_grid(float *step, int *power, int unit)
390 {
391         const float loga= log10(*step);
392         float rem;
393         
394         *power= (int)(loga);
395         
396         rem= loga - (*power);
397         rem= pow(10.0, rem);
398         
399         if (loga < 0.0) {
400                 if (rem < 0.2) rem= 0.2;
401                 else if(rem < 0.5) rem= 0.5;
402                 else rem= 1.0;
403                 
404                 *step= rem * pow(10.0, (float)(*power));
405                 
406                 /* for frames, we want 1.0 frame intervals only */
407                 if (unit == V2D_UNIT_FRAMES) {
408                         rem = 1.0;
409                         *step = 1.0;
410                 }
411                 
412                 /* prevents printing 1.0 2.0 3.0 etc */
413                 if (rem == 1.0) (*power)++;     
414         }
415         else {
416                 if (rem < 2.0) rem= 2.0;
417                 else if(rem < 5.0) rem= 5.0;
418                 else rem= 10.0;
419                 
420                 *step= rem * pow(10.0, (float)(*power));
421                 
422                 (*power)++;
423                 /* prevents printing 1.0, 2.0, 3.0, etc. */
424                 if (rem == 10.0) (*power)++;    
425         }
426 }
427
428 /* Intialise settings necessary for drawing gridlines in a 2d-view 
429  *      - Currently, will return pointer to View2DGrid struct that needs to 
430  *        be freed with UI_view2d_free_grid()
431  *      - Is used for scrollbar drawing too (for units drawing)
432  *      
433  *      - unit  = V2D_UNIT_*  grid steps in seconds or frames 
434  *      - clamp = V2D_CLAMP_* only show whole-number intervals
435  *      - winx  = width of region we're drawing to
436  *      - winy  = height of region we're drawing into
437  */
438 View2DGrid *UI_view2d_calc_grid(const bContext *C, View2D *v2d, short unit, short clamp, int winx, int winy)
439 {
440         View2DGrid *grid;
441         float space, pixels, seconddiv;
442         int secondgrid;
443         
444         /* grid here is allocated... */
445         grid= MEM_callocN(sizeof(View2DGrid), "View2DGrid");
446         
447         /* rule: gridstep is minimal GRIDSTEP pixels */
448         if (unit == V2D_UNIT_FRAMES) {
449                 secondgrid= 1;
450                 seconddiv= 0.01f * FPS;
451         }
452         else {
453                 secondgrid= 0;
454                 seconddiv= 1.0f;
455         }
456         
457         /* calculate x-axis grid scale */
458         space= v2d->cur.xmax - v2d->cur.xmin;
459         pixels= v2d->mask.xmax - v2d->mask.xmin;
460         
461         grid->dx= (MINGRIDSTEP * space) / (seconddiv * pixels);
462         step_to_grid(&grid->dx, &grid->powerx, unit);
463         grid->dx *= seconddiv;
464         
465         if (clamp == V2D_GRID_CLAMP) {
466                 if (grid->dx < 0.1f) grid->dx= 0.1f;
467                 grid->powerx-= 2;
468                 if (grid->powerx < -2) grid->powerx= -2;
469         }
470         
471         /* calculate y-axis grid scale */
472         space= (v2d->cur.ymax - v2d->cur.ymin);
473         pixels= winy;
474         
475         grid->dy= MINGRIDSTEP * space / pixels;
476         step_to_grid(&grid->dy, &grid->powery, unit);
477         
478         if (clamp == V2D_GRID_CLAMP) {
479                 if (grid->dy < 1.0f) grid->dy= 1.0f;
480                 if (grid->powery < 1) grid->powery= 1;
481         }
482         
483         /* calculate start position */
484         grid->startx= seconddiv*(v2d->cur.xmin/seconddiv - fmod(v2d->cur.xmin/seconddiv, grid->dx/seconddiv));
485         if (v2d->cur.xmin < 0.0f) grid->startx-= grid->dx;
486         
487         grid->starty= (v2d->cur.ymin - fmod(v2d->cur.ymin, grid->dy));
488         if (v2d->cur.ymin < 0.0f) grid->starty-= grid->dy;
489
490         return grid;
491 }
492
493 /* Draw gridlines in the given 2d-region */
494 void UI_view2d_draw_grid(const bContext *C, View2D *v2d, View2DGrid *grid, int flag)
495 {
496         float vec1[2], vec2[2];
497         int a, step;
498         
499         /* vertical lines */
500         if (flag & V2D_VERTICAL_LINES) {
501                 /* initialise initial settings */
502                 vec1[0]= vec2[0]= grid->startx;
503                 vec1[1]= grid->starty;
504                 vec2[1]= v2d->cur.ymax;
505                 
506                 /* minor gridlines */
507                 step= (v2d->mask.xmax - v2d->mask.xmin + 1) / MINGRIDSTEP;
508                 UI_ThemeColor(TH_GRID);
509                 
510                 for (a=0; a<step; a++) {
511                         glBegin(GL_LINE_STRIP);
512                                 glVertex2fv(vec1); 
513                                 glVertex2fv(vec2);
514                         glEnd();
515                         
516                         vec2[0]= vec1[0]+= grid->dx;
517                 }
518                 
519                 /* major gridlines */
520                 vec2[0]= vec1[0]-= 0.5f*grid->dx;
521                 UI_ThemeColorShade(TH_GRID, 16);
522                 
523                 step++;
524                 for (a=0; a<=step; a++) {
525                         glBegin(GL_LINE_STRIP);
526                                 glVertex2fv(vec1); 
527                                 glVertex2fv(vec2);
528                         glEnd();
529                         
530                         vec2[0]= vec1[0]-= grid->dx;
531                 }
532         }
533         
534         /* horizontal lines */
535         if (flag & V2D_HORIZONTAL_LINES) {
536                 /* only major gridlines */
537                 vec1[0]= grid->startx;
538                 vec1[1]= vec2[1]= grid->starty;
539                 vec2[0]= v2d->cur.xmax;
540                 
541                 step= (v2d->mask.ymax - v2d->mask.ymax + 1) / MINGRIDSTEP;
542                 
543                 UI_ThemeColor(TH_GRID);
544                 for (a=0; a<=step; a++) {
545                         glBegin(GL_LINE_STRIP);
546                                 glVertex2fv(vec1); 
547                                 glVertex2fv(vec2);
548                         glEnd();
549                         
550                         vec2[1]= vec1[1]+= grid->dy;
551                 }
552                 
553                 /* fine grid lines */
554                 // er... only in IPO-Editor it seems (how to expose this in nice way)?
555                 vec2[1]= vec1[1]-= 0.5f*grid->dy;
556                 step++;
557                 
558 #if 0
559                 if (curarea->spacetype==SPACE_IPO) { 
560                         UI_ThemeColorShade(TH_GRID, 16);
561                         for (a=0; a<step; a++) {
562                                 glBegin(GL_LINE_STRIP);
563                                         glVertex2fv(vec1); 
564                                         glVertex2fv(vec2);
565                                 glEnd();
566                                 
567                                 vec2[1]= vec1[1]-= grid->dy;
568                         }
569                 }
570 #endif
571         }
572         
573         /* Axes are drawn as darker lines */
574         UI_ThemeColorShade(TH_GRID, -50);
575         
576         /* horizontal axis */
577         if (flag & V2D_HORIZONTAL_AXIS) {
578                 vec1[0]= v2d->cur.xmin;
579                 vec2[0]= v2d->cur.xmax;
580                 vec1[1]= vec2[1]= 0.0f;
581                 
582                 glBegin(GL_LINE_STRIP);
583                         glVertex2fv(vec1);
584                         glVertex2fv(vec2);
585                 glEnd();
586         }
587         
588         /* vertical axis */
589         if (flag & V2D_VERTICAL_AXIS) {
590                 vec1[1]= v2d->cur.ymin;
591                 vec2[1]= v2d->cur.ymax;
592                 vec1[0]= vec2[0]= 0.0f;
593                 
594                 glBegin(GL_LINE_STRIP);
595                         glVertex2fv(vec1); 
596                         glVertex2fv(vec2);
597                 glEnd();
598         }
599 }
600
601 /* free temporary memory used for drawing grid */
602 void UI_view2d_free_grid(View2DGrid *grid)
603 {
604         MEM_freeN(grid);
605 }
606
607 /* *********************************************************************** */
608 /* Scrollbars */
609
610 /* View2DScrollers is typedef'd in UI_view2d.h */
611 struct View2DScrollers {        
612                 /* focus bubbles */
613         int vert_min, vert_max; /* vertical scrollbar */
614         int hor_min, hor_max;   /* horizontal scrollbar */
615         
616                 /* scales */
617         View2DGrid *grid;               /* grid for coordinate drawing */
618         short xunits, xclamp;   /* units and clamping options for x-axis */
619         short yunits, yclamp;   /* units and clamping options for y-axis */
620 };
621
622 /* Calculate relevant scroller properties */
623 View2DScrollers *UI_view2d_calc_scrollers(const bContext *C, View2D *v2d, short xunits, short xclamp, short yunits, short yclamp)
624 {
625         View2DScrollers *scrollers;
626         rcti vert, hor;
627         float fac, totsize, scrollsize;
628         
629         vert= v2d->vert;
630         hor= v2d->hor;
631         
632         /* scrollers is allocated here... */
633         scrollers= MEM_callocN(sizeof(View2DScrollers), "View2DScrollers");
634         
635         /* scroller 'buttons':
636          *      - These should always remain within the visible region of the scrollbar
637          *      - They represent the region of 'tot' that is visible in 'cur'
638          */
639         
640         /* horizontal scrollers */
641         if (v2d->scroll & (HOR_SCROLL|HOR_SCROLLO)) {
642                 /* slider 'button' extents */
643                 totsize= v2d->tot.xmax - v2d->tot.xmin;
644                 scrollsize= hor.xmax - hor.xmin;
645                 
646                 fac= (v2d->cur.xmin- v2d->tot.xmin) / totsize;
647                 //if (fac < 0.0f) fac= 0.0f;
648                 scrollers->hor_min= hor.xmin + (fac * scrollsize);
649                 
650                 fac= (v2d->cur.xmax - v2d->tot.xmin) / totsize;
651                 //if (fac > 1.0f) fac= 1.0f;
652                 scrollers->hor_max= hor.xmin + (fac * scrollsize);
653                 
654                 if (scrollers->hor_min > scrollers->hor_max) 
655                         scrollers->hor_min= scrollers->hor_max;
656         }
657         
658         /* vertical scrollers */
659         if (v2d->scroll & VERT_SCROLL) {
660                 /* slider 'button' extents */
661                 totsize= v2d->tot.ymax - v2d->tot.ymin;
662                 scrollsize= vert.ymax - vert.ymin;
663                 
664                 fac= (v2d->cur.ymin- v2d->tot.ymin) / totsize;
665                 //if (fac < 0.0f) fac= 0.0f;
666                 scrollers->vert_min= vert.ymin + (fac * scrollsize);
667                 
668                 fac= (v2d->cur.ymax - v2d->tot.ymin) / totsize;
669                 //if (fac > 1.0f) fac= 1.0f;
670                 scrollers->vert_max= vert.ymin + (fac * scrollsize);
671                 
672                 if (scrollers->vert_min > scrollers->vert_max) 
673                         scrollers->vert_min= scrollers->vert_max;
674         }
675         
676         /* grid markings on scrollbars */
677         if (v2d->scroll & (HOR_SCROLLGRID|VERT_SCROLLGRID)) {
678                 /* store clamping */
679                 scrollers->xclamp= xclamp;
680                 scrollers->xunits= xunits;
681                 scrollers->yclamp= yclamp;
682                 scrollers->yunits= yunits;
683                 
684                 /* calculate grid */
685                 if (v2d->scroll & HOR_SCROLLGRID)
686                         scrollers->grid= UI_view2d_calc_grid(C, v2d, xunits, xclamp, (hor.xmax - hor.xmin), (vert.ymax - vert.ymin));
687                 else if (v2d->scroll & VERT_SCROLLGRID)
688                         scrollers->grid= UI_view2d_calc_grid(C, v2d, yunits, yclamp, (hor.xmax - hor.xmin), (vert.ymax - vert.ymin));
689         }
690         
691         /* return scrollers */
692         return scrollers;
693 }
694
695 /* XXX */
696 extern void ui_rasterpos_safe(float x, float y, float aspect);
697
698 /* Print scale marking along a time scrollbar */
699 static void scroll_printstr(View2DScrollers *scrollers, float x, float y, float val, int power, short unit, char dir)
700 {
701         int len;
702         char str[32];
703         
704         /* adjust the scale unit to work ok */
705         if (dir == 'v') {
706                 /* here we bump up the power by factor of 10, as 
707                  * rotation values (hence 'degrees') are divided by 10 to 
708                  * be able to show the curves at the same time
709                  */
710                 if ELEM(unit, V2D_UNIT_DEGREES, V2D_UNIT_TIME) {
711                         power += 1;
712                         val *= 10;
713                 }
714         }
715         
716         /* get string to print */
717         if (power <= 0) sprintf(str, "%.*f", 1-power, val);
718         else sprintf(str, "%d", (int)floor(val + 0.375));
719         
720         /* get length of string, and adjust printing location to fit it into the horizontal scrollbar */
721         len= strlen(str);
722         if (dir == 'h') x-= 4*len;  // err... why not just half len?
723         
724         /* Add degree sympbol to end of string for vertical scrollbar? */
725         if ((dir == 'v') && (unit == V2D_UNIT_DEGREES)) {
726                 str[len]= 186;
727                 str[len+1]= 0;
728         }
729         
730         /* draw it */
731         ui_rasterpos_safe(x, y, 1.0);
732         UI_DrawString(G.fonts, str, 0); // XXX check this again when new text-drawing api is done
733 }
734
735
736 /* Draw scrollbars in the given 2d-region */
737 void UI_view2d_draw_scrollers(const bContext *C, View2D *v2d, View2DScrollers *scrollers, int flag)
738 {
739         const int darker= -40, dark= 0, light= 20, lighter= 50;
740         rcti vert, hor;
741         
742         vert= v2d->vert;
743         hor= v2d->hor;
744         
745         /* horizontal scrollbar */
746         if (v2d->scroll & (HOR_SCROLL|HOR_SCROLLO)) {
747                 /* scroller backdrop */
748                 UI_ThemeColorShade(TH_SHADE1, light);
749                 glRecti(hor.xmin,  hor.ymin,  hor.xmax,  hor.ymax);
750                 
751                 /* slider 'button' */
752                         // FIXME: implement fancy one... but only when we get this working first!
753                 {
754                         UI_ThemeColorShade(TH_SHADE1, dark);
755                         glRecti(scrollers->hor_min,  hor.ymin,  scrollers->hor_max,  hor.ymax);
756                         
757                                 /* draw lines on either end of 'box' */
758                         glLineWidth(2.0);
759                                 UI_ThemeColorShade(TH_SHADE1, darker);
760                                 sdrawline(scrollers->hor_min, hor.ymin, scrollers->hor_min, hor.ymax);
761                                 sdrawline(scrollers->hor_max, hor.ymin, scrollers->hor_max, hor.ymax);
762                         glLineWidth(1.0);
763                 }
764                 
765                 /* scale indicators */
766                 // XXX will need to update the font drawing when the new stuff comes in
767                 if (v2d->scroll & HOR_SCROLLGRID) {
768                         View2DGrid *grid= scrollers->grid;
769                         float fac, dfac, fac2;
770                         int val;
771                         
772                         /* the numbers: convert grid->startx and -dx to scroll coordinates 
773                          *      - fac is x-coordinate to draw to
774                          *      - dfac is gap between scale markings
775                          */
776                         fac= (grid->startx- v2d->cur.xmin) / (v2d->cur.xmax - v2d->cur.xmin);
777                         fac= hor.xmin + fac*(hor.xmax - hor.xmin);
778                         
779                         dfac= (grid->dx) / (v2d->cur.xmax - v2d->cur.xmin);
780                         dfac= dfac*(hor.xmax-hor.xmin);
781                         
782                         UI_ThemeColor(TH_TEXT);
783                         val= grid->startx;
784                         
785                         /* if we're clamping to whole numbers only, make sure entries won't be repeated */
786                         if (scrollers->xclamp == V2D_GRID_CLAMP) {
787                                 while (grid->dx < 0.9999f) {
788                                         grid->dx *= 2.0f;
789                                         dfac *= 2.0f;
790                                 }
791                         }
792                         if (scrollers->xunits == V2D_UNIT_FRAMES)
793                                 grid->powerx= 1;
794                         
795                         /* draw numbers in the appropriate range */
796                         if (dfac != 0.0f) {
797                                 for (; fac < hor.xmax; fac+=dfac, val+=grid->dx) {
798                                         switch (scrollers->xunits) {
799                                                 case V2D_UNIT_FRAMES:           /* frames (as whole numbers)*/
800                                                         scroll_printstr(scrollers, fac, 3.0+(float)(hor.ymin), val, grid->powerx, V2D_UNIT_FRAMES, 'h');
801                                                         break;
802                                                 
803                                                 case V2D_UNIT_SECONDS:          /* seconds */
804                                                         fac2= val/FPS;
805                                                         scroll_printstr(scrollers, fac, 3.0+(float)(hor.ymin), fac2, grid->powerx, V2D_UNIT_SECONDS, 'h');
806                                                         break;
807                                                         
808                                                 case V2D_UNIT_SECONDSSEQ:       /* seconds with special calculations (only used for sequencer only) */
809                                                 {
810                                                         float time;
811                                                         
812                                                         fac2= val/FPS;
813                                                         time= floor(fac2);
814                                                         fac2= fac2-time;
815                                                         
816                                                         scroll_printstr(scrollers, fac, 3.0+(float)(hor.ymin), time+FPS*fac2/100.0, grid->powerx, V2D_UNIT_SECONDSSEQ, 'h');
817                                                 }
818                                                         break;
819                                                         
820                                                 case V2D_UNIT_DEGREES:          /* IPO-Editor for rotation IPO-Drivers */
821                                                         /* HACK: although we're drawing horizontal, we make this draw as 'vertical', just to get degree signs */
822                                                         scroll_printstr(scrollers, fac, 3.0+(float)(hor.ymin), val, grid->powerx, V2D_UNIT_DEGREES, 'v');
823                                                         break;
824                                         }
825                                 }
826                         }
827                 }
828                 
829                 /* decoration outer bevel line */
830                 UI_ThemeColorShade(TH_SHADE1, lighter);
831                 if (v2d->scroll & B_SCROLL)
832                         sdrawline(hor.xmin, hor.ymax, hor.xmax, hor.ymax);
833                 else if (v2d->scroll & T_SCROLL)
834                         sdrawline(hor.xmin, hor.ymin, hor.xmax, hor.ymin);
835         }
836         
837         /* vertical scrollbar */
838         if (v2d->scroll & VERT_SCROLL) {
839                 /* scroller backdrop  */
840                 UI_ThemeColorShade(TH_SHADE1, light);
841                 glRecti(vert.xmin,  vert.ymin,  vert.xmax,  vert.ymax);
842                 
843                 /* slider 'button' */
844                         // FIXME: implement fancy one... but only when we get this working first!
845                 {
846                         UI_ThemeColorShade(TH_SHADE1, dark);
847                         glRecti(vert.xmin,  scrollers->vert_min,  vert.xmax,  scrollers->vert_max);
848                         
849                                 /* draw lines on either end of 'box' */
850                         glLineWidth(2.0);
851                                 UI_ThemeColorShade(TH_SHADE1, darker);
852                                 sdrawline(vert.xmin, scrollers->vert_min, vert.xmax, scrollers->vert_min);
853                                 sdrawline(vert.xmin, scrollers->vert_max, vert.xmax, scrollers->vert_max);
854                         glLineWidth(1.0);
855                 }
856                 
857                 /* scale indiators */
858                 // XXX will need to update the font drawing when the new stuff comes in
859                 if (v2d->scroll & VERT_SCROLLGRID) {
860                         
861                 }       
862                 
863                 /* decoration outer bevel line */
864                 UI_ThemeColorShade(TH_SHADE1, lighter);
865                 if (v2d->scroll & R_SCROLL)
866                         sdrawline(vert.xmin, vert.ymin, vert.xmin, vert.ymax);
867                 else 
868                         sdrawline(vert.xmax, vert.ymin, vert.xmax, vert.ymax);
869         }
870 }
871
872 /* free temporary memory used for drawing scrollers */
873 void UI_view2d_free_scrollers(View2DScrollers *scrollers)
874 {
875         /* need to free grid as well... */
876         if (scrollers->grid) MEM_freeN(scrollers->grid);
877         MEM_freeN(scrollers);
878 }
879
880 /* *********************************************************************** */
881 /* Coordinate Conversions */
882
883 /* Convert from screen/region space to 2d-View space 
884  *      
885  *      - x,y                   = coordinates to convert
886  *      - viewx,viewy           = resultant coordinates
887  */
888 void UI_view2d_region_to_view(View2D *v2d, int x, int y, float *viewx, float *viewy)
889 {
890         float div, ofs;
891
892         if (viewx) {
893                 div= v2d->mask.xmax - v2d->mask.xmin;
894                 ofs= v2d->mask.xmin;
895                 
896                 *viewx= v2d->cur.xmin + (v2d->cur.xmax-v2d->cur.xmin) * ((float)x - ofs) / div;
897         }
898
899         if (viewy) {
900                 div= v2d->mask.ymax - v2d->mask.ymin;
901                 ofs= v2d->mask.ymin;
902                 
903                 *viewy= v2d->cur.ymin + (v2d->cur.ymax - v2d->cur.ymin) * ((float)y - ofs) / div;
904         }
905 }
906
907 /* Convert from 2d-View space to screen/region space
908  *      - Coordinates are clamped to lie within bounds of region
909  *
910  *      - x,y                           = coordinates to convert
911  *      - regionx,regiony       = resultant coordinates 
912  */
913 void UI_view2d_view_to_region(View2D *v2d, float x, float y, short *regionx, short *regiony)
914 {
915         /* set initial value in case coordinate lies outside of bounds */
916         if (regionx)
917                 *regionx= V2D_IS_CLIPPED;
918         if (regiony)
919                 *regiony= V2D_IS_CLIPPED;
920         
921         /* express given coordinates as proportional values */
922         x= (x - v2d->cur.xmin) / (v2d->cur.xmax - v2d->cur.xmin);
923         y= (y - v2d->cur.ymin) / (v2d->cur.ymax - v2d->cur.ymin);
924         
925         /* check if values are within bounds */
926         if ((x>=0.0f) && (x<=1.0f) && (y>=0.0f) && (y<=1.0f)) {
927                 if (regionx)
928                         *regionx= v2d->mask.xmin + x*(v2d->mask.xmax-v2d->mask.xmin);
929                 if (regiony)
930                         *regiony= v2d->mask.ymin + y*(v2d->mask.ymax-v2d->mask.ymin);
931         }
932 }
933
934 /* Convert from 2d-view space to screen/region space
935  *      - Coordinates are NOT clamped to lie within bounds of region
936  *
937  *      - x,y                           = coordinates to convert
938  *      - regionx,regiony       = resultant coordinates 
939  */
940 void UI_view2d_to_region_no_clip(View2D *v2d, float x, float y, short *regionx, short *regiony)
941 {
942         /* step 1: express given coordinates as proportional values */
943         x= (x - v2d->cur.xmin) / (v2d->cur.xmax - v2d->cur.xmin);
944         y= (y - v2d->cur.ymin) / (v2d->cur.ymax - v2d->cur.ymin);
945         
946         /* step 2: convert proportional distances to screen coordinates  */
947         x= v2d->mask.xmin + x*(v2d->mask.xmax - v2d->mask.xmin);
948         y= v2d->mask.ymin + y*(v2d->mask.ymax - v2d->mask.ymin);
949         
950         /* although we don't clamp to lie within region bounds, we must avoid exceeding size of shorts */
951         if (regionx) {
952                 if (x < -32760) *regionx= -32760;
953                 else if(x > 32760) *regionx= 32760;
954                 else *regionx= x;
955         }
956         if (regiony) {
957                 if (y < -32760) *regiony= -32760;
958                 else if(y > 32760) *regiony= 32760;
959                 else *regiony= y;
960         }
961 }
962
963 /* *********************************************************************** */
964 /* Utilities */
965
966 /* View2D data by default resides in region, so get from region stored in context */
967 View2D *UI_view2d_fromcontext(const bContext *C)
968 {
969         if (C->area == NULL) return NULL;
970         if (C->region == NULL) return NULL;
971         return &(C->region->v2d);
972 }
973
974 /* Calculate the scale per-axis of the drawing-area
975  *      - Is used to inverse correct drawing of icons, etc. that need to follow view 
976  *        but not be affected by scale
977  *
978  *      - x,y   = scale on each axis
979  */
980 void UI_view2d_getscale(View2D *v2d, float *x, float *y) 
981 {
982         if (x) *x = (v2d->mask.xmax - v2d->mask.xmin) / (v2d->cur.xmax - v2d->cur.xmin);
983         if (y) *y = (v2d->mask.ymax - v2d->mask.ymin) / (v2d->cur.ymax - v2d->cur.ymin);
984 }