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