A Simple X Image Viewer
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

window.c 6.6 KiB

14 yıl önce
14 yıl önce
14 yıl önce
14 yıl önce
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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 arrow;
  26. static Cursor hand;
  27. static GC bgc;
  28. Atom wm_delete_win;
  29. void win_set_sizehints(win_t *win) {
  30. XSizeHints sizehints;
  31. if (!win)
  32. return;
  33. sizehints.flags = PMinSize | PMaxSize;
  34. sizehints.min_width = win->w;
  35. sizehints.max_width = win->w;
  36. sizehints.min_height = win->h;
  37. sizehints.max_height = win->h;
  38. XSetWMNormalHints(win->env.dpy, win->xwin, &sizehints);
  39. }
  40. void win_open(win_t *win) {
  41. win_env_t *e;
  42. XClassHint classhint;
  43. XColor bgcol;
  44. int gmask;
  45. if (!win)
  46. return;
  47. e = &win->env;
  48. if (!(e->dpy = XOpenDisplay(NULL)))
  49. die("could not open display");
  50. e->scr = DefaultScreen(e->dpy);
  51. e->scrw = DisplayWidth(e->dpy, e->scr);
  52. e->scrh = DisplayHeight(e->dpy, e->scr);
  53. e->vis = DefaultVisual(e->dpy, e->scr);
  54. e->cmap = DefaultColormap(e->dpy, e->scr);
  55. e->depth = DefaultDepth(e->dpy, e->scr);
  56. if (!XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), BG_COLOR,
  57. &bgcol, &bgcol))
  58. die("could not allocate color: %s", BG_COLOR);
  59. win->bgcol = bgcol.pixel;
  60. win->pm = 0;
  61. win->fullscreen = 0;
  62. /* determine window offsets, width & height */
  63. if (!options->geometry)
  64. gmask = 0;
  65. else
  66. gmask = XParseGeometry(options->geometry, &win->x, &win->y,
  67. &win->w, &win->h);
  68. if (!(gmask & WidthValue))
  69. win->w = WIN_WIDTH;
  70. if (win->w > e->scrw)
  71. win->w = e->scrw;
  72. if (!(gmask & HeightValue))
  73. win->h = WIN_HEIGHT;
  74. if (win->h > e->scrh)
  75. win->h = e->scrh;
  76. if (!(gmask & XValue))
  77. win->x = (e->scrw - win->w) / 2;
  78. else if (gmask & XNegative)
  79. win->x += e->scrw - win->w;
  80. if (!(gmask & YValue))
  81. win->y = (e->scrh - win->h) / 2;
  82. else if (gmask & YNegative)
  83. win->y += e->scrh - win->h;
  84. win->xwin = XCreateWindow(e->dpy, RootWindow(e->dpy, e->scr),
  85. win->x, win->y, win->w, win->h, 0,
  86. e->depth, InputOutput, e->vis, 0, None);
  87. if (win->xwin == None)
  88. die("could not create window");
  89. XSelectInput(e->dpy, win->xwin, StructureNotifyMask | KeyPressMask |
  90. ButtonPressMask | ButtonReleaseMask | Button2MotionMask);
  91. arrow = XCreateFontCursor(e->dpy, XC_left_ptr);
  92. hand = XCreateFontCursor(e->dpy, XC_fleur);
  93. bgc = XCreateGC(e->dpy, win->xwin, 0, None);
  94. win_set_title(win, "sxiv");
  95. classhint.res_name = "sxiv";
  96. classhint.res_class = "sxiv";
  97. XSetClassHint(e->dpy, win->xwin, &classhint);
  98. if (options->fixed)
  99. win_set_sizehints(win);
  100. XMapWindow(e->dpy, win->xwin);
  101. XFlush(e->dpy);
  102. wm_delete_win = XInternAtom(e->dpy, "WM_DELETE_WINDOW", False);
  103. XSetWMProtocols(e->dpy, win->xwin, &wm_delete_win, 1);
  104. if (options->fullscreen)
  105. win_toggle_fullscreen(win);
  106. }
  107. void win_close(win_t *win) {
  108. if (!win)
  109. return;
  110. XFreeCursor(win->env.dpy, arrow);
  111. XFreeCursor(win->env.dpy, hand);
  112. XFreeGC(win->env.dpy, bgc);
  113. XDestroyWindow(win->env.dpy, win->xwin);
  114. XCloseDisplay(win->env.dpy);
  115. }
  116. int win_configure(win_t *win, XConfigureEvent *c) {
  117. int changed;
  118. if (!win)
  119. return 0;
  120. changed = win->w != c->width || win->h != c->height;
  121. win->x = c->x;
  122. win->y = c->y;
  123. win->w = c->width;
  124. win->h = c->height;
  125. win->bw = c->border_width;
  126. return changed;
  127. }
  128. int win_moveresize(win_t *win, int x, int y, unsigned int w, unsigned int h) {
  129. if (!win)
  130. return 0;
  131. x = MAX(0, x);
  132. y = MAX(0, y);
  133. w = MIN(w, win->env.scrw - 2 * win->bw);
  134. h = MIN(h, win->env.scrh - 2 * win->bw);
  135. if (win->x == x && win->y == y && win->w == w && win->h == h)
  136. return 0;
  137. win->x = x;
  138. win->y = y;
  139. win->w = w;
  140. win->h = h;
  141. if (options->fixed)
  142. win_set_sizehints(win);
  143. XMoveResizeWindow(win->env.dpy, win->xwin, win->x, win->y, win->w, win->h);
  144. return 1;
  145. }
  146. void win_toggle_fullscreen(win_t *win) {
  147. XEvent ev;
  148. XClientMessageEvent *cm;
  149. if (!win)
  150. return;
  151. win->fullscreen ^= 1;
  152. memset(&ev, 0, sizeof(ev));
  153. ev.type = ClientMessage;
  154. cm = &ev.xclient;
  155. cm->window = win->xwin;
  156. cm->message_type = XInternAtom(win->env.dpy, "_NET_WM_STATE", False);
  157. cm->format = 32;
  158. cm->data.l[0] = win->fullscreen;
  159. cm->data.l[1] = XInternAtom(win->env.dpy, "_NET_WM_STATE_FULLSCREEN", False);
  160. cm->data.l[2] = XInternAtom(win->env.dpy, "_NET_WM_STATE_ABOVE", False);
  161. cm->data.l[3] = 0;
  162. XSendEvent(win->env.dpy, DefaultRootWindow(win->env.dpy), False,
  163. SubstructureNotifyMask, &ev);
  164. }
  165. void win_clear(win_t *win) {
  166. win_env_t *e;
  167. XGCValues gcval;
  168. if (!win)
  169. return;
  170. e = &win->env;
  171. if (win->pm)
  172. XFreePixmap(e->dpy, win->pm);
  173. win->pm = XCreatePixmap(e->dpy, win->xwin, e->scrw, e->scrh, e->depth);
  174. gcval.foreground = win->fullscreen ? BlackPixel(e->dpy, e->scr) : win->bgcol;
  175. XChangeGC(e->dpy, bgc, GCForeground, &gcval);
  176. XFillRectangle(e->dpy, win->pm, bgc, 0, 0, e->scrw, e->scrh);
  177. }
  178. void win_draw(win_t *win) {
  179. if (!win)
  180. return;
  181. XSetWindowBackgroundPixmap(win->env.dpy, win->xwin, win->pm);
  182. XClearWindow(win->env.dpy, win->xwin);
  183. }
  184. void win_set_title(win_t *win, const char *title) {
  185. if (!win)
  186. return;
  187. if (!title)
  188. title = "sxiv";
  189. XStoreName(win->env.dpy, win->xwin, title);
  190. XSetIconName(win->env.dpy, win->xwin, title);
  191. XChangeProperty(win->env.dpy, win->xwin,
  192. XInternAtom(win->env.dpy, "_NET_WM_NAME", False),
  193. XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
  194. PropModeReplace, (unsigned char *) title, strlen(title));
  195. XChangeProperty(win->env.dpy, win->xwin,
  196. XInternAtom(win->env.dpy, "_NET_WM_ICON_NAME", False),
  197. XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
  198. PropModeReplace, (unsigned char *) title, strlen(title));
  199. }
  200. void win_set_cursor(win_t *win, win_cur_t cursor) {
  201. if (!win)
  202. return;
  203. switch (cursor) {
  204. case CURSOR_HAND:
  205. XDefineCursor(win->env.dpy, win->xwin, hand);
  206. break;
  207. case CURSOR_ARROW:
  208. default:
  209. XDefineCursor(win->env.dpy, win->xwin, arrow);
  210. break;
  211. }
  212. }