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