2 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
18 * The Original Code is Copyright (C) 2008 Blender Foundation.
19 * All rights reserved.
21 * Contributor(s): Blender Foundation
23 * ***** END GPL LICENSE BLOCK *****
26 /** \file blender/editors/interface/interface_handlers.c
27 * \ingroup edinterface
39 #include "MEM_guardedalloc.h"
41 #include "DNA_sensor_types.h"
42 #include "DNA_controller_types.h"
43 #include "DNA_actuator_types.h"
45 #include "DNA_object_types.h"
46 #include "DNA_scene_types.h"
49 #include "BLI_blenlib.h"
50 #include "BLI_utildefines.h"
51 #include "BLI_string_cursor_utf8.h"
53 #include "BLF_translation.h"
57 #include "BKE_blender.h"
58 #include "BKE_colortools.h"
59 #include "BKE_context.h"
60 #include "BKE_idprop.h"
61 #include "BKE_report.h"
62 #include "BKE_texture.h"
63 #include "BKE_tracking.h"
66 #include "ED_screen.h"
68 #include "ED_keyframing.h"
70 #include "UI_interface.h"
74 #include "interface_intern.h"
76 #include "RNA_access.h"
81 /* place the mouse at the scaled down location when un-grabbing */
82 #define USE_CONT_MOUSE_CORRECT
85 static void ui_add_smart_controller(bContext *C, uiBut *from, uiBut *to);
86 static void ui_add_link(bContext *C, uiBut *from, uiBut *to);
88 /***************** structs and defines ****************/
90 #define BUTTON_TOOLTIP_DELAY 0.500
91 #define BUTTON_FLASH_DELAY 0.020
92 #define MENU_SCROLL_INTERVAL 0.1
93 #define BUTTON_AUTO_OPEN_THRESH 0.3
94 #define BUTTON_MOUSE_TOWARDS_THRESH 1.0
96 typedef enum uiButtonActivateType {
99 BUTTON_ACTIVATE_APPLY,
100 BUTTON_ACTIVATE_TEXT_EDITING,
102 } uiButtonActivateType;
104 typedef enum uiHandleButtonState {
106 BUTTON_STATE_HIGHLIGHT,
107 BUTTON_STATE_WAIT_FLASH,
108 BUTTON_STATE_WAIT_RELEASE,
109 BUTTON_STATE_WAIT_KEY_EVENT,
110 BUTTON_STATE_NUM_EDITING,
111 BUTTON_STATE_TEXT_EDITING,
112 BUTTON_STATE_TEXT_SELECTING,
113 BUTTON_STATE_MENU_OPEN,
114 BUTTON_STATE_WAIT_DRAG,
116 } uiHandleButtonState;
118 typedef struct uiHandleButtonData {
126 uiHandleButtonState state;
128 /* booleans (could be made into flags) */
129 char cancel, escapecancel;
130 char applied, appliedinteractive;
135 double value, origvalue, startvalue;
136 float vec[3], origvec[3];
137 int togdual, togonly;
142 wmTimer *tooltiptimer;
146 wmTimer *autoopentimer;
148 /* text selection/editing */
149 int maxlen, selextend, selstartx;
151 /* number editing / dragging */
152 int draglastx, draglasty;
153 int dragstartx, dragstarty;
154 int dragchange, draglock, dragsel;
155 float dragf, dragfstart;
158 #ifdef USE_CONT_MOUSE_CORRECT
159 /* when ungrabbing buttons which are #ui_is_a_warp_but(), we may want to position them
160 * FLT_MAX signifies do-nothing, use #ui_block_to_window_fl() to get this into a usable space */
161 float ungrab_mval[2];
164 /* menu open (watch uiFreeActiveButtons) */
165 uiPopupBlockHandle *menu;
168 /* search box (watch uiFreeActiveButtons) */
172 uiButtonActivateType posttype;
174 } uiHandleButtonData;
176 typedef struct uiAfterFunc {
177 struct uiAfterFunc *next, *prev;
179 uiButHandleFunc func;
184 uiButHandleNFunc funcN;
187 uiButHandleRenameFunc rename_func;
191 uiBlockHandleFunc handle_func;
192 void *handle_func_arg;
195 uiMenuHandleFunc butm_func;
199 wmOperatorType *optype;
204 PropertyRNA *rnaprop;
206 bContextStore *context;
208 char undostr[BKE_UNDO_STR_MAX];
213 static int ui_but_contains_pt(uiBut *but, int mx, int my);
214 static int ui_mouse_inside_button(ARegion *ar, uiBut *but, int x, int y);
215 static void button_activate_state(bContext *C, uiBut *but, uiHandleButtonState state);
216 static int ui_handler_region_menu(bContext *C, wmEvent *event, void *userdata);
217 static void ui_handle_button_activate(bContext *C, ARegion *ar, uiBut *but, uiButtonActivateType type);
218 static void button_timers_tooltip_remove(bContext *C, uiBut *but);
220 /* ******************** menu navigation helpers ************** */
222 static int ui_but_editable(uiBut *but)
224 return ELEM5(but->type, LABEL, SEPR, ROUNDBOX, LISTBOX, PROGRESSBAR);
227 static uiBut *ui_but_prev(uiBut *but)
231 if (!ui_but_editable(but)) return but;
236 static uiBut *ui_but_next(uiBut *but)
240 if (!ui_but_editable(but)) return but;
245 static uiBut *ui_but_first(uiBlock *block)
249 but = block->buttons.first;
251 if (!ui_but_editable(but)) return but;
257 static uiBut *ui_but_last(uiBlock *block)
261 but = block->buttons.last;
263 if (!ui_but_editable(but)) return but;
269 static int ui_is_a_warp_but(uiBut *but)
271 if (U.uiflag & USER_CONTINUOUS_MOUSE) {
272 if (ELEM6(but->type, NUM, NUMABS, HSVCIRCLE, TRACKPREVIEW, HSVCUBE, BUT_CURVE)) {
280 static float ui_mouse_scale_warp_factor(const short shift)
282 return shift ? 0.05f : 1.0f;
285 static void ui_mouse_scale_warp(uiHandleButtonData *data,
286 const float mx, const float my,
287 float *r_mx, float *r_my,
290 const float fac = ui_mouse_scale_warp_factor(shift);
292 /* slow down the mouse, this is fairly picky */
293 *r_mx = (data->dragstartx * (1.0f - fac) + mx * fac);
294 *r_my = (data->dragstarty * (1.0f - fac) + my * fac);
297 /* file selectors are exempt from utf-8 checks */
298 int ui_is_but_utf8(uiBut *but)
301 const int subtype = RNA_property_subtype(but->rnaprop);
302 return !(ELEM4(subtype, PROP_FILEPATH, PROP_DIRPATH, PROP_FILENAME, PROP_BYTESTRING));
305 return !(but->flag & UI_BUT_NO_UTF8);
309 /* ********************** button apply/revert ************************/
311 static ListBase UIAfterFuncs = {NULL, NULL};
313 static void ui_apply_but_func(bContext *C, uiBut *but)
316 uiBlock *block = but->block;
318 /* these functions are postponed and only executed after all other
319 * handling is done, i.e. menus are closed, in order to avoid conflicts
320 * with these functions removing the buttons we are working with */
322 if (but->func || but->funcN || block->handle_func || but->rename_func ||
323 (but->type == BUTM && block->butm_func) || but->optype || but->rnaprop)
325 after = MEM_callocN(sizeof(uiAfterFunc), "uiAfterFunc");
327 if (but->func && ELEM(but, but->func_arg1, but->func_arg2)) {
328 /* exception, this will crash due to removed button otherwise */
329 but->func(C, but->func_arg1, but->func_arg2);
332 after->func = but->func;
334 after->func_arg1 = but->func_arg1;
335 after->func_arg2 = but->func_arg2;
336 after->func_arg3 = but->func_arg3;
338 after->funcN = but->funcN;
339 after->func_argN = MEM_dupallocN(but->func_argN);
341 after->rename_func = but->rename_func;
342 after->rename_arg1 = but->rename_arg1;
343 after->rename_orig = but->rename_orig; /* needs free! */
345 after->handle_func = block->handle_func;
346 after->handle_func_arg = block->handle_func_arg;
347 after->retval = but->retval;
349 if (but->type == BUTM) {
350 after->butm_func = block->butm_func;
351 after->butm_func_arg = block->butm_func_arg;
355 after->optype = but->optype;
356 after->opcontext = but->opcontext;
357 after->opptr = but->opptr;
359 after->rnapoin = but->rnapoin;
360 after->rnaprop = but->rnaprop;
363 after->context = CTX_store_copy(but->context);
369 BLI_addtail(&UIAfterFuncs, after);
373 static void ui_apply_autokey_undo(bContext *C, uiBut *but)
375 Scene *scene = CTX_data_scene(C);
378 if (but->flag & UI_BUT_UNDO) {
379 const char *str = NULL;
381 /* define which string to use for undo */
382 if (ELEM(but->type, LINK, INLINK)) str = "Add button link";
383 else if (ELEM(but->type, MENU, ICONTEXTROW)) str = but->drawstr;
384 else if (but->drawstr[0]) str = but->drawstr;
387 /* fallback, else we don't get an undo! */
388 if (str == NULL || str[0] == '\0') {
389 str = "Unknown Action";
392 /* delayed, after all other funcs run, popups are closed, etc */
393 after = MEM_callocN(sizeof(uiAfterFunc), "uiAfterFunc");
394 BLI_strncpy(after->undostr, str, sizeof(after->undostr));
395 BLI_addtail(&UIAfterFuncs, after);
399 ui_but_anim_autokey(C, but, scene, scene->r.cfra);
401 /* make a little report about what we've done! */
403 char *buf = WM_prop_pystring_assign(C, &but->rnapoin, but->rnaprop, but->rnaindex);
405 BKE_report(CTX_wm_reports(C), RPT_PROPERTY, buf);
408 WM_event_add_notifier(C, NC_SPACE | ND_SPACE_INFO_REPORT, NULL);
413 static void ui_apply_but_funcs_after(bContext *C)
415 uiAfterFunc *afterf, after;
419 /* copy to avoid recursive calls */
420 funcs = UIAfterFuncs;
421 UIAfterFuncs.first = UIAfterFuncs.last = NULL;
423 for (afterf = funcs.first; afterf; afterf = after.next) {
424 after = *afterf; /* copy to avoid memleak on exit() */
425 BLI_freelinkN(&funcs, afterf);
428 CTX_store_set(C, after.context);
431 /* free in advance to avoid leak on exit */
432 opptr = *after.opptr,
433 MEM_freeN(after.opptr);
437 WM_operator_name_call(C, after.optype->idname, after.opcontext, (after.opptr) ? &opptr : NULL);
440 WM_operator_properties_free(&opptr);
442 if (after.rnapoin.data)
443 RNA_property_update(C, &after.rnapoin, after.rnaprop);
446 CTX_store_set(C, NULL);
447 CTX_store_free(after.context);
451 after.func(C, after.func_arg1, after.func_arg2);
453 after.funcN(C, after.func_argN, after.func_arg2);
455 MEM_freeN(after.func_argN);
457 if (after.handle_func)
458 after.handle_func(C, after.handle_func_arg, after.retval);
460 after.butm_func(C, after.butm_func_arg, after.a2);
462 if (after.rename_func)
463 after.rename_func(C, after.rename_arg1, after.rename_orig);
464 if (after.rename_orig)
465 MEM_freeN(after.rename_orig);
467 if (after.undostr[0])
468 ED_undo_push(C, after.undostr);
472 static void ui_apply_but_BUT(bContext *C, uiBut *but, uiHandleButtonData *data)
474 ui_apply_but_func(C, but);
476 data->retval = but->retval;
477 data->applied = TRUE;
480 static void ui_apply_but_BUTM(bContext *C, uiBut *but, uiHandleButtonData *data)
482 ui_set_but_val(but, but->hardmin);
483 ui_apply_but_func(C, but);
485 data->retval = but->retval;
486 data->applied = TRUE;
489 static void ui_apply_but_BLOCK(bContext *C, uiBut *but, uiHandleButtonData *data)
491 if (ELEM3(but->type, MENU, ICONROW, ICONTEXTROW))
492 ui_set_but_val(but, data->value);
495 ui_apply_but_func(C, but);
496 data->retval = but->retval;
497 data->applied = TRUE;
500 static void ui_apply_but_TOG(bContext *C, uiBut *but, uiHandleButtonData *data)
506 if (but->type == BUT_TOGDUAL && data->togdual) {
507 if (but->pointype == UI_BUT_POIN_SHORT) {
510 else if (but->pointype == UI_BUT_POIN_INT) {
515 value = ui_get_but_val(but);
519 w = UI_BITBUT_TEST(lvalue, but->bitnr);
520 if (w) lvalue = UI_BITBUT_CLR(lvalue, but->bitnr);
521 else lvalue = UI_BITBUT_SET(lvalue, but->bitnr);
523 if (but->type == TOGR) {
524 if (!data->togonly) {
525 lvalue = 1 << (but->bitnr);
527 ui_set_but_val(but, (double)lvalue);
530 if (lvalue == 0) lvalue = 1 << (but->bitnr);
534 ui_set_but_val(but, (double)lvalue);
535 if (but->type == ICONTOG || but->type == ICONTOGN) ui_check_but(but);
539 if (value == 0.0) push = 1;
542 if (ELEM3(but->type, TOGN, ICONTOGN, OPTIONN)) push = !push;
543 ui_set_but_val(but, (double)push);
544 if (but->type == ICONTOG || but->type == ICONTOGN) ui_check_but(but);
547 /* end local hack... */
548 if (but->type == BUT_TOGDUAL && data->togdual) {
549 if (but->pointype == UI_BUT_POIN_SHORT) {
552 else if (but->pointype == UI_BUT_POIN_INT) {
557 ui_apply_but_func(C, but);
559 data->retval = but->retval;
560 data->applied = TRUE;
563 static void ui_apply_but_ROW(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data)
567 ui_set_but_val(but, but->hardmax);
569 /* states of other row buttons */
570 for (bt = block->buttons.first; bt; bt = bt->next)
571 if (bt != but && bt->poin == but->poin && ELEM(bt->type, ROW, LISTROW))
574 ui_apply_but_func(C, but);
576 data->retval = but->retval;
577 data->applied = TRUE;
580 static void ui_apply_but_TEX(bContext *C, uiBut *but, uiHandleButtonData *data)
585 ui_set_but_string(C, but, data->str);
588 /* give butfunc the original text too */
589 /* feature used for bone renaming, channels, etc */
590 /* afterfunc frees origstr */
591 but->rename_orig = data->origstr;
592 data->origstr = NULL;
593 ui_apply_but_func(C, but);
595 data->retval = but->retval;
596 data->applied = TRUE;
599 static void ui_apply_but_NUM(bContext *C, uiBut *but, uiHandleButtonData *data)
602 if (ui_set_but_string(C, but, data->str)) {
603 data->value = ui_get_but_val(but);
611 ui_set_but_val(but, data->value);
614 ui_apply_but_func(C, but);
616 data->retval = but->retval;
617 data->applied = TRUE;
620 static void ui_apply_but_TOG3(bContext *C, uiBut *but, uiHandleButtonData *data)
622 if (but->pointype == UI_BUT_POIN_SHORT) {
623 short *sp = (short *)but->poin;
625 if (UI_BITBUT_TEST(sp[1], but->bitnr)) {
626 sp[1] = UI_BITBUT_CLR(sp[1], but->bitnr);
627 sp[0] = UI_BITBUT_CLR(sp[0], but->bitnr);
629 else if (UI_BITBUT_TEST(sp[0], but->bitnr)) {
630 sp[1] = UI_BITBUT_SET(sp[1], but->bitnr);
633 sp[0] = UI_BITBUT_SET(sp[0], but->bitnr);
637 if (UI_BITBUT_TEST(*(but->poin + 2), but->bitnr)) {
638 *(but->poin + 2) = UI_BITBUT_CLR(*(but->poin + 2), but->bitnr);
639 *(but->poin) = UI_BITBUT_CLR(*(but->poin), but->bitnr);
641 else if (UI_BITBUT_TEST(*(but->poin), but->bitnr)) {
642 *(but->poin + 2) = UI_BITBUT_SET(*(but->poin + 2), but->bitnr);
645 *(but->poin) = UI_BITBUT_SET(*(but->poin), but->bitnr);
650 ui_apply_but_func(C, but);
651 data->retval = but->retval;
652 data->applied = TRUE;
655 static void ui_apply_but_VEC(bContext *C, uiBut *but, uiHandleButtonData *data)
657 ui_set_but_vectorf(but, data->vec);
659 ui_apply_but_func(C, but);
661 data->retval = but->retval;
662 data->applied = TRUE;
665 static void ui_apply_but_COLORBAND(bContext *C, uiBut *but, uiHandleButtonData *data)
667 ui_apply_but_func(C, but);
668 data->retval = but->retval;
669 data->applied = TRUE;
672 static void ui_apply_but_CURVE(bContext *C, uiBut *but, uiHandleButtonData *data)
674 ui_apply_but_func(C, but);
675 data->retval = but->retval;
676 data->applied = TRUE;
679 static void ui_apply_but_IDPOIN(bContext *C, uiBut *but, uiHandleButtonData *data)
681 ui_set_but_string(C, but, data->str);
683 ui_apply_but_func(C, but);
684 data->retval = but->retval;
685 data->applied = TRUE;
688 #ifdef WITH_INTERNATIONAL
689 static void ui_apply_but_CHARTAB(bContext *C, uiBut *but, uiHandleButtonData *data)
691 ui_apply_but_func(C, but);
692 data->retval = but->retval;
693 data->applied = TRUE;
697 /* ****************** drag drop code *********************** */
699 static int ui_but_mouse_inside_icon(uiBut *but, ARegion *ar, wmEvent *event)
702 int x = event->x, y = event->y;
704 ui_window_to_block(ar, but->block, &x, &y);
706 BLI_rcti_rctf_copy(&rect, &but->rect);
709 /* use button size itself */
711 else if (but->flag & UI_ICON_LEFT) {
712 rect.xmax = rect.xmin + (BLI_rcti_size_y(&rect));
715 int delta = BLI_rcti_size_x(&rect) - BLI_rcti_size_y(&rect);
716 rect.xmin += delta / 2;
717 rect.xmax -= delta / 2;
720 return BLI_rcti_isect_pt(&rect, x, y);
723 static int ui_but_start_drag(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
725 /* prevent other WM gestures to start while we try to drag */
726 WM_gestures_remove(C);
728 if (ABS(data->dragstartx - event->x) + ABS(data->dragstarty - event->y) > U.dragthreshold) {
731 button_activate_state(C, but, BUTTON_STATE_EXIT);
734 drag = WM_event_start_drag(C, but->icon, but->dragtype, but->dragpoin, ui_get_but_val(but));
736 WM_event_drag_image(drag, but->imb, but->imb_scale, BLI_rctf_size_x(&but->rect), BLI_rctf_size_y(&but->rect));
743 /* ********************** linklines *********************** */
745 static void ui_delete_active_linkline(uiBlock *block)
749 uiLinkLine *line, *nline;
752 for (but = block->buttons.first; but; but = but->next) {
753 if (but->type == LINK && but->link) {
754 for (line = but->link->lines.first; line; line = nline) {
757 if (line->flag & UI_SELECT) {
758 BLI_remlink(&but->link->lines, line);
760 link = line->from->link;
762 /* are there more pointers allowed? */
765 if (*(link->totlink) == 1) {
766 *(link->totlink) = 0;
767 MEM_freeN(*(link->ppoin));
768 *(link->ppoin) = NULL;
772 for (a = 0; a < (*(link->totlink)); a++) {
774 if ((*(link->ppoin))[a] != line->to->poin) {
775 (*(link->ppoin))[b] = (*(link->ppoin))[a];
779 (*(link->totlink))--;
783 *(link->poin) = NULL;
794 static uiLinkLine *ui_is_a_link(uiBut *from, uiBut *to)
801 for (line = link->lines.first; line; line = line->next) {
802 if (line->from == from && line->to == to) {
810 /* XXX BAD BAD HACK, fixme later **************** */
811 /* Try to add an AND Controller between the sensor and the actuator logic bricks and to connect them all */
812 static void ui_add_smart_controller(bContext *C, uiBut *from, uiBut *to)
816 bActuator *act_to, *act_iter;
818 bController ***sens_from_links;
821 uiLink *link = from->link;
823 PointerRNA props_ptr, object_ptr;
826 sens_from_links = (bController ***)(link->ppoin);
829 act_to = (bActuator *)(to->poin);
831 /* (1) get the object */
832 CTX_DATA_BEGIN (C, Object *, ob_iter, selected_editable_objects)
834 for (sens_iter = ob_iter->sensors.first; sens_iter; sens_iter = sens_iter->next) {
835 if (&(sens_iter->links) == sens_from_links) {
845 /* (2) check if the sensor and the actuator are from the same object */
846 for (act_iter = ob->actuators.first; act_iter; act_iter = (bActuator *)act_iter->next) {
847 if (act_iter == act_to)
851 /* only works if the sensor and the actuator are from the same object */
852 if (!act_iter) return;
854 /* in case the linked controller is not the active one */
855 RNA_pointer_create((ID *)ob, &RNA_Object, ob, &object_ptr);
857 WM_operator_properties_create(&props_ptr, "LOGIC_OT_controller_add");
858 RNA_string_set(&props_ptr, "object", ob->id.name + 2);
860 /* (3) add a new controller */
861 if (WM_operator_name_call(C, "LOGIC_OT_controller_add", WM_OP_EXEC_DEFAULT, &props_ptr) & OPERATOR_FINISHED) {
862 cont = (bController *)ob->controllers.last;
863 cont->type = CONT_LOGIC_AND; /* Quick fix to make sure we always have an AND controller. It might be nicer to make sure the operator gives us the right one though... */
865 /* (4) link the sensor->controller->actuator */
866 tmp_but = MEM_callocN(sizeof(uiBut), "uiBut");
867 uiSetButLink(tmp_but, (void **)&cont, (void ***)&(cont->links), &(cont->totlinks), from->link->tocode, (int)to->hardmin);
868 tmp_but->hardmin = from->link->tocode;
869 tmp_but->poin = (char *)cont;
871 tmp_but->type = INLINK;
872 ui_add_link(C, from, tmp_but);
874 tmp_but->type = LINK;
875 ui_add_link(C, tmp_but, to);
877 /* (5) garbage collection */
878 MEM_freeN(tmp_but->link);
881 WM_operator_properties_free(&props_ptr);
884 static void ui_add_link(bContext *C, uiBut *from, uiBut *to)
886 /* in 'from' we have to add a link to 'to' */
892 if ((line = ui_is_a_link(from, to))) {
893 line->flag |= UI_SELECT;
894 ui_delete_active_linkline(from->block);
898 if (from->type == INLINK && to->type == INLINK) {
901 else if (from->type == LINK && to->type == INLINK) {
902 if (from->link->tocode != (int)to->hardmin) {
903 ui_add_smart_controller(C, from, to);
907 else if (from->type == INLINK && to->type == LINK) {
908 if (to->link->tocode == (int)from->hardmin) {
915 /* are there more pointers allowed? */
917 oldppoin = *(link->ppoin);
919 (*(link->totlink))++;
920 *(link->ppoin) = MEM_callocN(*(link->totlink) * sizeof(void *), "new link");
922 for (a = 0; a < (*(link->totlink)) - 1; a++) {
923 (*(link->ppoin))[a] = oldppoin[a];
925 (*(link->ppoin))[a] = to->poin;
927 if (oldppoin) MEM_freeN(oldppoin);
930 *(link->poin) = to->poin;
936 static void ui_apply_but_LINK(bContext *C, uiBut *but, uiHandleButtonData *data)
938 ARegion *ar = CTX_wm_region(C);
941 for (bt = but->block->buttons.first; bt; bt = bt->next) {
942 if (ui_mouse_inside_button(ar, bt, but->linkto[0] + ar->winrct.xmin, but->linkto[1] + ar->winrct.ymin) )
945 if (bt && bt != but) {
946 if (!ELEM(bt->type, LINK, INLINK) || !ELEM(but->type, LINK, INLINK))
949 if (but->type == LINK) ui_add_link(C, but, bt);
950 else ui_add_link(C, bt, but);
952 ui_apply_but_func(C, but);
953 data->retval = but->retval;
955 data->applied = TRUE;
958 static void ui_apply_but_IMAGE(bContext *C, uiBut *but, uiHandleButtonData *data)
960 ui_apply_but_func(C, but);
961 data->retval = but->retval;
962 data->applied = TRUE;
965 static void ui_apply_but_HISTOGRAM(bContext *C, uiBut *but, uiHandleButtonData *data)
967 ui_apply_but_func(C, but);
968 data->retval = but->retval;
969 data->applied = TRUE;
972 static void ui_apply_but_WAVEFORM(bContext *C, uiBut *but, uiHandleButtonData *data)
974 ui_apply_but_func(C, but);
975 data->retval = but->retval;
976 data->applied = TRUE;
979 static void ui_apply_but_TRACKPREVIEW(bContext *C, uiBut *but, uiHandleButtonData *data)
981 ui_apply_but_func(C, but);
982 data->retval = but->retval;
983 data->applied = TRUE;
987 static void ui_apply_button(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, int interactive)
993 CurveMapping *editcumap;
997 /* if we cancel and have not applied yet, there is nothing to do,
998 * otherwise we have to restore the original value again */
1003 if (data->str) MEM_freeN(data->str);
1004 data->str = data->origstr;
1005 data->origstr = NULL;
1006 data->value = data->origvalue;
1007 data->origvalue = 0.0;
1008 copy_v3_v3(data->vec, data->origvec);
1009 data->origvec[0] = data->origvec[1] = data->origvec[2] = 0.0f;
1012 /* we avoid applying interactive edits a second time
1013 * at the end with the appliedinteractive flag */
1015 data->appliedinteractive = TRUE;
1017 else if (data->appliedinteractive) {
1022 /* ensures we are writing actual values */
1023 editstr = but->editstr;
1024 editval = but->editval;
1025 editvec = but->editvec;
1026 editcoba = but->editcoba;
1027 editcumap = but->editcumap;
1028 but->editstr = NULL;
1029 but->editval = NULL;
1030 but->editvec = NULL;
1031 but->editcoba = NULL;
1032 but->editcumap = NULL;
1034 /* handle different types */
1035 switch (but->type) {
1037 ui_apply_but_BUT(C, but, data);
1041 ui_apply_but_TEX(C, but, data);
1052 ui_apply_but_TOG(C, but, data);
1056 ui_apply_but_ROW(C, block, but, data);
1063 ui_apply_but_NUM(C, but, data);
1068 ui_apply_but_TOG3(C, but, data);
1075 ui_apply_but_BLOCK(C, but, data);
1079 ui_apply_but_VEC(C, but, data);
1081 ui_apply_but_BLOCK(C, but, data);
1084 ui_apply_but_BUTM(C, but, data);
1089 ui_apply_but_VEC(C, but, data);
1092 ui_apply_but_COLORBAND(C, but, data);
1095 ui_apply_but_CURVE(C, but, data);
1098 ui_apply_but_IDPOIN(C, but, data);
1100 #ifdef WITH_INTERNATIONAL
1102 ui_apply_but_CHARTAB(C, but, data);
1107 ui_apply_but_BUT(C, but, data);
1111 ui_apply_but_LINK(C, but, data);
1114 ui_apply_but_IMAGE(C, but, data);
1117 ui_apply_but_HISTOGRAM(C, but, data);
1120 ui_apply_but_WAVEFORM(C, but, data);
1123 ui_apply_but_TRACKPREVIEW(C, but, data);
1129 but->editstr = editstr;
1130 but->editval = editval;
1131 but->editvec = editvec;
1132 but->editcoba = editcoba;
1133 but->editcumap = editcumap;
1136 /* ******************* drop event ******************** */
1138 /* only call if event type is EVT_DROP */
1139 static void ui_but_drop(bContext *C, wmEvent *event, uiBut *but, uiHandleButtonData *data)
1142 ListBase *drags = event->customdata; /* drop event type has listbase customdata by default */
1144 for (wmd = drags->first; wmd; wmd = wmd->next) {
1145 if (wmd->type == WM_DRAG_ID) {
1146 /* align these types with UI_but_active_drop_name */
1147 if (ELEM3(but->type, TEX, IDPOIN, SEARCH_MENU)) {
1148 ID *id = (ID *)wmd->poin;
1150 if (but->poin == NULL && but->rnapoin.data == NULL) {}
1151 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
1152 BLI_strncpy(data->str, id->name + 2, data->maxlen);
1153 button_activate_state(C, but, BUTTON_STATE_EXIT);
1160 /* ******************* copy and paste ******************** */
1162 /* c = copy, v = paste */
1163 static void ui_but_copy_paste(bContext *C, uiBut *but, uiHandleButtonData *data, char mode)
1165 static ColorBand but_copypaste_coba = {0};
1166 char buf[UI_MAX_DRAW_STR + 1] = {0};
1168 if (mode == 'v' && but->lock == TRUE) {
1173 /* extract first line from clipboard in case of multi-line copies */
1174 char *p, *pbuf = WM_clipboard_text_get(0);
1178 while (*p && *p != '\r' && *p != '\n' && i < UI_MAX_DRAW_STR) {
1188 if (ELEM4(but->type, NUM, NUMABS, NUMSLI, HSVSLI)) {
1190 if (but->poin == NULL && but->rnapoin.data == NULL) {
1193 else if (mode == 'c') {
1194 ui_get_but_string(but, buf, sizeof(buf));
1195 WM_clipboard_text_set(buf, 0);
1200 if (ui_set_but_string_eval_num(C, but, buf, &val)) {
1201 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
1203 ui_set_but_string(C, but, buf);
1204 button_activate_state(C, but, BUTTON_STATE_EXIT);
1210 else if (but->type == COLOR) {
1213 if (but->poin == NULL && but->rnapoin.data == NULL) {
1216 else if (mode == 'c') {
1218 ui_get_but_vectorf(but, rgb);
1219 /* convert to linear color to do compatible copy between gamma and non-gamma */
1220 if (but->rnaprop && RNA_property_subtype(but->rnaprop) == PROP_COLOR_GAMMA)
1221 srgb_to_linearrgb_v3_v3(rgb, rgb);
1223 BLI_snprintf(buf, sizeof(buf), "[%f, %f, %f]", rgb[0], rgb[1], rgb[2]);
1224 WM_clipboard_text_set(buf, 0);
1228 if (sscanf(buf, "[%f, %f, %f]", &rgb[0], &rgb[1], &rgb[2]) == 3) {
1229 /* assume linear colors in buffer */
1230 if (but->rnaprop && RNA_property_subtype(but->rnaprop) == PROP_COLOR_GAMMA)
1231 linearrgb_to_srgb_v3_v3(rgb, rgb);
1233 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
1234 ui_set_but_vectorf(but, rgb);
1235 button_activate_state(C, but, BUTTON_STATE_EXIT);
1240 /* text/string and ID data */
1241 else if (ELEM3(but->type, TEX, IDPOIN, SEARCH_MENU)) {
1242 uiHandleButtonData *active_data = but->active;
1244 if (but->poin == NULL && but->rnapoin.data == NULL) {
1247 else if (mode == 'c') {
1248 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
1249 BLI_strncpy(buf, active_data->str, UI_MAX_DRAW_STR);
1250 WM_clipboard_text_set(active_data->str, 0);
1251 active_data->cancel = TRUE;
1252 button_activate_state(C, but, BUTTON_STATE_EXIT);
1255 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
1257 if (ui_is_but_utf8(but)) BLI_strncpy_utf8(active_data->str, buf, active_data->maxlen);
1258 else BLI_strncpy(active_data->str, buf, active_data->maxlen);
1260 if (but->type == SEARCH_MENU) {
1261 /* else uiSearchboxData.active member is not updated [#26856] */
1262 ui_searchbox_update(C, data->searchbox, but, 1);
1264 button_activate_state(C, but, BUTTON_STATE_EXIT);
1267 /* colorband (not supported by system clipboard) */
1268 else if (but->type == BUT_COLORBAND) {
1270 if (but->poin == NULL)
1273 memcpy(&but_copypaste_coba, but->poin, sizeof(ColorBand));
1276 if (but_copypaste_coba.tot == 0)
1280 but->poin = MEM_callocN(sizeof(ColorBand), "colorband");
1282 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
1283 memcpy(data->coba, &but_copypaste_coba, sizeof(ColorBand));
1284 button_activate_state(C, but, BUTTON_STATE_EXIT);
1287 /* operator button (any type) */
1288 else if (but->optype) {
1292 opptr = uiButGetOperatorPtrRNA(but); /* allocated when needed, the button owns it */
1294 str = WM_operator_pystring(C, but->optype, opptr, 0);
1296 WM_clipboard_text_set(str, 0);
1303 /* ************************ password text ******************************
1305 * Functions to convert password strings that should not be displayed
1306 * to asterisk representation (e.g. mysecretpasswd -> *************)
1308 * It converts every UTF-8 character to an asterisk, and also remaps
1309 * the cursor position and selection start/end.
1311 * Note: remaping is used, because password could contain UTF-8 characters.
1315 static int ui_text_position_from_hidden(uiBut *but, int pos)
1320 for (i = 0, strpos = but->drawstr; i < pos; i++)
1321 strpos = BLI_str_find_next_char_utf8(strpos, NULL);
1323 return (strpos - but->drawstr);
1326 static int ui_text_position_to_hidden(uiBut *but, int pos)
1328 return BLI_strnlen_utf8(but->drawstr, pos);
1331 void ui_button_text_password_hide(char password_str[UI_MAX_DRAW_STR], uiBut *but, int restore)
1333 if (!(but->rnaprop && RNA_property_subtype(but->rnaprop) == PROP_PASSWORD))
1337 /* restore original string */
1338 BLI_strncpy(but->drawstr, password_str, UI_MAX_DRAW_STR);
1340 /* remap cursor positions */
1341 if (but->pos >= 0) {
1342 but->pos = ui_text_position_from_hidden(but, but->pos);
1343 but->selsta = ui_text_position_from_hidden(but, but->selsta);
1344 but->selend = ui_text_position_from_hidden(but, but->selend);
1348 /* convert text to hidden test using asterisks (e.g. pass -> ****) */
1349 int i, len = BLI_strlen_utf8(but->drawstr);
1351 /* remap cursor positions */
1352 if (but->pos >= 0) {
1353 but->pos = ui_text_position_to_hidden(but, but->pos);
1354 but->selsta = ui_text_position_to_hidden(but, but->selsta);
1355 but->selend = ui_text_position_to_hidden(but, but->selend);
1358 /* save original string */
1359 BLI_strncpy(password_str, but->drawstr, UI_MAX_DRAW_STR);
1361 for (i = 0; i < len; i++)
1362 but->drawstr[i] = '*';
1363 but->drawstr[i] = '\0';
1368 /* ************* in-button text selection/editing ************* */
1371 static int ui_textedit_delete_selection(uiBut *but, uiHandleButtonData *data)
1373 char *str = data->str;
1374 int len = strlen(str);
1376 if (but->selsta != but->selend && len) {
1377 memmove(str + but->selsta, str + but->selend, (len - but->selend) + 1);
1381 but->pos = but->selend = but->selsta;
1385 /* note, but->block->aspect is used here, when drawing button style is getting scaled too */
1386 static void ui_textedit_set_cursor_pos(uiBut *but, uiHandleButtonData *data, short x)
1388 uiStyle *style = UI_GetStyle(); // XXX pass on as arg
1389 uiFontStyle *fstyle = &style->widget;
1390 int startx = but->rect.xmin;
1391 char *origstr, password_str[UI_MAX_DRAW_STR];
1393 uiStyleFontSet(fstyle);
1395 if (fstyle->kerning == 1) /* for BLF_width */
1396 BLF_enable(fstyle->uifont_id, BLF_KERNING_DEFAULT);
1398 ui_button_text_password_hide(password_str, but, FALSE);
1400 origstr = MEM_callocN(sizeof(char) * data->maxlen, "ui_textedit origstr");
1402 BLI_strncpy(origstr, but->drawstr, data->maxlen);
1404 /* XXX solve generic */
1405 if (but->type == NUM || but->type == NUMSLI)
1406 startx += (int)(0.5f * (BLI_rctf_size_y(&but->rect)));
1407 else if (ELEM(but->type, TEX, SEARCH_MENU)) {
1409 if (but->flag & UI_HAS_ICON)
1410 startx += UI_DPI_ICON_SIZE;
1413 /* mouse dragged outside the widget to the left */
1414 if (x < startx && but->ofs > 0) {
1417 origstr[but->ofs] = 0;
1420 if (BLI_str_cursor_step_prev_utf8(origstr, but->ofs, &i)) {
1421 /* 0.25 == scale factor for less sensitivity */
1422 if (BLF_width(fstyle->uifont_id, origstr + i) > (startx - x) * 0.25f) {
1427 break; /* unlikely but possible */
1431 but->pos = but->ofs;
1433 /* mouse inside the widget */
1434 else if (x >= startx) {
1437 /* keep track of previous distance from the cursor to the char */
1438 float cdist, cdist_prev = 0.0f;
1441 const float aspect_sqrt = sqrtf(but->block->aspect);
1443 but->pos = pos_prev = strlen(origstr) - but->ofs;
1446 /* XXX does not take zoom level into account */
1447 cdist = startx + aspect_sqrt * BLF_width(fstyle->uifont_id, origstr + but->ofs);
1449 /* check if position is found */
1451 /* check is previous location was in fact closer */
1452 if (((float)x - cdist) > (cdist_prev - (float)x)) {
1453 but->pos = pos_prev;
1458 pos_prev = but->pos;
1459 /* done with tricky distance checks */
1462 if (but->pos <= 0) break;
1463 if (BLI_str_cursor_step_prev_utf8(origstr, but->ofs, &pos_i)) {
1465 origstr[but->pos + but->ofs] = 0;
1468 break; /* unlikely but possible */
1471 but->pos += but->ofs;
1472 if (but->pos < 0) but->pos = 0;
1475 if (fstyle->kerning == 1)
1476 BLF_disable(fstyle->uifont_id, BLF_KERNING_DEFAULT);
1478 ui_button_text_password_hide(password_str, but, TRUE);
1483 static void ui_textedit_set_cursor_select(uiBut *but, uiHandleButtonData *data, short x)
1485 if (x > data->selstartx) data->selextend = EXTEND_RIGHT;
1486 else if (x < data->selstartx) data->selextend = EXTEND_LEFT;
1488 ui_textedit_set_cursor_pos(but, data, x);
1490 if (data->selextend == EXTEND_RIGHT) but->selend = but->pos;
1491 else if (data->selextend == EXTEND_LEFT) but->selsta = but->pos;
1496 /* this is used for both utf8 and ascii, its meant to be used for single keys,
1497 * notice the buffer is either copied or not, so its not suitable for pasting in
1499 static int ui_textedit_type_buf(uiBut *but, uiHandleButtonData *data,
1500 const char *utf8_buf, int utf8_buf_len)
1503 int len, changed = 0;
1508 if (len - (but->selend - but->selsta) + 1 <= data->maxlen) {
1509 int step = utf8_buf_len;
1511 /* type over the current selection */
1512 if ((but->selend - but->selsta) > 0) {
1513 changed = ui_textedit_delete_selection(but, data);
1517 if (len + step < data->maxlen) {
1518 memmove(&str[but->pos + step], &str[but->pos], (len + 1) - but->pos);
1519 memcpy(&str[but->pos], utf8_buf, step * sizeof(char));
1528 static int ui_textedit_type_ascii(uiBut *but, uiHandleButtonData *data, char ascii)
1530 char buf[2] = {ascii, '\0'};
1532 if (ui_is_but_utf8(but) && (BLI_str_utf8_size(buf) == -1)) {
1533 printf("%s: entering invalid ascii char into an ascii key (%d)\n",
1534 __func__, (int)(unsigned char)ascii);
1539 /* in some cases we want to allow invalid utf8 chars */
1540 return ui_textedit_type_buf(but, data, buf, 1);
1543 static void ui_textedit_move(uiBut *but, uiHandleButtonData *data, strCursorJumpDirection direction,
1544 int select, strCursorJumpType jump)
1546 const char *str = data->str;
1547 const int len = strlen(str);
1548 const int pos_prev = but->pos;
1549 const int has_sel = (but->selend - but->selsta) > 0;
1553 /* special case, quit selection and set cursor */
1554 if (has_sel && !select) {
1555 if (jump == STRCUR_JUMP_ALL) {
1556 but->selsta = but->selend = but->pos = direction ? len : 0;
1560 but->selsta = but->pos = but->selend;
1563 but->pos = but->selend = but->selsta;
1566 data->selextend = 0;
1569 int pos_i = but->pos;
1570 BLI_str_cursor_step_utf8(str, len, &pos_i, direction, jump);
1574 /* existing selection */
1577 if (data->selextend == 0) {
1578 data->selextend = EXTEND_RIGHT;
1582 if (data->selextend == EXTEND_RIGHT) {
1583 but->selend = but->pos;
1586 but->selsta = but->pos;
1590 if (data->selextend == EXTEND_LEFT) {
1591 but->selsta = but->pos;
1594 but->selend = but->pos;
1598 if (but->selend < but->selsta) {
1599 SWAP(short, but->selsta, but->selend);
1600 data->selextend = (data->selextend == EXTEND_RIGHT) ? EXTEND_LEFT : EXTEND_RIGHT;
1603 } /* new selection */
1606 data->selextend = EXTEND_RIGHT;
1607 but->selend = but->pos;
1608 but->selsta = pos_prev;
1611 data->selextend = EXTEND_LEFT;
1612 but->selend = pos_prev;
1613 but->selsta = but->pos;
1620 static int ui_textedit_delete(uiBut *but, uiHandleButtonData *data, int direction, strCursorJumpType jump)
1622 char *str = data->str;
1623 const int len = strlen(str);
1627 if (jump == STRCUR_JUMP_ALL) {
1628 if (len) changed = 1;
1632 else if (direction) { /* delete */
1633 if ((but->selend - but->selsta) > 0) {
1634 changed = ui_textedit_delete_selection(but, data);
1636 else if (but->pos >= 0 && but->pos < len) {
1639 BLI_str_cursor_step_utf8(str, len, &pos, direction, jump);
1640 step = pos - but->pos;
1641 memmove(&str[but->pos], &str[but->pos + step], (len + 1) - but->pos);
1645 else { /* backspace */
1647 if ((but->selend - but->selsta) > 0) {
1648 changed = ui_textedit_delete_selection(but, data);
1650 else if (but->pos > 0) {
1654 BLI_str_cursor_step_utf8(str, len, &pos, direction, jump);
1655 step = but->pos - pos;
1656 memmove(&str[but->pos - step], &str[but->pos], (len + 1) - but->pos);
1666 static int ui_textedit_autocomplete(bContext *C, uiBut *but, uiHandleButtonData *data)
1673 if (data->searchbox)
1674 ui_searchbox_autocomplete(C, data->searchbox, but, data->str);
1676 but->autocomplete_func(C, str, but->autofunc_arg);
1678 but->pos = strlen(str);
1679 but->selsta = but->selend = but->pos;
1684 static int ui_textedit_copypaste(uiBut *but, uiHandleButtonData *data, int paste, int copy, int cut)
1686 char buf[UI_MAX_DRAW_STR] = {0};
1687 char *str, *p, *pbuf;
1689 int str_len, buf_len;
1692 str_len = strlen(str);
1696 /* TODO, ensure UTF8 ui_is_but_utf8() - campbell */
1697 /* extract the first line from the clipboard */
1698 p = pbuf = WM_clipboard_text_get(0);
1703 while (*p && *p != '\r' && *p != '\n' && buf_len < UI_MAX_DRAW_STR - 1) {
1704 buf[buf_len++] = *p;
1709 /* paste over the current selection */
1710 if ((but->selend - but->selsta) > 0) {
1711 ui_textedit_delete_selection(but, data);
1712 str_len = strlen(str);
1715 for (y = 0; y < buf_len; y++) {
1716 /* add contents of buffer */
1717 if (str_len + 1 < data->maxlen) {
1718 for (x = data->maxlen; x > but->pos; x--)
1719 str[x] = str[x - 1];
1720 str[but->pos] = buf[y];
1723 str[str_len] = '\0';
1735 else if (copy || cut) {
1736 /* copy the contents to the copypaste buffer */
1737 for (x = but->selsta; x <= but->selend; x++) {
1738 if (x == but->selend)
1741 buf[(x - but->selsta)] = str[x];
1744 WM_clipboard_text_set(buf, 0);
1746 /* for cut only, delete the selection afterwards */
1748 if ((but->selend - but->selsta) > 0)
1749 changed = ui_textedit_delete_selection(but, data);
1755 static void ui_textedit_begin(bContext *C, uiBut *but, uiHandleButtonData *data)
1760 MEM_freeN(data->str);
1764 /* retrieve string */
1765 data->maxlen = ui_get_but_string_max_length(but);
1766 data->str = MEM_callocN(sizeof(char) * data->maxlen + 1, "textedit str");
1767 ui_get_but_string(but, data->str, data->maxlen);
1769 if (ELEM3(but->type, NUM, NUMABS, NUMSLI)) {
1770 ui_convert_to_unit_alt_name(but, data->str, data->maxlen);
1773 /* won't change from now on */
1774 len = strlen(data->str);
1776 data->origstr = BLI_strdupn(data->str, len);
1777 data->selextend = 0;
1778 data->selstartx = 0;
1780 /* set cursor pos to the end of the text */
1781 but->editstr = data->str;
1786 /* optional searchbox */
1787 if (but->type == SEARCH_MENU) {
1788 data->searchbox = ui_searchbox_create(C, data->region, but);
1789 ui_searchbox_update(C, data->searchbox, but, 1); /* 1 = reset */
1794 WM_cursor_modal(CTX_wm_window(C), BC_TEXTEDITCURSOR);
1797 static void ui_textedit_end(bContext *C, uiBut *but, uiHandleButtonData *data)
1800 if (ui_is_but_utf8(but)) {
1801 int strip = BLI_utf8_invalid_strip(but->editstr, strlen(but->editstr));
1802 /* not a file?, strip non utf-8 chars */
1804 /* wont happen often so isn't that annoying to keep it here for a while */
1805 printf("%s: invalid utf8 - stripped chars %d\n", __func__, strip);
1809 if (data->searchbox) {
1810 if (data->cancel == 0)
1811 ui_searchbox_apply(but, data->searchbox);
1813 ui_searchbox_free(C, data->searchbox);
1814 data->searchbox = NULL;
1817 but->editstr = NULL;
1821 WM_cursor_restore(CTX_wm_window(C));
1824 static void ui_textedit_next_but(uiBlock *block, uiBut *actbut, uiHandleButtonData *data)
1828 /* label and roundbox can overlap real buttons (backdrops...) */
1829 if (ELEM4(actbut->type, LABEL, SEPR, ROUNDBOX, LISTBOX))
1832 for (but = actbut->next; but; but = but->next) {
1833 if (ELEM7(but->type, TEX, NUM, NUMABS, NUMSLI, HSVSLI, IDPOIN, SEARCH_MENU)) {
1834 if (!(but->flag & UI_BUT_DISABLED)) {
1835 data->postbut = but;
1836 data->posttype = BUTTON_ACTIVATE_TEXT_EDITING;
1841 for (but = block->buttons.first; but != actbut; but = but->next) {
1842 if (ELEM7(but->type, TEX, NUM, NUMABS, NUMSLI, HSVSLI, IDPOIN, SEARCH_MENU)) {
1843 if (!(but->flag & UI_BUT_DISABLED)) {
1844 data->postbut = but;
1845 data->posttype = BUTTON_ACTIVATE_TEXT_EDITING;
1852 static void ui_textedit_prev_but(uiBlock *block, uiBut *actbut, uiHandleButtonData *data)
1856 /* label and roundbox can overlap real buttons (backdrops...) */
1857 if (ELEM4(actbut->type, LABEL, SEPR, ROUNDBOX, LISTBOX))
1860 for (but = actbut->prev; but; but = but->prev) {
1861 if (ELEM7(but->type, TEX, NUM, NUMABS, NUMSLI, HSVSLI, IDPOIN, SEARCH_MENU)) {
1862 if (!(but->flag & UI_BUT_DISABLED)) {
1863 data->postbut = but;
1864 data->posttype = BUTTON_ACTIVATE_TEXT_EDITING;
1869 for (but = block->buttons.last; but != actbut; but = but->prev) {
1870 if (ELEM7(but->type, TEX, NUM, NUMABS, NUMSLI, HSVSLI, IDPOIN, SEARCH_MENU)) {
1871 if (!(but->flag & UI_BUT_DISABLED)) {
1872 data->postbut = but;
1873 data->posttype = BUTTON_ACTIVATE_TEXT_EDITING;
1881 static void ui_do_but_textedit(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
1883 int mx, my, changed = 0, inbox = 0, update = 0, retval = WM_UI_HANDLER_CONTINUE;
1885 switch (event->type) {
1887 case WHEELDOWNMOUSE:
1889 if (data->searchbox)
1890 ui_searchbox_event(C, data->searchbox, but, event);
1895 data->cancel = TRUE;
1896 data->escapecancel = TRUE;
1897 button_activate_state(C, but, BUTTON_STATE_EXIT);
1898 retval = WM_UI_HANDLER_BREAK;
1902 /* exit on LMB only on RELEASE for searchbox, to mimic other popups, and allow multiple menu levels */
1903 if (data->searchbox)
1904 inbox = ui_searchbox_inside(data->searchbox, event->x, event->y);
1906 if (event->val == KM_PRESS) {
1909 ui_window_to_block(data->region, block, &mx, &my);
1911 if (ui_but_contains_pt(but, mx, my)) {
1912 ui_textedit_set_cursor_pos(but, data, mx);
1913 but->selsta = but->selend = but->pos;
1914 data->selstartx = mx;
1916 button_activate_state(C, but, BUTTON_STATE_TEXT_SELECTING);
1917 retval = WM_UI_HANDLER_BREAK;
1919 else if (inbox == 0) {
1920 /* if searchbox, click outside will cancel */
1921 if (data->searchbox)
1922 data->cancel = data->escapecancel = TRUE;
1923 button_activate_state(C, but, BUTTON_STATE_EXIT);
1924 retval = WM_UI_HANDLER_BREAK;
1928 button_activate_state(C, but, BUTTON_STATE_EXIT);
1929 retval = WM_UI_HANDLER_BREAK;
1935 if (event->val == KM_PRESS) {
1936 switch (event->type) {
1940 if (event->ctrl || event->oskey) {
1941 if (event->type == VKEY)
1942 changed = ui_textedit_copypaste(but, data, 1, 0, 0);
1943 else if (event->type == CKEY)
1944 changed = ui_textedit_copypaste(but, data, 0, 1, 0);
1945 else if (event->type == XKEY)
1946 changed = ui_textedit_copypaste(but, data, 0, 0, 1);
1948 retval = WM_UI_HANDLER_BREAK;
1952 ui_textedit_move(but, data, STRCUR_DIR_NEXT,
1953 event->shift, event->ctrl ? STRCUR_JUMP_DELIM : STRCUR_JUMP_NONE);
1954 retval = WM_UI_HANDLER_BREAK;
1957 ui_textedit_move(but, data, STRCUR_DIR_PREV,
1958 event->shift, event->ctrl ? STRCUR_JUMP_DELIM : STRCUR_JUMP_NONE);
1959 retval = WM_UI_HANDLER_BREAK;
1962 if (data->searchbox) {
1963 ui_searchbox_event(C, data->searchbox, but, event);
1966 /* pass on purposedly */
1968 ui_textedit_move(but, data, STRCUR_DIR_NEXT,
1969 event->shift, STRCUR_JUMP_ALL);
1970 retval = WM_UI_HANDLER_BREAK;
1973 if (data->searchbox) {
1974 ui_searchbox_event(C, data->searchbox, but, event);
1977 /* pass on purposedly */
1979 ui_textedit_move(but, data, STRCUR_DIR_PREV,
1980 event->shift, STRCUR_JUMP_ALL);
1981 retval = WM_UI_HANDLER_BREAK;
1985 button_activate_state(C, but, BUTTON_STATE_EXIT);
1986 retval = WM_UI_HANDLER_BREAK;
1989 changed = ui_textedit_delete(but, data, 1,
1990 event->ctrl ? STRCUR_JUMP_DELIM : STRCUR_JUMP_NONE);
1991 retval = WM_UI_HANDLER_BREAK;
1995 changed = ui_textedit_delete(but, data, 0,
1996 event->shift ? STRCUR_JUMP_ALL : (event->ctrl ? STRCUR_JUMP_DELIM : STRCUR_JUMP_NONE));
1997 retval = WM_UI_HANDLER_BREAK;
2001 /* there is a key conflict here, we can't tab with autocomplete */
2002 if (but->autocomplete_func || data->searchbox) {
2003 changed = ui_textedit_autocomplete(C, but, data);
2004 update = 1; /* do live update for tab key */
2006 /* the hotkey here is not well defined, was G.qual so we check all */
2007 else if (event->shift || event->ctrl || event->alt || event->oskey) {
2008 ui_textedit_prev_but(block, but, data);
2009 button_activate_state(C, but, BUTTON_STATE_EXIT);
2012 ui_textedit_next_but(block, but, data);
2013 button_activate_state(C, but, BUTTON_STATE_EXIT);
2015 retval = WM_UI_HANDLER_BREAK;
2019 if ((event->ascii || event->utf8_buf[0]) && (retval == WM_UI_HANDLER_CONTINUE)) {
2020 char ascii = event->ascii;
2021 const char *utf8_buf = event->utf8_buf;
2023 /* exception that's useful for number buttons, some keyboard
2024 * numpads have a comma instead of a period */
2025 if (ELEM3(but->type, NUM, NUMABS, NUMSLI)) { /* could use data->min*/
2026 if (event->type == PADPERIOD && ascii == ',') {
2028 utf8_buf = NULL; /* force ascii fallback */
2032 if (utf8_buf && utf8_buf[0]) {
2033 int utf8_buf_len = BLI_str_utf8_size(utf8_buf);
2034 /* keep this printf until utf8 is well tested */
2035 if (utf8_buf_len != 1) {
2036 printf("%s: utf8 char '%.*s'\n", __func__, utf8_buf_len, utf8_buf);
2039 // strcpy(utf8_buf, "12345");
2040 changed = ui_textedit_type_buf(but, data, event->utf8_buf, utf8_buf_len);
2043 changed = ui_textedit_type_ascii(but, data, ascii);
2046 retval = WM_UI_HANDLER_BREAK;
2049 /* textbutton with magnifier icon: do live update for search button */
2050 if (but->icon == ICON_VIEWZOOM)
2055 /* only update when typing for TAB key */
2056 if (update && data->interactive) {
2057 ui_apply_button(C, block, but, data, 1);
2062 but->changed = TRUE;
2064 if (data->searchbox)
2065 ui_searchbox_update(C, data->searchbox, but, 1); /* 1 = reset */
2068 if (changed || (retval == WM_UI_HANDLER_BREAK))
2069 ED_region_tag_redraw(data->region);
2072 static void ui_do_but_textedit_select(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2074 int mx, my, retval = WM_UI_HANDLER_CONTINUE;
2076 switch (event->type) {
2081 ui_window_to_block(data->region, block, &mx, &my);
2083 ui_textedit_set_cursor_select(but, data, mx);
2084 retval = WM_UI_HANDLER_BREAK;
2088 if (event->val == KM_RELEASE)
2089 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2090 retval = WM_UI_HANDLER_BREAK;
2094 if (retval == WM_UI_HANDLER_BREAK) {
2096 ED_region_tag_redraw(data->region);
2100 /* ************* number editing for various types ************* */
2102 static void ui_numedit_begin(uiBut *but, uiHandleButtonData *data)
2104 if (but->type == BUT_CURVE) {
2105 but->editcumap = (CurveMapping *)but->poin;
2107 else if (but->type == BUT_COLORBAND) {
2108 data->coba = (ColorBand *)but->poin;
2109 but->editcoba = data->coba;
2111 else if (ELEM3(but->type, BUT_NORMAL, HSVCUBE, HSVCIRCLE)) {
2112 ui_get_but_vectorf(but, data->origvec);
2113 copy_v3_v3(data->vec, data->origvec);
2114 but->editvec = data->vec;
2117 float softrange, softmin, softmax;
2119 data->startvalue = ui_get_but_val(but);
2120 data->origvalue = data->startvalue;
2121 data->value = data->origvalue;
2122 but->editval = &data->value;
2124 softmin = but->softmin;
2125 softmax = but->softmax;
2126 softrange = softmax - softmin;
2128 data->dragfstart = (softrange == 0.0f) ? 0.0f : ((float)data->value - softmin) / softrange;
2129 data->dragf = data->dragfstart;
2132 data->dragchange = 0;
2136 static void ui_numedit_end(uiBut *but, uiHandleButtonData *data)
2138 but->editval = NULL;
2139 but->editvec = NULL;
2140 but->editcoba = NULL;
2141 but->editcumap = NULL;
2143 data->dragstartx = 0;
2144 data->draglastx = 0;
2145 data->dragchange = 0;
2146 data->dragcbd = NULL;
2150 static void ui_numedit_apply(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data)
2152 if (data->interactive) {
2153 ui_apply_button(C, block, but, data, 1);
2159 ED_region_tag_redraw(data->region);
2162 /* ****************** menu opening for various types **************** */
2164 static void ui_blockopen_begin(bContext *C, uiBut *but, uiHandleButtonData *data)
2166 uiBlockCreateFunc func = NULL;
2167 uiBlockHandleCreateFunc handlefunc = NULL;
2168 uiMenuCreateFunc menufunc = NULL;
2169 char *menustr = NULL;
2172 switch (but->type) {
2175 if (but->menu_create_func) {
2176 menufunc = but->menu_create_func;
2180 func = but->block_create_func;
2181 arg = but->poin ? but->poin : but->func_argN;
2185 if (but->menu_create_func) {
2186 menufunc = but->menu_create_func;
2190 data->origvalue = ui_get_but_val(but);
2191 data->value = data->origvalue;
2192 but->editval = &data->value;
2198 menufunc = ui_block_func_ICONROW;
2202 menufunc = ui_block_func_ICONTEXTROW;
2206 ui_get_but_vectorf(but, data->origvec);
2207 copy_v3_v3(data->vec, data->origvec);
2208 but->editvec = data->vec;
2210 handlefunc = ui_block_func_COLOR;
2214 /* quiet warnings for unhandled types */
2219 if (func || handlefunc) {
2220 data->menu = ui_popup_block_create(C, data->region, but, func, handlefunc, arg);
2221 if (but->block->handle)
2222 data->menu->popup = but->block->handle->popup;
2224 else if (menufunc || menustr) {
2225 data->menu = ui_popup_menu_create(C, data->region, but, menufunc, arg, menustr);
2226 if (but->block->handle)
2227 data->menu->popup = but->block->handle->popup;
2230 /* this makes adjacent blocks auto open from now on */
2231 //if (but->block->auto_open == 0) but->block->auto_open = 1;
2234 static void ui_blockopen_end(bContext *C, uiBut *but, uiHandleButtonData *data)
2237 but->editval = NULL;
2238 but->editvec = NULL;
2240 but->block->auto_open_last = PIL_check_seconds_timer();
2244 ui_popup_block_free(C, data->menu);
2249 int ui_button_open_menu_direction(uiBut *but)
2251 uiHandleButtonData *data = but->active;
2253 if (data && data->menu)
2254 return data->menu->direction;
2259 /* ***************** events for different button types *************** */
2261 static int ui_do_but_BUT(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2263 if (data->state == BUTTON_STATE_HIGHLIGHT) {
2264 if (event->type == LEFTMOUSE && event->val == KM_PRESS) {
2265 button_activate_state(C, but, BUTTON_STATE_WAIT_RELEASE);
2266 return WM_UI_HANDLER_BREAK;
2268 else if (event->type == LEFTMOUSE && but->block->handle) {
2269 button_activate_state(C, but, BUTTON_STATE_EXIT);
2270 return WM_UI_HANDLER_BREAK;
2272 else if (ELEM(event->type, PADENTER, RETKEY) && event->val == KM_PRESS) {
2273 button_activate_state(C, but, BUTTON_STATE_WAIT_FLASH);
2274 return WM_UI_HANDLER_BREAK;
2277 else if (data->state == BUTTON_STATE_WAIT_RELEASE) {
2278 if (event->type == LEFTMOUSE && event->val != KM_PRESS) {
2279 if (!(but->flag & UI_SELECT))
2280 data->cancel = TRUE;
2281 button_activate_state(C, but, BUTTON_STATE_EXIT);
2282 return WM_UI_HANDLER_BREAK;
2286 return WM_UI_HANDLER_CONTINUE;
2289 static int ui_do_but_HOTKEYEVT(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2291 if (data->state == BUTTON_STATE_HIGHLIGHT) {
2292 if (ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val == KM_PRESS) {
2293 but->drawstr[0] = 0;
2294 but->modifier_key = 0;
2295 button_activate_state(C, but, BUTTON_STATE_WAIT_KEY_EVENT);
2296 return WM_UI_HANDLER_BREAK;
2299 else if (data->state == BUTTON_STATE_WAIT_KEY_EVENT) {
2301 if (event->type == MOUSEMOVE)
2302 return WM_UI_HANDLER_CONTINUE;
2304 if (event->type == LEFTMOUSE && event->val == KM_PRESS) {
2305 /* only cancel if click outside the button */
2306 if (ui_mouse_inside_button(but->active->region, but, event->x, event->y) == 0) {
2307 /* data->cancel doesnt work, this button opens immediate */
2308 if (but->flag & UI_BUT_IMMEDIATE)
2309 ui_set_but_val(but, 0);
2311 data->cancel = TRUE;
2312 button_activate_state(C, but, BUTTON_STATE_EXIT);
2313 return WM_UI_HANDLER_BREAK;
2318 but->modifier_key = 0;
2319 if (event->shift) but->modifier_key |= KM_SHIFT;
2320 if (event->alt) but->modifier_key |= KM_ALT;
2321 if (event->ctrl) but->modifier_key |= KM_CTRL;
2322 if (event->oskey) but->modifier_key |= KM_OSKEY;
2325 ED_region_tag_redraw(data->region);
2327 if (event->val == KM_PRESS) {
2328 if (ISHOTKEY(event->type)) {
2330 if (WM_key_event_string(event->type)[0])
2331 ui_set_but_val(but, event->type);
2333 data->cancel = TRUE;
2335 button_activate_state(C, but, BUTTON_STATE_EXIT);
2336 return WM_UI_HANDLER_BREAK;
2338 else if (event->type == ESCKEY) {
2339 data->cancel = TRUE;
2340 data->escapecancel = TRUE;
2341 button_activate_state(C, but, BUTTON_STATE_EXIT);
2347 return WM_UI_HANDLER_CONTINUE;
2350 static int ui_do_but_KEYEVT(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2352 if (data->state == BUTTON_STATE_HIGHLIGHT) {
2353 if (ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val == KM_PRESS) {
2354 button_activate_state(C, but, BUTTON_STATE_WAIT_KEY_EVENT);
2355 return WM_UI_HANDLER_BREAK;
2358 else if (data->state == BUTTON_STATE_WAIT_KEY_EVENT) {
2359 if (event->type == MOUSEMOVE)
2360 return WM_UI_HANDLER_CONTINUE;
2362 if (event->val == KM_PRESS) {
2363 if (WM_key_event_string(event->type)[0])
2364 ui_set_but_val(but, event->type);
2366 data->cancel = TRUE;
2368 button_activate_state(C, but, BUTTON_STATE_EXIT);
2372 return WM_UI_HANDLER_CONTINUE;
2375 static int ui_do_but_TEX(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2377 if (data->state == BUTTON_STATE_HIGHLIGHT) {
2378 if (ELEM(event->type, LEFTMOUSE, EVT_BUT_OPEN) && event->val == KM_PRESS) {
2379 if (but->dt == UI_EMBOSSN && !event->ctrl) {
2383 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2384 return WM_UI_HANDLER_BREAK;
2388 else if (data->state == BUTTON_STATE_TEXT_EDITING) {
2389 ui_do_but_textedit(C, block, but, data, event);
2390 return WM_UI_HANDLER_BREAK;
2392 else if (data->state == BUTTON_STATE_TEXT_SELECTING) {
2393 ui_do_but_textedit_select(C, block, but, data, event);
2394 return WM_UI_HANDLER_BREAK;
2397 return WM_UI_HANDLER_CONTINUE;
2400 static int ui_do_but_TOG(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2402 if (data->state == BUTTON_STATE_HIGHLIGHT) {
2403 if (ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val == KM_PRESS) {
2404 data->togdual = event->ctrl;
2405 data->togonly = !event->shift;
2406 button_activate_state(C, but, BUTTON_STATE_EXIT);
2407 return WM_UI_HANDLER_BREAK;
2410 return WM_UI_HANDLER_CONTINUE;
2413 static int ui_do_but_EXIT(bContext *C, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2416 if (data->state == BUTTON_STATE_HIGHLIGHT) {
2418 /* first handle click on icondrag type button */
2419 if (event->type == LEFTMOUSE && but->dragpoin) {
2420 if (ui_but_mouse_inside_icon(but, data->region, event)) {
2422 /* tell the button to wait and keep checking further events to
2423 * see if it should start dragging */
2424 button_activate_state(C, but, BUTTON_STATE_WAIT_DRAG);
2425 data->dragstartx = event->x;
2426 data->dragstarty = event->y;
2427 return WM_UI_HANDLER_CONTINUE;
2431 if (ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->val == KM_PRESS) {
2432 int ret = WM_UI_HANDLER_BREAK;
2433 /* XXX (a bit ugly) Special case handling for filebrowser drag button */
2434 if (but->dragpoin && but->imb && ui_but_mouse_inside_icon(but, data->region, event)) {
2435 ret = WM_UI_HANDLER_CONTINUE;
2437 button_activate_state(C, but, BUTTON_STATE_EXIT);
2441 else if (data->state == BUTTON_STATE_WAIT_DRAG) {
2443 /* this function also ends state */
2444 if (ui_but_start_drag(C, but, data, event)) {
2445 return WM_UI_HANDLER_BREAK;
2448 /* If the mouse has been pressed and released, getting to
2449 * this point without triggering a drag, then clear the
2450 * drag state for this button and continue to pass on the event */
2451 if (event->type == LEFTMOUSE && event->val == KM_RELEASE) {
2452 button_activate_state(C, but, BUTTON_STATE_EXIT);
2453 return WM_UI_HANDLER_CONTINUE;
2456 /* while waiting for a drag to be triggered, always block
2457 * other events from getting handled */
2458 return WM_UI_HANDLER_BREAK;
2461 return WM_UI_HANDLER_CONTINUE;
2464 /* var names match ui_numedit_but_NUM */
2465 static float ui_numedit_apply_snapf(uiBut *but, float tempf, float softmin, float softmax, float softrange, int snap)
2467 if (tempf == softmin || tempf == softmax || snap == 0) {
2473 if (ui_is_but_unit(but)) {
2474 UnitSettings *unit = but->block->unit;
2475 int unit_type = RNA_SUBTYPE_UNIT_VALUE(uiButGetUnitType(but));
2477 if (bUnit_IsValid(unit->system, unit_type)) {
2478 fac = (float)bUnit_BaseScalar(unit->system, unit_type);
2479 if (ELEM3(unit_type, B_UNIT_LENGTH, B_UNIT_AREA, B_UNIT_VOLUME)) {
2480 fac /= unit->scale_length;
2486 /* snap in unit-space */
2488 /* softmin /= fac; */ /* UNUSED */
2489 /* softmax /= fac; */ /* UNUSED */
2494 if (softrange < 2.10f) tempf = 0.1f * floorf(10.0f * tempf);
2495 else if (softrange < 21.0f) tempf = floorf(tempf);
2496 else tempf = 10.0f * floorf(tempf / 10.0f);
2498 else if (snap == 2) {
2499 if (softrange < 2.10f) tempf = 0.01f * floorf(100.0f * tempf);
2500 else if (softrange < 21.0f) tempf = 0.1f * floorf(10.0f * tempf);
2501 else tempf = floor(tempf);
2511 static float ui_numedit_apply_snap(int temp, float softmin, float softmax, int snap)
2513 if (temp == softmin || temp == softmax)
2520 temp = 10 * (temp / 10);
2523 temp = 100 * (temp / 100);
2530 static int ui_numedit_but_NUM(uiBut *but, uiHandleButtonData *data, float fac, int snap, int mx)
2532 float deler, tempf, softmin, softmax, softrange;
2533 int lvalue, temp, changed = 0;
2535 if (mx == data->draglastx)
2538 /* drag-lock - prevent unwanted scroll adjustments */
2539 /* change value (now 3) to adjust threshold in pixels */
2540 if (data->draglock) {
2541 if (abs(mx - data->dragstartx) <= 3)
2545 data->dragstartx = mx; /* ignore mouse movement within drag-lock */
2548 softmin = but->softmin;
2549 softmax = but->softmax;
2550 softrange = softmax - softmin;
2552 if (ui_is_a_warp_but(but)) {
2553 /* Mouse location isn't screen clamped to the screen so use a linear mapping
2554 * 2px == 1-int, or 1px == 1-ClickStep */
2555 if (ui_is_but_float(but)) {
2556 fac *= 0.01f * but->a1;
2557 tempf = (float)data->startvalue + ((float)(mx - data->dragstartx) * fac);
2558 tempf = ui_numedit_apply_snapf(but, tempf, softmin, softmax, softrange, snap);
2560 #if 1 /* fake moving the click start, nicer for dragging back after passing the limit */
2561 if (tempf < softmin) {
2562 data->dragstartx -= (softmin - tempf) / fac;
2565 else if (tempf > softmax) {
2566 data->dragstartx += (tempf - softmax) / fac;
2570 CLAMP(tempf, softmin, softmax);
2573 if (tempf != (float)data->value) {
2574 data->dragchange = 1;
2575 data->value = tempf;
2580 if (softrange > 256) fac = 1.0; /* 1px == 1 */
2581 else if (softrange > 32) fac = 1.0 / 2.0; /* 2px == 1 */
2582 else fac = 1.0 / 16.0; /* 16px == 1? */
2584 temp = data->startvalue + (((double)mx - data->dragstartx) * (double)fac);
2585 temp = ui_numedit_apply_snap(temp, softmin, softmax, snap);
2587 #if 1 /* fake moving the click start, nicer for dragging back after passing the limit */
2588 if (temp < softmin) {
2589 data->dragstartx -= (softmin - temp) / fac;
2592 else if (temp > softmax) {
2593 data->dragstartx += (temp - softmax) / fac;
2597 CLAMP(temp, softmin, softmax);
2600 if (temp != data->value) {
2601 data->dragchange = 1;
2607 data->draglastx = mx;
2610 /* Use a non-linear mapping of the mouse drag especially for large floats (normal behavior) */
2612 if (!ui_is_but_float(but)) {
2613 /* prevent large ranges from getting too out of control */
2614 if (softrange > 600) deler = powf(softrange, 0.75);
2615 else if (softrange < 100) deler = 200.0;
2616 else if (softrange < 25) deler = 50.0;
2620 if (softrange > 11) {
2621 /* non linear change in mouse input- good for high precicsion */
2622 data->dragf += (((float)(mx - data->draglastx)) / deler) * (fabsf(data->dragstartx - mx) * 0.002f);
2624 else if (softrange > 129) { /* only scale large int buttons */
2625 /* non linear change in mouse input- good for high precicsionm ints need less fine tuning */
2626 data->dragf += (((float)(mx - data->draglastx)) / deler) * (fabsf(data->dragstartx - mx) * 0.004f);
2630 data->dragf += ((float)(mx - data->draglastx)) / deler;
2633 CLAMP(data->dragf, 0.0f, 1.0f);
2634 data->draglastx = mx;
2635 tempf = (softmin + data->dragf * softrange);
2638 if (!ui_is_but_float(but)) {
2639 temp = floorf(tempf + 0.5f);
2641 temp = ui_numedit_apply_snap(temp, softmin, softmax, snap);
2643 CLAMP(temp, softmin, softmax);
2644 lvalue = (int)data->value;
2646 if (temp != lvalue) {
2647 data->dragchange = 1;
2648 data->value = (double)temp;
2654 tempf = ui_numedit_apply_snapf(but, tempf, softmin, softmax, softrange, snap);
2656 CLAMP(tempf, softmin, softmax);
2658 if (tempf != (float)data->value) {
2659 data->dragchange = 1;
2660 data->value = tempf;
2670 static int ui_do_but_NUM(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2672 int mx, my; /* mouse location scaled to fit the UI */
2673 int screen_mx, screen_my; /* mouse location kept at screen pixel coords */
2675 int retval = WM_UI_HANDLER_CONTINUE;
2677 mx = screen_mx = event->x;
2678 my = screen_my = event->y;
2680 ui_window_to_block(data->region, block, &mx, &my);
2682 if (data->state == BUTTON_STATE_HIGHLIGHT) {
2683 /* XXX hardcoded keymap check.... */
2684 if (event->type == WHEELDOWNMOUSE && event->alt) {
2685 mx = but->rect.xmin;
2688 else if (event->type == WHEELUPMOUSE && event->alt) {
2689 mx = but->rect.xmax;
2692 else if (event->val == KM_PRESS) {
2693 if (ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->ctrl) {
2694 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2695 retval = WM_UI_HANDLER_BREAK;
2697 else if (event->type == LEFTMOUSE) {
2698 data->dragstartx = data->draglastx = ui_is_a_warp_but(but) ? screen_mx : mx;
2699 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2700 retval = WM_UI_HANDLER_BREAK;
2702 else if (ELEM(event->type, PADENTER, RETKEY) && event->val == KM_PRESS) {
2705 else if (event->type == MINUSKEY && event->val == KM_PRESS) {
2706 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2707 data->value = -data->value;
2708 button_activate_state(C, but, BUTTON_STATE_EXIT);
2709 retval = WM_UI_HANDLER_BREAK;
2714 else if (data->state == BUTTON_STATE_NUM_EDITING) {
2715 if (event->type == ESCKEY) {
2716 data->cancel = TRUE;
2717 data->escapecancel = TRUE;
2718 button_activate_state(C, but, BUTTON_STATE_EXIT);
2720 else if (event->type == LEFTMOUSE && event->val != KM_PRESS) {
2721 if (data->dragchange)
2722 button_activate_state(C, but, BUTTON_STATE_EXIT);
2726 else if (event->type == MOUSEMOVE) {
2731 if (event->shift) fac /= 10.0f;
2732 if (event->alt) fac /= 20.0f;
2734 snap = (event->ctrl) ? (event->shift) ? 2 : 1 : 0;
2736 if (ui_numedit_but_NUM(but, data, fac, snap, (ui_is_a_warp_but(but) ? screen_mx : mx)))
2737 ui_numedit_apply(C, block, but, data);
2739 retval = WM_UI_HANDLER_BREAK;
2741 else if (data->state == BUTTON_STATE_TEXT_EDITING) {
2742 ui_do_but_textedit(C, block, but, data, event);
2743 retval = WM_UI_HANDLER_BREAK;
2745 else if (data->state == BUTTON_STATE_TEXT_SELECTING) {
2746 ui_do_but_textedit_select(C, block, but, data, event);
2747 retval = WM_UI_HANDLER_BREAK;
2751 /* we can click on the side arrows to increment/decrement,
2752 * or click inside to edit the value directly */
2753 float tempf, softmin, softmax;
2756 softmin = but->softmin;
2757 softmax = but->softmax;
2759 if (!ui_is_but_float(but)) {
2760 if (mx < (but->rect.xmin + BLI_rctf_size_x(&but->rect) / 3 - 3)) {
2761 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2763 temp = (int)data->value - 1;
2764 if (temp >= softmin && temp <= softmax)
2765 data->value = (double)temp;
2767 data->cancel = TRUE;
2769 button_activate_state(C, but, BUTTON_STATE_EXIT);
2771 else if (mx > (but->rect.xmin + (2 * BLI_rctf_size_x(&but->rect) / 3) + 3)) {
2772 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2774 temp = (int)data->value + 1;
2775 if (temp >= softmin && temp <= softmax)
2776 data->value = (double)temp;
2778 data->cancel = TRUE;
2780 button_activate_state(C, but, BUTTON_STATE_EXIT);
2783 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2787 if (mx < (but->rect.xmin + BLI_rctf_size_x(&but->rect) / 3 - 3)) {
2788 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2790 tempf = (float)data->value - 0.01f * but->a1;
2791 if (tempf < softmin) tempf = softmin;
2792 data->value = tempf;
2794 button_activate_state(C, but, BUTTON_STATE_EXIT);
2796 else if (mx > but->rect.xmin + (2 * (BLI_rctf_size_x(&but->rect) / 3) + 3)) {
2797 button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
2799 tempf = (float)data->value + 0.01f * but->a1;
2800 if (tempf > softmax) tempf = softmax;
2801 data->value = tempf;
2803 button_activate_state(C, but, BUTTON_STATE_EXIT);
2806 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2810 retval = WM_UI_HANDLER_BREAK;
2816 static int ui_numedit_but_SLI(uiBut *but, uiHandleButtonData *data, const short shift, const short ctrl, int mx)
2818 float deler, f, tempf, softmin, softmax, softrange;
2819 int temp, lvalue, changed = 0;
2821 softmin = but->softmin;
2822 softmax = but->softmax;
2823 softrange = softmax - softmin;
2825 if (but->type == NUMSLI) {
2826 deler = (BLI_rctf_size_x(&but->rect) - 5.0f * but->aspect);
2828 else if (but->type == HSVSLI) {
2829 deler = (BLI_rctf_size_x(&but->rect) / 2.0f - 5.0f * but->aspect);
2831 else if (but->type == SCROLL) {
2832 int horizontal = (BLI_rctf_size_x(&but->rect) > BLI_rctf_size_y(&but->rect));
2833 float size = (horizontal) ? BLI_rctf_size_x(&but->rect) : -BLI_rctf_size_y(&but->rect);
2834 deler = size * (but->softmax - but->softmin) / (but->softmax - but->softmin + but->a1);
2837 deler = (BLI_rctf_size_x(&but->rect) - 5.0f * but->aspect);
2840 f = (float)(mx - data->dragstartx) / deler + data->dragfstart;
2843 f = (f - data->dragfstart) / 10.0f + data->dragfstart;
2845 CLAMP(f, 0.0f, 1.0f);
2846 tempf = softmin + f * softrange;
2847 temp = floorf(tempf + 0.5f);
2850 if (tempf == softmin || tempf == softmax) {
2853 else if (ui_is_but_float(but)) {
2856 if (tempf == softmin || tempf == softmax) {}
2857 else if (softmax - softmin < 2.10f) tempf = 0.01f * floorf(100.0f * tempf);
2858 else if (softmax - softmin < 21.0f) tempf = 0.1f * floorf(10.0f * tempf);
2859 else tempf = floorf(tempf);
2862 if (softmax - softmin < 2.10f) tempf = 0.1f * floorf(10.0f * tempf);
2863 else if (softmax - softmin < 21.0f) tempf = floorf(tempf);
2864 else tempf = 10.0f * floorf(tempf / 10.0f);
2868 temp = 10 * (temp / 10);
2873 if (!ui_is_but_float(but)) {
2874 lvalue = floor(data->value + 0.5);
2876 CLAMP(temp, softmin, softmax);
2878 if (temp != lvalue) {
2880 data->dragchange = 1;
2885 CLAMP(tempf, softmin, softmax);
2887 if (tempf != (float)data->value) {
2888 data->value = tempf;
2889 data->dragchange = 1;
2897 static int ui_do_but_SLI(bContext *C, uiBlock *block, uiBut *but, uiHandleButtonData *data, wmEvent *event)
2899 int mx, my, click = 0;
2900 int retval = WM_UI_HANDLER_CONTINUE;
2904 ui_window_to_block(data->region, block, &mx, &my);
2906 if (data->state == BUTTON_STATE_HIGHLIGHT) {
2907 /* XXX hardcoded keymap check.... */
2908 if (event->type == WHEELDOWNMOUSE && event->alt) {
2909 mx = but->rect.xmin;
2912 else if (event->type == WHEELUPMOUSE && event->alt) {
2913 mx = but->rect.xmax;
2916 else if (event->val == KM_PRESS) {
2917 if (ELEM3(event->type, LEFTMOUSE, PADENTER, RETKEY) && event->ctrl) {
2918 button_activate_state(C, but, BUTTON_STATE_TEXT_EDITING);
2919 retval = WM_UI_HANDLER_BREAK;