| 1 |
/* wtext.cxx |
| 2 |
$Id: wtext.cxx,v 1.9 1998/10/15 04:11:51 elf Exp $ |
| 3 |
|
| 4 |
written by Marc Singer |
| 5 |
22 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 |
Text edit and display control. |
| 32 |
|
| 33 |
*/ |
| 34 |
|
| 35 |
|
| 36 |
#include "standard.h" |
| 37 |
|
| 38 |
#include "ldisplay.h" |
| 39 |
#include "lwindow.h" |
| 40 |
#include "wtext.h" |
| 41 |
|
| 42 |
#include <X11/Xutil.h> |
| 43 |
|
| 44 |
//#include "wtext.h" |
| 45 |
#define PIX_MARGIN_LEFT 5 |
| 46 |
#define WIDTH_CARET 3 |
| 47 |
#define PIX_OFFSET_CARET 1 |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
#define EVENT_MASK ( ButtonPressMask \ |
| 52 |
| ButtonReleaseMask \ |
| 53 |
| ExposureMask \ |
| 54 |
| KeyPressMask \ |
| 55 |
| KeyReleaseMask \ |
| 56 |
| EnterWindowMask \ |
| 57 |
| LeaveWindowMask \ |
| 58 |
| FocusChangeMask ) |
| 59 |
|
| 60 |
EventMap g_rgEMText[] = { |
| 61 |
{ ButtonPress, (PFNEvent) &WTextEdit::buttondown }, |
| 62 |
{ ButtonRelease, (PFNEvent) &WTextEdit::buttonup }, |
| 63 |
{ Expose, (PFNEvent) &WTextEdit::expose }, |
| 64 |
{ CreateSelfNotify, (PFNEvent) &WTextEdit::createself }, |
| 65 |
{ EnterNotify, (PFNEvent) &WTextEdit::enter }, |
| 66 |
{ LeaveNotify, (PFNEvent) &WTextEdit::leave }, |
| 67 |
{ KeyPress, (PFNEvent) &WTextEdit::keypress }, |
| 68 |
{ KeyRelease, (PFNEvent) &WTextEdit::keyrelease }, |
| 69 |
{ FocusIn, (PFNEvent) &WTextEdit::focusin }, |
| 70 |
{ FocusOut, (PFNEvent) &WTextEdit::focusout }, |
| 71 |
{ 0, NULL }, |
| 72 |
}; |
| 73 |
|
| 74 |
void WTextEdit::buttondown (XButtonEvent* pEvent) |
| 75 |
{ |
| 76 |
} |
| 77 |
|
| 78 |
void WTextEdit::buttonup (XButtonEvent* pEvent) |
| 79 |
{ |
| 80 |
} |
| 81 |
|
| 82 |
void WTextEdit::createself (XoCreateSelfWindowEvent* /* pEvent */) |
| 83 |
{ |
| 84 |
m_sz = (char*) malloc (m_cchMax = 1024); |
| 85 |
m_cchLim = 0; |
| 86 |
*m_sz = 0; |
| 87 |
} |
| 88 |
|
| 89 |
void WTextEdit::enter (XEnterWindowEvent* pEvent) |
| 90 |
{ |
| 91 |
XFontStruct* pFont = m_pDisplay->find_font (m_fid); |
| 92 |
if (!pFont) |
| 93 |
return; |
| 94 |
|
| 95 |
m_fFocus = true; |
| 96 |
XClearArea (pEvent->display, pEvent->window, |
| 97 |
PIX_MARGIN_LEFT + m_widthString + PIX_OFFSET_CARET, |
| 98 |
(height () - pFont->ascent - pFont->descent)/2, |
| 99 |
WIDTH_CARET, pFont->ascent + pFont->descent, True); |
| 100 |
XGrabKeyboard (pEvent->display, pEvent->window, True, |
| 101 |
GrabModeAsync, GrabModeAsync, CurrentTime); |
| 102 |
} |
| 103 |
|
| 104 |
void WTextEdit::leave (XLeaveWindowEvent* pEvent) |
| 105 |
{ |
| 106 |
XFontStruct* pFont = m_pDisplay->find_font (m_fid); |
| 107 |
if (!pFont) |
| 108 |
return; |
| 109 |
|
| 110 |
m_fFocus = false; |
| 111 |
XClearArea (pEvent->display, pEvent->window, |
| 112 |
PIX_MARGIN_LEFT + m_widthString + PIX_OFFSET_CARET, |
| 113 |
(height () - pFont->ascent - pFont->descent)/2, |
| 114 |
WIDTH_CARET, pFont->ascent + pFont->descent, True); |
| 115 |
XUngrabKeyboard (pEvent->display, CurrentTime); |
| 116 |
} |
| 117 |
|
| 118 |
void WTextEdit::focusin (XFocusInEvent* pEvent) |
| 119 |
{ |
| 120 |
} |
| 121 |
|
| 122 |
void WTextEdit::focusout (XFocusOutEvent* pEvent) |
| 123 |
{ |
| 124 |
} |
| 125 |
|
| 126 |
void WTextEdit::expose (XExposeEvent* pEvent) |
| 127 |
{ |
| 128 |
GC gc = m_pDisplay->gc (); |
| 129 |
|
| 130 |
XSetState (pEvent->display, gc, m_foreground, m_background, |
| 131 |
GXcopy, AllPlanes); |
| 132 |
|
| 133 |
XRectangle rect = { static_cast<short int> (pEvent->x), |
| 134 |
static_cast<short int> (pEvent->y), |
| 135 |
static_cast<short unsigned> (pEvent->width), |
| 136 |
static_cast<short unsigned> (pEvent->height) }; |
| 137 |
XSetClipRectangles (pEvent->display, gc, 0, 0, &rect, 1, Unsorted); |
| 138 |
|
| 139 |
XDrawRectangle (pEvent->display, pEvent->window, gc, |
| 140 |
0, 0, |
| 141 |
width () - 1, height () - 1 |
| 142 |
); |
| 143 |
|
| 144 |
XFontStruct* pFont = m_pDisplay->find_font (m_fid); |
| 145 |
if (pFont) { |
| 146 |
int direction; |
| 147 |
int ascent; |
| 148 |
int descent; |
| 149 |
XCharStruct xchar; |
| 150 |
XSetFont (pEvent->display, gc, pFont->fid); |
| 151 |
XTextExtents (pFont, m_sz, m_cchLim, |
| 152 |
&direction, &ascent, &descent, &xchar); |
| 153 |
XDrawString (pEvent->display, pEvent->window, gc, |
| 154 |
PIX_MARGIN_LEFT, |
| 155 |
(height () - ascent - descent)/2 + ascent, |
| 156 |
m_sz, m_cchLim); |
| 157 |
|
| 158 |
if (m_fFocus) |
| 159 |
XCopyPlane (pEvent->display, m_pixmapCaret, pEvent->window, gc, |
| 160 |
0, 0, WIDTH_CARET, ascent + descent, |
| 161 |
PIX_MARGIN_LEFT + m_widthString + PIX_OFFSET_CARET, |
| 162 |
(height () - ascent - descent)/2, |
| 163 |
0x1); |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
void WTextEdit::keypress (XKeyPressedEvent* pEvent) |
| 169 |
{ |
| 170 |
KeySym keysym = XLookupKeysym (pEvent, 0); |
| 171 |
// printf ("keysym 0x%lx\n", (long) keysym); |
| 172 |
|
| 173 |
char sz[80]; |
| 174 |
memset (sz, 0, sizeof (sz)); |
| 175 |
XLookupString (pEvent, sz, sizeof (sz), &keysym, NULL); |
| 176 |
// printf ("string '%s' (0x%x)\n", sz, *sz); |
| 177 |
|
| 178 |
for (int i = 0; sz[i]; ++i) { |
| 179 |
if (sz[i] >= 0x20 && sz[i] <= 0x7e) |
| 180 |
m_sz[m_cchLim++] = sz[i]; |
| 181 |
else if (sz[i] == 0x8 && m_cchLim) |
| 182 |
--m_cchLim; |
| 183 |
} |
| 184 |
m_sz[m_cchLim] = 0; |
| 185 |
// printf ("'%s'\n", pInfo->sz); |
| 186 |
|
| 187 |
if (m_cchLim == 0 && m_widthString == 0) |
| 188 |
return; |
| 189 |
|
| 190 |
XFontStruct* pFont = m_pDisplay->find_font (m_fid); |
| 191 |
if (pFont) { |
| 192 |
int direction; |
| 193 |
int ascent; |
| 194 |
int descent; |
| 195 |
XCharStruct xchar; |
| 196 |
XTextExtents (pFont, m_sz, m_cchLim, |
| 197 |
&direction, &ascent, &descent, &xchar); |
| 198 |
if (m_widthString < xchar.width) |
| 199 |
XClearArea (pEvent->display, pEvent->window, |
| 200 |
PIX_MARGIN_LEFT + m_widthString, |
| 201 |
(height () - ascent - descent)/2, |
| 202 |
xchar.width - m_widthString |
| 203 |
+ WIDTH_CARET + PIX_OFFSET_CARET, |
| 204 |
ascent + descent, True); |
| 205 |
else |
| 206 |
XClearArea (pEvent->display, pEvent->window, |
| 207 |
PIX_MARGIN_LEFT + xchar.width, |
| 208 |
(height () - ascent - descent)/2, |
| 209 |
m_widthString - xchar.width |
| 210 |
+ WIDTH_CARET + PIX_OFFSET_CARET, |
| 211 |
ascent + descent, True); |
| 212 |
m_widthString = xchar.width; |
| 213 |
} |
| 214 |
|
| 215 |
// XClearArea (pEvent->display, pEvent->window, 0, 0, 0, 0, True); |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
void WTextEdit::keyrelease (XKeyReleasedEvent* /* pEvent */) |
| 220 |
{ |
| 221 |
|
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
void WTextEdit::register_template (LDisplay* pDisplay) |
| 226 |
{ |
| 227 |
WTextEdit* pWindow = new WTextEdit (pDisplay); |
| 228 |
pWindow->event_map (g_rgEMText); |
| 229 |
pWindow->select_events (EVENT_MASK); |
| 230 |
|
| 231 |
// -- Prepare instance data |
| 232 |
XrmValue value; |
| 233 |
pDisplay->find_resource ("xo.text.font", "Toolkit.Text.Font", |
| 234 |
NULL, &value); |
| 235 |
XFontStruct* pFont = pDisplay->find_font (value.addr); |
| 236 |
if (pFont) |
| 237 |
pWindow->m_fid = pFont->fid; |
| 238 |
pDisplay->find_resource ("xo.text.background", "Toolkit.Text.Background", |
| 239 |
NULL, &value); |
| 240 |
pDisplay->find_pixel (value.addr, &pWindow->m_background); |
| 241 |
|
| 242 |
pDisplay->find_resource ("xo.text.foreground", "Toolkit.Text.Foreground", |
| 243 |
NULL, &value); |
| 244 |
pDisplay->find_pixel (value.addr, &pWindow->m_foreground); |
| 245 |
|
| 246 |
pDisplay->find_resource ("xo.text.highlight", "Toolkit.Text.Highlight", |
| 247 |
NULL, &value); |
| 248 |
pDisplay->find_pixel (value.addr, &pWindow->m_highlight); |
| 249 |
|
| 250 |
pWindow->set_background_pixel (pWindow->m_background); |
| 251 |
|
| 252 |
{ |
| 253 |
int width = WIDTH_CARET; |
| 254 |
int height = pFont->ascent + pFont->descent; |
| 255 |
char rgb[128]; |
| 256 |
for (int i = 0; i < height; ++i) |
| 257 |
rgb[i] = (i == 0 || i == height - 1) ? 5 : 2; |
| 258 |
// rgb[i] = (i == 0 || i == height - 1) ? 5 : 0xff; |
| 259 |
pWindow->m_pixmapCaret |
| 260 |
= XCreateBitmapFromData (pDisplay->display (), |
| 261 |
XDefaultRootWindow (pDisplay->display ()), |
| 262 |
rgb, width, height); |
| 263 |
} |
| 264 |
|
| 265 |
pDisplay->hash_template ("text", pWindow); |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
void WTextEdit::text (const char* sz) |
| 271 |
{ |
| 272 |
if (!sz) { |
| 273 |
*m_sz = 0; |
| 274 |
return; |
| 275 |
} |
| 276 |
|
| 277 |
int cb = strlen (sz); |
| 278 |
if (cb > m_cchMax - 1) |
| 279 |
cb = m_cchMax - 1; |
| 280 |
memcpy (m_sz, sz, cb); |
| 281 |
m_sz[cb] = 0; |
| 282 |
m_cchLim = cb; |
| 283 |
|
| 284 |
XFontStruct* pFont = m_pDisplay->find_font (m_fid); |
| 285 |
if (pFont) { |
| 286 |
int direction; |
| 287 |
int ascent; |
| 288 |
int descent; |
| 289 |
XCharStruct xchar; |
| 290 |
XTextExtents (pFont, m_sz, m_cchLim, |
| 291 |
&direction, &ascent, &descent, &xchar); |
| 292 |
m_widthString = xchar.width; |
| 293 |
} |
| 294 |
} |