home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume15 / twm / part01 / add_window.c next >
Encoding:
C/C++ Source or Header  |  1988-06-12  |  12.3 KB  |  411 lines

  1. /*****************************************************************************/
  2. /**       Copyright 1988 by Evans & Sutherland Computer Corporation,        **/
  3. /**                          Salt Lake City, Utah                           **/
  4. /**                                                                         **/
  5. /**                           All Rights Reserved                           **/
  6. /**                                                                         **/
  7. /**    Permission to use, copy, modify, and distribute this software and    **/
  8. /**    its documentation  for  any  purpose  and  without  fee is hereby    **/
  9. /**    granted, provided that the above copyright notice appear  in  all    **/
  10. /**    copies and that both  that  copyright  notice  and  this  permis-    **/
  11. /**    sion  notice appear in supporting  documentation,  and  that  the    **/
  12. /**    name  of Evans & Sutherland  not be used in advertising or publi-    **/
  13. /**    city pertaining to distribution  of the software without  specif-    **/
  14. /**    ic, written prior permission.                                        **/
  15. /**                                                                         **/
  16. /**    EVANS  & SUTHERLAND  DISCLAIMS  ALL  WARRANTIES  WITH  REGARD  TO    **/
  17. /**    THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILI-    **/
  18. /**    TY AND FITNESS, IN NO EVENT SHALL EVANS &  SUTHERLAND  BE  LIABLE    **/
  19. /**    FOR  ANY  SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY  DAM-    **/
  20. /**    AGES  WHATSOEVER RESULTING FROM  LOSS OF USE,  DATA  OR  PROFITS,    **/
  21. /**    WHETHER   IN  AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS    **/
  22. /**    ACTION, ARISING OUT OF OR IN  CONNECTION  WITH  THE  USE  OR PER-    **/
  23. /**    FORMANCE OF THIS SOFTWARE.                                           **/
  24. /*****************************************************************************/
  25.  
  26. /**********************************************************************
  27.  *
  28.  * $Header: add_window.c,v 1.7 88/04/15 07:10:05 tlastran Exp $
  29.  *
  30.  * Add a new window, put the titlbar and other stuff around
  31.  * the window
  32.  *
  33.  * 31-Mar-88 Tom LaStrange        Initial Version.
  34.  *
  35.  **********************************************************************/
  36.  
  37. #ifndef lint
  38. static char RCSinfo[]=
  39. "$Header: add_window.c,v 1.7 88/04/15 07:10:05 tlastran Exp $";
  40. #endif lint
  41.  
  42. #include <stdio.h>
  43. #include "twm.h"
  44. #include "util.h"
  45. #include "resize.h"
  46. #include "gram.h"
  47. #include "list.h"
  48.  
  49. #include "iconify.bm"
  50. #include "resize.bm"
  51. #include "xterm.bm"
  52. #include "focus.bm"
  53. #include "hilite.bm"
  54.  
  55. static char NoName[] = "No Name"; /* name if no name is specified */
  56.  
  57. /***********************************************************************
  58.  *
  59.  *  Procedure:
  60.  *    AddWindow - add a new window to the twm list
  61.  *
  62.  *  Returned Value:
  63.  *    (TwmWindow *) - pointer to the TwmWindow structure
  64.  *
  65.  *  Inputs:
  66.  *    w    - the window id of the window to add
  67.  *
  68.  ***********************************************************************
  69.  */
  70.  
  71. TwmWindow *
  72. AddWindow(w)
  73. Window w;
  74. {
  75.     TwmWindow *tmp_win;            /* new twm window structure */
  76.     unsigned long valuemask;        /* mask for create windows */
  77.     XSetWindowAttributes attributes;    /* attributes for create windows */
  78.     int icon_width;        /* width of the icon pixmap */
  79.     int icon_height;        /* height of the icon pixmap */
  80.     int width;            /* tmp variable */
  81.     int x;
  82.     Pixmap pm;            /* tmp pixmap variable */
  83.     XWindowChanges xwc;        /* change window structure */
  84.     unsigned int xwcm;        /* change window mask */
  85.  
  86. #ifdef DEBUG
  87.     fprintf(stderr, "AddWindow: w = 0x%x\n", w);
  88. #endif
  89.  
  90.     /* allocate space for the twm window */
  91.     tmp_win = (TwmWindow *)malloc(sizeof(TwmWindow));
  92.     tmp_win->w = w;
  93.  
  94.     XGetWindowAttributes(dpy, tmp_win->w, &tmp_win->attr);
  95.     XFetchName(dpy, tmp_win->w, &tmp_win->name);
  96. #ifdef DEBUG
  97.     fprintf(stderr, "  name = \"%s\"\n", tmp_win->name);
  98. #endif
  99.     tmp_win->wmhints = XGetWMHints(dpy, tmp_win->w);
  100.     if (XGetNormalHints(dpy, tmp_win->w, &tmp_win->hints) == 0)
  101.     tmp_win->hints.flags = 0;
  102.  
  103.     if (tmp_win->hints.flags & PSize)
  104.     {
  105. #ifdef DEBUG
  106.     fprintf(stderr, "  program specified hints\n");
  107. #endif
  108.     tmp_win->attr.x = tmp_win->hints.x;
  109.     tmp_win->attr.y = tmp_win->hints.y;
  110.     tmp_win->attr.width = tmp_win->hints.width;
  111.     tmp_win->attr.height = tmp_win->hints.height;
  112.     }
  113.     if (tmp_win->hints.flags & USSize)
  114.     {
  115. #ifdef DEBUG
  116.     fprintf(stderr, "  user specified hints\n");
  117. #endif
  118.     tmp_win->attr.x = tmp_win->hints.x;
  119.     tmp_win->attr.y = tmp_win->hints.y;
  120.     tmp_win->attr.width = tmp_win->hints.width;
  121.     tmp_win->attr.height = tmp_win->hints.height;
  122.     }
  123.  
  124.     if (tmp_win->name == NULL)
  125.     tmp_win->name = NoName;
  126.  
  127.     tmp_win->auto_raise = LookInList(AUTO_RAISE, tmp_win->name);
  128.     if (LookInList(NO_TITLE, tmp_win->name))
  129.     {
  130.     tmp_win->title_height = 0;
  131.     }
  132.     else
  133.     {
  134.     tmp_win->title_height = TITLE_BAR_HEIGHT + BorderWidth;
  135.     }
  136.  
  137.     if (tmp_win->attr.y < tmp_win->title_height)
  138.     tmp_win->attr.y = tmp_win->title_height;
  139.  
  140.     xwcm = CWX | CWY | CWWidth | CWHeight | CWBorderWidth;
  141.  
  142. #ifdef DEBUG
  143.     fprintf(stderr, "  position window  %d, %d  %dx%d\n", 
  144.         tmp_win->attr.x,
  145.         tmp_win->attr.y,
  146.         tmp_win->attr.width,
  147.         tmp_win->attr.height);
  148. #endif
  149.     xwc.x = tmp_win->attr.x + tmp_win->attr.border_width;
  150.     xwc.y = tmp_win->attr.y + tmp_win->attr.border_width;
  151.     xwc.width = tmp_win->attr.width;
  152.     xwc.height = tmp_win->attr.height;
  153.     xwc.border_width = 0;
  154.  
  155.     XConfigureWindow(dpy, tmp_win->w, xwcm, &xwc);
  156.  
  157.  
  158.     tmp_win->full_name = tmp_win->name;
  159.  
  160.     if (strncmp("xterm", tmp_win->name, 5) == 0 ||
  161.     strncmp("yterm", tmp_win->name, 5) == 0 ||
  162.     strncmp("console", tmp_win->name, 7) == 0 ||
  163.     strncmp("login", tmp_win->name, 5) == 0)
  164.     tmp_win->xterm = TRUE;
  165.     else
  166.     tmp_win->xterm = FALSE;
  167.  
  168.     if (strncmp("xterm_", tmp_win->name, 6) == 0)
  169.     tmp_win->name = &tmp_win->name[6];
  170.  
  171.     tmp_win->name_width = XTextWidth(TitleBarFont, tmp_win->name,
  172.     strlen(tmp_win->name));
  173.     tmp_win->icon_name = tmp_win->name;
  174.  
  175.     tmp_win->iconified = FALSE;
  176.     tmp_win->icon = FALSE;
  177.  
  178.     /* add the window into the twm list */
  179.     tmp_win->next = TwmRoot.next;
  180.     if (TwmRoot.next != NULL)
  181.     TwmRoot.next->prev = tmp_win;
  182.     tmp_win->prev = &TwmRoot;
  183.     TwmRoot.next = tmp_win;
  184.  
  185.     /* create windows */
  186.  
  187.     tmp_win->frame_x = tmp_win->attr.x;
  188.     tmp_win->frame_y = tmp_win->attr.y - tmp_win->title_height;
  189.  
  190.     tmp_win->frame = XCreateSimpleWindow(dpy, Root,
  191.     tmp_win->frame_x,
  192.     tmp_win->frame_y,
  193.     tmp_win->attr.width,
  194.     tmp_win->attr.height + tmp_win->title_height,
  195.     BorderWidth,
  196.     Foreground, Background);
  197.  
  198.     tmp_win->title_w = XCreateSimpleWindow(dpy, tmp_win->frame,
  199.     -BorderWidth, -BorderWidth,
  200.     tmp_win->attr.width, TITLE_BAR_HEIGHT,
  201.     BorderWidth,
  202.     Foreground, Background);
  203.  
  204.     /* the three buttons have the pixmap as the background of the
  205.      * window, that way I don't have to worry about repainting them
  206.      * on expose events.
  207.      */
  208.  
  209.     valuemask = CWEventMask | CWBackPixmap;
  210.     attributes.event_mask = ButtonPressMask;
  211.     attributes.background_pixmap = MakePixmap(tmp_win->title_w, TitleNormalGC,
  212.     iconify_bits, iconify_width, iconify_height);
  213.  
  214.     tmp_win->iconify_w = XCreateWindow(dpy, tmp_win->title_w,
  215.     TITLE_BAR_SPACE, TITLE_BAR_SPACE,
  216.     iconify_width, iconify_height,
  217.     0, DefaultDepth(dpy, 0), CopyFromParent, DefaultVisual(dpy,0),
  218.     valuemask, &attributes);
  219.  
  220.     attributes.background_pixmap = MakePixmap(tmp_win->title_w, TitleNormalGC,
  221.     focus_bits, focus_width, focus_height);
  222.  
  223.     tmp_win->focus_w = XCreateWindow(dpy, tmp_win->title_w,
  224.     tmp_win->attr.width - resize_width -3 - focus_width, TITLE_BAR_SPACE,
  225.     iconify_width, iconify_height,
  226.     0, DefaultDepth(dpy, 0), CopyFromParent, DefaultVisual(dpy,0),
  227.     valuemask, &attributes);
  228.  
  229.     attributes.background_pixmap = MakePixmap(tmp_win->title_w, TitleNormalGC,
  230.     resize_bits, resize_width, resize_height);
  231.  
  232.     tmp_win->resize_w = XCreateWindow(dpy, tmp_win->title_w,
  233.     tmp_win->attr.width - resize_width - 1,
  234.     TITLE_BAR_SPACE,
  235.     resize_width, resize_height,
  236.     0, DefaultDepth(dpy, 0), CopyFromParent, DefaultVisual(dpy,0),
  237.     valuemask, &attributes);
  238.     
  239.     valuemask = CWBackPixmap;
  240.     attributes.background_pixmap = MakePixmap(tmp_win->title_w, TitleNormalGC,
  241.     hilite_bits, hilite_width, hilite_height);
  242.  
  243.     tmp_win->hilite_w = XCreateWindow(dpy, tmp_win->title_w,
  244.     TitleBarX, 1,
  245.     10, hilite_height,
  246.     0, DefaultDepth(dpy, 0), CopyFromParent, DefaultVisual(dpy,0),
  247.     valuemask, &attributes);
  248.     
  249.     XDefineCursor(dpy, tmp_win->title_w, ArrowCursor);
  250.     XDefineCursor(dpy, tmp_win->iconify_w, ButtonCursor);
  251.     XDefineCursor(dpy, tmp_win->focus_w, ButtonCursor);
  252.     XDefineCursor(dpy, tmp_win->resize_w, ButtonCursor);
  253.  
  254.     XSelectInput(dpy, tmp_win->w, StructureNotifyMask);
  255.     XSelectInput(dpy, tmp_win->frame,
  256.     ButtonPressMask | EnterWindowMask | LeaveWindowMask);
  257.  
  258.     XSelectInput(dpy, tmp_win->title_w, 
  259.     ButtonPressMask | ButtonReleaseMask |
  260.     ExposureMask | ButtonMotionMask);
  261.  
  262.     XAddToSaveSet(dpy, tmp_win->w);
  263.     XReparentWindow(dpy, tmp_win->w, tmp_win->frame, 0, tmp_win->title_height);
  264.  
  265.     SetupWindow(tmp_win,
  266.     tmp_win->frame_x,
  267.     tmp_win->frame_y,
  268.     tmp_win->attr.width,
  269.     tmp_win->attr.height + tmp_win->title_height);
  270.  
  271.     pm = NULL;
  272.     if (tmp_win->wmhints && tmp_win->wmhints->flags & IconPixmapHint)
  273.     {
  274.     XGetGeometry(dpy, tmp_win->wmhints->icon_pixmap,
  275.         &JunkRoot, &JunkX, &JunkY,
  276.         &icon_width, &icon_height, &JunkBW,
  277.         &JunkDepth);
  278.  
  279.     pm = XCreatePixmap(dpy, Root, icon_width, icon_height,
  280.         DefaultDepth(dpy, 0));
  281.  
  282.     XCopyPlane(dpy, tmp_win->wmhints->icon_pixmap, pm, MenuNormalGC,
  283.         0,0, icon_width, icon_height, 0, 0, 1 );
  284.     }
  285.     else
  286.     {
  287.     if (tmp_win->xterm)
  288.     {
  289.         pm = MakePixmap(Root, MenuNormalGC,
  290.         xterm_bits, xterm_width, xterm_height);
  291.         icon_width = xterm_width;
  292.         icon_height = xterm_height;
  293.     }
  294.     else if (UnknownPm != NULL)
  295.     {
  296.         XImage *image;
  297.         unsigned mask;
  298.  
  299.         icon_width = UnknownWidth;
  300.         icon_height = UnknownHeight;
  301.         mask = Foreground ^ Background;
  302.         image = XGetImage(dpy, UnknownPm, 0, 0, icon_width, icon_height,
  303.         mask, XYPixmap);
  304.  
  305.         pm = XCreatePixmap(dpy, Root, icon_width, icon_height,
  306.         DefaultDepth(dpy, 0));
  307.  
  308.         XPutImage(dpy, pm, TitleNormalGC,
  309.         image, 0, 0, 0, 0, icon_width, icon_height);
  310.     }
  311.     }
  312.  
  313.     if (pm == NULL)
  314.     {
  315.     icon_height = 0;
  316.     icon_width = 0;
  317.     valuemask = 0;
  318.     }
  319.     else
  320.     {
  321.     valuemask = CWBackPixmap;
  322.     attributes.background_pixmap = pm;
  323.     }
  324.  
  325.     width = XTextWidth(IconFont,
  326.     tmp_win->icon_name, strlen(tmp_win->icon_name));
  327.  
  328.     width += 6;
  329.     if (width < icon_width)
  330.     {
  331.     tmp_win->icon_x = (icon_width - width)/2;
  332.     tmp_win->icon_x += 3;
  333.     width = icon_width;
  334.     }
  335.     else
  336.     {
  337.     tmp_win->icon_x = 3;
  338.     }
  339.     tmp_win->icon_y = icon_height + IconFontHeight;
  340.  
  341.     if (tmp_win->wmhints && tmp_win->wmhints->flags & IconWindowHint)
  342.     {
  343.     tmp_win->icon_w = tmp_win->wmhints->icon_window;
  344.     }
  345.     else
  346.     {
  347.     tmp_win->icon_w = XCreateSimpleWindow(dpy, Root,
  348.         0,0,
  349.         width,
  350.         icon_height + IconFontHeight + 4,
  351.         2, Foreground, Background);
  352.     }
  353.  
  354.     XSelectInput(dpy, tmp_win->icon_w,
  355.     ButtonPressMask | ExposureMask);
  356.  
  357.     if (pm != NULL)
  358.     {
  359.     if (width == icon_width)
  360.         x = 0;
  361.     else
  362.         x = (width - icon_width)/2;
  363.  
  364.     XCreateWindow(dpy, tmp_win->icon_w,
  365.         x, 0,
  366.         icon_width, icon_height,
  367.         0, DefaultDepth(dpy, 0), CopyFromParent,
  368.         DefaultVisual(dpy,0),
  369.         valuemask, &attributes);
  370.     }
  371.  
  372.     XDefineCursor(dpy, tmp_win->icon_w, ArrowCursor);
  373.  
  374.     XSaveContext(dpy, tmp_win->w, TwmContext, tmp_win);
  375.     XSaveContext(dpy, tmp_win->frame, TwmContext, tmp_win);
  376.     XSaveContext(dpy, tmp_win->title_w, TwmContext, tmp_win);
  377.     XSaveContext(dpy, tmp_win->iconify_w, TwmContext, tmp_win);
  378.     XSaveContext(dpy, tmp_win->resize_w, TwmContext, tmp_win);
  379.     XSaveContext(dpy, tmp_win->icon_w, TwmContext, tmp_win);
  380.     XSaveContext(dpy, tmp_win->focus_w, TwmContext, tmp_win);
  381.     XSaveContext(dpy, tmp_win->hilite_w, TwmContext, tmp_win);
  382.  
  383.     return (tmp_win);
  384. }
  385.  
  386. /***********************************************************************
  387.  *
  388.  *  Procedure:
  389.  *    MappedNotOverride - checks to see if we should really
  390.  *        put a twm frame on the window
  391.  *
  392.  *  Returned Value:
  393.  *    TRUE    - go ahead and frame the window
  394.  *    FALSE    - don't frame the window
  395.  *
  396.  *  Inputs:
  397.  *    w    - the window to check
  398.  *
  399.  ***********************************************************************
  400.  */
  401.  
  402. int
  403. MappedNotOverride(w)
  404.     Window w;
  405. {
  406.     XWindowAttributes wa;
  407.  
  408.     XGetWindowAttributes(dpy, w, &wa);
  409.     return ((wa.map_state != IsUnmapped) && (wa.override_redirect != True));
  410. }
  411.