9cf38f66fe5f97676b7ce8105e4b4db3bba55f01
[blender.git] / source / blender / editors / interface / interface_region_tooltip.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_region_tooltip.c
27  *  \ingroup edinterface
28  *
29  * ToolTip Region and Construction
30  */
31
32 /* TODO(campbell):
33  * We may want to have a higher level API that initializes a timer,
34  * checks for mouse motion and clears the tool-tip afterwards.
35  * We never want multiple tool-tips at once so this could be handled on the window / window-manager level.
36  *
37  * For now it's not a priority, so leave as-is.
38  */
39
40 #include <stdarg.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <assert.h>
44
45 #include "MEM_guardedalloc.h"
46
47 #include "DNA_userdef_types.h"
48 #include "DNA_brush_types.h"
49
50 #include "BLI_math.h"
51 #include "BLI_string.h"
52 #include "BLI_string_utf8.h"
53 #include "BLI_rect.h"
54 #include "BLI_utildefines.h"
55
56 #include "BKE_context.h"
57 #include "BKE_screen.h"
58 #include "BKE_library.h"
59
60 #include "WM_api.h"
61 #include "WM_types.h"
62
63 #include "RNA_access.h"
64
65 #include "BIF_gl.h"
66
67 #include "UI_interface.h"
68
69 #include "BLF_api.h"
70 #include "BLT_translation.h"
71
72 #ifdef WITH_PYTHON
73 #  include "BPY_extern.h"
74 #endif
75
76 #include "ED_screen.h"
77
78 #include "interface_intern.h"
79 #include "interface_regions_intern.h"
80
81 #define UI_TIP_PAD_FAC      1.3f
82 #define UI_TIP_PADDING      (int)(UI_TIP_PAD_FAC * UI_UNIT_Y)
83 #define UI_TIP_MAXWIDTH     600
84
85
86 typedef struct uiTooltipFormat {
87         enum {
88                 UI_TIP_STYLE_NORMAL = 0,
89                 UI_TIP_STYLE_HEADER,
90                 UI_TIP_STYLE_MONO,
91         } style : 3;
92         enum {
93                 UI_TIP_LC_MAIN = 0,     /* primary text */
94                 UI_TIP_LC_VALUE,        /* the value of buttons (also shortcuts) */
95                 UI_TIP_LC_ACTIVE,       /* titles of active enum values */
96                 UI_TIP_LC_NORMAL,       /* regular text */
97                 UI_TIP_LC_PYTHON,       /* Python snippet */
98                 UI_TIP_LC_ALERT,        /* description of why operator can't run */
99         } color_id : 4;
100         int is_pad : 1;
101 } uiTooltipFormat;
102
103 typedef struct uiTooltipField {
104         char *text;
105         char *text_suffix;
106         struct {
107                 uint x_pos;     /* x cursor position at the end of the last line */
108                 uint lines;     /* number of lines, 1 or more with word-wrap */
109         } geom;
110         uiTooltipFormat format;
111
112 } uiTooltipField;
113
114 typedef struct uiTooltipData {
115         rcti bbox;
116         uiTooltipField *fields;
117         uint            fields_len;
118         uiFontStyle fstyle;
119         int wrap_width;
120         int toth, lineh;
121 } uiTooltipData;
122
123 #define UI_TIP_LC_MAX 6
124
125 BLI_STATIC_ASSERT(UI_TIP_LC_MAX == UI_TIP_LC_ALERT + 1, "invalid lc-max");
126 BLI_STATIC_ASSERT(sizeof(uiTooltipFormat) <= sizeof(int), "oversize");
127
128 static uiTooltipField *text_field_add_only(
129         uiTooltipData *data)
130 {
131         data->fields_len += 1;
132         data->fields = MEM_recallocN(data->fields, sizeof(*data->fields) * data->fields_len);
133         return &data->fields[data->fields_len - 1];
134 }
135
136 static uiTooltipField *text_field_add(
137         uiTooltipData *data,
138         const uiTooltipFormat *format)
139 {
140         uiTooltipField *field = text_field_add_only(data);
141         field->format = *format;
142         return field;
143 }
144
145 /* -------------------------------------------------------------------- */
146 /** \name ToolTip Callbacks (Draw & Free)
147  * \{ */
148
149 static void rgb_tint(
150         float col[3],
151         float h, float h_strength,
152         float v, float v_strength)
153 {
154         float col_hsv_from[3];
155         float col_hsv_to[3];
156
157         rgb_to_hsv_v(col, col_hsv_from);
158
159         col_hsv_to[0] = h;
160         col_hsv_to[1] = h_strength;
161         col_hsv_to[2] = (col_hsv_from[2] * (1.0f - v_strength)) + (v * v_strength);
162
163         hsv_to_rgb_v(col_hsv_to, col);
164 }
165
166 static void ui_tooltip_region_draw_cb(const bContext *UNUSED(C), ARegion *ar)
167 {
168         const float pad_px = UI_TIP_PADDING;
169         uiTooltipData *data = ar->regiondata;
170         uiWidgetColors *theme = ui_tooltip_get_theme();
171         rcti bbox = data->bbox;
172         float tip_colors[UI_TIP_LC_MAX][3];
173         unsigned char drawcol[4] = {0, 0, 0, 255}; /* to store color in while drawing (alpha is always 255) */
174
175         float *main_color    = tip_colors[UI_TIP_LC_MAIN]; /* the color from the theme */
176         float *value_color   = tip_colors[UI_TIP_LC_VALUE];
177         float *active_color  = tip_colors[UI_TIP_LC_ACTIVE];
178         float *normal_color  = tip_colors[UI_TIP_LC_NORMAL];
179         float *python_color  = tip_colors[UI_TIP_LC_PYTHON];
180         float *alert_color   = tip_colors[UI_TIP_LC_ALERT];
181
182         float background_color[3];
183         float tone_bg;
184         int i;
185
186         wmOrtho2_region_pixelspace(ar);
187
188         /* draw background */
189         ui_draw_tooltip_background(UI_style_get(), NULL, &bbox);
190
191         /* set background_color */
192         rgb_uchar_to_float(background_color, (const unsigned char *)theme->inner);
193
194         /* calculate normal_color */
195         rgb_uchar_to_float(main_color, (const unsigned char *)theme->text);
196         copy_v3_v3(active_color, main_color);
197         copy_v3_v3(normal_color, main_color);
198         copy_v3_v3(python_color, main_color);
199         copy_v3_v3(alert_color, main_color);
200         copy_v3_v3(value_color, main_color);
201
202         /* find the brightness difference between background and text colors */
203
204         tone_bg = rgb_to_grayscale(background_color);
205         /* tone_fg = rgb_to_grayscale(main_color); */
206
207         /* mix the colors */
208         rgb_tint(value_color,  0.0f, 0.0f, tone_bg, 0.2f);  /* light gray */
209         rgb_tint(active_color, 0.6f, 0.2f, tone_bg, 0.2f);  /* light blue */
210         rgb_tint(normal_color, 0.0f, 0.0f, tone_bg, 0.4f);  /* gray       */
211         rgb_tint(python_color, 0.0f, 0.0f, tone_bg, 0.5f);  /* dark gray  */
212         rgb_tint(alert_color,  0.0f, 0.8f, tone_bg, 0.1f);  /* red        */
213
214         /* draw text */
215         BLF_wordwrap(data->fstyle.uifont_id, data->wrap_width);
216         BLF_wordwrap(blf_mono_font, data->wrap_width);
217
218         bbox.xmin += 0.5f * pad_px;  /* add padding to the text */
219         bbox.ymax -= 0.25f * pad_px;
220
221         for (i = 0; i < data->fields_len; i++) {
222                 const uiTooltipField *field = &data->fields[i];
223                 const uiTooltipField *field_next = (i + 1) != data->fields_len ? &data->fields[i + 1] : NULL;
224
225                 bbox.ymin = bbox.ymax - (data->lineh * field->geom.lines);
226                 if (field->format.style == UI_TIP_STYLE_HEADER) {
227                         /* draw header and active data (is done here to be able to change color) */
228                         uiFontStyle fstyle_header = data->fstyle;
229
230                         /* override text-style */
231                         fstyle_header.shadow = 1;
232                         fstyle_header.shadowcolor = rgb_to_grayscale(tip_colors[UI_TIP_LC_MAIN]);
233                         fstyle_header.shadx = fstyle_header.shady = 0;
234                         fstyle_header.shadowalpha = 1.0f;
235                         fstyle_header.word_wrap = true;
236
237                         rgb_float_to_uchar(drawcol, tip_colors[UI_TIP_LC_MAIN]);
238                         UI_fontstyle_set(&fstyle_header);
239                         UI_fontstyle_draw(&fstyle_header, &bbox, field->text, drawcol);
240
241                         fstyle_header.shadow = 0;
242
243                         /* offset to the end of the last line */
244                         if (field->text_suffix) {
245                                 float xofs = field->geom.x_pos;
246                                 float yofs = data->lineh * (field->geom.lines - 1);
247                                 bbox.xmin += xofs;
248                                 bbox.ymax -= yofs;
249
250                                 rgb_float_to_uchar(drawcol, tip_colors[UI_TIP_LC_ACTIVE]);
251                                 UI_fontstyle_draw(&fstyle_header, &bbox, field->text_suffix, drawcol);
252
253                                 /* undo offset */
254                                 bbox.xmin -= xofs;
255                                 bbox.ymax += yofs;
256                         }
257                 }
258                 else if (field->format.style == UI_TIP_STYLE_MONO) {
259                         uiFontStyle fstyle_mono = data->fstyle;
260                         fstyle_mono.uifont_id = blf_mono_font;
261                         fstyle_mono.word_wrap = true;
262
263                         UI_fontstyle_set(&fstyle_mono);
264                         /* XXX, needed because we dont have mono in 'U.uifonts' */
265                         BLF_size(fstyle_mono.uifont_id, fstyle_mono.points * U.pixelsize, U.dpi);
266                         rgb_float_to_uchar(drawcol, tip_colors[field->format.color_id]);
267                         UI_fontstyle_draw(&fstyle_mono, &bbox, field->text, drawcol);
268                 }
269                 else {
270                         uiFontStyle fstyle_normal = data->fstyle;
271                         BLI_assert(field->format.style == UI_TIP_STYLE_NORMAL);
272                         fstyle_normal.word_wrap = true;
273
274                         /* draw remaining data */
275                         rgb_float_to_uchar(drawcol, tip_colors[field->format.color_id]);
276                         UI_fontstyle_set(&fstyle_normal);
277                         UI_fontstyle_draw(&fstyle_normal, &bbox, field->text, drawcol);
278                 }
279
280                 bbox.ymax -= data->lineh * field->geom.lines;
281
282                 if (field_next && field_next->format.is_pad) {
283                         bbox.ymax -= data->lineh * (UI_TIP_PAD_FAC - 1);
284                 }
285         }
286
287         BLF_disable(data->fstyle.uifont_id, BLF_WORD_WRAP);
288         BLF_disable(blf_mono_font, BLF_WORD_WRAP);
289 }
290
291 static void ui_tooltip_region_free_cb(ARegion *ar)
292 {
293         uiTooltipData *data;
294
295         data = ar->regiondata;
296
297         for (int  i = 0; i < data->fields_len; i++) {
298                 const uiTooltipField *field = &data->fields[i];
299                 MEM_freeN(field->text);
300                 if (field->text_suffix) {
301                         MEM_freeN(field->text_suffix);
302                 }
303         }
304         MEM_freeN(data->fields);
305         MEM_freeN(data);
306         ar->regiondata = NULL;
307 }
308
309 /** \} */
310
311 /* -------------------------------------------------------------------- */
312 /** \name ToolTip Creation
313  * \{ */
314
315 static bool ui_tooltip_data_append_from_keymap(
316         bContext *C, uiTooltipData *data,
317         wmKeyMap *keymap)
318 {
319         const int fields_len_init = data->fields_len;
320         char buf[512];
321
322         for (wmKeyMapItem *kmi = keymap->items.first; kmi; kmi = kmi->next) {
323                 wmOperatorType *ot = WM_operatortype_find(kmi->idname, true);
324                 if (ot != NULL) {
325                         /* Tip */
326                         {
327                                 uiTooltipField *field = text_field_add(
328                                         data, &(uiTooltipFormat){
329                                             .style = UI_TIP_STYLE_NORMAL,
330                                             .color_id = UI_TIP_LC_MAIN,
331                                             .is_pad = true,
332                                         });
333                                 field->text = BLI_strdup(ot->description[0] ? ot->description : ot->name);
334                         }
335                         /* Shortcut */
336                         {
337                                 uiTooltipField *field = text_field_add(
338                                         data, &(uiTooltipFormat){
339                                             .style = UI_TIP_STYLE_NORMAL,
340                                             .color_id = UI_TIP_LC_NORMAL,
341                                         });
342                                 bool found = false;
343                                 if (WM_keymap_item_to_string(kmi, false, buf, sizeof(buf))) {
344                                         found = true;
345                                 }
346                                 field->text = BLI_sprintfN(TIP_("Shortcut: %s"), found ? buf : "None");
347                         }
348
349                         /* Python */
350                         if (U.flag & USER_TOOLTIPS_PYTHON) {
351                                 uiTooltipField *field = text_field_add(
352                                         data, &(uiTooltipFormat){
353                                             .style = UI_TIP_STYLE_NORMAL,
354                                             .color_id = UI_TIP_LC_PYTHON,
355                                         });
356                                 char *str = WM_operator_pystring_ex(C, NULL, false, false, ot, kmi->ptr);
357                                 WM_operator_pystring_abbreviate(str, 32);
358                                 field->text = BLI_sprintfN(TIP_("Python: %s"), str);
359                                 MEM_freeN(str);
360                         }
361                 }
362         }
363
364         return (fields_len_init != data->fields_len);
365 }
366
367
368 /**
369  * Special tool-system exception.
370  */
371 static uiTooltipData *ui_tooltip_data_from_tool(bContext *C, uiBut *but, bool is_label)
372 {
373         if (but->optype == NULL) {
374                 return NULL;
375         }
376
377         if (!STREQ(but->optype->idname, "WM_OT_tool_set_by_name")) {
378                 return NULL;
379         }
380
381         /* Needed to get the space-data's type (below). */
382         if (CTX_wm_space_data(C) == NULL) {
383                 return NULL;
384         }
385
386         char tool_name[MAX_NAME];
387         RNA_string_get(but->opptr, "name", tool_name);
388         BLI_assert(tool_name[0] != '\0');
389
390         /* We have a tool, now extract the info. */
391         uiTooltipData *data = MEM_callocN(sizeof(uiTooltipData), "uiTooltipData");
392
393 #ifdef WITH_PYTHON
394         /* it turns out to be most simple to do this via Python since C
395          * doesn't have access to information about non-active tools.
396          */
397
398         uiTooltipField *field_title = NULL;
399
400         /* Title (when icon-only). */
401         if (but->drawstr[0] == '\0') {
402                 uiTooltipField *field = text_field_add(
403                         data, &(uiTooltipFormat){
404                             .style = UI_TIP_STYLE_NORMAL,
405                             .color_id = UI_TIP_LC_MAIN,
406                             .is_pad = true,
407                         });
408                 field->text = BLI_strdup(tool_name);
409                 field_title = field;
410         }
411
412         /* Tip. */
413         if (is_label == false) {
414                 const char *expr_imports[] = {"bpy", "bl_ui", NULL};
415                 char expr[256];
416                 SNPRINTF(
417                         expr,
418                         "bl_ui.space_toolsystem_common.description_from_name("
419                         "bpy.context, "
420                         "bpy.context.space_data.type, "
421                         "'%s') + '.'",
422                         tool_name);
423
424                 char *expr_result = NULL;
425                 if (BPY_execute_string_as_string(C, expr_imports, expr, true, &expr_result)) {
426                         if (!STREQ(expr_result, ".")) {
427                                 uiTooltipField *field = text_field_add(
428                                         data, &(uiTooltipFormat){
429                                             .style = UI_TIP_STYLE_NORMAL,
430                                             .color_id = UI_TIP_LC_MAIN,
431                                             .is_pad = true,
432                                         });
433                                 field->text = expr_result;
434                         }
435                         else {
436                                 MEM_freeN(expr_result);
437                         }
438                 }
439                 else {
440                         BLI_assert(0);
441                 }
442         }
443
444         /* Shortcut. */
445         {
446                 /* There are different kinds of shortcuts:
447                  *
448                  * - Direct access to the tool (as if the toolbar button is pressed).
449                  * - The key is bound to a brush type (not the exact brush name).
450                  * - The key is assigned to the operator it's self (bypassing the tool, executing the operator).
451                  *
452                  * Either way case it's useful to show the shortcut.
453                  */
454                 char *shortcut = NULL;
455
456                 {
457                         uiStringInfo op_keymap = {BUT_GET_OP_KEYMAP, NULL};
458                         UI_but_string_info_get(C, but, &op_keymap, NULL);
459                         shortcut = op_keymap.strinfo;
460                 }
461
462                 if (shortcut == NULL) {
463                         int mode = CTX_data_mode_enum(C);
464                         const char *tool_attr = NULL;
465                         uint tool_offset = 0;
466
467                         switch (mode) {
468                                 case CTX_MODE_SCULPT:
469                                         tool_attr = "sculpt_tool";
470                                         tool_offset = offsetof(Brush, sculpt_tool);
471                                         break;
472                                 case CTX_MODE_PAINT_VERTEX:
473                                         tool_attr = "vertex_paint_tool";
474                                         tool_offset = offsetof(Brush, vertexpaint_tool);
475                                         break;
476                                 case CTX_MODE_PAINT_WEIGHT:
477                                         tool_attr = "weight_paint_tool";
478                                         tool_offset = offsetof(Brush, vertexpaint_tool);
479                                         break;
480                                 case CTX_MODE_PAINT_TEXTURE:
481                                         tool_attr = "texture_paint_tool";
482                                         tool_offset = offsetof(Brush, imagepaint_tool);
483                                         break;
484                                 default:
485                                         break;
486                         }
487
488                         if (tool_attr != NULL) {
489                                 struct Main *bmain = CTX_data_main(C);
490                                 Brush *brush = (Brush *)BKE_libblock_find_name(bmain, ID_BR, tool_name);
491                                 if (brush) {
492                                         Object *ob = CTX_data_active_object(C);
493                                         wmOperatorType *ot = WM_operatortype_find("paint.brush_select", true);
494
495                                         PointerRNA op_props;
496                                         WM_operator_properties_create_ptr(&op_props, ot);
497                                         RNA_enum_set(&op_props, "paint_mode", ob->mode);
498                                         RNA_enum_set(&op_props, tool_attr, *(((char *)brush) + tool_offset));
499
500                                         /* Check for direct access to the tool. */
501                                         char shortcut_brush[128] = "";
502                                         if (WM_key_event_operator_string(
503                                                     C, ot->idname, WM_OP_INVOKE_REGION_WIN, op_props.data, true,
504                                                     shortcut_brush, ARRAY_SIZE(shortcut_brush)))
505                                         {
506                                                 shortcut = BLI_strdup(shortcut_brush);
507                                         }
508                                         WM_operator_properties_free(&op_props);
509                                 }
510                         }
511                 }
512
513                 if (shortcut == NULL) {
514                         /* Check for direct access to the tool. */
515                         char shortcut_toolbar[128] = "";
516                         if (WM_key_event_operator_string(
517                                     C, "WM_OT_toolbar", WM_OP_INVOKE_REGION_WIN, NULL, true,
518                                     shortcut_toolbar, ARRAY_SIZE(shortcut_toolbar)))
519                         {
520                                 /* Generate keymap in order to inspect it.
521                                  * Note, we could make a utility to avoid the keymap generation part of this. */
522                                 const char *expr_imports[] = {"bpy", "bl_ui", NULL};
523                                 const char *expr = (
524                                         "getattr("
525                                         "bl_ui.space_toolsystem_common.keymap_from_context("
526                                         "bpy.context, "
527                                         "bpy.context.space_data.type), "
528                                         "'as_pointer', lambda: 0)()");
529
530                                 intptr_t expr_result = 0;
531                                 if (BPY_execute_string_as_intptr(C, expr_imports, expr, true, &expr_result)) {
532                                         if (expr_result != 0) {
533                                                 wmKeyMap *keymap = (wmKeyMap *)expr_result;
534                                                 for (wmKeyMapItem *kmi = keymap->items.first; kmi; kmi = kmi->next) {
535                                                         if (STREQ(kmi->idname, but->optype->idname)) {
536                                                                 char tool_name_test[MAX_NAME];
537                                                                 RNA_string_get(kmi->ptr, "name", tool_name_test);
538                                                                 if (STREQ(tool_name, tool_name_test)) {
539                                                                         char buf[128];
540                                                                         WM_keymap_item_to_string(kmi, false, buf, sizeof(buf));
541                                                                         shortcut = BLI_sprintfN("%s, %s", shortcut_toolbar, buf);
542                                                                         break;
543                                                                 }
544                                                         }
545                                                 }
546                                         }
547                                 }
548                                 else {
549                                         BLI_assert(0);
550                                 }
551                         }
552                 }
553
554                 if (shortcut != NULL) {
555                         if (is_label && field_title) {
556                                 char *text_prev = field_title->text;
557                                 field_title->text = BLI_sprintfN(TIP_("%s: %s"), text_prev, shortcut);
558                                 MEM_freeN(text_prev);
559                         }
560                         else {
561                                 uiTooltipField *field = text_field_add(
562                                         data, &(uiTooltipFormat){
563                                             .style = UI_TIP_STYLE_NORMAL,
564                                             .color_id = UI_TIP_LC_VALUE,
565                                             .is_pad = true,
566                                         });
567                                 field->text = BLI_sprintfN(TIP_("Shortcut: %s"), shortcut);
568                         }
569                         MEM_freeN(shortcut);
570                 }
571         }
572
573         /* Keymap */
574
575         /* This is too handy not to expose somehow, let's be sneaky for now. */
576         if ((is_label == false) && CTX_wm_window(C)->eventstate->shift) {
577                 const char *expr_imports[] = {"bpy", "bl_ui", NULL};
578                 char expr[256];
579                 SNPRINTF(
580                         expr,
581                         "getattr("
582                         "bl_ui.space_toolsystem_common.keymap_from_name("
583                         "bpy.context, "
584                         "bpy.context.space_data.type, "
585                         "'%s'), "
586                         "'as_pointer', lambda: 0)()",
587                         tool_name);
588
589                 intptr_t expr_result = 0;
590                 if (BPY_execute_string_as_intptr(C, expr_imports, expr, true, &expr_result)) {
591                         if (expr_result != 0) {
592                                 {
593                                         uiTooltipField *field = text_field_add(
594                                                 data, &(uiTooltipFormat){
595                                                     .style = UI_TIP_STYLE_NORMAL,
596                                                     .color_id = UI_TIP_LC_NORMAL,
597                                                     .is_pad = true,
598                                                 });
599                                         field->text = BLI_strdup("Tool Keymap:");
600                                 }
601                                 wmKeyMap *keymap = (wmKeyMap *)expr_result;
602                                 ui_tooltip_data_append_from_keymap(C, data, keymap);
603                         }
604                 }
605                 else {
606                         BLI_assert(0);
607                 }
608         }
609 #endif  /* WITH_PYTHON */
610
611         if (data->fields_len == 0) {
612                 MEM_freeN(data);
613                 return NULL;
614         }
615         else {
616                 return data;
617         }
618 }
619
620 static uiTooltipData *ui_tooltip_data_from_button(bContext *C, uiBut *but)
621 {
622         uiStringInfo but_tip = {BUT_GET_TIP, NULL};
623         uiStringInfo enum_label = {BUT_GET_RNAENUM_LABEL, NULL};
624         uiStringInfo enum_tip = {BUT_GET_RNAENUM_TIP, NULL};
625         uiStringInfo op_keymap = {BUT_GET_OP_KEYMAP, NULL};
626         uiStringInfo prop_keymap = {BUT_GET_PROP_KEYMAP, NULL};
627         uiStringInfo rna_struct = {BUT_GET_RNASTRUCT_IDENTIFIER, NULL};
628         uiStringInfo rna_prop = {BUT_GET_RNAPROP_IDENTIFIER, NULL};
629
630         char buf[512];
631
632         /* create tooltip data */
633         uiTooltipData *data = MEM_callocN(sizeof(uiTooltipData), "uiTooltipData");
634
635         UI_but_string_info_get(C, but, &but_tip, &enum_label, &enum_tip, &op_keymap, &prop_keymap, &rna_struct, &rna_prop, NULL);
636
637         /* Tip */
638         if (but_tip.strinfo) {
639                 {
640                         uiTooltipField *field = text_field_add(
641                                 data, &(uiTooltipFormat){
642                                     .style = UI_TIP_STYLE_HEADER,
643                                     .color_id = UI_TIP_LC_NORMAL,
644                                 });
645                         if (enum_label.strinfo) {
646                                 field->text = BLI_sprintfN("%s:  ", but_tip.strinfo);
647                                 field->text_suffix = BLI_strdup(enum_label.strinfo);
648                         }
649                         else {
650                                 field->text = BLI_sprintfN("%s.", but_tip.strinfo);
651                         }
652                 }
653
654                 /* special case enum rna buttons */
655                 if ((but->type & UI_BTYPE_ROW) && but->rnaprop && RNA_property_flag(but->rnaprop) & PROP_ENUM_FLAG) {
656                         uiTooltipField *field = text_field_add(
657                                 data, &(uiTooltipFormat){
658                                     .style = UI_TIP_STYLE_NORMAL,
659                                     .color_id = UI_TIP_LC_NORMAL,
660                                 });
661                         field->text = BLI_strdup(IFACE_("(Shift-Click/Drag to select multiple)"));
662                 }
663
664         }
665         /* Enum field label & tip */
666         if (enum_tip.strinfo) {
667                 uiTooltipField *field = text_field_add(
668                         data, &(uiTooltipFormat){
669                             .style = UI_TIP_STYLE_NORMAL,
670                             .color_id = UI_TIP_LC_VALUE,
671                             .is_pad = true,
672                         });
673                 field->text = BLI_strdup(enum_tip.strinfo);
674         }
675
676         /* Op shortcut */
677         if (op_keymap.strinfo) {
678                 uiTooltipField *field = text_field_add(
679                         data, &(uiTooltipFormat){
680                             .style = UI_TIP_STYLE_NORMAL,
681                             .color_id = UI_TIP_LC_VALUE,
682                             .is_pad = true,
683                         });
684                 field->text = BLI_sprintfN(TIP_("Shortcut: %s"), op_keymap.strinfo);
685         }
686
687         /* Property context-toggle shortcut */
688         if (prop_keymap.strinfo) {
689                 uiTooltipField *field = text_field_add(
690                         data, &(uiTooltipFormat){
691                             .style = UI_TIP_STYLE_NORMAL,
692                             .color_id = UI_TIP_LC_VALUE,
693                             .is_pad = true,
694                         });
695                 field->text = BLI_sprintfN(TIP_("Shortcut: %s"), prop_keymap.strinfo);
696         }
697
698         if (ELEM(but->type, UI_BTYPE_TEXT, UI_BTYPE_SEARCH_MENU)) {
699                 /* better not show the value of a password */
700                 if ((but->rnaprop && (RNA_property_subtype(but->rnaprop) == PROP_PASSWORD)) == 0) {
701                         /* full string */
702                         ui_but_string_get(but, buf, sizeof(buf));
703                         if (buf[0]) {
704                                 uiTooltipField *field = text_field_add(
705                                         data, &(uiTooltipFormat){
706                                             .style = UI_TIP_STYLE_NORMAL,
707                                             .color_id = UI_TIP_LC_VALUE,
708                                             .is_pad = true,
709                                         });
710                                 field->text = BLI_sprintfN(TIP_("Value: %s"), buf);
711                         }
712                 }
713         }
714
715         if (but->rnaprop) {
716                 int unit_type = UI_but_unit_type_get(but);
717
718                 if (unit_type == PROP_UNIT_ROTATION) {
719                         if (RNA_property_type(but->rnaprop) == PROP_FLOAT) {
720                                 float value = RNA_property_array_check(but->rnaprop) ?
721                                                   RNA_property_float_get_index(&but->rnapoin, but->rnaprop, but->rnaindex) :
722                                                   RNA_property_float_get(&but->rnapoin, but->rnaprop);
723
724                                 uiTooltipField *field = text_field_add(
725                                         data, &(uiTooltipFormat){
726                                             .style = UI_TIP_STYLE_NORMAL,
727                                             .color_id = UI_TIP_LC_VALUE,
728                                         });
729                                 field->text = BLI_sprintfN(TIP_("Radians: %f"), value);
730                         }
731                 }
732
733                 if (but->flag & UI_BUT_DRIVEN) {
734                         if (ui_but_anim_expression_get(but, buf, sizeof(buf))) {
735                                 uiTooltipField *field = text_field_add(
736                                         data, &(uiTooltipFormat){
737                                             .style = UI_TIP_STYLE_NORMAL,
738                                             .color_id = UI_TIP_LC_NORMAL,
739                                         });
740                                 field->text = BLI_sprintfN(TIP_("Expression: %s"), buf);
741                         }
742                 }
743
744                 if (but->rnapoin.id.data) {
745                         const ID *id = but->rnapoin.id.data;
746                         if (ID_IS_LINKED(id)) {
747                                 uiTooltipField *field = text_field_add(
748                                         data, &(uiTooltipFormat){
749                                             .style = UI_TIP_STYLE_NORMAL,
750                                             .color_id = UI_TIP_LC_NORMAL,
751                                         });
752                                 field->text = BLI_sprintfN(TIP_("Library: %s"), id->lib->name);
753                         }
754                 }
755         }
756         else if (but->optype) {
757                 PointerRNA *opptr;
758                 char *str;
759                 opptr = UI_but_operator_ptr_get(but); /* allocated when needed, the button owns it */
760
761                 /* so the context is passed to fieldf functions (some py fieldf functions use it) */
762                 WM_operator_properties_sanitize(opptr, false);
763
764                 str = WM_operator_pystring_ex(C, NULL, false, false, but->optype, opptr);
765
766                 /* avoid overly verbose tips (eg, arrays of 20 layers), exact limit is arbitrary */
767                 WM_operator_pystring_abbreviate(str, 32);
768
769                 /* operator info */
770                 if (U.flag & USER_TOOLTIPS_PYTHON) {
771                         uiTooltipField *field = text_field_add(
772                                 data, &(uiTooltipFormat){
773                                     .style = UI_TIP_STYLE_MONO,
774                                     .color_id = UI_TIP_LC_PYTHON,
775                                     .is_pad = true,
776                                 });
777                         field->text = BLI_sprintfN(TIP_("Python: %s"), str);
778                 }
779
780                 MEM_freeN(str);
781         }
782
783         /* button is disabled, we may be able to tell user why */
784         if (but->flag & UI_BUT_DISABLED) {
785                 const char *disabled_msg = NULL;
786
787                 /* if operator poll check failed, it can give pretty precise info why */
788                 if (but->optype) {
789                         CTX_wm_operator_poll_msg_set(C, NULL);
790                         WM_operator_poll_context(C, but->optype, but->opcontext);
791                         disabled_msg = CTX_wm_operator_poll_msg_get(C);
792                 }
793                 /* alternatively, buttons can store some reasoning too */
794                 else if (but->disabled_info) {
795                         disabled_msg = TIP_(but->disabled_info);
796                 }
797
798                 if (disabled_msg && disabled_msg[0]) {
799                         uiTooltipField *field = text_field_add(
800                                 data, &(uiTooltipFormat){
801                                     .style = UI_TIP_STYLE_NORMAL,
802                                     .color_id = UI_TIP_LC_ALERT,
803                                 });
804                         field->text = BLI_sprintfN(TIP_("Disabled: %s"), disabled_msg);
805                 }
806         }
807
808         if ((U.flag & USER_TOOLTIPS_PYTHON) && !but->optype && rna_struct.strinfo) {
809                 {
810                         uiTooltipField *field = text_field_add(
811                                 data, &(uiTooltipFormat){
812                                     .style = UI_TIP_STYLE_MONO,
813                                     .color_id = UI_TIP_LC_PYTHON,
814                                     .is_pad = true,
815                                 });
816                         if (rna_prop.strinfo) {
817                                 /* Struct and prop */
818                                 field->text = BLI_sprintfN(TIP_("Python: %s.%s"), rna_struct.strinfo, rna_prop.strinfo);
819                         }
820                         else {
821                                 /* Only struct (e.g. menus) */
822                                 field->text = BLI_sprintfN(TIP_("Python: %s"), rna_struct.strinfo);
823                         }
824                 }
825
826                 if (but->rnapoin.id.data) {
827                         uiTooltipField *field = text_field_add(
828                                 data, &(uiTooltipFormat){
829                                     .style = UI_TIP_STYLE_MONO,
830                                     .color_id = UI_TIP_LC_PYTHON,
831                                 });
832
833                         /* this could get its own 'BUT_GET_...' type */
834
835                         /* never fails */
836                         /* move ownership (no need for re-alloc) */
837                         if (but->rnaprop) {
838                                 field->text = RNA_path_full_property_py_ex(&but->rnapoin, but->rnaprop, but->rnaindex, true);
839                         }
840                         else {
841                                 field->text = RNA_path_full_struct_py(&but->rnapoin);
842                         }
843
844                 }
845         }
846
847         /* Free strinfo's... */
848         if (but_tip.strinfo)
849                 MEM_freeN(but_tip.strinfo);
850         if (enum_label.strinfo)
851                 MEM_freeN(enum_label.strinfo);
852         if (enum_tip.strinfo)
853                 MEM_freeN(enum_tip.strinfo);
854         if (op_keymap.strinfo)
855                 MEM_freeN(op_keymap.strinfo);
856         if (prop_keymap.strinfo)
857                 MEM_freeN(prop_keymap.strinfo);
858         if (rna_struct.strinfo)
859                 MEM_freeN(rna_struct.strinfo);
860         if (rna_prop.strinfo)
861                 MEM_freeN(rna_prop.strinfo);
862
863         if (data->fields_len == 0) {
864                 MEM_freeN(data);
865                 return NULL;
866         }
867         else {
868                 return data;
869         }
870 }
871
872 static uiTooltipData *ui_tooltip_data_from_gizmo(bContext *C, wmGizmo *gz)
873 {
874         uiTooltipData *data = MEM_callocN(sizeof(uiTooltipData), "uiTooltipData");
875
876         /* TODO(campbell): a way for gizmos to have their own descriptions (low priority). */
877
878         /* Operator Actions */
879         {
880                 bool use_drag = gz->drag_part != -1 && gz->highlight_part != gz->drag_part;
881
882                 const struct {
883                         int part;
884                         const char *prefix;
885                 } mpop_actions[] = {
886                         {
887                                 .part = gz->highlight_part,
888                                 .prefix = use_drag ? TIP_("Click") : NULL,
889                         }, {
890                                 .part = use_drag ? gz->drag_part : -1,
891                                 .prefix = use_drag ? TIP_("Drag") : NULL,
892                         },
893                 };
894
895                 for (int i = 0; i < ARRAY_SIZE(mpop_actions); i++) {
896                         wmGizmoOpElem *mpop = (mpop_actions[i].part != -1) ? WM_gizmo_operator_get(gz, mpop_actions[i].part) : NULL;
897                         if (mpop != NULL) {
898                                 /* Description */
899                                 const char *info = RNA_struct_ui_description(mpop->type->srna);
900                                 if (!(info && info[0])) {
901                                         info  = RNA_struct_ui_name(mpop->type->srna);
902                                 }
903
904                                 if (info && info[0]) {
905                                         char *text = NULL;
906                                         if (mpop_actions[i].prefix != NULL) {
907                                                 text = BLI_sprintfN("%s: %s", mpop_actions[i].prefix, info);
908                                         }
909                                         else {
910                                                 text = BLI_strdup(info);
911                                         }
912
913                                         if (text != NULL) {
914                                                 uiTooltipField *field = text_field_add(
915                                                         data, &(uiTooltipFormat){
916                                                             .style = UI_TIP_STYLE_HEADER,
917                                                             .color_id = UI_TIP_LC_VALUE,
918                                                             .is_pad = true,
919                                                         });
920                                                 field->text = text;
921                                         }
922                                 }
923
924                                 /* Shortcut */
925                                 {
926                                         bool found = false;
927                                         IDProperty *prop = mpop->ptr.data;
928                                         char buf[128];
929                                         if (WM_key_event_operator_string(
930                                                     C, mpop->type->idname, WM_OP_INVOKE_DEFAULT, prop, true,
931                                                     buf, ARRAY_SIZE(buf)))
932                                         {
933                                                 found = true;
934                                         }
935                                         uiTooltipField *field = text_field_add(
936                                                 data, &(uiTooltipFormat){
937                                                     .style = UI_TIP_STYLE_NORMAL,
938                                                     .color_id = UI_TIP_LC_VALUE,
939                                                     .is_pad = true,
940                                                 });
941                                         field->text = BLI_sprintfN(TIP_("Shortcut: %s"), found ? buf : "None");
942                                 }
943                         }
944                 }
945         }
946
947         /* Property Actions */
948         if (gz->type->target_property_defs_len) {
949                 wmGizmoProperty *gz_prop_array = WM_gizmo_target_property_array(gz);
950                 for (int i = 0; i < gz->type->target_property_defs_len; i++) {
951                         /* TODO(campbell): function callback descriptions. */
952                         wmGizmoProperty *gz_prop = &gz_prop_array[i];
953                         if (gz_prop->prop != NULL) {
954                                 const char *info = RNA_property_ui_description(gz_prop->prop);
955                                 if (info && info[0]) {
956                                         uiTooltipField *field = text_field_add(
957                                                 data, &(uiTooltipFormat){
958                                                     .style = UI_TIP_STYLE_NORMAL,
959                                                     .color_id = UI_TIP_LC_VALUE,
960                                                     .is_pad = true,
961                                                 });
962                                         field->text = BLI_strdup(info);
963                                 }
964                         }
965                 }
966         }
967
968         if (data->fields_len == 0) {
969                 MEM_freeN(data);
970                 return NULL;
971         }
972         else {
973                 return data;
974         }
975 }
976
977
978 static ARegion *ui_tooltip_create_with_data(
979         bContext *C, uiTooltipData *data,
980         const float init_position[2], const rcti *init_rect_overlap,
981         const float aspect)
982 {
983         const float pad_px = UI_TIP_PADDING;
984         wmWindow *win = CTX_wm_window(C);
985         const int winx = WM_window_pixels_x(win);
986         const int winy = WM_window_pixels_y(win);
987         uiStyle *style = UI_style_get();
988         static ARegionType type;
989         ARegion *ar;
990         int fonth, fontw;
991         int h, i;
992         rcti rect_i;
993         int font_flag = 0;
994
995         /* create area region */
996         ar = ui_region_temp_add(CTX_wm_screen(C));
997
998         memset(&type, 0, sizeof(ARegionType));
999         type.draw = ui_tooltip_region_draw_cb;
1000         type.free = ui_tooltip_region_free_cb;
1001         type.regionid = RGN_TYPE_TEMPORARY;
1002         ar->type = &type;
1003
1004         /* set font, get bb */
1005         data->fstyle = style->widget; /* copy struct */
1006         ui_fontscale(&data->fstyle.points, aspect);
1007
1008         UI_fontstyle_set(&data->fstyle);
1009
1010         data->wrap_width = min_ii(UI_TIP_MAXWIDTH * U.pixelsize / aspect, winx - (UI_TIP_PADDING * 2));
1011
1012         font_flag |= BLF_WORD_WRAP;
1013         if (data->fstyle.kerning == 1) {
1014                 font_flag |= BLF_KERNING_DEFAULT;
1015         }
1016         BLF_enable(data->fstyle.uifont_id, font_flag);
1017         BLF_enable(blf_mono_font, font_flag);
1018         BLF_wordwrap(data->fstyle.uifont_id, data->wrap_width);
1019         BLF_wordwrap(blf_mono_font, data->wrap_width);
1020
1021         /* these defines tweaked depending on font */
1022 #define TIP_BORDER_X (16.0f / aspect)
1023 #define TIP_BORDER_Y (6.0f / aspect)
1024
1025         h = BLF_height_max(data->fstyle.uifont_id);
1026
1027         for (i = 0, fontw = 0, fonth = 0; i < data->fields_len; i++) {
1028                 uiTooltipField *field = &data->fields[i];
1029                 uiTooltipField *field_next = (i + 1) != data->fields_len ? &data->fields[i + 1] : NULL;
1030
1031                 struct ResultBLF info;
1032                 int w, x_pos = 0;
1033                 int font_id;
1034
1035                 if (field->format.style == UI_TIP_STYLE_MONO) {
1036                         BLF_size(blf_mono_font, data->fstyle.points * U.pixelsize, U.dpi);
1037                         font_id = blf_mono_font;
1038                 }
1039                 else {
1040                         BLI_assert(ELEM(field->format.style, UI_TIP_STYLE_NORMAL, UI_TIP_STYLE_HEADER));
1041                         font_id = data->fstyle.uifont_id;
1042                 }
1043                 w = BLF_width_ex(font_id, field->text, BLF_DRAW_STR_DUMMY_MAX, &info);
1044
1045                 /* check for suffix (enum label) */
1046                 if (field->text_suffix && field->text_suffix[0]) {
1047                         x_pos = info.width;
1048                         w = max_ii(w, x_pos + BLF_width(font_id, field->text_suffix, BLF_DRAW_STR_DUMMY_MAX));
1049                 }
1050                 fontw = max_ii(fontw, w);
1051
1052                 fonth += h * info.lines;
1053                 if (field_next && field_next->format.is_pad) {
1054                         fonth += h * (UI_TIP_PAD_FAC - 1);
1055                 }
1056
1057                 field->geom.lines = info.lines;
1058                 field->geom.x_pos = x_pos;
1059         }
1060
1061         //fontw *= aspect;
1062
1063         BLF_disable(data->fstyle.uifont_id, font_flag);
1064         BLF_disable(blf_mono_font, font_flag);
1065
1066         ar->regiondata = data;
1067
1068         data->toth = fonth;
1069         data->lineh = h;
1070
1071         /* Compute position. */
1072         {
1073                 rctf rect_fl;
1074                 rect_fl.xmin = init_position[0] - TIP_BORDER_X;
1075                 rect_fl.xmax = rect_fl.xmin + fontw + pad_px;
1076                 rect_fl.ymax = init_position[1] - TIP_BORDER_Y;
1077                 rect_fl.ymin = rect_fl.ymax - fonth - TIP_BORDER_Y;
1078                 BLI_rcti_rctf_copy(&rect_i, &rect_fl);
1079         }
1080
1081 #undef TIP_BORDER_X
1082 #undef TIP_BORDER_Y
1083
1084
1085         /* Clamp to window bounds. */
1086         {
1087                 /* Ensure at least 5 px above screen bounds
1088                  * UI_UNIT_Y is just a guess to be above the menu item */
1089                 if (init_rect_overlap != NULL) {
1090                         const int pad = max_ff(1.0f, U.pixelsize) * 5;
1091                         const rcti init_rect = {
1092                                 .xmin = init_rect_overlap->xmin - pad,
1093                                 .xmax = init_rect_overlap->xmax + pad,
1094                                 .ymin = init_rect_overlap->ymin - pad,
1095                                 .ymax = init_rect_overlap->ymax + pad,
1096                         };
1097                         const rcti rect_clamp = {
1098                                 .xmin = 0,
1099                                 .xmax = winx,
1100                                 .ymin = 0,
1101                                 .ymax = winy,
1102                         };
1103                         /* try right. */
1104                         const int size_x = BLI_rcti_size_x(&rect_i);
1105                         const int size_y = BLI_rcti_size_y(&rect_i);
1106                         const int cent_overlap_x = BLI_rcti_cent_x(&init_rect);
1107                         const int cent_overlap_y = BLI_rcti_cent_y(&init_rect);
1108                         struct {
1109                                 rcti xpos;
1110                                 rcti xneg;
1111                                 rcti ypos;
1112                                 rcti yneg;
1113                         } rect;
1114
1115                         {       /* xpos */
1116                                 rcti r = rect_i;
1117                                 r.xmin = init_rect.xmax;
1118                                 r.xmax = r.xmin + size_x;
1119                                 r.ymin = cent_overlap_y - (size_y / 2);
1120                                 r.ymax = r.ymin + size_y;
1121                                 rect.xpos = r;
1122                         }
1123                         {       /* xneg */
1124                                 rcti r = rect_i;
1125                                 r.xmin = init_rect.xmin - size_x;
1126                                 r.xmax = r.xmin + size_x;
1127                                 r.ymin = cent_overlap_y - (size_y / 2);
1128                                 r.ymax = r.ymin + size_y;
1129                                 rect.xneg = r;
1130                         }
1131                         {       /* ypos */
1132                                 rcti r = rect_i;
1133                                 r.xmin = cent_overlap_x - (size_x / 2);
1134                                 r.xmax = r.xmin + size_x;
1135                                 r.ymin = init_rect.ymax;
1136                                 r.ymax = r.ymin + size_y;
1137                                 rect.ypos = r;
1138                         }
1139                         {       /* yneg */
1140                                 rcti r = rect_i;
1141                                 r.xmin = cent_overlap_x - (size_x / 2);
1142                                 r.xmax = r.xmin + size_x;
1143                                 r.ymin = init_rect.ymin - size_y;
1144                                 r.ymax = r.ymin + size_y;
1145                                 rect.yneg = r;
1146                         }
1147
1148                         bool found = false;
1149                         for (int j = 0; j < 4; j++) {
1150                                 const rcti *r = (&rect.xpos) + j;
1151                                 if (BLI_rcti_inside_rcti(&rect_clamp, r)) {
1152                                         rect_i = *r;
1153                                         found = true;
1154                                         break;
1155                                 }
1156                         }
1157                         if (!found) {
1158                                 /* Fallback, we could pick the best fallback, for now just use xpos. */
1159                                 int offset_dummy[2];
1160                                 rect_i = rect.xpos;
1161                                 BLI_rcti_clamp(&rect_i, &rect_clamp, offset_dummy);
1162                         }
1163
1164                 }
1165                 else {
1166                         const int pad = max_ff(1.0f, U.pixelsize) * 5;
1167                         const rcti rect_clamp = {
1168                                 .xmin = pad,
1169                                 .xmax = winx - pad,
1170                                 .ymin = pad + (UI_UNIT_Y * 2),
1171                                 .ymax = winy - pad,
1172                         };
1173                         int offset_dummy[2];
1174                         BLI_rcti_clamp(&rect_i, &rect_clamp, offset_dummy);
1175                 }
1176         }
1177
1178         /* add padding */
1179         BLI_rcti_resize(&rect_i,
1180                         BLI_rcti_size_x(&rect_i) + pad_px,
1181                         BLI_rcti_size_y(&rect_i) + pad_px);
1182
1183         /* widget rect, in region coords */
1184         {
1185                 /* Compensate for margin offset, visually this corrects the position. */
1186                 const int margin = UI_POPUP_MARGIN;
1187                 if (init_rect_overlap != NULL) {
1188                         BLI_rcti_translate(&rect_i, margin, margin / 2);
1189                 }
1190
1191                 data->bbox.xmin = margin;
1192                 data->bbox.xmax = BLI_rcti_size_x(&rect_i) - margin;
1193                 data->bbox.ymin = margin;
1194                 data->bbox.ymax = BLI_rcti_size_y(&rect_i);
1195
1196                 /* region bigger for shadow */
1197                 ar->winrct.xmin = rect_i.xmin - margin;
1198                 ar->winrct.xmax = rect_i.xmax + margin;
1199                 ar->winrct.ymin = rect_i.ymin - margin;
1200                 ar->winrct.ymax = rect_i.ymax + margin;
1201         }
1202
1203         /* adds subwindow */
1204         ED_region_init(ar);
1205
1206         /* notify change and redraw */
1207         ED_region_tag_redraw(ar);
1208
1209         return ar;
1210 }
1211
1212 /** \} */
1213
1214 /* -------------------------------------------------------------------- */
1215 /** \name ToolTip Public API
1216  * \{ */
1217
1218
1219 ARegion *UI_tooltip_create_from_button(bContext *C, ARegion *butregion, uiBut *but, bool is_label)
1220 {
1221         wmWindow *win = CTX_wm_window(C);
1222         /* aspect values that shrink text are likely unreadable */
1223         const float aspect = min_ff(1.0f, but->block->aspect);
1224         float init_position[2];
1225
1226         if (but->drawflag & UI_BUT_NO_TOOLTIP) {
1227                 return NULL;
1228         }
1229         uiTooltipData *data = NULL;
1230
1231         if (data == NULL) {
1232                 data = ui_tooltip_data_from_tool(C, but, is_label);
1233         }
1234
1235         if (data == NULL) {
1236                 data = ui_tooltip_data_from_button(C, but);
1237         }
1238
1239         if (data == NULL) {
1240                 return NULL;
1241         }
1242
1243         const bool is_no_overlap = is_label && UI_but_has_tooltip_label(but);
1244         rcti init_rect;
1245         if (is_no_overlap) {
1246                 rctf overlap_rect_fl;
1247                 init_position[0] = BLI_rctf_cent_x(&but->rect);
1248                 init_position[1] = BLI_rctf_cent_y(&but->rect);
1249                 if (butregion) {
1250                         ui_block_to_window_fl(butregion, but->block, &init_position[0], &init_position[1]);
1251                         ui_block_to_window_rctf(butregion, but->block, &overlap_rect_fl, &but->rect);
1252                 }
1253                 else {
1254                         overlap_rect_fl = but->rect;
1255                 }
1256                 BLI_rcti_rctf_copy_round(&init_rect, &overlap_rect_fl);
1257         }
1258         else {
1259                 init_position[0] = BLI_rctf_cent_x(&but->rect);
1260                 init_position[1] = but->rect.ymin - (UI_POPUP_MARGIN / 2);
1261                 if (butregion) {
1262                         ui_block_to_window_fl(butregion, but->block, &init_position[0], &init_position[1]);
1263                         init_position[0] = win->eventstate->x;
1264                 }
1265         }
1266
1267         ARegion *ar = ui_tooltip_create_with_data(C, data, init_position, is_no_overlap ? &init_rect : NULL, aspect);
1268
1269         return ar;
1270 }
1271
1272 ARegion *UI_tooltip_create_from_gizmo(bContext *C, wmGizmo *gz)
1273 {
1274         wmWindow *win = CTX_wm_window(C);
1275         const float aspect = 1.0f;
1276         float init_position[2];
1277
1278         uiTooltipData *data = ui_tooltip_data_from_gizmo(C, gz);
1279         if (data == NULL) {
1280                 return NULL;
1281         }
1282
1283         init_position[0] = win->eventstate->x;
1284         init_position[1] = win->eventstate->y;
1285
1286         return ui_tooltip_create_with_data(C, data, init_position, NULL, aspect);
1287 }
1288
1289 void UI_tooltip_free(bContext *C, bScreen *sc, ARegion *ar)
1290 {
1291         ui_region_temp_remove(C, sc, ar);
1292 }
1293
1294 /** \} */