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