[PATCH weston 5/8] window: add simple text tooltip handlers

Tiago Vignatti tiago.vignatti at intel.com
Mon May 7 05:23:11 PDT 2012


Using set_transient.

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

diff --git a/clients/window.c b/clients/window.c
index 61713ca..b7d9a45 100644
--- a/clients/window.c
+++ b/clients/window.c
@@ -118,6 +118,7 @@ enum {
 struct window {
 	struct display *display;
 	struct window *parent;
+	struct tooltip *tooltip;
 	struct wl_surface *surface;
 	struct wl_shell_surface *shell_surface;
 	struct wl_region *input_region;
@@ -231,6 +232,12 @@ struct cursor {
 	struct cursor_image *images;
 };
 
+struct tooltip {
+	struct window *window;
+	struct widget *widget;
+	char *entry;
+};
+
 enum {
 	POINTER_DEFAULT = 100,
 	POINTER_UNSET
@@ -973,6 +980,82 @@ 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,
+		      const char *entry)
+{
+	struct window *window;
+	struct tooltip *tooltip;
+	const int32_t margin = 3;
+
+	/* 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_METHOD_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);
+	window_schedule_resize(window, 200, 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)
 {
@@ -2289,6 +2372,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 9c45fbd..5a8840c 100644
--- a/clients/window.h
+++ b/clients/window.h
@@ -307,6 +307,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,
+                      const 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