svn merge -r 21041:21301 https://svn.blender.org/svnroot/bf-blender/branches/blender2...
[blender.git] / source / blender / editors / interface / view2d.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2008 Blender Foundation.
21  * All rights reserved.
22  * 
23  * Contributor(s): Blender Foundation, Joshua Leung
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 #include <limits.h>
29 #include <math.h>
30 #include <string.h>
31
32 #include "MEM_guardedalloc.h"
33
34 #include "DNA_scene_types.h"
35 #include "DNA_screen_types.h"
36 #include "DNA_space_types.h"
37 #include "DNA_view2d_types.h"
38
39 #include "BLI_blenlib.h"
40
41 #include "BKE_context.h"
42 #include "BKE_global.h"
43 #include "BKE_utildefines.h"
44
45 #include "WM_api.h"
46
47 #include "BIF_gl.h"
48 #include "BIF_glutil.h"
49
50 #include "BLF_api.h"
51
52 #include "ED_screen.h"
53
54 #include "UI_interface.h"
55 #include "UI_resources.h"
56 #include "UI_view2d.h"
57
58 #include "interface_intern.h"
59
60 /* *********************************************************************** */
61
62 /* helper to allow scrollbars to dynamically hide */
63 static int view2d_scroll_mapped(int scroll)
64 {
65         if(scroll & V2D_SCROLL_HORIZONTAL_HIDE)
66                 scroll &= ~(V2D_SCROLL_HORIZONTAL);
67         if(scroll & V2D_SCROLL_VERTICAL_HIDE)
68                 scroll &= ~(V2D_SCROLL_VERTICAL);
69         return scroll;
70 }
71
72 /* called each time cur changes, to dynamically update masks */
73 static void view2d_masks(View2D *v2d)
74 {
75         int scroll;
76         
77         /* mask - view frame */
78         v2d->mask.xmin= v2d->mask.ymin= 0;
79         v2d->mask.xmax= v2d->winx - 1;  /* -1 yes! masks are pixels */
80         v2d->mask.ymax= v2d->winy - 1;
81
82 #if 0
83         v2d->scroll &= ~(V2D_SCROLL_HORIZONTAL_HIDE|V2D_SCROLL_VERTICAL_HIDE);
84         /* check size if: */
85         if (v2d->scroll & V2D_SCROLL_HORIZONTAL)
86                 if(!(v2d->scroll & V2D_SCROLL_SCALE_HORIZONTAL))
87                         if (v2d->tot.xmax-v2d->tot.xmin <= v2d->cur.xmax-v2d->cur.xmin)
88                                 v2d->scroll |= V2D_SCROLL_HORIZONTAL_HIDE;
89         if (v2d->scroll & V2D_SCROLL_VERTICAL)
90                 if(!(v2d->scroll & V2D_SCROLL_SCALE_VERTICAL))
91                         if (v2d->tot.ymax-v2d->tot.ymin <= v2d->cur.ymax-v2d->cur.ymin)
92                                 v2d->scroll |= V2D_SCROLL_VERTICAL_HIDE;
93 #endif
94         scroll= view2d_scroll_mapped(v2d->scroll);
95         
96         /* scrollers shrink mask area, but should be based off regionsize 
97          *      - they can only be on one to two edges of the region they define
98          *      - if they overlap, they must not occupy the corners (which are reserved for other widgets)
99          */
100         if (scroll) {
101                 /* vertical scroller */
102                 if (scroll & V2D_SCROLL_LEFT) {
103                         /* on left-hand edge of region */
104                         v2d->vert= v2d->mask;
105                         v2d->vert.xmax= V2D_SCROLL_WIDTH;
106                         v2d->mask.xmin= v2d->vert.xmax + 1;
107                 }
108                 else if (scroll & V2D_SCROLL_RIGHT) {
109                         /* on right-hand edge of region */
110                         v2d->vert= v2d->mask;
111                         v2d->vert.xmax++; /* one pixel extra... was leaving a minor gap... */
112                         v2d->vert.xmin= v2d->vert.xmax - V2D_SCROLL_WIDTH;
113                         v2d->mask.xmax= v2d->vert.xmin - 1;
114                 }
115                 
116                 /* horizontal scroller */
117                 if (scroll & (V2D_SCROLL_BOTTOM|V2D_SCROLL_BOTTOM_O)) {
118                         /* on bottom edge of region (V2D_SCROLL_BOTTOM_O is outliner, the other is for standard) */
119                         v2d->hor= v2d->mask;
120                         v2d->hor.ymax= V2D_SCROLL_HEIGHT;
121                         v2d->mask.ymin= v2d->hor.ymax + 1;
122                 }
123                 else if (scroll & V2D_SCROLL_TOP) {
124                         /* on upper edge of region */
125                         v2d->hor= v2d->mask;
126                         v2d->hor.ymin= v2d->hor.ymax - V2D_SCROLL_HEIGHT;
127                         v2d->mask.ymax= v2d->hor.ymin - 1;
128                 }
129                 
130                 /* adjust vertical scroller if there's a horizontal scroller, to leave corner free */
131                 if (scroll & V2D_SCROLL_VERTICAL) {
132                         /* just set y min/max for vertical scroller to y min/max of mask as appropriate */
133                         if (scroll & (V2D_SCROLL_BOTTOM|V2D_SCROLL_BOTTOM_O)) {
134                                 /* on bottom edge of region (V2D_SCROLL_BOTTOM_O is outliner, the other is for standard) */
135                                 v2d->vert.ymin= v2d->mask.ymin;
136                         }
137                         else if (scroll & V2D_SCROLL_TOP) {
138                                 /* on upper edge of region */
139                                 v2d->vert.ymax= v2d->mask.ymax;
140                         }
141                 }
142         }
143         
144 }
145
146 /* Refresh and Validation */
147
148 /* Initialise all relevant View2D data (including view rects if first time) and/or refresh mask sizes after view resize
149  *      - for some of these presets, it is expected that the region will have defined some
150  *        additional settings necessary for the customisation of the 2D viewport to its requirements
151  *      - this function should only be called from region init() callbacks, where it is expected that
152  *        this is called before UI_view2d_size_update(), as this one checks that the rects are properly initialised. 
153  */
154 void UI_view2d_region_reinit(View2D *v2d, short type, int winx, int winy)
155 {
156         short tot_changed= 0;
157         
158         /* initialise data if there is a need for such */
159         if ((v2d->flag & V2D_IS_INITIALISED) == 0) {
160                 /* set initialised flag so that View2D doesn't get reinitialised next time again */
161                 v2d->flag |= V2D_IS_INITIALISED;
162                 
163                 /* see eView2D_CommonViewTypes in UI_view2d.h for available view presets */
164                 switch (type) {
165                         /* 'standard view' - optimum setup for 'standard' view behaviour, that should be used new views as basis for their
166                          *      own unique View2D settings, which should be used instead of this in most cases...
167                          */
168                         case V2D_COMMONVIEW_STANDARD:
169                         {
170                                 /* for now, aspect ratio should be maintained, and zoom is clamped within sane default limits */
171                                 v2d->keepzoom= (V2D_KEEPASPECT|V2D_KEEPZOOM);
172                                 v2d->minzoom= 0.01f;
173                                 v2d->maxzoom= 1000.0f;
174                                 
175                                 /* tot rect and cur should be same size, and aligned using 'standard' OpenGL coordinates for now 
176                                  *      - region can resize 'tot' later to fit other data
177                                  *      - keeptot is only within bounds, as strict locking is not that critical
178                                  *      - view is aligned for (0,0) -> (winx-1, winy-1) setup
179                                  */
180                                 v2d->align= (V2D_ALIGN_NO_NEG_X|V2D_ALIGN_NO_NEG_Y);
181                                 v2d->keeptot= V2D_KEEPTOT_BOUNDS;
182                                 
183                                 v2d->tot.xmin= v2d->tot.ymin= 0.0f;
184                                 v2d->tot.xmax= (float)(winx - 1);
185                                 v2d->tot.ymax= (float)(winy - 1);
186                                 
187                                 v2d->cur= v2d->tot;
188                                 
189                                 /* scrollers - should we have these by default? */
190                                 // XXX for now, we don't override this, or set it either!
191                         }
192                                 break;
193                         
194                         /* 'list/channel view' - zoom, aspect ratio, and alignment restrictions are set here */
195                         case V2D_COMMONVIEW_LIST:
196                         {
197                                 /* zoom + aspect ratio are locked */
198                                 v2d->keepzoom = (V2D_LOCKZOOM_X|V2D_LOCKZOOM_Y|V2D_KEEPZOOM|V2D_KEEPASPECT);
199                                 v2d->minzoom= v2d->maxzoom= 1.0f;
200                                 
201                                 /* tot rect has strictly regulated placement, and must only occur in +/- quadrant */
202                                 v2d->align = (V2D_ALIGN_NO_NEG_X|V2D_ALIGN_NO_POS_Y);
203                                 v2d->keeptot = V2D_KEEPTOT_STRICT;
204                                 tot_changed= 1;
205                                 
206                                 /* scroller settings are currently not set here... that is left for regions... */
207                         }
208                                 break;
209                                 
210                         /* 'header' regions - zoom, aspect ratio, alignment, and panning restrictions are set here */
211                         case V2D_COMMONVIEW_HEADER:
212                         {
213                                 /* zoom + aspect ratio are locked */
214                                 v2d->keepzoom = (V2D_LOCKZOOM_X|V2D_LOCKZOOM_Y|V2D_KEEPZOOM|V2D_KEEPASPECT);
215                                 v2d->minzoom= v2d->maxzoom= 1.0f;
216                                 v2d->min[0]= v2d->max[0]= (float)(winx-1);
217                                 v2d->min[1]= v2d->max[1]= (float)(winy-1);
218                                 
219                                 /* tot rect has strictly regulated placement, and must only occur in +/+ quadrant */
220                                 v2d->align = (V2D_ALIGN_NO_NEG_X|V2D_ALIGN_NO_NEG_Y);
221                                 v2d->keeptot = V2D_KEEPTOT_STRICT;
222                                 tot_changed= 1;
223                                 
224                                 /* panning in y-axis is prohibited */
225                                 v2d->keepofs= V2D_LOCKOFS_Y;
226                                 
227                                 /* absolutely no scrollers allowed */
228                                 v2d->scroll= 0;
229                                 
230                                 /* pixel offsets need to be applied for smooth UI controls */
231                                 v2d->flag |= (V2D_PIXELOFS_X|V2D_PIXELOFS_Y);
232                         }
233                                 break;
234                         
235                         /* panels view, with horizontal/vertical align */
236                         case V2D_COMMONVIEW_PANELS_UI:
237                         {
238                                 /* for now, aspect ratio should be maintained, and zoom is clamped within sane default limits */
239                                 v2d->keepzoom= (V2D_KEEPASPECT|V2D_KEEPZOOM);
240                                 v2d->minzoom= 0.5f;
241                                 v2d->maxzoom= 2.0f;
242                                 
243                                 v2d->align= (V2D_ALIGN_NO_NEG_X|V2D_ALIGN_NO_POS_Y);
244                                 v2d->keeptot= V2D_KEEPTOT_BOUNDS;
245                                 
246                                 v2d->tot.xmin= 0.0f;
247                                 v2d->tot.xmax= winx;
248
249                                 v2d->tot.ymax= 0.0f;
250                                 v2d->tot.ymin= -winy;
251
252                                 v2d->cur= v2d->tot;
253                         }
254                                 break;
255
256                                 /* other view types are completely defined using their own settings already */
257                         default:
258                                 /* we don't do anything here, as settings should be fine, but just make sure that rect */
259                                 break;  
260                 }
261         }
262         
263         /* store view size */
264         v2d->winx= winx;
265         v2d->winy= winy;
266         
267         /* set masks */
268         view2d_masks(v2d);
269         
270         /* set 'tot' rect before setting cur? */
271         if (tot_changed) 
272                 UI_view2d_totRect_set(v2d, winx, winy);
273         else
274                 UI_view2d_curRect_validate(v2d);
275         
276 }
277
278 /* Ensure View2D rects remain in a viable configuration 
279  *      - cur is not allowed to be: larger than max, smaller than min, or outside of tot
280  */
281 // XXX pre2.5 -> this used to be called  test_view2d()
282 void UI_view2d_curRect_validate(View2D *v2d)
283 {
284         float totwidth, totheight, curwidth, curheight, width, height;
285         float winx, winy;
286         rctf *cur, *tot;
287         
288         /* use mask as size of region that View2D resides in, as it takes into account scrollbars already  */
289         winx= (float)(v2d->mask.xmax - v2d->mask.xmin + 1);
290         winy= (float)(v2d->mask.ymax - v2d->mask.ymin + 1);
291         
292         /* get pointers to rcts for less typing */
293         cur= &v2d->cur;
294         tot= &v2d->tot;
295         
296         /* we must satisfy the following constraints (in decreasing order of importance):
297          *      - alignment restrictions are respected
298          *      - cur must not fall outside of tot
299          *      - axis locks (zoom and offset) must be maintained
300          *      - zoom must not be excessive (check either sizes or zoom values)
301          *      - aspect ratio should be respected (NOTE: this is quite closely realted to zoom too)
302          */
303         
304         /* Step 1: if keepzoom, adjust the sizes of the rects only
305          *      - firstly, we calculate the sizes of the rects
306          *      - curwidth and curheight are saved as reference... modify width and height values here
307          */
308         totwidth= tot->xmax - tot->xmin;
309         totheight= tot->ymax - tot->ymin;
310         curwidth= width= cur->xmax - cur->xmin;
311         curheight= height= cur->ymax - cur->ymin;
312         
313         /* if zoom is locked, size on the appropriate axis is reset to mask size */
314         if (v2d->keepzoom & V2D_LOCKZOOM_X)
315                 width= winx;
316         if (v2d->keepzoom & V2D_LOCKZOOM_Y)
317                 height= winy;
318                 
319         /* values used to divide, so make it safe */
320         if(width<1) width= 1;
321         if(height<1) height= 1;
322         if(winx<1) winx= 1;
323         if(winy<1) winy= 1;
324         
325         /* keepzoom (V2D_KEEPZOOM set), indicates that zoom level on each axis must not exceed limits 
326          * NOTE: in general, it is not expected that the lock-zoom will be used in conjunction with this
327          */
328         if (v2d->keepzoom & V2D_KEEPZOOM) {
329                 float zoom, fac;
330                 
331                 /* check if excessive zoom on x-axis */
332                 if ((v2d->keepzoom & V2D_LOCKZOOM_X)==0) {
333                         zoom= winx / width;
334                         if ((zoom < v2d->minzoom) || (zoom > v2d->maxzoom)) {
335                                 fac= (zoom < v2d->minzoom) ? (zoom / v2d->minzoom) : (zoom / v2d->maxzoom);
336                                 width *= fac;
337                         }
338                 }
339                 
340                 /* check if excessive zoom on y-axis */
341                 if ((v2d->keepzoom & V2D_LOCKZOOM_Y)==0) {
342                         zoom= winy / height;
343                         if ((zoom < v2d->minzoom) || (zoom > v2d->maxzoom)) {
344                                 fac= (zoom < v2d->minzoom) ? (zoom / v2d->minzoom) : (zoom / v2d->maxzoom);
345                                 height *= fac;
346                         }
347                 }
348         }
349         else {
350                 /* make sure sizes don't exceed that of the min/max sizes (even though we're not doing zoom clamping) */
351                 CLAMP(width, v2d->min[0], v2d->max[0]);
352                 CLAMP(height, v2d->min[1], v2d->max[1]);
353         }
354         
355         /* check if we should restore aspect ratio (if view size changed) */
356         if (v2d->keepzoom & V2D_KEEPASPECT) {
357                 short do_x=0, do_y=0, do_cur, do_win;
358                 float curRatio, winRatio;
359                 
360                 /* when a window edge changes, the aspect ratio can't be used to
361                  * find which is the best new 'cur' rect. thats why it stores 'old' 
362                  */
363                 if (winx != v2d->oldwinx) do_x= 1;
364                 if (winy != v2d->oldwiny) do_y= 1;
365                 
366                 curRatio= height / width;
367                 winRatio= winy / winx;
368                 
369                 /* both sizes change (area/region maximised)  */
370                 if (do_x == do_y) {
371                         if (do_x && do_y) {
372                                 /* here is 1,1 case, so all others must be 0,0 */
373                                 if (ABS(winx - v2d->oldwinx) > ABS(winy - v2d->oldwiny)) do_y= 0;
374                                 else do_x= 0;
375                         }
376                         else if (winRatio > 1.0f) do_x= 0; 
377                         else do_x= 1;
378                 }
379                 do_cur= do_x;
380                 do_win= do_y;
381                 
382                 if (do_cur) {
383                         if ((v2d->keeptot == V2D_KEEPTOT_STRICT) && (winx != v2d->oldwinx)) {
384                                 /* special exception for Outliner (and later channel-lists):
385                                  *      - The view may be moved left to avoid contents being pushed out of view when view shrinks. 
386                                  *      - The keeptot code will make sure cur->xmin will not be less than tot->xmin (which cannot be allowed)
387                                  *      - width is not adjusted for changed ratios here...
388                                  */
389                                 if (winx < v2d->oldwinx) {
390                                         float temp = v2d->oldwinx - winx;
391                                         
392                                         cur->xmin -= temp;
393                                         cur->xmax -= temp;
394                                         
395                                         /* width does not get modified, as keepaspect here is just set to make 
396                                          * sure visible area adjusts to changing view shape! 
397                                          */
398                                 }
399                         }
400                         else {
401                                 /* portrait window: correct for x */
402                                 width= height / winRatio;
403                         }
404                 }
405                 else {
406                         if ((v2d->keeptot == V2D_KEEPTOT_STRICT) && (winy != v2d->oldwiny)) {
407                                 /* special exception for Outliner (and later channel-lists):
408                                  *      - Currently, no actions need to be taken here...
409                                  */
410
411                                 if (winy < v2d->oldwiny) {
412                                         float temp = v2d->oldwiny - winy;
413                                         
414                                         cur->ymin += temp;
415                                         cur->ymax += temp;
416                                 }
417
418                         }
419                         else {
420                                 /* landscape window: correct for y */
421                                 height = width * winRatio;
422                         }
423                 }
424                 
425                 /* store region size for next time */
426                 v2d->oldwinx= (short)winx; 
427                 v2d->oldwiny= (short)winy;
428         }
429         
430         /* Step 2: apply new sizes to cur rect, but need to take into account alignment settings here... */
431         if ((width != curwidth) || (height != curheight)) {
432                 float temp, dh;
433                 
434                 /* resize from centerpoint */
435                 if (width != curwidth) {
436                         if (v2d->keepofs & V2D_LOCKOFS_X) {
437                                 cur->xmax += width - (cur->xmax - cur->xmin);
438                         }
439                         else {
440                                 temp= (cur->xmax + cur->xmin) * 0.5f;
441                                 dh= width * 0.5f;
442                                 
443                                 cur->xmin = temp - dh;
444                                 cur->xmax = temp + dh;
445                         }
446                 }
447                 if (height != curheight) {
448                         if (v2d->keepofs & V2D_LOCKOFS_Y) {
449                                 cur->ymax += height - (cur->ymax - cur->ymin);
450                         }
451                         else {
452                                 temp= (cur->ymax + cur->ymin) * 0.5f;
453                                 dh= height * 0.5f;
454                                 
455                                 cur->ymin = temp - dh;
456                                 cur->ymax = temp + dh;
457                         }
458                 }
459         }
460         
461         /* Step 3: adjust so that it doesn't fall outside of bounds of 'tot' */
462         if (v2d->keeptot) {
463                 float temp, diff;
464                 
465                 /* recalculate extents of cur */
466                 curwidth= cur->xmax - cur->xmin;
467                 curheight= cur->ymax - cur->ymin;
468                 
469                 /* width */
470                 if ( (curwidth > totwidth) && !(v2d->keepzoom & (V2D_KEEPZOOM|V2D_LOCKZOOM_X)) ) {
471                         /* if zoom doesn't have to be maintained, just clamp edges */
472                         if (cur->xmin < tot->xmin) cur->xmin= tot->xmin;
473                         if (cur->xmax > tot->xmax) cur->xmax= tot->xmax;
474                 }
475                 else if (v2d->keeptot == V2D_KEEPTOT_STRICT) {
476                         /* This is an exception for the outliner (and later channel-lists, headers) 
477                          *      - must clamp within tot rect (absolutely no excuses)
478                          *      --> therefore, cur->xmin must not be less than tot->xmin
479                          */
480                         if (cur->xmin < tot->xmin) {
481                                 /* move cur across so that it sits at minimum of tot */
482                                 temp= tot->xmin - cur->xmin;
483                                 
484                                 cur->xmin += temp;
485                                 cur->xmax += temp;
486                         }
487                         else if (cur->xmax > tot->xmax) {
488                                 /* - only offset by difference of cur-xmax and tot-xmax if that would not move 
489                                  *      cur-xmin to lie past tot-xmin
490                                  * - otherwise, simply shift to tot-xmin???
491                                  */
492                                 temp= cur->xmax - tot->xmax;
493                                 
494                                 if ((cur->xmin - temp) < tot->xmin) {
495                                         /* only offset by difference from cur-min and tot-min */
496                                         temp= cur->xmin - tot->xmin;
497                                         
498                                         cur->xmin -= temp;
499                                         cur->xmax -= temp;
500                                 }
501                                 else {
502                                         cur->xmin -= temp;
503                                         cur->xmax -= temp;
504                                 }
505                         }
506                 }
507                 else {
508                         /* This here occurs when:
509                          *      - width too big, but maintaining zoom (i.e. widths cannot be changed)
510                          *      - width is OK, but need to check if outside of boundaries
511                          * 
512                          * So, resolution is to just shift view by the gap between the extremities.
513                          * We favour moving the 'minimum' across, as that's origin for most things
514                          * (XXX - in the past, max was favoured... if there are bugs, swap!)
515                          */
516                         if ((cur->ymin < tot->ymin) && (cur->ymax > tot->ymax)) {
517                                 /* outside boundaries on both sides, so take middle-point of tot, and place in balanced way */
518                                 temp= (tot->ymax + tot->ymin) * 0.5f;
519                                 diff= curheight * 0.5f;
520                                 
521                                 cur->ymin= temp - diff;
522                                 cur->ymax= temp + diff;
523                         }
524                         else if (cur->xmin < tot->xmin) {
525                                 /* move cur across so that it sits at minimum of tot */
526                                 temp= tot->xmin - cur->xmin;
527                                 
528                                 cur->xmin += temp;
529                                 cur->xmax += temp;
530                         }
531                         else if (cur->xmax > tot->xmax) {
532                                 /* - only offset by difference of cur-xmax and tot-xmax if that would not move 
533                                  *      cur-xmin to lie past tot-xmin
534                                  * - otherwise, simply shift to tot-xmin???
535                                  */
536                                 temp= cur->xmax - tot->xmax;
537                                 
538                                 if ((cur->xmin - temp) < tot->xmin) {
539                                         /* only offset by difference from cur-min and tot-min */
540                                         temp= cur->xmin - tot->xmin;
541                                         
542                                         cur->xmin -= temp;
543                                         cur->xmax -= temp;
544                                 }
545                                 else {
546                                         cur->xmin -= temp;
547                                         cur->xmax -= temp;
548                                 }
549                         }
550                 }
551                 
552                 /* height */
553                 if ( (curheight > totheight) && !(v2d->keepzoom & (V2D_KEEPZOOM|V2D_LOCKZOOM_Y)) ) {
554                         /* if zoom doesn't have to be maintained, just clamp edges */
555                         if (cur->ymin < tot->ymin) cur->ymin= tot->ymin;
556                         if (cur->ymax > tot->ymax) cur->ymax= tot->ymax;
557                 }
558                 else {
559                         /* This here occurs when:
560                          *      - height too big, but maintaining zoom (i.e. heights cannot be changed)
561                          *      - height is OK, but need to check if outside of boundaries
562                          * 
563                          * So, resolution is to just shift view by the gap between the extremities.
564                          * We favour moving the 'minimum' across, as that's origin for most things
565                          */
566                         if ((cur->ymin < tot->ymin) && (cur->ymax > tot->ymax)) {
567                                 /* outside boundaries on both sides, so take middle-point of tot, and place in balanced way */
568                                 temp= (tot->ymax + tot->ymin) * 0.5f;
569                                 diff= curheight * 0.5f;
570                                 
571                                 cur->ymin= temp - diff;
572                                 cur->ymax= temp + diff;
573                         }
574                         else if (cur->ymin < tot->ymin) {
575                                 /* there's still space remaining, so shift up */
576                                 temp= tot->ymin - cur->ymin;
577                                 
578                                 cur->ymin += temp;
579                                 cur->ymax += temp;
580                         }
581                         else if (cur->ymax > tot->ymax) {
582                                 /* there's still space remaining, so shift down */
583                                 temp= cur->ymax - tot->ymax;
584                                 
585                                 cur->ymin -= temp;
586                                 cur->ymax -= temp;
587                         }
588                 }
589         }
590         
591         /* Step 4: Make sure alignment restrictions are respected */
592         if (v2d->align) {
593                 /* If alignment flags are set (but keeptot is not), they must still be respected, as although
594                  * they don't specify any particular bounds to stay within, they do define ranges which are 
595                  * invalid.
596                  *
597                  * Here, we only check to make sure that on each axis, the 'cur' rect doesn't stray into these 
598                  * invalid zones, otherwise we offset.
599                  */
600                 
601                 /* handle width - posx and negx flags are mutually exclusive, so watch out */
602                 if ((v2d->align & V2D_ALIGN_NO_POS_X) && !(v2d->align & V2D_ALIGN_NO_NEG_X)) {
603                         /* width is in negative-x half */
604                         if (v2d->cur.xmax > 0) {
605                                 v2d->cur.xmin -= v2d->cur.xmax;
606                                 v2d->cur.xmax= 0.0f;
607                         }
608                 }
609                 else if ((v2d->align & V2D_ALIGN_NO_NEG_X) && !(v2d->align & V2D_ALIGN_NO_POS_X)) {
610                         /* width is in positive-x half */
611                         if (v2d->cur.xmin < 0) {
612                                 v2d->cur.xmax -= v2d->cur.xmin;
613                                 v2d->cur.xmin = 0.0f;
614                         }
615                 }
616                 
617                 /* handle height - posx and negx flags are mutually exclusive, so watch out */
618                 if ((v2d->align & V2D_ALIGN_NO_POS_Y) && !(v2d->align & V2D_ALIGN_NO_NEG_Y)) {
619                         /* height is in negative-y half */
620                         if (v2d->cur.ymax > 0) {
621                                 v2d->cur.ymin -= v2d->cur.ymax;
622                                 v2d->cur.ymax = 0.0f;
623                         }
624                 }
625                 else if ((v2d->align & V2D_ALIGN_NO_NEG_Y) && !(v2d->align & V2D_ALIGN_NO_POS_Y)) {
626                         /* height is in positive-y half */
627                         if (v2d->cur.ymin < 0) {
628                                 v2d->cur.ymax -= v2d->cur.ymin;
629                                 v2d->cur.ymin = 0.0f;
630                         }
631                 }
632         }
633         
634         /* set masks */
635         view2d_masks(v2d);
636 }
637
638 /* ------------------ */
639
640 /* Called by menus to activate it, or by view2d operators to make sure 'related' views stay in synchrony */
641 void UI_view2d_sync(bScreen *screen, ScrArea *area, View2D *v2dcur, int flag)
642 {
643         ScrArea *sa;
644         ARegion *ar;
645         
646         /* don't continue if no view syncing to be done */
647         if ((v2dcur->flag & (V2D_VIEWSYNC_SCREEN_TIME|V2D_VIEWSYNC_AREA_VERTICAL)) == 0)
648                 return;
649                 
650         /* check if doing within area syncing (i.e. channels/vertical) */
651         if ((v2dcur->flag & V2D_VIEWSYNC_AREA_VERTICAL) && (area)) {
652                 for (ar= area->regionbase.first; ar; ar= ar->next) {
653                         /* don't operate on self */
654                         if (v2dcur != &ar->v2d) {
655                                 /* only if view has vertical locks enabled */
656                                 if (ar->v2d.flag & V2D_VIEWSYNC_AREA_VERTICAL) {
657                                         if (flag == V2D_LOCK_COPY) {
658                                                 /* other views with locks on must copy active */
659                                                 ar->v2d.cur.ymin= v2dcur->cur.ymin;
660                                                 ar->v2d.cur.ymax= v2dcur->cur.ymax;
661                                         }
662                                         else { /* V2D_LOCK_SET */
663                                                 /* active must copy others */
664                                                 v2dcur->cur.ymin= ar->v2d.cur.ymin;
665                                                 v2dcur->cur.ymax= ar->v2d.cur.ymax;
666                                         }
667                                         
668                                         /* region possibly changed, so refresh */
669                                         ED_region_tag_redraw(ar);
670                                 }
671                         }
672                 }
673         }
674         
675         /* check if doing whole screen syncing (i.e. time/horizontal) */
676         if ((v2dcur->flag & V2D_VIEWSYNC_SCREEN_TIME) && (screen)) {
677                 for (sa= screen->areabase.first; sa; sa= sa->next) {
678                         for (ar= sa->regionbase.first; ar; ar= ar->next) {
679                                 /* don't operate on self */
680                                 if (v2dcur != &ar->v2d) {
681                                         /* only if view has horizontal locks enabled */
682                                         if (ar->v2d.flag & V2D_VIEWSYNC_SCREEN_TIME) {
683                                                 if (flag == V2D_LOCK_COPY) {
684                                                         /* other views with locks on must copy active */
685                                                         ar->v2d.cur.xmin= v2dcur->cur.xmin;
686                                                         ar->v2d.cur.xmax= v2dcur->cur.xmax;
687                                                 }
688                                                 else { /* V2D_LOCK_SET */
689                                                         /* active must copy others */
690                                                         v2dcur->cur.xmin= ar->v2d.cur.xmin;
691                                                         v2dcur->cur.xmax= ar->v2d.cur.xmax;
692                                                 }
693                                                 
694                                                 /* region possibly changed, so refresh */
695                                                 ED_region_tag_redraw(ar);
696                                         }
697                                 }
698                         }
699                 }
700         }
701 }
702
703
704 /* Restore 'cur' rect to standard orientation (i.e. optimal maximum view of tot) 
705  * This does not take into account if zooming the view on an axis will improve the view (if allowed)
706  */
707 void UI_view2d_curRect_reset (View2D *v2d)
708 {
709         float width, height;
710         
711         /* assume width and height of 'cur' rect by default, should be same size as mask */
712         width= (float)(v2d->mask.xmax - v2d->mask.xmin + 1);
713         height= (float)(v2d->mask.ymax - v2d->mask.ymin + 1);
714         
715         /* handle width - posx and negx flags are mutually exclusive, so watch out */
716         if ((v2d->align & V2D_ALIGN_NO_POS_X) && !(v2d->align & V2D_ALIGN_NO_NEG_X)) {
717                 /* width is in negative-x half */
718                 v2d->cur.xmin= (float)-width;
719                 v2d->cur.xmax= 0.0f;
720         }
721         else if ((v2d->align & V2D_ALIGN_NO_NEG_X) && !(v2d->align & V2D_ALIGN_NO_POS_X)) {
722                 /* width is in positive-x half */
723                 v2d->cur.xmin= 0.0f;
724                 v2d->cur.xmax= (float)width;
725         }
726         else {
727                 /* width is centered around x==0 */
728                 const float dx= (float)width / 2.0f;
729                 
730                 v2d->cur.xmin= -dx;
731                 v2d->cur.xmax= dx;
732         }
733         
734         /* handle height - posx and negx flags are mutually exclusive, so watch out */
735         if ((v2d->align & V2D_ALIGN_NO_POS_Y) && !(v2d->align & V2D_ALIGN_NO_NEG_Y)) {
736                 /* height is in negative-y half */
737                 v2d->cur.ymin= (float)-height;
738                 v2d->cur.ymax= 0.0f;
739         }
740         else if ((v2d->align & V2D_ALIGN_NO_NEG_Y) && !(v2d->align & V2D_ALIGN_NO_POS_Y)) {
741                 /* height is in positive-y half */
742                 v2d->cur.ymin= 0.0f;
743                 v2d->cur.ymax= (float)height;
744         }
745         else {
746                 /* height is centered around y==0 */
747                 const float dy= (float)height / 2.0f;
748                 
749                 v2d->cur.ymin= -dy;
750                 v2d->cur.ymax= dy;
751         }
752 }
753
754 /* ------------------ */
755
756 /* Change the size of the maximum viewable area (i.e. 'tot' rect) */
757 void UI_view2d_totRect_set (View2D *v2d, int width, int height)
758 {
759         int scroll= view2d_scroll_mapped(v2d->scroll);
760         
761         /* don't do anything if either value is 0 */
762         width= abs(width);
763         height= abs(height);
764         
765         /* hrumf! */
766         if(scroll & V2D_SCROLL_HORIZONTAL) 
767                 width -= V2D_SCROLL_WIDTH;
768         if(scroll & V2D_SCROLL_VERTICAL) 
769                 height -= V2D_SCROLL_HEIGHT;
770         
771         if (ELEM3(0, v2d, width, height)) {
772                 printf("Error: View2D totRect set exiting: v2d=%p width=%d height=%d \n", v2d, width, height); // XXX temp debug info
773                 return;
774         }
775         
776         /* handle width - posx and negx flags are mutually exclusive, so watch out */
777         if ((v2d->align & V2D_ALIGN_NO_POS_X) && !(v2d->align & V2D_ALIGN_NO_NEG_X)) {
778                 /* width is in negative-x half */
779                 v2d->tot.xmin= (float)-width;
780                 v2d->tot.xmax= 0.0f;
781         }
782         else if ((v2d->align & V2D_ALIGN_NO_NEG_X) && !(v2d->align & V2D_ALIGN_NO_POS_X)) {
783                 /* width is in positive-x half */
784                 v2d->tot.xmin= 0.0f;
785                 v2d->tot.xmax= (float)width;
786         }
787         else {
788                 /* width is centered around x==0 */
789                 const float dx= (float)width / 2.0f;
790                 
791                 v2d->tot.xmin= -dx;
792                 v2d->tot.xmax= dx;
793         }
794         
795         /* handle height - posx and negx flags are mutually exclusive, so watch out */
796         if ((v2d->align & V2D_ALIGN_NO_POS_Y) && !(v2d->align & V2D_ALIGN_NO_NEG_Y)) {
797                 /* height is in negative-y half */
798                 v2d->tot.ymin= (float)-height;
799                 v2d->tot.ymax= 0.0f;
800         }
801         else if ((v2d->align & V2D_ALIGN_NO_NEG_Y) && !(v2d->align & V2D_ALIGN_NO_POS_Y)) {
802                 /* height is in positive-y half */
803                 v2d->tot.ymin= 0.0f;
804                 v2d->tot.ymax= (float)height;
805         }
806         else {
807                 /* height is centered around y==0 */
808                 const float dy= (float)height / 2.0f;
809                 
810                 v2d->tot.ymin= -dy;
811                 v2d->tot.ymax= dy;
812         }
813         
814         /* make sure that 'cur' rect is in a valid state as a result of these changes */
815         UI_view2d_curRect_validate(v2d);
816 }
817
818 /* *********************************************************************** */
819 /* View Matrix Setup */
820
821 /* mapping function to ensure 'cur' draws extended over the area where sliders are */
822 static void view2d_map_cur_using_mask(View2D *v2d, rctf *curmasked)
823 {
824         *curmasked= v2d->cur;
825         
826         if (view2d_scroll_mapped(v2d->scroll)) {
827                 float dx= (v2d->cur.xmax-v2d->cur.xmin)/((float)(v2d->mask.xmax-v2d->mask.xmin+1));
828                 float dy= (v2d->cur.ymax-v2d->cur.ymin)/((float)(v2d->mask.ymax-v2d->mask.ymin+1));
829                 
830                 if (v2d->mask.xmin != 0)
831                         curmasked->xmin -= dx*(float)v2d->mask.xmin;
832                 if (v2d->mask.xmax+1 != v2d->winx)
833                         curmasked->xmax += dx*(float)(v2d->winx - v2d->mask.xmax-1);
834                 
835                 if (v2d->mask.ymin != 0)
836                         curmasked->ymin -= dy*(float)v2d->mask.ymin;
837                 if (v2d->mask.ymax+1 != v2d->winy)
838                         curmasked->ymax += dy*(float)(v2d->winy - v2d->mask.ymax-1);
839                 
840         }
841 }
842
843 /* Set view matrices to use 'cur' rect as viewing frame for View2D drawing */
844 void UI_view2d_view_ortho(const bContext *C, View2D *v2d)
845 {
846         rctf curmasked;
847         float xofs, yofs;
848         
849         /* pixel offsets (-0.375f) are needed to get 1:1 correspondance with pixels for smooth UI drawing, 
850          * but only applied where requsted
851          */
852         /* XXX ton: fix this! */
853         xofs= 0.0; // (v2d->flag & V2D_PIXELOFS_X) ? 0.375f : 0.0f;
854         yofs= 0.0; // (v2d->flag & V2D_PIXELOFS_Y) ? 0.375f : 0.0f;
855
856         /* XXX brecht: instead of zero at least use a tiny offset, otherwise
857          * pixel rounding is effectively random due to float inaccuracy */
858         xofs= 0.001f;
859         yofs= 0.001f;
860         
861         /* apply mask-based adjustments to cur rect (due to scrollers), to eliminate scaling artifacts */
862         view2d_map_cur_using_mask(v2d, &curmasked);
863         
864         /* set matrix on all appropriate axes */
865         wmOrtho2(curmasked.xmin-xofs, curmasked.xmax-xofs, curmasked.ymin-yofs, curmasked.ymax-yofs);
866         
867         /* XXX is this necessary? */
868         wmLoadIdentity();
869 }
870
871 /* Set view matrices to only use one axis of 'cur' only
872  *      - xaxis         = if non-zero, only use cur x-axis, otherwise use cur-yaxis (mostly this will be used for x)
873  */
874 void UI_view2d_view_orthoSpecial(const bContext *C, View2D *v2d, short xaxis)
875 {
876         ARegion *ar= CTX_wm_region(C);
877         rctf curmasked;
878         float xofs, yofs;
879         
880         /* pixel offsets (-0.375f) are needed to get 1:1 correspondance with pixels for smooth UI drawing, 
881          * but only applied where requsted
882          */
883         /* XXX temp (ton) */
884         xofs= 0.0f; // (v2d->flag & V2D_PIXELOFS_X) ? 0.375f : 0.0f;
885         yofs= 0.0f; // (v2d->flag & V2D_PIXELOFS_Y) ? 0.375f : 0.0f;
886         
887         /* apply mask-based adjustments to cur rect (due to scrollers), to eliminate scaling artifacts */
888         view2d_map_cur_using_mask(v2d, &curmasked);
889         
890         /* only set matrix with 'cur' coordinates on relevant axes */
891         if (xaxis)
892                 wmOrtho2(curmasked.xmin-xofs, curmasked.xmax-xofs, -yofs, ar->winy-yofs);
893         else
894                 wmOrtho2(-xofs, ar->winx-xofs, curmasked.ymin-yofs, curmasked.ymax-yofs);
895                 
896         /* XXX is this necessary? */
897         wmLoadIdentity();
898
899
900
901 /* Restore view matrices after drawing */
902 void UI_view2d_view_restore(const bContext *C)
903 {
904         ED_region_pixelspace(CTX_wm_region(C));
905 }
906
907 /* *********************************************************************** */
908 /* Gridlines */
909
910 /* minimum pixels per gridstep */
911 #define MINGRIDSTEP     35
912
913 /* View2DGrid is typedef'd in UI_view2d.h */
914 struct View2DGrid {
915         float dx, dy;                   /* stepsize (in pixels) between gridlines */
916         float startx, starty;   /* initial coordinates to start drawing grid from */
917         int powerx, powery;             /* step as power of 10 */
918 };
919
920 /* --------------- */
921
922 /* try to write step as a power of 10 */
923 static void step_to_grid(float *step, int *power, int unit)
924 {
925         const float loga= (float)log10(*step);
926         float rem;
927         
928         *power= (int)(loga);
929         
930         rem= loga - (*power);
931         rem= (float)pow(10.0, rem);
932         
933         if (loga < 0.0f) {
934                 if (rem < 0.2f) rem= 0.2f;
935                 else if(rem < 0.5f) rem= 0.5f;
936                 else rem= 1.0f;
937                 
938                 *step= rem * (float)pow(10.0, (*power));
939                 
940                 /* for frames, we want 1.0 frame intervals only */
941                 if (unit == V2D_UNIT_FRAMES) {
942                         rem = 1.0f;
943                         *step = 1.0f;
944                 }
945                 
946                 /* prevents printing 1.0 2.0 3.0 etc */
947                 if (rem == 1.0f) (*power)++;    
948         }
949         else {
950                 if (rem < 2.0f) rem= 2.0f;
951                 else if(rem < 5.0f) rem= 5.0f;
952                 else rem= 10.0f;
953                 
954                 *step= rem * (float)pow(10.0, (*power));
955                 
956                 (*power)++;
957                 /* prevents printing 1.0, 2.0, 3.0, etc. */
958                 if (rem == 10.0f) (*power)++;   
959         }
960 }
961
962 /* Intialise settings necessary for drawing gridlines in a 2d-view 
963  *      - Currently, will return pointer to View2DGrid struct that needs to 
964  *        be freed with UI_view2d_grid_free()
965  *      - Is used for scrollbar drawing too (for units drawing)
966  *      - Units + clamping args will be checked, to make sure they are valid values that can be used
967  *        so it is very possible that we won't return grid at all!
968  *      
969  *      - xunits,yunits = V2D_UNIT_*  grid steps in seconds or frames 
970  *      - xclamp,yclamp = V2D_CLAMP_* only show whole-number intervals
971  *      - winx                  = width of region we're drawing to
972  *      - winy                  = height of region we're drawing into
973  */
974 View2DGrid *UI_view2d_grid_calc(const bContext *C, View2D *v2d, short xunits, short xclamp, short yunits, short yclamp, int winx, int winy)
975 {
976         Scene *scene= CTX_data_scene(C);
977         View2DGrid *grid;
978         float space, pixels, seconddiv;
979         int secondgrid;
980         
981         /* check that there are at least some workable args */
982         if (ELEM(V2D_ARG_DUMMY, xunits, xclamp) && ELEM(V2D_ARG_DUMMY, yunits, yclamp))
983                 return NULL;
984         
985         /* grid here is allocated... */
986         grid= MEM_callocN(sizeof(View2DGrid), "View2DGrid");
987         
988         /* rule: gridstep is minimal GRIDSTEP pixels */
989         if (xunits == V2D_UNIT_SECONDS) {
990                 secondgrid= 1;
991                 seconddiv= (float)(0.01 * FPS);
992         }
993         else {
994                 secondgrid= 0;
995                 seconddiv= 1.0f;
996         }
997         
998         /* calculate x-axis grid scale (only if both args are valid) */
999         if (ELEM(V2D_ARG_DUMMY, xunits, xclamp) == 0) {
1000                 space= v2d->cur.xmax - v2d->cur.xmin;
1001                 pixels= (float)(v2d->mask.xmax - v2d->mask.xmin);
1002                 
1003                 grid->dx= (MINGRIDSTEP * space) / (seconddiv * pixels);
1004                 step_to_grid(&grid->dx, &grid->powerx, xunits);
1005                 grid->dx *= seconddiv;
1006                 
1007                 if (xclamp == V2D_GRID_CLAMP) {
1008                         if (grid->dx < 0.1f) grid->dx= 0.1f;
1009                         grid->powerx-= 2;
1010                         if (grid->powerx < -2) grid->powerx= -2;
1011                 }
1012         }
1013         
1014         /* calculate y-axis grid scale (only if both args are valid) */
1015         if (ELEM(V2D_ARG_DUMMY, yunits, yclamp) == 0) {
1016                 space= v2d->cur.ymax - v2d->cur.ymin;
1017                 pixels= (float)winy;
1018                 
1019                 grid->dy= MINGRIDSTEP * space / pixels;
1020                 step_to_grid(&grid->dy, &grid->powery, yunits);
1021                 
1022                 if (yclamp == V2D_GRID_CLAMP) {
1023                         if (grid->dy < 1.0f) grid->dy= 1.0f;
1024                         if (grid->powery < 1) grid->powery= 1;
1025                 }
1026         }
1027         
1028         /* calculate start position */
1029         if (ELEM(V2D_ARG_DUMMY, xunits, xclamp) == 0) {
1030                 grid->startx= seconddiv*(v2d->cur.xmin/seconddiv - (float)fmod(v2d->cur.xmin/seconddiv, grid->dx/seconddiv));
1031                 if (v2d->cur.xmin < 0.0f) grid->startx-= grid->dx;
1032         }
1033         else
1034                 grid->startx= v2d->cur.xmin;
1035                 
1036         if (ELEM(V2D_ARG_DUMMY, yunits, yclamp) == 0) {
1037                 grid->starty= (v2d->cur.ymin - (float)fmod(v2d->cur.ymin, grid->dy));
1038                 if (v2d->cur.ymin < 0.0f) grid->starty-= grid->dy;
1039         }
1040         else
1041                 grid->starty= v2d->cur.ymin;
1042         
1043         return grid;
1044 }
1045
1046 /* Draw gridlines in the given 2d-region */
1047 void UI_view2d_grid_draw(const bContext *C, View2D *v2d, View2DGrid *grid, int flag)
1048 {
1049         float vec1[2], vec2[2];
1050         int a, step;
1051         
1052         /* check for grid first, as it may not exist */
1053         if (grid == NULL)
1054                 return;
1055         
1056         /* vertical lines */
1057         if (flag & V2D_VERTICAL_LINES) {
1058                 /* initialise initial settings */
1059                 vec1[0]= vec2[0]= grid->startx;
1060                 vec1[1]= grid->starty;
1061                 vec2[1]= v2d->cur.ymax;
1062                 
1063                 /* minor gridlines */
1064                 step= (v2d->mask.xmax - v2d->mask.xmin + 1) / MINGRIDSTEP;
1065                 UI_ThemeColor(TH_GRID);
1066                 
1067                 for (a=0; a<step; a++) {
1068                         glBegin(GL_LINE_STRIP);
1069                                 glVertex2fv(vec1); 
1070                                 glVertex2fv(vec2);
1071                         glEnd();
1072                         
1073                         vec2[0]= vec1[0]+= grid->dx;
1074                 }
1075                 
1076                 /* major gridlines */
1077                 vec2[0]= vec1[0]-= 0.5f*grid->dx;
1078                 UI_ThemeColorShade(TH_GRID, 16);
1079                 
1080                 step++;
1081                 for (a=0; a<=step; a++) {
1082                         glBegin(GL_LINE_STRIP);
1083                                 glVertex2fv(vec1); 
1084                                 glVertex2fv(vec2);
1085                         glEnd();
1086                         
1087                         vec2[0]= vec1[0]-= grid->dx;
1088                 }
1089         }
1090         
1091         /* horizontal lines */
1092         if (flag & V2D_HORIZONTAL_LINES) {
1093                 /* only major gridlines */
1094                 vec1[1]= vec2[1]= grid->starty;
1095                 vec1[0]= grid->startx;
1096                 vec2[0]= v2d->cur.xmax;
1097                 
1098                 step= (v2d->mask.ymax - v2d->mask.ymin + 1) / MINGRIDSTEP;
1099                 
1100                 UI_ThemeColor(TH_GRID);
1101                 for (a=0; a<=step; a++) {
1102                         glBegin(GL_LINE_STRIP);
1103                                 glVertex2fv(vec1); 
1104                                 glVertex2fv(vec2);
1105                         glEnd();
1106                         
1107                         vec2[1]= vec1[1]+= grid->dy;
1108                 }
1109                 
1110                 /* fine grid lines */
1111                 vec2[1]= vec1[1]-= 0.5f*grid->dy;
1112                 step++;
1113                 
1114                 if (flag & V2D_HORIZONTAL_FINELINES) { 
1115                         UI_ThemeColorShade(TH_GRID, 16);
1116                         for (a=0; a<step; a++) {
1117                                 glBegin(GL_LINE_STRIP);
1118                                         glVertex2fv(vec1); 
1119                                         glVertex2fv(vec2);
1120                                 glEnd();
1121                                 
1122                                 vec2[1]= vec1[1]-= grid->dy;
1123                         }
1124                 }
1125         }
1126         
1127         /* Axes are drawn as darker lines */
1128         UI_ThemeColorShade(TH_GRID, -50);
1129         
1130         /* horizontal axis */
1131         if (flag & V2D_HORIZONTAL_AXIS) {
1132                 vec1[0]= v2d->cur.xmin;
1133                 vec2[0]= v2d->cur.xmax;
1134                 vec1[1]= vec2[1]= 0.0f;
1135                 
1136                 glBegin(GL_LINE_STRIP);
1137                         glVertex2fv(vec1);
1138                         glVertex2fv(vec2);
1139                 glEnd();
1140         }
1141         
1142         /* vertical axis */
1143         if (flag & V2D_VERTICAL_AXIS) {
1144                 vec1[1]= v2d->cur.ymin;
1145                 vec2[1]= v2d->cur.ymax;
1146                 vec1[0]= vec2[0]= 0.0f;
1147                 
1148                 glBegin(GL_LINE_STRIP);
1149                         glVertex2fv(vec1); 
1150                         glVertex2fv(vec2);
1151                 glEnd();
1152         }
1153 }
1154
1155 /* Draw a constant grid in given 2d-region */
1156 void UI_view2d_constant_grid_draw(const bContext *C, View2D *v2d)
1157 {
1158         float start, step= 25.0f;
1159
1160         UI_ThemeColorShade(TH_BACK, -10);
1161         
1162         start= v2d->cur.xmin - (float)fmod(v2d->cur.xmin, step);
1163         
1164         glBegin(GL_LINES);
1165         for(; start<v2d->cur.xmax; start+=step) {
1166                 glVertex2f(start, v2d->cur.ymin);
1167                 glVertex2f(start, v2d->cur.ymax);
1168         }
1169
1170         start= v2d->cur.ymin - (float)fmod(v2d->cur.ymin, step);
1171         for(; start<v2d->cur.ymax; start+=step) {
1172                 glVertex2f(v2d->cur.xmin, start);
1173                 glVertex2f(v2d->cur.xmax, start);
1174         }
1175         
1176         /* X and Y axis */
1177         UI_ThemeColorShade(TH_BACK, -18);
1178         glVertex2f(0.0f, v2d->cur.ymin);
1179         glVertex2f(0.0f, v2d->cur.ymax);
1180         glVertex2f(v2d->cur.xmin, 0.0f);
1181         glVertex2f(v2d->cur.xmax, 0.0f);
1182         
1183         glEnd();
1184 }
1185
1186 /* free temporary memory used for drawing grid */
1187 void UI_view2d_grid_free(View2DGrid *grid)
1188 {
1189         /* only free if there's a grid */
1190         if (grid)
1191                 MEM_freeN(grid);
1192 }
1193
1194 /* *********************************************************************** */
1195 /* Scrollers */
1196
1197 /* View2DScrollers is typedef'd in UI_view2d.h 
1198  * WARNING: the start of this struct must not change, as view2d_ops.c uses this too. 
1199  *                 For now, we don't need to have a separate (internal) header for structs like this...
1200  */
1201 struct View2DScrollers {        
1202                 /* focus bubbles */
1203         int vert_min, vert_max; /* vertical scrollbar */
1204         int hor_min, hor_max;   /* horizontal scrollbar */
1205         
1206                 /* scales */
1207         View2DGrid *grid;               /* grid for coordinate drawing */
1208         short xunits, xclamp;   /* units and clamping options for x-axis */
1209         short yunits, yclamp;   /* units and clamping options for y-axis */
1210 };
1211
1212 /* Calculate relevant scroller properties */
1213 View2DScrollers *UI_view2d_scrollers_calc(const bContext *C, View2D *v2d, short xunits, short xclamp, short yunits, short yclamp)
1214 {
1215         View2DScrollers *scrollers;
1216         rcti vert, hor;
1217         float fac, totsize, scrollsize;
1218         int scroll= view2d_scroll_mapped(v2d->scroll);
1219         
1220         vert= v2d->vert;
1221         hor= v2d->hor;
1222         
1223         /* scrollers is allocated here... */
1224         scrollers= MEM_callocN(sizeof(View2DScrollers), "View2DScrollers");
1225         
1226         /* scroller 'buttons':
1227          *      - These should always remain within the visible region of the scrollbar
1228          *      - They represent the region of 'tot' that is visible in 'cur'
1229          */
1230         
1231         /* horizontal scrollers */
1232         if (scroll & V2D_SCROLL_HORIZONTAL) {
1233                 /* scroller 'button' extents */
1234                 totsize= v2d->tot.xmax - v2d->tot.xmin;
1235                 scrollsize= (float)(hor.xmax - hor.xmin);
1236                 
1237                 fac= (v2d->cur.xmin - v2d->tot.xmin) / totsize;
1238                 scrollers->hor_min= (int)(hor.xmin + (fac * scrollsize));
1239                 
1240                 fac= (v2d->cur.xmax - v2d->tot.xmin) / totsize;
1241                 scrollers->hor_max= (int)(hor.xmin + (fac * scrollsize));
1242                 
1243                 if (scrollers->hor_min > scrollers->hor_max) 
1244                         scrollers->hor_min= scrollers->hor_max;
1245         }
1246         
1247         /* vertical scrollers */
1248         if (scroll & V2D_SCROLL_VERTICAL) {
1249                 /* scroller 'button' extents */
1250                 totsize= v2d->tot.ymax - v2d->tot.ymin;
1251                 scrollsize= (float)(vert.ymax - vert.ymin);
1252                 
1253                 fac= (v2d->cur.ymin- v2d->tot.ymin) / totsize;
1254                 scrollers->vert_min= (int)(vert.ymin + (fac * scrollsize));
1255                 
1256                 fac= (v2d->cur.ymax - v2d->tot.ymin) / totsize;
1257                 scrollers->vert_max= (int)(vert.ymin + (fac * scrollsize));
1258                 
1259                 if (scrollers->vert_min > scrollers->vert_max) 
1260                         scrollers->vert_min= scrollers->vert_max;
1261         }
1262         
1263         /* grid markings on scrollbars */
1264         if (scroll & (V2D_SCROLL_SCALE_HORIZONTAL|V2D_SCROLL_SCALE_VERTICAL)) {
1265                 /* store clamping */
1266                 scrollers->xclamp= xclamp;
1267                 scrollers->xunits= xunits;
1268                 scrollers->yclamp= yclamp;
1269                 scrollers->yunits= yunits;
1270                 
1271                 scrollers->grid= UI_view2d_grid_calc(C, v2d, xunits, xclamp, yunits, yclamp, (hor.xmax - hor.xmin), (vert.ymax - vert.ymin));
1272         }
1273         
1274         /* return scrollers */
1275         return scrollers;
1276 }
1277
1278 /* Print scale marking along a time scrollbar */
1279 static void scroll_printstr(View2DScrollers *scrollers, Scene *scene, float x, float y, float val, int power, short unit, char dir)
1280 {
1281         int len;
1282         char str[32];
1283         
1284         /* adjust the scale unit to work ok */
1285         if (dir == 'v') {
1286                 /* here we bump up the power by factor of 10, as 
1287                  * rotation values (hence 'degrees') are divided by 10 to 
1288                  * be able to show the curves at the same time
1289                  */
1290                 if ELEM(unit, V2D_UNIT_DEGREES, V2D_UNIT_TIME) {
1291                         power += 1;
1292                         val *= 10;
1293                 }
1294         }
1295         
1296         /* get string to print */
1297         if (unit == V2D_UNIT_SECONDS) {
1298                 /* Timecode:
1299                  *      - In general, minutes and seconds should be shown, as most clips will be
1300                  *        within this length. Hours will only be included if relevant.
1301                  *      - Only show frames when zoomed in enough for them to be relevant 
1302                  *        (using separator of '!' for frames).
1303                  *        When showing frames, use slightly different display to avoid confusion with mm:ss format
1304                  * TODO: factor into reusable function.
1305                  * Meanwhile keep in sync:
1306                  *        source/blender/editors/animation/anim_draw.c
1307                  *        source/blender/editors/interface/view2d.c
1308                  */
1309                 int hours=0, minutes=0, seconds=0, frames=0;
1310                 char neg[2]= "";
1311                 
1312                 /* get values */
1313                 if (val < 0) {
1314                         /* correction for negative values */
1315                         sprintf(neg, "-");
1316                         val = -val;
1317                 }
1318                 if (val >= 3600) {
1319                         /* hours */
1320                         /* XXX should we only display a single digit for hours since clips are 
1321                          *         VERY UNLIKELY to be more than 1-2 hours max? However, that would 
1322                          *         go against conventions...
1323                          */
1324                         hours= (int)val / 3600;
1325                         val= (float)fmod(val, 3600);
1326                 }
1327                 if (val >= 60) {
1328                         /* minutes */
1329                         minutes= (int)val / 60;
1330                         val= (float)fmod(val, 60);
1331                 }
1332                 if (power <= 0) {
1333                         /* seconds + frames
1334                          *      Frames are derived from 'fraction' of second. We need to perform some additional rounding
1335                          *      to cope with 'half' frames, etc., which should be fine in most cases
1336                          */
1337                         seconds= (int)val;
1338                         frames= (int)floor( ((val - seconds) * FPS) + 0.5f );
1339                 }
1340                 else {
1341                         /* seconds (with pixel offset) */
1342                         seconds= (int)floor(val + 0.375f);
1343                 }
1344                 
1345                 /* print timecode to temp string buffer */
1346                 if (power <= 0) {
1347                         /* include "frames" in display */
1348                         if (hours) sprintf(str, "%s%02d:%02d:%02d!%02d", neg, hours, minutes, seconds, frames);
1349                         else if (minutes) sprintf(str, "%s%02d:%02d!%02d", neg, minutes, seconds, frames);
1350                         else sprintf(str, "%s%d!%02d", neg, seconds, frames);
1351                 }
1352                 else {
1353                         /* don't include 'frames' in display */
1354                         if (hours) sprintf(str, "%s%02d:%02d:%02d", neg, hours, minutes, seconds);
1355                         else sprintf(str, "%s%02d:%02d", neg, minutes, seconds);
1356                 }
1357         }
1358         else {
1359                 /* round to whole numbers if power is >= 1 (i.e. scale is coarse) */
1360                 if (power <= 0) sprintf(str, "%.*f", 1-power, val);
1361                 else sprintf(str, "%d", (int)floor(val + 0.375f));
1362         }
1363         
1364         /* get length of string, and adjust printing location to fit it into the horizontal scrollbar */
1365         len= strlen(str);
1366         if (dir == 'h') {
1367                 /* seconds/timecode display has slightly longer strings... */
1368                 if (unit == V2D_UNIT_SECONDS)
1369                         x-= 3*len;
1370                 else
1371                         x-= 4*len;
1372         }
1373         
1374         /* Add degree sympbol to end of string for vertical scrollbar? */
1375         if ((dir == 'v') && (unit == V2D_UNIT_DEGREES)) {
1376                 str[len]= 186;
1377                 str[len+1]= 0;
1378         }
1379         
1380         /* draw it */
1381         BLF_draw_default(x, y, 0.0f, str);
1382 }
1383
1384 /* local defines for scrollers drawing */
1385         /* radius of scroller 'button' caps */
1386 #define V2D_SCROLLCAP_RAD               5
1387         /* shading factor for scroller 'bar' */
1388 #define V2D_SCROLLBAR_SHADE             0.1f
1389         /* shading factor for scroller 'button' caps */
1390 #define V2D_SCROLLCAP_SHADE             0.2f
1391
1392 /* Draw scrollbars in the given 2d-region */
1393 void UI_view2d_scrollers_draw(const bContext *C, View2D *v2d, View2DScrollers *vs)
1394 {
1395         Scene *scene= CTX_data_scene(C);
1396         const short darker= -50, dark= -10, light= 20, lighter= 50;
1397         rcti vert, hor, corner;
1398         int scroll= view2d_scroll_mapped(v2d->scroll);
1399         
1400         /* make copies of rects for less typing */
1401         vert= v2d->vert;
1402         hor= v2d->hor;
1403         
1404         /* horizontal scrollbar */
1405         if (scroll & V2D_SCROLL_HORIZONTAL) {
1406                 /* scroller backdrop */
1407                 UI_ThemeColorShade(TH_SHADE1, light);
1408                 glRecti(hor.xmin,  hor.ymin,  hor.xmax,  hor.ymax);
1409                 
1410                 /* scroller 'button' 
1411                  *      - if view is zoomable in x, draw handles too 
1412                  *      - handles are drawn darker
1413                  *      - no slider when view is > total for non-zoomable views
1414                  *        (otherwise, zoomable ones tend to flicker)
1415                  */
1416                 if ( (v2d->scroll & V2D_SCROLL_SCALE_HORIZONTAL) ||
1417                          ((v2d->tot.xmax - v2d->tot.xmin) > (v2d->cur.xmax - v2d->cur.xmin)) ) 
1418                 { 
1419                         if (v2d->keepzoom & V2D_LOCKZOOM_X) {
1420                                 /* draw base bar as rounded shape */
1421                                 UI_ThemeColorShade(TH_SHADE1, dark);
1422                                 uiSetRoundBox(15);
1423                                 
1424                                 /* check that box is large enough for round drawing */
1425                                 if ((vs->hor_max - vs->hor_min) < (V2D_SCROLLCAP_RAD * 2)) {
1426                                         /* Rounded box still gets drawn at the minimum size limit
1427                                          * This doesn't represent extreme scaling well, but looks nicer...
1428                                          */
1429                                         float mid= 0.5f * (vs->hor_max + vs->hor_min);
1430                                         
1431                                         gl_round_box_shade(GL_POLYGON, 
1432                                                 mid-V2D_SCROLLCAP_RAD, (float)hor.ymin+2, 
1433                                                 mid+V2D_SCROLLCAP_RAD, (float)hor.ymax-2, 
1434                                                 V2D_SCROLLCAP_RAD, V2D_SCROLLBAR_SHADE, -V2D_SCROLLBAR_SHADE);
1435                                 }
1436                                 else {
1437                                         /* draw rounded box as per normal */
1438                                         gl_round_box_shade(GL_POLYGON, 
1439                                                 (float)vs->hor_min, (float)hor.ymin+2, 
1440                                                 (float)vs->hor_max, (float)hor.ymax-2, 
1441                                                 V2D_SCROLLCAP_RAD, V2D_SCROLLBAR_SHADE, -V2D_SCROLLBAR_SHADE);
1442                                 }
1443                         }
1444                         else {
1445                                 /* base bar drawn as shaded rect */
1446                                 UI_ThemeColorShade(TH_SHADE1, dark);
1447                                 uiSetRoundBox(0);
1448                                 gl_round_box_shade(GL_POLYGON, 
1449                                         (float)vs->hor_min, (float)hor.ymin+2, 
1450                                         (float)vs->hor_max, (float)hor.ymax-2, 
1451                                         V2D_SCROLLCAP_RAD, V2D_SCROLLBAR_SHADE, -V2D_SCROLLBAR_SHADE);
1452                                 
1453                                 /* 'minimum' handle */
1454                                 uiSetRoundBox(9);
1455                                 UI_ThemeColorShade(TH_SHADE1, darker);
1456                                 
1457                                 gl_round_box_shade(GL_POLYGON, 
1458                                         (float)vs->hor_min-V2D_SCROLLER_HANDLE_SIZE, (float)hor.ymin+2, 
1459                                         (float)vs->hor_min+V2D_SCROLLER_HANDLE_SIZE, (float)hor.ymax-2, 
1460                                         V2D_SCROLLCAP_RAD, V2D_SCROLLCAP_SHADE, -V2D_SCROLLCAP_SHADE);
1461                                 
1462                                 /* maximum handle */
1463                                 uiSetRoundBox(6);
1464                                 UI_ThemeColorShade(TH_SHADE1, darker);
1465                                 
1466                                 gl_round_box_shade(GL_POLYGON, 
1467                                         (float)vs->hor_max-V2D_SCROLLER_HANDLE_SIZE, (float)hor.ymin+2, 
1468                                         (float)vs->hor_max+V2D_SCROLLER_HANDLE_SIZE, (float)hor.ymax-2, 
1469                                         V2D_SCROLLCAP_RAD, V2D_SCROLLCAP_SHADE, -V2D_SCROLLCAP_SHADE);
1470                         }
1471                 }
1472                 
1473                 /* scale indicators */
1474                 // XXX will need to update the font drawing when the new stuff comes in
1475                 if ((scroll & V2D_SCROLL_SCALE_HORIZONTAL) && (vs->grid)) {
1476                         View2DGrid *grid= vs->grid;
1477                         float fac, dfac, fac2, val;
1478                         
1479                         /* the numbers: convert grid->startx and -dx to scroll coordinates 
1480                          *      - fac is x-coordinate to draw to
1481                          *      - dfac is gap between scale markings
1482                          */
1483                         fac= (grid->startx - v2d->cur.xmin) / (v2d->cur.xmax - v2d->cur.xmin);
1484                         fac= (float)hor.xmin + fac*(hor.xmax - hor.xmin);
1485                         
1486                         dfac= (grid->dx) / (v2d->cur.xmax - v2d->cur.xmin);
1487                         dfac= dfac * (hor.xmax - hor.xmin);
1488                         
1489                         /* set starting value, and text color */
1490                         UI_ThemeColor(TH_TEXT);
1491                         val= grid->startx;
1492                         
1493                         /* if we're clamping to whole numbers only, make sure entries won't be repeated */
1494                         if (vs->xclamp == V2D_GRID_CLAMP) {
1495                                 while (grid->dx < 0.9999f) {
1496                                         grid->dx *= 2.0f;
1497                                         dfac *= 2.0f;
1498                                 }
1499                         }
1500                         if (vs->xunits == V2D_UNIT_FRAMES)
1501                                 grid->powerx= 1;
1502                         
1503                         /* draw numbers in the appropriate range */
1504                         if (dfac > 0.0f) {
1505                                 for (; fac < hor.xmax; fac+=dfac, val+=grid->dx) {
1506                                         switch (vs->xunits) {                                                   
1507                                                 case V2D_UNIT_FRAMES:           /* frames (as whole numbers)*/
1508                                                         scroll_printstr(vs, scene, fac, 3.0f+(float)(hor.ymin), val, grid->powerx, V2D_UNIT_FRAMES, 'h');
1509                                                         break;
1510                                                         
1511                                                 case V2D_UNIT_FRAMESCALE:       /* frames (not always as whole numbers) */
1512                                                         scroll_printstr(vs, scene, fac, 3.0f+(float)(hor.ymin), val, grid->powerx, V2D_UNIT_FRAMESCALE, 'h');
1513                                                         break;
1514                                                 
1515                                                 case V2D_UNIT_SECONDS:          /* seconds */
1516                                                         fac2= val/(float)FPS;
1517                                                         scroll_printstr(vs, scene, fac, 3.0f+(float)(hor.ymin), fac2, grid->powerx, V2D_UNIT_SECONDS, 'h');
1518                                                         break;
1519                                                         
1520                                                 case V2D_UNIT_SECONDSSEQ:       /* seconds with special calculations (only used for sequencer only) */
1521                                                 {
1522                                                         float time;
1523                                                         
1524                                                         fac2= val/(float)FPS;
1525                                                         time= (float)floor(fac2);
1526                                                         fac2= fac2-time;
1527                                                         
1528                                                         scroll_printstr(vs, scene, fac, 3.0f+(float)(hor.ymin), time+(float)FPS*fac2/100.0f, grid->powerx, V2D_UNIT_SECONDSSEQ, 'h');
1529                                                 }
1530                                                         break;
1531                                                         
1532                                                 case V2D_UNIT_DEGREES:          /* Graph Editor for rotation Drivers */
1533                                                         /* HACK: although we're drawing horizontal, we make this draw as 'vertical', just to get degree signs */
1534                                                         scroll_printstr(vs, scene, fac, 3.0f+(float)(hor.ymin), val, grid->powerx, V2D_UNIT_DEGREES, 'v');
1535                                                         break;
1536                                         }
1537                                 }
1538                         }
1539                 }
1540                 
1541                 /* decoration outer bevel line */
1542                 UI_ThemeColorShade(TH_SHADE1, lighter);
1543                 if (scroll & (V2D_SCROLL_BOTTOM|V2D_SCROLL_BOTTOM_O))
1544                         sdrawline(hor.xmin, hor.ymax, hor.xmax, hor.ymax);
1545                 else if (scroll & V2D_SCROLL_TOP)
1546                         sdrawline(hor.xmin, hor.ymin, hor.xmax, hor.ymin);
1547         }
1548         
1549         /* vertical scrollbar */
1550         if (scroll & V2D_SCROLL_VERTICAL) {
1551                 /* scroller backdrop  */
1552                 UI_ThemeColorShade(TH_SHADE1, light);
1553                 glRecti(vert.xmin,  vert.ymin,  vert.xmax,  vert.ymax);
1554                 
1555                 /* scroller 'button' 
1556                  *      - if view is zoomable in y, draw handles too 
1557                  *      - handles are drawn darker
1558                  *      - no slider when view is > total for non-zoomable views
1559                  *        (otherwise, zoomable ones tend to flicker)
1560                  */
1561                 if ( (v2d->scroll & V2D_SCROLL_SCALE_VERTICAL) ||
1562                          ((v2d->tot.ymax - v2d->tot.ymin) > (v2d->cur.ymax - v2d->cur.ymin)) ) 
1563                 { 
1564                         if (v2d->keepzoom & V2D_LOCKZOOM_Y) {
1565                                 /* draw base bar as rounded shape */
1566                                 UI_ThemeColorShade(TH_SHADE1, dark);
1567                                 uiSetRoundBox(15);
1568                                 
1569                                 /* check that box is large enough for round drawing */
1570                                 if ((vs->vert_max - vs->vert_min) < (V2D_SCROLLCAP_RAD * 2)) {
1571                                         /* Rounded box still gets drawn at the minimum size limit
1572                                          * This doesn't represent extreme scaling well, but looks nicer...
1573                                          */
1574                                         float mid= 0.5f * (vs->vert_max + vs->vert_min);
1575                                         
1576                                         gl_round_box_vertical_shade(GL_POLYGON, 
1577                                                 (float)vert.xmin+2, mid-V2D_SCROLLCAP_RAD, 
1578                                                 (float)vert.xmax-2, mid+V2D_SCROLLCAP_RAD, 
1579                                                 V2D_SCROLLCAP_RAD, V2D_SCROLLBAR_SHADE, -V2D_SCROLLBAR_SHADE);
1580                                 }
1581                                 else {
1582                                         /* draw rounded box as per normal */
1583                                         gl_round_box_vertical_shade(GL_POLYGON, 
1584                                                 (float)vert.xmin+2, (float)vs->vert_min, 
1585                                                 (float)vert.xmax-2, (float)vs->vert_max, 
1586                                                 V2D_SCROLLCAP_RAD, V2D_SCROLLBAR_SHADE, -V2D_SCROLLBAR_SHADE);
1587                                 }
1588                         }
1589                         else {
1590                                 /* base bar drawn as shaded rect */
1591                                 UI_ThemeColorShade(TH_SHADE1, dark);
1592                                 uiSetRoundBox(0);
1593                                 gl_round_box_vertical_shade(GL_POLYGON, 
1594                                         (float)vert.xmin+2, (float)vs->vert_min, 
1595                                         (float)vert.xmax-2, (float)vs->vert_max,
1596                                         V2D_SCROLLCAP_RAD, V2D_SCROLLBAR_SHADE, -V2D_SCROLLBAR_SHADE);
1597                                 
1598                                 /* 'minimum' handle */
1599                                 UI_ThemeColorShade(TH_SHADE1, darker);
1600                                 uiSetRoundBox(12);
1601                                 
1602                                 gl_round_box_vertical_shade(GL_POLYGON, 
1603                                         (float)vert.xmin+2, (float)vs->vert_min-V2D_SCROLLER_HANDLE_SIZE, 
1604                                         (float)vert.xmax-2, (float)vs->vert_min+V2D_SCROLLER_HANDLE_SIZE, 
1605                                         V2D_SCROLLCAP_RAD, V2D_SCROLLCAP_SHADE, -V2D_SCROLLCAP_SHADE);
1606                                 
1607                                 /* maximum handle */
1608                                 UI_ThemeColorShade(TH_SHADE1, darker);
1609                                 uiSetRoundBox(3);
1610                                 
1611                                 gl_round_box_vertical_shade(GL_POLYGON, 
1612                                         (float)vert.xmin+2, (float)vs->vert_max-V2D_SCROLLER_HANDLE_SIZE, 
1613                                         (float)vert.xmax-2, (float)vs->vert_max+V2D_SCROLLER_HANDLE_SIZE, 
1614                                         V2D_SCROLLCAP_RAD, V2D_SCROLLCAP_SHADE, -V2D_SCROLLCAP_SHADE);
1615                         }
1616                 }
1617                 
1618                 /* scale indiators */
1619                 // XXX will need to update the font drawing when the new stuff comes in
1620                 if ((scroll & V2D_SCROLL_SCALE_VERTICAL) && (vs->grid)) {
1621                         View2DGrid *grid= vs->grid;
1622                         float fac, dfac, val;
1623                         
1624                         /* the numbers: convert grid->starty and dy to scroll coordinates 
1625                          *      - fac is y-coordinate to draw to
1626                          *      - dfac is gap between scale markings
1627                          *      - these involve a correction for horizontal scrollbar
1628                          *        NOTE: it's assumed that that scrollbar is there if this is involved!
1629                          */
1630                         fac= (grid->starty- v2d->cur.ymin) / (v2d->cur.ymax - v2d->cur.ymin);
1631                         fac= (vert.ymin + V2D_SCROLL_HEIGHT) + fac*(vert.ymax - vert.ymin - V2D_SCROLL_HEIGHT);
1632                         
1633                         dfac= (grid->dy) / (v2d->cur.ymax - v2d->cur.ymin);
1634                         dfac= dfac * (vert.ymax - vert.ymin - V2D_SCROLL_HEIGHT);
1635                         
1636                         /* set starting value, and text color */
1637                         UI_ThemeColor(TH_TEXT);
1638                         val= grid->starty;
1639                         
1640                         /* if vertical clamping (to whole numbers) is used (i.e. in Sequencer), apply correction */
1641                         // XXX only relevant to Sequencer, so need to review this when we port that code
1642                         if (vs->yclamp == V2D_GRID_CLAMP)
1643                                 fac += 0.5f * dfac;
1644                                 
1645                         /* draw vertical steps */
1646                         if (dfac > 0.0f) {
1647                                 for (; fac < vert.ymax; fac+= dfac, val += grid->dy) {
1648                                         scroll_printstr(vs, scene, (float)(vert.xmax)-14.0f, fac, val, grid->powery, vs->yunits, 'v');
1649                                 }
1650                         }
1651                 }       
1652                 
1653                 /* decoration outer bevel line */
1654                 UI_ThemeColorShade(TH_SHADE1, lighter);
1655                 if (scroll & V2D_SCROLL_RIGHT)
1656                         sdrawline(vert.xmin, vert.ymin, vert.xmin, vert.ymax);
1657                 else if (scroll & V2D_SCROLL_LEFT)
1658                         sdrawline(vert.xmax, vert.ymin, vert.xmax, vert.ymax);
1659         }
1660         
1661         /* draw a 'sunken square' to cover up any overlapping corners resulting from intersection of overflowing scroller data */
1662         if ((scroll & V2D_SCROLL_VERTICAL) && (scroll & V2D_SCROLL_HORIZONTAL)) {
1663                 /* set bounds (these should be right) */
1664                 corner.xmin= vert.xmin;
1665                 corner.xmax= vert.xmax;
1666                 corner.ymin= hor.ymin;
1667                 corner.ymax= hor.ymax;
1668                 
1669                 /* firstly, draw using background color to cover up any overlapping junk */
1670                 UI_ThemeColor(TH_SHADE1);
1671                 glRecti(corner.xmin, corner.ymin, corner.xmax, corner.ymax);
1672                 
1673                 /* now, draw suggestive highlighting... */
1674                         /* first, dark lines on top to suggest scrollers overlap box */
1675                 UI_ThemeColorShade(TH_SHADE1, darker);
1676                 sdrawline(corner.xmin, corner.ymin, corner.xmin, corner.ymax);
1677                 sdrawline(corner.xmin, corner.ymax, corner.xmax, corner.ymax);
1678                         /* now, light lines on bottom to show box is sunken in */
1679                 UI_ThemeColorShade(TH_SHADE1, lighter);
1680                 sdrawline(corner.xmax, corner.ymin, corner.xmax, corner.ymax);
1681                 sdrawline(corner.xmin, corner.ymin, corner.xmax, corner.ymin);
1682         }
1683 }
1684
1685 /* free temporary memory used for drawing scrollers */
1686 void UI_view2d_scrollers_free(View2DScrollers *scrollers)
1687 {
1688         /* need to free grid as well... */
1689         if (scrollers->grid) MEM_freeN(scrollers->grid);
1690         MEM_freeN(scrollers);
1691 }
1692
1693 /* *********************************************************************** */
1694 /* List View Utilities */
1695
1696 /* Get the view-coordinates of the nominated cell 
1697  *      - columnwidth, rowheight        = size of each 'cell'
1698  *      - startx, starty                        = coordinates (in 'tot' rect space) that the list starts from
1699  *                                                        This should be (0,0) for most views. However, for those where the starting row was offsetted
1700  *                                                        (like for Animation Editor channel lists, to make the first entry more visible), these will be 
1701  *                                                        the min-coordinates of the first item.
1702  *      - column, row                           = the 2d-corodinates (in 2D-view / 'tot' rect space) the cell exists at
1703  *      - rect                                  = coordinates of the cell (passed as single var instead of 4 separate, as it's more useful this way)
1704  */
1705 void UI_view2d_listview_cell_to_view(View2D *v2d, short columnwidth, short rowheight, float startx, float starty, int column, int row, rctf *rect)
1706 {
1707         /* sanity checks */
1708         if ELEM(NULL, v2d, rect)
1709                 return;
1710         if ((columnwidth <= 0) && (rowheight <= 0)) {
1711                 rect->xmin= rect->xmax= 0.0f;
1712                 rect->ymin= rect->ymax= 0.0f;
1713                 return;
1714         }
1715         
1716         /* x-coordinates */
1717         rect->xmin= startx + (float)(columnwidth * column);
1718         rect->xmax= startx + (float)(columnwidth * (column + 1));
1719         
1720         if ((v2d->align & V2D_ALIGN_NO_POS_X) && !(v2d->align & V2D_ALIGN_NO_NEG_X)) {
1721                 /* simply negate the values for the coordinates if in negative half */
1722                 rect->xmin = -rect->xmin;
1723                 rect->xmax = -rect->xmax;
1724         }
1725         
1726         /* y-coordinates */
1727         rect->ymin= starty + (float)(rowheight * row);
1728         rect->ymax= starty + (float)(rowheight * (row + 1));
1729         
1730         if ((v2d->align & V2D_ALIGN_NO_POS_Y) && !(v2d->align & V2D_ALIGN_NO_NEG_Y)) {
1731                 /* simply negate the values for the coordinates if in negative half */
1732                 rect->ymin = -rect->ymin;
1733                 rect->ymax = -rect->ymax;
1734         }
1735 }
1736
1737 /* Get the 'cell' (row, column) that the given 2D-view coordinates (i.e. in 'tot' rect space) lie in.
1738  *      - columnwidth, rowheight        = size of each 'cell'
1739  *      - startx, starty                        = coordinates (in 'tot' rect space) that the list starts from
1740  *                                                        This should be (0,0) for most views. However, for those where the starting row was offsetted
1741  *                                                        (like for Animation Editor channel lists, to make the first entry more visible), these will be 
1742  *                                                        the min-coordinates of the first item.
1743  *      - viewx, viewy                  = 2D-coordinates (in 2D-view / 'tot' rect space) to get the cell for
1744  *      - column, row                           = the 'coordinates' of the relevant 'cell'
1745  */
1746 void UI_view2d_listview_view_to_cell(View2D *v2d, short columnwidth, short rowheight, float startx, float starty, 
1747                                                 float viewx, float viewy, int *column, int *row)
1748 {
1749         /* adjust view coordinates to be all positive ints, corrected for the start offset */
1750         const int x= (int)(floor(fabs(viewx) + 0.5f) - startx); 
1751         const int y= (int)(floor(fabs(viewy) + 0.5f) - starty);
1752         
1753         /* sizes must not be negative */
1754         if ( (v2d == NULL) || ((columnwidth <= 0) && (rowheight <= 0)) ) {
1755                 if (column) *column= 0;
1756                 if (row) *row= 0;
1757                 
1758                 return;
1759         }
1760         
1761         /* get column */
1762         if ((column) && (columnwidth > 0))
1763                 *column= x / columnwidth;
1764         else if (column)
1765                 *column= 0;
1766         
1767         /* get row */
1768         if ((row) && (rowheight > 0))
1769                 *row= y / rowheight;
1770         else if (row)
1771                 *row= 0;
1772 }
1773
1774 /* Get the 'extreme' (min/max) column and row indices which are visible within the 'cur' rect 
1775  *      - columnwidth, rowheight        = size of each 'cell'
1776  *      - startx, starty                        = coordinates that the list starts from, which should be (0,0) for most views
1777  *      - column/row_min/max            = the starting and ending column/row indices
1778  */
1779 void UI_view2d_listview_visible_cells(View2D *v2d, short columnwidth, short rowheight, float startx, float starty, 
1780                                                 int *column_min, int *column_max, int *row_min, int *row_max)
1781 {
1782         /* using 'cur' rect coordinates, call the cell-getting function to get the cells for this */
1783         if (v2d) {
1784                 /* min */
1785                 UI_view2d_listview_view_to_cell(v2d, columnwidth, rowheight, startx, starty, 
1786                                         v2d->cur.xmin, v2d->cur.ymin, column_min, row_min);
1787                                         
1788                 /* max*/
1789                 UI_view2d_listview_view_to_cell(v2d, columnwidth, rowheight, startx, starty, 
1790                                         v2d->cur.xmax, v2d->cur.ymax, column_max, row_max);
1791         }
1792 }
1793
1794 /* *********************************************************************** */
1795 /* Coordinate Conversions */
1796
1797 /* Convert from screen/region space to 2d-View space 
1798  *      
1799  *      - x,y                   = coordinates to convert
1800  *      - viewx,viewy           = resultant coordinates
1801  */
1802 void UI_view2d_region_to_view(View2D *v2d, int x, int y, float *viewx, float *viewy)
1803 {
1804         float div, ofs;
1805
1806         if (viewx) {
1807                 div= (float)(v2d->mask.xmax - v2d->mask.xmin);
1808                 ofs= (float)v2d->mask.xmin;
1809                 
1810                 *viewx= v2d->cur.xmin + (v2d->cur.xmax-v2d->cur.xmin) * ((float)x - ofs) / div;
1811         }
1812
1813         if (viewy) {
1814                 div= (float)(v2d->mask.ymax - v2d->mask.ymin);
1815                 ofs= (float)v2d->mask.ymin;
1816                 
1817                 *viewy= v2d->cur.ymin + (v2d->cur.ymax - v2d->cur.ymin) * ((float)y - ofs) / div;
1818         }
1819 }
1820
1821 /* Convert from 2d-View space to screen/region space
1822  *      - Coordinates are clamped to lie within bounds of region
1823  *
1824  *      - x,y                           = coordinates to convert
1825  *      - regionx,regiony       = resultant coordinates 
1826  */
1827 void UI_view2d_view_to_region(View2D *v2d, float x, float y, int *regionx, int *regiony)
1828 {
1829         /* set initial value in case coordinate lies outside of bounds */
1830         if (regionx)
1831                 *regionx= V2D_IS_CLIPPED;
1832         if (regiony)
1833                 *regiony= V2D_IS_CLIPPED;
1834         
1835         /* express given coordinates as proportional values */
1836         x= (x - v2d->cur.xmin) / (v2d->cur.xmax - v2d->cur.xmin);
1837         y= (y - v2d->cur.ymin) / (v2d->cur.ymax - v2d->cur.ymin);
1838         
1839         /* check if values are within bounds */
1840         if ((x>=0.0f) && (x<=1.0f) && (y>=0.0f) && (y<=1.0f)) {
1841                 if (regionx)
1842                         *regionx= (int)(v2d->mask.xmin + x*(v2d->mask.xmax-v2d->mask.xmin));
1843                 if (regiony)
1844                         *regiony= (int)(v2d->mask.ymin + y*(v2d->mask.ymax-v2d->mask.ymin));
1845         }
1846 }
1847
1848 /* Convert from 2d-view space to screen/region space
1849  *      - Coordinates are NOT clamped to lie within bounds of region
1850  *
1851  *      - x,y                           = coordinates to convert
1852  *      - regionx,regiony       = resultant coordinates 
1853  */
1854 void UI_view2d_to_region_no_clip(View2D *v2d, float x, float y, int *regionx, int *regiony)
1855 {
1856         /* step 1: express given coordinates as proportional values */
1857         x= (x - v2d->cur.xmin) / (v2d->cur.xmax - v2d->cur.xmin);
1858         y= (y - v2d->cur.ymin) / (v2d->cur.ymax - v2d->cur.ymin);
1859         
1860         /* step 2: convert proportional distances to screen coordinates  */
1861         x= v2d->mask.xmin + x*(v2d->mask.xmax - v2d->mask.xmin);
1862         y= v2d->mask.ymin + y*(v2d->mask.ymax - v2d->mask.ymin);
1863         
1864         /* although we don't clamp to lie within region bounds, we must avoid exceeding size of ints */
1865         if (regionx) {
1866                 if (x < INT_MIN) *regionx= INT_MIN;
1867                 else if(x > INT_MAX) *regionx= INT_MAX;
1868                 else *regionx= (int)x;
1869         }
1870         if (regiony) {
1871                 if (y < INT_MIN) *regiony= INT_MIN;
1872                 else if(y > INT_MAX) *regiony= INT_MAX;
1873                 else *regiony= (int)y;
1874         }
1875 }
1876
1877 /* *********************************************************************** */
1878 /* Utilities */
1879
1880 /* View2D data by default resides in region, so get from region stored in context */
1881 View2D *UI_view2d_fromcontext(const bContext *C)
1882 {
1883         ScrArea *area= CTX_wm_area(C);
1884         ARegion *region= CTX_wm_region(C);
1885
1886         if (area == NULL) return NULL;
1887         if (region == NULL) return NULL;
1888         return &(region->v2d);
1889 }
1890
1891 /* same as above, but it returns regionwindow. Utility for pulldowns or buttons */
1892 View2D *UI_view2d_fromcontext_rwin(const bContext *C)
1893 {
1894         ScrArea *area= CTX_wm_area(C);
1895         ARegion *region= CTX_wm_region(C);
1896
1897         if (area == NULL) return NULL;
1898         if (region == NULL) return NULL;
1899         if (region->regiontype!=RGN_TYPE_WINDOW) {
1900                 ARegion *ar= area->regionbase.first;
1901                 for(; ar; ar= ar->next)
1902                         if(ar->regiontype==RGN_TYPE_WINDOW)
1903                                 return &(ar->v2d);
1904                 return NULL;
1905         }
1906         return &(region->v2d);
1907 }
1908
1909
1910 /* Calculate the scale per-axis of the drawing-area
1911  *      - Is used to inverse correct drawing of icons, etc. that need to follow view 
1912  *        but not be affected by scale
1913  *
1914  *      - x,y   = scale on each axis
1915  */
1916 void UI_view2d_getscale(View2D *v2d, float *x, float *y) 
1917 {
1918         if (x) *x = (v2d->mask.xmax - v2d->mask.xmin) / (v2d->cur.xmax - v2d->cur.xmin);
1919         if (y) *y = (v2d->mask.ymax - v2d->mask.ymin) / (v2d->cur.ymax - v2d->cur.ymin);
1920 }
1921
1922 /* Check if mouse is within scrollers
1923  *      - Returns appropriate code for match
1924  *              'h' = in horizontal scroller
1925  *              'v' = in vertical scroller
1926  *              0 = not in scroller
1927  *      
1928  *      - x,y   = mouse coordinates in screen (not region) space
1929  */
1930 short UI_view2d_mouse_in_scrollers (const bContext *C, View2D *v2d, int x, int y)
1931 {
1932         ARegion *ar= CTX_wm_region(C);
1933         int co[2];
1934         int scroll= view2d_scroll_mapped(v2d->scroll);
1935         
1936         /* clamp x,y to region-coordinates first */
1937         co[0]= x - ar->winrct.xmin;
1938         co[1]= y - ar->winrct.ymin;
1939         
1940         /* check if within scrollbars */
1941         if (scroll & V2D_SCROLL_HORIZONTAL) {
1942                 if (IN_2D_HORIZ_SCROLL(v2d, co)) return 'h';
1943         }
1944         if (scroll & V2D_SCROLL_VERTICAL) {
1945                 if (IN_2D_VERT_SCROLL(v2d, co)) return 'v';
1946         }       
1947         
1948         /* not found */
1949         return 0;
1950 }
1951
1952 /* ******************* view2d text drawing cache ******************** */
1953
1954 /* assumes caches are used correctly, so for time being no local storage in v2d */
1955 static ListBase strings= {NULL, NULL};
1956
1957 typedef struct View2DString {
1958         struct View2DString *next, *prev;
1959         float col[4];
1960         char str[128]; 
1961         short mval[2];
1962         rcti rect;
1963 } View2DString;
1964
1965
1966 void UI_view2d_text_cache_add(View2D *v2d, float x, float y, char *str)
1967 {
1968         int mval[2];
1969         
1970         UI_view2d_view_to_region(v2d, x, y, mval, mval+1);
1971         
1972         if(mval[0]!=V2D_IS_CLIPPED && mval[1]!=V2D_IS_CLIPPED) {
1973                 /* use calloc, rect has to be zeroe'd */
1974                 View2DString *v2s= MEM_callocN(sizeof(View2DString), "View2DString");
1975                 
1976                 BLI_addtail(&strings, v2s);
1977                 BLI_strncpy(v2s->str, str, 128);
1978                 v2s->mval[0]= mval[0];
1979                 v2s->mval[1]= mval[1];
1980                 glGetFloatv(GL_CURRENT_COLOR, v2s->col);
1981         }
1982 }
1983
1984 /* no clip (yet) */
1985 void UI_view2d_text_cache_rectf(View2D *v2d, rctf *rect, char *str)
1986 {
1987         View2DString *v2s= MEM_callocN(sizeof(View2DString), "View2DString");
1988         
1989         UI_view2d_to_region_no_clip(v2d, rect->xmin, rect->ymin, &v2s->rect.xmin, &v2s->rect.ymin);
1990         UI_view2d_to_region_no_clip(v2d, rect->xmax, rect->ymax, &v2s->rect.xmax, &v2s->rect.ymax);
1991         
1992         BLI_addtail(&strings, v2s);
1993         BLI_strncpy(v2s->str, str, 128);
1994         glGetFloatv(GL_CURRENT_COLOR, v2s->col);
1995 }
1996
1997
1998 void UI_view2d_text_cache_draw(ARegion *ar)
1999 {
2000         View2DString *v2s;
2001         
2002         //      wmPushMatrix();
2003         ED_region_pixelspace(ar);
2004         
2005         for(v2s= strings.first; v2s; v2s= v2s->next) {
2006                 glColor3fv(v2s->col);
2007                 if(v2s->rect.xmin==v2s->rect.xmax)
2008                         BLF_draw_default((float)v2s->mval[0], (float)v2s->mval[1], 0.0, v2s->str);
2009                 else {
2010                         int xofs=0, yofs;
2011                         
2012                         yofs= ceil( 0.5f*(v2s->rect.ymax - v2s->rect.ymin - BLF_height_default("28")));
2013                         if(yofs<1) yofs= 1;
2014                         
2015                         BLF_clipping(v2s->rect.xmin-4, v2s->rect.ymin-4, v2s->rect.xmax+4, v2s->rect.ymax+4);
2016                         BLF_enable(BLF_CLIPPING);
2017                         
2018                         BLF_draw_default(v2s->rect.xmin+xofs, v2s->rect.ymin+yofs, 0.0f, v2s->str);
2019
2020                         BLF_disable(BLF_CLIPPING);
2021                 }
2022         }
2023         
2024         //      wmPopMatrix();
2025         
2026         if(strings.first) 
2027                 BLI_freelistN(&strings);
2028 }
2029
2030
2031 /* ******************************************************** */
2032
2033