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