A Simple X Image Viewer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

143 lines
3.4 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 <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. XGCValues gcval;
  28. if (!win)
  29. return;
  30. e = &win->env;
  31. if (!(e->dpy = XOpenDisplay(NULL)))
  32. DIE("could not open display");
  33. e->scr = DefaultScreen(e->dpy);
  34. e->scrw = DisplayWidth(e->dpy, e->scr);
  35. e->scrh = DisplayHeight(e->dpy, e->scr);
  36. e->vis = DefaultVisual(e->dpy, e->scr);
  37. e->cmap = DefaultColormap(e->dpy, e->scr);
  38. e->depth = DefaultDepth(e->dpy, e->scr);
  39. if (!XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), BG_COLOR,
  40. &bgcol, &bgcol))
  41. DIE("could not allocate color: %s", BG_COLOR);
  42. win->w = WIN_WIDTH;
  43. win->h = WIN_HEIGHT;
  44. if (win->w > e->scrw)
  45. win->w = e->scrw;
  46. if (win->h > e->scrh)
  47. win->h = e->scrh;
  48. win->x = (e->scrw - win->w) / 2;
  49. win->y = (e->scrh - win->h) / 2;
  50. win->xwin = XCreateWindow(e->dpy, RootWindow(e->dpy, e->scr),
  51. win->x, win->y, win->w, win->h, 0,
  52. e->depth, InputOutput, e->vis, 0, None);
  53. if (win->xwin == None)
  54. DIE("could not create window");
  55. XSelectInput(e->dpy, win->xwin,
  56. StructureNotifyMask | KeyPressMask);
  57. gcval.foreground = bgcol.pixel;
  58. win->bgc = XCreateGC(e->dpy, win->xwin, GCForeground, &gcval);
  59. win->pm = 0;
  60. win_set_title(win, "sxiv");
  61. if ((classhint = XAllocClassHint())) {
  62. classhint->res_name = "sxiv";
  63. classhint->res_class = "sxiv";
  64. XSetClassHint(e->dpy, win->xwin, classhint);
  65. XFree(classhint);
  66. }
  67. XMapWindow(e->dpy, win->xwin);
  68. XFlush(e->dpy);
  69. }
  70. void win_close(win_t *win) {
  71. if (!win)
  72. return;
  73. XDestroyWindow(win->env.dpy, win->xwin);
  74. XCloseDisplay(win->env.dpy);
  75. }
  76. void win_set_title(win_t *win, const char *title) {
  77. if (!win)
  78. return;
  79. if (!title)
  80. title = "sxiv";
  81. XStoreName(win->env.dpy, win->xwin, title);
  82. XSetIconName(win->env.dpy, win->xwin, title);
  83. }
  84. int win_configure(win_t *win, XConfigureEvent *c) {
  85. int changed;
  86. if (!win)
  87. return 0;
  88. changed = win->w != c->width || win->h != c->height;
  89. win->x = c->x;
  90. win->y = c->y;
  91. win->w = c->width;
  92. win->h = c->height;
  93. win->bw = c->border_width;
  94. return changed;
  95. }
  96. void win_clear(win_t *win) {
  97. win_env_t *e;
  98. if (!win)
  99. return;
  100. e = &win->env;
  101. if (win->pm)
  102. XFreePixmap(e->dpy, win->pm);
  103. win->pm = XCreatePixmap(e->dpy, win->xwin, e->scrw, e->scrh, e->depth);
  104. XFillRectangle(e->dpy, win->pm, win->bgc, 0, 0, e->scrw, e->scrh);
  105. }
  106. void win_draw(win_t *win) {
  107. if (!win)
  108. return;
  109. XSetWindowBackgroundPixmap(win->env.dpy, win->xwin, win->pm);
  110. XClearWindow(win->env.dpy, win->xwin);
  111. }