Next: Panels ( panel ) Up: Graphics Previous: Colors ( color )   Contents   Index

# Windows ( window )

Definition

The data type window provides an interface for graphical input and output of basic two-dimensional geometric objects. Application programs using this data type have to be linked with libW.a and (on UNIX systems) with the X11 base library libX11.a (cf. section 1.6):

CC prog.c -lW -lP -lG -lL -lX11 -lm

An instance W of type window is an iso-oriented rectangular window in the two-dimensional plane. The default representation of W on the screen is a square of maximal possible edge length positioned in the upper right corner of the display.

In general, a window consists of two rectangular sections, a panel section in the upper part and a drawing section in the rest of the window. The panel section contains panel items such as sliders, choice fields, string items and buttons. They have to be created before the window is opened by special panel operations described in section panel-operations.

The drawing section can be used for the output of geometric objects such as points, lines, segments, arrows, circles, polygons, graph, ...and for the input of all these objects using the mouse input device. All drawing and input operations in the drawing section use a coordinate system that is defined by three parameters of type double: xmin, the minimal x-coordinate, xmax, the maximal x-coordinate, and ymin, the minimal y-coordinate. The two parameters xmin and xmax define the scaling factor scaling as w/(xmax - xmin), where w is the width of the drawing section in pixels. The maximal y-coordinate ymax of the drawing section is equal to ymin + h*scaling and depends on the actual shape of the window on the screen. Here, h is the height of the drawing section in pixels.

A list of all window parameters:

1. The foreground color parameter (default black) defines the default color to be used in all drawing operations. There are 18 predefined colors (enumeration in < LEDA/graphics/x_window.htex2html_wrap_inline> ): black, white, red, green, blue, yellow, violet, orange, cyan, brown, pink, green2, blue2, grey1, grey2, grey3 ivory, and invisible. Note that all drawing operations have an optional color argument that can be used to override the default foreground color. On monochrome systems all colors different from white are displayed as black. The color invisible can be used for invisible (transparent) objects.

2. The background color parameter (default white) defines the default background color (e.g. used by W.clear()).

3. The text font parameter defines the name of the font to be used in all text drawing operations.

4. Minimal and maximal coordinates of the drawing area xmin (default 0), xmax (default 100), ymin (default 0).

5. The grid dist parameter (default 0) defines the width of the grid that is used in the drawing area. A grid width of 0 indicates that no grid is to be used.

6. The frame label parameter defines a string to be displayed in the frame of the window.

7. The show coordinates flag (default true) determines whether the current coordinates of the mouse cursor in the drawing section are displayed in the upper right corner.

8. The flush output flag (default true) determines whether the graphics output stream is flushed after each draw action.

9. The line width parameter (default value 1 pixel) defines the width of all kinds of lines (segments, arrows, edges, circles, polygons).

10. The line style parameter defines the style of lines. Possible line styles are solid (default), dashed, and dotted.

11. The point style parameter defines the style points are drawn by the draw_point operation. Possible point styles are pixel_point, cross_point (default), plus_point, circle_point, disc_point, rect_point, and box_point.

12. The node width parameter (default value 8 pixels) defines the diameter of nodes created by the draw_node and draw_filled_node operations.

13. The text mode parameter defines how text is inserted into the window. Possible values are transparent (default) and opaque.

14. The show orientation parameter defines, whether or not the direction or orientation of segments, lines, rays, triangles, polygons and gen_polygons will be shown (default false.)

15. The drawing mode parameter defines the logical operation that is used for setting pixels in all drawing operations. Possible values are src_mode (default) and xor_mode. In src_mode pixels are set to the respective color value, in xor_mode the value is bitwise added to the current pixel value.

16. The redraw function parameter is a pointer to a function of type void (*F)(window*). It is called with a pointer to the corresponding window as argument to redraw (parts of) the window whenever a redrawing is necessary, e.g., if the shape of the window is changed or previously hidden parts of it become visible.

17. The window delete handler parameter is a pointer to a function of type void (*F)(window*). It is called with a pointer to the corresponding window as argument when the window is to be closed by the window manager (e.g. by pressing the x-button on Windows-NT systems). The default window delete handler closes the window and terminates the program.

18. The buttons per line parameter (default ) defines the maximal number of buttons in one line of the panel section.

19. The precision parameter (default 16) defines the precision that is used for representing window coordinates, more precisely, all x and y coordinates generated by window input operations are doubles whose mantissa are truncated after precision - 1 bits after the binary point.

In addition to call-back (handler) functions LEDA windows now also support the usage of function objects. Function object classes have to be derived from the window_handler base class.

class window_handler {
...
virtual void operator()() { }

// parameter access functions
double  get_double(int nr) const;
int     get_int() const;
window* get_window_ptr() const;
char*   get_char_ptr() const;
};


Derived classes have to implement the handling function in the definition of the operator() method. The different get_ methods can be called to retrieve parameters.

If both, a handler function and an object for the same action is supplied the object has higher priority.

#include < LEDA/graphics/window.h >

Creation

 window W creates a squared window with maximal possible edge length (minimum of width and height of the display). window W(const char* label) creates a maximal squared window with frame label label. window W(int w, int h) creates a window W of physical size w pixels x h pixels . window W(int w, int h, const char* label) creates a window W of physical size w pixels x h pixels and frame label label.

All four variants initialize the coordinates of W to xmin = 0, xmax = 100 and ymin = 0. The init operation (see below) can later be used to change the window coordinates and scaling. Please note, that a window is not displayed before the function display is called for it.

Operations

3.1 Initialization

 void W.init(double x0, double x1, double y0) sets xmin to x0, xmax to x1, and ymin to y0, the scaling factor scaling to w/(xmax - xmin), and ymax to ymin + h/scaling. Here w and h are the width and height of the drawing section in pixels. void W.init_rectangle(double x0, double x1, double y0, double y1) adjusts the window such that the points (x0, y0) and (x1, y1) are contained in the drawing section. double W.set_grid_dist(double d) sets the grid distance of W to d. grid_style W.set_grid_style(grid_style s) sets the grid style of W to s. int W.set_grid_mode(int d) sets the grid distance of W to d pixels. int W.set_precision(int prec) sets the precision of W to prec. void W.init(double x0, double x1, double y0, int d, bool erase=true) same as W.init( x0, x1, y0) followed by W.set_grid_mode(d). If the optional flag erase is set to false the window will not be erased. void W.display() opens W and displays it at the center of the screen. Note that W.display() has to be called before all drawing operations and that all operations adding panel items to W (cf. panel-operations) have to be called before the first call of W.display(). void W.display(int x, int y) opens W and displays it with its left upper corner at position (x, y). Special values for x and y are window : : min, window : : center, and window : : max for positioning W at the minimal or maximal x or y coordinate or centering it in the x or y dimension. void W.display(window& W0, int x, int y) opens W and displays it with its left upper corner at position (x, y) relative to the upper left corner of window W0.

W.open...can be used as a synonym for W.display... Note, that the open operation for panels (cf. Panels) is defined slightly different.

 void W.close() closes W by removing it from the display. void W.clear() clears W using the current background color or pixmap, i.e., if W has a background pixmap defined it is tiled with P such that the upper left corner is the tiling origin. Otherwise, it is filled with background color of W. void W.clear(double x0, double y0, double x1, double y1) only clears the rectangular area (x0, y0, x1, y1) of window W using the current background color or pixmap. void W.clear(color c) clears W with color c and sets the background color of W to c. void W.clear(double xorig, double yorig) clears W. If a background pixmap is defined the point (xorig, yorig) is used as the origin of tiling. void W.redraw() repaints the drawing area if W has a redraw function.

3.2 Setting parameters

 color W.set_color(color c) sets the foreground color parameter to c and returns its previous value. color W.set_fill_color(color c) sets the fill color parameter (used by « operators) to c and returns its previous value. color W.set_bg_color(color c) sets the background color parameter to c and returns its previous value. char* W.set_bg_pixmap(char* pr) sets the background pixmap to pr and returns its previous value. int W.set_line_width(int pix) sets the line width parameter to pix pixels and returns its previous value. line_style W.set_line_style(line_style s) sets the line style parameter to s and returns its previous value. int W.set_node_width(int pix) sets the node width parameter to pix pixels and returns its previous value. text_mode W.set_text_mode(text_mode m) sets the text mode parameter to m and returns its previous value. drawing_mode W.set_mode(drawing_mode m) sets the drawing mode parameter to m and returns its previous value. int W.set_cursor(int cursor_id = -1) sets the mouse cursor of W to cursor_id. Here cursor_id must be one of the constants predefined in or -1 for the system default cursor. Returns the previous cursor. void W.set_show_coordinates(bool b) sets the show coordinates flag to b. bool W.set_show_orientation(bool orient) sets the show orientation parameter to orient. void W.set_frame_label(string s) makes s the window frame label. void W.set_icon_label(string s) makes s the window icon label. void W.reset_frame_label() restores the standard LEDA frame label. void W.set_window_delete_handler(void (*F)(window*)) sets the window delete handler function parameter to F. void W.set_window_delete_object(const window_handler& obj) sets the window delete object parameter to obj. void W.set_show_coord_handler(void (*F)(window*, double, double)) sets the show coordinate handler function parameter to F. void W.set_show_coord_object(const window_handler& obj) sets the show coordinate object parameter to obj. void W.set_redraw(void (*F)(window*)) sets the redraw function parameter to F. void W.set_redraw(const window_handler& obj) sets the redraw object parameter to obj. void W.set_redraw(void (*F)(window*, double, double, double, double)=0) sets the redraw function parameter to F. void W.set_redraw2(const window_handler& obj) sets the redraw object parameter to obj. void W.set_bg_redraw(void (*F)(window*, double, double, double, double)=0) sets the background redraw function parameter to F. void W.set_bg_redraw(const window_handler& obj) sets the background redraw object parameter to obj. void W.start_timer(int msec, void (*F)(window*)) starts a timer that runs F every msec milliseconds with a pointer to W. void W.start_timer(int msec, const window_handler& obj) starts a timer that runs the operator() of obj every msec milliseconds. void W.stop_timer() stops the timer. void W.set_flush(bool b) sets the flush parameter to b. void W.set_icon_pixrect(char* pr) makes pr the new icon of W. void* W.set_client_data(void* p, int i=0) sets the i-th client data pointer of W to p and returns its previous value. Precondition i < 16.

 int W.get_line_width() returns the current line width. line_style W.get_line_style() returns the current line style. int W.get_node_width() returns the current node width. text_mode W.get_text_mode() returns the current text mode. drawing_mode W.get_mode() returns the current drawing mode. int W.get_cursor() returns the id of the current cursor, i.e, one of the constants predefined in or -1 for the default cursor. double W.xmin() returns the minimal x-coordinate of the drawing area of W. double W.ymin() returns the minimal y-coordinate of the drawing area of W. double W.xmax() returns the maximal x-coordinate of the drawing area of W. double W.ymax() returns the maximal y-coordinate of the drawing area of W. double W.scale() returns the scaling factor of the drawing area of W, i.e. the number of pixels of a unit length line segment. double W.get_grid_dist() returns the width of the current grid (zero if no grid is used). grid_style W.get_grid_style() returns the current grid style. int W.get_grid_mode() returns the width of the current grid in pixels (zero if no grid is used). bool W.get_show_orientation() returns the show orientation parameter. void* W.get_client_data(int i=0) returns the i-th client data pointer of of W. Precondition i < 16. GraphWin* W.get_graphwin() returns a pointer to the GraphWin (see Graph Windows) that uses W as its display window or NULL if W is not used by any GraphWin. GeoWinTypeName* W.get_geowin() returns a pointer to the GeoWin (see Section GeoWin) that uses W as its display window or NULL if W is not used by any GeoWin. int W.width() returns the width of W in pixels. int W.height() returns the height of W in pixels. int W.menu_bar_height() returns the height of the menu bar of W in pixels and 0 if W has no menu bar (see W.make_menu_bar()). int W.xpos() returns the x-coordinate of the upper left corner of the frame of W. int W.ypos() returns the y-coordinate of the upper left corner of the frame of W. int W.get_state() returns the state of W. void W.set_state(int stat) sets the state of W to stat. bool W.contains(const point& p) returns true if p lies in the drawing area.

3.4 Drawing Operations

All drawing operations have an optional color argument at the end of the parameter list. If this argument is omitted the current foreground color (cf. section parameters) of W is used.

3.4.1 Drawing points

 void W.draw_point(double x, double y, color c=window::fgcol) draws the point (x, y) (a cross of two short segments). void W.draw_point(const point& p, color c=window::fgcol) draws point p. void W.draw_pixel(double x, double y, color c=window::fgcol) sets the color of the pixel at position (x, y) to c. void W.draw_pixel(const point& p, color c=window::fgcol) sets the color of the pixel at position p to c. void W.draw_pixels(const list& L, color c=window::fgcol) sets the color of all pixels in L to c. void W.draw_pixels(int n, double* xcoord, double* ycoord, color c=window::fgcol) draws all pixels (xcoord[i], ycoord[i]) for 0 < = i < = n - 1.

3.4.2 Drawing line segments

 void W.draw_segment(double x1, double y1, double x2, double y2, color c=window::fgcol) draws a line segment from (x1, y1) to (x2, y2). void W.draw_segment(const point& p, const point& q, color c=window::fgcol) draws a line segment from point p to point q. void W.draw_segment(const segment& s, color c=window::fgcol) draws line segment s. void W.draw_segment(point p, point q, line l, color c = window::fgcol) draws the part of the line l between p and q. This version of draw_segment should be used if p or q may lie far outside W. Precondition p and q lie on l or at least close to l. void W.draw_segments(const list& L, color c=window::fgcol) draws all segments in L.

3.4.3 Drawing lines

 void W.draw_line(double x1, double y1, double x2, double y2, color c=window::fgcol) draws a straight line passing through points (x1, y1) and (x2, y2). void W.draw_line(const point& p, const point& q, color c=window::fgcol) draws a straight line passing through points p and q. void W.draw_line(const segment& s, color c=window::fgcol) draws the line supporting segment s. void W.draw_line(const line& l, color c=window::fgcol) draws line l. void W.draw_hline(double y, color c=window::fgcol) draws a horizontal line with y-coordinate y. void W.draw_vline(double x, color c=window::fgcol) draws a vertical line with x-coordinate x.

3.4.4 Drawing Rays

 void W.draw_ray(double x1, double y1, double x2, double y2, color c=window::fgcol) draws a ray starting in (x1, y1) and passing through (x2, y2). void W.draw_ray(const point& p, const point& q, color c=window::fgcol) draws a ray starting in p and passing through q. void W.draw_ray(const segment& s, color c=window::fgcol) draws a ray starting in s.source() containing s. void W.draw_ray(const ray& r, color c=window::fgcol) draws ray r. void W.draw_ray(point p, point q, line l, color c = window::fgcol) draws the part of the line l on the ray with source p and passing through q. This version of draw_ray should be used if p may lie far outside W. Precondition p and q lie on l or at least close to l.

3.4.5 Drawing Arcs and Curves

 void W.draw_arc(const point& p, const point& q, const point& r, color c=window::fgcol) draws a circular arc starting in p passing through q and ending in r. void W.draw_bezier(const list& C, int n, color c=window::fgcol) draws the bezier curve with control polygon C by a polyline with n points. void W.draw_spline(const list& L, int n, color c=window::fgcol) draws a spline curve through the points of L. Each segment is approximated by a polyline with m points. void W.draw_closed_spline(const list& L, int n, color c=window::fgcol) draws a closed spline through the points of L. void W.draw_spline(const polygon& P, int n, color c=window::fgcol) draws a closed spline through the vertices of P.

3.4.6 Drawing arrows

 void W.draw_arrow(double x1, double y1, double x2, double y2, color c =window::fgcol) draws an arrow pointing from (x1, y1) to (x2, y2). void W.draw_arrow(const point& p, const point& q, color c=window::fgcol) draws an arrow pointing from point p to point q. void W.draw_arrow(const segment& s, color=window::fgcol) draws an arrow pointing from s.start() to s.end(). void W.draw_polyline_arrow(const list& lp, color c=window::fgcol) draws a polyline arrow with vertex sequence lp. void W.draw_arc_arrow(const point& p, const point& q, const point& r, color c=window::fgcol) draws a circular arc arrow starting in p passing through q and ending in r. void W.draw_bezier_arrow(const list& C, int n, color c=window::fgcol) draws the bezier curve with control polygon C by a polyline with n points, the last segment is drawn as an arrow. void W.draw_spline_arrow(const list& L, int n, color c=window::fgcol) draws a spline curve through the points of L. Each segment is approximated by a polyline with n points. The last segment is drawn as an arrow. point W.draw_arrow_head(const point& p, double dir, color c=window::fgcol) draws an arrow head at position p pointing to direction dir, where dir is an angle from [0, 2].

3.4.7 Drawing circles

 void W.draw_circle(double x, double y, double r, color c=window::fgcol) draws the circle with center (x, y) and radius r. void W.draw_circle(const point& p, double r, color c=window::fgcol) draws the circle with center p and radius r. void W.draw_circle(const circle& C, color c=window::fgcol) draws circle C. void W.draw_ellipse(double x, double y, double r1, double r2, color c=window::fgcol) draws the ellipse with center (x, y) and radii r1 and r2. void W.draw_ellipse(const point& p, double r1, double r2, color c=window::fgcol) draws the ellipse with center p and radii r1 and r2.

3.4.8 Drawing discs

 void W.draw_disc(double x, double y, double r, color c=window::fgcol) draws a filled circle with center (x, y) and radius r. void W.draw_disc(const point& p, double r, color c=window::fgcol) draws a filled circle with center p and radius r. void W.draw_disc(const circle& C, color c=window::fgcol) draws filled circle C. void W.draw_filled_circle(double x, double y, double r, color c=window::fgcol) draws a filled circle with center (x, y) and radius r. void W.draw_filled_circle(const point& p, double r, color c=window::fgcol) draws a filled circle with center p and radius r. void W.draw_filled_circle(const circle& C, color c=window::fgcol) draws filled circle C. void W.draw_filled_ellipse(double x, double y, double r1, double r2, color c=window::fgcol) draws a filled ellipse with center (x, y) and radii r1 and r2. void W.draw_filled_ellipse(const point& p, double r1, double r2, color c=window::fgcol) draws a filled ellipse with center p and radii r1 and r2.

3.4.9 Drawing polygons

 void W.draw_polyline(const list& lp, color c=window::fgcol) draws a polyline with vertex sequence lp. void W.draw_polyline(int n, double* xc, double* yc, color c=window::fgcol) draws a polyline with vertex sequence (xc[0], yc[0]),...,(xc[n - 1], yc[n - 1]). void W.draw_polygon(const list& lp, color c=window::fgcol) draws the polygon with vertex sequence lp. void W.draw_oriented_polygon(const list& lp, color c=window::fgcol) draws the polygon with vertex sequence lp and indicates the orientation by an arrow. void W.draw_polygon(const polygon& P, color c=window::fgcol) draws polygon P. void W.draw_oriented_polygon(const polygon& P, color c=window::fgcol) draws polygon P and indicates the orientation by an arrow. void W.draw_filled_polygon(const list& lp, color c=window::fgcol) draws the filled polygon with vertex sequence lp. void W.draw_filled_polygon(const polygon& P, color c=window::fgcol) draws filled polygon P. void W.draw_polygon(const gen_polygon& P, color c=window::fgcol) draws polygon P. void W.draw_oriented_polygon(const gen_polygon& P, color c=window::fgcol) draws polygon P and indicates the orientation by an arrow. void W.draw_filled_polygon(const gen_polygon& P, color c=window::fgcol) draws filled polygon P. void W.draw_rectangle(double x0, double y0, double x1, double y1, color=window::fgcol) draws a rectangle with lower left corner (x0, y0) and upper right corner (x1, y1). Precondition x0 < x1 and y0 < y1. void W.draw_rectangle(point p, point q, color=window::fgcol) draws a rectangle with lower left corner p and upper right corner q. Precondition p < q. void W.draw_rectangle(const rectangle& R, color=window::fgcol) draws rectangle R. void W.draw_box(double x0, double y0, double x1, double y1, color c=window::fgcol) draws a filled rectangle with lower left corner (x0, y0) and upper right corner (x1, y1). Precondition x0 < x1 and y0 < y1. void W.draw_filled_rectangle(point p, point q, color=window::fgcol) draws a filled rectangle with lower left corner p and upper right corner q. Precondition p < q. void W.draw_filled_rectangle(const rectangle& R, color=window::fgcol) draws rectangle R. void W.draw_box(point p, point q, color c=window::fgcol) same as draw_filled_rectangle(p,q,c). void W.draw_box(const rectangle& R, color c=window::fgcol) same as draw_filled_rectangle(p,q,c). void W.draw_roundrect(double x0, double y0, double x1, double y1, double rndness, color col=window::fgcol) draws a rectangle (x0, y0, x1, y1) with round corners. The rndness argument must be a real number in the interval [0, 1] and defines the roundness'' of the rectangle. void W.draw_roundrect(point p, point q, double rndness, color col=window::fgcol) draws a round rectangle with lower left corner p, upper right corner q, and roundness rndness. void W.draw_roundbox(double x0, double y0, double x1, double y1, double rndness, color col=window::fgcol) draws a filled rectangle (x0, y0, x1, y1) with round corners. The rndness argument must be a real number in the interval [0, 1] and defined the roundness'' of the rectangle. void W.draw_roundbox(point p, point q, double rndness, color col=window::fgcol) draws a round filled rectangle with lower left corner p, upper right corner q, and roundness rndness. void W.draw_triangle(point a, point b, point c, color=window::fgcol) draws triangle (a, b, c). void W.draw_triangle(const triangle& T, color=window::fgcol) draws triangle T. void W.draw_filled_triangle(point a, point b, point c, color=window::fgcol) draws filled triangle (a, b, c). void W.draw_filled_triangle(const triangle& T, color=window::fgcol) draws filled triangle T.

3.4.10 Drawing functions

 void W.plot_xy(double x0, double x1, win_draw_func F, color c=window::fgcol) draws the graph of function F in the x-range [x0, x1], i.e., all pixels (x, y) with y = F(x) and x0 < = x < = x1. void W.plot_yx(double y0, double y1, win_draw_func F, color c=window::fgcol) draws the graph of function F in the y-range [y0, y1], i.e., all pixels (x, y) with x = F(y) and y0 < = y < = y1.

3.4.11 Drawing text

 void W.draw_text(double x, double y, string s, color c=window::fgcol) writes string s starting at position (x, y). void W.draw_text(const point& p, string s, color c=window::fgcol) writes string s starting at position p. void W.draw_ctext(double x, double y, string s, color c=window::fgcol) writes string s centered at position (x, y). void W.draw_ctext(const point& p, string s, color c=window::fgcol) writes string s centered at position p. void W.draw_ctext(string s, color c=window::fgcol) writes string s centered in window W. double W.text_box(double x0, double x1, double y, string s, bool draw=true) formats and writes string s into a box with its left border at x-coordinate x0, its right border at x1, and its upper border at y-coordinate y. Some LaTeX-like formatting commands can be used: \bf, \tt, \rm, \n, \c, \, ... returns y-coordinate of lower border of box. If the optional last parameter draw is set to false no drawing takes place and only the lower y-coordinate of the box is computed. void W.text_box(string s) as above with x = W.xmin(), x1 = W.xmax(), and y = W.ymax(). void W.message(string s) displays the message s (each call adds a new line). void W.del_message() deletes the text written by all previous message operations.

3.4.12 Drawing nodes

Nodes are represented by circles of diameter nodewidth.

 void W.draw_node(double x0, double y0, color c=window::fgcol) draws a node at position (x0, y0). void W.draw_node(const point& p, color c=window::fgcol) draws a node at position p. void W.draw_filled_node(double x0, double y0, color c=window::bgcol) draws a filled node at position (x0, y0). void W.draw_filled_node(const point& p, color c=window::bgcol) draws a filled node at position p. void W.draw_text_node(double x, double y, string s, color c=window::bgcol) draws a node with label s at position (x, y). void W.draw_text_node(const point& p, string s, color c=window::bgcol) draws a node with label s at position p. void W.draw_int_node(double x, double y, int i, color c=window::bgcol) draws a node with integer label i at position (x, y). void W.draw_int_node(const point& p, int i, color c=window::bgcol) draws a node with integer label i at position p.

3.4.13 Drawing edges

Edges are drawn as straigth line segments or arrows with a clearance of nodewidth/2 at each end.

 void W.draw_edge(double x1, double y1, double x2, double y2, color c=window::fgcol) draws an edge from (x1, y1) to (x2, y2). void W.draw_edge(const point& p, const point& q, color c=window::fgcol) draws an edge from p to q. void W.draw_edge(const segment& s, color c=window::fgcol) draws an edge from s.start() to s.end(). void W.draw_edge_arrow(double x1, double y1, double x2, double y2, color c=window::fgcol) draws a directed edge from (x1, y1) to (x2, y2). void W.draw_edge_arrow(const point& p, const point& q, color c=window::fgcol) draws a directed edge from p to q. void W.draw_edge_arrow(const segment& s, color c=window::fgcol) draws a directed edge from s.start() to s.end().

3.4.14 Bitmaps and Pixrects

 char* W.create_bitmap(int w, int h, unsigned char* bm_data) creates a bitmap (monochrome pixrect) of width w, height h, from the bits in data. char* W.create_pixrect(int w, int h, unsigned int bgra) creates a a solid pixrect of width w und height h. char* W.create_pixrect(const char** xpm_str) creates a pixrect from the xpm data string xpm_str. char* W.create_pixrect(string xpm_file) creates a pixrect from the xpm file xpm_file. char* W.create_pixrect(int w, int h, unsigned char* bm_data, int fg=window::fgcol, int bg=window::bgcol) creates a pixrect of width w, height h, foreground color fg, and background color bg from bitmap data. char* W.get_pixrect(double x1, double y1, double x2, double y2) creates a color pixrect of width w = x2 - x1, height h = y2 - y1, and copies all pixels from the rectangular area (x1, x2, y1, y2) of W into it. char* W.get_window_pixrect() creates a pixrect copy of the current window contents. int W.get_pixrect_width(char* pr) returns the width (number of pixels in a row) of pixrect pr. int W.get_pixrect_height(char* pr) returns the height (number of pixels in a column) of pixrect pr. void W.put_pixrect(double x, double y, char* pr) copies the contents of pixrect pr with lower left corner at position (x, y) into W. void W.put_pixrect(point p, char* pr) copies the contents of pixrect pr with lower left corner at position p into W. void W.center_pixrect(double x, double y, char* pr) copies the contents of pixrect pr into W such that its center lies on position (x, y). void W.center_pixrect(char* pr) copies the contents of pixrect pr into W such that its center lies on the center of W. void W.put_pixrect(char* pr) copies pixrect pr with lower left corner at position (W.xmin(),W.ymin()) into W. void W.set_pixrect(char* pr) copies pixrect pr with upper left corner at position (0, 0) into W. void W.fit_pixrect(char* pr) scales pixrect pr to fit into W. void W.put_bitmap(double x, double y, char* bm, color c=window::fgcol) draws all pixels corresponding to 1-bits in bm with color c, here the lower left corner of bm corresponds to the pixel at position (x, y) in W. void W.put_pixrect(double x, double y, char* pr, int x0, int y0, int w, int h) copies (pixel) rectangle (x0, y0, x0 + w, y0 + h) of pr with lower left corner at position (x, y) into W. void W.del_bitmap(char* bm) destroys bitmap bm. void W.del_pixrect(char* pr) destroys pixrect pr. void W.copy_rect(double x0, double y0, double x1, double y1, double x, double y) copies all pixels of rectangle (x0, y0, x1, y1) into the rectangle (x, y, x + w, y + h), where w = x1 - x0 and h = y1 - y0. void W.screenshot(string fname, bool full_color=true) creates a screenshot of the current window. On unix systems suffix .ps is appended to fname and the output format is postscript. On windows systems the suffix .wmf is added and the format is windows metafile. If the flag full_color is set to false colors will be translated into grey scales.

3.4.15 Buffering

 void W.start_buffering() starts buffering mode for W. If W has no associated buffer a buffer pixrect buf of the same size as the current drawing area of W is created. All subsequent drawing operations draw into buf instead of W until buffering mode is ended by calling W.stop_buffering(). void W.flush_buffer() copies the contents of the buffer pixrect into the drawing area of W. void W.flush_buffer(double dx, double dy) copies the contents of the buffer pixrect translated by vector (dx, dy) into the drawing area of W. void W.flush_buffer(double x0, double y0, double x1, double y1) copies the contents of rectangle (x0, y0, x1, y1) of the buffer pixrect into the corresponding rectangle of the drawin area. void W.flush_buffer(double dx, double dy, double x0, double y0, double x1, double y1) copies the contents of rectangle (x0, y0, x1, y1) of the buffer pixrect into the corresponding rectangle of the drawin area translated by vector (dx, dy). void W.stop_buffering() ends buffering mode. void W.stop_buffering(char*& prect) ends buffering mode and returns the current buffer pixrect in prect.

3.4.16 Clipping

 void W.set_clip_rectangle(double x0, double y0, double x1, double y1) sets the clipping region of W to rectangle (x0, y0, x1, y1). void W.set_clip_ellipse(double x0, double y0, double r1, double r2) sets the clipping region of W to ellipse with center (x, y) and horizontal radius r1 and vertical radius r2. void W.reset_clipping() restores the clipping region to the entire drawing area of W.

3.5 Input

The main input operation for reading positions, mouse clicks, and buttons from a window W is the operation W.read_mouse(). This operation is blocking, i.e., waits for a button to be pressed which is either a real'' button on the mouse device pressed inside the drawing area of W or a button in the panel section of W. In both cases, the number of the selected button is returned. Mouse buttons have pre-defined numbers MOUSE_BUTTON(1) for the left button, MOUSE_BUTTON(2) for the middle button, and MOUSE_BUTTON(3) for the right button. The numbers of the panel buttons can be defined by the user. If the selected button has an associated action function or sub-window this function/window is executed/opened (cf. panel-operations for details).

There is also a non-blocking version W.get_mouse() which returns the constant NO_BUTTON if no button was pressed.

The window data type also provides two more general input operations W.read_event() and W.get_event() for reading events. They return the event type (enumeration in < LEDA/graphics/x_window.htex2html_wrap_inline> ), the value of the event, the position of the event in the drawing section, and a time stamp of the event.

3.5.2 Events

 int W.read_event(int& val, double& x, double& y, unsigned long& t) waits for next event in window W and returns it. Assigns the button or key to val, the position in W to (x, y), and the time stamp of the event to t. Possible events are (cf. < LEDA/graphics/x_window.htex2html_wrap_inline> ): key_press_event, key_release_event, button_press_event, button_release_event, configure_event, motion_event, destroy_event. int W.read_event(int& val, double& x, double& y, unsigned long& t, int timeout) as above, but waits only timeout milliseconds; if no event occured the special event noevent is returned. int W.read_event(int& val, double& x, double& y) waits for next event in window W and returns it. Assigns the button or key to val and the position in W to (x, y). int W.read_event() waits for next event in window W and returns it. int W.get_event(int& val, double& x, double& y) if there is an event for window W in the event queue a W.readevent operation is performed, otherwise the integer constant noevent is returned. bool W.shift_key_down() returns true if a shift key was pressed during the last handled mouse button event. bool W.ctrl_key_down() returns true if a ctrl key was pressed during the last handled mouse button event. bool W.alt_key_down() returns true if an alt key was pressed during the last handled mouse button event. int W.button_press_time() returns the time-stamp (in msec) of the last button press event. int W.button_release_time() returns the time-stamp (in msec) of the last button release event.

3.6 Panel Input

The operations listed in this section are useful for simple input of strings, numbers, and Boolean values.

 bool W.confirm(string s) displays string s and asks for confirmation. Returns true iff the answer was yes''. void W.acknowledge(string s) displays string s and asks for acknowledgement. int W.read_panel(string h, int n, string* S) displays a panel with header h and an array of n buttons with labels S[0..n - 1], returns the index of the selected button. int W.read_vpanel(string h, int n, string* S) like read_panel with vertical button layout. string W.read_string(string p) displays a panel with prompt p for string input, returns the input. double W.read_real(string p) displays a panel with prompt p for double input returns the input. int W.read_int(string p) displays a panel with prompt p for integer input, returns the input.

3.7 Input and output operators

For input and output of basic geometric objects in the plane such as points, lines, line segments, circles, and polygons the < < and tex2html_wrap_inline> tex2html_wrap_inline> operators can be used. Similar to C++input streams windows have an internal state indicating whether there is more input to read or not. Its initial value is true and it is turned to false if an input sequence is terminated by clicking the right mouse button (similar to ending stream input by the eof character). In conditional statements, objects of type window are automatically converted to boolean by returning this internal state. Thus, they can be used in conditional statements in the same way as C++input streams. For example, to read a sequence of points terminated by a right button click, use  while (W  > >  p) { ...} ''.

3.7.1 Output

 window& W « const point& p like W.draw_point(p). window& W « const segment& s like W.draw_segment(s). window& W « const ray& r like W.draw_ray(r). window& W « const line& l like W.draw_line(l). window& W « const circle& C like W.draw_circle(C). window& W « const polygon& P like W.draw_polygon(P). window& W « const gen_polygon& P like W.draw_polygon(P). window& W « const rectangle& R like W.draw_rectangle(R). window& W « const triangle& T like W.draw_triangle(T).

3.7.2 Input

 window& W » point& p reads a point p: clicking the left button assigns the current cursor position to p. window& W » segment& s reads a segment s: use the left button to define the start and end point of s. window& W » ray& r reads a ray r: use the left button to define the start point and a second point on r. window& W » line& l reads a line l: use the left button to define two different points on l. window& W » circle& C reads a circle C: use the left button to define the center of C and a point on C. window& W » rectangle& R reads a rectangle R: use the left button to define two opposite corners of R. window& W » triangle& T reads a triangle T: use the left button to define the corners of T. window& W » polygon& P reads a polygon P: use the left button to define the sequence of vertices of P, end the sequence by clicking the right button. window& W » gen_polygon& P reads a generalized polygon P; input the polygons defining P and end the input by clicking the middle button. list W.read_polygon() as above, however, returns list of vertices.

As long as an input operation has not been completed the last read point can be erased by simultaneously pressing the shift key and the left mouse button.

3.8 Non-Member Functions

 int read_mouse(window*& w, double& x, double& y) waits for mouse input, assigns a pointer to the corresponding window to w and the position in *w to (x, y) and returns the pressed button. int get_mouse(window*& w, double& x, double& y) non-blocking variant of read_mouse, returns NO_BUTTON if no button was pressed. void put_back_event() puts last handled event back to the event queue.

3.9 Panel Operations

The panel section of a window is used for displaying text messages and for updating the values of variables. It consists of a list of panel items and a list of buttons. The operations in this section add panel items or buttons to the panel section of W. Note that they have to be called before the window is displayed the first time.

In general, a panel item consists of a string label and an associated variable of a certain type (int, bool, string, double, color). The value of this variable can be manipulated through the item. Each button has a label (displayed on the button) and an associated number. The number of a button is either defined by the user or is the rank of the button in the list of all buttons. If a button is pressed (i.e. selected by a mouse click) during a readmouse operation its number is returned.

Action functions can be associated with buttons and some items (e.g. slider items) whenever a button with an associated action function is pressed this function is called with the number of the button as actual parameter. Action functions of items are called whenever the value of the corresponding variable is changed with the new value as actual parameter. All action functions must have the type void  func(int).

Another way to define a button is to associate another window with it. In this case the button will have a menu sign and as soon as it is pressed the attached window will open. This method can be used to implement pop-up menues. The return value of the current readmouse operation will be the number associated with the button in the menu.

3.9.1 General Settings

 void W.set_panel_bg_color(color c) sets the background color of the panel area to c. void W.buttons_per_line(int n) defines the maximal number n of buttons per line. void W.set_button_space(int s) sets the space between to adjacent buttons to s pixels. void W.set_item_height(int h) sets the vertical size of all items to h pixels. void W.set_item_width(int w) sets the horizontal size of all slider and string items to w pixels. void W.set_bitmap_colors(int c0, int c1) sets the unpressed/pressed colors used for drawing the pixels in bitmap buttons to c0 and c1.

3.9.2 Simple Panel Items

 panel_item W.text_item(string s) adds a text_item s to W. panel_item W.bool_item(string s, bool& x, const char* hlp=0) adds a boolean item with label s and variable x to W. panel_item W.bool_item(string s, bool& x, void (*F)(int), const char* hlp=0) as above with action function F. panel_item W.bool_item(string s, bool& x, const window_handler& obj, const char* hlp=0) as above with handler object obj. panel_item W.int_item(string s, int& x, const char* hlp=0) adds an integer item with label s and variable x to W. panel_item W.string_item(string s, string& x, void (*F)(char*), const char* hlp=0) as above with action function F. panel_item W.string_item(string s, string& x, const window_handler& obj, const char* hlp=0) as above with handler object obj. panel_item W.string_item(string s, string& x, const char* hlp=0) adds a string item with label s and variable x to W. panel_item W.double_item(string s, double& x, const char* hlp=0) adds a real item with label s and variable x to W. panel_item W.color_item(string s, color& x, const char* hlp=0) adds a color item with label s and variable x to W. panel_item W.color_item(string s, color& x, void (*F)(int), const char* hlp=0) as above with action function F. panel_item W.color_item(string s, color& x, const window_handler& obj, const char* hlp=0) as above with handler object obj. panel_item W.pstyle_item(string s, point_style& x, const char* hlp=0) adds a point style item with label s and variable x to W. panel_item W.pstyle_item(string s, point_style& x, void(*F)(int), const char* hlp=0) as above with action function F. panel_item W.pstyle_item(string s, point_style& x, const window_handler& obj, const char* hlp=0) as above with handler object obj. panel_item W.lstyle_item(string s, line_style& x, const char* hlp=0) adds a line style item with label s and variable x to W. panel_item W.lstyle_item(string s, line_style& x, void(*F)(int), const char* hlp=0) as above with action function F. panel_item W.lstyle_item(string s, line_style& x, const window_handler& obj, const char* hlp=0) as above with handler object obj. panel_item W.lwidth_item(string s, int& x, const char* hlp=0) adds a line width item with label s and variable x to W. panel_item W.lwidth_item(string s, int& x, void(*F)(int), const char* hlp=0) as above with action function F. panel_item W.lwidth_item(string s, int& x, const window_handler& obj, const char* hlp=0) as above with handler object obj.

3.9.3 Integer Choice Items

 panel_item W.int_item(string s, int& x, int l, int h, int step, const char* hlp=0) adds an integer choice item with label s, variable x, range l,..., h, and step size step to W. panel_item W.int_item(string s, int& x, int l, int h, int step, void (*F)(int), const char* hlp=0) adds an integer choice item with label s, variable x, range l,..., h, and step size step to W. Function F(x) is executed whenever the value of x is changed. panel_item W.int_item(string s, int& x, int l, int h, int step, const window_handler& obj, const char* hlp=0) as above with handler object obj. panel_item W.int_item(string s, int& x, int l, int h, const char* hlp=0) adds an integer slider item with label s, variable x, and range l,...,h to W. panel_item W.int_item(string s, int& x, int l, int h, void (*F)(int), const char* hlp=0) adds an integer slider item with label s, variable x, and range l,...,h to W. Function F(x) is executed whenever the value of x has changed by moving the slider. panel_item W.int_item(string s, int& x, int l, int h, const window_handler& obj, const char* hlp=0) as above with handler object obj.

 panel_item W.string_item(string s, string& x, const list& L, const char* hlp=0) adds a string item with label s, variable x, and menu L to W. panel_item W.string_item(string s, string& x, const list& L, const window_handler& obj, const char* hlp=0) as above with handler object obj. panel_item W.string_item(string s, string& x, const list& L, int sz, const char* hlp=0) menu L is displayed in a scroll box of height sz. panel_item W.string_item(string s, string& x, const list& L, int sz, void (*F)(char*), const char* hlp=0) as above with action function F. panel_item W.string_item(string s, string& x, const list& L, int sz, const window_handler& obj, const char* hlp=0) as above with handler object obj. void W.set_menu(panel_item it, const list& L, int sz=0) replaces the menu of string menu item it by a menu for list L (table style if sz = 0 and scroll box with sz entries otherwise).

3.9.5 Choice Items

 panel_item W.choice_item(string s, int& x, const list& L, void (*F)(int)=0, const char* hlp=0) adds an integer item with label s, variable x, and choices from L to W. panel_item W.choice_item(string s, int& x, const list& L, const window_handler& obj, const char* hlp=0) as above with handler object obj. panel_item W.choice_item(string s, int& x, string s1, ..., string sk) adds an integer item with label s, variable x, and choices s1, ..., sk to W (k < = 8). panel_item W.choice_item(string s, int& x, int n, int w, int h, unsigned char** bm, const char* hlp=0) adds an integer item with label s, variable x, and n bitmaps bm[0], ..., bm[n-1] each of width w and height h. panel_item W.choice_item(string s, int& x, int n, int w, int h, unsigned char** bm, void (*F)(int), const char* hlp=0) panel_item W.choice_item(string s, int& x, int n, int w, int h, unsigned char** bm, const window_handler& obj, const char* hlp=0) as above with handler object obj.

3.9.6 Multiple Choice Items

 panel_item W.choice_mult_item(string s, int& x, const list& L, const char* hlp=0) panel_item W.choice_mult_item(string s, int& x, string s1, const char* hlp=0) panel_item W.choice_mult_item(string s, int& x, string s1, string s2, const char* hlp=0) panel_item W.choice_mult_item(string s, int& x, const list& L, void (*F)(int), const char* hlp=0) panel_item W.choice_mult_item(string s, int& x, const list& L, const window_handler& obj, const char* hlp=0) panel_item W.choice_mult_item(string s, int& x, int n, int w, int h, unsigned char** bm, const char* hlp=0) panel_item W.choice_mult_item(string s, int& x, int n, int w, int h, unsigned char** bm, void (*F)(int), const char* hlp=0) panel_item W.choice_mult_item(string s, int& x, int n, int w, int h, unsigned char** bm, const window_handler& obj, const char* hlp=0)

3.9.7 Buttons

The first occurence of character '&' in a button label makes the following character c an accelerator character, i.e., the button can be selected by typing ALT-c from the keyboard.

3.9.8. Manipulating Panel Items and Buttons

Disabling and Enabling Items or buttons

 void W.disable_item(panel_item it) disables panel item it. void W.enable_item(panel_item it) enables panel item it. bool W.is_enabled(panel_item it) tests whether item it is enabled or not. void W.disable_button(int b) disables button b. void W.enable_button(int b) enables button b. void W.disable_buttons() disables all buttons. void W.enable_buttons() enables all buttons. bool W.is_enabled(int b) tests whether button b is enabled or not. void W.disable_panel(bool disable_every_item=true) disables the entire panel section of W. void W.enable_panel() enables the entire panel section of W.

Accessing and Updating Item Data

 void W.set_text(panel_item it, string s) replaces the text of text item it by s. panel_item W.get_item(string s) returns the item with label s and NULL if no such item exists in W. int W.get_button(string s) returns the button with label s and -1 if no such button exists in W. string W.get_button_label(int but) returns the label of button but. void W.set_button_label(int but, string s) sets the label of button but to s. void W.set_button_pixrects(int but, char *pr1, char* pr2) sets the pixrects of button but to pr1 and pr2. window* W.get_window(int but) returns a pointer to the subwindow attached to button but (NULL if but has no subwindow) window* W.set_window(int but, window* M) associates subwindow (menu) *M with button but. Returns a pointer to the window previously attached to but. void W.set_function(int but, void (*F)(int)) assign action function F to button but. void W.set_object(int but, const window_handler& obj) assign handler object obj to button but.

3.9.9. Miscellanous

 void W.redraw_panel() redraw the panel area of W. void W.redraw_panel(panel_item it) redraw item i in the panel area of W. void W.display_help_text(string fname) displays the help text contained in name.hlp. The file name.hlp must exist either in the current working directory or in \$LEDAROOT/incl/Help. void W.set_tooltip(int i, double x0, double y0, double x1, double y1, string txt) inserts a tooltip with id i, rectangle (x0, y0,x1,y1) and text txt into the window. The text is shown when the mouse pointer enters the rectangle. The text disappears as soon as the mouse pointer leaves the rectangle. CAUTION: Currently the method has to be called after the call of W.display(). Setting a tooltip before the call W.display() has no effect. void W.del_tooltip(int i) removes the tooltip with id i.

Example

Example programs can be found on LEDAROOT/demo/win and LEDAROOT/test/win.

Next: Panels ( panel ) Up: Graphics Previous: Colors ( color )   Contents   Index