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