Merge from 2.5 rev 20515 through 20647.
[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);
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 MOUSEMOVE:
1249                         if(data->searchbox)
1250                                 ui_searchbox_event(data->searchbox, event);
1251                         
1252                         break;
1253                 case RIGHTMOUSE:
1254                 case ESCKEY:
1255                         data->cancel= 1;
1256                         data->escapecancel= 1;
1257                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1258                         retval= WM_UI_HANDLER_BREAK;
1259                         break;
1260                 case LEFTMOUSE: {
1261                         
1262                         /* exit on LMB only on RELEASE for searchbox, to mimic other popups, and allow multiple menu levels */
1263                         if(data->searchbox)
1264                                 inbox= BLI_in_rcti(&data->searchbox->winrct, event->x, event->y);
1265
1266                         if(event->val==KM_PRESS) {
1267                                 mx= event->x;
1268                                 my= event->y;
1269                                 ui_window_to_block(data->region, block, &mx, &my);
1270
1271                                 if ((but->y1 <= my) && (my <= but->y2) && (but->x1 <= mx) && (mx <= but->x2)) {
1272                                         ui_textedit_set_cursor_pos(but, data, mx);
1273                                         but->selsta = but->selend = but->pos;
1274                                         data->selstartx= mx;
1275
1276                                         button_activate_state(C, but, BUTTON_STATE_TEXT_SELECTING);
1277                                         retval= WM_UI_HANDLER_BREAK;
1278                                 }
1279                                 else if(inbox==0) {
1280                                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1281                                         retval= WM_UI_HANDLER_BREAK;
1282                                 }
1283                         }
1284                         else if(inbox) {
1285                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
1286                                 retval= WM_UI_HANDLER_BREAK;
1287                         }
1288                         break;
1289                 }
1290         }
1291
1292         if(event->val==KM_PRESS) {
1293                 switch (event->type) {
1294                         case VKEY:
1295                         case XKEY:
1296                         case CKEY:
1297                                 if(event->ctrl || event->oskey) {
1298                                         if(event->type == VKEY)
1299                                                 changed= ui_textedit_copypaste(but, data, 1, 0, 0);
1300                                         else if(event->type == CKEY)
1301                                                 changed= ui_textedit_copypaste(but, data, 0, 1, 0);
1302                                         else if(event->type == XKEY)
1303                                                 changed= ui_textedit_copypaste(but, data, 0, 0, 1);
1304
1305                                         retval= WM_UI_HANDLER_BREAK;
1306                                 }
1307                                 break;
1308                         case RIGHTARROWKEY:
1309                                 ui_textedit_move(but, data, 1, event->shift, event->ctrl);
1310                                 retval= WM_UI_HANDLER_BREAK;
1311                                 break;
1312                         case LEFTARROWKEY:
1313                                 ui_textedit_move(but, data, 0, event->shift, event->ctrl);
1314                                 retval= WM_UI_HANDLER_BREAK;
1315                                 break;
1316                         case DOWNARROWKEY:
1317                                 if(data->searchbox) {
1318                                         ui_searchbox_event(data->searchbox, event);
1319                                         break;
1320                                 }
1321                                 /* pass on purposedly */
1322                         case ENDKEY:
1323                                 ui_textedit_move_end(but, data, 1, event->shift);
1324                                 retval= WM_UI_HANDLER_BREAK;
1325                                 break;
1326                         case UPARROWKEY:
1327                                 if(data->searchbox) {
1328                                         ui_searchbox_event(data->searchbox, event);
1329                                         break;
1330                                 }
1331                                 /* pass on purposedly */
1332                         case HOMEKEY:
1333                                 ui_textedit_move_end(but, data, 0, event->shift);
1334                                 retval= WM_UI_HANDLER_BREAK;
1335                                 break;
1336                         case PADENTER:
1337                         case RETKEY:
1338                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
1339                                 retval= WM_UI_HANDLER_BREAK;
1340                                 break;
1341                         case DELKEY:
1342                                 changed= ui_textedit_delete(but, data, 1, 0);
1343                                 retval= WM_UI_HANDLER_BREAK;
1344                                 break;
1345
1346                         case BACKSPACEKEY:
1347                                 changed= ui_textedit_delete(but, data, 0, event->shift);
1348                                 retval= WM_UI_HANDLER_BREAK;
1349                                 break;
1350                                 
1351                         case TABKEY:
1352                                 /* there is a key conflict here, we can't tab with autocomplete */
1353                                 if(but->autocomplete_func) {
1354                                         changed= ui_textedit_autocomplete(C, but, data);
1355                                         retval= WM_UI_HANDLER_BREAK;
1356                                 }
1357                                 /* the hotkey here is not well defined, was G.qual so we check all */
1358                                 else if(event->shift || event->ctrl || event->alt || event->oskey) {
1359                                         ui_textedit_prev_but(block, but, data);
1360                                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1361                                 }
1362                                 else {
1363                                         ui_textedit_next_but(block, but, data);
1364                                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1365                                 }
1366                                 retval= WM_UI_HANDLER_BREAK;
1367                                 break;
1368                 }
1369
1370                 if(event->ascii && (retval == WM_UI_HANDLER_CONTINUE)) {
1371                         changed= ui_textedit_type_ascii(but, data, event->ascii);
1372                         retval= WM_UI_HANDLER_BREAK;
1373                 }
1374         }
1375
1376         if(changed) {
1377                 if(data->interactive) ui_apply_button(C, block, but, data, 1);
1378                 else ui_check_but(but);
1379                 
1380                 if(data->searchbox)
1381                         ui_searchbox_update(C, data->searchbox, but);
1382         }
1383
1384         if(changed || (retval == WM_UI_HANDLER_BREAK))
1385                 ED_region_tag_redraw(data->region);
1386 }
1387
1388 static void ui_do_but_textedit_select(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
1389 {
1390         int mx, my, retval= WM_UI_HANDLER_CONTINUE;
1391
1392         switch(event->type) {
1393                 case MOUSEMOVE: {
1394                         mx= event->x;
1395                         my= event->y;
1396                         ui_window_to_block(data->region, block, &mx, &my);
1397
1398                         ui_textedit_set_cursor_select(but, data, mx);
1399                         retval= WM_UI_HANDLER_BREAK;
1400                         break;
1401                 }
1402                 case LEFTMOUSE:
1403                         if(event->val == 0)
1404                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
1405                         retval= WM_UI_HANDLER_BREAK;
1406                         break;
1407         }
1408
1409         if(retval == WM_UI_HANDLER_BREAK) {
1410                 ui_check_but(but);
1411                 ED_region_tag_redraw(data->region);
1412         }
1413 }
1414
1415 /* ************* number editing for various types ************* */
1416
1417 static void ui_numedit_begin(uiBut *but, uiHandleButtonData *data)
1418 {
1419         float softrange, softmin, softmax;
1420
1421         if(but->type == BUT_CURVE) {
1422                 data->cumap= (CurveMapping*)but->poin;
1423                 but->editcumap= data->coba;
1424         }
1425         else if(but->type == BUT_COLORBAND) {
1426                 data->coba= (ColorBand*)but->poin;
1427                 but->editcoba= data->coba;
1428         }
1429         else if(ELEM(but->type, BUT_NORMAL, HSVCUBE)) {
1430                 ui_get_but_vectorf(but, data->origvec);
1431                 VECCOPY(data->vec, data->origvec);
1432                 but->editvec= data->vec;
1433         }
1434         else {
1435                 data->startvalue= ui_get_but_val(but);
1436                 data->origvalue= data->startvalue;
1437                 data->value= data->origvalue;
1438                 but->editval= &data->value;
1439
1440                 softmin= but->softmin;
1441                 softmax= but->softmax;
1442                 softrange= softmax - softmin;
1443
1444                 data->dragfstart= (softrange == 0.0)? 0.0: (data->value - softmin)/softrange;
1445                 data->dragf= data->dragfstart;
1446         }
1447
1448         data->dragchange= 0;
1449         data->draglock= 1;
1450 }
1451
1452 static void ui_numedit_end(uiBut *but, uiHandleButtonData *data)
1453 {
1454         but->editval= NULL;
1455         but->editvec= NULL;
1456         but->editcoba= NULL;
1457         but->editcumap= NULL;
1458
1459         data->dragstartx= 0;
1460         data->draglastx= 0;
1461         data->dragchange= 0;
1462         data->dragcbd= NULL;
1463         data->dragsel= 0;
1464 }
1465
1466 static void ui_numedit_apply(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data)
1467 {
1468         if(data->interactive) ui_apply_button(C, block, but, data, 1);
1469         else ui_check_but(but);
1470
1471         ED_region_tag_redraw(data->region);
1472 }
1473
1474 /* ****************** menu opening for various types **************** */
1475
1476 static void ui_blockopen_begin(bContext *C, uiBut *but, uiHandleButtonData *data)
1477 {
1478         uiBlockCreateFunc func= NULL;
1479         uiBlockHandleCreateFunc handlefunc= NULL;
1480         uiMenuCreateFunc menufunc= NULL;
1481         void *arg= NULL;
1482
1483         switch(but->type) {
1484                 case BLOCK:
1485                 case PULLDOWN:
1486                         if(but->menu_create_func) {
1487                                 menufunc= but->menu_create_func;
1488                                 arg= but->poin;
1489                         }
1490                         else {
1491                                 func= but->block_create_func;
1492                                 arg= but->poin;
1493                         }
1494                         break;
1495                 case MENU:
1496                         if(but->menu_create_func) {
1497                                 menufunc= but->menu_create_func;
1498                                 arg= but->poin;
1499                         }
1500                         else {
1501                                 data->origvalue= ui_get_but_val(but);
1502                                 data->value= data->origvalue;
1503                                 but->editval= &data->value;
1504
1505                                 handlefunc= ui_block_func_MENU;
1506                                 arg= but;
1507                         }
1508                         break;
1509                 case ICONROW:
1510                         handlefunc= ui_block_func_ICONROW;
1511                         arg= but;
1512                         break;
1513                 case ICONTEXTROW:
1514                         handlefunc= ui_block_func_ICONTEXTROW;
1515                         arg= but;
1516                         break;
1517                 case COL:
1518                         ui_get_but_vectorf(but, data->origvec);
1519                         VECCOPY(data->vec, data->origvec);
1520                         but->editvec= data->vec;
1521
1522                         handlefunc= ui_block_func_COL;
1523                         arg= but;
1524                         break;
1525         }
1526
1527         if(func || handlefunc) {
1528                 data->menu= ui_popup_block_create(C, data->region, but, func, handlefunc, arg);
1529                 if(but->block->handle)
1530                         data->menu->popup= but->block->handle->popup;
1531         }
1532         else if(menufunc) {
1533                 data->menu= ui_popup_menu_create(C, data->region, but, menufunc, arg);
1534                 if(but->block->handle)
1535                         data->menu->popup= but->block->handle->popup;
1536         }
1537
1538         /* this makes adjacent blocks auto open from now on */
1539         //if(but->block->auto_open==0) but->block->auto_open= 1;
1540 }
1541
1542 static void ui_blockopen_end(bContext *C, uiBut *but, uiHandleButtonData *data)
1543 {
1544         if(but) {
1545                 but->editval= NULL;
1546                 but->editvec= NULL;
1547
1548                 but->block->auto_open_last= PIL_check_seconds_timer();
1549         }
1550
1551         if(data->menu) {
1552                 ui_popup_block_free(C, data->menu);
1553                 data->menu= NULL;
1554         }
1555 }
1556
1557 /* ***************** events for different button types *************** */
1558
1559 static int ui_do_but_BUT(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
1560 {
1561         if(data->state == BUTTON_STATE_HIGHLIGHT) {
1562                 if(event->type == LEFTMOUSE && event->val==KM_PRESS) {
1563                         button_activate_state(C, but, BUTTON_STATE_WAIT_RELEASE);
1564                         return WM_UI_HANDLER_BREAK;
1565                 }
1566                 else if(event->type == LEFTMOUSE && but->block->handle) {
1567                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1568                         return WM_UI_HANDLER_BREAK;
1569                 }
1570                 else if(ELEM(event->type, PADENTER, RETKEY) && event->val==KM_PRESS) {
1571                         button_activate_state(C, but, BUTTON_STATE_WAIT_FLASH);
1572                         return WM_UI_HANDLER_BREAK;
1573                 }
1574         }
1575         else if(data->state == BUTTON_STATE_WAIT_RELEASE) {
1576                 if(event->type == LEFTMOUSE && event->val!=KM_PRESS) {
1577                         if(!(but->flag & UI_SELECT))
1578                                 data->cancel= 1;
1579                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1580                         return WM_UI_HANDLER_BREAK;
1581                 }
1582         }
1583
1584         return WM_UI_HANDLER_CONTINUE;
1585 }
1586
1587 static int ui_do_but_KEYEVT(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
1588 {
1589         if(data->state == BUTTON_STATE_HIGHLIGHT) {
1590                 if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val==KM_PRESS) {
1591                         button_activate_state(C, but, BUTTON_STATE_WAIT_KEY_EVENT);
1592                         return WM_UI_HANDLER_BREAK;
1593                 }
1594         }
1595         else if(data->state == BUTTON_STATE_WAIT_KEY_EVENT) {
1596                 if(event->type == MOUSEMOVE)
1597                         return WM_UI_HANDLER_CONTINUE;
1598
1599                 if(event->val==KM_PRESS) {
1600                         if(WM_key_event_string(event->type)[0])
1601                                 ui_set_but_val(but, event->type);
1602                         else
1603                                 data->cancel= 1;
1604
1605                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1606                 }
1607         }
1608
1609         return WM_UI_HANDLER_CONTINUE;
1610 }
1611
1612 static int ui_do_but_TEX(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
1613 {
1614         if(data->state == BUTTON_STATE_HIGHLIGHT) {
1615                 if(ELEM4(event->type, LEFTMOUSE, PADENTER, RETKEY, EVT_BUT_OPEN) && event->val==KM_PRESS) {
1616                         button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
1617                         return WM_UI_HANDLER_BREAK;
1618                 }
1619         }
1620         else if(data->state == BUTTON_STATE_TEXT_EDITING) {
1621                 ui_do_but_textedit(C, block, but, data, event);
1622                 return WM_UI_HANDLER_BREAK;
1623         }
1624         else if(data->state == BUTTON_STATE_TEXT_SELECTING) {
1625                 ui_do_but_textedit_select(C, block, but, data, event);
1626                 return WM_UI_HANDLER_BREAK;
1627         }
1628
1629         return WM_UI_HANDLER_CONTINUE;
1630 }
1631
1632 static int ui_do_but_TOG(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
1633 {
1634         if(data->state == BUTTON_STATE_HIGHLIGHT) {
1635                 if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val==KM_PRESS) {
1636                         data->togdual= event->ctrl;
1637                         data->togonly= !event->shift;
1638                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1639                         return WM_UI_HANDLER_BREAK;
1640                 }
1641         }
1642         return WM_UI_HANDLER_CONTINUE;
1643 }
1644
1645 static int ui_do_but_EXIT(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
1646 {
1647         if(data->state == BUTTON_STATE_HIGHLIGHT) {
1648                 if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val==KM_PRESS) {
1649                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1650                         return WM_UI_HANDLER_BREAK;
1651                 }
1652         }
1653
1654         return WM_UI_HANDLER_CONTINUE;
1655 }
1656
1657 static int ui_numedit_but_NUM(uiBut *but, uiHandleButtonData *data, float fac, int snap, int mx)
1658 {
1659         float deler, tempf, softmin, softmax, softrange;
1660         int lvalue, temp, changed= 0;
1661         
1662         if(mx == data->draglastx)
1663                 return changed;
1664         
1665         /* drag-lock - prevent unwanted scroll adjustments */
1666         /* change value (now 3) to adjust threshold in pixels */
1667         if(data->draglock) {
1668                 if(abs(mx-data->dragstartx) <= 3)
1669                         return changed;
1670
1671                 data->draglock= 0;
1672                 data->dragstartx= mx;  /* ignore mouse movement within drag-lock */
1673         }
1674
1675         softmin= but->softmin;
1676         softmax= but->softmax;
1677         softrange= softmax - softmin;
1678
1679         deler= 500;
1680         if(!ui_is_but_float(but)) {
1681                 if((softrange)<100) deler= 200.0;
1682                 if((softrange)<25) deler= 50.0;
1683         }
1684         deler /= fac;
1685
1686         if(ui_is_but_float(but) && softrange > 11) {
1687                 /* non linear change in mouse input- good for high precicsion */
1688                 data->dragf+= (((float)(mx-data->draglastx))/deler) * (fabs(data->dragstartx-mx)*0.002);
1689         } else if (!ui_is_but_float(but) && softrange > 129) { /* only scale large int buttons */
1690                 /* non linear change in mouse input- good for high precicsionm ints need less fine tuning */
1691                 data->dragf+= (((float)(mx-data->draglastx))/deler) * (fabs(data->dragstartx-mx)*0.004);
1692         } else {
1693                 /*no scaling */
1694                 data->dragf+= ((float)(mx-data->draglastx))/deler ;
1695         }
1696
1697         if(data->dragf>1.0) data->dragf= 1.0;
1698         if(data->dragf<0.0) data->dragf= 0.0;
1699         data->draglastx= mx;
1700         tempf= (softmin + data->dragf*softrange);
1701         
1702         if(!ui_is_but_float(but)) {
1703                 temp= floor(tempf+.5);
1704                 
1705                 if(tempf==softmin || tempf==softmax);
1706                 else if(snap) {
1707                         if(snap == 2) temp= 100*(temp/100);
1708                         else temp= 10*(temp/10);
1709                 }
1710
1711                 CLAMP(temp, softmin, softmax);
1712                 lvalue= (int)data->value;
1713                         
1714                 if(temp != lvalue) {
1715                         data->dragchange= 1;
1716                         data->value= (double)temp;
1717                         changed= 1;
1718                 }
1719         }
1720         else {
1721                 temp= 0;
1722
1723                 if(snap) {
1724                         if(snap == 2) {
1725                                 if(tempf==softmin || tempf==softmax);
1726                                 else if(softrange < 2.10) tempf= 0.01*floor(100.0*tempf);
1727                                 else if(softrange < 21.0) tempf= 0.1*floor(10.0*tempf);
1728                                 else tempf= floor(tempf);
1729                         }
1730                         else {
1731                                 if(tempf==softmin || tempf==softmax);
1732                                 else if(softrange < 2.10) tempf= 0.1*floor(10*tempf);
1733                                 else if(softrange < 21.0) tempf= floor(tempf);
1734                                 else tempf= 10.0*floor(tempf/10.0);
1735                         }
1736                 }
1737
1738                 CLAMP(tempf, softmin, softmax);
1739
1740                 if(tempf != data->value) {
1741                         data->dragchange= 1;
1742                         data->value= tempf;
1743                         changed= 1;
1744                 }
1745         }
1746
1747         return changed;
1748 }
1749
1750 static int ui_do_but_NUM(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
1751 {
1752         int mx, my, click= 0;
1753         int retval= WM_UI_HANDLER_CONTINUE;
1754         
1755         mx= event->x;
1756         my= event->y;
1757         ui_window_to_block(data->region, block, &mx, &my);
1758
1759         if(data->state == BUTTON_STATE_HIGHLIGHT) {
1760                 if(event->val==KM_PRESS) {
1761                         if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->shift) {
1762                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
1763                                 retval= WM_UI_HANDLER_BREAK;
1764                         }
1765                         else if(event->type == LEFTMOUSE) {
1766                                 data->dragstartx= mx;
1767                                 data->draglastx= mx;
1768                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
1769                                 retval= WM_UI_HANDLER_BREAK;
1770                         }
1771                         else if(ELEM(event->type, PADENTER, RETKEY) && event->val==KM_PRESS)
1772                                 click= 1;
1773                 }
1774         }
1775         else if(data->state == BUTTON_STATE_NUM_EDITING) {
1776                 if(event->type == ESCKEY) {
1777                         data->cancel= 1;
1778                         data->escapecancel= 1;
1779                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1780                 }
1781                 else if(event->type == LEFTMOUSE && event->val!=KM_PRESS) {
1782                         if(data->dragchange)
1783                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
1784                         else
1785                                 click= 1;
1786                 }
1787                 else if(event->type == MOUSEMOVE) {
1788                         float fac;
1789                         int snap;
1790
1791                         fac= 1.0f;
1792                         if(event->shift) fac /= 10.0f;
1793                         if(event->alt) fac /= 20.0f;
1794
1795                         if(event->custom == EVT_DATA_TABLET) {
1796                                 wmTabletData *wmtab= event->customdata;
1797
1798                                 /* de-sensitise based on tablet pressure */
1799                                 if (ELEM(wmtab->Active, DEV_STYLUS, DEV_ERASER))
1800                                         fac *= wmtab->Pressure;
1801                         }
1802                         
1803                         snap= (event->ctrl)? (event->shift)? 2: 1: 0;
1804
1805                         if(ui_numedit_but_NUM(but, data, fac, snap, mx))
1806                                 ui_numedit_apply(C, block, but, data);
1807                 }
1808                 retval= WM_UI_HANDLER_BREAK;
1809         }
1810         else if(data->state == BUTTON_STATE_TEXT_EDITING) {
1811                 ui_do_but_textedit(C, block, but, data, event);
1812                 retval= WM_UI_HANDLER_BREAK;
1813         }
1814         else if(data->state == BUTTON_STATE_TEXT_SELECTING) {
1815                 ui_do_but_textedit_select(C, block, but, data, event);
1816                 retval= WM_UI_HANDLER_BREAK;
1817         }
1818         
1819         if(click) {
1820                 /* we can click on the side arrows to increment/decrement,
1821                  * or click inside to edit the value directly */
1822                 float tempf, softmin, softmax;
1823                 int temp;
1824
1825                 softmin= but->softmin;
1826                 softmax= but->softmax;
1827
1828                 if(!ui_is_but_float(but)) {
1829                         if(mx < (but->x1 + (but->x2 - but->x1)/3 - 3)) {
1830                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
1831
1832                                 temp= (int)data->value - 1;
1833                                 if(temp>=softmin && temp<=softmax)
1834                                         data->value= (double)temp;
1835                                 else
1836                                         data->cancel= 1;
1837
1838                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
1839                         }
1840                         else if(mx > (but->x1 + (2*(but->x2 - but->x1)/3) + 3)) {
1841                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
1842
1843                                 temp= (int)data->value + 1;
1844                                 if(temp>=softmin && temp<=softmax)
1845                                         data->value= (double)temp;
1846                                 else
1847                                         data->cancel= 1;
1848
1849                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
1850                         }
1851                         else
1852                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
1853                 }
1854                 else {
1855                         if(mx < (but->x1 + (but->x2 - but->x1)/3 - 3)) {
1856                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
1857
1858                                 tempf= data->value - 0.01*but->a1;
1859                                 if (tempf < softmin) tempf = softmin;
1860                                 data->value= tempf;
1861
1862                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
1863                         }
1864                         else if(mx > but->x1 + (2*((but->x2 - but->x1)/3) + 3)) {
1865                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
1866
1867                                 tempf= data->value + 0.01*but->a1;
1868                                 if (tempf > softmax) tempf = softmax;
1869                                 data->value= tempf;
1870
1871                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
1872                         }
1873                         else
1874                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
1875                 }
1876
1877                 retval= WM_UI_HANDLER_BREAK;
1878         }
1879         
1880         return retval;
1881 }
1882
1883 static int ui_numedit_but_SLI(uiBut *but, uiHandleButtonData *data, int shift, int ctrl, int mx)
1884 {
1885         float deler, f, tempf, softmin, softmax, softrange;
1886         int temp, lvalue, changed= 0;
1887
1888         softmin= but->softmin;
1889         softmax= but->softmax;
1890         softrange= softmax - softmin;
1891
1892         if(but->type==NUMSLI) deler= ((but->x2-but->x1) - 5.0*but->aspect);
1893         else if(but->type==HSVSLI) deler= ((but->x2-but->x1)/2 - 5.0*but->aspect);
1894         else deler= (but->x2-but->x1- 5.0*but->aspect);
1895
1896         f= (float)(mx-data->dragstartx)/deler + data->dragfstart;
1897         
1898         if(shift)
1899                 f= (f-data->dragfstart)/10.0 + data->dragfstart;
1900
1901         CLAMP(f, 0.0, 1.0);
1902         tempf= softmin + f*softrange;
1903         temp= floor(tempf+.5);
1904
1905         if(ctrl) {
1906                 if(tempf==softmin || tempf==softmax);
1907                 else if(ui_is_but_float(but)) {
1908
1909                         if(shift) {
1910                                 if(tempf==softmin || tempf==softmax);
1911                                 else if(softmax-softmin < 2.10) tempf= 0.01*floor(100.0*tempf);
1912                                 else if(softmax-softmin < 21.0) tempf= 0.1*floor(10.0*tempf);
1913                                 else tempf= floor(tempf);
1914                         }
1915                         else {
1916                                 if(softmax-softmin < 2.10) tempf= 0.1*floor(10*tempf);
1917                                 else if(softmax-softmin < 21.0) tempf= floor(tempf);
1918                                 else tempf= 10.0*floor(tempf/10.0);
1919                         }
1920                 }
1921                 else {
1922                         temp= 10*(temp/10);
1923                         tempf= temp;
1924                 }
1925         }
1926
1927         if(!ui_is_but_float(but)) {
1928                 lvalue= floor(data->value+0.5);
1929
1930                 CLAMP(temp, softmin, softmax);
1931
1932                 if(temp != lvalue) {
1933                         data->value= temp;
1934                         data->dragchange= 1;
1935                         changed= 1;
1936                 }
1937         }
1938         else {
1939                 CLAMP(tempf, softmin, softmax);
1940
1941                 if(tempf != data->value) {
1942                         data->value= tempf;
1943                         data->dragchange= 1;
1944                         changed= 1;
1945                 }
1946         }
1947
1948         return changed;
1949 }
1950
1951 static int ui_do_but_SLI(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
1952 {
1953         int mx, my, click= 0;
1954         int retval= WM_UI_HANDLER_CONTINUE;
1955
1956         mx= event->x;
1957         my= event->y;
1958         ui_window_to_block(data->region, block, &mx, &my);
1959
1960         if(data->state == BUTTON_STATE_HIGHLIGHT) {
1961                 if(event->val==KM_PRESS) {
1962                         if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->shift) {
1963                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
1964                                 retval= WM_UI_HANDLER_BREAK;
1965                         }
1966                         else if(event->type == LEFTMOUSE) {
1967                                 data->dragstartx= mx;
1968                                 data->draglastx= mx;
1969                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
1970                                 retval= WM_UI_HANDLER_BREAK;
1971                         }
1972                         else if(ELEM(event->type, PADENTER, RETKEY) && event->val==KM_PRESS)
1973                                 click= 1;
1974                 }
1975         }
1976         else if(data->state == BUTTON_STATE_NUM_EDITING) {
1977                 if(event->type == ESCKEY) {
1978                         data->cancel= 1;
1979                         data->escapecancel= 1;
1980                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1981                 }
1982                 else if(event->type == LEFTMOUSE && event->val!=KM_PRESS) {
1983                         if(data->dragchange)
1984                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
1985                         else
1986                                 click= 1;
1987                 }
1988                 else if(event->type == MOUSEMOVE) {
1989                         if(ui_numedit_but_SLI(but, data, event->shift, event->ctrl, mx))
1990                                 ui_numedit_apply(C, block, but, data);
1991                 }
1992                 retval= WM_UI_HANDLER_BREAK;
1993         }
1994         else if(data->state == BUTTON_STATE_TEXT_EDITING) {
1995                 ui_do_but_textedit(C, block, but, data, event);
1996                 retval= WM_UI_HANDLER_BREAK;
1997         }
1998         else if(data->state == BUTTON_STATE_TEXT_SELECTING) {
1999                 ui_do_but_textedit_select(C, block, but, data, event);
2000                 retval= WM_UI_HANDLER_BREAK;
2001         }
2002
2003         if(click) {
2004                 if (event->ctrl) {
2005                         /* nudge slider to the left or right */
2006                         float f, tempf, softmin, softmax, softrange;
2007                         int temp;
2008                         
2009                         button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2010                         
2011                         softmin= but->softmin;
2012                         softmax= but->softmax;
2013                         softrange= softmax - softmin;
2014                         
2015                         tempf= data->value;
2016                         temp= (int)data->value;
2017                         
2018                         if(but->type==SLI) f= (float)(mx-but->x1)/(but->x2-but->x1);
2019                         else f= (float)(mx- but->x1)/(but->x2-but->x1);
2020                         
2021                         f= softmin + f*softrange;
2022                         
2023                         if(!ui_is_but_float(but)) {
2024                                 if(f<temp) temp--;
2025                                 else temp++;
2026                                 
2027                                 if(temp>=softmin && temp<=softmax)
2028                                         data->value= temp;
2029                                 else
2030                                         data->cancel= 1;
2031                         } 
2032                         else {
2033                                 if(f<tempf) tempf-=.01;
2034                                 else tempf+=.01;
2035                                 
2036                                 if(tempf>=softmin && tempf<=softmax)
2037                                         data->value= tempf;
2038                                 else
2039                                         data->cancel= 1;
2040                         }
2041                         
2042                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2043                         retval= WM_UI_HANDLER_BREAK;
2044                 }
2045                 else {
2046                         /* edit the value directly */
2047                         button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2048                         retval= WM_UI_HANDLER_BREAK;
2049                 }
2050         }
2051         
2052         return retval;
2053 }
2054
2055 static int ui_do_but_BLOCK(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2056 {
2057         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2058                 if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val==KM_PRESS) {
2059                         button_activate_state(C, but, BUTTON_STATE_MENU_OPEN);
2060                         return WM_UI_HANDLER_BREAK;
2061                 }
2062         }
2063
2064         return WM_UI_HANDLER_CONTINUE;
2065 }
2066
2067 static int ui_numedit_but_NORMAL(uiBut *but, uiHandleButtonData *data, int mx, int my)
2068 {
2069         float dx, dy, rad, radsq, mrad, *fp;
2070         int mdx, mdy, changed= 1;
2071         
2072         /* button is presumed square */
2073         /* if mouse moves outside of sphere, it does negative normal */
2074
2075         fp= data->origvec;
2076         rad= (but->x2 - but->x1);
2077         radsq= rad*rad;
2078         
2079         if(fp[2]>0.0f) {
2080                 mdx= (rad*fp[0]);
2081                 mdy= (rad*fp[1]);
2082         }
2083         else if(fp[2]> -1.0f) {
2084                 mrad= rad/sqrt(fp[0]*fp[0] + fp[1]*fp[1]);
2085                 
2086                 mdx= 2.0f*mrad*fp[0] - (rad*fp[0]);
2087                 mdy= 2.0f*mrad*fp[1] - (rad*fp[1]);
2088         }
2089         else mdx= mdy= 0;
2090         
2091         dx= (float)(mx+mdx-data->dragstartx);
2092         dy= (float)(my+mdy-data->dragstarty);
2093
2094         fp= data->vec;
2095         mrad= dx*dx+dy*dy;
2096         if(mrad < radsq) {      /* inner circle */
2097                 fp[0]= dx;
2098                 fp[1]= dy;
2099                 fp[2]= sqrt( radsq-dx*dx-dy*dy );
2100         }
2101         else {  /* outer circle */
2102                 
2103                 mrad= rad/sqrt(mrad);   // veclen
2104                 
2105                 dx*= (2.0f*mrad - 1.0f);
2106                 dy*= (2.0f*mrad - 1.0f);
2107                 
2108                 mrad= dx*dx+dy*dy;
2109                 if(mrad < radsq) {
2110                         fp[0]= dx;
2111                         fp[1]= dy;
2112                         fp[2]= -sqrt( radsq-dx*dx-dy*dy );
2113                 }
2114         }
2115         Normalize(fp);
2116
2117         data->draglastx= mx;
2118         data->draglasty= my;
2119
2120         return changed;
2121 }
2122
2123 static int ui_do_but_NORMAL(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2124 {
2125         int mx, my;
2126
2127         mx= event->x;
2128         my= event->y;
2129         ui_window_to_block(data->region, block, &mx, &my);
2130
2131         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2132                 if(event->type==LEFTMOUSE && event->val==KM_PRESS) {
2133                         data->dragstartx= mx;
2134                         data->dragstarty= my;
2135                         data->draglastx= mx;
2136                         data->draglasty= my;
2137                         button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2138
2139                         /* also do drag the first time */
2140                         if(ui_numedit_but_NORMAL(but, data, mx, my))
2141                                 ui_numedit_apply(C, block, but, data);
2142                         
2143                         return WM_UI_HANDLER_BREAK;
2144                 }
2145         }
2146         else if(data->state == BUTTON_STATE_NUM_EDITING) {
2147                 if(event->type == MOUSEMOVE) {
2148                         if(mx!=data->draglastx || my!=data->draglasty) {
2149                                 if(ui_numedit_but_NORMAL(but, data, mx, my))
2150                                         ui_numedit_apply(C, block, but, data);
2151                         }
2152                 }
2153                 else if(event->type==LEFTMOUSE && event->val!=KM_PRESS)
2154                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2155
2156                 return WM_UI_HANDLER_BREAK;
2157         }
2158         
2159         return WM_UI_HANDLER_CONTINUE;
2160 }
2161
2162 static int ui_numedit_but_HSVCUBE(uiBut *but, uiHandleButtonData *data, int mx, int my)
2163 {
2164         float x, y;
2165         int changed= 1;
2166
2167         /* relative position within box */
2168         x= ((float)mx-but->x1)/(but->x2-but->x1);
2169         y= ((float)my-but->y1)/(but->y2-but->y1);
2170         CLAMP(x, 0.0, 1.0);
2171         CLAMP(y, 0.0, 1.0);
2172         
2173         if(but->a1==0) {
2174                 but->hsv[0]= x; 
2175                 but->hsv[2]= y; 
2176         }
2177         else if(but->a1==1) {
2178                 but->hsv[0]= x;                                 
2179                 but->hsv[1]= y;                                 
2180         }
2181         else if(but->a1==2) {
2182                 but->hsv[2]= x; 
2183                 but->hsv[1]= y; 
2184         }
2185         else
2186                 but->hsv[0]= x; 
2187
2188         ui_set_but_hsv(but);    // converts to rgb
2189         
2190         // update button values and strings
2191         ui_update_block_buts_hsv(but->block, but->hsv);
2192
2193         data->draglastx= mx;
2194         data->draglasty= my;
2195
2196         return changed;
2197 }
2198
2199 static int ui_do_but_HSVCUBE(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2200 {
2201         int mx, my;
2202
2203         mx= event->x;
2204         my= event->y;
2205         ui_window_to_block(data->region, block, &mx, &my);
2206
2207         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2208                 if(event->type==LEFTMOUSE && event->val==KM_PRESS) {
2209                         data->dragstartx= mx;
2210                         data->dragstarty= my;
2211                         data->draglastx= mx;
2212                         data->draglasty= my;
2213                         button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2214
2215                         /* also do drag the first time */
2216                         if(ui_numedit_but_HSVCUBE(but, data, mx, my))
2217                                 ui_numedit_apply(C, block, but, data);
2218                         
2219                         return WM_UI_HANDLER_BREAK;
2220                 }
2221         }
2222         else if(data->state == BUTTON_STATE_NUM_EDITING) {
2223                 if(event->type == MOUSEMOVE) {
2224                         if(mx!=data->draglastx || my!=data->draglasty) {
2225                                 if(ui_numedit_but_HSVCUBE(but, data, mx, my))
2226                                         ui_numedit_apply(C, block, but, data);
2227                         }
2228                 }
2229                 else if(event->type==LEFTMOUSE && event->val!=KM_PRESS)
2230                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2231                 
2232                 return WM_UI_HANDLER_BREAK;
2233         }
2234
2235         return WM_UI_HANDLER_CONTINUE;
2236 }
2237
2238 static int verg_colorband(const void *a1, const void *a2)
2239 {
2240         const CBData *x1=a1, *x2=a2;
2241         
2242         if( x1->pos > x2->pos ) return 1;
2243         else if( x1->pos < x2->pos) return -1;
2244         return WM_UI_HANDLER_CONTINUE;
2245 }
2246
2247 static void ui_colorband_update(ColorBand *coba)
2248 {
2249         int a;
2250         
2251         if(coba->tot<2) return;
2252         
2253         for(a=0; a<coba->tot; a++) coba->data[a].cur= a;
2254                 qsort(coba->data, coba->tot, sizeof(CBData), verg_colorband);
2255         for(a=0; a<coba->tot; a++) {
2256                 if(coba->data[a].cur==coba->cur) {
2257                         coba->cur= a;
2258                         break;
2259                 }
2260         }
2261 }
2262
2263 static int ui_numedit_but_COLORBAND(uiBut *but, uiHandleButtonData *data, int mx)
2264 {
2265         float dx;
2266         int changed= 0;
2267
2268         if(data->draglastx == mx)
2269                 return changed;
2270
2271         dx= ((float)(mx - data->draglastx))/(but->x2-but->x1);
2272         data->dragcbd->pos += dx;
2273         CLAMP(data->dragcbd->pos, 0.0, 1.0);
2274         
2275         ui_colorband_update(data->coba);
2276         data->dragcbd= data->coba->data + data->coba->cur;      /* because qsort */
2277         
2278         data->draglastx= mx;
2279         changed= 1;
2280
2281         return changed;
2282 }
2283
2284 static int ui_do_but_COLORBAND(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2285 {
2286         ColorBand *coba;
2287         CBData *cbd;
2288         int mx, my, a, xco, mindist= 12;
2289
2290         mx= event->x;
2291         my= event->y;
2292         ui_window_to_block(data->region, block, &mx, &my);
2293
2294         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2295                 if(event->type==LEFTMOUSE && event->val==KM_PRESS) {
2296                         coba= (ColorBand*)but->poin;
2297
2298                         if(event->ctrl) {
2299                                 /* insert new key on mouse location */
2300                                 if(coba->tot < MAXCOLORBAND-1) {
2301                                         float pos= ((float)(mx - but->x1))/(but->x2-but->x1);
2302                                         float col[4];
2303                                         
2304                                         do_colorband(coba, pos, col);   /* executes it */
2305                                         
2306                                         coba->tot++;
2307                                         coba->cur= coba->tot-1;
2308                                         
2309                                         coba->data[coba->cur].r= col[0];
2310                                         coba->data[coba->cur].g= col[1];
2311                                         coba->data[coba->cur].b= col[2];
2312                                         coba->data[coba->cur].a= col[3];
2313                                         coba->data[coba->cur].pos= pos;
2314
2315                                         ui_colorband_update(coba);
2316                                 }
2317
2318                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2319                         }
2320                         else {
2321                                 data->dragstartx= mx;
2322                                 data->dragstarty= my;
2323                                 data->draglastx= mx;
2324                                 data->draglasty= my;
2325
2326                                 /* activate new key when mouse is close */
2327                                 for(a=0, cbd= coba->data; a<coba->tot; a++, cbd++) {
2328                                         xco= but->x1 + (cbd->pos*(but->x2-but->x1));
2329                                         xco= ABS(xco-mx);
2330                                         if(a==coba->cur) xco+= 5; // selected one disadvantage
2331                                         if(xco<mindist) {
2332                                                 coba->cur= a;
2333                                                 mindist= xco;
2334                                         }
2335                                 }
2336                 
2337                                 data->dragcbd= coba->data + coba->cur;
2338                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2339                         }
2340
2341                         return WM_UI_HANDLER_BREAK;
2342                 }
2343         }
2344         else if(data->state == BUTTON_STATE_NUM_EDITING) {
2345                 if(event->type == MOUSEMOVE) {
2346                         if(mx!=data->draglastx || my!=data->draglasty) {
2347                                 if(ui_numedit_but_COLORBAND(but, data, mx))
2348                                         ui_numedit_apply(C, block, but, data);
2349                         }
2350                 }
2351                 else if(event->type==LEFTMOUSE && event->val!=KM_PRESS)
2352                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2353                 
2354                 return WM_UI_HANDLER_BREAK;
2355         }
2356
2357         return WM_UI_HANDLER_CONTINUE;
2358 }
2359
2360 static int ui_numedit_but_CURVE(uiBut *but, uiHandleButtonData *data, int snap, int mx, int my)
2361 {
2362         CurveMapping *cumap= data->cumap;
2363         CurveMap *cuma= cumap->cm+cumap->cur;
2364         CurveMapPoint *cmp= cuma->curve;
2365         float fx, fy, zoomx, zoomy, offsx, offsy;
2366         int a, changed= 0;
2367
2368         zoomx= (but->x2-but->x1)/(cumap->curr.xmax-cumap->curr.xmin);
2369         zoomy= (but->y2-but->y1)/(cumap->curr.ymax-cumap->curr.ymin);
2370         offsx= cumap->curr.xmin;
2371         offsy= cumap->curr.ymin;
2372
2373         if(data->dragsel != -1) {
2374                 int moved_point= 0;             /* for ctrl grid, can't use orig coords because of sorting */
2375                 
2376                 fx= (mx-data->draglastx)/zoomx;
2377                 fy= (my-data->draglasty)/zoomy;
2378                 for(a=0; a<cuma->totpoint; a++) {
2379                         if(cmp[a].flag & SELECT) {
2380                                 float origx= cmp[a].x, origy= cmp[a].y;
2381                                 cmp[a].x+= fx;
2382                                 cmp[a].y+= fy;
2383                                 if(snap) {
2384                                         cmp[a].x= 0.125f*floor(0.5f + 8.0f*cmp[a].x);
2385                                         cmp[a].y= 0.125f*floor(0.5f + 8.0f*cmp[a].y);
2386                                 }
2387                                 if(cmp[a].x!=origx || cmp[a].y!=origy)
2388                                         moved_point= 1;
2389                         }
2390                 }
2391
2392                 curvemapping_changed(cumap, 0); /* no remove doubles */
2393                 
2394                 if(moved_point) {
2395                         data->draglastx= mx;
2396                         data->draglasty= my;
2397                         changed= 1;
2398                 }
2399
2400                 data->dragchange= 1; /* mark for selection */
2401         }
2402         else {
2403                 fx= (mx-data->draglastx)/zoomx;
2404                 fy= (my-data->draglasty)/zoomy;
2405                 
2406                 /* clamp for clip */
2407                 if(cumap->flag & CUMA_DO_CLIP) {
2408                         if(cumap->curr.xmin-fx < cumap->clipr.xmin)
2409                                 fx= cumap->curr.xmin - cumap->clipr.xmin;
2410                         else if(cumap->curr.xmax-fx > cumap->clipr.xmax)
2411                                 fx= cumap->curr.xmax - cumap->clipr.xmax;
2412                         if(cumap->curr.ymin-fy < cumap->clipr.ymin)
2413                                 fy= cumap->curr.ymin - cumap->clipr.ymin;
2414                         else if(cumap->curr.ymax-fy > cumap->clipr.ymax)
2415                                 fy= cumap->curr.ymax - cumap->clipr.ymax;
2416                 }                               
2417
2418                 cumap->curr.xmin-=fx;
2419                 cumap->curr.ymin-=fy;
2420                 cumap->curr.xmax-=fx;
2421                 cumap->curr.ymax-=fy;
2422                 
2423                 data->draglastx= mx;
2424                 data->draglasty= my;
2425
2426                 changed= 1;
2427         }
2428
2429         return changed;
2430 }
2431
2432 static int ui_do_but_CURVE(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2433 {
2434         int mx, my, a, changed= 0;
2435
2436         mx= event->x;
2437         my= event->y;
2438         ui_window_to_block(data->region, block, &mx, &my);
2439
2440         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2441                 if(event->type==LEFTMOUSE && event->val==KM_PRESS) {
2442                         CurveMapping *cumap= (CurveMapping*)but->poin;
2443                         CurveMap *cuma= cumap->cm+cumap->cur;
2444                         CurveMapPoint *cmp= cuma->curve;
2445                         float fx, fy, zoomx, zoomy, offsx, offsy;
2446                         float dist, mindist= 200.0f; // 14 pixels radius
2447                         int sel= -1;
2448
2449                         zoomx= (but->x2-but->x1)/(cumap->curr.xmax-cumap->curr.xmin);
2450                         zoomy= (but->y2-but->y1)/(cumap->curr.ymax-cumap->curr.ymin);
2451                         offsx= cumap->curr.xmin;
2452                         offsy= cumap->curr.ymin;
2453
2454                         if(event->ctrl) {
2455                                 fx= ((float)my - but->x1)/zoomx + offsx;
2456                                 fy= ((float)my - but->y1)/zoomy + offsy;
2457                                 
2458                                 curvemap_insert(cuma, fx, fy);
2459                                 curvemapping_changed(cumap, 0);
2460                                 changed= 1;
2461                         }
2462
2463                         /* check for selecting of a point */
2464                         cmp= cuma->curve;       /* ctrl adds point, new malloc */
2465                         for(a=0; a<cuma->totpoint; a++) {
2466                                 fx= but->x1 + zoomx*(cmp[a].x-offsx);
2467                                 fy= but->y1 + zoomy*(cmp[a].y-offsy);
2468                                 dist= (fx-mx)*(fx-mx) + (fy-my)*(fy-my);
2469                                 if(dist < mindist) {
2470                                         sel= a;
2471                                         mindist= dist;
2472                                 }
2473                         }
2474
2475                         if (sel == -1) {
2476                                 /* if the click didn't select anything, check if it's clicked on the 
2477                                  * curve itself, and if so, add a point */
2478                                 fx= ((float)mx - but->x1)/zoomx + offsx;
2479                                 fy= ((float)my - but->y1)/zoomy + offsy;
2480                                 
2481                                 cmp= cuma->table;
2482
2483                                 /* loop through the curve segment table and find what's near the mouse.
2484                                  * 0.05 is kinda arbitrary, but seems to be what works nicely. */
2485                                 for(a=0; a<=CM_TABLE; a++) {                    
2486                                         if ( ( fabs(fx - cmp[a].x) < (0.05) ) && ( fabs(fy - cmp[a].y) < (0.05) ) ) {
2487                                         
2488                                                 curvemap_insert(cuma, fx, fy);
2489                                                 curvemapping_changed(cumap, 0);
2490
2491                                                 changed= 1;
2492                                                 
2493                                                 /* reset cmp back to the curve points again, rather than drawing segments */            
2494                                                 cmp= cuma->curve;
2495                                                 
2496                                                 /* find newly added point and make it 'sel' */
2497                                                 for(a=0; a<cuma->totpoint; a++)
2498                                                         if(cmp[a].x == fx)
2499                                                                 sel = a;
2500                                                         
2501                                                 break;
2502                                         }
2503                                 }
2504                         }
2505
2506                         if(sel!= -1) {
2507                                 /* ok, we move a point */
2508                                 /* deselect all if this one is deselect. except if we hold shift */
2509                                 if(event->shift==0 && (cmp[sel].flag & SELECT)==0)
2510                                         for(a=0; a<cuma->totpoint; a++)
2511                                                 cmp[a].flag &= ~SELECT;
2512                                 cmp[sel].flag |= SELECT;
2513                         }
2514                         else {
2515                                 /* move the view */
2516                                 data->cancel= 1;
2517                         }
2518
2519                         data->dragsel= sel;
2520
2521                         data->dragstartx= mx;
2522                         data->dragstarty= my;
2523                         data->draglastx= mx;
2524                         data->draglasty= my;
2525
2526                         button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2527                         return WM_UI_HANDLER_BREAK;
2528                 }
2529         }
2530         else if(data->state == BUTTON_STATE_NUM_EDITING) {
2531                 if(event->type == MOUSEMOVE) {
2532                         if(mx!=data->draglastx || my!=data->draglasty) {
2533                                 if(ui_numedit_but_CURVE(but, data, event->shift, mx, my))
2534                                         ui_numedit_apply(C, block, but, data);
2535                         }
2536                 }
2537                 else if(event->type==LEFTMOUSE && event->val!=KM_PRESS) {
2538                         if(data->dragsel != -1) {
2539                                 CurveMapping *cumap= data->cumap;
2540                                 CurveMap *cuma= cumap->cm+cumap->cur;
2541                                 CurveMapPoint *cmp= cuma->curve;
2542
2543                                 if(!data->dragchange) {
2544                                         /* deselect all, select one */
2545                                         if(event->shift==0) {
2546                                                 for(a=0; a<cuma->totpoint; a++)
2547                                                         cmp[a].flag &= ~SELECT;
2548                                                 cmp[data->dragsel].flag |= SELECT;
2549                                         }
2550                                 }
2551                                 else
2552                                         curvemapping_changed(cumap, 1); /* remove doubles */
2553                         }
2554
2555                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2556                 }
2557
2558                 return WM_UI_HANDLER_BREAK;
2559         }
2560
2561         return WM_UI_HANDLER_CONTINUE;
2562 }
2563
2564 #ifdef INTERNATIONAL
2565 static int ui_do_but_CHARTAB(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2566 {
2567         /* XXX 2.50 bad global and state access */
2568 #if 0
2569         float sx, sy, ex, ey;
2570         float width, height;
2571         float butw, buth;
2572         int mx, my, x, y, cs, che;
2573
2574         mx= event->x;
2575         my= event->y;
2576         ui_window_to_block(data->region, block, &mx, &my);
2577
2578         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2579                 if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val==KM_PRESS) {
2580                         /* Calculate the size of the button */
2581                         width = abs(but->x2 - but->x1);
2582                         height = abs(but->y2 - but->y1);
2583
2584                         butw = floor(width / 12);
2585                         buth = floor(height / 6);
2586
2587                         /* Initialize variables */
2588                         sx = but->x1;
2589                         ex = but->x1 + butw;
2590                         sy = but->y1 + height - buth;
2591                         ey = but->y1 + height;
2592
2593                         cs = G.charstart;
2594
2595                         /* And the character is */
2596                         x = (int) ((mx / butw) - 0.5);
2597                         y = (int) (6 - ((my / buth) - 0.5));
2598
2599                         che = cs + (y*12) + x;
2600
2601                         if(che > G.charmax)
2602                                 che = 0;
2603
2604                         if(G.obedit)
2605                         {
2606                                 do_textedit(0,0,che);
2607                         }
2608
2609                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2610                         return WM_UI_HANDLER_BREAK;
2611                 }
2612                 else if(ELEM(event->type, WHEELUPMOUSE, PAGEUPKEY)) {
2613                         for(but= block->buttons.first; but; but= but->next) {
2614                                 if(but->type == CHARTAB) {
2615                                         G.charstart = G.charstart - (12*6);
2616                                         if(G.charstart < 0)
2617                                                 G.charstart = 0;
2618                                         if(G.charstart < G.charmin)
2619                                                 G.charstart = G.charmin;
2620                                         ui_draw_but(but);
2621
2622                                         //Really nasty... to update the num button from the same butblock
2623                                         for(bt= block->buttons.first; bt; bt= bt->next)
2624                                         {
2625                                                 if(ELEM(bt->type, NUM, NUMABS)) {
2626                                                         ui_check_but(bt);
2627                                                         ui_draw_but(bt);
2628                                                 }
2629                                         }
2630                                         retval=UI_CONT;
2631                                         break;
2632                                 }
2633                         }
2634
2635                         return WM_UI_HANDLER_BREAK;
2636                 }
2637                 else if(ELEM(event->type, WHEELDOWNMOUSE, PAGEDOWNKEY)) {
2638                         for(but= block->buttons.first; but; but= but->next)
2639                         {
2640                                 if(but->type == CHARTAB)
2641                                 {
2642                                         G.charstart = G.charstart + (12*6);
2643                                         if(G.charstart > (0xffff - 12*6))
2644                                                 G.charstart = 0xffff - (12*6);
2645                                         if(G.charstart > G.charmax - 12*6)
2646                                                 G.charstart = G.charmax - 12*6;
2647                                         ui_draw_but(but);
2648
2649                                         for(bt= block->buttons.first; bt; bt= bt->next)
2650                                         {
2651                                                 if(ELEM(bt->type, NUM, NUMABS)) {
2652                                                         ui_check_but(bt);
2653                                                         ui_draw_but(bt);
2654                                                 }
2655                                         }
2656                                         
2657                                         but->flag |= UI_ACTIVE;
2658                                         retval=UI_RETURN_OK;
2659                                         break;
2660                                 }
2661                         }
2662
2663                         return WM_UI_HANDLER_BREAK;
2664                 }
2665         }
2666 #endif
2667
2668         return WM_UI_HANDLER_CONTINUE;
2669 }
2670 #endif
2671
2672 static int ui_do_button(bContext *C, uiBlock *block, uiBut *but, wmEvent *event)
2673 {
2674         uiHandleButtonData *data;
2675         int retval;
2676
2677         data= but->active;
2678         retval= WM_UI_HANDLER_CONTINUE;
2679
2680         if(but->flag & UI_BUT_DISABLED)
2681                 return WM_UI_HANDLER_BREAK;
2682
2683         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2684                 /* handle copy-paste */
2685                 if(ELEM(event->type, CKEY, VKEY) && event->val==KM_PRESS && (event->ctrl || event->oskey)) {
2686                         ui_but_copy_paste(C, but, data, (event->type == CKEY)? 'c': 'v');
2687                         return WM_UI_HANDLER_BREAK;
2688                 }
2689                 /* handle keyframeing */
2690                 else if(event->type == IKEY && event->val == KM_PRESS) {
2691                         if(event->alt)
2692                                 ui_but_anim_delete_keyframe(C);
2693                         else
2694                                 ui_but_anim_insert_keyframe(C);
2695
2696                         ED_region_tag_redraw(CTX_wm_region(C));
2697
2698                         return WM_UI_HANDLER_BREAK;
2699                 }
2700                 /* handle driver adding */
2701                 else if(event->type == DKEY && event->val == KM_PRESS) {
2702                         if(event->alt)
2703                                 ui_but_anim_remove_driver(C);
2704                         else
2705                                 ui_but_anim_add_driver(C);
2706                                 
2707                         ED_region_tag_redraw(CTX_wm_region(C));
2708                         
2709                         return WM_UI_HANDLER_BREAK;
2710                 }
2711                 /* handle menu */
2712                 else if(event->type == RIGHTMOUSE && event->val == KM_PRESS) {
2713                         ui_but_anim_menu(C, but);
2714                         return WM_UI_HANDLER_BREAK;
2715                 }
2716         }
2717
2718         /* verify if we can edit this button */
2719         if(ELEM(event->type, LEFTMOUSE, RETKEY)) {
2720                 /* this should become disabled button .. */
2721                 if(but->lock) {
2722                         if(but->lockstr) {
2723                                 BKE_report(NULL, RPT_WARNING, but->lockstr);
2724                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2725                                 return WM_UI_HANDLER_BREAK;
2726                         }
2727                 } 
2728                 else if(but->pointype && but->poin==0) {
2729                         /* there's a pointer needed */
2730                         BKE_reportf(NULL, RPT_WARNING, "DoButton pointer error: %s", but->str);
2731                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2732                         return WM_UI_HANDLER_BREAK;
2733                 }
2734         }
2735
2736         switch(but->type) {
2737         case BUT:
2738                 retval= ui_do_but_BUT(C, but, data, event);
2739                 break;
2740         case KEYEVT:
2741                 retval= ui_do_but_KEYEVT(C, but, data, event);
2742                 break;
2743         case TOGBUT: 
2744         case TOG: 
2745         case TOGR: 
2746         case ICONTOG:
2747         case ICONTOGN:
2748         case TOGN:
2749         case BUT_TOGDUAL:
2750         case OPTION:
2751         case OPTIONN:
2752                 retval= ui_do_but_TOG(C, but, data, event);
2753                 break;
2754 #if 0
2755         case SCROLL:
2756                 /* DrawBut(b, 1); */
2757                 /* do_scrollbut(b); */
2758                 /* DrawBut(b,0); */
2759                 break;
2760 #endif
2761         case NUM:
2762         case NUMABS:
2763                 retval= ui_do_but_NUM(C, block, but, data, event);
2764                 break;
2765         case SLI:
2766         case NUMSLI:
2767         case HSVSLI:
2768                 retval= ui_do_but_SLI(C, block, but, data, event);
2769                 break;
2770         case ROUNDBOX:  
2771         case LABEL:     
2772         case TOG3:      
2773         case ROW:
2774                 retval= ui_do_but_EXIT(C, but, data, event);
2775                 break;
2776         case TEX:
2777         case IDPOIN:
2778         case SEARCH_MENU:
2779                 retval= ui_do_but_TEX(C, block, but, data, event);
2780                 break;
2781         case MENU:
2782         case ICONROW:
2783         case ICONTEXTROW:
2784         case BLOCK:
2785         case PULLDOWN:
2786                 retval= ui_do_but_BLOCK(C, but, data, event);
2787                 break;
2788         case BUTM:
2789                 retval= ui_do_but_BUT(C, but, data, event);
2790                 break;
2791         case COL:
2792                 if(but->a1 == -1)  // signal to prevent calling up color picker
2793                         retval= ui_do_but_EXIT(C, but, data, event);
2794                 else
2795                         retval= ui_do_but_BLOCK(C, but, data, event);
2796                 break;
2797         case BUT_NORMAL:
2798                 retval= ui_do_but_NORMAL(C, block, but, data, event);
2799                 break;
2800         case BUT_COLORBAND:
2801                 retval= ui_do_but_COLORBAND(C, block, but, data, event);
2802                 break;
2803         case BUT_CURVE:
2804                 retval= ui_do_but_CURVE(C, block, but, data, event);
2805                 break;
2806         case HSVCUBE:
2807                 retval= ui_do_but_HSVCUBE(C, block, but, data, event);
2808                 break;
2809 #ifdef INTERNATIONAL
2810         case CHARTAB:
2811                 retval= ui_do_but_CHARTAB(C, block, but, data, event);
2812                 break;
2813 #endif
2814         /* XXX 2.50 links not implemented yet */
2815 #if 0
2816         case LINK:
2817         case INLINK:
2818                 retval= retval= ui_do_but_LINK(block, but);
2819                 break;
2820 #endif
2821         }
2822         
2823         return retval;
2824 }
2825
2826 /* ************************ button utilities *********************** */
2827
2828 static int ui_but_contains_pt(uiBut *but, int mx, int my)
2829 {
2830         return ((but->x1<mx && but->x2>=mx) && (but->y1<my && but->y2>=my));
2831 }
2832
2833 static uiBut *ui_but_find_activated(ARegion *ar)
2834 {
2835         uiBlock *block;
2836         uiBut *but;
2837
2838         for(block=ar->uiblocks.first; block; block=block->next)
2839                 for(but=block->buttons.first; but; but= but->next)
2840                         if(but->active)
2841                                 return but;
2842
2843         return NULL;
2844 }
2845
2846 int ui_button_is_active(ARegion *ar)
2847 {
2848         return (ui_but_find_activated(ar) != NULL);
2849 }
2850
2851 static void ui_blocks_set_tooltips(ARegion *ar, int enable)
2852 {
2853         uiBlock *block;
2854
2855         if(!ar)
2856                 return;
2857
2858         /* we disabled buttons when when they were already shown, and
2859          * re-enable them on mouse move */
2860         for(block=ar->uiblocks.first; block; block=block->next)
2861                 block->tooltipdisabled= !enable;
2862 }
2863
2864 static int ui_mouse_inside_region(ARegion *ar, int x, int y)
2865 {
2866         uiBlock *block;
2867         int mx, my;
2868
2869         /* check if the mouse is in the region, and in case of a view2d also check
2870          * if it is inside the view2d itself, not over scrollbars for example */
2871         if(!BLI_in_rcti(&ar->winrct, x, y)) {
2872                 for(block=ar->uiblocks.first; block; block=block->next)
2873                         block->auto_open= 0;
2874
2875                 return 0;
2876         }
2877
2878         if(ar->v2d.mask.xmin!=ar->v2d.mask.xmax) {
2879                 mx= x;
2880                 my= y;
2881                 ui_window_to_region(ar, &mx, &my);
2882
2883                 if(!BLI_in_rcti(&ar->v2d.mask, mx, my))
2884                         return 0;
2885         }
2886
2887         return 1;
2888 }
2889
2890 static int ui_mouse_inside_button(ARegion *ar, uiBut *but, int x, int y)
2891 {
2892         if(!ui_mouse_inside_region(ar, x, y))
2893                 return 0;
2894
2895         ui_window_to_block(ar, but->block, &x, &y);
2896
2897         if(!ui_but_contains_pt(but, x, y))
2898                 return 0;
2899         
2900         return 1;
2901 }
2902
2903 static uiBut *ui_but_find_mouse_over(ARegion *ar, int x, int y)
2904 {
2905         uiBlock *block;
2906         uiBut *but, *butover= NULL;
2907         int mx, my;
2908
2909         if(!ui_mouse_inside_region(ar, x, y))
2910                 return NULL;
2911
2912         for(block=ar->uiblocks.first; block; block=block->next) {
2913                 mx= x;
2914                 my= y;
2915                 ui_window_to_block(ar, block, &mx, &my);
2916
2917                 for(but=block->buttons.first; but; but= but->next) {
2918                         if(ELEM3(but->type, LABEL, ROUNDBOX, SEPR))
2919                                 continue;
2920
2921                         if(ui_but_contains_pt(but, mx, my))
2922                                 /* give precedence to already activated buttons */
2923                                 if(!butover || (!butover->active && but->active))
2924                                         butover= but;
2925                 }
2926         }
2927
2928         return butover;
2929 }
2930
2931 /* ****************** button state handling **************************/
2932
2933 static int button_modal_state(uiHandleButtonState state)
2934 {
2935         return ELEM6(state, BUTTON_STATE_WAIT_RELEASE, BUTTON_STATE_WAIT_KEY_EVENT,
2936                 BUTTON_STATE_NUM_EDITING, BUTTON_STATE_TEXT_EDITING,
2937                 BUTTON_STATE_TEXT_SELECTING, BUTTON_STATE_MENU_OPEN);
2938 }
2939
2940 static void button_tooltip_timer_reset(uiBut *but)
2941 {
2942         uiHandleButtonData *data;
2943
2944         data= but->active;
2945
2946         if(data->tooltiptimer) {
2947                 WM_event_remove_window_timer(data->window, data->tooltiptimer);
2948                 data->tooltiptimer= NULL;
2949         }
2950
2951         if(U.flag & USER_TOOLTIPS)
2952                 if(!but->block->tooltipdisabled)
2953                         data->tooltiptimer= WM_event_add_window_timer(data->window, TIMER, BUTTON_TOOLTIP_DELAY);
2954 }
2955
2956 static void button_activate_state(bContext *C, uiBut *but, uiHandleButtonState state)
2957 {
2958         uiHandleButtonData *data;
2959
2960         data= but->active;
2961         if(data->state == state)
2962                 return;
2963
2964         /* highlight has timers for tooltips and auto open */
2965         if(state == BUTTON_STATE_HIGHLIGHT) {
2966                 but->flag &= ~UI_SELECT;
2967
2968                 button_tooltip_timer_reset(but);
2969
2970                 /* automatic open pulldown block timer */
2971                 if(ELEM3(but->type, BLOCK, PULLDOWN, ICONTEXTROW)) {
2972                         if(!data->autoopentimer) {
2973                                 int time;
2974
2975                                 if(but->block->auto_open==2) time= 1;    // test for toolbox
2976                                 else if(but->block->flag & UI_BLOCK_LOOP || but->block->auto_open) time= 5*U.menuthreshold2;
2977                                 else if(U.uiflag & USER_MENUOPENAUTO) time= 5*U.menuthreshold1;
2978                                 else time= -1;
2979
2980                                 if(time >= 0)
2981                                         data->autoopentimer= WM_event_add_window_timer(data->window, TIMER, 0.02*(double)time);
2982                         }
2983                 }
2984         }
2985         else {
2986                 but->flag |= UI_SELECT;
2987
2988                 if(data->tooltiptimer) {
2989                         WM_event_remove_window_timer(data->window, data->tooltiptimer);
2990                         data-