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