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