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