View2D: Presets for Initialising Views
[blender-staging.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 "BLI_blenlib.h"
39
40 #include "BKE_global.h"
41 #include "BKE_utildefines.h"
42
43 #include "WM_api.h"
44
45 #include "BIF_gl.h"
46 #include "BIF_glutil.h"
47
48 #include "ED_screen.h"
49
50 #include "UI_resources.h"
51 #include "UI_text.h"
52 #include "UI_view2d.h"
53
54 #include "UI_interface.h"
55 #include "interface.h"
56
57 /* *********************************************************************** */
58 /* Refresh and Validation */
59
60 /* Initialise all relevant View2D data (including view rects if first time) and/or refresh mask sizes after view resize
61  *      - for some of these presets, it is expected that the region will have defined some
62  *        additional settings necessary for the customisation of the 2D viewport to its requirements
63  *      - this function should only be called from region init() callbacks, where it is expected that
64  *        this is called before UI_view2d_size_update(), as this one checks that the rects are properly initialised. 
65  */
66 // eView2D_CommonViewTypes <--- only check handle these types...
67 void UI_view2d_region_reinit(View2D *v2d, short type, int winx, int winy)
68 {
69         short tot_changed= 0;
70         
71         /* store view size */
72         v2d->winx= winx;
73         v2d->winy= winy;
74         
75         /* mask - view frame */
76         v2d->mask.xmin= v2d->mask.ymin= 0;
77         v2d->mask.xmax= winx - 1;       /* -1 yes! masks are pixels */
78         v2d->mask.ymax= winy - 1;
79         
80         /* scrollers shrink mask area, but should be based off regionsize 
81          *      - they can only be on one to two edges of the region they define
82          *      - if they overlap, they must not occupy the corners (which are reserved for other widgets)
83          */
84         if (v2d->scroll) {
85                 /* vertical scroller */
86                 if (v2d->scroll & V2D_SCROLL_LEFT) {
87                         /* on left-hand edge of region */
88                         v2d->vert= v2d->mask;
89                         v2d->vert.xmax= V2D_SCROLL_WIDTH;
90                         v2d->mask.xmin= v2d->vert.xmax + 1;
91                 }
92                 else if (v2d->scroll & V2D_SCROLL_RIGHT) {
93                         /* on right-hand edge of region */
94                         v2d->vert= v2d->mask;
95                         v2d->vert.xmin= v2d->vert.xmax - V2D_SCROLL_WIDTH;
96                         v2d->mask.xmax= v2d->vert.xmin - 1;
97                 }
98                 
99                 /* horizontal scroller */
100                 if (v2d->scroll & (V2D_SCROLL_BOTTOM|V2D_SCROLL_BOTTOM_O)) {
101                         /* on bottom edge of region (V2D_SCROLL_BOTTOM_O is outliner, the other is for standard) */
102                         v2d->hor= v2d->mask;
103                         v2d->hor.ymax= V2D_SCROLL_HEIGHT;
104                         v2d->mask.ymin= v2d->hor.ymax + 1;
105                 }
106                 else if (v2d->scroll & V2D_SCROLL_TOP) {
107                         /* on upper edge of region */
108                         v2d->hor= v2d->mask;
109                         v2d->hor.ymin= v2d->hor.ymax - V2D_SCROLL_HEIGHT;
110                         v2d->mask.ymax= v2d->hor.ymin - 1;
111                 }
112                 
113                 /* adjust vertical scroller if there's a horizontal scroller, to leave corner free */
114                 if (v2d->scroll & V2D_SCROLL_VERTICAL) {
115                         /* just set y min/max for vertical scroller to y min/max of mask as appropriate */
116                         if (v2d->scroll & (V2D_SCROLL_BOTTOM|V2D_SCROLL_BOTTOM_O)) {
117                                 /* on bottom edge of region (V2D_SCROLL_BOTTOM_O is outliner, the other is for standard) */
118                                 v2d->vert.ymin= v2d->mask.ymin;
119                         }
120                         else if (v2d->scroll & V2D_SCROLL_TOP) {
121                                 /* on upper edge of region */
122                                 v2d->vert.ymax= v2d->mask.ymax;
123                         }
124                 }
125         }
126         
127         /* initialise data if there is a need for such */
128         if ((v2d->flag & V2D_IS_INITIALISED) == 0) {
129                 v2d->flag |= V2D_IS_INITIALISED;
130                 
131                 /* see eView2D_CommonViewTypes in UI_view2d.h for available view presets */
132                 switch (type) {
133                         /* 'standard view' - from (0,0) to (winx,winy), with other restrictions defined by region already */
134                         case V2D_COMMONVIEW_VIEWCANVAS: 
135                         {
136                                 /* just set 'tot' rect alignment restictions for now */
137                                 v2d->align= V2D_ALIGN_NO_NEG_X|V2D_ALIGN_NO_NEG_Y;
138                                 tot_changed= 1;
139                                 
140                                 // XXX... should we set min/max here too? probably not essential yet
141                         }
142                                 break;
143                         
144                         /* 'list/channel view' - zoom, aspect ratio, and alignment restrictions are set here */
145                         case V2D_COMMONVIEW_LIST:
146                         {
147                                 /* zoom + aspect ratio are locked */
148                                 v2d->keepzoom = (V2D_LOCKZOOM_X|V2D_LOCKZOOM_Y|V2D_KEEPZOOM|V2D_KEEPASPECT);
149                                 v2d->minzoom= v2d->maxzoom= 1.0f;
150                                 
151                                 /* tot rect has strictly regulated placement, and must only occur in +/- quadrant */
152                                 v2d->align = (V2D_ALIGN_NO_NEG_X|V2D_ALIGN_NO_POS_Y);
153                                 v2d->keeptot = V2D_KEEPTOT_STRICT;
154                                 tot_changed= 1;
155                                 
156                                 /* scroller settings are currently not set here... that is left for regions... */
157                         }
158                                 break;
159                                 
160                         /* 'header' regions - zoom, aspect ratio, alignment, and panning restrictions are set here */
161                         case V2D_COMMONVIEW_HEADER:
162                         {
163                                 /* zoom + aspect ratio are locked */
164                                 v2d->keepzoom = (V2D_LOCKZOOM_X|V2D_LOCKZOOM_Y|V2D_KEEPZOOM|V2D_KEEPASPECT);
165                                 v2d->minzoom= v2d->maxzoom= 1.0f;
166                                 v2d->min[0]= v2d->max[0]= winx;
167                                 v2d->min[1]= v2d->max[1]= winy;
168                                 
169                                 /* tot rect has strictly regulated placement, and must only occur in +/+ quadrant */
170                                 v2d->align = (V2D_ALIGN_NO_NEG_X|V2D_ALIGN_NO_NEG_Y);
171                                 v2d->keeptot = V2D_KEEPTOT_STRICT;
172                                 tot_changed= 1;
173                                 
174                                 /* panning in y-axis is prohibited */
175                                 v2d->keepofs= V2D_LOCKOFS_Y;
176                                 
177                                 /* absolutely no scrollers allowed */
178                                 v2d->scroll= 0;
179                         }
180                                 break;
181                                 
182                         /* 'timeline/animeditors' - only set x-axis settings (y axis settings have already been set by regions, so don't overwrite! */
183                         case V2D_COMMONVIEW_TIMELINE:
184                         {
185                                 /* zoom on x-axis is free, but zoom factors are usually standard */
186                                 v2d->minzoom= 0.5f;
187                                 v2d->maxzoom= 10.0f;
188                                 
189                                 /* size limits on x-axis are also standard */
190                                 v2d->min[0]= 0.0f; // XXX... would 1.0f be better?
191                                 v2d->max[0]= MAXFRAMEF;
192                                 
193                                 /* scrollers for x-axis must be shown, and with scales */
194                                 v2d->scroll |= (V2D_SCROLL_BOTTOM|V2D_SCROLL_SCALE_HORIZONTAL);
195                                 
196                                 /* 'tot' rect x-axis size */
197                                 v2d->tot.xmin= (float)(SFRA - 10);
198                                 v2d->tot.xmax= (float)(EFRA + 10);
199                                 v2d->cur.xmin= v2d->mask.xmin;
200                                 v2d->cur.xmax= v2d->mask.xmax;
201                                 tot_changed= 0; // er..
202                         }
203                                 break;
204                                 
205                         /* other view types are completely defined using their own settings already */
206                         default:
207                                 /* we don't do anything here, as settings should be fine, but just make sure that rect */
208                                 break;  
209                 }
210         }
211         
212         /* set 'tot' rect before setting cur? */
213         if (tot_changed) 
214                 UI_view2d_totRect_set(v2d, winx, winy);
215         else
216                 UI_view2d_curRect_validate(v2d);
217 }
218
219 /* Ensure View2D rects remain in a viable configuration 
220  *      - cur is not allowed to be: larger than max, smaller than min, or outside of tot
221  */
222 // XXX pre2.5 -> this used to be called  test_view2d()
223 void UI_view2d_curRect_validate(View2D *v2d)
224 {
225         float totwidth, totheight, curwidth, curheight, width, height;
226         float winx, winy;
227         rctf *cur, *tot;
228         
229         /* use mask as size of region that View2D resides in, as it takes into account scrollbars already  */
230         winx= (float)(v2d->mask.xmax - v2d->mask.xmin + 1);
231         winy= (float)(v2d->mask.ymax - v2d->mask.ymin + 1);
232         
233         /* get pointers to rcts for less typing */
234         cur= &v2d->cur;
235         tot= &v2d->tot;
236         
237         /* we must satisfy the following constraints (in decreasing order of importance):
238          *      - cur must not fall outside of tot
239          *      - axis locks (zoom and offset) must be maintained
240          *      - zoom must not be excessive (check either sizes or zoom values)
241          *      - aspect ratio should be respected (NOTE: this is quite closely realted to zoom too)
242          */
243         
244         /* Step 1: if keepzoom, adjust the sizes of the rects only
245          *      - firstly, we calculate the sizes of the rects
246          *      - curwidth and curheight are saved as reference... modify width and height values here
247          */
248         totwidth= tot->xmax - tot->xmin;
249         totheight= tot->ymax - tot->ymin;
250         curwidth= width= cur->xmax - cur->xmin;
251         curheight= height= cur->ymax - cur->ymin;
252         
253         /* if zoom is locked, size on the appropriate axis is reset to mask size */
254         if (v2d->keepzoom & V2D_LOCKZOOM_X)
255                 width= winx;
256         if (v2d->keepzoom & V2D_LOCKZOOM_Y)
257                 height= winy;
258                 
259         /* keepzoom (V2D_KEEPZOOM set), indicates that zoom level on each axis must not exceed limits 
260          * NOTE: in general, it is not expected that the lock-zoom will be used in conjunction with this
261          */
262         if (v2d->keepzoom & V2D_KEEPZOOM) {
263                 float zoom, fac;
264                 
265                 /* check if excessive zoom on x-axis */
266                 if ((v2d->keepzoom & V2D_LOCKZOOM_X)==0) {
267                         zoom= winx / width;
268                         if ((zoom < v2d->minzoom) || (zoom > v2d->maxzoom)) {
269                                 fac= (zoom < v2d->minzoom) ? (zoom / v2d->minzoom) : (zoom / v2d->maxzoom);
270                                 width *= fac;
271                         }
272                 }
273                 
274                 /* check if excessive zoom on y-axis */
275                 if ((v2d->keepzoom & V2D_LOCKZOOM_Y)==0) {
276                         zoom= winy / height;
277                         if ((zoom < v2d->minzoom) || (zoom > v2d->maxzoom)) {
278                                 fac= (zoom < v2d->minzoom) ? (zoom / v2d->minzoom) : (zoom / v2d->maxzoom);
279                                 height *= fac;
280                         }
281                 }
282         }
283         else {
284                 /* make sure sizes don't exceed that of the min/max sizes (even though we're not doing zoom clamping) */
285                 CLAMP(width, v2d->min[0], v2d->max[0]);
286                 CLAMP(height, v2d->min[1], v2d->max[1]);
287         }
288         
289         /* check if we should restore aspect ratio (if view size changed) */
290         if (v2d->keepzoom & V2D_KEEPASPECT) {
291                 short do_x=0, do_y=0, do_cur, do_win;
292                 float curRatio, winRatio;
293                 
294                 /* when a window edge changes, the aspect ratio can't be used to
295                  * find which is the best new 'cur' rect. thats why it stores 'old' 
296                  */
297                 if (winx != v2d->oldwinx) do_x= 1;
298                 if (winy != v2d->oldwiny) do_y= 1;
299                 
300                 curRatio= height / width;
301                 winRatio= winy / winx;
302                 
303                 /* both sizes change (area/region maximised)  */
304                 if (do_x == do_y) {
305                         if (do_x && do_y) {
306                                 /* here is 1,1 case, so all others must be 0,0 */
307                                 if (ABS(winx - v2d->oldwinx) > ABS(winy - v2d->oldwiny)) do_y= 0;
308                                 else do_x= 0;
309                         }
310                         else if (winRatio > 1.0f) do_x= 0; 
311                         else do_x= 1;
312                 }
313                 do_cur= do_x;
314                 do_win= do_y;
315                 
316                 if (do_cur) {
317                         if ((v2d->keeptot == V2D_KEEPTOT_STRICT) && (winx != v2d->oldwinx)) {
318                                 /* special exception for Outliner (and later channel-lists):
319                                  *      - The view may be moved left to avoid contents being pushed out of view when view shrinks. 
320                                  *      - The keeptot code will make sure cur->xmin will not be less than tot->xmin (which cannot be allowed)
321                                  *      - width is not adjusted for changed ratios here...
322                                  */
323                                 if (winx < v2d->oldwinx) {
324                                         float temp = v2d->oldwinx - winx;
325                                         
326                                         cur->xmin -= temp;
327                                         cur->xmax -= temp;
328                                         
329                                         /* width does not get modified, as keepaspect here is just set to make 
330                                          * sure visible area adjusts to changing view shape! 
331                                          */
332                                 }
333                         }
334                         else {
335                                 /* portrait window: correct for x */
336                                 width= height / winRatio;
337                         }
338                 }
339                 else {
340                         /* landscape window: correct for y */
341                         height = width * winRatio;
342                 }
343                 
344                 /* store region size for next time */
345                 v2d->oldwinx= winx; 
346                 v2d->oldwiny= winy;
347         }
348         
349         /* Step 2: apply new sizes of cur rect to cur rect */
350         if ((width != curwidth) || (height != curheight)) {
351                 float temp, dh;
352                 
353                 /* resize around 'center' of frame */
354                 if (width != curwidth) {
355                         temp= (cur->xmax + cur->xmin) * 0.5f;
356                         dh= width * 0.5f;
357                         
358                         cur->xmin = temp - dh;
359                         cur->xmax = temp + dh;
360                 }
361                 if (height != curheight) {
362                         temp= (cur->ymax + cur->ymin) * 0.5f;
363                         dh= height * 0.5f;
364                         
365                         cur->ymin = temp - dh;
366                         cur->ymax = temp + dh;
367                 }
368         }
369         
370         /* Step 3: adjust so that it doesn't fall outside of bounds of tot */
371         if (v2d->keeptot) {
372                 float temp, diff;
373                 
374                 /* recalculate extents of cur */
375                 curwidth= cur->xmax - cur->xmin;
376                 curheight= cur->ymax - cur->ymin;
377                 
378                 /* width */
379                 if ( (curwidth > totwidth) && !(v2d->keepzoom & (V2D_KEEPZOOM|V2D_LOCKZOOM_X)) ) {
380                         /* if zoom doesn't have to be maintained, just clamp edges */
381                         if (cur->xmin < tot->xmin) cur->xmin= tot->xmin;
382                         if (cur->xmax > tot->xmax) cur->xmax= tot->xmax;
383                 }
384                 else if (v2d->keeptot == V2D_KEEPTOT_STRICT) {
385                         /* This is an exception for the outliner (and later channel-lists, headers) 
386                          *      - must clamp within tot rect (absolutely no excuses)
387                          *      --> therefore, cur->xmin must not be less than tot->xmin
388                          */
389                         if (cur->xmin < tot->xmin) {
390                                 /* move cur across so that it sits at minimum of tot */
391                                 temp= tot->xmin - cur->xmin;
392                                 
393                                 cur->xmin += temp;
394                                 cur->xmax += temp;
395                         }
396                         else if (cur->xmax > tot->xmax) {
397                                 /* - only offset by difference of cur-xmax and tot-xmax if that would not move 
398                                  *      cur-xmin to lie past tot-xmin
399                                  * - otherwise, simply shift to tot-xmin???
400                                  */
401                                 temp= cur->xmax - tot->xmax;
402                                 
403                                 if ((cur->xmin - temp) < tot->xmin) {
404                                         /* only offset by difference from cur-min and tot-min */
405                                         temp= cur->xmin - tot->xmin;
406                                         
407                                         cur->xmin -= temp;
408                                         cur->xmax -= temp;
409                                 }
410                                 else {
411                                         cur->xmin -= temp;
412                                         cur->xmax -= temp;
413                                 }
414                         }
415                 }
416                 else {
417                         /* This here occurs when:
418                          *      - width too big, but maintaining zoom (i.e. widths cannot be changed)
419                          *      - width is OK, but need to check if outside of boundaries
420                          * 
421                          * So, resolution is to just shift view by the gap between the extremities.
422                          * We favour moving the 'minimum' across, as that's origin for most things
423                          * (XXX - in the past, max was favoured... if there are bugs, swap!)
424                          */
425                         if ((cur->ymin < tot->ymin) && (cur->ymax > tot->ymax)) {
426                                 /* outside boundaries on both sides, so take middle-point of tot, and place in balanced way */
427                                 temp= (tot->ymax + tot->ymin) * 0.5f;
428                                 diff= curheight * 0.5f;
429                                 
430                                 cur->ymin= temp - diff;
431                                 cur->ymax= temp + diff;
432                         }
433                         else if (cur->xmin < tot->xmin) {
434                                 /* move cur across so that it sits at minimum of tot */
435                                 temp= tot->xmin - cur->xmin;
436                                 
437                                 cur->xmin += temp;
438                                 cur->xmax += temp;
439                         }
440                         else if (cur->xmax > tot->xmax) {
441                                 /* - only offset by difference of cur-xmax and tot-xmax if that would not move 
442                                  *      cur-xmin to lie past tot-xmin
443                                  * - otherwise, simply shift to tot-xmin???
444                                  */
445                                 temp= cur->xmax - tot->xmax;
446                                 
447                                 if ((cur->xmin - temp) < tot->xmin) {
448                                         /* only offset by difference from cur-min and tot-min */
449                                         temp= cur->xmin - tot->xmin;
450                                         
451                                         cur->xmin -= temp;
452                                         cur->xmax -= temp;
453                                 }
454                                 else {
455                                         cur->xmin -= temp;
456                                         cur->xmax -= temp;
457                                 }
458                         }
459                 }
460                 
461                 /* height */
462                 if ( (curheight > totheight) && !(v2d->keepzoom & (V2D_KEEPZOOM|V2D_LOCKZOOM_Y)) ) {
463                         /* if zoom doesn't have to be maintained, just clamp edges */
464                         if (cur->ymin < tot->ymin) cur->ymin= tot->ymin;
465                         if (cur->ymax > tot->ymax) cur->ymax= tot->ymax;
466                 }
467                 else {
468                         /* This here occurs when:
469                          *      - height too big, but maintaining zoom (i.e. heights cannot be changed)
470                          *      - height is OK, but need to check if outside of boundaries
471                          * 
472                          * So, resolution is to just shift view by the gap between the extremities.
473                          * We favour moving the 'minimum' across, as that's origin for most things
474                          */
475                         if ((cur->ymin < tot->ymin) && (cur->ymax > tot->ymax)) {
476                                 /* outside boundaries on both sides, so take middle-point of tot, and place in balanced way */
477                                 temp= (tot->ymax + tot->ymin) * 0.5f;
478                                 diff= curheight * 0.5f;
479                                 
480                                 cur->ymin= temp - diff;
481                                 cur->ymax= temp + diff;
482                         }
483                         else if (cur->ymin < tot->ymin) {
484                                 /* there's still space remaining, so shift up */
485                                 temp= tot->ymin - cur->ymin;
486                                 
487                                 cur->ymin += temp;
488                                 cur->ymax += temp;
489                         }
490                         else if (cur->ymax > tot->ymax) {
491                                 /* there's still space remaining, so shift down */
492                                 temp= cur->ymax - tot->ymax;
493                                 
494                                 cur->ymin -= temp;
495                                 cur->ymax -= temp;
496                         }
497                 }
498         }
499         
500 }
501
502 /* ------------------ */
503
504 /* Restore 'cur' rect to standard orientation (i.e. optimal maximum view of tot) 
505  * This does not take into account if zooming the view on an axis will improve the view (if allowed)
506  */
507 void UI_view2d_curRect_reset (View2D *v2d)
508 {
509         float width, height;
510         
511         /* assume width and height of 'cur' rect by default, should be same size as mask */
512         width= (float)(v2d->mask.xmax - v2d->mask.xmin + 1);
513         height= (float)(v2d->mask.ymax - v2d->mask.ymin + 1);
514         
515         /* handle width - posx and negx flags are mutually exclusive, so watch out */
516         if ((v2d->align & V2D_ALIGN_NO_POS_X) && !(v2d->align & V2D_ALIGN_NO_NEG_X)) {
517                 /* width is in negative-x half */
518                 v2d->cur.xmin= (float)-width;
519                 v2d->cur.xmax= 0.0f;
520         }
521         else if ((v2d->align & V2D_ALIGN_NO_NEG_X) && !(v2d->align & V2D_ALIGN_NO_POS_X)) {
522                 /* width is in positive-x half */
523                 v2d->cur.xmin= 0.0f;
524                 v2d->cur.xmax= (float)width;
525         }
526         else {
527                 /* width is centered around x==0 */
528                 const float dx= (float)width / 2.0f;
529                 
530                 v2d->cur.xmin= -dx;
531                 v2d->cur.xmax= dx;
532         }
533         
534         /* handle height - posx and negx flags are mutually exclusive, so watch out */
535         if ((v2d->align & V2D_ALIGN_NO_POS_Y) && !(v2d->align & V2D_ALIGN_NO_NEG_Y)) {
536                 /* height is in negative-y half */
537                 v2d->cur.ymin= (float)-height;
538                 v2d->cur.ymax= 0.0f;
539         }
540         else if ((v2d->align & V2D_ALIGN_NO_NEG_Y) && !(v2d->align & V2D_ALIGN_NO_POS_Y)) {
541                 /* height is in positive-y half */
542                 v2d->cur.ymin= 0.0f;
543                 v2d->cur.ymax= (float)height;
544         }
545         else {
546                 /* height is centered around y==0 */
547                 const float dy= (float)height / 2.0f;
548                 
549                 v2d->cur.ymin= -dy;
550                 v2d->cur.ymax= dy;
551         }
552 }
553
554 /* ------------------ */
555
556 /* Change the size of the maximum viewable area (i.e. 'tot' rect) */
557 void UI_view2d_totRect_set (View2D *v2d, int width, int height)
558 {
559         /* don't do anything if either value is 0 */
560         if (ELEM3(0, v2d, width, height))
561                 return;
562         
563         /* handle width - posx and negx flags are mutually exclusive, so watch out */
564         if ((v2d->align & V2D_ALIGN_NO_POS_X) && !(v2d->align & V2D_ALIGN_NO_NEG_X)) {
565                 /* width is in negative-x half */
566                 v2d->tot.xmin= (float)-width;
567                 v2d->tot.xmax= 0.0f;
568         }
569         else if ((v2d->align & V2D_ALIGN_NO_NEG_X) && !(v2d->align & V2D_ALIGN_NO_POS_X)) {
570                 /* width is in positive-x half */
571                 v2d->tot.xmin= 0.0f;
572                 v2d->tot.xmax= (float)width;
573         }
574         else {
575                 /* width is centered around x==0 */
576                 const float dx= (float)width / 2.0f;
577                 
578                 v2d->tot.xmin= -dx;
579                 v2d->tot.xmax= dx;
580         }
581         
582         /* handle height - posx and negx flags are mutually exclusive, so watch out */
583         if ((v2d->align & V2D_ALIGN_NO_POS_Y) && !(v2d->align & V2D_ALIGN_NO_NEG_Y)) {
584                 /* height is in negative-y half */
585                 v2d->tot.ymin= (float)-height;
586                 v2d->tot.ymax= 0.0f;
587         }
588         else if ((v2d->align & V2D_ALIGN_NO_NEG_Y) && !(v2d->align & V2D_ALIGN_NO_POS_Y)) {
589                 /* height is in positive-y half */
590                 v2d->tot.ymin= 0.0f;
591                 v2d->tot.ymax= (float)height;
592         }
593         else {
594                 /* height is centered around y==0 */
595                 const float dy= (float)height / 2.0f;
596                 
597                 v2d->tot.ymin= -dy;
598                 v2d->tot.ymax= dy;
599         }
600         
601         /* make sure that 'cur' rect is in a valid state as a result of these changes */
602         UI_view2d_curRect_validate(v2d);
603 }
604
605 /* *********************************************************************** */
606 /* View Matrix Setup */
607
608 /* mapping function to ensure 'cur' draws extended over the area where sliders are */
609 static void view2d_map_cur_using_mask(View2D *v2d, rctf *curmasked)
610 {
611         *curmasked= v2d->cur;
612         
613         if ((v2d->scroll)) {
614                 float dx= (v2d->cur.xmax-v2d->cur.xmin)/((float)(v2d->mask.xmax-v2d->mask.xmin+1));
615                 float dy= (v2d->cur.ymax-v2d->cur.ymin)/((float)(v2d->mask.ymax-v2d->mask.ymin+1));
616                 
617                 if (v2d->mask.xmin != 0)
618                         curmasked->xmin -= dx*(float)v2d->mask.xmin;
619                 if (v2d->mask.xmax+1 != v2d->winx)
620                         curmasked->xmax += dx*(float)(v2d->winx - v2d->mask.xmax-1);
621                 
622                 if (v2d->mask.ymin != 0)
623                         curmasked->ymin -= dy*(float)v2d->mask.ymin;
624                 if (v2d->mask.ymax+1 != v2d->winy)
625                         curmasked->ymax += dy*(float)(v2d->winy - v2d->mask.ymax-1);
626                 
627         }
628 }
629
630 /* Set view matrices to use 'cur' rect as viewing frame for View2D drawing 
631 *       - this assumes viewport/scissor been set for the region, taking scrollbars into account
632 */
633
634 void UI_view2d_view_ortho(const bContext *C, View2D *v2d)
635 {
636         rctf curmasked;
637         
638         /* set the matrix - pixel offsets (-0.375) for 1:1 correspondance are not applied, 
639          * as they were causing some unwanted offsets when drawing 
640          */
641         view2d_map_cur_using_mask(v2d, &curmasked);
642         wmOrtho2(C->window, curmasked.xmin, curmasked.xmax, curmasked.ymin, curmasked.ymax);
643         
644         /* XXX is this necessary? */
645         wmLoadIdentity(C->window);
646 }
647
648 /* Set view matrices to only use one axis of 'cur' only
649  *      - this assumes viewport/scissor been set for the region, taking scrollbars into account
650  *
651  *      - xaxis         = if non-zero, only use cur x-axis, otherwise use cur-yaxis (mostly this will be used for x)
652  */
653 void UI_view2d_view_orthoSpecial(const bContext *C, View2D *v2d, short xaxis)
654 {
655         ARegion *ar= C->region;
656         rctf curmasked;
657         
658         /* set the matrix - pixel offsets (-0.375) for 1:1 correspondance are not applied, 
659          * as they were causing some unwanted offsets when drawing 
660          */
661         view2d_map_cur_using_mask(v2d, &curmasked);
662         if (xaxis)
663                 wmOrtho2(C->window, curmasked.xmin, curmasked.xmax, 0, ar->winy);
664         else
665                 wmOrtho2(C->window, 0, ar->winx, curmasked.ymin, curmasked.ymax);
666                 
667         /* XXX is this necessary? */
668         wmLoadIdentity(C->window);
669
670
671
672 /* Restore view matrices after drawing */
673 void UI_view2d_view_restore(const bContext *C)
674 {
675         ED_region_pixelspace(C, C->region);
676 }
677
678 /* *********************************************************************** */
679 /* Gridlines */
680
681 /* minimum pixels per gridstep */
682 #define MINGRIDSTEP     35
683
684 /* View2DGrid is typedef'd in UI_view2d.h */
685 struct View2DGrid {
686         float dx, dy;                   /* stepsize (in pixels) between gridlines */
687         float startx, starty;   /* initial coordinates to start drawing grid from */
688         int powerx, powery;             /* step as power of 10 */
689 };
690
691 /* --------------- */
692
693 /* try to write step as a power of 10 */
694 static void step_to_grid(float *step, int *power, int unit)
695 {
696         const float loga= log10(*step);
697         float rem;
698         
699         *power= (int)(loga);
700         
701         rem= loga - (*power);
702         rem= pow(10.0, rem);
703         
704         if (loga < 0.0) {
705                 if (rem < 0.2) rem= 0.2;
706                 else if(rem < 0.5) rem= 0.5;
707                 else rem= 1.0;
708                 
709                 *step= rem * pow(10.0, (float)(*power));
710                 
711                 /* for frames, we want 1.0 frame intervals only */
712                 if (unit == V2D_UNIT_FRAMES) {
713                         rem = 1.0;
714                         *step = 1.0;
715                 }
716                 
717                 /* prevents printing 1.0 2.0 3.0 etc */
718                 if (rem == 1.0) (*power)++;     
719         }
720         else {
721                 if (rem < 2.0) rem= 2.0;
722                 else if(rem < 5.0) rem= 5.0;
723                 else rem= 10.0;
724                 
725                 *step= rem * pow(10.0, (float)(*power));
726                 
727                 (*power)++;
728                 /* prevents printing 1.0, 2.0, 3.0, etc. */
729                 if (rem == 10.0) (*power)++;    
730         }
731 }
732
733 /* Intialise settings necessary for drawing gridlines in a 2d-view 
734  *      - Currently, will return pointer to View2DGrid struct that needs to 
735  *        be freed with UI_view2d_grid_free()
736  *      - Is used for scrollbar drawing too (for units drawing)
737  *      - Units + clamping args will be checked, to make sure they are valid values that can be used
738  *        so it is very possible that we won't return grid at all!
739  *      
740  *      - xunits,yunits = V2D_UNIT_*  grid steps in seconds or frames 
741  *      - xclamp,yclamp = V2D_CLAMP_* only show whole-number intervals
742  *      - winx                  = width of region we're drawing to
743  *      - winy                  = height of region we're drawing into
744  */
745 View2DGrid *UI_view2d_grid_calc(const bContext *C, View2D *v2d, short xunits, short xclamp, short yunits, short yclamp, int winx, int winy)
746 {
747         View2DGrid *grid;
748         float space, pixels, seconddiv;
749         int secondgrid;
750         
751         /* check that there are at least some workable args */
752         if (ELEM(V2D_ARG_DUMMY, xunits, xclamp) && ELEM(V2D_ARG_DUMMY, yunits, yclamp))
753                 return NULL;
754         
755         /* grid here is allocated... */
756         grid= MEM_callocN(sizeof(View2DGrid), "View2DGrid");
757         
758         /* rule: gridstep is minimal GRIDSTEP pixels */
759         if (xunits == V2D_UNIT_SECONDS) {
760                 secondgrid= 1;
761                 seconddiv= 0.01f * FPS;
762         }
763         else {
764                 secondgrid= 0;
765                 seconddiv= 1.0f;
766         }
767         
768         /* calculate x-axis grid scale (only if both args are valid) */
769         if (ELEM(V2D_ARG_DUMMY, xunits, xclamp) == 0) {
770                 space= v2d->cur.xmax - v2d->cur.xmin;
771                 pixels= v2d->mask.xmax - v2d->mask.xmin;
772                 
773                 grid->dx= (MINGRIDSTEP * space) / (seconddiv * pixels);
774                 step_to_grid(&grid->dx, &grid->powerx, xunits);
775                 grid->dx *= seconddiv;
776                 
777                 if (xclamp == V2D_GRID_CLAMP) {
778                         if (grid->dx < 0.1f) grid->dx= 0.1f;
779                         grid->powerx-= 2;
780                         if (grid->powerx < -2) grid->powerx= -2;
781                 }
782                 
783                 grid->startx= v2d->cur.xmin;
784         }
785         
786         /* calculate y-axis grid scale (only if both args are valid) */
787         if (ELEM(V2D_ARG_DUMMY, yunits, yclamp) == 0) {
788                 space= v2d->cur.ymax - v2d->cur.ymin;
789                 pixels= winy;
790                 
791                 grid->dy= MINGRIDSTEP * space / pixels;
792                 step_to_grid(&grid->dy, &grid->powery, yunits);
793                 
794                 if (yclamp == V2D_GRID_CLAMP) {
795                         if (grid->dy < 1.0f) grid->dy= 1.0f;
796                         if (grid->powery < 1) grid->powery= 1;
797                 }
798                 
799                 grid->starty= v2d->cur.ymin;
800         }
801         
802         /* calculate start position */
803         if (ELEM(V2D_ARG_DUMMY, xunits, xclamp) == 0) {
804                 grid->startx= seconddiv*(v2d->cur.xmin/seconddiv - fmod(v2d->cur.xmin/seconddiv, grid->dx/seconddiv));
805                 if (v2d->cur.xmin < 0.0f) grid->startx-= grid->dx;
806         }
807         if (ELEM(V2D_ARG_DUMMY, yunits, yclamp) == 0) {
808                 grid->starty= (v2d->cur.ymin - fmod(v2d->cur.ymin, grid->dy));
809                 if (v2d->cur.ymin < 0.0f) grid->starty-= grid->dy;
810         }
811         
812         return grid;
813 }
814
815 /* Draw gridlines in the given 2d-region */
816 void UI_view2d_grid_draw(const bContext *C, View2D *v2d, View2DGrid *grid, int flag)
817 {
818         float vec1[2], vec2[2];
819         int a, step;
820         
821         /* check for grid first, as it may not exist */
822         if (grid == NULL)
823                 return;
824         
825         /* vertical lines */
826         if (flag & V2D_VERTICAL_LINES) {
827                 /* initialise initial settings */
828                 vec1[0]= vec2[0]= grid->startx;
829                 vec1[1]= grid->starty;
830                 vec2[1]= v2d->cur.ymax;
831                 
832                 /* minor gridlines */
833                 step= (v2d->mask.xmax - v2d->mask.xmin + 1) / MINGRIDSTEP;
834                 UI_ThemeColor(TH_GRID);
835                 
836                 for (a=0; a<step; a++) {
837                         glBegin(GL_LINE_STRIP);
838                                 glVertex2fv(vec1); 
839                                 glVertex2fv(vec2);
840                         glEnd();
841                         
842                         vec2[0]= vec1[0]+= grid->dx;
843                 }
844                 
845                 /* major gridlines */
846                 vec2[0]= vec1[0]-= 0.5f*grid->dx;
847                 UI_ThemeColorShade(TH_GRID, 16);
848                 
849                 step++;
850                 for (a=0; a<=step; a++) {
851                         glBegin(GL_LINE_STRIP);
852                                 glVertex2fv(vec1); 
853                                 glVertex2fv(vec2);
854                         glEnd();
855                         
856                         vec2[0]= vec1[0]-= grid->dx;
857                 }
858         }
859         
860         /* horizontal lines */
861         if (flag & V2D_HORIZONTAL_LINES) {
862                 /* only major gridlines */
863                 vec1[1]= vec2[1]= grid->starty;
864                 vec1[0]= grid->startx;
865                 vec2[0]= v2d->cur.xmax;
866                 
867                 step= (v2d->mask.ymax - v2d->mask.ymin + 1) / MINGRIDSTEP;
868                 
869                 UI_ThemeColor(TH_GRID);
870                 for (a=0; a<=step; a++) {
871                         glBegin(GL_LINE_STRIP);
872                                 glVertex2fv(vec1); 
873                                 glVertex2fv(vec2);
874                         glEnd();
875                         
876                         vec2[1]= vec1[1]+= grid->dy;
877                 }
878                 
879                 /* fine grid lines */
880                 vec2[1]= vec1[1]-= 0.5f*grid->dy;
881                 step++;
882                 
883                 if (flag & V2D_HORIZONTAL_FINELINES) { 
884                         UI_ThemeColorShade(TH_GRID, 16);
885                         for (a=0; a<step; a++) {
886                                 glBegin(GL_LINE_STRIP);
887                                         glVertex2fv(vec1); 
888                                         glVertex2fv(vec2);
889                                 glEnd();
890                                 
891                                 vec2[1]= vec1[1]-= grid->dy;
892                         }
893                 }
894         }
895         
896         /* Axes are drawn as darker lines */
897         UI_ThemeColorShade(TH_GRID, -50);
898         
899         /* horizontal axis */
900         if (flag & V2D_HORIZONTAL_AXIS) {
901                 vec1[0]= v2d->cur.xmin;
902                 vec2[0]= v2d->cur.xmax;
903                 vec1[1]= vec2[1]= 0.0f;
904                 
905                 glBegin(GL_LINE_STRIP);
906                         glVertex2fv(vec1);
907                         glVertex2fv(vec2);
908                 glEnd();
909         }
910         
911         /* vertical axis */
912         if (flag & V2D_VERTICAL_AXIS) {
913                 vec1[1]= v2d->cur.ymin;
914                 vec2[1]= v2d->cur.ymax;
915                 vec1[0]= vec2[0]= 0.0f;
916                 
917                 glBegin(GL_LINE_STRIP);
918                         glVertex2fv(vec1); 
919                         glVertex2fv(vec2);
920                 glEnd();
921         }
922 }
923
924 /* free temporary memory used for drawing grid */
925 void UI_view2d_grid_free(View2DGrid *grid)
926 {
927         /* only free if there's a grid */
928         if (grid)
929                 MEM_freeN(grid);
930 }
931
932 /* *********************************************************************** */
933 /* Scrollbars */
934
935 /* View2DScrollers is typedef'd in UI_view2d.h 
936  * WARNING: the start of this struct must not change, as view2d_ops.c uses this too. 
937  *                 For now, we don't need to have a separate (internal) header for structs like this...
938  */
939 struct View2DScrollers {        
940                 /* focus bubbles */
941         int vert_min, vert_max; /* vertical scrollbar */
942         int hor_min, hor_max;   /* horizontal scrollbar */
943         
944                 /* scales */
945         View2DGrid *grid;               /* grid for coordinate drawing */
946         short xunits, xclamp;   /* units and clamping options for x-axis */
947         short yunits, yclamp;   /* units and clamping options for y-axis */
948 };
949
950 /* Calculate relevant scroller properties */
951 View2DScrollers *UI_view2d_scrollers_calc(const bContext *C, View2D *v2d, short xunits, short xclamp, short yunits, short yclamp)
952 {
953         View2DScrollers *scrollers;
954         rcti vert, hor;
955         float fac, totsize, scrollsize;
956         
957         vert= v2d->vert;
958         hor= v2d->hor;
959         
960         /* scrollers is allocated here... */
961         scrollers= MEM_callocN(sizeof(View2DScrollers), "View2DScrollers");
962         
963         /* scroller 'buttons':
964          *      - These should always remain within the visible region of the scrollbar
965          *      - They represent the region of 'tot' that is visible in 'cur'
966          */
967         
968         /* horizontal scrollers */
969         if (v2d->scroll & V2D_SCROLL_HORIZONTAL) {
970                 /* scroller 'button' extents */
971                 totsize= v2d->tot.xmax - v2d->tot.xmin;
972                 scrollsize= hor.xmax - hor.xmin;
973                 
974                 fac= (v2d->cur.xmin- v2d->tot.xmin) / totsize;
975                 scrollers->hor_min= hor.xmin + (fac * scrollsize);
976                 
977                 fac= (v2d->cur.xmax - v2d->tot.xmin) / totsize;
978                 scrollers->hor_max= hor.xmin + (fac * scrollsize);
979                 
980                 if (scrollers->hor_min > scrollers->hor_max) 
981                         scrollers->hor_min= scrollers->hor_max;
982         }
983         
984         /* vertical scrollers */
985         if (v2d->scroll & V2D_SCROLL_VERTICAL) {
986                 /* scroller 'button' extents */
987                 totsize= v2d->tot.ymax - v2d->tot.ymin;
988                 scrollsize= vert.ymax - vert.ymin;
989                 
990                 fac= (v2d->cur.ymin- v2d->tot.ymin) / totsize;
991                 scrollers->vert_min= vert.ymin + (fac * scrollsize);
992                 
993                 fac= (v2d->cur.ymax - v2d->tot.ymin) / totsize;
994                 scrollers->vert_max= vert.ymin + (fac * scrollsize);
995                 
996                 if (scrollers->vert_min > scrollers->vert_max) 
997                         scrollers->vert_min= scrollers->vert_max;
998         }
999         
1000         /* grid markings on scrollbars */
1001         if (v2d->scroll & (V2D_SCROLL_SCALE_HORIZONTAL|V2D_SCROLL_SCALE_VERTICAL)) {
1002                 /* store clamping */
1003                 scrollers->xclamp= xclamp;
1004                 scrollers->xunits= xunits;
1005                 scrollers->yclamp= yclamp;
1006                 scrollers->yunits= yunits;
1007                 
1008                 scrollers->grid= UI_view2d_grid_calc(C, v2d, xunits, xclamp, yunits, yclamp, (hor.xmax - hor.xmin), (vert.ymax - vert.ymin));
1009         }
1010         
1011         /* return scrollers */
1012         return scrollers;
1013 }
1014
1015 /* Print scale marking along a time scrollbar */
1016 static void scroll_printstr(View2DScrollers *scrollers, float x, float y, float val, int power, short unit, char dir)
1017 {
1018         int len;
1019         char str[32];
1020         
1021         /* adjust the scale unit to work ok */
1022         if (dir == 'v') {
1023                 /* here we bump up the power by factor of 10, as 
1024                  * rotation values (hence 'degrees') are divided by 10 to 
1025                  * be able to show the curves at the same time
1026                  */
1027                 if ELEM(unit, V2D_UNIT_DEGREES, V2D_UNIT_TIME) {
1028                         power += 1;
1029                         val *= 10;
1030                 }
1031         }
1032         
1033         /* get string to print */
1034         if (unit == V2D_UNIT_SECONDS) {
1035                 /* SMPTE timecode style:
1036                  *      - In general, minutes and seconds should be shown, as most clips will be
1037                  *        within this length. Hours will only be included if relevant.
1038                  *      - Only show frames when zoomed in enough for them to be relevant 
1039                  *        (using separator convention of ';' for frames, ala QuickTime).
1040                  *        When showing frames, use slightly different display to avoid confusion with mm:ss format
1041                  */
1042                 int hours=0, minutes=0, seconds=0, frames=0;
1043                 char neg[2]= "";
1044                 
1045                 /* get values */
1046                 if (val < 0) {
1047                         /* correction for negative values */
1048                         sprintf(neg, "-");
1049                         val = -val;
1050                 }
1051                 if (val >= 3600) {
1052                         /* hours */
1053                         /* XXX should we only display a single digit for hours since clips are 
1054                          *         VERY UNLIKELY to be more than 1-2 hours max? However, that would 
1055                          *         go against conventions...
1056                          */
1057                         hours= (int)val / 3600;
1058                         val= fmod(val, 3600);
1059                 }
1060                 if (val >= 60) {
1061                         /* minutes */
1062                         minutes= (int)val / 60;
1063                         val= fmod(val, 60);
1064                 }
1065                 if (power <= 0) {
1066                         /* seconds + frames
1067                          *      Frames are derived from 'fraction' of second. We need to perform some additional rounding
1068                          *      to cope with 'half' frames, etc., which should be fine in most cases
1069                          */
1070                         seconds= (int)val;
1071                         frames= (int)floor( ((val - seconds) * FPS) + 0.5f );
1072                 }
1073                 else {
1074                         /* seconds (with pixel offset) */
1075                         seconds= (int)floor(val + 0.375f);
1076                 }
1077                 
1078                 /* print timecode to temp string buffer */
1079                 if (power <= 0) {
1080                         /* include "frames" in display */
1081                         if (hours) sprintf(str, "%s%02d:%02d:%02d;%02d", neg, hours, minutes, seconds, frames);
1082                         else if (minutes) sprintf(str, "%s%02d:%02d;%02d", neg, minutes, seconds, frames);
1083                         else sprintf(str, "%s%d;%02d", neg, seconds, frames);
1084                 }
1085                 else {
1086                         /* don't include 'frames' in display */
1087                         if (hours) sprintf(str, "%s%02d:%02d:%02d", neg, hours, minutes, seconds);
1088                         else sprintf(str, "%s%02d:%02d", neg, minutes, seconds);
1089                 }
1090         }
1091         else {
1092                 /* round to whole numbers if power is >= 1 (i.e. scale is coarse) */
1093                 if (power <= 0) sprintf(str, "%.*f", 1-power, val);
1094                 else sprintf(str, "%d", (int)floor(val + 0.375f));
1095         }
1096         
1097         /* get length of string, and adjust printing location to fit it into the horizontal scrollbar */
1098         len= strlen(str);
1099         if (dir == 'h') {
1100                 /* seconds/timecode display has slightly longer strings... */
1101                 if (unit == V2D_UNIT_SECONDS)
1102                         x-= 3*len;
1103                 else
1104                         x-= 4*len;
1105         }
1106         
1107         /* Add degree sympbol to end of string for vertical scrollbar? */
1108         if ((dir == 'v') && (unit == V2D_UNIT_DEGREES)) {
1109                 str[len]= 186;
1110                 str[len+1]= 0;
1111         }
1112         
1113         /* draw it */
1114         ui_rasterpos_safe(x, y, 1.0);
1115         UI_DrawString(G.fonts, str, 0); // XXX check this again when new text-drawing api is done
1116 }
1117
1118 /* local defines for scrollers drawing */
1119         /* radius of scroller 'button' caps */
1120 #define V2D_SCROLLCAP_RAD               5
1121         /* shading factor for scroller 'bar' */
1122 #define V2D_SCROLLBAR_SHADE             0.1f
1123         /* shading factor for scroller 'button' caps */
1124 #define V2D_SCROLLCAP_SHADE             0.2f
1125
1126 /* Draw scrollbars in the given 2d-region */
1127 void UI_view2d_scrollers_draw(const bContext *C, View2D *v2d, View2DScrollers *vs)
1128 {
1129         const short darker= -50, dark= -10, light= 20, lighter= 50;
1130         rcti vert, hor, corner;
1131         
1132         /* make copies of rects for less typing */
1133         vert= v2d->vert;
1134         hor= v2d->hor;
1135         
1136         /* horizontal scrollbar */
1137         if (v2d->scroll & V2D_SCROLL_HORIZONTAL) {
1138                 /* scroller backdrop */
1139                 UI_ThemeColorShade(TH_SHADE1, light);
1140                 glRecti(hor.xmin,  hor.ymin,  hor.xmax,  hor.ymax);
1141                 
1142                 /* scroller 'button' 
1143                  *      - if view is zoomable in x, draw handles too 
1144                  *      - handles are drawn darker
1145                  */
1146                 if (v2d->keepzoom & V2D_LOCKZOOM_X) {
1147                         /* draw base bar as rounded shape */
1148                         UI_ThemeColorShade(TH_SHADE1, dark);
1149                         uiSetRoundBox(15);
1150                         
1151                         /* check that box is large enough for round drawing */
1152                         if ((vs->hor_max - vs->hor_min) < (V2D_SCROLLCAP_RAD * 2)) {
1153                                 /* Rounded box still gets drawn at the minimum size limit
1154                                  * This doesn't represent extreme scaling well, but looks nicer...
1155                                  */
1156                                 float mid= 0.5f * (vs->hor_max + vs->hor_min);
1157                                 
1158                                 gl_round_box_shade(GL_POLYGON, 
1159                                         mid-V2D_SCROLLCAP_RAD, hor.ymin+2, 
1160                                         mid+V2D_SCROLLCAP_RAD, hor.ymax-2, 
1161                                         V2D_SCROLLCAP_RAD, V2D_SCROLLBAR_SHADE, -V2D_SCROLLBAR_SHADE);
1162                         }
1163                         else {
1164                                 /* draw rounded box as per normal */
1165                                 gl_round_box_shade(GL_POLYGON, 
1166                                         vs->hor_min, hor.ymin+2, 
1167                                         vs->hor_max, hor.ymax-2, 
1168                                         V2D_SCROLLCAP_RAD, V2D_SCROLLBAR_SHADE, -V2D_SCROLLBAR_SHADE);
1169                         }
1170                 }
1171                 else {
1172                         /* base bar drawn as shaded rect */
1173                         UI_ThemeColorShade(TH_SHADE1, dark);
1174                         uiSetRoundBox(0);
1175                         gl_round_box_shade(GL_POLYGON, 
1176                                 vs->hor_min, hor.ymin+2, 
1177                                 vs->hor_max, hor.ymax-2, 
1178                                 V2D_SCROLLCAP_RAD, V2D_SCROLLBAR_SHADE, -V2D_SCROLLBAR_SHADE);
1179                         
1180                         /* 'minimum' handle */
1181                         uiSetRoundBox(9);
1182                         UI_ThemeColorShade(TH_SHADE1, darker);
1183                         
1184                         gl_round_box_shade(GL_POLYGON, 
1185                                 vs->hor_min-V2D_SCROLLER_HANDLE_SIZE, hor.ymin+2, 
1186                                 vs->hor_min+V2D_SCROLLER_HANDLE_SIZE, hor.ymax-2, 
1187                                 V2D_SCROLLCAP_RAD, V2D_SCROLLCAP_SHADE, -V2D_SCROLLCAP_SHADE);
1188                         
1189                         /* maximum handle */
1190                         uiSetRoundBox(6);
1191                         UI_ThemeColorShade(TH_SHADE1, darker);
1192                         
1193                         gl_round_box_shade(GL_POLYGON, 
1194                                 vs->hor_max-V2D_SCROLLER_HANDLE_SIZE, hor.ymin+2, 
1195                                 vs->hor_max+V2D_SCROLLER_HANDLE_SIZE, hor.ymax-2, 
1196                                 V2D_SCROLLCAP_RAD, V2D_SCROLLCAP_SHADE, -V2D_SCROLLCAP_SHADE);
1197                 }
1198                 
1199                 /* scale indicators */
1200                 // XXX will need to update the font drawing when the new stuff comes in
1201                 if ((v2d->scroll & V2D_SCROLL_SCALE_HORIZONTAL) && (vs->grid)) {
1202                         View2DGrid *grid= vs->grid;
1203                         float fac, dfac, fac2, val;
1204                         
1205                         /* the numbers: convert grid->startx and -dx to scroll coordinates 
1206                          *      - fac is x-coordinate to draw to
1207                          *      - dfac is gap between scale markings
1208                          */
1209                         fac= (grid->startx - v2d->cur.xmin) / (v2d->cur.xmax - v2d->cur.xmin);
1210                         fac= hor.xmin + fac*(hor.xmax - hor.xmin);
1211                         
1212                         dfac= (grid->dx) / (v2d->cur.xmax - v2d->cur.xmin);
1213                         dfac= dfac * (hor.xmax - hor.xmin);
1214                         
1215                         /* set starting value, and text color */
1216                         UI_ThemeColor(TH_TEXT);
1217                         val= grid->startx;
1218                         
1219                         /* if we're clamping to whole numbers only, make sure entries won't be repeated */
1220                         if (vs->xclamp == V2D_GRID_CLAMP) {
1221                                 while (grid->dx < 0.9999f) {
1222                                         grid->dx *= 2.0f;
1223                                         dfac *= 2.0f;
1224                                 }
1225                         }
1226                         if (vs->xunits == V2D_UNIT_FRAMES)
1227                                 grid->powerx= 1;
1228                         
1229                         /* draw numbers in the appropriate range */
1230                         if (dfac != 0.0f) {
1231                                 for (; fac < hor.xmax; fac+=dfac, val+=grid->dx) {
1232                                         switch (vs->xunits) {
1233                                                 case V2D_UNIT_FRAMES:           /* frames (as whole numbers)*/
1234                                                         scroll_printstr(vs, fac, 3.0+(float)(hor.ymin), val, grid->powerx, V2D_UNIT_FRAMES, 'h');
1235                                                         break;
1236                                                 
1237                                                 case V2D_UNIT_SECONDS:          /* seconds */
1238                                                         fac2= val/FPS;
1239                                                         scroll_printstr(vs, fac, 3.0+(float)(hor.ymin), fac2, grid->powerx, V2D_UNIT_SECONDS, 'h');
1240                                                         break;
1241                                                         
1242                                                 case V2D_UNIT_SECONDSSEQ:       /* seconds with special calculations (only used for sequencer only) */
1243                                                 {
1244                                                         float time;
1245                                                         
1246                                                         fac2= val/FPS;
1247                                                         time= floor(fac2);
1248                                                         fac2= fac2-time;
1249                                                         
1250                                                         scroll_printstr(vs, fac, 3.0+(float)(hor.ymin), time+FPS*fac2/100.0, grid->powerx, V2D_UNIT_SECONDSSEQ, 'h');
1251                                                 }
1252                                                         break;
1253                                                         
1254                                                 case V2D_UNIT_DEGREES:          /* IPO-Editor for rotation IPO-Drivers */
1255                                                         /* HACK: although we're drawing horizontal, we make this draw as 'vertical', just to get degree signs */
1256                                                         scroll_printstr(vs, fac, 3.0+(float)(hor.ymin), val, grid->powerx, V2D_UNIT_DEGREES, 'v');
1257                                                         break;
1258                                         }
1259                                 }
1260                         }
1261                 }
1262                 
1263                 /* decoration outer bevel line */
1264                 UI_ThemeColorShade(TH_SHADE1, lighter);
1265                 if (v2d->scroll & (V2D_SCROLL_BOTTOM|V2D_SCROLL_BOTTOM_O))
1266                         sdrawline(hor.xmin, hor.ymax, hor.xmax, hor.ymax);
1267                 else if (v2d->scroll & V2D_SCROLL_TOP)
1268                         sdrawline(hor.xmin, hor.ymin, hor.xmax, hor.ymin);
1269         }
1270         
1271         /* vertical scrollbar */
1272         if (v2d->scroll & V2D_SCROLL_VERTICAL) {
1273                 /* scroller backdrop  */
1274                 UI_ThemeColorShade(TH_SHADE1, light);
1275                 glRecti(vert.xmin,  vert.ymin,  vert.xmax,  vert.ymax);
1276                 
1277                 /* scroller 'button' 
1278                  *      - if view is zoomable in y, draw handles too 
1279                  *      - handles are drawn darker
1280                  */
1281                 if (v2d->keepzoom & V2D_LOCKZOOM_Y) {
1282                         /* draw base bar as rounded shape */
1283                         UI_ThemeColorShade(TH_SHADE1, dark);
1284                         uiSetRoundBox(15);
1285                         
1286                         /* check that box is large enough for round drawing */
1287                         if ((vs->vert_max - vs->vert_min) < (V2D_SCROLLCAP_RAD * 2)) {
1288                                 /* Rounded box still gets drawn at the minimum size limit
1289                                  * This doesn't represent extreme scaling well, but looks nicer...
1290                                  */
1291                                 float mid= 0.5f * (vs->vert_max + vs->vert_min);
1292                                 
1293                                 gl_round_box_vertical_shade(GL_POLYGON, 
1294                                         vert.xmin+2, mid-V2D_SCROLLCAP_RAD, 
1295                                         vert.xmax-2, mid+V2D_SCROLLCAP_RAD, 
1296                                         V2D_SCROLLCAP_RAD, V2D_SCROLLBAR_SHADE, -V2D_SCROLLBAR_SHADE);
1297                         }
1298                         else {
1299                                 /* draw rounded box as per normal */
1300                                 gl_round_box_vertical_shade(GL_POLYGON, 
1301                                         vert.xmin+2, vs->vert_min, 
1302                                         vert.xmax-2, vs->vert_max, 
1303                                         V2D_SCROLLCAP_RAD, V2D_SCROLLBAR_SHADE, -V2D_SCROLLBAR_SHADE);
1304                         }
1305                 }
1306                 else {
1307                         /* base bar drawn as shaded rect */
1308                         UI_ThemeColorShade(TH_SHADE1, dark);
1309                         uiSetRoundBox(0);
1310                         gl_round_box_vertical_shade(GL_POLYGON, 
1311                                 vert.xmin+2, vs->vert_min, 
1312                                 vert.xmax-2, vs->vert_max,
1313                                 V2D_SCROLLCAP_RAD, V2D_SCROLLBAR_SHADE, -V2D_SCROLLBAR_SHADE);
1314                         
1315                         /* 'minimum' handle */
1316                         UI_ThemeColorShade(TH_SHADE1, darker);
1317                         uiSetRoundBox(12);
1318                         
1319                         gl_round_box_vertical_shade(GL_POLYGON, 
1320                                 vert.xmin+2, vs->vert_min-V2D_SCROLLER_HANDLE_SIZE, 
1321                                 vert.xmax-2, vs->vert_min+V2D_SCROLLER_HANDLE_SIZE, 
1322                                 V2D_SCROLLCAP_RAD, V2D_SCROLLCAP_SHADE, -V2D_SCROLLCAP_SHADE);
1323                         
1324                         /* maximum handle */
1325                         UI_ThemeColorShade(TH_SHADE1, darker);
1326                         uiSetRoundBox(3);
1327                         
1328                         gl_round_box_vertical_shade(GL_POLYGON, 
1329                                 vert.xmin+2, vs->vert_max-V2D_SCROLLER_HANDLE_SIZE, 
1330                                 vert.xmax-2, vs->vert_max+V2D_SCROLLER_HANDLE_SIZE, 
1331                                 V2D_SCROLLCAP_RAD, V2D_SCROLLCAP_SHADE, -V2D_SCROLLCAP_SHADE);
1332                 }
1333                 
1334                 /* scale indiators */
1335                 // XXX will need to update the font drawing when the new stuff comes in
1336                 if ((v2d->scroll & V2D_SCROLL_SCALE_VERTICAL) && (vs->grid)) {
1337                         View2DGrid *grid= vs->grid;
1338                         float fac, dfac, val;
1339                         
1340                         /* the numbers: convert grid->starty and dy to scroll coordinates 
1341                          *      - fac is y-coordinate to draw to
1342                          *      - dfac is gap between scale markings
1343                          *      - these involve a correction for horizontal scrollbar
1344                          *        NOTE: it's assumed that that scrollbar is there if this is involved!
1345                          */
1346                         fac= (grid->starty- v2d->cur.ymin) / (v2d->cur.ymax - v2d->cur.ymin);
1347                         fac= (vert.ymin + V2D_SCROLL_HEIGHT) + fac*(vert.ymax - vert.ymin - V2D_SCROLL_HEIGHT);
1348                         
1349                         dfac= (grid->dy) / (v2d->cur.ymax - v2d->cur.ymin);
1350                         dfac= dfac * (vert.ymax - vert.ymin - V2D_SCROLL_HEIGHT);
1351                         
1352                         /* set starting value, and text color */
1353                         UI_ThemeColor(TH_TEXT);
1354                         val= grid->starty;
1355                         
1356                         /* if vertical clamping (to whole numbers) is used (i.e. in Sequencer), apply correction */
1357                         // XXX only relevant to Sequencer, so need to review this when we port that code
1358                         if (vs->yclamp == V2D_GRID_CLAMP)
1359                                 fac += 0.5f * dfac;
1360                                 
1361                         /* draw vertical steps */
1362                         for (; fac < vert.ymax; fac+= dfac, val += grid->dy) {
1363                                 scroll_printstr(vs, (float)(vert.xmax)-14.0, fac, val, grid->powery, vs->yunits, 'v');
1364                         }                       
1365                 }       
1366                 
1367                 /* decoration outer bevel line */
1368                 UI_ThemeColorShade(TH_SHADE1, lighter);
1369                 if (v2d->scroll & V2D_SCROLL_RIGHT)
1370                         sdrawline(vert.xmin, vert.ymin, vert.xmin, vert.ymax);
1371                 else if (v2d->scroll & V2D_SCROLL_LEFT)
1372                         sdrawline(vert.xmax, vert.ymin, vert.xmax, vert.ymax);
1373         }
1374         
1375         /* draw a 'sunken square' to cover up any overlapping corners resulting from intersection of overflowing scroller data */
1376         if ((v2d->scroll & V2D_SCROLL_VERTICAL) && (v2d->scroll & V2D_SCROLL_HORIZONTAL)) {
1377                 /* set bounds (these should be right) */
1378                 corner.xmin= vert.xmin;
1379                 corner.xmax= vert.xmax;
1380                 corner.ymin= hor.ymin;
1381                 corner.ymax= hor.ymax;
1382                 
1383                 /* firstly, draw using background color to cover up any overlapping junk */
1384                 UI_ThemeColor(TH_SHADE1);
1385                 glRecti(corner.xmin, corner.ymin, corner.xmax, corner.ymax);
1386                 
1387                 /* now, draw suggestive highlighting... */
1388                         /* first, dark lines on top to suggest scrollers overlap box */
1389                 UI_ThemeColorShade(TH_SHADE1, darker);
1390                 sdrawline(corner.xmin, corner.ymin, corner.xmin, corner.ymax);
1391                 sdrawline(corner.xmin, corner.ymax, corner.xmax, corner.ymax);
1392                         /* now, light lines on bottom to show box is sunken in */
1393                 UI_ThemeColorShade(TH_SHADE1, lighter);
1394                 sdrawline(corner.xmax, corner.ymin, corner.xmax, corner.ymax);
1395                 sdrawline(corner.xmin, corner.ymin, corner.xmax, corner.ymin);
1396         }
1397 }
1398
1399 /* free temporary memory used for drawing scrollers */
1400 void UI_view2d_scrollers_free(View2DScrollers *scrollers)
1401 {
1402         /* need to free grid as well... */
1403         if (scrollers->grid) MEM_freeN(scrollers->grid);
1404         MEM_freeN(scrollers);
1405 }
1406
1407 /* *********************************************************************** */
1408 /* Coordinate Conversions */
1409
1410 /* Convert from screen/region space to 2d-View space 
1411  *      
1412  *      - x,y                   = coordinates to convert
1413  *      - viewx,viewy           = resultant coordinates
1414  */
1415 void UI_view2d_region_to_view(View2D *v2d, int x, int y, float *viewx, float *viewy)
1416 {
1417         float div, ofs;
1418
1419         if (viewx) {
1420                 div= v2d->mask.xmax - v2d->mask.xmin;
1421                 ofs= v2d->mask.xmin;
1422                 
1423                 *viewx= v2d->cur.xmin + (v2d->cur.xmax-v2d->cur.xmin) * ((float)x - ofs) / div;
1424         }
1425
1426         if (viewy) {
1427                 div= v2d->mask.ymax - v2d->mask.ymin;
1428                 ofs= v2d->mask.ymin;
1429                 
1430                 *viewy= v2d->cur.ymin + (v2d->cur.ymax - v2d->cur.ymin) * ((float)y - ofs) / div;
1431         }
1432 }
1433
1434 /* Convert from 2d-View space to screen/region space
1435  *      - Coordinates are clamped to lie within bounds of region
1436  *
1437  *      - x,y                           = coordinates to convert
1438  *      - regionx,regiony       = resultant coordinates 
1439  */
1440 void UI_view2d_view_to_region(View2D *v2d, float x, float y, short *regionx, short *regiony)
1441 {
1442         /* set initial value in case coordinate lies outside of bounds */
1443         if (regionx)
1444                 *regionx= V2D_IS_CLIPPED;
1445         if (regiony)
1446                 *regiony= V2D_IS_CLIPPED;
1447         
1448         /* express given coordinates as proportional values */
1449         x= (x - v2d->cur.xmin) / (v2d->cur.xmax - v2d->cur.xmin);
1450         y= (y - v2d->cur.ymin) / (v2d->cur.ymax - v2d->cur.ymin);
1451         
1452         /* check if values are within bounds */
1453         if ((x>=0.0f) && (x<=1.0f) && (y>=0.0f) && (y<=1.0f)) {
1454                 if (regionx)
1455                         *regionx= v2d->mask.xmin + x*(v2d->mask.xmax-v2d->mask.xmin);
1456                 if (regiony)
1457                         *regiony= v2d->mask.ymin + y*(v2d->mask.ymax-v2d->mask.ymin);
1458         }
1459 }
1460
1461 /* Convert from 2d-view space to screen/region space
1462  *      - Coordinates are NOT clamped to lie within bounds of region
1463  *
1464  *      - x,y                           = coordinates to convert
1465  *      - regionx,regiony       = resultant coordinates 
1466  */
1467 void UI_view2d_to_region_no_clip(View2D *v2d, float x, float y, short *regionx, short *regiony)
1468 {
1469         /* step 1: express given coordinates as proportional values */
1470         x= (x - v2d->cur.xmin) / (v2d->cur.xmax - v2d->cur.xmin);
1471         y= (y - v2d->cur.ymin) / (v2d->cur.ymax - v2d->cur.ymin);
1472         
1473         /* step 2: convert proportional distances to screen coordinates  */
1474         x= v2d->mask.xmin + x*(v2d->mask.xmax - v2d->mask.xmin);
1475         y= v2d->mask.ymin + y*(v2d->mask.ymax - v2d->mask.ymin);
1476         
1477         /* although we don't clamp to lie within region bounds, we must avoid exceeding size of shorts */
1478         if (regionx) {
1479                 if (x < -32760) *regionx= -32760;
1480                 else if(x > 32760) *regionx= 32760;
1481                 else *regionx= x;
1482         }
1483         if (regiony) {
1484                 if (y < -32760) *regiony= -32760;
1485                 else if(y > 32760) *regiony= 32760;
1486                 else *regiony= y;
1487         }
1488 }
1489
1490 /* *********************************************************************** */
1491 /* Utilities */
1492
1493 /* View2D data by default resides in region, so get from region stored in context */
1494 View2D *UI_view2d_fromcontext(const bContext *C)
1495 {
1496         if (C->area == NULL) return NULL;
1497         if (C->region == NULL) return NULL;
1498         return &(C->region->v2d);
1499 }
1500
1501 /* same as above, but it returns regionwindow. Utility for pulldowns or buttons */
1502 View2D *UI_view2d_fromcontext_rwin(const bContext *C)
1503 {
1504         if (C->area == NULL) return NULL;
1505         if (C->region == NULL) return NULL;
1506         if (C->region->regiontype!=RGN_TYPE_WINDOW) {
1507                 ARegion *ar= C->area->regionbase.first;
1508                 for(; ar; ar= ar->next)
1509                         if(ar->regiontype==RGN_TYPE_WINDOW)
1510                                 return &(ar->v2d);
1511                 return NULL;
1512         }
1513         return &(C->region->v2d);
1514 }
1515
1516
1517 /* Calculate the scale per-axis of the drawing-area
1518  *      - Is used to inverse correct drawing of icons, etc. that need to follow view 
1519  *        but not be affected by scale
1520  *
1521  *      - x,y   = scale on each axis
1522  */
1523 void UI_view2d_getscale(View2D *v2d, float *x, float *y) 
1524 {
1525         if (x) *x = (v2d->mask.xmax - v2d->mask.xmin) / (v2d->cur.xmax - v2d->cur.xmin);
1526         if (y) *y = (v2d->mask.ymax - v2d->mask.ymin) / (v2d->cur.ymax - v2d->cur.ymin);
1527 }
1528
1529
1530 void UI_view2d_sync(View2D *v2d, View2D *v2dfrom, int flag)
1531 {
1532         
1533         if(flag == V2D_LOCK_COPY) {
1534                 v2d->cur.xmin= v2dfrom->cur.xmin;
1535                 v2d->cur.xmax= v2dfrom->cur.xmax;
1536         }
1537         else {
1538                 v2dfrom->cur.xmin= v2d->cur.xmin;
1539                 v2dfrom->cur.xmax= v2d->cur.xmax;
1540         }
1541 }
1542