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