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