0.51.1 pre snapshot. Be careful, it may be buggy. It fixes some bugs though.
[wmaker-crm.git] / src / framewin.c
blob2c4e76ffc8d50fe9b1404e072313fd6422a8e740
1 /*
2 * Window Maker window manager
3 *
4 * Copyright (c) 1997, 1998 Alfredo K. Kojima
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 * USA.
22 #include "wconfig.h"
24 #include <X11/Xlib.h>
25 #include <X11/Xutil.h>
27 #include <stdlib.h>
28 #include <string.h>
30 #include <wraster.h>
32 #include "WindowMaker.h"
33 #include "GNUstep.h"
34 #include "texture.h"
35 #include "screen.h"
36 #include "wcore.h"
37 #include "framewin.h"
38 #include "stacking.h"
39 #include "funcs.h"
41 #define DBLCLICK_TIME wPreferences.dblclick_time
43 extern WPreferences wPreferences;
45 static void handleExpose(WObjDescriptor *desc, XEvent *event);
46 static void handleButtonExpose(WObjDescriptor *desc, XEvent *event);
48 static void buttonMouseDown(WObjDescriptor *desc, XEvent *event);
49 static void titlebarMouseDown(WObjDescriptor *desc, XEvent *event);
50 static void resizebarMouseDown(WObjDescriptor *desc, XEvent *event);
52 static void checkTitleSize(WFrameWindow *fwin);
55 static void paintButton(WCoreWindow *button, WTexture *texture,
56 unsigned long color, WPixmap *image, int pushed);
58 static void updateTitlebar(WFrameWindow *fwin);
61 WFrameWindow*
62 wFrameWindowCreate(WScreen *scr, int wlevel, int x, int y,
63 int width, int height, int flags,
64 WTexture **title_texture, WTexture **resize_texture,
65 unsigned long *color, GC *gc, WFont **font)
67 WFrameWindow *fwin;
69 fwin = wmalloc(sizeof(WFrameWindow));
70 memset(fwin, 0, sizeof(WFrameWindow));
72 fwin->screen_ptr = scr;
74 fwin->flags.single_texture = (flags & WFF_SINGLE_STATE) ? 1 : 0;
76 fwin->title_texture = title_texture;
77 fwin->resizebar_texture = resize_texture;
78 fwin->title_pixel = color;
79 fwin->title_gc = gc;
80 fwin->font = font;
82 fwin->core = wCoreCreateTopLevel(scr, x, y, width, height,
83 FRAME_BORDER_WIDTH);
84 if (wPreferences.use_saveunders) {
85 unsigned long vmask;
86 XSetWindowAttributes attribs;
88 vmask = CWSaveUnder;
89 attribs.save_under = True;
90 XChangeWindowAttributes(dpy, fwin->core->window, vmask, &attribs);
93 /* setup stacking information */
94 fwin->core->stacking = wmalloc(sizeof(WStacking));
95 fwin->core->stacking->above = NULL;
96 fwin->core->stacking->under = NULL;
97 fwin->core->stacking->child_of = NULL;
98 fwin->core->stacking->window_level = wlevel;
100 AddToStackList(fwin->core);
102 wFrameWindowUpdateBorders(fwin, flags);
104 return fwin;
109 void
110 wFrameWindowUpdateBorders(WFrameWindow *fwin, int flags)
112 int theight;
113 int bsize;
114 int width, height;
115 int i;
116 WScreen *scr = fwin->screen_ptr;
118 width = fwin->core->width;
119 if (flags & WFF_IS_SHADED)
120 height = -1;
121 else
122 height = fwin->core->height - fwin->top_width - fwin->bottom_width;
124 if (flags & WFF_TITLEBAR)
125 theight = (*fwin->font)->height + TITLEBAR_EXTRA_HEIGHT;
126 else
127 theight = 0;
129 if (wPreferences.new_style) {
130 bsize = theight;
131 } else {
132 bsize = theight - 7;
135 if (fwin->titlebar) {
136 /* if we had a titlebar and is requesting for one,
137 * check if the size has changed and resize it */
138 if (flags & WFF_TITLEBAR) {
139 fwin->top_width = theight;
141 fwin->flags.need_texture_remake = 1;
143 if (wPreferences.new_style) {
144 if (fwin->left_button) {
145 wCoreConfigure(fwin->left_button, 0, 0, bsize, bsize);
148 if (fwin->right_button) {
149 wCoreConfigure(fwin->right_button, width-bsize+1,
150 0, bsize, bsize);
152 } else { /* !new_style */
153 if (fwin->left_button) {
154 wCoreConfigure(fwin->left_button, 3, (theight-bsize)/2,
155 bsize, bsize);
158 if (fwin->right_button) {
159 wCoreConfigure(fwin->right_button, width-bsize-3,
160 (theight-bsize)/2, bsize, bsize);
163 updateTitlebar(fwin);
164 } else {
165 /* we had a titlebar, but now we don't need it anymore */
166 for (i=0; i < (fwin->flags.single_texture ? 1 : 3); i++) {
167 FREE_PIXMAP(fwin->title_back[i]);
168 if (wPreferences.new_style) {
169 FREE_PIXMAP(fwin->lbutton_back[i]);
170 FREE_PIXMAP(fwin->rbutton_back[i]);
173 if (fwin->left_button)
174 wCoreDestroy(fwin->left_button);
175 fwin->left_button = NULL;
177 if (fwin->right_button)
178 wCoreDestroy(fwin->right_button);
179 fwin->right_button = NULL;
181 wCoreDestroy(fwin->titlebar);
182 fwin->titlebar = NULL;
184 fwin->top_width = 0;
186 } else {
187 /* if we didn't have a titlebar and are being requested for
188 * one, create it */
189 if (flags & WFF_TITLEBAR) {
190 fwin->top_width = theight;
192 fwin->flags.titlebar = 1;
193 fwin->titlebar = wCoreCreate(fwin->core, 0, 0, width+1, theight);
195 if (flags & WFF_LEFT_BUTTON) {
196 fwin->flags.left_button = 1;
197 if (wPreferences.new_style) {
198 fwin->left_button = wCoreCreate(fwin->core, 0, 0,
199 bsize, bsize);
200 if (width < theight*4) {
201 fwin->flags.lbutton_dont_fit = 1;
202 } else {
203 XMapRaised(dpy, fwin->left_button->window);
205 } else {
206 fwin->left_button =
207 wCoreCreate(fwin->titlebar, 3, (theight-bsize)/2,
208 bsize, bsize);
210 XSetWindowBackground(dpy, fwin->left_button->window,
211 scr->widget_texture->normal.pixel);
213 if (width < theight*3) {
214 fwin->flags.lbutton_dont_fit = 1;
215 } else {
216 XMapRaised(dpy, fwin->left_button->window);
222 if (flags & WFF_RIGHT_BUTTON) {
223 fwin->flags.right_button = 1;
224 if (wPreferences.new_style) {
225 fwin->right_button =
226 wCoreCreate(fwin->core, width-bsize+1, 0,
227 bsize, bsize);
228 } else {
229 fwin->right_button =
230 wCoreCreate(fwin->titlebar, width-bsize-3,
231 (theight-bsize)/2, bsize, bsize);
232 XSetWindowBackground(dpy, fwin->right_button->window,
233 scr->widget_texture->normal.pixel);
236 if (width < theight*2) {
237 fwin->flags.rbutton_dont_fit = 1;
238 } else {
239 XMapRaised(dpy, fwin->right_button->window);
243 if (wPreferences.new_style)
244 updateTitlebar(fwin);
246 XMapRaised(dpy, fwin->titlebar->window);
248 fwin->flags.need_texture_remake = 1;
251 checkTitleSize(fwin);
253 if (flags & WFF_RESIZEBAR) {
254 fwin->bottom_width = RESIZEBAR_HEIGHT;
255 } else {
256 fwin->bottom_width = 0;
259 if (flags & WFF_RESIZEBAR) {
260 if (!fwin->resizebar) {
261 fwin->flags.resizebar = 1;
262 fwin->resizebar = wCoreCreate(fwin->core, 0,
263 height + fwin->top_width,
264 width, RESIZEBAR_HEIGHT);
265 fwin->resizebar_corner_width = RESIZEBAR_CORNER_WIDTH;
266 if (width < RESIZEBAR_CORNER_WIDTH*2 + RESIZEBAR_MIN_WIDTH) {
267 fwin->resizebar_corner_width = (width - RESIZEBAR_MIN_WIDTH)/2;
268 if (fwin->resizebar_corner_width < 0)
269 fwin->resizebar_corner_width = 0;
272 XMapWindow(dpy, fwin->resizebar->window);
273 XLowerWindow(dpy, fwin->resizebar->window);
275 fwin->flags.need_texture_remake = 1;
276 } else {
277 if (height+fwin->top_width+fwin->bottom_width != fwin->core->height) {
278 wCoreConfigure(fwin->resizebar, 0, height + fwin->top_width,
279 width, RESIZEBAR_HEIGHT);
282 } else {
283 if (fwin->resizebar) {
284 fwin->bottom_width = 0;
285 wCoreDestroy(fwin->resizebar);
286 fwin->resizebar = NULL;
290 if (height + fwin->top_width + fwin->bottom_width != fwin->core->height
291 && !(flags & WFF_IS_SHADED)) {
292 wFrameWindowResize(fwin, width,
293 height + fwin->top_width + fwin->bottom_width);
296 /* setup object descriptors */
298 if (fwin->titlebar) {
299 fwin->titlebar->descriptor.handle_expose = handleExpose;
300 fwin->titlebar->descriptor.parent = fwin;
301 fwin->titlebar->descriptor.parent_type = WCLASS_FRAME;
302 fwin->titlebar->descriptor.handle_mousedown = titlebarMouseDown;
305 if (fwin->resizebar) {
306 fwin->resizebar->descriptor.handle_expose = handleExpose;
307 fwin->resizebar->descriptor.parent = fwin;
308 fwin->resizebar->descriptor.parent_type = WCLASS_FRAME;
309 fwin->resizebar->descriptor.handle_mousedown = resizebarMouseDown;
312 if (fwin->left_button) {
313 fwin->left_button->descriptor.handle_expose = handleButtonExpose;
314 fwin->left_button->descriptor.parent = fwin;
315 fwin->left_button->descriptor.parent_type = WCLASS_FRAME;
316 fwin->left_button->descriptor.handle_mousedown = buttonMouseDown;
319 if (fwin->right_button) {
320 fwin->right_button->descriptor.parent = fwin;
321 fwin->right_button->descriptor.parent_type = WCLASS_FRAME;
322 fwin->right_button->descriptor.handle_expose = handleButtonExpose;
323 fwin->right_button->descriptor.handle_mousedown = buttonMouseDown;
326 checkTitleSize(fwin);
331 void
332 wFrameWindowDestroy(WFrameWindow *fwin)
334 int i;
336 if (fwin->left_button)
337 wCoreDestroy(fwin->left_button);
339 if (fwin->right_button)
340 wCoreDestroy(fwin->right_button);
342 if (fwin->resizebar)
343 wCoreDestroy(fwin->resizebar);
345 if (fwin->titlebar)
346 wCoreDestroy(fwin->titlebar);
348 RemoveFromStackList(fwin->core);
350 wCoreDestroy(fwin->core);
352 if (fwin->title)
353 free(fwin->title);
355 for (i=0; i < (fwin->flags.single_texture ? 1 : 3); i++) {
356 FREE_PIXMAP(fwin->title_back[i]);
357 if (wPreferences.new_style) {
358 FREE_PIXMAP(fwin->lbutton_back[i]);
359 FREE_PIXMAP(fwin->rbutton_back[i]);
363 free(fwin);
367 void
368 wFrameWindowChangeState(WFrameWindow *fwin, int state)
370 if (fwin->flags.state==state)
371 return;
373 fwin->flags.state = state;
374 fwin->flags.need_texture_change = 1;
376 wFrameWindowPaint(fwin);
380 static void
381 updateTitlebar(WFrameWindow *fwin)
383 int x, w;
384 int theight;
386 theight = (*fwin->font)->height + TITLEBAR_EXTRA_HEIGHT;
388 x = 0;
389 w = fwin->core->width + 1;
391 if (wPreferences.new_style) {
392 if (fwin->flags.hide_left_button || !fwin->left_button
393 || fwin->flags.lbutton_dont_fit) {
394 x = 0;
395 } else {
396 x = fwin->left_button->width;
397 w -= fwin->left_button->width;
401 if (wPreferences.new_style) {
402 if (!fwin->flags.hide_right_button && fwin->right_button
403 && !fwin->flags.rbutton_dont_fit) {
404 w -= fwin->right_button->width;
408 if (wPreferences.new_style || fwin->titlebar->width!=w)
409 fwin->flags.need_texture_remake = 1;
411 wCoreConfigure(fwin->titlebar, x, 0, w, theight);
415 void
416 wFrameWindowHideButton(WFrameWindow *fwin, int flags)
418 if ((flags & WFF_RIGHT_BUTTON) && fwin->right_button) {
419 XUnmapWindow(dpy, fwin->right_button->window);
420 fwin->flags.hide_right_button = 1;
423 if ((flags & WFF_LEFT_BUTTON) && fwin->left_button) {
424 XUnmapWindow(dpy, fwin->left_button->window);
425 fwin->flags.hide_left_button = 1;
428 if (fwin->titlebar) {
429 if (wPreferences.new_style) {
430 updateTitlebar(fwin);
431 } else {
432 XClearWindow(dpy, fwin->titlebar->window);
433 wFrameWindowPaint(fwin);
435 checkTitleSize(fwin);
440 void
441 wFrameWindowShowButton(WFrameWindow *fwin, int flags)
443 if ((flags & WFF_RIGHT_BUTTON) && fwin->right_button
444 && fwin->flags.hide_right_button) {
446 if (!fwin->flags.rbutton_dont_fit)
447 XMapWindow(dpy, fwin->right_button->window);
449 fwin->flags.hide_right_button = 0;
452 if ((flags & WFF_LEFT_BUTTON) && fwin->left_button
453 && fwin->flags.hide_left_button) {
455 if (!fwin->flags.lbutton_dont_fit)
456 XMapWindow(dpy, fwin->left_button->window);
458 fwin->flags.hide_left_button = 0;
462 if (fwin->titlebar) {
463 if (wPreferences.new_style) {
464 updateTitlebar(fwin);
465 } else {
466 XClearWindow(dpy, fwin->titlebar->window);
467 wFrameWindowPaint(fwin);
469 checkTitleSize(fwin);
474 static void
475 renderTexture(WScreen *scr, WTexture *texture, int width, int height,
476 int bwidth, int bheight, int left, int right,
477 Pixmap *title, Pixmap *lbutton, Pixmap *rbutton)
479 RImage *img;
480 RImage *limg, *rimg, *mimg;
481 int x, w;
483 *title = None;
484 *lbutton = None;
485 *rbutton = None;
487 img = wTextureRenderImage(texture, width, height, WREL_FLAT);
488 if (!img) {
489 wwarning(_("could not render gradient: %s"), RMessageForError(RErrorCode));
490 return;
493 if (wPreferences.new_style) {
494 if (left) {
495 limg = RGetSubImage(img, 0, 0, bwidth, bheight);
496 } else
497 limg = NULL;
499 x = 0;
500 w = img->width;
502 if (limg) {
503 RBevelImage(limg, RBEV_RAISED2);
504 if (!RConvertImage(scr->rcontext, limg, lbutton)) {
505 wwarning(_("error rendering image:%s"), RMessageForError(RErrorCode));
507 x += limg->width;
508 w -= limg->width;
509 RDestroyImage(limg);
512 if (right) {
513 rimg = RGetSubImage(img, width - bwidth, 0, bwidth, bheight);
514 } else
515 rimg = NULL;
517 if (rimg) {
518 RBevelImage(rimg, RBEV_RAISED2);
519 if (!RConvertImage(scr->rcontext, rimg, rbutton)) {
520 wwarning(_("error rendering image:%s"), RMessageForError(RErrorCode));
522 w -= rimg->width;
523 RDestroyImage(rimg);
526 if (w!=width) {
527 mimg = RGetSubImage(img, x, 0, w, img->height);
528 RBevelImage(mimg, RBEV_RAISED2);
530 if (!RConvertImage(scr->rcontext, mimg, title)) {
531 wwarning(_("error rendering image:%s"), RMessageForError(RErrorCode));
533 RDestroyImage(mimg);
534 } else {
535 RBevelImage(img, RBEV_RAISED2);
537 if (!RConvertImage(scr->rcontext, img, title)) {
538 wwarning(_("error rendering image:%s"), RMessageForError(RErrorCode));
541 } else {
542 RBevelImage(img, RBEV_RAISED2);
544 if (!RConvertImage(scr->rcontext, img, title)) {
545 wwarning(_("error rendering image:%s"), RMessageForError(RErrorCode));
549 RDestroyImage(img);
553 static void
554 updateTexture(WFrameWindow *fwin)
556 int i;
557 unsigned long pixel;
559 i = fwin->flags.state;
560 if (fwin->titlebar) {
561 if (fwin->title_texture[i]->any.type!=WTEX_SOLID) {
562 XSetWindowBackgroundPixmap(dpy, fwin->titlebar->window,
563 fwin->title_back[i]);
564 if (wPreferences.new_style) {
565 if (fwin->left_button && fwin->lbutton_back[i])
566 XSetWindowBackgroundPixmap(dpy, fwin->left_button->window,
567 fwin->lbutton_back[i]);
569 if (fwin->right_button && fwin->rbutton_back[i])
570 XSetWindowBackgroundPixmap(dpy, fwin->right_button->window,
571 fwin->rbutton_back[i]);
573 } else {
574 pixel = fwin->title_texture[i]->solid.normal.pixel;
575 XSetWindowBackground(dpy, fwin->titlebar->window, pixel);
576 if (wPreferences.new_style) {
577 if (fwin->left_button)
578 XSetWindowBackground(dpy, fwin->left_button->window,
579 pixel);
580 if (fwin->right_button)
581 XSetWindowBackground(dpy, fwin->right_button->window,
582 pixel);
585 XClearWindow(dpy, fwin->titlebar->window);
587 if (fwin->left_button) {
588 XClearWindow(dpy, fwin->left_button->window);
589 handleButtonExpose(&fwin->left_button->descriptor, NULL);
591 if (fwin->right_button) {
592 XClearWindow(dpy, fwin->right_button->window);
593 handleButtonExpose(&fwin->right_button->descriptor, NULL);
597 if (fwin->resizebar) {
598 XSetWindowBackground(dpy, fwin->resizebar->window,
599 fwin->resizebar_texture[0]->solid.normal.pixel);
600 XClearWindow(dpy, fwin->resizebar->window);
606 static void
607 remakeTexture(WFrameWindow *fwin, int state)
609 Pixmap pmap, lpmap, rpmap;
611 if (fwin->title_texture[state] && fwin->titlebar) {
612 FREE_PIXMAP(fwin->title_back[state]);
613 if (wPreferences.new_style) {
614 FREE_PIXMAP(fwin->lbutton_back[state]);
615 FREE_PIXMAP(fwin->rbutton_back[state]);
618 if (fwin->title_texture[state]->any.type!=WTEX_SOLID) {
619 int left, right;
620 int width;
622 /* eventually surrounded by if new_style */
623 left = fwin->left_button && !fwin->flags.hide_left_button
624 && !fwin->flags.lbutton_dont_fit;
625 right = fwin->right_button && !fwin->flags.hide_right_button
626 && !fwin->flags.rbutton_dont_fit;
628 width = fwin->core->width+1;
630 renderTexture(fwin->screen_ptr, fwin->title_texture[state],
631 width, fwin->titlebar->height,
632 fwin->titlebar->height, fwin->titlebar->height,
633 left, right, &pmap, &lpmap, &rpmap);
635 fwin->title_back[state] = pmap;
636 if (wPreferences.new_style) {
637 fwin->lbutton_back[state] = lpmap;
638 fwin->rbutton_back[state] = rpmap;
645 void
646 wFrameWindowPaint(WFrameWindow *fwin)
649 if (fwin->flags.is_client_window_frame)
650 fwin->flags.justification = wPreferences.title_justification;
652 if (fwin->flags.need_texture_remake) {
653 int i;
655 fwin->flags.need_texture_remake = 0;
656 fwin->flags.need_texture_change = 0;
658 if (fwin->flags.single_texture) {
659 remakeTexture(fwin, 0);
660 updateTexture(fwin);
661 } else {
662 /* first render the texture for the current state... */
663 remakeTexture(fwin, fwin->flags.state);
664 /* ... and paint it */
665 updateTexture(fwin);
667 for (i=0; i < 3; i++) {
668 if (i!=fwin->flags.state)
669 remakeTexture(fwin, i);
674 if (fwin->flags.need_texture_change) {
675 fwin->flags.need_texture_change = 0;
677 updateTexture(fwin);
680 if (fwin->titlebar && !fwin->flags.repaint_only_resizebar
681 && fwin->title_texture[fwin->flags.state]->any.type==WTEX_SOLID) {
682 wDrawBevel(fwin->titlebar->window, fwin->titlebar->width,
683 fwin->titlebar->height,
684 (WTexSolid*)fwin->title_texture[fwin->flags.state],
685 WREL_RAISED);
688 if (fwin->resizebar && !fwin->flags.repaint_only_titlebar) {
689 Window win;
690 int w, h;
691 int cw;
692 GC light_gc, dim_gc;
693 WTexSolid *texture = (WTexSolid*)fwin->resizebar_texture[0];
695 w = fwin->resizebar->width;
696 h = fwin->resizebar->height;
697 cw = fwin->resizebar_corner_width;
698 light_gc = texture->light_gc;
699 dim_gc = texture->dim_gc;
700 win = fwin->resizebar->window;
702 XDrawLine(dpy, win, dim_gc, 0, 0, w, 0);
703 XDrawLine(dpy, win, light_gc, 0, 1, w, 1);
705 XDrawLine(dpy, win, dim_gc, cw, 2, cw, h);
706 XDrawLine(dpy, win, light_gc, cw+1, 2, cw+1, h);
708 XDrawLine(dpy, win, dim_gc, w-cw-2, 2, w-cw-2, h);
709 XDrawLine(dpy, win, light_gc, w-cw-1, 2, w-cw-1, h);
711 #ifdef SHADOW_RESIZEBAR
712 XDrawLine(dpy, win, light_gc, 0, 1, 0, h-1);
713 XDrawLine(dpy, win, dim_gc, w-1, 2, w-1, h-1);
714 XDrawLine(dpy, win, dim_gc, 1, h-1, cw, h-1);
715 XDrawLine(dpy, win, dim_gc, cw+2, h-1, w-cw-2, h-1);
716 XDrawLine(dpy, win, dim_gc, w-cw, h-1, w-1, h-1);
717 #endif /* SHADOW_RESIZEBAR */
721 if (fwin->title && fwin->titlebar && !fwin->flags.repaint_only_resizebar) {
722 int x, w;
723 int lofs = 6, rofs = 6;
724 int titlelen;
725 char *title;
726 int allButtons = 1;
729 if (!wPreferences.new_style) {
730 if (fwin->left_button && !fwin->flags.hide_left_button
731 && !fwin->flags.lbutton_dont_fit)
732 lofs += fwin->left_button->width + 3;
733 else
734 allButtons = 0;
736 if (fwin->right_button && !fwin->flags.hide_right_button
737 && !fwin->flags.rbutton_dont_fit)
738 rofs += fwin->right_button->width + 3;
739 else
740 allButtons = 0;
742 title = ShrinkString(*fwin->font, fwin->title,
743 fwin->titlebar->width - lofs - rofs);
744 titlelen = strlen(title);
745 w = wTextWidth((*fwin->font)->font, title, titlelen);
747 switch (fwin->flags.justification) {
748 case WTJ_LEFT:
749 x = lofs;
750 break;
752 case WTJ_RIGHT:
753 x = fwin->titlebar->width - w - rofs;
754 break;
756 default:
757 if (!allButtons)
758 x = lofs + (fwin->titlebar->width - w - lofs - rofs) / 2;
759 else
760 x = (fwin->titlebar->width - w) / 2;
761 break;
764 #ifdef TITLE_TEXT_SHADOW
765 if(wPreferences.title_shadow){
766 int shadowx,shadowy;
767 XSetForeground(dpy, *fwin->title_gc,
768 fwin->title_pixel[fwin->flags.state+3]);
769 for(shadowx=0;shadowx<TITLE_TEXT_SHADOW_WIDTH;shadowx++)
770 for(shadowy=0;shadowy<TITLE_TEXT_SHADOW_HEIGHT;shadowy++)
771 wDrawString(fwin->titlebar->window, *fwin->font,
772 *fwin->title_gc,
773 x + shadowx + TITLE_TEXT_SHADOW_X_OFFSET,
774 (*fwin->font)->y + TITLEBAR_EXTRA_HEIGHT/2
775 + shadowy + TITLE_TEXT_SHADOW_Y_OFFSET, title,
776 titlelen);
778 #endif /* TITLE_TEXT_SHADOW */
780 XSetForeground(dpy, *fwin->title_gc,
781 fwin->title_pixel[fwin->flags.state]);
783 wDrawString(fwin->titlebar->window, *fwin->font,
784 *fwin->title_gc, x,
785 (*fwin->font)->y + TITLEBAR_EXTRA_HEIGHT/2, title,
786 titlelen);
788 free(title);
793 static void
794 reconfigure(WFrameWindow *fwin, int x, int y, int width, int height,
795 Bool dontMove)
797 int k = (wPreferences.new_style ? 4 : 3);
798 int resizedHorizontally = 0;
800 if (dontMove)
801 XResizeWindow(dpy, fwin->core->window, width, height);
802 else
803 XMoveResizeWindow(dpy, fwin->core->window, x, y, width, height);
806 if (fwin->core->height != height && fwin->resizebar)
807 XMoveWindow(dpy, fwin->resizebar->window, 0,
808 height - fwin->resizebar->height);
810 if (fwin->core->width != width) {
811 fwin->flags.need_texture_remake = 1;
812 resizedHorizontally = 1;
815 fwin->core->width = width;
816 fwin->core->height = height;
818 if (fwin->titlebar && resizedHorizontally) {
819 /* Check if the titlebar is wide enough to hold the buttons.
820 * Temporarily remove them if can't
822 if (fwin->left_button) {
823 if (width < fwin->top_width*k && !fwin->flags.lbutton_dont_fit) {
825 if (!fwin->flags.hide_left_button) {
826 XUnmapWindow(dpy, fwin->left_button->window);
828 fwin->flags.lbutton_dont_fit = 1;
829 } else if (width >= fwin->top_width*k && fwin->flags.lbutton_dont_fit) {
831 if (!fwin->flags.hide_left_button) {
832 XMapWindow(dpy, fwin->left_button->window);
834 fwin->flags.lbutton_dont_fit = 0;
838 if (fwin->right_button) {
839 if (width < fwin->top_width*2 && !fwin->flags.rbutton_dont_fit) {
841 if (!fwin->flags.hide_right_button) {
842 XUnmapWindow(dpy, fwin->right_button->window);
844 fwin->flags.rbutton_dont_fit = 1;
845 } else if (width >= fwin->top_width*2 && fwin->flags.rbutton_dont_fit) {
847 if (!fwin->flags.hide_right_button) {
848 XMapWindow(dpy, fwin->right_button->window);
850 fwin->flags.rbutton_dont_fit = 0;
854 if (wPreferences.new_style) {
855 if (fwin->right_button)
856 XMoveWindow(dpy, fwin->right_button->window,
857 width - fwin->right_button->width + 1, 0);
858 } else {
859 if (fwin->right_button)
860 XMoveWindow(dpy, fwin->right_button->window,
861 width - fwin->right_button->width - 3,
862 (fwin->titlebar->height - fwin->right_button->height)/2);
864 updateTitlebar(fwin);
865 checkTitleSize(fwin);
868 if (fwin->resizebar) {
869 wCoreConfigure(fwin->resizebar, 0,
870 fwin->core->height - fwin->resizebar->height,
871 fwin->core->width, fwin->resizebar->height);
873 fwin->resizebar_corner_width = RESIZEBAR_CORNER_WIDTH;
874 if (fwin->core->width < RESIZEBAR_CORNER_WIDTH*2 + RESIZEBAR_MIN_WIDTH) {
875 fwin->resizebar_corner_width = fwin->core->width/2;
880 void
881 wFrameWindowConfigure(WFrameWindow *fwin, int x, int y, int width, int height)
883 reconfigure(fwin, x, y, width, height, False);
886 void
887 wFrameWindowResize(WFrameWindow *fwin, int width, int height)
889 reconfigure(fwin, 0, 0, width, height, True);
894 int
895 wFrameWindowChangeTitle(WFrameWindow *fwin, char *new_title)
897 /* check if the title is the same as before */
898 if (fwin->title) {
899 if (new_title && (strcmp(fwin->title, new_title) == 0)) {
900 return 0;
902 } else {
903 if (!new_title)
904 return 0;
907 if (fwin->title)
908 free(fwin->title);
910 fwin->title = wstrdup(new_title);
912 if (fwin->titlebar) {
913 XClearWindow(dpy, fwin->titlebar->window);
915 wFrameWindowPaint(fwin);
917 checkTitleSize(fwin);
919 return 1;
923 #ifdef OLWM_HINTS
924 void
925 wFrameWindowUpdatePushButton(WFrameWindow *fwin, Bool pushed)
927 fwin->flags.right_button_pushed_in = pushed;
929 paintButton(fwin->right_button, fwin->title_texture[fwin->flags.state],
930 fwin->title_pixel[fwin->flags.state],
931 fwin->rbutton_image, pushed);
933 #endif /* OLWM_HINTS */
937 /*********************************************************************/
939 static void
940 handleExpose(WObjDescriptor *desc, XEvent *event)
942 WFrameWindow *fwin = (WFrameWindow*)desc->parent;
945 if (fwin->titlebar && fwin->titlebar->window == event->xexpose.window)
946 fwin->flags.repaint_only_titlebar = 1;
947 if (fwin->resizebar && fwin->resizebar->window == event->xexpose.window)
948 fwin->flags.repaint_only_resizebar = 1;
949 wFrameWindowPaint(fwin);
950 fwin->flags.repaint_only_titlebar = 0;
951 fwin->flags.repaint_only_resizebar = 0;
955 static void
956 checkTitleSize(WFrameWindow *fwin)
958 int width;
960 if (!fwin->title) {
961 fwin->flags.incomplete_title = 0;
962 return;
965 if (!fwin->titlebar) {
966 fwin->flags.incomplete_title = 1;
967 return;
968 } else {
969 width = fwin->titlebar->width - 6 - 6;
972 if (!wPreferences.new_style) {
973 if (fwin->left_button && !fwin->flags.hide_left_button
974 && !fwin->flags.lbutton_dont_fit)
975 width -= fwin->left_button->width + 3;
977 if (fwin->right_button && !fwin->flags.hide_right_button
978 && !fwin->flags.rbutton_dont_fit)
979 width -= fwin->right_button->width + 3;
981 if (wTextWidth((*fwin->font)->font, fwin->title,
982 strlen(fwin->title)) > width) {
983 fwin->flags.incomplete_title = 1;
984 } else {
985 fwin->flags.incomplete_title = 0;
990 static void
991 paintButton(WCoreWindow *button, WTexture *texture, unsigned long color,
992 WPixmap *image, int pushed)
994 WScreen *scr = button->screen_ptr;
995 GC copy_gc = scr->copy_gc;
996 int x=0, y=0, d=0;
997 int left=0, width=0;
999 /* setup stuff according to the state */
1000 if (pushed) {
1001 if (image) {
1002 if (image->width>=image->height*2) {
1003 /* the image contains 2 pictures: the second is for the
1004 * pushed state */
1005 width = image->width/2;
1006 left = image->width/2;
1007 } else {
1008 width = image->width;
1011 XSetClipMask(dpy, copy_gc, None);
1012 XSetForeground(dpy, copy_gc, scr->white_pixel);
1013 d=1;
1014 if (wPreferences.new_style) {
1015 XFillRectangle(dpy, button->window, copy_gc, 0, 0,
1016 button->width-1, button->height-1);
1017 XSetForeground(dpy, copy_gc, scr->black_pixel);
1018 XDrawRectangle(dpy, button->window, copy_gc, 0, 0,
1019 button->width-1, button->height-1);
1020 } else {
1021 XFillRectangle(dpy, button->window, copy_gc, 0, 0,
1022 button->width, button->height);
1023 XSetForeground(dpy, copy_gc, scr->black_pixel);
1024 XDrawRectangle(dpy, button->window, copy_gc, 0, 0,
1025 button->width, button->height);
1027 } else {
1028 XClearWindow(dpy, button->window);
1030 if (image) {
1031 if (image->width>=image->height*2)
1032 width = image->width/2;
1033 else
1034 width = image->width;
1036 d=0;
1038 if (wPreferences.new_style) {
1039 if (texture->any.type==WTEX_SOLID || pushed) {
1040 wDrawBevel(button->window, button->width, button->height,
1041 (WTexSolid*)texture, WREL_RAISED);
1043 } else {
1044 wDrawBevel(button->window, button->width, button->height,
1045 scr->widget_texture, WREL_RAISED);
1049 if (image) {
1050 /* display image */
1051 XSetClipMask(dpy, copy_gc, image->mask);
1052 x = (button->width - width)/2 + d;
1053 y = (button->height - image->height)/2 + d;
1054 XSetClipOrigin(dpy, copy_gc, x-left, y);
1055 if (!wPreferences.new_style) {
1056 XSetForeground(dpy, copy_gc, scr->black_pixel);
1057 if (!pushed) {
1058 if (image->depth==1)
1059 XCopyPlane(dpy, image->image, button->window, copy_gc,
1060 left, 0, width, image->height, x, y, 1);
1061 else
1062 XCopyArea(dpy, image->image, button->window, copy_gc,
1063 left, 0, width, image->height, x, y);
1064 } else {
1065 XSetForeground(dpy, copy_gc, scr->dark_pixel);
1066 XFillRectangle(dpy, button->window, copy_gc, 0, 0,
1067 button->width, button->height);
1069 } else {
1070 if (pushed) {
1071 XSetForeground(dpy, copy_gc, scr->black_pixel);
1072 } else {
1073 XSetForeground(dpy, copy_gc, color);
1074 XSetBackground(dpy, copy_gc, texture->any.color.pixel);
1076 XFillRectangle(dpy, button->window, copy_gc, 0, 0,
1077 button->width, button->height);
1083 static void
1084 handleButtonExpose(WObjDescriptor *desc, XEvent *event)
1086 WFrameWindow *fwin = (WFrameWindow*)desc->parent;
1087 WCoreWindow *button = (WCoreWindow*)desc->self;
1089 if (button == fwin->left_button) {
1090 paintButton(button, fwin->title_texture[fwin->flags.state],
1091 fwin->title_pixel[fwin->flags.state],
1092 fwin->lbutton_image, False);
1093 } else {
1094 Bool pushed = False;
1096 #ifdef OLWM_HINTS
1097 if (fwin->flags.right_button_pushed_in)
1098 pushed = True;
1099 #endif
1100 /* emulate the olwm pushpin in the "out" state */
1101 paintButton(button, fwin->title_texture[fwin->flags.state],
1102 fwin->title_pixel[fwin->flags.state],
1103 fwin->rbutton_image, pushed);
1108 static void
1109 titlebarMouseDown(WObjDescriptor *desc, XEvent *event)
1111 WFrameWindow *fwin = desc->parent;
1112 WCoreWindow *titlebar = desc->self;
1114 if (IsDoubleClick(fwin->core->screen_ptr, event)) {
1115 if (fwin->on_dblclick_titlebar)
1116 (*fwin->on_dblclick_titlebar)(titlebar, fwin->child, event);
1117 } else {
1118 if (fwin->on_mousedown_titlebar)
1119 (*fwin->on_mousedown_titlebar)(titlebar, fwin->child, event);
1124 static void
1125 resizebarMouseDown(WObjDescriptor *desc, XEvent *event)
1127 WFrameWindow *fwin = desc->parent;
1128 WCoreWindow *resizebar = desc->self;
1130 if (fwin->on_mousedown_resizebar)
1131 (*fwin->on_mousedown_resizebar)(resizebar, fwin->child, event);
1135 static void
1136 buttonMouseDown(WObjDescriptor *desc, XEvent *event)
1138 WFrameWindow *fwin = desc->parent;
1139 WCoreWindow *button = desc->self;
1140 WPixmap *image;
1141 XEvent ev;
1142 int done=0, execute=1;
1143 WTexture *texture;
1144 unsigned long pixel;
1145 int clickButton = event->xbutton.button;
1147 if (IsDoubleClick(fwin->core->screen_ptr, event)) {
1148 if (button == fwin->right_button && fwin->on_dblclick_right) {
1149 (*fwin->on_dblclick_right)(button, fwin->child, event);
1151 return;
1154 if (button == fwin->left_button) {
1155 image = fwin->lbutton_image;
1156 } else {
1157 image = fwin->rbutton_image;
1160 pixel = fwin->title_pixel[fwin->flags.state];
1161 texture = fwin->title_texture[fwin->flags.state];
1162 paintButton(button, texture, pixel, image, True);
1164 while (!done) {
1165 WMMaskEvent(dpy, LeaveWindowMask|EnterWindowMask|ButtonReleaseMask
1166 |ButtonPressMask|ExposureMask, &ev);
1167 switch (ev.type) {
1168 case LeaveNotify:
1169 execute = 0;
1170 paintButton(button, texture, pixel, image, False);
1171 break;
1173 case EnterNotify:
1174 execute = 1;
1175 paintButton(button, texture, pixel, image, True);
1176 break;
1178 case ButtonPress:
1179 break;
1181 case ButtonRelease:
1182 if (ev.xbutton.button == clickButton)
1183 done = 1;
1184 break;
1186 default:
1187 WMHandleEvent(&ev);
1190 paintButton(button, texture, pixel, image, False);
1192 if (execute) {
1193 if (button == fwin->left_button) {
1194 if (fwin->on_click_left)
1195 (*fwin->on_click_left)(button, fwin->child, &ev);
1196 } else if (button == fwin->right_button) {
1197 if (fwin->on_click_right)
1198 (*fwin->on_click_right)(button, fwin->child, &ev);