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