4 #include "../src/config.h"
8 #include <X11/extensions/shape.h>
12 typedef struct W_Balloon
{
15 WMHashTable
*table
; /* Table from view ptr to text */
21 WMHandlerID timer
; /* timer for showing balloon */
23 WMHandlerID noDelayTimer
;
27 Window forWindow
; /* window for which the balloon
28 * is being show in the moment */
31 WMAlignment alignment
:2;
40 #define DEFAULT_WIDTH 60
41 #define DEFAULT_HEIGHT 14
42 #define DEFAULT_ALIGNMENT WALeft
43 #define DEFAULT_DELAY 500
45 #define NO_DELAY_DELAY 150
48 static void destroyBalloon(Balloon
*bPtr
);
51 static void handleEvents(XEvent
*event
, void *data
);
53 static void showText(Balloon
*bPtr
, int x
, int y
, int w
, int h
, char *text
);
57 W_CreateBalloon(WMScreen
*scr
)
61 bPtr
= wmalloc(sizeof(Balloon
));
62 memset(bPtr
, 0, sizeof(Balloon
));
64 bPtr
->view
= W_CreateUnmanagedTopView(scr
);
69 bPtr
->view
->self
= bPtr
;
71 bPtr
->textColor
= WMRetainColor(bPtr
->view
->screen
->black
);
73 WMCreateEventHandler(bPtr
->view
, StructureNotifyMask
, handleEvents
, bPtr
);
75 W_ResizeView(bPtr
->view
, DEFAULT_WIDTH
, DEFAULT_HEIGHT
);
76 bPtr
->flags
.alignment
= DEFAULT_ALIGNMENT
;
78 bPtr
->table
= WMCreateHashTable(WMIntHashCallbacks
);
80 bPtr
->delay
= DEFAULT_DELAY
;
82 bPtr
->flags
.enabled
= 1;
90 WMSetBalloonTextAlignment(WMScreen
*scr
, WMAlignment alignment
)
92 scr
->balloon
->flags
.alignment
= alignment
;
98 WMSetBalloonTextForView(char *text
, WMView
*view
)
100 char *oldText
= NULL
;
101 WMScreen
*scr
= view
->screen
;
104 oldText
= WMHashInsert(scr
->balloon
->table
, view
, wstrdup(text
));
106 oldText
= WMHashGet(scr
->balloon
->table
, view
);
108 WMHashRemove(scr
->balloon
->table
, view
);
118 WMSetBalloonFont(WMScreen
*scr
, WMFont
*font
)
120 Balloon
*bPtr
= scr
->balloon
;
122 if (bPtr
->font
!=NULL
)
123 WMReleaseFont(bPtr
->font
);
126 bPtr
->font
= WMRetainFont(font
);
133 WMSetBalloonTextColor(WMScreen
*scr
, WMColor
*color
)
135 Balloon
*bPtr
= scr
->balloon
;
138 WMReleaseColor(bPtr
->textColor
);
140 bPtr
->textColor
= WMRetainColor(color
);
145 WMSetBalloonDelay(WMScreen
*scr
, int delay
)
147 scr
->balloon
->delay
= delay
;
152 WMSetBalloonEnabled(WMScreen
*scr
, Bool flag
)
154 scr
->balloon
->flags
.enabled
= flag
;
156 W_UnmapView(scr
->balloon
->view
);
161 clearNoDelay(void *data
)
163 Balloon
*bPtr
= (Balloon
*)data
;
165 bPtr
->flags
.noDelay
= 0;
166 bPtr
->noDelayTimer
= NULL
;
171 W_BalloonHandleLeaveView(WMView
*view
)
173 Balloon
*bPtr
= view
->screen
->balloon
;
175 if (bPtr
->forWindow
== view
->window
) {
176 if (bPtr
->view
->flags
.mapped
) {
177 W_UnmapView(bPtr
->view
);
178 bPtr
->noDelayTimer
= WMAddTimerHandler(NO_DELAY_DELAY
,
182 WMDeleteTimerHandler(bPtr
->timer
);
186 bPtr
->forWindow
= None
;
192 * botar balao perto do cursor
193 * so mapear balao se o mouse ficar parado pelo delay
198 showBalloon(void *data
)
201 WMView
*view
= (WMView
*)data
;
202 Balloon
*bPtr
= view
->screen
->balloon
;
208 text
= WMHashGet(bPtr
->table
, view
);
212 XTranslateCoordinates(view
->screen
->display
, view
->window
,
213 view
->screen
->rootWin
, 0, 0, &x
, &y
, &foo
);
215 if (!bPtr
->view
->flags
.realized
)
216 W_RealizeView(bPtr
->view
);
218 showText(bPtr
, x
, y
, view
->size
.width
, view
->size
.height
, text
);
220 bPtr
->flags
.noDelay
= 1;
226 W_BalloonHandleEnterView(WMView
*view
)
228 Balloon
*bPtr
= view
->screen
->balloon
;
231 if (!bPtr
->flags
.enabled
)
234 text
= WMHashGet(bPtr
->table
, view
);
236 if (bPtr
->view
->flags
.realized
)
237 W_UnmapView(bPtr
->view
);
243 WMDeleteTimerHandler(bPtr
->timer
);
246 if (bPtr
->noDelayTimer
)
247 WMDeleteTimerHandler(bPtr
->noDelayTimer
);
248 bPtr
->noDelayTimer
= NULL
;
250 bPtr
->forWindow
= view
->window
;
252 if (bPtr
->flags
.noDelay
) {
257 bPtr
->timer
= WMAddTimerHandler(bPtr
->delay
, showBalloon
, view
);
267 #define TLEFT (TOP|LEFT)
268 #define TRIGHT (TOP|RIGHT)
269 #define BLEFT (BOTTOM|LEFT)
270 #define BRIGHT (BOTTOM|RIGHT)
278 drawBalloon(Display
*dpy
, Pixmap pix
, GC gc
, int x
, int y
, int w
, int h
,
284 XFillArc(dpy
, pix
, gc
, x
, y
, rad
, rad
, 90*64, 90*64);
285 XFillArc(dpy
, pix
, gc
, x
, y
+h
-1-rad
, rad
, rad
, 180*64, 90*64);
287 XFillArc(dpy
, pix
, gc
, x
+w
-1-rad
, y
, rad
, rad
, 0*64, 90*64);
288 XFillArc(dpy
, pix
, gc
, x
+w
-1-rad
, y
+h
-1-rad
, rad
, rad
, 270*64, 90*64);
290 XFillRectangle(dpy
, pix
, gc
, x
, y
+rad
/2, w
, h
-rad
);
291 XFillRectangle(dpy
, pix
, gc
, x
+rad
/2, y
, w
-rad
, h
);
295 pt
[1].y
= y
+h
-1+SPACE
;
303 pt
[0].x
= x
+w
-h
+2*h
/16;
304 pt
[1].x
= x
+w
-h
+11*h
/16;
305 pt
[2].x
= x
+w
-h
+7*h
/16;
307 pt
[0].x
= x
+h
-2*h
/16;
308 pt
[1].x
= x
+h
-11*h
/16;
309 pt
[2].x
= x
+h
-7*h
/16;
311 XFillPolygon(dpy
, pix
, gc
, pt
, 3, Convex
, CoordModeOrigin
);
316 makePixmap(WMScreen
*scr
, int width
, int height
, int side
, Pixmap
*mask
)
318 Display
*dpy
= WMScreenDisplay(scr
);
322 WMColor
*black
= WMBlackColor(scr
);
323 WMColor
*white
= WMWhiteColor(scr
);
325 bitmap
= XCreatePixmap(dpy
, scr
->rootWin
, width
+SPACE
, height
+SPACE
, 1);
327 XSetForeground(dpy
, scr
->monoGC
, 0);
328 XFillRectangle(dpy
, bitmap
, scr
->monoGC
, 0, 0, width
+SPACE
, height
+SPACE
);
330 pixmap
= XCreatePixmap(dpy
, scr
->rootWin
, width
+SPACE
, height
+SPACE
,
333 XFillRectangle(dpy
, pixmap
, WMColorGC(black
), 0, 0,
334 width
+SPACE
, height
+SPACE
);
343 XSetForeground(dpy
, scr
->monoGC
, 1);
344 drawBalloon(dpy
, bitmap
, scr
->monoGC
, x
, y
, width
, height
, side
);
345 drawBalloon(dpy
, pixmap
, WMColorGC(white
), x
+1, y
+1, width
-2, height
-2,
350 WMReleaseColor(black
);
351 WMReleaseColor(white
);
358 showText(Balloon
*bPtr
, int x
, int y
, int h
, int w
, char *text
)
360 WMScreen
*scr
= bPtr
->view
->screen
;
361 Display
*dpy
= WMScreenDisplay(scr
);
366 WMFont
*font
= bPtr
->font
? bPtr
->font
: scr
->normalFont
;
378 while (ptr
&& ptr2
) {
379 ptr2
= strchr(ptr
, '\n');
381 w
= WMWidthOfString(font
, ptr
, ptr2
- ptr
);
383 w
= WMWidthOfString(font
, ptr
, strlen(ptr
));
393 textHeight
= W_GetTextHeight(font
, text
, width
, False
);
395 height
= textHeight
+ 4;
402 if (x
+ width
> scr
->rootView
->size
.width
) {
404 bx
= x
- width
+ w
/2;
411 if (bx
+ width
> scr
->rootView
->size
.width
)
412 bx
= scr
->rootView
->size
.width
- width
;
414 if (y
- (height
+ SPACE
) < 0) {
420 by
= y
- (height
+ SPACE
);
423 pixmap
= makePixmap(scr
, width
, height
, side
, &mask
);
425 W_PaintText(bPtr
->view
, pixmap
, font
, 8, ty
+ (height
- textHeight
)/2,
426 width
, bPtr
->flags
.alignment
,
427 WMColorGC(bPtr
->textColor
? bPtr
->textColor
: scr
->black
),
428 False
, text
, strlen(text
));
430 XSetWindowBackgroundPixmap(dpy
, bPtr
->view
->window
, pixmap
);
432 W_ResizeView(bPtr
->view
, width
, height
+SPACE
);
434 XFreePixmap(dpy
, pixmap
);
437 XShapeCombineMask(dpy
, bPtr
->view
->window
, ShapeBounding
, 0, 0, mask
,
440 XFreePixmap(dpy
, mask
);
442 W_MoveView(bPtr
->view
, bx
, by
);
444 W_MapView(bPtr
->view
);
449 handleEvents(XEvent
*event
, void *data
)
451 Balloon
*bPtr
= (Balloon
*)data
;
453 switch (event
->type
) {
455 destroyBalloon(bPtr
);
462 destroyBalloon(Balloon
*bPtr
)
467 e
= WMEnumerateHashTable(bPtr
->table
);
469 while ((str
= WMNextHashEnumeratorItem(&e
))) {
472 WMFreeHashTable(bPtr
->table
);
475 WMReleaseColor(bPtr
->textColor
);
478 WMReleaseFont(bPtr
->font
);