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