* Added a better mouse pointer for the eyedropper tool (thanks Bart) and also
[blender.git] / source / blender / src / drawipo.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. The Blender
10  * Foundation also sells licenses for use in proprietary software under
11  * the Blender License.  See http://www.blender.org/BL/ for information
12  * about this.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * The Original Code is: all of this file.
27  *
28  * Contributor(s): none yet.
29  *
30  * ***** END GPL/BL DUAL LICENSE BLOCK *****
31  */
32
33 #include <stdio.h>
34 #include <math.h>
35 #include <string.h>
36
37 #ifdef HAVE_CONFIG_H
38 #include <config.h>
39 #endif
40
41 #ifndef _WIN32
42 #include <unistd.h>
43 #else
44 #include <io.h>
45 #endif   
46
47 #include "BMF_Api.h"
48 #include "MEM_guardedalloc.h"
49
50 #include "BLI_blenlib.h"
51 #include "BLI_arithb.h"
52
53 #include "DNA_action_types.h"
54 #include "DNA_curve_types.h"
55 #include "DNA_ipo_types.h"
56 #include "DNA_key_types.h"
57 #include "DNA_object_types.h"
58 #include "DNA_scene_types.h"
59 #include "DNA_screen_types.h"
60 #include "DNA_space_types.h"
61 #include "DNA_sequence_types.h"
62 #include "DNA_userdef_types.h"
63
64 #include "BKE_curve.h"
65 #include "BKE_depsgraph.h"
66 #include "BKE_global.h"
67 #include "BKE_ipo.h"
68 #include "BKE_key.h"
69 #include "BKE_utildefines.h"
70
71 #include "BIF_cursors.h"
72 #include "BIF_gl.h"
73 #include "BIF_graphics.h"
74 #include "BIF_resources.h"
75 #include "BIF_screen.h"
76 #include "BIF_interface.h"
77 #include "BIF_mywindow.h"
78 #include "BIF_space.h"
79 #include "BIF_toolbox.h"
80 #include "BIF_glutil.h"
81 #include "BIF_editseq.h"
82
83 #include "BSE_drawipo.h"
84 #include "BSE_view.h"
85 #include "BSE_editipo.h"
86 #include "BSE_editipo_types.h"
87 #include "BSE_editnla_types.h"
88
89 #include "BPY_extern.h"
90
91 #include "mydevice.h"
92 #include "blendef.h"
93 #include "butspace.h"   // shouldnt be...
94 #include "winlay.h"
95
96 /* local define... also used in editipo ... */
97 #define ISPOIN(a, b, c)                       ( (a->b) && (a->c) )  
98 #define ISPOIN3(a, b, c, d)           ( (a->b) && (a->c) && (a->d) )
99 #define ISPOIN4(a, b, c, d, e)        ( (a->b) && (a->c) && (a->d) && (a->e) )   
100
101                 /* minimum pixels per gridstep */
102 #define IPOSTEP 35
103
104 static float ipogrid_dx, ipogrid_dy, ipogrid_startx, ipogrid_starty;
105 static int ipomachtx, ipomachty;
106
107 static int vertymin, vertymax, horxmin, horxmax;        /* globals to test LEFTMOUSE for scrollbar */
108
109 extern short ACTWIDTH;          /* this is ugly! */
110
111 static void scroll_prstr(float x, float y, float val, char dir, int disptype)
112 {
113         int len, macht;
114         char str[32];
115         
116         if(dir=='v') {
117                 macht= ipomachty;
118                 if ELEM(disptype, IPO_DISPDEGR, IPO_DISPTIME) {
119                         macht+=1;
120                         val *= 10;
121                 }
122         }
123         else macht= ipomachtx;
124         
125         if (macht<=0) sprintf(str, "%.*f", 1-macht, val);
126         else sprintf(str, "%d", (int)floor(val + 0.375));
127         
128         len= strlen(str);
129         if(dir=='h') x-= 4*len;
130         
131         if(dir=='v' && disptype==IPO_DISPDEGR) {
132                 str[len]= 186; /* Degree symbol */
133                 str[len+1]= 0;
134         }
135         
136         glRasterPos2f(x,  y);
137         BMF_DrawString(G.fonts, str);
138 }
139
140 static void step_to_grid(float *step, int *macht)
141 {
142         float loga, rem;
143         
144         /* try to write step as a power of 10 */
145         
146         loga= log10(*step);
147         *macht= (int)(loga);
148
149         rem= loga- *macht;
150         rem= pow(10.0, rem);
151         
152         if(loga<0.0) {
153                 if(rem < 0.2) rem= 0.2;
154                 else if(rem < 0.5) rem= 0.5;
155                 else rem= 1.0;
156
157                 *step= rem*pow(10.0, (float)*macht);
158                 
159                 // partial of a frame have no meaning
160                 switch(curarea->spacetype) {
161                 case SPACE_TIME: {
162                         SpaceTime *stime= curarea->spacedata.first;
163                         if(stime->flag & TIME_DRAWFRAMES) {
164                                 rem = 1.0;
165                                 *step = 1.0;
166                         }
167                         break;
168                 }
169                 case SPACE_SEQ: {
170                         SpaceTime * sseq= curarea->spacedata.first;
171                         if (sseq->flag & SEQ_DRAWFRAMES) {
172                                 rem = 1.0;
173                                 *step = 1.0;
174                         }
175                 }
176                 default:
177                         break;
178                 }
179
180                 
181                 
182                 if(rem==1.0) (*macht)++;        // prevents printing 1.0 2.0 3.0 etc
183         }
184         else {
185                 if(rem < 2.0) rem= 2.0;
186                 else if(rem < 5.0) rem= 5.0;
187                 else rem= 10.0;
188                 
189                 *step= rem*pow(10.0, (float)*macht);
190                 
191                 (*macht)++;
192                 if(rem==10.0) (*macht)++;       // prevents printing 1.0 2.0 3.0 etc
193         }
194 }
195
196 void calc_ipogrid()
197 {
198         float space, pixels, secondiv=1.0;
199         int secondgrid= 0;
200         /* rule: gridstep is minimal IPOSTEP pixels */
201         /* how large is IPOSTEP pixels? */
202         
203         if(G.v2d==0) return;
204         
205         /* detect of we have seconds or frames, should become argument */
206
207         switch(curarea->spacetype) {
208         case SPACE_TIME: {
209                 SpaceTime *stime= curarea->spacedata.first;
210                 if(!(stime->flag & TIME_DRAWFRAMES)) {
211                         secondgrid= 1;
212                         secondiv= 0.01 * (float)G.scene->r.frs_sec;
213                 }
214                 break;
215         }
216         case SPACE_SEQ: {
217                 SpaceSeq * sseq = curarea->spacedata.first;
218                 if (!(sseq->flag & SEQ_DRAWFRAMES)) {
219                         secondgrid = 1;
220                         secondiv = 0.01 * (float)G.scene->r.frs_sec;
221                 }
222         }
223         default:
224                 break;
225         }
226         
227         space= G.v2d->cur.xmax - G.v2d->cur.xmin;
228         pixels= G.v2d->mask.xmax-G.v2d->mask.xmin;
229         
230         ipogrid_dx= IPOSTEP*space/(secondiv*pixels);
231         step_to_grid(&ipogrid_dx, &ipomachtx);
232         ipogrid_dx*= secondiv;
233         
234         if ELEM3(curarea->spacetype, SPACE_SEQ, SPACE_SOUND, SPACE_TIME) {
235                 if(ipogrid_dx < 0.1) ipogrid_dx= 0.1;
236                 ipomachtx-= 2;
237                 if(ipomachtx<-2) ipomachtx= -2;
238         }
239         
240         space= (G.v2d->cur.ymax - G.v2d->cur.ymin);
241         pixels= curarea->winy;
242         ipogrid_dy= IPOSTEP*space/pixels;
243         step_to_grid(&ipogrid_dy, &ipomachty);
244         
245         if ELEM3(curarea->spacetype, SPACE_SEQ, SPACE_SOUND, SPACE_TIME) {
246                 if(ipogrid_dy < 1.0) ipogrid_dy= 1.0;
247                 if(ipomachty<1) ipomachty= 1;
248         }
249         
250         ipogrid_startx= secondiv*(G.v2d->cur.xmin/secondiv - fmod(G.v2d->cur.xmin/secondiv, ipogrid_dx/secondiv));
251         if(G.v2d->cur.xmin<0.0) ipogrid_startx-= ipogrid_dx;
252         
253         ipogrid_starty= (G.v2d->cur.ymin-fmod(G.v2d->cur.ymin, ipogrid_dy));
254         if(G.v2d->cur.ymin<0.0) ipogrid_starty-= ipogrid_dy;
255         
256 }
257
258 void draw_ipogrid(void)
259 {
260         float vec1[2], vec2[2];
261         int a, step;
262         
263         vec1[0]= vec2[0]= ipogrid_startx;
264         vec1[1]= ipogrid_starty;
265         vec2[1]= G.v2d->cur.ymax;
266         
267         step= (G.v2d->mask.xmax-G.v2d->mask.xmin+1)/IPOSTEP;
268         
269         BIF_ThemeColor(TH_GRID);
270         
271         for(a=0; a<step; a++) {
272                 glBegin(GL_LINE_STRIP);
273                 glVertex2fv(vec1); glVertex2fv(vec2);
274                 glEnd();
275                 vec2[0]= vec1[0]+= ipogrid_dx;
276         }
277         
278         vec2[0]= vec1[0]-= 0.5*ipogrid_dx;
279         
280         BIF_ThemeColorShade(TH_GRID, 16);
281         
282         step++;
283         for(a=0; a<=step; a++) {
284                 glBegin(GL_LINE_STRIP);
285                 glVertex2fv(vec1); glVertex2fv(vec2);
286                 glEnd();
287                 vec2[0]= vec1[0]-= ipogrid_dx;
288         }
289         
290         if ELEM4(curarea->spacetype, SPACE_SOUND, SPACE_ACTION, SPACE_NLA, SPACE_TIME);
291         else {
292                 vec1[0]= ipogrid_startx;
293                 vec1[1]= vec2[1]= ipogrid_starty;
294                 vec2[0]= G.v2d->cur.xmax;
295                 
296                 step= (curarea->winy+1)/IPOSTEP;
297                 
298                 BIF_ThemeColor(TH_GRID);
299                 for(a=0; a<=step; a++) {
300                         glBegin(GL_LINE_STRIP);
301                         glVertex2fv(vec1); glVertex2fv(vec2);
302                         glEnd();
303                         vec2[1]= vec1[1]+= ipogrid_dy;
304                 }
305                 vec2[1]= vec1[1]-= 0.5*ipogrid_dy;
306                 step++;
307                 
308                 if(curarea->spacetype==SPACE_IPO) {
309                         BIF_ThemeColorShade(TH_GRID, 16);
310                         for(a=0; a<step; a++) {
311                                 glBegin(GL_LINE_STRIP);
312                                 glVertex2fv(vec1); glVertex2fv(vec2);
313                                 glEnd();
314                                 vec2[1]= vec1[1]-= ipogrid_dy;
315                         }
316                 }
317         }
318         
319         BIF_ThemeColorShade(TH_GRID, -50);
320         
321         if (curarea->spacetype!=SPACE_ACTION && curarea->spacetype!=SPACE_NLA)
322         {       /* Horizontal axis */
323                 vec1[0]= G.v2d->cur.xmin;
324                 vec2[0]= G.v2d->cur.xmax;
325                 vec1[1]= vec2[1]= 0.0;
326                 glBegin(GL_LINE_STRIP);
327                 
328                 glVertex2fv(vec1);
329                 glVertex2fv(vec2);
330                 
331                 glEnd();
332         }
333         
334         /* Vertical axis */
335         
336         vec1[1]= G.v2d->cur.ymin;
337         vec2[1]= G.v2d->cur.ymax;
338         vec1[0]= vec2[0]= 0.0;
339         glBegin(GL_LINE_STRIP);
340         glVertex2fv(vec1); glVertex2fv(vec2);
341         glEnd();
342         
343         /* Limits box */
344         if(curarea->spacetype==SPACE_IPO) {
345                 if(G.sipo->blocktype==ID_SEQ) {
346                         Sequence * last_seq = get_last_seq();
347                         float start = 0.0;
348                         float end = 100.0;
349
350                         if (last_seq && 
351                             ((last_seq->flag & SEQ_IPO_FRAME_LOCKED) != 0)) {
352                                 start = last_seq->startdisp;
353                                 end = last_seq->enddisp;
354                         }
355
356                         glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); 
357                         glRectf(start,  0.0,  end,  1.0); 
358                         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
359                 }
360                 else if(ELEM(G.sipo->blocktype, ID_CU, ID_CO)) {
361                         glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); 
362                         glRectf(0.0,  1.0,  G.v2d->cur.xmax,  1.0); 
363                         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
364                 }
365         }
366 }
367
368 void areamouseco_to_ipoco(View2D *v2d, short *mval, float *x, float *y)
369 {
370         float div, ofs;
371         
372         div= v2d->mask.xmax-v2d->mask.xmin;
373         ofs= v2d->mask.xmin;
374         
375         *x= v2d->cur.xmin+ (v2d->cur.xmax-v2d->cur.xmin)*(mval[0]-ofs)/div;
376         
377         div= v2d->mask.ymax-v2d->mask.ymin;
378         ofs= v2d->mask.ymin;
379         
380         *y= v2d->cur.ymin+ (v2d->cur.ymax-v2d->cur.ymin)*(mval[1]-ofs)/div;
381 }
382
383 void ipoco_to_areaco(View2D *v2d, float *vec, short *mval)
384 {
385         float x, y;
386         
387         mval[0]= IS_CLIPPED;
388         
389         x= (vec[0] - v2d->cur.xmin)/(v2d->cur.xmax-v2d->cur.xmin);
390         y= (vec[1] - v2d->cur.ymin)/(v2d->cur.ymax-v2d->cur.ymin);
391
392         if(x>=0.0 && x<=1.0) {
393                 if(y>=0.0 && y<=1.0) {
394                         mval[0]= v2d->mask.xmin + x*(v2d->mask.xmax-v2d->mask.xmin);
395                         mval[1]= v2d->mask.ymin + y*(v2d->mask.ymax-v2d->mask.ymin);
396                 }
397         }
398 }
399
400 void ipoco_to_areaco_noclip(View2D *v2d, float *vec, short *mval)
401 {
402         float x, y;
403         
404         x= (vec[0] - v2d->cur.xmin)/(v2d->cur.xmax-v2d->cur.xmin);
405         y= (vec[1] - v2d->cur.ymin)/(v2d->cur.ymax-v2d->cur.ymin);
406         
407         x= v2d->mask.xmin + x*(v2d->mask.xmax-v2d->mask.xmin);
408         y= v2d->mask.ymin + y*(v2d->mask.ymax-v2d->mask.ymin);
409         
410         if(x<-32760) mval[0]= -32760;
411         else if(x>32760) mval[0]= 32760;
412         else mval[0]= x;
413         
414         if(y<-32760) mval[1]= -32760;
415         else if(y>32760) mval[1]= 32760;
416         else mval[1]= y;
417 }
418
419 int in_ipo_buttons(void)
420 {
421         short mval[2];
422         
423         getmouseco_areawin(mval);
424         
425         if(mval[0]< G.v2d->mask.xmax) return 0;
426         else return 1;
427 }
428
429 static View2D *spacelink_get_view2d(SpaceLink *sl)
430 {
431         if(sl->spacetype==SPACE_IPO) 
432                 return &((SpaceIpo *)sl)->v2d;
433         else if(sl->spacetype==SPACE_SOUND) 
434                 return &((SpaceSound *)sl)->v2d;
435         if(sl->spacetype==SPACE_ACTION) 
436                 return &((SpaceAction *)sl)->v2d;
437         if(sl->spacetype==SPACE_NLA) 
438                 return &((SpaceNla *)sl)->v2d;
439         if(sl->spacetype==SPACE_TIME) 
440                 return &((SpaceTime *)sl)->v2d;
441         if(sl->spacetype==SPACE_SEQ)
442                 return &((SpaceSeq *)sl)->v2d;
443         return NULL;
444 }
445
446 /* copies changes in this view from or to all 2d views with lock option open */
447 /* do not call this inside of drawing routines, to prevent eternal loops */
448 void view2d_do_locks(ScrArea *cursa, int flag)
449 {
450         ScrArea *sa;
451         View2D *v2d, *curv2d;
452         SpaceLink *sl;
453         
454         curv2d= spacelink_get_view2d(cursa->spacedata.first);
455         if(curv2d==NULL) return;
456         if((curv2d->flag & V2D_VIEWLOCK)==0) return;
457
458         for(sa= G.curscreen->areabase.first; sa; sa= sa->next) {
459                 if(sa!=cursa) {
460                         for(sl= sa->spacedata.first; sl; sl= sl->next) {
461                                 
462                                 v2d= spacelink_get_view2d(sl);
463                                 if(v2d) {
464                                         if(v2d->flag & V2D_VIEWLOCK) {
465                                                 if(flag & V2D_LOCK_COPY) {
466                                                         v2d->cur.xmin= curv2d->cur.xmin;
467                                                         v2d->cur.xmax= curv2d->cur.xmax;
468                                                 }
469                                                 else {
470                                                         curv2d->cur.xmin= v2d->cur.xmin;
471                                                         curv2d->cur.xmax= v2d->cur.xmax;
472                                                         scrarea_queue_winredraw(sa);
473                                                 }
474                                                 
475                                                 if(flag & V2D_LOCK_REDRAW) {
476                                                         if(sl == sa->spacedata.first)
477                                                                 scrarea_do_windraw(sa);
478                                                 }
479                                                 else
480                                                         scrarea_queue_winredraw(sa);
481                                         }
482                                 }
483                         }
484                 }
485         }
486 }
487
488 /* event based, note: curarea is in here... */
489 void view2d_zoom(View2D *v2d, float factor, int winx, int winy) 
490 {
491         float dx= factor*(v2d->cur.xmax-v2d->cur.xmin);
492         float dy= factor*(v2d->cur.ymax-v2d->cur.ymin);
493         if ((v2d->keepzoom & V2D_LOCKZOOM_X)==0) {
494                 v2d->cur.xmin+= dx;
495                 v2d->cur.xmax-= dx;
496         }
497         if ((v2d->keepzoom & V2D_LOCKZOOM_Y)==0) {
498                 v2d->cur.ymin+= dy;
499                 v2d->cur.ymax-= dy;
500         }
501         test_view2d(v2d, winx, winy);
502         view2d_do_locks(curarea, V2D_LOCK_COPY);
503 }
504
505
506 void test_view2d(View2D *v2d, int winx, int winy)
507 {
508         /* cur is not allowed to be larger than max, smaller than min, or outside of tot */
509         rctf *cur, *tot;
510         float dx, dy, temp, fac, zoom;
511         
512         /* correct winx for scroll */
513         if(v2d->scroll & L_SCROLL) winx-= SCROLLB;
514         if(v2d->scroll & B_SCROLL) winy-= SCROLLH;
515         
516         /* header completely closed window */
517         if(winy<=0) return;
518         
519         cur= &v2d->cur;
520         tot= &v2d->tot;
521         
522         dx= cur->xmax-cur->xmin;
523         dy= cur->ymax-cur->ymin;
524
525         /* Reevan's test */
526         if (v2d->keepzoom & V2D_LOCKZOOM_Y)
527                 v2d->cur.ymax=v2d->cur.ymin+((float)winy);
528
529         if (v2d->keepzoom & V2D_LOCKZOOM_X)
530                 v2d->cur.xmax=v2d->cur.xmin+((float)winx);
531
532         if(v2d->keepzoom) {
533                 
534                 zoom= ((float)winx)/dx;
535                 
536                 if(zoom<v2d->minzoom || zoom>v2d->maxzoom) {
537                         if(zoom<v2d->minzoom) fac= zoom/v2d->minzoom;
538                         else fac= zoom/v2d->maxzoom;
539                         
540                         dx*= fac;
541                         temp= 0.5*(cur->xmax+cur->xmin);
542                         
543                         cur->xmin= temp-0.5*dx;
544                         cur->xmax= temp+0.5*dx;
545                 }
546                 
547                 zoom= ((float)winy)/dy;
548                 
549                 if(zoom<v2d->minzoom || zoom>v2d->maxzoom) {
550                         if(zoom<v2d->minzoom) fac= zoom/v2d->minzoom;
551                         else fac= zoom/v2d->maxzoom;
552                         
553                         dy*= fac;
554                         temp= 0.5*(cur->ymax+cur->ymin);
555                         cur->ymin= temp-0.5*dy;
556                         cur->ymax= temp+0.5*dy;
557                 }
558         }
559         else {
560                 if(dx<G.v2d->min[0]) {
561                         dx= G.v2d->min[0];
562                         temp= 0.5*(cur->xmax+cur->xmin);
563                         cur->xmin= temp-0.5*dx;
564                         cur->xmax= temp+0.5*dx;
565                 }
566                 else if(dx>G.v2d->max[0]) {
567                         dx= G.v2d->max[0];
568                         temp= 0.5*(cur->xmax+cur->xmin);
569                         cur->xmin= temp-0.5*dx;
570                         cur->xmax= temp+0.5*dx;
571                 }
572                 
573                 if(dy<G.v2d->min[1]) {
574                         dy= G.v2d->min[1];
575                         temp= 0.5*(cur->ymax+cur->ymin);
576                         cur->ymin= temp-0.5*dy;
577                         cur->ymax= temp+0.5*dy;
578                 }
579                 else if(dy>G.v2d->max[1]) {
580                         dy= G.v2d->max[1];
581                         temp= 0.5*(cur->ymax+cur->ymin);
582                         cur->ymin= temp-0.5*dy;
583                         cur->ymax= temp+0.5*dy;
584                 }
585         }
586
587         if(v2d->keepaspect) {
588                 short do_x=0, do_y=0;
589                 
590                 /* when a window edge changes, the aspect ratio can't be used to
591                    find which is the best new 'cur' rect. thats why it stores 'old' */
592                 if(winx!=v2d->oldwinx) do_x= 1;
593                 if(winy!=v2d->oldwiny) do_y= 1;
594                 
595                 dx= (cur->ymax-cur->ymin)/(cur->xmax-cur->xmin);
596                 dy= ((float)winy)/((float)winx);
597                 
598                 if(do_x==do_y) {        // both sizes change, ctrl+uparrow
599                         if(do_x==1 && do_y==1) {
600                                 if( ABS(winx-v2d->oldwinx)>ABS(winy-v2d->oldwiny)) do_y= 0;
601                                 else do_x= 0;
602                         }
603                         else if( dy > 1.0) do_x= 0; else do_x= 1;
604                 }
605                 
606                 v2d->oldwinx= winx; 
607                 v2d->oldwiny= winy;
608                 
609                 if( do_x ) {
610                         
611                         /* portrait window: correct for x */
612                         dx= cur->ymax-cur->ymin;
613                         temp= (cur->xmax+cur->xmin);
614                         
615                         cur->xmin= temp/2.0 - 0.5*dx/dy;
616                         cur->xmax= temp/2.0 + 0.5*dx/dy;
617                 }
618                 else {
619                         dx= cur->xmax-cur->xmin;
620                         temp= (cur->ymax+cur->ymin);
621                         
622                         cur->ymin= temp/2.0 - 0.5*dy*dx;
623                         cur->ymax= temp/2.0 + 0.5*dy*dx;
624                 }
625         }
626         
627         if(v2d->keeptot) {
628                 dx= cur->xmax-cur->xmin;
629                 dy= cur->ymax-cur->ymin;
630                 
631                 if(dx > tot->xmax-tot->xmin) {
632                         if(v2d->keepzoom==0) {
633                                 if(cur->xmin<tot->xmin) cur->xmin= tot->xmin;
634                                 if(cur->xmax>tot->xmax) cur->xmax= tot->xmax;
635                         }
636                         else {
637                                 if(cur->xmax < tot->xmax) {
638                                         dx= tot->xmax-cur->xmax;
639                                         cur->xmin+= dx;
640                                         cur->xmax+= dx;
641                                 }
642                                 else if(cur->xmin > tot->xmin) {
643                                         dx= cur->xmin-tot->xmin;
644                                         cur->xmin-= dx;
645                                         cur->xmax-= dx;
646                                 }
647                         }
648                 }
649                 else {
650                         if(cur->xmin < tot->xmin) {
651                                 dx= tot->xmin-cur->xmin;
652                                 cur->xmin+= dx;
653                                 cur->xmax+= dx;
654                         }
655                         else if(cur->xmax > tot->xmax) {
656                                 dx= cur->xmax-tot->xmax;
657                                 cur->xmin-= dx;
658                                 cur->xmax-= dx;
659                         }
660                 }
661                 
662                 if(dy > tot->ymax-tot->ymin) {
663                         if(v2d->keepzoom==0) {
664                                 if(cur->ymin<tot->ymin) cur->ymin= tot->ymin;
665                                 if(cur->ymax>tot->ymax) cur->ymax= tot->ymax;
666                         }
667                         else {
668                                 if(cur->ymax < tot->ymax) {
669                                         dy= tot->ymax-cur->ymax;
670                                         cur->ymin+= dy;
671                                         cur->ymax+= dy;
672                                 }
673                                 else if(cur->ymin > tot->ymin) {
674                                         dy= cur->ymin-tot->ymin;
675                                         cur->ymin-= dy;
676                                         cur->ymax-= dy;
677                                 }
678                         }
679                 }
680                 else {
681                         if(cur->ymin < tot->ymin) {
682                                 dy= tot->ymin-cur->ymin;
683                                 cur->ymin+= dy;
684                                 cur->ymax+= dy;
685                         }
686                         else if(cur->ymax > tot->ymax) {
687                                 dy= cur->ymax-tot->ymax;
688                                 cur->ymin-= dy;
689                                 cur->ymax-= dy;
690                         }
691                 }
692         }
693 }
694
695 #define IPOBUTX 70
696 static int calc_ipobuttonswidth(ScrArea *sa)
697 {
698         SpaceIpo *sipo= sa->spacedata.first;
699         EditIpo *ei;
700         int ipowidth = IPOBUTX;
701         int a;
702         
703         /* default width when no space ipo or no channels */
704         if (sipo == NULL) return IPOBUTX;
705         if ((sipo->totipo==0) || (sipo->editipo==NULL)) return IPOBUTX;
706
707         ei= sipo->editipo;
708         
709         for(a=0; a<sipo->totipo; a++, ei++) {
710                 if (BMF_GetStringWidth(G.font, ei->name) + 18 > ipowidth) 
711                         ipowidth = BMF_GetStringWidth(G.font, ei->name) + 18;
712         }
713         return ipowidth;
714
715 }
716
717 void calc_scrollrcts(ScrArea *sa, View2D *v2d, int winx, int winy)
718 {
719         v2d->mask.xmin= v2d->mask.ymin= 0;
720         v2d->mask.xmax= winx;
721         v2d->mask.ymax= winy;
722         
723         if(sa->spacetype==SPACE_ACTION) {
724                 if(sa->winx > ACTWIDTH+50) { 
725                         v2d->mask.xmin+= ACTWIDTH;
726                         v2d->hor.xmin+=ACTWIDTH;
727                 }
728         }
729         else if(sa->spacetype==SPACE_NLA){
730                 if(sa->winx > NLAWIDTH+50) { 
731                         v2d->mask.xmin+= NLAWIDTH;
732                         v2d->hor.xmin+=NLAWIDTH;
733                 }
734         }
735         else if(sa->spacetype==SPACE_IPO) {
736                 int ipobutx = calc_ipobuttonswidth(sa);
737                 
738                 v2d->mask.xmax-= ipobutx;
739                 
740                 if(v2d->mask.xmax<ipobutx)
741                         v2d->mask.xmax= winx;
742         }
743         
744         if(v2d->scroll) {
745                 if(v2d->scroll & L_SCROLL) {
746                         v2d->vert= v2d->mask;
747                         v2d->vert.xmax= SCROLLB;
748                         v2d->mask.xmin= SCROLLB;
749                 }
750                 else if(v2d->scroll & R_SCROLL) {
751                         v2d->vert= v2d->mask;
752                         v2d->vert.xmin= v2d->vert.xmax-SCROLLB;
753                         v2d->mask.xmax= v2d->vert.xmin;
754                 }
755                 
756                 if(v2d->scroll & B_SCROLL) {
757                         v2d->hor= v2d->mask;
758                         v2d->hor.ymax= SCROLLH;
759                         v2d->mask.ymin= SCROLLH;
760                 }
761                 else if(v2d->scroll & T_SCROLL) {
762                         v2d->hor= v2d->mask;
763                         v2d->hor.ymin= v2d->hor.ymax-SCROLLH;
764                         v2d->mask.ymax= v2d->hor.ymin;
765                 }
766         }
767 }
768
769         /* draws a line in left vertical scrollbar at the given height */
770 static void draw_solution_line(View2D *v2d, float h)
771 {
772         float vec[2];
773         short mval[2];
774
775         vec[0]= v2d->cur.xmin;
776         vec[1]= h;
777         ipoco_to_areaco(v2d, vec, mval);
778         if(mval[0]!=IS_CLIPPED) {
779                 glBegin(GL_LINES);
780                 glVertex2f(v2d->vert.xmin, mval[1]);
781                 glVertex2f(v2d->vert.xmax, mval[1]);
782                 glEnd();
783         }
784 }
785
786 static void draw_solution(SpaceIpo *sipo)
787 {
788         View2D *v2d= &sipo->v2d;
789         EditIpo *ei;
790         int a;
791
792         if (!(v2d->scroll & VERT_SCROLL)) return;
793
794         ei= sipo->editipo;
795         for(a=0; a<sipo->totipo; a++, ei++) {
796                 if ISPOIN(ei, flag & IPO_VISIBLE, icu) {
797                         cpack(ei->col);
798                                 
799                                 /* DISPBITS ipos have 'multiple' values. */
800                         if(ei->disptype==IPO_DISPBITS) {
801                                 int b, val= ei->icu->curval;
802                                         
803                                 for (b=0; b<31; b++)
804                                         if (val & (1<<b))
805                                                 draw_solution_line(v2d, b+1);
806                         } else {
807                                 draw_solution_line(v2d, ei->icu->curval);
808                         }
809                 }
810         }
811 }
812
813 /* used for drawing timeline */
814 void draw_view2d_numbers_horiz(int drawframes)
815 {
816         float fac, fac2, dfac, val;
817         
818         /* the numbers: convert ipogrid_startx and -dx to scroll coordinates */
819         
820         fac= (ipogrid_startx- G.v2d->cur.xmin)/(G.v2d->cur.xmax-G.v2d->cur.xmin);
821         fac= G.v2d->mask.xmin+fac*(G.v2d->mask.xmax-G.v2d->mask.xmin);
822         
823         dfac= (ipogrid_dx)/(G.v2d->cur.xmax-G.v2d->cur.xmin);
824         dfac= dfac*(G.v2d->mask.xmax-G.v2d->mask.xmin);
825         
826         BIF_ThemeColor(TH_TEXT);
827         val= ipogrid_startx;
828         while(fac < G.v2d->mask.xmax) {
829                 
830                 if(drawframes) {
831                         ipomachtx= 1;
832                         scroll_prstr(fac, 2.0+(float)(G.v2d->mask.ymin), val, 'h', 0);
833                 }
834                 else {
835                         fac2= val/(float)G.scene->r.frs_sec;
836                         scroll_prstr(fac, 2.0+(float)(G.v2d->mask.ymin), fac2, 'h', 0);
837                 }
838                 
839                 fac+= dfac;
840                 val+= ipogrid_dx;
841         }
842 }
843
844
845 void drawscroll(int disptype)
846 {
847         rcti vert, hor;
848         float fac, dfac, val, fac2, tim;
849         int darker, dark, light, lighter;
850         
851         vert= (G.v2d->vert);
852         hor= (G.v2d->hor);
853         
854         darker= -40;
855         dark= 0;
856         light= 20;
857         lighter= 50;
858         
859         if(G.v2d->scroll & HOR_SCROLL) {
860                 
861                 BIF_ThemeColorShade(TH_SHADE1, light);
862                 glRecti(hor.xmin,  hor.ymin,  hor.xmax,  hor.ymax);
863                 
864                 /* slider */
865                 fac= (G.v2d->cur.xmin- G.v2d->tot.xmin)/(G.v2d->tot.xmax-G.v2d->tot.xmin);
866                 if(fac<0.0) fac= 0.0;
867                 horxmin= hor.xmin+fac*(hor.xmax-hor.xmin);
868                 
869                 fac= (G.v2d->cur.xmax- G.v2d->tot.xmin)/(G.v2d->tot.xmax-G.v2d->tot.xmin);
870                 if(fac>1.0) fac= 1.0;
871                 horxmax= hor.xmin+fac*(hor.xmax-hor.xmin);
872                 
873                 if(horxmin > horxmax) horxmin= horxmax;
874                 
875                 BIF_ThemeColorShade(TH_SHADE1, dark);
876                 glRecti(horxmin,  hor.ymin,  horxmax,  hor.ymax);
877
878                 /* decoration bright line */
879                 BIF_ThemeColorShade(TH_SHADE1, lighter);
880                 sdrawline(hor.xmin, hor.ymax, hor.xmax, hor.ymax);
881
882                 /* the numbers: convert ipogrid_startx and -dx to scroll coordinates */
883                 fac= (ipogrid_startx- G.v2d->cur.xmin)/(G.v2d->cur.xmax-G.v2d->cur.xmin);
884                 fac= hor.xmin+fac*(hor.xmax-hor.xmin);
885                 
886                 dfac= (ipogrid_dx)/(G.v2d->cur.xmax-G.v2d->cur.xmin);
887                 dfac= dfac*(hor.xmax-hor.xmin);
888                 
889                 BIF_ThemeColor(TH_TEXT);
890                 val= ipogrid_startx;
891                 while(fac < hor.xmax) {
892                         
893                         if(curarea->spacetype==SPACE_OOPS);
894                         else if(curarea->spacetype==SPACE_SEQ) {
895                                 SpaceSeq * sseq = curarea->spacedata.first;
896                                 if (sseq->flag & SEQ_DRAWFRAMES) {
897                                         ipomachtx = 1;
898                                         scroll_prstr(fac, 3.0+(float)(hor.ymin), val, 'h', disptype);
899                                 } else {
900                                         fac2= val/(float)G.scene->r.frs_sec;
901                                         tim= floor(fac2);
902                                         fac2= fac2-tim;
903                                         scroll_prstr(fac, 3.0+(float)(hor.ymin), tim+G.scene->r.frs_sec*fac2/100.0, 'h', disptype);
904                                 }
905                         }
906                         else if (curarea->spacetype==SPACE_SOUND) {
907                                 SpaceSound *ssound= curarea->spacedata.first;
908                                 
909                                 if(ssound->flag & SND_DRAWFRAMES) {
910                                         ipomachtx= 1;
911                                         scroll_prstr(fac, 3.0+(float)(hor.ymin), val, 'h', disptype);
912                                 }
913                                 else {
914                                         fac2= val/(float)G.scene->r.frs_sec;
915                                         scroll_prstr(fac, 3.0+(float)(hor.ymin), fac2, 'h', disptype);
916                                 }
917                         }
918                         else if (curarea->spacetype==SPACE_TIME) {
919                                 SpaceTime *stime= curarea->spacedata.first;
920                                 
921                                 if(stime->flag & TIME_DRAWFRAMES) {
922                                         ipomachtx= 1;
923                                         scroll_prstr(fac, 3.0+(float)(hor.ymin), val, 'h', disptype);
924                                 }
925                                 else {
926                                         fac2= val/(float)G.scene->r.frs_sec;
927                                         scroll_prstr(fac, 3.0+(float)(hor.ymin), fac2, 'h', disptype);
928                                 }
929                         }
930                         else if (curarea->spacetype==SPACE_IPO) {
931                                 EditIpo *ei= get_active_editipo();
932                                 
933                                 if(ei && ei->icu && ei->icu->driver) {
934                                         int adrcode= ei->icu->driver->adrcode;
935                                         
936                                         if(adrcode==OB_ROT_X || adrcode==OB_ROT_Y || adrcode==OB_ROT_Z) {
937                                                 scroll_prstr(fac, 3.0+(float)(hor.ymin), val, 'v', IPO_DISPDEGR);
938                                         }
939                                         else 
940                                                 scroll_prstr(fac, 3.0+(float)(hor.ymin), val, 'h', disptype);
941                                 }
942                                 else 
943                                         scroll_prstr(fac, 3.0+(float)(hor.ymin), val, 'h', disptype);
944                         }
945                         else {
946                                 scroll_prstr(fac, 3.0+(float)(hor.ymin), val, 'h', disptype);
947                         }
948                         
949                         fac+= dfac;
950                         val+= ipogrid_dx;
951                 }
952         }
953         
954         if(G.v2d->scroll & VERT_SCROLL) {
955                 BIF_ThemeColorShade(TH_SHADE1, light);
956                 glRecti(vert.xmin,  vert.ymin,  vert.xmax,  vert.ymax);
957                 
958                 /* slider */
959                 fac= (G.v2d->cur.ymin- G.v2d->tot.ymin)/(G.v2d->tot.ymax-G.v2d->tot.ymin);
960                 if(fac<0.0) fac= 0.0;
961                 vertymin= vert.ymin+fac*(vert.ymax-vert.ymin);
962                 
963                 fac= (G.v2d->cur.ymax- G.v2d->tot.ymin)/(G.v2d->tot.ymax-G.v2d->tot.ymin);
964                 if(fac>1.0) fac= 1.0;
965                 vertymax= vert.ymin+fac*(vert.ymax-vert.ymin);
966                 
967                 if(vertymin > vertymax) vertymin= vertymax;
968                 
969                 BIF_ThemeColorShade(TH_SHADE1, dark);
970                 glRecti(vert.xmin,  vertymin,  vert.xmax,  vertymax);
971
972                 /* decoration black line */
973                 BIF_ThemeColorShade(TH_SHADE1, darker);
974                 if(G.v2d->scroll & HOR_SCROLL) 
975                         sdrawline(vert.xmax, vert.ymin+SCROLLH, vert.xmax, vert.ymax);
976                 else 
977                         sdrawline(vert.xmax, vert.ymin, vert.xmax, vert.ymax);
978
979                 /* the numbers: convert ipogrid_starty and -dy to scroll coordinates */
980                 fac= (ipogrid_starty- G.v2d->cur.ymin)/(G.v2d->cur.ymax-G.v2d->cur.ymin);
981                 fac= vert.ymin+SCROLLH+fac*(vert.ymax-vert.ymin-SCROLLH);
982                 
983                 dfac= (ipogrid_dy)/(G.v2d->cur.ymax-G.v2d->cur.ymin);
984                 dfac= dfac*(vert.ymax-vert.ymin-SCROLLH);
985                 
986                 if(curarea->spacetype==SPACE_OOPS);
987                 else if(curarea->spacetype==SPACE_SEQ) {
988                         BIF_ThemeColor(TH_TEXT);
989                         val= ipogrid_starty;
990                         fac+= 0.5*dfac;
991                         while(fac < vert.ymax) {
992                                 scroll_prstr((float)(vert.xmax)-14.0, fac, val, 'v', disptype);
993                                 fac+= dfac;
994                                 val+= ipogrid_dy;
995                         }
996                 }
997                 else if (curarea->spacetype==SPACE_NLA){
998                 }
999                 else if (curarea->spacetype==SPACE_ACTION){
1000                         /* No digits on vertical axis in action mode! */
1001                 }
1002                 else {
1003                         BIF_ThemeColor(TH_TEXT);
1004                         val= ipogrid_starty;
1005                         while(fac < vert.ymax) {
1006                                 scroll_prstr((float)(vert.xmax)-14.0, fac, val, 'v', disptype);
1007                                 fac+= dfac;
1008                                 val+= ipogrid_dy;
1009                         }
1010                 }
1011         }
1012 }
1013
1014 static void draw_ipobuts(SpaceIpo *sipo)
1015 {
1016         ScrArea *area= sipo->area;
1017         View2D *v2d= &sipo->v2d;
1018         Object *ob= OBACT;
1019         uiBlock *block;
1020         uiBut *but;
1021         EditIpo *ei;
1022         int a, y, sel, tot, ipobutx;
1023         char naam[20];
1024         
1025         if(area->winx< calc_ipobuttonswidth(area)) return;
1026         
1027         if(sipo->butofs) {
1028                 tot= 30+IPOBUTY*sipo->totipo;
1029                 if(tot<area->winy) sipo->butofs= 0;
1030         }
1031         
1032         ipobutx = calc_ipobuttonswidth(area);
1033         
1034         BIF_ThemeColor(TH_SHADE2);
1035         glRects(v2d->mask.xmax,  0,  area->winx,  area->winy);
1036         
1037         cpack(0x0);
1038         sdrawline(v2d->mask.xmax, 0, v2d->mask.xmax, area->winy);
1039
1040         if(sipo->totipo==0) return;
1041         if(sipo->editipo==0) return;
1042         
1043         sprintf(naam, "ipowin %d", area->win);
1044         block= uiNewBlock(&area->uiblocks, naam, UI_EMBOSSN, UI_HELV, area->win);
1045
1046         ei= sipo->editipo;
1047         y= area->winy-30+sipo->butofs;
1048         
1049         if(ob && sipo->blocktype==ID_KE) {
1050                 int icon;
1051                 if(ob->shapeflag & OB_SHAPE_LOCK) icon= ICON_PIN_HLT; else icon= ICON_PIN_DEHLT;
1052                 uiDefIconButBitC(block, TOG, OB_SHAPE_LOCK, B_SETKEY, icon, 
1053                                                  v2d->mask.xmax+18,y,25,20, &ob->shapeflag, 0, 0, 0, 0, "Always show the current Shape for this Object");
1054                 y-= IPOBUTY;
1055         }
1056         
1057         for(a=0; a<sipo->totipo; a++, ei++, y-=IPOBUTY) {
1058                 // this button defines visiblity, bit zero of flag (IPO_VISIBLE)
1059                 but= uiDefButBitS(block, TOG, IPO_VISIBLE, a+1, ei->name,  v2d->mask.xmax+18, y, ipobutx-15, IPOBUTY-1, &(ei->flag), 0, 0, 0, 0, "");
1060                 // no hilite, its not visible, but most of all the winmatrix is not correct later on...
1061                 uiButSetFlag(but, UI_TEXT_LEFT|UI_NO_HILITE);
1062                 
1063                 // this fake button defines selection of curves
1064                 if(ei->icu) {
1065                         cpack(ei->col);
1066                         
1067                         glRects(v2d->mask.xmax+8,  y+2,  v2d->mask.xmax+15, y+IPOBUTY-2);
1068                         sel= ei->flag & (IPO_SELECT + IPO_EDIT);
1069                         
1070                         uiEmboss((float)(v2d->mask.xmax+8), (float)(y+2), (float)(v2d->mask.xmax+15), (float)(y+IPOBUTY-2), sel);
1071                         
1072                         if(ei->icu->driver) {
1073                                 cpack(0x0);
1074                                 fdrawbox((float)v2d->mask.xmax+11,  (float)y+8,  (float)v2d->mask.xmax+12.5, (float)y+9.5);
1075                         }
1076                 }
1077                 
1078                 if(ei->flag & IPO_ACTIVE) {
1079                         cpack(0x0);
1080                         fdrawbox(v2d->mask.xmax+7,  y+1,  v2d->mask.xmax+16, y+IPOBUTY-1);
1081                 }
1082         }
1083         uiDrawBlock(block);
1084 }
1085
1086 static void draw_ipovertices(int sel)
1087 {
1088         EditIpo *ei;
1089         BezTriple *bezt;
1090         float v1[2];
1091         int val, ok, nr, a, b;
1092         
1093         if(G.f & G_PICKSEL) return;
1094         
1095         glPointSize(3.0);
1096         
1097         ei= G.sipo->editipo;
1098         for(nr=0; nr<G.sipo->totipo; nr++, ei++) {
1099                 if ISPOIN(ei, flag & IPO_VISIBLE, icu) {
1100                         
1101                         if(G.sipo->showkey) {
1102                                 if(sel) BIF_ThemeColor(TH_TEXT_HI);
1103                                  else BIF_ThemeColor(TH_TEXT);
1104                         } else if(ei->flag & IPO_EDIT) {
1105                                 if(sel) BIF_ThemeColor(TH_VERTEX_SELECT); 
1106                                 else BIF_ThemeColor(TH_VERTEX);
1107                         } else {
1108                                 if(sel) BIF_ThemeColor(TH_TEXT_HI);
1109                                  else BIF_ThemeColor(TH_TEXT);
1110                                  
1111                                 val= (ei->icu->flag & IPO_SELECT)!=0;
1112                                 if(sel != val) continue;
1113                         }
1114
1115                         /* We can't change the color in the middle of
1116                          * GL_POINTS because then Blender will segfault
1117                          * on TNT2 / Linux with NVidia's drivers
1118                          * (at least up to ver. 4349) */                
1119                         
1120                         bglBegin(GL_POINTS);
1121                         
1122                         bezt= ei->icu->bezt;
1123                         a= ei->icu->totvert;
1124                         while(a--) {                            
1125                                 
1126                                 if(ei->disptype==IPO_DISPBITS) {
1127                                         ok= 0;
1128                                         if(ei->flag & IPO_EDIT) {
1129                                                 if( (bezt->f2 & 1) == sel ) ok= 1;
1130                                         }
1131                                         else ok= 1;
1132                                         
1133                                         if(ok) {
1134                                                 val= bezt->vec[1][1];
1135                                                 b= 0;
1136                                                 v1[0]= bezt->vec[1][0];
1137                                                 
1138                                                 while(b<31) {
1139                                                         if(val & (1<<b)) {      
1140                                                                 v1[1]= b+1;
1141                                                                 bglVertex3fv(v1);
1142                                                         }
1143                                                         b++;
1144                                                 }
1145                                         }
1146                                 }
1147                                 else {
1148                                         
1149                                         if(ei->flag & IPO_EDIT) {
1150                                                 if(ei->icu->ipo==IPO_BEZ) {
1151                                                         if( (bezt->f1 & 1) == sel )     
1152                                                                 bglVertex3fv(bezt->vec[0]);
1153                                                         if( (bezt->f3 & 1) == sel )     
1154                                                                 bglVertex3fv(bezt->vec[2]);
1155                                                 }
1156                                                 if( (bezt->f2 & 1) == sel )     
1157                                                         bglVertex3fv(bezt->vec[1]);
1158                                                 
1159                                         }
1160                                         else {
1161                                                 bglVertex3fv(bezt->vec[1]);
1162                                         }
1163                                 }
1164                                 
1165                                 bezt++;
1166                         }
1167
1168                         bglEnd();
1169                 }
1170         }
1171         
1172         glPointSize(1.0);
1173 }
1174
1175 static void draw_ipohandles(int sel)
1176 {
1177         extern unsigned int nurbcol[];
1178         EditIpo *ei;
1179         BezTriple *bezt;
1180         float *fp;
1181         unsigned int *col;
1182         int a, b;
1183         
1184         if(sel) col= nurbcol+4;
1185         else col= nurbcol;
1186         
1187         ei= G.sipo->editipo;
1188         for(a=0; a<G.sipo->totipo; a++, ei++) {
1189                 if ISPOIN4(ei, flag & IPO_VISIBLE, flag & IPO_EDIT, icu, disptype!=IPO_DISPBITS) {
1190                         if(ei->icu->ipo==IPO_BEZ) {
1191                                 bezt= ei->icu->bezt;
1192                                 b= ei->icu->totvert;
1193                                 while(b--) {
1194                                         
1195                                         if( (bezt->f2 & 1)==sel) {
1196                                                 fp= bezt->vec[0];
1197                                                 cpack(col[bezt->h1]);
1198                                                 
1199                                                 glBegin(GL_LINE_STRIP); 
1200                                                 glVertex2fv(fp); glVertex2fv(fp+3); 
1201                                                 glEnd();
1202                                                 cpack(col[bezt->h2]);
1203                                                 
1204                                                 glBegin(GL_LINE_STRIP); 
1205                                                 glVertex2fv(fp+3); glVertex2fv(fp+6); 
1206                                                 glEnd();
1207                                         }
1208                                         else if( (bezt->f1 & 1)==sel) {
1209                                                 fp= bezt->vec[0];
1210                                                 cpack(col[bezt->h1]);
1211                                                 
1212                                                 glBegin(GL_LINE_STRIP); 
1213                                                 glVertex2fv(fp); glVertex2fv(fp+3); 
1214                                                 glEnd();
1215                                         }
1216                                         else if( (bezt->f3 & 1)==sel) {
1217                                                 fp= bezt->vec[1];
1218                                                 cpack(col[bezt->h2]);
1219                                                 
1220                                                 glBegin(GL_LINE_STRIP); 
1221                                                 glVertex2fv(fp); glVertex2fv(fp+3); 
1222                                                 glEnd();
1223                                         }
1224                                         
1225                                         bezt++;
1226                                 }
1227                         }
1228                 }
1229         }
1230 }
1231
1232 int pickselcode;
1233
1234 static void init_pickselcode(void)
1235 {
1236         pickselcode= 1;
1237 }
1238
1239 static void draw_ipocurves(int sel)
1240 {
1241         EditIpo *ei;
1242         IpoCurve *icu;
1243         BezTriple *bezt, *prevbezt;
1244         float *fp, fac, data[120], v1[2], v2[2], v3[2], v4[2];
1245         float cycdx=0, cycdy=0, cycxofs, cycyofs;
1246         int a, b, resol, cycount, val, nr;
1247         
1248         
1249         ei= G.sipo->editipo;
1250         for(nr=0; nr<G.sipo->totipo; nr++, ei++) {
1251                 if ISPOIN3(ei, flag & IPO_VISIBLE, icu, icu->bezt) {
1252                         
1253                         if(G.f & G_PICKSEL) {
1254                                 glLoadName(pickselcode++);
1255                                 val= 1;
1256                         }
1257                         else {
1258                                 val= (ei->flag & (IPO_SELECT+IPO_EDIT))!=0;
1259                                 val= (val==sel);
1260                         }
1261                         
1262                         if(val) {
1263                                 
1264                                 cycyofs= cycxofs= 0.0;
1265                                 cycount= 1;
1266                                 
1267                                 icu= ei->icu;   
1268                                 
1269                                 /* curve */
1270                                 if(G.sipo->showkey) BIF_ThemeColor(TH_TEXT); 
1271                                 else cpack(ei->col);
1272                                 
1273                                 /* cyclic */
1274                                 if(icu->extrap & IPO_CYCL) {
1275                                         cycdx= (icu->bezt+icu->totvert-1)->vec[1][0] - icu->bezt->vec[1][0];
1276                                         cycdy= (icu->bezt+icu->totvert-1)->vec[1][1] - icu->bezt->vec[1][1];
1277                                         if(cycdx>0.01) {
1278                                                 
1279                                                 while(icu->bezt->vec[1][0]+cycxofs > G.v2d->cur.xmin) {
1280                                                         cycxofs-= cycdx;
1281                                                         if(icu->extrap & IPO_DIR) cycyofs-= cycdy;
1282                                                         cycount++;
1283                                                 }
1284                                                 bezt= icu->bezt+(icu->totvert-1);
1285                                                 fac= 0.0;
1286                                                 while(bezt->vec[1][0]+fac < G.v2d->cur.xmax) {
1287                                                         cycount++;
1288                                                         fac+= cycdx;
1289                                                 }
1290                                         }
1291                                 }
1292                                 
1293                                 while(cycount--) {
1294                                         
1295                                         if(ei->disptype==IPO_DISPBITS) {
1296                                                 
1297                                                 /* lines */
1298                                                 cpack(ei->col);
1299                                                 bezt= icu->bezt;
1300                                                 a= icu->totvert;
1301                                                 
1302                                                 while(a--) {
1303                                                         val= bezt->vec[1][1];
1304                                                         b= 0;
1305                                                         
1306                                                         while(b<31) {
1307                                                                 if(val & (1<<b)) {
1308                                                                         v1[1]= b+1;
1309                                                                         
1310                                                                         glBegin(GL_LINE_STRIP);
1311                                                                         if(icu->extrap & IPO_CYCL) ;
1312                                                                         else if(a==icu->totvert-1) {
1313                                                                                 v1[0]= G.v2d->cur.xmin+cycxofs;
1314                                                                                 glVertex2fv(v1);
1315                                                                         }
1316                                                                         v1[0]= bezt->vec[1][0]+cycxofs;
1317                                                                         glVertex2fv(v1); 
1318                                                                         
1319                                                                         if(a) v1[0]= (bezt+1)->vec[1][0]+cycxofs;
1320                                                                         else if(icu->extrap & IPO_CYCL) ;
1321                                                                         else v1[0]= G.v2d->cur.xmax+cycxofs;
1322                                                                         
1323                                                                         glVertex2fv(v1);
1324                                                                         glEnd();
1325                                                                 }
1326                                                                 b++;
1327                                                         }
1328                                                         bezt++;
1329                                                 }
1330                                                 
1331                                         }
1332                                         else {
1333                                                 
1334                                                 b= icu->totvert-1;
1335                                                 prevbezt= icu->bezt;
1336                                                 bezt= prevbezt+1;
1337                                                 
1338                                                 glBegin(GL_LINE_STRIP);
1339                                                 
1340                                                 /* extrapolate to left? */
1341                                                 if( (icu->extrap & IPO_CYCL)==0) {
1342                                                         if(prevbezt->vec[1][0] > G.v2d->cur.xmin) {
1343                                                                 v1[0]= G.v2d->cur.xmin;
1344                                                                 if(icu->extrap==IPO_HORIZ || icu->ipo==IPO_CONST) v1[1]= prevbezt->vec[1][1];
1345                                                                 else {
1346                                                                         fac= (prevbezt->vec[0][0]-prevbezt->vec[1][0])/(prevbezt->vec[1][0]-v1[0]);
1347                                                                         if(fac!=0.0) fac= 1.0/fac;
1348                                                                         v1[1]= prevbezt->vec[1][1]-fac*(prevbezt->vec[0][1]-prevbezt->vec[1][1]);
1349                                                                 }
1350                                                                 glVertex2fv(v1);
1351                                                         }
1352                                                 }
1353                                                 
1354                                                 if(b==0) {
1355                                                         v1[0]= prevbezt->vec[1][0]+cycxofs;
1356                                                         v1[1]= prevbezt->vec[1][1]+cycyofs;
1357                                                         glVertex2fv(v1);
1358                                                 }
1359                                                 
1360                                                 while(b--) {
1361                                                         if(icu->ipo==IPO_CONST) {
1362                                                                 v1[0]= prevbezt->vec[1][0]+cycxofs;
1363                                                                 v1[1]= prevbezt->vec[1][1]+cycyofs;
1364                                                                 glVertex2fv(v1);
1365                                                                 v1[0]= bezt->vec[1][0]+cycxofs;
1366                                                                 v1[1]= prevbezt->vec[1][1]+cycyofs;
1367                                                                 glVertex2fv(v1);
1368                                                         }
1369                                                         else if(icu->ipo==IPO_LIN) {
1370                                                                 v1[0]= prevbezt->vec[1][0]+cycxofs;
1371                                                                 v1[1]= prevbezt->vec[1][1]+cycyofs;
1372                                                                 glVertex2fv(v1);
1373                                                         }
1374                                                         else {
1375                                                                 /* resol not depending on horizontal resolution anymore, drivers for example... */
1376                                                                 if(icu->driver) resol= 32;
1377                                                                 else resol= 3.0*sqrt(bezt->vec[1][0] - prevbezt->vec[1][0]);
1378                                                                 
1379                                                                 if(resol<2) {
1380                                                                         v1[0]= prevbezt->vec[1][0]+cycxofs;
1381                                                                         v1[1]= prevbezt->vec[1][1]+cycyofs;
1382                                                                         glVertex2fv(v1);
1383                                                                 }
1384                                                                 else {
1385                                                                         if(resol>32) resol= 32;
1386                                                                         
1387                                                                         v1[0]= prevbezt->vec[1][0]+cycxofs;
1388                                                                         v1[1]= prevbezt->vec[1][1]+cycyofs;
1389                                                                         v2[0]= prevbezt->vec[2][0]+cycxofs;
1390                                                                         v2[1]= prevbezt->vec[2][1]+cycyofs;
1391                                                                         
1392                                                                         v3[0]= bezt->vec[0][0]+cycxofs;
1393                                                                         v3[1]= bezt->vec[0][1]+cycyofs;
1394                                                                         v4[0]= bezt->vec[1][0]+cycxofs;
1395                                                                         v4[1]= bezt->vec[1][1]+cycyofs;
1396                                                                         
1397                                                                         correct_bezpart(v1, v2, v3, v4);
1398                                                                         
1399                                                                         forward_diff_bezier(v1[0], v2[0], v3[0], v4[0], data, resol, 3);
1400                                                                         forward_diff_bezier(v1[1], v2[1], v3[1], v4[1], data+1, resol, 3);
1401                                                                         
1402                                                                         fp= data;
1403                                                                         while(resol--) {
1404                                                                                 glVertex2fv(fp);
1405                                                                                 fp+= 3;
1406                                                                         }
1407                                                                 }
1408                                                         }
1409                                                         prevbezt= bezt;
1410                                                         bezt++;
1411                                                         
1412                                                         /* last point? */
1413                                                         if(b==0) {
1414                                                                 v1[0]= prevbezt->vec[1][0]+cycxofs;
1415                                                                 v1[1]= prevbezt->vec[1][1]+cycyofs;
1416                                                                 glVertex2fv(v1);
1417                                                         }
1418                                                 }
1419                                                 
1420                                                 /* extrapolate to right? */
1421                                                 if( (icu->extrap & IPO_CYCL)==0) {
1422                                                         if(prevbezt->vec[1][0] < G.v2d->cur.xmax) {
1423                                                                 v1[0]= G.v2d->cur.xmax;
1424                                                                 if(icu->extrap==IPO_HORIZ || icu->ipo==IPO_CONST) v1[1]= prevbezt->vec[1][1];
1425                                                                 else {
1426                                                                         fac= (prevbezt->vec[2][0]-prevbezt->vec[1][0])/(prevbezt->vec[1][0]-v1[0]);
1427                                                                         if(fac!=0.0) fac= 1.0/fac;
1428                                                                         v1[1]= prevbezt->vec[1][1]-fac*(prevbezt->vec[2][1]-prevbezt->vec[1][1]);
1429                                                                 }
1430                                                                 glVertex2fv(v1);
1431                                                         }
1432                                                 }
1433                                                 
1434                                                 glEnd();
1435                                                 
1436                                         }
1437                                         cycxofs+= cycdx;
1438                                         if(icu->extrap & IPO_DIR) cycyofs+= cycdy;
1439                                 }
1440                                 
1441                                 /* line that indicates the end of a speed curve */
1442                                 if(G.sipo->blocktype==ID_CU && icu->adrcode==CU_SPEED) {
1443                                         b= icu->totvert-1;
1444                                         if(b) {
1445                                                 glColor3ub(0, 0, 0);
1446                                                 bezt= icu->bezt+b;
1447                                                 glBegin(GL_LINES);
1448                                                 glVertex2f(bezt->vec[1][0], 0.0);
1449                                                 glVertex2f(bezt->vec[1][0], bezt->vec[1][1]);
1450                                                 glEnd();
1451                                         }
1452                                 }
1453                         }
1454                 }
1455         }
1456 }
1457
1458 static void draw_cfra(SpaceIpo *sipo)
1459 {
1460         View2D *v2d= &sipo->v2d;
1461         Object *ob;
1462         float vec[2];
1463         
1464         vec[0]= (G.scene->r.cfra);
1465         vec[0]*= G.scene->r.framelen;
1466         
1467         vec[1]= v2d->cur.ymin;
1468         glColor3ub(0x60, 0xc0, 0x40);   // no theme, should be global color once...
1469         glLineWidth(2.0);
1470         
1471         glBegin(GL_LINE_STRIP);
1472         glVertex2fv(vec);
1473         vec[1]= v2d->cur.ymax;
1474         glVertex2fv(vec);
1475         glEnd();
1476         
1477         if(sipo->blocktype==ID_OB) {
1478                 ob= (G.scene->basact) ? (G.scene->basact->object) : 0;
1479                 if(ob && ob->sf!=0.0 && (ob->ipoflag & OB_OFFS_OB) ) {
1480                         vec[0]-= ob->sf;
1481                         
1482                         BIF_ThemeColorShade(TH_HILITE, -30);
1483                         
1484                         glBegin(GL_LINE_STRIP);
1485                         glVertex2fv(vec);
1486                         vec[1]= G.v2d->cur.ymin;
1487                         glVertex2fv(vec);
1488                         glEnd();
1489                 }
1490         }
1491         
1492         glLineWidth(1.0);
1493 }
1494
1495 static void draw_ipokey(SpaceIpo *sipo)
1496 {
1497         IpoKey *ik;
1498         
1499         glBegin(GL_LINES);
1500         for (ik= sipo->ipokey.first; ik; ik= ik->next) {
1501                 if(ik->flag & 1) glColor3ub(0xFF, 0xFF, 0x99);
1502                 else glColor3ub(0xAA, 0xAA, 0x55);
1503                 
1504                 glVertex2f(ik->val, G.v2d->cur.ymin);
1505                 glVertex2f(ik->val, G.v2d->cur.ymax);
1506         }
1507         glEnd();
1508 }
1509
1510 static void draw_key(SpaceIpo *sipo, int visible)
1511 {
1512         View2D *v2d= &sipo->v2d;
1513         Key *key;
1514         KeyBlock *kb, *act=NULL;
1515         Object *ob= OBACT;
1516         unsigned int col;
1517         int index;
1518         
1519         key= ob_get_key((Object *)sipo->from);
1520         if(key==NULL)
1521                 return;
1522         
1523         if(key->type== KEY_RELATIVE) if(visible==0) return;
1524         
1525         for(index=1, kb= key->block.first; kb; kb= kb->next, index++) {
1526                 if(kb->type==KEY_LINEAR) setlinestyle(2);
1527                 else if(kb->type==KEY_BSPLINE) setlinestyle(4);
1528                 else setlinestyle(0);
1529                 
1530                 if(kb==key->refkey) col= 0x22FFFF;
1531                 else col= 0xFFFF00;
1532                 
1533                 if(ob->shapenr!=index) col-= 0x225500;
1534                 else act= kb;
1535                 
1536                 cpack(col);
1537                 
1538                 glBegin(GL_LINE_STRIP);
1539                 glVertex2f(v2d->cur.xmin, kb->pos);
1540                 glVertex2f(v2d->cur.xmax, kb->pos);
1541                 glEnd();
1542                 
1543         }
1544         
1545         if(act) {
1546                 if(act->type==KEY_LINEAR) setlinestyle(2);
1547                 else if(act->type==KEY_BSPLINE) setlinestyle(4);
1548                 else setlinestyle(0);
1549                 
1550                 if(act==key->refkey) cpack(0x22FFFF);
1551                 else cpack(0xFFFF00);
1552                 
1553                 glBegin(GL_LINE_STRIP);
1554                 glVertex2f(v2d->cur.xmin, act->pos);
1555                 glVertex2f(v2d->cur.xmax, act->pos);
1556                 glEnd();
1557         }
1558         
1559         setlinestyle(0);
1560 }
1561
1562 /* ************************** buttons *********************** */
1563
1564
1565 #define B_SETSPEED              3401
1566 #define B_MUL_IPO               3402
1567 #define B_TRANS_IPO             3403
1568 #define B_IPO_NONE              3404
1569 #define B_IPO_DRIVER    3405
1570 #define B_IPO_REDR              3406
1571 #define B_IPO_DEPCHANGE 3407
1572 #define B_IPO_DRIVERTYPE 3408
1573
1574 static float hspeed= 0;
1575
1576 static void boundbox_ipo_curves(SpaceIpo *si)
1577 {
1578         EditIpo *ei;
1579         Key *key;
1580         KeyBlock *kb;
1581         int a, first= 1;
1582
1583         ei= si->editipo;
1584         if(ei==0)
1585                 return;
1586
1587         for(a=0; a<si->totipo; a++, ei++) {
1588                 
1589                 if(ei->icu) {
1590                         if(ei->flag & IPO_VISIBLE) {
1591         
1592                                 boundbox_ipocurve(ei->icu);
1593                                 if(first) {
1594                                         si->v2d.tot= ei->icu->totrct;
1595                                         first= 0;
1596                                 }
1597                                 else BLI_union_rctf(&(si->v2d.tot), &(ei->icu->totrct));
1598                         }
1599                 }
1600         }
1601         /* keylines? */
1602         if(si->blocktype==ID_KE) {
1603                 key= ob_get_key((Object *)si->from);
1604                 if(key && key->block.first) {
1605                         kb= key->block.first;
1606                         if(kb->pos < si->v2d.tot.ymin) si->v2d.tot.ymin= kb->pos;
1607                         kb= key->block.last;
1608                         if(kb->pos > si->v2d.tot.ymax) si->v2d.tot.ymax= kb->pos;
1609                 }
1610         }
1611         si->tot= si->v2d.tot;
1612 }
1613
1614
1615 /* is used for both read and write... */
1616 static void ipo_editvertex_buts(uiBlock *block, SpaceIpo *si, float min, float max)
1617 {
1618         Object *ob= OBACT;
1619         EditIpo *ei;
1620         BezTriple *bezt;
1621         float median[3];
1622         int a, b, tot, iskey=0;
1623         
1624         median[0]= median[1]= median[2]= 0.0;
1625         tot= 0;
1626         
1627         ei= G.sipo->editipo;
1628         for(a=0; a<G.sipo->totipo; a++, ei++) {
1629                 
1630                 if ISPOIN(ei, flag & IPO_VISIBLE, icu) {
1631                         if( (ei->flag & IPO_EDIT) || G.sipo->showkey) {
1632
1633                                 if(ei->icu->bezt) {
1634                                         bezt= ei->icu->bezt;
1635                                         b= ei->icu->totvert;
1636                                         while(b--) {
1637                                                 // all three selected 
1638                                                 if(bezt->f2 & 1) {
1639                                                         VecAddf(median, median, bezt->vec[1]);
1640                                                         tot++;
1641                                                 }
1642                                                 else {
1643                                                         if(bezt->f1 & 1) {
1644                                                                 VecAddf(median, median, bezt->vec[0]);
1645                                                                 tot++;
1646                                                         }
1647                                                         if(bezt->f3 & 1) {
1648                                                                 VecAddf(median, median, bezt->vec[2]);
1649                                                                 tot++;
1650                                                         }
1651                                                 }
1652                                                 bezt++;
1653                                         }
1654                                         
1655                                 }
1656                         }
1657                 }
1658         }
1659         /* check for keys */
1660         if(tot==0) {
1661                 if(G.sipo->blocktype==ID_KE) {
1662                         Key *key= ob_get_key((Object *)G.sipo->from);
1663                         KeyBlock *kb;
1664                         
1665                         if(key==NULL || ob->shapenr==0) return;
1666                         iskey= 1;
1667                         
1668                         kb= BLI_findlink(&key->block, ob->shapenr-1);
1669                         median[1]+= kb->pos;
1670                         tot++;
1671                 }
1672         }
1673         if(tot==0) return;
1674
1675         median[0] /= (float)tot;
1676         median[1] /= (float)tot;
1677         median[2] /= (float)tot;
1678         
1679         if(block) {     // buttons
1680         
1681                 VECCOPY(si->median, median);
1682                 
1683                 uiBlockBeginAlign(block);
1684                 if(tot==1) {
1685                         if(iskey) 
1686                                 uiDefButF(block, NUM, B_TRANS_IPO, "Key Y:",    10, 80, 300, 19, &(si->median[1]), min, max, 10, 0, "");
1687                         else {
1688                                 uiDefButF(block, NUM, B_TRANS_IPO, "Vertex X:", 10, 100, 150, 19, &(si->median[0]), min, max, 100, 0, "");
1689                                 uiDefButF(block, NUM, B_TRANS_IPO, "Vertex Y:", 160, 100, 150, 19, &(si->median[1]), min, max, 100, 0, "");
1690                         }
1691                 }
1692                 else {
1693                         if(iskey) 
1694                                 uiDefButF(block, NUM, B_TRANS_IPO, "Median Key Y:",     10, 80, 300, 19, &(si->median[1]), min, max, 10, 0, "");
1695                         else {
1696                                 uiDefButF(block, NUM, B_TRANS_IPO, "Median X:", 10, 100, 150, 19, &(si->median[0]), min, max, 100, 0, "");
1697                                 uiDefButF(block, NUM, B_TRANS_IPO, "Median Y:", 160, 100, 150, 19, &(si->median[1]), min, max, 100, 0, "");
1698                         }
1699                 }
1700         }
1701         else if(iskey) {        // apply
1702                 VecSubf(median, si->median, median);
1703
1704                 if(G.sipo->blocktype==ID_KE) {
1705                         Key *key= ob_get_key((Object *)G.sipo->from);
1706                         KeyBlock *kb;
1707                         
1708                         if(key==NULL || ob->shapenr==0) return;
1709                         
1710                         kb= BLI_findlink(&key->block, ob->shapenr-1);
1711                         kb->pos+= median[1];
1712                         tot++;
1713
1714                         sort_keys(key);
1715                 }
1716         }
1717         else {
1718                 
1719                 VecSubf(median, si->median, median);
1720
1721                 ei= G.sipo->editipo;
1722                 for(a=0; a<G.sipo->totipo; a++, ei++) {
1723                         
1724                         if ISPOIN(ei, flag & IPO_VISIBLE, icu) {
1725                                 if( (ei->flag & IPO_EDIT) || G.sipo->showkey) {
1726
1727                                         if(ei->icu->bezt) {
1728                                                 bezt= ei->icu->bezt;
1729                                                 b= ei->icu->totvert;
1730                                                 while(b--) {
1731                                                         // all three selected
1732                                                         if(bezt->f2 & 1) {
1733                                                                 VecAddf(bezt->vec[0], bezt->vec[0], median);
1734                                                                 VecAddf(bezt->vec[1], bezt->vec[1], median);
1735                                                                 VecAddf(bezt->vec[2], bezt->vec[2], median);
1736                                                         }
1737                                                         else {
1738                                                                 if(bezt->f1 & 1) {
1739                                                                         VecAddf(bezt->vec[0], bezt->vec[0], median);
1740                                                                 }
1741                                                                 if(bezt->f3 & 1) {
1742                                                                         VecAddf(bezt->vec[2], bezt->vec[2], median);
1743                                                                 }
1744                                                         }
1745                                                         bezt++;
1746                                                 }
1747                                                 
1748                                         }
1749                                 }
1750                         }
1751                 }
1752         }
1753 }
1754
1755 void do_ipobuts(unsigned short event)
1756 {
1757         Object *ob= OBACT;
1758         EditIpo *ei;
1759         
1760         if(G.sipo->from==NULL) return;
1761         
1762         switch(event) {
1763         case B_IPO_REDR:
1764                 ei= get_active_editipo();
1765                 if(ei) {
1766                         if(ei->icu->driver) {
1767                                 if (ei->icu->driver->type == IPO_DRIVER_TYPE_PYTHON) {
1768                                         /* first del pydriver's global dict, just in case
1769                                          * an available pydrivers.py module needs to be reloaded */
1770                                         BPY_pydriver_update();
1771                                         /* eval user's expression once for validity; update DAG */
1772                                         BPY_pydriver_eval(ei->icu->driver);
1773                                         DAG_scene_sort(G.scene);
1774                                 }
1775                                 else if(G.sipo->blocktype==ID_KE || G.sipo->blocktype==ID_AC) 
1776                                         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
1777                                 else
1778                                         DAG_object_flush_update(G.scene, ob, OB_RECALC_OB);
1779                         }
1780                 }
1781                 allqueue(REDRAWIPO, 0);
1782                 allqueue(REDRAWVIEW3D, 0);
1783                 break;
1784         case B_SETSPEED:
1785                 set_speed_editipo(hspeed);
1786                 break;
1787         case B_MUL_IPO:
1788                 scale_editipo();
1789                 allqueue(REDRAWIPO, 0);
1790                 break;
1791         case B_TRANS_IPO:
1792                 ipo_editvertex_buts(NULL, G.sipo, 0.0, 0.0);
1793                 editipo_changed(G.sipo, 1);
1794                 allqueue(REDRAWIPO, 0);
1795                 break;
1796         case B_SETKEY:
1797                 ob->shapeflag &= ~OB_SHAPE_TEMPLOCK;
1798                 DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
1799                 allqueue(REDRAWVIEW3D, 0);
1800                 allqueue(REDRAWIPO, 0);
1801                 allqueue(REDRAWBUTSEDIT, 0);
1802                 break;
1803         case B_IPO_DRIVER:
1804                 ei= get_active_editipo();
1805                 if(ei) {
1806                         if(ei->icu==NULL) {
1807                                 ei->icu= verify_ipocurve(G.sipo->from, G.sipo->blocktype, G.sipo->actname, G.sipo->constname, ei->adrcode);
1808                                 ei->flag |= IPO_SELECT;
1809                                 ei->icu->flag= ei->flag;
1810                         }
1811                         if(ei->icu->driver) {
1812                                 MEM_freeN(ei->icu->driver);
1813                                 ei->icu->driver= NULL;
1814                                 if(ei->icu->bezt==NULL) {
1815                                         BLI_remlink( &(G.sipo->ipo->curve), ei->icu);
1816                                         free_ipo_curve(ei->icu);
1817                                         ei->icu= NULL;
1818                                 }
1819                         }
1820                         else {
1821                                 ei->icu->driver= MEM_callocN(sizeof(IpoDriver), "ipo driver");
1822                                 ei->icu->driver->blocktype= ID_OB;
1823                                 ei->icu->driver->adrcode= OB_LOC_X;
1824                         }
1825
1826                         allqueue(REDRAWVIEW3D, 0);
1827                         allqueue(REDRAWIPO, 0);
1828                         allqueue(REDRAWBUTSEDIT, 0);
1829                         DAG_scene_sort(G.scene);
1830                         
1831                         BIF_undo_push("Add/Remove Ipo driver");
1832                 }
1833                 break;
1834         case B_IPO_DRIVERTYPE:
1835                 ei= get_active_editipo();
1836                 if(ei) {
1837                         if(ei->icu->driver) {
1838                                 IpoDriver *driver= ei->icu->driver;
1839
1840                                 if(driver->type == IPO_DRIVER_TYPE_PYTHON) {
1841                                         /* pydriver expression shouldn't reference own ob,
1842                                          * so we need to store ob ptr to check against it */
1843                                         driver->ob= ob;
1844                                 }
1845                                 else {
1846                                         driver->ob= NULL;
1847                                         driver->blocktype= ID_OB;
1848                                         driver->adrcode= OB_LOC_X;
1849                                         driver->flag &= ~IPO_DRIVER_FLAG_INVALID;
1850                                 }
1851                         }
1852                         allqueue(REDRAWVIEW3D, 0);
1853                         allqueue(REDRAWIPO, 0);
1854                         allqueue(REDRAWBUTSEDIT, 0);
1855                         DAG_scene_sort(G.scene);
1856         
1857                         BIF_undo_push("Change Ipo driver type");
1858                 }
1859                 break;
1860         case B_IPO_DEPCHANGE:
1861                 ei= get_active_editipo();
1862                 if(ei) {
1863                         if(ei->icu->driver) {
1864                                 IpoDriver *driver= ei->icu->driver;
1865                                 
1866                                 if(driver->type == IPO_DRIVER_TYPE_PYTHON) {
1867                                 }
1868                                 else {
1869                                         if(driver->ob) {
1870                                                 if(ob==driver->ob) {
1871                                                         error("Cannot assign a Driver to own Object");
1872                                                         driver->ob= NULL;
1873                                                 }
1874                                                 else {
1875                                                         /* check if type is still OK */
1876                                                         if(driver->ob->type==OB_ARMATURE && driver->blocktype==ID_AR);
1877                                                         else driver->blocktype= ID_OB;
1878                                                 }
1879                                         }
1880                                 }
1881                                 DAG_scene_sort(G.scene);
1882                                 
1883                                 if(G.sipo->blocktype==ID_KE || G.sipo->blocktype==ID_AC) 
1884                                         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
1885                                 else
1886                                         DAG_object_flush_update(G.scene, ob, OB_RECALC_OB);
1887                         }
1888                 }
1889                 allqueue(REDRAWVIEW3D, 0);
1890                 allqueue(REDRAWIPO, 0);
1891                 allqueue(REDRAWBUTSEDIT, 0);
1892                 break;
1893         }
1894 }
1895
1896 static char *ipodriver_modeselect_pup(Object *ob)
1897 {
1898         static char string[265];
1899         char tmpstr[64];
1900         char formatstring[64];
1901         
1902         strcpy(string, "Driver type: %t");
1903         
1904         strcpy(formatstring, "|%s %%x%d %%i%d");
1905         
1906         if(ob) {
1907                 sprintf(tmpstr,formatstring,"Object",ID_OB, ICON_OBJECT);
1908                 strcat(string,tmpstr);
1909         }
1910         if(ob && ob->type==OB_ARMATURE) {
1911                 sprintf(tmpstr,formatstring,"Pose",ID_AR, ICON_POSE_DEHLT);
1912                 strcat(string,tmpstr);
1913         }
1914         
1915         return (string);
1916 }
1917
1918 static char *ipodriver_channelselect_pup(void)
1919 {
1920         static char string[1024];
1921         char *tmp;
1922         
1923         strcpy(string, "Driver channel: %t");
1924         tmp= string+strlen(string);
1925         
1926         tmp+= sprintf(tmp, "|Loc X %%x%d", OB_LOC_X);
1927         tmp+= sprintf(tmp, "|Loc Y %%x%d", OB_LOC_Y);
1928         tmp+= sprintf(tmp, "|Loc Z %%x%d", OB_LOC_Z);
1929         tmp+= sprintf(tmp, "|Rot X %%x%d", OB_ROT_X);
1930         tmp+= sprintf(tmp, "|Rot Y %%x%d", OB_ROT_Y);
1931         tmp+= sprintf(tmp, "|Rot Z %%x%d", OB_ROT_Z);
1932         tmp+= sprintf(tmp, "|Scale X %%x%d", OB_SIZE_X);
1933         tmp+= sprintf(tmp, "|Scale Y %%x%d", OB_SIZE_Y);
1934         tmp+= sprintf(tmp, "|Scale Z %%x%d", OB_SIZE_Z);
1935         
1936         return (string);
1937 }
1938
1939 static void ipo_panel_properties(short cntrl)   // IPO_HANDLER_PROPERTIES
1940 {
1941         extern int totipo_curve;        // editipo.c
1942         uiBlock *block;
1943         EditIpo *ei;
1944         char name[48];
1945         
1946         block= uiNewBlock(&curarea->uiblocks, "ipo_panel_properties", UI_EMBOSS, UI_HELV, curarea->win);
1947         uiPanelControl(UI_PNL_SOLID | UI_PNL_CLOSE | cntrl);
1948         uiSetPanelHandler(IPO_HANDLER_PROPERTIES);  // for close and esc
1949         if(uiNewPanel(curarea, block, "Transform Properties", "Ipo", 10, 230, 318, 204)==0) return;
1950
1951         /* this is new panel height, newpanel doesnt force new size on existing panels */
1952         uiNewPanelHeight(block, 204);
1953         
1954         /* driver buttons first */
1955         ei= get_active_editipo();
1956         if(ei) {
1957                 
1958                 sprintf(name, "Driven Channel: %s", ei->name);
1959                 uiDefBut(block, LABEL, 0, name,         10, 265, 200, 19, NULL, 1.0, 0.0, 0, 0, "");
1960                 
1961                 if(ei->icu && ei->icu->driver) {
1962                         IpoDriver *driver= ei->icu->driver;
1963
1964                         uiDefBut(block, BUT, B_IPO_DRIVER, "Remove",                            210,265,100,20, NULL, 0.0f, 0.0f, 0, 0, "Remove Driver for this Ipo Channel");
1965                         
1966                         uiBlockBeginAlign(block);
1967                         uiDefIconButS(block, TOG, B_IPO_DRIVERTYPE, ICON_PYTHON, 10,240,25,20, &driver->type, (float)IPO_DRIVER_TYPE_NORMAL, (float)IPO_DRIVER_TYPE_PYTHON, 0, 0, "Use a one-line Python Expression as Driver");
1968
1969                         if(driver->type == IPO_DRIVER_TYPE_PYTHON) {
1970                                 uiDefBut(block, TEX, B_IPO_REDR, "",                            35,240,275,20, driver->name, 0, 127, 0, 0, "Python Expression");
1971                                 if(driver->flag & IPO_DRIVER_FLAG_INVALID) {
1972                                         uiDefBut(block, LABEL, 0, "Error: invalid Python expression",
1973                                                         5,215,230,19, NULL, 0, 0, 0, 0, "");
1974                                 }
1975                                 uiBlockEndAlign(block);
1976                         }
1977                         else {
1978                                 uiDefIDPoinBut(block, test_obpoin_but, ID_OB, B_IPO_DEPCHANGE, "OB:",   35, 240, 125, 20, &(driver->ob), "Driver Object");
1979                                 if(driver->ob) {
1980                                         int icon=ICON_OBJECT;
1981                                         
1982                                         if(driver->ob->type==OB_ARMATURE && driver->blocktype==ID_AR) {
1983                                                 icon = ICON_POSE_DEHLT;
1984                                                 uiDefBut(block, TEX, B_IPO_REDR, "BO:",                         10,220,150,20, driver->name, 0, 31, 0, 0, "Bone name");
1985                                         }
1986                                         else driver->blocktype= ID_OB;  /* safety when switching object button */
1987                                         
1988                                         uiBlockBeginAlign(block);
1989                                         uiDefIconTextButS(block, MENU, B_IPO_DEPCHANGE, icon, 
1990                                                                           ipodriver_modeselect_pup(driver->ob), 165,240,145,20, &(driver->blocktype), 0, 0, 0, 0, "Driver type");
1991
1992                                         uiDefButS(block, MENU, B_IPO_REDR, 
1993                                                                 ipodriver_channelselect_pup(),                  165,220,145,20, &(driver->adrcode), 0, 0, 0, 0, "Driver channel");
1994                                 }
1995                                 uiBlockEndAlign(block);
1996                         }
1997                 }
1998                 else {
1999                         uiDefBut(block, BUT, B_IPO_DRIVER, "Add Driver",        210,265,100,19, NULL, 0.0f, 0.0f, 0, 0, "Create a Driver for this Ipo Channel");
2000                 }
2001         }
2002         else 
2003                 uiDefBut(block, LABEL, 0, " ",          10, 265, 150, 19, NULL, 1.0, 0.0, 0, 0, "");
2004
2005         boundbox_ipo_curves(G.sipo);    // should not be needed... transform/draw calls should update
2006         
2007         /* note ranges for buttons below are idiot... we need 2 ranges, one for sliding scale, one for real clip */
2008         if(G.sipo->ipo && G.sipo->ipo->curve.first && totipo_curve) {
2009                 extern int totipo_vertsel;      // editipo.c
2010                 uiDefBut(block, LABEL, 0, "Visible curves",             10, 200, 150, 19, NULL, 1.0, 0.0, 0, 0, "");
2011                 
2012                 uiBlockBeginAlign(block);
2013                 uiDefButF(block, NUM, B_MUL_IPO, "Xmin:",               10, 180, 150, 19, &G.sipo->tot.xmin, G.sipo->tot.xmin-1000.0, MAXFRAMEF, 100, 0, "");
2014                 uiDefButF(block, NUM, B_MUL_IPO, "Xmax:",               160, 180, 150, 19, &G.sipo->tot.xmax, G.sipo->tot.ymin-1000.0, MAXFRAMEF, 100, 0, "");
2015                 
2016                 uiDefButF(block, NUM, B_MUL_IPO, "Ymin:",               10, 160, 150, 19, &G.sipo->tot.ymin, G.sipo->tot.ymin-1000.0, 5000.0, 100, 0, "");
2017                 uiDefButF(block, NUM, B_MUL_IPO, "Ymax:",               160, 160, 150, 19, &G.sipo->tot.ymax, G.sipo->tot.ymin-1000.0, 5000.0, 100, 0, "");
2018
2019                 /* SPEED BUTTON */
2020                 if(totipo_vertsel) {
2021                         uiBlockBeginAlign(block);
2022                         uiDefButF(block, NUM, B_IPO_NONE, "Speed:",             10,130,150,19, &hspeed, 0.0, 180.0, 1, 0, "");
2023                         uiDefBut(block, BUT, B_SETSPEED,"SET",                  160,130,50,19, 0, 0, 0, 0, 0, "");
2024                 }
2025         }
2026
2027         /* this one also does keypositions */
2028         if(G.sipo->ipo) ipo_editvertex_buts(block, G.sipo, -10000, MAXFRAMEF);
2029 }
2030
2031 static void ipo_blockhandlers(ScrArea *sa)
2032 {
2033         SpaceIpo *sipo= sa->spacedata.first;
2034         short a;
2035
2036         /* warning; blocks need to be freed each time, handlers dont remove (for ipo moved to drawipospace) */
2037
2038         for(a=0; a<SPACE_MAXHANDLER; a+=2) {
2039                 switch(sipo->blockhandler[a]) {
2040
2041                 case IPO_HANDLER_PROPERTIES:
2042                         ipo_panel_properties(sipo->blockhandler[a+1]);
2043                         break;
2044                 
2045                 }
2046                 /* clear action value for event */
2047                 sipo->blockhandler[a+1]= 0;
2048         }
2049         uiDrawBlocksPanels(sa, 0);
2050
2051 }
2052
2053
2054 void drawipospace(ScrArea *sa, void *spacedata)
2055 {
2056         SpaceIpo *sipo= sa->spacedata.first;
2057         View2D *v2d= &sipo->v2d;
2058         EditIpo *ei;
2059         float col[3];
2060         int ofsx, ofsy, a, disptype;
2061
2062         bwin_clear_viewmat(sa->win);    /* clear buttons view */
2063         glLoadIdentity();
2064         
2065         uiFreeBlocksWin(&sa->uiblocks, sa->win);        /* for panel handler to work */
2066         
2067         test_editipo(0);        /* test if current editipo is correct, make_editipo sets v2d->cur, call here because of calc_ipobuttonswidth() */
2068         
2069         v2d->hor.xmax+=calc_ipobuttonswidth(sa);
2070         calc_scrollrcts(sa, G.v2d, sa->winx, sa->winy);
2071
2072         BIF_GetThemeColor3fv(TH_BACK, col);
2073         glClearColor(col[0], col[1], col[2], 0.0); 
2074
2075         if (sipo->pin)
2076                 glClearColor(col[0]+0.05,col[1],col[2], 0.0);   // litepink
2077         else
2078                 glClearColor(col[0],col[1],col[2], 0.0);
2079
2080         glClear(GL_COLOR_BUFFER_BIT);
2081         
2082         if(sa->winx>SCROLLB+10 && sa->winy>SCROLLH+10) {
2083                 if(v2d->scroll) {       
2084                         ofsx= sa->winrct.xmin;  // ivm mywin 
2085                         ofsy= sa->winrct.ymin;
2086                         glViewport(ofsx+v2d->mask.xmin,  ofsy+v2d->mask.ymin, ( ofsx+v2d->mask.xmax-1)-(ofsx+v2d->mask.xmin)+1, ( ofsy+v2d->mask.ymax-1)-( ofsy+v2d->mask.ymin)+1); 
2087                         glScissor(ofsx+v2d->mask.xmin,  ofsy+v2d->mask.ymin, ( ofsx+v2d->mask.xmax-1)-(ofsx+v2d->mask.xmin)+1, ( ofsy+v2d->mask.ymax-1)-( ofsy+v2d->mask.ymin)+1);
2088                 } 
2089         }
2090
2091         myortho2(v2d->cur.xmin, v2d->cur.xmax, v2d->cur.ymin, v2d->cur.ymax);
2092  
2093         if(sipo->editipo) {
2094                 
2095                 /* correct scale for degrees? */
2096                 disptype= -1;
2097                 ei= sipo->editipo;
2098                 for(a=0; a<sipo->totipo; a++, ei++) {
2099                         if(ei->flag & IPO_VISIBLE) {
2100                                 if(disptype== -1) disptype= ei->disptype;
2101                                 else if(disptype!=ei->disptype) disptype= 0;
2102                         }
2103                 }
2104                 
2105                 calc_ipogrid(); 
2106                 draw_ipogrid();
2107                 
2108                 draw_cfra(sipo);
2109                 
2110                 /* ipokeys */
2111                 if(sipo->showkey) {
2112                         if(sipo->ipokey.first==0) make_ipokey();
2113                         draw_ipokey(sipo);
2114                 }
2115                 
2116                 if(sipo->blocktype==ID_KE) {
2117                         ei= sipo->editipo;
2118                         draw_key(sipo, ei->flag & IPO_VISIBLE);
2119                 }
2120
2121                 /* draw deselect */
2122                 draw_ipocurves(0);
2123                 draw_ipohandles(0);
2124                 draw_ipovertices(0);
2125                 
2126                 /* draw select */
2127                 draw_ipocurves(1);
2128                 draw_ipohandles(1);
2129                 draw_ipovertices(1);
2130                 
2131                 /* restore viewport */
2132                 mywinset(sa->win);
2133                 
2134                 if(sa->winx>SCROLLB+10 && sa->winy>SCROLLH+10) {
2135                         
2136                         /* ortho at pixel level sa */
2137                         myortho2(-0.375, sa->winx-0.375, -0.375, sa->winy-0.375);
2138                         
2139                         if(v2d->scroll) {
2140                                 drawscroll(disptype);
2141                                 draw_solution(sipo);
2142                         }
2143                         
2144                         draw_ipobuts(sipo);
2145                 }
2146         }
2147         else {
2148                 calc_ipogrid();
2149                 draw_ipogrid();
2150         }
2151         
2152         myortho2(-0.375, sa->winx-0.375, -0.375, sa->winy-0.375);
2153         draw_area_emboss(sa);
2154
2155         /* it is important to end a view in a transform compatible with buttons */
2156         bwin_scalematrix(sa->win, sipo->blockscale, sipo->blockscale, sipo->blockscale);
2157         /* only draw panels when relevant */
2158         if(sipo->editipo) ipo_blockhandlers(sa);
2159
2160         sa->win_swap= WIN_BACK_OK;
2161 }
2162
2163 void scroll_ipobuts()
2164 {
2165         int tot;
2166         short yo, mval[2];
2167         
2168         tot= 30+IPOBUTY*G.sipo->totipo;
2169         if(tot<curarea->winy) return;
2170         
2171         getmouseco_areawin(mval);
2172         yo= mval[1];
2173         
2174         while(get_mbut()&M_MOUSE) {
2175                 getmouseco_areawin(mval);
2176                 if(mval[1]!=yo) {
2177                         G.sipo->butofs+= (mval[1]-yo);
2178                         if(G.sipo->butofs<0) G.sipo->butofs= 0;
2179                         else if(G.sipo->butofs+curarea->winy>tot) G.sipo->butofs= tot-curarea->winy;
2180                         
2181                         scrarea_do_windraw(curarea);
2182                         screen_swapbuffers();
2183                         
2184                         yo= mval[1];
2185                 }
2186                 else BIF_wait_for_statechange();
2187         }
2188 }
2189
2190 /* total mess function, especially with mousewheel, needs cleanup badly (ton) */
2191 int view2dzoom(unsigned short event)
2192 {
2193         ScrArea *sa;
2194         float fac, dx, dy, wtemp;
2195         short mval[2], mvalo[2];
2196         short is_wheel= (event==WHEELUPMOUSE) || (event==WHEELDOWNMOUSE);
2197         
2198         getmouseco_areawin(mvalo);
2199         mval[0]= mvalo[0];
2200         mval[1]= mvalo[1];
2201         
2202         while( (get_mbut()&(L_MOUSE|M_MOUSE)) || is_wheel ) {
2203                 
2204                 /* regular mousewheel:   zoom regular
2205                 * alt-shift mousewheel: zoom y only
2206                 * alt-ctrl mousewheel:  zoom x only
2207                 */
2208                 if (event==WHEELUPMOUSE) {
2209                         if(U.uiflag & USER_WHEELZOOMDIR)
2210                                 wtemp = -0.0375;
2211                         else
2212                                 wtemp = 0.03;
2213                         if(curarea->spacetype!=SPACE_BUTS) wtemp*= 3;
2214                         
2215                         dx= (float)(wtemp*(G.v2d->cur.xmax-G.v2d->cur.xmin));
2216                         dy= (float)(wtemp*(G.v2d->cur.ymax-G.v2d->cur.ymin));
2217                         
2218                         switch (G.qual & (LR_CTRLKEY|LR_SHIFTKEY|LR_ALTKEY)) {
2219                         case 0:
2220                                 break;
2221                         case (LR_SHIFTKEY|LR_ALTKEY):
2222                                 dx = 0;
2223                                 break;
2224                         case (LR_CTRLKEY|LR_ALTKEY):
2225                                 dy = 0;
2226                                 break;
2227                         default:
2228                                 if(curarea->spacetype==SPACE_BUTS);     // exception
2229                                 else return 0;
2230                                 break;
2231                         }
2232                 }
2233                 else if (event==WHEELDOWNMOUSE) {
2234                         if(U.uiflag & USER_WHEELZOOMDIR)
2235                                 wtemp = 0.03;
2236                         else
2237                                 wtemp = -0.0375;
2238                         if(curarea->spacetype!=SPACE_BUTS) wtemp*= 3;
2239                         
2240                         dx= (float)(wtemp*(G.v2d->cur.xmax-G.v2d->cur.xmin));
2241                         dy= (float)(wtemp*(G.v2d->cur.ymax-G.v2d->cur.ymin));
2242                         
2243                         switch (G.qual & (LR_CTRLKEY|LR_SHIFTKEY|LR_ALTKEY)) {
2244                                 case 0:
2245                                 break;
2246                         case (LR_SHIFTKEY|LR_ALTKEY):
2247                                 dx = 0;
2248                                 break;
2249                         case (LR_CTRLKEY|LR_ALTKEY):
2250                                 dy = 0;
2251                                 break;
2252                         default:
2253                                 if(curarea->spacetype==SPACE_BUTS);
2254                                 else return 0;
2255                                 break;
2256                         }
2257                 }
2258                 else {
2259                         getmouseco_areawin(mval);
2260                         if(U.viewzoom==USER_ZOOM_SCALE) {
2261                                 float dist;
2262                                 
2263                                 dist = (G.v2d->mask.xmax - G.v2d->mask.xmin)/2.0;
2264                                 dx= 1.0-(fabs(mvalo[0]-dist)+2.0)/(fabs(mval[0]-dist)+2.0);
2265                                 dx*= 0.5*(G.v2d->cur.xmax-G.v2d->cur.xmin);
2266
2267                                 dist = (G.v2d->mask.ymax - G.v2d->mask.ymin)/2.0;
2268                                 dy= 1.0-(fabs(mvalo[1]-dist)+2.0)/(fabs(mval[1]-dist)+2.0);
2269                                 dy*= 0.5*(G.v2d->cur.ymax-G.v2d->cur.ymin);
2270         
2271                         }
2272                         else {
2273                                 fac= 0.01*(mval[0]-mvalo[0]);
2274                                 dx= fac*(G.v2d->cur.xmax-G.v2d->cur.xmin);
2275                                 fac= 0.01*(mval[1]-mvalo[1]);
2276                                 dy= fac*(G.v2d->cur.ymax-G.v2d->cur.ymin);
2277                                 
2278                                 if(U.viewzoom==USER_ZOOM_CONT) {
2279                                         dx/= 20.0;
2280                                         dy/= 20.0;
2281                                 }
2282                         }
2283                 }
2284
2285                 if (ELEM(event, WHEELUPMOUSE, WHEELDOWNMOUSE) || mval[0]!=mvalo[0] || mval[1]!=mvalo[1]) {
2286                         
2287                         if(U.viewzoom!=USER_ZOOM_CONT) {
2288                                 mvalo[0]= mval[0];
2289                                 mvalo[1]= mval[1];
2290                         }
2291                         
2292                         if( ELEM(curarea->spacetype, SPACE_NLA, SPACE_ACTION) ) {
2293                                 if(mvalo[0] < G.v2d->mask.xmin) {
2294                                         G.v2d->cur.ymin+= dy;
2295                                         G.v2d->cur.ymax-= dy;
2296                                 }
2297                                 else {
2298                                         G.v2d->cur.xmin+= dx;
2299                                         G.v2d->cur.xmax-= dx;
2300                                 }
2301                         }
2302                         else if (ELEM(curarea->spacetype, SPACE_SOUND, SPACE_TIME)) {
2303                                 G.v2d->cur.xmin+= dx;
2304                                 G.v2d->cur.xmax-= dx;
2305                         }
2306                         else if (curarea->spacetype == SPACE_SEQ) {
2307                                 /* less sensitivity on y scale */
2308                                 G.v2d->cur.xmin+= dx;
2309                                 G.v2d->cur.xmax-= dx;
2310                                 if (!(ELEM(event, WHEELUPMOUSE, WHEELDOWNMOUSE))) {
2311                                         G.v2d->cur.ymin+= dy/2;
2312                                         G.v2d->cur.ymax-= dy/2;
2313                                 }
2314                         }
2315                         else {
2316                                 G.v2d->cur.xmin+= dx;
2317                                 G.v2d->cur.xmax-= dx;
2318                                 G.v2d->cur.ymin+= dy;
2319                                 G.v2d->cur.ymax-= dy;
2320                         }
2321                         
2322                         test_view2d(G.v2d, curarea->winx, curarea->winy);       /* cur min max rects */
2323                         
2324                         sa= curarea;    /* now when are you going to kill this one! */
2325                         view2d_do_locks(curarea, V2D_LOCK_COPY|V2D_LOCK_REDRAW);
2326                         areawinset(sa->win);
2327
2328                         scrarea_do_windraw(curarea);
2329                         screen_swapbuffers();
2330                 }
2331                 else BIF_wait_for_statechange();
2332                 /* return if we were using the mousewheel
2333                 */
2334                 if ( is_wheel ) return 1;
2335         }
2336         return 1;
2337 }
2338
2339 void center_currframe(void)
2340 {
2341         /* place the current frame in the
2342          * center of the 2D window.
2343          */
2344         float width;
2345   
2346         width = G.v2d->cur.xmax - G.v2d->cur.xmin;
2347         G.v2d->cur.xmin = CFRA - 0.5*(width);
2348         G.v2d->cur.xmax = CFRA + 0.5*(width);
2349
2350         test_view2d(G.v2d, curarea->winx, curarea->winy);
2351         view2d_do_locks(curarea, V2D_LOCK_COPY);
2352
2353         scrarea_queue_winredraw(curarea);
2354 }
2355
2356 /* total mess function, especially with mousewheel, needs cleanup badly (ton) */
2357 int view2dmove(unsigned short event)
2358 {
2359         /* return 1 when something was done */
2360         float facx=0.0, facy=0.0, dx, dy, left=1.0, right=1.0;
2361         short mval[2], mvalo[2], leftret=1, mousebut;
2362         short is_wheel= (event==WHEELUPMOUSE) || (event==WHEELDOWNMOUSE);
2363         int oldcursor, cursor;
2364         Window *win;
2365         
2366         /* when wheel is used, we only draw it once */
2367         
2368         /* try to do some zooming if the
2369          * middlemouse and ctrl are pressed
2370          * or if the mousewheel is being used.
2371          * Return if zooming was done.
2372          */
2373         
2374         /* check for left mouse / right mouse button select */
2375         if (U.flag & USER_LMOUSESELECT) mousebut = R_MOUSE;
2376                 else mousebut = L_MOUSE;
2377         
2378         if ( (G.qual & LR_CTRLKEY) || is_wheel ) {
2379                 /* patch for oops & buttonswin, standard scroll no zoom */
2380                 if(curarea->spacetype==SPACE_OOPS) {
2381                         SpaceOops *soops= curarea->spacedata.first;
2382                         if(soops->type==SO_OUTLINER);
2383                         else if (view2dzoom(event)) {
2384                                 return 0;
2385                         }
2386                 }
2387                 else if(curarea->spacetype==SPACE_BUTS && (G.qual & LR_CTRLKEY)==0);
2388                 else if (view2dzoom(event)) {
2389                         return 0;
2390                 }
2391         }
2392         
2393         /* test where mouse is */
2394         getmouseco_areawin(mvalo);
2395         /* initialize this too */
2396         mval[0]= mvalo[0];
2397         mval[1]= mvalo[1];
2398         
2399         if ELEM7(curarea->spacetype, SPACE_IPO, SPACE_SEQ, SPACE_OOPS, SPACE_SOUND, SPACE_ACTION, SPACE_NLA, SPACE_TIME) {
2400
2401                 if( BLI_in_rcti(&G.v2d->mask, (int)mvalo[0], (int)mvalo[1]) ) {
2402                         facx= (G.v2d->cur.xmax-G.v2d->cur.xmin)/(float)(G.v2d->mask.xmax-G.v2d->mask.xmin);
2403                         facy= (G.v2d->cur.ymax-G.v2d->cur.ymin)/(float)(G.v2d->mask.ymax-G.v2d->mask.ymin);
2404                 }
2405                 /* stoopid exception to allow scroll in lefthand side */
2406                 else if(curarea->spacetype==SPACE_ACTION && BLI_in_rcti(&G.v2d->mask, ACTWIDTH+(int)mvalo[0], (int)mvalo[1]) ) {
2407                         facx= 0.0f;
2408                         facy= (G.v2d->cur.ymax-G.v2d->cur.ymin)/(float)(G.v2d->mask.ymax-G.v2d->mask.ymin);
2409                 }
2410                 else if(curarea->spacetype==SPACE_NLA && BLI_in_rcti(&G.v2d->mask, NLAWIDTH+(int)mvalo[0], (int)mvalo[1]) ) {
2411                         facx= 0.0f;
2412                         facy= (G.v2d->cur.ymax-G.v2d->cur.ymin)/(float)(G.v2d->mask.ymax-G.v2d->mask.ymin);
2413                 }
2414                 else if(IN_2D_VERT_SCROLL((int)mvalo)) {
2415                         facy= -(G.v2d->tot.ymax-G.v2d->tot.ymin)/(float)(G.v2d->mask.ymax-G.v2d->mask.ymin);
2416                         if(get_mbut() & mousebut) {
2417                                 /* which part of scrollbar should move? */
2418                                 if(mvalo[1]< (vertymin+vertymax)/2 ) right= 0.0;
2419                                 else left= 0.0;
2420                                 leftret= 0;
2421                         }
2422                         if(is_wheel)
2423                                 facy= -facy;
2424                 }
2425                 else if(IN_2D_HORIZ_SCROLL((int)mvalo)) {
2426                         facx= -(G.v2d->tot.xmax-G.v2d->tot.xmin)/(float)(G.v2d->mask.xmax-G.v2d->mask.xmin);
2427                         if(get_mbut() & mousebut) {
2428                                 /* which part of scrollbar should move? */
2429                                 if(mvalo[0]< (horxmin+horxmax)/2 ) right= 0.0;
2430                                 else left= 0.0;
2431                                 leftret= 0;
2432                         }
2433                 } 
2434         }
2435         else {
2436                 facx= (G.v2d->cur.xmax-G.v2d->cur.xmin)/(float)(curarea->winx);
2437                 facy= (G.v2d->cur.ymax-G.v2d->cur.ymin)/(float)(curarea->winy);         
2438         }
2439         
2440         cursor = BC_NSEW_SCROLLCURSOR;
2441         
2442         /* no x move in outliner */
2443         if(curarea->spacetype==SPACE_OOPS && G.v2d->scroll) {
2444                 facx= 0.0;
2445                 cursor = BC_NS_SCROLLCURSOR;
2446         }
2447         
2448         /* no y move in audio & time */
2449         if ELEM(curarea->spacetype, SPACE_SOUND, SPACE_TIME) {
2450                 facy= 0.0;
2451                 cursor = BC_EW_SCROLLCURSOR;
2452         }
2453         
2454         /* store the old cursor to temporarily change it */
2455         oldcursor=get_cursor();
2456         win=winlay_get_active_window();
2457
2458         
2459         if(get_mbut() & mousebut && leftret) return 0;
2460         if(facx==0.0 && facy==0.0) return 1;
2461         
2462         if (!is_wheel) SetBlenderCursor(cursor);
2463         
2464         while( (get_mbut()&(L_MOUSE|M_MOUSE)) || is_wheel) {
2465
2466       /* If the mousewheel is used with shift key
2467        * the scroll up and down. If the mousewheel
2468        * is used with the ctrl key then scroll left
2469        * and right.
2470        */
2471                 if (is_wheel) {
2472                         
2473                         if(event==WHEELDOWNMOUSE) {     
2474                                 facx= -facx; facy= -facy;
2475                         }
2476                         switch (G.qual & (LR_CTRLKEY|LR_SHIFTKEY|LR_ALTKEY)) {
2477                         case (LR_SHIFTKEY):
2478                                 dx = 0.0;
2479                                 dy= facy*20.0;
2480                                 break;
2481                         case (LR_CTRLKEY):
2482                                 dx= facx*20.0;
2483                                 dy = 0.0;
2484                                 break;
2485                         default:
2486                                 if(curarea->spacetype==SPACE_OOPS) {
2487                                         dx= 0.0;
2488                                         dy= facy*20;
2489                                 }
2490                                 else if(curarea->spacetype==SPACE_BUTS) {
2491                                         if(G.buts->align==BUT_HORIZONTAL) {
2492                                                 dx= facx*30; dy= 0.0;
2493                                         } else {
2494                                                 dx= 0.0; dy= facy*30;
2495                                         }
2496                                 }
2497                                 else return 0;
2498                                 break;
2499                         }
2500                 }
2501                 else {
2502
2503                         
2504                         getmouseco_areawin(mval);
2505                         dx= facx*(mvalo[0]-mval[0]);
2506                         dy= facy*(mvalo[1]-mval[1]);
2507                 }
2508
2509                 if(mval[0]!=mvalo[0] || mval[1]!=mvalo[1] || is_wheel) {
2510                         ScrArea *sa;
2511                         
2512                         G.v2d->cur.xmin+= left*dx;
2513                         G.v2d->cur.xmax+= right*dx;
2514                         G.v2d->cur.ymin+= left*dy;
2515                         G.v2d->cur.ymax+= right*dy;
2516                         
2517                         test_view2d(G.v2d, curarea->winx, curarea->winy);
2518                         
2519                         sa= curarea;    /* bad global */
2520                         view2d_do_locks(curarea, V2D_LOCK_COPY|V2D_LOCK_REDRAW);
2521                         areawinset(sa->win);
2522                         
2523                         scrarea_do_windraw(curarea);
2524                         screen_swapbuffers();
2525                                 
2526                         mvalo[0]= mval[0];
2527                         mvalo[1]= mval[1];
2528                 }
2529                 else BIF_wait_for_statechange();
2530                         /* return if we were using the mousewheel
2531                         */
2532                 if ( is_wheel ) return 1;
2533         }
2534
2535         window_set_cursor(win, oldcursor);
2536     return 1;
2537 }
2538
2539 void view2dborder(void)
2540 {
2541         
2542 }
2543
2544 EditIpo *select_proj_ipo(rctf *rectf, int event)
2545 {
2546         EditIpo *ei;
2547         float xmin, ymin, xmax, ymax;
2548         /* this was IGLuint, but it's a useless typedef... */
2549         GLuint buffer[MAXPICKBUF];
2550         int a, b;
2551         int hits;
2552         unsigned int code;
2553         short mval[2];
2554         
2555         G.f |= G_PICKSEL;
2556         
2557         if(rectf==0) {
2558                 getmouseco_areawin(mval);
2559                 
2560                 mval[0]-= 6; mval[1]-= 6;
2561                 areamouseco_to_ipoco(G.v2d, mval, &xmin, &ymin);
2562                 mval[0]+= 12; mval[1]+= 12;
2563                 areamouseco_to_ipoco(G.v2d, mval, &xmax, &ymax);
2564                 
2565                 myortho2(xmin, xmax, ymin, ymax);
2566         }
2567         else myortho2(rectf->xmin, rectf->xmax, rectf->ymin, rectf->ymax);
2568         
2569         glSelectBuffer( MAXPICKBUF, buffer); 
2570         glRenderMode(GL_SELECT);
2571         glInitNames();  /* whatfor? but otherwise it does not work */
2572         glPushName(-1);
2573         
2574         /* get rid of buttons view */
2575         glPushMatrix();
2576         glLoadIdentity();
2577         
2578         init_pickselcode();     /* drawipo.c */
2579         draw_ipocurves(0);      
2580         
2581         /* restore buttons view */
2582         glPopMatrix();
2583
2584         G.f -= G_PICKSEL;
2585         
2586         hits= glRenderMode(GL_RENDER);
2587         glPopName();    /* see above (pushname) */
2588         if(hits<1) return 0;
2589         
2590         code= 1;
2591         ei= G.sipo->editipo;
2592         for(a=0; a<G.sipo->totipo; a++, ei++) {
2593                 if ISPOIN(ei, icu, flag & IPO_VISIBLE) {
2594                         if(rectf) {
2595                                 for(b=0; b<hits; b++) {
2596                                         /* conversion for glSelect */
2597                                         if(code == buffer[ (4 * b) + 3] ) {
2598                                                 if(event==LEFTMOUSE) ei->flag |= IPO_SELECT;
2599                                                 else ei->flag &= ~IPO_SELECT;
2600                                                 ei->icu->flag= ei->flag;
2601                                         }
2602                                 }
2603                         }
2604                         else {
2605                                 /* also conversion for glSelect */
2606                                 if(code==buffer[ 3 ]) return ei;
2607                         }
2608                         code++;
2609                 }
2610         }
2611         return 0;
2612 }
2613