libxputty  0.1
A damn tiny abstraction Layer to create X11 window/widgets with cairo surfaces
xtooltip.c
Go to the documentation of this file.
1 /*
2  * 0BSD
3  *
4  * BSD Zero Clause License
5  *
6  * Copyright (c) 2019 Hermann Meyer
7  *
8  * Permission to use, copy, modify, and/or distribute this software for any
9  * purpose with or without fee is hereby granted.
10 
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
12  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13  * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
14  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
16  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17  * PERFORMANCE OF THIS SOFTWARE.
18  *
19  */
20 
21 
22 #include "xtooltip.h"
23 #include "xtooltip_private.h"
24 
25 void tooltip_set_text(Widget_t *w, const char* label) {
26  Widget_t *wid = NULL;
27  bool is_tooltip = false;
28  int i = 0;
29  for(;i<w->childlist->elem;i++) {
30  wid = w->childlist->childs[i];
31  if (wid->flags & IS_TOOLTIP) {
32  wid->label = label;
33  _get_width(wid);
34  is_tooltip = true;
35  break;
36  }
37  }
38  if (!is_tooltip) add_tooltip(w, label);
39 }
40 
41 void add_tooltip(Widget_t *w, const char* label) {
42  Widget_t *wid = create_tooltip(w, 25, 25);
43  wid->label = label;
44  _get_width(wid);
45 }
46 
47 Widget_t* create_tooltip(Widget_t *parent, int width, int height) {
48 
49  int x1, y1;
50  Window child;
51  XTranslateCoordinates( parent->app->dpy, parent->widget, DefaultRootWindow(parent->app->dpy), 0, 0, &x1, &y1, &child );
52  Widget_t *wid = create_window(parent->app, DefaultRootWindow(parent->app->dpy), x1+10, y1+10, width, height);
53 
54  XSetWindowAttributes attributes;
55  attributes.override_redirect = True;
56  XChangeWindowAttributes(parent->app->dpy, wid->widget, CWOverrideRedirect, &attributes);
57 
58  Atom window_type = XInternAtom(wid->app->dpy, "_NET_WM_WINDOW_TYPE", False);
59  Atom window_type_tooltip = XInternAtom(wid->app->dpy, "_NET_WM_WINDOW_TYPE_TOOLTIP", False);
60  XChangeProperty(wid->app->dpy, wid->widget, window_type,
61  XA_ATOM, 32, PropModeReplace, (unsigned char *) &window_type_tooltip,1 );
62 
63  Atom window_state = XInternAtom(wid->app->dpy, "_NET_WM_STATE", False);
64  Atom window_state_modal = XInternAtom(wid->app->dpy, "_NET_WM_STATE_MODAL", False);
65  XChangeProperty(wid->app->dpy, wid->widget, window_state,
66  XA_ATOM, 32, PropModeReplace, (unsigned char *) &window_state_modal, 1);
67 
68  XSetTransientForHint(parent->app->dpy,wid->widget,parent->widget);
69  wid->flags &= ~USE_TRANSPARENCY;
71  wid->flags |= IS_TOOLTIP;
72  parent->flags |= HAS_TOOLTIP;
73  wid->scale.gravity = NONE;
74  childlist_add_child(parent->childlist, wid);
75  return wid;
76 }
tooltip_set_text
void tooltip_set_text(Widget_t *w, const char *label)
tooltip_set_text - set a (new) text to a tooltip for Widget_t
Definition: xtooltip.c:25
HAS_TOOLTIP
@ HAS_TOOLTIP
Definition: xwidget.h:249
Resize_t::gravity
Gravity gravity
Definition: xwidget.h:192
xtooltip_private.h
NONE
@ NONE
Definition: xwidget.h:173
Childlist_t::childs
Widget_t ** childs
Definition: xchildlist.h:51
childlist_add_child
void childlist_add_child(Childlist_t *childlist, Widget_t *child)
childlist_add_child - internal use to add a child to the Childlist_t You usually didn't need to cal...
Definition: xchildlist.c:42
Func_t::expose_callback
xevfunc expose_callback
Definition: xwidget.h:80
_get_width
void _get_width(Widget_t *w)
_get_width - get the width of a tooltip text and resize the tooltip widget to match the size
Definition: xtooltip_private.c:25
add_tooltip
void add_tooltip(Widget_t *w, const char *label)
add_tooltip - add a tooltip to Widget_t
Definition: xtooltip.c:41
Widget_t::flags
long long flags
Definition: xwidget.h:324
Widget_t::scale
Resize_t scale
Definition: xwidget.h:356
Widget_t::app
Xputty * app
Definition: xwidget.h:300
Xputty::dpy
Display * dpy
Definition: xputty.h:181
USE_TRANSPARENCY
@ USE_TRANSPARENCY
Definition: xwidget.h:243
Widget_t::widget
Window widget
Definition: xwidget.h:302
Widget_t
Widget_t - struct to hold the basic Widget_t info.
Definition: xwidget.h:298
Widget_t::childlist
Childlist_t * childlist
Definition: xwidget.h:336
create_window
Widget_t * create_window(Xputty *app, Window win, int x, int y, int width, int height)
*create_window - create a Window You need to create as least minimun one Window to get started....
Definition: xwidget.c:145
IS_TOOLTIP
@ IS_TOOLTIP
Definition: xwidget.h:241
Childlist_t::elem
int elem
Definition: xchildlist.h:57
Widget_t::func
Func_t func
Definition: xwidget.h:310
create_tooltip
Widget_t * create_tooltip(Widget_t *parent, int width, int height)
create_tooltip - create a tooltip for a Widget_t
Definition: xtooltip.c:47
xtooltip.h
Widget_t::label
const char * label
Definition: xwidget.h:326
_draw_tooltip
void _draw_tooltip(void *w_, void *user_data)
_draw_tooltip - draw tooltip on expose call
Definition: xtooltip_private.c:32