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