Merge branch 'master' into blender2.8
[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)
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         char tool_name[MAX_NAME];
382         RNA_string_get(but->opptr, "name", tool_name);
383         BLI_assert(tool_name[0] != '\0');
384
385         /* We have a tool, now extract the info. */
386         uiTooltipData *data = MEM_callocN(sizeof(uiTooltipData), "uiTooltipData");
387
388 #ifdef WITH_PYTHON
389         /* it turns out to be most simple to do this via Python since C
390          * doesn't have access to information about non-active tools.
391          */
392
393         /* Title (when icon-only). */
394         if (but->drawstr[0] == '\0') {
395                 uiTooltipField *field = text_field_add(
396                         data, &(uiTooltipFormat){
397                             .style = UI_TIP_STYLE_NORMAL,
398                             .color_id = UI_TIP_LC_MAIN,
399                             .is_pad = true,
400                         });
401                 field->text = BLI_strdup(tool_name);
402         }
403
404         /* Tip. */
405         {
406                 const char *expr_imports[] = {"bpy", "bl_ui", NULL};
407                 char expr[256];
408                 SNPRINTF(
409                         expr,
410                         "bl_ui.space_toolsystem_common.description_from_name("
411                         "bpy.context, "
412                         "bpy.context.space_data.type, "
413                         "'%s') + '.'",
414                         tool_name);
415
416                 char *expr_result = NULL;
417                 if (BPY_execute_string_as_string(C, expr_imports, expr, true, &expr_result)) {
418                         if (!STREQ(expr_result, ".")) {
419                                 uiTooltipField *field = text_field_add(
420                                         data, &(uiTooltipFormat){
421                                             .style = UI_TIP_STYLE_NORMAL,
422                                             .color_id = UI_TIP_LC_MAIN,
423                                             .is_pad = true,
424                                         });
425                                 field->text = expr_result;
426                         }
427                         else {
428                                 MEM_freeN(expr_result);
429                         }
430                 }
431                 else {
432                         BLI_assert(0);
433                 }
434         }
435
436         /* Shortcut. */
437         {
438                 /* There are different kinds of shortcuts:
439                  *
440                  * - Direct access to the tool (as if the toolbar button is pressed).
441                  * - The key is bound to a brush type (not the exact brush name).
442                  * - The key is assigned to the operator it's self (bypassing the tool, executing the operator).
443                  *
444                  * Either way case it's useful to show the shortcut.
445                  */
446                 char *shortcut = NULL;
447
448                 {
449                         uiStringInfo op_keymap = {BUT_GET_OP_KEYMAP, NULL};
450                         UI_but_string_info_get(C, but, &op_keymap, NULL);
451                         shortcut = op_keymap.strinfo;
452                 }
453
454                 if (shortcut == NULL) {
455                         int mode = CTX_data_mode_enum(C);
456                         const char *tool_attr = NULL;
457                         uint tool_offset = 0;
458
459                         switch (mode) {
460                                 case CTX_MODE_SCULPT:
461                                         tool_attr = "sculpt_tool";
462                                         tool_offset = offsetof(Brush, sculpt_tool);
463                                         break;
464                                 case CTX_MODE_PAINT_VERTEX:
465                                         tool_attr = "vertex_paint_tool";
466                                         tool_offset = offsetof(Brush, vertexpaint_tool);
467                                         break;
468                                 case CTX_MODE_PAINT_WEIGHT:
469                                         tool_attr = "weight_paint_tool";
470                                         tool_offset = offsetof(Brush, vertexpaint_tool);
471                                         break;
472                                 case CTX_MODE_PAINT_TEXTURE:
473                                         tool_attr = "texture_paint_tool";
474                                         tool_offset = offsetof(Brush, imagepaint_tool);
475                                         break;
476                                 default:
477                                         break;
478                         }
479
480                         if (tool_attr != NULL) {
481                                 struct Main *bmain = CTX_data_main(C);
482                                 Brush *brush = (Brush *)BKE_libblock_find_name(bmain, ID_BR, tool_name);
483                                 if (brush) {
484                                         Object *ob = CTX_data_active_object(C);
485                                         wmOperatorType *ot = WM_operatortype_find("paint.brush_select", true);
486
487                                         PointerRNA op_props;
488                                         WM_operator_properties_create_ptr(&op_props, ot);
489                                         RNA_enum_set(&op_props, "paint_mode", ob->mode);
490                                         RNA_enum_set(&op_props, tool_attr, *(((char *)brush) + tool_offset));
491
492                                         /* Check for direct access to the tool. */
493                                         char shortcut_brush[128] = "";
494                                         if (WM_key_event_operator_string(
495                                                     C, ot->idname, WM_OP_INVOKE_REGION_WIN, op_props.data, true,
496                                                     shortcut_brush, ARRAY_SIZE(shortcut_brush)))
497                                         {
498                                                 shortcut = BLI_strdup(shortcut_brush);
499                                         }
500                                         WM_operator_properties_free(&op_props);
501                                 }
502                         }
503                 }
504
505                 if (shortcut == NULL) {
506                         /* Check for direct access to the tool. */
507                         char shortcut_toolbar[128] = "";
508                         if (WM_key_event_operator_string(
509                                     C, "WM_OT_toolbar", WM_OP_INVOKE_REGION_WIN, NULL, true,
510                                     shortcut_toolbar, ARRAY_SIZE(shortcut_toolbar)))
511                         {
512                                 /* Generate keymap in order to inspect it.
513                                  * Note, we could make a utility to avoid the keymap generation part of this. */
514                                 const char *expr_imports[] = {"bpy", "bl_ui", NULL};
515                                 const char *expr = (
516                                         "getattr("
517                                         "bl_ui.space_toolsystem_common.keymap_from_context("
518                                         "bpy.context, "
519                                         "bpy.context.space_data.type), "
520                                         "'as_pointer', lambda: 0)()");
521
522                                 intptr_t expr_result = 0;
523                                 if (BPY_execute_string_as_intptr(C, expr_imports, expr, true, &expr_result)) {
524                                         if (expr_result != 0) {
525                                                 wmKeyMap *keymap = (wmKeyMap *)expr_result;
526                                                 for (wmKeyMapItem *kmi = keymap->items.first; kmi; kmi = kmi->next) {
527                                                         if (STREQ(kmi->idname, but->optype->idname)) {
528                                                                 char tool_name_test[MAX_NAME];
529                                                                 RNA_string_get(kmi->ptr, "name", tool_name_test);
530                                                                 if (STREQ(tool_name, tool_name_test)) {
531                                                                         char buf[128];
532                                                                         WM_keymap_item_to_string(kmi, false, buf, sizeof(buf));
533                                                                         shortcut = BLI_sprintfN("%s, %s", shortcut_toolbar, buf);
534                                                                         break;
535                                                                 }
536                                                         }
537                                                 }
538                                         }
539                                 }
540                                 else {
541                                         BLI_assert(0);
542                                 }
543                         }
544                 }
545
546                 if (shortcut != NULL) {
547                         uiTooltipField *field = text_field_add(
548                                 data, &(uiTooltipFormat){
549                                     .style = UI_TIP_STYLE_NORMAL,
550                                     .color_id = UI_TIP_LC_VALUE,
551                                     .is_pad = true,
552                                 });
553                         field->text = BLI_sprintfN(TIP_("Shortcut: %s"), shortcut);
554                         MEM_freeN(shortcut);
555                 }
556         }
557
558         /* Keymap */
559
560         /* This is too handy not to expose somehow, let's be sneaky for now. */
561         if (CTX_wm_window(C)->eventstate->shift) {
562                 const char *expr_imports[] = {"bpy", "bl_ui", NULL};
563                 char expr[256];
564                 SNPRINTF(
565                         expr,
566                         "getattr("
567                         "bl_ui.space_toolsystem_common.keymap_from_name("
568                         "bpy.context, "
569                         "bpy.context.space_data.type, "
570                         "'%s'), "
571                         "'as_pointer', lambda: 0)()",
572                         tool_name);
573
574                 intptr_t expr_result = 0;
575                 if (BPY_execute_string_as_intptr(C, expr_imports, expr, true, &expr_result)) {
576                         if (expr_result != 0) {
577                                 {
578                                         uiTooltipField *field = text_field_add(
579                                                 data, &(uiTooltipFormat){
580                                                     .style = UI_TIP_STYLE_NORMAL,
581                                                     .color_id = UI_TIP_LC_NORMAL,
582                                                     .is_pad = true,
583                                                 });
584                                         field->text = BLI_strdup("Tool Keymap:");
585                                 }
586                                 wmKeyMap *keymap = (wmKeyMap *)expr_result;
587                                 ui_tooltip_data_append_from_keymap(C, data, keymap);
588                         }
589                 }
590                 else {
591                         BLI_assert(0);
592                 }
593         }
594 #endif  /* WITH_PYTHON */
595
596         if (data->fields_len == 0) {
597                 MEM_freeN(data);
598                 return NULL;
599         }
600         else {
601                 return data;
602         }
603 }
604
605 static uiTooltipData *ui_tooltip_data_from_button(bContext *C, uiBut *but)
606 {
607         uiStringInfo but_tip = {BUT_GET_TIP, NULL};
608         uiStringInfo enum_label = {BUT_GET_RNAENUM_LABEL, NULL};
609         uiStringInfo enum_tip = {BUT_GET_RNAENUM_TIP, NULL};
610         uiStringInfo op_keymap = {BUT_GET_OP_KEYMAP, NULL};
611         uiStringInfo prop_keymap = {BUT_GET_PROP_KEYMAP, NULL};
612         uiStringInfo rna_struct = {BUT_GET_RNASTRUCT_IDENTIFIER, NULL};
613         uiStringInfo rna_prop = {BUT_GET_RNAPROP_IDENTIFIER, NULL};
614
615         char buf[512];
616
617         /* create tooltip data */
618         uiTooltipData *data = MEM_callocN(sizeof(uiTooltipData), "uiTooltipData");
619
620         UI_but_string_info_get(C, but, &but_tip, &enum_label, &enum_tip, &op_keymap, &prop_keymap, &rna_struct, &rna_prop, NULL);
621
622         /* Tip */
623         if (but_tip.strinfo) {
624                 {
625                         uiTooltipField *field = text_field_add(
626                                 data, &(uiTooltipFormat){
627                                     .style = UI_TIP_STYLE_HEADER,
628                                     .color_id = UI_TIP_LC_NORMAL,
629                                 });
630                         if (enum_label.strinfo) {
631                                 field->text = BLI_sprintfN("%s:  ", but_tip.strinfo);
632                                 field->text_suffix = BLI_strdup(enum_label.strinfo);
633                         }
634                         else {
635                                 field->text = BLI_sprintfN("%s.", but_tip.strinfo);
636                         }
637                 }
638
639                 /* special case enum rna buttons */
640                 if ((but->type & UI_BTYPE_ROW) && but->rnaprop && RNA_property_flag(but->rnaprop) & PROP_ENUM_FLAG) {
641                         uiTooltipField *field = text_field_add(
642                                 data, &(uiTooltipFormat){
643                                     .style = UI_TIP_STYLE_NORMAL,
644                                     .color_id = UI_TIP_LC_NORMAL,
645                                 });
646                         field->text = BLI_strdup(IFACE_("(Shift-Click/Drag to select multiple)"));
647                 }
648
649         }
650         /* Enum field label & tip */
651         if (enum_tip.strinfo) {
652                 uiTooltipField *field = text_field_add(
653                         data, &(uiTooltipFormat){
654                             .style = UI_TIP_STYLE_NORMAL,
655                             .color_id = UI_TIP_LC_VALUE,
656                             .is_pad = true,
657                         });
658                 field->text = BLI_strdup(enum_tip.strinfo);
659         }
660
661         /* Op shortcut */
662         if (op_keymap.strinfo) {
663                 uiTooltipField *field = text_field_add(
664                         data, &(uiTooltipFormat){
665                             .style = UI_TIP_STYLE_NORMAL,
666                             .color_id = UI_TIP_LC_VALUE,
667                             .is_pad = true,
668                         });
669                 field->text = BLI_sprintfN(TIP_("Shortcut: %s"), op_keymap.strinfo);
670         }
671
672         /* Property context-toggle shortcut */
673         if (prop_keymap.strinfo) {
674                 uiTooltipField *field = text_field_add(
675                         data, &(uiTooltipFormat){
676                             .style = UI_TIP_STYLE_NORMAL,
677                             .color_id = UI_TIP_LC_VALUE,
678                             .is_pad = true,
679                         });
680                 field->text = BLI_sprintfN(TIP_("Shortcut: %s"), prop_keymap.strinfo);
681         }
682
683         if (ELEM(but->type, UI_BTYPE_TEXT, UI_BTYPE_SEARCH_MENU)) {
684                 /* better not show the value of a password */
685                 if ((but->rnaprop && (RNA_property_subtype(but->rnaprop) == PROP_PASSWORD)) == 0) {
686                         /* full string */
687                         ui_but_string_get(but, buf, sizeof(buf));
688                         if (buf[0]) {
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_("Value: %s"), buf);
696                         }
697                 }
698         }
699
700         if (but->rnaprop) {
701                 int unit_type = UI_but_unit_type_get(but);
702
703                 if (unit_type == PROP_UNIT_ROTATION) {
704                         if (RNA_property_type(but->rnaprop) == PROP_FLOAT) {
705                                 float value = RNA_property_array_check(but->rnaprop) ?
706                                                   RNA_property_float_get_index(&but->rnapoin, but->rnaprop, but->rnaindex) :
707                                                   RNA_property_float_get(&but->rnapoin, but->rnaprop);
708
709                                 uiTooltipField *field = text_field_add(
710                                         data, &(uiTooltipFormat){
711                                             .style = UI_TIP_STYLE_NORMAL,
712                                             .color_id = UI_TIP_LC_VALUE,
713                                         });
714                                 field->text = BLI_sprintfN(TIP_("Radians: %f"), value);
715                         }
716                 }
717
718                 if (but->flag & UI_BUT_DRIVEN) {
719                         if (ui_but_anim_expression_get(but, buf, sizeof(buf))) {
720                                 uiTooltipField *field = text_field_add(
721                                         data, &(uiTooltipFormat){
722                                             .style = UI_TIP_STYLE_NORMAL,
723                                             .color_id = UI_TIP_LC_NORMAL,
724                                         });
725                                 field->text = BLI_sprintfN(TIP_("Expression: %s"), buf);
726                         }
727                 }
728
729                 if (but->rnapoin.id.data) {
730                         const ID *id = but->rnapoin.id.data;
731                         if (ID_IS_LINKED(id)) {
732                                 uiTooltipField *field = text_field_add(
733                                         data, &(uiTooltipFormat){
734                                             .style = UI_TIP_STYLE_NORMAL,
735                                             .color_id = UI_TIP_LC_NORMAL,
736                                         });
737                                 field->text = BLI_sprintfN(TIP_("Library: %s"), id->lib->name);
738                         }
739                 }
740         }
741         else if (but->optype) {
742                 PointerRNA *opptr;
743                 char *str;
744                 opptr = UI_but_operator_ptr_get(but); /* allocated when needed, the button owns it */
745
746                 /* so the context is passed to fieldf functions (some py fieldf functions use it) */
747                 WM_operator_properties_sanitize(opptr, false);
748
749                 str = WM_operator_pystring_ex(C, NULL, false, false, but->optype, opptr);
750
751                 /* avoid overly verbose tips (eg, arrays of 20 layers), exact limit is arbitrary */
752                 WM_operator_pystring_abbreviate(str, 32);
753
754                 /* operator info */
755                 if (U.flag & USER_TOOLTIPS_PYTHON) {
756                         uiTooltipField *field = text_field_add(
757                                 data, &(uiTooltipFormat){
758                                     .style = UI_TIP_STYLE_MONO,
759                                     .color_id = UI_TIP_LC_PYTHON,
760                                     .is_pad = true,
761                                 });
762                         field->text = BLI_sprintfN(TIP_("Python: %s"), str);
763                 }
764
765                 MEM_freeN(str);
766         }
767
768         /* button is disabled, we may be able to tell user why */
769         if (but->flag & UI_BUT_DISABLED) {
770                 const char *disabled_msg = NULL;
771
772                 /* if operator poll check failed, it can give pretty precise info why */
773                 if (but->optype) {
774                         CTX_wm_operator_poll_msg_set(C, NULL);
775                         WM_operator_poll_context(C, but->optype, but->opcontext);
776                         disabled_msg = CTX_wm_operator_poll_msg_get(C);
777                 }
778                 /* alternatively, buttons can store some reasoning too */
779                 else if (but->disabled_info) {
780                         disabled_msg = TIP_(but->disabled_info);
781                 }
782
783                 if (disabled_msg && disabled_msg[0]) {
784                         uiTooltipField *field = text_field_add(
785                                 data, &(uiTooltipFormat){
786                                     .style = UI_TIP_STYLE_NORMAL,
787                                     .color_id = UI_TIP_LC_ALERT,
788                                 });
789                         field->text = BLI_sprintfN(TIP_("Disabled: %s"), disabled_msg);
790                 }
791         }
792
793         if ((U.flag & USER_TOOLTIPS_PYTHON) && !but->optype && rna_struct.strinfo) {
794                 {
795                         uiTooltipField *field = text_field_add(
796                                 data, &(uiTooltipFormat){
797                                     .style = UI_TIP_STYLE_MONO,
798                                     .color_id = UI_TIP_LC_PYTHON,
799                                     .is_pad = true,
800                                 });
801                         if (rna_prop.strinfo) {
802                                 /* Struct and prop */
803                                 field->text = BLI_sprintfN(TIP_("Python: %s.%s"), rna_struct.strinfo, rna_prop.strinfo);
804                         }
805                         else {
806                                 /* Only struct (e.g. menus) */
807                                 field->text = BLI_sprintfN(TIP_("Python: %s"), rna_struct.strinfo);
808                         }
809                 }
810
811                 if (but->rnapoin.id.data) {
812                         uiTooltipField *field = text_field_add(
813                                 data, &(uiTooltipFormat){
814                                     .style = UI_TIP_STYLE_MONO,
815                                     .color_id = UI_TIP_LC_PYTHON,
816                                 });
817
818                         /* this could get its own 'BUT_GET_...' type */
819
820                         /* never fails */
821                         /* move ownership (no need for re-alloc) */
822                         if (but->rnaprop) {
823                                 field->text = RNA_path_full_property_py_ex(&but->rnapoin, but->rnaprop, but->rnaindex, true);
824                         }
825                         else {
826                                 field->text = RNA_path_full_struct_py(&but->rnapoin);
827                         }
828
829                 }
830         }
831
832         /* Free strinfo's... */
833         if (but_tip.strinfo)
834                 MEM_freeN(but_tip.strinfo);
835         if (enum_label.strinfo)
836                 MEM_freeN(enum_label.strinfo);
837         if (enum_tip.strinfo)
838                 MEM_freeN(enum_tip.strinfo);
839         if (op_keymap.strinfo)
840                 MEM_freeN(op_keymap.strinfo);
841         if (prop_keymap.strinfo)
842                 MEM_freeN(prop_keymap.strinfo);
843         if (rna_struct.strinfo)
844                 MEM_freeN(rna_struct.strinfo);
845         if (rna_prop.strinfo)
846                 MEM_freeN(rna_prop.strinfo);
847
848         if (data->fields_len == 0) {
849                 MEM_freeN(data);
850                 return NULL;
851         }
852         else {
853                 return data;
854         }
855 }
856
857 static uiTooltipData *ui_tooltip_data_from_gizmo(bContext *C, wmGizmo *gz)
858 {
859         uiTooltipData *data = MEM_callocN(sizeof(uiTooltipData), "uiTooltipData");
860
861         /* TODO(campbell): a way for gizmos to have their own descriptions (low priority). */
862
863         /* Operator Actions */
864         {
865                 bool use_drag = gz->drag_part != -1 && gz->highlight_part != gz->drag_part;
866
867                 const struct {
868                         int part;
869                         const char *prefix;
870                 } mpop_actions[] = {
871                         {
872                                 .part = gz->highlight_part,
873                                 .prefix = use_drag ? TIP_("Click") : NULL,
874                         }, {
875                                 .part = use_drag ? gz->drag_part : -1,
876                                 .prefix = use_drag ? TIP_("Drag") : NULL,
877                         },
878                 };
879
880                 for (int i = 0; i < ARRAY_SIZE(mpop_actions); i++) {
881                         wmGizmoOpElem *mpop = (mpop_actions[i].part != -1) ? WM_gizmo_operator_get(gz, mpop_actions[i].part) : NULL;
882                         if (mpop != NULL) {
883                                 /* Description */
884                                 const char *info = RNA_struct_ui_description(mpop->type->srna);
885                                 if (!(info && info[0])) {
886                                         info  = RNA_struct_ui_name(mpop->type->srna);
887                                 }
888
889                                 if (info && info[0]) {
890                                         char *text = NULL;
891                                         if (mpop_actions[i].prefix != NULL) {
892                                                 text = BLI_sprintfN("%s: %s", mpop_actions[i].prefix, info);
893                                         }
894                                         else {
895                                                 text = BLI_strdup(info);
896                                         }
897
898                                         if (text != NULL) {
899                                                 uiTooltipField *field = text_field_add(
900                                                         data, &(uiTooltipFormat){
901                                                             .style = UI_TIP_STYLE_HEADER,
902                                                             .color_id = UI_TIP_LC_VALUE,
903                                                             .is_pad = true,
904                                                         });
905                                                 field->text = text;
906                                         }
907                                 }
908
909                                 /* Shortcut */
910                                 {
911                                         bool found = false;
912                                         IDProperty *prop = mpop->ptr.data;
913                                         char buf[128];
914                                         if (WM_key_event_operator_string(
915                                                     C, mpop->type->idname, WM_OP_INVOKE_DEFAULT, prop, true,
916                                                     buf, ARRAY_SIZE(buf)))
917                                         {
918                                                 found = true;
919                                         }
920                                         uiTooltipField *field = text_field_add(
921                                                 data, &(uiTooltipFormat){
922                                                     .style = UI_TIP_STYLE_NORMAL,
923                                                     .color_id = UI_TIP_LC_VALUE,
924                                                     .is_pad = true,
925                                                 });
926                                         field->text = BLI_sprintfN(TIP_("Shortcut: %s"), found ? buf : "None");
927                                 }
928                         }
929                 }
930         }
931
932         /* Property Actions */
933         if (gz->type->target_property_defs_len) {
934                 wmGizmoProperty *gz_prop_array = WM_gizmo_target_property_array(gz);
935                 for (int i = 0; i < gz->type->target_property_defs_len; i++) {
936                         /* TODO(campbell): function callback descriptions. */
937                         wmGizmoProperty *gz_prop = &gz_prop_array[i];
938                         if (gz_prop->prop != NULL) {
939                                 const char *info = RNA_property_ui_description(gz_prop->prop);
940                                 if (info && info[0]) {
941                                         uiTooltipField *field = text_field_add(
942                                                 data, &(uiTooltipFormat){
943                                                     .style = UI_TIP_STYLE_NORMAL,
944                                                     .color_id = UI_TIP_LC_VALUE,
945                                                     .is_pad = true,
946                                                 });
947                                         field->text = BLI_strdup(info);
948                                 }
949                         }
950                 }
951         }
952
953         if (data->fields_len == 0) {
954                 MEM_freeN(data);
955                 return NULL;
956         }
957         else {
958                 return data;
959         }
960 }
961
962
963 static ARegion *ui_tooltip_create_with_data(
964         bContext *C, uiTooltipData *data,
965         const float init_position[2],
966         const float aspect)
967 {
968         const float pad_px = UI_TIP_PADDING;
969         wmWindow *win = CTX_wm_window(C);
970         const int winx = WM_window_pixels_x(win);
971         const int winy = WM_window_pixels_y(win);
972         uiStyle *style = UI_style_get();
973         static ARegionType type;
974         ARegion *ar;
975         int fonth, fontw;
976         int h, i;
977         rcti rect_i;
978         int font_flag = 0;
979
980         /* create area region */
981         ar = ui_region_temp_add(CTX_wm_screen(C));
982
983         memset(&type, 0, sizeof(ARegionType));
984         type.draw = ui_tooltip_region_draw_cb;
985         type.free = ui_tooltip_region_free_cb;
986         type.regionid = RGN_TYPE_TEMPORARY;
987         ar->type = &type;
988
989         /* set font, get bb */
990         data->fstyle = style->widget; /* copy struct */
991         ui_fontscale(&data->fstyle.points, aspect);
992
993         UI_fontstyle_set(&data->fstyle);
994
995         data->wrap_width = min_ii(UI_TIP_MAXWIDTH * U.pixelsize / aspect, winx - (UI_TIP_PADDING * 2));
996
997         font_flag |= BLF_WORD_WRAP;
998         if (data->fstyle.kerning == 1) {
999                 font_flag |= BLF_KERNING_DEFAULT;
1000         }
1001         BLF_enable(data->fstyle.uifont_id, font_flag);
1002         BLF_enable(blf_mono_font, font_flag);
1003         BLF_wordwrap(data->fstyle.uifont_id, data->wrap_width);
1004         BLF_wordwrap(blf_mono_font, data->wrap_width);
1005
1006         /* these defines tweaked depending on font */
1007 #define TIP_BORDER_X (16.0f / aspect)
1008 #define TIP_BORDER_Y (6.0f / aspect)
1009
1010         h = BLF_height_max(data->fstyle.uifont_id);
1011
1012         for (i = 0, fontw = 0, fonth = 0; i < data->fields_len; i++) {
1013                 uiTooltipField *field = &data->fields[i];
1014                 uiTooltipField *field_next = (i + 1) != data->fields_len ? &data->fields[i + 1] : NULL;
1015
1016                 struct ResultBLF info;
1017                 int w, x_pos = 0;
1018                 int font_id;
1019
1020                 if (field->format.style == UI_TIP_STYLE_MONO) {
1021                         BLF_size(blf_mono_font, data->fstyle.points * U.pixelsize, U.dpi);
1022                         font_id = blf_mono_font;
1023                 }
1024                 else {
1025                         BLI_assert(ELEM(field->format.style, UI_TIP_STYLE_NORMAL, UI_TIP_STYLE_HEADER));
1026                         font_id = data->fstyle.uifont_id;
1027                 }
1028                 w = BLF_width_ex(font_id, field->text, BLF_DRAW_STR_DUMMY_MAX, &info);
1029
1030                 /* check for suffix (enum label) */
1031                 if (field->text_suffix && field->text_suffix[0]) {
1032                         x_pos = info.width;
1033                         w = max_ii(w, x_pos + BLF_width(font_id, field->text_suffix, BLF_DRAW_STR_DUMMY_MAX));
1034                 }
1035                 fontw = max_ii(fontw, w);
1036
1037                 fonth += h * info.lines;
1038                 if (field_next && field_next->format.is_pad) {
1039                         fonth += h * (UI_TIP_PAD_FAC - 1);
1040                 }
1041
1042                 field->geom.lines = info.lines;
1043                 field->geom.x_pos = x_pos;
1044         }
1045
1046         //fontw *= aspect;
1047
1048         BLF_disable(data->fstyle.uifont_id, font_flag);
1049         BLF_disable(blf_mono_font, font_flag);
1050
1051         ar->regiondata = data;
1052
1053         data->toth = fonth;
1054         data->lineh = h;
1055
1056         /* Compute position. */
1057         {
1058                 rctf rect_fl;
1059                 rect_fl.xmin = init_position[0] - TIP_BORDER_X;
1060                 rect_fl.xmax = rect_fl.xmin + fontw + pad_px;
1061                 rect_fl.ymax = init_position[1] - TIP_BORDER_Y;
1062                 rect_fl.ymin = rect_fl.ymax - fonth - TIP_BORDER_Y;
1063                 BLI_rcti_rctf_copy(&rect_i, &rect_fl);
1064         }
1065
1066 #undef TIP_BORDER_X
1067 #undef TIP_BORDER_Y
1068
1069         /* Clamp to window bounds. */
1070         {
1071                 /* Ensure at least 5 px above screen bounds
1072                  * UI_UNIT_Y is just a guess to be above the menu item */
1073                 const int pad = max_ff(1.0f, U.pixelsize) * 5;
1074                 const rcti rect_clamp = {
1075                         .xmin = pad,
1076                         .xmax = winx - pad,
1077                         .ymin = pad + (UI_UNIT_Y * 2),
1078                         .ymax = winy - pad,
1079                 };
1080                 int offset_dummy[2];
1081                 BLI_rcti_clamp(&rect_i, &rect_clamp, offset_dummy);
1082         }
1083
1084         /* add padding */
1085         BLI_rcti_resize(&rect_i,
1086                         BLI_rcti_size_x(&rect_i) + pad_px,
1087                         BLI_rcti_size_y(&rect_i) + pad_px);
1088
1089         /* widget rect, in region coords */
1090         {
1091                 const int margin = UI_POPUP_MARGIN;
1092
1093                 data->bbox.xmin = margin;
1094                 data->bbox.xmax = BLI_rcti_size_x(&rect_i) - margin;
1095                 data->bbox.ymin = margin;
1096                 data->bbox.ymax = BLI_rcti_size_y(&rect_i);
1097
1098                 /* region bigger for shadow */
1099                 ar->winrct.xmin = rect_i.xmin - margin;
1100                 ar->winrct.xmax = rect_i.xmax + margin;
1101                 ar->winrct.ymin = rect_i.ymin - margin;
1102                 ar->winrct.ymax = rect_i.ymax + margin;
1103         }
1104
1105         /* adds subwindow */
1106         ED_region_init(ar);
1107
1108         /* notify change and redraw */
1109         ED_region_tag_redraw(ar);
1110
1111         return ar;
1112 }
1113
1114 /** \} */
1115
1116 /* -------------------------------------------------------------------- */
1117 /** \name ToolTip Public API
1118  * \{ */
1119
1120
1121 ARegion *UI_tooltip_create_from_button(bContext *C, ARegion *butregion, uiBut *but)
1122 {
1123         wmWindow *win = CTX_wm_window(C);
1124         /* aspect values that shrink text are likely unreadable */
1125         const float aspect = min_ff(1.0f, but->block->aspect);
1126         float init_position[2];
1127
1128         if (but->drawflag & UI_BUT_NO_TOOLTIP) {
1129                 return NULL;
1130         }
1131         uiTooltipData *data = NULL;
1132
1133         if (data == NULL) {
1134                 data = ui_tooltip_data_from_tool(C, but);
1135         }
1136
1137         if (data == NULL) {
1138                 data = ui_tooltip_data_from_button(C, but);
1139         }
1140
1141         if (data == NULL) {
1142                 return NULL;
1143         }
1144
1145         init_position[0] = BLI_rctf_cent_x(&but->rect);
1146         init_position[1] = but->rect.ymin;
1147
1148         if (butregion) {
1149                 ui_block_to_window_fl(butregion, but->block, &init_position[0], &init_position[1]);
1150                 init_position[0] = win->eventstate->x;
1151         }
1152
1153         return ui_tooltip_create_with_data(C, data, init_position, aspect);
1154 }
1155
1156 ARegion *UI_tooltip_create_from_gizmo(bContext *C, wmGizmo *gz)
1157 {
1158         wmWindow *win = CTX_wm_window(C);
1159         const float aspect = 1.0f;
1160         float init_position[2];
1161
1162         uiTooltipData *data = ui_tooltip_data_from_gizmo(C, gz);
1163         if (data == NULL) {
1164                 return NULL;
1165         }
1166
1167         init_position[0] = win->eventstate->x;
1168         init_position[1] = win->eventstate->y;
1169
1170         return ui_tooltip_create_with_data(C, data, init_position, aspect);
1171 }
1172
1173 void UI_tooltip_free(bContext *C, bScreen *sc, ARegion *ar)
1174 {
1175         ui_region_temp_remove(C, sc, ar);
1176 }
1177
1178 /** \} */