* make function to map mouse into region coords
[blender.git] / source / blender / editors / interface / interface_ops.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL 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. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2008 Blender Foundation.
21  * All rights reserved.
22  * 
23  * Contributor(s): Blender Foundation
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 #include <math.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 #include "MEM_guardedalloc.h"
33
34 #include "DNA_color_types.h"
35 #include "DNA_object_types.h"
36 #include "DNA_screen_types.h"
37 #include "DNA_texture_types.h"
38 #include "DNA_userdef_types.h"
39 #include "DNA_windowmanager_types.h"
40
41 #include "BLI_arithb.h"
42 #include "BLI_blenlib.h"
43 #include "PIL_time.h"
44
45 #include "BKE_colortools.h"
46 #include "BKE_global.h"
47 #include "BKE_texture.h"
48 #include "BKE_utildefines.h"
49
50 #include "UI_interface.h"
51 #include "UI_text.h"
52 #include "interface.h"
53
54 #include "WM_api.h"
55 #include "WM_types.h"
56
57 /***************** structs and defines ****************/
58
59 #define BUTTON_TOOLTIP_DELAY            500
60 #define BUTTON_FLASH_DELAY                      20
61 #define BUTTON_AUTO_OPEN_THRESH         0.3
62 #define BUTTON_MOUSE_TOWARDS_THRESH     1.0
63
64 typedef enum uiActivateButState {
65         BUTTON_STATE_INIT,
66         BUTTON_STATE_HIGHLIGHT,
67         BUTTON_STATE_WAIT_FLASH,
68         BUTTON_STATE_WAIT_RELEASE,
69         BUTTON_STATE_WAIT_KEY_EVENT,
70         BUTTON_STATE_NUM_EDITING,
71         BUTTON_STATE_TEXT_EDITING,
72         BUTTON_STATE_TEXT_SELECTING,
73         BUTTON_STATE_BLOCK_OPEN,
74         BUTTON_STATE_EXIT
75 } uiActivateButState;
76
77 typedef struct uiActivateBut {
78         ARegion *region;
79         wmOperator *operator;
80
81         int interactive;
82
83         /* overall state */
84         uiActivateButState state;
85         int cancel, retval;
86         int applied, appliedinteractive;
87         wmTimerHandle *flashtimer;
88
89         /* edited value */
90         char *str, *origstr;
91         double value, origvalue;
92         float vec[3], origvec[3];
93         int togdual, togonly;
94         ColorBand *coba;
95         CurveMapping *cumap;
96
97         /* tooltip */
98         ARegion *tooltip;
99         wmTimerHandle *tooltiptimer;
100         wmTimerHandle *autoopentimer;
101         int tooltipdisabled;
102
103         /* text selection/editing */
104         int maxlen, selextend, selstartx;
105
106         /* number editing / dragging */
107         int draglastx, draglasty;
108         int dragstartx, dragstarty;
109         int dragchange, draglock, dragsel;
110         float dragf, dragfstart;
111         CBData *dragcbd;
112
113         /* block open */
114         uiMenuBlockHandle *blockhandle;
115         int blockretval;
116 } uiActivateBut;
117
118 static void button_activate_state(bContext *C, uiBut *but, uiActivateButState state);
119
120 /* ********************** button apply/revert ************************ */
121
122 static void ui_apply_but_func(uiBut *but)
123 {
124         if (but->func)
125                 but->func(but->func_arg1, but->func_arg2);
126         if(but->func3)
127                 but->func3(but->func_arg1, but->func_arg2, but->func_arg3);
128 }
129
130 static void ui_apply_but_BUT(uiBut *but, uiActivateBut *data)
131 {
132         ui_apply_but_func(but);
133
134         data->retval= but->retval;
135         data->applied= 1;
136 }
137
138 static void ui_apply_but_BUTM(uiBut *but, uiActivateBut *data)
139 {
140         ui_set_but_val(but, but->min);
141         ui_apply_but_func(but);
142
143         data->retval= but->retval;
144         data->applied= 1;
145 }
146
147 static void ui_apply_but_BLOCK(uiBut *but, uiActivateBut *data)
148 {
149         if(but->type == COL)
150                 ui_set_but_vectorf(but, data->vec);
151         else if(ELEM3(but->type, MENU, ICONROW, ICONTEXTROW))
152                 ui_set_but_val(but, data->value);
153
154         ui_check_but(but);
155         ui_apply_but_func(but);
156         data->retval= but->retval;
157         data->applied= 1;
158 }
159
160 static void ui_apply_but_TOG(uiBlock *block, uiBut *but, uiActivateBut *data)
161 {
162         double value;
163         int w, lvalue, push;
164         
165         /* local hack... */
166         if(but->type==BUT_TOGDUAL && data->togdual) {
167                 if(but->pointype==SHO)
168                         but->poin += 2;
169                 else if(but->pointype==INT)
170                         but->poin += 4;
171         }
172         
173         value= ui_get_but_val(but);
174         lvalue= (int)value;
175         
176         if(but->bit) {
177                 w= BTST(lvalue, but->bitnr);
178                 if(w) lvalue = BCLR(lvalue, but->bitnr);
179                 else lvalue = BSET(lvalue, but->bitnr);
180                 
181                 if(but->type==TOGR) {
182                         if(!data->togonly) {
183                                 lvalue= 1<<(but->bitnr);
184         
185                                 ui_set_but_val(but, (double)lvalue);
186                         }
187                         else {
188                                 if(lvalue==0) lvalue= 1<<(but->bitnr);
189                         }
190                 }
191                 
192                 ui_set_but_val(but, (double)lvalue);
193                 if(but->type==ICONTOG || but->type==ICONTOGN) ui_check_but(but);
194         }
195         else {
196                 
197                 if(value==0.0) push= 1; 
198                 else push= 0;
199                 
200                 if(but->type==TOGN || but->type==ICONTOGN) push= !push;
201                 ui_set_but_val(but, (double)push);
202                 if(but->type==ICONTOG || but->type==ICONTOGN) ui_check_but(but);                
203         }
204         
205         /* end local hack... */
206         if(but->type==BUT_TOGDUAL && data->togdual) {
207                 if(but->pointype==SHO)
208                         but->poin -= 2;
209                 else if(but->pointype==INT)
210                         but->poin -= 4;
211         }
212         
213         ui_apply_but_func(but);
214
215         data->retval= but->retval;
216         data->applied= 1;
217 }
218
219 static void ui_apply_but_ROW(uiBlock *block, uiBut *but, uiActivateBut *data)
220 {
221         ui_set_but_val(but, but->max);
222         ui_apply_but_func(but);
223
224         data->retval= but->retval;
225         data->applied= 1;
226 }
227
228 static void ui_apply_but_TEX(uiBut *but, uiActivateBut *data)
229 {
230         if(!data->str)
231                 return;
232
233         ui_set_but_string(but, data->str);
234         ui_check_but(but);
235
236         /* give butfunc the original text too */
237         /* feature used for bone renaming, channels, etc */
238         if(but->func_arg2==NULL) but->func_arg2= data->origstr;
239         ui_apply_but_func(but);
240         if(but->func_arg2==data->origstr) but->func_arg2= NULL;
241
242         data->retval= but->retval;
243         data->applied= 1;
244 }
245
246 static void ui_apply_but_NUM(uiBut *but, uiActivateBut *data)
247 {
248         if(data->str) {
249                 /* XXX 2.50 missing python api */
250 #if 0
251                 if(BPY_button_eval(data->str, &data->value)) {
252                         WM_report(C, WM_LOG_WARNING, "Invalid Python expression, check console");
253                         data->value = 0.0f; /* Zero out value on error */
254                         
255                         if(data->str[0]) {
256                                 data->cancel= 1; /* invalidate return value if eval failed, except when string was null */
257                                 return;
258                         }
259                 }
260 #else
261                 data->value= atof(data->str);
262 #endif
263
264                 if(!ui_is_but_float(but)) data->value= (int)data->value;
265                 if(but->type==NUMABS) data->value= fabs(data->value);
266                 if(data->value<but->min) data->value= but->min;
267                 if(data->value>but->max) data->value= but->max;
268         }
269
270         ui_set_but_val(but, data->value);
271         ui_check_but(but);
272         ui_apply_but_func(but);
273
274         data->retval= but->retval;
275         data->applied= 1;
276 }
277
278 static void ui_apply_but_LABEL(uiBut *but, uiActivateBut *data)
279 {
280         ui_apply_but_func(but);
281         data->retval= but->retval;
282         data->applied= 1;
283 }
284
285 static void ui_apply_but_TOG3(uiBut *but, uiActivateBut *data)
286
287         if(but->pointype==SHO ) {
288                 short *sp= (short *)but->poin;
289                 
290                 if( BTST(sp[1], but->bitnr)) {
291                         sp[1]= BCLR(sp[1], but->bitnr);
292                         sp[0]= BCLR(sp[0], but->bitnr);
293                 }
294                 else if( BTST(sp[0], but->bitnr)) {
295                         sp[1]= BSET(sp[1], but->bitnr);
296                 } else {
297                         sp[0]= BSET(sp[0], but->bitnr);
298                 }
299         }
300         else {
301                 if( BTST(*(but->poin+2), but->bitnr)) {
302                         *(but->poin+2)= BCLR(*(but->poin+2), but->bitnr);
303                         *(but->poin)= BCLR(*(but->poin), but->bitnr);
304                 }
305                 else if( BTST(*(but->poin), but->bitnr)) {
306                         *(but->poin+2)= BSET(*(but->poin+2), but->bitnr);
307                 } else {
308                         *(but->poin)= BSET(*(but->poin), but->bitnr);
309                 }
310         }
311         
312         ui_check_but(but);
313         ui_apply_but_func(but);
314         data->retval= but->retval;
315         data->applied= 1;
316 }
317
318 static void ui_apply_but_VEC(uiBut *but, uiActivateBut *data)
319 {
320         ui_set_but_vectorf(but, data->vec);
321         ui_check_but(but);
322         ui_apply_but_func(but);
323
324         data->retval= but->retval;
325         data->applied= 1;
326 }
327
328 static void ui_apply_but_COLORBAND(uiBut *but, uiActivateBut *data)
329 {
330         ui_apply_but_func(but);
331         data->retval= but->retval;
332         data->applied= 1;
333 }
334
335 static void ui_apply_but_CURVE(uiBut *but, uiActivateBut *data)
336 {
337         ui_apply_but_func(but);
338         data->retval= but->retval;
339         data->applied= 1;
340 }
341
342 static void ui_apply_but_IDPOIN(uiBut *but, uiActivateBut *data)
343 {
344         but->idpoin_func(data->str, but->idpoin_idpp);
345         ui_check_but(but);
346         ui_apply_but_func(but);
347         data->retval= but->retval;
348         data->applied= 1;
349 }
350
351 #ifdef INTERNATIONAL
352 static void ui_apply_but_CHARTAB(uiBut *but, uiActivateBut *data)
353 {
354         ui_apply_but_func(but);
355         data->retval= but->retval;
356         data->applied= 1;
357 }
358 #endif
359
360 static void ui_apply_button(uiBlock *block, uiBut *but, uiActivateBut *data, int interactive)
361 {
362         char *editstr;
363         double *editval;
364         float *editvec;
365         ColorBand *editcoba;
366         CurveMapping *editcumap;
367
368         data->retval= 0;
369
370         /* if we cancel and have not applied yet, there is nothing to do,
371          * otherwise we have to restore the original value again */
372         if(data->cancel) {
373                 if(!data->applied)
374                         return;
375
376                 if(data->str) MEM_freeN(data->str);
377                 data->str= data->origstr;
378                 data->origstr= NULL;
379                 data->value= data->origvalue;
380                 data->origvalue= 0.0;
381                 VECCOPY(data->vec, data->origvec);
382                 data->origvec[0]= data->origvec[1]= data->origvec[2]= 0.0f;
383         }
384         else {
385                 /* we avoid applying interactive edits a second time
386                  * at the end with the appliedinteractive flag */
387                 if(interactive)
388                         data->appliedinteractive= 1;
389                 else if(data->appliedinteractive)
390                         return;
391         }
392
393         /* ensures we are writing actual values */
394         editstr= but->editstr;
395         editval= but->editval;
396         editvec= but->editvec;
397         editcoba= but->editcoba;
398         editcumap= but->editcumap;
399         but->editstr= NULL;
400         but->editval= NULL;
401         but->editvec= NULL;
402         but->editcoba= NULL;
403         but->editcumap= NULL;
404
405         /* handle different types */
406         switch(but->type) {
407                 case BUT:
408                         ui_apply_but_BUT(but, data);
409                         break;
410                 case TEX:
411                         ui_apply_but_TEX(but, data);
412                         break;
413                 case TOG: 
414                 case TOGR: 
415                 case ICONTOG:
416                 case ICONTOGN:
417                 case TOGN:
418                 case BUT_TOGDUAL:
419                         ui_apply_but_TOG(block, but, data);
420                         break;
421                 case ROW:
422                         ui_apply_but_ROW(block, but, data);
423                         break;
424                 case SCROLL:
425                         break;
426                 case NUM:
427                 case NUMABS:
428                         ui_apply_but_NUM(but, data);
429                         break;
430                 case SLI:
431                 case NUMSLI:
432                         ui_apply_but_NUM(but, data);
433                         break;
434                 case HSVSLI:
435                         break;
436                 case ROUNDBOX:  
437                 case LABEL:     
438                         ui_apply_but_LABEL(but, data);
439                         break;
440                 case TOG3:      
441                         ui_apply_but_TOG3(but, data);
442                         break;
443                 case MENU:
444                 case ICONROW:
445                 case ICONTEXTROW:
446                 case BLOCK:
447                 case PULLDOWN:
448                 case COL:
449                         ui_apply_but_BLOCK(but, data);
450                         break;
451                 case BUTM:
452                         ui_apply_but_BUTM(but, data);
453                         break;
454                 case BUT_NORMAL:
455                 case HSVCUBE:
456                         ui_apply_but_VEC(but, data);
457                         break;
458                 case BUT_COLORBAND:
459                         ui_apply_but_COLORBAND(but, data);
460                         break;
461                 case BUT_CURVE:
462                         ui_apply_but_CURVE(but, data);
463                         break;
464                 case IDPOIN:
465                         ui_apply_but_IDPOIN(but, data);
466                         break;
467 #ifdef INTERNATIONAL
468                 case CHARTAB:
469                         ui_apply_but_CHARTAB(but, data);
470                         break;
471 #endif
472                 case LINK:
473                 case INLINK:
474                         break;
475         }
476
477         but->editstr= editstr;
478         but->editval= editval;
479         but->editvec= editvec;
480         but->editcoba= editcoba;
481         but->editcumap= editcumap;
482 }
483
484 /* ******************* copy and paste ********************  */
485
486 /* c = copy, v = paste */
487 static void ui_but_copy_paste(bContext *C, uiBut *but, uiActivateBut *data, char mode)
488 {
489         static ColorBand but_copypaste_coba = {0};
490         char buf[UI_MAX_DRAW_STR+1]= {0};
491         double val;
492         
493         if(mode=='v' && but->lock)
494                 return;
495
496         if(mode=='v') {
497                 /* extract first line from clipboard in case of multi-line copies */
498                 char *p = NULL; /* XXX 2.48 getClipboard(0); */
499                 if(p) {
500                         int i = 0;
501                         while (*p && *p!='\r' && *p!='\n' && i<UI_MAX_DRAW_STR) {
502                                 buf[i++]=*p;
503                                 p++;
504                         }
505                         buf[i]= 0;
506                 }
507         }
508         
509         /* numeric value */
510         if ELEM4(but->type, NUM, NUMABS, NUMSLI, HSVSLI) {
511                 
512                 if(but->poin==NULL && but->rnapoin.data==NULL);
513                 else if(mode=='c') {
514                         sprintf(buf, "%f", ui_get_but_val(but));
515                         /* XXX 2.48 putClipboard(buf, 0); */
516                 }
517                 else {
518                         if (sscanf(buf, " %lf ", &val) == 1) {
519                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
520                                 data->value= val;
521                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
522                         }
523                 }
524         }
525
526         /* RGB triple */
527         else if(but->type==COL) {
528                 float rgb[3];
529                 
530                 if(but->poin==NULL && but->rnapoin.data==NULL);
531                 else if(mode=='c') {
532
533                         ui_get_but_vectorf(but, rgb);
534                         sprintf(buf, "[%f, %f, %f]", rgb[0], rgb[1], rgb[2]);
535                         /* XXX 2.48 putClipboard(buf, 0); */
536                         
537                 }
538                 else {
539                         if (sscanf(buf, "[%f, %f, %f]", &rgb[0], &rgb[1], &rgb[2]) == 3) {
540                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
541                                 VECCOPY(data->vec, rgb);
542                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
543                         }
544                 }
545         }
546
547         /* text/string and ID data */
548         else if(ELEM(but->type, TEX, IDPOIN)) {
549                 uiActivateBut *data= but->activate;
550
551                 if(but->poin==NULL && but->rnapoin.data==NULL);
552                 else if(mode=='c') {
553                         button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
554                         BLI_strncpy(buf, data->str, UI_MAX_DRAW_STR);
555                         /* XXX 2.48 putClipboard(data->str, 0); */
556                         data->cancel= 1;
557                         button_activate_state(C, but, BUTTON_STATE_EXIT);
558                 }
559                 else {
560                         button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
561                         BLI_strncpy(data->str, buf, data->maxlen);
562                         button_activate_state(C, but, BUTTON_STATE_EXIT);
563                 }
564         }
565         /* colorband (not supported by system clipboard) */
566         else if(but->type==BUT_COLORBAND) {
567                 if(mode=='c') {
568                         if(but->poin)
569                                 return;
570
571                         memcpy(&but_copypaste_coba, but->poin, sizeof(ColorBand));
572                 }
573                 else {
574                         if(but_copypaste_coba.tot==0)
575                                 return;
576
577                         if(!but->poin)
578                                 but->poin= MEM_callocN(sizeof(ColorBand), "colorband");
579
580                         button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
581                         memcpy(data->coba, &but_copypaste_coba, sizeof(ColorBand) );
582                         button_activate_state(C, but, BUTTON_STATE_EXIT);
583                 }
584         }
585 }
586
587 /* ************* in-button text selection/editing ************* */
588
589 /* return 1 if char ch is special character, otherwise return 0 */
590 static short test_special_char(char ch)
591 {
592         switch(ch) {
593                 case '\\':
594                 case '/':
595                 case '~':
596                 case '!':
597                 case '@':
598                 case '#':
599                 case '$':
600                 case '%':
601                 case '^':
602                 case '&':
603                 case '*':
604                 case '(':
605                 case ')':
606                 case '+':
607                 case '=':
608                 case '{':
609                 case '}':
610                 case '[':
611                 case ']':
612                 case ':':
613                 case ';':
614                 case '\'':
615                 case '\"':
616                 case '<':
617                 case '>':
618                 case ',':
619                 case '.':
620                 case '?':
621                 case '_':
622                 case '-':
623                 case ' ':
624                         return 1;
625                         break;
626                 default:
627                         break;
628         }
629         return 0;
630 }
631
632 static int ui_textedit_delete_selection(uiBut *but, uiActivateBut *data)
633 {
634         char *str;
635         int x, changed;
636         
637         str= data->str;
638         changed= (but->selsta != but->selend);
639         
640         for(x=0; x< strlen(str); x++) {
641                 if (but->selend + x <= strlen(str) ) {
642                         str[but->selsta + x]= str[but->selend + x];
643                 } else {
644                         str[but->selsta + x]= '\0';
645                         break;
646                 }
647         }
648
649         but->pos = but->selend = but->selsta;
650
651         return changed;
652 }
653
654 static void ui_textedit_set_cursor_pos(uiBut *but, uiActivateBut *data, short x)
655 {
656         char *origstr;
657         
658         origstr= MEM_callocN(sizeof(char)*(data->maxlen+1), "ui_textedit origstr");
659         
660         BLI_strncpy(origstr, but->drawstr, data->maxlen+1);
661         but->pos= strlen(origstr)-but->ofs;
662         
663         while((but->aspect*UI_GetStringWidth(but->font, origstr+but->ofs, 0) + but->x1) > x) {
664                 if (but->pos <= 0) break;
665                 but->pos--;
666                 origstr[but->pos+but->ofs] = 0;
667         }
668         
669         but->pos -= strlen(but->str);
670         but->pos += but->ofs;
671         if(but->pos<0) but->pos= 0;
672
673         MEM_freeN(origstr);
674 }
675
676 static void ui_textedit_set_cursor_select(uiBut *but, uiActivateBut *data, short x)
677 {
678         if (x > data->selstartx) data->selextend = EXTEND_RIGHT;
679         else if (x < data->selstartx) data->selextend = EXTEND_LEFT;
680
681         ui_textedit_set_cursor_pos(but, data, x);
682                                                 
683         if (data->selextend == EXTEND_RIGHT) but->selend = but->pos;
684         if (data->selextend == EXTEND_LEFT) but->selsta = but->pos;
685
686         ui_check_but(but);
687 }
688
689 static int ui_textedit_type_ascii(uiBut *but, uiActivateBut *data, char ascii)
690 {
691         char *str;
692         int len, x, changed= 0;
693
694         str= data->str;
695         len= strlen(str);
696
697         if(len-(but->selend - but->selsta)+1 <= data->maxlen) {
698                 /* type over the current selection */
699                 if ((but->selend - but->selsta) > 0)
700                         changed= ui_textedit_delete_selection(but, data);
701
702                 len= strlen(str);
703                 if(len < data->maxlen) {
704                         for(x= data->maxlen; x>but->pos; x--)
705                                 str[x]= str[x-1];
706                         str[but->pos]= ascii;
707                         str[len+1]= '\0';
708
709                         but->pos++; 
710                         changed= 1;
711                 }
712         }
713
714         return 1;
715 }
716
717 void ui_textedit_move(uiBut *but, uiActivateBut *data, int direction, int select, int jump)
718 {
719         char *str;
720         int len;
721
722         str= data->str;
723         len= strlen(str);
724
725         if(direction) { /* right*/
726                 /* if there's a selection */
727                 if ((but->selend - but->selsta) > 0) {
728                         /* extend the selection based on the first direction taken */
729                         if(select) {
730                                 if (!data->selextend) {
731                                         data->selextend = EXTEND_RIGHT;
732                                 }
733                                 if (data->selextend == EXTEND_RIGHT) {
734                                         but->selend++;
735                                         if (but->selend > len) but->selend = len;
736                                 } else if (data->selextend == EXTEND_LEFT) {
737                                         but->selsta++;
738                                         /* if the selection start has gone past the end,
739                                         * flip them so they're in sync again */
740                                         if (but->selsta == but->selend) {
741                                                 but->pos = but->selsta;
742                                                 data->selextend = EXTEND_RIGHT;
743                                         }
744                                 }
745                         } else {
746                                 but->selsta = but->pos = but->selend;
747                                 data->selextend = 0;
748                         }
749                 } else {
750                         if(select) {
751                                 /* make a selection, starting from the cursor position */
752                                 but->selsta = but->pos;
753                                 
754                                 but->pos++;
755                                 if(but->pos>strlen(str)) but->pos= strlen(str);
756                                 
757                                 but->selend = but->pos;
758                         } else if(jump) {
759                                 /* jump betweenn special characters (/,\,_,-, etc.),
760                                  * look at function test_special_char() for complete
761                                  * list of special character, ctr -> */
762                                 while(but->pos < len) {
763                                         but->pos++;
764                                         if(test_special_char(str[but->pos])) break;
765                                 }
766                         } else {
767                                 but->pos++;
768                                 if(but->pos>strlen(str)) but->pos= strlen(str);
769                         }
770                 }
771         }
772         else { /* left */
773                 /* if there's a selection */
774                 if ((but->selend - but->selsta) > 0) {
775                         /* extend the selection based on the first direction taken */
776                         if(select) {
777                                 if (!data->selextend) {
778                                         data->selextend = EXTEND_LEFT;
779                                 }
780                                 if (data->selextend == EXTEND_LEFT) {
781                                         but->selsta--;
782                                         if (but->selsta < 0) but->selsta = 0;
783                                 } else if (data->selextend == EXTEND_RIGHT) {
784                                         but->selend--;
785                                         /* if the selection start has gone past the end,
786                                         * flip them so they're in sync again */
787                                         if (but->selsta == but->selend) {
788                                                 but->pos = but->selsta;
789                                                 data->selextend = EXTEND_LEFT;
790                                         }
791                                 }
792                         } else {
793                                 but->pos = but->selend = but->selsta;
794                                 data->selextend = 0;
795                         }
796                 } else {
797                         if(select) {
798                                 /* make a selection, starting from the cursor position */
799                                 but->selend = but->pos;
800                                 
801                                 but->pos--;
802                                 if(but->pos<0) but->pos= 0;
803                                 
804                                 but->selsta = but->pos;
805                         } else if(jump) {
806                                 /* jump betweenn special characters (/,\,_,-, etc.),
807                                  * look at function test_special_char() for complete
808                                  * list of special character, ctr -> */
809                                 while(but->pos > 0){
810                                         but->pos--;
811                                         if(test_special_char(str[but->pos])) break;
812                                 }
813                         } else {
814                                 if(but->pos>0) but->pos--;
815                         }
816                 }
817         }
818 }
819
820 void ui_textedit_move_end(uiBut *but, uiActivateBut *data, int direction, int select)
821 {
822         char *str;
823
824         str= data->str;
825
826         if(direction) { /* right */
827                 if(select) {
828                         but->selsta = but->pos;
829                         but->selend = strlen(str);
830                         data->selextend = EXTEND_RIGHT;
831                 } else {
832                         but->selsta = but->selend = but->pos= strlen(str);
833                 }
834         }
835         else { /* left */
836                 if(select) {
837                         but->selend = but->pos;
838                         but->selsta = 0;
839                         data->selextend = EXTEND_LEFT;
840                 } else {
841                         but->selsta = but->selend = but->pos= 0;
842                 }
843         }
844 }
845
846 static int ui_textedit_delete(uiBut *but, uiActivateBut *data, int direction, int all)
847 {
848         char *str;
849         int len, x, changed= 0;
850
851         str= data->str;
852         len= strlen(str);
853
854         if(all) {
855                 if(len) changed=1;
856                 str[0]= 0;
857                 but->pos= 0;
858         }
859         else if(direction) { /* delete */
860                 if ((but->selend - but->selsta) > 0) {
861                         changed= ui_textedit_delete_selection(but, data);
862                 }
863                 else if(but->pos>=0 && but->pos<len) {
864                         for(x=but->pos; x<len; x++)
865                                 str[x]= str[x+1];
866                         str[len-1]='\0';
867                         changed= 1;
868                 }
869         }
870         else { /* backspace */
871                 if(len!=0) {
872                         if ((but->selend - but->selsta) > 0) {
873                                 ui_textedit_delete_selection(but, data);
874                         }
875                         else if(but->pos>0) {
876                                 for(x=but->pos; x<len; x++)
877                                         str[x-1]= str[x];
878                                 str[len-1]='\0';
879
880                                 but->pos--;
881                                 changed= 1;
882                         }
883                 } 
884         }
885
886         return changed;
887 }
888
889 static int ui_textedit_autocomplete(uiBut *but, uiActivateBut *data)
890 {
891         char *str;
892         int changed= 1;
893
894         str= data->str;
895         but->autocomplete_func(str, but->autofunc_arg);
896         but->pos= strlen(str);
897
898         return changed;
899 }
900
901 static int ui_textedit_copypaste(uiBut *but, uiActivateBut *data, int paste, int copy, int cut)
902 {
903         char buf[UI_MAX_DRAW_STR]={0};
904         char *str, *p;
905         int len, x, y, i, changed= 0;
906
907         str= data->str;
908         len= strlen(str);
909         
910         /* paste */
911         if (paste) {
912                 /* extract the first line from the clipboard */
913                 p = NULL; /* XXX 2.48 getClipboard(0); */
914
915                 if(p && p[0]) {
916                         while (*p && *p!='\r' && *p!='\n' && i<UI_MAX_DRAW_STR) {
917                                 buf[i++]=*p;
918                                 p++;
919                         }
920                         buf[i]= 0;
921
922                         /* paste over the current selection */
923                         if ((but->selend - but->selsta) > 0)
924                                 ui_textedit_delete_selection(but, data);
925                         
926                         for (y=0; y<strlen(buf); y++)
927                         {
928                                 /* add contents of buffer */
929                                 if(len < data->maxlen) {
930                                         for(x= data->maxlen; x>but->pos; x--)
931                                                 str[x]= str[x-1];
932                                         str[but->pos]= buf[y];
933                                         but->pos++; 
934                                         len++;
935                                         str[len]= '\0';
936                                 }
937                         }
938
939                         changed= 1;
940                 }
941         }
942         /* cut & copy */
943         else if (copy || cut) {
944                 /* copy the contents to the copypaste buffer */
945                 for(x= but->selsta; x <= but->selend; x++) {
946                         if (x==but->selend)
947                                 buf[x] = '\0';
948                         else
949                                 buf[(x - but->selsta)] = str[x];
950                 }
951                 /* XXX 2.48 putClipboard(buf, 0); */
952                 
953                 /* for cut only, delete the selection afterwards */
954                 if(cut)
955                         if((but->selend - but->selsta) > 0)
956                                 changed= ui_textedit_delete_selection(but, data);
957         } 
958
959         return changed;
960 }
961
962 static void ui_textedit_begin(uiBut *but, uiActivateBut *data)
963 {
964         if(data->str) {
965                 MEM_freeN(data->str);
966                 data->str= NULL;
967         }
968
969         /* retrieve string */
970         if(but->type == TEX) {
971                 data->maxlen= but->max;
972                 data->str= MEM_callocN(sizeof(char)*(data->maxlen+1), "textedit str");
973
974                 ui_get_but_string(but, data->str, data->maxlen+1);
975         }
976         else if(but->type == IDPOIN) {
977                 ID *id;
978                 
979                 data->maxlen= 22;
980                 data->str= MEM_callocN(sizeof(char)*(data->maxlen+1), "textedit str");
981
982                 id= *but->idpoin_idpp;
983                 if(id) BLI_strncpy(data->str, id->name+2, data->maxlen+1);
984                 else data->str[0]= 0;
985         }
986         else {
987                 double value;
988
989                 data->maxlen= UI_MAX_DRAW_STR;
990                 data->str= MEM_callocN(sizeof(char)*(data->maxlen+1), "textedit str");
991                 
992                 value= ui_get_but_val(but);
993                 if(ui_is_but_float(but)) {
994                         if(but->a2) { /* amount of digits defined */
995                                 if(but->a2==1) sprintf(data->str, "%.1f", value);
996                                 else if(but->a2==2) sprintf(data->str, "%.2f", value);
997                                 else if(but->a2==3) sprintf(data->str, "%.3f", value);
998                                 else sprintf(data->str, "%.4f", value);
999                         }
1000                         else sprintf(data->str, "%.3f", value);
1001                 }
1002                 else {
1003                         sprintf(data->str, "%d", (int)value);
1004                 }
1005         }
1006
1007         data->origstr= BLI_strdup(data->str);
1008         data->selextend= 0;
1009         data->selstartx= 0;
1010
1011         /* set cursor pos to the end of the text */
1012         but->editstr= data->str;
1013         but->pos= strlen(data->str);
1014         but->selsta= 0;
1015         but->selend= strlen(but->drawstr) - strlen(but->str);
1016
1017         ui_check_but(but);
1018 }
1019
1020 static void ui_textedit_end(uiBut *but, uiActivateBut *data)
1021 {
1022         if(but) {
1023                 but->editstr= 0;
1024                 but->pos= -1;
1025         }
1026 }
1027
1028 static void ui_textedit_next_but(uiBlock *block, uiBut *actbut)
1029 {
1030         uiBut *but;
1031
1032         /* label and roundbox can overlap real buttons (backdrops...) */
1033         if(actbut->type==LABEL && actbut->type==ROUNDBOX)
1034                 return;
1035
1036         for(but= actbut->next; but; but= but->next) {
1037                 if(ELEM5(but->type, TEX, NUM, NUMABS, NUMSLI, HSVSLI)) {
1038                         but->activateflag= UI_ACTIVATE_TEXT_EDITING;
1039                         return;
1040                 }
1041         }
1042         for(but= block->buttons.first; but!=actbut; but= but->next) {
1043                 if(ELEM5(but->type, TEX, NUM, NUMABS, NUMSLI, HSVSLI)) {
1044                         but->activateflag= UI_ACTIVATE_TEXT_EDITING;
1045                         return;
1046                 }
1047         }
1048 }
1049
1050 static void ui_textedit_prev_but(uiBlock *block, uiBut *actbut)
1051 {
1052         uiBut *but;
1053
1054         /* label and roundbox can overlap real buttons (backdrops...) */
1055         if(actbut->type==LABEL && actbut->type==ROUNDBOX)
1056                 return;
1057
1058         for(but= actbut->prev; but; but= but->prev) {
1059                 if(ELEM5(but->type, TEX, NUM, NUMABS, NUMSLI, HSVSLI)) {
1060                         but->activateflag= UI_ACTIVATE_TEXT_EDITING;
1061                         return;
1062                 }
1063         }
1064         for(but= block->buttons.last; but!=actbut; but= but->prev) {
1065                 if(ELEM5(but->type, TEX, NUM, NUMABS, NUMSLI, HSVSLI)) {
1066                         but->activateflag= UI_ACTIVATE_TEXT_EDITING;
1067                         return;
1068                 }
1069         }
1070 }
1071
1072 static void ui_do_but_textedit(bContext *C, uiBlock *block, uiBut *but, uiActivateBut *data, wmEvent *event)
1073 {
1074         int mx, my, changed= 0, handled= 0;
1075
1076         switch(event->type) {
1077                 case RIGHTMOUSE:
1078                 case ESCKEY:
1079                         data->cancel= 1;
1080                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1081                         handled= 1;
1082                         break;
1083                 case LEFTMOUSE: {
1084                         if(event->val) {
1085                                 mx= event->x;
1086                                 my= event->y;
1087                                 ui_window_to_block(data->region, block, &mx, &my);
1088
1089                                 if ((but->y1 <= my) && (my <= but->y2) && (but->x1 <= mx) && (mx <= but->x2)) {
1090                                         ui_textedit_set_cursor_pos(but, data, mx);
1091                                         but->selsta = but->selend = but->pos;
1092                                         data->selstartx= mx;
1093
1094                                         button_activate_state(C, but, BUTTON_STATE_TEXT_SELECTING);
1095                                         handled= 1;
1096                                 }
1097                                 else {
1098                                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1099                                         handled= 1;
1100                                 }
1101                         }
1102                         break;
1103                 }
1104         }
1105
1106         if(event->val) {
1107                 switch (event->type) {
1108                         case VKEY:
1109                         case XKEY:
1110                         case CKEY:
1111                                 if(event->ctrl || event->oskey) {
1112                                         if(event->type == VKEY)
1113                                                 changed= ui_textedit_copypaste(but, data, 1, 0, 0);
1114                                         else if(event->type == XKEY)
1115                                                 changed= ui_textedit_copypaste(but, data, 0, 1, 0);
1116                                         else if(event->type == CKEY)
1117                                                 changed= ui_textedit_copypaste(but, data, 0, 0, 1);
1118
1119                                         handled= 1;
1120                                 }
1121                                 break;
1122                         case RIGHTARROWKEY:
1123                                 ui_textedit_move(but, data, 1, event->shift, event->ctrl);
1124                                 handled= 1;
1125                                 break;
1126                         case LEFTARROWKEY:
1127                                 ui_textedit_move(but, data, 0, event->shift, event->ctrl);
1128                                 handled= 1;
1129                                 break;
1130                         case DOWNARROWKEY:
1131                         case ENDKEY:
1132                                 ui_textedit_move_end(but, data, 1, event->shift);
1133                                 handled= 1;
1134                                 break;
1135                         case UPARROWKEY:
1136                         case HOMEKEY:
1137                                 ui_textedit_move_end(but, data, 0, event->shift);
1138                                 handled= 1;
1139                                 break;
1140                         case PADENTER:
1141                         case RETKEY:
1142                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
1143                                 handled= 1;
1144                                 break;
1145                         case DELKEY:
1146                                 changed= ui_textedit_delete(but, data, 1, 0);
1147                                 handled= 1;
1148                                 break;
1149
1150                         case BACKSPACEKEY:
1151                                 changed= ui_textedit_delete(but, data, 0, event->shift);
1152                                 handled= 1;
1153                                 break;
1154                                 
1155                         case TABKEY:
1156                                 /* there is a key conflict here, we can't tab with autocomplete */
1157                                 if(but->autocomplete_func) {
1158                                         changed= ui_textedit_autocomplete(but, data);
1159                                         handled= 1;
1160                                 }
1161                                 /* the hotkey here is not well defined, was G.qual so we check all */
1162                                 else if(event->shift || event->ctrl || event->alt || event->oskey) {
1163                                         ui_textedit_prev_but(block, but);
1164                                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1165                                 }
1166                                 else {
1167                                         ui_textedit_next_but(block, but);
1168                                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1169                                 }
1170                                 handled= 1;
1171                                 break;
1172                 }
1173
1174                 if(event->ascii && !handled) {
1175                         changed= ui_textedit_type_ascii(but, data, event->ascii);
1176                         handled= 1;
1177                 }
1178         }
1179
1180         if(changed) {
1181                 if(data->interactive) ui_apply_button(block, but, data, 1);
1182                 else ui_check_but(but);
1183         }
1184
1185         if(changed || handled)
1186                 WM_event_add_notifier(C, WM_NOTE_WINDOW_REDRAW, 0, NULL);
1187 }
1188
1189 static void ui_do_but_textedit_select(bContext *C, uiBlock *block, uiBut *but, uiActivateBut *data, wmEvent *event)
1190 {
1191         int mx, my, handled= 0;
1192
1193         switch(event->type) {
1194                 case MOUSEMOVE: {
1195                         mx= event->x;
1196                         my= event->y;
1197                         ui_window_to_block(data->region, block, &mx, &my);
1198
1199                         ui_textedit_set_cursor_select(but, data, mx);
1200                         handled= 1;
1201                         break;
1202                 }
1203                 case LEFTMOUSE:
1204                         if(event->val == 0)
1205                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
1206                         handled= 1;
1207                         break;
1208         }
1209
1210         if(handled) {
1211                 ui_check_but(but);
1212                 WM_event_add_notifier(C, WM_NOTE_WINDOW_REDRAW, 0, NULL);
1213         }
1214 }
1215
1216 /* ************* number editing for various types ************* */
1217
1218 static void ui_numedit_begin(uiBut *but, uiActivateBut *data)
1219 {
1220         float butrange;
1221
1222         if(but->type == BUT_CURVE) {
1223                 data->cumap= (CurveMapping*)but->poin;
1224                 but->editcumap= data->coba;
1225         }
1226         else if(but->type == BUT_COLORBAND) {
1227                 data->coba= (ColorBand*)but->poin;
1228                 but->editcoba= data->coba;
1229         }
1230         else if(ELEM(but->type, BUT_NORMAL, HSVCUBE)) {
1231                 ui_get_but_vectorf(but, data->origvec);
1232                 VECCOPY(data->vec, data->origvec);
1233                 but->editvec= data->vec;
1234         }
1235         else {
1236                 data->origvalue= ui_get_but_val(but);
1237                 data->value= data->origvalue;
1238                 but->editval= &data->value;
1239
1240                 butrange= (but->max - but->min);
1241                 data->dragfstart= (butrange == 0.0)? 0.0f: (data->value - but->min)/butrange;
1242                 data->dragf= data->dragfstart;
1243         }
1244
1245         data->dragchange= 0;
1246         data->draglock= 1;
1247 }
1248
1249 static void ui_numedit_end(uiBut *but, uiActivateBut *data)
1250 {
1251         but->editval= NULL;
1252         but->editvec= NULL;
1253         but->editcoba= NULL;
1254         but->editcumap= NULL;
1255
1256         data->dragstartx= 0;
1257         data->draglastx= 0;
1258         data->dragchange= 0;
1259         data->dragcbd= NULL;
1260         data->dragsel= 0;
1261 }
1262
1263 static void ui_numedit_apply(bContext *C, uiBlock *block, uiBut *but, uiActivateBut *data)
1264 {
1265         if(data->interactive) ui_apply_button(block, but, data, 1);
1266         else ui_check_but(but);
1267
1268         WM_event_add_notifier(C, WM_NOTE_WINDOW_REDRAW, 0, NULL);
1269 }
1270
1271 /* ****************** block opening for various types **************** */
1272
1273 static void ui_blockopen_begin(bContext *C, uiBut *but, uiActivateBut *data)
1274 {
1275         uiBlockFuncFP func= NULL;
1276         void *arg= NULL;
1277
1278         switch(but->type) {
1279                 case BLOCK:
1280                 case PULLDOWN:
1281                         func= but->block_func;
1282                         arg= but->poin;
1283                         break;
1284                 case MENU:
1285                         data->origvalue= ui_get_but_val(but);
1286                         data->value= data->origvalue;
1287                         but->editval= &data->value;
1288
1289                         func= ui_block_func_MENU;
1290                         arg= but;
1291                         break;
1292                 case ICONROW:
1293                         func= ui_block_func_ICONROW;
1294                         arg= but;
1295                         break;
1296                 case ICONTEXTROW:
1297                         func= ui_block_func_ICONTEXTROW;
1298                         arg= but;
1299                         break;
1300                 case COL:
1301                         ui_get_but_vectorf(but, data->origvec);
1302                         VECCOPY(data->vec, data->origvec);
1303                         but->editvec= data->vec;
1304
1305                         func= ui_block_func_COL;
1306                         arg= but;
1307                         break;
1308         }
1309
1310         if(func)
1311                 data->blockhandle= ui_menu_block_create(C, data->region, but, func, arg);
1312
1313         /* this makes adjacent blocks auto open from now on */
1314         if(but->block->auto_open==0) but->block->auto_open= 1;
1315 }
1316
1317 static void ui_blockopen_end(bContext *C, uiBut *but, uiActivateBut *data)
1318 {
1319         if(but) {
1320                 but->editval= NULL;
1321                 but->editvec= NULL;
1322
1323                 but->block->auto_open_last= PIL_check_seconds_timer();
1324         }
1325
1326         if(data->blockhandle) {
1327                 ui_menu_block_free(C, data->blockhandle);
1328                 data->blockhandle= NULL;
1329         }
1330 }
1331
1332 /* ***************** events for different button types *************** */
1333
1334 static int ui_do_but_BUT(bContext *C, uiBut *but, uiActivateBut *data, wmEvent *event)
1335 {
1336         if(data->state == BUTTON_STATE_HIGHLIGHT) {
1337                 if(event->type == LEFTMOUSE && event->val) {
1338                         button_activate_state(C, but, BUTTON_STATE_WAIT_RELEASE);
1339                         return 1;
1340                 }
1341                 else if(ELEM(event->type, PADENTER, RETKEY) && event->val) {
1342                         button_activate_state(C, but, BUTTON_STATE_WAIT_FLASH);
1343                         return 1;
1344                 }
1345         }
1346         else if(data->state == BUTTON_STATE_WAIT_RELEASE) {
1347                 if(event->type == LEFTMOUSE && event->val==0) {
1348                         if(!(but->flag & UI_SELECT))
1349                                 data->cancel= 1;
1350                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1351                         return 1;
1352                 }
1353         }
1354         return 0;
1355 }
1356
1357 static int ui_do_but_KEYEVT(bContext *C, uiBut *but, uiActivateBut *data, wmEvent *event)
1358 {
1359         if(data->state == BUTTON_STATE_HIGHLIGHT) {
1360                 if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val) {
1361                         button_activate_state(C, but, BUTTON_STATE_WAIT_KEY_EVENT);
1362                         return 1;
1363                 }
1364         }
1365         else if(data->state == BUTTON_STATE_WAIT_KEY_EVENT) {
1366                 if(event->type == MOUSEMOVE)
1367                         return 0;
1368
1369                 /* XXX 2.50 missing function */
1370 #if 0
1371                 if(event->val) {
1372                         if(!key_event_to_string(event)[0])
1373                                 data->cancel= 1;
1374                         else
1375                                 ui_set_but_val(but, event->type);
1376
1377                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1378                 }
1379 #endif
1380         }
1381         return 0;
1382 }
1383
1384 static int ui_do_but_TEX(bContext *C, uiBlock *block, uiBut *but, uiActivateBut *data, wmEvent *event)
1385 {
1386         if(data->state == BUTTON_STATE_HIGHLIGHT) {
1387                 if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val) {
1388                         button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
1389                         return 1;
1390                 }
1391         }
1392         else if(data->state == BUTTON_STATE_TEXT_EDITING) {
1393                 ui_do_but_textedit(C, block, but, data, event);
1394                 return 1;
1395         }
1396         else if(data->state == BUTTON_STATE_TEXT_SELECTING) {
1397                 ui_do_but_textedit_select(C, block, but, data, event);
1398                 return 1;
1399         }
1400         return 0;
1401 }
1402
1403 static int ui_do_but_TOG(bContext *C, uiBut *but, uiActivateBut *data, wmEvent *event)
1404 {
1405         if(data->state == BUTTON_STATE_HIGHLIGHT) {
1406                 if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val) {
1407                         data->togdual= event->ctrl;
1408                         data->togonly= !event->shift;
1409                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1410                         return 1;
1411                 }
1412         }
1413         return 0;
1414 }
1415
1416 static int ui_do_but_EXIT(bContext *C, uiBut *but, uiActivateBut *data, wmEvent *event)
1417 {
1418         if(data->state == BUTTON_STATE_HIGHLIGHT) {
1419                 if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val) {
1420                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1421                         return 1;
1422                 }
1423         }
1424         return 0;
1425 }
1426
1427 static int ui_numedit_but_NUM(uiBut *but, uiActivateBut *data, float fac, int snap, int mx)
1428 {
1429         float deler, tempf;
1430         int lvalue, temp, changed= 0;
1431         
1432         if(mx == data->draglastx)
1433                 return changed;
1434         
1435         /* drag-lock - prevent unwanted scroll adjustments */
1436         /* change value (now 3) to adjust threshold in pixels */
1437         if(data->draglock) {
1438                 if(abs(mx-data->dragstartx) <= 3)
1439                         return changed;
1440
1441                 data->draglock= 0;
1442                 data->dragstartx= mx;  /* ignore mouse movement within drag-lock */
1443         }
1444
1445         deler= 500;
1446         if(!ui_is_but_float(but)) {
1447                 if((but->max-but->min)<100) deler= 200.0;
1448                 if((but->max-but->min)<25) deler= 50.0;
1449         }
1450         deler /= fac;
1451
1452         if(ui_is_but_float(but) && but->max-but->min > 11) {
1453                 /* non linear change in mouse input- good for high precicsion */
1454                 data->dragf+= (((float)(mx-data->draglastx))/deler) * (fabs(data->dragstartx-mx)*0.002);
1455         } else if (!ui_is_but_float(but) && but->max-but->min > 129) { /* only scale large int buttons */
1456                 /* non linear change in mouse input- good for high precicsionm ints need less fine tuning */
1457                 data->dragf+= (((float)(mx-data->draglastx))/deler) * (fabs(data->dragstartx-mx)*0.004);
1458         } else {
1459                 /*no scaling */
1460                 data->dragf+= ((float)(mx-data->draglastx))/deler ;
1461         }
1462         
1463         if(data->dragf>1.0) data->dragf= 1.0;
1464         if(data->dragf<0.0) data->dragf= 0.0;
1465         data->draglastx= mx;
1466         tempf= ( but->min + data->dragf*(but->max-but->min));
1467         
1468         if(!ui_is_but_float(but)) {
1469                 
1470                 temp= floor(tempf+.5);
1471                 
1472                 if(tempf==but->min || tempf==but->max);
1473                 else if(snap) {
1474                         if(snap == 2) temp= 100*(temp/100);
1475                         else temp= 10*(temp/10);
1476                 }
1477                 if( temp>=but->min && temp<=but->max) {
1478                         lvalue= (int)data->value;
1479                         
1480                         if(temp != lvalue ) {
1481                                 data->dragchange= 1;
1482                                 data->value= (double)temp;
1483                                 changed= 1;
1484                         }
1485                 }
1486
1487         }
1488         else {
1489                 temp= 0;
1490                 if(snap) {
1491                         if(snap == 2) {
1492                                 if(tempf==but->min || tempf==but->max);
1493                                 else if(but->max-but->min < 2.10) tempf= 0.01*floor(100.0*tempf);
1494                                 else if(but->max-but->min < 21.0) tempf= 0.1*floor(10.0*tempf);
1495                                 else tempf= floor(tempf);
1496                         }
1497                         else {
1498                                 if(tempf==but->min || tempf==but->max);
1499                                 else if(but->max-but->min < 2.10) tempf= 0.1*floor(10*tempf);
1500                                 else if(but->max-but->min < 21.0) tempf= floor(tempf);
1501                                 else tempf= 10.0*floor(tempf/10.0);
1502                         }
1503                 }
1504
1505                 if( tempf>=but->min && tempf<=but->max) {
1506                         if(tempf != data->value) {
1507                                 data->dragchange= 1;
1508                                 data->value= tempf;
1509                                 changed= 1;
1510                         }
1511                 }
1512         }
1513
1514         return changed;
1515 }
1516
1517 static int ui_do_but_NUM(bContext *C, uiBlock *block, uiBut *but, uiActivateBut *data, wmEvent *event)
1518 {
1519         int mx, my, click= 0;
1520         int handled= 0;
1521         
1522         mx= event->x;
1523         my= event->y;
1524         ui_window_to_block(data->region, block, &mx, &my);
1525
1526         if(data->state == BUTTON_STATE_HIGHLIGHT) {
1527                 if(event->val) {
1528                         if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->shift) {
1529                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
1530                                 handled= 1;
1531                         }
1532                         else if(event->type == LEFTMOUSE) {
1533                                 data->dragstartx= mx;
1534                                 data->draglastx= mx;
1535                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
1536                                 handled= 1;
1537                         }
1538                         else if(ELEM(event->type, PADENTER, RETKEY) && event->val)
1539                                 click= 1;
1540                 }
1541         }
1542         else if(data->state == BUTTON_STATE_NUM_EDITING) {
1543                 if(event->type == LEFTMOUSE && event->val==0) {
1544                         if(data->dragchange)
1545                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
1546                         else
1547                                 click= 1;
1548                 }
1549                 else if(event->type == MOUSEMOVE) {
1550                         float fac;
1551                         int snap;
1552
1553                         fac= 1.0f;
1554                         if(event->shift) fac /= 10.0f;
1555                         if(event->alt) fac /= 20.0f;
1556
1557                         if(event->custom == EVT_DATA_TABLET) {
1558                                 wmTabletData *wmtab= event->customdata;
1559
1560                                 /* de-sensitise based on tablet pressure */
1561                                 if (ELEM(wmtab->Active, DEV_STYLUS, DEV_ERASER))
1562                                         fac *= wmtab->Pressure;
1563                         }
1564                         
1565                         snap= (event->ctrl)? (event->shift)? 2: 1: 0;
1566
1567                         if(ui_numedit_but_NUM(but, data, fac, snap, mx))
1568                                 ui_numedit_apply(C, block, but, data);
1569                 }
1570                 handled= 1;
1571         }
1572         else if(data->state == BUTTON_STATE_TEXT_EDITING) {
1573                 ui_do_but_textedit(C, block, but, data, event);
1574                 handled= 1;
1575         }
1576         else if(data->state == BUTTON_STATE_TEXT_SELECTING) {
1577                 ui_do_but_textedit_select(C, block, but, data, event);
1578                 handled= 1;
1579         }
1580         
1581         if(click) {
1582                 /* we can click on the side arrows to increment/decrement,
1583                  * or click inside to edit the value directly */
1584                 float tempf;
1585                 int temp;
1586
1587                 if(!ui_is_but_float(but)) {
1588                         if(mx < (but->x1 + (but->x2 - but->x1)/3 - 3)) {
1589                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
1590
1591                                 temp= (int)data->value - 1;
1592                                 if(temp>=but->min && temp<=but->max)
1593                                         data->value= (double)temp;
1594                                 else
1595                                         data->cancel= 1;
1596
1597                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
1598                         }
1599                         else if(mx > (but->x1 + (2*(but->x2 - but->x1)/3) + 3)) {
1600                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
1601
1602                                 temp= (int)data->value + 1;
1603                                 if(temp>=but->min && temp<=but->max)
1604                                         data->value= (double)temp;
1605                                 else
1606                                         data->cancel= 1;
1607
1608                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
1609                         }
1610                         else
1611                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
1612                 }
1613                 else {
1614                         if(mx < (but->x1 + (but->x2 - but->x1)/3 - 3)) {
1615                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
1616
1617                                 tempf= data->value - 0.01*but->a1;
1618                                 if (tempf < but->min) tempf = but->min;
1619                                 data->value= tempf;
1620
1621                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
1622                         }
1623                         else if(mx > but->x1 + (2*((but->x2 - but->x1)/3) + 3)) {
1624                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
1625
1626                                 tempf= data->value + 0.01*but->a1;
1627                                 if (tempf < but->min) tempf = but->min;
1628                                 data->value= tempf;
1629
1630                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
1631                         }
1632                         else
1633                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
1634                 }
1635                 handled= 1;
1636         }
1637         
1638         return handled;
1639 }
1640
1641 static int ui_numedit_but_SLI(uiBut *but, uiActivateBut *data, int shift, int ctrl, int mx)
1642 {
1643         float deler, f, tempf;
1644         int temp, lvalue, changed= 0;
1645
1646         if(but->type==NUMSLI) deler= ((but->x2-but->x1)/2 - 5.0*but->aspect);
1647         else if(but->type==HSVSLI) deler= ((but->x2-but->x1)/2 - 5.0*but->aspect);
1648         else deler= (but->x2-but->x1- 5.0*but->aspect);
1649
1650         f= (float)(mx-data->dragstartx)/deler + data->dragfstart;
1651         
1652         if(shift)
1653                 f= (f-data->dragfstart)/10.0 + data->dragfstart;
1654
1655         CLAMP(f, 0.0, 1.0);
1656         tempf= but->min+f*(but->max-but->min);          
1657         temp= floor(tempf+.5);
1658
1659         if(ctrl) {
1660                 if(tempf==but->min || tempf==but->max);
1661                 else if(ui_is_but_float(but)) {
1662
1663                         if(shift) {
1664                                 if(tempf==but->min || tempf==but->max);
1665                                 else if(but->max-but->min < 2.10) tempf= 0.01*floor(100.0*tempf);
1666                                 else if(but->max-but->min < 21.0) tempf= 0.1*floor(10.0*tempf);
1667                                 else tempf= floor(tempf);
1668                         }
1669                         else {
1670                                 if(but->max-but->min < 2.10) tempf= 0.1*floor(10*tempf);
1671                                 else if(but->max-but->min < 21.0) tempf= floor(tempf);
1672                                 else tempf= 10.0*floor(tempf/10.0);
1673                         }
1674                 }
1675                 else {
1676                         temp= 10*(temp/10);
1677                         tempf= temp;
1678                 }
1679         }
1680
1681         if(!ui_is_but_float(but)) {
1682                 lvalue= floor(data->value+0.5);
1683
1684                 if(temp != lvalue) {
1685                         data->value= temp;
1686                         data->dragchange= 1;
1687                         changed= 1;
1688                 }
1689         }
1690         else {
1691                 if(tempf != data->value) {
1692                         data->value= tempf;
1693                         data->dragchange= 1;
1694                         changed= 1;
1695                 }
1696         }
1697
1698         return changed;
1699 }
1700
1701 static int ui_do_but_SLI(bContext *C, uiBlock *block, uiBut *but, uiActivateBut *data, wmEvent *event)
1702 {
1703         int mx, my, click= 0;
1704         int handled= 0;
1705
1706         mx= event->x;
1707         my= event->y;
1708         ui_window_to_block(data->region, block, &mx, &my);
1709
1710         if(data->state == BUTTON_STATE_HIGHLIGHT) {
1711                 if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val) {
1712                         /* start either dragging as slider, or editing as text */
1713                         if(mx>= -6+(but->x1+but->x2)/2) {
1714                                 if(event->type == LEFTMOUSE) {
1715                                         data->dragstartx= mx;
1716                                         data->draglastx= mx;
1717                                         button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
1718                                 }
1719                                 else
1720                                         click= 1;
1721                         }
1722                         else
1723                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
1724                         
1725                         handled= 1;
1726                 }
1727         }
1728         else if(data->state == BUTTON_STATE_NUM_EDITING) {
1729                 if(event->type == LEFTMOUSE && event->val==0) {
1730                         if(data->dragchange)
1731                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
1732                         else
1733                                 click= 1;
1734                 }
1735                 else if(event->type == MOUSEMOVE) {
1736                         if(ui_numedit_but_SLI(but, data, event->shift, event->ctrl, mx))
1737                                 ui_numedit_apply(C, block, but, data);
1738                 }
1739                 handled= 1;
1740         }
1741         else if(data->state == BUTTON_STATE_TEXT_EDITING) {
1742                 ui_do_but_textedit(C, block, but, data, event);
1743                 handled= 1;
1744         }
1745         else if(data->state == BUTTON_STATE_TEXT_SELECTING) {
1746                 ui_do_but_textedit_select(C, block, but, data, event);
1747                 handled= 1;
1748         }
1749
1750         if(click) {
1751                 float f, h;
1752                 float tempf;
1753                 int temp;
1754                 
1755                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
1756
1757                 tempf= data->value;
1758                 temp= (int)data->value;
1759
1760                 h= but->y2-but->y1;
1761
1762                 if(but->type==SLI) f= (float)(mx-but->x1)/(but->x2-but->x1-h);
1763                 else f= (float)(mx- (but->x1+but->x2)/2)/((but->x2-but->x1)/2 - h);
1764                 
1765                 f= but->min+f*(but->max-but->min);
1766
1767                 if(!ui_is_but_float(but)) {
1768                         if(f<temp) temp--;
1769                         else temp++;
1770
1771                         if(temp>=but->min && temp<=but->max)
1772                                 data->value= temp;
1773                         else
1774                                 data->cancel= 1;
1775                 } 
1776                 else {
1777                         if(f<tempf) tempf-=.01;
1778                         else tempf+=.01;
1779
1780                         if(tempf>=but->min && tempf<=but->max)
1781                                 data->value= tempf;
1782                         else
1783                                 data->cancel= 1;
1784                 }
1785
1786                 button_activate_state(C, but, BUTTON_STATE_EXIT);
1787                 handled= 1;
1788         }
1789         
1790         return handled;
1791 }
1792
1793 static int ui_do_but_BLOCK(bContext *C, uiBut *but, uiActivateBut *data, wmEvent *event)
1794 {
1795         if(data->state == BUTTON_STATE_HIGHLIGHT) {
1796                 if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val) {
1797                         button_activate_state(C, but, BUTTON_STATE_BLOCK_OPEN);
1798                         return 1;
1799                 }
1800         }
1801         else if(data->state == BUTTON_STATE_BLOCK_OPEN) {
1802                 if(event->type == MESSAGE) {
1803                         uiMenuBlockHandle *handle= event->customdata;
1804
1805                         if(handle == data->blockhandle) {
1806                                 data->blockretval= handle->blockretval;
1807
1808                                 if(handle->blockretval == UI_RETURN_OK) {
1809                                         if(but->type == COL)
1810                                                 VECCOPY(data->vec, handle->retvec)
1811                                         else if(ELEM3(but->type, MENU, ICONROW, ICONTEXTROW))
1812                                                 data->value= handle->retvalue;
1813                                 }
1814
1815                                 if(handle->blockretval == UI_RETURN_OUT)
1816                                         /* we close the block and proceed as if nothing happened */
1817                                         button_activate_state(C, but, BUTTON_STATE_HIGHLIGHT);
1818                                 else
1819                                         /* ok/cancel, we exit and will send message in _exit */
1820                                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1821
1822                                 return 1;
1823                         }
1824                 }
1825         }
1826         return 0;
1827 }
1828
1829 static int ui_numedit_but_NORMAL(uiBut *but, uiActivateBut *data, int mx, int my)
1830 {
1831         float dx, dy, rad, radsq, mrad, *fp;
1832         int mdx, mdy, changed= 1;
1833         
1834         /* button is presumed square */
1835         /* if mouse moves outside of sphere, it does negative normal */
1836
1837         fp= data->origvec;
1838         rad= (but->x2 - but->x1);
1839         radsq= rad*rad;
1840         
1841         if(fp[2]>0.0f) {
1842                 mdx= (rad*fp[0]);
1843                 mdy= (rad*fp[1]);
1844         }
1845         else if(fp[2]> -1.0f) {
1846                 mrad= rad/sqrt(fp[0]*fp[0] + fp[1]*fp[1]);
1847                 
1848                 mdx= 2.0f*mrad*fp[0] - (rad*fp[0]);
1849                 mdy= 2.0f*mrad*fp[1] - (rad*fp[1]);
1850         }
1851         else mdx= mdy= 0;
1852         
1853         dx= (float)(mx+mdx-data->dragstartx);
1854         dy= (float)(my+mdy-data->dragstarty);
1855
1856         fp= data->vec;
1857         mrad= dx*dx+dy*dy;
1858         if(mrad < radsq) {      /* inner circle */
1859                 fp[0]= dx;
1860                 fp[1]= dy;
1861                 fp[2]= sqrt( radsq-dx*dx-dy*dy );
1862         }
1863         else {  /* outer circle */
1864                 
1865                 mrad= rad/sqrt(mrad);   // veclen
1866                 
1867                 dx*= (2.0f*mrad - 1.0f);
1868                 dy*= (2.0f*mrad - 1.0f);
1869                 
1870                 mrad= dx*dx+dy*dy;
1871                 if(mrad < radsq) {
1872                         fp[0]= dx;
1873                         fp[1]= dy;
1874                         fp[2]= -sqrt( radsq-dx*dx-dy*dy );
1875                 }
1876         }
1877         Normalize(fp);
1878
1879         data->draglastx= mx;
1880         data->draglasty= my;
1881
1882         return changed;
1883 }
1884
1885 static int ui_do_but_NORMAL(bContext *C, uiBlock *block, uiBut *but, uiActivateBut *data, wmEvent *event)
1886 {
1887         int mx, my;
1888
1889         mx= event->x;
1890         my= event->y;
1891         ui_window_to_block(data->region, block, &mx, &my);
1892
1893         if(data->state == BUTTON_STATE_HIGHLIGHT) {
1894                 if(event->type==LEFTMOUSE && event->val) {
1895                         data->dragstartx= mx;
1896                         data->dragstarty= my;
1897                         data->draglastx= mx;
1898                         data->draglasty= my;
1899                         button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
1900
1901                         /* also do drag the first time */
1902                         if(ui_numedit_but_NORMAL(but, data, mx, my))
1903                                 ui_numedit_apply(C, block, but, data);
1904                         
1905                         return 1;
1906                 }
1907         }
1908         else if(data->state == BUTTON_STATE_NUM_EDITING) {
1909                 if(event->type == MOUSEMOVE) {
1910                         if(mx!=data->draglastx || my!=data->draglasty) {
1911                                 if(ui_numedit_but_NORMAL(but, data, mx, my))
1912                                         ui_numedit_apply(C, block, but, data);
1913                         }
1914                 }
1915                 else if(event->type==LEFTMOUSE && event->val==0)
1916                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1917                 return 1;
1918         }
1919         
1920         return 0;
1921 }
1922
1923 static int ui_numedit_but_HSVCUBE(uiBut *but, uiActivateBut *data, int mx, int my)
1924 {
1925         float x, y;
1926         int changed= 1;
1927
1928         /* relative position within box */
1929         x= ((float)mx-but->x1)/(but->x2-but->x1);
1930         y= ((float)my-but->y1)/(but->y2-but->y1);
1931         CLAMP(x, 0.0, 1.0);
1932         CLAMP(y, 0.0, 1.0);
1933         
1934         if(but->a1==0) {
1935                 but->hsv[0]= x; 
1936                 but->hsv[2]= y; 
1937         }
1938         else if(but->a1==1) {
1939                 but->hsv[0]= x;                                 
1940                 but->hsv[1]= y;                                 
1941         }
1942         else if(but->a1==2) {
1943                 but->hsv[2]= x; 
1944                 but->hsv[1]= y; 
1945         }
1946         else
1947                 but->hsv[0]= x; 
1948
1949         ui_set_but_hsv(but);    // converts to rgb
1950         
1951         // update button values and strings
1952         ui_update_block_buts_hsv(but->block, but->hsv);
1953
1954         data->draglastx= mx;
1955         data->draglasty= my;
1956
1957         return changed;
1958 }
1959
1960 static int ui_do_but_HSVCUBE(bContext *C, uiBlock *block, uiBut *but, uiActivateBut *data, wmEvent *event)
1961 {
1962         int mx, my;
1963
1964         mx= event->x;
1965         my= event->y;
1966         ui_window_to_block(data->region, block, &mx, &my);
1967
1968         if(data->state == BUTTON_STATE_HIGHLIGHT) {
1969                 if(event->type==LEFTMOUSE && event->val) {
1970                         data->dragstartx= mx;
1971                         data->dragstarty= my;
1972                         data->draglastx= mx;
1973                         data->draglasty= my;
1974                         button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
1975
1976                         /* also do drag the first time */
1977                         if(ui_numedit_but_HSVCUBE(but, data, mx, my))
1978                                 ui_numedit_apply(C, block, but, data);
1979                         
1980                         return 1;
1981                 }
1982         }
1983         else if(data->state == BUTTON_STATE_NUM_EDITING) {
1984                 if(event->type == MOUSEMOVE) {
1985                         if(mx!=data->draglastx || my!=data->draglasty) {
1986                                 if(ui_numedit_but_HSVCUBE(but, data, mx, my))
1987                                         ui_numedit_apply(C, block, but, data);
1988                         }
1989                 }
1990                 else if(event->type==LEFTMOUSE && event->val==0)
1991                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1992                 
1993                 return 1;
1994         }
1995         return 0;
1996 }
1997
1998 static int verg_colorband(const void *a1, const void *a2)
1999 {
2000         const CBData *x1=a1, *x2=a2;
2001         
2002         if( x1->pos > x2->pos ) return 1;
2003         else if( x1->pos < x2->pos) return -1;
2004         return 0;
2005 }
2006
2007 static void ui_colorband_update(ColorBand *coba)
2008 {
2009         int a;
2010         
2011         if(coba->tot<2) return;
2012         
2013         for(a=0; a<coba->tot; a++) coba->data[a].cur= a;
2014                 qsort(coba->data, coba->tot, sizeof(CBData), verg_colorband);
2015         for(a=0; a<coba->tot; a++) {
2016                 if(coba->data[a].cur==coba->cur) {
2017                         coba->cur= a;
2018                         break;
2019                 }
2020         }
2021 }
2022
2023 static int ui_numedit_but_COLORBAND(uiBut *but, uiActivateBut *data, int mx)
2024 {
2025         float dx;
2026         int changed= 0;
2027
2028         if(data->draglastx == mx)
2029                 return changed;
2030
2031         dx= ((float)(mx - data->draglastx))/(but->x2-but->x1);
2032         data->dragcbd->pos += dx;
2033         CLAMP(data->dragcbd->pos, 0.0, 1.0);
2034         
2035         ui_colorband_update(data->coba);
2036         data->dragcbd= data->coba->data + data->coba->cur;      /* because qsort */
2037         
2038         data->draglastx= mx;
2039         changed= 1;
2040
2041         return changed;
2042 }
2043
2044 static int ui_do_but_COLORBAND(bContext *C, uiBlock *block, uiBut *but, uiActivateBut *data, wmEvent *event)
2045 {
2046         ColorBand *coba;
2047         CBData *cbd;
2048         int mx, my, a, xco, mindist= 12;
2049
2050         mx= event->x;
2051         my= event->y;
2052         ui_window_to_block(data->region, block, &mx, &my);
2053
2054         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2055                 if(event->type==LEFTMOUSE && event->val) {
2056                         coba= (ColorBand*)but->poin;
2057
2058                         if(event->ctrl) {
2059                                 /* insert new key on mouse location */
2060                                 if(coba->tot < MAXCOLORBAND-1) {
2061                                         float pos= ((float)(mx - but->x1))/(but->x2-but->x1);
2062                                         float col[4];
2063                                         
2064                                         do_colorband(coba, pos, col);   /* executes it */
2065                                         
2066                                         coba->tot++;
2067                                         coba->cur= coba->tot-1;
2068                                         
2069                                         coba->data[coba->cur].r= col[0];
2070                                         coba->data[coba->cur].g= col[1];
2071                                         coba->data[coba->cur].b= col[2];
2072                                         coba->data[coba->cur].a= col[3];
2073                                         coba->data[coba->cur].pos= pos;
2074
2075                                         ui_colorband_update(coba);
2076                                 }
2077
2078                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2079                         }
2080                         else {
2081                                 data->dragstartx= mx;
2082                                 data->dragstarty= my;
2083                                 data->draglastx= mx;
2084                                 data->draglasty= my;
2085
2086                                 /* activate new key when mouse is close */
2087                                 for(a=0, cbd= coba->data; a<coba->tot; a++, cbd++) {
2088                                         xco= but->x1 + (cbd->pos*(but->x2-but->x1));
2089                                         xco= ABS(xco-mx);
2090                                         if(a==coba->cur) xco+= 5; // selected one disadvantage
2091                                         if(xco<mindist) {
2092                                                 coba->cur= a;
2093                                                 mindist= xco;
2094                                         }
2095                                 }
2096                 
2097                                 data->dragcbd= coba->data + coba->cur;
2098                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2099                         }
2100                         return 1;
2101                 }
2102         }
2103         else if(data->state == BUTTON_STATE_NUM_EDITING) {
2104                 if(event->type == MOUSEMOVE) {
2105                         if(mx!=data->draglastx || my!=data->draglasty) {
2106                                 if(ui_numedit_but_COLORBAND(but, data, mx))
2107                                         ui_numedit_apply(C, block, but, data);
2108                         }
2109                 }
2110                 else if(event->type==LEFTMOUSE && event->val==0)
2111                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2112                 
2113                 return 1;
2114         }
2115         return 0;
2116 }
2117
2118 static int ui_numedit_but_CURVE(uiBut *but, uiActivateBut *data, int snap, int mx, int my)
2119 {
2120         CurveMapping *cumap= data->cumap;
2121         CurveMap *cuma= cumap->cm+cumap->cur;
2122         CurveMapPoint *cmp= cuma->curve;
2123         float fx, fy, zoomx, zoomy, offsx, offsy;
2124         int a, changed= 0;
2125
2126         zoomx= (but->x2-but->x1)/(cumap->curr.xmax-cumap->curr.xmin);
2127         zoomy= (but->y2-but->y1)/(cumap->curr.ymax-cumap->curr.ymin);
2128         offsx= cumap->curr.xmin;
2129         offsy= cumap->curr.ymin;
2130
2131         if(data->dragsel != -1) {
2132                 int moved_point= 0;             /* for ctrl grid, can't use orig coords because of sorting */
2133                 
2134                 fx= (mx-data->draglastx)/zoomx;
2135                 fy= (my-data->draglasty)/zoomy;
2136                 for(a=0; a<cuma->totpoint; a++) {
2137                         if(cmp[a].flag & SELECT) {
2138                                 float origx= cmp[a].x, origy= cmp[a].y;
2139                                 cmp[a].x+= fx;
2140                                 cmp[a].y+= fy;
2141                                 if(snap) {
2142                                         cmp[a].x= 0.125f*floor(0.5f + 8.0f*cmp[a].x);
2143                                         cmp[a].y= 0.125f*floor(0.5f + 8.0f*cmp[a].y);
2144                                 }
2145                                 if(cmp[a].x!=origx || cmp[a].y!=origy)
2146                                         moved_point= 1;
2147                         }
2148                 }
2149
2150                 curvemapping_changed(cumap, 0); /* no remove doubles */
2151                 
2152                 if(moved_point) {
2153                         data->draglastx= mx;
2154                         data->draglasty= my;
2155                         changed= 1;
2156                 }
2157
2158                 data->dragchange= 1; /* mark for selection */
2159         }
2160         else {
2161                 fx= (mx-data->draglastx)/zoomx;
2162                 fy= (my-data->draglasty)/zoomy;
2163                 
2164                 /* clamp for clip */
2165                 if(cumap->flag & CUMA_DO_CLIP) {
2166                         if(cumap->curr.xmin-fx < cumap->clipr.xmin)
2167                                 fx= cumap->curr.xmin - cumap->clipr.xmin;
2168                         else if(cumap->curr.xmax-fx > cumap->clipr.xmax)
2169                                 fx= cumap->curr.xmax - cumap->clipr.xmax;
2170                         if(cumap->curr.ymin-fy < cumap->clipr.ymin)
2171                                 fy= cumap->curr.ymin - cumap->clipr.ymin;
2172                         else if(cumap->curr.ymax-fy > cumap->clipr.ymax)
2173                                 fy= cumap->curr.ymax - cumap->clipr.ymax;
2174                 }                               
2175
2176                 cumap->curr.xmin-=fx;
2177                 cumap->curr.ymin-=fy;
2178                 cumap->curr.xmax-=fx;
2179                 cumap->curr.ymax-=fy;
2180                 
2181                 data->draglastx= mx;
2182                 data->draglasty= my;
2183
2184                 changed= 1;
2185         }
2186
2187         return changed;
2188 }
2189
2190 static int ui_do_but_CURVE(bContext *C, uiBlock *block, uiBut *but, uiActivateBut *data, wmEvent *event)
2191 {
2192         int mx, my, a, changed= 0;
2193
2194         mx= event->x;
2195         my= event->y;
2196         ui_window_to_block(data->region, block, &mx, &my);
2197
2198         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2199                 if(event->type==LEFTMOUSE && event->val) {
2200                         CurveMapping *cumap= (CurveMapping*)but->poin;
2201                         CurveMap *cuma= cumap->cm+cumap->cur;
2202                         CurveMapPoint *cmp= cuma->curve;
2203                         float fx, fy, zoomx, zoomy, offsx, offsy;
2204                         float dist, mindist= 200.0f; // 14 pixels radius
2205                         int sel= -1;
2206
2207                         zoomx= (but->x2-but->x1)/(cumap->curr.xmax-cumap->curr.xmin);
2208                         zoomy= (but->y2-but->y1)/(cumap->curr.ymax-cumap->curr.ymin);
2209                         offsx= cumap->curr.xmin;
2210                         offsy= cumap->curr.ymin;
2211
2212                         if(event->ctrl) {
2213                                 fx= ((float)my - but->x1)/zoomx + offsx;
2214                                 fy= ((float)my - but->y1)/zoomy + offsy;
2215                                 
2216                                 curvemap_insert(cuma, fx, fy);
2217                                 curvemapping_changed(cumap, 0);
2218                                 changed= 1;
2219                         }
2220
2221                         /* check for selecting of a point */
2222                         cmp= cuma->curve;       /* ctrl adds point, new malloc */
2223                         for(a=0; a<cuma->totpoint; a++) {
2224                                 fx= but->x1 + zoomx*(cmp[a].x-offsx);
2225                                 fy= but->y1 + zoomy*(cmp[a].y-offsy);
2226                                 dist= (fx-mx)*(fx-mx) + (fy-my)*(fy-my);
2227                                 if(dist < mindist) {
2228                                         sel= a;
2229                                         mindist= dist;
2230                                 }
2231                         }
2232
2233                         if (sel == -1) {
2234                                 /* if the click didn't select anything, check if it's clicked on the 
2235                                  * curve itself, and if so, add a point */
2236                                 fx= ((float)mx - but->x1)/zoomx + offsx;
2237                                 fy= ((float)my - but->y1)/zoomy + offsy;
2238                                 
2239                                 cmp= cuma->table;
2240
2241                                 /* loop through the curve segment table and find what's near the mouse.
2242                                  * 0.05 is kinda arbitrary, but seems to be what works nicely. */
2243                                 for(a=0; a<=CM_TABLE; a++) {                    
2244                                         if ( ( fabs(fx - cmp[a].x) < (0.05) ) && ( fabs(fy - cmp[a].y) < (0.05) ) ) {
2245                                         
2246                                                 curvemap_insert(cuma, fx, fy);
2247                                                 curvemapping_changed(cumap, 0);
2248
2249                                                 changed= 1;
2250                                                 
2251                                                 /* reset cmp back to the curve points again, rather than drawing segments */            
2252                                                 cmp= cuma->curve;
2253                                                 
2254                                                 /* find newly added point and make it 'sel' */
2255                                                 for(a=0; a<cuma->totpoint; a++)
2256                                                         if(cmp[a].x == fx)
2257                                                                 sel = a;
2258                                                         
2259                                                 break;
2260                                         }
2261                                 }
2262                         }
2263
2264                         if(sel!= -1) {
2265                                 /* ok, we move a point */
2266                                 /* deselect all if this one is deselect. except if we hold shift */
2267                                 if(event->shift==0 && (cmp[sel].flag & SELECT)==0)
2268                                         for(a=0; a<cuma->totpoint; a++)
2269                                                 cmp[a].flag &= ~SELECT;
2270                                 cmp[sel].flag |= SELECT;
2271                         }
2272                         else {
2273                                 /* move the view */
2274                                 data->cancel= 1;
2275                         }
2276
2277                         data->dragsel= sel;
2278
2279                         data->dragstartx= mx;
2280                         data->dragstarty= my;
2281                         data->draglastx= mx;
2282                         data->draglasty= my;
2283
2284                         button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2285                         return 1;
2286                 }
2287         }
2288         else if(data->state == BUTTON_STATE_NUM_EDITING) {
2289                 if(event->type == MOUSEMOVE) {
2290                         if(mx!=data->draglastx || my!=data->draglasty) {
2291                                 if(ui_numedit_but_CURVE(but, data, event->shift, mx, my))
2292                                         ui_numedit_apply(C, block, but, data);
2293                         }
2294                 }
2295                 else if(event->type==LEFTMOUSE && event->val==0) {
2296                         if(data->dragsel != -1) {
2297                                 CurveMapping *cumap= data->cumap;
2298                                 CurveMap *cuma= cumap->cm+cumap->cur;
2299                                 CurveMapPoint *cmp= cuma->curve;
2300
2301                                 if(!data->dragchange) {
2302                                         /* deselect all, select one */
2303                                         if(event->shift==0) {
2304                                                 for(a=0; a<cuma->totpoint; a++)
2305                                                         cmp[a].flag &= ~SELECT;
2306                                                 cmp[data->dragsel].flag |= SELECT;
2307                                         }
2308                                 }
2309                                 else
2310                                         curvemapping_changed(cumap, 1); /* remove doubles */
2311                         }
2312
2313                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2314                 }
2315                 return 1;
2316         }
2317         return 0;
2318 }
2319
2320 #ifdef INTERNATIONAL
2321 static int ui_do_but_CHARTAB(bContext *C, uiBlock *block, uiBut *but, uiActivateBut *data, wmEvent *event)
2322 {
2323         /* XXX 2.50 bad global and state access */
2324 #if 0
2325         float sx, sy, ex, ey;
2326         float width, height;
2327         float butw, buth;
2328         int mx, my, x, y, cs, che;
2329
2330         mx= event->x;
2331         my= event->y;
2332         ui_window_to_block(data->region, block, &mx, &my);
2333
2334         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2335                 if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val) {
2336                         /* Calculate the size of the button */
2337                         width = abs(but->x2 - but->x1);
2338                         height = abs(but->y2 - but->y1);
2339
2340                         butw = floor(width / 12);
2341                         buth = floor(height / 6);
2342
2343                         /* Initialize variables */
2344                         sx = but->x1;
2345                         ex = but->x1 + butw;
2346                         sy = but->y1 + height - buth;
2347                         ey = but->y1 + height;
2348
2349                         cs = G.charstart;
2350
2351                         /* And the character is */
2352                         x = (int) ((mx / butw) - 0.5);
2353                         y = (int) (6 - ((my / buth) - 0.5));
2354
2355                         che = cs + (y*12) + x;
2356
2357                         if(che > G.charmax)
2358                                 che = 0;
2359
2360                         if(G.obedit)
2361                         {
2362                                 do_textedit(0,0,che);
2363                         }
2364
2365                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2366                         return 1;
2367                 }
2368                 else if(ELEM(event->type, WHEELUPMOUSE, PAGEUPKEY)) {
2369                         for(but= block->buttons.first; but; but= but->next) {
2370                                 if(but->type == CHARTAB) {
2371                                         G.charstart = G.charstart - (12*6);
2372                                         if(G.charstart < 0)
2373                                                 G.charstart = 0;
2374                                         if(G.charstart < G.charmin)
2375                                                 G.charstart = G.charmin;
2376                                         ui_draw_but(but);
2377
2378                                         //Really nasty... to update the num button from the same butblock
2379                                         for(bt= block->buttons.first; bt; bt= bt->next)
2380                                         {
2381                                                 if(ELEM(bt->type, NUM, NUMABS)) {
2382                                                         ui_check_but(bt);
2383                                                         ui_draw_but(bt);
2384                                                 }
2385                                         }
2386                                         retval=UI_CONT;
2387                                         break;
2388                                 }
2389                         }
2390                         return 1;
2391                 }
2392                 else if(ELEM(event->type, WHEELDOWNMOUSE, PAGEDOWNKEY)) {
2393                         for(but= block->buttons.first; but; but= but->next)
2394                         {
2395                                 if(but->type == CHARTAB)
2396                                 {
2397                                         G.charstart = G.charstart + (12*6);
2398                                         if(G.charstart > (0xffff - 12*6))
2399                                                 G.charstart = 0xffff - (12*6);
2400                                         if(G.charstart > G.charmax - 12*6)
2401                                                 G.charstart = G.charmax - 12*6;
2402                                         ui_draw_but(but);
2403
2404                                         for(bt= block->buttons.first; bt; bt= bt->next)
2405                                         {
2406                                                 if(ELEM(bt->type, NUM, NUMABS)) {
2407                                                         ui_check_but(bt);
2408                                                         ui_draw_but(bt);
2409                                                 }
2410                                         }
2411                                         
2412                                         but->flag |= UI_ACTIVE;
2413                                         retval=UI_RETURN_OK;
2414                                         break;
2415                                 }
2416                         }
2417                         return 1;
2418                 }
2419         }
2420 #endif
2421         return 0;
2422 }
2423 #endif
2424
2425 static int ui_do_button(bContext *C, uiBlock *block, uiBut *but, wmEvent *event)
2426 {
2427         uiActivateBut *data;
2428         int handled;
2429
2430         data= but->activate;
2431         handled= 0;
2432
2433         /* handle copy-paste */
2434         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2435                 if(ELEM(event->type, CKEY, VKEY) && event->val && (event->ctrl || event->oskey)) {
2436                         ui_but_copy_paste(C, but, data, (event->type == CKEY)? 'c': 'v');
2437                         return 1;
2438                 }
2439         }
2440
2441         /* verify if we can edit this button */
2442         if(ELEM(event->type, LEFTMOUSE, RETKEY)) {
2443                 if(but->lock) {
2444                         if(but->lockstr) {
2445                                 WM_report(C, WM_LOG_WARNING, but->lockstr);
2446                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2447                                 return 1;
2448                         }
2449                 } 
2450                 else if(but->pointype && but->poin==0) {
2451                         /* there's a pointer needed */
2452                         WM_reportf(C, WM_LOG_WARNING, "DoButton pointer error: %s", but->str);
2453                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2454                         return 1;
2455                 }
2456         }
2457
2458         switch(but->type) {
2459         case BUT:
2460                 handled= ui_do_but_BUT(C, but, data, event);
2461                 break;
2462         case KEYEVT:
2463                 handled= ui_do_but_KEYEVT(C, but, data, event);
2464                 break;
2465         case TOG: 
2466         case TOGR: 
2467         case ICONTOG:
2468         case ICONTOGN:
2469         case TOGN:
2470         case BUT_TOGDUAL:
2471                 handled= ui_do_but_TOG(C, but, data, event);
2472                 break;
2473 #if 0
2474         case SCROLL:
2475                 /* DrawBut(b, 1); */
2476                 /* do_scrollbut(b); */
2477                 /* DrawBut(b,0); */
2478                 break;
2479 #endif
2480         case NUM:
2481         case NUMABS:
2482                 handled= ui_do_but_NUM(C, block, but, data, event);
2483                 break;
2484         case SLI:
2485         case NUMSLI:
2486         case HSVSLI:
2487                 handled= ui_do_but_SLI(C, block, but, data, event);
2488                 break;
2489         case ROUNDBOX:  
2490         case LABEL:     
2491         case TOG3:      
2492         case ROW:
2493                 handled= ui_do_but_EXIT(C, but, data, event);
2494                 break;
2495         case TEX:
2496         case IDPOIN:
2497                 handled= ui_do_but_TEX(C, block, but, data, event);
2498                 break;
2499         case MENU:
2500                 handled= ui_do_but_BLOCK(C, but, data, event);
2501                 break;
2502         case ICONROW:
2503                 handled= ui_do_but_BLOCK(C, but, data, event);
2504                 break;
2505         case ICONTEXTROW:
2506                 handled= ui_do_but_BLOCK(C, but, data, event);
2507                 break;
2508         case BLOCK:
2509         case PULLDOWN:
2510                 handled= ui_do_but_BLOCK(C, but, data, event);
2511                 break;
2512         case BUTM:
2513                 handled= ui_do_but_BUT(C, but, data, event);
2514                 break;
2515         case COL:
2516                 if(but->a1 == -1)  // signal to prevent calling up color picker
2517                         handled= ui_do_but_EXIT(C, but, data, event);
2518                 else
2519                         handled= ui_do_but_BLOCK(C, but, data, event);
2520                 break;
2521         case BUT_NORMAL:
2522                 handled= ui_do_but_NORMAL(C, block, but, data, event);
2523                 break;
2524         case BUT_COLORBAND:
2525                 handled= ui_do_but_COLORBAND(C, block, but, data, event);
2526                 break;
2527         case BUT_CURVE:
2528                 handled= ui_do_but_CURVE(C, block, but, data, event);
2529                 break;
2530         case HSVCUBE:
2531                 handled= ui_do_but_HSVCUBE(C, block, but, data, event);
2532                 break;
2533 #ifdef INTERNATIONAL
2534         case CHARTAB:
2535                 handled= ui_do_but_CHARTAB(C, block, but, data, event);
2536                 break;
2537 #endif
2538         /* XXX 2.50 links not implemented yet */
2539 #if 0
2540         case LINK:
2541         case INLINK:
2542                 handled= retval= ui_do_but_LINK(block, but);
2543                 break;
2544 #endif
2545         }
2546         
2547         return handled;
2548 }
2549
2550 /* ************************ button utilities *********************** */
2551
2552 static int ui_but_contains_pt(uiBut *but, int mx, int my)
2553 {
2554         return ((but->x1<mx && but->x2>=mx) && (but->y1<my && but->y2>=my));
2555 }
2556
2557 static uiBut *ui_but_find_activated(ARegion *ar, uiActivateBut *data, uiBlock **rblock)
2558 {
2559         uiBlock *block;
2560         uiBut *but;
2561
2562         for(block=ar->uiblocks.first; block; block=block->next) {
2563                 for(but=block->buttons.first; but; but= but->next) {
2564                         if((but->activate == data && data) || (but->activate && data == NULL)) {
2565                                 if(rblock) *rblock= block;
2566                                 return but;
2567                         }
2568                 }
2569         }
2570
2571         if(rblock) *rblock= NULL;
2572         return NULL;
2573 }
2574
2575 static uiBut *ui_but_find_signal(ARegion *ar, uiActivateBut *data, uiBlock **rblock)
2576 {
2577         uiBlock *block;
2578         uiBut *but;
2579
2580         for(block=ar->uiblocks.first; block; block=block->next) {
2581                 for(but=block->buttons.first; but; but= but->next) {
2582                         if(but->activateflag) {
2583                                 if(rblock) *rblock= block;
2584                                 return but;
2585                         }
2586                 }
2587         }
2588
2589         if(rblock) *rblock= NULL;
2590         return NULL;
2591 }
2592
2593 static int inside_region(ARegion *ar, int x, int y)
2594 {
2595         if(BLI_in_rcti(&ar->winrct, x, y)) {
2596                 /* XXX still can be zero */
2597                 if(ar->v2d.mask.xmin!=ar->v2d.mask.xmax) {
2598                         int arx = x;
2599                         int ary = y;
2600                         ui_window_to_region(ar, &arx, &ary);
2601                         return BLI_in_rcti(&ar->v2d.mask, arx, ary);
2602                 }
2603                 return 1;
2604         }
2605         return 0;
2606 }
2607
2608 static uiBut *ui_but_find_mouse_over(ARegion *ar, int x, int y, uiBlock **rblock)
2609 {
2610         uiBlock *block, *blockover= NULL;
2611         uiBut *but, *butover= NULL;
2612         int mx, my;
2613
2614         if(inside_region(ar, x, y)) {
2615                 for(block=ar->uiblocks.first; block; block=block->next) {
2616                         mx= x;
2617                         my= y;
2618                         ui_window_to_block(ar, block, &mx, &my);
2619
2620                         for(but=block->buttons.first; but; but= but->next) {
2621                                 /* give precedence to already activated buttons */
2622                                 if(ui_but_contains_pt(but, mx, my)) {
2623                                         if(!butover || (!butover->activate && but->activate)) {
2624                                                 butover= but;
2625                                                 blockover= block;
2626                                         }
2627                                 }
2628                         }
2629                 }
2630         }
2631         if(rblock)
2632                 *rblock= blockover;
2633
2634         return butover;
2635 }
2636
2637 /*********************** button activate operator ***************************
2638  * this operator runs from the moment the mouse is position over a button and
2639  * handles all interaction with the button, from highlight to text editing,
2640  * dragging, having a block open to leaving the button to activate another. */
2641
2642 static void button_disable_timers(bContext *C, uiActivateBut *data)
2643 {
2644         if(data->tooltiptimer) {
2645                 WM_event_remove_window_timer(C->window, data->tooltiptimer);
2646                 data->tooltiptimer= NULL;
2647         }
2648         if(data->tooltip) {
2649                 ui_tooltip_free(C, data->tooltip);
2650                 data->tooltip= NULL;
2651         }
2652         data->tooltipdisabled= 1;
2653
2654         if(data->autoopentimer) {
2655                 WM_event_remove_window_timer(C->window, data->autoopentimer);
2656                 data->autoopentimer= NULL;
2657         }
2658 }
2659
2660 static void button_activate_state(bContext *C, uiBut *but, uiActivateButState state)
2661 {
2662         uiActivateBut *data;
2663
2664         data= but->activate;
2665         if(data->state == state)
2666                 return;
2667
2668         if(state == BUTTON_STATE_HIGHLIGHT) {
2669                 but->flag &= ~UI_SELECT;
2670
2671                 /* XXX 2.50 U missing from context */
2672                 if(U.flag & USER_TOOLTIPS)
2673                         if(!data->tooltiptimer && !data->tooltipdisabled)
2674                                 data->tooltiptimer= WM_event_add_window_timer(C->window, BUTTON_TOOLTIP_DELAY, ~0);
2675
2676                 /* automatic open pulldown block timer */
2677                 if(but->type==BLOCK || but->type==MENU || but->type==PULLDOWN || but->type==ICONTEXTROW) {
2678                         if(!data->autoopentimer) {
2679                                 int time;
2680
2681                                 if(but->block->auto_open==2) time= 1;    // test for toolbox
2682                                 else if(but->block->flag & UI_BLOCK_LOOP || but->block->auto_open) time= 5*U.menuthreshold2;
2683                                 else if(U.uiflag & USER_MENUOPENAUTO) time= 5*U.menuthreshold1;
2684                                 else time= -1;
2685
2686                                 if(time >= 0)
2687                                         data->autoopentimer= WM_event_add_window_timer(C->window, time*20, ~0);
2688                         }
2689                 }
2690         }
2691         else {
2692                 but->flag |= UI_SELECT;
2693                 button_disable_timers(C, data);
2694         }
2695
2696         if(state == BUTTON_STATE_TEXT_EDITING && data->state != BUTTON_STATE_TEXT_SELECTING)
2697                 ui_textedit_begin(but, data);
2698         else if(data->state == BUTTON_STATE_TEXT_EDITING && state != BUTTON_STATE_TEXT_SELECTING)
2699                 ui_textedit_end(but, data);
2700         
2701         if(state == BUTTON_STATE_NUM_EDITING)
2702                 ui_numedit_begin(but, data);
2703         else if(data->state == BUTTON_STATE_NUM_EDITING)
2704                 ui_numedit_end(but, data);
2705
2706         if(state == BUTTON_STATE_BLOCK_OPEN) {
2707                 ui_blockopen_begin(C, but, data);
2708                 /* note we move the handler to the region when the block is open,
2709                  * so we don't interfere with the events as long as it's open */
2710                 /* XXX (to brecht) removing this makes thing work proper */
2711                 //WM_event_remove_modal_handler(&C->window->handlers, data->operator);
2712                 //WM_event_add_modal_handler(C, &data->region->handlers, data->operator);
2713         }
2714         else if(data->state == BUTTON_STATE_BLOCK_OPEN) {
2715                 ui_blockopen_end(C, but, data);
2716                 /* XXX (to brecht) removing this makes thing work proper */
2717                 //WM_event_remove_modal_handler(&data->region->handlers, data->operator);
2718                 //WM_event_add_modal_handler(C, &C->window->handlers, data->operator);
2719         }
2720         
2721         if(state == BUTTON_STATE_WAIT_FLASH) {
2722                 data->flashtimer= WM_event_add_window_timer(C->window, BUTTON_FLASH_DELAY, ~0);
2723         }
2724         else if(data->flashtimer) {
2725                 WM_event_remove_window_timer(C->window, data->flashtimer);
2726                 data->flashtimer= NULL;
2727         }
2728
2729         data->state= state;
2730         WM_event_add_notifier(C, WM_NOTE_WINDOW_REDRAW, 0, NULL);
2731 }
2732
2733 static void button_activate_init(bContext *C, ARegion *ar, wmOperator *op, uiBut *but, uiBut *lastbut)
2734 {
2735         uiActivateBut *data;
2736
2737         /* setup struct */
2738         data= MEM_callocN(sizeof(uiActivateBut), "uiActivateBut");
2739         data->region= ar;
2740         data->operator= op;
2741         data->interactive= 0;
2742         data->state = BUTTON_STATE_INIT;
2743         op->customdata= data;
2744
2745         /* activate button */
2746         but->flag |= UI_ACTIVE;
2747         but->activate= data;
2748
2749         if(but == lastbut)
2750                 data->tooltipdisabled= 1;
2751
2752         /* we disable auto_open in the block after a threshold, because we still
2753          * want to allow auto opening adjacent menus even if no button is activated
2754          * inbetween going over to the other button, but only for a short while */
2755         if(!lastbut && but->block->auto_open)
2756                 if(but->block->auto_open_last+BUTTON_AUTO_OPEN_THRESH < PIL_check_seconds_timer())
2757                         but->block->auto_open= 0;
2758
2759         if(but->block->flag & UI_BLOCK_LOOP)
2760                 WM_event_add_modal_handler(C, &C->window->handlers, op);
2761         else
2762                 /* regular button handler on area, handles mouse-exit in WM */
2763                 WM_event_add_modal_handler(C, &C->area->handlers, op);
2764
2765         button_activate_state(C, but, BUTTON_STATE_HIGHLIGHT);
2766
2767         if(but->activateflag == UI_ACTIVATE_OPEN)
2768                 button_activate_state(C, but, BUTTON_STATE_BLOCK_OPEN);
2769         else if(but->activateflag == UI_ACTIVATE_TEXT_EDITING)
2770                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2771         else if(but->activateflag == UI_ACTIVATE_APPLY)
2772                 button_activate_state(C, but, BUTTON_STATE_WAIT_FLASH);
2773         
2774         but->activateflag= 0;
2775 }
2776
2777 static void button_activate_exit(bContext *C, uiActivateBut *data, wmOperator *op)
2778 {
2779         uiBut *but;
2780         uiBlock *block;
2781
2782         /* verify if we still have a button, can be NULL! */
2783         but= ui_but_find_activated(data->region, data, &block);
2784
2785         /* stop text editing */
2786         if(data->state == BUTTON_STATE_TEXT_EDITING || data->state == BUTTON_STATE_TEXT_SELECTING) {
2787                 data->cancel= 1;
2788                 ui_textedit_end(but, data);
2789         }
2790
2791         if(data->state == BUTTON_STATE_NUM_EDITING)
2792                 ui_numedit_end(but, data);
2793         
2794         if(data->state == BUTTON_STATE_BLOCK_OPEN) {
2795                 ui_blockopen_end(C, but, data);
2796         }
2797
2798         if(but) {
2799                 /* if someone is expecting a message */
2800                 if(but->block->handle && !(but->block->flag & UI_BLOCK_KEEP_OPEN) && !data->cancel) {
2801                         uiMenuBlockHandle *handle;
2802
2803                         handle= but->block->handle;
2804                         handle->butretval= data->retval;
2805                         if(data->blockretval) {
2806                                 handle->blockretval= data->blockretval;
2807
2808                                 /* if we got a cancel from a block menu, then also cancel this
2809                                  * button, we set that here to instead of in do_BLOCK to make
2810                                  * a distinction with cancel after lost highlight for example */
2811                                 if(data->blockretval == UI_RETURN_CANCEL)
2812                                         data->cancel= 1;
2813                         }
2814                         else
2815                                 handle->blockretval= UI_RETURN_OK;
2816
2817                         WM_event_add_message(C->wm, handle, 0);
2818                 }
2819
2820                 /* apply the button action or value */
2821                 ui_apply_button(block, but, data, 0);
2822
2823                 /* clean up button */
2824                 but->activate= NULL;
2825                 but->flag &= ~(UI_ACTIVE|UI_SELECT);
2826         }
2827
2828         /* redraw */
2829         WM_event_add_notifier(C, WM_NOTE_WINDOW_REDRAW, 0, NULL);
2830
2831         /* clean up */
2832         button_disable_timers(C, data);
2833
2834         if(data->str)
2835                 MEM_freeN(data->str);
2836         if(data->origstr)
2837                 MEM_freeN(data->origstr);
2838         if(data->flashtimer)
2839                 WM_event_remove_window_timer(C->window, data->flashtimer);
2840
2841         MEM_freeN(op->customdata);
2842         op->customdata= NULL;
2843 }
2844
2845 static int button_activate_try_init(bContext *C, ARegion *ar, wmOperator *op, wmEvent *event, uiBut *lastbut)
2846 {
2847         uiBut *but;
2848
2849         /* try to activate a button */
2850         if(!ar)
2851                 return OPERATOR_PASS_THROUGH;
2852
2853         if(ui_but_find_activated(ar, NULL, NULL))
2854                 return OPERATOR_PASS_THROUGH;
2855
2856         but= ui_but_find_signal(ar, NULL, NULL);
2857
2858         if(!but)
2859                 but= ui_but_find_mouse_over(ar, event->x, event->y, NULL);
2860
2861         if(lastbut && but && but!=lastbut)
2862                 return OPERATOR_PASS_THROUGH;
2863
2864         if(but && !but->activate) {
2865                 button_activate_init(C, ar, op, but, lastbut);
2866                 return OPERATOR_RUNNING_MODAL;
2867         }
2868
2869         return OPERATOR_PASS_THROUGH;
2870 }
2871
2872 static int button_activate_try_exit(bContext *C, wmOperator *op, wmEvent *event)
2873 {
2874         ARegion *ar;
2875         uiActivateBut *data;
2876         uiBut *but;
2877
2878         data= op->customdata;
2879         ar= data->region;
2880
2881         but= ui_but_find_activated(data->region, data, NULL);
2882
2883         /* exit the current button */
2884         button_activate_exit(C, op->customdata, op);
2885         
2886         /* adds empty mousemove in queue for re-init operator (if mouse is still over button) */
2887         WM_event_add_mousemove(C);
2888
2889         return 1;
2890 }
2891
2892 static int button_activate_invoke(bContext *C, wmOperator *op, wmEvent *event)
2893 {
2894         return button_activate_try_init(C, C->region, op, event, NULL);
2895 }
2896
2897 static int button_activate_cancel(bContext *C, wmOperator *op)
2898 {
2899         uiActivateBut *data;
2900
2901         data= op->customdata;
2902         data->cancel= 1;
2903         button_activate_exit(C, op->customdata, op);
2904
2905         return OPERATOR_CANCELLED;
2906 }
2907
2908 static int button_activate_modal(bContext *C, wmOperator *op, wmEvent *event)
2909 {
2910         uiActivateBut *data;
2911         uiBut *but;
2912         uiBlock *block;
2913         int handled= 0;
2914
2915         data= op->customdata;
2916         /* XXX (for brecht) this happens when cancel() frees, and modal handler still runs */
2917         if(data==NULL)
2918                 return OPERATOR_FINISHED;
2919         
2920         /* check if the button dissappeared somehow */
2921         if(!(but= ui_but_find_activated(data->region, data, &block))) {
2922                 data->cancel= 1;
2923                 button_activate_try_exit(C, op, event);
2924                 return OPERATOR_CANCELLED|OPERATOR_PASS_THROUGH;
2925         }
2926
2927         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2928                 switch(event->type) {
2929                         case MOUSEMOVE:
2930                                 /* verify if we are still over the button, if not exit */
2931                                 but= ui_but_find_mouse_over(data->region, event->x, event->y, &block);
2932
2933                                 if(!but || but->activate != data) {
2934                                         data->cancel= 1;
2935                                         if(button_activate_try_exit(C, op, event))
2936                                                 return OPERATOR_CANCELLED|OPERATOR_PASS_THROUGH;
2937                                         else
2938                                                 return OPERATOR_RUNNING_MODAL|OPERATOR_PASS_THROUGH;
2939                                 }
2940                                 break;
2941                         case TIMER: {
2942                                 /* handle tooltip timer */
2943                                 if(event->customdata == data->tooltiptimer) {
2944                                         if(!data->tooltip) {
2945                                                 data->tooltip= ui_tooltip_create(C, data->region, but);
2946                                                 WM_event_add_notifier(C, WM_NOTE_WINDOW_REDRAW, 0, NULL);
2947                                         }
2948
2949                                         WM_event_remove_window_timer(C->window, data->tooltiptimer);
2950                                         data->tooltiptimer= NULL;
2951                                 }
2952                                 else if(event->customdata == data->autoopentimer) {
2953                                         button_activate_state(C, but, BUTTON_STATE_BLOCK_OPEN);
2954
2955                                         WM_event_remove_window_timer(C->window, data->autoopentimer);
2956                                         data->autoopentimer= NULL;
2957                                 }
2958
2959                                 break;
2960                         default:
2961                                 handled= ui_do_button(C, block, but, event);
2962                         }
2963                 }
2964
2965         }
2966         else if(data->state == BUTTON_STATE_WAIT_RELEASE) {
2967                 switch(event->type) {
2968                         case MOUSEMOVE:
2969                                 /* deselect the button when moving the mouse away */
2970                                 but= ui_but_find_mouse_over(data->region, event->x, event->y, &block);
2971
2972                                 if(but && but->activate == data) {
2973                                         if(!(but->flag & UI_SELECT)) {
2974                                                 but->flag |= UI_SELECT;
2975                                                 WM_event_add_notifier(C, WM_NOTE_WINDOW_REDRAW, 0, NULL);
2976                                         }
2977                                 }
2978                                 else {
2979                                         but= ui_but_find_activated(data->region, data, &block);
2980                                         if(but->flag & UI_SELECT) {
2981                                                 but->flag &= ~UI_SELECT;
2982                                                 WM_event_add_notifier(C, WM_NOTE_WINDOW_REDRAW, 0, NULL);
2983                                         }
2984                                 }
2985                                 break;
2986                 }
2987
2988                 ui_do_button(C, block, but, event);
2989                 handled= 1;
2990         }
2991         else if(data->state == BUTTON_STATE_WAIT_FLASH) {
2992                 switch(event->type) {
2993                         case TIMER: {
2994                                 if(event->customdata == data->flashtimer)
2995                                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2996                         }
2997                 }
2998         }
2999         else if(data->state == BUTTON_STATE_BLOCK_OPEN) {
3000                 switch(event->type) {
3001                         case MOUSEMOVE: {
3002                                 uiBut *bt= ui_but_find_mouse_over(data->region, event->x, event->y, &block);
3003
3004                                 if(bt && bt->activate != data) {
3005                                         data->cancel= 1;
3006                                         if(button_activate_try_exit(C, op, event))
3007                                                 return OPERATOR_CANCELLED|OPERATOR_PASS_THROUGH;
3008                                         else
3009                                                 return OPERATOR_RUNNING_MODAL|OPERATOR_PASS_THROUGH;