Blender  V2.59
view2d.c
Go to the documentation of this file.
00001 /*
00002  * $Id: view2d.c 37615 2011-06-18 09:01:26Z campbellbarton $
00003  *
00004  * ***** BEGIN GPL LICENSE BLOCK *****
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (at your option) any later version. 
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software Foundation,
00018  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019  *
00020  * The Original Code is Copyright (C) 2008 Blender Foundation.
00021  * All rights reserved.
00022  * 
00023  * Contributor(s): Blender Foundation, Joshua Leung
00024  *
00025  * ***** END GPL LICENSE BLOCK *****
00026  */
00027 
00033 #include <float.h>
00034 #include <limits.h>
00035 #include <math.h>
00036 #include <string.h>
00037 
00038 #include "MEM_guardedalloc.h"
00039 
00040 #include "DNA_scene_types.h"
00041 #include "DNA_userdef_types.h"
00042 
00043 #include "BLI_blenlib.h"
00044 #include "BLI_utildefines.h"
00045 
00046 #include "BKE_context.h"
00047 #include "BKE_screen.h"
00048 #include "BKE_global.h"
00049 
00050 
00051 #include "WM_api.h"
00052 
00053 #include "BIF_gl.h"
00054 
00055 #include "BLF_api.h"
00056 
00057 #include "ED_anim_api.h"
00058 #include "ED_screen.h"
00059 
00060 #include "UI_interface.h"
00061 #include "UI_view2d.h"
00062 
00063 #include "interface_intern.h"
00064 
00065 /* *********************************************************************** */
00066 
00067 /* XXX still unresolved: scrolls hide/unhide vs region mask handling */
00068 /* XXX there's V2D_SCROLL_HORIZONTAL_HIDE and V2D_SCROLL_HORIZONTAL_FULLR ... */
00069 
00070 /* helper to allow scrollbars to dynamically hide
00071  *      - returns a copy of the scrollbar settings with the flags to display 
00072  *        horizontal/vertical scrollbars removed
00073  *      - input scroll value is the v2d->scroll var
00074  *      - hide flags are set per region at drawtime
00075  */
00076 static int view2d_scroll_mapped(int scroll)
00077 {
00078         if(scroll & V2D_SCROLL_HORIZONTAL_HIDE)
00079                 scroll &= ~(V2D_SCROLL_HORIZONTAL);
00080         if(scroll & V2D_SCROLL_VERTICAL_HIDE)
00081                 scroll &= ~(V2D_SCROLL_VERTICAL);
00082         return scroll;
00083 }
00084 
00085 /* called each time cur changes, to dynamically update masks */
00086 static void view2d_masks(View2D *v2d)
00087 {
00088         int scroll;
00089         
00090         /* mask - view frame */
00091         v2d->mask.xmin= v2d->mask.ymin= 0;
00092         v2d->mask.xmax= v2d->winx - 1;  /* -1 yes! masks are pixels */
00093         v2d->mask.ymax= v2d->winy - 1;
00094 
00095 #if 0
00096         // XXX see above
00097         v2d->scroll &= ~(V2D_SCROLL_HORIZONTAL_HIDE|V2D_SCROLL_VERTICAL_HIDE);
00098         /* check size if: */
00099         if (v2d->scroll & V2D_SCROLL_HORIZONTAL)
00100                 if(!(v2d->scroll & V2D_SCROLL_SCALE_HORIZONTAL))
00101                         if (v2d->tot.xmax-v2d->tot.xmin <= v2d->cur.xmax-v2d->cur.xmin)
00102                                 v2d->scroll |= V2D_SCROLL_HORIZONTAL_HIDE;
00103         if (v2d->scroll & V2D_SCROLL_VERTICAL)
00104                 if(!(v2d->scroll & V2D_SCROLL_SCALE_VERTICAL))
00105                         if (v2d->tot.ymax-v2d->tot.ymin <= v2d->cur.ymax-v2d->cur.ymin)
00106                                 v2d->scroll |= V2D_SCROLL_VERTICAL_HIDE;
00107 #endif
00108         scroll= view2d_scroll_mapped(v2d->scroll);
00109         
00110         /* scrollers shrink mask area, but should be based off regionsize 
00111          *      - they can only be on one to two edges of the region they define
00112          *      - if they overlap, they must not occupy the corners (which are reserved for other widgets)
00113          */
00114         if (scroll) {
00115                 /* vertical scroller */
00116                 if (scroll & V2D_SCROLL_LEFT) {
00117                         /* on left-hand edge of region */
00118                         v2d->vert= v2d->mask;
00119                         v2d->vert.xmax= V2D_SCROLL_WIDTH;
00120                         v2d->mask.xmin= v2d->vert.xmax + 1;
00121                 }
00122                 else if (scroll & V2D_SCROLL_RIGHT) {
00123                         /* on right-hand edge of region */
00124                         v2d->vert= v2d->mask;
00125                         v2d->vert.xmax++; /* one pixel extra... was leaving a minor gap... */
00126                         v2d->vert.xmin= v2d->vert.xmax - V2D_SCROLL_WIDTH;
00127                         v2d->mask.xmax= v2d->vert.xmin - 1;
00128                 }
00129                 
00130                 /* horizontal scroller */
00131                 if (scroll & (V2D_SCROLL_BOTTOM|V2D_SCROLL_BOTTOM_O)) {
00132                         /* on bottom edge of region (V2D_SCROLL_BOTTOM_O is outliner, the other is for standard) */
00133                         v2d->hor= v2d->mask;
00134                         v2d->hor.ymax= V2D_SCROLL_HEIGHT;
00135                         v2d->mask.ymin= v2d->hor.ymax + 1;
00136                 }
00137                 else if (scroll & V2D_SCROLL_TOP) {
00138                         /* on upper edge of region */
00139                         v2d->hor= v2d->mask;
00140                         v2d->hor.ymin= v2d->hor.ymax - V2D_SCROLL_HEIGHT;
00141                         v2d->mask.ymax= v2d->hor.ymin - 1;
00142                 }
00143                 
00144                 /* adjust vertical scroller if there's a horizontal scroller, to leave corner free */
00145                 if (scroll & V2D_SCROLL_VERTICAL) {
00146                         /* just set y min/max for vertical scroller to y min/max of mask as appropriate */
00147                         if (scroll & (V2D_SCROLL_BOTTOM|V2D_SCROLL_BOTTOM_O)) {
00148                                 /* on bottom edge of region (V2D_SCROLL_BOTTOM_O is outliner, the other is for standard) */
00149                                 v2d->vert.ymin= v2d->mask.ymin;
00150                         }
00151                         else if (scroll & V2D_SCROLL_TOP) {
00152                                 /* on upper edge of region */
00153                                 v2d->vert.ymax= v2d->mask.ymax;
00154                         }
00155                 }
00156         }
00157         
00158 }
00159 
00160 /* Refresh and Validation */
00161 
00162 /* Initialise all relevant View2D data (including view rects if first time) and/or refresh mask sizes after view resize
00163  *      - for some of these presets, it is expected that the region will have defined some
00164  *        additional settings necessary for the customisation of the 2D viewport to its requirements
00165  *      - this function should only be called from region init() callbacks, where it is expected that
00166  *        this is called before UI_view2d_size_update(), as this one checks that the rects are properly initialised. 
00167  */
00168 void UI_view2d_region_reinit(View2D *v2d, short type, int winx, int winy)
00169 {
00170         short tot_changed= 0, init= 0;
00171         uiStyle *style= U.uistyles.first;
00172 
00173         /* initialise data if there is a need for such */
00174         if ((v2d->flag & V2D_IS_INITIALISED) == 0) {
00175                 /* set initialised flag so that View2D doesn't get reinitialised next time again */
00176                 v2d->flag |= V2D_IS_INITIALISED;
00177 
00178                 init= 1;
00179                 
00180                 /* see eView2D_CommonViewTypes in UI_view2d.h for available view presets */
00181                 switch (type) {
00182                         /* 'standard view' - optimum setup for 'standard' view behaviour, that should be used new views as basis for their
00183                          *      own unique View2D settings, which should be used instead of this in most cases...
00184                          */
00185                         case V2D_COMMONVIEW_STANDARD:
00186                         {
00187                                 /* for now, aspect ratio should be maintained, and zoom is clamped within sane default limits */
00188                                 v2d->keepzoom= (V2D_KEEPASPECT|V2D_LIMITZOOM);
00189                                 v2d->minzoom= 0.01f;
00190                                 v2d->maxzoom= 1000.0f;
00191                                 
00192                                 /* tot rect and cur should be same size, and aligned using 'standard' OpenGL coordinates for now 
00193                                  *      - region can resize 'tot' later to fit other data
00194                                  *      - keeptot is only within bounds, as strict locking is not that critical
00195                                  *      - view is aligned for (0,0) -> (winx-1, winy-1) setup
00196                                  */
00197                                 v2d->align= (V2D_ALIGN_NO_NEG_X|V2D_ALIGN_NO_NEG_Y);
00198                                 v2d->keeptot= V2D_KEEPTOT_BOUNDS;
00199                                 
00200                                 v2d->tot.xmin= v2d->tot.ymin= 0.0f;
00201                                 v2d->tot.xmax= (float)(winx - 1);
00202                                 v2d->tot.ymax= (float)(winy - 1);
00203                                 
00204                                 v2d->cur= v2d->tot;
00205                                 
00206                                 /* scrollers - should we have these by default? */
00207                                 // XXX for now, we don't override this, or set it either!
00208                         }
00209                                 break;
00210                         
00211                         /* 'list/channel view' - zoom, aspect ratio, and alignment restrictions are set here */
00212                         case V2D_COMMONVIEW_LIST:
00213                         {
00214                                 /* zoom + aspect ratio are locked */
00215                                 v2d->keepzoom = (V2D_LOCKZOOM_X|V2D_LOCKZOOM_Y|V2D_LIMITZOOM|V2D_KEEPASPECT);
00216                                 v2d->minzoom= v2d->maxzoom= 1.0f;
00217                                 
00218                                 /* tot rect has strictly regulated placement, and must only occur in +/- quadrant */
00219                                 v2d->align = (V2D_ALIGN_NO_NEG_X|V2D_ALIGN_NO_POS_Y);
00220                                 v2d->keeptot = V2D_KEEPTOT_STRICT;
00221                                 tot_changed= 1;
00222                                 
00223                                 /* scroller settings are currently not set here... that is left for regions... */
00224                         }
00225                                 break;
00226                                 
00227                         /* 'stack view' - practically the same as list/channel view, except is located in the pos y half instead. 
00228                          *      zoom, aspect ratio, and alignment restrictions are set here */
00229                         case V2D_COMMONVIEW_STACK:
00230                         {
00231                                 /* zoom + aspect ratio are locked */
00232                                 v2d->keepzoom = (V2D_LOCKZOOM_X|V2D_LOCKZOOM_Y|V2D_LIMITZOOM|V2D_KEEPASPECT);
00233                                 v2d->minzoom= v2d->maxzoom= 1.0f;
00234                                 
00235                                 /* tot rect has strictly regulated placement, and must only occur in +/+ quadrant */
00236                                 v2d->align = (V2D_ALIGN_NO_NEG_X|V2D_ALIGN_NO_NEG_Y);
00237                                 v2d->keeptot = V2D_KEEPTOT_STRICT;
00238                                 tot_changed= 1;
00239                                 
00240                                 /* scroller settings are currently not set here... that is left for regions... */
00241                         }
00242                                 break;
00243                                 
00244                         /* 'header' regions - zoom, aspect ratio, alignment, and panning restrictions are set here */
00245                         case V2D_COMMONVIEW_HEADER:
00246                         {
00247                                 /* zoom + aspect ratio are locked */
00248                                 v2d->keepzoom = (V2D_LOCKZOOM_X|V2D_LOCKZOOM_Y|V2D_LIMITZOOM|V2D_KEEPASPECT);
00249                                 v2d->minzoom= v2d->maxzoom= 1.0f;
00250                                 v2d->min[0]= v2d->max[0]= (float)(winx-1);
00251                                 v2d->min[1]= v2d->max[1]= (float)(winy-1);
00252                                 
00253                                 /* tot rect has strictly regulated placement, and must only occur in +/+ quadrant */
00254                                 v2d->align = (V2D_ALIGN_NO_NEG_X|V2D_ALIGN_NO_NEG_Y);
00255                                 v2d->keeptot = V2D_KEEPTOT_STRICT;
00256                                 tot_changed= 1;
00257                                 
00258                                 /* panning in y-axis is prohibited */
00259                                 v2d->keepofs= V2D_LOCKOFS_Y;
00260                                 
00261                                 /* absolutely no scrollers allowed */
00262                                 v2d->scroll= 0;
00263                                 
00264                         }
00265                                 break;
00266                         
00267                         /* panels view, with horizontal/vertical align */
00268                         case V2D_COMMONVIEW_PANELS_UI:
00269                         {
00270                                 float panelzoom= (style) ? style->panelzoom : 1.0f;
00271                                 
00272                                 /* for now, aspect ratio should be maintained, and zoom is clamped within sane default limits */
00273                                 v2d->keepzoom= (V2D_KEEPASPECT|V2D_LIMITZOOM|V2D_KEEPZOOM);
00274                                 v2d->minzoom= 0.5f;
00275                                 v2d->maxzoom= 2.0f;
00276                                 //tot_changed= 1;
00277                                 
00278                                 v2d->align= (V2D_ALIGN_NO_NEG_X|V2D_ALIGN_NO_POS_Y);
00279                                 v2d->keeptot= V2D_KEEPTOT_BOUNDS;
00280                                 
00281                                 v2d->scroll |= (V2D_SCROLL_RIGHT|V2D_SCROLL_BOTTOM);
00282                                 v2d->scroll |= V2D_SCROLL_HORIZONTAL_HIDE;
00283                                 v2d->scroll &= ~V2D_SCROLL_VERTICAL_HIDE;
00284 
00285                                 v2d->tot.xmin= 0.0f;
00286                                 v2d->tot.xmax= winx;
00287                                 
00288                                 v2d->tot.ymax= 0.0f;
00289                                 v2d->tot.ymin= -winy;
00290                                 
00291                                 v2d->cur.xmin= 0.0f;
00292                                 /* bad workaround for keeping zoom level with scrollers */
00293                                 v2d->cur.xmax= (winx - V2D_SCROLL_WIDTH)*panelzoom;
00294                                 
00295                                 v2d->cur.ymax= 0.0f;
00296                                 v2d->cur.ymin= (-winy)*panelzoom;
00297                         }
00298                                 break;
00299                                 
00300                                 /* other view types are completely defined using their own settings already */
00301                         default:
00302                                 /* we don't do anything here, as settings should be fine, but just make sure that rect */
00303                                 break;  
00304                 }
00305         }
00306         
00307         /* store view size */
00308         v2d->winx= winx;
00309         v2d->winy= winy;
00310         
00311         /* set masks */
00312         view2d_masks(v2d);
00313         
00314         /* set 'tot' rect before setting cur? */
00315         if (tot_changed) 
00316                 UI_view2d_totRect_set_resize(v2d, winx, winy, !init);
00317         else
00318                 UI_view2d_curRect_validate_resize(v2d, !init);
00319 }
00320 
00321 /* Ensure View2D rects remain in a viable configuration 
00322  *      - cur is not allowed to be: larger than max, smaller than min, or outside of tot
00323  */
00324 // XXX pre2.5 -> this used to be called  test_view2d()
00325 void UI_view2d_curRect_validate_resize(View2D *v2d, int resize)
00326 {
00327         float totwidth, totheight, curwidth, curheight, width, height;
00328         float winx, winy;
00329         rctf *cur, *tot;
00330         
00331         /* use mask as size of region that View2D resides in, as it takes into account scrollbars already  */
00332         winx= (float)(v2d->mask.xmax - v2d->mask.xmin + 1);
00333         winy= (float)(v2d->mask.ymax - v2d->mask.ymin + 1);
00334         
00335         /* get pointers to rcts for less typing */
00336         cur= &v2d->cur;
00337         tot= &v2d->tot;
00338         
00339         /* we must satisfy the following constraints (in decreasing order of importance):
00340          *      - alignment restrictions are respected
00341          *      - cur must not fall outside of tot
00342          *      - axis locks (zoom and offset) must be maintained
00343          *      - zoom must not be excessive (check either sizes or zoom values)
00344          *      - aspect ratio should be respected (NOTE: this is quite closely realted to zoom too)
00345          */
00346         
00347         /* Step 1: if keepzoom, adjust the sizes of the rects only
00348          *      - firstly, we calculate the sizes of the rects
00349          *      - curwidth and curheight are saved as reference... modify width and height values here
00350          */
00351         totwidth= tot->xmax - tot->xmin;
00352         totheight= tot->ymax - tot->ymin;
00353         curwidth= width= cur->xmax - cur->xmin;
00354         curheight= height= cur->ymax - cur->ymin;
00355         
00356         /* if zoom is locked, size on the appropriate axis is reset to mask size */
00357         if (v2d->keepzoom & V2D_LOCKZOOM_X)
00358                 width= winx;
00359         if (v2d->keepzoom & V2D_LOCKZOOM_Y)
00360                 height= winy;
00361                 
00362         /* values used to divide, so make it safe 
00363          * NOTE: width and height must use FLT_MIN instead of 1, otherwise it is impossible to
00364          *              get enough resolution in Graph Editor for editing some curves
00365          */
00366         if(width < FLT_MIN) width= 1;
00367         if(height < FLT_MIN) height= 1;
00368         if(winx < 1) winx= 1;
00369         if(winy < 1) winy= 1;
00370         
00371         /* V2D_LIMITZOOM indicates that zoom level should be preserved when the window size changes */
00372         if (resize && (v2d->keepzoom & V2D_KEEPZOOM)) {
00373                 float zoom, oldzoom;
00374 
00375                 if ((v2d->keepzoom & V2D_LOCKZOOM_X)==0) {
00376                         zoom= winx / width;
00377                         oldzoom= v2d->oldwinx / curwidth;
00378 
00379                         if(oldzoom != zoom)
00380                                 width *= zoom/oldzoom;
00381                 }
00382 
00383                 if ((v2d->keepzoom & V2D_LOCKZOOM_Y)==0) {
00384                         zoom= winy / height;
00385                         oldzoom= v2d->oldwiny / curheight;
00386 
00387                         if(oldzoom != zoom)
00388                                 height *= zoom/oldzoom;
00389                 }
00390         }
00391         /* keepzoom (V2D_LIMITZOOM set), indicates that zoom level on each axis must not exceed limits 
00392          * NOTE: in general, it is not expected that the lock-zoom will be used in conjunction with this
00393          */
00394         else if (v2d->keepzoom & V2D_LIMITZOOM) {
00395                 float zoom, fac;
00396                 
00397                 /* check if excessive zoom on x-axis */
00398                 if ((v2d->keepzoom & V2D_LOCKZOOM_X)==0) {
00399                         zoom= winx / width;
00400                         if ((zoom < v2d->minzoom) || (zoom > v2d->maxzoom)) {
00401                                 fac= (zoom < v2d->minzoom) ? (zoom / v2d->minzoom) : (zoom / v2d->maxzoom);
00402                                 width *= fac;
00403                         }
00404                 }
00405                 
00406                 /* check if excessive zoom on y-axis */
00407                 if ((v2d->keepzoom & V2D_LOCKZOOM_Y)==0) {
00408                         zoom= winy / height;
00409                         if ((zoom < v2d->minzoom) || (zoom > v2d->maxzoom)) {
00410                                 fac= (zoom < v2d->minzoom) ? (zoom / v2d->minzoom) : (zoom / v2d->maxzoom);
00411                                 height *= fac;
00412                         }
00413                 }
00414         }
00415         else {
00416                 /* make sure sizes don't exceed that of the min/max sizes (even though we're not doing zoom clamping) */
00417                 CLAMP(width, v2d->min[0], v2d->max[0]);
00418                 CLAMP(height, v2d->min[1], v2d->max[1]);
00419         }
00420         
00421         /* check if we should restore aspect ratio (if view size changed) */
00422         if (v2d->keepzoom & V2D_KEEPASPECT) {
00423                 short do_x=0, do_y=0, do_cur, do_win;
00424                 float curRatio, winRatio;
00425                 
00426                 /* when a window edge changes, the aspect ratio can't be used to
00427                  * find which is the best new 'cur' rect. thats why it stores 'old' 
00428                  */
00429                 if (winx != v2d->oldwinx) do_x= 1;
00430                 if (winy != v2d->oldwiny) do_y= 1;
00431                 
00432                 curRatio= height / width;
00433                 winRatio= winy / winx;
00434                 
00435                 /* both sizes change (area/region maximised)  */
00436                 if (do_x == do_y) {
00437                         if (do_x && do_y) {
00438                                 /* here is 1,1 case, so all others must be 0,0 */
00439                                 if (ABS(winx - v2d->oldwinx) > ABS(winy - v2d->oldwiny)) do_y= 0;
00440                                 else do_x= 0;
00441                         }
00442                         else if (winRatio > 1.0f) do_x= 0; 
00443                         else do_x= 1;
00444                 }
00445                 do_cur= do_x;
00446                 do_win= do_y;
00447                 
00448                 if (do_cur) {
00449                         if ((v2d->keeptot == V2D_KEEPTOT_STRICT) && (winx != v2d->oldwinx)) {
00450                                 /* special exception for Outliner (and later channel-lists):
00451                                  *      - The view may be moved left to avoid contents being pushed out of view when view shrinks. 
00452                                  *      - The keeptot code will make sure cur->xmin will not be less than tot->xmin (which cannot be allowed)
00453                                  *      - width is not adjusted for changed ratios here...
00454                                  */
00455                                 if (winx < v2d->oldwinx) {
00456                                         float temp = v2d->oldwinx - winx;
00457                                         
00458                                         cur->xmin -= temp;
00459                                         cur->xmax -= temp;
00460                                         
00461                                         /* width does not get modified, as keepaspect here is just set to make 
00462                                          * sure visible area adjusts to changing view shape! 
00463                                          */
00464                                 }
00465                         }
00466                         else {
00467                                 /* portrait window: correct for x */
00468                                 width= height / winRatio;
00469                         }
00470                 }
00471                 else {
00472                         if ((v2d->keeptot == V2D_KEEPTOT_STRICT) && (winy != v2d->oldwiny)) {
00473                                 /* special exception for Outliner (and later channel-lists):
00474                                  *      - Currently, no actions need to be taken here...
00475                                  */
00476 
00477                                 if (winy < v2d->oldwiny) {
00478                                         float temp = v2d->oldwiny - winy;
00479                                         
00480                                         cur->ymin += temp;
00481                                         cur->ymax += temp;
00482                                 }
00483 
00484                         }
00485                         else {
00486                                 /* landscape window: correct for y */
00487                                 height = width * winRatio;
00488                         }
00489                 }
00490                 
00491                 /* store region size for next time */
00492                 v2d->oldwinx= (short)winx; 
00493                 v2d->oldwiny= (short)winy;
00494         }
00495         
00496         /* Step 2: apply new sizes to cur rect, but need to take into account alignment settings here... */
00497         if ((width != curwidth) || (height != curheight)) {
00498                 float temp, dh;
00499                 
00500                 /* resize from centerpoint, unless otherwise specified */
00501                 if (width != curwidth) {
00502                         if (v2d->keepofs & V2D_LOCKOFS_X) {
00503                                 cur->xmax += width - (cur->xmax - cur->xmin);
00504                         }
00505                         else if (v2d->keepofs & V2D_KEEPOFS_X) {
00506                                 if(v2d->align & V2D_ALIGN_NO_POS_X)
00507                                         cur->xmin -= width - (cur->xmax - cur->xmin);
00508                                 else
00509                                         cur->xmax += width - (cur->xmax - cur->xmin);
00510                         }
00511                         else {
00512                                 temp= (cur->xmax + cur->xmin) * 0.5f;
00513                                 dh= width * 0.5f;
00514                                 
00515                                 cur->xmin = temp - dh;
00516                                 cur->xmax = temp + dh;
00517                         }
00518                 }
00519                 if (height != curheight) {
00520                         if (v2d->keepofs & V2D_LOCKOFS_Y) {
00521                                 cur->ymax += height - (cur->ymax - cur->ymin);
00522                         }
00523                         else if (v2d->keepofs & V2D_KEEPOFS_Y) {
00524                                 if(v2d->align & V2D_ALIGN_NO_POS_Y)
00525                                         cur->ymin -= height - (cur->ymax - cur->ymin);
00526                                 else
00527                                         cur->ymax += height - (cur->ymax - cur->ymin);
00528                         }
00529                         else {
00530                                 temp= (cur->ymax + cur->ymin) * 0.5f;
00531                                 dh= height * 0.5f;
00532                                 
00533                                 cur->ymin = temp - dh;
00534                                 cur->ymax = temp + dh;
00535                         }
00536                 }
00537         }
00538         
00539         /* Step 3: adjust so that it doesn't fall outside of bounds of 'tot' */
00540         if (v2d->keeptot) {
00541                 float temp, diff;
00542                 
00543                 /* recalculate extents of cur */
00544                 curwidth= cur->xmax - cur->xmin;
00545                 curheight= cur->ymax - cur->ymin;
00546                 
00547                 /* width */
00548                 if ( (curwidth > totwidth) && !(v2d->keepzoom & (V2D_KEEPZOOM|V2D_LOCKZOOM_X|V2D_LIMITZOOM)) ) {
00549                         /* if zoom doesn't have to be maintained, just clamp edges */
00550                         if (cur->xmin < tot->xmin) cur->xmin= tot->xmin;
00551                         if (cur->xmax > tot->xmax) cur->xmax= tot->xmax;
00552                 }
00553                 else if (v2d->keeptot == V2D_KEEPTOT_STRICT) {
00554                         /* This is an exception for the outliner (and later channel-lists, headers) 
00555                          *      - must clamp within tot rect (absolutely no excuses)
00556                          *      --> therefore, cur->xmin must not be less than tot->xmin
00557                          */
00558                         if (cur->xmin < tot->xmin) {
00559                                 /* move cur across so that it sits at minimum of tot */
00560                                 temp= tot->xmin - cur->xmin;
00561                                 
00562                                 cur->xmin += temp;
00563                                 cur->xmax += temp;
00564                         }
00565                         else if (cur->xmax > tot->xmax) {
00566                                 /* - only offset by difference of cur-xmax and tot-xmax if that would not move 
00567                                  *      cur-xmin to lie past tot-xmin
00568                                  * - otherwise, simply shift to tot-xmin???
00569                                  */
00570                                 temp= cur->xmax - tot->xmax;
00571                                 
00572                                 if ((cur->xmin - temp) < tot->xmin) {
00573                                         /* only offset by difference from cur-min and tot-min */
00574                                         temp= cur->xmin - tot->xmin;
00575                                         
00576                                         cur->xmin -= temp;
00577                                         cur->xmax -= temp;
00578                                 }
00579                                 else {
00580                                         cur->xmin -= temp;
00581                                         cur->xmax -= temp;
00582                                 }
00583                         }
00584                 }
00585                 else {
00586                         /* This here occurs when:
00587                          *      - width too big, but maintaining zoom (i.e. widths cannot be changed)
00588                          *      - width is OK, but need to check if outside of boundaries
00589                          * 
00590                          * So, resolution is to just shift view by the gap between the extremities.
00591                          * We favour moving the 'minimum' across, as that's origin for most things
00592                          * (XXX - in the past, max was favoured... if there are bugs, swap!)
00593                          */
00594                         if ((cur->xmin < tot->xmin) && (cur->xmax > tot->xmax)) {
00595                                 /* outside boundaries on both sides, so take middle-point of tot, and place in balanced way */
00596                                 temp= (tot->xmax + tot->xmin) * 0.5f;
00597                                 diff= curheight * 0.5f;
00598                                 
00599                                 cur->xmin= temp - diff;
00600                                 cur->xmax= temp + diff;
00601                         }
00602                         else if (cur->xmin < tot->xmin) {
00603                                 /* move cur across so that it sits at minimum of tot */
00604                                 temp= tot->xmin - cur->xmin;
00605                                 
00606                                 cur->xmin += temp;
00607                                 cur->xmax += temp;
00608                         }
00609                         else if (cur->xmax > tot->xmax) {
00610                                 /* - only offset by difference of cur-xmax and tot-xmax if that would not move 
00611                                  *      cur-xmin to lie past tot-xmin
00612                                  * - otherwise, simply shift to tot-xmin???
00613                                  */
00614                                 temp= cur->xmax - tot->xmax;
00615                                 
00616                                 if ((cur->xmin - temp) < tot->xmin) {
00617                                         /* only offset by difference from cur-min and tot-min */
00618                                         temp= cur->xmin - tot->xmin;
00619                                         
00620                                         cur->xmin -= temp;
00621                                         cur->xmax -= temp;
00622                                 }
00623                                 else {
00624                                         cur->xmin -= temp;
00625                                         cur->xmax -= temp;
00626                                 }
00627                         }
00628                 }
00629                 
00630                 /* height */
00631                 if ( (curheight > totheight) && !(v2d->keepzoom & (V2D_KEEPZOOM|V2D_LOCKZOOM_Y|V2D_LIMITZOOM)) ) {
00632                         /* if zoom doesn't have to be maintained, just clamp edges */
00633                         if (cur->ymin < tot->ymin) cur->ymin= tot->ymin;
00634                         if (cur->ymax > tot->ymax) cur->ymax= tot->ymax;
00635                 }
00636                 else {
00637                         /* This here occurs when:
00638                          *      - height too big, but maintaining zoom (i.e. heights cannot be changed)
00639                          *      - height is OK, but need to check if outside of boundaries
00640                          * 
00641                          * So, resolution is to just shift view by the gap between the extremities.
00642                          * We favour moving the 'minimum' across, as that's origin for most things
00643                          */
00644                         if ((cur->ymin < tot->ymin) && (cur->ymax > tot->ymax)) {
00645                                 /* outside boundaries on both sides, so take middle-point of tot, and place in balanced way */
00646                                 temp= (tot->ymax + tot->ymin) * 0.5f;
00647                                 diff= curheight * 0.5f;
00648                                 
00649                                 cur->ymin= temp - diff;
00650                                 cur->ymax= temp + diff;
00651                         }
00652                         else if (cur->ymin < tot->ymin) {
00653                                 /* there's still space remaining, so shift up */
00654                                 temp= tot->ymin - cur->ymin;
00655                                 
00656                                 cur->ymin += temp;
00657                                 cur->ymax += temp;
00658                         }
00659                         else if (cur->ymax > tot->ymax) {
00660                                 /* there's still space remaining, so shift down */
00661                                 temp= cur->ymax - tot->ymax;
00662                                 
00663                                 cur->ymin -= temp;
00664                                 cur->ymax -= temp;
00665                         }
00666                 }
00667         }
00668         
00669         /* Step 4: Make sure alignment restrictions are respected */
00670         if (v2d->align) {
00671                 /* If alignment flags are set (but keeptot is not), they must still be respected, as although
00672                  * they don't specify any particular bounds to stay within, they do define ranges which are 
00673                  * invalid.
00674                  *
00675                  * Here, we only check to make sure that on each axis, the 'cur' rect doesn't stray into these 
00676                  * invalid zones, otherwise we offset.
00677                  */
00678                 
00679                 /* handle width - posx and negx flags are mutually exclusive, so watch out */
00680                 if ((v2d->align & V2D_ALIGN_NO_POS_X) && !(v2d->align & V2D_ALIGN_NO_NEG_X)) {
00681                         /* width is in negative-x half */
00682                         if (v2d->cur.xmax > 0) {
00683                                 v2d->cur.xmin -= v2d->cur.xmax;
00684                                 v2d->cur.xmax= 0.0f;
00685                         }
00686                 }
00687                 else if ((v2d->align & V2D_ALIGN_NO_NEG_X) && !(v2d->align & V2D_ALIGN_NO_POS_X)) {
00688                         /* width is in positive-x half */
00689                         if (v2d->cur.xmin < 0) {
00690                                 v2d->cur.xmax -= v2d->cur.xmin;
00691                                 v2d->cur.xmin = 0.0f;
00692                         }
00693                 }
00694                 
00695                 /* handle height - posx and negx flags are mutually exclusive, so watch out */
00696                 if ((v2d->align & V2D_ALIGN_NO_POS_Y) && !(v2d->align & V2D_ALIGN_NO_NEG_Y)) {
00697                         /* height is in negative-y half */
00698                         if (v2d->cur.ymax > 0) {
00699                                 v2d->cur.ymin -= v2d->cur.ymax;
00700                                 v2d->cur.ymax = 0.0f;
00701                         }
00702                 }
00703                 else if ((v2d->align & V2D_ALIGN_NO_NEG_Y) && !(v2d->align & V2D_ALIGN_NO_POS_Y)) {
00704                         /* height is in positive-y half */
00705                         if (v2d->cur.ymin < 0) {
00706                                 v2d->cur.ymax -= v2d->cur.ymin;
00707                                 v2d->cur.ymin = 0.0f;
00708                         }
00709                 }
00710         }
00711         
00712         /* set masks */
00713         view2d_masks(v2d);
00714 }
00715 
00716 void UI_view2d_curRect_validate(View2D *v2d)
00717 {
00718         UI_view2d_curRect_validate_resize(v2d, 0);
00719 }
00720 
00721 /* ------------------ */
00722 
00723 /* Called by menus to activate it, or by view2d operators to make sure 'related' views stay in synchrony */
00724 void UI_view2d_sync(bScreen *screen, ScrArea *area, View2D *v2dcur, int flag)
00725 {
00726         ScrArea *sa;
00727         ARegion *ar;
00728         
00729         /* don't continue if no view syncing to be done */
00730         if ((v2dcur->flag & (V2D_VIEWSYNC_SCREEN_TIME|V2D_VIEWSYNC_AREA_VERTICAL)) == 0)
00731                 return;
00732                 
00733         /* check if doing within area syncing (i.e. channels/vertical) */
00734         if ((v2dcur->flag & V2D_VIEWSYNC_AREA_VERTICAL) && (area)) {
00735                 for (ar= area->regionbase.first; ar; ar= ar->next) {
00736                         /* don't operate on self */
00737                         if (v2dcur != &ar->v2d) {
00738                                 /* only if view has vertical locks enabled */
00739                                 if (ar->v2d.flag & V2D_VIEWSYNC_AREA_VERTICAL) {
00740                                         if (flag == V2D_LOCK_COPY) {
00741                                                 /* other views with locks on must copy active */
00742                                                 ar->v2d.cur.ymin= v2dcur->cur.ymin;
00743                                                 ar->v2d.cur.ymax= v2dcur->cur.ymax;
00744                                         }
00745                                         else { /* V2D_LOCK_SET */
00746                                                 /* active must copy others */
00747                                                 v2dcur->cur.ymin= ar->v2d.cur.ymin;
00748                                                 v2dcur->cur.ymax= ar->v2d.cur.ymax;
00749                                         }
00750                                         
00751                                         /* region possibly changed, so refresh */
00752                                         ED_region_tag_redraw(ar);
00753                                 }
00754                         }
00755                 }
00756         }
00757         
00758         /* check if doing whole screen syncing (i.e. time/horizontal) */
00759         if ((v2dcur->flag & V2D_VIEWSYNC_SCREEN_TIME) && (screen)) {
00760                 for (sa= screen->areabase.first; sa; sa= sa->next) {
00761                         for (ar= sa->regionbase.first; ar; ar= ar->next) {
00762                                 /* don't operate on self */
00763                                 if (v2dcur != &ar->v2d) {
00764                                         /* only if view has horizontal locks enabled */
00765                                         if (ar->v2d.flag & V2D_VIEWSYNC_SCREEN_TIME) {
00766                                                 if (flag == V2D_LOCK_COPY) {
00767                                                         /* other views with locks on must copy active */
00768                                                         ar->v2d.cur.xmin= v2dcur->cur.xmin;
00769                                                         ar->v2d.cur.xmax= v2dcur->cur.xmax;
00770                                                 }
00771                                                 else { /* V2D_LOCK_SET */
00772                                                         /* active must copy others */
00773                                                         v2dcur->cur.xmin= ar->v2d.cur.xmin;
00774                                                         v2dcur->cur.xmax= ar->v2d.cur.xmax;
00775                                                 }
00776                                                 
00777                                                 /* region possibly changed, so refresh */
00778                                                 ED_region_tag_redraw(ar);
00779                                         }
00780                                 }
00781                         }
00782                 }
00783         }
00784 }
00785 
00786 
00787 /* Restore 'cur' rect to standard orientation (i.e. optimal maximum view of tot) 
00788  * This does not take into account if zooming the view on an axis will improve the view (if allowed)
00789  */
00790 void UI_view2d_curRect_reset (View2D *v2d)
00791 {
00792         float width, height;
00793         
00794         /* assume width and height of 'cur' rect by default, should be same size as mask */
00795         width= (float)(v2d->mask.xmax - v2d->mask.xmin + 1);
00796         height= (float)(v2d->mask.ymax - v2d->mask.ymin + 1);
00797         
00798         /* handle width - posx and negx flags are mutually exclusive, so watch out */
00799         if ((v2d->align & V2D_ALIGN_NO_POS_X) && !(v2d->align & V2D_ALIGN_NO_NEG_X)) {
00800                 /* width is in negative-x half */
00801                 v2d->cur.xmin= (float)-width;
00802                 v2d->cur.xmax= 0.0f;
00803         }
00804         else if ((v2d->align & V2D_ALIGN_NO_NEG_X) && !(v2d->align & V2D_ALIGN_NO_POS_X)) {
00805                 /* width is in positive-x half */
00806                 v2d->cur.xmin= 0.0f;
00807                 v2d->cur.xmax= (float)width;
00808         }
00809         else {
00810                 /* width is centered around x==0 */
00811                 const float dx= (float)width / 2.0f;
00812                 
00813                 v2d->cur.xmin= -dx;
00814                 v2d->cur.xmax= dx;
00815         }
00816         
00817         /* handle height - posx and negx flags are mutually exclusive, so watch out */
00818         if ((v2d->align & V2D_ALIGN_NO_POS_Y) && !(v2d->align & V2D_ALIGN_NO_NEG_Y)) {
00819                 /* height is in negative-y half */
00820                 v2d->cur.ymin= (float)-height;
00821                 v2d->cur.ymax= 0.0f;
00822         }
00823         else if ((v2d->align & V2D_ALIGN_NO_NEG_Y) && !(v2d->align & V2D_ALIGN_NO_POS_Y)) {
00824                 /* height is in positive-y half */
00825                 v2d->cur.ymin= 0.0f;
00826                 v2d->cur.ymax= (float)height;
00827         }
00828         else {
00829                 /* height is centered around y==0 */
00830                 const float dy= (float)height / 2.0f;
00831                 
00832                 v2d->cur.ymin= -dy;
00833                 v2d->cur.ymax= dy;
00834         }
00835 }
00836 
00837 /* ------------------ */
00838 
00839 /* Change the size of the maximum viewable area (i.e. 'tot' rect) */
00840 void UI_view2d_totRect_set_resize (View2D *v2d, int width, int height, int resize)
00841 {
00842         int scroll= view2d_scroll_mapped(v2d->scroll);
00843         
00844         /* don't do anything if either value is 0 */
00845         width= abs(width);
00846         height= abs(height);
00847         
00848         /* hrumf! */
00849         /* XXX: there are work arounds for this in the panel and file browse code. */
00850         if(scroll & V2D_SCROLL_HORIZONTAL) 
00851                 width -= V2D_SCROLL_WIDTH;
00852         if(scroll & V2D_SCROLL_VERTICAL) 
00853                 height -= V2D_SCROLL_HEIGHT;
00854         
00855         if (ELEM3(0, v2d, width, height)) {
00856                 if (G.f & G_DEBUG)
00857                         printf("Error: View2D totRect set exiting: v2d=%p width=%d height=%d \n", (void *)v2d, width, height); // XXX temp debug info
00858                 return;
00859         }
00860         
00861         /* handle width - posx and negx flags are mutually exclusive, so watch out */
00862         if ((v2d->align & V2D_ALIGN_NO_POS_X) && !(v2d->align & V2D_ALIGN_NO_NEG_X)) {
00863                 /* width is in negative-x half */
00864                 v2d->tot.xmin= (float)-width;
00865                 v2d->tot.xmax= 0.0f;
00866         }
00867         else if ((v2d->align & V2D_ALIGN_NO_NEG_X) && !(v2d->align & V2D_ALIGN_NO_POS_X)) {
00868                 /* width is in positive-x half */
00869                 v2d->tot.xmin= 0.0f;
00870                 v2d->tot.xmax= (float)width;
00871         }
00872         else {
00873                 /* width is centered around x==0 */
00874                 const float dx= (float)width / 2.0f;
00875                 
00876                 v2d->tot.xmin= -dx;
00877                 v2d->tot.xmax= dx;
00878         }
00879         
00880         /* handle height - posx and negx flags are mutually exclusive, so watch out */
00881         if ((v2d->align & V2D_ALIGN_NO_POS_Y) && !(v2d->align & V2D_ALIGN_NO_NEG_Y)) {
00882                 /* height is in negative-y half */
00883                 v2d->tot.ymin= (float)-height;
00884                 v2d->tot.ymax= 0.0f;
00885         }
00886         else if ((v2d->align & V2D_ALIGN_NO_NEG_Y) && !(v2d->align & V2D_ALIGN_NO_POS_Y)) {
00887                 /* height is in positive-y half */
00888                 v2d->tot.ymin= 0.0f;
00889                 v2d->tot.ymax= (float)height;
00890         }
00891         else {
00892                 /* height is centered around y==0 */
00893                 const float dy= (float)height / 2.0f;
00894                 
00895                 v2d->tot.ymin= -dy;
00896                 v2d->tot.ymax= dy;
00897         }
00898         
00899         /* make sure that 'cur' rect is in a valid state as a result of these changes */
00900         UI_view2d_curRect_validate_resize(v2d, resize);
00901 }
00902 
00903 void UI_view2d_totRect_set(View2D *v2d, int width, int height)
00904 {
00905         UI_view2d_totRect_set_resize(v2d, width, height, 0);
00906 }
00907 
00908 int UI_view2d_tab_set(View2D *v2d, int tab)
00909 {
00910         float default_offset[2]= {0.0f, 0.0f};
00911         float *offset, *new_offset;
00912         int changed= 0;
00913 
00914         /* if tab changed, change offset */
00915         if(tab != v2d->tab_cur && v2d->tab_offset) {
00916                 if(tab < v2d->tab_num)
00917                         offset= &v2d->tab_offset[tab*2];
00918                 else
00919                         offset= default_offset;
00920 
00921                 v2d->cur.xmax += offset[0] - v2d->cur.xmin;
00922                 v2d->cur.xmin= offset[0];
00923 
00924                 v2d->cur.ymin += offset[1] - v2d->cur.ymax;
00925                 v2d->cur.ymax= offset[1];
00926 
00927                 /* validation should happen in subsequent totRect_set */
00928 
00929                 changed= 1;
00930         }
00931 
00932         /* resize array if needed */
00933         if(tab >= v2d->tab_num) {
00934                 new_offset= MEM_callocN(sizeof(float)*(tab+1)*2, "view2d tab offset");
00935 
00936                 if(v2d->tab_offset) {
00937                         memcpy(new_offset, v2d->tab_offset, sizeof(float)*v2d->tab_num*2);
00938                         MEM_freeN(v2d->tab_offset);
00939                 }
00940 
00941                 v2d->tab_offset= new_offset;
00942                 v2d->tab_num= tab+1;
00943         }
00944 
00945         /* set current tab and offset */
00946         v2d->tab_cur= tab;
00947         v2d->tab_offset[2*tab+0]= v2d->cur.xmin;
00948         v2d->tab_offset[2*tab+1]= v2d->cur.ymax;
00949 
00950         return changed;
00951 }
00952 
00953 /* *********************************************************************** */
00954 /* View Matrix Setup */
00955 
00956 /* mapping function to ensure 'cur' draws extended over the area where sliders are */
00957 static void view2d_map_cur_using_mask(View2D *v2d, rctf *curmasked)
00958 {
00959         *curmasked= v2d->cur;
00960         
00961         if (view2d_scroll_mapped(v2d->scroll)) {
00962                 float dx= (v2d->cur.xmax-v2d->cur.xmin)/((float)(v2d->mask.xmax-v2d->mask.xmin+1));
00963                 float dy= (v2d->cur.ymax-v2d->cur.ymin)/((float)(v2d->mask.ymax-v2d->mask.ymin+1));
00964                 
00965                 if (v2d->mask.xmin != 0)
00966                         curmasked->xmin -= dx*(float)v2d->mask.xmin;
00967                 if (v2d->mask.xmax+1 != v2d->winx)
00968                         curmasked->xmax += dx*(float)(v2d->winx - v2d->mask.xmax-1);
00969                 
00970                 if (v2d->mask.ymin != 0)
00971                         curmasked->ymin -= dy*(float)v2d->mask.ymin;
00972                 if (v2d->mask.ymax+1 != v2d->winy)
00973                         curmasked->ymax += dy*(float)(v2d->winy - v2d->mask.ymax-1);
00974                 
00975         }
00976 }
00977 
00978 /* Set view matrices to use 'cur' rect as viewing frame for View2D drawing */
00979 void UI_view2d_view_ortho(View2D *v2d)
00980 {
00981         rctf curmasked;
00982         float xofs, yofs;
00983         
00984         /* pixel offsets (-0.375f) are needed to get 1:1 correspondance with pixels for smooth UI drawing, 
00985          * but only applied where requsted
00986          */
00987         /* XXX brecht: instead of zero at least use a tiny offset, otherwise
00988          * pixel rounding is effectively random due to float inaccuracy */
00989         xofs= 0.001f*(v2d->cur.xmax - v2d->cur.xmin)/(v2d->mask.xmax - v2d->mask.xmin);
00990         yofs= 0.001f*(v2d->cur.ymax - v2d->cur.ymin)/(v2d->mask.ymax - v2d->mask.ymin);
00991         
00992         /* apply mask-based adjustments to cur rect (due to scrollers), to eliminate scaling artifacts */
00993         view2d_map_cur_using_mask(v2d, &curmasked);
00994         
00995         curmasked.xmin-= xofs; curmasked.xmax-=xofs;
00996         curmasked.ymin-= yofs; curmasked.ymax-=yofs;
00997         
00998         /* XXX ton: this flag set by outliner, for icons */
00999         if(v2d->flag & V2D_PIXELOFS_X) {
01000                 curmasked.xmin= floorf(curmasked.xmin) - 0.001f;
01001                 curmasked.xmax= floorf(curmasked.xmax) - 0.001f;
01002         }
01003         if(v2d->flag & V2D_PIXELOFS_Y) {
01004                 curmasked.ymin= floorf(curmasked.ymin) - 0.001f;
01005                 curmasked.ymax= floorf(curmasked.ymax) - 0.001f;
01006         }
01007         
01008         /* set matrix on all appropriate axes */
01009         wmOrtho2(curmasked.xmin-xofs, curmasked.xmax-xofs, curmasked.ymin-yofs, curmasked.ymax-yofs);
01010 
01011         /* XXX is this necessary? */
01012         glLoadIdentity();
01013 }
01014 
01015 /* Set view matrices to only use one axis of 'cur' only
01016  *      - xaxis         = if non-zero, only use cur x-axis, otherwise use cur-yaxis (mostly this will be used for x)
01017  */
01018 void UI_view2d_view_orthoSpecial(ARegion *ar, View2D *v2d, short xaxis)
01019 {
01020         rctf curmasked;
01021         float xofs, yofs;
01022         
01023         /* pixel offsets (-0.375f) are needed to get 1:1 correspondance with pixels for smooth UI drawing, 
01024          * but only applied where requsted
01025          */
01026         /* XXX temp (ton) */
01027         xofs= 0.0f; // (v2d->flag & V2D_PIXELOFS_X) ? 0.375f : 0.0f;
01028         yofs= 0.0f; // (v2d->flag & V2D_PIXELOFS_Y) ? 0.375f : 0.0f;
01029         
01030         /* apply mask-based adjustments to cur rect (due to scrollers), to eliminate scaling artifacts */
01031         view2d_map_cur_using_mask(v2d, &curmasked);
01032         
01033         /* only set matrix with 'cur' coordinates on relevant axes */
01034         if (xaxis)
01035                 wmOrtho2(curmasked.xmin-xofs, curmasked.xmax-xofs, -yofs, ar->winy-yofs);
01036         else
01037                 wmOrtho2(-xofs, ar->winx-xofs, curmasked.ymin-yofs, curmasked.ymax-yofs);
01038                 
01039         /* XXX is this necessary? */
01040         glLoadIdentity();
01041 } 
01042 
01043 
01044 /* Restore view matrices after drawing */
01045 void UI_view2d_view_restore(const bContext *C)
01046 {
01047         ARegion *ar= CTX_wm_region(C);
01048         int width= ar->winrct.xmax-ar->winrct.xmin+1;
01049         int height= ar->winrct.ymax-ar->winrct.ymin+1;
01050         
01051         wmOrtho2(0.0f, (float)width, 0.0f, (float)height);
01052         glLoadIdentity();
01053         
01054         //      ED_region_pixelspace(CTX_wm_region(C));
01055 }
01056 
01057 /* *********************************************************************** */
01058 /* Gridlines */
01059 
01060 /* View2DGrid is typedef'd in UI_view2d.h */
01061 struct View2DGrid {
01062         float dx, dy;                   /* stepsize (in pixels) between gridlines */
01063         float startx, starty;   /* initial coordinates to start drawing grid from */
01064         int powerx, powery;             /* step as power of 10 */
01065 };
01066 
01067 /* --------------- */
01068 
01069 /* try to write step as a power of 10 */
01070 static void step_to_grid(float *step, int *power, int unit)
01071 {
01072         const float loga= (float)log10(*step);
01073         float rem;
01074         
01075         *power= (int)(loga);
01076         
01077         rem= loga - (*power);
01078         rem= (float)pow(10.0, rem);
01079         
01080         if (loga < 0.0f) {
01081                 if (rem < 0.2f) rem= 0.2f;
01082                 else if(rem < 0.5f) rem= 0.5f;
01083                 else rem= 1.0f;
01084                 
01085                 *step= rem * (float)pow(10.0, (*power));
01086                 
01087                 /* for frames, we want 1.0 frame intervals only */
01088                 if (unit == V2D_UNIT_FRAMES) {
01089                         rem = 1.0f;
01090                         *step = 2.0f; /* use 2 since there are grid lines drawn in between, this way to get 1 line per frane */
01091                 }
01092                 
01093                 /* prevents printing 1.0 2.0 3.0 etc */
01094                 if (rem == 1.0f) (*power)++;    
01095         }
01096         else {
01097                 if (rem < 2.0f) rem= 2.0f;
01098                 else if(rem < 5.0f) rem= 5.0f;
01099                 else rem= 10.0f;
01100                 
01101                 *step= rem * (float)pow(10.0, (*power));
01102                 
01103                 (*power)++;
01104                 /* prevents printing 1.0, 2.0, 3.0, etc. */
01105                 if (rem == 10.0f) (*power)++;   
01106         }
01107 }
01108 
01109 /* Intialise settings necessary for drawing gridlines in a 2d-view 
01110  *      - Currently, will return pointer to View2DGrid struct that needs to 
01111  *        be freed with UI_view2d_grid_free()
01112  *      - Is used for scrollbar drawing too (for units drawing)
01113  *      - Units + clamping args will be checked, to make sure they are valid values that can be used
01114  *        so it is very possible that we won't return grid at all!
01115  *      
01116  *      - xunits,yunits = V2D_UNIT_*  grid steps in seconds or frames 
01117  *      - xclamp,yclamp = V2D_CLAMP_* only show whole-number intervals
01118  *      - winx                  = width of region we're drawing to, note: not used but keeping for completeness.
01119  *      - winy                  = height of region we're drawing into
01120  */
01121 View2DGrid *UI_view2d_grid_calc(Scene *scene, View2D *v2d, short xunits, short xclamp, short yunits, short yclamp, int UNUSED(winx), int winy)
01122 {
01123 
01124         View2DGrid *grid;
01125         float space, pixels, seconddiv;
01126         
01127         /* check that there are at least some workable args */
01128         if (ELEM(V2D_ARG_DUMMY, xunits, xclamp) && ELEM(V2D_ARG_DUMMY, yunits, yclamp))
01129                 return NULL;
01130         
01131         /* grid here is allocated... */
01132         grid= MEM_callocN(sizeof(View2DGrid), "View2DGrid");
01133         
01134         /* rule: gridstep is minimal GRIDSTEP pixels */
01135         if (xunits == V2D_UNIT_SECONDS) {
01136                 seconddiv= (float)(0.01 * FPS);
01137         }
01138         else {
01139                 seconddiv= 1.0f;
01140         }
01141         
01142         /* calculate x-axis grid scale (only if both args are valid) */
01143         if (ELEM(V2D_ARG_DUMMY, xunits, xclamp) == 0) {
01144                 space= v2d->cur.xmax - v2d->cur.xmin;
01145                 pixels= (float)(v2d->mask.xmax - v2d->mask.xmin);
01146                 
01147                 if(pixels!=0.0f) {
01148                         grid->dx= (U.v2d_min_gridsize * space) / (seconddiv * pixels);
01149                         step_to_grid(&grid->dx, &grid->powerx, xunits);
01150                         grid->dx *= seconddiv;
01151                 }
01152                 
01153                 if (xclamp == V2D_GRID_CLAMP) {
01154                         if (grid->dx < 0.1f) grid->dx= 0.1f;
01155                         grid->powerx-= 2;
01156                         if (grid->powerx < -2) grid->powerx= -2;
01157                 }
01158         }
01159         
01160         /* calculate y-axis grid scale (only if both args are valid) */
01161         if (ELEM(V2D_ARG_DUMMY, yunits, yclamp) == 0) {
01162                 space= v2d->cur.ymax - v2d->cur.ymin;
01163                 pixels= (float)winy;
01164                 
01165                 grid->dy= U.v2d_min_gridsize * space / pixels;
01166                 step_to_grid(&grid->dy, &grid->powery, yunits);
01167                 
01168                 if (yclamp == V2D_GRID_CLAMP) {
01169                         if (grid->dy < 1.0f) grid->dy= 1.0f;
01170                         if (grid->powery < 1) grid->powery= 1;
01171                 }
01172         }
01173         
01174         /* calculate start position */
01175         if (ELEM(V2D_ARG_DUMMY, xunits, xclamp) == 0) {
01176                 grid->startx= seconddiv*(v2d->cur.xmin/seconddiv - (float)fmod(v2d->cur.xmin/seconddiv, grid->dx/seconddiv));
01177                 if (v2d->cur.xmin < 0.0f) grid->startx-= grid->dx;
01178         }
01179         else
01180                 grid->startx= v2d->cur.xmin;
01181                 
01182         if (ELEM(V2D_ARG_DUMMY, yunits, yclamp) == 0) {
01183                 grid->starty= (v2d->cur.ymin - (float)fmod(v2d->cur.ymin, grid->dy));
01184                 if (v2d->cur.ymin < 0.0f) grid->starty-= grid->dy;
01185         }
01186         else
01187                 grid->starty= v2d->cur.ymin;
01188         
01189         return grid;
01190 }
01191 
01192 /* Draw gridlines in the given 2d-region */
01193 void UI_view2d_grid_draw(View2D *v2d, View2DGrid *grid, int flag)
01194 {
01195         float vec1[2], vec2[2];
01196         int a, step;
01197         
01198         /* check for grid first, as it may not exist */
01199         if (grid == NULL)
01200                 return;
01201         
01202         /* vertical lines */
01203         if (flag & V2D_VERTICAL_LINES) {
01204                 /* initialise initial settings */
01205                 vec1[0]= vec2[0]= grid->startx;
01206                 vec1[1]= grid->starty;
01207                 vec2[1]= v2d->cur.ymax;
01208                 
01209                 /* minor gridlines */
01210                 step= (v2d->mask.xmax - v2d->mask.xmin + 1) / U.v2d_min_gridsize;
01211                 UI_ThemeColor(TH_GRID);
01212                 
01213                 for (a=0; a<step; a++) {
01214                         glBegin(GL_LINE_STRIP);
01215                                 glVertex2fv(vec1); 
01216                                 glVertex2fv(vec2);
01217                         glEnd();
01218                         
01219                         vec2[0]= vec1[0]+= grid->dx;
01220                 }
01221                 
01222                 /* major gridlines */
01223                 vec2[0]= vec1[0]-= 0.5f*grid->dx;
01224                 UI_ThemeColorShade(TH_GRID, 16);
01225                 
01226                 step++;
01227                 for (a=0; a<=step; a++) {
01228                         glBegin(GL_LINE_STRIP);
01229                                 glVertex2fv(vec1); 
01230                                 glVertex2fv(vec2);
01231                         glEnd();
01232                         
01233                         vec2[0]= vec1[0]-= grid->dx;
01234                 }
01235         }
01236         
01237         /* horizontal lines */
01238         if (flag & V2D_HORIZONTAL_LINES) {
01239                 /* only major gridlines */
01240                 vec1[1]= vec2[1]= grid->starty;
01241                 vec1[0]= grid->startx;
01242                 vec2[0]= v2d->cur.xmax;
01243                 
01244                 step= (v2d->mask.ymax - v2d->mask.ymin + 1) / U.v2d_min_gridsize;
01245                 
01246                 UI_ThemeColor(TH_GRID);
01247                 for (a=0; a<=step; a++) {
01248                         glBegin(GL_LINE_STRIP);
01249                                 glVertex2fv(vec1); 
01250                                 glVertex2fv(vec2);
01251                         glEnd();
01252                         
01253                         vec2[1]= vec1[1]+= grid->dy;
01254                 }
01255                 
01256                 /* fine grid lines */
01257                 vec2[1]= vec1[1]-= 0.5f*grid->dy;
01258                 step++;
01259                 
01260                 if (flag & V2D_HORIZONTAL_FINELINES) { 
01261                         UI_ThemeColorShade(TH_GRID, 16);
01262                         for (a=0; a<step; a++) {
01263                                 glBegin(GL_LINE_STRIP);
01264                                         glVertex2fv(vec1); 
01265                                         glVertex2fv(vec2);
01266                                 glEnd();
01267                                 
01268                                 vec2[1]= vec1[1]-= grid->dy;
01269                         }
01270                 }
01271         }
01272         
01273         /* Axes are drawn as darker lines */
01274         UI_ThemeColorShade(TH_GRID, -50);
01275         
01276         /* horizontal axis */
01277         if (flag & V2D_HORIZONTAL_AXIS) {
01278                 vec1[0]= v2d->cur.xmin;
01279                 vec2[0]= v2d->cur.xmax;
01280                 vec1[1]= vec2[1]= 0.0f;
01281                 
01282                 glBegin(GL_LINE_STRIP);
01283                         glVertex2fv(vec1);
01284                         glVertex2fv(vec2);
01285                 glEnd();
01286         }
01287         
01288         /* vertical axis */
01289         if (flag & V2D_VERTICAL_AXIS) {
01290                 vec1[1]= v2d->cur.ymin;
01291                 vec2[1]= v2d->cur.ymax;
01292                 vec1[0]= vec2[0]= 0.0f;
01293                 
01294                 glBegin(GL_LINE_STRIP);
01295                         glVertex2fv(vec1); 
01296                         glVertex2fv(vec2);
01297                 glEnd();
01298         }
01299 }
01300 
01301 /* Draw a constant grid in given 2d-region */
01302 void UI_view2d_constant_grid_draw(View2D *v2d)
01303 {
01304         float start, step= 25.0f;
01305 
01306         UI_ThemeColorShade(TH_BACK, -10);
01307         
01308         start= v2d->cur.xmin - (float)fmod(v2d->cur.xmin, step);
01309         
01310         glBegin(GL_LINES);
01311         for(; start<v2d->cur.xmax; start+=step) {
01312                 glVertex2f(start, v2d->cur.ymin);
01313                 glVertex2f(start, v2d->cur.ymax);
01314         }
01315 
01316         start= v2d->cur.ymin - (float)fmod(v2d->cur.ymin, step);
01317         for(; start<v2d->cur.ymax; start+=step) {
01318                 glVertex2f(v2d->cur.xmin, start);
01319                 glVertex2f(v2d->cur.xmax, start);
01320         }
01321         
01322         /* X and Y axis */
01323         UI_ThemeColorShade(TH_BACK, -18);
01324         glVertex2f(0.0f, v2d->cur.ymin);
01325         glVertex2f(0.0f, v2d->cur.ymax);
01326         glVertex2f(v2d->cur.xmin, 0.0f);
01327         glVertex2f(v2d->cur.xmax, 0.0f);
01328         
01329         glEnd();
01330 }
01331 
01332 /* the price we pay for not exposting structs :( */
01333 void UI_view2d_grid_size(View2DGrid *grid, float *r_dx, float *r_dy)
01334 {
01335         *r_dx= grid->dx;
01336         *r_dy= grid->dy;
01337 }
01338 
01339 /* free temporary memory used for drawing grid */
01340 void UI_view2d_grid_free(View2DGrid *grid)
01341 {
01342         /* only free if there's a grid */
01343         if (grid)
01344                 MEM_freeN(grid);
01345 }
01346 
01347 /* *********************************************************************** */
01348 /* Scrollers */
01349 
01350 /* View2DScrollers is typedef'd in UI_view2d.h 
01351  * WARNING: the start of this struct must not change, as view2d_ops.c uses this too. 
01352  *                 For now, we don't need to have a separate (internal) header for structs like this...
01353  */
01354 struct View2DScrollers {
01355                 /* focus bubbles */
01356         int vert_min, vert_max; /* vertical scrollbar */
01357         int hor_min, hor_max;   /* horizontal scrollbar */
01358         
01359         rcti hor, vert;                 /* exact size of slider backdrop */
01360         int horfull, vertfull;  /* set if sliders are full, we don't draw them */
01361         
01362         /* scales */
01363         View2DGrid *grid;               /* grid for coordinate drawing */
01364         short xunits, xclamp;   /* units and clamping options for x-axis */
01365         short yunits, yclamp;   /* units and clamping options for y-axis */
01366 };
01367 
01368 /* Calculate relevant scroller properties */
01369 View2DScrollers *UI_view2d_scrollers_calc(const bContext *C, View2D *v2d, short xunits, short xclamp, short yunits, short yclamp)
01370 {
01371         View2DScrollers *scrollers;
01372         rcti vert, hor;
01373         float fac1, fac2, totsize, scrollsize;
01374         int scroll= view2d_scroll_mapped(v2d->scroll);
01375         
01376         /* scrollers is allocated here... */
01377         scrollers= MEM_callocN(sizeof(View2DScrollers), "View2DScrollers");
01378         
01379         vert= v2d->vert;
01380         hor= v2d->hor;
01381         
01382         /* slider rects need to be smaller than region */
01383         hor.xmin+=4;
01384         hor.xmax-=4;
01385         if (scroll & V2D_SCROLL_BOTTOM)
01386                 hor.ymin+=4;
01387         else
01388                 hor.ymax-=4;
01389         
01390         if (scroll & V2D_SCROLL_LEFT)
01391                 vert.xmin+=4;
01392         else
01393                 vert.xmax-=4;
01394         vert.ymin+=4;
01395         vert.ymax-=4;
01396         
01397         CLAMP(vert.ymin, vert.ymin, vert.ymax-V2D_SCROLLER_HANDLE_SIZE);
01398         CLAMP(hor.xmin, hor.xmin, hor.xmax-V2D_SCROLLER_HANDLE_SIZE);
01399         
01400         /* store in scrollers, used for drawing */
01401         scrollers->vert= vert;
01402         scrollers->hor= hor;
01403         
01404         /* scroller 'buttons':
01405          *      - These should always remain within the visible region of the scrollbar
01406          *      - They represent the region of 'tot' that is visible in 'cur'
01407          */
01408         
01409         /* horizontal scrollers */
01410         if (scroll & V2D_SCROLL_HORIZONTAL) {
01411                 /* scroller 'button' extents */
01412                 totsize= v2d->tot.xmax - v2d->tot.xmin;
01413                 scrollsize= (float)(hor.xmax - hor.xmin);
01414                 if(totsize==0.0f) totsize = 1.0f; /* avoid divide by zero */
01415                 
01416                 fac1= (v2d->cur.xmin - v2d->tot.xmin) / totsize;
01417                 if(fac1<=0.0f)
01418                         scrollers->hor_min= hor.xmin;
01419                 else
01420                         scrollers->hor_min= (int)(hor.xmin + (fac1 * scrollsize));
01421                 
01422                 fac2= (v2d->cur.xmax - v2d->tot.xmin) / totsize;
01423                 if(fac2>=1.0f)
01424                         scrollers->hor_max= hor.xmax;
01425                 else
01426                         scrollers->hor_max= (int)(hor.xmin + (fac2 * scrollsize));
01427                 
01428                 /* prevent inverted sliders */
01429                 if (scrollers->hor_min > scrollers->hor_max) 
01430                         scrollers->hor_min= scrollers->hor_max;
01431                 /* prevent sliders from being too small, and disappearing */
01432                 if ((scrollers->hor_max - scrollers->hor_min) < V2D_SCROLLER_HANDLE_SIZE) {
01433                         scrollers->hor_max= scrollers->hor_min + V2D_SCROLLER_HANDLE_SIZE;
01434 
01435                         CLAMP(scrollers->hor_max, hor.xmin+V2D_SCROLLER_HANDLE_SIZE, hor.xmax);
01436                         CLAMP(scrollers->hor_min, hor.xmin, hor.xmax-V2D_SCROLLER_HANDLE_SIZE);
01437                 }
01438                 
01439                 /* check whether sliders can disappear due to the full-range being used */
01440                 if(v2d->keeptot) {
01441                         if ((fac1 <= 0.0f) && (fac2 >= 1.0f)) { 
01442                                 v2d->scroll |= V2D_SCROLL_HORIZONTAL_FULLR;
01443                                 scrollers->horfull= 1;
01444                         }
01445                         else    
01446                                 v2d->scroll &= ~V2D_SCROLL_HORIZONTAL_FULLR;
01447                 }
01448         }
01449         
01450         /* vertical scrollers */
01451         if (scroll & V2D_SCROLL_VERTICAL) {
01452                 /* scroller 'button' extents */
01453                 totsize= v2d->tot.ymax - v2d->tot.ymin;
01454                 scrollsize= (float)(vert.ymax - vert.ymin);
01455                 if(totsize==0.0f) totsize = 1.0f; /* avoid divide by zero */
01456                 
01457                 fac1= (v2d->cur.ymin- v2d->tot.ymin) / totsize;
01458                 if(fac1<=0.0f)
01459                         scrollers->vert_min= vert.ymin;
01460                 else
01461                         scrollers->vert_min= (int)(vert.ymin + (fac1 * scrollsize));
01462                 
01463                 fac2= (v2d->cur.ymax - v2d->tot.ymin) / totsize;
01464                 if(fac2>=1.0f)
01465                         scrollers->vert_max= vert.ymax;
01466                 else
01467                         scrollers->vert_max= (int)(vert.ymin + (fac2 * scrollsize));
01468                 
01469                 /* prevent inverted sliders */
01470                 if (scrollers->vert_min > scrollers->vert_max) 
01471                         scrollers->vert_min= scrollers->vert_max;
01472                 /* prevent sliders from being too small, and disappearing */
01473                 if ((scrollers->vert_max - scrollers->vert_min) < V2D_SCROLLER_HANDLE_SIZE) {
01474                         
01475                         scrollers->vert_max= scrollers->vert_min + V2D_SCROLLER_HANDLE_SIZE;
01476                         
01477                         CLAMP(scrollers->vert_max, vert.ymin+V2D_SCROLLER_HANDLE_SIZE, vert.ymax);
01478                         CLAMP(scrollers->vert_min, vert.ymin, vert.ymax-V2D_SCROLLER_HANDLE_SIZE);
01479                 }
01480 
01481                 /* check whether sliders can disappear due to the full-range being used */
01482                 if(v2d->keeptot) {
01483                         if ((fac1 <= 0.0f) && (fac2 >= 1.0f)) { 
01484                                 v2d->scroll |= V2D_SCROLL_VERTICAL_FULLR;
01485                                 scrollers->vertfull= 1;
01486                         }
01487                         else    
01488                                 v2d->scroll &= ~V2D_SCROLL_VERTICAL_FULLR;
01489                 }
01490         }
01491         
01492         /* grid markings on scrollbars */
01493         if (scroll & (V2D_SCROLL_SCALE_HORIZONTAL|V2D_SCROLL_SCALE_VERTICAL)) {
01494                 /* store clamping */
01495                 scrollers->xclamp= xclamp;
01496                 scrollers->xunits= xunits;
01497                 scrollers->yclamp= yclamp;
01498                 scrollers->yunits= yunits;
01499                 
01500                 scrollers->grid= UI_view2d_grid_calc(CTX_data_scene(C), v2d, xunits, xclamp, yunits, yclamp, (hor.xmax - hor.xmin), (vert.ymax - vert.ymin));
01501         }
01502         
01503         /* return scrollers */
01504         return scrollers;
01505 }
01506 
01507 /* Print scale marking along a time scrollbar */
01508 static void scroll_printstr(Scene *scene, float x, float y, float val, int power, short unit, char dir)
01509 {
01510         int len;
01511         char str[32];
01512         
01513         /* adjust the scale unit to work ok */
01514         if (dir == 'v') {
01515                 /* here we bump up the power by factor of 10, as 
01516                  * rotation values (hence 'degrees') are divided by 10 to 
01517                  * be able to show the curves at the same time
01518                  */
01519                 if (ELEM(unit, V2D_UNIT_DEGREES, V2D_UNIT_TIME)) {
01520                         power += 1;
01521                         val *= 10;
01522                 }
01523         }
01524         
01525         /* get string to print */
01526         ANIM_timecode_string_from_frame(str, scene, power, (unit == V2D_UNIT_SECONDS), val);
01527         
01528         /* get length of string, and adjust printing location to fit it into the horizontal scrollbar */
01529         len= strlen(str);
01530         if (dir == 'h') {
01531                 /* seconds/timecode display has slightly longer strings... */
01532                 if (unit == V2D_UNIT_SECONDS)
01533                         x-= 3*len;
01534                 else
01535                         x-= 4*len;
01536         }
01537         
01538         /* Add degree sympbol to end of string for vertical scrollbar? */
01539         if ((dir == 'v') && (unit == V2D_UNIT_DEGREES)) {
01540                 str[len]= 186;
01541                 str[len+1]= 0;
01542         }
01543         
01544         /* draw it */
01545         BLF_draw_default_ascii(x, y, 0.0f, str, sizeof(str)-1);
01546 }
01547 
01548 /* Draw scrollbars in the given 2d-region */
01549 void UI_view2d_scrollers_draw(const bContext *C, View2D *v2d, View2DScrollers *vs)
01550 {
01551         Scene *scene= CTX_data_scene(C);
01552         rcti vert, hor;
01553         int scroll= view2d_scroll_mapped(v2d->scroll);
01554         
01555         /* make copies of rects for less typing */
01556         vert= vs->vert;
01557         hor= vs->hor;
01558         
01559         /* horizontal scrollbar */
01560         if (scroll & V2D_SCROLL_HORIZONTAL) {
01561                 /* only draw scrollbar when it doesn't fill the entire space */
01562                 if(vs->horfull==0) {
01563                         bTheme *btheme= U.themes.first;
01564                         uiWidgetColors wcol= btheme->tui.wcol_scroll;
01565                         rcti slider;
01566                         int state;
01567                         
01568                         slider.xmin= vs->hor_min;
01569                         slider.xmax= vs->hor_max;
01570                         slider.ymin= hor.ymin;
01571                         slider.ymax= hor.ymax;
01572                         
01573                         state= (v2d->scroll_ui & V2D_SCROLL_H_ACTIVE)?UI_SCROLL_PRESSED:0;
01574                         
01575                         /* show zoom handles if:
01576                          *      - zooming on x-axis is allowed (no scroll otherwise)
01577                          *      - slider bubble is large enough (no overdraw confusion)
01578                          *      - scale is shown on the scroller 
01579                          *        (workaround to make sure that button windows don't show these, 
01580                          *              and only the time-grids with their zoomability can do so)
01581                          */
01582                         if ((v2d->keepzoom & V2D_LOCKZOOM_X)==0 && 
01583                                 (v2d->scroll & V2D_SCROLL_SCALE_HORIZONTAL) &&
01584                                 (slider.xmax - slider.xmin > V2D_SCROLLER_HANDLE_SIZE))
01585                         {
01586                                 state |= UI_SCROLL_ARROWS;
01587                         }
01588                         
01589                         UI_ThemeColor(TH_BACK);
01590                         glRecti(v2d->hor.xmin, v2d->hor.ymin, v2d->hor.xmax, v2d->hor.ymax);
01591                         
01592                         uiWidgetScrollDraw(&wcol, &hor, &slider, state);
01593                 }
01594                 
01595                 /* scale indicators */
01596                 if ((scroll & V2D_SCROLL_SCALE_HORIZONTAL) && (vs->grid)) {
01597                         View2DGrid *grid= vs->grid;
01598                         float fac, dfac, fac2, val;
01599                         
01600                         /* the numbers: convert grid->startx and -dx to scroll coordinates 
01601                          *      - fac is x-coordinate to draw to
01602                          *      - dfac is gap between scale markings
01603                          */
01604                         fac= (grid->startx - v2d->cur.xmin) / (v2d->cur.xmax - v2d->cur.xmin);
01605                         fac= (float)hor.xmin + fac*(hor.xmax - hor.xmin);
01606                         
01607                         dfac= (grid->dx) / (v2d->cur.xmax - v2d->cur.xmin);
01608                         dfac= dfac * (hor.xmax - hor.xmin);
01609                         
01610                         /* set starting value, and text color */
01611                         UI_ThemeColor(TH_TEXT);
01612                         val= grid->startx;
01613                         
01614                         /* if we're clamping to whole numbers only, make sure entries won't be repeated */
01615                         if (vs->xclamp == V2D_GRID_CLAMP) {
01616                                 while (grid->dx < 0.9999f) {
01617                                         grid->dx *= 2.0f;
01618                                         dfac *= 2.0f;
01619                                 }
01620                         }
01621                         if (vs->xunits == V2D_UNIT_FRAMES)
01622                                 grid->powerx= 1;
01623                         
01624                         /* draw numbers in the appropriate range */
01625                         if (dfac > 0.0f) {
01626                                 float h= 2.0f+(float)(hor.ymin);
01627                                 
01628                                 for (; fac < hor.xmax-10; fac+=dfac, val+=grid->dx) {
01629                                         
01630                                         /* make prints look nicer for scrollers */
01631                                         if(fac < hor.xmin+10)
01632                                                 continue;
01633                                         
01634                                         switch (vs->xunits) {                                                   
01635                                                 case V2D_UNIT_FRAMES:           /* frames (as whole numbers)*/
01636                                                         scroll_printstr(scene, fac, h, val, grid->powerx, V2D_UNIT_FRAMES, 'h');
01637                                                         break;
01638                                                         
01639                                                 case V2D_UNIT_FRAMESCALE:       /* frames (not always as whole numbers) */
01640                                                         scroll_printstr(scene, fac, h, val, grid->powerx, V2D_UNIT_FRAMESCALE, 'h');
01641                                                         break;
01642                                                 
01643                                                 case V2D_UNIT_SECONDS:          /* seconds */
01644                                                         fac2= val/(float)FPS;
01645                                                         scroll_printstr(scene, fac, h, fac2, grid->powerx, V2D_UNIT_SECONDS, 'h');
01646                                                         break;
01647                                                         
01648                                                 case V2D_UNIT_SECONDSSEQ:       /* seconds with special calculations (only used for sequencer only) */
01649                                                 {
01650                                                         float time;
01651                                                         
01652                                                         fac2= val/(float)FPS;
01653                                                         time= (float)floor(fac2);
01654                                                         fac2= fac2-time;
01655                                                         
01656                                                         scroll_printstr(scene, fac, h, time+(float)FPS*fac2/100.0f, grid->powerx, V2D_UNIT_SECONDSSEQ, 'h');
01657                                                 }
01658                                                         break;
01659                                                         
01660                                                 case V2D_UNIT_DEGREES:          /* Graph Editor for rotation Drivers */
01661                                                         /* HACK: although we're drawing horizontal, we make this draw as 'vertical', just to get degree signs */
01662                                                         scroll_printstr(scene, fac, h, val, grid->powerx, V2D_UNIT_DEGREES, 'v');
01663                                                         break;
01664                                         }
01665                                 }
01666                         }
01667                 }
01668         }
01669         
01670         /* vertical scrollbar */
01671         if (scroll & V2D_SCROLL_VERTICAL) {
01672                 /* only draw scrollbar when it doesn't fill the entire space */
01673                 if(vs->vertfull==0) {
01674                         bTheme *btheme= U.themes.first;
01675                         uiWidgetColors wcol= btheme->tui.wcol_scroll;
01676                         rcti slider;
01677                         int state;
01678                         
01679                         slider.xmin= vert.xmin;
01680                         slider.xmax= vert.xmax;
01681                         slider.ymin= vs->vert_min;
01682                         slider.ymax= vs->vert_max;
01683                         
01684                         state= (v2d->scroll_ui & V2D_SCROLL_V_ACTIVE)?UI_SCROLL_PRESSED:0;
01685                         
01686                         /* show zoom handles if:
01687                          *      - zooming on y-axis is allowed (no scroll otherwise)
01688                          *      - slider bubble is large enough (no overdraw confusion)
01689                          *      - scale is shown on the scroller 
01690                          *        (workaround to make sure that button windows don't show these, 
01691                          *              and only the time-grids with their zoomability can do so)
01692                          */
01693                         if ((v2d->keepzoom & V2D_LOCKZOOM_Y)==0 && 
01694                                 (v2d->scroll & V2D_SCROLL_SCALE_VERTICAL) &&
01695                                 (slider.ymax - slider.ymin > V2D_SCROLLER_HANDLE_SIZE))
01696                         {
01697                                 state |= UI_SCROLL_ARROWS;
01698                         }
01699                                 
01700                         UI_ThemeColor(TH_BACK);
01701                         glRecti(v2d->vert.xmin, v2d->vert.ymin, v2d->vert.xmax, v2d->vert.ymax);
01702                         
01703                         uiWidgetScrollDraw(&wcol, &vert, &slider, state);
01704                 }
01705                 
01706                 
01707                 /* scale indiators */
01708                 if ((scroll & V2D_SCROLL_SCALE_VERTICAL) && (vs->grid)) {
01709                         View2DGrid *grid= vs->grid;
01710                         float fac, dfac, val;
01711                         
01712                         /* the numbers: convert grid->starty and dy to scroll coordinates 
01713                          *      - fac is y-coordinate to draw to
01714                          *      - dfac is gap between scale markings
01715                          *      - these involve a correction for horizontal scrollbar
01716                          *        NOTE: it's assumed that that scrollbar is there if this is involved!
01717                          */
01718                         fac= (grid->starty- v2d->cur.ymin) / (v2d->cur.ymax - v2d->cur.ymin);
01719                         fac= vert.ymin + fac*(vert.ymax - vert.ymin);
01720                         
01721                         dfac= (grid->dy) / (v2d->cur.ymax - v2d->cur.ymin);
01722                         dfac= dfac * (vert.ymax - vert.ymin);
01723                         
01724                         /* set starting value, and text color */
01725                         UI_ThemeColor(TH_TEXT);
01726                         val= grid->starty;
01727                         
01728                         /* if vertical clamping (to whole numbers) is used (i.e. in Sequencer), apply correction */
01729                         if (vs->yclamp == V2D_GRID_CLAMP)
01730                                 fac += 0.5f * dfac;
01731                                 
01732                         /* draw vertical steps */
01733                         if (dfac > 0.0f) {
01734                                 
01735                                 BLF_rotation_default(90.0f);
01736                                 BLF_enable_default(BLF_ROTATION);
01737 
01738                                 for (; fac < vert.ymax-10; fac+= dfac, val += grid->dy) {
01739                                         
01740                                         /* make prints look nicer for scrollers */
01741                                         if(fac < vert.ymin+10)
01742                                                 continue;
01743                                         
01744                                         scroll_printstr(scene, (float)(vert.xmax)-2.0f, fac, val, grid->powery, vs->yunits, 'v');
01745                                 }
01746                                 
01747                                 BLF_disable_default(BLF_ROTATION);
01748                         }
01749                 }       
01750         }
01751         
01752 }
01753 
01754 /* free temporary memory used for drawing scrollers */
01755 void UI_view2d_scrollers_free(View2DScrollers *scrollers)
01756 {
01757         /* need to free grid as well... */
01758         if (scrollers->grid) MEM_freeN(scrollers->grid);
01759         MEM_freeN(scrollers);
01760 }
01761 
01762 /* *********************************************************************** */
01763 /* List View Utilities */
01764 
01765 /* Get the view-coordinates of the nominated cell 
01766  *      - columnwidth, rowheight        = size of each 'cell'
01767  *      - startx, starty                        = coordinates (in 'tot' rect space) that the list starts from
01768  *                                                        This should be (0,0) for most views. However, for those where the starting row was offsetted
01769  *                                                        (like for Animation Editor channel lists, to make the first entry more visible), these will be 
01770  *                                                        the min-coordinates of the first item.
01771  *      - column, row                           = the 2d-corodinates (in 2D-view / 'tot' rect space) the cell exists at
01772  *      - rect                                  = coordinates of the cell (passed as single var instead of 4 separate, as it's more useful this way)
01773  */
01774 void UI_view2d_listview_cell_to_view(View2D *v2d, short columnwidth, short rowheight, float startx, float starty, int column, int row, rctf *rect)
01775 {
01776         /* sanity checks */
01777         if ELEM(NULL, v2d, rect)
01778                 return;
01779         if ((columnwidth <= 0) && (rowheight <= 0)) {
01780                 rect->xmin= rect->xmax= 0.0f;
01781                 rect->ymin= rect->ymax= 0.0f;
01782                 return;
01783         }
01784         
01785         /* x-coordinates */
01786         rect->xmin= startx + (float)(columnwidth * column);
01787         rect->xmax= startx + (float)(columnwidth * (column + 1));
01788         
01789         if ((v2d->align & V2D_ALIGN_NO_POS_X) && !(v2d->align & V2D_ALIGN_NO_NEG_X)) {
01790                 /* simply negate the values for the coordinates if in negative half */
01791                 rect->xmin = -rect->xmin;
01792                 rect->xmax = -rect->xmax;
01793         }
01794         
01795         /* y-coordinates */
01796         rect->ymin= starty + (float)(rowheight * row);
01797         rect->ymax= starty + (float)(rowheight * (row + 1));
01798         
01799         if ((v2d->align & V2D_ALIGN_NO_POS_Y) && !(v2d->align & V2D_ALIGN_NO_NEG_Y)) {
01800                 /* simply negate the values for the coordinates if in negative half */
01801                 rect->ymin = -rect->ymin;
01802                 rect->ymax = -rect->ymax;
01803         }
01804 }
01805 
01806 /* Get the 'cell' (row, column) that the given 2D-view coordinates (i.e. in 'tot' rect space) lie in.
01807  *      - columnwidth, rowheight        = size of each 'cell'
01808  *      - startx, starty                        = coordinates (in 'tot' rect space) that the list starts from
01809  *                                                        This should be (0,0) for most views. However, for those where the starting row was offsetted
01810  *                                                        (like for Animation Editor channel lists, to make the first entry more visible), these will be 
01811  *                                                        the min-coordinates of the first item.
01812  *      - viewx, viewy                  = 2D-coordinates (in 2D-view / 'tot' rect space) to get the cell for
01813  *      - column, row                           = the 'coordinates' of the relevant 'cell'
01814  */
01815 void UI_view2d_listview_view_to_cell(View2D *v2d, short columnwidth, short rowheight, float startx, float starty, 
01816                                                 float viewx, float viewy, int *column, int *row)
01817 {
01818         /* adjust view coordinates to be all positive ints, corrected for the start offset */
01819         const int x= (int)(floorf(fabsf(viewx) + 0.5f) - startx);
01820         const int y= (int)(floorf(fabsf(viewy) + 0.5f) - starty);
01821         
01822         /* sizes must not be negative */
01823         if ( (v2d == NULL) || ((columnwidth <= 0) && (rowheight <= 0)) ) {
01824                 if (column) *column= 0;
01825                 if (row) *row= 0;
01826                 
01827                 return;
01828         }
01829         
01830         /* get column */
01831         if ((column) && (columnwidth > 0))
01832                 *column= x / columnwidth;
01833         else if (column)
01834                 *column= 0;
01835         
01836         /* get row */
01837         if ((row) && (rowheight > 0))
01838                 *row= y / rowheight;
01839         else if (row)
01840                 *row= 0;
01841 }
01842 
01843 /* Get the 'extreme' (min/max) column and row indices which are visible within the 'cur' rect 
01844  *      - columnwidth, rowheight        = size of each 'cell'
01845  *      - startx, starty                        = coordinates that the list starts from, which should be (0,0) for most views
01846  *      - column/row_min/max            = the starting and ending column/row indices
01847  */
01848 void UI_view2d_listview_visible_cells(View2D *v2d, short columnwidth, short rowheight, float startx, float starty, 
01849                                                 int *column_min, int *column_max, int *row_min, int *row_max)
01850 {
01851         /* using 'cur' rect coordinates, call the cell-getting function to get the cells for this */
01852         if (v2d) {
01853                 /* min */
01854                 UI_view2d_listview_view_to_cell(v2d, columnwidth, rowheight, startx, starty, 
01855                                         v2d->cur.xmin, v2d->cur.ymin, column_min, row_min);
01856                                         
01857                 /* max*/
01858                 UI_view2d_listview_view_to_cell(v2d, columnwidth, rowheight, startx, starty, 
01859                                         v2d->cur.xmax, v2d->cur.ymax, column_max, row_max);
01860         }
01861 }
01862 
01863 /* *********************************************************************** */
01864 /* Coordinate Conversions */
01865 
01866 /* Convert from screen/region space to 2d-View space 
01867  *      
01868  *      - x,y                   = coordinates to convert
01869  *      - viewx,viewy           = resultant coordinates
01870  */
01871 void UI_view2d_region_to_view(View2D *v2d, int x, int y, float *viewx, float *viewy)
01872 {
01873         float div, ofs;
01874 
01875         if (viewx) {
01876                 div= (float)(v2d->mask.xmax - v2d->mask.xmin);
01877                 ofs= (float)v2d->mask.xmin;
01878                 
01879                 *viewx= v2d->cur.xmin + (v2d->cur.xmax-v2d->cur.xmin) * ((float)x - ofs) / div;
01880         }
01881 
01882         if (viewy) {
01883                 div= (float)(v2d->mask.ymax - v2d->mask.ymin);
01884                 ofs= (float)v2d->mask.ymin;
01885                 
01886                 *viewy= v2d->cur.ymin + (v2d->cur.ymax - v2d->cur.ymin) * ((float)y - ofs) / div;
01887         }
01888 }
01889 
01890 /* Convert from 2d-View space to screen/region space
01891  *      - Coordinates are clamped to lie within bounds of region
01892  *
01893  *      - x,y                           = coordinates to convert
01894  *      - regionx,regiony       = resultant coordinates 
01895  */
01896 void UI_view2d_view_to_region(View2D *v2d, float x, float y, int *regionx, int *regiony)
01897 {
01898         /* set initial value in case coordinate lies outside of bounds */
01899         if (regionx)
01900                 *regionx= V2D_IS_CLIPPED;
01901         if (regiony)
01902                 *regiony= V2D_IS_CLIPPED;
01903         
01904         /* express given coordinates as proportional values */
01905         x= (x - v2d->cur.xmin) / (v2d->cur.xmax - v2d->cur.xmin);
01906         y= (y - v2d->cur.ymin) / (v2d->cur.ymax - v2d->cur.ymin);
01907         
01908         /* check if values are within bounds */
01909         if ((x>=0.0f) && (x<=1.0f) && (y>=0.0f) && (y<=1.0f)) {
01910                 if (regionx)
01911                         *regionx= (int)(v2d->mask.xmin + x*(v2d->mask.xmax-v2d->mask.xmin));
01912                 if (regiony)
01913                         *regiony= (int)(v2d->mask.ymin + y*(v2d->mask.ymax-v2d->mask.ymin));
01914         }
01915 }
01916 
01917 /* Convert from 2d-view space to screen/region space
01918  *      - Coordinates are NOT clamped to lie within bounds of region
01919  *
01920  *      - x,y                           = coordinates to convert
01921  *      - regionx,regiony       = resultant coordinates 
01922  */
01923 void UI_view2d_to_region_no_clip(View2D *v2d, float x, float y, int *regionx, int *regiony)
01924 {
01925         /* step 1: express given coordinates as proportional values */
01926         x= (x - v2d->cur.xmin) / (v2d->cur.xmax - v2d->cur.xmin);
01927         y= (y - v2d->cur.ymin) / (v2d->cur.ymax - v2d->cur.ymin);
01928         
01929         /* step 2: convert proportional distances to screen coordinates  */
01930         x= v2d->mask.xmin + x*(v2d->mask.xmax - v2d->mask.xmin);
01931         y= v2d->mask.ymin + y*(v2d->mask.ymax - v2d->mask.ymin);
01932         
01933         /* although we don't clamp to lie within region bounds, we must avoid exceeding size of ints */
01934         if (regionx) {
01935                 if (x < INT_MIN) *regionx= INT_MIN;
01936                 else if(x > INT_MAX) *regionx= INT_MAX;
01937                 else *regionx= (int)x;
01938         }
01939         if (regiony) {
01940                 if (y < INT_MIN) *regiony= INT_MIN;
01941                 else if(y > INT_MAX) *regiony= INT_MAX;
01942                 else *regiony= (int)y;
01943         }
01944 }
01945 
01946 /* *********************************************************************** */
01947 /* Utilities */
01948 
01949 /* View2D data by default resides in region, so get from region stored in context */
01950 View2D *UI_view2d_fromcontext(const bContext *C)
01951 {
01952         ScrArea *area= CTX_wm_area(C);
01953         ARegion *region= CTX_wm_region(C);
01954 
01955         if (area == NULL) return NULL;
01956         if (region == NULL) return NULL;
01957         return &(region->v2d);
01958 }
01959 
01960 /* same as above, but it returns regionwindow. Utility for pulldowns or buttons */
01961 View2D *UI_view2d_fromcontext_rwin(const bContext *C)
01962 {
01963         ScrArea *sa= CTX_wm_area(C);
01964         ARegion *region= CTX_wm_region(C);
01965 
01966         if (sa == NULL) return NULL;
01967         if (region == NULL) return NULL;
01968         if (region->regiontype!=RGN_TYPE_WINDOW) {
01969                 ARegion *ar= BKE_area_find_region_type(sa, RGN_TYPE_WINDOW);
01970                 return ar ? &(ar->v2d) : NULL;
01971         }
01972         return &(region->v2d);
01973 }
01974 
01975 
01976 /* Calculate the scale per-axis of the drawing-area
01977  *      - Is used to inverse correct drawing of icons, etc. that need to follow view 
01978  *        but not be affected by scale
01979  *
01980  *      - x,y   = scale on each axis
01981  */
01982 void UI_view2d_getscale(View2D *v2d, float *x, float *y) 
01983 {
01984         if (x) *x = (v2d->mask.xmax - v2d->mask.xmin) / (v2d->cur.xmax - v2d->cur.xmin);
01985         if (y) *y = (v2d->mask.ymax - v2d->mask.ymin) / (v2d->cur.ymax - v2d->cur.ymin);
01986 }
01987 
01988 /* Check if mouse is within scrollers
01989  *      - Returns appropriate code for match
01990  *              'h' = in horizontal scroller
01991  *              'v' = in vertical scroller
01992  *              0 = not in scroller
01993  *      
01994  *      - x,y   = mouse coordinates in screen (not region) space
01995  */
01996 short UI_view2d_mouse_in_scrollers (const bContext *C, View2D *v2d, int x, int y)
01997 {
01998         ARegion *ar= CTX_wm_region(C);
01999         int co[2];
02000         int scroll= view2d_scroll_mapped(v2d->scroll);
02001         
02002         /* clamp x,y to region-coordinates first */
02003         co[0]= x - ar->winrct.xmin;
02004         co[1]= y - ar->winrct.ymin;
02005         
02006         /* check if within scrollbars */
02007         if (scroll & V2D_SCROLL_HORIZONTAL) {
02008                 if (IN_2D_HORIZ_SCROLL(v2d, co)) return 'h';
02009         }
02010         if (scroll & V2D_SCROLL_VERTICAL) {
02011                 if (IN_2D_VERT_SCROLL(v2d, co)) return 'v';
02012         }       
02013         
02014         /* not found */
02015         return 0;
02016 }
02017 
02018 /* ******************* view2d text drawing cache ******************** */
02019 
02020 /* assumes caches are used correctly, so for time being no local storage in v2d */
02021 static ListBase strings= {NULL, NULL};
02022 
02023 typedef struct View2DString {
02024         struct View2DString *next, *prev;
02025         union {
02026                 unsigned char ub[4];
02027                 int pack;
02028         } col;
02029         int mval[2];
02030         rcti rect;
02031 } View2DString;
02032 
02033 
02034 void UI_view2d_text_cache_add(View2D *v2d, float x, float y, const char *str, const char col[4])
02035 {
02036         int mval[2];
02037         
02038         UI_view2d_view_to_region(v2d, x, y, mval, mval+1);
02039         
02040         if(mval[0]!=V2D_IS_CLIPPED && mval[1]!=V2D_IS_CLIPPED) {
02041                 int len= strlen(str)+1;
02042                 /* use calloc, rect has to be zeroe'd */
02043                 View2DString *v2s= MEM_callocN(sizeof(View2DString)+len, "View2DString");
02044                 char *v2s_str= (char *)(v2s+1);
02045                 memcpy(v2s_str, str, len);
02046 
02047                 BLI_addtail(&strings, v2s);
02048                 v2s->col.pack= *((int *)col);
02049                 v2s->mval[0]= mval[0];
02050                 v2s->mval[1]= mval[1];
02051         }
02052 }
02053 
02054 /* no clip (yet) */
02055 void UI_view2d_text_cache_rectf(View2D *v2d, rctf *rect, const char *str, const char col[4])
02056 {
02057         int len= strlen(str)+1;
02058         View2DString *v2s= MEM_callocN(sizeof(View2DString)+len, "View2DString");
02059         char *v2s_str= (char *)(v2s+1);
02060         memcpy(v2s_str, str, len);
02061 
02062         UI_view2d_to_region_no_clip(v2d, rect->xmin, rect->ymin, &v2s->rect.xmin, &v2s->rect.ymin);
02063         UI_view2d_to_region_no_clip(v2d, rect->xmax, rect->ymax, &v2s->rect.xmax, &v2s->rect.ymax);
02064 
02065         v2s->col.pack= *((int *)col);
02066         v2s->mval[0]= v2s->rect.xmin;
02067         v2s->mval[1]= v2s->rect.ymin;
02068 
02069         BLI_addtail(&strings, v2s);
02070 }
02071 
02072 
02073 void UI_view2d_text_cache_draw(ARegion *ar)
02074 {
02075         View2DString *v2s;
02076         int col_pack_prev= 0;
02077         
02078         // glMatrixMode(GL_PROJECTION);
02079         // glPushMatrix();
02080         // glMatrixMode(GL_MODELVIEW);
02081         // glPushMatrix();
02082         ED_region_pixelspace(ar);
02083         
02084         for(v2s= strings.first; v2s; v2s= v2s->next) {
02085                 const char *str= (const char *)(v2s+1);
02086                 int xofs=0, yofs;
02087 
02088                 yofs= ceil( 0.5f*(v2s->rect.ymax - v2s->rect.ymin - BLF_height_default("28")));
02089                 if(yofs<1) yofs= 1;
02090 
02091                 if(col_pack_prev != v2s->col.pack) {
02092                         glColor3ubv(v2s->col.ub);
02093                         col_pack_prev= v2s->col.pack;
02094                 }
02095 
02096                 if(v2s->rect.xmin >= v2s->rect.xmax)
02097                         BLF_draw_default((float)v2s->mval[0]+xofs, (float)v2s->mval[1]+yofs, 0.0, str, 65535);
02098                 else {
02099                         BLF_clipping_default(v2s->rect.xmin-4, v2s->rect.ymin-4, v2s->rect.xmax+4, v2s->rect.ymax+4);
02100                         BLF_enable_default(BLF_CLIPPING);
02101                         BLF_draw_default(v2s->rect.xmin+xofs, v2s->rect.ymin+yofs, 0.0f, str, 65535);
02102                         BLF_disable_default(BLF_CLIPPING);
02103                 }
02104         }
02105         
02106         // glMatrixMode(GL_PROJECTION);
02107         // glPopMatrix();
02108         // glMatrixMode(GL_MODELVIEW);
02109         // glPopMatrix();
02110         
02111         if(strings.first) 
02112                 BLI_freelistN(&strings);
02113 }
02114 
02115 
02116 /* ******************************************************** */
02117 
02118