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;
|
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
|
|
|
|
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));
|
|
|
|
|
|
|
|
g_tooltip.font_color.color[0] = 1;
|
|
|
|
g_tooltip.font_color.color[1] = 1;
|
|
|
|
g_tooltip.font_color.color[2] = 1;
|
|
|
|
g_tooltip.font_color.alpha = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cleanup_tooltip()
|
|
|
|
{
|
|
|
|
stop_tooltip_timeout();
|
|
|
|
tooltip_hide(0);
|
|
|
|
tooltip_copy_text(0);
|
|
|
|
if (g_tooltip.window) XDestroyWindow(server.dsp, g_tooltip.window);
|
|
|
|
if (g_tooltip.font_desc) pango_font_description_free(g_tooltip.font_desc);
|
|
|
|
}
|
|
|
|
|
2009-09-14 21:28:17 +00:00
|
|
|
|
|
|
|
void init_tooltip()
|
|
|
|
{
|
|
|
|
if (!g_tooltip.font_desc)
|
|
|
|
g_tooltip.font_desc = pango_font_description_from_string("sans 10");
|
|
|
|
|
|
|
|
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;
|
|
|
|
unsigned long mask = CWEventMask|CWColormap|CWBorderPixel|CWBackPixel|CWOverrideRedirect;
|
2009-09-14 21:28:17 +00:00
|
|
|
if (g_tooltip.window) XDestroyWindow(server.dsp, g_tooltip.window);
|
2009-12-30 11:27:29 +00:00
|
|
|
g_tooltip.window = XCreateWindow(server.dsp, server.root_win, 0, 0, 100, 20, 0, server.depth, InputOutput, server.visual, mask, &attr);
|
2009-09-14 21:28:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-08-07 12:17:56 +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
|
2010-08-19 20:33:42 +00:00
|
|
|
x = area->posx + area->width / 2 + e->xmotion.x_root - e->xmotion.x;
|
|
|
|
y = area->posy + area->height / 2 + e->xmotion.y_root - e->xmotion.y;
|
|
|
|
if (!panel_horizontal)
|
|
|
|
y -= height/2;
|
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();
|
2009-09-14 21:28:17 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-05 20:38:49 +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;
|
|
|
|
XTranslateCoordinates( server.dsp, server.root_win, g_tooltip.panel->main_win, x, y, &mx, &my, &w);
|
|
|
|
Area* area = click_area(g_tooltip.panel, mx, my);
|
2009-12-27 20:33:02 +00:00
|
|
|
stop_tooltip_timeout();
|
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;
|
|
|
|
XMapWindow(server.dsp, g_tooltip.window);
|
2010-01-14 21:05:42 +00:00
|
|
|
tooltip_update();
|
2009-11-16 10:06:45 +00:00
|
|
|
XFlush(server.dsp);
|
2009-09-14 21:28:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void tooltip_update_geometry()
|
|
|
|
{
|
|
|
|
cairo_surface_t *cs;
|
|
|
|
cairo_t *c;
|
|
|
|
PangoLayout* layout;
|
|
|
|
cs = cairo_xlib_surface_create(server.dsp, g_tooltip.window, server.visual, width, height);
|
|
|
|
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);
|
2010-01-09 00:11:01 +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
|
|
|
|
2009-11-16 17:17:53 +00:00
|
|
|
Panel* panel = g_tooltip.panel;
|
2009-09-14 21:28:17 +00:00
|
|
|
if (panel_horizontal && panel_position & BOTTOM)
|
|
|
|
y = panel->posy-height;
|
|
|
|
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;
|
2009-11-16 17:17:53 +00:00
|
|
|
Panel* panel = g_tooltip.panel;
|
2009-10-31 16:33:35 +00:00
|
|
|
int screen_width = server.monitor[panel->monitor].x + server.monitor[panel->monitor].width;
|
|
|
|
int screen_height = server.monitor[panel->monitor].y + server.monitor[panel->monitor].height;
|
2009-09-14 21:28:17 +00:00
|
|
|
if ( x+width <= screen_width && y+height <= screen_height && x>=0 && y>=0)
|
|
|
|
return; // no adjustment needed
|
|
|
|
|
|
|
|
if (panel_horizontal) {
|
|
|
|
min_x=0;
|
|
|
|
max_width=screen_width;
|
|
|
|
max_height=screen_height-panel->area.height;
|
|
|
|
if (panel_position & BOTTOM)
|
|
|
|
min_y=0;
|
|
|
|
else
|
|
|
|
min_y=panel->area.height;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
max_width=screen_width-panel->area.width;
|
|
|
|
min_y=0;
|
|
|
|
max_height=screen_height;
|
|
|
|
if (panel_position & LEFT)
|
|
|
|
min_x=panel->area.width;
|
|
|
|
else
|
|
|
|
min_x=0;
|
|
|
|
}
|
|
|
|
|
2009-10-31 16:33:35 +00:00
|
|
|
if (x+width > server.monitor[panel->monitor].x + server.monitor[panel->monitor].width)
|
|
|
|
x = server.monitor[panel->monitor].x + server.monitor[panel->monitor].width - width;
|
|
|
|
if ( y+height > server.monitor[panel->monitor].y + server.monitor[panel->monitor].height)
|
|
|
|
y = server.monitor[panel->monitor].y + server.monitor[panel->monitor].height - height;
|
2009-09-14 21:28:17 +00:00
|
|
|
|
|
|
|
if (x<min_x)
|
|
|
|
x=min_x;
|
|
|
|
if (width>max_width)
|
|
|
|
width = max_width;
|
|
|
|
if (y<min_y)
|
|
|
|
y=min_y;
|
|
|
|
if (height>max_height)
|
|
|
|
height=max_height;
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
tooltip_adjust_geometry();
|
|
|
|
XMoveResizeWindow(server.dsp, g_tooltip.window, x, y, width, height);
|
|
|
|
|
|
|
|
// Stuff for drawing the tooltip
|
|
|
|
cairo_surface_t *cs;
|
|
|
|
cairo_t *c;
|
|
|
|
PangoLayout* layout;
|
|
|
|
cs = cairo_xlib_surface_create(server.dsp, g_tooltip.window, server.visual, width, height);
|
|
|
|
c = cairo_create(cs);
|
2010-01-09 00:11:01 +00:00
|
|
|
Color bc = g_tooltip.bg->back;
|
|
|
|
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);
|
2009-12-30 23:27:31 +00:00
|
|
|
draw_rect(c, b.width, b.width, width-2*b.width, height-2*b.width, b.rounded-b.width/1.571);
|
|
|
|
cairo_set_source_rgba(c, bc.color[0], bc.color[1], bc.color[2], bc.alpha);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
cairo_rectangle(c, 0., 0, width, height);
|
|
|
|
cairo_set_source_rgb(c, bc.color[0], bc.color[1], bc.color[2]);
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
draw_rect(c, b.width/2.0, b.width/2.0, width - b.width, height - b.width, b.rounded);
|
|
|
|
else
|
|
|
|
cairo_rectangle(c, b.width/2.0, b.width/2.0, width-b.width, height-b.width);
|
2009-12-30 23:27:31 +00:00
|
|
|
cairo_set_source_rgba(c, b.color[0], b.color[1], b.color[2], b.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;
|
2009-09-14 21:28:17 +00:00
|
|
|
cairo_set_source_rgba(c, fc.color[0], fc.color[1], fc.color[2], fc.alpha);
|
|
|
|
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);
|
|
|
|
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);
|
2009-09-14 21:28:17 +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.)
|
2010-01-09 00:11:01 +00:00
|
|
|
cairo_move_to(c, -r1.x/2+g_tooltip.bg->border.width+g_tooltip.paddingx, -r1.y/2+g_tooltip.bg->border.width+g_tooltip.paddingy);
|
2009-09-14 21:28:17 +00:00
|
|
|
pango_cairo_show_layout (c, layout);
|
|
|
|
|
|
|
|
g_object_unref (layout);
|
|
|
|
cairo_destroy (c);
|
|
|
|
cairo_surface_destroy (cs);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void tooltip_trigger_hide(Tooltip* tooltip)
|
|
|
|
{
|
|
|
|
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();
|
2009-09-14 21:28:17 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-05 20:38:49 +00:00
|
|
|
void tooltip_hide(void* arg)
|
2009-09-14 21:28:17 +00:00
|
|
|
{
|
2009-12-27 20:33:02 +00:00
|
|
|
stop_tooltip_timeout();
|
2009-09-14 21:28:17 +00:00
|
|
|
if (g_tooltip.mapped) {
|
|
|
|
g_tooltip.mapped = False;
|
|
|
|
XUnmapWindow(server.dsp, g_tooltip.window);
|
2009-11-16 10:06:45 +00:00
|
|
|
XFlush(server.dsp);
|
2009-09-14 21:28:17 +00:00
|
|
|
}
|
|
|
|
}
|
2009-11-16 07:27:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
void start_show_timeout()
|
|
|
|
{
|
2009-12-27 20:33:02 +00:00
|
|
|
if (g_tooltip.timeout)
|
2010-01-05 20:38:49 +00:00
|
|
|
change_timeout(g_tooltip.timeout, g_tooltip.show_timeout_msec, 0, tooltip_show, 0);
|
2009-12-27 20:33:02 +00:00
|
|
|
else
|
2010-01-05 20:38:49 +00:00
|
|
|
g_tooltip.timeout = add_timeout(g_tooltip.show_timeout_msec, 0, tooltip_show, 0);
|
2009-11-16 07:27:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void start_hide_timeout()
|
|
|
|
{
|
2009-12-27 20:33:02 +00:00
|
|
|
if (g_tooltip.timeout)
|
2010-01-05 20:38:49 +00:00
|
|
|
change_timeout(g_tooltip.timeout, g_tooltip.hide_timeout_msec, 0, tooltip_hide, 0);
|
2009-11-16 07:27:28 +00:00
|
|
|
else
|
2010-01-05 20:38:49 +00:00
|
|
|
g_tooltip.timeout = add_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
|
|
|
{
|
2009-12-27 20:33:02 +00:00
|
|
|
if (g_tooltip.timeout) {
|
|
|
|
stop_timeout(g_tooltip.timeout);
|
|
|
|
g_tooltip.timeout = 0;
|
|
|
|
}
|
2009-11-16 07:27:28 +00:00
|
|
|
}
|
2009-11-19 13:53:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
void tooltip_copy_text(Area* area)
|
|
|
|
{
|
|
|
|
free(g_tooltip.tooltip_text);
|
|
|
|
if (area && area->_get_tooltip_text)
|
|
|
|
g_tooltip.tooltip_text = strdup(area->_get_tooltip_text(area));
|
|
|
|
else
|
|
|
|
g_tooltip.tooltip_text = 0;
|
|
|
|
g_tooltip.area = area;
|
|
|
|
}
|