move bmesh painting code into its own function, trunk has vpaint_paint_face(), this...
[blender-staging.git] / source / blender / editors / interface / view2d_ops.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version. 
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2008 Blender Foundation.
19  * All rights reserved.
20  * 
21  * Contributor(s): Blender Foundation, Joshua Leung
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 /** \file blender/editors/interface/view2d_ops.c
27  *  \ingroup edinterface
28  */
29
30
31 #include <math.h>
32
33 #include "MEM_guardedalloc.h"
34
35 #include "DNA_userdef_types.h"
36
37 #include "BLI_blenlib.h"
38 #include "BLI_utildefines.h"
39
40 #include "BKE_context.h"
41
42 #include "RNA_access.h"
43 #include "RNA_define.h"
44
45 #include "WM_api.h"
46 #include "WM_types.h"
47
48
49 #include "ED_screen.h"
50
51 #include "UI_view2d.h"
52 #include "UI_interface.h"
53
54 #include "PIL_time.h" /* USER_ZOOM_CONT */
55
56 static int view2d_poll(bContext *C)
57 {
58         ARegion *ar= CTX_wm_region(C);
59
60         return (ar != NULL) && (ar->v2d.flag & V2D_IS_INITIALISED);
61 }
62
63 /* ********************************************************* */
64 /* VIEW PANNING OPERATOR                                                                 */
65
66 /*      This group of operators come in several forms:
67  *              1) Modal 'dragging' with MMB - where movement of mouse dictates amount to pan view by
68  *              2) Scrollwheel 'steps' - rolling mousewheel by one step moves view by predefined amount
69  *
70  *      In order to make sure this works, each operator must define the following RNA-Operator Props:
71  *              deltax, deltay  - define how much to move view by (relative to zoom-correction factor)
72  */
73
74 /* ------------------ Shared 'core' stuff ---------------------- */
75  
76 /* temp customdata for operator */
77 typedef struct v2dViewPanData {
78         bScreen *sc;                    /* screen where view pan was initiated */
79         ScrArea *sa;                    /* area where view pan was initiated */
80         ARegion *ar;                    /* region where view pan was initiated */
81         View2D *v2d;                    /* view2d we're operating in */
82         
83         float facx, facy;               /* amount to move view relative to zoom */
84         
85                 /* options for version 1 */
86         int startx, starty;             /* mouse x/y values in window when operator was initiated */
87         int lastx, lasty;               /* previous x/y values of mouse in window */
88         int invoke_event;               /* event starting pan, for modal exit */
89         
90         short in_scroller;              /* for MMB in scrollers (old feature in past, but now not that useful) */
91 } v2dViewPanData;
92  
93 /* initialise panning customdata */
94 static int view_pan_init(bContext *C, wmOperator *op)
95 {
96         ARegion *ar= CTX_wm_region(C);
97         v2dViewPanData *vpd;
98         View2D *v2d;
99         float winx, winy;
100         
101         /* regions now have v2d-data by default, so check for region */
102         if (ar == NULL)
103                 return 0;
104                 
105         /* check if panning is allowed at all */
106         v2d= &ar->v2d;
107         if ((v2d->keepofs & V2D_LOCKOFS_X) && (v2d->keepofs & V2D_LOCKOFS_Y))
108                 return 0;
109         
110         /* set custom-data for operator */
111         vpd= MEM_callocN(sizeof(v2dViewPanData), "v2dViewPanData");
112         op->customdata= vpd;
113         
114         /* set pointers to owners */
115         vpd->sc= CTX_wm_screen(C);
116         vpd->sa= CTX_wm_area(C);
117         vpd->v2d= v2d;
118         vpd->ar = ar;
119         
120         /* calculate translation factor - based on size of view */
121         winx= (float)(ar->winrct.xmax - ar->winrct.xmin + 1);
122         winy= (float)(ar->winrct.ymax - ar->winrct.ymin + 1);
123         vpd->facx= (v2d->cur.xmax - v2d->cur.xmin) / winx;
124         vpd->facy= (v2d->cur.ymax - v2d->cur.ymin) / winy;
125         
126         return 1;
127 }
128
129 /* apply transform to view (i.e. adjust 'cur' rect) */
130 static void view_pan_apply(wmOperator *op)
131 {
132         v2dViewPanData *vpd= op->customdata;
133         View2D *v2d= vpd->v2d;
134         float dx, dy;
135         
136         /* calculate amount to move view by */
137         dx= vpd->facx * (float)RNA_int_get(op->ptr, "deltax");
138         dy= vpd->facy * (float)RNA_int_get(op->ptr, "deltay");
139         
140         /* only move view on an axis if change is allowed */
141         if ((v2d->keepofs & V2D_LOCKOFS_X)==0) {
142                 v2d->cur.xmin += dx;
143                 v2d->cur.xmax += dx;
144         }
145         if ((v2d->keepofs & V2D_LOCKOFS_Y)==0) {
146                 v2d->cur.ymin += dy;
147                 v2d->cur.ymax += dy;
148         }
149         
150         /* validate that view is in valid configuration after this operation */
151         UI_view2d_curRect_validate(v2d);
152         
153         /* request updates to be done... */
154         ED_region_tag_redraw(vpd->ar);
155         
156         UI_view2d_sync(vpd->sc, vpd->sa, v2d, V2D_LOCK_COPY);
157         
158         /* exceptions */
159         if (vpd->sa->spacetype==SPACE_OUTLINER) {
160                 /* don't rebuild full tree, since we're just changing our view */
161                 SpaceOops *soops= vpd->sa->spacedata.first;
162                 soops->storeflag |= SO_TREESTORE_REDRAW;
163         }
164 }
165
166 /* cleanup temp customdata  */
167 static void view_pan_exit(wmOperator *op)
168 {
169         if (op->customdata) {
170                 MEM_freeN(op->customdata);
171                 op->customdata= NULL;                           
172         }
173
174  
175 /* ------------------ Modal Drag Version (1) ---------------------- */
176
177 /* for 'redo' only, with no user input */
178 static int view_pan_exec(bContext *C, wmOperator *op)
179 {
180         if (!view_pan_init(C, op))
181                 return OPERATOR_CANCELLED;
182         
183         view_pan_apply(op);
184         view_pan_exit(op);
185         return OPERATOR_FINISHED;
186 }
187
188 /* set up modal operator and relevant settings */
189 static int view_pan_invoke(bContext *C, wmOperator *op, wmEvent *event)
190 {
191         wmWindow *window= CTX_wm_window(C);
192         v2dViewPanData *vpd;
193         View2D *v2d;
194         
195         /* set up customdata */
196         if (!view_pan_init(C, op))
197                 return OPERATOR_PASS_THROUGH;
198         
199         vpd= op->customdata;
200         v2d= vpd->v2d;
201         
202         /* set initial settings */
203         vpd->startx= vpd->lastx= event->x;
204         vpd->starty= vpd->lasty= event->y;
205         vpd->invoke_event= event->type;
206         
207         if (event->type == MOUSEPAN) {
208                 RNA_int_set(op->ptr, "deltax", event->prevx - event->x);
209                 RNA_int_set(op->ptr, "deltay", event->prevy - event->y);
210                 
211                 view_pan_apply(op);
212                 view_pan_exit(op);
213                 return OPERATOR_FINISHED;
214         }
215         
216         RNA_int_set(op->ptr, "deltax", 0);
217         RNA_int_set(op->ptr, "deltay", 0);
218         
219         if (v2d->keepofs & V2D_LOCKOFS_X)
220                 WM_cursor_modal(window, BC_NS_SCROLLCURSOR);
221         else if (v2d->keepofs & V2D_LOCKOFS_Y)
222                 WM_cursor_modal(window, BC_EW_SCROLLCURSOR);
223         else
224                 WM_cursor_modal(window, BC_NSEW_SCROLLCURSOR);
225         
226         /* add temp handler */
227         WM_event_add_modal_handler(C, op);
228
229         return OPERATOR_RUNNING_MODAL;
230 }
231
232 /* handle user input - calculations of mouse-movement need to be done here, not in the apply callback! */
233 static int view_pan_modal(bContext *C, wmOperator *op, wmEvent *event)
234 {
235         v2dViewPanData *vpd= op->customdata;
236         
237         /* execute the events */
238         switch (event->type) {
239                 case MOUSEMOVE:
240                 {
241                         /* calculate new delta transform, then store mouse-coordinates for next-time */
242                         RNA_int_set(op->ptr, "deltax", (vpd->lastx - event->x));
243                         RNA_int_set(op->ptr, "deltay", (vpd->lasty - event->y));
244                         
245                         vpd->lastx= event->x;
246                         vpd->lasty= event->y;
247                         
248                         view_pan_apply(op);
249                 }
250                         break;
251                 /* XXX - Mode switching isn't implemented. See comments in 36818.
252                  * switch to zoom *
253                 case LEFTMOUSE:
254                         if (event->val==KM_PRESS) {
255                                 * calculate overall delta mouse-movement for redo *
256                                 RNA_int_set(op->ptr, "deltax", (vpd->startx - vpd->lastx));
257                                 RNA_int_set(op->ptr, "deltay", (vpd->starty - vpd->lasty));
258                                 
259                                 view_pan_exit(op);
260                                 WM_cursor_restore(CTX_wm_window(C));
261                                 WM_operator_name_call(C, "VIEW2D_OT_zoom", WM_OP_INVOKE_DEFAULT, NULL);
262                                 return OPERATOR_FINISHED;
263                         }*/
264                         
265                 default:
266                         if (event->type == vpd->invoke_event || event->type==ESCKEY) {
267                                 if (event->val==KM_RELEASE) {
268                                         /* calculate overall delta mouse-movement for redo */
269                                         RNA_int_set(op->ptr, "deltax", (vpd->startx - vpd->lastx));
270                                         RNA_int_set(op->ptr, "deltay", (vpd->starty - vpd->lasty));
271                                         
272                                         view_pan_exit(op);
273                                         WM_cursor_restore(CTX_wm_window(C));
274                                         
275                                         return OPERATOR_FINISHED;
276                                 }
277                         }
278                         break;
279         }
280
281         return OPERATOR_RUNNING_MODAL;
282 }
283
284 static int view_pan_cancel(bContext *UNUSED(C), wmOperator *op)
285 {
286         view_pan_exit(op);
287         return OPERATOR_CANCELLED;
288 }
289
290 static void VIEW2D_OT_pan(wmOperatorType *ot)
291 {
292         /* identifiers */
293         ot->name= "Pan View";
294         ot->description= "Pan the view";
295         ot->idname= "VIEW2D_OT_pan";
296         
297         /* api callbacks */
298         ot->exec= view_pan_exec;
299         ot->invoke= view_pan_invoke;
300         ot->modal= view_pan_modal;
301         ot->cancel= view_pan_cancel;
302         
303         /* operator is modal */
304         ot->flag= OPTYPE_BLOCKING|OPTYPE_GRAB_POINTER;
305         
306         /* rna - must keep these in sync with the other operators */
307         RNA_def_int(ot->srna, "deltax", 0, INT_MIN, INT_MAX, "Delta X", "", INT_MIN, INT_MAX);
308         RNA_def_int(ot->srna, "deltay", 0, INT_MIN, INT_MAX, "Delta Y", "", INT_MIN, INT_MAX);
309 }
310
311 /* ------------------ Scrollwheel Versions (2) ---------------------- */
312
313 /* this operator only needs this single callback, where it callsthe view_pan_*() methods */
314 static int view_scrollright_exec(bContext *C, wmOperator *op)
315 {
316         v2dViewPanData *vpd;
317         
318         /* initialise default settings (and validate if ok to run) */
319         if (!view_pan_init(C, op))
320                 return OPERATOR_PASS_THROUGH;
321                 
322         /* also, check if can pan in horizontal axis */
323         vpd= op->customdata;
324         if (vpd->v2d->keepofs & V2D_LOCKOFS_X) {
325                 view_pan_exit(op);
326                 return OPERATOR_PASS_THROUGH;
327         }
328         
329         /* set RNA-Props - only movement in positive x-direction */
330         RNA_int_set(op->ptr, "deltax", 20);
331         RNA_int_set(op->ptr, "deltay", 0);
332         
333         /* apply movement, then we're done */
334         view_pan_apply(op);
335         view_pan_exit(op);
336         
337         return OPERATOR_FINISHED;
338 }
339
340 static void VIEW2D_OT_scroll_right(wmOperatorType *ot)
341 {
342         /* identifiers */
343         ot->name= "Scroll Right";
344         ot->description= "Scroll the view right";
345         ot->idname= "VIEW2D_OT_scroll_right";
346         
347         /* api callbacks */
348         ot->exec= view_scrollright_exec;
349         
350         /* rna - must keep these in sync with the other operators */
351         RNA_def_int(ot->srna, "deltax", 0, INT_MIN, INT_MAX, "Delta X", "", INT_MIN, INT_MAX);
352         RNA_def_int(ot->srna, "deltay", 0, INT_MIN, INT_MAX, "Delta Y", "", INT_MIN, INT_MAX);
353 }
354
355
356
357 /* this operator only needs this single callback, where it callsthe view_pan_*() methods */
358 static int view_scrollleft_exec(bContext *C, wmOperator *op)
359 {
360         v2dViewPanData *vpd;
361         
362         /* initialise default settings (and validate if ok to run) */
363         if (!view_pan_init(C, op))
364                 return OPERATOR_PASS_THROUGH;
365                 
366         /* also, check if can pan in horizontal axis */
367         vpd= op->customdata;
368         if (vpd->v2d->keepofs & V2D_LOCKOFS_X) {
369                 view_pan_exit(op);
370                 return OPERATOR_PASS_THROUGH;
371         }
372         
373         /* set RNA-Props - only movement in negative x-direction */
374         RNA_int_set(op->ptr, "deltax", -20);
375         RNA_int_set(op->ptr, "deltay", 0);
376         
377         /* apply movement, then we're done */
378         view_pan_apply(op);
379         view_pan_exit(op);
380         
381         return OPERATOR_FINISHED;
382 }
383
384 static void VIEW2D_OT_scroll_left(wmOperatorType *ot)
385 {
386         /* identifiers */
387         ot->name= "Scroll Left";
388         ot->description= "Scroll the view left";
389         ot->idname= "VIEW2D_OT_scroll_left";
390         
391         /* api callbacks */
392         ot->exec= view_scrollleft_exec;
393         
394         /* rna - must keep these in sync with the other operators */
395         RNA_def_int(ot->srna, "deltax", 0, INT_MIN, INT_MAX, "Delta X", "", INT_MIN, INT_MAX);
396         RNA_def_int(ot->srna, "deltay", 0, INT_MIN, INT_MAX, "Delta Y", "", INT_MIN, INT_MAX);
397 }
398
399
400 /* this operator only needs this single callback, where it callsthe view_pan_*() methods */
401 static int view_scrolldown_exec(bContext *C, wmOperator *op)
402 {
403         v2dViewPanData *vpd;
404         
405         /* initialise default settings (and validate if ok to run) */
406         if (!view_pan_init(C, op))
407                 return OPERATOR_PASS_THROUGH;
408                 
409         /* also, check if can pan in vertical axis */
410         vpd= op->customdata;
411         if (vpd->v2d->keepofs & V2D_LOCKOFS_Y) {
412                 view_pan_exit(op);
413                 return OPERATOR_PASS_THROUGH;
414         }
415         
416         /* set RNA-Props */
417         RNA_int_set(op->ptr, "deltax", 0);
418         RNA_int_set(op->ptr, "deltay", -40);
419         
420         if(RNA_boolean_get(op->ptr, "page")) {
421                 ARegion *ar= CTX_wm_region(C);
422                 RNA_int_set(op->ptr, "deltay", ar->v2d.mask.ymin - ar->v2d.mask.ymax);
423         }
424         
425         /* apply movement, then we're done */
426         view_pan_apply(op);
427         view_pan_exit(op);
428         
429         return OPERATOR_FINISHED;
430 }
431
432 static void VIEW2D_OT_scroll_down(wmOperatorType *ot)
433 {
434         /* identifiers */
435         ot->name= "Scroll Down";
436         ot->description= "Scroll the view down";
437         ot->idname= "VIEW2D_OT_scroll_down";
438         
439         /* api callbacks */
440         ot->exec= view_scrolldown_exec;
441         
442         /* rna - must keep these in sync with the other operators */
443         RNA_def_int(ot->srna, "deltax", 0, INT_MIN, INT_MAX, "Delta X", "", INT_MIN, INT_MAX);
444         RNA_def_int(ot->srna, "deltay", 0, INT_MIN, INT_MAX, "Delta Y", "", INT_MIN, INT_MAX);
445         RNA_def_boolean(ot->srna, "page", 0, "Page", "Scroll down one page");
446 }
447
448
449
450 /* this operator only needs this single callback, where it callsthe view_pan_*() methods */
451 static int view_scrollup_exec(bContext *C, wmOperator *op)
452 {
453         v2dViewPanData *vpd;
454         
455         /* initialise default settings (and validate if ok to run) */
456         if (!view_pan_init(C, op))
457                 return OPERATOR_PASS_THROUGH;
458                 
459         /* also, check if can pan in vertical axis */
460         vpd= op->customdata;
461         if (vpd->v2d->keepofs & V2D_LOCKOFS_Y) {
462                 view_pan_exit(op);
463                 return OPERATOR_PASS_THROUGH;
464         }
465         
466         /* set RNA-Props */
467         RNA_int_set(op->ptr, "deltax", 0);
468         RNA_int_set(op->ptr, "deltay", 40);
469         
470         if(RNA_boolean_get(op->ptr, "page")) {
471                 ARegion *ar= CTX_wm_region(C);
472                 RNA_int_set(op->ptr, "deltay", ar->v2d.mask.ymax - ar->v2d.mask.ymin);
473         }
474         
475         /* apply movement, then we're done */
476         view_pan_apply(op);
477         view_pan_exit(op);
478         
479         return OPERATOR_FINISHED;
480 }
481
482 static void VIEW2D_OT_scroll_up(wmOperatorType *ot)
483 {
484         /* identifiers */
485         ot->name= "Scroll Up";
486         ot->description= "Scroll the view up";
487         ot->idname= "VIEW2D_OT_scroll_up";
488         
489         /* api callbacks */
490         ot->exec= view_scrollup_exec;
491         
492         /* rna - must keep these in sync with the other operators */
493         RNA_def_int(ot->srna, "deltax", 0, INT_MIN, INT_MAX, "Delta X", "", INT_MIN, INT_MAX);
494         RNA_def_int(ot->srna, "deltay", 0, INT_MIN, INT_MAX, "Delta Y", "", INT_MIN, INT_MAX);
495         RNA_def_boolean(ot->srna, "page", 0, "Page", "Scroll up one page");
496 }
497
498 /* ********************************************************* */
499 /* SINGLE-STEP VIEW ZOOMING OPERATOR                                             */
500
501 /*      This group of operators come in several forms:
502  *              1) Scrollwheel 'steps' - rolling mousewheel by one step zooms view by predefined amount
503  *              2) Scrollwheel 'steps' + alt + ctrl/shift - zooms view on one axis only (ctrl=x, shift=y)  // XXX this could be implemented...
504  *              3) Pad +/- Keys - pressing each key moves the zooms the view by a predefined amount
505  *
506  *      In order to make sure this works, each operator must define the following RNA-Operator Props:
507  *              zoomfacx, zoomfacy      - These two zoom factors allow for non-uniform scaling.
508  *                                                        It is safe to scale by 0, as these factors are used to determine
509  *                                                        amount to enlarge 'cur' by
510  */
511
512 /* ------------------ 'Shared' stuff ------------------------ */
513
514 /* temp customdata for operator */
515 typedef struct v2dViewZoomData {
516         View2D *v2d;                    /* view2d we're operating in */
517         ARegion *ar;
518
519         /* needed for continuous zoom */
520         wmTimer *timer;
521         double timer_lastdraw;
522
523         int lastx, lasty;               /* previous x/y values of mouse in window */
524         int invoke_event;               /* event type that invoked, for modal exits */
525         float dx, dy;                   /* running tally of previous delta values (for obtaining final zoom) */
526         float mx_2d, my_2d;             /* initial mouse location in v2d coords */
527 } v2dViewZoomData;
528
529
530 /* initialise panning customdata */
531 static int view_zoomdrag_init(bContext *C, wmOperator *op)
532 {
533         ARegion *ar= CTX_wm_region(C);
534         v2dViewZoomData *vzd;
535         View2D *v2d;
536         
537         /* regions now have v2d-data by default, so check for region */
538         if (ar == NULL)
539                 return 0;
540         v2d= &ar->v2d;
541         
542         /* check that 2d-view is zoomable */
543         if ((v2d->keepzoom & V2D_LOCKZOOM_X) && (v2d->keepzoom & V2D_LOCKZOOM_Y))
544                 return 0;
545         
546         /* set custom-data for operator */
547         vzd= MEM_callocN(sizeof(v2dViewZoomData), "v2dViewZoomData");
548         op->customdata= vzd;
549         
550         /* set pointers to owners */
551         vzd->v2d= v2d;
552         vzd->ar = ar;
553         
554         return 1;
555 }
556
557 /* check if step-zoom can be applied */
558 static int view_zoom_poll(bContext *C)
559 {
560         ARegion *ar= CTX_wm_region(C);
561         View2D *v2d;
562         
563         /* check if there's a region in context to work with */
564         if (ar == NULL)
565                 return 0;
566         v2d= &ar->v2d;
567         
568         /* check that 2d-view is zoomable */
569         if ((v2d->keepzoom & V2D_LOCKZOOM_X) && (v2d->keepzoom & V2D_LOCKZOOM_Y))
570                 return 0;
571                 
572         /* view is zoomable */
573         return 1;
574 }
575  
576 /* apply transform to view (i.e. adjust 'cur' rect) */
577 static void view_zoomstep_apply(bContext *C, wmOperator *op)
578 {
579         v2dViewZoomData *vzd= op->customdata;
580         ARegion *ar= CTX_wm_region(C);
581         View2D *v2d= &ar->v2d;
582         float dx, dy, facx, facy;
583         
584         /* calculate amount to move view by, ensuring symmetry so the
585          * old zoom level is restored after zooming back the same amount 
586          */
587         facx= RNA_float_get(op->ptr, "zoomfacx");
588         facy= RNA_float_get(op->ptr, "zoomfacy");
589
590         if (facx >= 0.0f) {
591                 dx= (v2d->cur.xmax - v2d->cur.xmin) * facx;
592                 dy= (v2d->cur.ymax - v2d->cur.ymin) * facy;
593         }
594         else {
595                 dx= ((v2d->cur.xmax - v2d->cur.xmin)/(1.0f + 2.0f*facx)) * facx;
596                 dy= ((v2d->cur.ymax - v2d->cur.ymin)/(1.0f + 2.0f*facy)) * facy;
597         }
598
599         /* only resize view on an axis if change is allowed */
600         if ((v2d->keepzoom & V2D_LOCKZOOM_X)==0) {
601                 if (v2d->keepofs & V2D_LOCKOFS_X) {
602                         v2d->cur.xmax -= 2*dx;
603                 }
604                 else if (v2d->keepofs & V2D_KEEPOFS_X) {
605                         if (v2d->align & V2D_ALIGN_NO_POS_X)
606                                 v2d->cur.xmin += 2*dx;
607                         else
608                                 v2d->cur.xmax -= 2*dx;
609                 }
610                 else {
611                         if (U.uiflag & USER_ZOOM_TO_MOUSEPOS) {
612                                 float mval_fac = (vzd->mx_2d - v2d->cur.xmin) / (v2d->cur.xmax-v2d->cur.xmin);
613                                 float mval_faci = 1.0f - mval_fac;
614                                 float ofs= (mval_fac * dx) - (mval_faci * dx);
615                                 
616                                 v2d->cur.xmin += ofs + dx;
617                                 v2d->cur.xmax += ofs - dx;
618                         }
619                         else {
620                                 v2d->cur.xmin += dx;
621                                 v2d->cur.xmax -= dx;
622                         }
623                 }
624         }
625         if ((v2d->keepzoom & V2D_LOCKZOOM_Y)==0) {
626                 if (v2d->keepofs & V2D_LOCKOFS_Y) {
627                         v2d->cur.ymax -= 2*dy;
628                 }
629                 else if (v2d->keepofs & V2D_KEEPOFS_Y) {
630                         if (v2d->align & V2D_ALIGN_NO_POS_Y)
631                                 v2d->cur.ymin += 2*dy;
632                         else
633                                 v2d->cur.ymax -= 2*dy;
634                 }
635                 else {
636                         if (U.uiflag & USER_ZOOM_TO_MOUSEPOS) {
637                                 float mval_fac = (vzd->my_2d - v2d->cur.ymin) / (v2d->cur.ymax-v2d->cur.ymin);
638                                 float mval_faci = 1.0f - mval_fac;
639                                 float ofs= (mval_fac * dy) - (mval_faci * dy);
640                                 
641                                 v2d->cur.ymin += ofs + dy;
642                                 v2d->cur.ymax += ofs - dy;
643                         } 
644                         else {
645                                 v2d->cur.ymin += dy;
646                                 v2d->cur.ymax -= dy;
647                         }
648                 }
649         }
650
651         /* validate that view is in valid configuration after this operation */
652         UI_view2d_curRect_validate(v2d);
653
654         /* request updates to be done... */
655         ED_region_tag_redraw(vzd->ar);
656         UI_view2d_sync(CTX_wm_screen(C), CTX_wm_area(C), v2d, V2D_LOCK_COPY);
657 }
658
659 /* --------------- Individual Operators ------------------- */
660
661 /* cleanup temp customdata  */
662 static void view_zoomstep_exit(wmOperator *op)
663 {
664         if (op->customdata) {
665                 MEM_freeN(op->customdata);
666                 op->customdata= NULL;                           
667         }
668 }
669
670 /* this operator only needs this single callback, where it calls the view_zoom_*() methods */
671 static int view_zoomin_exec(bContext *C, wmOperator *op)
672 {
673         /* check that there's an active region, as View2D data resides there */
674         if (!view_zoom_poll(C))
675                 return OPERATOR_PASS_THROUGH;
676         
677         /* set RNA-Props - zooming in by uniform factor */
678         RNA_float_set(op->ptr, "zoomfacx", 0.0375f);
679         RNA_float_set(op->ptr, "zoomfacy", 0.0375f);
680         
681         /* apply movement, then we're done */
682         view_zoomstep_apply(C, op);
683         
684         view_zoomstep_exit(op);
685         
686         return OPERATOR_FINISHED;
687 }
688
689 static int view_zoomin_invoke(bContext *C, wmOperator *op, wmEvent *event)
690 {
691         v2dViewZoomData *vzd;
692         
693         if (!view_zoomdrag_init(C, op))
694                 return OPERATOR_PASS_THROUGH;
695         
696         vzd= op->customdata;
697         
698         if (U.uiflag & USER_ZOOM_TO_MOUSEPOS) {
699                 ARegion *ar= CTX_wm_region(C);
700                 
701                 /* store initial mouse position (in view space) */
702                 UI_view2d_region_to_view(&ar->v2d, 
703                                 event->mval[0], event->mval[1],
704                                 &vzd->mx_2d, &vzd->my_2d);
705         }
706         
707         return view_zoomin_exec(C, op);
708 }
709
710 static void VIEW2D_OT_zoom_in(wmOperatorType *ot)
711 {
712         /* identifiers */
713         ot->name= "Zoom In";
714         ot->description= "Zoom in the view";
715         ot->idname= "VIEW2D_OT_zoom_in";
716         
717         /* api callbacks */
718         ot->invoke= view_zoomin_invoke;
719 //      ot->exec= view_zoomin_exec;  // XXX, needs view_zoomdrag_init called first.
720         ot->poll= view_zoom_poll;
721         
722         /* rna - must keep these in sync with the other operators */
723         RNA_def_float(ot->srna, "zoomfacx", 0, -FLT_MAX, FLT_MAX, "Zoom Factor X", "", -FLT_MAX, FLT_MAX);
724         RNA_def_float(ot->srna, "zoomfacy", 0, -FLT_MAX, FLT_MAX, "Zoom Factor Y", "", -FLT_MAX, FLT_MAX);
725 }
726         
727 /* this operator only needs this single callback, where it callsthe view_zoom_*() methods */
728 static int view_zoomout_exec(bContext *C, wmOperator *op)
729 {
730         /* check that there's an active region, as View2D data resides there */
731         if (!view_zoom_poll(C))
732                 return OPERATOR_PASS_THROUGH;
733         
734         /* set RNA-Props - zooming in by uniform factor */
735         RNA_float_set(op->ptr, "zoomfacx", -0.0375f);
736         RNA_float_set(op->ptr, "zoomfacy", -0.0375f);
737         
738         /* apply movement, then we're done */
739         view_zoomstep_apply(C, op);
740
741         view_zoomstep_exit(op);
742         
743         return OPERATOR_FINISHED;
744 }
745
746 static int view_zoomout_invoke(bContext *C, wmOperator *op, wmEvent *event)
747 {
748         v2dViewZoomData *vzd;
749         
750         if (!view_zoomdrag_init(C, op))
751                 return OPERATOR_PASS_THROUGH;
752
753         vzd= op->customdata;
754         
755         if(U.uiflag & USER_ZOOM_TO_MOUSEPOS) {
756                 ARegion *ar= CTX_wm_region(C);
757                 
758                 /* store initial mouse position (in view space) */
759                 UI_view2d_region_to_view(&ar->v2d, 
760                                 event->mval[0], event->mval[1],
761                                 &vzd->mx_2d, &vzd->my_2d);
762         }
763         
764         return view_zoomout_exec(C, op);
765 }
766
767 static void VIEW2D_OT_zoom_out(wmOperatorType *ot)
768 {
769         /* identifiers */
770         ot->name= "Zoom Out";
771         ot->description= "Zoom out the view";
772         ot->idname= "VIEW2D_OT_zoom_out";
773         
774         /* api callbacks */
775         ot->invoke= view_zoomout_invoke;
776 //      ot->exec= view_zoomout_exec; // XXX, needs view_zoomdrag_init called first.
777         ot->poll= view_zoom_poll;
778         
779         /* rna - must keep these in sync with the other operators */
780         RNA_def_float(ot->srna, "zoomfacx", 0, -FLT_MAX, FLT_MAX, "Zoom Factor X", "", -FLT_MAX, FLT_MAX);
781         RNA_def_float(ot->srna, "zoomfacy", 0, -FLT_MAX, FLT_MAX, "Zoom Factor Y", "", -FLT_MAX, FLT_MAX);
782 }
783
784 /* ********************************************************* */
785 /* DRAG-ZOOM OPERATOR                                                                    */
786
787 /*      MMB Drag - allows non-uniform scaling by dragging mouse
788  *
789  *      In order to make sure this works, each operator must define the following RNA-Operator Props:
790  *              deltax, deltay  - amounts to add to each side of the 'cur' rect
791  */
792
793 /* apply transform to view (i.e. adjust 'cur' rect) */
794 static void view_zoomdrag_apply(bContext *C, wmOperator *op)
795 {
796         v2dViewZoomData *vzd= op->customdata;
797         View2D *v2d= vzd->v2d;
798         float dx, dy;
799         
800         /* get amount to move view by */
801         dx= RNA_float_get(op->ptr, "deltax");
802         dy= RNA_float_get(op->ptr, "deltay");
803
804         /* continous zoom shouldn't move that fast... */
805         if (U.viewzoom == USER_ZOOM_CONT) { // XXX store this setting as RNA prop?
806                 double time= PIL_check_seconds_timer();
807                 float time_step= (float)(time - vzd->timer_lastdraw);
808
809                 dx *= time_step * 0.5f;
810                 dy *= time_step * 0.5f;
811                 
812                 vzd->timer_lastdraw= time;
813         }
814
815         /* only move view on an axis if change is allowed */
816         if ((v2d->keepzoom & V2D_LOCKZOOM_X)==0) {
817                 if (v2d->keepofs & V2D_LOCKOFS_X) {
818                         v2d->cur.xmax -= 2*dx;
819                 }
820                 else {
821                         if (U.uiflag & USER_ZOOM_TO_MOUSEPOS) {
822                                 float mval_fac = (vzd->mx_2d - v2d->cur.xmin) / (v2d->cur.xmax-v2d->cur.xmin);
823                                 float mval_faci = 1.0f - mval_fac;
824                                 float ofs= (mval_fac * dx) - (mval_faci * dx);
825                                 
826                                 v2d->cur.xmin += ofs + dx;
827                                 v2d->cur.xmax += ofs - dx;
828                         }
829                         else {
830                                 v2d->cur.xmin += dx;
831                                 v2d->cur.xmax -= dx;
832                         }
833                 }
834         }
835         if ((v2d->keepzoom & V2D_LOCKZOOM_Y)==0) {
836                 if (v2d->keepofs & V2D_LOCKOFS_Y) {
837                         v2d->cur.ymax -= 2*dy;
838                 }
839                 else {
840                         if (U.uiflag & USER_ZOOM_TO_MOUSEPOS) {
841                                 float mval_fac = (vzd->my_2d - v2d->cur.ymin) / (v2d->cur.ymax-v2d->cur.ymin);
842                                 float mval_faci = 1.0f - mval_fac;
843                                 float ofs= (mval_fac * dy) - (mval_faci * dy);
844                                 
845                                 v2d->cur.ymin += ofs + dy;
846                                 v2d->cur.ymax += ofs - dy;
847                         }
848                         else {
849                                 v2d->cur.ymin += dy;
850                                 v2d->cur.ymax -= dy;
851                         }
852                 }
853         }
854         
855         /* validate that view is in valid configuration after this operation */
856         UI_view2d_curRect_validate(v2d);
857         
858         /* request updates to be done... */
859         ED_region_tag_redraw(vzd->ar);
860         UI_view2d_sync(CTX_wm_screen(C), CTX_wm_area(C), v2d, V2D_LOCK_COPY);
861 }
862
863 /* cleanup temp customdata  */
864 static void view_zoomdrag_exit(bContext *C, wmOperator *op)
865 {
866         if (op->customdata) {
867                 v2dViewZoomData *vzd= op->customdata;
868                 
869                 if(vzd->timer)
870                         WM_event_remove_timer(CTX_wm_manager(C), CTX_wm_window(C), vzd->timer);
871                 
872                 MEM_freeN(op->customdata);
873                 op->customdata= NULL;                           
874         }
875
876
877 static int view_zoomdrag_cancel(bContext *C, wmOperator *op)
878 {
879         view_zoomdrag_exit(C, op);
880
881         return OPERATOR_CANCELLED;
882 }
883
884 /* for 'redo' only, with no user input */
885 static int view_zoomdrag_exec(bContext *C, wmOperator *op)
886 {
887         if (!view_zoomdrag_init(C, op))
888                 return OPERATOR_PASS_THROUGH;
889         
890         view_zoomdrag_apply(C, op);
891         view_zoomdrag_exit(C, op);
892         return OPERATOR_FINISHED;
893 }
894
895 /* set up modal operator and relevant settings */
896 static int view_zoomdrag_invoke(bContext *C, wmOperator *op, wmEvent *event)
897 {
898         wmWindow *window= CTX_wm_window(C);
899         v2dViewZoomData *vzd;
900         View2D *v2d;
901         
902         /* set up customdata */
903         if (!view_zoomdrag_init(C, op))
904                 return OPERATOR_PASS_THROUGH;
905         
906         vzd= op->customdata;
907         v2d= vzd->v2d;
908         
909         if (event->type == MOUSEZOOM) {
910                 float dx, dy, fac;
911                 
912                 vzd->lastx= event->prevx;
913                 vzd->lasty= event->prevy;
914                 
915                 /* As we have only 1D information (magnify value), feed both axes
916                  * with magnify information that is stored in x axis 
917                  */
918                 fac= 0.01f * (event->x - event->prevx);
919                 dx= fac * (v2d->cur.xmax - v2d->cur.xmin) / 10.0f;
920                 dy= fac * (v2d->cur.ymax - v2d->cur.ymin) / 10.0f;
921
922                 RNA_float_set(op->ptr, "deltax", dx);
923                 RNA_float_set(op->ptr, "deltay", dy);
924                 
925                 view_zoomdrag_apply(C, op);
926                 view_zoomdrag_exit(C, op);
927                 return OPERATOR_FINISHED;
928         }       
929         
930         /* set initial settings */
931         vzd->lastx= event->x;
932         vzd->lasty= event->y;
933         RNA_float_set(op->ptr, "deltax", 0);
934         RNA_float_set(op->ptr, "deltay", 0);
935         
936         /* for modal exit test */
937         vzd->invoke_event= event->type;
938         
939         if (U.uiflag & USER_ZOOM_TO_MOUSEPOS) {
940                 ARegion *ar= CTX_wm_region(C);
941                 
942                 /* store initial mouse position (in view space) */
943                 UI_view2d_region_to_view(&ar->v2d, 
944                                 event->mval[0], event->mval[1],
945                                 &vzd->mx_2d, &vzd->my_2d);
946         }
947
948         if (v2d->keepofs & V2D_LOCKOFS_X)
949                 WM_cursor_modal(window, BC_NS_SCROLLCURSOR);
950         else if (v2d->keepofs & V2D_LOCKOFS_Y)
951                 WM_cursor_modal(window, BC_EW_SCROLLCURSOR);
952         else
953                 WM_cursor_modal(window, BC_NSEW_SCROLLCURSOR);
954         
955         /* add temp handler */
956         WM_event_add_modal_handler(C, op);
957
958         if (U.viewzoom == USER_ZOOM_CONT) {
959                 /* needs a timer to continue redrawing */
960                 vzd->timer= WM_event_add_timer(CTX_wm_manager(C), CTX_wm_window(C), TIMER, 0.01f);
961                 vzd->timer_lastdraw= PIL_check_seconds_timer();
962         }
963
964         return OPERATOR_RUNNING_MODAL;
965 }
966
967 /* handle user input - calculations of mouse-movement need to be done here, not in the apply callback! */
968 static int view_zoomdrag_modal(bContext *C, wmOperator *op, wmEvent *event)
969 {
970         v2dViewZoomData *vzd= op->customdata;
971         View2D *v2d= vzd->v2d;
972         
973         /* execute the events */
974         if (event->type == TIMER && event->customdata == vzd->timer) {
975                 view_zoomdrag_apply(C, op);
976         }
977         else if(event->type == MOUSEMOVE) {
978                 float dx, dy;
979                 
980                 /* calculate new delta transform, based on zooming mode */
981                 if (U.viewzoom == USER_ZOOM_SCALE) {
982                         /* 'scale' zooming */
983                         float dist;
984                         
985                         /* x-axis transform */
986                         dist = (v2d->mask.xmax - v2d->mask.xmin) / 2.0f;
987                         dx= 1.0f - (fabsf(vzd->lastx - dist) + 2.0f) / (fabsf(event->x - dist) + 2.0f);
988                         dx*= 0.5f * (v2d->cur.xmax - v2d->cur.xmin);
989                         
990                         /* y-axis transform */
991                         dist = (v2d->mask.ymax - v2d->mask.ymin) / 2.0f;
992                         dy= 1.0f - (fabsf(vzd->lasty - dist) + 2.0f) / (fabsf(event->y - dist) + 2.0f);
993                         dy*= 0.5f * (v2d->cur.ymax - v2d->cur.ymin);
994                 }
995                 else {
996                         /* 'continuous' or 'dolly' */
997                         float fac;
998                         
999                         /* x-axis transform */
1000                         fac= 0.01f * (event->x - vzd->lastx);
1001                         dx= fac * (v2d->cur.xmax - v2d->cur.xmin);
1002                         
1003                         /* y-axis transform */
1004                         fac= 0.01f * (event->y - vzd->lasty);
1005                         dy= fac * (v2d->cur.ymax - v2d->cur.ymin);
1006 #if 0
1007                         /* continous zoom shouldn't move that fast... */
1008                         if (U.viewzoom == USER_ZOOM_CONT) { // XXX store this setting as RNA prop?
1009                                 double time= PIL_check_seconds_timer();
1010                                 float time_step= (float)(time - vzd->timer_lastdraw);
1011
1012                                 dx /= (0.1f / time_step);
1013                                 dy /= (0.1f / time_step);
1014                                 
1015                                 vzd->timer_lastdraw= time;
1016                         }
1017 #endif
1018                 }
1019                 
1020                 /* set transform amount, and add current deltas to stored total delta (for redo) */
1021                 RNA_float_set(op->ptr, "deltax", dx);
1022                 RNA_float_set(op->ptr, "deltay", dy);
1023                 vzd->dx += dx;
1024                 vzd->dy += dy;
1025                 
1026                 /* store mouse coordinates for next time, if not doing continuous zoom
1027                  *      - continuous zoom only depends on distance of mouse to starting point to determine rate of change
1028                  */
1029                 if (U.viewzoom != USER_ZOOM_CONT) { // XXX store this setting as RNA prop?
1030                         vzd->lastx= event->x;
1031                         vzd->lasty= event->y;
1032                 }
1033                 
1034                 /* apply zooming */
1035                 view_zoomdrag_apply(C, op);
1036         } 
1037         else if (event->type == vzd->invoke_event || event->type==ESCKEY) {
1038                 if (event->val == KM_RELEASE) {
1039                         
1040                         /* for redo, store the overall deltas - need to respect zoom-locks here... */
1041                         if ((v2d->keepzoom & V2D_LOCKZOOM_X)==0)
1042                                 RNA_float_set(op->ptr, "deltax", vzd->dx);
1043                         else
1044                                 RNA_float_set(op->ptr, "deltax", 0);
1045                                 
1046                         if ((v2d->keepzoom & V2D_LOCKZOOM_Y)==0)
1047                                 RNA_float_set(op->ptr, "deltay", vzd->dy);
1048                         else
1049                                 RNA_float_set(op->ptr, "deltay", 0);
1050                         
1051                         /* free customdata */
1052                         view_zoomdrag_exit(C, op);
1053                         WM_cursor_restore(CTX_wm_window(C));
1054                         
1055                         return OPERATOR_FINISHED;
1056                 }
1057         }
1058
1059         return OPERATOR_RUNNING_MODAL;
1060 }
1061
1062 static void VIEW2D_OT_zoom(wmOperatorType *ot)
1063 {
1064         /* identifiers */
1065         ot->name= "Zoom 2D View";
1066         ot->description= "Zoom in/out the view";
1067         ot->idname= "VIEW2D_OT_zoom";
1068         
1069         /* api callbacks */
1070         ot->exec= view_zoomdrag_exec;
1071         ot->invoke= view_zoomdrag_invoke;
1072         ot->modal= view_zoomdrag_modal;
1073         ot->cancel= view_zoomdrag_cancel;
1074         
1075         ot->poll= view_zoom_poll;
1076         
1077         /* operator is repeatable */
1078         // ot->flag= OPTYPE_BLOCKING;
1079         
1080         /* rna - must keep these in sync with the other operators */
1081         RNA_def_float(ot->srna, "deltax", 0, -FLT_MAX, FLT_MAX, "Delta X", "", -FLT_MAX, FLT_MAX);
1082         RNA_def_float(ot->srna, "deltay", 0, -FLT_MAX, FLT_MAX, "Delta Y", "", -FLT_MAX, FLT_MAX);
1083 }
1084
1085 /* ********************************************************* */
1086 /* BORDER-ZOOM */
1087
1088 /* The user defines a rect using standard borderselect tools, and we use this rect to 
1089  * define the new zoom-level of the view in the following ways:
1090  *      1) LEFTMOUSE - zoom in to view
1091  *      2) RIGHTMOUSE - zoom out of view
1092  *
1093  * Currently, these key mappings are hardcoded, but it shouldn't be too important to
1094  * have custom keymappings for this...
1095  */
1096  
1097 static int view_borderzoom_exec(bContext *C, wmOperator *op)
1098 {
1099         ARegion *ar= CTX_wm_region(C);
1100         View2D *v2d= &ar->v2d;
1101         rctf rect;
1102         int gesture_mode;
1103         
1104         /* convert coordinates of rect to 'tot' rect coordinates */
1105         UI_view2d_region_to_view(v2d, RNA_int_get(op->ptr, "xmin"), RNA_int_get(op->ptr, "ymin"), &rect.xmin, &rect.ymin);
1106         UI_view2d_region_to_view(v2d, RNA_int_get(op->ptr, "xmax"), RNA_int_get(op->ptr, "ymax"), &rect.xmax, &rect.ymax);
1107         
1108         /* check if zooming in/out view */
1109         gesture_mode= RNA_int_get(op->ptr, "gesture_mode");
1110         
1111         if (gesture_mode == GESTURE_MODAL_IN) {
1112                 /* zoom in: 
1113                  *      - 'cur' rect will be defined by the coordinates of the border region 
1114                  *      - just set the 'cur' rect to have the same coordinates as the border region
1115                  *        if zoom is allowed to be changed
1116                  */
1117                 if ((v2d->keepzoom & V2D_LOCKZOOM_X)==0) {
1118                         v2d->cur.xmin= rect.xmin;
1119                         v2d->cur.xmax= rect.xmax;
1120                 }
1121                 if ((v2d->keepzoom & V2D_LOCKZOOM_Y)==0) {
1122                         v2d->cur.ymin= rect.ymin;
1123                         v2d->cur.ymax= rect.ymax;
1124                 }
1125         }
1126         else /* if (gesture_mode == GESTURE_MODAL_OUT) */ {
1127                 /* zoom out:
1128                  *      - the current 'cur' rect coordinates are going to end upwhere the 'rect' ones are, 
1129                  *        but the 'cur' rect coordinates will need to be adjusted to take in more of the view
1130                  *      - calculate zoom factor, and adjust using center-point
1131                  */
1132                 float zoom, center, size;
1133                 
1134                 // TODO: is this zoom factor calculation valid? It seems to produce same results everytime...
1135                 if ((v2d->keepzoom & V2D_LOCKZOOM_X)==0) {
1136                         size= (v2d->cur.xmax - v2d->cur.xmin);
1137                         zoom= size / (rect.xmax - rect.xmin);
1138                         center= (v2d->cur.xmax + v2d->cur.xmin) * 0.5f;
1139                         
1140                         v2d->cur.xmin= center - (size * zoom);
1141                         v2d->cur.xmax= center + (size * zoom);
1142                 }
1143                 if ((v2d->keepzoom & V2D_LOCKZOOM_Y)==0) {
1144                         size= (v2d->cur.ymax - v2d->cur.ymin);
1145                         zoom= size / (rect.ymax - rect.ymin);
1146                         center= (v2d->cur.ymax + v2d->cur.ymin) * 0.5f;
1147                         
1148                         v2d->cur.ymin= center - (size * zoom);
1149                         v2d->cur.ymax= center + (size * zoom);
1150                 }
1151         }
1152         
1153         /* validate that view is in valid configuration after this operation */
1154         UI_view2d_curRect_validate(v2d);
1155         
1156         /* request updates to be done... */
1157         ED_region_tag_redraw(ar);
1158         UI_view2d_sync(CTX_wm_screen(C), CTX_wm_area(C), v2d, V2D_LOCK_COPY);
1159         
1160         return OPERATOR_FINISHED;
1161
1162
1163 static void VIEW2D_OT_zoom_border(wmOperatorType *ot)
1164 {
1165         /* identifiers */
1166         ot->name= "Zoom to Border";
1167         ot->description= "Zoom in the view to the nearest item contained in the border";
1168         ot->idname= "VIEW2D_OT_zoom_border";
1169         
1170         /* api callbacks */
1171         ot->invoke= WM_border_select_invoke;
1172         ot->exec= view_borderzoom_exec;
1173         ot->modal= WM_border_select_modal;
1174         ot->cancel= WM_border_select_cancel;
1175         
1176         ot->poll= view_zoom_poll;
1177         
1178         /* rna */
1179         RNA_def_int(ot->srna, "gesture_mode", 0, INT_MIN, INT_MAX, "Gesture Mode", "", INT_MIN, INT_MAX);
1180         RNA_def_int(ot->srna, "xmin", 0, INT_MIN, INT_MAX, "X Min", "", INT_MIN, INT_MAX);
1181         RNA_def_int(ot->srna, "xmax", 0, INT_MIN, INT_MAX, "X Max", "", INT_MIN, INT_MAX);
1182         RNA_def_int(ot->srna, "ymin", 0, INT_MIN, INT_MAX, "Y Min", "", INT_MIN, INT_MAX);
1183         RNA_def_int(ot->srna, "ymax", 0, INT_MIN, INT_MAX, "Y Max", "", INT_MIN, INT_MAX);
1184 }
1185
1186 /* ********************************************************* */
1187 /* SCROLLERS */
1188
1189 /*      Scrollers should behave in the following ways, when clicked on with LMB (and dragged):
1190  *              1) 'Handles' on end of 'bubble' - when the axis that the scroller represents is zoomable, 
1191  *                      enlarge 'cur' rect on the relevant side 
1192  *              2) 'Bubble'/'bar' - just drag, and bar should move with mouse (view pans opposite)
1193  *
1194  *      In order to make sure this works, each operator must define the following RNA-Operator Props:
1195  *              deltax, deltay  - define how much to move view by (relative to zoom-correction factor)
1196  */
1197
1198 /* customdata for scroller-invoke data */
1199 typedef struct v2dScrollerMove {
1200         View2D *v2d;                    /* View2D data that this operation affects */
1201         ARegion *ar;                    /* region that the scroller is in */
1202         
1203         short scroller;                 /* scroller that mouse is in ('h' or 'v') */
1204         short zone;                             /* -1 is min zoomer, 0 is bar, 1 is max zoomer */ // XXX find some way to provide visual feedback of this (active color?)
1205         
1206         float fac;                              /* view adjustment factor, based on size of region */
1207         float delta;                    /* amount moved by mouse on axis of interest */
1208         
1209         float scrollbarwidth;   /* width of the scrollbar itself, used for page up/down clicks */
1210         
1211         int lastx, lasty;               /* previous mouse coordinates (in screen coordinates) for determining movement */
1212 } v2dScrollerMove;
1213
1214
1215 /* View2DScrollers is typedef'd in UI_view2d.h 
1216  * This is a CUT DOWN VERSION of the 'real' version, which is defined in view2d.c, as we only need focus bubble info
1217  * WARNING: the start of this struct must not change, so that it stays in sync with the 'real' version
1218  *                 For now, we don't need to have a separate (internal) header for structs like this...
1219  */
1220 struct View2DScrollers {        
1221                 /* focus bubbles */
1222         int vert_min, vert_max; /* vertical scrollbar */
1223         int hor_min, hor_max;   /* horizontal scrollbar */
1224 };
1225
1226 /* quick enum for vsm->zone (scroller handles) */
1227 enum {
1228         SCROLLHANDLE_MIN= -1,
1229         SCROLLHANDLE_BAR,
1230         SCROLLHANDLE_MAX,
1231         SCROLLHANDLE_MIN_OUTSIDE,
1232         SCROLLHANDLE_MAX_OUTSIDE
1233 } /*eV2DScrollerHandle_Zone*/;
1234
1235 /* ------------------------ */
1236
1237 /* check if mouse is within scroller handle 
1238  *      - mouse                 =       relevant mouse coordinate in region space
1239  *      - sc_min, sc_max        =       extents of scroller 'groove' (potential available space for scroller)
1240  *      - sh_min, sh_max        =       positions of scrollbar handles
1241  */
1242 static short mouse_in_scroller_handle(int mouse, int sc_min, int sc_max, int sh_min, int sh_max)
1243 {
1244         short in_min, in_max, in_bar, out_min, out_max, in_view=1;
1245         
1246         /* firstly, check if 
1247          *      - 'bubble' fills entire scroller 
1248          *      - 'bubble' completely out of view on either side 
1249          */
1250         if ((sh_min <= sc_min) && (sh_max >= sc_max)) in_view= 0;
1251         if (sh_min == sh_max) {
1252                 if (sh_min <= sc_min) in_view= 0;
1253                 if (sh_max >= sc_max) in_view= 0;
1254         }
1255         else {
1256                 if (sh_max <= sc_min) in_view= 0;
1257                 if (sh_min >= sc_max) in_view= 0;
1258         }
1259         
1260         
1261         if (in_view == 0) {
1262                 return SCROLLHANDLE_BAR;
1263         }
1264         
1265         /* check if mouse is in or past either handle */
1266         // TODO: check if these extents are still valid or not
1267         in_max= ( (mouse >= (sh_max - V2D_SCROLLER_HANDLE_SIZE)) && (mouse <= (sh_max + V2D_SCROLLER_HANDLE_SIZE)) );
1268         in_min= ( (mouse <= (sh_min + V2D_SCROLLER_HANDLE_SIZE)) && (mouse >= (sh_min - V2D_SCROLLER_HANDLE_SIZE)) );
1269         in_bar= ( (mouse < (sh_max - V2D_SCROLLER_HANDLE_SIZE)) && (mouse > (sh_min + V2D_SCROLLER_HANDLE_SIZE)) );
1270         out_min= mouse < (sh_min - V2D_SCROLLER_HANDLE_SIZE);
1271         out_max= mouse > (sh_max + V2D_SCROLLER_HANDLE_SIZE);
1272         
1273         if (in_bar)
1274                 return SCROLLHANDLE_BAR;
1275         else if (in_max)
1276                 return SCROLLHANDLE_MAX;
1277         else if (in_min)
1278                 return SCROLLHANDLE_MIN;
1279         else if (out_min)
1280                 return SCROLLHANDLE_MIN_OUTSIDE;                          
1281         else if (out_max)
1282                 return SCROLLHANDLE_MAX_OUTSIDE;
1283         
1284         /* unlikely to happen, though we just cover it in case */
1285         return SCROLLHANDLE_BAR;
1286
1287
1288 /* initialise customdata for scroller manipulation operator */
1289 static void scroller_activate_init(bContext *C, wmOperator *op, wmEvent *event, short in_scroller)
1290 {
1291         v2dScrollerMove *vsm;
1292         View2DScrollers *scrollers;
1293         ARegion *ar= CTX_wm_region(C);
1294         View2D *v2d= &ar->v2d;
1295         float mask_size;
1296         
1297         /* set custom-data for operator */
1298         vsm= MEM_callocN(sizeof(v2dScrollerMove), "v2dScrollerMove");
1299         op->customdata= vsm;
1300         
1301         /* set general data */
1302         vsm->v2d= v2d;
1303         vsm->ar= ar;
1304         vsm->scroller= in_scroller;
1305         
1306         /* store mouse-coordinates, and convert mouse/screen coordinates to region coordinates */
1307         vsm->lastx = event->x;
1308         vsm->lasty = event->y;
1309         
1310         /* 'zone' depends on where mouse is relative to bubble 
1311          *      - zooming must be allowed on this axis, otherwise, default to pan
1312          */
1313         scrollers= UI_view2d_scrollers_calc(C, v2d, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY, V2D_ARG_DUMMY);
1314         if (in_scroller == 'h') {
1315                 /* horizontal scroller - calculate adjustment factor first */
1316                 mask_size= (float)(v2d->hor.xmax - v2d->hor.xmin);
1317                 vsm->fac= (v2d->tot.xmax - v2d->tot.xmin) / mask_size;
1318                 
1319                 /* get 'zone' (i.e. which part of scroller is activated) */
1320                 vsm->zone= mouse_in_scroller_handle(event->mval[0], v2d->hor.xmin, v2d->hor.xmax, scrollers->hor_min, scrollers->hor_max);
1321                 
1322                 if ((v2d->keepzoom & V2D_LOCKZOOM_X) && ELEM(vsm->zone, SCROLLHANDLE_MIN, SCROLLHANDLE_MAX)) {
1323                         /* default to scroll, as handles not usable */
1324                         vsm->zone= SCROLLHANDLE_BAR;
1325                 }
1326
1327                 vsm->scrollbarwidth = scrollers->hor_max - scrollers->hor_min;
1328         }
1329         else {
1330                 /* vertical scroller - calculate adjustment factor first */
1331                 mask_size= (float)(v2d->vert.ymax - v2d->vert.ymin);
1332                 vsm->fac= (v2d->tot.ymax - v2d->tot.ymin) / mask_size;
1333                 
1334                 /* get 'zone' (i.e. which part of scroller is activated) */
1335                 vsm->zone= mouse_in_scroller_handle(event->mval[1], v2d->vert.ymin, v2d->vert.ymax, scrollers->vert_min, scrollers->vert_max);
1336                         
1337                 if ((v2d->keepzoom & V2D_LOCKZOOM_Y) && ELEM(vsm->zone, SCROLLHANDLE_MIN, SCROLLHANDLE_MAX)) {
1338                         /* default to scroll, as handles not usable */
1339                         vsm->zone= SCROLLHANDLE_BAR;
1340                 }
1341                 
1342                 vsm->scrollbarwidth = scrollers->vert_max - scrollers->vert_min;
1343         }
1344         
1345         UI_view2d_scrollers_free(scrollers);
1346         ED_region_tag_redraw(ar);
1347 }
1348
1349 /* cleanup temp customdata  */
1350 static void scroller_activate_exit(bContext *C, wmOperator *op)
1351 {
1352         if (op->customdata) {
1353                 v2dScrollerMove *vsm= op->customdata;
1354
1355                 vsm->v2d->scroll_ui &= ~(V2D_SCROLL_H_ACTIVE|V2D_SCROLL_V_ACTIVE);
1356                 
1357                 MEM_freeN(op->customdata);
1358                 op->customdata= NULL;           
1359                 
1360                 ED_region_tag_redraw(CTX_wm_region(C));
1361         }
1362 }
1363
1364 static int scroller_activate_cancel(bContext *C, wmOperator *op)
1365 {
1366         scroller_activate_exit(C, op);
1367
1368         return OPERATOR_CANCELLED;
1369 }
1370
1371 /* apply transform to view (i.e. adjust 'cur' rect) */
1372 static void scroller_activate_apply(bContext *C, wmOperator *op)
1373 {
1374         v2dScrollerMove *vsm= op->customdata;
1375         View2D *v2d= vsm->v2d;
1376         float temp;
1377         
1378         /* calculate amount to move view by */
1379         temp= vsm->fac * vsm->delta;
1380         
1381         /* type of movement */
1382         switch (vsm->zone) {
1383                 case SCROLLHANDLE_MIN:
1384                         /* only expand view on axis if zoom is allowed */
1385                         if ((vsm->scroller == 'h') && !(v2d->keepzoom & V2D_LOCKZOOM_X))
1386                                 v2d->cur.xmin -= temp;
1387                         if ((vsm->scroller == 'v') && !(v2d->keepzoom & V2D_LOCKZOOM_Y))
1388                                 v2d->cur.ymin -= temp;
1389                         break;
1390                         
1391                 case SCROLLHANDLE_MAX:
1392                         
1393                         /* only expand view on axis if zoom is allowed */
1394                         if ((vsm->scroller == 'h') && !(v2d->keepzoom & V2D_LOCKZOOM_X))
1395                                 v2d->cur.xmax += temp;
1396                         if ((vsm->scroller == 'v') && !(v2d->keepzoom & V2D_LOCKZOOM_Y))
1397                                 v2d->cur.ymax += temp;
1398                         break;
1399                         
1400                 case SCROLLHANDLE_MIN_OUTSIDE:
1401                 case SCROLLHANDLE_MAX_OUTSIDE:
1402                 case SCROLLHANDLE_BAR:
1403                 default:
1404                         /* only move view on an axis if panning is allowed */
1405                         if ((vsm->scroller == 'h') && !(v2d->keepofs & V2D_LOCKOFS_X)) {
1406                                 v2d->cur.xmin += temp;
1407                                 v2d->cur.xmax += temp;
1408                         }
1409                         if ((vsm->scroller == 'v') && !(v2d->keepofs & V2D_LOCKOFS_Y)) {
1410                                 v2d->cur.ymin += temp;
1411                                 v2d->cur.ymax += temp;
1412                         }
1413                         break;
1414                         
1415         }
1416         
1417         /* validate that view is in valid configuration after this operation */
1418         UI_view2d_curRect_validate(v2d);
1419         
1420         /* request updates to be done... */
1421         ED_region_tag_redraw(vsm->ar);
1422         UI_view2d_sync(CTX_wm_screen(C), CTX_wm_area(C), v2d, V2D_LOCK_COPY);
1423 }
1424
1425 /* handle user input for scrollers - calculations of mouse-movement need to be done here, not in the apply callback! */
1426 static int scroller_activate_modal(bContext *C, wmOperator *op, wmEvent *event)
1427 {
1428         v2dScrollerMove *vsm= op->customdata;
1429         
1430         /* execute the events */
1431         switch (event->type) {
1432                 case MOUSEMOVE:
1433                 {
1434                         /* calculate new delta transform, then store mouse-coordinates for next-time */
1435                         if (ELEM(vsm->zone, SCROLLHANDLE_BAR, SCROLLHANDLE_MAX)) {
1436                                 /* if using bar (i.e. 'panning') or 'max' zoom widget */
1437                                 switch (vsm->scroller) {
1438                                         case 'h': /* horizontal scroller - so only horizontal movement ('cur' moves opposite to mouse) */
1439                                                 vsm->delta= (float)(event->x - vsm->lastx);
1440                                                 break;
1441                                         case 'v': /* vertical scroller - so only vertical movement ('cur' moves opposite to mouse) */
1442                                                 vsm->delta= (float)(event->y - vsm->lasty);
1443                                                 break;
1444                                 }
1445                         }
1446                         else if (vsm->zone == SCROLLHANDLE_MIN) {
1447                                 /* using 'min' zoom widget */
1448                                 switch (vsm->scroller) {
1449                                         case 'h': /* horizontal scroller - so only horizontal movement ('cur' moves with mouse) */
1450                                                 vsm->delta= (float)(vsm->lastx - event->x);
1451                                                 break;
1452                                         case 'v': /* vertical scroller - so only vertical movement ('cur' moves with to mouse) */
1453                                                 vsm->delta= (float)(vsm->lasty - event->y);
1454                                                 break;
1455                                 }
1456                         }
1457                         
1458                         /* store previous coordinates */
1459                         vsm->lastx= event->x;
1460                         vsm->lasty= event->y;
1461                         
1462                         scroller_activate_apply(C, op);
1463                 }
1464                         break;
1465                         
1466                 case LEFTMOUSE:
1467                         if (event->val==KM_RELEASE) {
1468                                 /* single-click was in empty space outside bubble, so scroll by 1 'page' */
1469                                 if (ELEM(vsm->zone, SCROLLHANDLE_MIN_OUTSIDE, SCROLLHANDLE_MAX_OUTSIDE)) {
1470                                         if (vsm->zone == SCROLLHANDLE_MIN_OUTSIDE)
1471                                                 vsm->delta = -vsm->scrollbarwidth * 0.8f;
1472                                         else if (vsm->zone == SCROLLHANDLE_MAX_OUTSIDE)
1473                                                 vsm->delta = vsm->scrollbarwidth * 0.8f;
1474                                         
1475                                         scroller_activate_apply(C, op);
1476                                         scroller_activate_exit(C, op);
1477                                         return OPERATOR_FINISHED;
1478                                 }
1479                                 
1480                                 /* otherwise, end the drag action  */
1481                                 if (vsm->lastx || vsm->lasty) {
1482                                         scroller_activate_exit(C, op);
1483                                         return OPERATOR_FINISHED;
1484                                 }
1485                         }
1486                         break;
1487         }
1488
1489         return OPERATOR_RUNNING_MODAL;
1490 }
1491
1492
1493 /* a click (or click drag in progress) should have occurred, so check if it happened in scrollbar */
1494 static int scroller_activate_invoke(bContext *C, wmOperator *op, wmEvent *event)
1495 {
1496         ARegion *ar= CTX_wm_region(C);
1497         View2D *v2d= &ar->v2d;
1498         short in_scroller= 0;
1499                 
1500         /* check if mouse in scrollbars, if they're enabled */
1501         in_scroller= UI_view2d_mouse_in_scrollers(C, v2d, event->x, event->y);
1502         
1503         /* if in a scroller, init customdata then set modal handler which will catch mousedown to start doing useful stuff */
1504         if (in_scroller) {
1505                 v2dScrollerMove *vsm;
1506                 
1507                 /* initialise customdata */
1508                 scroller_activate_init(C, op, event, in_scroller);
1509                 vsm= (v2dScrollerMove *)op->customdata;
1510                 
1511                 /* check if zoom zones are inappropriate (i.e. zoom widgets not shown), so cannot continue
1512                  * NOTE: see view2d.c for latest conditions, and keep this in sync with that
1513                  */
1514                 if (ELEM(vsm->zone, SCROLLHANDLE_MIN, SCROLLHANDLE_MAX)) {
1515                         if ( ((vsm->scroller=='h') && (v2d->scroll & V2D_SCROLL_SCALE_HORIZONTAL)==0) ||
1516                                  ((vsm->scroller=='v') && (v2d->scroll & V2D_SCROLL_SCALE_VERTICAL)==0) )
1517                         {
1518                                 /* switch to bar (i.e. no scaling gets handled) */
1519                                 vsm->zone= SCROLLHANDLE_BAR;
1520                         }
1521                 }
1522                 
1523                 /* check if zone is inappropriate (i.e. 'bar' but panning is banned), so cannot continue */
1524                 if (vsm->zone == SCROLLHANDLE_BAR) {
1525                         if ( ((vsm->scroller=='h') && (v2d->keepofs & V2D_LOCKOFS_X)) ||
1526                                  ((vsm->scroller=='v') && (v2d->keepofs & V2D_LOCKOFS_Y)) )
1527                         {
1528                                 /* free customdata initialised */
1529                                 scroller_activate_exit(C, op);
1530                                 
1531                                 /* can't catch this event for ourselves, so let it go to someone else? */
1532                                 return OPERATOR_PASS_THROUGH;
1533                         }                       
1534                 }
1535                 
1536                 /* zone is also inappropriate if scroller is not visible... */
1537                 if ( ((vsm->scroller=='h') && (v2d->scroll & (V2D_SCROLL_HORIZONTAL_HIDE|V2D_SCROLL_HORIZONTAL_FULLR))) ||
1538                          ((vsm->scroller=='v') && (v2d->scroll & (V2D_SCROLL_VERTICAL_HIDE|V2D_SCROLL_VERTICAL_FULLR))) )
1539                 {
1540                         /* free customdata initialised */
1541                         scroller_activate_exit(C, op);
1542                                 
1543                         /* can't catch this event for ourselves, so let it go to someone else? */
1544                         /* XXX note: if handlers use mask rect to clip input, input will fail for this case */
1545                         return OPERATOR_PASS_THROUGH;
1546                 }
1547                 
1548                 /* activate the scroller */
1549                 if (vsm->scroller=='h')
1550                         v2d->scroll_ui |= V2D_SCROLL_H_ACTIVE;
1551                 else
1552                         v2d->scroll_ui |= V2D_SCROLL_V_ACTIVE;
1553                 
1554                 /* still ok, so can add */
1555                 WM_event_add_modal_handler(C, op);
1556                 return OPERATOR_RUNNING_MODAL;
1557         }
1558         else {
1559                 /* not in scroller, so nothing happened... (pass through let's something else catch event) */
1560                 return OPERATOR_PASS_THROUGH;
1561         }
1562 }
1563
1564 /* LMB-Drag in Scrollers - not repeatable operator! */
1565 static void VIEW2D_OT_scroller_activate(wmOperatorType *ot)
1566 {
1567         /* identifiers */
1568         ot->name= "Scroller Activate";
1569         ot->description= "Scroll view by mouse click and drag";
1570         ot->idname= "VIEW2D_OT_scroller_activate";
1571
1572         /* flags */
1573         ot->flag= OPTYPE_BLOCKING;
1574         
1575         /* api callbacks */
1576         ot->invoke= scroller_activate_invoke;
1577         ot->modal= scroller_activate_modal;
1578         ot->cancel= scroller_activate_cancel;
1579
1580         ot->poll= view2d_poll;
1581 }
1582
1583 /* ********************************************************* */
1584 /* RESET */
1585
1586 static int reset_exec(bContext *C, wmOperator *UNUSED(op))
1587 {
1588         uiStyle *style= UI_GetStyle();
1589         ARegion *ar= CTX_wm_region(C);
1590         View2D *v2d= &ar->v2d;
1591         int winx, winy;
1592
1593         /* zoom 1.0 */
1594         winx= (float)(v2d->mask.xmax - v2d->mask.xmin + 1);
1595         winy= (float)(v2d->mask.ymax - v2d->mask.ymin + 1);
1596
1597         v2d->cur.xmax= v2d->cur.xmin + winx;
1598         v2d->cur.ymax= v2d->cur.ymin + winy;
1599         
1600         /* align */
1601         if (v2d->align) {
1602                 /* posx and negx flags are mutually exclusive, so watch out */
1603                 if ((v2d->align & V2D_ALIGN_NO_POS_X) && !(v2d->align & V2D_ALIGN_NO_NEG_X)) {
1604                         v2d->cur.xmax= 0.0f;
1605                         v2d->cur.xmin= -winx*style->panelzoom;
1606                 }
1607                 else if ((v2d->align & V2D_ALIGN_NO_NEG_X) && !(v2d->align & V2D_ALIGN_NO_POS_X)) {
1608                         v2d->cur.xmax= winx*style->panelzoom;
1609                         v2d->cur.xmin= 0.0f;
1610                 }
1611
1612                 /* - posx and negx flags are mutually exclusive, so watch out */
1613                 if ((v2d->align & V2D_ALIGN_NO_POS_Y) && !(v2d->align & V2D_ALIGN_NO_NEG_Y)) {
1614                         v2d->cur.ymax= 0.0f;
1615                         v2d->cur.ymin= -winy*style->panelzoom;
1616                 }
1617                 else if ((v2d->align & V2D_ALIGN_NO_NEG_Y) && !(v2d->align & V2D_ALIGN_NO_POS_Y)) {
1618                         v2d->cur.ymax= winy*style->panelzoom;
1619                         v2d->cur.ymin= 0.0f;
1620                 }
1621         }
1622
1623         /* validate that view is in valid configuration after this operation */
1624         UI_view2d_curRect_validate(v2d);
1625         
1626         /* request updates to be done... */
1627         ED_region_tag_redraw(ar);
1628         UI_view2d_sync(CTX_wm_screen(C), CTX_wm_area(C), v2d, V2D_LOCK_COPY);
1629         
1630         return OPERATOR_FINISHED;
1631 }
1632  
1633 static void VIEW2D_OT_reset(wmOperatorType *ot)
1634 {
1635         /* identifiers */
1636         ot->name= "Reset View";
1637         ot->description= "Reset the view";
1638         ot->idname= "VIEW2D_OT_reset";
1639         
1640         /* api callbacks */
1641         ot->exec= reset_exec;
1642         ot->poll= view2d_poll;
1643 }
1644  
1645 /* ********************************************************* */
1646 /* Registration */
1647
1648 void UI_view2d_operatortypes(void)
1649 {
1650         WM_operatortype_append(VIEW2D_OT_pan);
1651         
1652         WM_operatortype_append(VIEW2D_OT_scroll_left);
1653         WM_operatortype_append(VIEW2D_OT_scroll_right);
1654         WM_operatortype_append(VIEW2D_OT_scroll_up);
1655         WM_operatortype_append(VIEW2D_OT_scroll_down);
1656         
1657         WM_operatortype_append(VIEW2D_OT_zoom_in);
1658         WM_operatortype_append(VIEW2D_OT_zoom_out);
1659         
1660         WM_operatortype_append(VIEW2D_OT_zoom);
1661         WM_operatortype_append(VIEW2D_OT_zoom_border);
1662         
1663         WM_operatortype_append(VIEW2D_OT_scroller_activate);
1664
1665         WM_operatortype_append(VIEW2D_OT_reset);
1666 }
1667
1668 void UI_view2d_keymap(wmKeyConfig *keyconf)
1669 {
1670         wmKeyMap *keymap= WM_keymap_find(keyconf, "View2D", 0, 0);
1671         
1672         /* pan/scroll */
1673         WM_keymap_add_item(keymap, "VIEW2D_OT_pan", MIDDLEMOUSE, KM_PRESS, 0, 0);
1674         WM_keymap_add_item(keymap, "VIEW2D_OT_pan", MIDDLEMOUSE, KM_PRESS, KM_SHIFT, 0);
1675         
1676         WM_keymap_add_item(keymap, "VIEW2D_OT_pan", MOUSEPAN, 0, 0, 0);
1677         
1678         WM_keymap_add_item(keymap, "VIEW2D_OT_scroll_right", WHEELDOWNMOUSE, KM_PRESS, KM_CTRL, 0);
1679         WM_keymap_add_item(keymap, "VIEW2D_OT_scroll_left", WHEELUPMOUSE, KM_PRESS, KM_CTRL, 0);
1680         
1681         WM_keymap_add_item(keymap, "VIEW2D_OT_scroll_down", WHEELDOWNMOUSE, KM_PRESS, KM_SHIFT, 0);
1682         WM_keymap_add_item(keymap, "VIEW2D_OT_scroll_up", WHEELUPMOUSE, KM_PRESS, KM_SHIFT, 0);
1683         
1684         /* zoom - single step */
1685         WM_keymap_add_item(keymap, "VIEW2D_OT_zoom_out", WHEELOUTMOUSE, KM_PRESS, 0, 0);
1686         WM_keymap_add_item(keymap, "VIEW2D_OT_zoom_in", WHEELINMOUSE, KM_PRESS, 0, 0);
1687         WM_keymap_add_item(keymap, "VIEW2D_OT_zoom_out", PADMINUS, KM_PRESS, 0, 0);
1688         WM_keymap_add_item(keymap, "VIEW2D_OT_zoom_in", PADPLUSKEY, KM_PRESS, 0, 0);
1689         
1690         /* scroll up/down - no modifiers, only when zoom fails */
1691                 /* these may fail if zoom is disallowed, in which case they should pass on event */
1692         WM_keymap_add_item(keymap, "VIEW2D_OT_scroll_down", WHEELDOWNMOUSE, KM_PRESS, 0, 0);
1693         WM_keymap_add_item(keymap, "VIEW2D_OT_scroll_up", WHEELUPMOUSE, KM_PRESS, 0, 0);
1694                 /* these may be necessary if vertical scroll is disallowed */
1695         WM_keymap_add_item(keymap, "VIEW2D_OT_scroll_right", WHEELDOWNMOUSE, KM_PRESS, 0, 0);
1696         WM_keymap_add_item(keymap, "VIEW2D_OT_scroll_left", WHEELUPMOUSE, KM_PRESS, 0, 0);
1697         
1698         /* alternatives for page up/down to scroll */
1699 #if 0 // XXX disabled, since this causes conflicts with hotkeys in animation editors
1700                 /* scroll up/down may fall through to left/right */
1701         WM_keymap_add_item(keymap, "VIEW2D_OT_scroll_down", PAGEDOWNKEY, KM_PRESS, 0, 0);
1702         WM_keymap_add_item(keymap, "VIEW2D_OT_scroll_up", PAGEUPKEY, KM_PRESS, 0, 0);
1703         WM_keymap_add_item(keymap, "VIEW2D_OT_scroll_right", PAGEDOWNKEY, KM_PRESS, 0, 0);
1704         WM_keymap_add_item(keymap, "VIEW2D_OT_scroll_left", PAGEUPKEY, KM_PRESS, 0, 0);
1705                 /* shift for moving view left/right with page up/down */
1706         WM_keymap_add_item(keymap, "VIEW2D_OT_scroll_right", PAGEDOWNKEY, KM_PRESS, KM_SHIFT, 0);
1707         WM_keymap_add_item(keymap, "VIEW2D_OT_scroll_left", PAGEUPKEY, KM_PRESS, KM_SHIFT, 0);
1708 #endif
1709         
1710         /* zoom - drag */
1711         WM_keymap_add_item(keymap, "VIEW2D_OT_zoom", MIDDLEMOUSE, KM_PRESS, KM_CTRL, 0);
1712         WM_keymap_add_item(keymap, "VIEW2D_OT_zoom", MOUSEZOOM, 0, 0, 0);
1713         
1714         /* borderzoom - drag */
1715         WM_keymap_add_item(keymap, "VIEW2D_OT_zoom_border", BKEY, KM_PRESS, KM_SHIFT, 0);
1716         
1717         /* scrollers */
1718         WM_keymap_add_item(keymap, "VIEW2D_OT_scroller_activate", LEFTMOUSE, KM_PRESS, 0, 0);
1719
1720         /* Alternative keymap for buttons listview */
1721         keymap= WM_keymap_find(keyconf, "View2D Buttons List", 0, 0);
1722         WM_keymap_add_item(keymap, "VIEW2D_OT_pan", MIDDLEMOUSE, KM_PRESS, 0, 0);
1723         WM_keymap_add_item(keymap, "VIEW2D_OT_pan", MOUSEPAN, 0, 0, 0);
1724         WM_keymap_add_item(keymap, "VIEW2D_OT_scroll_down", WHEELDOWNMOUSE, KM_PRESS, 0, 0);
1725         WM_keymap_add_item(keymap, "VIEW2D_OT_scroll_up", WHEELUPMOUSE, KM_PRESS, 0, 0);
1726         
1727         RNA_boolean_set(WM_keymap_add_item(keymap, "VIEW2D_OT_scroll_down", PAGEDOWNKEY, KM_PRESS, 0, 0)->ptr, "page", 1);
1728         RNA_boolean_set(WM_keymap_add_item(keymap, "VIEW2D_OT_scroll_up", PAGEUPKEY, KM_PRESS, 0, 0)->ptr, "page", 1);
1729         
1730         WM_keymap_add_item(keymap, "VIEW2D_OT_zoom", MIDDLEMOUSE, KM_PRESS, KM_CTRL, 0);
1731         WM_keymap_add_item(keymap, "VIEW2D_OT_zoom", MOUSEZOOM, 0, 0, 0);
1732         WM_keymap_add_item(keymap, "VIEW2D_OT_zoom_out", PADMINUS, KM_PRESS, 0, 0);
1733         WM_keymap_add_item(keymap, "VIEW2D_OT_zoom_in", PADPLUSKEY, KM_PRESS, 0, 0);
1734         WM_keymap_add_item(keymap, "VIEW2D_OT_reset", HOMEKEY, KM_PRESS, 0, 0);
1735         WM_keymap_add_item(keymap, "VIEW2D_OT_scroller_activate", LEFTMOUSE, KM_PRESS, 0, 0);
1736 }
1737