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