A Simple X Image Viewer
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 <stdlib.h>
  19. #include <stdio.h>
  20. #include <X11/Xutil.h>
  21. #include "sxiv.h"
  22. #include "window.h"
  23. void win_open(win_t *win) {
  24. win_env_t *e;
  25. XClassHint *classhint;
  26. XColor bgcol;
  27. XSetWindowAttributes attr;
  28. unsigned long mask;
  29. if (!win)
  30. return;
  31. e = &win->env;
  32. if (!(e->dpy = XOpenDisplay(NULL)))
  33. DIE("could not open display");
  34. e->scr = DefaultScreen(e->dpy);
  35. e->scrw = DisplayWidth(e->dpy, e->scr);
  36. e->scrh = DisplayHeight(e->dpy, e->scr);
  37. e->vis = DefaultVisual(e->dpy, e->scr);
  38. e->cmap = DefaultColormap(e->dpy, e->scr);
  39. e->depth = DefaultDepth(e->dpy, e->scr);
  40. if (!XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), BG_COLOR,
  41. &bgcol, &bgcol))
  42. DIE("could not allocate color: %s", BG_COLOR);
  43. if (win->w > e->scrw)
  44. win->w = e->scrw;
  45. if (win->h > e->scrh)
  46. win->h = e->scrh;
  47. win->x = (e->scrw - win->w) / 2;
  48. win->y = (e->scrh - win->h) / 2;
  49. attr.backing_store = NotUseful;
  50. attr.background_pixel = bgcol.pixel;
  51. attr.save_under = False;
  52. mask = CWBackingStore | CWBackPixel | CWSaveUnder;
  53. win->xwin = XCreateWindow(e->dpy, RootWindow(e->dpy, e->scr),
  54. win->x, win->y, win->w, win->h, 0,
  55. e->depth, InputOutput, e->vis, mask, &attr);
  56. if (win->xwin == None)
  57. DIE("could not create window");
  58. XSelectInput(e->dpy, win->xwin,
  59. StructureNotifyMask | ExposureMask | KeyPressMask);
  60. if ((classhint = XAllocClassHint())) {
  61. classhint->res_name = "sxvi";
  62. classhint->res_class = "sxvi";
  63. XSetClassHint(e->dpy, win->xwin, classhint);
  64. XFree(classhint);
  65. }
  66. XMapWindow(e->dpy, win->xwin);
  67. XFlush(e->dpy);
  68. }
  69. void win_close(win_t *win) {
  70. if (!win)
  71. return;
  72. XDestroyWindow(win->env.dpy, win->xwin);
  73. XCloseDisplay(win->env.dpy);
  74. }
  75. int win_configure(win_t *win, XConfigureEvent *cev) {
  76. int changed;
  77. if (!win)
  78. return 0;
  79. changed = win->x != cev->x || win->y != cev->y ||
  80. win->w != cev->width || win->h != cev->height;
  81. win->x = cev->x;
  82. win->y = cev->y;
  83. win->w = cev->width;
  84. win->h = cev->height;
  85. win->bw = cev->border_width;
  86. return changed;
  87. }
  88. void win_clear(win_t *win) {
  89. if (!win)
  90. return;
  91. XClearWindow(win->env.dpy, win->xwin);
  92. }