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