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