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