-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtbo-tooltip.c
163 lines (137 loc) · 4.12 KB
/
tbo-tooltip.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
* This file is part of TBO, a gnome comic editor
* Copyright (C) 2010 Daniel Garcia Moreno <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <string.h>
#include <math.h>
#include <gtk/gtk.h>
#include <glib.h>
#include <cairo.h>
#include "tbo-drawing.h"
#include "tbo-tooltip.h"
#include "tbo-window.h"
#define TOOLTIP_ALPHA 0.7
static GString *TOOLTIP = NULL;
static int X=0, Y=0;
static double ALPHA = 0.0;
void
cairo_rounded_rectangle (cairo_t *cr, int xx, int yy, int w, int h)
{
double x = xx,
y = yy,
width = w,
height = h,
aspect = 1.0, /* aspect ratio */
corner_radius = height / 10.0; /* and corner curvature radius */
double radius = corner_radius / aspect;
double degrees = M_PI / 180.0;
cairo_new_sub_path (cr);
cairo_arc (cr, x + width - radius, y + radius, radius, -90 * degrees, 0 * degrees);
cairo_arc (cr, x + width - radius, y + height - radius, radius, 0 * degrees, 90 * degrees);
cairo_arc (cr, x + radius, y + height - radius, radius, 90 * degrees, 180 * degrees);
cairo_arc (cr, x + radius, y + radius, radius, 180 * degrees, 270 * degrees);
cairo_close_path (cr);
}
gboolean
quit_tooltip_cb (gpointer p)
{
tbo_tooltip_set (NULL, 0, 0, (TboWindow*) p);
return FALSE;
}
void
tbo_tooltip_draw_background (cairo_t *cr, int w, int h)
{
int margin = 5;
cairo_set_source_rgba (cr, 0, 0, 0, ALPHA);
cairo_rounded_rectangle (cr, -margin, -margin, w + margin * 2, h + margin * 2);
cairo_fill (cr);
}
void
tbo_tooltip_set (const char *tooltip, int x, int y, TboWindow *tbo)
{
if (!TOOLTIP)
{
if (tooltip)
{
TOOLTIP = g_string_new (tooltip);
ALPHA = TOOLTIP_ALPHA;
X = x;
Y = y;
}
}
else
{
// if it's the same passing
if (x == X && y == Y && !strcmp (tooltip, TOOLTIP->str))
return;
// removing tooltip if tooltip == NULL
if (!tooltip && TOOLTIP)
{
ALPHA = 0.0;
g_string_free (TOOLTIP, TRUE);
TOOLTIP = NULL;
}
else
{
g_string_free (TOOLTIP, TRUE);
TOOLTIP = g_string_new (tooltip);
ALPHA = TOOLTIP_ALPHA;
X = x;
Y = y;
}
}
tbo_drawing_update (TBO_DRAWING (tbo->drawing));
}
GString *
tbo_tooltip_get ()
{
return TOOLTIP;
}
void
tbo_tooltip_draw (cairo_t *cr)
{
if (!TOOLTIP)
return;
int w=0, h=0;
int posx, posy;
gchar *text = TOOLTIP->str;
PangoLayout *layout;
layout = pango_cairo_create_layout (cr);
pango_layout_set_text (layout, text, -1);
pango_layout_get_size (layout, &w, &h);
w = (double)w / PANGO_SCALE;
h = (double)h / PANGO_SCALE;
//pango_layout_set_font_description (layout, desc);
pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER);
posx = X - w / 2;
posy = Y - h / 2;
cairo_translate (cr, posx, posy);
tbo_tooltip_draw_background (cr, w, h);
cairo_set_source_rgba (cr, 1, 1, 1, ALPHA);
pango_cairo_show_layout (cr, layout);
cairo_translate (cr, -posx, -posy);
}
void
tbo_tooltip_set_center_timeout (const char *tooltip, int timeout, TboWindow *tbo)
{
int x, y;
GtkAllocation alloc;
gtk_widget_get_allocation (tbo->drawing, &alloc);
x = alloc.width / 2;
y = alloc.height / 2;
tbo_tooltip_set (tooltip, x, y, tbo);
g_timeout_add (timeout, quit_tooltip_cb, tbo);
}