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