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