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