Fixed Container.Background support. We can have that support or not.
[idesk.git] / src / XImlib2Caption.cpp
blobc49447b7fbe32734f19027682af36ce082eec77c
1 /* vim:tabstop=4:expandtab:shiftwidth=4
2 *
3 * Idesk -- XImlib2Caption.cpp
5 * Copyright (c) 2002, Chris (nikon) (nikon@sc.rr.com)
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
11 * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * Neither the name of the <ORGANIZATION> nor the names of its
19 * contributors may be used to endorse or promote products derived from
20 * this software without specific prior written permission.
22 * (See the included file COPYING / BSD )
24 #ifdef SHAPE
25 #include <X11/extensions/shape.h>
26 #endif // SHAPE
27 #include "XImlib2Caption.h"
28 #include "XIconWithShadow.h"
30 XImlib2Caption::XImlib2Caption(AbstractContainer * c, AbstractIcon * iParent,
31 AbstractConfig * con, AbstractIconConfig * iConfig)
32 : AbstractCaption(c, iParent, con, iConfig),
33 XImlib2Image(c, iParent, con, iConfig), FONTSHIFT(4)
35 XDesktopContainer * xContainer =
36 dynamic_cast<XDesktopContainer *>(AbstractImage::container);
38 visual = imlib_context_get_visual();
39 cmap = imlib_context_get_colormap();
40 configure();
41 createFont();
44 XImlib2Caption::~XImlib2Caption()
46 XDesktopContainer * xContainer =
47 dynamic_cast<XDesktopContainer *>(AbstractImage::container);
49 XftColorFree( xContainer->getDisplay(), visual, cmap, &fontColor );
51 if (shadowOn)
52 XftColorFree( xContainer->getDisplay(), visual, cmap, &shadowFontColor);
54 if(gc)
55 XFreeGC(xContainer->getDisplay(), gc);
57 XDestroyWindow(xContainer->getDisplay(), window);
58 XftDrawDestroy(fontDrawHandle);
61 void XImlib2Caption::configure()
63 DesktopIconConfig * dIconConfig =
64 dynamic_cast<DesktopIconConfig *>(AbstractImage::iconConfig);
66 //grab the text
67 text = AbstractImage::iconConfig->getCaption();
69 //set boldness
70 if (dIconConfig->getBoldness())
71 boldVal = XFT_WEIGHT_BOLD;
72 else
73 boldVal = XFT_WEIGHT_MEDIUM;
75 //set shadow options
76 shadowOn = dIconConfig->isShadow();
77 shadowX = dIconConfig->getShadowX();
78 shadowY = dIconConfig->getShadowY();
79 shadowColor = dIconConfig->getShadowColor();
81 //captionOnHover
82 captionOnHover = dIconConfig->getCaptionOnHover();
84 //captionPlacement
85 captionPlacement = dIconConfig->getCaptionPlacement();
88 void XImlib2Caption::createFont()
90 XDesktopContainer * xContainer =
91 dynamic_cast<XDesktopContainer *>(AbstractImage::container);
92 DesktopIconConfig * dIconConfig =
93 dynamic_cast<DesktopIconConfig *>(AbstractImage::iconConfig);
95 font = XftFontOpen( xContainer->getDisplay(),
96 DefaultScreen( xContainer->getDisplay()),
97 XFT_FAMILY,
98 XftTypeString,
99 dIconConfig->getFont().c_str(),
100 XFT_SIZE,
101 XftTypeDouble,
102 (double)dIconConfig->getFontSize(),
103 XFT_WEIGHT, XftTypeInteger, boldVal,
104 NULL );
106 XColor screen, exact;
107 if (!XAllocNamedColor (xContainer->getDisplay(), cmap, (char*)dIconConfig->getFontColor().c_str(), &screen, &exact))
108 cout << "BAD COLORS!!!\n";
110 XftColorAllocName(xContainer->getDisplay(), visual, cmap,
111 (char*)dIconConfig->getFontColor().c_str(), &fontColor );
113 if (shadowOn)
114 XftColorAllocName(xContainer->getDisplay(), visual, cmap,
115 (char*)shadowColor.c_str(), &shadowFontColor );
119 Window * XImlib2Caption::getWindow()
121 return &window;
124 void XImlib2Caption::createWindow()
126 XDesktopContainer * xContainer =
127 dynamic_cast<XDesktopContainer *>(AbstractImage::container);
129 DesktopConfig * dConfig =
130 dynamic_cast<DesktopConfig *>(XImlib2Image::config);
132 XSetWindowAttributes attr;
133 attr.override_redirect = True;
134 attr.cursor = XCreateFontCursor(xContainer->getDisplay(), dConfig->getCursorOver());
136 attr.event_mask = SubstructureRedirectMask |
137 SubstructureNotifyMask |
138 ButtonPressMask |
139 ButtonReleaseMask |
140 PointerMotionMask |
141 PropertyChangeMask |
142 ExposureMask;
144 XftTextExtentsUtf8( xContainer->getDisplay(), font, (XftChar8*)text.c_str(), text.length(), &fontInfo );
146 // fix window extents so shadow text is not cut
147 // TODO: fix window extents if shadow is negative:
148 // Would have to edit x and y of the window
149 width = fontInfo.width + FONTSHIFT;
150 height = font->height;
152 if (shadowOn)
154 width += shadowX > 0 ? shadowX : 0;
155 height += shadowY > 0 ? shadowY : 0;
157 window = XCreateWindow( xContainer->getDisplay(),
158 xContainer->getWindow(),
161 width,
162 height,
164 CopyFromParent,
165 CopyFromParent,
166 CopyFromParent,
167 CWOverrideRedirect|CWEventMask|CWCursor,
168 &attr );
171 void XImlib2Caption::renderFont2Imlib()
174 XDesktopContainer * xContainer =
175 dynamic_cast<XDesktopContainer *>(AbstractImage::container);
177 Display * display = xContainer->getDisplay();
178 int screen = DefaultScreen(display);
180 pixmap = XCreatePixmap(display, window, width,
181 height,
182 DefaultDepth(display, screen));
184 XSetWindowBackgroundPixmap (display, window, pixmap);
186 gc = XCreateGC(display, window, 0, 0);
188 if( pixmap == (Pixmap) NULL )
190 printf("\nError creando Pixmap\n");
191 XCloseDisplay(display);
192 exit(1);
195 fontDrawHandle = XftDrawCreate(display, pixmap, visual, cmap);
197 XSetFillStyle(display, gc, FillSolid);
198 XSetForeground(display, gc, BlackPixel(display, 0));
199 XSetBackground(display, gc, WhitePixel(display, 0));
200 XFillRectangle(display, pixmap, gc, 0, 0, width, height);
201 Pixmap shapeMask = XCreatePixmap(display, window, width, height,1);
202 GC tempGc = XCreateGC(display, shapeMask, 0, 0);
203 XCopyPlane(display, pixmap, shapeMask, tempGc, 0, 0, width, height, 0, 0, 1);
204 #ifdef SHAPE
205 XShapeCombineMask(display, window, 0, 0, 0, shapeMask, ShapeUnion);
206 #endif // SHAPE
207 imlib_context_set_drawable(pixmap);
210 void XImlib2Caption::draw()
212 XDesktopContainer * xContainer =
213 dynamic_cast<XDesktopContainer *>(AbstractImage::container);
214 XIconWithShadow * sIcon = dynamic_cast<XIconWithShadow *>(AbstractImage::iconParent);
216 Display * display = xContainer->getDisplay();
218 int fX, fY;
220 setXY(fX, fY);
222 Imlib_Image cropImage = xContainer->createCropImage(fX, fY, width, height);
224 imlib_context_set_image(cropImage);
226 imlib_context_set_drawable(window);
227 imlib_render_image_on_drawable(0, 0);
229 Pixmap shapeMask;
230 imlib_render_pixmaps_for_whole_image (&pixmap, &shapeMask);
231 XSetWindowBackgroundPixmap (display, window, pixmap);
232 imlib_free_image();
234 imlib_context_set_drawable(xContainer->getWindow());
236 imlib_context_set_image(xContainer->bg->spareRoot);
238 // if the icon shadow is enabled and the shadow is visible underneath the
239 // text window
240 if (sIcon && //TODO Fix this
241 sIcon->getShadowX() > fX - sIcon->getWidth() &&
242 sIcon->getShadowY() > fY - sIcon->getHeight() &&
243 sIcon->getShadowX() < fX + sIcon->getWidth() &&
244 sIcon->getShadowY() < fY + sIcon->getHeight() )
245 sIcon->renderShadowToImage(pixmap, fX, fY);
247 updateText();
249 XMoveWindow(display, window, fX, fY );
251 //apply the buffer to the window
252 XSetFillStyle(display, gc, FillTiled);
253 XSetTile(display, gc, pixmap);
254 XFillRectangle(display, window, gc, 0, 0, width, height);
255 imlib_free_pixmap_and_mask(pixmap);
258 void XImlib2Caption::updateText()
260 XDesktopContainer * xContainer =
261 dynamic_cast<XDesktopContainer *>(AbstractImage::container);
263 XftDrawChange(fontDrawHandle, pixmap);
264 if (shadowOn)
265 XftDrawStringUtf8( fontDrawHandle, &shadowFontColor, font, FONTSHIFT/2 + shadowX, font->ascent + shadowY, (XftChar8*)text.c_str(), text.length() );
267 XftDrawStringUtf8(fontDrawHandle, &fontColor, font, FONTSHIFT/2, font->ascent, (XftChar8*)text.c_str(), text.length() );
270 void XImlib2Caption::lowerWindow()
272 XDesktopContainer * xContainer =
273 dynamic_cast<XDesktopContainer *>(AbstractImage::container);
275 XLowerWindow( xContainer->getDisplay(), window );
278 void XImlib2Caption::setXY(int & fX, int & fY)
280 //TODO fix pixel placement
281 if (captionPlacement == "Right")
282 fX = AbstractImage::iconParent->getX() + AbstractImage::iconParent->getWidth() + 10;
283 else if (captionPlacement == "Left")
284 fX = AbstractImage::iconParent->getX() - fontInfo.width - 10;
285 else //(captionPlacement == "Bottom" || captionPlacement == "Top")
286 fX = AbstractImage::iconParent->getX() +
287 (AbstractImage::iconParent->getWidth() - fontInfo.width)/2;
289 if (captionPlacement == "Left" || captionPlacement == "Right")
290 fY = AbstractImage::iconParent->getY() + (AbstractImage::iconParent->getHeight() - fontInfo.height)/2;
291 else if (captionPlacement == "Top")
292 fY = AbstractImage::iconParent->getY() - fontInfo.height - 2*FONTSHIFT;
293 else //(captionPlacement == "Bottom")
294 fY = AbstractImage::iconParent->getY() + AbstractImage::iconParent->getHeight() + FONTSHIFT;
297 int XImlib2Caption::getFontWidth()
299 return fontInfo.width;
302 int XImlib2Caption::getFontHeight()
304 return font->height;
306 void XImlib2Caption::mapWindow()
308 XDesktopContainer * xContainer =
309 dynamic_cast<XDesktopContainer *>(AbstractImage::container);
311 XMapWindow( xContainer->getDisplay(), window );
314 void XImlib2Caption::initMapWindow()
316 if (!captionOnHover)
317 mapWindow();
320 void XImlib2Caption::unmapWindow()
322 XDesktopContainer * xContainer =
323 dynamic_cast<XDesktopContainer *>(AbstractImage::container);
325 XUnmapWindow( xContainer->getDisplay(), window );
328 void XImlib2Caption::moveWindow(int xC, int yC)
330 XDesktopContainer * xContainer =
331 dynamic_cast<XDesktopContainer *>(AbstractImage::container);
332 setXY(xC, yC);
333 XMoveWindow( xContainer->getDisplay(), window, xC, yC );
336 void XImlib2Caption::mouseOverEffect()
338 if (captionOnHover)
339 mapWindow();
342 void XImlib2Caption::mouseOffEffect()
344 if (captionOnHover)
345 unmapWindow();