phani@aloha1.eng.hawaii.edu (Phanindra Vemuri) (06/06/89)
Hi,
I had posted this before. But I noticed something else besides what
I have mentioned earlier viz; a green background window appears on the
screen every time the program is run, and which cannot be destroyed unless
I exit X. Also there appears to be no process running which keeps this window
alive...
The "earlier" stuff follows....
I plan to do some graphics on the X windows.However, I do not
want to use the Xlib primitives for window management. My idea
is to use the widgets already provided on X and draw the graphics
in the windows associated with the widget.
I started out with a trial code, in which I created a labelwidget and
by using the display macros obtained the display pointer, the
window I.D., and the screen number. Using these parameters I attempted
to draw a rectengle in this window. Everytime I try to run this program
it gives a "BadDrawable, window or Pixmap" error.
I cannot understand why since I seem to have used the right macros.
Could someone who has done stuff like this please help me out?
I have appended below the source code of both the ways in which I tried
to code it up...
******************************************************************************
METHOD ONE
*****************************************************************************
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/X.h>
#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include <X11/Label.h>
#include <X11/Xos.h>
#include <X11/Viewport.h>
#include <X11/Label.h>
#include <stdio.h>
Display *display;
Drawable root;
XGCValues values;
main(argc, argv)
int argc;
char **argv;
{
Cardinal i;
Arg arglist[10];
int screen;
Window win;
GC gc;
XtAppContext context;
char *display_name = NULL;
int width,height,x = 0,y = 0,depth,border_width = 4,bd_wt =4,wid,ht;
XtToolkitInitialize();
i = 0;
XtSetArg(arglist[i], XtNwidth, 100); i++;
XtSetArg(arglist[i], XtNheight, 150); i++;
context = XtCreateApplicationContext();
printf("after context getting\n");
if((display = XtOpenDisplay(context,display_name,NULL,"XGraph",NULL,NULL,&argc,argv)) == NULL)
{
printf("Could not get Display structure pointer \n");
exit(0);
}
printf("After display opening\n");
screen = DefaultScreen(display);
printf("screen is %d and win is %d\n",screen,win);
/* XDefaultGC(display,screen); */
values.foreground = BlackPixel(display,screen);
values.background = WhitePixel(display,screen);
gc = XCreateGC(display, RootWindow(display,screen), (GCForeground | GCBackground), &values);
printf("After XcreateGc\n");
XGetGeometry(display, RootWindow(display,screen), &root, &x, &y, &width, &height, &border_width, &depth);
wid = width/3;
ht = height/3;
x = 0;
y = 0;
bd_wt = 4;
win = XCreateWindow(display, RootWindow(display,screen), x, y, wid,ht,bd_wt,BlackPixel(display,screen),WhitePixel(display,screen));
/*
width = DisplayWidth(display,screen);
printf("after width\n");
height = DisplayHeight(display,screen);
*/
printf("width is %d and height is %d\n", width, height);
/*
XtRealizeWidget(toplevel); This causes X windows to be created for parent and all children
win = XtWindow(child);
*/
/*win = XConnectionNumber(display); */
draw_graphics(win, gc, width, height);
XtMainLoop();
}
draw_graphics(win,gc,window_width,window_height)
Window win;
GC gc;
int window_width,window_height;
{
int x,y;
unsigned int width,height;
/*
height = window_height/2;
width = 3 * window_width /4;
x = window_width/2 - width/2;
y = window_height/2 - height/2;
*/
x = 0;
y = 0;
height = 50;
width = 50;
printf("x is %d, y is %d , width is %d, height is %d, \n", x,y,width,height);
XDrawPoint(display, win, gc, x, y);
XDrawRectangle(display,win,gc,x,y,width,height);
printf("After drawing rectangle\n");
}
******************************************************************************
METHOD TWO
********************************************************************************
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/X.h>
#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include <X11/Label.h>
#include <X11/Xos.h>
#include <X11/Viewport.h>
#include <X11/Label.h>
#include <stdio.h>
Widget toplevel;
Widget child;
Display *display;
Drawable root;
XGCValues values;
main(argc, argv)
int argc;
char **argv;
{
Cardinal i;
Arg arglist[10];
int screen;
Window win;
XEvent report;
GC gc;
char *display_name = NULL;
int width,height,x = 0,y = 0,depth,border_width = 4,bd_wt =4,wid,ht;
toplevel = XtInitialize("main", "XGraph", NULL, NULL, &argc, argv);
i = 0;
XtSetArg(arglist[i], XtNwidth, 250); i++;
XtSetArg(arglist[i], XtNheight,300); i++;
printf("after Xtinitialize\n");
if( (child = XtCreateManagedWidget(argv[0], labelWidgetClass, toplevel,arglist,i)) == NULL)
{
printf("Could not create widget \n");
exit(0);
}
printf("after create\n");
if( (display = XtDisplay(toplevel)) == 0)
{
printf("Could not get Display structure pointer \n");
exit(0);
}
screen = DefaultScreen(display);
printf("screen is %d and win is %d\n",screen,win);
/* XDefaultGC(display,screen); */
values.foreground = BlackPixel(display,screen);
values.background = WhitePixel(display,screen);
gc = XCreateGC(display, RootWindow(display,screen), (GCForeground | GCBackground), &values);
printf("After XcreateGc\n");
XGetGeometry(display, RootWindow(display,screen), &root, &x, &y, &width, &height, &border_width, &depth);
bd_wt = 4;
/*
width = DisplayWidth(display,screen);
printf("after width\n");
height = DisplayHeight(display,screen);
*/
printf("width is %d and height is %d\n", width, height);
XtRealizeWidget(toplevel); /*This causes X windows to be created for parent and all children */
win = XtWindow(child);
/* win = XConnectionNumber(display); */
draw_graphics(win, gc, width, height);
XtMainLoop();
}
draw_graphics(win,gc,window_width,window_height)
Window win;
GC gc;
int window_width,window_height;
{
int x,y;
unsigned int width,height;
/*
height = window_height/2;
width = 3 * window_width /4;
x = window_width/2 - width/2;
y = window_height/2 - height/2;
*/
x = 0;
y = 0;
height = 100;
width = 100;
printf("x is %d, y is %d , width is %d, height is %d, \n", x,y,width,height);
XDrawPoint(display, win, gc, x, y);
XDrawRectangle(display,win,gc,x,y,width,height);
printf("After drawing rectangle\n");
}
My e-mail addresses are
1. phani@wiliki.eng.hawaii.edu
2. phani@aloha1.eng.hawaii.edu