[PATCH 3/6 weston] window: add simple text tooltip handlers

Tiago Vignatti tiago.vignatti at intel.com
Mon May 21 06:47:47 PDT 2012


Using set_transient.

Signed-off-by: Tiago Vignatti <tiago.vignatti at intel.com>
---
 clients/window.c |   86 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 clients/window.h |    6 ++++
 2 files changed, 92 insertions(+)

diff --git a/clients/window.c b/clients/window.c
index 5c4d28b..0d0c1d9 100644
--- a/clients/window.c
+++ b/clients/window.c
@@ -133,6 +133,7 @@ struct window_output {
 struct window {
 	struct display *display;
 	struct window *parent;
+	struct tooltip *tooltip;
 	struct wl_list window_output_list;
 	struct wl_surface *surface;
 	struct wl_shell_surface *shell_surface;
@@ -268,6 +269,12 @@ struct menu {
 	menu_func_t func;
 };
 
+struct tooltip {
+	struct window *window;
+	struct widget *widget;
+	char *entry;
+};
+
 struct cursor_image {
 	cairo_surface_t *surface;
 	int width, height;
@@ -1165,6 +1172,84 @@ window_get_wl_shell_surface(struct window *window)
 }
 
 static void
+tooltip_redraw_handler(struct widget *widget, void *data)
+{
+	cairo_t *cr;
+	const int32_t r = 3;
+	struct tooltip *tooltip = data;
+	int32_t width, height;
+	struct window *window = widget->window;
+
+	cr = cairo_create(window->cairo_surface);
+	cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
+	cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 0.0);
+	cairo_paint(cr);
+
+	width = window->allocation.width;
+	height = window->allocation.height;
+	rounded_rect(cr, 0, 0, width, height, r);
+
+	cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
+	cairo_set_source_rgba(cr, 0.0, 0.0, 0.4, 0.8);
+	cairo_fill(cr);
+
+	cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
+	cairo_move_to(cr, 10, 16);
+	cairo_show_text(cr, tooltip->entry);
+
+	cairo_destroy(cr);
+}
+
+void
+window_create_tooltip(struct window *parent, int32_t x, int32_t y, char *entry)
+{
+	struct window *window;
+	struct tooltip *tooltip;
+	const int32_t margin = 3;
+	int width;
+
+	/* we allow only one tooltip per window */
+	if (parent->tooltip)
+		return;
+
+	tooltip = malloc(sizeof *tooltip);
+	if (!tooltip)
+		return;
+
+	window = window_create_transient(parent->display, parent, x, y,
+			WL_SHELL_SURFACE_TRANSIENT_INACTIVE);
+	if (!window)
+		return;
+
+	tooltip->window = window;
+	tooltip->widget = window_add_widget(tooltip->window, tooltip);
+	tooltip->entry = strdup(entry);
+
+	widget_set_redraw_handler(tooltip->widget, tooltip_redraw_handler);
+	/* assuming each letter gets six pixels */
+	width = 6 * strlen(tooltip->entry) + 20;
+	window_schedule_resize(window, width, 20 + margin * 2);
+
+	parent->tooltip = tooltip;
+}
+
+void
+window_destroy_tooltip(struct window *parent)
+{
+	struct tooltip *tooltip = parent->tooltip;
+
+	if (!tooltip)
+		return;
+
+	widget_destroy(tooltip->widget);
+	window_destroy(tooltip->window);
+	free(tooltip->entry);
+	free(tooltip);
+
+	parent->tooltip = NULL;
+}
+
+static void
 frame_resize_handler(struct widget *widget,
 		     int32_t width, int32_t height, void *data)
 {
@@ -2690,6 +2775,7 @@ window_create(struct display *display)
 	if (!window)
 		return NULL;
 
+	window->tooltip = NULL;
 	return window;
 }
 
diff --git a/clients/window.h b/clients/window.h
index 162cc34..330e532 100644
--- a/clients/window.h
+++ b/clients/window.h
@@ -308,6 +308,12 @@ window_set_title(struct window *window, const char *title);
 const char *
 window_get_title(struct window *window);
 
+void
+window_create_tooltip(struct window *parent, int32_t x, int32_t y, char *entry);
+
+void
+window_destroy_tooltip(struct window *parent);
+
 struct widget *
 widget_add_widget(struct widget *parent, void *data);
 void
-- 
1.7.9.5



More information about the wayland-devel mailing list