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