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