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