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