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