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