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