home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / x / xlibpr3.zip / winman / Icons.c < prev    next >
C/C++ Source or Header  |  1990-01-15  |  6KB  |  258 lines

  1. /*
  2.  * Copyright 1989 O'Reilly and Associates, Inc.
  3.  * See ../Copyright for complete rights and liability information.
  4.  */
  5. #include <X11/Xlib.h>
  6. #include <X11/Xatom.h>
  7. #include <X11/Xutil.h>
  8. #include <X11/cursorfont.h>
  9.  
  10. #include <stdio.h>
  11.  
  12. extern Display *display;
  13. extern int screen_num;
  14. extern XFontStruct *font_info;
  15.  
  16. typedef struct _windowList {
  17.     struct _windowList *next;
  18.     Window window;
  19.     Window icon;
  20.     Bool own;
  21.     char *icon_name;
  22. } WindowListRec, *WindowList;
  23.     
  24. WindowList Icons = NULL;
  25.  
  26. Bool isIcon(win, x, y, assoc, icon_name, makeicon)
  27. Window win;
  28. int x, y;
  29. Window *assoc;
  30. char *icon_name;
  31. Bool makeicon;
  32. {
  33.     WindowList win_list;
  34.     Window makeIcon();
  35.  
  36.     /* go through linked list of window-icon structures */    
  37.     for (win_list = Icons; win_list; win_list = win_list->next) {
  38.         if (win == win_list->icon) { /* win is icon */
  39.             *assoc = win_list->window; 
  40.             strcpy(icon_name, win_list->icon_name);
  41.             return(True);
  42.         }
  43.         if (win == win_list->window) { /* win is main window */
  44.             *assoc = win_list->icon; 
  45.             strcpy(icon_name, win_list->icon_name);
  46.             return(False);
  47.         }
  48.     }
  49.     /* window not in list means icon not created yet.
  50.      * Create icon and add main window to save set
  51.      * in case window manager dies */
  52.         if (makeicon) {
  53.         *assoc = makeIcon(win, x, y, icon_name);
  54.         XAddToSaveSet(display, win);
  55.     }
  56.     return(False);
  57. }
  58.  
  59. /* NOT NECESSARY DUE TO SAVE SET
  60. this is called when the window manager exits gracefully 
  61.  * to turn all icons back into windows
  62. clearIcons()
  63. {
  64.     WindowList win_list;
  65.  
  66.  
  67.     go through linked list of window-icon structures 
  68.     for (win_list = Icons; win_list; win_list = win_list->next) {
  69.         XUnmapWindow(display, win_list->icon);
  70.         XMapWindow(display, win_list->window);
  71.     }
  72. }
  73. */
  74.  
  75. removeIcon(window)
  76. Window window;
  77. {
  78.     WindowList win_list, win_list1;
  79.  
  80.     for (win_list = Icons; win_list; win_list = win_list->next) 
  81.         if (win_list->window == window) {
  82.             if (win_list->own) 
  83.                 XDestroyWindow(display, win_list->icon);
  84.             break;
  85.         }
  86.     if (win_list) {
  87.         if (win_list==Icons) Icons = Icons->next;
  88.         else 
  89.             for (win_list1 = Icons; win_list1->next; 
  90.                     win_list1 = win_list1->next) 
  91.                 if (win_list1->next == win_list) {
  92.                     win_list1->next = win_list->next;
  93.                     break;
  94.                 };
  95.     }
  96. }
  97.  
  98. char *
  99. getIconName(window)
  100. Window window;
  101. {
  102.     char *name;
  103.  
  104.     if (XGetIconName( display, window, &name )) return( name );
  105.  
  106.     if (XFetchName( display, window, &name )) return( name );
  107.  
  108.     return( "Icon" );
  109. }
  110.  
  111. char *
  112. getDefaultIconSize(window, icon_w, icon_h)
  113. Window window;
  114. int *icon_w, *icon_h;
  115. {
  116.     /* Determine the size of the icon window.  */ 
  117.     char *icon_name;
  118.  
  119.     icon_name = getIconName(window);
  120.  
  121.     *icon_h = font_info->ascent + font_info->descent + 4;
  122.     *icon_w = XTextWidth(font_info, icon_name, strlen(icon_name));
  123.  
  124.     return(icon_name);
  125. }
  126.  
  127. Window makeIcon(window, x, y, icon_name_return)
  128. Window window;    /* associated window. */
  129. int x, y;    /* current mouse position. */
  130. char *icon_name_return;
  131. {
  132.     int icon_x, icon_y;    /* Icon U. L. X and Y coordinates. */
  133.     int icon_w, icon_h;    /* Icon width and height. */
  134.     int icon_bdr;    /* Icon border width. */
  135.     int depth;    /* for XGetGeometry */
  136.     Window root;    /* for XGetGeometry */
  137.     XSetWindowAttributes icon_attrib;    /* for icon creation */
  138.     unsigned long icon_attrib_mask;
  139.     XWMHints *wmhints;    /* see if icon position provided */
  140.     XWMHints *XGetWMHints();
  141.     Window finishIcon();
  142.     char *icon_name;
  143.  
  144.     /*
  145.     * Process window manager hints.
  146.     * If icon window hint exists, use it directly
  147.     * If icon pixmap hint exists, get its size
  148.     * otherwise, get default size.
  149.     * If icon position hint exists, use it
  150.     * otherwise, use the position passed (current mouse position)
  151.     */ 
  152.     if (wmhints = XGetWMHints(display, window)) {
  153.         if (wmhints->flags&IconWindowHint)
  154.             /* icon window was passed; use it as is */
  155.             return(finishIcon(window, wmhints->icon_window, 
  156.                 False, icon_name));
  157.         else if (wmhints->flags&IconPixmapHint) 
  158.         { 
  159.             /* Pixmap was passed.
  160.              * Determine size of icon 
  161.              * window from pixmap. Only 
  162.              * icon_w and icon_h are significant. */ 
  163.                 if (!XGetGeometry(display, wmhints->icon_pixmap, 
  164.                 &root, &icon_x, &icon_y,
  165.                 &icon_w, &icon_h, &icon_bdr, &depth)) {
  166.                     fprintf(stderr, "winman: client passed invalid \
  167.                         icon pixmap." );
  168.                     return( NULL );
  169.                 }
  170.             else {
  171.                 icon_attrib.background_pixmap = wmhints->icon_pixmap;
  172.                 icon_attrib_mask = CWBorderPixel|CWBackPixmap;
  173.             }
  174.         }
  175.         /* else no window or pixmap passed */
  176.         else {
  177.             icon_name = getDefaultIconSize(window, &icon_w, &icon_h);
  178.             icon_attrib_mask = CWBorderPixel | CWBackPixel;
  179.             icon_attrib.background_pixel = (unsigned long) 
  180.                     WhitePixel(display,screen_num);
  181.         }
  182.     }
  183.     /* else no hints at all exist */
  184.     else {
  185.         icon_name = getDefaultIconSize(window, &icon_w, &icon_h);
  186.         icon_attrib_mask = CWBorderPixel | CWBackPixel;
  187.     }
  188.     /* Pad sizes. */
  189.     icon_w += 2;
  190.     icon_h += 2;
  191.  
  192.     strcpy(icon_name_return, icon_name);
  193.     
  194.     /* Set the icon border attributes.  */ 
  195.     icon_bdr = 2;
  196.     icon_attrib.border_pixel = (unsigned long) 
  197.             BlackPixel(display,screen_num);
  198.  
  199.     /* If icon position hint exists, get it.
  200.      * This also checks to see if wmhints is NULL,
  201.      * which it will be if WMHints were never set at all */
  202.     if (wmhints && (wmhints->flags&IconPositionHint)) 
  203.     {
  204.          icon_x = wmhints->icon_x;
  205.          icon_y = wmhints->icon_y;
  206.     } 
  207.     else 
  208.     {
  209.         /* put it where the mouse was */
  210.          icon_x = x;
  211.          icon_y = y;
  212.     }
  213.  
  214.     /* Create the icon window.  */
  215.     return(finishIcon(window, XCreateWindow(display, 
  216.             RootWindow(display, screen_num),
  217.             icon_x, icon_y, icon_w, icon_h,
  218.             icon_bdr, 0, CopyFromParent, CopyFromParent,
  219.             icon_attrib_mask, &icon_attrib),
  220.             True, icon_name));
  221. }
  222.  
  223. Window finishIcon(window, icon, own, icon_name)
  224. Window window, icon;
  225. Bool own;    /* whether winman created the icon window */
  226. char *icon_name;
  227. {
  228.     WindowList win_list;
  229.     Cursor ManCursor;
  230.  
  231.  
  232.     /* if icon window didn't get created, return failure */
  233.      if (icon == NULL) return(NULL);
  234.  
  235.     /*
  236.      * Use the man cursor whenever the mouse is in the icon window.
  237.      */
  238.     ManCursor = XCreateFontCursor(display, XC_man);
  239.     XDefineCursor(display, icon, ManCursor);
  240.         
  241.      /* Select events for the icon window */
  242.     XSelectInput(display, icon, ExposureMask);
  243.         
  244.     /*
  245.      * Set the event window's icon window to be the new icon window.
  246.      */
  247.     win_list = (WindowList) malloc(sizeof(WindowListRec));
  248.     win_list->window = window;
  249.     win_list->icon = icon;
  250.     win_list->own = own;
  251.     win_list->icon_name = icon_name;
  252.     win_list->next = Icons;
  253.     Icons = win_list;
  254.  
  255.     return(icon);
  256. }
  257.  
  258.