2009-09-15 07:37:37 +00:00
|
|
|
/**************************************************************************
|
|
|
|
*
|
2009-09-16 11:00:23 +00:00
|
|
|
* Copyright (C) 2009 Andreas.Fink (Andreas.Fink85@gmail.com)
|
2009-09-15 07:37:37 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License version 2
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* 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, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
**************************************************************************/
|
|
|
|
|
2009-09-14 21:28:17 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
2009-11-19 13:53:01 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
2009-09-14 21:28:17 +00:00
|
|
|
#include <cairo.h>
|
|
|
|
#include <cairo-xlib.h>
|
|
|
|
|
|
|
|
#include "server.h"
|
|
|
|
#include "tooltip.h"
|
|
|
|
#include "panel.h"
|
2009-11-16 07:27:28 +00:00
|
|
|
#include "timer.h"
|
2009-09-14 21:28:17 +00:00
|
|
|
|
2010-08-19 20:33:42 +00:00
|
|
|
static int x, y, width, height;
|
2015-04-11 11:46:03 +00:00
|
|
|
static int just_shown;
|
2009-09-14 21:28:17 +00:00
|
|
|
|
2009-11-16 07:27:28 +00:00
|
|
|
// the next functions are helper functions for tooltip handling
|
|
|
|
void start_show_timeout();
|
|
|
|
void start_hide_timeout();
|
2009-12-27 20:33:02 +00:00
|
|
|
void stop_tooltip_timeout();
|
2009-11-16 07:27:28 +00:00
|
|
|
|
2015-12-12 14:13:32 +00:00
|
|
|
void tooltip_init_fonts();
|
|
|
|
|
2010-04-18 12:07:36 +00:00
|
|
|
Tooltip g_tooltip;
|
|
|
|
|
|
|
|
void default_tooltip()
|
|
|
|
{
|
|
|
|
// give the tooltip some reasonable default values
|
|
|
|
memset(&g_tooltip, 0, sizeof(Tooltip));
|
|
|
|
|
2015-11-20 22:28:37 +00:00
|
|
|
g_tooltip.font_color.rgb[0] = 1;
|
|
|
|
g_tooltip.font_color.rgb[1] = 1;
|
|
|
|
g_tooltip.font_color.rgb[2] = 1;
|
2010-04-18 12:07:36 +00:00
|
|
|
g_tooltip.font_color.alpha = 1;
|
2015-04-11 11:46:03 +00:00
|
|
|
just_shown = 0;
|
2010-04-18 12:07:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void cleanup_tooltip()
|
|
|
|
{
|
|
|
|
stop_tooltip_timeout();
|
2015-04-11 09:51:10 +00:00
|
|
|
tooltip_hide(NULL);
|
|
|
|
tooltip_copy_text(NULL);
|
|
|
|
if (g_tooltip.window)
|
2016-01-01 12:49:10 +00:00
|
|
|
XDestroyWindow(server.display, g_tooltip.window);
|
2015-04-11 09:51:10 +00:00
|
|
|
g_tooltip.window = 0;
|
|
|
|
pango_font_description_free(g_tooltip.font_desc);
|
|
|
|
g_tooltip.font_desc = NULL;
|
2010-04-18 12:07:36 +00:00
|
|
|
}
|
|
|
|
|
2009-09-14 21:28:17 +00:00
|
|
|
void init_tooltip()
|
|
|
|
{
|
2015-11-20 22:28:37 +00:00
|
|
|
if (!g_tooltip.bg)
|
2011-07-22 10:02:07 +00:00
|
|
|
g_tooltip.bg = &g_array_index(backgrounds, Background, 0);
|
2015-12-12 14:13:32 +00:00
|
|
|
tooltip_init_fonts();
|
2009-09-14 21:28:17 +00:00
|
|
|
|
|
|
|
XSetWindowAttributes attr;
|
|
|
|
attr.override_redirect = True;
|
2009-11-16 10:06:45 +00:00
|
|
|
attr.event_mask = StructureNotifyMask;
|
2009-12-30 11:27:29 +00:00
|
|
|
attr.colormap = server.colormap;
|
|
|
|
attr.background_pixel = 0;
|
|
|
|
attr.border_pixel = 0;
|
2015-11-20 22:28:37 +00:00
|
|
|
unsigned long mask = CWEventMask | CWColormap | CWBorderPixel | CWBackPixel | CWOverrideRedirect;
|
2015-04-11 09:51:10 +00:00
|
|
|
if (g_tooltip.window)
|
2016-01-01 12:49:10 +00:00
|
|
|
XDestroyWindow(server.display, g_tooltip.window);
|
|
|
|
g_tooltip.window = XCreateWindow(server.display,
|
2015-12-12 14:13:32 +00:00
|
|
|
server.root_win,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
100,
|
|
|
|
20,
|
|
|
|
0,
|
|
|
|
server.depth,
|
|
|
|
InputOutput,
|
|
|
|
server.visual,
|
|
|
|
mask,
|
|
|
|
&attr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void tooltip_init_fonts()
|
|
|
|
{
|
|
|
|
if (!g_tooltip.font_desc)
|
|
|
|
g_tooltip.font_desc = pango_font_description_from_string(get_default_font());
|
2009-09-14 21:28:17 +00:00
|
|
|
}
|
|
|
|
|
2015-12-12 14:13:32 +00:00
|
|
|
void tooltip_default_font_changed()
|
|
|
|
{
|
|
|
|
if (g_tooltip.has_font)
|
|
|
|
return;
|
|
|
|
if (!g_tooltip.has_font) {
|
|
|
|
pango_font_description_free(g_tooltip.font_desc);
|
|
|
|
g_tooltip.font_desc = NULL;
|
|
|
|
}
|
|
|
|
tooltip_init_fonts();
|
|
|
|
tooltip_update();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-11-20 22:28:37 +00:00
|
|
|
void tooltip_trigger_show(Area *area, Panel *p, XEvent *e)
|
2009-09-14 21:28:17 +00:00
|
|
|
{
|
2010-08-07 12:17:56 +00:00
|
|
|
// Position the tooltip in the center of the area
|
2015-04-17 20:17:25 +00:00
|
|
|
x = area->posx + MIN(area->width / 3, 22) + e->xmotion.x_root - e->xmotion.x;
|
2010-08-19 20:33:42 +00:00
|
|
|
y = area->posy + area->height / 2 + e->xmotion.y_root - e->xmotion.y;
|
2015-04-11 11:46:03 +00:00
|
|
|
just_shown = 1;
|
2009-11-16 17:17:53 +00:00
|
|
|
g_tooltip.panel = p;
|
|
|
|
if (g_tooltip.mapped && g_tooltip.area != area) {
|
2009-11-19 13:53:01 +00:00
|
|
|
tooltip_copy_text(area);
|
2009-09-14 21:28:17 +00:00
|
|
|
tooltip_update();
|
2009-12-27 20:33:02 +00:00
|
|
|
stop_tooltip_timeout();
|
2015-11-20 22:28:37 +00:00
|
|
|
} else if (!g_tooltip.mapped) {
|
2009-11-16 07:27:28 +00:00
|
|
|
start_show_timeout();
|
2009-09-14 21:28:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-20 22:28:37 +00:00
|
|
|
void tooltip_show(void *arg)
|
2009-09-14 21:28:17 +00:00
|
|
|
{
|
2010-08-07 12:17:56 +00:00
|
|
|
int mx, my;
|
|
|
|
Window w;
|
2016-01-01 12:49:10 +00:00
|
|
|
XTranslateCoordinates(server.display, server.root_win, g_tooltip.panel->main_win, x, y, &mx, &my, &w);
|
2015-11-20 22:28:37 +00:00
|
|
|
Area *area;
|
2011-06-08 16:57:32 +00:00
|
|
|
area = click_area(g_tooltip.panel, mx, my);
|
2009-11-16 17:17:53 +00:00
|
|
|
if (!g_tooltip.mapped && area->_get_tooltip_text) {
|
2009-11-19 13:53:01 +00:00
|
|
|
tooltip_copy_text(area);
|
2009-09-14 21:28:17 +00:00
|
|
|
g_tooltip.mapped = True;
|
2016-01-01 12:49:10 +00:00
|
|
|
XMapWindow(server.display, g_tooltip.window);
|
2010-01-14 21:05:42 +00:00
|
|
|
tooltip_update();
|
2016-01-01 12:49:10 +00:00
|
|
|
XFlush(server.display);
|
2009-09-14 21:28:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void tooltip_update_geometry()
|
|
|
|
{
|
|
|
|
cairo_surface_t *cs;
|
|
|
|
cairo_t *c;
|
2015-11-20 22:28:37 +00:00
|
|
|
PangoLayout *layout;
|
2016-01-01 12:49:10 +00:00
|
|
|
cs = cairo_xlib_surface_create(server.display, g_tooltip.window, server.visual, width, height);
|
2009-09-14 21:28:17 +00:00
|
|
|
c = cairo_create(cs);
|
|
|
|
layout = pango_cairo_create_layout(c);
|
|
|
|
pango_layout_set_font_description(layout, g_tooltip.font_desc);
|
2009-11-19 13:53:01 +00:00
|
|
|
pango_layout_set_text(layout, g_tooltip.tooltip_text, -1);
|
2009-09-14 21:28:17 +00:00
|
|
|
PangoRectangle r1, r2;
|
|
|
|
pango_layout_get_pixel_extents(layout, &r1, &r2);
|
2015-11-20 22:28:37 +00:00
|
|
|
width = 2 * g_tooltip.bg->border.width + 2 * g_tooltip.paddingx + r2.width;
|
|
|
|
height = 2 * g_tooltip.bg->border.width + 2 * g_tooltip.paddingy + r2.height;
|
2009-09-14 21:28:17 +00:00
|
|
|
|
2015-11-20 22:28:37 +00:00
|
|
|
Panel *panel = g_tooltip.panel;
|
2009-09-14 21:28:17 +00:00
|
|
|
if (panel_horizontal && panel_position & BOTTOM)
|
2015-11-20 22:28:37 +00:00
|
|
|
y = panel->posy - height;
|
2009-09-14 21:28:17 +00:00
|
|
|
else if (panel_horizontal && panel_position & TOP)
|
|
|
|
y = panel->posy + panel->area.height;
|
|
|
|
else if (panel_position & LEFT)
|
|
|
|
x = panel->posx + panel->area.width;
|
|
|
|
else
|
2009-10-31 16:33:35 +00:00
|
|
|
x = panel->posx - width;
|
2009-10-31 17:56:59 +00:00
|
|
|
|
2009-09-14 21:28:17 +00:00
|
|
|
g_object_unref(layout);
|
|
|
|
cairo_destroy(c);
|
|
|
|
cairo_surface_destroy(cs);
|
|
|
|
}
|
|
|
|
|
|
|
|
void tooltip_adjust_geometry()
|
|
|
|
{
|
|
|
|
// adjust coordinates and size to not go offscreen
|
|
|
|
// it seems quite impossible that the height needs to be adjusted, but we do it anyway.
|
|
|
|
|
|
|
|
int min_x, min_y, max_width, max_height;
|
2015-11-20 22:28:37 +00:00
|
|
|
Panel *panel = g_tooltip.panel;
|
2015-12-22 22:16:01 +00:00
|
|
|
int screen_width = server.monitors[panel->monitor].x + server.monitors[panel->monitor].width;
|
|
|
|
int screen_height = server.monitors[panel->monitor].y + server.monitors[panel->monitor].height;
|
|
|
|
if (x + width <= screen_width && y + height <= screen_height && x >= server.monitors[panel->monitor].x &&
|
|
|
|
y >= server.monitors[panel->monitor].y)
|
2015-11-20 22:28:37 +00:00
|
|
|
return; // no adjustment needed
|
2009-09-14 21:28:17 +00:00
|
|
|
|
|
|
|
if (panel_horizontal) {
|
2015-11-20 22:28:37 +00:00
|
|
|
min_x = 0;
|
2015-12-22 22:16:01 +00:00
|
|
|
max_width = server.monitors[panel->monitor].width;
|
|
|
|
max_height = server.monitors[panel->monitor].height - panel->area.height;
|
2009-09-14 21:28:17 +00:00
|
|
|
if (panel_position & BOTTOM)
|
2015-11-20 22:28:37 +00:00
|
|
|
min_y = 0;
|
2009-09-14 21:28:17 +00:00
|
|
|
else
|
2015-11-20 22:28:37 +00:00
|
|
|
min_y = panel->area.height;
|
|
|
|
} else {
|
2015-12-22 22:16:01 +00:00
|
|
|
max_width = server.monitors[panel->monitor].width - panel->area.width;
|
2015-11-20 22:28:37 +00:00
|
|
|
min_y = 0;
|
2015-12-22 22:16:01 +00:00
|
|
|
max_height = server.monitors[panel->monitor].height;
|
2009-09-14 21:28:17 +00:00
|
|
|
if (panel_position & LEFT)
|
2015-11-20 22:28:37 +00:00
|
|
|
min_x = panel->area.width;
|
2009-09-14 21:28:17 +00:00
|
|
|
else
|
2015-11-20 22:28:37 +00:00
|
|
|
min_x = 0;
|
2009-09-14 21:28:17 +00:00
|
|
|
}
|
|
|
|
|
2015-12-22 22:16:01 +00:00
|
|
|
if (x + width > server.monitors[panel->monitor].x + server.monitors[panel->monitor].width)
|
|
|
|
x = server.monitors[panel->monitor].x + server.monitors[panel->monitor].width - width;
|
|
|
|
if (y + height > server.monitors[panel->monitor].y + server.monitors[panel->monitor].height)
|
|
|
|
y = server.monitors[panel->monitor].y + server.monitors[panel->monitor].height - height;
|
2009-09-14 21:28:17 +00:00
|
|
|
|
2015-11-20 22:28:37 +00:00
|
|
|
if (x < min_x)
|
|
|
|
x = min_x;
|
|
|
|
if (width > max_width)
|
2009-09-14 21:28:17 +00:00
|
|
|
width = max_width;
|
2015-11-20 22:28:37 +00:00
|
|
|
if (y < min_y)
|
|
|
|
y = min_y;
|
|
|
|
if (height > max_height)
|
|
|
|
height = max_height;
|
2009-09-14 21:28:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void tooltip_update()
|
|
|
|
{
|
2009-11-19 13:53:01 +00:00
|
|
|
if (!g_tooltip.tooltip_text) {
|
2010-01-05 20:38:49 +00:00
|
|
|
tooltip_hide(0);
|
2009-09-14 21:28:17 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
tooltip_update_geometry();
|
2015-04-11 11:46:03 +00:00
|
|
|
if (just_shown) {
|
|
|
|
if (!panel_horizontal)
|
2015-11-20 22:28:37 +00:00
|
|
|
y -= height / 2; // center vertically
|
2015-04-11 11:46:03 +00:00
|
|
|
just_shown = 0;
|
|
|
|
}
|
2009-09-14 21:28:17 +00:00
|
|
|
tooltip_adjust_geometry();
|
2016-01-01 12:49:10 +00:00
|
|
|
XMoveResizeWindow(server.display, g_tooltip.window, x, y, width, height);
|
2009-09-14 21:28:17 +00:00
|
|
|
|
|
|
|
// Stuff for drawing the tooltip
|
|
|
|
cairo_surface_t *cs;
|
|
|
|
cairo_t *c;
|
2015-11-20 22:28:37 +00:00
|
|
|
PangoLayout *layout;
|
2016-01-01 12:49:10 +00:00
|
|
|
cs = cairo_xlib_surface_create(server.display, g_tooltip.window, server.visual, width, height);
|
2009-09-14 21:28:17 +00:00
|
|
|
c = cairo_create(cs);
|
2015-11-20 22:28:37 +00:00
|
|
|
Color bc = g_tooltip.bg->fill_color;
|
2010-01-09 00:11:01 +00:00
|
|
|
Border b = g_tooltip.bg->border;
|
2010-04-18 12:07:36 +00:00
|
|
|
if (server.real_transparency) {
|
2009-12-31 14:20:32 +00:00
|
|
|
clear_pixmap(g_tooltip.window, 0, 0, width, height);
|
2015-11-20 22:28:37 +00:00
|
|
|
draw_rect(c, b.width, b.width, width - 2 * b.width, height - 2 * b.width, b.radius - b.width / 1.571);
|
|
|
|
cairo_set_source_rgba(c, bc.rgb[0], bc.rgb[1], bc.rgb[2], bc.alpha);
|
|
|
|
} else {
|
2009-12-30 23:27:31 +00:00
|
|
|
cairo_rectangle(c, 0., 0, width, height);
|
2015-11-20 22:28:37 +00:00
|
|
|
cairo_set_source_rgb(c, bc.rgb[0], bc.rgb[1], bc.rgb[2]);
|
2009-12-30 23:27:31 +00:00
|
|
|
}
|
|
|
|
cairo_fill(c);
|
2009-09-14 21:28:17 +00:00
|
|
|
cairo_set_line_width(c, b.width);
|
2010-04-18 12:07:36 +00:00
|
|
|
if (server.real_transparency)
|
2015-11-20 22:28:37 +00:00
|
|
|
draw_rect(c, b.width / 2.0, b.width / 2.0, width - b.width, height - b.width, b.radius);
|
2010-04-18 12:07:36 +00:00
|
|
|
else
|
2015-11-20 22:28:37 +00:00
|
|
|
cairo_rectangle(c, b.width / 2.0, b.width / 2.0, width - b.width, height - b.width);
|
|
|
|
cairo_set_source_rgba(c, b.color.rgb[0], b.color.rgb[1], b.color.rgb[2], b.color.alpha);
|
2009-09-14 21:28:17 +00:00
|
|
|
cairo_stroke(c);
|
|
|
|
|
2010-01-09 00:11:01 +00:00
|
|
|
Color fc = g_tooltip.font_color;
|
2015-11-20 22:28:37 +00:00
|
|
|
cairo_set_source_rgba(c, fc.rgb[0], fc.rgb[1], fc.rgb[2], fc.alpha);
|
2009-09-14 21:28:17 +00:00
|
|
|
layout = pango_cairo_create_layout(c);
|
|
|
|
pango_layout_set_font_description(layout, g_tooltip.font_desc);
|
2009-11-19 13:53:01 +00:00
|
|
|
pango_layout_set_text(layout, g_tooltip.tooltip_text, -1);
|
2009-09-14 21:28:17 +00:00
|
|
|
PangoRectangle r1, r2;
|
|
|
|
pango_layout_get_pixel_extents(layout, &r1, &r2);
|
2015-11-20 22:28:37 +00:00
|
|
|
pango_layout_set_width(layout, width * PANGO_SCALE);
|
|
|
|
pango_layout_set_height(layout, height * PANGO_SCALE);
|
2010-04-19 22:06:23 +00:00
|
|
|
pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_END);
|
2015-11-20 22:28:37 +00:00
|
|
|
// I do not know why this is the right way, but with the below cairo_move_to it seems to be centered (horiz. and
|
|
|
|
// vert.)
|
2015-01-21 00:01:40 +00:00
|
|
|
cairo_move_to(c,
|
2015-11-20 22:28:37 +00:00
|
|
|
-r1.x / 2 + g_tooltip.bg->border.width + g_tooltip.paddingx,
|
|
|
|
-r1.y / 2 + 1 + g_tooltip.bg->border.width + g_tooltip.paddingy);
|
|
|
|
pango_cairo_show_layout(c, layout);
|
2009-09-14 21:28:17 +00:00
|
|
|
|
2015-11-20 22:28:37 +00:00
|
|
|
g_object_unref(layout);
|
|
|
|
cairo_destroy(c);
|
|
|
|
cairo_surface_destroy(cs);
|
2009-09-14 21:28:17 +00:00
|
|
|
}
|
|
|
|
|
2015-08-03 16:20:16 +00:00
|
|
|
void tooltip_trigger_hide()
|
2009-09-14 21:28:17 +00:00
|
|
|
{
|
|
|
|
if (g_tooltip.mapped) {
|
2009-11-19 13:53:01 +00:00
|
|
|
tooltip_copy_text(0);
|
2009-11-16 07:27:28 +00:00
|
|
|
start_hide_timeout();
|
2015-11-20 22:28:37 +00:00
|
|
|
} else {
|
2009-11-16 07:27:28 +00:00
|
|
|
// tooltip not visible yet, but maybe a timeout is still pending
|
2009-12-27 20:33:02 +00:00
|
|
|
stop_tooltip_timeout();
|
2009-09-14 21:28:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-20 22:28:37 +00:00
|
|
|
void tooltip_hide(void *arg)
|
2009-09-14 21:28:17 +00:00
|
|
|
{
|
|
|
|
if (g_tooltip.mapped) {
|
|
|
|
g_tooltip.mapped = False;
|
2016-01-01 12:49:10 +00:00
|
|
|
XUnmapWindow(server.display, g_tooltip.window);
|
|
|
|
XFlush(server.display);
|
2009-09-14 21:28:17 +00:00
|
|
|
}
|
|
|
|
}
|
2009-11-16 07:27:28 +00:00
|
|
|
|
|
|
|
void start_show_timeout()
|
|
|
|
{
|
2015-04-17 20:17:25 +00:00
|
|
|
change_timeout(&g_tooltip.timeout, g_tooltip.show_timeout_msec, 0, tooltip_show, 0);
|
2009-11-16 07:27:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void start_hide_timeout()
|
|
|
|
{
|
2015-04-17 20:17:25 +00:00
|
|
|
change_timeout(&g_tooltip.timeout, g_tooltip.hide_timeout_msec, 0, tooltip_hide, 0);
|
2009-11-16 07:27:28 +00:00
|
|
|
}
|
|
|
|
|
2009-12-27 20:33:02 +00:00
|
|
|
void stop_tooltip_timeout()
|
2009-11-16 07:27:28 +00:00
|
|
|
{
|
2015-04-11 09:51:10 +00:00
|
|
|
stop_timeout(g_tooltip.timeout);
|
2009-11-16 07:27:28 +00:00
|
|
|
}
|
2009-11-19 13:53:01 +00:00
|
|
|
|
2015-11-20 22:28:37 +00:00
|
|
|
void tooltip_copy_text(Area *area)
|
2009-11-19 13:53:01 +00:00
|
|
|
{
|
|
|
|
free(g_tooltip.tooltip_text);
|
|
|
|
if (area && area->_get_tooltip_text)
|
2015-08-07 00:03:06 +00:00
|
|
|
g_tooltip.tooltip_text = area->_get_tooltip_text(area);
|
2009-11-19 13:53:01 +00:00
|
|
|
else
|
2015-04-11 09:51:10 +00:00
|
|
|
g_tooltip.tooltip_text = NULL;
|
2009-11-19 13:53:01 +00:00
|
|
|
g_tooltip.area = area;
|
|
|
|
}
|