UI: keep showing label tooltip once it's open
[blender.git] / source / blender / windowmanager / intern / wm_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  * ***** END GPL LICENSE BLOCK *****
19  */
20
21 /** \file blender/windowmanager/intern/wm_tooltip.c
22  *  \ingroup wm
23  *
24  * Manages a per-window tool-tip.
25  */
26
27 #include "MEM_guardedalloc.h"
28
29 #include "BLI_utildefines.h"
30
31 #include "BKE_context.h"
32
33 #include "ED_screen.h"
34
35 #include "UI_interface.h"
36
37 #include "WM_api.h"
38 #include "WM_types.h"
39
40 #include "PIL_time.h"
41
42 static double g_tooltip_time_closed;
43 double WM_tooltip_time_closed(void)
44 {
45         return g_tooltip_time_closed;
46 }
47
48 void WM_tooltip_immediate_init(
49         bContext *C, wmWindow *win, ARegion *ar,
50         wmTooltipInitFn init)
51 {
52         WM_tooltip_timer_clear(C, win);
53
54         bScreen *screen = WM_window_get_active_screen(win);
55         if (screen->tool_tip == NULL) {
56                 screen->tool_tip = MEM_callocN(sizeof(*screen->tool_tip), __func__);
57         }
58         screen->tool_tip->region_from = ar;
59         screen->tool_tip->init = init;
60         WM_tooltip_init(C, win);
61 }
62
63 void WM_tooltip_timer_init_ex(
64         bContext *C, wmWindow *win, ARegion *ar,
65         wmTooltipInitFn init, double delay)
66 {
67         WM_tooltip_timer_clear(C, win);
68
69         bScreen *screen = WM_window_get_active_screen(win);
70         wmWindowManager *wm = CTX_wm_manager(C);
71         if (screen->tool_tip == NULL) {
72                 screen->tool_tip = MEM_callocN(sizeof(*screen->tool_tip), __func__);
73         }
74         screen->tool_tip->region_from = ar;
75         screen->tool_tip->timer = WM_event_add_timer(wm, win, TIMER, delay);
76         screen->tool_tip->init = init;
77 }
78
79 void WM_tooltip_timer_init(
80         bContext *C, wmWindow *win, ARegion *ar,
81         wmTooltipInitFn init)
82 {
83         WM_tooltip_timer_init_ex(C, win, ar, init, UI_TOOLTIP_DELAY);
84 }
85
86 void WM_tooltip_timer_clear(bContext *C, wmWindow *win)
87 {
88         wmWindowManager *wm = CTX_wm_manager(C);
89         bScreen *screen = WM_window_get_active_screen(win);
90         if (screen->tool_tip != NULL) {
91                 if (screen->tool_tip->timer != NULL) {
92                         WM_event_remove_timer(wm, win, screen->tool_tip->timer);
93                         screen->tool_tip->timer = NULL;
94                 }
95         }
96 }
97
98 void WM_tooltip_clear(bContext *C, wmWindow *win)
99 {
100         WM_tooltip_timer_clear(C, win);
101         bScreen *screen = WM_window_get_active_screen(win);
102         if (screen->tool_tip != NULL) {
103                 if (screen->tool_tip->region) {
104                         UI_tooltip_free(C, screen, screen->tool_tip->region);
105                         screen->tool_tip->region = NULL;
106                         g_tooltip_time_closed = PIL_check_seconds_timer();
107                 }
108                 MEM_freeN(screen->tool_tip);
109                 screen->tool_tip = NULL;
110         }
111 }
112
113 void WM_tooltip_init(bContext *C, wmWindow *win)
114 {
115         WM_tooltip_timer_clear(C, win);
116         bScreen *screen = WM_window_get_active_screen(win);
117         if (screen->tool_tip->region) {
118                 UI_tooltip_free(C, screen, screen->tool_tip->region);
119                 screen->tool_tip->region = NULL;
120         }
121         const int pass_prev = screen->tool_tip->pass;
122         double pass_delay = 0.0;
123         screen->tool_tip->region = screen->tool_tip->init(
124                 C, screen->tool_tip->region_from,
125                 &screen->tool_tip->pass, &pass_delay, &screen->tool_tip->exit_on_event);
126         if (pass_prev != screen->tool_tip->pass) {
127                 /* The pass changed, add timer for next pass. */
128                 wmWindowManager *wm = CTX_wm_manager(C);
129                 screen->tool_tip->timer = WM_event_add_timer(wm, win, TIMER, pass_delay);
130         }
131         if (screen->tool_tip->region == NULL) {
132                 WM_tooltip_clear(C, win);
133         }
134 }
135
136 void WM_tooltip_refresh(bContext *C, wmWindow *win)
137 {
138         WM_tooltip_timer_clear(C, win);
139         bScreen *screen = WM_window_get_active_screen(win);
140         if (screen->tool_tip != NULL) {
141                 if (screen->tool_tip->region) {
142                         UI_tooltip_free(C, screen, screen->tool_tip->region);
143                         screen->tool_tip->region = NULL;
144                 }
145                 WM_tooltip_init(C, win);
146         }
147 }