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