home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / oct93 / graphics / graphtal.lha / Graphtal / X11_Window.C < prev    next >
C/C++ Source or Header  |  1993-03-09  |  4KB  |  159 lines

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