home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 4 / FreshFish_May-June1994.bin / bbs / may94 / util / edit / jade.lha / Jade / src / x11_windows.c < prev    next >
C/C++ Source or Header  |  1994-04-19  |  14KB  |  527 lines

  1. /* x11_windows.c -- Window handling for X11
  2.    Copyright (C) 1993, 1994 John Harper <jsh@ukc.ac.uk>
  3.  
  4. This file is part of Jade.
  5.  
  6. Jade is free software; you can redistribute it and/or modify it
  7. under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. Jade is distributed in the hope that it will be useful, but
  12. WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with Jade; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include "jade.h"
  21. #include "jade_protos.h"
  22. #include "revision.h"
  23.  
  24. #include <string.h>
  25. #include <X11/Xutil.h>
  26.  
  27. _PR int sleepwin(VW *);
  28. _PR int unsleep(VW *);
  29. _PR void os_newvw(VW *);
  30. _PR void os_killvw(VW *);
  31. _PR void updatedimensions(VW *);
  32. _PR void _updatedimensions(VW *, int, int);
  33. _PR Window newwindow(VW *, VW *, bool);
  34. _PR void killwindow(VW *);
  35. _PR void activatewin(VW *);
  36. _PR void setvwtitle(VW *);
  37. _PR void setvwpos(VW *, long, long, long, long);
  38. _PR VW *findwindowvw(Window);
  39. _PR int ezreq(u_char *, u_char *, int, ...);
  40. _PR int setfont(VW *);
  41. _PR void unsetfont(VW *);
  42. _PR void resetslptxtitles(TX *);
  43. _PR bool makemousepos(POS *, VW *);
  44. _PR void sys_windows_init(void);
  45.  
  46. #define INPUT_EVENTS ButtonPressMask | ButtonReleaseMask | KeyPressMask \
  47.              | ExposureMask | StructureNotifyMask | FocusChangeMask
  48.  
  49. static XSizeHints SizeHints;
  50. static XClassHint ClassHints = { "jade", "Editor" };
  51. static XWMHints WMHints;
  52.  
  53. /*
  54.  * Let the window-manager handle all iconifying...
  55.  */
  56. int
  57. sleepwin(VW *vw)
  58. {
  59.     XIconifyWindow(XDisplay, vw->vw_Window, XScreen);
  60.     return(TRUE);
  61. }
  62. int
  63. unsleep(VW *vw)
  64. {
  65.     return(TRUE);
  66. }
  67.  
  68. void
  69. os_newvw(VW *vw)
  70. {
  71.     ;
  72. }
  73. void
  74. os_killvw(VW *vw)
  75. {
  76.     ;
  77. }
  78.  
  79. void
  80. updatedimensions(VW *vw)
  81. {
  82.     XWindowAttributes xwa;
  83.     XGetWindowAttributes(XDisplay, vw->vw_Window, &xwa);
  84.     _updatedimensions(vw, xwa.width, xwa.height);
  85. }
  86.  
  87. void
  88. _updatedimensions(VW *vw, int width, int height)
  89. {
  90.     if(vw->vw_Window && !vw->vw_Sleeping)
  91.     {
  92.     vw->vw_WindowSys.ws_MessageLineY = height - vw->vw_FontY - 2;
  93.     vw->vw_WindowSys.ws_MessageFontY = vw->vw_WindowSys.ws_MessageLineY + 2 + vw->vw_Font->ascent;
  94.     vw->vw_XStartPix = 0;
  95.     vw->vw_YStartPix = 0;
  96.     /*
  97.      * only measure *text* area, not where messages go
  98.      */
  99.     vw->vw_XEndPix = width;
  100.     vw->vw_YEndPix = (vw->vw_WindowSys.ws_MessageLineY - 1) - vw->vw_YStartPix;
  101.     vw->vw_XWidthPix = vw->vw_XEndPix - vw->vw_XStartPix;
  102.     vw->vw_YHeightPix = vw->vw_YEndPix - vw->vw_YStartPix;
  103.  
  104.     vw->vw_FontStart = vw->vw_YStartPix + vw->vw_Font->ascent;
  105.  
  106.     vw->vw_MaxX = (vw->vw_XWidthPix / vw->vw_FontX);
  107.     vw->vw_MaxY = (vw->vw_YHeightPix / vw->vw_FontY);
  108.     if((vw->vw_XStepRatio <= 0)
  109.        || ((vw->vw_XStep = vw->vw_MaxX / vw->vw_XStepRatio) <= 0))
  110.         vw->vw_XStep = 1;
  111.     if((vw->vw_YStepRatio <= 0)
  112.        || ((vw->vw_YStep = vw->vw_MaxY / vw->vw_YStepRatio) <= 0))
  113.         vw->vw_YStep = 1;
  114.     }
  115. }
  116.  
  117. /*
  118.  * The only thing necessary in `vw' is the font stuff (I think)
  119.  */
  120. Window
  121. newwindow(VW *oldVW, VW *vw, bool useDefDims)
  122. {
  123.     unsigned int x, y, width, height;
  124.     Window win;
  125.     SizeHints.flags = 0;
  126.     if(!useDefDims && oldVW)
  127.     {
  128.     x = y = 0;
  129.     width = vw->vw_FontX * oldVW->vw_MaxX;
  130.     height = (vw->vw_FontY * (oldVW->vw_MaxY + 1)) + 3;
  131.     SizeHints.flags |= PPosition | PSize;
  132.     }
  133.     else
  134.     {
  135.     if(DefDims[0] != -1)
  136.     {
  137.         x = DefDims[0];
  138.         SizeHints.flags |= USPosition;
  139.     }
  140.     else
  141.         x = 0;
  142.     if(DefDims[1] != -1)
  143.     {
  144.         y = DefDims[1];
  145.         SizeHints.flags |= USPosition;
  146.     }
  147.     else
  148.         y = 0;
  149.     if(DefDims[2] != -1)
  150.     {
  151.         width = DefDims[2];
  152.         SizeHints.flags |= USSize;
  153.     }
  154.     else
  155.         width = 80;
  156.     if(DefDims[3] != -1)
  157.     {
  158.         height = DefDims[3];
  159.         SizeHints.flags |= USSize;
  160.     }
  161.     else
  162.         height = 24;
  163.     width = vw->vw_FontX * width;
  164.     height = (vw->vw_FontY * (height + 1)) + 3;
  165.     }
  166.     win = XCreateSimpleWindow(XDisplay, DefaultRootWindow(XDisplay),
  167.                   x, y, width, height,
  168.                   WINDOW_BORDER_WIDTH, XForePixel, XBackPixel);
  169.     if(win)
  170.     {
  171.     XGCValues xgcv;
  172.     vw->vw_Window = win;
  173. /*
  174.  *    xgcv.plane_mask = XForePixel | XBackPixel;
  175.  */
  176.     xgcv.foreground = XForePixel;
  177.     xgcv.background = XBackPixel;
  178.     xgcv.line_width = 1;
  179.     xgcv.font = vw->vw_Font->fid;
  180.     vw->vw_WindowSys.ws_TextFontGC = XCreateGC(XDisplay, vw->vw_Window,
  181.                            GCForeground | GCBackground
  182.                            | GCLineWidth | GCFont,
  183.                            &xgcv);
  184.     xgcv.foreground = XBackPixel;
  185.     xgcv.background = XForePixel;
  186.     vw->vw_WindowSys.ws_BlkFontGC = XCreateGC(XDisplay, vw->vw_Window,
  187.                           GCForeground | GCBackground
  188.                           | GCLineWidth | GCFont,
  189.                           &xgcv);
  190.     SizeHints.x = x,
  191.     SizeHints.y = y,
  192.     SizeHints.width = width,
  193.     SizeHints.height = height,
  194.     SizeHints.base_width = 0;
  195.     SizeHints.base_height = vw->vw_FontY + 3;
  196.     SizeHints.width_inc = vw->vw_FontX;
  197.     SizeHints.height_inc = vw->vw_FontY;
  198.     SizeHints.min_width = SizeHints.base_width + SizeHints.width_inc;
  199.     SizeHints.min_height = SizeHints.base_height + SizeHints.height_inc;
  200.     SizeHints.flags |= PMinSize | PResizeInc | PBaseSize;
  201.     WMHints.flags = InputHint;
  202.     WMHints.input = True;
  203.     XSetWMProperties(XDisplay, win, NULL, NULL, Argv, Argc, &SizeHints, &WMHints, &ClassHints);
  204.     XStoreName(XDisplay, win, /* "jade" */ VERSSTRING);
  205.     XSetWMProtocols(XDisplay, win, &Wm_Del_Win, 1);
  206.     XSelectInput(XDisplay, win, INPUT_EVENTS);
  207.     XMapWindow(XDisplay, win);
  208.     return(win);
  209.     }
  210.     return(FALSE);
  211. }
  212. void
  213. killwindow(VW *vw)
  214. {
  215.     XFreeGC(XDisplay, vw->vw_WindowSys.ws_BlkFontGC);
  216.     XFreeGC(XDisplay, vw->vw_WindowSys.ws_TextFontGC);
  217.     XDestroyWindow(XDisplay, vw->vw_Window);
  218. }
  219.  
  220. void
  221. activatewin(VW *vw)
  222. {
  223.     /* Not sure about all this??  */
  224.     XRaiseWindow(XDisplay, vw->vw_Window);
  225.     XWarpPointer(XDisplay, None, vw->vw_Window, 0, 0, 0, 0, 1, 1);
  226. }
  227.  
  228. void
  229. setvwtitle(VW *vw)
  230. {
  231.     if(vw->vw_LastTitle)
  232.     {
  233.     int len = strlen(vw->vw_LastTitle);
  234.     if(len > vw->vw_MaxX)
  235.         drawmsg(vw, vw->vw_LastTitle + (len - vw->vw_MaxX), vw->vw_MaxX);
  236.     else
  237.         drawmsg(vw, vw->vw_LastTitle, strlen(vw->vw_LastTitle));
  238.     }
  239.     else
  240.     drawmsg(vw, "", 0);
  241. }
  242.  
  243. void
  244. setvwpos(VW *vw, long x, long y, long w, long h)
  245. {
  246.     XMoveResizeWindow(XDisplay, vw->vw_Window,
  247.               (unsigned int)x, (unsigned int)y,
  248.               (unsigned int)w, (unsigned int)h);
  249. }
  250.  
  251. VW *
  252. findwindowvw(Window win)
  253. {
  254.     VW *vw = ViewChain;
  255.     while(vw)
  256.     {
  257.     if(vw->vw_Window == win)
  258.         break;
  259.     vw = vw->vw_Next;
  260.     }
  261.     return(vw);
  262. }
  263.  
  264. int
  265. ezreq(u_char *bodyfmt, u_char *gadfmt, int arg1, ...)
  266. {
  267.     return(0);
  268. }
  269.  
  270. int
  271. setfont(VW *vw)
  272. {
  273.     XFontStruct *font;
  274.     if((font = XLoadQueryFont(XDisplay, VSTR(vw->vw_FontName)))
  275.        || (font = XLoadQueryFont(XDisplay, DEFAULT_FONT)))
  276.     {
  277.     if(vw->vw_Font)
  278.         XFreeFont(XDisplay, vw->vw_Font);
  279.     vw->vw_Font = font;
  280.     vw->vw_FontX = XTextWidth(font, "M", 1);
  281.     vw->vw_FontY = vw->vw_Font->ascent + vw->vw_Font->descent;
  282.     if(vw->vw_Window)
  283.     {
  284.         int w, h;
  285.         w = vw->vw_MaxX * vw->vw_FontX;
  286.         h = ((vw->vw_MaxY + 1) * vw->vw_FontY) + 3;
  287.         XSetFont(XDisplay, vw->vw_WindowSys.ws_TextFontGC, font->fid);
  288.         XSetFont(XDisplay, vw->vw_WindowSys.ws_BlkFontGC, font->fid);
  289.         updatedimensions(vw);
  290.         SizeHints.width = w;
  291.         SizeHints.height = h;
  292.         SizeHints.base_width = 0;
  293.         SizeHints.base_height = vw->vw_FontY + 3;
  294.         SizeHints.width_inc = vw->vw_FontX;
  295.         SizeHints.height_inc = vw->vw_FontY;
  296.         SizeHints.min_width = SizeHints.base_width + SizeHints.width_inc;
  297.         SizeHints.min_height = SizeHints.base_height + SizeHints.height_inc;
  298.         SizeHints.flags = PResizeInc | PMinSize | PBaseSize;
  299.         XSetWMNormalHints(XDisplay, vw->vw_Window, &SizeHints);
  300.         XResizeWindow(XDisplay, vw->vw_Window, w, h);
  301. #if 0
  302.         vw->vw_DeferRefresh++;
  303. #endif
  304.     }
  305.     return(TRUE);
  306.     }
  307.     return(FALSE);
  308. }
  309. void
  310. unsetfont(VW *vw)
  311. {
  312.     if(vw->vw_Font)
  313.     {
  314.     XFreeFont(XDisplay, vw->vw_Font);
  315.     vw->vw_Font = NULL;
  316.     }
  317. }
  318.  
  319. _PR VALUE cmd_set_font(VALUE fontname, VALUE vw);
  320. DEFUN("set-font", cmd_set_font, subr_set_font, (VALUE fontname, VALUE vw), V_Subr2, DOC_set_font) /*
  321. ::doc:set_font::
  322. (set-fo