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