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