1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
|
/*
COPYRIGHT (C) 1994 - 2002 Cay S. Horstmann. All Rights Reserved.
NOTE TO STUDENTS: Do not attempt to study the contents of this file. You
can, and should, use the services provided in this file without knowing
the highly technical details of the implementation.
*/
#include <cmath>
#include <cstdlib>
#include "ccc_x11.h"
/* CONSTANTS ****************************************************************/
const double DEFAULT_XMIN = -10;
const double DEFAULT_YMIN = 10;
const double DEFAULT_XMAX = 10;
const double DEFAULT_YMAX = -10;
static int DEF_WIDTH = 480;
static int DEF_HEIGHT = 480;
// The following is an alias for an X fonts.
// Make sure this is available on your system. Substitute as necessary.
#define SYS_FONT_NAME "-adobe-courier-bold-r-normal-*-14-*-*-*-*-*-*"
/* Our program wrapper. */
extern int ccc_win_main();
GraphicWindow cwin;
/**************************************************************************/
GraphicWindow::GraphicWindow()
: _user_xmin(DEFAULT_XMIN), _user_ymin(DEFAULT_YMIN),
_user_xmax(DEFAULT_XMAX), _user_ymax(DEFAULT_YMAX),
_disp_xmax(DEF_WIDTH), _disp_ymax(DEF_HEIGHT)
{
}
void GraphicWindow::open(Display* d, Window w)
{
display = d;
win = w;
screen_num = DefaultScreen(display);
XGCValues xgc_values; // ignored
xgc = XCreateGC(display, win, 0, &xgc_values);
// specify black foreground since default window background is
// white and default foreground is undefined.
XSetForeground(display, xgc, BlackPixel(display, screen_num));
// set line attributes. This class only uses rounded styles.
XSetLineAttributes(display, xgc, 2 /* pen thickness */, LineSolid,
CapRound, JoinRound);
// set up the font - uses only 14pt bold Courier
_fontinfo_ptr = XLoadQueryFont(display, SYS_FONT_NAME);
if (_fontinfo_ptr == NULL)
{ cerr << "Unable to initialize font: " << SYS_FONT_NAME << endl;
exit(1);
}
XSetFont(display, xgc, _fontinfo_ptr->fid);
// sets the rop mode, using Copy for the user prompts
XSetFunction(display, xgc, GXcopy /*0x3 COPY*/);
_ppm = XCreatePixmap(display, win, DEF_WIDTH, DEF_HEIGHT,
DefaultDepth(display, screen_num));
clear();
}
void GraphicWindow::close()
{
XFreePixmap(display, _ppm);
XFreeFont(display, _fontinfo_ptr);
}
void GraphicWindow::repaint()
{
XCopyArea(display, _ppm, win, xgc, 0, 0, DEF_WIDTH, DEF_HEIGHT, 0, 0);
}
void GraphicWindow::clear()
{
XClearWindow(display, win);
XGCValues vals;
XGetGCValues(display, xgc, GCForeground, &vals);
unsigned long color = WhitePixel(display, screen_num);
unsigned long fg = vals.foreground;
XSetForeground(display, xgc, color);
XFillRectangle(display, _ppm, xgc, 0, 0, DEF_WIDTH + 1, DEF_HEIGHT + 1);
XSetForeground(display, xgc, fg);
}
void GraphicWindow::coord(double xmin, double ymin,
double xmax, double ymax)
{
_user_xmin = xmin;
_user_xmax = xmax;
_user_ymin = ymin;
_user_ymax = ymax;
}
int GraphicWindow::user_to_disp_x(double x) const
{
return (int) ((x - _user_xmin) * _disp_xmax / (_user_xmax - _user_xmin));
}
int GraphicWindow::user_to_disp_y(double y) const
{
return (int) ((y - _user_ymin) * _disp_ymax / (_user_ymax - _user_ymin));
}
int GraphicWindow::user_to_disp_dx(double x) const
{
return (int) (x * _disp_xmax / (_user_xmax - _user_xmin));
}
int GraphicWindow::user_to_disp_dy(double y) const
{
return (int) (y * _disp_ymax / (_user_ymax - _user_ymin));
}
double GraphicWindow::disp_to_user_x(int x) const
{
return (double)x * (_user_xmax - _user_xmin) / _disp_xmax + _user_xmin;
}
double GraphicWindow::disp_to_user_y(int y) const
{
return (double)y * (_user_ymax - _user_ymin) / _disp_ymax + _user_ymin;
}
void GraphicWindow::point(double x, double y)
{
const int POINT_RADIUS = 3;
int disp_x = user_to_disp_x(x);
int disp_y = user_to_disp_y(y);
XSetForeground(display, xgc, BlackPixel(display, screen_num));
XDrawArc(display, win, xgc, disp_x - POINT_RADIUS, disp_y - POINT_RADIUS,
2 * POINT_RADIUS, 2 * POINT_RADIUS, 0 , 360 * 64);
XDrawArc(display, _ppm, xgc, disp_x - POINT_RADIUS,
disp_y - POINT_RADIUS, 2 * POINT_RADIUS, 2 * POINT_RADIUS, 0 , 360 * 64);
}
void GraphicWindow::ellipse(double x, double y, double ra, double rb)
{
int disp_x = user_to_disp_x(x);
int disp_y = user_to_disp_y(y);
int disp_rx = abs(user_to_disp_dx(ra));
int disp_ry = abs(user_to_disp_dy(rb));
XSetForeground(display, xgc, BlackPixel(display, screen_num));
XDrawArc(display, win,
xgc, // the drawable
disp_x - disp_rx, // the x coord of upperleft corner of bounding rect
disp_y - disp_ry, // the y coord of upperleft corner of bounding rect
disp_rx * 2, // the major axis length
disp_ry * 2, // the minor axis length
0, // offset from 3 o'clock
360 * 64); // complete circle (360 degrees * 64 clicks per degree)
XDrawArc(display, _ppm, xgc, disp_x - disp_rx,
disp_y - disp_ry, disp_rx * 2, disp_ry * 2, 0, 360 * 64);
}
void GraphicWindow::line(double xfrom, double yfrom, double xto,
double yto)
{
XSetForeground(display, xgc, BlackPixel(display, screen_num));
XDrawLine(display, win, xgc, user_to_disp_x(xfrom), user_to_disp_y(yfrom),
user_to_disp_x(xto), user_to_disp_y(yto));
XDrawLine(display, _ppm, xgc, user_to_disp_x(xfrom),
user_to_disp_y(yfrom), user_to_disp_x(xto), user_to_disp_y(yto));
}
void GraphicWindow::text(const char t[], double x, double y)
{
int direction, ascent, descent;
XCharStruct overall;
XTextExtents(_fontinfo_ptr, t, strlen(t), &direction, &ascent, &descent, &overall);
int disp_x = user_to_disp_x(x);
int disp_y = user_to_disp_y(y) + ascent;
XSetForeground(display, xgc, BlackPixel(display, screen_num));
XDrawString(display, win, xgc, disp_x, disp_y, t, strlen(t));
XDrawString(display, _ppm, xgc, disp_x, disp_y,
t, strlen(t));
}
GraphicWindow& GraphicWindow::operator<<(Point p)
{
point(p.get_x(), p.get_y());
return *this;
}
GraphicWindow& GraphicWindow::operator<<(Circle c)
{
ellipse (c.get_center().get_x(),
c.get_center().get_y(),
c.get_radius(),
c.get_radius());
return *this;
}
GraphicWindow& GraphicWindow::operator<<(Message m)
{
_display_string = m.get_text();
text(_display_string.c_str(), m.get_start().get_x(), m.get_start().get_y());
return *this;
}
GraphicWindow& GraphicWindow::operator<< (Line l)
{
line(l.get_start().get_x(), l.get_start().get_y(),
l.get_end().get_x(), l.get_end().get_y());
return *this;
}
void GraphicWindow::statusline_prompt(string s)
{
int direction, ascent, descent;
XCharStruct overall;
const char* t = s.c_str();
XTextExtents(_fontinfo_ptr, t, strlen(t),
&direction, &ascent, &descent, &overall);
XSetBackground(display, xgc, WhitePixel(display, screen_num));
XClearArea(display, win,
0,
0, /*clear from top*/
0 /*clear to right edge of screen*/,
ascent + descent,
0 /* generate exposure events */);
XDrawImageString(display, win, xgc, 0, ascent, t, strlen(t));
XDrawImageString(display, _ppm, xgc, 0, ascent, t, strlen(t));
}
Point GraphicWindow::get_mouse(string outstr)
{
XEvent report;
Point p;
int x, y;
cwin.statusline_prompt(outstr);
// Event Loop
while (true)
{
XNextEvent(display, &report);
switch(report.type)
{
case Expose:
cwin.repaint();
break;
case ButtonPress:
if (report.xbutton.button == Button1)
{
x = report.xbutton.x;
y = report.xbutton.y;
p = Point(cwin.disp_to_user_x(x), cwin.disp_to_user_y(y));
cwin.statusline_prompt(outstr);
return p;
}
}
}
return p; // to keep compiler happy, will never get here!
}
/***************************************************************************/
int GraphicWindow::put_string(const char instr[], int str_x, int curr_x)
{
int direction, ascent, descent;
XCharStruct overall;
XTextExtents(_fontinfo_ptr, instr, strlen(instr), &direction, &ascent, &descent, &overall);
XClearArea(display, win,
str_x - 1,
0, /*clear from top*/
curr_x + 1, /*clear to right edge of screen*/
ascent + descent + 1,
0 /* generate exposure events */);
XSetForeground(display, xgc, BlackPixel(display, screen_num));
XDrawImageString(display, win, xgc, str_x, ascent, instr, strlen(instr));
curr_x = str_x + overall.width;
/* redraw cursor */
XDrawLine(display, win, xgc, curr_x, 0, curr_x, ascent + descent);
return curr_x;
}
/***************************************************************************/
string GraphicWindow::get_string(string outstr)
{
XEvent report;
string instring;
char instr[50];
char buffer[3];
int buffsize = 3;
KeySym keysym;
XComposeStatus compose;
int count, length;
int str_x; // initial x position, at the end of the prompt string
int str_y; // initial yposition
int curr_x; // current x position (measured from str_x)
int direction; // draw chars l->r
int ascent; // above font's baseline
int descent; // below font's baseline
XCharStruct overall;
_display_string=outstr;
statusline_prompt(outstr);
XTextExtents(_fontinfo_ptr, _display_string.c_str(),
_display_string.length(), &direction, &ascent, &descent, &overall);
str_x = overall.width;
curr_x = overall.width;
str_y = ascent; // set initial cursor position
instr[0] = '\0';
curr_x = put_string(instr, str_x, curr_x);
//Event loop
while(1)
{
XNextEvent(display, &report);
switch (report.type)
{
case Expose:
cwin.repaint();
break;
case KeyPress:
/* get characters until carriage return */
count = XLookupString(&(report.xkey), buffer,
buffsize, &keysym, &compose);
/* got a carriage return */
if ((keysym == XK_Return) || (keysym == XK_KP_Enter)
|| (keysym == XK_Linefeed))
{
XClearArea(display, win,
0,
0, /*clear from top*/
0, /*clear to right edge of screen*/
ascent + descent + 1,
0 /* generate expoure events */);
instring = instr;
return instring;
}
/* eat white space */
else if (((keysym >= XK_KP_Space) && (keysym <= XK_KP_9))
|| ((keysym >= XK_space) && (keysym <= XK_asciitilde)))
{
if ((strlen(instr) + strlen(buffer)) >= 50)
XBell(display, 100);
else
{
buffer[1] = '\0';
strcat (instr, buffer);
}
}
/* got a delete key */
else if ((keysym == XK_BackSpace) || (keysym == XK_Delete))
{
if ((length = strlen(instr)) > 0)
{
// resize the string
instr[length-1] = '\0';
}
else
XBell(display, 100);
}
/* display the new string, and reposition cursor */
curr_x = put_string(instr, str_x, curr_x);
}
}
return instring; // never get here but it keeps compiler happy :)!
}
int GraphicWindow::get_int(const string& out_string)
{
return atoi(get_string(out_string).c_str());
}
double GraphicWindow::get_double(const string& out_string)
{
return atof(get_string(out_string).c_str());
}
#define BITMAPDEPTH 1
#define TOO_SMALL 0
#define BIG_ENOUGH 1
/***************************************************************************/
int main(int argc, char** argv)
{
// The usual X overhead...
unsigned int width, height; // window size
unsigned int border_width = 4;
char* window_name = argv[0];
char* icon_name = "GCT";
Pixmap icon_pixmap;
XSizeHints* size_hints;
XWMHints* wm_hints;
XClassHint* class_hints;
XTextProperty windowName;
XTextProperty iconName;
XEvent report;
int window_size_ok = 1;
char* display_name = 0;
char* progname = argv[0];
Window win;
Display* display;
int screen_num;
// check allocation of hints
if (!(size_hints = XAllocSizeHints())
|| !(wm_hints = XAllocWMHints())
|| !(class_hints = XAllocClassHint()))
{
cerr << progname << " error: failure allocating memory" << endl;
exit(-1);
}
progname = argv[0];
// connect to X server
display = XOpenDisplay(display_name);
if (display == NULL)
{
cerr << progname << " error: can't connect to server " <<
XDisplayName(display_name) << endl;
exit(-1);
}
// get screen size from display structure macro
screen_num = DefaultScreen(display);
win = XCreateSimpleWindow(display, RootWindow(display, screen_num),
0, 0, DEF_WIDTH, DEF_HEIGHT, border_width, BlackPixel(display,
screen_num), WhitePixel(display, screen_num));
if (XStringListToTextProperty(&window_name, 1, &windowName) == 0)
{
cerr << progname
<< " error: structure allocation for windowName failed."
<< endl;
exit(-1);
}
if (XStringListToTextProperty(&icon_name, 1, &iconName) == 0)
{
cerr << progname
<< " error: structure allocation for iconName failed."
<< endl;
exit(-1);
}
size_hints->flags = PPosition | PSize | PMinSize;
size_hints->min_width = DEF_WIDTH;
size_hints->min_height = DEF_HEIGHT;
wm_hints->initial_state = NormalState;
wm_hints->input = True;
wm_hints->icon_pixmap = icon_pixmap;
wm_hints->flags = StateHint | IconPixmapHint | InputHint;
class_hints->res_name = progname;
class_hints->res_class = "chigcx";
XSetWMProperties(display, win, &windowName, &iconName, argv, argc,
size_hints, wm_hints, class_hints);
// Select desired event types
XSelectInput(display, win, ExposureMask | KeyPressMask
| ButtonPressMask);
// Highly intuitive way to state that we can process "delete window"
Atom delete_atom = XInternAtom(display, "WM_DELETE_WINDOW", false);
XSetWMProtocols(display, win, &delete_atom, 1);
XMapWindow(display, win);
cwin.open(display, win);
// Event loop
int draw_flag = 1;
while (true)
{
XNextEvent(display, &report);
switch ((int)report.type)
{
case Expose:
if (report.xexpose.count == 0)
{
if (window_size_ok)
{
if (draw_flag)
{
ccc_win_main();
draw_flag = 0;
}
else
cwin.repaint();
}
}
break;
case ConfigureNotify:
/*
window has been resized, change width and
height to send to draw_text and draw_graphics
in next Expose
*/
width = report.xconfigure.width;
height = report.xconfigure.height;
window_size_ok = (width >= size_hints->min_width)
&& (height >= size_hints->min_height);
break;
case ClientMessage:
if (report.xclient.data.l[0] == delete_atom)
{
cwin.close();
exit(0);
}
break;
case ButtonPress:
case KeyPress:
default:
break;
}
}
return 0;
} |
Partager