A Simple X Image Viewer
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

322 rindas
7.9 KiB

  1. /* sxiv: window.c
  2. * Copyright (c) 2011 Bert Muennich <muennich at informatik.hu-berlin.de>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. */
  18. #include <string.h>
  19. #include <X11/Xutil.h>
  20. #include <X11/cursorfont.h>
  21. #include "config.h"
  22. #include "options.h"
  23. #include "util.h"
  24. #include "window.h"
  25. static Cursor carrow;
  26. static Cursor chand;
  27. static Cursor cwatch;
  28. static GC gc;
  29. Atom wm_delete_win;
  30. void win_set_sizehints(win_t *win) {
  31. XSizeHints sizehints;
  32. if (!win)
  33. return;
  34. sizehints.flags = PMinSize | PMaxSize;
  35. sizehints.min_width = win->w;
  36. sizehints.max_width = win->w;
  37. sizehints.min_height = win->h;
  38. sizehints.max_height = win->h;
  39. XSetWMNormalHints(win->env.dpy, win->xwin, &sizehints);
  40. }
  41. void win_open(win_t *win) {
  42. win_env_t *e;
  43. XClassHint classhint;
  44. XColor col;
  45. XGCValues gcval;
  46. int gmask;
  47. if (!win)
  48. return;
  49. e = &win->env;
  50. if (!(e->dpy = XOpenDisplay(NULL)))
  51. die("could not open display");
  52. e->scr = DefaultScreen(e->dpy);
  53. e->scrw = DisplayWidth(e->dpy, e->scr);
  54. e->scrh = DisplayHeight(e->dpy, e->scr);
  55. e->vis = DefaultVisual(e->dpy, e->scr);
  56. e->cmap = DefaultColormap(e->dpy, e->scr);
  57. e->depth = DefaultDepth(e->dpy, e->scr);
  58. if (XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), BG_COLOR,
  59. &col, &col))
  60. win->bgcol = col.pixel;
  61. else
  62. die("could not allocate color: %s", BG_COLOR);
  63. if (XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), SEL_COLOR,
  64. &col, &col))
  65. win->selcol = col.pixel;
  66. else
  67. die("could not allocate color: %s", BG_COLOR);
  68. win->pm = 0;
  69. win->fullscreen = 0;
  70. /* determine window offsets, width & height */
  71. if (!options->geometry)
  72. gmask = 0;
  73. else
  74. gmask = XParseGeometry(options->geometry, &win->x, &win->y,
  75. &win->w, &win->h);
  76. if (!(gmask & WidthValue))
  77. win->w = WIN_WIDTH;
  78. if (win->w > e->scrw)
  79. win->w = e->scrw;
  80. if (!(gmask & HeightValue))
  81. win->h = WIN_HEIGHT;
  82. if (win->h > e->scrh)
  83. win->h = e->scrh;
  84. if (!(gmask & XValue))
  85. win->x = (e->scrw - win->w) / 2;
  86. else if (gmask & XNegative)
  87. win->x += e->scrw - win->w;
  88. if (!(gmask & YValue))
  89. win->y = (e->scrh - win->h) / 2;
  90. else if (gmask & YNegative)
  91. win->y += e->scrh - win->h;
  92. win->xwin = XCreateWindow(e->dpy, RootWindow(e->dpy, e->scr),
  93. win->x, win->y, win->w, win->h, 0,
  94. e->depth, InputOutput, e->vis, 0, None);
  95. if (win->xwin == None)
  96. die("could not create window");
  97. XSelectInput(e->dpy, win->xwin, StructureNotifyMask | KeyPressMask |
  98. ButtonPressMask | ButtonReleaseMask | Button2MotionMask);
  99. carrow = XCreateFontCursor(e->dpy, XC_left_ptr);
  100. chand = XCreateFontCursor(e->dpy, XC_fleur);
  101. cwatch = XCreateFontCursor(e->dpy, XC_watch);
  102. gcval.line_width = 2;
  103. gc = XCreateGC(e->dpy, win->xwin, GCLineWidth, &gcval);
  104. win_set_title(win, "sxiv");
  105. classhint.res_name = "sxiv";
  106. classhint.res_class = "sxiv";
  107. XSetClassHint(e->dpy, win->xwin, &classhint);
  108. if (options->fixed)
  109. win_set_sizehints(win);
  110. XMapWindow(e->dpy, win->xwin);
  111. XFlush(e->dpy);
  112. wm_delete_win = XInternAtom(e->dpy, "WM_DELETE_WINDOW", False);
  113. XSetWMProtocols(e->dpy, win->xwin, &wm_delete_win, 1);
  114. if (options->fullscreen)
  115. win_toggle_fullscreen(win);
  116. }
  117. void win_close(win_t *win) {
  118. if (!win)
  119. return;
  120. XFreeCursor(win->env.dpy, carrow);
  121. XFreeCursor(win->env.dpy, chand);
  122. XFreeCursor(win->env.dpy, cwatch);
  123. XFreeGC(win->env.dpy, gc);
  124. XDestroyWindow(win->env.dpy, win->xwin);
  125. XCloseDisplay(win->env.dpy);
  126. }
  127. int win_configure(win_t *win, XConfigureEvent *c) {
  128. int changed;
  129. if (!win)
  130. return 0;
  131. changed = win->w != c->width || win->h != c->height;
  132. win->x = c->x;
  133. win->y = c->y;
  134. win->w = c->width;
  135. win->h = c->height;
  136. win->bw = c->border_width;
  137. return changed;
  138. }
  139. int win_moveresize(win_t *win, int x, int y, unsigned int w, unsigned int h) {
  140. if (!win)
  141. return 0;
  142. x = MAX(0, x);
  143. y = MAX(0, y);
  144. w = MIN(w, win->env.scrw - 2 * win->bw);
  145. h = MIN(h, win->env.scrh - 2 * win->bw);
  146. if (win->x == x && win->y == y && win->w == w && win->h == h)
  147. return 0;
  148. win->x = x;
  149. win->y = y;
  150. win->w = w;
  151. win->h = h;
  152. if (options->fixed)
  153. win_set_sizehints(win);
  154. XMoveResizeWindow(win->env.dpy, win->xwin, win->x, win->y, win->w, win->h);
  155. return 1;
  156. }
  157. void win_toggle_fullscreen(win_t *win) {
  158. XEvent ev;
  159. XClientMessageEvent *cm;
  160. if (!win)
  161. return;
  162. win->fullscreen ^= 1;
  163. memset(&ev, 0, sizeof(ev));
  164. ev.type = ClientMessage;
  165. cm = &ev.xclient;
  166. cm->window = win->xwin;
  167. cm->message_type = XInternAtom(win->env.dpy, "_NET_WM_STATE", False);
  168. cm->format = 32;
  169. cm->data.l[0] = win->fullscreen;
  170. cm->data.l[1] = XInternAtom(win->env.dpy, "_NET_WM_STATE_FULLSCREEN", False);
  171. cm->data.l[2] = XInternAtom(win->env.dpy, "_NET_WM_STATE_ABOVE", False);
  172. cm->data.l[3] = 0;
  173. XSendEvent(win->env.dpy, DefaultRootWindow(win->env.dpy), False,
  174. SubstructureNotifyMask, &ev);
  175. }
  176. Pixmap win_create_pixmap(win_t *win, int w, int h) {
  177. if (!win)
  178. return 0;
  179. return XCreatePixmap(win->env.dpy, win->xwin, w, h, win->env.depth);
  180. }
  181. void win_free_pixmap(win_t *win, Pixmap pm) {
  182. if (win && pm)
  183. XFreePixmap(win->env.dpy, pm);
  184. }
  185. void win_clear(win_t *win) {
  186. win_env_t *e;
  187. XGCValues gcval;
  188. if (!win)
  189. return;
  190. e = &win->env;
  191. gcval.foreground = win->fullscreen ? BlackPixel(e->dpy, e->scr) :
  192. win->bgcol;
  193. if (win->pm)
  194. XFreePixmap(e->dpy, win->pm);
  195. win->pm = XCreatePixmap(e->dpy, win->xwin, e->scrw, e->scrh, e->depth);
  196. XChangeGC(e->dpy, gc, GCForeground, &gcval);
  197. XFillRectangle(e->dpy, win->pm, gc, 0, 0, e->scrw, e->scrh);
  198. }
  199. void win_draw_pixmap(win_t *win, Pixmap pm, int x, int y, int w, int h) {
  200. if (win)
  201. XCopyArea(win->env.dpy, pm, win->pm, gc, 0, 0, w, h, x, y);
  202. }
  203. void win_draw_rect(win_t *win, int x, int y, int w, int h, Bool sel) {
  204. win_env_t *e;
  205. XGCValues gcval;
  206. if (!win)
  207. return;
  208. e = &win->env;
  209. if (sel)
  210. gcval.foreground = win->selcol;
  211. else
  212. gcval.foreground = win->fullscreen ? BlackPixel(e->dpy, e->scr) :
  213. win->bgcol;
  214. XChangeGC(e->dpy, gc, GCForeground, &gcval);
  215. XDrawRectangle(e->dpy, win->pm, gc, x, y, w, h);
  216. }
  217. void win_draw(win_t *win) {
  218. if (!win)
  219. return;
  220. XSetWindowBackgroundPixmap(win->env.dpy, win->xwin, win->pm);
  221. XClearWindow(win->env.dpy, win->xwin);
  222. }
  223. void win_set_title(win_t *win, const char *title) {
  224. if (!win)
  225. return;
  226. if (!title)
  227. title = "sxiv";
  228. XStoreName(win->env.dpy, win->xwin, title);
  229. XSetIconName(win->env.dpy, win->xwin, title);
  230. XChangeProperty(win->env.dpy, win->xwin,
  231. XInternAtom(win->env.dpy, "_NET_WM_NAME", False),
  232. XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
  233. PropModeReplace, (unsigned char *) title, strlen(title));
  234. XChangeProperty(win->env.dpy, win->xwin,
  235. XInternAtom(win->env.dpy, "_NET_WM_ICON_NAME", False),
  236. XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
  237. PropModeReplace, (unsigned char *) title, strlen(title));
  238. }
  239. void win_set_cursor(win_t *win, win_cur_t cursor) {
  240. if (!win)
  241. return;
  242. switch (cursor) {
  243. case CURSOR_HAND:
  244. XDefineCursor(win->env.dpy, win->xwin, chand);
  245. break;
  246. case CURSOR_WATCH:
  247. XDefineCursor(win->env.dpy, win->xwin, cwatch);
  248. break;
  249. case CURSOR_ARROW:
  250. default:
  251. XDefineCursor(win->env.dpy, win->xwin, carrow);
  252. break;
  253. }
  254. }