void balloon_help_create (Display* dpy, Pixel fg, Pixel bg, Pixel shine, Pixel shadow, XFontStruct* font) { if (b_dpy) balloon_help_destroy (); b_dpy = dpy; b_fontStruct = font; b_win = create_window (dpy, bg); b_gc = create_gc (dpy, b_win, fg, bg, b_fontStruct); b_shineGC = create_gc (dpy, b_win, shine, bg, b_fontStruct); b_shadowGC = create_gc (dpy, b_win, shadow, bg, b_fontStruct); create_pixmap_mask (1, 1); b_maskGC = create_gc (dpy, b_mask, bg, fg, b_fontStruct); b_winMapped = False; b_timer = None; b_delay = 500; b_screenWidth = DisplayWidth (b_dpy, DefaultScreen(b_dpy)); b_screenHeight = DisplayHeight (b_dpy, DefaultScreen(b_dpy)); b_lastShape = SHAPE_CONE_FREE; }
static void Initialize(Widget treq,Widget tnew,ArgList args,Cardinal *nargs) { XmTabWidget wid = (XmTabWidget) tnew; wid->tab.raise = 0; wid->tab.cut_size = 0; wid->tab.active_tab = NULL; create_gc(wid); }
int main(int argc, char* argv[]) { Display* display; /* pointer to X Display structure. */ int screen_num; /* number of screen to place the window on. */ Window win; /* pointer to the newly created window. */ unsigned int display_width, display_height; /* height and width of the X display. */ unsigned int width, height; /* height and width for the new window. */ char *display_name = getenv("DISPLAY"); /* address of the X display. */ GC gc; /* GC (graphics context) used for drawing */ /* in our window. */ /* open connection with the X server. */ display = XOpenDisplay(display_name); if (display == NULL) { fprintf(stderr, "%s: cannot connect to X server '%s'\n", argv[0], display_name); exit(1); } /* get the geometry of the default screen for our display. */ screen_num = DefaultScreen(display); display_width = DisplayWidth(display, screen_num); display_height = DisplayHeight(display, screen_num); /* make the new window occupy 1/9 of the screen's size. */ width = (display_width / 3); height = (display_height / 3); printf("window width - '%d'; height - '%d'\n", width, height); /* create a simple window, as a direct child of the screen's */ /* root window. Use the screen's white color as the background */ /* color of the window. Place the new window's top-left corner */ /* at the given 'x,y' coordinates. */ win = create_simple_window(display, width, height, 0, 0); /* allocate a new GC (graphics context) for drawing in the window. */ gc = create_gc(display, win, 0); XSync(display, False); /* draw one pixel near each corner of the window */ XDrawPoint(display, win, gc, 5, 5); XDrawPoint(display, win, gc, 5, height-5); XDrawPoint(display, win, gc, width-5, 5); XDrawPoint(display, win, gc, width-5, height-5); /* draw two intersecting lines, one horizontal and one vertical, */ /* which intersect at point "50,100". */ XDrawLine(display, win, gc, 50, 0, 50, 200); XDrawLine(display, win, gc, 0, 100, 200, 100); /* now use the XDrawArc() function to draw a circle whose diameter */ /* is 30 pixels, and whose center is at location '50,100'. */ XDrawArc(display, win, gc, 50-(30/2), 100-(30/2), 30, 30, 0, 360*64); { XPoint points[] = { {0, 0}, {15, 15}, {0, 15}, {0, 0} }; int npoints = sizeof(points)/sizeof(XPoint); /* draw a small triangle at the top-left corner of the window. */ /* the triangle is made of a set of consecutive lines, whose */ /* end-point pixels are specified in the 'points' array. */ XDrawLines(display, win, gc, points, npoints, CoordModeOrigin); } /* draw a rectangle whose top-left corner is at '120,150', its width is */ /* 50 pixels, and height is 60 pixels. */ XDrawRectangle(display, win, gc, 120, 150, 50, 60); /* draw a filled rectangle of the same size as above, to the left of the */ /* previous rectangle. */ XFillRectangle(display, win, gc, 60, 150, 50, 60); /* flush all pending requests to the X server. */ XFlush(display); /* make a delay for a short period. */ sleep(4); /* close the connection to the X server. */ XCloseDisplay(display); return(0); }
int main(int argc, char **argv) { Display *display; Window win; //窗口ID Window win_draw; unsigned int width, height; //窗口尺寸 unsigned int border_width = 4; //边界空白 unsigned int display_width, display_height;//屏幕尺寸 int i; int count; XEvent report; unsigned long valuemask = 0; XGCValues values; char *getenv(); XColor color; //draw int x1, y1, x2, y2; // 和X 服务器连接 if ( (display=XOpenDisplay(getenv("DISPLAY"))) == NULL ){ printf("Cannot connect to X server %s"); exit(-1); } //获得缺省的 screen_num screen_num = DefaultScreen(display); //获得屏幕的宽度和高度 display_width = DisplayWidth(display, screen_num); display_height = DisplayHeight(display, screen_num); cmap = DefaultColormap(display, screen_num); //指定所建立窗口的宽度和高度 width = display_width/2; height = display_height/2; //建立窗口 win = XCreateSimpleWindow(display, //display RootWindow(display,screen_num), //父窗口 0, 0, width, height, //位置和大小 border_width, //边界宽度 BlackPixel(display,screen_num), //前景色 WhitePixel(display,screen_num));//背景色 //选择窗口感兴趣的事件掩码 XSelectInput(display, win, ExposureMask | ButtonPressMask | //按下 ButtonReleaseMask | //抬起 ButtonMotionMask | //移动 KeyPressMask | StructureNotifyMask); //建立GC gc = XCreateGC(display, win, valuemask, &values); XSetLineAttributes(display, gc, 3, LineSolid, CapRound, JoinRound); panelgc = create_gc(display, win, "#C0C0C0"); dimgc = create_gc(display, win, "#666666"); lightgc = create_gc(display, win, "#F5F5F5"); //建立绘图窗口 win_draw = XCreateSimpleWindow(display, //display win, //父窗口 10, 10, width - 140,height - 40,//位置和大小 border_width, //边界宽度 BlackPixel(display,screen_num), //前景色 WhitePixel(display,screen_num));//背景色 //选择窗口感兴趣的事件掩码 XSelectInput(display, win_draw, ExposureMask | ButtonPressMask | //按下 ButtonReleaseMask | //抬起 ButtonMotionMask | //移动 KeyPressMask | StructureNotifyMask); //建立按钮窗口 for(i=0; i<4; i++) create_button(i, display, win, width - 100, i * 30 + 30, 80, 20); //显示窗口 XMapWindow(display, win); XMapWindow(display, win_draw); //进入事件循环 while (1) { //取得队列中的事件 XNextEvent(display, &report); switch (report.type) { //曝光事件, 窗口应重绘 case Expose: //取得最后一个曝光事件 if (report.xexpose.count != 0) break; XClearArea(display, win, 0, 0, width, height, False); for(i=0;i<4;i++) button_flush(i, display); break; //窗口尺寸改变, 重新取得窗口的宽度和高度 case ConfigureNotify: width = report.xconfigure.width; height = report.xconfigure.height; break; //鼠标左键开始绘图. case ButtonPress: if(report.xbutton.button == Button1 && report.xbutton.window == win_draw){ x1 = report.xbutton.x; y1 = report.xbutton.y; XDrawPoint(display, win_draw, gc,x1,y1); } else if(report.xbutton.button == Button1 && report.xbutton.window == buttons[0].win){ process_button(0, display); } else if(report.xbutton.button == Button1 && report.xbutton.window == buttons[1].win){ process_button(1, display); } else if(report.xbutton.button == Button1 && report.xbutton.window == buttons[2].win){ process_button(2, display); } else if(report.xbutton.button == Button1 && report.xbutton.window == buttons[3].win){ process_button(3, display); } else if(report.xbutton.button == Button3){ XFreeGC(display, gc); exit(1); } break; case ButtonRelease: if(report.xbutton.button == Button1 && report.xbutton.window == win_draw){ x2 = report.xbutton.x; y2 = report.xbutton.y; XDrawLine(display, win_draw, gc, x1, y1, x2, y2); } break; case MotionNotify: if(report.xmotion.state & Button1Mask && report.xmotion.window == win_draw){ x2 = report.xmotion.x; y2 = report.xmotion.y; XDrawLine(display, win_draw, gc, x1, y1, x2, y2); x1 = x2; y1 = y2; } break; case KeyPress: XFreeGC(display, gc); exit(1); default: break; } } }
void drawer () { int x,i,j,imagewidth,left = 0,outside,r,k; float s,realcentre,imgcentre,realx,imgx; Display* display; /* pointer to X Display structure. */ int screen_num; /* number of screen to place the window on. */ Window win; /* pointer to the newly created window. */ unsigned int display_width, display_height; /* height and width of the X display. */ unsigned int width, height; /* height and width for the new window. */ char *display_name = getenv("DISPLAY"); /* address of the X display. */ GC gc; /* GC (graphics context) used for drawing */ /* in our window. */ /* open connection with the X server. */ display = XOpenDisplay(display_name); if (display == NULL) { fprintf(stderr, "cannot connect to X server '%s'\n",display_name); exit(EXIT_FAILURE); } /* get the geometry of the default screen for our display. */ screen_num = DefaultScreen(display); display_width = DisplayWidth(display, screen_num); display_height = DisplayHeight(display, screen_num); /* make the new window occupy much of the screen's size. */ width = (display_width * 0.5); height = (display_height * 0.5); // printf("window width - '%d'; height - '%d'\n", width, height); /* create a simple window, as a direct child of the screen's */ /* root window. Use the screen's white color as the background */ /* color of the window. Place the new window's top-left corner */ /* at the given 'x,y' coordinates. */ win = create_simple_window(display, width, height, 0, 0); /* allocate a new GC (graphics context) for drawing in the window. */ gc = create_gc(display, win, 0); XSync(display, False); k = -15; imagewidth = 1000; x = 1; r = 2; s = (float)(2*r)/imagewidth; realcentre = 0.0; imgcentre = 0.0; for(i=0;i<imagewidth;i++){ for(j=0;j<imagewidth;j++){ realx = s*(j-imagewidth/2) + realcentre; imgx = s*(i-imagewidth/2) + imgcentre; outside = testmal(realx,imgx,k); if (outside == 0){ // printf("%f %f\n",realx,imgx); XDrawPoint(display, win, gc, left+j/x, height/2-(imagewidth/2)/x+i/x); /*Flush all pending requests to the X server.*/ XFlush(display); } } } /* make a delay for a short period. */ sleep(300); /* close the connection to the X server. */ XCloseDisplay(display); }
void main(int argc, char* argv[]) { Display* display; /* pointer to X Display structure. */ int screen_num; /* number of screen to place the window on. */ Window win; /* pointer to the newly created window. */ unsigned int display_width, display_height; /* height and width of the X display. */ unsigned int width, height; /* height and width for the new window. */ char *display_name = getenv("DISPLAY"); /* address of the X display. */ GC gc; /* GC (graphics context) used for drawing */ /* in our window. */ /* open connection with the X server. */ display = XOpenDisplay(display_name); if (display == NULL) { fprintf(stderr, "%s: cannot connect to X server '%s'\n", argv[0], display_name); exit(1); } /* get the geometry of the default screen for our display. */ screen_num = DefaultScreen(display); display_width = DisplayWidth(display, screen_num); display_height = DisplayHeight(display, screen_num); /* make the new window occupy 1/9 of the screen's size. */ width = (display_width / 3); height = (display_height / 3); printf("window width - '%d'; height - '%d'\n", width, height); /* create a simple window, as a direct child of the screen's */ /* root window. Use the screen's white color as the background */ /* color of the window. Place the new window's top-left corner */ /* at the given 'x,y' coordinates. */ win = create_simple_window(display, width, height, 0, 0); /* allocate a new GC (graphics context) for drawing in the window. */ gc = create_gc(display, win, 0); XSync(display, False); { /* this variable will contain the ID of the newly created pixmap. */ Pixmap bitmap; /* these variables will contain the dimensions of the loaded bitmap. */ unsigned int bitmap_width, bitmap_height; /* these variables will contain the location of the hotspot of the */ /* loaded bitmap. */ int hotspot_x, hotspot_y; /* load the bitmap found in the file "icon.bmp", create a pixmap */ /* containing its data in the server, and put its ID in the 'bitmap' */ /* variable. */ int rc = XReadBitmapFile(display, win, "icon.bmp", &bitmap_width, &bitmap_height, &bitmap, &hotspot_x, &hotspot_y); /* check for failure or success. */ switch (rc) { case BitmapOpenFailed: fprintf(stderr, "XReadBitmapFile - could not open file 'icon.bmp'.\n"); exit(1); break; case BitmapFileInvalid: fprintf(stderr, "XReadBitmapFile - file '%s' doesn't contain a valid bitmap.\n", "icon.bmp"); exit(1); break; case BitmapNoMemory: fprintf(stderr, "XReadBitmapFile - not enough memory.\n"); exit(1); break; } /* start drawing the given pixmap on to our window. */ { int i, j; for(i=0; i<6; i++) { for(j=0; j<6; j++) { XCopyPlane(display, bitmap, win, gc, 0, 0, bitmap_width, bitmap_height, j*bitmap_width, i*bitmap_height, 1); XSync(display, False); usleep(100000); } } } } /* flush all pending requests to the X server. */ XFlush(display); /* make a delay for a short period. */ sleep(4); /* close the connection to the X server. */ XCloseDisplay(display); }
int main(int argc, char *argv[]) { Display* display; int screen_num; Window win; unsigned int display_width, display_height; unsigned int win_width, win_height; char *display_name = getenv("DISPLAY"); /*用于绘制的图形环境*/ GC gc; /*不同xlib函数的返回值,应该就是一个整数*/ /*int rc;*/ Status rc; Pixmap bitmap; unsigned int bitmap_width, bitmap_height; int hotspot_x, hotspot_y; int i,j; display = XOpenDisplay(display_name); if (display == NULL) { fprintf(stderr, "%s: cannot connect to X server '%s'\n", argv[0], display_name); exit(1); } /******窗口的创建******/ screen_num = DefaultScreen(display); display_width = DisplayWidth(display, screen_num); display_height = DisplayHeight(display, screen_num); win_width = (display_width / 3); win_height = (display_height / 3); win = create_simple_window(display, win_width, win_height, 0, 0); /******图形上下文的创建******/ /*图形上下文环境,提供绘图环境包含前景色背景色线风格等各种属性*/ gc = create_gc(display, win); /*这个函数刷新所有的输出缓存直至所有事件被Xserver收到并处理。 *输入参数为False将会不会忽略所有排队事件, 如果为True则忽略所有事件包括该函数调用之前的排队事件。 * */ XSync(display, False); /******在这里开始绘图******/ /*使用当前路径的icon.bmp文件,不是windows的bmp,它是个数组,和xpm格式也不同*/ rc = XReadBitmapFile(display, win, "icon.bmp", &bitmap_width, &bitmap_height, &bitmap, &hotspot_x, &hotspot_y); /*xpm格式的文件无法读取*/ /*rc = XReadBitmapFile(display, win, "home.xpm", &bitmap_width, &bitmap_height, &bitmap, &hotspot_x, &hotspot_y);*/ /*如果读取文件错误的出错处理*/ switch (rc) { case BitmapOpenFailed: fprintf(stderr, "XReadBitmapFile - could not open file 'icon.bmp'.\n"); exit(1); break; case BitmapFileInvalid: fprintf(stderr, "XReadBitmapFile - file '%s' doesn't contain a valid bitmap.\n", "icon.bmp"); exit(1); break; case BitmapNoMemory: fprintf(stderr, "XReadBitmapFile - not enough memory.\n"); exit(1); break; } /*不用上面的方法,用下面的方法获得图片也行,这需要把icon.bmp包含成为头文件 * 这里面的icon_bitmap_bits就是这个icon.bmp对应的表示图像的数组 * 另外宽度和高度这里设成20,如果不是这个数,那么绘制出来的可能乱了*/ /*bitmap_width = 20; bitmap_height = 20; bitmap = XCreateBitmapFromData(display, win, icon_bitmap_bits, bitmap_width, bitmap_height); if (!bitmap) { fprintf(stderr, "XCreateBitmapFromData - error creating pixmap\n"); exit(1); } */ /*这里绘制了36个图标*/ for(i=0; i<6; i++) { for(j=0; j<6; j++) { /*这里在指定的window上面绘制图片,各个参数如下: * bitmap指定绘制的源(矩形) * win指定绘制的目标(矩形) * gc图形上下文环境,提供绘图环境包含前景色背景色线风格等各种属性 * (0,0)表示相对于源的左上角坐标 * (bitmap_width,bitmap_height)表示绘制的大小 * (j*bitmap_width,i*bitmap_height)表示相对于目标的左上角坐标 * 1和色深位相关,不太清楚。 * */ XCopyPlane(display, bitmap, win, gc, 0, 0, bitmap_width, bitmap_height, j*bitmap_width, i*bitmap_height, 1); /*刷新输出缓存直至所有的到X的请求被收到并处理 *这里如果传入参数False就不会忽略事件队列中的事件了*/ XSync(display, False);//如果没有这句话不会动态一个个的绘制 usleep(100000); } } /*把所有的请求通知给X server,我尝试过了,不用这一句也行,因为前面有XSync*/ /*XFlush函数刷新输出缓存,由于输出缓存通过调用XPending,XNextEvent,XWinEvents *根据需要会被自动刷新,大多数客户程序是不用调用它的。 *服务端发起的事件可能被放到库事件队列中的. * */ XFlush(display); /******延迟5秒后释放资源,结束程序******/ sleep(10); XCloseDisplay(display); return 0; }
int main(int argc, char* argv[]) { int sockfd, oldsockfd, portno, n; socklen_t clilen; struct sockaddr_in serv_addr, cli_addr; struct hostent *server; pthread_t recv_thread; Colormap map; Display* display; int screen_num; Window win; unsigned int display_width, display_height; unsigned int width, height; char *display_name = getenv("DISPLAY"); GC gc, his_gc, refresh_gc; if(argc == 2) { // We are server sending = 1; if(argc < 2) { fprintf(stderr,"ERROR, no port provided\n"); exit(1); } oldsockfd = socket(AF_INET, SOCK_STREAM, 0); if(oldsockfd < 0) error("ERROR opening socket"); bzero((char *) &serv_addr, sizeof(serv_addr)); portno = atoi(argv[1]); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = INADDR_ANY; serv_addr.sin_port = htons(portno); int on = 1; if(setsockopt(oldsockfd, SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof(on)) < 0) error("ERROR on sockopt"); if(bind(oldsockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) error("ERROR on binding"); listen(oldsockfd,5); clilen = sizeof(cli_addr); sockfd = accept(oldsockfd, (struct sockaddr *) &cli_addr, &clilen); if (sockfd < 0) error("ERROR on accept"); } else if(argc == 3) { // We are client sending = 1; portno = atoi(argv[2]); sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) error("ERROR opening socket"); server = gethostbyname(argv[1]); if(server == NULL) { fprintf(stderr,"ERROR, no such host\n"); exit(0); } bzero((char *) &serv_addr, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; bcopy((char *)(server->h_addr), (char *)(&serv_addr.sin_addr.s_addr), server->h_length); serv_addr.sin_port = htons(portno); if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) error("ERROR connecting"); } else { server = 0; } display = XOpenDisplay(display_name); if (display == NULL) { fprintf(stderr, "%s: cannot connect to X server '%s'\n", argv[0], display_name); exit(1); } screen_num = DefaultScreen(display); display_width = DisplayWidth(display, screen_num); display_height = DisplayHeight(display, screen_num); width = (display_width / 3); height = (display_height / 3); Atom wm_delete; win = create_simple_window(display, &wm_delete, width, height, 0, 0); XGCValues values; values.graphics_exposures = 0; gc = create_gc(display, win); his_gc = create_gc(display, win); refresh_gc = XCreateGC(display, win, GCGraphicsExposures, &values); XSetForeground(display, refresh_gc, WhitePixel(display, screen_num)); XSetBackground(display, refresh_gc, WhitePixel(display, screen_num)); map = DefaultColormap(display, 0); XParseColor(display, map, "#FFFFFF", &cols[0]); XAllocColor(display, map, &cols[0]); XParseColor(display, map, "#000000", &cols[1]); XAllocColor(display, map, &cols[1]); XParseColor(display, map, "#FF0000", &cols[2]); XAllocColor(display, map, &cols[2]); XParseColor(display, map, "#00FF00", &cols[3]); XAllocColor(display, map, &cols[3]); XParseColor(display, map, "#0000FF", &cols[4]); XAllocColor(display, map, &cols[4]); XParseColor(display, map, "#FFFF00", &cols[5]); XAllocColor(display, map, &cols[5]); XParseColor(display, map, "#FF00FF", &cols[6]); XAllocColor(display, map, &cols[6]); XParseColor(display, map, "#00FFFF", &cols[7]); XAllocColor(display, map, &cols[7]); XParseColor(display, map, "#999999", &cols[8]); XAllocColor(display, map, &cols[8]); XParseColor(display, map, "#666666", &cols[9]); XAllocColor(display, map, &cols[9]); set_title(display, win); back_buffer = XCreatePixmap(display, win, MAX_WIDTH, MAX_HEIGHT, DefaultDepth(display, screen_num)); XFillRectangle(display, back_buffer, refresh_gc, 0, 0, MAX_WIDTH, MAX_HEIGHT); if(sending) { recv_info_t info; info.display = display; info.window = win; info.gc = gc; info.his_gc = his_gc; info.refresh_gc = refresh_gc; info.sockfd = sockfd; pthread_create(&recv_thread, NULL, recv_line, &info); } XSelectInput(display, win, ExposureMask | KeyPressMask | ButtonPressMask | Button1MotionMask | StructureNotifyMask); XEvent an_event; while(1) { XNextEvent(display, &an_event); switch(an_event.type) { case ClientMessage: if(an_event.xclient.data.l[0] == wm_delete) quitme(display, gc, his_gc, refresh_gc, sockfd, 0); break; case Expose: handle_expose(display, refresh_gc, (XExposeEvent*)&an_event.xexpose); break; case ConfigureNotify: width = an_event.xconfigure.width; height = an_event.xconfigure.height; break; case ButtonPress: handle_button_down(display, gc, (XButtonEvent*)&an_event.xbutton, width, height); break; case MotionNotify: handle_drag(display, gc, (XButtonEvent*)&an_event.xbutton, width, height, sockfd); break; case KeyPress: handle_key(display, win, gc, his_gc, refresh_gc, map, (XKeyEvent*)&an_event.xkey, sockfd); break; default: break; } } quitme(display, gc, his_gc, refresh_gc, sockfd, 0); }
int main(int argc, char* argv[]) { Display* display; /* pointer to X Display structure. */ int screen_num; /* number of screen to place the window on. */ Window win; /* pointer to the newly created window. */ unsigned int display_width, display_height; /* height and width of the X display. */ unsigned int width, height; /* height and width for the new window. */ char *display_name = getenv("DISPLAY"); /* address of the X display. */ GC gc; /* GC (graphics context) used for drawing */ /* in our window. */ Cursor font_cursor, /* handles for the cursors we will create. */ icon_cursor; /* open connection with the X server. */ display = XOpenDisplay(display_name); if (display == NULL) { fprintf(stderr, "%s: cannot connect to X server '%s'\n", argv[0], display_name); exit(1); } /* get the geometry of the default screen for our display. */ screen_num = DefaultScreen(display); display_width = DisplayWidth(display, screen_num); display_height = DisplayHeight(display, screen_num); /* make the new window occupy 1/9 of the screen's size. */ width = (display_width / 3); height = (display_height / 3); printf("window width - '%d'; height - '%d'\n", width, height); /* create a simple window, as a direct child of the screen's */ /* root window. Use the screen's white color as the background */ /* color of the window. Place the new window's top-left corner */ /* at the given 'x,y' coordinates. */ win = create_simple_window(display, width, height, 0, 0); /* allocate a new GC (graphics context) for drawing in the window. */ gc = create_gc(display, win, 0); XSync(display, False); /* play with cursors... */ { /* this variable will contain the ID of the newly created pixmap. */ Pixmap bitmap; /* these variables will contain the dimensions of the loaded bitmap. */ unsigned int bitmap_width, bitmap_height; /* these variables will contain the location of the hotspot of the */ /* loaded bitmap. */ int hotspot_x, hotspot_y; /* load the bitmap found in the file "icon.bmp", create a pixmap */ /* containing its data in the server, and put its ID in the 'bitmap' */ /* variable. */ int rc = XReadBitmapFile(display, win, "icon.bmp", &bitmap_width, &bitmap_height, &bitmap, &hotspot_x, &hotspot_y); /* check for failure or success. */ switch (rc) { case BitmapOpenFailed: fprintf(stderr, "XReadBitmapFile - could not open file 'icon.bmp'.\n"); exit(1); break; case BitmapFileInvalid: fprintf(stderr, "XReadBitmapFile - file '%s' doesn't contain a valid bitmap.\n", "icon.bmp"); exit(1); break; case BitmapNoMemory: fprintf(stderr, "XReadBitmapFile - not enough memory.\n"); exit(1); break; } /* create a 'watch' cursor. */ font_cursor = XCreateFontCursor(display, XC_watch); /* attach this cursor to our window. */ XDefineCursor(display, win, font_cursor); XSync(display, False); /* make a short delay. */ sleep(3); /* detach this cursor from our window. */ XUndefineCursor(display, win); /* create a 'box' cursor. */ font_cursor = XCreateFontCursor(display, XC_box_spiral); /* attach this cursor to our window. */ XDefineCursor(display, win, font_cursor); XSync(display, False); /* make a short delay. */ sleep(3); /* detach this cursor from our window. */ XUndefineCursor(display, win); /* create a cursor out of our icon's pixmap. */ { /* first, define forground and background colors for the cursor. */ XColor cursor_fg, cursor_bg; /* access the default color map of our screen. */ Colormap screen_colormap = DefaultColormap(display, DefaultScreen(display)); /* allocate black and while colors. */ Status rc = XAllocNamedColor(display, screen_colormap, "black", &cursor_fg, &cursor_fg); if (rc == 0) { fprintf(stderr, "XAllocNamedColor - canot allocate 'black' !\n"); exit(1); } rc = XAllocNamedColor(display, screen_colormap, "white", &cursor_bg, &cursor_bg); if (rc == 0) { fprintf(stderr, "XAllocNamedColor - canot allocate 'white' !\n"); exit(1); } /* finally, generate the cursor. make the 'hot spot' be close to the */ /* top-left corner of the cursor - location (x=5, y=4). */ icon_cursor = XCreatePixmapCursor(display, bitmap, bitmap, &cursor_fg, &cursor_bg, 5, 4); } /* attach the icon cursor to our window. */ XDefineCursor(display, win, icon_cursor); XSync(display, False); /* make a short delay. */ sleep(4); /* detach this cursor from our window. */ XUndefineCursor(display, win); /* finally, free our icon pixmap. */ XFreePixmap(display, bitmap); } /* flush all pending requests to the X server. */ XFlush(display); /* make a delay for a short period. */ sleep(4); /* close the connection to the X server. */ XCloseDisplay(display); return(0); }
void main(int argc, char* argv[]) { Display* display; /* pointer to X Display structure. */ int screen_num; /* number of screen to place the window on. */ Window win; /* pointer to the newly created window. */ unsigned int display_width, display_height; /* height and width of the X display. */ unsigned int width, height; /* height and width for the new window. */ char *display_name = getenv("DISPLAY"); /* address of the X display. */ GC gc, rev_gc; /* GC (graphics context) used for drawing */ /* in our window. */ short pixels[1000][1000]; /* used to store pixels on screen that were */ /* explicitly drawn or erased by the user. */ /* initialize the 'pixels' array to contain 0 values. */ { int x, y; for (x=0; x<1000; x++) for (y=0; y<1000; y++) pixels[x][y] = 0; } /* open connection with the X server. */ display = XOpenDisplay(display_name); if (display == NULL) { fprintf(stderr, "%s: cannot connect to X server '%s'\n", argv[0], display_name); exit(1); } /* get the geometry of the default screen for our display. */ screen_num = DefaultScreen(display); display_width = DisplayWidth(display, screen_num); display_height = DisplayHeight(display, screen_num); /* make the new window occupy 1/9 of the screen's size. */ width = (display_width / 3); height = (display_height / 3); printf("window width - '%d'; height - '%d'\n", width, height); /* create a simple window, as a direct child of the screen's */ /* root window. Use the screen's white color as the background */ /* color of the window. Place the new window's top-left corner */ /* at the given 'x,y' coordinates. */ win = create_simple_window(display, width, height, 0, 0); /* allocate two new GCs (graphics contexts) for drawing in the window. */ /* the first is used for drawing black over white, the second is used */ /* for drawing white over black. */ gc = create_gc(display, win, 0); rev_gc = create_gc(display, win, 1); /* subscribe to the given set of event types. */ XSelectInput(display, win, ExposureMask | KeyPressMask | ButtonPressMask | Button1MotionMask | Button2MotionMask | Button3MotionMask | StructureNotifyMask); /* perform an events loop */ { int done = 0; XEvent an_event; while (!done) { XNextEvent(display, &an_event); switch (an_event.type) { case Expose: /* redraw our window. */ handle_expose(display, gc, rev_gc, (XExposeEvent*)&an_event.xexpose, width, height, pixels); break; case ConfigureNotify: /* update the size of our window, for expose events. */ width = an_event.xconfigure.width; height = an_event.xconfigure.height; break; case ButtonPress: /* invert the pixel under the mouse pointer. */ handle_button_down(display, gc, rev_gc, (XButtonEvent*)&an_event.xbutton, width, height, pixels); break; case MotionNotify: /* invert the pixel under the mouse pointer. */ handle_drag(display, gc, rev_gc, (XButtonEvent*)&an_event.xbutton, width, height, pixels); break; case KeyPress: /* exit the application by braking out of the events loop. */ done = 1; break; default: /* ignore any other event types. */ break; } /* end switch on event type */ } /* end while events handling */ } /* free the GCs. */ XFreeGC(display, gc); XFreeGC(display, rev_gc); /* close the connection to the X server. */ XCloseDisplay(display); }
static void da_expose(Widget w, XtPointer client_data, XtPointer call_data) { XExposeEvent *e = &((XmDrawingAreaCallbackStruct *) call_data) ->event->xexpose; XmString *items, *selected_items, out_string, tmp_string; Boolean underline; int item_count, selected_count; Widget list = (Widget) client_data; Dimension width, height, extent, item_extent, string_height; int i; XmFontList font_list; XRectangle clip; /* Extract items, selected items and font from XmList */ XtVaGetValues(list, XmNitems, &items, XmNitemCount, &item_count, XmNselectedItems, &selected_items, XmNselectedItemCount, &selected_count, XmNfontList, &font_list, NULL); XtVaGetValues(w, XmNwidth, &width, XmNheight, &height, NULL); underline = (selected_count > 0); create_gc(w); extent = 0; out_string = NULL; /* * Form list items into a single compound string, inserting * separators where needed to avoid drawing outside the XmDrawingArea */ for (i = 0; i < item_count; i++) { item_extent = XmStringWidth(font_list, items[i]); if (out_string != NULL && (extent + item_extent > width)) { extent = 0; out_string = string_append(out_string, XmStringSeparatorCreate()); } tmp_string = XmStringConcat(out_string, items[i]); XmStringFree(out_string); out_string = tmp_string; extent = extent + item_extent; } string_height = XmStringHeight(font_list, out_string); clip.x = e->x; clip.y = e->y; clip.width = e->width; clip.height = e->height; XSetClipRectangles(XtDisplay(w), gc, 0, 0, &clip, 1, YXBanded); /* Draw compound string, underlining the selected item if any */ if (underline) XmStringDrawUnderline(XtDisplay(w), XtWindow(w), font_list, out_string, gc, 0, (height - string_height) / 2, width, XmALIGNMENT_CENTER, XmSTRING_DIRECTION_L_TO_R, NULL, selected_items[0]); else XmStringDraw(XtDisplay(w), XtWindow(w), font_list, out_string, gc, 0, (height - string_height) / 2, width, XmALIGNMENT_CENTER, XmSTRING_DIRECTION_L_TO_R, NULL); XmStringFree(out_string); }
void main(int argc, char* argv[]) { Display* display; /* pointer to X Display structure. */ int screen_num; /* number of screen to place the window on. */ Window win; /* pointer to the newly created window. */ unsigned int display_width, display_height; /* height and width of the X display. */ unsigned int win_width, win_height; /* height and width for the new window. */ char *display_name = getenv("DISPLAY"); /* address of the X display. */ GC gc; /* GC (graphics context) used for drawing */ /* in our window. */ /* open connection with the X server. */ display = XOpenDisplay(display_name); if (display == NULL) { fprintf(stderr, "%s: cannot connect to X server '%s'\n", argv[0], display_name); exit(1); } /* get the geometry of the default screen for our display. */ screen_num = DefaultScreen(display); display_width = DisplayWidth(display, screen_num); display_height = DisplayHeight(display, screen_num); /* make the new window occupy 1/9 of the screen's size. */ win_width = (display_width / 3); win_height = (display_height / 3); printf("window width - '%d'; height - '%d'\n", win_width, win_height); /* create a simple window, as a direct child of the screen's */ /* root window. Use the screen's white color as the background */ /* color of the window. Place the new window's top-left corner */ /* at the given 'x,y' coordinates. */ win = create_simple_window(display, win_width, win_height, 0, 0); /* allocate a new GC (graphics context) for drawing in the window. */ gc = create_gc(display, win, 0); XFlush(display); sleep(3); /* example of resizing a window. */ { int i; /* start shrinking our window in a loop. */ for (i=0; i<40; i++) { win_width -= 3; win_height -= 3; XResizeWindow(display, win, win_width, win_height); XFlush(display); usleep(20000); } /* start shrinking our window in a loop. */ for (i=0; i<40; i++) { win_width += 3; win_height += 3; XResizeWindow(display, win, win_width, win_height); XFlush(display); usleep(20000); } } sleep(1); /* example of moving a window. */ { int i; XWindowAttributes win_attr; int x, y; int scr_x, scr_y; Window child_win; /* this variable will store the ID of the parent window of our window. */ Window parent_win; /* first, get the current attributes of our window. */ XGetWindowAttributes(display, win, &win_attr); x = win_attr.x; y = win_attr.y; /* next, find the parent window of our window. */ { /* this variable will store the ID of the root window of the screen */ /* our window is mapped on. */ Window root_win; /* this variable will store an array of IDs of the child windows of */ /* our window. */ Window* child_windows; /* and this one will store the number of child windows our window has. */ int num_child_windows; /* finally, make the query for the above values. */ XQueryTree(display, win, &root_win, &parent_win, &child_windows, &num_child_windows); /* we need to free the list of child IDs, as it was dynamically */ /* allocated by the XQueryTree function. */ XFree(child_windows); } /* next, translate the location coordinates to screen coordinates. */ /* this is done using the root window as the destination window. */ /* this works since the root window always spans the entire screen */ /* area, and thus has its top-left corner always at the top-left */ /* corner of the screen. */ XTranslateCoordinates(display, parent_win, win_attr.root, x, y, &scr_x, &scr_y, &child_win); /* start moving the window to the left. */ for (i=0; i<40; i++) { scr_x -= 3; XMoveWindow(display, win, scr_x, scr_y); XFlush(display); usleep(20000); } /* start moving the window to down. */ for (i=0; i<40; i++) { scr_y += 3; XMoveWindow(display, win, scr_x, scr_y); XFlush(display); usleep(20000); } /* start moving the window to the right. */ for (i=0; i<40; i++) { scr_x += 3; XMoveWindow(display, win, scr_x, scr_y); XFlush(display); usleep(20000); } /* start moving the window up. */ for (i=0; i<40; i++) { scr_y -= 3; XMoveWindow(display, win, scr_x, scr_y); XFlush(display); usleep(20000); } } sleep(1); /* example of iconifying and de-iconifying a window. */ { /* iconify our window. */ XIconifyWindow(display, win, DefaultScreen(display)); XFlush(display); sleep(2); /* de-iconify our window. */ XMapWindow(display, win); XFlush(display); sleep(2); } /* flush all pending requests to the X server. */ XFlush(display); /* make a delay for a short period. */ sleep(2); /* close the connection to the X server. */ XCloseDisplay(display); }
int main(int argc, char* argv[]) { Display* display; Window win; char *display_name = getenv("DISPLAY"); GC gc, rev_gc; Atom wmDelete; struct CEventDescriptor event = { .Type = EVENT_NULL }; InitGUIServer(); ClearScreen(); while (!ProcessGUIServer(&event)) ; display = XOpenDisplay(display_name); if (display == NULL) { fprintf(stderr, "%s: cannot connect to X server '%s'\n", argv[0], display_name); exit(1); } win = create_simple_window(display, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0); wmDelete = XInternAtom(display, "WM_DELETE_WINDOW", True); XSetWMProtocols(display, win, &wmDelete, 1); gc = create_gc(display, win, 0); rev_gc = create_gc(display, win, 1); XSelectInput(display, win, ExposureMask | KeyPressMask | ButtonPressMask | StructureNotifyMask); { int done = 0; XEvent an_event; while (!done) { XNextEvent(display, &an_event); switch (an_event.type) { case Expose: handle_expose(display, gc, rev_gc, (XExposeEvent*)&an_event.xexpose); break; case ButtonPress: handle_button_down(display, gc, rev_gc, (XButtonEvent*)&an_event.xbutton); break; case KeyPress: done = handle_key_down(display, gc, rev_gc, (XKeyEvent*)&an_event.xkey); break; case ClientMessage: done = True; break; default: break; } } } XFreeGC(display, gc); XFreeGC(display, rev_gc); XCloseDisplay(display); return 0; }
void main(int argc, char* argv[]) { Display* display; /* pointer to X Display structure. */ int screen_num; /* number of screen to place the window on. */ Window win; /* pointer to the newly created window. */ unsigned int display_width, display_height; /* height and width of the X display. */ unsigned int width, height; /* height and width for the new window. */ char *display_name = getenv("DISPLAY"); /* address of the X display. */ GC gc; /* GC (graphics context) used for drawing */ /* in our window. */ Colormap screen_colormap; /* color map to use for allocating colors. */ XColor red, brown, blue, yellow, green; /* used for allocation of the given color */ /* map entries. */ Status rc; /* return status of various Xlib functions. */ /* open connection with the X server. */ display = XOpenDisplay(display_name); if (display == NULL) { fprintf(stderr, "%s: cannot connect to X server '%s'\n", argv[0], display_name); exit(1); } /* get the geometry of the default screen for our display. */ screen_num = DefaultScreen(display); display_width = DisplayWidth(display, screen_num); display_height = DisplayHeight(display, screen_num); /* make the new window occupy 1/9 of the screen's size. */ width = (display_width / 3); height = (display_height / 3); printf("window width - '%d'; height - '%d'\n", width, height); /* create a simple window, as a direct child of the screen's */ /* root window. Use the screen's white color as the background */ /* color of the window. Place the new window's top-left corner */ /* at the given 'x,y' coordinates. */ win = create_simple_window(display, width, height, 0, 0); /* allocate a new GC (graphics context) for drawing in the window. */ gc = create_gc(display, win, 0); XSync(display, False); /* get access to the screen's color map. */ screen_colormap = DefaultColormap(display, DefaultScreen(display)); /* allocate the set of colors we will want to use for the drawing. */ rc = XAllocNamedColor(display, screen_colormap, "red", &red, &red); if (rc == 0) { fprintf(stderr, "XAllocNamedColor - failed to allocated 'red' color.\n"); exit(1); } rc = XAllocNamedColor(display, screen_colormap, "brown", &brown, &brown); if (rc == 0) { fprintf(stderr, "XAllocNamedColor - failed to allocated 'brown' color.\n"); exit(1); } rc = XAllocNamedColor(display, screen_colormap, "blue", &blue, &blue); if (rc == 0) { fprintf(stderr, "XAllocNamedColor - failed to allocated 'blue' color.\n"); exit(1); } rc = XAllocNamedColor(display, screen_colormap, "yellow", &yellow, &yellow); if (rc == 0) { fprintf(stderr, "XAllocNamedColor - failed to allocated 'yellow' color.\n"); exit(1); } rc = XAllocNamedColor(display, screen_colormap, "green", &green, &green); if (rc == 0) { fprintf(stderr, "XAllocNamedColor - failed to allocated 'green' color.\n"); exit(1); } /* draw one pixel near each corner of the window */ /* draw the pixels in a red color. */ XSetForeground(display, gc, red.pixel); XDrawPoint(display, win, gc, 5, 5); XDrawPoint(display, win, gc, 5, height-5); XDrawPoint(display, win, gc, width-5, 5); XDrawPoint(display, win, gc, width-5, height-5); /* draw two intersecting lines, one horizontal and one vertical, */ /* which intersect at point "50,100". */ /* draw the line in a brown color. */ XSetForeground(display, gc, brown.pixel); XDrawLine(display, win, gc, 50, 0, 50, 200); XDrawLine(display, win, gc, 0, 100, 200, 100); /* now use the XDrawArc() function to draw a circle whose diameter */ /* is 30 pixels, and whose center is at location '50,100'. */ /* draw the arc in a blue color. */ XSetForeground(display, gc, blue.pixel); XDrawArc(display, win, gc, 50-(30/2), 100-(30/2), 30, 30, 0, 360*64); { XPoint points[] = { {0, 0}, {15, 15}, {0, 15}, {0, 0} }; int npoints = sizeof(points)/sizeof(XPoint); /* draw a small triangle at the top-left corner of the window. */ /* the triangle is made of a set of consecutive lines, whose */ /* end-point pixels are specified in the 'points' array. */ /* draw the triangle in a yellow color. */ XSetForeground(display, gc, yellow.pixel); XDrawLines(display, win, gc, points, npoints, CoordModeOrigin); } /* draw a rectangle whose top-left corner is at '120,150', its width is */ /* 50 pixels, and height is 60 pixels. */ /* draw the rectangle in a black color. */ XSetForeground(display, gc, BlackPixel(display, screen_num)); XDrawRectangle(display, win, gc, 120, 150, 50, 60); /* draw a filled rectangle of the same size as above, to the left of the */ /* previous rectangle. */ /* draw the rectangle in a green color. */ XSetForeground(display, gc, green.pixel); XFillRectangle(display, win, gc, 60, 150, 50, 60); /* flush all pending requests to the X server. */ XFlush(display); /* make a delay for a short period. */ sleep(4); /* close the connection to the X server. */ XCloseDisplay(display); }