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