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