/X11/ex5/example5.c
https://github.com/nonstop/sandbox · C · 165 lines · 120 code · 22 blank · 23 comment · 8 complexity · b14a1fde599895cac34925d1b4dfe4cf MD5 · raw file
- #include <X11/Xlib.h>
- #include <X11/Xutil.h>
- #include <X11/Xos.h>
- #include <stdio.h>
- #include <string.h>
- #include "stdlib.h"
-
- #define X 0
- #define Y 0
- #define WIDTH 300
- #define HEIGHT 300
- #define WIDTH_MIN 100
- #define HEIGHT_MIN 100
- #define BORDER_WIDTH 5
- #define TITLE "Draw line!"
- #define ICON_TITLE "Draw line!"
- #define PRG_CLASS "Draw line!"
-
- /*
- * SetWindowManagerHints - ÐÒÏÃÅÄÕÒÁ ÐÅÒÅÄÁÅÔ ÉÎÆÏÒÍÁÃÉÀ Ï
- * Ó×ÏÊÓÔ×ÁÈ ÐÒÏÇÒÁÍÍÙ ÍÅÎÅÄÖÅÒÕ ÏËÏÎ.
- */
-
- static void SetWindowManagerHints (
- Display * display, /*õËÁÚÁÔÅÌØ ÎÁ ÓÔÒÕËÔÕÒÕ Display */
- char * PClass, /*ëÌÁÓÓ ÐÒÏÇÒÁÍÍÙ */
- char * argv[], /*áÒÇÕÍÅÎÔÙ ÐÒÏÇÒÁÍÍÙ */
- int argc, /*þÉÓÌÏ ÁÒÇÕÍÅÎÔÏ× */
- Window window, /*éÄÅÎÔÉÆÉËÁÔÏÒ ÏËÎÁ */
- int x, /*ëÏÏÒÄÉÎÁÔÙ ÌÅ×ÏÇÏ ×ÅÒÈÎÅÇÏ */
- int y, /*ÕÇÌÁ ÏËÎÁ */
- int win_wdt,
- int win_hgt, /*ûÉÒÉÎÁ É ×ÙÓÏÔÁ ÏËÎÁ */
- int win_wdt_min,
- int win_hgt_min, /*íÉÎÉÍÁÌØÎÙÅ ÛÉÒÉÎÁ É ×ÙÓÏÔÁ ÏËÎÁ */
- char * ptrTitle, /*úÁÇÏÌÏ×ÏË ÏËÎÁ */
- char * ptrITitle,/*úÁÇÏÌÏ×ÏË ÐÉËÔÏÇÒÁÍÍÙ ÏËÎÁ */
- Pixmap pixmap /*òÉÓÕÎÏË ÐÉËÔÏÇÒÁÍÍÙ */
- )
- {
- XSizeHints size_hints; /*òÅËÏÍÅÎÄÁÃÉÉ Ï ÒÁÚÍÅÒÁÈ ÏËÎÁ*/
-
- XWMHints wm_hints;
- XClassHint class_hint;
- XTextProperty windowname, iconname;
-
- if ( !XStringListToTextProperty (&ptrTitle, 1, &windowname ) ||
- !XStringListToTextProperty (&ptrITitle, 1, &iconname ) ) {
- puts ( "No memory!\n");
- exit ( 1 );
- }
-
- size_hints.flags = PPosition | PSize | PMinSize;
- size_hints.min_width = win_wdt_min;
- size_hints.min_height = win_hgt_min;
- wm_hints.flags = StateHint | IconPixmapHint |
- InputHint;
- wm_hints.initial_state = NormalState;
- wm_hints.input = True;
- wm_hints.icon_pixmap= pixmap;
-
- class_hint.res_name = argv[0];
- class_hint.res_class = PClass;
-
- XSetWMProperties ( display, window, &windowname,
- &iconname, argv, argc, &size_hints, &wm_hints,
- &class_hint );
- }
-
- /*
- *main - ÏÓÎÏ×ÎÁÑ ÐÒÏÃÅÄÕÒÁ ÐÒÏÇÒÁÍÍÙ
- */
-
- int main(int argc, char *argv[])
- {
- Display *display; /* õËÁÚÁÔÅÌØ ÎÁ ÓÔÒÕËÔÕÒÕ Display */
- int screen_number; /* îÏÍÅÒ ÜËÒÁÎÁ */
- GC gc;
- XEvent report;
- Window window;
- int new_x, new_y,
- x = 0,
- y = 0;
- int button;
-
- /* õÓÔÁÎÁ×ÌÉ×ÁÅÍ Ó×ÑÚØ Ó ÓÅÒ×ÅÒÏÍ */
- if ( ( display = XOpenDisplay ( NULL ) ) == NULL ) {
- puts ("Can not connect to the X server!\n");
- exit ( 1 );
- }
-
- /* ðÏÌÕÞÁÅÍ ÎÏÍÅÒ ÏÓÎÏ×ÎÏÇÏ ÜËÒÁÎÁ */
- screen_number = DefaultScreen ( display );
-
- /* óÏÚÄÁÅÍ ÏËÎÏ */
- window = XCreateSimpleWindow ( display,
- RootWindow ( display, screen_number ),
- X, Y, WIDTH, HEIGHT, BORDER_WIDTH,
- BlackPixel ( display, screen_number ),
- WhitePixel ( display, screen_number ) );
-
- /* úÁÄÁÅÍ ÒÅËÏÍÅÎÄÁÃÉÉ ÄÌÑ ÍÅÎÅÄÖÅÒÁ ÏËÏÎ */
- SetWindowManagerHints ( display, PRG_CLASS, argv, argc,
- window, X, Y, WIDTH, HEIGHT, WIDTH_MIN,
- HEIGHT_MIN, TITLE, ICON_TITLE, 0 );
-
- /* ÷ÙÂÉÒÁÅÍ ÓÏÂÙÔÉÑ, ÏÂÒÁÂÁÔÙ×ÁÅÍÙÅ ÐÒÏÇÒÁÍÍÏÊ */
- XSelectInput ( display, window, ExposureMask | KeyPressMask | ButtonPressMask |
- ButtonReleaseMask | PointerMotionMask | Button1MotionMask | Button2MotionMask );
-
- /* ðÏËÁÚÙ×ÁÅÍ ÏËÎÏ */
- XMapWindow ( display, window );
-
- /* ãÉËÌ ÐÏÌÕÞÅÎÉÑ É ÏÂÒÁÂÏÔËÉ ÏÛÉÂÏË */
- while ( 1 ) {
- XNextEvent ( display, &report );
- gc = XCreateGC ( display, window, 0 , NULL );
-
- switch ( report.type ) {
- case Expose :
- /* úÁÐÒÏÓ ÎÁ ÐÅÒÅÒÉÓÏ×ËÕ */
- if ( report.xexpose.count != 0 )
- break;
- XDrawString( display, window, gc, 50, 50, "Push button to draw", strlen("Push button to draw") );
- break;
-
- case ButtonPress:
- /* ÓÏÈÒÁÎÑÅÍ ËÏÏÒÄÉÎÁÔÙ ËÎÏÐËÉ ÍÙÛÉ × ÃÅÌÙÈ ÐÅÒÅÍÅÎÎÙÈ */
- /* ÔÁËÖÅ ÓÏÈÒÁÎÑÅÍ ÉÄÅÎÔÉÆÉËÁÔÏÒ ÏËÎÁ, × ËÏÔÏÒÏÍ ÂÙÌÁ */
- /* ÎÁÖÁÔÁ ËÎÏÐËÁ ÍÙÛÉ */
- x = new_x;
- y = new_y;
- new_x = report.xbutton.x;
- new_y = report.xbutton.y;
- window = report.xbutton.window;
- /* ÐÒÏ×ÅÒÑÅÍ, ËÁËÁÑ ÉÚ ËÎÏÐÏË ÂÙÌÁ ÎÁÖÁÔÁ, */
- switch( report.xbutton.button) {
- case Button1:
- /* ÒÉÓÕÅÍ ÎÏ×ÕÀ ÌÉÎÉÀ Ë ÐÏÚÉÃÉÉ ÍÙÛÉ */
- XDrawLine( display, window, gc, x, y, new_x, new_y );
- break;
- case Button3:
- /* ÏÞÉÝÁÅÍ ÜËÒÁÎ */
- XClearWindow( display, window );
- XDrawString( display, window, gc, 50, 50, "Push button to draw or any key to exit",
- strlen("Push button to draw or any key to exit") );
- break;
- default:
- break;
- }
- /*îÅ ÚÁÂÙ×ÁÊÔÅ ÐÒÏ ÏÔÐÕÓËÁÎÉÅ ËÎÏÐËÉ*/
- case ButtonRelease:
- break;
- /*÷ÙÈÏÄ ÐÒÉ ÎÁÖÁÔÉÉ ÌÀÂÏÊ ËÌÁ×ÉÛÉ*/
- case KeyPress :
- XCloseDisplay ( display );
- exit ( 0 );
- }
-
- XFreeGC ( display, gc );
- }
- return 0;
- }