home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The World of Computer Software
/
World_Of_Computer_Software-02-385-Vol-1of3.iso
/
n
/
newmarch.zip
/
FONTINFO.C
< prev
next >
Wrap
C/C++ Source or Header
|
1992-09-08
|
6KB
|
239 lines
/* Author: $Author: jan $
* File: $Source: /usr/usrs/jan/desktop/X_Book.boo/programs/RCS/fontinfo.c,v $
* Date: $Date: 1992/09/09 00:09:53 $
* Revision: $Revision: 1.1 $
*/
#include "copyright.h"
/*
** File: onewindow.c
** Generic program schema for a one window program
*/
#include <stdio.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
/*
** debug lists all events to an
** xterm window as they occur
*/
#define DEBUG
char WINDOW_NAME[] = "Window";
char ICON_NAME[] = "Icon";
Display *display; /* the display device */
int screen; /* the screen on the display */
Window main_window;
GC gc;
unsigned long foreground, background;
char string[] = "hello world";
void
fontinfo ()
{
int direction, ascent, descent;
int height, width;
XCharStruct overall;
GContext font;
font = XGContextFromGC(gc);
XQueryTextExtents (display, font,
string, strlen (string),
&direction, &ascent,
&descent,
&overall);
height = ascent + descent;
width = overall.width;
fprintf (stderr, "height : %d, width: %d\n",
height, width);
}
/*
**
*/
Window
openWindow (x, y, width, height, border_width,
argc, argv)
int x, y; /* coords of the upper left
corner in pixels */
int width,
height; /* size of the window in pixels */
int border_width; /* the border width is
not included in the
other dimensions */
int argc;
char **argv;
{
Window new_window;
XSizeHints size_hints;
/* now create the window */
new_window = XCreateSimpleWindow (display,
DefaultRootWindow (display),
x, y, width, height,
border_width,
foreground, background);
/* set up the size hints for the window manager */
size_hints.x = x;
size_hints.y = y;
size_hints.width = width;
size_hints.height = height;
/* and state what hints are included */
size_hints.flags = PPosition | PSize;
/* let the window manager know about the window */
XSetStandardProperties (display, new_window,
WINDOW_NAME, ICON_NAME,
None, /* no icon map */
argv, argc, &size_hints);
/* Decide what events the window will receive */
XSelectInput (display, new_window,
(ButtonPressMask | KeyPressMask |
ExposureMask));
/* Return the window ID */
return (new_window);
}
/*
**
*/
GC
getGC ()
{ GC gc;
XGCValues gcValues;
gc = XCreateGC (display, main_window,
(unsigned long) 0, &gcValues);
XSetBackground (display, gc, background);
XSetForeground (display, gc, foreground);
return (gc);
}
/*
** Terminate the program gracefully
*/
quitX ()
{
XCloseDisplay (display);
exit (0);
}
/*
** An expose event occurs when the contents of
** a window are invalidated and at least some
** of it needs to be redrawn
*/
void
doExposeEvent (pEvent)
XExposeEvent *pEvent;
{
XDrawImageString (display, main_window, gc,
10, 10, "Press q or any button to quit",
strlen ("Press q or any button to quit"));
}
/*
** A button has been pressed within the window - quit
*/
void doButtonPressEvent (pEvent)
XButtonEvent *pEvent;
{
quitX ();
}
/*
** A key has been pressed.
** It needs to be decoded and acted on.
** Quit if it is a 'q'.
*/
void
doKeyPressEvent (pEvent)
XKeyEvent *pEvent;
{ int key_buffer_size = 10;
char key_buffer[9];
XComposeStatus compose_status;
KeySym key_sym;
XLookupString (pEvent, key_buffer,
key_buffer_size,
&key_sym, &compose_status);
if (key_buffer[0] == 'q')
quitX ();
}
void
initX ()
{
/* set the display name from the
** environment vbl DISPLAY */
display = XOpenDisplay (NULL);
if (display == NULL)
{ fprintf (stderr,
"Unable to open display %s\n",
XDisplayName (NULL));
exit (1);
}
screen = DefaultScreen (display);
/* use the default foreground and
** background colors
*/
foreground = BlackPixel (display, screen);
background = WhitePixel (display, screen);
}
main (argc, argv)
int argc;
char **argv;
{ XEvent event;
initX ();
main_window = openWindow (10, 20, 200, 100, 5,
argc, argv);
gc = getGC ();
fontinfo();
/* Display the window on the screen */
XMapWindow (display, main_window);
XFlush (display);
while (True)
{
XNextEvent (display, &event);
#ifdef DEBUG
printf ("Event number is %d\n", event.type);
#endif
switch (event.type)
{
case Expose:
doExposeEvent (&event);
break;
case ButtonPress:
doButtonPressEvent (&event);
break;
case KeyPress:
doKeyPressEvent (&event);
break;
case MappingNotify:
XRefreshKeyboardMapping (&event);
break;
}
}
}