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