style cleanup
[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                 {
1963                         mx = event->x;
1964                         my = event->y;
1965                         ui_window_to_block(data->region, block, &mx, &my);
1966
1967                         ui_textedit_set_cursor_select(but, data, mx);
1968                         retval = WM_UI_HANDLER_BREAK;
1969                         break;
1970                 }
1971                 case LEFTMOUSE:
1972                         if (event->val == KM_RELEASE)
1973                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
1974                         retval = WM_UI_HANDLER_BREAK;
1975                         break;
1976         }
1977
1978         if (retval == WM_UI_HANDLER_BREAK) {
1979                 ui_check_but(but);
1980                 ED_region_tag_redraw(data->region);
1981         }
1982 }
1983
1984 /* ************* number editing for various types ************* */
1985
1986 static void ui_numedit_begin(uiBut *but, uiHandleButtonData *data)
1987 {
1988         if (but->type == BUT_CURVE) {
1989                 but->editcumap = (CurveMapping *)but->poin;
1990         }
1991         else if (but->type == BUT_COLORBAND) {
1992                 data->coba = (ColorBand *)but->poin;
1993                 but->editcoba = data->coba;
1994         }
1995         else if (ELEM3(but->type, BUT_NORMAL, HSVCUBE, HSVCIRCLE)) {
1996                 ui_get_but_vectorf(but, data->origvec);
1997                 copy_v3_v3(data->vec, data->origvec);
1998                 but->editvec = data->vec;
1999         }
2000         else {
2001                 float softrange, softmin, softmax;
2002
2003                 data->startvalue = ui_get_but_val(but);
2004                 data->origvalue = data->startvalue;
2005                 data->value = data->origvalue;
2006                 but->editval = &data->value;
2007
2008                 softmin = but->softmin;
2009                 softmax = but->softmax;
2010                 softrange = softmax - softmin;
2011
2012                 data->dragfstart = (softrange == 0.0f) ? 0.0f : ((float)data->value - softmin) / softrange;
2013                 data->dragf = data->dragfstart;
2014         }
2015
2016         data->dragchange = 0;
2017         data->draglock = 1;
2018 }
2019
2020 static void ui_numedit_end(uiBut *but, uiHandleButtonData *data)
2021 {
2022         but->editval = NULL;
2023         but->editvec = NULL;
2024         but->editcoba = NULL;
2025         but->editcumap = NULL;
2026
2027         data->dragstartx = 0;
2028         data->draglastx = 0;
2029         data->dragchange = 0;
2030         data->dragcbd = NULL;
2031         data->dragsel = 0;
2032 }
2033
2034 static void ui_numedit_apply(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data)
2035 {
2036         if (data->interactive) ui_apply_button(C, block, but, data, 1);
2037         else ui_check_but(but);
2038
2039         ED_region_tag_redraw(data->region);
2040 }
2041
2042 /* ****************** menu opening for various types **************** */
2043
2044 static void ui_blockopen_begin(bContext *C, uiBut *but, uiHandleButtonData *data)
2045 {
2046         uiBlockCreateFunc func = NULL;
2047         uiBlockHandleCreateFunc handlefunc = NULL;
2048         uiMenuCreateFunc menufunc = NULL;
2049         char *menustr = NULL;
2050         void *arg = NULL;
2051
2052         switch (but->type) {
2053                 case BLOCK:
2054                 case PULLDOWN:
2055                         if (but->menu_create_func) {
2056                                 menufunc = but->menu_create_func;
2057                                 arg = but->poin;
2058                         }
2059                         else {
2060                                 func = but->block_create_func;
2061                                 arg = but->poin ? but->poin : but->func_argN;
2062                         }
2063                         break;
2064                 case MENU:
2065                         if (but->menu_create_func) {
2066                                 menufunc = but->menu_create_func;
2067                                 arg = but->poin;
2068                         }
2069                         else {
2070                                 data->origvalue = ui_get_but_val(but);
2071                                 data->value = data->origvalue;
2072                                 but->editval = &data->value;
2073
2074                                 menustr = but->str;
2075                         }
2076                         break;
2077                 case ICONROW:
2078                         menufunc = ui_block_func_ICONROW;
2079                         arg = but;
2080                         break;
2081                 case ICONTEXTROW:
2082                         menufunc = ui_block_func_ICONTEXTROW;
2083                         arg = but;
2084                         break;
2085                 case COL:
2086                         ui_get_but_vectorf(but, data->origvec);
2087                         copy_v3_v3(data->vec, data->origvec);
2088                         but->editvec = data->vec;
2089
2090                         handlefunc = ui_block_func_COL;
2091                         arg = but;
2092                         break;
2093         }
2094
2095         if (func || handlefunc) {
2096                 data->menu = ui_popup_block_create(C, data->region, but, func, handlefunc, arg);
2097                 if (but->block->handle)
2098                         data->menu->popup = but->block->handle->popup;
2099         }
2100         else if (menufunc || menustr) {
2101                 data->menu = ui_popup_menu_create(C, data->region, but, menufunc, arg, menustr);
2102                 if (but->block->handle)
2103                         data->menu->popup = but->block->handle->popup;
2104         }
2105
2106         /* this makes adjacent blocks auto open from now on */
2107         //if (but->block->auto_open ==0 ) but->block->auto_open = 1;
2108 }
2109
2110 static void ui_blockopen_end(bContext *C, uiBut *but, uiHandleButtonData *data)
2111 {
2112         if (but) {
2113                 but->editval = NULL;
2114                 but->editvec = NULL;
2115
2116                 but->block->auto_open_last = PIL_check_seconds_timer();
2117         }
2118
2119         if (data->menu) {
2120                 ui_popup_block_free(C, data->menu);
2121                 data->menu = NULL;
2122         }
2123 }
2124
2125 /* ***************** events for different button types *************** */
2126
2127 static int ui_do_but_BUT(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2128 {
2129         if (data->state == BUTTON_STATE_HIGHLIGHT) {
2130                 if (event->type == LEFTMOUSE && event->val == KM_PRESS) {
2131                         button_activate_state(C, but, BUTTON_STATE_WAIT_RELEASE);
2132                         return WM_UI_HANDLER_BREAK;
2133                 }
2134                 else if (event->type == LEFTMOUSE && but->block->handle) {
2135                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2136                         return WM_UI_HANDLER_BREAK;
2137                 }
2138                 else if (ELEM(event->type, PADENTER, RETKEY) && event->val == KM_PRESS) {
2139                         button_activate_state(C, but, BUTTON_STATE_WAIT_FLASH);
2140                         return WM_UI_HANDLER_BREAK;
2141                 }
2142         }
2143         else if (data->state == BUTTON_STATE_WAIT_RELEASE) {
2144                 if (event->type == LEFTMOUSE && event->val != KM_PRESS) {
2145                         if (!(but->flag & UI_SELECT))
2146                                 data->cancel = 1;
2147                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2148                         return WM_UI_HANDLER_BREAK;
2149                 }
2150         }
2151
2152         return WM_UI_HANDLER_CONTINUE;
2153 }
2154
2155 static int ui_do_but_HOTKEYEVT(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2156 {
2157         if (data->state == BUTTON_STATE_HIGHLIGHT) {
2158                 if (ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val == KM_PRESS) {
2159                         but->drawstr[0] = 0;
2160                         but->modifier_key = 0;
2161                         button_activate_state(C, but, BUTTON_STATE_WAIT_KEY_EVENT);
2162                         return WM_UI_HANDLER_BREAK;
2163                 }
2164         }
2165         else if (data->state == BUTTON_STATE_WAIT_KEY_EVENT) {
2166                 
2167                 if (event->type == MOUSEMOVE)
2168                         return WM_UI_HANDLER_CONTINUE;
2169                 
2170                 if (event->type == LEFTMOUSE && event->val == KM_PRESS) {
2171                         /* only cancel if click outside the button */
2172                         if (ui_mouse_inside_button(but->active->region, but, event->x, event->y) == 0) {
2173                                 /* data->cancel doesnt work, this button opens immediate */
2174                                 if (but->flag & UI_BUT_IMMEDIATE)
2175                                         ui_set_but_val(but, 0);
2176                                 else
2177                                         data->cancel = 1;
2178                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2179                                 return WM_UI_HANDLER_BREAK;
2180                         }
2181                 }
2182                 
2183                 /* always set */
2184                 but->modifier_key = 0;
2185                 if (event->shift) but->modifier_key |= KM_SHIFT;
2186                 if (event->alt) but->modifier_key |= KM_ALT;
2187                 if (event->ctrl) but->modifier_key |= KM_CTRL;
2188                 if (event->oskey) but->modifier_key |= KM_OSKEY;
2189
2190                 ui_check_but(but);
2191                 ED_region_tag_redraw(data->region);
2192                         
2193                 if (event->val == KM_PRESS) {
2194                         if (ISHOTKEY(event->type)) { 
2195                                 
2196                                 if (WM_key_event_string(event->type)[0])
2197                                         ui_set_but_val(but, event->type);
2198                                 else
2199                                         data->cancel = 1;
2200                                 
2201                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2202                                 return WM_UI_HANDLER_BREAK;
2203                         }
2204                         else if (event->type == ESCKEY) {
2205                                 data->cancel = 1;
2206                                 data->escapecancel = 1;
2207                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2208                         }
2209                         
2210                 }
2211         }
2212         
2213         return WM_UI_HANDLER_CONTINUE;
2214 }
2215
2216 static int ui_do_but_KEYEVT(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2217 {
2218         if (data->state == BUTTON_STATE_HIGHLIGHT) {
2219                 if (ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val == KM_PRESS) {
2220                         button_activate_state(C, but, BUTTON_STATE_WAIT_KEY_EVENT);
2221                         return WM_UI_HANDLER_BREAK;
2222                 }
2223         }
2224         else if (data->state == BUTTON_STATE_WAIT_KEY_EVENT) {
2225                 if (event->type == MOUSEMOVE)
2226                         return WM_UI_HANDLER_CONTINUE;
2227
2228                 if (event->val == KM_PRESS) {
2229                         if (WM_key_event_string(event->type)[0])
2230                                 ui_set_but_val(but, event->type);
2231                         else
2232                                 data->cancel = 1;
2233
2234                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2235                 }
2236         }
2237
2238         return WM_UI_HANDLER_CONTINUE;
2239 }
2240
2241 static int ui_do_but_TEX(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2242 {
2243         if (data->state == BUTTON_STATE_HIGHLIGHT) {
2244                 if (ELEM(event->type, LEFTMOUSE, EVT_BUT_OPEN) && event->val == KM_PRESS) {
2245                         if (but->dt == UI_EMBOSSN && !event->ctrl) ;
2246                         else {
2247                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2248                                 return WM_UI_HANDLER_BREAK;
2249                         }
2250                 }
2251         }
2252         else if (data->state == BUTTON_STATE_TEXT_EDITING) {
2253                 ui_do_but_textedit(C, block, but, data, event);
2254                 return WM_UI_HANDLER_BREAK;
2255         }
2256         else if (data->state == BUTTON_STATE_TEXT_SELECTING) {
2257                 ui_do_but_textedit_select(C, block, but, data, event);
2258                 return WM_UI_HANDLER_BREAK;
2259         }
2260
2261         return WM_UI_HANDLER_CONTINUE;
2262 }
2263
2264 static int ui_do_but_TOG(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2265 {
2266         if (data->state == BUTTON_STATE_HIGHLIGHT) {
2267                 if (ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val == KM_PRESS) {
2268                         data->togdual = event->ctrl;
2269                         data->togonly = !event->shift;
2270                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2271                         return WM_UI_HANDLER_BREAK;
2272                 }
2273         }
2274         return WM_UI_HANDLER_CONTINUE;
2275 }
2276
2277 static int ui_do_but_EXIT(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2278 {
2279         
2280         if (data->state == BUTTON_STATE_HIGHLIGHT) {
2281
2282                 /* first handle click on icondrag type button */
2283                 if (event->type == LEFTMOUSE && but->dragpoin) {
2284                         if (ui_but_mouse_inside_icon(but, data->region, event)) {
2285                                 
2286                                 /* tell the button to wait and keep checking further events to
2287                                  * see if it should start dragging */
2288                                 button_activate_state(C, but, BUTTON_STATE_WAIT_DRAG);
2289                                 data->dragstartx = event->x;
2290                                 data->dragstarty = event->y;
2291                                 return WM_UI_HANDLER_CONTINUE;
2292                         }
2293                 }
2294                 
2295                 if (ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val == KM_PRESS) {
2296                         int ret = WM_UI_HANDLER_BREAK;
2297                         /* XXX (a bit ugly) Special case handling for filebrowser drag button */
2298                         if (but->dragpoin && but->imb && ui_but_mouse_inside_icon(but, data->region, event)) {
2299                                 ret = WM_UI_HANDLER_CONTINUE;
2300                         }
2301                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2302                         return ret;
2303                 }
2304         }
2305         else if (data->state == BUTTON_STATE_WAIT_DRAG) {
2306                 
2307                 /* this function also ends state */
2308                 if (ui_but_start_drag(C, but, data, event)) {
2309                         return WM_UI_HANDLER_BREAK;
2310                 }
2311                 
2312                 /* If the mouse has been pressed and released, getting to 
2313                  * this point without triggering a drag, then clear the 
2314                  * drag state for this button and continue to pass on the event */
2315                 if (event->type == LEFTMOUSE && event->val == KM_RELEASE) {
2316                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2317                         return WM_UI_HANDLER_CONTINUE;
2318                 }
2319                 
2320                 /* while waiting for a drag to be triggered, always block 
2321                  * other events from getting handled */
2322                 return WM_UI_HANDLER_BREAK;
2323         }
2324         
2325         return WM_UI_HANDLER_CONTINUE;
2326 }
2327
2328 /* var names match ui_numedit_but_NUM */
2329 static float ui_numedit_apply_snapf(uiBut *but, float tempf, float softmin, float softmax, float softrange, int snap)
2330 {
2331         if (tempf == softmin || tempf == softmax || snap == 0) {
2332                 /* pass */
2333         }
2334         else {
2335                 float fac = 1.0f;
2336                 
2337                 if (ui_is_but_unit(but)) {
2338                         UnitSettings *unit = but->block->unit;
2339                         int unit_type = RNA_SUBTYPE_UNIT_VALUE(uiButGetUnitType(but));
2340
2341                         if (bUnit_IsValid(unit->system, unit_type)) {
2342                                 fac = (float)bUnit_BaseScalar(unit->system, unit_type);
2343                                 if (ELEM3(unit_type, B_UNIT_LENGTH, B_UNIT_AREA, B_UNIT_VOLUME)) {
2344                                         fac /= unit->scale_length;
2345                                 }
2346                         }
2347                 }
2348
2349                 if (fac != 1.0f) {
2350                         /* snap in unit-space */
2351                         tempf /= fac;
2352                         /* softmin /= fac; */ /* UNUSED */
2353                         /* softmax /= fac; */ /* UNUSED */
2354                         softrange /= fac;
2355                 }
2356
2357                 if (snap == 1) {
2358                         if (softrange < 2.10f) tempf = 0.1f * floorf(10.0f * tempf);
2359                         else if (softrange < 21.0f) tempf = floorf(tempf);
2360                         else tempf = 10.0f * floorf(tempf / 10.0f);
2361                 }
2362                 else if (snap == 2) {
2363                         if (softrange < 2.10f) tempf = 0.01f * floorf(100.0f * tempf);
2364                         else if (softrange < 21.0f) tempf = 0.1f * floorf(10.0f * tempf);
2365                         else tempf = floor(tempf);
2366                 }
2367                 
2368                 if (fac != 1.0f)
2369                         tempf *= fac;
2370         }
2371
2372         return tempf;
2373 }
2374
2375 static float ui_numedit_apply_snap(int temp, float softmin, float softmax, int snap)
2376 {
2377         if (temp == softmin || temp == softmax)
2378                 return temp;
2379
2380         switch (snap) {
2381                 case 0:
2382                         break;
2383                 case 1:
2384                         temp = 10 * (temp / 10);
2385                         break;
2386                 case 2:
2387                         temp = 100 * (temp / 100);
2388                         break;
2389         }
2390
2391         return temp;
2392 }
2393
2394 static int ui_numedit_but_NUM(uiBut *but, uiHandleButtonData *data, float fac, int snap, int mx)
2395 {
2396         float deler, tempf, softmin, softmax, softrange;
2397         int lvalue, temp, changed = 0;
2398         
2399         if (mx == data->draglastx)
2400                 return changed;
2401         
2402         /* drag-lock - prevent unwanted scroll adjustments */
2403         /* change value (now 3) to adjust threshold in pixels */
2404         if (data->draglock) {
2405                 if (abs(mx - data->dragstartx) <= 3)
2406                         return changed;
2407
2408                 data->draglock = 0;
2409                 data->dragstartx = mx;  /* ignore mouse movement within drag-lock */
2410         }
2411
2412         softmin = but->softmin;
2413         softmax = but->softmax;
2414         softrange = softmax - softmin;
2415
2416         if (ui_is_a_warp_but(but)) {
2417                 /* Mouse location isn't screen clamped to the screen so use a linear mapping
2418                  * 2px == 1-int, or 1px == 1-ClickStep */
2419                 if (ui_is_but_float(but)) {
2420                         fac *= 0.01f * but->a1;
2421                         tempf = (float)data->startvalue + ((float)(mx - data->dragstartx) * fac);
2422                         tempf = ui_numedit_apply_snapf(but, tempf, softmin, softmax, softrange, snap);
2423
2424 #if 1       /* fake moving the click start, nicer for dragging back after passing the limit */
2425                         if (tempf < softmin) {
2426                                 data->dragstartx -= (softmin - tempf) / fac;
2427                                 tempf = softmin;
2428                         }
2429                         else if (tempf > softmax) {
2430                                 data->dragstartx += (tempf - softmax) / fac;
2431                                 tempf = softmax;
2432                         }
2433 #else
2434                         CLAMP(tempf, softmin, softmax);
2435 #endif
2436
2437                         if (tempf != (float)data->value) {
2438                                 data->dragchange = 1;
2439                                 data->value = tempf;
2440                                 changed = 1;
2441                         }
2442                 }
2443                 else {
2444                         if (softrange > 256) fac = 1.0;             /* 1px == 1 */
2445                         else if (softrange > 32) fac = 1.0 / 2.0;   /* 2px == 1 */
2446                         else fac = 1.0 / 16.0;                  /* 16px == 1? */
2447
2448                         temp = data->startvalue + (((double)mx - data->dragstartx) * (double)fac);
2449                         temp = ui_numedit_apply_snap(temp, softmin, softmax, snap);
2450
2451 #if 1       /* fake moving the click start, nicer for dragging back after passing the limit */
2452                         if (temp < softmin) {
2453                                 data->dragstartx -= (softmin - temp) / fac;
2454                                 temp = softmin;
2455                         }
2456                         else if (temp > softmax) {
2457                                 data->dragstartx += (temp - softmax) / fac;
2458                                 temp = softmax;
2459                         }
2460 #else
2461                         CLAMP(temp, softmin, softmax);
2462 #endif
2463
2464                         if (temp != data->value) {
2465                                 data->dragchange = 1;
2466                                 data->value = temp;
2467                                 changed = 1;
2468                         }
2469                 }
2470
2471                 data->draglastx = mx;
2472         }
2473         else {
2474                 /* Use a non-linear mapping of the mouse drag especially for large floats (normal behavior) */
2475                 deler = 500;
2476                 if (!ui_is_but_float(but)) {
2477                         /* prevent large ranges from getting too out of control */
2478                         if (softrange > 600) deler = powf(softrange, 0.75);
2479                         
2480                         if (softrange < 100) deler = 200.0;
2481                         if (softrange < 25) deler = 50.0;
2482                 }
2483                 deler /= fac;
2484
2485                 if (softrange > 11) {
2486                         /* non linear change in mouse input- good for high precicsion */
2487                         data->dragf += (((float)(mx - data->draglastx)) / deler) * (fabsf(data->dragstartx - mx) * 0.002f);
2488                 }
2489                 else if (softrange > 129) { /* only scale large int buttons */
2490                         /* non linear change in mouse input- good for high precicsionm ints need less fine tuning */
2491                         data->dragf += (((float)(mx - data->draglastx)) / deler) * (fabsf(data->dragstartx - mx) * 0.004f);
2492                 }
2493                 else {
2494                         /*no scaling */
2495                         data->dragf += ((float)(mx - data->draglastx)) / deler;
2496                 }
2497         
2498                 CLAMP(data->dragf, 0.0f, 1.0f);
2499                 data->draglastx = mx;
2500                 tempf = (softmin + data->dragf * softrange);
2501
2502
2503                 if (!ui_is_but_float(but)) {
2504                         temp = floorf(tempf + 0.5f);
2505
2506                         temp = ui_numedit_apply_snap(temp, softmin, softmax, snap);
2507
2508                         CLAMP(temp, softmin, softmax);
2509                         lvalue = (int)data->value;
2510                         
2511                         if (temp != lvalue) {
2512                                 data->dragchange = 1;
2513                                 data->value = (double)temp;
2514                                 changed = 1;
2515                         }
2516                 }
2517                 else {
2518                         temp = 0;
2519                         tempf = ui_numedit_apply_snapf(but, tempf, softmin, softmax, softrange, snap);
2520
2521                         CLAMP(tempf, softmin, softmax);
2522
2523                         if (tempf != (float)data->value) {
2524                                 data->dragchange = 1;
2525                                 data->value = tempf;
2526                                 changed = 1;
2527                         }
2528                 }
2529         }
2530
2531
2532         return changed;
2533 }
2534
2535 static int ui_do_but_NUM(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2536 {
2537         int mx, my; /* mouse location scaled to fit the UI */
2538         int screen_mx, screen_my; /* mouse location kept at screen pixel coords */
2539         int click = 0;
2540         int retval = WM_UI_HANDLER_CONTINUE;
2541         
2542         mx = screen_mx = event->x;
2543         my = screen_my = event->y;
2544
2545         ui_window_to_block(data->region, block, &mx, &my);
2546
2547         if (data->state == BUTTON_STATE_HIGHLIGHT) {
2548                 /* XXX hardcoded keymap check.... */
2549                 if (event->type == WHEELDOWNMOUSE && event->alt) {
2550                         mx = but->rect.xmin;
2551                         click = 1;
2552                 }
2553                 else if (event->type == WHEELUPMOUSE && event->alt) {
2554                         mx = but->rect.xmax;
2555                         click = 1;
2556                 }
2557                 else if (event->val == KM_PRESS) {
2558                         if (ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->ctrl) {
2559                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2560                                 retval = WM_UI_HANDLER_BREAK;
2561                         }
2562                         else if (event->type == LEFTMOUSE) {
2563                                 data->dragstartx = data->draglastx = ui_is_a_warp_but(but) ? screen_mx : mx;
2564                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2565                                 retval = WM_UI_HANDLER_BREAK;
2566                         }
2567                         else if (ELEM(event->type, PADENTER, RETKEY) && event->val == KM_PRESS)
2568                                 click = 1;
2569                         else if (event->type == MINUSKEY && event->val == KM_PRESS) {
2570                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2571                                 data->value = -data->value;
2572                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2573                                 retval = WM_UI_HANDLER_BREAK;
2574                         }
2575                 }
2576                 
2577         }
2578         else if (data->state == BUTTON_STATE_NUM_EDITING) {
2579                 if (event->type == ESCKEY) {
2580                         data->cancel = 1;
2581                         data->escapecancel = 1;
2582                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2583                 }
2584                 else if (event->type == LEFTMOUSE && event->val != KM_PRESS) {
2585                         if (data->dragchange)
2586                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2587                         else
2588                                 click = 1;
2589                 }
2590                 else if (event->type == MOUSEMOVE) {
2591                         float fac;
2592                         int snap;
2593
2594                         fac = 1.0f;
2595                         if (event->shift) fac /= 10.0f;
2596                         if (event->alt) fac /= 20.0f;
2597                         
2598                         snap = (event->ctrl) ? (event->shift) ? 2 : 1 : 0;
2599
2600                         if (ui_numedit_but_NUM(but, data, fac, snap, (ui_is_a_warp_but(but) ? screen_mx : mx)))
2601                                 ui_numedit_apply(C, block, but, data);
2602                 }
2603                 retval = WM_UI_HANDLER_BREAK;
2604         }
2605         else if (data->state == BUTTON_STATE_TEXT_EDITING) {
2606                 ui_do_but_textedit(C, block, but, data, event);
2607                 retval = WM_UI_HANDLER_BREAK;
2608         }
2609         else if (data->state == BUTTON_STATE_TEXT_SELECTING) {
2610                 ui_do_but_textedit_select(C, block, but, data, event);
2611                 retval = WM_UI_HANDLER_BREAK;
2612         }
2613         
2614         if (click) {
2615                 /* we can click on the side arrows to increment/decrement,
2616                  * or click inside to edit the value directly */
2617                 float tempf, softmin, softmax;
2618                 int temp;
2619
2620                 softmin = but->softmin;
2621                 softmax = but->softmax;
2622
2623                 if (!ui_is_but_float(but)) {
2624                         if (mx < (but->rect.xmin + BLI_RCT_SIZE_X(&but->rect) / 3 - 3)) {
2625                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2626
2627                                 temp = (int)data->value - 1;
2628                                 if (temp >= softmin && temp <= softmax)
2629                                         data->value = (double)temp;
2630                                 else
2631                                         data->cancel = 1;
2632
2633                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2634                         }
2635                         else if (mx > (but->rect.xmin + (2 * BLI_RCT_SIZE_X(&but->rect) / 3) + 3)) {
2636                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2637
2638                                 temp = (int)data->value + 1;
2639                                 if (temp >= softmin && temp <= softmax)
2640                                         data->value = (double)temp;
2641                                 else
2642                                         data->cancel = 1;
2643
2644                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2645                         }
2646                         else
2647                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2648                 }
2649                 else {
2650                         if (mx < (but->rect.xmin + BLI_RCT_SIZE_X(&but->rect) / 3 - 3)) {
2651                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2652
2653                                 tempf = (float)data->value - 0.01f * but->a1;
2654                                 if (tempf < softmin) tempf = softmin;
2655                                 data->value = tempf;
2656
2657                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2658                         }
2659                         else if (mx > but->rect.xmin + (2 * (BLI_RCT_SIZE_X(&but->rect) / 3) + 3)) {
2660                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2661
2662                                 tempf = (float)data->value + 0.01f * but->a1;
2663                                 if (tempf > softmax) tempf = softmax;
2664                                 data->value = tempf;
2665
2666                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2667                         }
2668                         else
2669                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2670                 }
2671
2672                 retval = WM_UI_HANDLER_BREAK;
2673         }
2674         
2675         return retval;
2676 }
2677
2678 static int ui_numedit_but_SLI(uiBut *but, uiHandleButtonData *data, const short shift, const short ctrl, int mx)
2679 {
2680         float deler, f, tempf, softmin, softmax, softrange;
2681         int temp, lvalue, changed = 0;
2682
2683         softmin = but->softmin;
2684         softmax = but->softmax;
2685         softrange = softmax - softmin;
2686
2687         if (but->type == NUMSLI) deler = (BLI_RCT_SIZE_X(&but->rect) - 5.0f * but->aspect);
2688         else if (but->type == HSVSLI) deler = (BLI_RCT_SIZE_X(&but->rect) / 2.0f - 5.0f * but->aspect);
2689         else if (but->type == SCROLL) {
2690                 int horizontal = (BLI_RCT_SIZE_X(&but->rect) > BLI_RCT_SIZE_Y(&but->rect));
2691                 float size = (horizontal) ? BLI_RCT_SIZE_X(&but->rect) : -BLI_RCT_SIZE_Y(&but->rect);
2692                 deler = size * (but->softmax - but->softmin) / (but->softmax - but->softmin + but->a1);
2693         }
2694         else deler = (BLI_RCT_SIZE_X(&but->rect) - 5.0f * but->aspect);
2695
2696         f = (float)(mx - data->dragstartx) / deler + data->dragfstart;
2697         
2698         if (shift)
2699                 f = (f - data->dragfstart) / 10.0f + data->dragfstart;
2700
2701         CLAMP(f, 0.0f, 1.0f);
2702         tempf = softmin + f * softrange;
2703         temp = floorf(tempf + 0.5f);
2704
2705         if (ctrl) {
2706                 if (tempf == softmin || tempf == softmax) ;
2707                 else if (ui_is_but_float(but)) {
2708
2709                         if (shift) {
2710                                 if (tempf == softmin || tempf == softmax) ;
2711                                 else if (softmax - softmin < 2.10f) tempf = 0.01f * floorf(100.0f * tempf);
2712                                 else if (softmax - softmin < 21.0f) tempf = 0.1f * floorf(10.0f * tempf);
2713                                 else tempf = floorf(tempf);
2714                         }
2715                         else {
2716                                 if (softmax - softmin < 2.10f) tempf = 0.1f * floorf(10.0f * tempf);
2717                                 else if (softmax - softmin < 21.0f) tempf = floorf(tempf);
2718                                 else tempf = 10.0f * floorf(tempf / 10.0f);
2719                         }
2720                 }
2721                 else {
2722                         temp = 10 * (temp / 10);
2723                         tempf = temp;
2724                 }
2725         }
2726
2727         if (!ui_is_but_float(but)) {
2728                 lvalue = floor(data->value + 0.5);
2729
2730                 CLAMP(temp, softmin, softmax);
2731
2732                 if (temp != lvalue) {
2733                         data->value = temp;
2734                         data->dragchange = 1;
2735                         changed = 1;
2736                 }
2737         }
2738         else {
2739                 CLAMP(tempf, softmin, softmax);
2740
2741                 if (tempf != (float)data->value) {
2742                         data->value = tempf;
2743                         data->dragchange = 1;
2744                         changed = 1;
2745                 }
2746         }
2747
2748         return changed;
2749 }
2750
2751 static int ui_do_but_SLI(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2752 {
2753         int mx, my, click = 0;
2754         int retval = WM_UI_HANDLER_CONTINUE;
2755
2756         mx = event->x;
2757         my = event->y;
2758         ui_window_to_block(data->region, block, &mx, &my);
2759
2760         if (data->state == BUTTON_STATE_HIGHLIGHT) {
2761                 /* XXX hardcoded keymap check.... */
2762                 if (event->type == WHEELDOWNMOUSE && event->alt) {
2763                         mx = but->rect.xmin;
2764                         click = 2;
2765                 }
2766                 else if (event->type == WHEELUPMOUSE && event->alt) {
2767                         mx = but->rect.xmax;
2768                         click = 2;
2769                 }
2770                 else if (event->val == KM_PRESS) {
2771                         if (ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->ctrl) {
2772                                 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2773                                 retval = WM_UI_HANDLER_BREAK;
2774                         }
2775                         /* alt-click on sides to get "arrows" like in NUM buttons, and match wheel usage above */
2776                         else if (event->type == LEFTMOUSE && event->alt) {
2777                                 int halfpos = BLI_RCT_CENTER_X(&but->rect);
2778                                 click = 2;
2779                                 if (mx < halfpos)
2780                                         mx = but->rect.xmin;
2781                                 else
2782                                         mx = but->rect.xmax;
2783                         }
2784                         else if (event->type == LEFTMOUSE) {
2785                                 data->dragstartx = mx;
2786                                 data->draglastx = mx;
2787                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2788                                 retval = WM_UI_HANDLER_BREAK;
2789                         }
2790                         else if (ELEM(event->type, PADENTER, RETKEY) && event->val == KM_PRESS)
2791                                 click = 1;
2792                         else if (event->type == MINUSKEY && event->val == KM_PRESS) {
2793                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2794                                 data->value = -data->value;
2795                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2796                                 retval = WM_UI_HANDLER_BREAK;
2797                         }
2798                 }
2799         }
2800         else if (data->state == BUTTON_STATE_NUM_EDITING) {
2801                 if (event->type == ESCKEY) {
2802                         data->cancel = 1;
2803                         data->escapecancel = 1;
2804                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2805                 }
2806                 else if (event->type == LEFTMOUSE && event->val != KM_PRESS) {
2807                         if (data->dragchange)
2808                                 button_activate_state(C, but, BUTTON_STATE_EXIT);
2809                         else
2810                                 click = 1;
2811                 }
2812                 else if (event->type == MOUSEMOVE) {
2813                         if (ui_numedit_but_SLI(but, data, event->shift, event->ctrl, mx))
2814                                 ui_numedit_apply(C, block, but, data);
2815                 }
2816                 retval = WM_UI_HANDLER_BREAK;
2817         }
2818         else if (data->state == BUTTON_STATE_TEXT_EDITING) {
2819                 ui_do_but_textedit(C, block, but, data, event);
2820                 retval = WM_UI_HANDLER_BREAK;
2821         }
2822         else if (data->state == BUTTON_STATE_TEXT_SELECTING) {
2823                 ui_do_but_textedit_select(C, block, but, data, event);
2824                 retval = WM_UI_HANDLER_BREAK;
2825         }
2826
2827         if (click) {
2828                 if (click == 2) {
2829                         /* nudge slider to the left or right */
2830                         float f, tempf, softmin, softmax, softrange;
2831                         int temp;
2832                         
2833                         button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2834                         
2835                         softmin = but->softmin;
2836                         softmax = but->softmax;
2837                         softrange = softmax - softmin;
2838                         
2839                         tempf = data->value;
2840                         temp = (int)data->value;
2841
2842 #if 0
2843                         if (but->type == SLI) {
2844                                 f = (float)(mx - but->rect.xmin) / (BLI_RCT_SIZE_X(&but->rect)); /* same as below */
2845                         }
2846                         else
2847 #endif
2848                         {
2849                                 f = (float)(mx - but->rect.xmin) / (BLI_RCT_SIZE_X(&but->rect));
2850                         }
2851                         
2852                         f = softmin + f * softrange;
2853                         
2854                         if (!ui_is_but_float(but)) {
2855                                 if (f < temp) temp--;
2856                                 else temp++;
2857                                 
2858                                 if (temp >= softmin && temp <= softmax)
2859                                         data->value = temp;
2860                                 else
2861                                         data->cancel = 1;
2862                         } 
2863                         else {
2864                                 if (f < tempf) tempf -= 0.01f;
2865                                 else tempf += 0.01f;
2866                                 
2867                                 if (tempf >= softmin && tempf <= softmax)
2868                                         data->value = tempf;
2869                                 else
2870                                         data->cancel = 1;
2871                         }
2872                         
2873                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2874                         retval = WM_UI_HANDLER_BREAK;
2875                 }
2876                 else {
2877                         /* edit the value directly */
2878                         button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2879                         retval = WM_UI_HANDLER_BREAK;
2880                 }
2881         }
2882         
2883         return retval;
2884 }
2885
2886 static int ui_do_but_SCROLL(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2887 {
2888         int mx, my /*, click= 0 */;
2889         int retval = WM_UI_HANDLER_CONTINUE;
2890         int horizontal = (BLI_RCT_SIZE_X(&but->rect) > BLI_RCT_SIZE_Y(&but->rect));
2891         
2892         mx = event->x;
2893         my = event->y;
2894         ui_window_to_block(data->region, block, &mx, &my);
2895
2896         if (data->state == BUTTON_STATE_HIGHLIGHT) {
2897                 if (event->val == KM_PRESS) {
2898                         if (event->type == LEFTMOUSE) {
2899                                 if (horizontal) {
2900                                         data->dragstartx = mx;
2901                                         data->draglastx = mx;
2902                                 }
2903                                 else {
2904                                         data->dragstartx = my;
2905                                         data->draglastx = my;
2906                                 }
2907                                 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2908                                 retval = WM_UI_HANDLER_BREAK;
2909                         }
2910                         /* UNUSED - otherwise code is ok, add back if needed */
2911 #if 0
2912                         else if (ELEM(event->type, PADENTER, RETKEY) && event->val == KM_PRESS)
2913                                 click = 1;
2914 #endif
2915                 }
2916         }
2917         else if (data->state == BUTTON_STATE_NUM_EDITING) {
2918                 if (event->type == ESCKEY) {
2919                         data->cancel = 1;
2920                         data->escapecancel = 1;
2921                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2922                 }
2923                 else if (event->type == LEFTMOUSE && event->val != KM_PRESS) {
2924                         button_activate_state(C, but, BUTTON_STATE_EXIT);
2925                 }
2926                 else if (event->type == MOUSEMOVE) {
2927                         if (ui_numedit_but_SLI(but, data, 0, 0, (horizontal) ? mx : my))
2928                                 ui_numedit_apply(C, block, but, data);
2929                 }
2930
2931                 retval = WM_UI_HANDLER_BREAK;
2932         }
2933         
2934         return retval;
2935 }
2936
2937
2938 static int ui_do_but_BLOCK(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2939 {
2940         
2941         if (data->state == BUTTON_STATE_HIGHLIGHT) {
2942                 
2943                 /* first handle click on icondrag type button */
2944                 if (event->type == LEFTMOUSE && but->dragpoin && event->val == KM_PRESS) {
2945                         if (ui_but_mouse_inside_icon(but, data->region, event)) {
2946                                 button_activate_state(C, but, BUTTON_STATE_WAIT_DRAG);
2947                                 data->dragstartx = event->x;
2948                                 data->dragstarty = event->y;
2949                                 return WM_UI_HANDLER_BREAK;
2950                         }
2951                 }
2952                 
2953                 /* regular open menu */
2954                 if (ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val == KM_PRESS) {
2955                         button_activate_state(C, but, BUTTON_STATE_MENU_OPEN);
2956                         return WM_UI_HANDLER_BREAK;
2957                 }
2958                 else if (ELEM3(but->type, MENU, ICONROW, ICONTEXTROW)) {
2959                         if (ELEM(event->type, WHEELDOWNMOUSE, WHEELUPMOUSE) && event->alt) {
2960                                 const int direction = (event->type == WHEELDOWNMOUSE) ? -1 : 1;
2961