Work on gesture, some more cleaning.
[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, C->window, 0, 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, C->window, 0, 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, C->window, 0, 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 void 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                 else if(ELEM(event->type, PADENTER, RETKEY) && event->val)
1340                         button_activate_state(C, but, BUTTON_STATE_WAIT_FLASH);
1341         }
1342         else if(data->state == BUTTON_STATE_WAIT_RELEASE) {
1343                 if(event->type == LEFTMOUSE && event->val==0) {
1344                         if(!(but->flag & UI_SELECT))
1345                                 data->cancel= 1;
1346                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1347                 }
1348         }
1349 }
1350
1351 static void ui_do_but_KEYEVT(bContext *C, uiBut *but, uiActivateBut *data, wmEvent *event)
1352 {
1353         if(data->state == BUTTON_STATE_HIGHLIGHT) {
1354                 if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val)
1355                         button_activate_state(C, but, BUTTON_STATE_WAIT_KEY_EVENT);
1356         }
1357         else if(data->state == BUTTON_STATE_WAIT_KEY_EVENT) {
1358                 if(event->type == MOUSEMOVE)
1359                         return;
1360
1361                 /* XXX 2.50 missing function */
1362 #if 0
1363                 if(event->val) {
1364                         if(!key_event_to_string(event)[0])
1365                                 data->cancel= 1;
1366                         else
1367                                 ui_set_but_val(but, event->type);
1368
1369                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1370                 }
1371 #endif
1372         }
1373 }
1374
1375 static void ui_do_but_TEX(bContext *C, uiBlock *block, uiBut *but, uiActivateBut *data, wmEvent *event)
1376 {
1377         if(data->state == BUTTON_STATE_HIGHLIGHT) {
1378                 if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val)
1379                         button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
1380         }
1381         else if(data->state == BUTTON_STATE_TEXT_EDITING)
1382                 ui_do_but_textedit(C, block, but, data, event);
1383         else if(data->state == BUTTON_STATE_TEXT_SELECTING)
1384                 ui_do_but_textedit_select(C, block, but, data, event);
1385 }
1386
1387 static void ui_do_but_TOG(bContext *C, uiBut *but, uiActivateBut *data, wmEvent *event)
1388 {
1389         if(data->state == BUTTON_STATE_HIGHLIGHT) {
1390                 if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val) {
1391                         data->togdual= event->ctrl;
1392                         data->togonly= !event->shift;
1393                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1394                 }
1395         }
1396 }
1397
1398 static void ui_do_but_EXIT(bContext *C, uiBut *but, uiActivateBut *data, wmEvent *event)
1399 {
1400         if(data->state == BUTTON_STATE_HIGHLIGHT)
1401                 if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val)
1402                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1403 }
1404
1405 static int ui_numedit_but_NUM(uiBut *but, uiActivateBut *data, float fac, int snap, int mx)
1406 {
1407         float deler, tempf;
1408         int lvalue, temp, changed= 0;
1409         
1410         if(mx == data->draglastx)
1411                 return changed;
1412         
1413         /* drag-lock - prevent unwanted scroll adjustments */
1414         /* change value (now 3) to adjust threshold in pixels */
1415         if(data->draglock) {
1416                 if(abs(mx-data->dragstartx) <= 3)
1417                         return changed;
1418
1419                 data->draglock= 0;
1420                 data->dragstartx= mx;  /* ignore mouse movement within drag-lock */
1421         }
1422
1423         deler= 500;
1424         if(!ui_is_but_float(but)) {
1425                 if((but->max-but->min)<100) deler= 200.0;
1426                 if((but->max-but->min)<25) deler= 50.0;
1427         }
1428         deler /= fac;
1429
1430         if(ui_is_but_float(but) && but->max-but->min > 11) {
1431                 /* non linear change in mouse input- good for high precicsion */
1432                 data->dragf+= (((float)(mx-data->draglastx))/deler) * (fabs(data->dragstartx-mx)*0.002);
1433         } else if (!ui_is_but_float(but) && but->max-but->min > 129) { /* only scale large int buttons */
1434                 /* non linear change in mouse input- good for high precicsionm ints need less fine tuning */
1435                 data->dragf+= (((float)(mx-data->draglastx))/deler) * (fabs(data->dragstartx-mx)*0.004);
1436         } else {
1437                 /*no scaling */
1438                 data->dragf+= ((float)(mx-data->draglastx))/deler ;
1439         }
1440         
1441         if(data->dragf>1.0) data->dragf= 1.0;
1442         if(data->dragf<0.0) data->dragf= 0.0;
1443         data->draglastx= mx;
1444         tempf= ( but->min + data->dragf*(but->max-but->min));
1445         
1446         if(!ui_is_but_float(but)) {
1447                 
1448                 temp= floor(tempf+.5);
1449                 
1450                 if(tempf==but->min || tempf==but->max);
1451                 else if(snap) {
1452                         if(snap == 2) temp= 100*(temp/100);
1453                         else temp= 10*(temp/10);
1454                 }
1455                 if( temp>=but->min && temp<=but->max) {
1456                         lvalue= (int)data->value;
1457                         
1458                         if(temp != lvalue ) {
1459                                 data->dragchange= 1;
1460                                 data->value= (double)temp;
1461                                 changed= 1;
1462                         }
1463                 }
1464
1465         }
1466         else {
1467                 temp= 0;
1468                 if(snap) {
1469                         if(snap == 2) {
1470                                 if(tempf==but->min || tempf==but->max);
1471                                 else if(but->max-but->min < 2.10) tempf= 0.01*floor(100.0*tempf);
1472                                 else if(but->max-but->min < 21.0) tempf= 0.1*floor(10.0*tempf);
1473                                 else tempf= floor(tempf);
1474                         }
1475                         else {
1476                                 if(tempf==but->min || tempf==but->max);
1477                                 else if(but->max-but->min < 2.10) tempf= 0.1*floor(10*tempf);
1478                                 else if(but->max-but->min < 21.0) tempf= floor(tempf);
1479                                 else tempf= 10.0*floor(tempf/10.0);
1480                         }
1481                 }
1482
1483                 if( tempf>=but->min && tempf<=but->max) {
1484                         if(tempf != data->value) {
1485                                 data->dragchange= 1;
1486                                 data->value= tempf;
1487                                 changed= 1;
1488                         }
1489                 }
1490         }
1491
1492         return changed;
1493 }
1494
1495 static void ui_do_but_NUM(bContext *C, uiBlock *block, uiBut *but, uiActivateBut *data, wmEvent *event)
1496 {
1497         int mx, my, click= 0;
1498
1499         mx= event->x;
1500         my= event->y;
1501         ui_window_to_block(data->region, block, &mx, &my);
1502
1503         if(data->state == BUTTON_STATE_HIGHLIGHT) {
1504                 if(event->val) {
1505                         if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->shift) {
1506                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
1507                         }
1508                         else if(event->type == LEFTMOUSE) {
1509                                 data->dragstartx= mx;
1510                                 data->draglastx= mx;
1511                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
1512                         }
1513                         else if(ELEM(event->type, PADENTER, RETKEY) && event->val)
1514                                 click= 1;
1515                 }
1516         }
1517         else if(data->state == BUTTON_STATE_NUM_EDITING) {
1518                 if(event->type == LEFTMOUSE && event->val==0) {
1519                         if(data->dragchange)
1520                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
1521                         else
1522                                 click= 1;
1523                 }
1524                 else if(event->type == MOUSEMOVE) {
1525                         float fac;
1526                         int snap;
1527
1528                         fac= 1.0f;
1529                         if(event->shift) fac /= 10.0f;
1530                         if(event->alt) fac /= 20.0f;
1531
1532                         if(event->custom == EVT_DATA_TABLET) {
1533                                 wmTabletData *wmtab= event->customdata;
1534
1535                                 /* de-sensitise based on tablet pressure */
1536                                 if (ELEM(wmtab->Active, DEV_STYLUS, DEV_ERASER))
1537                                         fac *= wmtab->Pressure;
1538                         }
1539                         
1540                         snap= (event->ctrl)? (event->shift)? 2: 1: 0;
1541
1542                         if(ui_numedit_but_NUM(but, data, fac, snap, mx))
1543                                 ui_numedit_apply(C, block, but, data);
1544                 }
1545         }
1546         else if(data->state == BUTTON_STATE_TEXT_EDITING)
1547                 ui_do_but_textedit(C, block, but, data, event);
1548         else if(data->state == BUTTON_STATE_TEXT_SELECTING)
1549                 ui_do_but_textedit_select(C, block, but, data, event);
1550
1551         if(click) {
1552                 /* we can click on the side arrows to increment/decrement,
1553                  * or click inside to edit the value directly */
1554                 float tempf;
1555                 int temp;
1556
1557                 if(!ui_is_but_float(but)) {
1558                         if(mx < (but->x1 + (but->x2 - but->x1)/3 - 3)) {
1559                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
1560
1561                                 temp= (int)data->value - 1;
1562                                 if(temp>=but->min && temp<=but->max)
1563                                         data->value= (double)temp;
1564                                 else
1565                                         data->cancel= 1;
1566
1567                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
1568                         }
1569                         else if(mx > (but->x1 + (2*(but->x2 - but->x1)/3) + 3)) {
1570                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
1571
1572                                 temp= (int)data->value + 1;
1573                                 if(temp>=but->min && temp<=but->max)
1574                                         data->value= (double)temp;
1575                                 else
1576                                         data->cancel= 1;
1577
1578                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
1579                         }
1580                         else
1581                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
1582                 }
1583                 else {
1584                         if(mx < (but->x1 + (but->x2 - but->x1)/3 - 3)) {
1585                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
1586
1587                                 tempf= data->value - 0.01*but->a1;
1588                                 if (tempf < but->min) tempf = but->min;
1589                                 data->value= tempf;
1590
1591                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
1592                         }
1593                         else if(mx > but->x1 + (2*((but->x2 - but->x1)/3) + 3)) {
1594                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
1595
1596                                 tempf= data->value + 0.01*but->a1;
1597                                 if (tempf < but->min) tempf = but->min;
1598                                 data->value= tempf;
1599
1600                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
1601                         }
1602                         else
1603                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
1604                 }
1605         }
1606 }
1607
1608 static int ui_numedit_but_SLI(uiBut *but, uiActivateBut *data, int shift, int ctrl, int mx)
1609 {
1610         float deler, f, tempf;
1611         int temp, lvalue, changed= 0;
1612
1613         if(but->type==NUMSLI) deler= ((but->x2-but->x1)/2 - 5.0*but->aspect);
1614         else if(but->type==HSVSLI) deler= ((but->x2-but->x1)/2 - 5.0*but->aspect);
1615         else deler= (but->x2-but->x1- 5.0*but->aspect);
1616
1617         f= (float)(mx-data->dragstartx)/deler + data->dragfstart;
1618         
1619         if(shift)
1620                 f= (f-data->dragfstart)/10.0 + data->dragfstart;
1621
1622         CLAMP(f, 0.0, 1.0);
1623         tempf= but->min+f*(but->max-but->min);          
1624         temp= floor(tempf+.5);
1625
1626         if(ctrl) {
1627                 if(tempf==but->min || tempf==but->max);
1628                 else if(ui_is_but_float(but)) {
1629
1630                         if(shift) {
1631                                 if(tempf==but->min || tempf==but->max);
1632                                 else if(but->max-but->min < 2.10) tempf= 0.01*floor(100.0*tempf);
1633                                 else if(but->max-but->min < 21.0) tempf= 0.1*floor(10.0*tempf);
1634                                 else tempf= floor(tempf);
1635                         }
1636                         else {
1637                                 if(but->max-but->min < 2.10) tempf= 0.1*floor(10*tempf);
1638                                 else if(but->max-but->min < 21.0) tempf= floor(tempf);
1639                                 else tempf= 10.0*floor(tempf/10.0);
1640                         }
1641                 }
1642                 else {
1643                         temp= 10*(temp/10);
1644                         tempf= temp;
1645                 }
1646         }
1647
1648         if(!ui_is_but_float(but)) {
1649                 lvalue= floor(data->value+0.5);
1650
1651                 if(temp != lvalue) {
1652                         data->value= temp;
1653                         data->dragchange= 1;
1654                         changed= 1;
1655                 }
1656         }
1657         else {
1658                 if(tempf != data->value) {
1659                         data->value= tempf;
1660                         data->dragchange= 1;
1661                         changed= 1;
1662                 }
1663         }
1664
1665         return changed;
1666 }
1667
1668 static void ui_do_but_SLI(bContext *C, uiBlock *block, uiBut *but, uiActivateBut *data, wmEvent *event)
1669 {
1670         int mx, my, click= 0;
1671
1672         mx= event->x;
1673         my= event->y;
1674         ui_window_to_block(data->region, block, &mx, &my);
1675
1676         if(data->state == BUTTON_STATE_HIGHLIGHT) {
1677                 if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val) {
1678                         /* start either dragging as slider, or editing as text */
1679                         if(mx>= -6+(but->x1+but->x2)/2) {
1680                                 if(event->type == LEFTMOUSE) {
1681                                         data->dragstartx= mx;
1682                                         data->draglastx= mx;
1683                                         button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
1684                                 }
1685                                 else
1686                                         click= 1;
1687                         }
1688                         else
1689                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
1690                 }
1691         }
1692         else if(data->state == BUTTON_STATE_NUM_EDITING) {
1693                 if(event->type == LEFTMOUSE && event->val==0) {
1694                         if(data->dragchange)
1695                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
1696                         else
1697                                 click= 1;
1698                 }
1699                 else if(event->type == MOUSEMOVE) {
1700                         if(ui_numedit_but_SLI(but, data, event->shift, event->ctrl, mx))
1701                                 ui_numedit_apply(C, block, but, data);
1702                 }
1703         }
1704         else if(data->state == BUTTON_STATE_TEXT_EDITING)
1705                 ui_do_but_textedit(C, block, but, data, event);
1706         else if(data->state == BUTTON_STATE_TEXT_SELECTING)
1707                 ui_do_but_textedit_select(C, block, but, data, event);
1708
1709         if(click) {
1710                 float f, h;
1711                 float tempf;
1712                 int temp;
1713                 
1714                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
1715
1716                 tempf= data->value;
1717                 temp= (int)data->value;
1718
1719                 h= but->y2-but->y1;
1720
1721                 if(but->type==SLI) f= (float)(mx-but->x1)/(but->x2-but->x1-h);
1722                 else f= (float)(mx- (but->x1+but->x2)/2)/((but->x2-but->x1)/2 - h);
1723                 
1724                 f= but->min+f*(but->max-but->min);
1725
1726                 if(!ui_is_but_float(but)) {
1727                         if(f<temp) temp--;
1728                         else temp++;
1729
1730                         if(temp>=but->min && temp<=but->max)
1731                                 data->value= temp;
1732                         else
1733                                 data->cancel= 1;
1734                 } 
1735                 else {
1736                         if(f<tempf) tempf-=.01;
1737                         else tempf+=.01;
1738
1739                         if(tempf>=but->min && tempf<=but->max)
1740                                 data->value= tempf;
1741                         else
1742                                 data->cancel= 1;
1743                 }
1744
1745                 button_activate_state(C, but, BUTTON_STATE_EXIT);
1746         }
1747 }
1748
1749 static void ui_do_but_BLOCK(bContext *C, uiBut *but, uiActivateBut *data, wmEvent *event)
1750 {
1751         if(data->state == BUTTON_STATE_HIGHLIGHT) {
1752                 if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val)
1753                         button_activate_state(C, but, BUTTON_STATE_BLOCK_OPEN);
1754         }
1755         else if(data->state == BUTTON_STATE_BLOCK_OPEN) {
1756                 if(event->type == MESSAGE) {
1757                         uiMenuBlockHandle *handle= event->customdata;
1758
1759                         if(handle == data->blockhandle) {
1760                                 data->blockretval= handle->blockretval;
1761
1762                                 if(handle->blockretval == UI_RETURN_OK) {
1763                                         if(but->type == COL)
1764                                                 VECCOPY(data->vec, handle->retvec)
1765                                         else if(ELEM3(but->type, MENU, ICONROW, ICONTEXTROW))
1766                                                 data->value= handle->retvalue;
1767                                 }
1768
1769                                 if(handle->blockretval == UI_RETURN_OUT)
1770                                         /* we close the block and proceed as if nothing happened */
1771                                         button_activate_state(C, but, BUTTON_STATE_HIGHLIGHT);
1772                                 else
1773                                         /* ok/cancel, we exit and will send message in _exit */
1774                                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1775                         }
1776                 }
1777         }
1778 }
1779
1780 static int ui_numedit_but_NORMAL(uiBut *but, uiActivateBut *data, int mx, int my)
1781 {
1782         float dx, dy, rad, radsq, mrad, *fp;
1783         int mdx, mdy, changed= 1;
1784         
1785         /* button is presumed square */
1786         /* if mouse moves outside of sphere, it does negative normal */
1787
1788         fp= data->origvec;
1789         rad= (but->x2 - but->x1);
1790         radsq= rad*rad;
1791         
1792         if(fp[2]>0.0f) {
1793                 mdx= (rad*fp[0]);
1794                 mdy= (rad*fp[1]);
1795         }
1796         else if(fp[2]> -1.0f) {
1797                 mrad= rad/sqrt(fp[0]*fp[0] + fp[1]*fp[1]);
1798                 
1799                 mdx= 2.0f*mrad*fp[0] - (rad*fp[0]);
1800                 mdy= 2.0f*mrad*fp[1] - (rad*fp[1]);
1801         }
1802         else mdx= mdy= 0;
1803         
1804         dx= (float)(mx+mdx-data->dragstartx);
1805         dy= (float)(my+mdy-data->dragstarty);
1806
1807         fp= data->vec;
1808         mrad= dx*dx+dy*dy;
1809         if(mrad < radsq) {      /* inner circle */
1810                 fp[0]= dx;
1811                 fp[1]= dy;
1812                 fp[2]= sqrt( radsq-dx*dx-dy*dy );
1813         }
1814         else {  /* outer circle */
1815                 
1816                 mrad= rad/sqrt(mrad);   // veclen
1817                 
1818                 dx*= (2.0f*mrad - 1.0f);
1819                 dy*= (2.0f*mrad - 1.0f);
1820                 
1821                 mrad= dx*dx+dy*dy;
1822                 if(mrad < radsq) {
1823                         fp[0]= dx;
1824                         fp[1]= dy;
1825                         fp[2]= -sqrt( radsq-dx*dx-dy*dy );
1826                 }
1827         }
1828         Normalize(fp);
1829
1830         data->draglastx= mx;
1831         data->draglasty= my;
1832
1833         return changed;
1834 }
1835
1836 static void ui_do_but_NORMAL(bContext *C, uiBlock *block, uiBut *but, uiActivateBut *data, wmEvent *event)
1837 {
1838         int mx, my;
1839
1840         mx= event->x;
1841         my= event->y;
1842         ui_window_to_block(data->region, block, &mx, &my);
1843
1844         if(data->state == BUTTON_STATE_HIGHLIGHT) {
1845                 if(event->type==LEFTMOUSE && event->val) {
1846                         data->dragstartx= mx;
1847                         data->dragstarty= my;
1848                         data->draglastx= mx;
1849                         data->draglasty= my;
1850                         button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
1851
1852                         /* also do drag the first time */
1853                         if(ui_numedit_but_NORMAL(but, data, mx, my))
1854                                 ui_numedit_apply(C, block, but, data);
1855                 }
1856         }
1857         else if(data->state == BUTTON_STATE_NUM_EDITING) {
1858                 if(event->type == MOUSEMOVE) {
1859                         if(mx!=data->draglastx || my!=data->draglasty) {
1860                                 if(ui_numedit_but_NORMAL(but, data, mx, my))
1861                                         ui_numedit_apply(C, block, but, data);
1862                         }
1863                 }
1864                 else if(event->type==LEFTMOUSE && event->val==0)
1865                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1866         }
1867 }
1868
1869 static int ui_numedit_but_HSVCUBE(uiBut *but, uiActivateBut *data, int mx, int my)
1870 {
1871         float x, y;
1872         int changed= 1;
1873
1874         /* relative position within box */
1875         x= ((float)mx-but->x1)/(but->x2-but->x1);
1876         y= ((float)my-but->y1)/(but->y2-but->y1);
1877         CLAMP(x, 0.0, 1.0);
1878         CLAMP(y, 0.0, 1.0);
1879         
1880         if(but->a1==0) {
1881                 but->hsv[0]= x; 
1882                 but->hsv[2]= y; 
1883         }
1884         else if(but->a1==1) {
1885                 but->hsv[0]= x;                                 
1886                 but->hsv[1]= y;                                 
1887         }
1888         else if(but->a1==2) {
1889                 but->hsv[2]= x; 
1890                 but->hsv[1]= y; 
1891         }
1892         else
1893                 but->hsv[0]= x; 
1894
1895         ui_set_but_hsv(but);    // converts to rgb
1896         
1897         // update button values and strings
1898         ui_update_block_buts_hsv(but->block, but->hsv);
1899
1900         data->draglastx= mx;
1901         data->draglasty= my;
1902
1903         return changed;
1904 }
1905
1906 static void ui_do_but_HSVCUBE(bContext *C, uiBlock *block, uiBut *but, uiActivateBut *data, wmEvent *event)
1907 {
1908         int mx, my;
1909
1910         mx= event->x;
1911         my= event->y;
1912         ui_window_to_block(data->region, block, &mx, &my);
1913
1914         if(data->state == BUTTON_STATE_HIGHLIGHT) {
1915                 if(event->type==LEFTMOUSE && event->val) {
1916                         data->dragstartx= mx;
1917                         data->dragstarty= my;
1918                         data->draglastx= mx;
1919                         data->draglasty= my;
1920                         button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
1921
1922                         /* also do drag the first time */
1923                         if(ui_numedit_but_HSVCUBE(but, data, mx, my))
1924                                 ui_numedit_apply(C, block, but, data);
1925                 }
1926         }
1927         else if(data->state == BUTTON_STATE_NUM_EDITING) {
1928                 if(event->type == MOUSEMOVE) {
1929                         if(mx!=data->draglastx || my!=data->draglasty) {
1930                                 if(ui_numedit_but_HSVCUBE(but, data, mx, my))
1931                                         ui_numedit_apply(C, block, but, data);
1932                         }
1933                 }
1934                 else if(event->type==LEFTMOUSE && event->val==0)
1935                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1936         }
1937 }
1938
1939 static int verg_colorband(const void *a1, const void *a2)
1940 {
1941         const CBData *x1=a1, *x2=a2;
1942         
1943         if( x1->pos > x2->pos ) return 1;
1944         else if( x1->pos < x2->pos) return -1;
1945         return 0;
1946 }
1947
1948 static void ui_colorband_update(ColorBand *coba)
1949 {
1950         int a;
1951         
1952         if(coba->tot<2) return;
1953         
1954         for(a=0; a<coba->tot; a++) coba->data[a].cur= a;
1955                 qsort(coba->data, coba->tot, sizeof(CBData), verg_colorband);
1956         for(a=0; a<coba->tot; a++) {
1957                 if(coba->data[a].cur==coba->cur) {
1958                         coba->cur= a;
1959                         break;
1960                 }
1961         }
1962 }
1963
1964 static int ui_numedit_but_COLORBAND(uiBut *but, uiActivateBut *data, int mx)
1965 {
1966         float dx;
1967         int changed= 0;
1968
1969         if(data->draglastx == mx)
1970                 return changed;
1971
1972         dx= ((float)(mx - data->draglastx))/(but->x2-but->x1);
1973         data->dragcbd->pos += dx;
1974         CLAMP(data->dragcbd->pos, 0.0, 1.0);
1975         
1976         ui_colorband_update(data->coba);
1977         data->dragcbd= data->coba->data + data->coba->cur;      /* because qsort */
1978         
1979         data->draglastx= mx;
1980         changed= 1;
1981
1982         return changed;
1983 }
1984
1985 static void ui_do_but_COLORBAND(bContext *C, uiBlock *block, uiBut *but, uiActivateBut *data, wmEvent *event)
1986 {
1987         ColorBand *coba;
1988         CBData *cbd;
1989         int mx, my, a, xco, mindist= 12;
1990
1991         mx= event->x;
1992         my= event->y;
1993         ui_window_to_block(data->region, block, &mx, &my);
1994
1995         if(data->state == BUTTON_STATE_HIGHLIGHT) {
1996                 if(event->type==LEFTMOUSE && event->val) {
1997                         coba= (ColorBand*)but->poin;
1998
1999                         if(event->ctrl) {
2000                                 /* insert new key on mouse location */
2001                                 if(coba->tot < MAXCOLORBAND-1) {
2002                                         float pos= ((float)(mx - but->x1))/(but->x2-but->x1);
2003                                         float col[4];
2004                                         
2005                                         do_colorband(coba, pos, col);   /* executes it */
2006                                         
2007                                         coba->tot++;
2008                                         coba->cur= coba->tot-1;
2009                                         
2010                                         coba->data[coba->cur].r= col[0];
2011                                         coba->data[coba->cur].g= col[1];
2012                                         coba->data[coba->cur].b= col[2];
2013                                         coba->data[coba->cur].a= col[3];
2014                                         coba->data[coba->cur].pos= pos;
2015
2016                                         ui_colorband_update(coba);
2017                                 }
2018
2019                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2020                         }
2021                         else {
2022                                 data->dragstartx= mx;
2023                                 data->dragstarty= my;
2024                                 data->draglastx= mx;
2025                                 data->draglasty= my;
2026
2027                                 /* activate new key when mouse is close */
2028                                 for(a=0, cbd= coba->data; a<coba->tot; a++, cbd++) {
2029                                         xco= but->x1 + (cbd->pos*(but->x2-but->x1));
2030                                         xco= ABS(xco-mx);
2031                                         if(a==coba->cur) xco+= 5; // selected one disadvantage
2032                                         if(xco<mindist) {
2033                                                 coba->cur= a;
2034                                                 mindist= xco;
2035                                         }
2036                                 }
2037                 
2038                                 data->dragcbd= coba->data + coba->cur;
2039                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2040                         }
2041                 }
2042         }
2043         else if(data->state == BUTTON_STATE_NUM_EDITING) {
2044                 if(event->type == MOUSEMOVE) {
2045                         if(mx!=data->draglastx || my!=data->draglasty) {
2046                                 if(ui_numedit_but_COLORBAND(but, data, mx))
2047                                         ui_numedit_apply(C, block, but, data);
2048                         }
2049                 }
2050                 else if(event->type==LEFTMOUSE && event->val==0)
2051                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2052         }
2053 }
2054
2055 static int ui_numedit_but_CURVE(uiBut *but, uiActivateBut *data, int snap, int mx, int my)
2056 {
2057         CurveMapping *cumap= data->cumap;
2058         CurveMap *cuma= cumap->cm+cumap->cur;
2059         CurveMapPoint *cmp= cuma->curve;
2060         float fx, fy, zoomx, zoomy, offsx, offsy;
2061         int a, changed= 0;
2062
2063         zoomx= (but->x2-but->x1)/(cumap->curr.xmax-cumap->curr.xmin);
2064         zoomy= (but->y2-but->y1)/(cumap->curr.ymax-cumap->curr.ymin);
2065         offsx= cumap->curr.xmin;
2066         offsy= cumap->curr.ymin;
2067
2068         if(data->dragsel != -1) {
2069                 int moved_point= 0;             /* for ctrl grid, can't use orig coords because of sorting */
2070                 
2071                 fx= (mx-data->draglastx)/zoomx;
2072                 fy= (my-data->draglasty)/zoomy;
2073                 for(a=0; a<cuma->totpoint; a++) {
2074                         if(cmp[a].flag & SELECT) {
2075                                 float origx= cmp[a].x, origy= cmp[a].y;
2076                                 cmp[a].x+= fx;
2077                                 cmp[a].y+= fy;
2078                                 if(snap) {
2079                                         cmp[a].x= 0.125f*floor(0.5f + 8.0f*cmp[a].x);
2080                                         cmp[a].y= 0.125f*floor(0.5f + 8.0f*cmp[a].y);
2081                                 }
2082                                 if(cmp[a].x!=origx || cmp[a].y!=origy)
2083                                         moved_point= 1;
2084                         }
2085                 }
2086
2087                 curvemapping_changed(cumap, 0); /* no remove doubles */
2088                 
2089                 if(moved_point) {
2090                         data->draglastx= mx;
2091                         data->draglasty= my;
2092                         changed= 1;
2093                 }
2094
2095                 data->dragchange= 1; /* mark for selection */
2096         }
2097         else {
2098                 fx= (mx-data->draglastx)/zoomx;
2099                 fy= (my-data->draglasty)/zoomy;
2100                 
2101                 /* clamp for clip */
2102                 if(cumap->flag & CUMA_DO_CLIP) {
2103                         if(cumap->curr.xmin-fx < cumap->clipr.xmin)
2104                                 fx= cumap->curr.xmin - cumap->clipr.xmin;
2105                         else if(cumap->curr.xmax-fx > cumap->clipr.xmax)
2106                                 fx= cumap->curr.xmax - cumap->clipr.xmax;
2107                         if(cumap->curr.ymin-fy < cumap->clipr.ymin)
2108                                 fy= cumap->curr.ymin - cumap->clipr.ymin;
2109                         else if(cumap->curr.ymax-fy > cumap->clipr.ymax)
2110                                 fy= cumap->curr.ymax - cumap->clipr.ymax;
2111                 }                               
2112
2113                 cumap->curr.xmin-=fx;
2114                 cumap->curr.ymin-=fy;
2115                 cumap->curr.xmax-=fx;
2116                 cumap->curr.ymax-=fy;
2117                 
2118                 data->draglastx= mx;
2119                 data->draglasty= my;
2120
2121                 changed= 1;
2122         }
2123
2124         return changed;
2125 }
2126
2127 static void ui_do_but_CURVE(bContext *C, uiBlock *block, uiBut *but, uiActivateBut *data, wmEvent *event)
2128 {
2129         int mx, my, a, changed= 0;
2130
2131         mx= event->x;
2132         my= event->y;
2133         ui_window_to_block(data->region, block, &mx, &my);
2134
2135         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2136                 if(event->type==LEFTMOUSE && event->val) {
2137                         CurveMapping *cumap= (CurveMapping*)but->poin;
2138                         CurveMap *cuma= cumap->cm+cumap->cur;
2139                         CurveMapPoint *cmp= cuma->curve;
2140                         float fx, fy, zoomx, zoomy, offsx, offsy;
2141                         float dist, mindist= 200.0f; // 14 pixels radius
2142                         int sel= -1;
2143
2144                         zoomx= (but->x2-but->x1)/(cumap->curr.xmax-cumap->curr.xmin);
2145                         zoomy= (but->y2-but->y1)/(cumap->curr.ymax-cumap->curr.ymin);
2146                         offsx= cumap->curr.xmin;
2147                         offsy= cumap->curr.ymin;
2148
2149                         if(event->ctrl) {
2150                                 fx= ((float)my - but->x1)/zoomx + offsx;
2151                                 fy= ((float)my - but->y1)/zoomy + offsy;
2152                                 
2153                                 curvemap_insert(cuma, fx, fy);
2154                                 curvemapping_changed(cumap, 0);
2155                                 changed= 1;
2156                         }
2157
2158                         /* check for selecting of a point */
2159                         cmp= cuma->curve;       /* ctrl adds point, new malloc */
2160                         for(a=0; a<cuma->totpoint; a++) {
2161                                 fx= but->x1 + zoomx*(cmp[a].x-offsx);
2162                                 fy= but->y1 + zoomy*(cmp[a].y-offsy);
2163                                 dist= (fx-mx)*(fx-mx) + (fy-my)*(fy-my);
2164                                 if(dist < mindist) {
2165                                         sel= a;
2166                                         mindist= dist;
2167                                 }
2168                         }
2169
2170                         if (sel == -1) {
2171                                 /* if the click didn't select anything, check if it's clicked on the 
2172                                  * curve itself, and if so, add a point */
2173                                 fx= ((float)mx - but->x1)/zoomx + offsx;
2174                                 fy= ((float)my - but->y1)/zoomy + offsy;
2175                                 
2176                                 cmp= cuma->table;
2177
2178                                 /* loop through the curve segment table and find what's near the mouse.
2179                                  * 0.05 is kinda arbitrary, but seems to be what works nicely. */
2180                                 for(a=0; a<=CM_TABLE; a++) {                    
2181                                         if ( ( fabs(fx - cmp[a].x) < (0.05) ) && ( fabs(fy - cmp[a].y) < (0.05) ) ) {
2182                                         
2183                                                 curvemap_insert(cuma, fx, fy);
2184                                                 curvemapping_changed(cumap, 0);
2185
2186                                                 changed= 1;
2187                                                 
2188                                                 /* reset cmp back to the curve points again, rather than drawing segments */            
2189                                                 cmp= cuma->curve;
2190                                                 
2191                                                 /* find newly added point and make it 'sel' */
2192                                                 for(a=0; a<cuma->totpoint; a++)
2193                                                         if(cmp[a].x == fx)
2194                                                                 sel = a;
2195                                                         
2196                                                 break;
2197                                         }
2198                                 }
2199                         }
2200
2201                         if(sel!= -1) {
2202                                 /* ok, we move a point */
2203                                 /* deselect all if this one is deselect. except if we hold shift */
2204                                 if(event->shift==0 && (cmp[sel].flag & SELECT)==0)
2205                                         for(a=0; a<cuma->totpoint; a++)
2206                                                 cmp[a].flag &= ~SELECT;
2207                                 cmp[sel].flag |= SELECT;
2208                         }
2209                         else {
2210                                 /* move the view */
2211                                 data->cancel= 1;
2212                         }
2213
2214                         data->dragsel= sel;
2215
2216                         data->dragstartx= mx;
2217                         data->dragstarty= my;
2218                         data->draglastx= mx;
2219                         data->draglasty= my;
2220
2221                         button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2222                 }
2223         }
2224         else if(data->state == BUTTON_STATE_NUM_EDITING) {
2225                 if(event->type == MOUSEMOVE) {
2226                         if(mx!=data->draglastx || my!=data->draglasty) {
2227                                 if(ui_numedit_but_CURVE(but, data, event->shift, mx, my))
2228                                         ui_numedit_apply(C, block, but, data);
2229                         }
2230                 }
2231                 else if(event->type==LEFTMOUSE && event->val==0) {
2232                         if(data->dragsel != -1) {
2233                                 CurveMapping *cumap= data->cumap;
2234                                 CurveMap *cuma= cumap->cm+cumap->cur;
2235                                 CurveMapPoint *cmp= cuma->curve;
2236
2237                                 if(!data->dragchange) {
2238                                         /* deselect all, select one */
2239                                         if(event->shift==0) {
2240                                                 for(a=0; a<cuma->totpoint; a++)
2241                                                         cmp[a].flag &= ~SELECT;
2242                                                 cmp[data->dragsel].flag |= SELECT;
2243                                         }
2244                                 }
2245                                 else
2246                                         curvemapping_changed(cumap, 1); /* remove doubles */
2247                         }
2248
2249                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2250                 }
2251         }
2252 }
2253
2254 #ifdef INTERNATIONAL
2255 static void ui_do_but_CHARTAB(bContext *C, uiBlock *block, uiBut *but, uiActivateBut *data, wmEvent *event)
2256 {
2257         /* XXX 2.50 bad global and state access */
2258 #if 0
2259         float sx, sy, ex, ey;
2260         float width, height;
2261         float butw, buth;
2262         int mx, my, x, y, cs, che;
2263
2264         mx= event->x;
2265         my= event->y;
2266         ui_window_to_block(data->region, block, &mx, &my);
2267
2268         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2269                 if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val) {
2270                         /* Calculate the size of the button */
2271                         width = abs(but->x2 - but->x1);
2272                         height = abs(but->y2 - but->y1);
2273
2274                         butw = floor(width / 12);
2275                         buth = floor(height / 6);
2276
2277                         /* Initialize variables */
2278                         sx = but->x1;
2279                         ex = but->x1 + butw;
2280                         sy = but->y1 + height - buth;
2281                         ey = but->y1 + height;
2282
2283                         cs = G.charstart;
2284
2285                         /* And the character is */
2286                         x = (int) ((mx / butw) - 0.5);
2287                         y = (int) (6 - ((my / buth) - 0.5));
2288
2289                         che = cs + (y*12) + x;
2290
2291                         if(che > G.charmax)
2292                                 che = 0;
2293
2294                         if(G.obedit)
2295                         {
2296                                 do_textedit(0,0,che);
2297                         }
2298
2299                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2300                 }
2301                 else if(ELEM(event->type, WHEELUPMOUSE, PAGEUPKEY)) {
2302                         for(but= block->buttons.first; but; but= but->next) {
2303                                 if(but->type == CHARTAB) {
2304                                         G.charstart = G.charstart - (12*6);
2305                                         if(G.charstart < 0)
2306                                                 G.charstart = 0;
2307                                         if(G.charstart < G.charmin)
2308                                                 G.charstart = G.charmin;
2309                                         ui_draw_but(but);
2310
2311                                         //Really nasty... to update the num button from the same butblock
2312                                         for(bt= block->buttons.first; bt; bt= bt->next)
2313                                         {
2314                                                 if(ELEM(bt->type, NUM, NUMABS)) {
2315                                                         ui_check_but(bt);
2316                                                         ui_draw_but(bt);
2317                                                 }
2318                                         }
2319                                         retval=UI_CONT;
2320                                         break;
2321                                 }
2322                         }
2323                         break;
2324                 }
2325                 else if(ELEM(event->type, WHEELDOWNMOUSE, PAGEDOWNKEY)) {
2326                         for(but= block->buttons.first; but; but= but->next)
2327                         {
2328                                 if(but->type == CHARTAB)
2329                                 {
2330                                         G.charstart = G.charstart + (12*6);
2331                                         if(G.charstart > (0xffff - 12*6))
2332                                                 G.charstart = 0xffff - (12*6);
2333                                         if(G.charstart > G.charmax - 12*6)
2334                                                 G.charstart = G.charmax - 12*6;
2335                                         ui_draw_but(but);
2336
2337                                         for(bt= block->buttons.first; bt; bt= bt->next)
2338                                         {
2339                                                 if(ELEM(bt->type, NUM, NUMABS)) {
2340                                                         ui_check_but(bt);
2341                                                         ui_draw_but(bt);
2342                                                 }
2343                                         }
2344                                         
2345                                         but->flag |= UI_ACTIVE;
2346                                         retval=UI_RETURN_OK;
2347                                         break;
2348                                 }
2349                         }
2350                         break;
2351                 }
2352         }
2353 #endif
2354 }
2355 #endif
2356
2357 static int ui_do_button(bContext *C, uiBlock *block, uiBut *but, wmEvent *event)
2358 {
2359         uiActivateBut *data;
2360         int handled;
2361
2362         data= but->activate;
2363         handled= 0;
2364
2365         /* handle copy-paste */
2366         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2367                 if(ELEM(event->type, CKEY, VKEY) && event->val && (event->ctrl || event->oskey)) {
2368                         ui_but_copy_paste(C, but, data, (event->type == CKEY)? 'c': 'v');
2369                         return 1;
2370                 }
2371         }
2372
2373         /* these events are swallowed */
2374         if(ELEM5(event->type, LEFTMOUSE, MIDDLEMOUSE, RIGHTMOUSE, WHEELUPMOUSE, WHEELDOWNMOUSE))
2375                 handled= 1;
2376         else if(ELEM(event->type, PADENTER, RETKEY))
2377                 handled= 1;
2378
2379         /* verify if we can edit this button */
2380         if(but->lock) {
2381                 if(but->lockstr) {
2382                         WM_report(C, WM_LOG_WARNING, but->lockstr);
2383                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2384                         return handled;
2385                 }
2386         } 
2387         else if(but->pointype && but->poin==0) {
2388                 /* there's a pointer needed */
2389                 WM_reportf(C, WM_LOG_WARNING, "DoButton pointer error: %s", but->str);
2390                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2391                 return handled;
2392         }
2393
2394         switch(but->type) {
2395         case BUT:
2396                 ui_do_but_BUT(C, but, data, event);
2397                 break;
2398         case KEYEVT:
2399                 ui_do_but_KEYEVT(C, but, data, event);
2400                 break;
2401         case TOG: 
2402         case TOGR: 
2403         case ICONTOG:
2404         case ICONTOGN:
2405         case TOGN:
2406         case BUT_TOGDUAL:
2407                 ui_do_but_TOG(C, but, data, event);
2408                 break;
2409 #if 0
2410         case SCROLL:
2411                 /* DrawBut(b, 1); */
2412                 /* do_scrollbut(b); */
2413                 /* DrawBut(b,0); */
2414                 break;
2415 #endif
2416         case NUM:
2417         case NUMABS:
2418                 ui_do_but_NUM(C, block, but, data, event);
2419                 break;
2420         case SLI:
2421         case NUMSLI:
2422         case HSVSLI:
2423                 ui_do_but_SLI(C, block, but, data, event);
2424                 break;
2425         case ROUNDBOX:  
2426         case LABEL:     
2427         case TOG3:      
2428         case ROW:
2429                 ui_do_but_EXIT(C, but, data, event);
2430                 break;
2431         case TEX:
2432         case IDPOIN:
2433                 ui_do_but_TEX(C, block, but, data, event);
2434                 break;
2435         case MENU:
2436                 ui_do_but_BLOCK(C, but, data, event);
2437                 break;
2438         case ICONROW:
2439                 ui_do_but_BLOCK(C, but, data, event);
2440                 break;
2441         case ICONTEXTROW:
2442                 ui_do_but_BLOCK(C, but, data, event);
2443                 break;
2444         case BLOCK:
2445         case PULLDOWN:
2446                 ui_do_but_BLOCK(C, but, data, event);
2447                 break;
2448         case BUTM:
2449                 ui_do_but_BUT(C, but, data, event);
2450                 break;
2451         case COL:
2452                 if(but->a1 == -1)  // signal to prevent calling up color picker
2453                         ui_do_but_EXIT(C, but, data, event);
2454                 else
2455                         ui_do_but_BLOCK(C, but, data, event);
2456                 break;
2457         case BUT_NORMAL:
2458                 ui_do_but_NORMAL(C, block, but, data, event);
2459                 break;
2460         case BUT_COLORBAND:
2461                 ui_do_but_COLORBAND(C, block, but, data, event);
2462                 break;
2463         case BUT_CURVE:
2464                 ui_do_but_CURVE(C, block, but, data, event);
2465                 break;
2466         case HSVCUBE:
2467                 ui_do_but_HSVCUBE(C, block, but, data, event);
2468                 break;
2469 #ifdef INTERNATIONAL
2470         case CHARTAB:
2471                 ui_do_but_CHARTAB(C, block, but, data, event);
2472                 break;
2473 #endif
2474         /* XXX 2.50 links not implemented yet */
2475 #if 0
2476         case LINK:
2477         case INLINK:
2478                 retval= ui_do_but_LINK(block, but);
2479                 break;
2480 #endif
2481         }
2482         
2483         return handled;
2484 }
2485
2486 /* ************************ button utilities *********************** */
2487
2488 static int ui_but_contains_pt(uiBut *but, int mx, int my)
2489 {
2490         return ((but->x1<mx && but->x2>=mx) && (but->y1<my && but->y2>=my));
2491 }
2492
2493 static uiBut *ui_but_find_activated(ARegion *ar, uiActivateBut *data, uiBlock **rblock)
2494 {
2495         uiBlock *block;
2496         uiBut *but;
2497
2498         for(block=ar->uiblocks.first; block; block=block->next) {
2499                 for(but=block->buttons.first; but; but= but->next) {
2500                         if((but->activate == data && data) || (but->activate && data == NULL)) {
2501                                 if(rblock) *rblock= block;
2502                                 return but;
2503                         }
2504                 }
2505         }
2506
2507         if(rblock) *rblock= NULL;
2508         return NULL;
2509 }
2510
2511 static uiBut *ui_but_find_signal(ARegion *ar, uiActivateBut *data, uiBlock **rblock)
2512 {
2513         uiBlock *block;
2514         uiBut *but;
2515
2516         for(block=ar->uiblocks.first; block; block=block->next) {
2517                 for(but=block->buttons.first; but; but= but->next) {
2518                         if(but->activateflag) {
2519                                 if(rblock) *rblock= block;
2520                                 return but;
2521                         }
2522                 }
2523         }
2524
2525         if(rblock) *rblock= NULL;
2526         return NULL;
2527 }
2528
2529 static uiBut *ui_but_find_mouse_over(ARegion *ar, int x, int y, uiBlock **rblock)
2530 {
2531         uiBlock *block, *blockover= NULL;
2532         uiBut *but, *butover= NULL;
2533         int mx, my;
2534
2535         for(block=ar->uiblocks.first; block; block=block->next) {
2536                 mx= x;
2537                 my= y;
2538                 ui_window_to_block(ar, block, &mx, &my);
2539
2540                 for(but=block->buttons.first; but; but= but->next) {
2541                         /* give precedence to already activated buttons */
2542                         if(ui_but_contains_pt(but, mx, my)) {
2543                                 if(!butover || (!butover->activate && but->activate)) {
2544                                         butover= but;
2545                                         blockover= block;
2546                                 }
2547                         }
2548                 }
2549         }
2550
2551         if(rblock)
2552                 *rblock= blockover;
2553
2554         return butover;
2555 }
2556
2557 /*********************** button activate operator ***************************
2558  * this operator runs from the moment the mouse is position over a button and
2559  * handles all interaction with the button, from highlight to text editing,
2560  * dragging, having a block open to leaving the button to activate another. */
2561
2562 static void button_disable_timers(bContext *C, uiActivateBut *data)
2563 {
2564         if(data->tooltiptimer) {
2565                 WM_event_remove_window_timer(C->window, data->tooltiptimer);
2566                 data->tooltiptimer= NULL;
2567         }
2568         if(data->tooltip) {
2569                 ui_tooltip_free(C, data->tooltip);
2570                 data->tooltip= NULL;
2571         }
2572         data->tooltipdisabled= 1;
2573
2574         if(data->autoopentimer) {
2575                 WM_event_remove_window_timer(C->window, data->autoopentimer);
2576                 data->autoopentimer= NULL;
2577         }
2578 }
2579
2580 static void button_activate_state(bContext *C, uiBut *but, uiActivateButState state)
2581 {
2582         uiActivateBut *data;
2583
2584         data= but->activate;
2585         if(data->state == state)
2586                 return;
2587
2588         if(state == BUTTON_STATE_HIGHLIGHT) {
2589                 but->flag &= ~UI_SELECT;
2590
2591                 /* XXX 2.50 U missing from context */
2592                 if(U.flag & USER_TOOLTIPS)
2593                         if(!data->tooltiptimer && !data->tooltipdisabled)
2594                                 data->tooltiptimer= WM_event_add_window_timer(C->window, BUTTON_TOOLTIP_DELAY, ~0);
2595
2596                 /* automatic open pulldown block timer */
2597                 if(but->type==BLOCK || but->type==MENU || but->type==PULLDOWN || but->type==ICONTEXTROW) {
2598                         if(!data->autoopentimer) {
2599                                 int time;
2600
2601                                 if(but->block->auto_open==2) time= 1;    // test for toolbox
2602                                 else if(but->block->flag & UI_BLOCK_LOOP || but->block->auto_open) time= 5*U.menuthreshold2;
2603                                 else if(U.uiflag & USER_MENUOPENAUTO) time= 5*U.menuthreshold1;
2604                                 else time= -1;
2605
2606                                 if(time >= 0)
2607                                         data->autoopentimer= WM_event_add_window_timer(C->window, time*20, ~0);
2608                         }
2609                 }
2610         }
2611         else {
2612                 but->flag |= UI_SELECT;
2613                 button_disable_timers(C, data);
2614         }
2615
2616         if(state == BUTTON_STATE_TEXT_EDITING && data->state != BUTTON_STATE_TEXT_SELECTING)
2617                 ui_textedit_begin(but, data);
2618         else if(data->state == BUTTON_STATE_TEXT_EDITING && state != BUTTON_STATE_TEXT_SELECTING)
2619                 ui_textedit_end(but, data);
2620         
2621         if(state == BUTTON_STATE_NUM_EDITING)
2622                 ui_numedit_begin(but, data);
2623         else if(data->state == BUTTON_STATE_NUM_EDITING)
2624                 ui_numedit_end(but, data);
2625
2626         if(state == BUTTON_STATE_BLOCK_OPEN) {
2627                 ui_blockopen_begin(C, but, data);
2628                 /* note we move the handler to the region when the block is open,
2629                  * so we don't interfere with the events as long as it's open */
2630                 WM_event_remove_modal_handler(&C->window->handlers, data->operator);
2631                 WM_event_add_modal_handler(&data->region->handlers, data->operator);
2632         }
2633         else if(data->state == BUTTON_STATE_BLOCK_OPEN) {
2634                 ui_blockopen_end(C, but, data);
2635                 WM_event_remove_modal_handler(&data->region->handlers, data->operator);
2636                 WM_event_add_modal_handler(&C->window->handlers, data->operator);
2637         }
2638         
2639         if(state == BUTTON_STATE_WAIT_FLASH) {
2640                 data->flashtimer= WM_event_add_window_timer(C->window, BUTTON_FLASH_DELAY, ~0);
2641         }
2642         else if(data->flashtimer) {
2643                 WM_event_remove_window_timer(C->window, data->flashtimer);
2644                 data->flashtimer= NULL;
2645         }
2646
2647         data->state= state;
2648         WM_event_add_notifier(C->wm, C->window, 0, WM_NOTE_WINDOW_REDRAW, 0, NULL);
2649 }
2650
2651 static void button_activate_init(bContext *C, ARegion *ar, wmOperator *op, uiBut *but, uiBut *lastbut)
2652 {
2653         uiActivateBut *data;
2654
2655         /* setup struct */
2656         data= MEM_callocN(sizeof(uiActivateBut), "uiActivateBut");
2657         data->region= ar;
2658         data->operator= op;
2659         data->interactive= 0;
2660         data->state = BUTTON_STATE_INIT;
2661         op->customdata= data;
2662
2663         /* activate button */
2664         but->flag |= UI_ACTIVE;
2665         but->activate= data;
2666
2667         if(but == lastbut)
2668                 data->tooltipdisabled= 1;
2669
2670         /* we disable auto_open in the block after a threshold, because we still
2671          * want to allow auto opening adjacent menus even if no button is activated
2672          * inbetween going over to the other button, but only for a short while */
2673         if(!lastbut && but->block->auto_open)
2674                 if(but->block->auto_open_last+BUTTON_AUTO_OPEN_THRESH < PIL_check_seconds_timer())
2675                         but->block->auto_open= 0;
2676         
2677         /* modal handler */
2678         WM_event_add_modal_handler(&C->window->handlers, op);
2679
2680         button_activate_state(C, but, BUTTON_STATE_HIGHLIGHT);
2681
2682         if(but->activateflag == UI_ACTIVATE_OPEN)
2683                 button_activate_state(C, but, BUTTON_STATE_BLOCK_OPEN);
2684         else if(but->activateflag == UI_ACTIVATE_TEXT_EDITING)
2685                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2686         else if(but->activateflag == UI_ACTIVATE_APPLY)
2687                 button_activate_state(C, but, BUTTON_STATE_WAIT_FLASH);
2688         
2689         but->activateflag= 0;
2690 }
2691
2692 static void button_activate_exit(bContext *C, uiActivateBut *data, wmOperator *op)
2693 {
2694         uiBut *but;
2695         uiBlock *block;
2696
2697         /* verify if we still have a button, can be NULL! */
2698         but= ui_but_find_activated(data->region, data, &block);
2699
2700         /* stop text editing */
2701         if(data->state == BUTTON_STATE_TEXT_EDITING || data->state == BUTTON_STATE_TEXT_SELECTING) {
2702                 data->cancel= 1;
2703                 ui_textedit_end(but, data);
2704         }
2705
2706         if(data->state == BUTTON_STATE_NUM_EDITING)
2707                 ui_numedit_end(but, data);
2708         
2709         if(data->state == BUTTON_STATE_BLOCK_OPEN) {
2710                 ui_blockopen_end(C, but, data);
2711                 WM_event_remove_modal_handler(&data->region->handlers, data->operator);
2712         }
2713         else
2714                 WM_event_remove_modal_handler(&C->window->handlers, op);                                
2715
2716         if(but) {
2717                 /* if someone is expecting a message */
2718                 if(but->block->handle && !(but->block->flag & UI_BLOCK_KEEP_OPEN) && !data->cancel) {
2719                         uiMenuBlockHandle *handle;
2720
2721                         handle= but->block->handle;
2722                         handle->butretval= data->retval;
2723                         if(data->blockretval) {
2724                                 handle->blockretval= data->blockretval;
2725
2726                                 /* if we got a cancel from a block menu, then also cancel this
2727                                  * button, we set that here to instead of in do_BLOCK to make
2728                                  * a distinction with cancel after lost highlight for example */
2729                                 if(data->blockretval == UI_RETURN_CANCEL)
2730                                         data->cancel= 1;
2731                         }
2732                         else
2733                                 handle->blockretval= UI_RETURN_OK;
2734
2735                         WM_event_add_message(C->wm, handle, 0);
2736                 }
2737
2738                 /* apply the button action or value */
2739                 ui_apply_button(block, but, data, 0);
2740
2741                 /* clean up button */
2742                 but->activate= NULL;
2743                 but->flag &= ~(UI_ACTIVE|UI_SELECT);
2744         }
2745
2746         /* redraw */
2747         WM_event_add_notifier(C->wm, C->window, 0, WM_NOTE_WINDOW_REDRAW, 0, NULL);
2748
2749         /* clean up */
2750         button_disable_timers(C, data);
2751
2752         if(data->str)
2753                 MEM_freeN(data->str);
2754         if(data->origstr)
2755                 MEM_freeN(data->origstr);
2756         if(data->flashtimer)
2757                 WM_event_remove_window_timer(C->window, data->flashtimer);
2758
2759         MEM_freeN(op->customdata);
2760         op->customdata= NULL;
2761 }
2762
2763 static int button_activate_try_init(bContext *C, ARegion *ar, wmOperator *op, wmEvent *event, uiBut *lastbut)
2764 {
2765         uiBut *but;
2766
2767         /* try to activate a button */
2768         if(!ar)
2769                 return OPERATOR_PASS_THROUGH;
2770
2771         if(ui_but_find_activated(ar, NULL, NULL))
2772                 return OPERATOR_PASS_THROUGH;
2773
2774         but= ui_but_find_signal(ar, NULL, NULL);
2775
2776         if(!but)
2777                 but= ui_but_find_mouse_over(ar, event->x, event->y, NULL);
2778
2779         if(lastbut && but && but!=lastbut)
2780                 return OPERATOR_PASS_THROUGH;
2781
2782         if(but && !but->activate) {
2783                 button_activate_init(C, ar, op, but, lastbut);
2784                 return OPERATOR_RUNNING_MODAL;
2785         }
2786
2787         return OPERATOR_PASS_THROUGH;
2788 }
2789
2790 static int button_activate_try_exit(bContext *C, wmOperator *op, wmEvent *event)
2791 {
2792         ARegion *ar;
2793         uiActivateBut *data;
2794         uiBut *but;
2795         int state;
2796
2797         data= op->customdata;
2798         ar= data->region;
2799
2800         but= ui_but_find_activated(data->region, data, NULL);
2801
2802         /* exit the current button, but try to re-init as well */
2803         button_activate_exit(C, op->customdata, op);
2804         state= button_activate_try_init(C, ar, op, event, but);
2805
2806         return (state != OPERATOR_RUNNING_MODAL);
2807 }
2808
2809 static int button_activate_invoke(bContext *C, wmOperator *op, wmEvent *event)
2810 {
2811         return button_activate_try_init(C, C->region, op, event, NULL);
2812 }
2813
2814 static int button_activate_cancel(bContext *C, wmOperator *op)
2815 {
2816         uiActivateBut *data;
2817
2818         data= op->customdata;
2819         data->cancel= 1;
2820         button_activate_exit(C, op->customdata, op);
2821
2822         return OPERATOR_CANCELLED;
2823 }
2824
2825 static int button_activate_modal(bContext *C, wmOperator *op, wmEvent *event)
2826 {
2827         uiActivateBut *data;
2828         uiBut *but;
2829         uiBlock *block;
2830         int handled= 0;
2831
2832         data= op->customdata;
2833
2834         /* check if the button dissappeared somehow */
2835         if(!(but= ui_but_find_activated(data->region, data, &block))) {
2836                 data->cancel= 1;
2837                 if(button_activate_try_exit(C, op, event))
2838                         return OPERATOR_CANCELLED|OPERATOR_PASS_THROUGH;
2839                 else
2840                         return OPERATOR_RUNNING_MODAL|OPERATOR_PASS_THROUGH;
2841         }
2842
2843         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2844                 switch(event->type) {
2845                         case MOUSEMOVE:
2846                                 /* verify if we are still over the button, if not exit */
2847                                 but= ui_but_find_mouse_over(data->region, event->x, event->y, &block);
2848
2849                                 if(!but || but->activate != data) {
2850                                         data->cancel= 1;
2851                                         if(button_activate_try_exit(C, op, event))
2852                                                 return OPERATOR_CANCELLED|OPERATOR_PASS_THROUGH;
2853                                         else
2854                                                 return OPERATOR_RUNNING_MODAL|OPERATOR_PASS_THROUGH;
2855                                 }
2856                                 break;
2857                         case TIMER: {
2858                                 /* handle tooltip timer */
2859                                 if(event->customdata == data->tooltiptimer) {
2860                                         if(!data->tooltip) {
2861                                                 data->tooltip= ui_tooltip_create(C, data->region, but);
2862                                                 WM_event_add_notifier(C->wm, C->window, 0, WM_NOTE_WINDOW_REDRAW, 0, NULL);
2863                                         }
2864
2865                                         WM_event_remove_window_timer(C->window, data->tooltiptimer);
2866                                         data->tooltiptimer= NULL;
2867                                 }
2868                                 else if(event->customdata == data->autoopentimer) {
2869                                         button_activate_state(C, but, BUTTON_STATE_BLOCK_OPEN);
2870
2871                                         WM_event_remove_window_timer(C->window, data->autoopentimer);
2872                                         data->autoopentimer= NULL;
2873                                 }
2874
2875                                 break;
2876                         }
2877                 }
2878
2879                 handled= ui_do_button(C, block, but, event);
2880         }
2881         else if(data->state == BUTTON_STATE_WAIT_RELEASE) {
2882                 switch(event->type) {
2883                         case MOUSEMOVE:
2884                                 /* deselect the button when moving the mouse away */
2885                                 but= ui_but_find_mouse_over(data->region, event->x, event->y, &block);
2886
2887                                 if(but && but->activate == data) {
2888                                         if(!(but->flag & UI_SELECT)) {
2889                                                 but->flag |= UI_SELECT;
2890                                                 WM_event_add_notifier(C->wm, C->window, 0, WM_NOTE_WINDOW_REDRAW, 0, NULL);
2891                                         }
2892                                 }
2893                                 else {
2894                                         but= ui_but_find_activated(data->region, data, &block);
2895                                         if(but->flag & UI_SELECT) {
2896                                                 but->flag &= ~UI_SELECT;
2897                                                 WM_event_add_notifier(C->wm, C->window, 0, WM_NOTE_WINDOW_REDRAW, 0, NULL);
2898                                         }
2899                                 }
2900                                 break;
2901                 }
2902
2903                 ui_do_button(C, block, but, event);
2904                 handled= 1;
2905         }
2906         else if(data->state == BUTTON_STATE_WAIT_FLASH) {
2907                 switch(event->type) {
2908                         case TIMER: {
2909                                 if(event->customdata == data->flashtimer)
2910                                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2911                         }
2912                 }
2913         }
2914         else if(data->state == BUTTON_STATE_BLOCK_OPEN) {
2915                 switch(event->type) {
2916                         case MOUSEMOVE: {
2917                                 uiBut *bt= ui_but_find_mouse_over(data->region, event->x, event->y, &block);
2918
2919                                 if(bt && bt->activate != data) {
2920                                         data->cancel= 1;
2921                                         if(button_activate_try_exit(C, op, event))
2922                                                 return OPERATOR_CANCELLED|OPERATOR_PASS_THROUGH;
2923                                         else
2924                                                 return OPERATOR_RUNNING_MODAL|OPERATOR_PASS_THROUGH;
2925                                 }
2926                                 break;
2927                         }
2928                 }
2929
2930                 ui_do_button(C, block, but, event);
2931                 handled= 0;
2932         }
2933         else {
2934                 ui_do_button(C, block, but, event);
2935                 handled= 1;
2936         }
2937
2938         if(data->state == BUTTON_STATE_EXIT) {
2939                 if(button_activate_try_exit(C, op, event))
2940                         return OPERATOR_CANCELLED|(handled==0? OPERATOR_PASS_THROUGH: 0);
2941                 else
2942                         return OPERATOR_RUNNING_MODAL|(handled==0? OPERATOR_PASS_THROUGH: 0);
2943         }
2944
2945         if(handled)
2946                 return OPERATOR_RUNNING_MODAL;
2947         else
2948                 return OPERATOR_RUNNING_MODAL|OPERATOR_PASS_THROUGH;
2949 }
2950
2951 static int button_activate_poll(bContext *C)
2952 {
2953         if(C->region==NULL) return 0;
2954         if(C->region->uiblocks.first==NULL) return 0;
2955         return 1;
2956 }
2957
2958 void ED_UI_OT_button_activate(wmOperatorType *ot)
2959 {
2960         /* identifiers */
2961         ot->name= "Button Highlight";
2962         ot->idname= "ED_UI_OT_button_activate";
2963         
2964         /* api callbacks */
2965         ot->invoke= button_activate_invoke;
2966         ot->cancel= button_activate_cancel;
2967         ot->modal= button_activate_modal;
2968         ot->poll= button_activate_poll;
2969 }
2970
2971 /* ******************** menu navigation helpers ************** */
2972
2973 static uiBut *ui_but_prev(uiBut *but)
2974 {
2975         while(but->prev) {
2976                 but= but->prev;
2977                 if(but->type!=LABEL && but->type!=SEPR && but->type!=ROUNDBOX) return but;
2978         }
2979         return NULL;
2980 }
2981
2982 static uiBut *ui_but_next(uiBut *but)
2983 {
2984         while(but->next) {
2985                 but= but->next;
2986                 if(but->type!=LABEL && but->type!=SEPR && but->type!=ROUNDBOX) return but;
2987         }
2988         return NULL;
2989 }
2990
2991 static uiBut *ui_but_first(uiBlock *block)
2992 {
2993         uiBut *but;
2994         
2995         but= block->buttons.first;
2996         while(but) {
2997                 if(but->type!=LABEL && but->type!=SEPR && but->type!=ROUNDBOX) return but;
2998                 but= but->next;
2999         }
3000         return NULL;
3001 }
3002
3003 static uiBut *ui_but_last(uiBlock *block)
3004 {
3005         uiBut *but;
3006         
3007         but= block->buttons.last;
3008         while(but) {
3009                 if(but->type!=LABEL && but->type!=SEPR && but->type!=ROUNDBOX) return but;
3010                 but= but->prev;
3011         }
3012         return NULL;
3013 }
3014
3015 /* ********************** menu navigate operator *****************************
3016  * this operator is invoked when the block/region is opened and cancelled when
3017  * it closes, it blocks nearly all events. this operator will not close the
3018  * actual block, instead this must be done by the button activate operator when
3019  * it receives the result message */
3020
3021 typedef struct uiBlockHandle {
3022         ARegion *region;
3023