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 LINK:
844                 case INLINK:
845                         ui_apply_but_LINK(C, but, data);
846                         break;
847                 default:
848                         break;
849         }
850
851         but->editstr= editstr;
852         but->editval= editval;
853         but->editvec= editvec;
854         but->editcoba= editcoba;
855         but->editcumap= editcumap;
856 }
857
858 /* ******************* copy and paste ********************  */
859
860 /* c = copy, v = paste */
861 static void ui_but_copy_paste(bContext *C, uiBut *but, uiHandleButtonData *data, char mode)
862 {
863         static ColorBand but_copypaste_coba = {0};
864         char buf[UI_MAX_DRAW_STR+1]= {0};
865         double val;
866         
867         if(mode=='v' && but->lock)
868                 return;
869
870         if(mode=='v') {
871                 /* extract first line from clipboard in case of multi-line copies */
872                 char *p, *pbuf= WM_clipboard_text_get(0);
873                 p= pbuf;
874                 if(p) {
875                         int i = 0;
876                         while (*p && *p!='\r' && *p!='\n' && i<UI_MAX_DRAW_STR) {
877                                 buf[i++]=*p;
878                                 p++;
879                         }
880                         buf[i]= 0;
881                         MEM_freeN(pbuf);
882                 }
883         }
884         
885         /* numeric value */
886         if ELEM4(but->type, NUM, NUMABS, NUMSLI, HSVSLI) {
887                 
888                 if(but->poin==NULL && but->rnapoin.data==NULL);
889                 else if(mode=='c') {
890                         sprintf(buf, "%f", ui_get_but_val(but));
891                         WM_clipboard_text_set(buf, 0);
892                 }
893                 else {
894                         if (sscanf(buf, " %lf ", &val) == 1) {
895                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
896                                 data->value= val;
897                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
898                         }
899                 }
900         }
901
902         /* RGB triple */
903         else if(but->type==COL) {
904                 float rgb[3];
905                 
906                 if(but->poin==NULL && but->rnapoin.data==NULL);
907                 else if(mode=='c') {
908
909                         ui_get_but_vectorf(but, rgb);
910                         sprintf(buf, "[%f, %f, %f]", rgb[0], rgb[1], rgb[2]);
911                         WM_clipboard_text_set(buf, 0);
912                         
913                 }
914                 else {
915                         if (sscanf(buf, "[%f, %f, %f]", &rgb[0], &rgb[1], &rgb[2]) == 3) {
916                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
917                                 VECCOPY(data->vec, rgb);
918                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
919                         }
920                 }
921         }
922
923         /* text/string and ID data */
924         else if(ELEM(but->type, TEX, IDPOIN)) {
925                 uiHandleButtonData *data= but->active;
926
927                 if(but->poin==NULL && but->rnapoin.data==NULL);
928                 else if(mode=='c') {
929                         button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
930                         BLI_strncpy(buf, data->str, UI_MAX_DRAW_STR);
931                         WM_clipboard_text_set(data->str, 0);
932                         data->cancel= 1;
933                         button_activate_state(C, but, BUTTON_STATE_EXIT);
934                 }
935                 else {
936                         button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
937                         BLI_strncpy(data->str, buf, data->maxlen);
938                         button_activate_state(C, but, BUTTON_STATE_EXIT);
939                 }
940         }
941         /* colorband (not supported by system clipboard) */
942         else if(but->type==BUT_COLORBAND) {
943                 if(mode=='c') {
944                         if(but->poin)
945                                 return;
946
947                         memcpy(&but_copypaste_coba, but->poin, sizeof(ColorBand));
948                 }
949                 else {
950                         if(but_copypaste_coba.tot==0)
951                                 return;
952
953                         if(!but->poin)
954                                 but->poin= MEM_callocN(sizeof(ColorBand), "colorband");
955
956                         button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
957                         memcpy(data->coba, &but_copypaste_coba, sizeof(ColorBand) );
958                         button_activate_state(C, but, BUTTON_STATE_EXIT);
959                 }
960         }
961 }
962
963 /* ************* in-button text selection/editing ************* */
964
965 /* return 1 if char ch is special character, otherwise return 0 */
966 static short test_special_char(char ch)
967 {
968         switch(ch) {
969                 case '\\':
970                 case '/':
971                 case '~':
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                         return 1;
1001                         break;
1002                 default:
1003                         break;
1004         }
1005         return 0;
1006 }
1007
1008 static int ui_textedit_delete_selection(uiBut *but, uiHandleButtonData *data)
1009 {
1010         char *str;
1011         int x, changed;
1012         
1013         str= data->str;
1014         changed= (but->selsta != but->selend);
1015         
1016         for(x=0; x< strlen(str); x++) {
1017                 if (but->selend + x <= strlen(str) ) {
1018                         str[but->selsta + x]= str[but->selend + x];
1019                 } else {
1020                         str[but->selsta + x]= '\0';
1021                         break;
1022                 }
1023         }
1024
1025         but->pos = but->selend = but->selsta;
1026
1027         return changed;
1028 }
1029
1030 static void ui_textedit_set_cursor_pos(uiBut *but, uiHandleButtonData *data, short x)
1031 {
1032         uiStyle *style= U.uistyles.first;       // XXX pass on as arg
1033         int startx= but->x1;
1034         char *origstr;
1035
1036         uiStyleFontSet(&style->widget);
1037
1038         origstr= MEM_callocN(sizeof(char)*(data->maxlen+1), "ui_textedit origstr");
1039         
1040         BLI_strncpy(origstr, but->drawstr, data->maxlen+1);
1041         but->pos= strlen(origstr)-but->ofs;
1042         
1043         /* XXX solve generic */
1044         if(but->type==NUM || but->type==NUMSLI)
1045                 startx += 20;
1046         
1047         while((BLF_width(origstr+but->ofs) + startx) > x) {
1048                 if (but->pos <= 0) break;
1049                 but->pos--;
1050                 origstr[but->pos+but->ofs] = 0;
1051         }
1052         
1053         but->pos -= strlen(but->str);
1054         but->pos += but->ofs;
1055         if(but->pos<0) but->pos= 0;
1056
1057         MEM_freeN(origstr);
1058 }
1059
1060 static void ui_textedit_set_cursor_select(uiBut *but, uiHandleButtonData *data, short x)
1061 {
1062         if (x > data->selstartx) data->selextend = EXTEND_RIGHT;
1063         else if (x < data->selstartx) data->selextend = EXTEND_LEFT;
1064
1065         ui_textedit_set_cursor_pos(but, data, x);
1066                                                 
1067         if (data->selextend == EXTEND_RIGHT) but->selend = but->pos;
1068         if (data->selextend == EXTEND_LEFT) but->selsta = but->pos;
1069
1070         ui_check_but(but);
1071 }
1072
1073 static int ui_textedit_type_ascii(uiBut *but, uiHandleButtonData *data, char ascii)
1074 {
1075         char *str;
1076         int len, x, changed= 0;
1077
1078         str= data->str;
1079         len= strlen(str);
1080
1081         if(len-(but->selend - but->selsta)+1 <= data->maxlen) {
1082                 /* type over the current selection */
1083                 if ((but->selend - but->selsta) > 0)
1084                         changed= ui_textedit_delete_selection(but, data);
1085
1086                 len= strlen(str);
1087                 if(len < data->maxlen) {
1088                         for(x= data->maxlen; x>but->pos; x--)
1089                                 str[x]= str[x-1];
1090                         str[but->pos]= ascii;
1091                         str[len+1]= '\0';
1092
1093                         but->pos++; 
1094                         changed= 1;
1095                 }
1096         }
1097
1098         return changed;
1099 }
1100
1101 void ui_textedit_move(uiBut *but, uiHandleButtonData *data, int direction, int select, int jump)
1102 {
1103         char *str;
1104         int len;
1105
1106         str= data->str;
1107         len= strlen(str);
1108
1109         if(direction) { /* right*/
1110                 /* if there's a selection */
1111                 if ((but->selend - but->selsta) > 0) {
1112                         /* extend the selection based on the first direction taken */
1113                         if(select) {
1114                                 if (!data->selextend) {
1115                                         data->selextend = EXTEND_RIGHT;
1116                                 }
1117                                 if (data->selextend == EXTEND_RIGHT) {
1118                                         but->selend++;
1119                                         if (but->selend > len) but->selend = len;
1120                                 } else if (data->selextend == EXTEND_LEFT) {
1121                                         but->selsta++;
1122                                         /* if the selection start has gone past the end,
1123                                         * flip them so they're in sync again */
1124                                         if (but->selsta == but->selend) {
1125                                                 but->pos = but->selsta;
1126                                                 data->selextend = EXTEND_RIGHT;
1127                                         }
1128                                 }
1129                         } else {
1130                                 but->selsta = but->pos = but->selend;
1131                                 data->selextend = 0;
1132                         }
1133                 } else {
1134                         if(select) {
1135                                 /* make a selection, starting from the cursor position */
1136                                 but->selsta = but->pos;
1137                                 
1138                                 but->pos++;
1139                                 if(but->pos>strlen(str)) but->pos= strlen(str);
1140                                 
1141                                 but->selend = but->pos;
1142                         } else if(jump) {
1143                                 /* jump betweenn special characters (/,\,_,-, etc.),
1144                                  * look at function test_special_char() for complete
1145                                  * list of special character, ctr -> */
1146                                 while(but->pos < len) {
1147                                         but->pos++;
1148                                         if(test_special_char(str[but->pos])) break;
1149                                 }
1150                         } else {
1151                                 but->pos++;
1152                                 if(but->pos>strlen(str)) but->pos= strlen(str);
1153                         }
1154                 }
1155         }
1156         else { /* left */
1157                 /* if there's a selection */
1158                 if ((but->selend - but->selsta) > 0) {
1159                         /* extend the selection based on the first direction taken */
1160                         if(select) {
1161                                 if (!data->selextend) {
1162                                         data->selextend = EXTEND_LEFT;
1163                                 }
1164                                 if (data->selextend == EXTEND_LEFT) {
1165                                         but->selsta--;
1166                                         if (but->selsta < 0) but->selsta = 0;
1167                                 } else if (data->selextend == EXTEND_RIGHT) {
1168                                         but->selend--;
1169                                         /* if the selection start has gone past the end,
1170                                         * flip them so they're in sync again */
1171                                         if (but->selsta == but->selend) {
1172                                                 but->pos = but->selsta;
1173                                                 data->selextend = EXTEND_LEFT;
1174                                         }
1175                                 }
1176                         } else {
1177                                 but->pos = but->selend = but->selsta;
1178                                 data->selextend = 0;
1179                         }
1180                 } else {
1181                         if(select) {
1182                                 /* make a selection, starting from the cursor position */
1183                                 but->selend = but->pos;
1184                                 
1185                                 but->pos--;
1186                                 if(but->pos<0) but->pos= 0;
1187                                 
1188                                 but->selsta = but->pos;
1189                         } else if(jump) {
1190                                 /* jump betweenn special characters (/,\,_,-, etc.),
1191                                  * look at function test_special_char() for complete
1192                                  * list of special character, ctr -> */
1193                                 while(but->pos > 0){
1194                                         but->pos--;
1195                                         if(test_special_char(str[but->pos])) break;
1196                                 }
1197                         } else {
1198                                 if(but->pos>0) but->pos--;
1199                         }
1200                 }
1201         }
1202 }
1203
1204 void ui_textedit_move_end(uiBut *but, uiHandleButtonData *data, int direction, int select)
1205 {
1206         char *str;
1207
1208         str= data->str;
1209
1210         if(direction) { /* right */
1211                 if(select) {
1212                         but->selsta = but->pos;
1213                         but->selend = strlen(str);
1214                         data->selextend = EXTEND_RIGHT;
1215                 } else {
1216                         but->selsta = but->selend = but->pos= strlen(str);
1217                 }
1218         }
1219         else { /* left */
1220                 if(select) {
1221                         but->selend = but->pos;
1222                         but->selsta = 0;
1223                         data->selextend = EXTEND_LEFT;
1224                 } else {
1225                         but->selsta = but->selend = but->pos= 0;
1226                 }
1227         }
1228 }
1229
1230 static int ui_textedit_delete(uiBut *but, uiHandleButtonData *data, int direction, int all)
1231 {
1232         char *str;
1233         int len, x, changed= 0;
1234
1235         str= data->str;
1236         len= strlen(str);
1237
1238         if(all) {
1239                 if(len) changed=1;
1240                 str[0]= 0;
1241                 but->pos= 0;
1242         }
1243         else if(direction) { /* delete */
1244                 if ((but->selend - but->selsta) > 0) {
1245                         changed= ui_textedit_delete_selection(but, data);
1246                 }
1247                 else if(but->pos>=0 && but->pos<len) {
1248                         for(x=but->pos; x<len; x++)
1249                                 str[x]= str[x+1];
1250                         str[len-1]='\0';
1251                         changed= 1;
1252                 }
1253         }
1254         else { /* backspace */
1255                 if(len!=0) {
1256                         if ((but->selend - but->selsta) > 0) {
1257                                 changed= ui_textedit_delete_selection(but, data);
1258                         }
1259                         else if(but->pos>0) {
1260                                 for(x=but->pos; x<len; x++)
1261                                         str[x-1]= str[x];
1262                                 str[len-1]='\0';
1263
1264                                 but->pos--;
1265                                 changed= 1;
1266                         }
1267                 } 
1268         }
1269
1270         return changed;
1271 }
1272
1273 static int ui_textedit_autocomplete(bContext *C, uiBut *but, uiHandleButtonData *data)
1274 {
1275         char *str;
1276         int changed= 1;
1277
1278         str= data->str;
1279
1280         if(data->searchbox)
1281                 ui_searchbox_autocomplete(C, data->searchbox, but, data->str);
1282         else
1283                 but->autocomplete_func(C, str, but->autofunc_arg);
1284
1285         but->pos= strlen(str);
1286         but->selsta= but->selend= but->pos;
1287
1288         return changed;
1289 }
1290
1291 static int ui_textedit_copypaste(uiBut *but, uiHandleButtonData *data, int paste, int copy, int cut)
1292 {
1293         char buf[UI_MAX_DRAW_STR]={0};
1294         char *str, *p, *pbuf;
1295         int len, x, y, i, changed= 0;
1296
1297         str= data->str;
1298         len= strlen(str);
1299         
1300         /* paste */
1301         if (paste) {
1302                 /* extract the first line from the clipboard */
1303                 p = pbuf= WM_clipboard_text_get(0);
1304
1305                 if(p && p[0]) {
1306                         i= 0;
1307                         while (*p && *p!='\r' && *p!='\n' && i<UI_MAX_DRAW_STR-1) {
1308                                 buf[i++]=*p;
1309                                 p++;
1310                         }
1311                         buf[i]= 0;
1312
1313                         /* paste over the current selection */
1314                         if ((but->selend - but->selsta) > 0)
1315                                 ui_textedit_delete_selection(but, data);
1316                         
1317                         for (y=0; y<strlen(buf); y++)
1318                         {
1319                                 /* add contents of buffer */
1320                                 if(len < data->maxlen) {
1321                                         for(x= data->maxlen; x>but->pos; x--)
1322                                                 str[x]= str[x-1];
1323                                         str[but->pos]= buf[y];
1324                                         but->pos++; 
1325                                         len++;
1326                                         str[len]= '\0';
1327                                 }
1328                         }
1329
1330                         changed= 1;
1331                 }
1332
1333                 if(pbuf)
1334                         MEM_freeN(pbuf);
1335         }
1336         /* cut & copy */
1337         else if (copy || cut) {
1338                 /* copy the contents to the copypaste buffer */
1339                 for(x= but->selsta; x <= but->selend; x++) {
1340                         if (x==but->selend)
1341                                 buf[x] = '\0';
1342                         else
1343                                 buf[(x - but->selsta)] = str[x];
1344                 }
1345
1346                 WM_clipboard_text_set(buf, 0);
1347                 
1348                 /* for cut only, delete the selection afterwards */
1349                 if(cut)
1350                         if((but->selend - but->selsta) > 0)
1351                                 changed= ui_textedit_delete_selection(but, data);
1352         } 
1353
1354         return changed;
1355 }
1356
1357 static void ui_textedit_begin(bContext *C, uiBut *but, uiHandleButtonData *data)
1358 {
1359         if(data->str) {
1360                 MEM_freeN(data->str);
1361                 data->str= NULL;
1362         }
1363
1364         /* retrieve string */
1365         data->maxlen= ui_get_but_string_max_length(but);
1366         data->str= MEM_callocN(sizeof(char)*(data->maxlen+1), "textedit str");
1367         ui_get_but_string(but, data->str, data->maxlen+1);
1368
1369         data->origstr= BLI_strdup(data->str);
1370         data->selextend= 0;
1371         data->selstartx= 0;
1372
1373         /* set cursor pos to the end of the text */
1374         but->editstr= data->str;
1375         but->pos= strlen(data->str);
1376         but->selsta= 0;
1377         but->selend= strlen(data->str);
1378
1379         /* optional searchbox */
1380         if(but->type==SEARCH_MENU) {
1381                 data->searchbox= ui_searchbox_create(C, data->region, but);
1382                 ui_searchbox_update(C, data->searchbox, but, 1); /* 1= reset */
1383         }
1384         
1385         ui_check_but(but);
1386 }
1387
1388 static void ui_textedit_end(bContext *C, uiBut *but, uiHandleButtonData *data)
1389 {
1390         if(but) {
1391                 if(data->searchbox) {
1392                         if(data->cancel==0)
1393                                 ui_searchbox_apply(but, data->searchbox);
1394
1395                         ui_searchbox_free(C, data->searchbox);
1396                         data->searchbox= NULL;
1397                 }
1398                 
1399                 but->editstr= NULL;
1400                 but->pos= -1;
1401         }
1402 }
1403
1404 static void ui_textedit_next_but(uiBlock *block, uiBut *actbut, uiHandleButtonData *data)
1405 {
1406         uiBut *but;
1407
1408         /* label and roundbox can overlap real buttons (backdrops...) */
1409         if(ELEM4(actbut->type, LABEL, SEPR, ROUNDBOX, LISTBOX))
1410                 return;
1411
1412         for(but= actbut->next; but; but= but->next) {
1413                 if(ELEM7(but->type, TEX, NUM, NUMABS, NUMSLI, HSVSLI, IDPOIN, SEARCH_MENU)) {
1414                         data->postbut= but;
1415                         data->posttype= BUTTON_ACTIVATE_TEXT_EDITING;
1416                         return;
1417                 }
1418         }
1419         for(but= block->buttons.first; but!=actbut; but= but->next) {
1420                 if(ELEM7(but->type, TEX, NUM, NUMABS, NUMSLI, HSVSLI, IDPOIN, SEARCH_MENU)) {
1421                         data->postbut= but;
1422                         data->posttype= BUTTON_ACTIVATE_TEXT_EDITING;
1423                         return;
1424                 }
1425         }
1426 }
1427
1428 static void ui_textedit_prev_but(uiBlock *block, uiBut *actbut, uiHandleButtonData *data)
1429 {
1430         uiBut *but;
1431
1432         /* label and roundbox can overlap real buttons (backdrops...) */
1433         if(ELEM4(actbut->type, LABEL, SEPR, ROUNDBOX, LISTBOX))
1434                 return;
1435
1436         for(but= actbut->prev; but; but= but->prev) {
1437                 if(ELEM7(but->type, TEX, NUM, NUMABS, NUMSLI, HSVSLI, IDPOIN, SEARCH_MENU)) {
1438                         data->postbut= but;
1439                         data->posttype= BUTTON_ACTIVATE_TEXT_EDITING;
1440                         return;
1441                 }
1442         }
1443         for(but= block->buttons.last; but!=actbut; but= but->prev) {
1444                 if(ELEM7(but->type, TEX, NUM, NUMABS, NUMSLI, HSVSLI, IDPOIN, SEARCH_MENU)) {
1445                         data->postbut= but;
1446                         data->posttype= BUTTON_ACTIVATE_TEXT_EDITING;
1447                         return;
1448                 }
1449         }
1450 }
1451
1452
1453 static void ui_do_but_textedit(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
1454 {
1455         int mx, my, changed= 0, inbox=0, update= 0, retval= WM_UI_HANDLER_CONTINUE;
1456
1457         switch(event->type) {
1458                 case WHEELUPMOUSE:
1459                 case WHEELDOWNMOUSE:
1460                 case MOUSEMOVE:
1461                         if(data->searchbox)
1462                                 ui_searchbox_event(C, data->searchbox, but, event);
1463                         
1464                         break;
1465                 case RIGHTMOUSE:
1466                 case ESCKEY:
1467                         data->cancel= 1;
1468                         data->escapecancel= 1;
1469                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1470                         retval= WM_UI_HANDLER_BREAK;
1471                         break;
1472                 case LEFTMOUSE: {
1473                         
1474                         /* exit on LMB only on RELEASE for searchbox, to mimic other popups, and allow multiple menu levels */
1475                         if(data->searchbox)
1476                                 inbox= ui_searchbox_inside(data->searchbox, event->x, event->y);
1477
1478                         if(event->val==KM_PRESS) {
1479                                 mx= event->x;
1480                                 my= event->y;
1481                                 ui_window_to_block(data->region, block, &mx, &my);
1482
1483                                 if ((but->y1 <= my) && (my <= but->y2) && (but->x1 <= mx) && (mx <= but->x2)) {
1484                                         ui_textedit_set_cursor_pos(but, data, mx);
1485                                         but->selsta = but->selend = but->pos;
1486                                         data->selstartx= mx;
1487
1488                                         button_activate_state(C, but, BUTTON_STATE_TEXT_SELECTING);
1489                                         retval= WM_UI_HANDLER_BREAK;
1490                                 }
1491                                 else if(inbox==0) {
1492                                         /* if searchbox, click outside will cancel */
1493                                         if(data->searchbox)
1494                                                 data->cancel= data->escapecancel= 1;
1495                                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1496                                         retval= WM_UI_HANDLER_BREAK;
1497                                 }
1498                         }
1499                         else if(inbox) {
1500                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
1501                                 retval= WM_UI_HANDLER_BREAK;
1502                         }
1503                         break;
1504                 }
1505         }
1506
1507         if(event->val==KM_PRESS) {
1508                 switch (event->type) {
1509                         case VKEY:
1510                         case XKEY:
1511                         case CKEY:
1512                                 if(event->ctrl || event->oskey) {
1513                                         if(event->type == VKEY)
1514                                                 changed= ui_textedit_copypaste(but, data, 1, 0, 0);
1515                                         else if(event->type == CKEY)
1516                                                 changed= ui_textedit_copypaste(but, data, 0, 1, 0);
1517                                         else if(event->type == XKEY)
1518                                                 changed= ui_textedit_copypaste(but, data, 0, 0, 1);
1519
1520                                         retval= WM_UI_HANDLER_BREAK;
1521                                 }
1522                                 break;
1523                         case RIGHTARROWKEY:
1524                                 ui_textedit_move(but, data, 1, event->shift, event->ctrl);
1525                                 retval= WM_UI_HANDLER_BREAK;
1526                                 break;
1527                         case LEFTARROWKEY:
1528                                 ui_textedit_move(but, data, 0, event->shift, event->ctrl);
1529                                 retval= WM_UI_HANDLER_BREAK;
1530                                 break;
1531                         case DOWNARROWKEY:
1532                                 if(data->searchbox) {
1533                                         ui_searchbox_event(C, data->searchbox, but, event);
1534                                         break;
1535                                 }
1536                                 /* pass on purposedly */
1537                         case ENDKEY:
1538                                 ui_textedit_move_end(but, data, 1, event->shift);
1539                                 retval= WM_UI_HANDLER_BREAK;
1540                                 break;
1541                         case UPARROWKEY:
1542                                 if(data->searchbox) {
1543                                         ui_searchbox_event(C, data->searchbox, but, event);
1544                                         break;
1545                                 }
1546                                 /* pass on purposedly */
1547                         case HOMEKEY:
1548                                 ui_textedit_move_end(but, data, 0, event->shift);
1549                                 retval= WM_UI_HANDLER_BREAK;
1550                                 break;
1551                         case PADENTER:
1552                         case RETKEY:
1553                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
1554                                 retval= WM_UI_HANDLER_BREAK;
1555                                 break;
1556                         case DELKEY:
1557                                 changed= ui_textedit_delete(but, data, 1, 0);
1558                                 retval= WM_UI_HANDLER_BREAK;
1559                                 break;
1560
1561                         case BACKSPACEKEY:
1562                                 changed= ui_textedit_delete(but, data, 0, event->shift);
1563                                 retval= WM_UI_HANDLER_BREAK;
1564                                 break;
1565                                 
1566                         case TABKEY:
1567                                 /* there is a key conflict here, we can't tab with autocomplete */
1568                                 if(but->autocomplete_func || data->searchbox) {
1569                                         changed= ui_textedit_autocomplete(C, but, data);
1570                                         update= 1; /* do live update for tab key */
1571                                         retval= WM_UI_HANDLER_BREAK;
1572                                 }
1573                                 /* the hotkey here is not well defined, was G.qual so we check all */
1574                                 else if(event->shift || event->ctrl || event->alt || event->oskey) {
1575                                         ui_textedit_prev_but(block, but, data);
1576                                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1577                                 }
1578                                 else {
1579                                         ui_textedit_next_but(block, but, data);
1580                                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1581                                 }
1582                                 retval= WM_UI_HANDLER_BREAK;
1583                                 break;
1584                 }
1585
1586                 if(event->ascii && (retval == WM_UI_HANDLER_CONTINUE)) {
1587                         changed= ui_textedit_type_ascii(but, data, event->ascii);
1588                         retval= WM_UI_HANDLER_BREAK;
1589                 }
1590         }
1591
1592         if(changed) {
1593                 /* never update while typing for now */
1594                 if(update && data->interactive) ui_apply_button(C, block, but, data, 1);
1595                 else ui_check_but(but);
1596                 
1597                 if(data->searchbox)
1598                         ui_searchbox_update(C, data->searchbox, but, 1); /* 1 = reset */
1599         }
1600
1601         if(changed || (retval == WM_UI_HANDLER_BREAK))
1602                 ED_region_tag_redraw(data->region);
1603 }
1604
1605 static void ui_do_but_textedit_select(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
1606 {
1607         int mx, my, retval= WM_UI_HANDLER_CONTINUE;
1608
1609         switch(event->type) {
1610                 case MOUSEMOVE: {
1611                         mx= event->x;
1612                         my= event->y;
1613                         ui_window_to_block(data->region, block, &mx, &my);
1614
1615                         ui_textedit_set_cursor_select(but, data, mx);
1616                         retval= WM_UI_HANDLER_BREAK;
1617                         break;
1618                 }
1619                 case LEFTMOUSE:
1620                         if(event->val == 0)
1621                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
1622                         retval= WM_UI_HANDLER_BREAK;
1623                         break;
1624         }
1625
1626         if(retval == WM_UI_HANDLER_BREAK) {
1627                 ui_check_but(but);
1628                 ED_region_tag_redraw(data->region);
1629         }
1630 }
1631
1632 /* ************* number editing for various types ************* */
1633
1634 static void ui_numedit_begin(uiBut *but, uiHandleButtonData *data)
1635 {
1636         float softrange, softmin, softmax;
1637
1638         if(but->type == BUT_CURVE) {
1639                 data->cumap= (CurveMapping*)but->poin;
1640                 but->editcumap= data->coba;
1641         }
1642         else if(but->type == BUT_COLORBAND) {
1643                 data->coba= (ColorBand*)but->poin;
1644                 but->editcoba= data->coba;
1645         }
1646         else if(ELEM3(but->type, BUT_NORMAL, HSVCUBE, HSVCIRCLE)) {
1647                 ui_get_but_vectorf(but, data->origvec);
1648                 VECCOPY(data->vec, data->origvec);
1649                 but->editvec= data->vec;
1650         }
1651         else {
1652                 data->startvalue= ui_get_but_val(but);
1653                 data->origvalue= data->startvalue;
1654                 data->value= data->origvalue;
1655                 but->editval= &data->value;
1656
1657                 softmin= but->softmin;
1658                 softmax= but->softmax;
1659                 softrange= softmax - softmin;
1660
1661                 data->dragfstart= (softrange == 0.0)? 0.0: (data->value - softmin)/softrange;
1662                 data->dragf= data->dragfstart;
1663         }
1664
1665         data->dragchange= 0;
1666         data->draglock= 1;
1667 }
1668
1669 static void ui_numedit_end(uiBut *but, uiHandleButtonData *data)
1670 {
1671         but->editval= NULL;
1672         but->editvec= NULL;
1673         but->editcoba= NULL;
1674         but->editcumap= NULL;
1675
1676         data->dragstartx= 0;
1677         data->draglastx= 0;
1678         data->dragchange= 0;
1679         data->dragcbd= NULL;
1680         data->dragsel= 0;
1681 }
1682
1683 static void ui_numedit_apply(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data)
1684 {
1685         if(data->interactive) ui_apply_button(C, block, but, data, 1);
1686         else ui_check_but(but);
1687
1688         ED_region_tag_redraw(data->region);
1689 }
1690
1691 /* ****************** menu opening for various types **************** */
1692
1693 static void ui_blockopen_begin(bContext *C, uiBut *but, uiHandleButtonData *data)
1694 {
1695         uiBlockCreateFunc func= NULL;
1696         uiBlockHandleCreateFunc handlefunc= NULL;
1697         uiMenuCreateFunc menufunc= NULL;
1698         void *arg= NULL;
1699
1700         switch(but->type) {
1701                 case BLOCK:
1702                 case PULLDOWN:
1703                         if(but->menu_create_func) {
1704                                 menufunc= but->menu_create_func;
1705                                 arg= but->poin;
1706                         }
1707                         else {
1708                                 func= but->block_create_func;
1709                                 arg= but->poin?but->poin:but->func_argN;
1710                         }
1711                         break;
1712                 case MENU:
1713                         if(but->menu_create_func) {
1714                                 menufunc= but->menu_create_func;
1715                                 arg= but->poin;
1716                         }
1717                         else {
1718                                 data->origvalue= ui_get_but_val(but);
1719                                 data->value= data->origvalue;
1720                                 but->editval= &data->value;
1721
1722                                 handlefunc= ui_block_func_MENU;
1723                                 arg= but;
1724                         }
1725                         break;
1726                 case ICONROW:
1727                         handlefunc= ui_block_func_ICONROW;
1728                         arg= but;
1729                         break;
1730                 case ICONTEXTROW:
1731                         handlefunc= ui_block_func_ICONTEXTROW;
1732                         arg= but;
1733                         break;
1734                 case COL:
1735                         ui_get_but_vectorf(but, data->origvec);
1736                         VECCOPY(data->vec, data->origvec);
1737                         but->editvec= data->vec;
1738
1739                         handlefunc= ui_block_func_COL;
1740                         arg= but;
1741                         break;
1742         }
1743
1744         if(func || handlefunc) {
1745                 data->menu= ui_popup_block_create(C, data->region, but, func, handlefunc, arg);
1746                 if(but->block->handle)
1747                         data->menu->popup= but->block->handle->popup;
1748         }
1749         else if(menufunc) {
1750                 data->menu= ui_popup_menu_create(C, data->region, but, menufunc, arg);
1751                 if(but->block->handle)
1752                         data->menu->popup= but->block->handle->popup;
1753         }
1754
1755         /* this makes adjacent blocks auto open from now on */
1756         //if(but->block->auto_open==0) but->block->auto_open= 1;
1757 }
1758
1759 static void ui_blockopen_end(bContext *C, uiBut *but, uiHandleButtonData *data)
1760 {
1761         if(but) {
1762                 but->editval= NULL;
1763                 but->editvec= NULL;
1764
1765                 but->block->auto_open_last= PIL_check_seconds_timer();
1766         }
1767
1768         if(data->menu) {
1769                 ui_popup_block_free(C, data->menu);
1770                 data->menu= NULL;
1771         }
1772 }
1773
1774 /* ***************** events for different button types *************** */
1775
1776 static int ui_do_but_BUT(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
1777 {
1778         if(data->state == BUTTON_STATE_HIGHLIGHT) {
1779                 if(event->type == LEFTMOUSE && event->val==KM_PRESS) {
1780                         button_activate_state(C, but, BUTTON_STATE_WAIT_RELEASE);
1781                         return WM_UI_HANDLER_BREAK;
1782                 }
1783                 else if(event->type == LEFTMOUSE && but->block->handle) {
1784                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1785                         return WM_UI_HANDLER_BREAK;
1786                 }
1787                 else if(ELEM(event->type, PADENTER, RETKEY) && event->val==KM_PRESS) {
1788                         button_activate_state(C, but, BUTTON_STATE_WAIT_FLASH);
1789                         return WM_UI_HANDLER_BREAK;
1790                 }
1791         }
1792         else if(data->state == BUTTON_STATE_WAIT_RELEASE) {
1793                 if(event->type == LEFTMOUSE && event->val!=KM_PRESS) {
1794                         if(!(but->flag & UI_SELECT))
1795                                 data->cancel= 1;
1796                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1797                         return WM_UI_HANDLER_BREAK;
1798                 }
1799         }
1800
1801         return WM_UI_HANDLER_CONTINUE;
1802 }
1803
1804 static int ui_do_but_KEYEVT(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
1805 {
1806         if(data->state == BUTTON_STATE_HIGHLIGHT) {
1807                 if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val==KM_PRESS) {
1808                         button_activate_state(C, but, BUTTON_STATE_WAIT_KEY_EVENT);
1809                         return WM_UI_HANDLER_BREAK;
1810                 }
1811         }
1812         else if(data->state == BUTTON_STATE_WAIT_KEY_EVENT) {
1813                 if(event->type == MOUSEMOVE)
1814                         return WM_UI_HANDLER_CONTINUE;
1815
1816                 if(event->val==KM_PRESS) {
1817                         if(WM_key_event_string(event->type)[0])
1818                                 ui_set_but_val(but, event->type);
1819                         else
1820                                 data->cancel= 1;
1821
1822                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1823                 }
1824         }
1825
1826         return WM_UI_HANDLER_CONTINUE;
1827 }
1828
1829 static int ui_do_but_TEX(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
1830 {
1831         if(data->state == BUTTON_STATE_HIGHLIGHT) {
1832                 if(ELEM4(event->type, LEFTMOUSE, PADENTER, RETKEY, EVT_BUT_OPEN) && event->val==KM_PRESS) {
1833                         button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
1834                         return WM_UI_HANDLER_BREAK;
1835                 }
1836         }
1837         else if(data->state == BUTTON_STATE_TEXT_EDITING) {
1838                 ui_do_but_textedit(C, block, but, data, event);
1839                 return WM_UI_HANDLER_BREAK;
1840         }
1841         else if(data->state == BUTTON_STATE_TEXT_SELECTING) {
1842                 ui_do_but_textedit_select(C, block, but, data, event);
1843                 return WM_UI_HANDLER_BREAK;
1844         }
1845
1846         return WM_UI_HANDLER_CONTINUE;
1847 }
1848
1849 static int ui_do_but_TOG(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
1850 {
1851         if(data->state == BUTTON_STATE_HIGHLIGHT) {
1852                 if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val==KM_PRESS) {
1853                         data->togdual= event->ctrl;
1854                         data->togonly= !event->shift;
1855                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1856                         return WM_UI_HANDLER_BREAK;
1857                 }
1858         }
1859         return WM_UI_HANDLER_CONTINUE;
1860 }
1861
1862 static int ui_do_but_EXIT(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
1863 {
1864         if(data->state == BUTTON_STATE_HIGHLIGHT) {
1865                 if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val==KM_PRESS) {
1866                         button_activate_state(C, but, BUTTON_STATE_EXIT);
1867                         return WM_UI_HANDLER_BREAK;
1868                 }
1869         }
1870
1871         return WM_UI_HANDLER_CONTINUE;
1872 }
1873
1874 static int ui_numedit_but_NUM(uiBut *but, uiHandleButtonData *data, float fac, int snap, int mx)
1875 {
1876         float deler, tempf, softmin, softmax, softrange;
1877         int lvalue, temp, changed= 0;
1878         
1879         if(mx == data->draglastx)
1880                 return changed;
1881         
1882         /* drag-lock - prevent unwanted scroll adjustments */
1883         /* change value (now 3) to adjust threshold in pixels */
1884         if(data->draglock) {
1885                 if(abs(mx-data->dragstartx) <= 3)
1886                         return changed;
1887
1888                 data->draglock= 0;
1889                 data->dragstartx= mx;  /* ignore mouse movement within drag-lock */
1890         }
1891
1892         softmin= but->softmin;
1893         softmax= but->softmax;
1894         softrange= softmax - softmin;
1895
1896         deler= 500;
1897         if(!ui_is_but_float(but)) {
1898                 if((softrange)<100) deler= 200.0;
1899                 if((softrange)<25) deler= 50.0;
1900         }
1901         deler /= fac;
1902
1903         if(ui_is_but_float(but) && softrange > 11) {
1904                 /* non linear change in mouse input- good for high precicsion */
1905                 data->dragf+= (((float)(mx-data->draglastx))/deler) * (fabs(data->dragstartx-mx)*0.002);
1906         } else if (!ui_is_but_float(but) && softrange > 129) { /* only scale large int buttons */
1907                 /* non linear change in mouse input- good for high precicsionm ints need less fine tuning */
1908                 data->dragf+= (((float)(mx-data->draglastx))/deler) * (fabs(data->dragstartx-mx)*0.004);
1909         } else {
1910                 /*no scaling */
1911                 data->dragf+= ((float)(mx-data->draglastx))/deler ;
1912         }
1913
1914         if(data->dragf>1.0) data->dragf= 1.0;
1915         if(data->dragf<0.0) data->dragf= 0.0;
1916         data->draglastx= mx;
1917         tempf= (softmin + data->dragf*softrange);
1918         
1919         if(!ui_is_but_float(but)) {
1920                 temp= floor(tempf+.5);
1921                 
1922                 if(tempf==softmin || tempf==softmax);
1923                 else if(snap) {
1924                         if(snap == 2) temp= 100*(temp/100);
1925                         else temp= 10*(temp/10);
1926                 }
1927
1928                 CLAMP(temp, softmin, softmax);
1929                 lvalue= (int)data->value;
1930                         
1931                 if(temp != lvalue) {
1932                         data->dragchange= 1;
1933                         data->value= (double)temp;
1934                         changed= 1;
1935                 }
1936         }
1937         else {
1938                 temp= 0;
1939
1940                 if(snap) {
1941                         if(snap == 2) {
1942                                 if(tempf==softmin || tempf==softmax);
1943                                 else if(softrange < 2.10) tempf= 0.01*floor(100.0*tempf);
1944                                 else if(softrange < 21.0) tempf= 0.1*floor(10.0*tempf);
1945                                 else tempf= floor(tempf);
1946                         }
1947                         else {
1948                                 if(tempf==softmin || tempf==softmax);
1949                                 else if(softrange < 2.10) tempf= 0.1*floor(10*tempf);
1950                                 else if(softrange < 21.0) tempf= floor(tempf);
1951                                 else tempf= 10.0*floor(tempf/10.0);
1952                         }
1953                 }
1954
1955                 CLAMP(tempf, softmin, softmax);
1956
1957                 if(tempf != data->value) {
1958                         data->dragchange= 1;
1959                         data->value= tempf;
1960                         changed= 1;
1961                 }
1962         }
1963
1964         return changed;
1965 }
1966
1967 static int ui_do_but_NUM(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
1968 {
1969         int mx, my, click= 0;
1970         int retval= WM_UI_HANDLER_CONTINUE;
1971         
1972         mx= event->x;
1973         my= event->y;
1974         ui_window_to_block(data->region, block, &mx, &my);
1975
1976         if(data->state == BUTTON_STATE_HIGHLIGHT) {
1977                 /* XXX hardcoded keymap check.... */
1978                 if(event->type == WHEELDOWNMOUSE && event->alt) {
1979                         mx= but->x1;
1980                         click= 1;
1981                 }
1982                 else if(event->type == WHEELUPMOUSE && event->alt) {
1983                         mx= but->x2;
1984                         click= 1;
1985                 }
1986                 else if(event->val==KM_PRESS) {
1987                         if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->ctrl) {
1988                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
1989                                 retval= WM_UI_HANDLER_BREAK;
1990                         }
1991                         else if(event->type == LEFTMOUSE) {
1992                                 data->dragstartx= mx;
1993                                 data->draglastx= mx;
1994                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
1995                                 retval= WM_UI_HANDLER_BREAK;
1996                         }
1997                         else if(ELEM(event->type, PADENTER, RETKEY) && event->val==KM_PRESS)
1998                                 click= 1;
1999                 }
2000                 
2001         }
2002         else if(data->state == BUTTON_STATE_NUM_EDITING) {
2003                 if(event->type == ESCKEY) {
2004                         data->cancel= 1;
2005                         data->escapecancel= 1;
2006                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2007                 }
2008                 else if(event->type == LEFTMOUSE && event->val!=KM_PRESS) {
2009                         if(data->dragchange)
2010                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2011                         else
2012                                 click= 1;
2013                 }
2014                 else if(event->type == MOUSEMOVE) {
2015                         float fac;
2016                         int snap;
2017
2018                         fac= 1.0f;
2019                         if(event->shift) fac /= 10.0f;
2020                         if(event->alt) fac /= 20.0f;
2021
2022                         if(event->custom == EVT_DATA_TABLET) {
2023                                 wmTabletData *wmtab= event->customdata;
2024
2025                                 /* de-sensitise based on tablet pressure */
2026                                 if (ELEM(wmtab->Active, DEV_STYLUS, DEV_ERASER))
2027                                         fac *= wmtab->Pressure;
2028                         }
2029                         
2030                         snap= (event->ctrl)? (event->shift)? 2: 1: 0;
2031
2032                         if(ui_numedit_but_NUM(but, data, fac, snap, mx))
2033                                 ui_numedit_apply(C, block, but, data);
2034                 }
2035                 retval= WM_UI_HANDLER_BREAK;
2036         }
2037         else if(data->state == BUTTON_STATE_TEXT_EDITING) {
2038                 ui_do_but_textedit(C, block, but, data, event);
2039                 retval= WM_UI_HANDLER_BREAK;
2040         }
2041         else if(data->state == BUTTON_STATE_TEXT_SELECTING) {
2042                 ui_do_but_textedit_select(C, block, but, data, event);
2043                 retval= WM_UI_HANDLER_BREAK;
2044         }
2045         
2046         if(click) {
2047                 /* we can click on the side arrows to increment/decrement,
2048                  * or click inside to edit the value directly */
2049                 float tempf, softmin, softmax;
2050                 int temp;
2051
2052                 softmin= but->softmin;
2053                 softmax= but->softmax;
2054
2055                 if(!ui_is_but_float(but)) {
2056                         if(mx < (but->x1 + (but->x2 - but->x1)/3 - 3)) {
2057                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2058
2059                                 temp= (int)data->value - 1;
2060                                 if(temp>=softmin && temp<=softmax)
2061                                         data->value= (double)temp;
2062                                 else
2063                                         data->cancel= 1;
2064
2065                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2066                         }
2067                         else if(mx > (but->x1 + (2*(but->x2 - but->x1)/3) + 3)) {
2068                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2069
2070                                 temp= (int)data->value + 1;
2071                                 if(temp>=softmin && temp<=softmax)
2072                                         data->value= (double)temp;
2073                                 else
2074                                         data->cancel= 1;
2075
2076                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2077                         }
2078                         else
2079                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2080                 }
2081                 else {
2082                         if(mx < (but->x1 + (but->x2 - but->x1)/3 - 3)) {
2083                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2084
2085                                 tempf= data->value - 0.01*but->a1;
2086                                 if (tempf < softmin) tempf = softmin;
2087                                 data->value= tempf;
2088
2089                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2090                         }
2091                         else if(mx > but->x1 + (2*((but->x2 - but->x1)/3) + 3)) {
2092                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2093
2094                                 tempf= data->value + 0.01*but->a1;
2095                                 if (tempf > softmax) tempf = softmax;
2096                                 data->value= tempf;
2097
2098                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2099                         }
2100                         else
2101                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2102                 }
2103
2104                 retval= WM_UI_HANDLER_BREAK;
2105         }
2106         
2107         return retval;
2108 }
2109
2110 static int ui_numedit_but_SLI(uiBut *but, uiHandleButtonData *data, int shift, int ctrl, int mx)
2111 {
2112         float deler, f, tempf, softmin, softmax, softrange;
2113         int temp, lvalue, changed= 0;
2114
2115         softmin= but->softmin;
2116         softmax= but->softmax;
2117         softrange= softmax - softmin;
2118
2119         if(but->type==NUMSLI) deler= ((but->x2-but->x1) - 5.0*but->aspect);
2120         else if(but->type==HSVSLI) deler= ((but->x2-but->x1)/2 - 5.0*but->aspect);
2121         else if(but->type==SCROLL) {
2122                 int horizontal= (but->x2 - but->x1 > but->y2 - but->y1);
2123                 float size= (horizontal)? (but->x2-but->x1): -(but->y2-but->y1);
2124                 deler= size*(but->softmax - but->softmin)/(but->softmax - but->softmin + but->a1);
2125         }
2126         else deler= (but->x2-but->x1- 5.0*but->aspect);
2127
2128         f= (float)(mx-data->dragstartx)/deler + data->dragfstart;
2129         
2130         if(shift)
2131                 f= (f-data->dragfstart)/10.0 + data->dragfstart;
2132
2133         CLAMP(f, 0.0, 1.0);
2134         tempf= softmin + f*softrange;
2135         temp= floor(tempf+.5);
2136
2137         if(ctrl) {
2138                 if(tempf==softmin || tempf==softmax);
2139                 else if(ui_is_but_float(but)) {
2140
2141                         if(shift) {
2142                                 if(tempf==softmin || tempf==softmax);
2143                                 else if(softmax-softmin < 2.10) tempf= 0.01*floor(100.0*tempf);
2144                                 else if(softmax-softmin < 21.0) tempf= 0.1*floor(10.0*tempf);
2145                                 else tempf= floor(tempf);
2146                         }
2147                         else {
2148                                 if(softmax-softmin < 2.10) tempf= 0.1*floor(10*tempf);
2149                                 else if(softmax-softmin < 21.0) tempf= floor(tempf);
2150                                 else tempf= 10.0*floor(tempf/10.0);
2151                         }
2152                 }
2153                 else {
2154                         temp= 10*(temp/10);
2155                         tempf= temp;
2156                 }
2157         }
2158
2159         if(!ui_is_but_float(but)) {
2160                 lvalue= floor(data->value+0.5);
2161
2162                 CLAMP(temp, softmin, softmax);
2163
2164                 if(temp != lvalue) {
2165                         data->value= temp;
2166                         data->dragchange= 1;
2167                         changed= 1;
2168                 }
2169         }
2170         else {
2171                 CLAMP(tempf, softmin, softmax);
2172
2173                 if(tempf != data->value) {
2174                         data->value= tempf;
2175                         data->dragchange= 1;
2176                         changed= 1;
2177                 }
2178         }
2179
2180         return changed;
2181 }
2182
2183 static int ui_do_but_SLI(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2184 {
2185         int mx, my, click= 0;
2186         int retval= WM_UI_HANDLER_CONTINUE;
2187
2188         mx= event->x;
2189         my= event->y;
2190         ui_window_to_block(data->region, block, &mx, &my);
2191
2192         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2193                 /* XXX hardcoded keymap check.... */
2194                 if(event->type == WHEELDOWNMOUSE && event->alt) {
2195                         mx= but->x1;
2196                         click= 2;
2197                 }
2198                 else if(event->type == WHEELUPMOUSE && event->alt) {
2199                         mx= but->x2;
2200                         click= 2;
2201                 }
2202                 else if(event->val==KM_PRESS) {
2203                         if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->ctrl) {
2204                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2205                                 retval= WM_UI_HANDLER_BREAK;
2206                         }
2207                         else if(event->type == LEFTMOUSE) {
2208                                 data->dragstartx= mx;
2209                                 data->draglastx= mx;
2210                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2211                                 retval= WM_UI_HANDLER_BREAK;
2212                         }
2213                         else if(ELEM(event->type, PADENTER, RETKEY) && event->val==KM_PRESS)
2214                                 click= 1;
2215                 }
2216         }
2217         else if(data->state == BUTTON_STATE_NUM_EDITING) {
2218                 if(event->type == ESCKEY) {
2219                         data->cancel= 1;
2220                         data->escapecancel= 1;
2221                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2222                 }
2223                 else if(event->type == LEFTMOUSE && event->val!=KM_PRESS) {
2224                         if(data->dragchange)
2225                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2226                         else
2227                                 click= 1;
2228                 }
2229                 else if(event->type == MOUSEMOVE) {
2230                         if(ui_numedit_but_SLI(but, data, event->shift, event->ctrl, mx))
2231                                 ui_numedit_apply(C, block, but, data);
2232                 }
2233                 retval= WM_UI_HANDLER_BREAK;
2234         }
2235         else if(data->state == BUTTON_STATE_TEXT_EDITING) {
2236                 ui_do_but_textedit(C, block, but, data, event);
2237                 retval= WM_UI_HANDLER_BREAK;
2238         }
2239         else if(data->state == BUTTON_STATE_TEXT_SELECTING) {
2240                 ui_do_but_textedit_select(C, block, but, data, event);
2241                 retval= WM_UI_HANDLER_BREAK;
2242         }
2243
2244         if(click) {
2245                 if (click==2) {
2246                         /* nudge slider to the left or right */
2247                         float f, tempf, softmin, softmax, softrange;
2248                         int temp;
2249                         
2250                         button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2251                         
2252                         softmin= but->softmin;
2253                         softmax= but->softmax;
2254                         softrange= softmax - softmin;
2255                         
2256                         tempf= data->value;
2257                         temp= (int)data->value;
2258                         
2259                         if(but->type==SLI) f= (float)(mx-but->x1)/(but->x2-but->x1);
2260                         else f= (float)(mx- but->x1)/(but->x2-but->x1);
2261                         
2262                         f= softmin + f*softrange;
2263                         
2264                         if(!ui_is_but_float(but)) {
2265                                 if(f<temp) temp--;
2266                                 else temp++;
2267                                 
2268                                 if(temp>=softmin && temp<=softmax)
2269                                         data->value= temp;
2270                                 else
2271                                         data->cancel= 1;
2272                         } 
2273                         else {
2274                                 if(f<tempf) tempf-=.01;
2275                                 else tempf+=.01;
2276                                 
2277                                 if(tempf>=softmin && tempf<=softmax)
2278                                         data->value= tempf;
2279                                 else
2280                                         data->cancel= 1;
2281                         }
2282                         
2283                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2284                         retval= WM_UI_HANDLER_BREAK;
2285                 }
2286                 else {
2287                         /* edit the value directly */
2288                         button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2289                         retval= WM_UI_HANDLER_BREAK;
2290                 }
2291         }
2292         
2293         return retval;
2294 }
2295
2296 static int ui_do_but_SCROLL(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2297 {
2298         int mx, my, click= 0;
2299         int retval= WM_UI_HANDLER_CONTINUE;
2300         int horizontal= (but->x2 - but->x1 > but->y2 - but->y1);
2301         
2302         mx= event->x;
2303         my= event->y;
2304         ui_window_to_block(data->region, block, &mx, &my);
2305
2306         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2307                 if(event->val==KM_PRESS) {
2308                         if(event->type == LEFTMOUSE) {
2309                                 if(horizontal) {
2310                                         data->dragstartx= mx;
2311                                         data->draglastx= mx;
2312                                 }
2313                                 else {
2314                                         data->dragstartx= my;
2315                                         data->draglastx= my;
2316                                 }
2317                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2318                                 retval= WM_UI_HANDLER_BREAK;
2319                         }
2320                         else if(ELEM(event->type, PADENTER, RETKEY) && event->val==KM_PRESS)
2321                                 click= 1;
2322                 }
2323         }
2324         else if(data->state == BUTTON_STATE_NUM_EDITING) {
2325                 if(event->type == ESCKEY) {
2326                         data->cancel= 1;
2327                         data->escapecancel= 1;
2328                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2329                 }
2330                 else if(event->type == LEFTMOUSE && event->val!=KM_PRESS) {
2331                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2332                 }
2333                 else if(event->type == MOUSEMOVE) {
2334                         if(ui_numedit_but_SLI(but, data, 0, 0, (horizontal)? mx: my))
2335                                 ui_numedit_apply(C, block, but, data);
2336                 }
2337
2338                 retval= WM_UI_HANDLER_BREAK;
2339         }
2340         
2341         return retval;
2342 }
2343
2344 static int ui_do_but_BLOCK(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2345 {
2346         
2347         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2348                 if(ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val==KM_PRESS) {
2349                         button_activate_state(C, but, BUTTON_STATE_MENU_OPEN);
2350                         return WM_UI_HANDLER_BREAK;
2351                 }
2352                 else if(ELEM3(but->type, MENU, ICONROW, ICONTEXTROW)) {
2353                         
2354                         if(event->type == WHEELDOWNMOUSE && event->alt) {
2355                                 data->value= ui_step_name_menu(but, -1);
2356                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2357                                 ui_apply_button(C, but->block, but, data, 1);
2358                                 return WM_UI_HANDLER_BREAK;
2359                         }
2360                         else if(event->type == WHEELUPMOUSE && event->alt) {
2361                                 data->value= ui_step_name_menu(but, 1);
2362                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2363                                 ui_apply_button(C, but->block, but, data, 1);
2364                                 return WM_UI_HANDLER_BREAK;
2365                         }
2366                 }
2367                 else if(but->type==COL) {
2368                         if( ELEM(event->type, WHEELDOWNMOUSE, WHEELUPMOUSE) && event->alt) {
2369                                 float col[3];
2370                                 
2371                                 ui_get_but_vectorf(but, col);
2372                                 rgb_to_hsv(col[0], col[1], col[2], but->hsv, but->hsv+1, but->hsv+2);
2373
2374                                 if(event->type==WHEELDOWNMOUSE)
2375                                         but->hsv[2]= CLAMPIS(but->hsv[2]-0.05f, 0.0f, 1.0f);
2376                                 else
2377                                         but->hsv[2]= CLAMPIS(but->hsv[2]+0.05f, 0.0f, 1.0f);
2378                                 
2379                                 hsv_to_rgb(but->hsv[0], but->hsv[1], but->hsv[2], data->vec, data->vec+1, data->vec+2);
2380                                 
2381                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2382                                 ui_apply_button(C, but->block, but, data, 1);
2383                                 return WM_UI_HANDLER_BREAK;
2384                         }
2385                 }
2386         }
2387
2388         return WM_UI_HANDLER_CONTINUE;
2389 }
2390
2391 static int ui_numedit_but_NORMAL(uiBut *but, uiHandleButtonData *data, int mx, int my)
2392 {
2393         float dx, dy, rad, radsq, mrad, *fp;
2394         int mdx, mdy, changed= 1;
2395         
2396         /* button is presumed square */
2397         /* if mouse moves outside of sphere, it does negative normal */
2398
2399         fp= data->origvec;
2400         rad= (but->x2 - but->x1);
2401         radsq= rad*rad;
2402         
2403         if(fp[2]>0.0f) {
2404                 mdx= (rad*fp[0]);
2405                 mdy= (rad*fp[1]);
2406         }
2407         else if(fp[2]> -1.0f) {
2408                 mrad= rad/sqrt(fp[0]*fp[0] + fp[1]*fp[1]);
2409                 
2410                 mdx= 2.0f*mrad*fp[0] - (rad*fp[0]);
2411                 mdy= 2.0f*mrad*fp[1] - (rad*fp[1]);
2412         }
2413         else mdx= mdy= 0;
2414         
2415         dx= (float)(mx+mdx-data->dragstartx);
2416         dy= (float)(my+mdy-data->dragstarty);
2417
2418         fp= data->vec;
2419         mrad= dx*dx+dy*dy;
2420         if(mrad < radsq) {      /* inner circle */
2421                 fp[0]= dx;
2422                 fp[1]= dy;
2423                 fp[2]= sqrt( radsq-dx*dx-dy*dy );
2424         }
2425         else {  /* outer circle */
2426                 
2427                 mrad= rad/sqrt(mrad);   // veclen
2428                 
2429                 dx*= (2.0f*mrad - 1.0f);
2430                 dy*= (2.0f*mrad - 1.0f);
2431                 
2432                 mrad= dx*dx+dy*dy;
2433                 if(mrad < radsq) {
2434                         fp[0]= dx;
2435                         fp[1]= dy;
2436                         fp[2]= -sqrt( radsq-dx*dx-dy*dy );
2437                 }
2438         }
2439         Normalize(fp);
2440
2441         data->draglastx= mx;
2442         data->draglasty= my;
2443
2444         return changed;
2445 }
2446
2447 static int ui_do_but_NORMAL(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2448 {
2449         int mx, my;
2450
2451         mx= event->x;
2452         my= event->y;
2453         ui_window_to_block(data->region, block, &mx, &my);
2454
2455         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2456                 if(event->type==LEFTMOUSE && event->val==KM_PRESS) {
2457                         data->dragstartx= mx;
2458                         data->dragstarty= my;
2459                         data->draglastx= mx;
2460                         data->draglasty= my;
2461                         button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2462
2463                         /* also do drag the first time */
2464                         if(ui_numedit_but_NORMAL(but, data, mx, my))
2465                                 ui_numedit_apply(C, block, but, data);
2466                         
2467                         return WM_UI_HANDLER_BREAK;
2468                 }
2469         }
2470         else if(data->state == BUTTON_STATE_NUM_EDITING) {
2471                 if(event->type == MOUSEMOVE) {
2472                         if(mx!=data->draglastx || my!=data->draglasty) {
2473                                 if(ui_numedit_but_NORMAL(but, data, mx, my))
2474                                         ui_numedit_apply(C, block, but, data);
2475                         }
2476                 }
2477                 else if(event->type==LEFTMOUSE && event->val!=KM_PRESS)
2478                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2479
2480                 return WM_UI_HANDLER_BREAK;
2481         }
2482         
2483         return WM_UI_HANDLER_CONTINUE;
2484 }
2485
2486 static int ui_numedit_but_HSVCUBE(uiBut *but, uiHandleButtonData *data, int mx, int my)
2487 {
2488         float x, y;
2489         int changed= 1;
2490
2491         /* relative position within box */
2492         x= ((float)mx-but->x1)/(but->x2-but->x1);
2493         y= ((float)my-but->y1)/(but->y2-but->y1);
2494         CLAMP(x, 0.0, 1.0);
2495         CLAMP(y, 0.0, 1.0);
2496         
2497         if(but->a1==0) {
2498                 but->hsv[0]= x; 
2499                 but->hsv[2]= y; 
2500         }
2501         else if(but->a1==1) {
2502                 but->hsv[0]= x;                                 
2503                 but->hsv[1]= y;                                 
2504         }
2505         else if(but->a1==2) {
2506                 but->hsv[2]= x; 
2507                 but->hsv[1]= y; 
2508         }
2509         else if(but->a1==3) {
2510                 but->hsv[0]= x; 
2511         }
2512         else
2513                 but->hsv[2]= y; 
2514
2515         ui_set_but_hsv(but);    // converts to rgb
2516         
2517         // update button values and strings
2518         ui_update_block_buts_hsv(but->block, but->hsv);
2519
2520         data->draglastx= mx;
2521         data->draglasty= my;
2522
2523         return changed;
2524 }
2525
2526 static int ui_do_but_HSVCUBE(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2527 {
2528         int mx, my;
2529
2530         mx= event->x;
2531         my= event->y;
2532         ui_window_to_block(data->region, block, &mx, &my);
2533
2534         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2535                 if(event->type==LEFTMOUSE && event->val==KM_PRESS) {
2536                         data->dragstartx= mx;
2537                         data->dragstarty= my;
2538                         data->draglastx= mx;
2539                         data->draglasty= my;
2540                         button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2541
2542                         /* also do drag the first time */
2543                         if(ui_numedit_but_HSVCUBE(but, data, mx, my))
2544                                 ui_numedit_apply(C, block, but, data);
2545                         
2546                         return WM_UI_HANDLER_BREAK;
2547                 }
2548         }
2549         else if(data->state == BUTTON_STATE_NUM_EDITING) {
2550                 if(event->type == MOUSEMOVE) {
2551                         if(mx!=data->draglastx || my!=data->draglasty) {
2552                                 if(ui_numedit_but_HSVCUBE(but, data, mx, my))
2553                                         ui_numedit_apply(C, block, but, data);
2554                         }
2555                 }
2556                 else if(event->type==LEFTMOUSE && event->val!=KM_PRESS)
2557                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2558                 
2559                 return WM_UI_HANDLER_BREAK;
2560         }
2561
2562         return WM_UI_HANDLER_CONTINUE;
2563 }
2564
2565 static int ui_numedit_but_HSVCIRCLE(uiBut *but, uiHandleButtonData *data, int mx, int my)
2566 {
2567         rcti rect;
2568         int changed= 1;
2569
2570         rect.xmin= but->x1; rect.xmax= but->x2;
2571         rect.ymin= but->y1; rect.ymax= but->y2;
2572         
2573         ui_hsvcircle_vals_from_pos(but->hsv, but->hsv+1, &rect, (float)mx, (float)my);
2574         
2575         ui_set_but_hsv(but);    // converts to rgb
2576         
2577         // update button values and strings
2578         // XXX 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
2587 static int ui_do_but_HSVCIRCLE(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2588 {
2589         int mx, my;
2590         
2591         mx= event->x;
2592         my= event->y;
2593         ui_window_to_block(data->region, block, &mx, &my);
2594         
2595         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2596                 if(event->type==LEFTMOUSE && event->val==KM_PRESS) {
2597                         data->dragstartx= mx;
2598                         data->dragstarty= my;
2599                         data->draglastx= mx;
2600                         data->draglasty= my;
2601                         button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2602                         
2603                         /* also do drag the first time */
2604                         if(ui_numedit_but_HSVCIRCLE(but, data, mx, my))
2605                                 ui_numedit_apply(C, block, but, data);
2606                         
2607                         return WM_UI_HANDLER_BREAK;
2608                 }
2609         }
2610         else if(data->state == BUTTON_STATE_NUM_EDITING) {
2611                 /* XXX hardcoded keymap check.... */
2612                 if(event->type == WHEELDOWNMOUSE) {
2613                         but->hsv[2]= CLAMPIS(but->hsv[2]-0.05f, 0.0f, 1.0f);
2614                         ui_set_but_hsv(but);    // converts to rgb
2615                         ui_numedit_apply(C, block, but, data);
2616                 }
2617                 else if(event->type == WHEELUPMOUSE) {
2618                         but->hsv[2]= CLAMPIS(but->hsv[2]+0.05f, 0.0f, 1.0f);
2619                         ui_set_but_hsv(but);    // converts to rgb
2620                         ui_numedit_apply(C, block, but, data);
2621                 }
2622                 else if(event->type == MOUSEMOVE) {
2623                         if(mx!=data->draglastx || my!=data->draglasty) {
2624                                 if(ui_numedit_but_HSVCIRCLE(but, data, mx, my))
2625                                         ui_numedit_apply(C, block, but, data);
2626                         }
2627                 }
2628                 else if(event->type==LEFTMOUSE && event->val!=KM_PRESS)
2629                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2630                 
2631                 return WM_UI_HANDLER_BREAK;
2632         }
2633         
2634         return WM_UI_HANDLER_CONTINUE;
2635 }
2636
2637
2638 static int verg_colorband(const void *a1, const void *a2)
2639 {
2640         const CBData *x1=a1, *x2=a2;
2641         
2642         if( x1->pos > x2->pos ) return 1;
2643         else if( x1->pos < x2->pos) return -1;
2644         return WM_UI_HANDLER_CONTINUE;
2645 }
2646
2647 static void ui_colorband_update(ColorBand *coba)
2648 {
2649         int a;
2650         
2651         if(coba->tot<2) return;
2652         
2653         for(a=0; a<coba->tot; a++) coba->data[a].cur= a;
2654                 qsort(coba->data, coba->tot, sizeof(CBData), verg_colorband);
2655         for(a=0; a<coba->tot; a++) {
2656                 if(coba->data[a].cur==coba->cur) {
2657                         coba->cur= a;
2658                         break;
2659                 }
2660         }
2661 }
2662
2663 static int ui_numedit_but_COLORBAND(uiBut *but, uiHandleButtonData *data, int mx)
2664 {
2665         float dx;
2666         int changed= 0;
2667
2668         if(data->draglastx == mx)
2669                 return changed;
2670
2671         dx= ((float)(mx - data->draglastx))/(but->x2-but->x1);
2672         data->dragcbd->pos += dx;
2673         CLAMP(data->dragcbd->pos, 0.0, 1.0);
2674         
2675         ui_colorband_update(data->coba);
2676         data->dragcbd= data->coba->data + data->coba->cur;      /* because qsort */
2677         
2678         data->draglastx= mx;
2679         changed= 1;
2680
2681         return changed;
2682 }
2683
2684 static int ui_do_but_COLORBAND(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2685 {
2686         ColorBand *coba;
2687         CBData *cbd;
2688         int mx, my, a, xco, mindist= 12;
2689
2690         mx= event->x;
2691         my= event->y;
2692         ui_window_to_block(data->region, block, &mx, &my);
2693
2694         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2695                 if(event->type==LEFTMOUSE && event->val==KM_PRESS) {
2696                         coba= (ColorBand*)but->poin;
2697
2698                         if(event->ctrl) {
2699                                 /* insert new key on mouse location */
2700                                 if(coba->tot < MAXCOLORBAND-1) {
2701                                         float pos= ((float)(mx - but->x1))/(but->x2-but->x1);
2702                                         float col[4];
2703                                         
2704                                         do_colorband(coba, pos, col);   /* executes it */
2705                                         
2706                                         coba->tot++;
2707                                         coba->cur= coba->tot-1;
2708                                         
2709                                         coba->data[coba->cur].r= col[0];
2710                                         coba->data[coba->cur].g= col[1];
2711                                         coba->data[coba->cur].b= col[2];
2712                                         coba->data[coba->cur].a= col[3];
2713                                         coba->data[coba->cur].pos= pos;
2714
2715                                         ui_colorband_update(coba);
2716                                 }
2717
2718                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2719                         }
2720                         else {
2721                                 data->dragstartx= mx;
2722                                 data->dragstarty= my;
2723                                 data->draglastx= mx;
2724                                 data->draglasty= my;
2725
2726                                 /* activate new key when mouse is close */
2727                                 for(a=0, cbd= coba->data; a<coba->tot; a++, cbd++) {
2728                                         xco= but->x1 + (cbd->pos*(but->x2-but->x1));
2729                                         xco= ABS(xco-mx);
2730                                         if(a==coba->cur) xco+= 5; // selected one disadvantage
2731                                         if(xco<mindist) {
2732                                                 coba->cur= a;
2733                                                 mindist= xco;
2734                                         }
2735                                 }
2736                 
2737                                 data->dragcbd= coba->data + coba->cur;
2738                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2739                         }
2740
2741                         return WM_UI_HANDLER_BREAK;
2742                 }
2743         }
2744         else if(data->state == BUTTON_STATE_NUM_EDITING) {
2745                 if(event->type == MOUSEMOVE) {
2746                         if(mx!=data->draglastx || my!=data->draglasty) {
2747                                 if(ui_numedit_but_COLORBAND(but, data, mx))
2748                                         ui_numedit_apply(C, block, but, data);
2749                         }
2750                 }
2751                 else if(event->type==LEFTMOUSE && event->val!=KM_PRESS)
2752                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2753                 
2754                 return WM_UI_HANDLER_BREAK;
2755         }
2756
2757         return WM_UI_HANDLER_CONTINUE;
2758 }
2759
2760 static int ui_numedit_but_CURVE(uiBut *but, uiHandleButtonData *data, int snap, int mx, int my)
2761 {
2762         CurveMapping *cumap= data->cumap;
2763         CurveMap *cuma= cumap->cm+cumap->cur;
2764         CurveMapPoint *cmp= cuma->curve;
2765         float fx, fy, zoomx, zoomy, offsx, offsy;
2766         int a, changed= 0;
2767
2768         zoomx= (but->x2-but->x1)/(cumap->curr.xmax-cumap->curr.xmin);
2769         zoomy= (but->y2-but->y1)/(cumap->curr.ymax-cumap->curr.ymin);
2770         offsx= cumap->curr.xmin;
2771         offsy= cumap->curr.ymin;
2772
2773         if(data->dragsel != -1) {
2774                 int moved_point= 0;             /* for ctrl grid, can't use orig coords because of sorting */
2775                 
2776                 fx= (mx-data->draglastx)/zoomx;
2777                 fy= (my-data->draglasty)/zoomy;
2778                 for(a=0; a<cuma->totpoint; a++) {
2779                         if(cmp[a].flag & SELECT) {
2780                                 float origx= cmp[a].x, origy= cmp[a].y;
2781                                 cmp[a].x+= fx;
2782                                 cmp[a].y+= fy;
2783                                 if(snap) {
2784                                         cmp[a].x= 0.125f*floor(0.5f + 8.0f*cmp[a].x);
2785                                         cmp[a].y= 0.125f*floor(0.5f + 8.0f*cmp[a].y);
2786                                 }
2787                                 if(cmp[a].x!=origx || cmp[a].y!=origy)
2788                                         moved_point= 1;
2789                         }
2790                 }
2791
2792                 curvemapping_changed(cumap, 0); /* no remove doubles */
2793                 
2794                 if(moved_point) {
2795                         data->draglastx= mx;
2796                         data->draglasty= my;
2797                         changed= 1;
2798                 }
2799
2800                 data->dragchange= 1; /* mark for selection */
2801         }
2802         else {
2803                 fx= (mx-data->draglastx)/zoomx;
2804                 fy= (my-data->draglasty)/zoomy;
2805                 
2806                 /* clamp for clip */
2807                 if(cumap->flag & CUMA_DO_CLIP) {
2808                         if(cumap->curr.xmin-fx < cumap->clipr.xmin)
2809                                 fx= cumap->curr.xmin - cumap->clipr.xmin;
2810                         else if(cumap->curr.xmax-fx > cumap->clipr.xmax)
2811                                 fx= cumap->curr.xmax - cumap->clipr.xmax;
2812                         if(cumap->curr.ymin-fy < cumap->clipr.ymin)
2813                                 fy= cumap->curr.ymin - cumap->clipr.ymin;
2814                         else if(cumap->curr.ymax-fy > cumap->clipr.ymax)
2815                                 fy= cumap->curr.ymax - cumap->clipr.ymax;
2816                 }                               
2817
2818                 cumap->curr.xmin-=fx;
2819                 cumap->curr.ymin-=fy;
2820                 cumap->curr.xmax-=fx;
2821                 cumap->curr.ymax-=fy;
2822                 
2823                 data->draglastx= mx;
2824                 data->draglasty= my;
2825
2826                 changed= 1;
2827         }
2828
2829         return changed;
2830 }
2831
2832 static int ui_do_but_CURVE(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2833 {
2834         int mx, my, a, changed= 0;
2835
2836         mx= event->x;
2837         my= event->y;
2838         ui_window_to_block(data->region, block, &mx, &my);
2839
2840         if(data->state == BUTTON_STATE_HIGHLIGHT) {
2841                 if(event->type==LEFTMOUSE && event->val==KM_PRESS) {
2842                         CurveMapping *cumap= (CurveMapping*)but->poin;
2843                         CurveMap *cuma= cumap->cm+cumap->cur;
2844                         CurveMapPoint *cmp= cuma->curve;
2845                         float fx, fy, zoomx, zoomy, offsx, offsy;
2846                         float dist, mindist= 200.0f; // 14 pixels radius
2847                         int sel= -1;
2848
2849                         zoomx= (but->x2-but->x1)/(cumap->curr.xmax-cumap->curr.xmin);
2850                         zoomy= (but->y2-but->y1)/(cumap->curr.ymax-cumap->curr.ymin);
2851                         offsx= cumap->curr.xmin;
2852                         offsy= cumap->curr.ymin;
2853
2854                         if(event->ctrl) {
2855                                 fx= ((float)my - but->x1)/zoomx + offsx;
2856                                 fy= ((float)my - but->y1)/zoomy + offsy;
2857                                 
2858                                 curvemap_insert(cuma, fx, fy);
2859                                 curvemapping_changed(cumap, 0);
2860                                 changed= 1;
2861                         }
2862
2863                         /* check for selecting of a point */
2864                         cmp= cuma->curve;       /* ctrl adds point, new malloc */
2865                         for(a=0; a<cuma->totpoint; a++) {
2866                                 fx= but->x1 + zoomx*(cmp[a].x-offsx);
2867                                 fy= but->y1 + zoomy*(cmp[a].y-offsy);
2868                                 dist= (fx-mx)*(fx-mx) + (fy-my)*(fy-my);
2869                                 if(dist < mindist) {
2870                                         sel= a;
2871                                         mindist= dist;
2872                                 }
2873                         }
2874
2875                         if (sel == -1) {
2876                                 /* if the click didn't select anything, check if it's clicked on the 
2877                                  * curve itself, and if so, add a point */
2878                                 fx= ((float)mx - but->x1)/zoomx + offsx;
2879                                 fy= ((float)my - but->y1)/zoomy + offsy;
2880                                 
2881                                 cmp= cuma->table;
2882
2883                                 /* loop through the curve segment table and find what's near the mouse.
2884                                  * 0.05 is kinda arbitrary, but seems to be what works nicely. */
2885                                 for(a=0; a<=CM_TABLE; a++) {                    
2886                                         if ( ( fabs(fx - cmp[a].x) < (0.05) ) && ( fabs(fy - cmp[a].y) < (0.05) ) ) {
2887                                         
2888                                                 curvemap_insert(cuma, fx, fy);
2889                                                 curvemapping_changed(cumap, 0);
2890
2891                                                 changed= 1;
2892                                                 
2893                                                 /* reset cmp back to the curve points again, rather than drawing segments */            
2894                                                 cmp= cuma->curve;
2895                                                 
2896                                                 /* find newly added point and make it 'sel' */
2897                                                 for(a=0; a<cuma->totpoint; a++)
2898                                                         if(cmp[a].x == fx)
2899                                                                 sel = a;
2900                                                         
2901                                                 break;
2902                                         }
2903                                 }
2904                         }
2905
2906                         if(sel!= -1) {
2907                                 /* ok, we move a point */
2908                              &n