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