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