Merge from trunk -r 25003:25745.
[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, "ui_textedit origstr");
1083         
1084         BLI_strncpy(origstr, but->drawstr, data->maxlen);
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+1 < 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+1 < 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);
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                         but->modifier_key= 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                 
1872                 if(event->type == MOUSEMOVE)
1873                         return WM_UI_HANDLER_CONTINUE;
1874                 
1875                 if(event->type == ESCKEY) {
1876                         /* data->cancel doesnt work, this button opens immediate */
1877                         if(but->flag & UI_BUT_IMMEDIATE)
1878                                 ui_set_but_val(but, 0);
1879                         else
1880                                 data->cancel= 1;
1881                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1882                         return WM_UI_HANDLER_BREAK;
1883                 }
1884                 
1885                 /* always set */
1886                 but->modifier_key = 0;
1887                 if(event->shift)
1888                         but->modifier_key |= KM_SHIFT;
1889                 if(event->alt)
1890                         but->modifier_key |= KM_ALT;
1891                 if(event->ctrl)
1892                         but->modifier_key |= KM_CTRL;
1893                 if(event->oskey)
1894                         but->modifier_key |= KM_OSKEY;
1895                 
1896                 ui_check_but(but);
1897                 ED_region_tag_redraw(data->region);
1898                         
1899                 if(event->val==KM_PRESS) {
1900                         if(ISHOTKEY(event->type)) { 
1901                                 
1902                                 if(WM_key_event_string(event->type)[0])
1903                                         ui_set_but_val(but, event->type);
1904                                 else
1905                                         data->cancel= 1;
1906                                 
1907                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
1908                                 return WM_UI_HANDLER_BREAK;
1909                         }
1910                 }
1911         }
1912         
1913         return WM_UI_HANDLER_CONTINUE;
1914 }
1915
1916
1917 static int ui_do_but_KEYEVT(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
1918 {
1919         if(data->state == BUTTON_STATE_HIGHLIGHT) {
1920                 if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val==KM_PRESS) {
1921                         short event= (short)ui_get_but_val(but);
1922                         /* hardcoded prevention from editing or assigning ESC */
1923                         if(event!=ESCKEY)
1924                                 button_activate_state(C, but, BUTTON_STATE_WAIT_KEY_EVENT);
1925                         return WM_UI_HANDLER_BREAK;
1926                 }
1927         }
1928         else if(data->state == BUTTON_STATE_WAIT_KEY_EVENT) {
1929                 if(event->type == MOUSEMOVE)
1930                         return WM_UI_HANDLER_CONTINUE;
1931
1932                 if(event->val==KM_PRESS) {
1933                         if(event->type!=ESCKEY && WM_key_event_string(event->type)[0])
1934                                 ui_set_but_val(but, event->type);
1935                         else
1936                                 data->cancel= 1;
1937
1938                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1939                 }
1940         }
1941
1942         return WM_UI_HANDLER_CONTINUE;
1943 }
1944
1945 static int ui_do_but_TEX(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
1946 {
1947         if(data->state == BUTTON_STATE_HIGHLIGHT) {
1948                 if(ELEM4(event->type, LEFTMOUSE, PADENTER, RETKEY, EVT_BUT_OPEN) && event->val==KM_PRESS) {
1949                         if(but->dt == UI_EMBOSSN && !event->ctrl);
1950                         else {
1951                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
1952                                 return WM_UI_HANDLER_BREAK;
1953                         }
1954                 }
1955         }
1956         else if(data->state == BUTTON_STATE_TEXT_EDITING) {
1957                 ui_do_but_textedit(C, block, but, data, event);
1958                 return WM_UI_HANDLER_BREAK;
1959         }
1960         else if(data->state == BUTTON_STATE_TEXT_SELECTING) {
1961                 ui_do_but_textedit_select(C, block, but, data, event);
1962                 return WM_UI_HANDLER_BREAK;
1963         }
1964
1965         return WM_UI_HANDLER_CONTINUE;
1966 }
1967
1968 static int ui_do_but_TOG(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
1969 {
1970         if(data->state == BUTTON_STATE_HIGHLIGHT) {
1971                 if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val==KM_PRESS) {
1972                         data->togdual= event->ctrl;
1973                         data->togonly= !event->shift;
1974                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1975                         return WM_UI_HANDLER_BREAK;
1976                 }
1977         }
1978         return WM_UI_HANDLER_CONTINUE;
1979 }
1980
1981 static int ui_do_but_EXIT(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
1982 {
1983         if(data->state == BUTTON_STATE_HIGHLIGHT) {
1984                 if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val==KM_PRESS) {
1985                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1986                         return WM_UI_HANDLER_BREAK;
1987                 }
1988         }
1989
1990         return WM_UI_HANDLER_CONTINUE;
1991 }
1992
1993 /* var names match ui_numedit_but_NUM */
1994 static float ui_numedit_apply_snapf(float tempf, float softmin, float softmax, float softrange, int snap)
1995 {
1996         if(tempf==softmin || tempf==softmax)
1997                 return tempf;
1998
1999         switch(snap) {
2000         case 0:
2001                 break;
2002         case 1:
2003                 if(tempf==softmin || tempf==softmax) { }
2004                 else if(softrange < 2.10) tempf= 0.1*floor(10*tempf);
2005                 else if(softrange < 21.0) tempf= floor(tempf);
2006                 else tempf= 10.0*floor(tempf/10.0);
2007                 break;
2008         case 2:
2009                 if(tempf==softmin || tempf==softmax) { }
2010                 else if(softrange < 2.10) tempf= 0.01*floor(100.0*tempf);
2011                 else if(softrange < 21.0) tempf= 0.1*floor(10.0*tempf);
2012                 else tempf= floor(tempf);
2013                 break;
2014         }
2015
2016         return tempf;
2017 }
2018
2019 static float ui_numedit_apply_snap(int temp, float softmin, float softmax, int snap)
2020 {
2021         if(temp==softmin || temp==softmax)
2022                 return temp;
2023
2024         switch(snap) {
2025         case 0:
2026                 break;
2027         case 1:
2028                 temp= 10*(temp/10);
2029                 break;
2030         case 2:
2031                 temp= 100*(temp/100);
2032                 break;
2033         }
2034
2035         return temp;
2036 }
2037
2038 static int ui_numedit_but_NUM(uiBut *but, uiHandleButtonData *data, float fac, int snap, int mx)
2039 {
2040         float deler, tempf, softmin, softmax, softrange;
2041         int lvalue, temp, changed= 0;
2042         
2043         if(mx == data->draglastx)
2044                 return changed;
2045         
2046         /* drag-lock - prevent unwanted scroll adjustments */
2047         /* change value (now 3) to adjust threshold in pixels */
2048         if(data->draglock) {
2049                 if(abs(mx-data->dragstartx) <= 3)
2050                         return changed;
2051
2052                 data->draglock= 0;
2053                 data->dragstartx= mx;  /* ignore mouse movement within drag-lock */
2054         }
2055
2056         softmin= but->softmin;
2057         softmax= but->softmax;
2058         softrange= softmax - softmin;
2059
2060
2061         if(ui_is_a_warp_but(but)) {
2062                 /* Mouse location isn't screen clamped to the screen so use a linear mapping
2063                  * 2px == 1-int, or 1px == 1-ClickStep */
2064                 if(ui_is_but_float(but)) {
2065                         fac *= 0.01*but->a1;
2066                         tempf = data->startvalue + ((mx - data->dragstartx) * fac);
2067                         tempf= ui_numedit_apply_snapf(tempf, softmin, softmax, softrange, snap);
2068
2069 #if 1           /* fake moving the click start, nicer for dragging back after passing the limit */
2070                         if(tempf < softmin) {
2071                                 data->dragstartx -= (softmin-tempf) / fac;
2072                                 tempf= softmin;
2073                         } else if (tempf > softmax) {
2074                                 data->dragstartx += (tempf-softmax) / fac;
2075                                 tempf= softmax;
2076                         }
2077 #else
2078                         CLAMP(tempf, softmin, softmax);
2079 #endif
2080
2081                         if(tempf != data->value) {
2082                                 data->dragchange= 1;
2083                                 data->value= tempf;
2084                                 changed= 1;
2085                         }
2086                 }
2087                 else {
2088                         fac = 0.5; /* simple 2px == 1 */
2089
2090                         temp= data->startvalue + ((mx - data->dragstartx) * fac);
2091                         temp= ui_numedit_apply_snap(temp, softmin, softmax, snap);
2092
2093 #if 1           /* fake moving the click start, nicer for dragging back after passing the limit */
2094                         if(temp < softmin) {
2095                                 data->dragstartx -= (softmin-temp) / fac;
2096                                 temp= softmin;
2097                         } else if (temp > softmax) {
2098                                 data->dragstartx += (temp-softmax) / fac;
2099                                 temp= softmax;
2100                         }
2101 #else
2102                         CLAMP(temp, softmin, softmax);
2103 #endif
2104
2105                         if(temp != data->value) {
2106                                 data->dragchange= 1;
2107                                 data->value= temp;
2108                                 changed= 1;
2109                         }
2110                 }
2111
2112                 data->draglastx= mx;
2113         }
2114         else {
2115                 /* Use a non-linear mapping of the mouse drag especially for large floats (normal behavior) */
2116                 deler= 500;
2117                 if(!ui_is_but_float(but)) {
2118                         if((softrange)<100) deler= 200.0;
2119                         if((softrange)<25) deler= 50.0;
2120                 }
2121                 deler /= fac;
2122
2123                 if(ui_is_but_float(but) && softrange > 11) {
2124                         /* non linear change in mouse input- good for high precicsion */
2125                         data->dragf+= (((float)(mx-data->draglastx))/deler) * (fabs(data->dragstartx-mx)*0.002);
2126                 } else if (!ui_is_but_float(but) && softrange > 129) { /* only scale large int buttons */
2127                         /* non linear change in mouse input- good for high precicsionm ints need less fine tuning */
2128                         data->dragf+= (((float)(mx-data->draglastx))/deler) * (fabs(data->dragstartx-mx)*0.004);
2129                 } else {
2130                         /*no scaling */
2131                         data->dragf+= ((float)(mx-data->draglastx))/deler ;
2132                 }
2133         
2134                 if(data->dragf>1.0) data->dragf= 1.0;
2135                 if(data->dragf<0.0) data->dragf= 0.0;
2136                 data->draglastx= mx;
2137                 tempf= (softmin + data->dragf*softrange);
2138
2139
2140                 if(!ui_is_but_float(but)) {
2141                         temp= floor(tempf+.5);
2142
2143                         temp= ui_numedit_apply_snap(temp, softmin, softmax, snap);
2144
2145                         CLAMP(temp, softmin, softmax);
2146                         lvalue= (int)data->value;
2147
2148                         if(temp != lvalue) {
2149                                 data->dragchange= 1;
2150                                 data->value= (double)temp;
2151                                 changed= 1;
2152                         }
2153                 }
2154                 else {
2155                         temp= 0;
2156                         tempf= ui_numedit_apply_snapf(tempf, softmin, softmax, softrange, snap);
2157
2158                         CLAMP(tempf, softmin, softmax);
2159
2160                         if(tempf != data->value) {
2161                                 data->dragchange= 1;
2162                                 data->value= tempf;
2163                                 changed= 1;
2164                         }
2165                 }
2166         }
2167
2168
2169         return changed;
2170 }
2171
2172 static int ui_do_but_NUM(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2173 {
2174         int mx, my;     /* mouse location scaled to fit the UI */
2175         int screen_mx, screen_my; /* mouse location kept at screen pixel coords */
2176         int click= 0;
2177         int retval= WM_UI_HANDLER_CONTINUE;
2178         
2179         mx= screen_mx= event->x;
2180         my= screen_my= event->y;
2181
2182         ui_window_to_block(data->region, block, &mx, &my);
2183
2184         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2185                 /* XXX hardcoded keymap check.... */
2186                 if(event->type == WHEELDOWNMOUSE && event->alt) {
2187                         mx= but->x1;
2188                         click= 1;
2189                 }
2190                 else if(event->type == WHEELUPMOUSE && event->alt) {
2191                         mx= but->x2;
2192                         click= 1;
2193                 }
2194                 else if(event->val==KM_PRESS) {
2195                         if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->ctrl) {
2196                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2197                                 retval= WM_UI_HANDLER_BREAK;
2198                         }
2199                         else if(event->type == LEFTMOUSE) {
2200                                 data->dragstartx= data->draglastx= ui_is_a_warp_but(but) ? screen_mx:mx;
2201                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2202                                 retval= WM_UI_HANDLER_BREAK;
2203                         }
2204                         else if(ELEM(event->type, PADENTER, RETKEY) && event->val==KM_PRESS)
2205                                 click= 1;
2206                 }
2207                 
2208         }
2209         else if(data->state == BUTTON_STATE_NUM_EDITING) {
2210                 if(event->type == ESCKEY) {
2211                         data->cancel= 1;
2212                         data->escapecancel= 1;
2213                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2214                 }
2215                 else if(event->type == LEFTMOUSE && event->val!=KM_PRESS) {
2216                         if(data->dragchange)
2217                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2218                         else
2219                                 click= 1;
2220                 }
2221                 else if(event->type == MOUSEMOVE) {
2222                         float fac;
2223                         int snap;
2224
2225                         fac= 1.0f;
2226                         if(event->shift) fac /= 10.0f;
2227                         if(event->alt) fac /= 20.0f;
2228                         
2229                         snap= (event->ctrl)? (event->shift)? 2: 1: 0;
2230
2231                         if(ui_numedit_but_NUM(but, data, fac, snap, (ui_is_a_warp_but(but) ? screen_mx:mx)))
2232                                 ui_numedit_apply(C, block, but, data);
2233                 }
2234                 retval= WM_UI_HANDLER_BREAK;
2235         }
2236         else if(data->state == BUTTON_STATE_TEXT_EDITING) {
2237                 ui_do_but_textedit(C, block, but, data, event);
2238                 retval= WM_UI_HANDLER_BREAK;
2239         }
2240         else if(data->state == BUTTON_STATE_TEXT_SELECTING) {
2241                 ui_do_but_textedit_select(C, block, but, data, event);
2242                 retval= WM_UI_HANDLER_BREAK;
2243         }
2244         
2245         if(click) {
2246                 /* we can click on the side arrows to increment/decrement,
2247                  * or click inside to edit the value directly */
2248                 float tempf, softmin, softmax;
2249                 int temp;
2250
2251                 softmin= but->softmin;
2252                 softmax= but->softmax;
2253
2254                 if(!ui_is_but_float(but)) {
2255                         if(mx < (but->x1 + (but->x2 - but->x1)/3 - 3)) {
2256                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2257
2258                                 temp= (int)data->value - 1;
2259                                 if(temp>=softmin && temp<=softmax)
2260                                         data->value= (double)temp;
2261                                 else
2262                                         data->cancel= 1;
2263
2264                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2265                         }
2266                         else if(mx > (but->x1 + (2*(but->x2 - but->x1)/3) + 3)) {
2267                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2268
2269                                 temp= (int)data->value + 1;
2270                                 if(temp>=softmin && temp<=softmax)
2271                                         data->value= (double)temp;
2272                                 else
2273                                         data->cancel= 1;
2274
2275                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2276                         }
2277                         else
2278                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2279                 }
2280                 else {
2281                         if(mx < (but->x1 + (but->x2 - but->x1)/3 - 3)) {
2282                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2283
2284                                 tempf= data->value - 0.01*but->a1;
2285                                 if (tempf < softmin) tempf = softmin;
2286                                 data->value= tempf;
2287
2288                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2289                         }
2290                         else if(mx > but->x1 + (2*((but->x2 - but->x1)/3) + 3)) {
2291                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2292
2293                                 tempf= data->value + 0.01*but->a1;
2294                                 if (tempf > softmax) tempf = softmax;
2295                                 data->value= tempf;
2296
2297                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2298                         }
2299                         else
2300                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2301                 }
2302
2303                 retval= WM_UI_HANDLER_BREAK;
2304         }
2305         
2306         return retval;
2307 }
2308
2309 static int ui_numedit_but_SLI(uiBut *but, uiHandleButtonData *data, int shift, int ctrl, int mx)
2310 {
2311         float deler, f, tempf, softmin, softmax, softrange;
2312         int temp, lvalue, changed= 0;
2313
2314         softmin= but->softmin;
2315         softmax= but->softmax;
2316         softrange= softmax - softmin;
2317
2318         if(but->type==NUMSLI) deler= ((but->x2-but->x1) - 5.0*but->aspect);
2319         else if(but->type==HSVSLI) deler= ((but->x2-but->x1)/2 - 5.0*but->aspect);
2320         else if(but->type==SCROLL) {
2321                 int horizontal= (but->x2 - but->x1 > but->y2 - but->y1);
2322                 float size= (horizontal)? (but->x2-but->x1): -(but->y2-but->y1);
2323                 deler= size*(but->softmax - but->softmin)/(but->softmax - but->softmin + but->a1);
2324         }
2325         else deler= (but->x2-but->x1- 5.0*but->aspect);
2326
2327         f= (float)(mx-data->dragstartx)/deler + data->dragfstart;
2328         
2329         if(shift)
2330                 f= (f-data->dragfstart)/10.0 + data->dragfstart;
2331
2332         CLAMP(f, 0.0, 1.0);
2333         tempf= softmin + f*softrange;
2334         temp= floor(tempf+.5);
2335
2336         if(ctrl) {
2337                 if(tempf==softmin || tempf==softmax);
2338                 else if(ui_is_but_float(but)) {
2339
2340                         if(shift) {
2341                                 if(tempf==softmin || tempf==softmax);
2342                                 else if(softmax-softmin < 2.10) tempf= 0.01*floor(100.0*tempf);
2343                                 else if(softmax-softmin < 21.0) tempf= 0.1*floor(10.0*tempf);
2344                                 else tempf= floor(tempf);
2345                         }
2346                         else {
2347                                 if(softmax-softmin < 2.10) tempf= 0.1*floor(10*tempf);
2348                                 else if(softmax-softmin < 21.0) tempf= floor(tempf);
2349                                 else tempf= 10.0*floor(tempf/10.0);
2350                         }
2351                 }
2352                 else {
2353                         temp= 10*(temp/10);
2354                         tempf= temp;
2355                 }
2356         }
2357
2358         if(!ui_is_but_float(but)) {
2359                 lvalue= floor(data->value+0.5);
2360
2361                 CLAMP(temp, softmin, softmax);
2362
2363                 if(temp != lvalue) {
2364                         data->value= temp;
2365                         data->dragchange= 1;
2366                         changed= 1;
2367                 }
2368         }
2369         else {
2370                 CLAMP(tempf, softmin, softmax);
2371
2372                 if(tempf != data->value) {
2373                         data->value= tempf;
2374                         data->dragchange= 1;
2375                         changed= 1;
2376                 }
2377         }
2378
2379         return changed;
2380 }
2381
2382 static int ui_do_but_SLI(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2383 {
2384         int mx, my, click= 0;
2385         int retval= WM_UI_HANDLER_CONTINUE;
2386
2387         mx= event->x;
2388         my= event->y;
2389         ui_window_to_block(data->region, block, &mx, &my);
2390
2391         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2392                 /* XXX hardcoded keymap check.... */
2393                 if(event->type == WHEELDOWNMOUSE && event->alt) {
2394                         mx= but->x1;
2395                         click= 2;
2396                 }
2397                 else if(event->type == WHEELUPMOUSE && event->alt) {
2398                         mx= but->x2;
2399                         click= 2;
2400                 }
2401                 else if(event->val==KM_PRESS) {
2402                         if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->ctrl) {
2403                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2404                                 retval= WM_UI_HANDLER_BREAK;
2405                         }
2406                         /* alt-click on sides to get "arrows" like in NUM buttons, and match wheel usage above */
2407                         else if(event->type == LEFTMOUSE && event->alt) {
2408                                 int halfpos = (but->x1 + but->x2) / 2;
2409                                 click = 2;
2410                                 if (mx < halfpos)
2411                                         mx = but->x1;
2412                                 else
2413                                         mx = but->x2;
2414                         }
2415                         else if(event->type == LEFTMOUSE) {
2416                                 data->dragstartx= mx;
2417                                 data->draglastx= mx;
2418                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2419                                 retval= WM_UI_HANDLER_BREAK;
2420                         }
2421                         else if(ELEM(event->type, PADENTER, RETKEY) && event->val==KM_PRESS)
2422                                 click= 1;
2423                 }
2424         }
2425         else if(data->state == BUTTON_STATE_NUM_EDITING) {
2426                 if(event->type == ESCKEY) {
2427                         data->cancel= 1;
2428                         data->escapecancel= 1;
2429                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2430                 }
2431                 else if(event->type == LEFTMOUSE && event->val!=KM_PRESS) {
2432                         if(data->dragchange)
2433                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2434                         else
2435                                 click= 1;
2436                 }
2437                 else if(event->type == MOUSEMOVE) {
2438                         if(ui_numedit_but_SLI(but, data, event->shift, event->ctrl, mx))
2439                                 ui_numedit_apply(C, block, but, data);
2440                 }
2441                 retval= WM_UI_HANDLER_BREAK;
2442         }
2443         else if(data->state == BUTTON_STATE_TEXT_EDITING) {
2444                 ui_do_but_textedit(C, block, but, data, event);
2445                 retval= WM_UI_HANDLER_BREAK;
2446         }
2447         else if(data->state == BUTTON_STATE_TEXT_SELECTING) {
2448                 ui_do_but_textedit_select(C, block, but, data, event);
2449                 retval= WM_UI_HANDLER_BREAK;
2450         }
2451
2452         if(click) {
2453                 if (click==2) {
2454                         /* nudge slider to the left or right */
2455                         float f, tempf, softmin, softmax, softrange;
2456                         int temp;
2457                         
2458                         button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2459                         
2460                         softmin= but->softmin;
2461                         softmax= but->softmax;
2462                         softrange= softmax - softmin;
2463                         
2464                         tempf= data->value;
2465                         temp= (int)data->value;
2466                         
2467                         /* XXX useles "if", same result for f, uh??? */
2468                         if(but->type==SLI) f= (float)(mx-but->x1)/(but->x2-but->x1);
2469                         else f= (float)(mx- but->x1)/(but->x2-but->x1);
2470                         
2471                         f= softmin + f*softrange;
2472                         
2473                         if(!ui_is_but_float(but)) {
2474                                 if(f<temp) temp--;
2475                                 else temp++;
2476                                 
2477                                 if(temp>=softmin && temp<=softmax)
2478                                         data->value= temp;
2479                                 else
2480                                         data->cancel= 1;
2481                         } 
2482                         else {
2483                                 if(f<tempf) tempf-=.01;
2484                                 else tempf+=.01;
2485                                 
2486                                 if(tempf>=softmin && tempf<=softmax)
2487                                         data->value= tempf;
2488                                 else
2489                                         data->cancel= 1;
2490                         }
2491                         
2492                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2493                         retval= WM_UI_HANDLER_BREAK;
2494                 }
2495                 else {
2496                         /* edit the value directly */
2497                         button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2498                         retval= WM_UI_HANDLER_BREAK;
2499                 }
2500         }
2501         
2502         return retval;
2503 }
2504
2505 static int ui_do_but_SCROLL(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2506 {
2507         int mx, my, click= 0;
2508         int retval= WM_UI_HANDLER_CONTINUE;
2509         int horizontal= (but->x2 - but->x1 > but->y2 - but->y1);
2510         
2511         mx= event->x;
2512         my= event->y;
2513         ui_window_to_block(data->region, block, &mx, &my);
2514
2515         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2516                 if(event->val==KM_PRESS) {
2517                         if(event->type == LEFTMOUSE) {
2518                                 if(horizontal) {
2519                                         data->dragstartx= mx;
2520                                         data->draglastx= mx;
2521                                 }
2522                                 else {
2523                                         data->dragstartx= my;
2524                                         data->draglastx= my;
2525                                 }
2526                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2527                                 retval= WM_UI_HANDLER_BREAK;
2528                         }
2529                         else if(ELEM(event->type, PADENTER, RETKEY) && event->val==KM_PRESS)
2530                                 click= 1;
2531                 }
2532         }
2533         else if(data->state == BUTTON_STATE_NUM_EDITING) {
2534                 if(event->type == ESCKEY) {
2535                         data->cancel= 1;
2536                         data->escapecancel= 1;
2537                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2538                 }
2539                 else if(event->type == LEFTMOUSE && event->val!=KM_PRESS) {
2540                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2541                 }
2542                 else if(event->type == MOUSEMOVE) {
2543                         if(ui_numedit_but_SLI(but, data, 0, 0, (horizontal)? mx: my))
2544                                 ui_numedit_apply(C, block, but, data);
2545                 }
2546
2547                 retval= WM_UI_HANDLER_BREAK;
2548         }
2549         
2550         return retval;
2551 }
2552
2553 static int ui_do_but_BLOCK(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2554 {
2555         
2556         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2557                 if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val==KM_PRESS) {
2558                         button_activate_state(C, but, BUTTON_STATE_MENU_OPEN);
2559                         return WM_UI_HANDLER_BREAK;
2560                 }
2561                 else if(ELEM3(but->type, MENU, ICONROW, ICONTEXTROW)) {
2562                         
2563                         if(event->type == WHEELDOWNMOUSE && event->alt) {
2564                                 data->value= ui_step_name_menu(but, -1);
2565                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2566                                 ui_apply_button(C, but->block, but, data, 1);
2567                                 return WM_UI_HANDLER_BREAK;
2568                         }
2569                         else if(event->type == WHEELUPMOUSE && event->alt) {
2570                                 data->value= ui_step_name_menu(but, 1);
2571                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2572                                 ui_apply_button(C, but->block, but, data, 1);
2573                                 return WM_UI_HANDLER_BREAK;
2574                         }
2575                 }
2576                 else if(but->type==COL) {
2577                         if( ELEM(event->type, WHEELDOWNMOUSE, WHEELUPMOUSE) && event->alt) {
2578                                 float col[3];
2579                                 
2580                                 ui_get_but_vectorf(but, col);
2581                                 rgb_to_hsv(col[0], col[1], col[2], but->hsv, but->hsv+1, but->hsv+2);
2582
2583                                 if(event->type==WHEELDOWNMOUSE)
2584                                         but->hsv[2]= CLAMPIS(but->hsv[2]-0.05f, 0.0f, 1.0f);
2585                                 else
2586                                         but->hsv[2]= CLAMPIS(but->hsv[2]+0.05f, 0.0f, 1.0f);
2587                                 
2588                                 hsv_to_rgb(but->hsv[0], but->hsv[1], but->hsv[2], data->vec, data->vec+1, data->vec+2);
2589                                 
2590                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2591                                 ui_apply_button(C, but->block, but, data, 1);
2592                                 return WM_UI_HANDLER_BREAK;
2593                         }
2594                 }
2595         }
2596
2597         return WM_UI_HANDLER_CONTINUE;
2598 }
2599
2600 static int ui_numedit_but_NORMAL(uiBut *but, uiHandleButtonData *data, int mx, int my)
2601 {
2602         float dx, dy, rad, radsq, mrad, *fp;
2603         int mdx, mdy, changed= 1;
2604         
2605         /* button is presumed square */
2606         /* if mouse moves outside of sphere, it does negative normal */
2607
2608         fp= data->origvec;
2609         rad= (but->x2 - but->x1);
2610         radsq= rad*rad;
2611         
2612         if(fp[2]>0.0f) {
2613                 mdx= (rad*fp[0]);
2614                 mdy= (rad*fp[1]);
2615         }
2616         else if(fp[2]> -1.0f) {
2617                 mrad= rad/sqrt(fp[0]*fp[0] + fp[1]*fp[1]);
2618                 
2619                 mdx= 2.0f*mrad*fp[0] - (rad*fp[0]);
2620                 mdy= 2.0f*mrad*fp[1] - (rad*fp[1]);
2621         }
2622         else mdx= mdy= 0;
2623         
2624         dx= (float)(mx+mdx-data->dragstartx);
2625         dy= (float)(my+mdy-data->dragstarty);
2626
2627         fp= data->vec;
2628         mrad= dx*dx+dy*dy;
2629         if(mrad < radsq) {      /* inner circle */
2630                 fp[0]= dx;
2631                 fp[1]= dy;
2632                 fp[2]= sqrt( radsq-dx*dx-dy*dy );
2633         }
2634         else {  /* outer circle */
2635                 
2636                 mrad= rad/sqrt(mrad);   // veclen
2637                 
2638                 dx*= (2.0f*mrad - 1.0f);
2639                 dy*= (2.0f*mrad - 1.0f);
2640                 
2641                 mrad= dx*dx+dy*dy;
2642                 if(mrad < radsq) {
2643                         fp[0]= dx;
2644                         fp[1]= dy;
2645                         fp[2]= -sqrt( radsq-dx*dx-dy*dy );
2646                 }
2647         }
2648         normalize_v3(fp);
2649
2650         data->draglastx= mx;
2651         data->draglasty= my;
2652
2653         return changed;
2654 }
2655
2656 static int ui_do_but_NORMAL(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2657 {
2658         int mx, my;
2659
2660         mx= event->x;
2661         my= event->y;
2662         ui_window_to_block(data->region, block, &mx, &my);
2663
2664         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2665                 if(event->type==LEFTMOUSE && event->val==KM_PRESS) {
2666                         data->dragstartx= mx;
2667                         data->dragstarty= my;
2668                         data->draglastx= mx;
2669                         data->draglasty= my;
2670                         button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2671
2672                         /* also do drag the first time */
2673                         if(ui_numedit_but_NORMAL(but, data, mx, my))
2674                                 ui_numedit_apply(C, block, but, data);
2675                         
2676                         return WM_UI_HANDLER_BREAK;
2677                 }
2678         }
2679         else if(data->state == BUTTON_STATE_NUM_EDITING) {
2680                 if(event->type == MOUSEMOVE) {
2681                         if(mx!=data->draglastx || my!=data->draglasty) {
2682                                 if(ui_numedit_but_NORMAL(but, data, mx, my))
2683                                         ui_numedit_apply(C, block, but, data);
2684                         }
2685                 }
2686                 else if(event->type==LEFTMOUSE && event->val!=KM_PRESS)
2687                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2688
2689                 return WM_UI_HANDLER_BREAK;
2690         }
2691         
2692         return WM_UI_HANDLER_CONTINUE;
2693 }
2694
2695 static int ui_numedit_but_HSVCUBE(uiBut *but, uiHandleButtonData *data, int mx, int my)
2696 {
2697         float x, y;
2698         int changed= 1;
2699         int color_profile = but->block->color_profile;
2700         
2701         if (but->rnaprop) {
2702                 if (RNA_property_subtype(but->rnaprop) == PROP_COLOR_GAMMA)
2703                         color_profile = BLI_PR_NONE;
2704         }
2705                 
2706         /* relative position within box */
2707         x= ((float)mx-but->x1)/(but->x2-but->x1);
2708         y= ((float)my-but->y1)/(but->y2-but->y1);
2709         CLAMP(x, 0.0, 1.0);
2710         CLAMP(y, 0.0, 1.0);
2711         
2712         if(but->a1==0) {
2713                 but->hsv[0]= x; 
2714                 but->hsv[2]= y; 
2715         }
2716         else if(but->a1==1) {
2717                 but->hsv[0]= x;                                 
2718                 but->hsv[1]= y;                                 
2719         }
2720         else if(but->a1==2) {
2721                 but->hsv[2]= x; 
2722                 but->hsv[1]= y; 
2723         }
2724         else if(but->a1==3) {
2725                 but->hsv[0]= x; 
2726         }
2727         else {
2728                 /* vertical 'value' strip */
2729                 but->hsv[2]= y; 
2730                 if (color_profile)
2731                         but->hsv[2] = srgb_to_linearrgb(but->hsv[2]);
2732         }
2733
2734         ui_set_but_hsv(but);    // converts to rgb
2735         
2736         // update button values and strings
2737         ui_update_block_buts_hsv(but->block, but->hsv);
2738
2739         data->draglastx= mx;
2740         data->draglasty= my;
2741
2742         return changed;
2743 }
2744
2745 static int ui_do_but_HSVCUBE(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2746 {
2747         int mx, my;
2748
2749         mx= event->x;
2750         my= event->y;
2751         ui_window_to_block(data->region, block, &mx, &my);
2752
2753         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2754                 if(event->type==LEFTMOUSE && event->val==KM_PRESS) {
2755                         data->dragstartx= mx;
2756                         data->dragstarty= my;
2757                         data->draglastx= mx;
2758                         data->draglasty= my;
2759                         button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2760
2761                         /* also do drag the first time */
2762                         if(ui_numedit_but_HSVCUBE(but, data, mx, my))
2763                                 ui_numedit_apply(C, block, but, data);
2764                         
2765                         return WM_UI_HANDLER_BREAK;
2766                 }
2767         }
2768         else if(data->state == BUTTON_STATE_NUM_EDITING) {
2769                 if(event->type == MOUSEMOVE) {
2770                         if(mx!=data->draglastx || my!=data->draglasty) {
2771                                 if(ui_numedit_but_HSVCUBE(but, data, mx, my))
2772                                         ui_numedit_apply(C, block, but, data);
2773                         }
2774                 }
2775                 else if(event->type==LEFTMOUSE && event->val!=KM_PRESS)
2776                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2777                 
2778                 return WM_UI_HANDLER_BREAK;
2779         }
2780
2781         return WM_UI_HANDLER_CONTINUE;
2782 }
2783
2784 static int ui_numedit_but_HSVCIRCLE(uiBut *but, uiHandleButtonData *data, int mx, int my)
2785 {
2786         rcti rect;
2787         int changed= 1;
2788
2789         rect.xmin= but->x1; rect.xmax= but->x2;
2790         rect.ymin= but->y1; rect.ymax= but->y2;
2791         
2792         ui_hsvcircle_vals_from_pos(but->hsv, but->hsv+1, &rect, (float)mx, (float)my);
2793         
2794         ui_set_but_hsv(but);    // converts to rgb
2795         
2796         // update button values and strings
2797         // XXX ui_update_block_buts_hsv(but->block, but->hsv);
2798         
2799         data->draglastx= mx;
2800         data->draglasty= my;
2801         
2802         return changed;
2803 }
2804
2805
2806 static int ui_do_but_HSVCIRCLE(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2807 {
2808         int mx, my;
2809         
2810         mx= event->x;
2811         my= event->y;
2812         ui_window_to_block(data->region, block, &mx, &my);
2813         
2814         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2815                 if(event->type==LEFTMOUSE && event->val==KM_PRESS) {
2816                         data->dragstartx= mx;
2817                         data->dragstarty= my;
2818                         data->draglastx= mx;
2819                         data->draglasty= my;
2820                         button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2821                         
2822                         /* also do drag the first time */
2823                         if(ui_numedit_but_HSVCIRCLE(but, data, mx, my))
2824                                 ui_numedit_apply(C, block, but, data);
2825                         
2826                         return WM_UI_HANDLER_BREAK;
2827                 }
2828         }
2829         else if(data->state == BUTTON_STATE_NUM_EDITING) {
2830                 /* XXX hardcoded keymap check.... */
2831                 if(event->type == WHEELDOWNMOUSE) {
2832                         but->hsv[2]= CLAMPIS(but->hsv[2]-0.05f, 0.0f, 1.0f);
2833                         ui_set_but_hsv(but);    // converts to rgb
2834                         ui_numedit_apply(C, block, but, data);
2835                 }
2836                 else if(event->type == WHEELUPMOUSE) {
2837                         but->hsv[2]= CLAMPIS(but->hsv[2]+0.05f, 0.0f, 1.0f);
2838                         ui_set_but_hsv(but);    // converts to rgb
2839                         ui_numedit_apply(C, block, but, data);
2840                 }
2841                 else if(event->type == MOUSEMOVE) {
2842                         if(mx!=data->draglastx || my!=data->draglasty) {
2843                                 if(ui_numedit_but_HSVCIRCLE(but, data, mx, my))
2844                                         ui_numedit_apply(C, block, but, data);
2845                         }
2846                 }
2847                 else if(event->type==LEFTMOUSE && event->val!=KM_PRESS)
2848                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2849                 
2850                 return WM_UI_HANDLER_BREAK;
2851         }
2852         
2853         return WM_UI_HANDLER_CONTINUE;
2854 }
2855
2856
2857 static int verg_colorband(const void *a1, const void *a2)
2858 {
2859         const CBData *x1=a1, *x2=a2;
2860         
2861         if( x1->pos > x2->pos ) return 1;
2862         else if( x1->pos < x2->pos) return -1;
2863         return WM_UI_HANDLER_CONTINUE;
2864 }
2865
2866 static void ui_colorband_update(ColorBand *coba)
2867 {
2868         int a;
2869         
2870         if(coba->tot<2) return;
2871         
2872         for(a=0; a<coba->tot; a++) coba->data[a].cur= a;
2873                 qsort(coba->data, coba->tot, sizeof(CBData), verg_colorband);
2874         for(a=0; a<coba->tot; a++) {
2875                 if(coba->data[a].cur==coba->cur) {
2876                         coba->cur= a;
2877                         break;
2878                 }
2879         }
2880 }
2881
2882 static int ui_numedit_but_COLORBAND(uiBut *but, uiHandleButtonData *data, int mx)
2883 {
2884         float dx;
2885         int changed= 0;
2886
2887         if(data->draglastx == mx)
2888                 return changed;
2889
2890         dx= ((float)(mx - data->draglastx))/(but->x2-but->x1);
2891         data->dragcbd->pos += dx;
2892         CLAMP(data->dragcbd->pos, 0.0, 1.0);
2893         
2894         ui_colorband_update(data->coba);
2895         data->dragcbd= data->coba->data + data->coba->cur;      /* because qsort */
2896         
2897         data->draglastx= mx;
2898         changed= 1;
2899
2900         return changed;
2901 }
2902
2903 static int ui_do_but_COLORBAND(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2904 {
2905         ColorBand *coba;
2906         CBData *cbd;
2907         int mx, my, a, xco, mindist= 12;
2908
2909         mx= event->x;
2910         my= event->y;
2911         ui_window_to_block(data->region, block, &mx, &my);
2912
2913         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2914                 if(event->type==LEFTMOUSE && event->val==KM_PRESS) {
2915                         coba= (ColorBand*)but->poin;
2916
2917                         if(event->ctrl) {
2918                                 /* insert new key on mouse location */
2919                                 if(coba->tot < MAXCOLORBAND-1) {
2920                                         float pos= ((float)(mx - but->x1))/(but->x2-but->x1);
2921                                         float col[4];
2922                                         
2923                                         do_colorband(coba, pos, col);   /* executes it */
2924                                         
2925                                         coba->tot++;
2926                                         coba->cur= coba->tot-1;
2927                                         
2928                                         coba->data[coba->cur].r= col[0];
2929                                         coba->data[coba->cur].g= col[1];
2930                                         coba->data[coba->cur].b= col[2];
2931                                         coba->data[coba->cur].a= col[3];
2932                                         coba->data[coba->cur].pos= pos;
2933
2934                                         ui_colorband_update(coba);
2935                                 }
2936
2937                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2938                         }
2939                         else {
2940                                 data->dragstartx= mx;
2941                                 data->dragstarty= my;
2942                                 data->draglastx= mx;
2943                                 data->draglasty= my;
2944
2945                                 /* activate new key when mouse is close */
2946                                 for(a=0, cbd= coba->data; a<coba->tot; a++, cbd++) {
2947                                         xco= but->x1 + (cbd->pos*(but->x2-but->x1));
2948                                         xco= ABS(xco-mx);
2949                                         if(a==coba->cur) xco+= 5; // selected one disadvantage
2950                                         if(xco<mindist) {
2951                                                 coba->cur= a;
2952                                                 mindist= xco;
2953                                         }
2954                                 }
2955                 
2956                                 data->dragcbd= coba->data + coba->cur;
2957                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2958                         }
2959
2960                         return WM_UI_HANDLER_BREAK;
2961                 }
2962         }
2963         else if(data->state == BUTTON_STATE_NUM_EDITING) {
2964                 if(event->type == MOUSEMOVE) {
2965                         if(mx!=data->draglastx || my!=data->draglasty) {
2966                                 if(ui_numedit_but_COLORBAND(but, data, mx))
2967                                         ui_numedit_apply(C, block, but, data);
2968                         }
2969                 }
2970                 else if(event->type==LEFTMOUSE && event->val!=KM_PRESS)
2971                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2972                 
2973                 return WM_UI_HANDLER_BREAK;
2974         }
2975
2976         return WM_UI_HANDLER_CONTINUE;
2977 }
2978
2979 static int ui_numedit_but_CURVE(uiBut *but, uiHandleButtonData *data, int snap, int mx, int my)
2980 {
2981         CurveMapping *cumap= data->cumap;
2982         CurveMap *cuma= cumap->cm+cumap->cur;
2983         CurveMapPoint *cmp= cuma->curve;
2984         float fx, fy, zoomx, zoomy, offsx, offsy;
2985         int a, changed= 0;
2986
2987         zoomx= (but->x2-but->x1)/(cumap->curr.xmax-cumap->curr.xmin);
2988         zoomy= (but->y2-but->y1)/(cumap->curr.ymax-cumap->curr.ymin);