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