home *** CD-ROM | disk | FTP | other *** search
/ Photo CD Demo 1 / Demo.bin / graphtal / x11_wndw.c < prev    next >
C/C++ Source or Header  |  1992-10-20  |  4KB  |  158 lines

  1. /*
  2.  * X11_Window.C - X11 window driver.
  3.  *
  4.  * Copyright (C) 1992, Christoph Streit (streit@iam.unibe.ch)
  5.  * All rights reserved.
  6.  *
  7.  * This software may be freely copied, modified, and redistributed
  8.  * provided that this copyright notice is preserved on all copies.
  9.  *
  10.  * You may not distribute this software, in whole or in part, as part of
  11.  * any commercial product without the express consent of the authors.
  12.  *
  13.  * There is no warranty or other guarantee of fitness of this software
  14.  * for any purpose.  It is provided solely "as is".
  15.  *
  16.  */
  17.  
  18. #include "X11_Window.h"
  19. #include "Error.h"
  20.  
  21. #include "X11_Window_icon"
  22.  
  23. //___________________________________________________________ X11_Window
  24.  
  25. X11_Window::X11_Window()
  26. : display_name(NULL), segments(NULL), RequestSize(10), segmentsInBuffer(0)
  27. {}
  28.  
  29. void X11_Window::open(int width, int height, const rcString& windowName)
  30. {
  31.   BaseWindow::open(width, height, windowName);
  32.   
  33.   if ((display = XOpenDisplay(display_name)) == NULL) 
  34.     Error(ERR_PANIC, rcString("cannot connect to X server ") 
  35.                  + XDisplayName(display_name));
  36.  
  37.   /*
  38.    * Buffering of XSegments is done to achieve better preformance.
  39.    * Check maximal request size of display server.
  40.    */
  41.   RequestSize = (XMaxRequestSize(display) < 10000) 
  42.                  ? XMaxRequestSize(display) : 10000;
  43.   segments = new XSegment[RequestSize];
  44.  
  45.   int screen = DefaultScreen(display);
  46.   win = XCreateSimpleWindow(display, RootWindow(display, screen),
  47.                 0, 0, resX, resY, 1, 
  48.                 BlackPixel(display, screen), 
  49.                 WhitePixel(display, screen));
  50.  
  51.   Pixmap iconPixmap = XCreateBitmapFromData(display, win,
  52.                         X11_Window_icon_bits,
  53.                         X11_Window_icon_width,
  54.                         X11_Window_icon_height);
  55.  
  56.   gc = XCreateGC(display, win, 0, NULL);
  57.   XSetForeground(display, gc, BlackPixel(display, screen));
  58.  
  59.   XSizeHints hints;
  60.   hints.flags = PSize | PMinSize | PMaxSize;
  61.   hints.width = resX; hints.height = resY;
  62.   hints.min_width = resX; hints.min_height = resY;
  63.   hints.max_width = resX; hints.max_height = resY;
  64.   XSetStandardProperties(display, win,
  65.              (char*) windowName.chars(), (char*) windowName.chars(), 
  66.              iconPixmap, 0, 0, &hints);
  67.  
  68.   XSelectInput(display, win, ExposureMask | KeyPressMask);
  69.   XMapWindow(display, win);
  70.  
  71.   /*
  72.    * Wait for window to come up.
  73.    */
  74.   XEvent report;
  75.   while(1) {
  76.     XNextEvent(display, &report);
  77.     if (report.type == Expose)
  78.       if (report.xexpose.count == 0)
  79.     break;
  80.   }
  81.  
  82.   XSetWindowAttributes attributes;
  83.   attributes.backing_store = Always;
  84.   XChangeWindowAttributes(display, win, CWBackingStore, &attributes);
  85. }
  86.  
  87. void X11_Window::close()
  88. {
  89.   BaseWindow::close();
  90.   XFreeGC(display, gc);
  91.   XCloseDisplay(display);
  92.   delete [] segments;
  93. }
  94.  
  95. void X11_Window::clear()
  96. {
  97.   BaseWindow::clear();
  98.   XClearWindow(display, win);
  99. }
  100.  
  101. char X11_Window::waitForKey()
  102. {
  103.   XEvent event;
  104.   const int bufsize = 20;
  105.   char buffer[bufsize];
  106.   KeySym keysym;
  107.   XComposeStatus stat;
  108.  
  109.   while(1) {
  110.     XNextEvent(display, &event);
  111.     if (event.type == KeyPress &&
  112.     XLookupString(&event.xkey, buffer, bufsize, &keysym, &stat) == 1)
  113.       return buffer[0];
  114.   }
  115. }
  116.  
  117. void X11_Window::writeText(const rcString& msg, int x, int y)
  118. {
  119.   XDrawString(display, win, gc, x, y, msg.chars(), msg.length());
  120.   XFlush(display);
  121. }
  122.  
  123. /*
  124.  * Buffer line segments. If we have request size XSegments,
  125.  * bring the hole buffer to screen.
  126.  */
  127.  
  128. void X11_Window::drawLine(int x1, int y1, int x2, int y2)
  129. {
  130.   if (segmentsInBuffer < RequestSize) {
  131.     segments[segmentsInBuffer].x1 = x1;
  132.     segments[segmentsInBuffer].y1 = resY-y1;
  133.     segments[segmentsInBuffer].x2 = x2;
  134.     segments[segmentsInBuffer].y2 = resY-y2;
  135.     segmentsInBuffer++;
  136.   }
  137.   else {
  138.     XDrawSegments(display, win, gc, segments, segmentsInBuffer);
  139.     segmentsInBuffer = 0;
  140.   }
  141. }
  142.  
  143. /*
  144.  * Flush segment buffer.
  145.  */
  146.  
  147. void X11_Window::flush()
  148. {
  149.   BaseWindow::flush();
  150.   if (segmentsInBuffer > 0) {
  151.     XDrawSegments(display, win, gc, segments, segmentsInBuffer);
  152.     segmentsInBuffer = 0;
  153.   }
  154.  
  155.   XFlush(display);
  156. }
  157.  
  158.