| 1 |
/* wbutton.cxx |
| 2 |
$Id: wbutton.cxx,v 1.14 1998/10/15 04:11:51 elf Exp $ |
| 3 |
|
| 4 |
written by Marc Singer |
| 5 |
9 May 1997 |
| 6 |
|
| 7 |
This file is part of the project XO. See the file README for |
| 8 |
more information. |
| 9 |
|
| 10 |
Copyright (C) 1997 Marc Singer |
| 11 |
|
| 12 |
This program is free software; you can redistribute it and/or |
| 13 |
modify it under the terms of the GNU General Public License as |
| 14 |
published by the Free Software Foundation; either version 2 of the |
| 15 |
License, or (at your option) any later version. |
| 16 |
|
| 17 |
This program is distributed in the hope that it will be useful, but |
| 18 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 20 |
General Public License for more details. |
| 21 |
|
| 22 |
You should have received a copy of the GNU General Public License |
| 23 |
in a file called COPYING along with this program; if not, write to |
| 24 |
the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA |
| 25 |
02139, USA. |
| 26 |
|
| 27 |
----------- |
| 28 |
DESCRIPTION |
| 29 |
----------- |
| 30 |
|
| 31 |
Implementation of a button window class. |
| 32 |
|
| 33 |
*/ |
| 34 |
|
| 35 |
#include "standard.h" |
| 36 |
|
| 37 |
|
| 38 |
#include "ldisplay.h" |
| 39 |
#include "lwindow.h" |
| 40 |
//#include "lwindowclass.h" |
| 41 |
|
| 42 |
#include "wbutton.h" |
| 43 |
|
| 44 |
|
| 45 |
#define EVENT_MASK ( ButtonPressMask \ |
| 46 |
| ButtonReleaseMask \ |
| 47 |
| ExposureMask \ |
| 48 |
| EnterWindowMask \ |
| 49 |
| LeaveWindowMask) |
| 50 |
|
| 51 |
EventMap g_rgEMButton[] = { |
| 52 |
{ ButtonPress, (PFNEvent) &WButton::buttondown }, |
| 53 |
{ ButtonRelease, (PFNEvent) &WButton::buttonup }, |
| 54 |
{ Expose, (PFNEvent) &WButton::expose }, |
| 55 |
// { CreateSelfNotify, (PFNEvent) &WButton::createself }, |
| 56 |
{ EnterNotify, (PFNEvent) &WButton::enter }, |
| 57 |
{ LeaveNotify, (PFNEvent) &WButton::leave }, |
| 58 |
{ 0, NULL }, |
| 59 |
}; |
| 60 |
|
| 61 |
void WButton::buttondown (XButtonEvent* pEvent) |
| 62 |
{ |
| 63 |
m_fPress = true; |
| 64 |
m_fOutOfWindow = false; |
| 65 |
|
| 66 |
XClearArea (pEvent->display, pEvent->window, 0, 0, 0, 0, True); |
| 67 |
} |
| 68 |
|
| 69 |
void WButton::buttonup (XButtonEvent* pEvent) |
| 70 |
{ |
| 71 |
m_fPress = false; |
| 72 |
if (!m_fOutOfWindow) { |
| 73 |
XClearArea (pEvent->display, pEvent->window, 0, 0, 0, 0, True); |
| 74 |
notify (ButtonPressNotify); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
void WButton::createself (XoCreateSelfWindowEvent* /* pEvent */) |
| 79 |
{ |
| 80 |
// fprintf (stderr, "create button\n"); |
| 81 |
} |
| 82 |
|
| 83 |
void WButton::enter (XEnterWindowEvent* pEvent) |
| 84 |
{ |
| 85 |
if (!m_fPress) |
| 86 |
return; |
| 87 |
m_fOutOfWindow = false; |
| 88 |
|
| 89 |
XClearArea (pEvent->display, pEvent->window, 0, 0, 0, 0, True); |
| 90 |
} |
| 91 |
|
| 92 |
void WButton::leave (XLeaveWindowEvent* pEvent) |
| 93 |
{ |
| 94 |
if (!m_fPress) |
| 95 |
return; |
| 96 |
m_fOutOfWindow = true; |
| 97 |
|
| 98 |
XClearArea (pEvent->display, pEvent->window, 0, 0, 0, 0, True); |
| 99 |
} |
| 100 |
|
| 101 |
void WButton::expose (XExposeEvent* pEvent) |
| 102 |
{ |
| 103 |
GC gc = m_pDisplay->gc (); |
| 104 |
|
| 105 |
bool fDown = m_fPress && !m_fOutOfWindow; |
| 106 |
int colorFG = fDown ? m_highlight : m_foreground; |
| 107 |
int colorBG = fDown ? m_foreground : m_highlight; |
| 108 |
|
| 109 |
XSetState (pEvent->display, gc, colorFG, colorBG, GXcopy, AllPlanes); |
| 110 |
|
| 111 |
XDrawRectangle (pEvent->display, pEvent->window, gc, |
| 112 |
0, 0, |
| 113 |
width () - 1, height () - 1 |
| 114 |
); |
| 115 |
|
| 116 |
XFontStruct* pFont = display ()->find_font (m_fid); |
| 117 |
if (pFont) { |
| 118 |
const char* szTitle = text (); |
| 119 |
if (!szTitle) |
| 120 |
szTitle = "Pressing"; |
| 121 |
int cchTitle = strlen (szTitle); |
| 122 |
int direction; |
| 123 |
int ascent; |
| 124 |
int descent; |
| 125 |
XCharStruct xchar; |
| 126 |
XSetFont (pEvent->display, gc, pFont->fid); |
| 127 |
// fprintf (stderr, "fontID 0x%x\n", pFont->fid); |
| 128 |
XTextExtents (pFont, szTitle, cchTitle, |
| 129 |
&direction, &ascent, &descent, &xchar); |
| 130 |
XDrawString (pEvent->display, pEvent->window, gc, |
| 131 |
(width () - xchar.width)/2, |
| 132 |
(height () - ascent - descent)/2 + ascent, |
| 133 |
szTitle, cchTitle); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
void WButton::register_template (LDisplay* pDisplay) |
| 139 |
{ |
| 140 |
WButton* pWindow = new WButton (pDisplay); |
| 141 |
pWindow->event_map (g_rgEMButton); |
| 142 |
pWindow->select_events (EVENT_MASK); |
| 143 |
|
| 144 |
// -- Prepare instance data |
| 145 |
XrmValue value; |
| 146 |
pDisplay->find_resource ("xo.button.font", "Toolkit.Button.Font", |
| 147 |
NULL, &value); |
| 148 |
XFontStruct* pFont = pDisplay->find_font (value.addr); |
| 149 |
if (pFont) |
| 150 |
pWindow->m_fid = pFont->fid; |
| 151 |
pDisplay->find_resource ("xo.button.background", "Toolkit.Button.Background", |
| 152 |
NULL, &value); |
| 153 |
pDisplay->find_pixel (value.addr, &pWindow->m_background); |
| 154 |
|
| 155 |
pDisplay->find_resource ("xo.button.foreground", "Toolkit.Button.Foreground", |
| 156 |
NULL, &value); |
| 157 |
pDisplay->find_pixel (value.addr, &pWindow->m_foreground); |
| 158 |
|
| 159 |
pDisplay->find_resource ("xo.button.highlight", "Toolkit.Button.Highlight", |
| 160 |
NULL, &value); |
| 161 |
pDisplay->find_pixel (value.addr, &pWindow->m_highlight); |
| 162 |
|
| 163 |
pWindow->set_background_pixel (pWindow->m_background); |
| 164 |
|
| 165 |
pDisplay->hash_template ("button", pWindow); |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
|