svn merge ^/trunk/blender -r43392:43420
[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         int scrollbar_orig;      /* initial location of scrollbar x/y, mouse relative */
1211         
1212         int lastx, lasty;               /* previous mouse coordinates (in screen coordinates) for determining movement */
1213 } v2dScrollerMove;
1214
1215
1216 /* View2DScrollers is typedef'd in UI_view2d.h 
1217  * This is a CUT DOWN VERSION of the 'real' version, which is defined in view2d.c, as we only need focus bubble info
1218  * WARNING: the start of this struct must not change, so that it stays in sync with the 'real' version
1219  *                 For now, we don't need to have a separate (internal) header for structs like this...
1220  */
1221 struct View2DScrollers {        
1222                 /* focus bubbles */
1223         int vert_min, vert_max; /* vertical scrollbar */
1224         int hor_min, hor_max;   /* horizontal scrollbar */
1225 };
1226
1227 /* quick enum for vsm->zone (scroller handles) */
1228 enum {
1229         SCROLLHANDLE_MIN= -1,
1230         SCROLLHANDLE_BAR,
1231         SCROLLHANDLE_MAX,
1232         SCROLLHANDLE_MIN_OUTSIDE,
1233         SCROLLHANDLE_MAX_OUTSIDE
1234 } /*eV2DScrollerHandle_Zone*/;
1235
1236 /* ------------------------ */
1237
1238 /* check if mouse is within scroller handle 
1239  *      - mouse                 =       relevant mouse coordinate in region space
1240  *      - sc_min, sc_max        =       extents of scroller 'groove' (potential available space for scroller)
1241  *      - sh_min, sh_max        =       positions of scrollbar handles
1242  */
1243 static short mouse_in_scroller_handle(int mouse, int sc_min, int sc_max, int sh_min, int sh_max)
1244 {
1245         short in_min, in_max, in_bar, out_min, out_max, in_view=1;
1246         
1247         /* firstly, check if 
1248          *      - 'bubble' fills entire scroller 
1249          *      - 'bubble' completely out of view on either side 
1250          */
1251         if ((sh_min <= sc_min) && (sh_max >= sc_max)) in_view= 0;
1252         if (sh_min == sh_max) {
1253                 if (sh_min <= sc_min) in_view= 0;
1254                 if (sh_max >= sc_max) in_view= 0;
1255         }
1256         else {
1257                 if (sh_max <= sc_min) in_view= 0;
1258                 if (sh_min >= sc_max) in_view= 0;
1259         }
1260         
1261         
1262         if (in_view == 0) {
1263                 return SCROLLHANDLE_BAR;
1264         }
1265         
1266         /* check if mouse is in or past either handle */
1267         // TODO: check if these extents are still valid or not
1268         in_max= ( (mouse >= (sh_max - V2D_SCROLLER_HANDLE_SIZE)) && (mouse <= (sh_max + V2D_SCROLLER_HANDLE_SIZE)) );
1269         in_min= ( (mouse <= (sh_min + V2D_SCROLLER_HANDLE_SIZE)) && (mouse >= (sh_min - V2D_SCROLLER_HANDLE_SIZE)) );
1270         in_bar= ( (mouse < (sh_max - V2D_SCROLLER_HANDLE_SIZE)) && (mouse > (sh_min + V2D_SCROLLER_HANDLE_SIZE)) );
1271         out_min= mouse < (sh_min - V2D_SCROLLER_HANDLE_SIZE);
1272         out_max= mouse > (sh_max + V2D_SCROLLER_HANDLE_SIZE);
1273         
1274         if (in_bar)
1275                 return SCROLLHANDLE_BAR;
1276         else if (in_max)
1277                 return SCROLLHANDLE_MAX;
1278         else if (in_min)
1279                 return SCROLLHANDLE_MIN;
1280         else if (out_min)
1281                 return SCROLLHANDLE_MIN_OUTSIDE;                          
1282         else if (out_max)
1283                 return SCROLLHANDLE_MAX_OUTSIDE;
1284         
1285         /* unlikely to happen, though we just cover it in case */
1286         return SCROLLHANDLE_BAR;
1287
1288
1289 /* initialise customdata for scroller manipulation operator */
1290 static void scroller_activate_init(bContext *C, wmOperator *op, wmEvent *event, short in_scroller)
1291 {
1292         v2dScrollerMove *vsm;
1293         View2DScrollers *scrollers;
1294         ARegion *ar= CTX_wm_region(C);
1295         View2D *v2d= &ar->v2d;
1296         float mask_size;
1297         
1298         /* set custom-data for operator */
1299         vsm= MEM_callocN(sizeof(v2dScrollerMove), "v2dScrollerMove");
1300         op->customdata= vsm;
1301         
1302         /* set general data */
1303         vsm->v2d= v2d;
1304         vsm->ar= ar;
1305         vsm->scroller= in_scroller;
1306
1307         /* store mouse-coordinates, and convert mouse/screen coordinates to region coordinates */
1308         vsm->lastx = event->x;
1309         vsm->lasty = event->y;
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
1315
1316         if (in_scroller == 'h') {
1317                 /* horizontal scroller - calculate adjustment factor first */
1318                 mask_size= (float)(v2d->hor.xmax - v2d->hor.xmin);
1319                 vsm->fac= (v2d->tot.xmax - v2d->tot.xmin) / mask_size;
1320                 
1321                 /* get 'zone' (i.e. which part of scroller is activated) */
1322                 vsm->zone= mouse_in_scroller_handle(event->mval[0], v2d->hor.xmin, v2d->hor.xmax, scrollers->hor_min, scrollers->hor_max);
1323                 
1324                 if ((v2d->keepzoom & V2D_LOCKZOOM_X) && ELEM(vsm->zone, SCROLLHANDLE_MIN, SCROLLHANDLE_MAX)) {
1325                         /* default to scroll, as handles not usable */
1326                         vsm->zone= SCROLLHANDLE_BAR;
1327                 }
1328
1329                 vsm->scrollbarwidth = scrollers->hor_max - scrollers->hor_min;
1330                 vsm->scrollbar_orig = ((scrollers->hor_max + scrollers->hor_min) / 2) + ar->winrct.xmin;
1331         }
1332         else {
1333                 /* vertical scroller - calculate adjustment factor first */
1334                 mask_size= (float)(v2d->vert.ymax - v2d->vert.ymin);
1335                 vsm->fac= (v2d->tot.ymax - v2d->tot.ymin) / mask_size;
1336                 
1337                 /* get 'zone' (i.e. which part of scroller is activated) */
1338                 vsm->zone= mouse_in_scroller_handle(event->mval[1], v2d->vert.ymin, v2d->vert.ymax, scrollers->vert_min, scrollers->vert_max);
1339                         
1340                 if ((v2d->keepzoom & V2D_LOCKZOOM_Y) && ELEM(vsm->zone, SCROLLHANDLE_MIN, SCROLLHANDLE_MAX)) {
1341                         /* default to scroll, as handles not usable */
1342                         vsm->zone= SCROLLHANDLE_BAR;
1343                 }
1344                 
1345                 vsm->scrollbarwidth = scrollers->vert_max - scrollers->vert_min;
1346                 vsm->scrollbar_orig = ((scrollers->vert_max + scrollers->vert_min) / 2) +  + ar->winrct.ymin;
1347         }
1348         
1349         UI_view2d_scrollers_free(scrollers);
1350         ED_region_tag_redraw(ar);
1351 }
1352
1353 /* cleanup temp customdata  */
1354 static void scroller_activate_exit(bContext *C, wmOperator *op)
1355 {
1356         if (op->customdata) {
1357                 v2dScrollerMove *vsm= op->customdata;
1358
1359                 vsm->v2d->scroll_ui &= ~(V2D_SCROLL_H_ACTIVE|V2D_SCROLL_V_ACTIVE);
1360                 
1361                 MEM_freeN(op->customdata);
1362                 op->customdata= NULL;           
1363                 
1364                 ED_region_tag_redraw(CTX_wm_region(C));
1365         }
1366 }
1367
1368 static int scroller_activate_cancel(bContext *C, wmOperator *op)
1369 {
1370         scroller_activate_exit(C, op);
1371
1372         return OPERATOR_CANCELLED;
1373 }
1374
1375 /* apply transform to view (i.e. adjust 'cur' rect) */
1376 static void scroller_activate_apply(bContext *C, wmOperator *op)
1377 {
1378         v2dScrollerMove *vsm= op->customdata;
1379         View2D *v2d= vsm->v2d;
1380         float temp;
1381         
1382         /* calculate amount to move view by */
1383         temp= vsm->fac * vsm->delta;
1384         
1385         /* type of movement */
1386         switch (vsm->zone) {
1387                 case SCROLLHANDLE_MIN:
1388                         /* only expand view on axis if zoom is allowed */
1389                         if ((vsm->scroller == 'h') && !(v2d->keepzoom & V2D_LOCKZOOM_X))
1390                                 v2d->cur.xmin -= temp;
1391                         if ((vsm->scroller == 'v') && !(v2d->keepzoom & V2D_LOCKZOOM_Y))
1392                                 v2d->cur.ymin -= temp;
1393                         break;
1394                         
1395                 case SCROLLHANDLE_MAX:
1396                         
1397                         /* only expand view on axis if zoom is allowed */
1398                         if ((vsm->scroller == 'h') && !(v2d->keepzoom & V2D_LOCKZOOM_X))
1399                                 v2d->cur.xmax += temp;
1400                         if ((vsm->scroller == 'v') && !(v2d->keepzoom & V2D_LOCKZOOM_Y))
1401                                 v2d->cur.ymax += temp;
1402                         break;
1403                         
1404                 case SCROLLHANDLE_MIN_OUTSIDE:
1405                 case SCROLLHANDLE_MAX_OUTSIDE:
1406                 case SCROLLHANDLE_BAR:
1407                 default:
1408                         /* only move view on an axis if panning is allowed */
1409                         if ((vsm->scroller == 'h') && !(v2d->keepofs & V2D_LOCKOFS_X)) {
1410                                 v2d->cur.xmin += temp;
1411                                 v2d->cur.xmax += temp;
1412                         }
1413                         if ((vsm->scroller == 'v') && !(v2d->keepofs & V2D_LOCKOFS_Y)) {
1414                                 v2d->cur.ymin += temp;
1415                                 v2d->cur.ymax += temp;
1416                         }
1417                         break;
1418                         
1419         }
1420         
1421         /* validate that view is in valid configuration after this operation */
1422         UI_view2d_curRect_validate(v2d);
1423         
1424         /* request updates to be done... */
1425         ED_region_tag_redraw(vsm->ar);
1426         UI_view2d_sync(CTX_wm_screen(C), CTX_wm_area(C), v2d, V2D_LOCK_COPY);
1427 }
1428
1429 /* handle user input for scrollers - calculations of mouse-movement need to be done here, not in the apply callback! */
1430 static int scroller_activate_modal(bContext *C, wmOperator *op, wmEvent *event)
1431 {
1432         v2dScrollerMove *vsm= op->customdata;
1433         
1434         /* execute the events */
1435         switch (event->type) {
1436                 case MOUSEMOVE:
1437                 {
1438                         /* calculate new delta transform, then store mouse-coordinates for next-time */
1439                         if (ELEM(vsm->zone, SCROLLHANDLE_BAR, SCROLLHANDLE_MAX)) {
1440                                 /* if using bar (i.e. 'panning') or 'max' zoom widget */
1441                                 switch (vsm->scroller) {
1442                                         case 'h': /* horizontal scroller - so only horizontal movement ('cur' moves opposite to mouse) */
1443                                                 vsm->delta= (float)(event->x - vsm->lastx);
1444                                                 break;
1445                                         case 'v': /* vertical scroller - so only vertical movement ('cur' moves opposite to mouse) */
1446                                                 vsm->delta= (float)(event->y - vsm->lasty);
1447                                                 break;
1448                                 }
1449                         }
1450                         else if (vsm->zone == SCROLLHANDLE_MIN) {
1451                                 /* using 'min' zoom widget */
1452                                 switch (vsm->scroller) {
1453                                         case 'h': /* horizontal scroller - so only horizontal movement ('cur' moves with mouse) */
1454                                                 vsm->delta= (float)(vsm->lastx - event->x);
1455                                                 break;
1456                                         case 'v': /* vertical scroller - so only vertical movement ('cur' moves with to mouse) */
1457                                                 vsm->delta= (float)(vsm->lasty - event->y);
1458                                                 break;
1459                                 }
1460                         }
1461                         
1462                         /* store previous coordinates */
1463                         vsm->lastx= event->x;
1464                         vsm->lasty= event->y;
1465                         
1466                         scroller_activate_apply(C, op);
1467                 }
1468                         break;
1469                         
1470                 case LEFTMOUSE:
1471                 case MIDDLEMOUSE:
1472                         if (event->val==KM_RELEASE) {
1473                                 /* single-click was in empty space outside bubble, so scroll by 1 'page' */
1474                                 if (ELEM(vsm->zone, SCROLLHANDLE_MIN_OUTSIDE, SCROLLHANDLE_MAX_OUTSIDE)) {
1475                                         if (vsm->zone == SCROLLHANDLE_MIN_OUTSIDE)
1476                                                 vsm->delta = -vsm->scrollbarwidth * 0.8f;
1477                                         else if (vsm->zone == SCROLLHANDLE_MAX_OUTSIDE)
1478                                                 vsm->delta = vsm->scrollbarwidth * 0.8f;
1479                                         
1480                                         scroller_activate_apply(C, op);
1481                                         scroller_activate_exit(C, op);
1482                                         return OPERATOR_FINISHED;
1483                                 }
1484                                 
1485                                 /* otherwise, end the drag action  */
1486                                 if (vsm->lastx || vsm->lasty) {
1487                                         scroller_activate_exit(C, op);
1488                                         return OPERATOR_FINISHED;
1489                                 }
1490                         }
1491                         break;
1492
1493         }
1494
1495         return OPERATOR_RUNNING_MODAL;
1496 }
1497
1498
1499 /* a click (or click drag in progress) should have occurred, so check if it happened in scrollbar */
1500 static int scroller_activate_invoke(bContext *C, wmOperator *op, wmEvent *event)
1501 {
1502         ARegion *ar= CTX_wm_region(C);
1503         View2D *v2d= &ar->v2d;
1504         short in_scroller= 0;
1505                 
1506         /* check if mouse in scrollbars, if they're enabled */
1507         in_scroller= UI_view2d_mouse_in_scrollers(C, v2d, event->x, event->y);
1508         
1509         /* if in a scroller, init customdata then set modal handler which will catch mousedown to start doing useful stuff */
1510         if (in_scroller) {
1511                 v2dScrollerMove *vsm;
1512                 
1513                 /* initialise customdata */
1514                 scroller_activate_init(C, op, event, in_scroller);
1515                 vsm= (v2dScrollerMove *)op->customdata;
1516                 
1517                 /* support for quick jump to location - gtk and qt do this on linux */
1518                 if (event->type == MIDDLEMOUSE) {
1519                         switch (vsm->scroller) {
1520                                 case 'h': /* horizontal scroller - so only horizontal movement ('cur' moves opposite to mouse) */
1521                                         vsm->delta= (float)(event->x - vsm->scrollbar_orig);
1522                                         break;
1523                                 case 'v': /* vertical scroller - so only vertical movement ('cur' moves opposite to mouse) */
1524                                         vsm->delta= (float)(event->y - vsm->scrollbar_orig);
1525                                         break;
1526                         }
1527                         scroller_activate_apply(C, op);
1528
1529                         vsm->zone= SCROLLHANDLE_BAR;
1530                 }
1531
1532                 /* check if zoom zones are inappropriate (i.e. zoom widgets not shown), so cannot continue
1533                  * NOTE: see view2d.c for latest conditions, and keep this in sync with that
1534                  */
1535                 if (ELEM(vsm->zone, SCROLLHANDLE_MIN, SCROLLHANDLE_MAX)) {
1536                         if ( ((vsm->scroller=='h') && (v2d->scroll & V2D_SCROLL_SCALE_HORIZONTAL)==0) ||
1537                                  ((vsm->scroller=='v') && (v2d->scroll & V2D_SCROLL_SCALE_VERTICAL)==0) )
1538                         {
1539                                 /* switch to bar (i.e. no scaling gets handled) */
1540                                 vsm->zone= SCROLLHANDLE_BAR;
1541                         }
1542                 }
1543                 
1544                 /* check if zone is inappropriate (i.e. 'bar' but panning is banned), so cannot continue */
1545                 if (vsm->zone == SCROLLHANDLE_BAR) {
1546                         if ( ((vsm->scroller=='h') && (v2d->keepofs & V2D_LOCKOFS_X)) ||
1547                                  ((vsm->scroller=='v') && (v2d->keepofs & V2D_LOCKOFS_Y)) )
1548                         {
1549                                 /* free customdata initialised */
1550                                 scroller_activate_exit(C, op);
1551                                 
1552                                 /* can't catch this event for ourselves, so let it go to someone else? */
1553                                 return OPERATOR_PASS_THROUGH;
1554                         }                       
1555                 }
1556                 
1557                 /* zone is also inappropriate if scroller is not visible... */
1558                 if ( ((vsm->scroller=='h') && (v2d->scroll & (V2D_SCROLL_HORIZONTAL_HIDE|V2D_SCROLL_HORIZONTAL_FULLR))) ||
1559                          ((vsm->scroller=='v') && (v2d->scroll & (V2D_SCROLL_VERTICAL_HIDE|V2D_SCROLL_VERTICAL_FULLR))) )
1560                 {
1561                         /* free customdata initialised */
1562                         scroller_activate_exit(C, op);
1563                                 
1564                         /* can't catch this event for ourselves, so let it go to someone else? */
1565                         /* XXX note: if handlers use mask rect to clip input, input will fail for this case */
1566                         return OPERATOR_PASS_THROUGH;
1567                 }
1568                 
1569                 /* activate the scroller */
1570                 if (vsm->scroller=='h')
1571                         v2d->scroll_ui |= V2D_SCROLL_H_ACTIVE;
1572                 else
1573                         v2d->scroll_ui |= V2D_SCROLL_V_ACTIVE;
1574                 
1575                 /* still ok, so can add */
1576                 WM_event_add_modal_handler(C, op);
1577                 return OPERATOR_RUNNING_MODAL;
1578         }
1579         else {
1580                 /* not in scroller, so nothing happened... (pass through let's something else catch event) */
1581                 return OPERATOR_PASS_THROUGH;
1582         }
1583 }
1584
1585 /* LMB-Drag in Scrollers - not repeatable operator! */
1586 static void VIEW2D_OT_scroller_activate(wmOperatorType *ot)
1587 {
1588         /* identifiers */
1589         ot->name= "Scroller Activate";
1590         ot->description= "Scroll view by mouse click and drag";
1591         ot->idname= "VIEW2D_OT_scroller_activate";
1592
1593         /* flags */
1594         ot->flag= OPTYPE_BLOCKING;
1595         
1596         /* api callbacks */
1597         ot->invoke= scroller_activate_invoke;
1598         ot->modal= scroller_activate_modal;
1599         ot->cancel= scroller_activate_cancel;
1600
1601         ot->poll= view2d_poll;
1602 }
1603
1604 /* ********************************************************* */
1605 /* RESET */
1606
1607 static int reset_exec(bContext *C, wmOperator *UNUSED(op))
1608 {
1609         uiStyle *style= UI_GetStyle();
1610         ARegion *ar= CTX_wm_region(C);
1611         View2D *v2d= &ar->v2d;
1612         int winx, winy;
1613
1614         /* zoom 1.0 */
1615         winx= (float)(v2d->mask.xmax - v2d->mask.xmin + 1);
1616         winy= (float)(v2d->mask.ymax - v2d->mask.ymin + 1);
1617
1618         v2d->cur.xmax= v2d->cur.xmin + winx;
1619         v2d->cur.ymax= v2d->cur.ymin + winy;
1620         
1621         /* align */
1622         if (v2d->align) {
1623                 /* posx and negx flags are mutually exclusive, so watch out */
1624                 if ((v2d->align & V2D_ALIGN_NO_POS_X) && !(v2d->align & V2D_ALIGN_NO_NEG_X)) {
1625                         v2d->cur.xmax= 0.0f;
1626                         v2d->cur.xmin= -winx*style->panelzoom;
1627                 }
1628                 else if ((v2d->align & V2D_ALIGN_NO_NEG_X) && !(v2d->align & V2D_ALIGN_NO_POS_X)) {
1629                         v2d->cur.xmax= winx*style->panelzoom;
1630                         v2d->cur.xmin= 0.0f;
1631                 }
1632
1633                 /* - posx and negx flags are mutually exclusive, so watch out */
1634                 if ((v2d->align & V2D_ALIGN_NO_POS_Y) && !(v2d->align & V2D_ALIGN_NO_NEG_Y)) {
1635                         v2d->cur.ymax= 0.0f;
1636                         v2d->cur.ymin= -winy*style->panelzoom;
1637                 }
1638                 else if ((v2d->align & V2D_ALIGN_NO_NEG_Y) && !(v2d->align & V2D_ALIGN_NO_POS_Y)) {
1639                         v2d->cur.ymax= winy*style->panelzoom;
1640                         v2d->cur.ymin= 0.0f;
1641                 }
1642         }
1643
1644         /* validate that view is in valid configuration after this operation */
1645         UI_view2d_curRect_validate(v2d);
1646         
1647         /* request updates to be done... */
1648         ED_region_tag_redraw(ar);
1649         UI_view2d_sync(CTX_wm_screen(C), CTX_wm_area(C), v2d, V2D_LOCK_COPY);
1650         
1651         return OPERATOR_FINISHED;
1652 }
1653  
1654 static void VIEW2D_OT_reset(wmOperatorType *ot)
1655 {
1656         /* identifiers */
1657         ot->name= "Reset View";
1658         ot->description= "Reset the view";
1659         ot->idname= "VIEW2D_OT_reset";
1660         
1661         /* api callbacks */
1662         ot->exec= reset_exec;
1663         ot->poll= view2d_poll;
1664 }
1665  
1666 /* ********************************************************* */
1667 /* Registration */
1668
1669 void UI_view2d_operatortypes(void)
1670 {
1671         WM_operatortype_append(VIEW2D_OT_pan);
1672         
1673         WM_operatortype_append(VIEW2D_OT_scroll_left);
1674         WM_operatortype_append(VIEW2D_OT_scroll_right);
1675         WM_operatortype_append(VIEW2D_OT_scroll_up);
1676         WM_operatortype_append(VIEW2D_OT_scroll_down);
1677         
1678         WM_operatortype_append(VIEW2D_OT_zoom_in);
1679         WM_operatortype_append(VIEW2D_OT_zoom_out);
1680         
1681         WM_operatortype_append(VIEW2D_OT_zoom);
1682         WM_operatortype_append(VIEW2D_OT_zoom_border);
1683         
1684         WM_operatortype_append(VIEW2D_OT_scroller_activate);
1685
1686         WM_operatortype_append(VIEW2D_OT_reset);
1687 }
1688
1689 void UI_view2d_keymap(wmKeyConfig *keyconf)
1690 {
1691         wmKeyMap *keymap= WM_keymap_find(keyconf, "View2D", 0, 0);
1692         
1693         /* scrollers */
1694         WM_keymap_add_item(keymap, "VIEW2D_OT_scroller_activate", LEFTMOUSE, KM_PRESS, 0, 0);
1695         WM_keymap_add_item(keymap, "VIEW2D_OT_scroller_activate", MIDDLEMOUSE, KM_PRESS, 0, 0);
1696
1697         /* pan/scroll */
1698         WM_keymap_add_item(keymap, "VIEW2D_OT_pan", MIDDLEMOUSE, KM_PRESS, 0, 0);
1699         WM_keymap_add_item(keymap, "VIEW2D_OT_pan", MIDDLEMOUSE, KM_PRESS, KM_SHIFT, 0);
1700         
1701         WM_keymap_add_item(keymap, "VIEW2D_OT_pan", MOUSEPAN, 0, 0, 0);
1702         
1703         WM_keymap_add_item(keymap, "VIEW2D_OT_scroll_right", WHEELDOWNMOUSE, KM_PRESS, KM_CTRL, 0);
1704         WM_keymap_add_item(keymap, "VIEW2D_OT_scroll_left", WHEELUPMOUSE, KM_PRESS, KM_CTRL, 0);
1705         
1706         WM_keymap_add_item(keymap, "VIEW2D_OT_scroll_down", WHEELDOWNMOUSE, KM_PRESS, KM_SHIFT, 0);
1707         WM_keymap_add_item(keymap, "VIEW2D_OT_scroll_up", WHEELUPMOUSE, KM_PRESS, KM_SHIFT, 0);
1708         
1709         /* zoom - single step */
1710         WM_keymap_add_item(keymap, "VIEW2D_OT_zoom_out", WHEELOUTMOUSE, KM_PRESS, 0, 0);
1711         WM_keymap_add_item(keymap, "VIEW2D_OT_zoom_in", WHEELINMOUSE, KM_PRESS, 0, 0);
1712         WM_keymap_add_item(keymap, "VIEW2D_OT_zoom_out", PADMINUS, KM_PRESS, 0, 0);
1713         WM_keymap_add_item(keymap, "VIEW2D_OT_zoom_in", PADPLUSKEY, KM_PRESS, 0, 0);
1714         
1715         /* scroll up/down - no modifiers, only when zoom fails */
1716                 /* these may fail if zoom is disallowed, in which case they should pass on event */
1717         WM_keymap_add_item(keymap, "VIEW2D_OT_scroll_down", WHEELDOWNMOUSE, KM_PRESS, 0, 0);
1718         WM_keymap_add_item(keymap, "VIEW2D_OT_scroll_up", WHEELUPMOUSE, KM_PRESS, 0, 0);
1719                 /* these may be necessary if vertical scroll is disallowed */
1720         WM_keymap_add_item(keymap, "VIEW2D_OT_scroll_right", WHEELDOWNMOUSE, KM_PRESS, 0, 0);
1721         WM_keymap_add_item(keymap, "VIEW2D_OT_scroll_left", WHEELUPMOUSE, KM_PRESS, 0, 0);
1722         
1723         /* alternatives for page up/down to scroll */
1724 #if 0 // XXX disabled, since this causes conflicts with hotkeys in animation editors
1725                 /* scroll up/down may fall through to left/right */
1726         WM_keymap_add_item(keymap, "VIEW2D_OT_scroll_down", PAGEDOWNKEY, KM_PRESS, 0, 0);
1727         WM_keymap_add_item(keymap, "VIEW2D_OT_scroll_up", PAGEUPKEY, KM_PRESS, 0, 0);
1728         WM_keymap_add_item(keymap, "VIEW2D_OT_scroll_right", PAGEDOWNKEY, KM_PRESS, 0, 0);
1729         WM_keymap_add_item(keymap, "VIEW2D_OT_scroll_left", PAGEUPKEY, KM_PRESS, 0, 0);
1730                 /* shift for moving view left/right with page up/down */
1731         WM_keymap_add_item(keymap, "VIEW2D_OT_scroll_right", PAGEDOWNKEY, KM_PRESS, KM_SHIFT, 0);
1732         WM_keymap_add_item(keymap, "VIEW2D_OT_scroll_left", PAGEUPKEY, KM_PRESS, KM_SHIFT, 0);
1733 #endif
1734         
1735         /* zoom - drag */
1736         WM_keymap_add_item(keymap, "VIEW2D_OT_zoom", MIDDLEMOUSE, KM_PRESS, KM_CTRL, 0);
1737         WM_keymap_add_item(keymap, "VIEW2D_OT_zoom", MOUSEZOOM, 0, 0, 0);
1738         
1739         /* borderzoom - drag */
1740         WM_keymap_add_item(keymap, "VIEW2D_OT_zoom_border", BKEY, KM_PRESS, KM_SHIFT, 0);
1741
1742         /* Alternative keymap for buttons listview */
1743         keymap= WM_keymap_find(keyconf, "View2D Buttons List", 0, 0);
1744
1745         WM_keymap_add_item(keymap, "VIEW2D_OT_scroller_activate", LEFTMOUSE, KM_PRESS, 0, 0);
1746         WM_keymap_add_item(keymap, "VIEW2D_OT_scroller_activate", MIDDLEMOUSE, KM_PRESS, 0, 0);
1747
1748         WM_keymap_add_item(keymap, "VIEW2D_OT_pan", MIDDLEMOUSE, KM_PRESS, 0, 0);
1749         WM_keymap_add_item(keymap, "VIEW2D_OT_pan", MOUSEPAN, 0, 0, 0);
1750         WM_keymap_add_item(keymap, "VIEW2D_OT_scroll_down", WHEELDOWNMOUSE, KM_PRESS, 0, 0);
1751         WM_keymap_add_item(keymap, "VIEW2D_OT_scroll_up", WHEELUPMOUSE, KM_PRESS, 0, 0);
1752         
1753         RNA_boolean_set(WM_keymap_add_item(keymap, "VIEW2D_OT_scroll_down", PAGEDOWNKEY, KM_PRESS, 0, 0)->ptr, "page", 1);
1754         RNA_boolean_set(WM_keymap_add_item(keymap, "VIEW2D_OT_scroll_up", PAGEUPKEY, KM_PRESS, 0, 0)->ptr, "page", 1);
1755         
1756         WM_keymap_add_item(keymap, "VIEW2D_OT_zoom", MIDDLEMOUSE, KM_PRESS, KM_CTRL, 0);
1757         WM_keymap_add_item(keymap, "VIEW2D_OT_zoom", MOUSEZOOM, 0, 0, 0);
1758         WM_keymap_add_item(keymap, "VIEW2D_OT_zoom_out", PADMINUS, KM_PRESS, 0, 0);
1759         WM_keymap_add_item(keymap, "VIEW2D_OT_zoom_in", PADPLUSKEY, KM_PRESS, 0, 0);
1760         WM_keymap_add_item(keymap, "VIEW2D_OT_reset", HOMEKEY, KM_PRESS, 0, 0);
1761 }
1762