2013-02-08 21:52:41 +01:00
|
|
|
/* Copyright 2011-2013 Bert Muennich
|
2011-01-17 14:57:59 +01:00
|
|
|
*
|
2013-02-08 21:52:41 +01:00
|
|
|
* This file is part of sxiv.
|
2011-08-18 01:18:26 +02:00
|
|
|
*
|
2013-02-08 21:52:41 +01:00
|
|
|
* sxiv is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published
|
|
|
|
* by the Free Software Foundation; either version 2 of the License,
|
|
|
|
* or (at your option) any later version.
|
2011-08-18 01:18:26 +02:00
|
|
|
*
|
2013-02-08 21:52:41 +01:00
|
|
|
* sxiv is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with sxiv. If not, see <http://www.gnu.org/licenses/>.
|
2011-01-17 14:57:59 +01:00
|
|
|
*/
|
|
|
|
|
2011-09-08 20:54:24 +02:00
|
|
|
#define _POSIX_C_SOURCE 200112L
|
2011-09-11 21:01:24 +02:00
|
|
|
#define _WINDOW_CONFIG
|
2011-09-08 20:54:24 +02:00
|
|
|
|
2011-01-23 16:14:41 +01:00
|
|
|
#include <string.h>
|
2012-02-12 19:00:41 +01:00
|
|
|
#include <locale.h>
|
2011-01-17 16:42:49 +01:00
|
|
|
#include <X11/Xutil.h>
|
2011-01-29 22:37:40 +01:00
|
|
|
#include <X11/cursorfont.h>
|
2011-01-17 16:42:49 +01:00
|
|
|
|
2011-02-03 10:15:01 +01:00
|
|
|
#include "options.h"
|
|
|
|
#include "util.h"
|
2011-01-17 14:57:59 +01:00
|
|
|
#include "window.h"
|
2011-07-22 14:49:06 +02:00
|
|
|
#include "config.h"
|
2011-01-17 14:57:59 +01:00
|
|
|
|
2012-02-12 19:00:41 +01:00
|
|
|
enum {
|
|
|
|
H_TEXT_PAD = 5,
|
|
|
|
V_TEXT_PAD = 1
|
|
|
|
};
|
|
|
|
|
2011-02-17 11:08:25 +01:00
|
|
|
static Cursor carrow;
|
2011-02-21 18:26:21 +01:00
|
|
|
static Cursor cnone;
|
2011-02-17 11:08:25 +01:00
|
|
|
static Cursor chand;
|
|
|
|
static Cursor cwatch;
|
2011-02-17 16:22:54 +01:00
|
|
|
static GC gc;
|
2011-01-23 16:14:41 +01:00
|
|
|
|
2011-02-09 10:01:49 +01:00
|
|
|
Atom wm_delete_win;
|
|
|
|
|
2013-01-07 14:30:42 +01:00
|
|
|
static struct {
|
2012-02-12 19:00:41 +01:00
|
|
|
int ascent;
|
|
|
|
int descent;
|
|
|
|
XFontStruct *xfont;
|
|
|
|
XFontSet set;
|
|
|
|
} font;
|
|
|
|
|
2013-01-07 14:30:42 +01:00
|
|
|
static int fontheight;
|
|
|
|
static int barheight;
|
|
|
|
|
2013-02-08 22:05:31 +01:00
|
|
|
void win_init_font(Display *dpy, const char *fontstr)
|
|
|
|
{
|
2012-02-12 19:00:41 +01:00
|
|
|
int n;
|
|
|
|
char *def, **missing;
|
|
|
|
|
|
|
|
font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
|
2012-02-12 19:06:07 +01:00
|
|
|
if (missing)
|
2012-02-12 19:00:41 +01:00
|
|
|
XFreeStringList(missing);
|
|
|
|
if (font.set) {
|
|
|
|
XFontStruct **xfonts;
|
|
|
|
char **font_names;
|
|
|
|
|
|
|
|
font.ascent = font.descent = 0;
|
|
|
|
XExtentsOfFontSet(font.set);
|
|
|
|
n = XFontsOfFontSet(font.set, &xfonts, &font_names);
|
|
|
|
while (n--) {
|
|
|
|
font.ascent = MAX(font.ascent, (*xfonts)->ascent);
|
|
|
|
font.descent = MAX(font.descent,(*xfonts)->descent);
|
|
|
|
xfonts++;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ((font.xfont = XLoadQueryFont(dpy, fontstr)) == NULL &&
|
|
|
|
(font.xfont = XLoadQueryFont(dpy, "fixed")) == NULL)
|
|
|
|
{
|
|
|
|
die("could not load font: %s", fontstr);
|
|
|
|
}
|
|
|
|
font.ascent = font.xfont->ascent;
|
|
|
|
font.descent = font.xfont->descent;
|
|
|
|
}
|
2013-01-07 14:30:42 +01:00
|
|
|
fontheight = font.ascent + font.descent;
|
|
|
|
barheight = fontheight + 2 * V_TEXT_PAD;
|
2012-02-12 19:00:41 +01:00
|
|
|
}
|
|
|
|
|
2013-02-08 22:05:31 +01:00
|
|
|
unsigned long win_alloc_color(win_t *win, const char *name)
|
|
|
|
{
|
2012-02-12 19:00:41 +01:00
|
|
|
XColor col;
|
|
|
|
|
|
|
|
if (win == NULL)
|
|
|
|
return 0UL;
|
|
|
|
if (XAllocNamedColor(win->env.dpy,
|
2013-01-07 14:30:42 +01:00
|
|
|
DefaultColormap(win->env.dpy, win->env.scr),
|
|
|
|
name, &col, &col) == 0)
|
2012-02-12 19:00:41 +01:00
|
|
|
{
|
|
|
|
die("could not allocate color: %s", name);
|
|
|
|
}
|
|
|
|
return col.pixel;
|
|
|
|
}
|
|
|
|
|
2013-02-08 22:05:31 +01:00
|
|
|
void win_init(win_t *win)
|
|
|
|
{
|
2011-01-18 16:32:40 +01:00
|
|
|
win_env_t *e;
|
2011-01-17 16:42:49 +01:00
|
|
|
|
2011-09-29 12:43:36 +02:00
|
|
|
if (win == NULL)
|
2011-01-17 16:42:49 +01:00
|
|
|
return;
|
|
|
|
|
2013-01-07 14:30:42 +01:00
|
|
|
memset(win, 0, sizeof(win_t));
|
|
|
|
|
2011-01-21 12:57:35 +01:00
|
|
|
e = &win->env;
|
2011-09-29 12:43:36 +02:00
|
|
|
if ((e->dpy = XOpenDisplay(NULL)) == NULL)
|
2011-02-03 10:15:01 +01:00
|
|
|
die("could not open display");
|
2011-01-21 12:57:35 +01:00
|
|
|
|
2011-01-18 16:32:40 +01:00
|
|
|
e->scr = DefaultScreen(e->dpy);
|
|
|
|
e->scrw = DisplayWidth(e->dpy, e->scr);
|
|
|
|
e->scrh = DisplayHeight(e->dpy, e->scr);
|
|
|
|
e->vis = DefaultVisual(e->dpy, e->scr);
|
|
|
|
e->cmap = DefaultColormap(e->dpy, e->scr);
|
|
|
|
e->depth = DefaultDepth(e->dpy, e->scr);
|
2011-01-17 20:14:15 +01:00
|
|
|
|
2013-01-07 14:30:42 +01:00
|
|
|
win->white = WhitePixel(e->dpy, e->scr);
|
|
|
|
win->bgcol = win_alloc_color(win, WIN_BG_COLOR);
|
|
|
|
win->fscol = win_alloc_color(win, WIN_FS_COLOR);
|
|
|
|
win->selcol = win_alloc_color(win, SEL_COLOR);
|
|
|
|
win->bar.bgcol = win_alloc_color(win, BAR_BG_COLOR);
|
|
|
|
win->bar.fgcol = win_alloc_color(win, BAR_FG_COLOR);
|
2012-02-12 19:00:41 +01:00
|
|
|
|
|
|
|
if (setlocale(LC_CTYPE, "") == NULL || XSupportsLocale() == 0)
|
|
|
|
warn("no locale support");
|
|
|
|
|
|
|
|
win_init_font(e->dpy, BAR_FONT);
|
2012-08-07 12:09:52 +02:00
|
|
|
|
|
|
|
wm_delete_win = XInternAtom(e->dpy, "WM_DELETE_WINDOW", False);
|
2011-04-11 16:58:38 +02:00
|
|
|
}
|
|
|
|
|
2013-02-08 22:05:31 +01:00
|
|
|
void win_set_sizehints(win_t *win)
|
|
|
|
{
|
2011-04-11 16:58:38 +02:00
|
|
|
XSizeHints sizehints;
|
|
|
|
|
2011-09-29 12:43:36 +02:00
|
|
|
if (win == NULL || win->xwin == None)
|
2011-04-11 16:58:38 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
sizehints.flags = PMinSize | PMaxSize;
|
|
|
|
sizehints.min_width = win->w;
|
|
|
|
sizehints.max_width = win->w;
|
2013-01-07 14:30:42 +01:00
|
|
|
sizehints.min_height = win->h + win->bar.h;
|
|
|
|
sizehints.max_height = win->h + win->bar.h;
|
2011-04-11 16:58:38 +02:00
|
|
|
XSetWMNormalHints(win->env.dpy, win->xwin, &sizehints);
|
|
|
|
}
|
|
|
|
|
2013-02-08 22:05:31 +01:00
|
|
|
void win_open(win_t *win)
|
|
|
|
{
|
2011-04-11 16:58:38 +02:00
|
|
|
win_env_t *e;
|
|
|
|
XClassHint classhint;
|
|
|
|
XColor col;
|
2011-09-11 21:01:24 +02:00
|
|
|
char none_data[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
|
2011-04-11 16:58:38 +02:00
|
|
|
Pixmap none;
|
|
|
|
int gmask;
|
|
|
|
|
2011-09-29 12:43:36 +02:00
|
|
|
if (win == NULL)
|
2011-04-11 16:58:38 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
e = &win->env;
|
|
|
|
|
2011-01-30 22:00:58 +01:00
|
|
|
/* determine window offsets, width & height */
|
2011-09-29 12:43:36 +02:00
|
|
|
if (options->geometry == NULL)
|
2011-01-30 22:00:58 +01:00
|
|
|
gmask = 0;
|
|
|
|
else
|
|
|
|
gmask = XParseGeometry(options->geometry, &win->x, &win->y,
|
|
|
|
&win->w, &win->h);
|
2011-09-29 12:43:36 +02:00
|
|
|
if ((gmask & WidthValue) == 0)
|
2011-01-30 22:00:58 +01:00
|
|
|
win->w = WIN_WIDTH;
|
|
|
|
if (win->w > e->scrw)
|
|
|
|
win->w = e->scrw;
|
2011-09-29 12:43:36 +02:00
|
|
|
if ((gmask & HeightValue) == 0)
|
2011-01-30 22:00:58 +01:00
|
|
|
win->h = WIN_HEIGHT;
|
|
|
|
if (win->h > e->scrh)
|
|
|
|
win->h = e->scrh;
|
2011-09-29 12:43:36 +02:00
|
|
|
if ((gmask & XValue) == 0)
|
2011-01-30 22:00:58 +01:00
|
|
|
win->x = (e->scrw - win->w) / 2;
|
2011-09-29 12:43:36 +02:00
|
|
|
else if ((gmask & XNegative) != 0)
|
2011-02-01 16:21:58 +01:00
|
|
|
win->x += e->scrw - win->w;
|
2011-09-29 12:43:36 +02:00
|
|
|
if ((gmask & YValue) == 0)
|
2011-01-30 22:00:58 +01:00
|
|
|
win->y = (e->scrh - win->h) / 2;
|
2011-09-29 12:43:36 +02:00
|
|
|
else if ((gmask & YNegative) != 0)
|
2011-02-01 16:21:58 +01:00
|
|
|
win->y += e->scrh - win->h;
|
2011-01-17 16:42:49 +01:00
|
|
|
|
2011-01-18 16:32:40 +01:00
|
|
|
win->xwin = XCreateWindow(e->dpy, RootWindow(e->dpy, e->scr),
|
|
|
|
win->x, win->y, win->w, win->h, 0,
|
2011-01-20 21:44:34 +01:00
|
|
|
e->depth, InputOutput, e->vis, 0, None);
|
2011-01-17 16:42:49 +01:00
|
|
|
if (win->xwin == None)
|
2011-02-03 10:15:01 +01:00
|
|
|
die("could not create window");
|
2011-01-17 16:42:49 +01:00
|
|
|
|
2012-10-29 18:25:17 +01:00
|
|
|
XSelectInput(e->dpy, win->xwin,
|
|
|
|
ExposureMask | ButtonReleaseMask | ButtonPressMask |
|
|
|
|
KeyPressMask | PointerMotionMask | StructureNotifyMask);
|
2011-01-29 22:37:40 +01:00
|
|
|
|
2011-02-17 11:08:25 +01:00
|
|
|
carrow = XCreateFontCursor(e->dpy, XC_left_ptr);
|
|
|
|
chand = XCreateFontCursor(e->dpy, XC_fleur);
|
|
|
|
cwatch = XCreateFontCursor(e->dpy, XC_watch);
|
2011-01-20 21:44:34 +01:00
|
|
|
|
2011-09-29 12:43:36 +02:00
|
|
|
if (XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), "black",
|
2012-03-02 19:42:14 +01:00
|
|
|
&col, &col) == 0)
|
2011-04-11 16:58:38 +02:00
|
|
|
{
|
2011-02-21 18:26:21 +01:00
|
|
|
die("could not allocate color: black");
|
2011-04-11 16:58:38 +02:00
|
|
|
}
|
2011-02-21 18:26:21 +01:00
|
|
|
none = XCreateBitmapFromData(e->dpy, win->xwin, none_data, 8, 8);
|
|
|
|
cnone = XCreatePixmapCursor(e->dpy, none, none, &col, &col, 0, 0);
|
|
|
|
|
2011-03-09 11:37:49 +01:00
|
|
|
gc = XCreateGC(e->dpy, win->xwin, 0, None);
|
2011-01-17 16:42:49 +01:00
|
|
|
|
2011-01-20 17:00:59 +01:00
|
|
|
win_set_title(win, "sxiv");
|
2011-01-20 16:32:16 +01:00
|
|
|
|
2012-03-28 21:14:01 +02:00
|
|
|
classhint.res_class = "Sxiv";
|
2012-10-29 00:53:50 +01:00
|
|
|
classhint.res_name = options->res_name != NULL ? options->res_name : "sxiv";
|
2011-02-01 16:40:37 +01:00
|
|
|
XSetClassHint(e->dpy, win->xwin, &classhint);
|
|
|
|
|
2012-08-07 12:09:52 +02:00
|
|
|
XSetWMProtocols(e->dpy, win->xwin, &wm_delete_win, 1);
|
|
|
|
|
2012-02-16 23:20:27 +01:00
|
|
|
if (!options->hide_bar) {
|
2013-01-07 14:30:42 +01:00
|
|
|
win->bar.h = barheight;
|
|
|
|
win->h -= win->bar.h;
|
2012-02-16 23:20:27 +01:00
|
|
|
}
|
2012-02-12 19:00:41 +01:00
|
|
|
|
2011-09-11 21:01:24 +02:00
|
|
|
if (options->fixed_win)
|
2011-02-04 00:25:57 +01:00
|
|
|
win_set_sizehints(win);
|
2011-01-17 16:42:49 +01:00
|
|
|
|
2011-01-18 16:32:40 +01:00
|
|
|
XMapWindow(e->dpy, win->xwin);
|
|
|
|
XFlush(e->dpy);
|
2011-02-09 10:01:49 +01:00
|
|
|
|
2011-01-28 13:34:16 +01:00
|
|
|
if (options->fullscreen)
|
|
|
|
win_toggle_fullscreen(win);
|
2011-01-17 16:42:49 +01:00
|
|
|
}
|
|
|
|
|
2013-02-08 22:05:31 +01:00
|
|
|
void win_close(win_t *win)
|
|
|
|
{
|
2011-09-29 12:43:36 +02:00
|
|
|
if (win == NULL || win->xwin == None)
|
2011-01-17 16:42:49 +01:00
|
|
|
return;
|
|
|
|
|
2011-02-17 11:08:25 +01:00
|
|
|
XFreeCursor(win->env.dpy, carrow);
|
2011-02-21 18:26:21 +01:00
|
|
|
XFreeCursor(win->env.dpy, cnone);
|
2011-02-17 11:08:25 +01:00
|
|
|
XFreeCursor(win->env.dpy, chand);
|
|
|
|
XFreeCursor(win->env.dpy, cwatch);
|
2011-01-29 22:37:40 +01:00
|
|
|
|
2011-02-17 16:22:54 +01:00
|
|
|
XFreeGC(win->env.dpy, gc);
|
2011-01-29 22:37:40 +01:00
|
|
|
|
2011-01-18 16:32:40 +01:00
|
|
|
XDestroyWindow(win->env.dpy, win->xwin);
|
|
|
|
XCloseDisplay(win->env.dpy);
|
2011-01-17 16:42:49 +01:00
|
|
|
}
|
2011-01-17 19:50:46 +01:00
|
|
|
|
2013-02-08 22:05:31 +01:00
|
|
|
bool win_configure(win_t *win, XConfigureEvent *c)
|
|
|
|
{
|
2011-09-11 21:01:24 +02:00
|
|
|
bool changed;
|
2011-01-17 19:50:46 +01:00
|
|
|
|
2012-10-29 18:25:17 +01:00
|
|
|
if (win == NULL || c == NULL)
|
2011-09-11 21:01:24 +02:00
|
|
|
return false;
|
2013-01-07 14:30:42 +01:00
|
|
|
|
|
|
|
if ((changed = win->w != c->width || win->h + win->bar.h != c->height)) {
|
2012-10-29 18:25:17 +01:00
|
|
|
if (win->pm != None) {
|
|
|
|
XFreePixmap(win->env.dpy, win->pm);
|
|
|
|
win->pm = None;
|
|
|
|
}
|
|
|
|
}
|
2011-01-22 23:27:29 +01:00
|
|
|
|
|
|
|
win->x = c->x;
|
|
|
|
win->y = c->y;
|
|
|
|
win->w = c->width;
|
2013-01-07 14:30:42 +01:00
|
|
|
win->h = c->height - win->bar.h;
|
2011-01-22 23:27:29 +01:00
|
|
|
win->bw = c->border_width;
|
|
|
|
|
2011-01-17 19:50:46 +01:00
|
|
|
return changed;
|
|
|
|
}
|
2011-01-18 20:11:28 +01:00
|
|
|
|
2013-02-08 22:05:31 +01:00
|
|
|
void win_expose(win_t *win, XExposeEvent *e)
|
|
|
|
{
|
2012-10-29 23:23:50 +01:00
|
|
|
if (win == NULL || win->xwin == None || win->pm == None || e == NULL)
|
2012-10-29 18:25:17 +01:00
|
|
|
return;
|
|
|
|
|
2012-10-29 23:23:50 +01:00
|
|
|
XCopyArea(win->env.dpy, win->pm, win->xwin, gc,
|
|
|
|
e->x, e->y, e->width, e->height, e->x, e->y);
|
2012-10-29 18:25:17 +01:00
|
|
|
}
|
|
|
|
|
2013-02-08 22:05:31 +01:00
|
|
|
bool win_moveresize(win_t *win, int x, int y, unsigned int w, unsigned int h)
|
|
|
|
{
|
2011-09-29 12:43:36 +02:00
|
|
|
if (win == NULL || win->xwin == None)
|
2011-09-11 21:01:24 +02:00
|
|
|
return false;
|
2011-02-04 00:25:57 +01:00
|
|
|
|
2013-02-09 01:39:18 +01:00
|
|
|
/* caller knows nothing about the bar */
|
|
|
|
h += win->bar.h;
|
|
|
|
|
2011-02-06 17:40:48 +01:00
|
|
|
x = MAX(0, x);
|
|
|
|
y = MAX(0, y);
|
2011-02-04 00:25:57 +01:00
|
|
|
w = MIN(w, win->env.scrw - 2 * win->bw);
|
|
|
|
h = MIN(h, win->env.scrh - 2 * win->bw);
|
|
|
|
|
2013-01-07 14:30:42 +01:00
|
|
|
if (win->x == x && win->y == y && win->w == w && win->h + win->bar.h == h)
|
2011-09-11 21:01:24 +02:00
|
|
|
return false;
|
2011-02-04 00:25:57 +01:00
|
|
|
|
2011-02-06 17:40:48 +01:00
|
|
|
win->x = x;
|
|
|
|
win->y = y;
|
2011-02-04 00:25:57 +01:00
|
|
|
win->w = w;
|
2013-01-07 14:30:42 +01:00
|
|
|
win->h = h - win->bar.h;
|
2011-02-04 00:25:57 +01:00
|
|
|
|
2011-09-11 21:01:24 +02:00
|
|
|
if (options->fixed_win)
|
2011-02-04 00:25:57 +01:00
|
|
|
win_set_sizehints(win);
|
|
|
|
|
2012-02-12 19:00:41 +01:00
|
|
|
XMoveResizeWindow(win->env.dpy, win->xwin, x, y, w, h);
|
2011-02-04 00:25:57 +01:00
|
|
|
|
2013-02-08 23:26:06 +01:00
|
|
|
if (win->pm != None) {
|
|
|
|
XFreePixmap(win->env.dpy, win->pm);
|
|
|
|
win->pm = None;
|
|
|
|
}
|
|
|
|
|
2011-09-11 21:01:24 +02:00
|
|
|
return true;
|
2011-02-04 00:25:57 +01:00
|
|
|
}
|
|
|
|
|
2013-02-08 22:05:31 +01:00
|
|
|
void win_toggle_fullscreen(win_t *win)
|
|
|
|
{
|
2011-01-23 16:14:41 +01:00
|
|
|
XEvent ev;
|
|
|
|
XClientMessageEvent *cm;
|
|
|
|
|
2011-09-29 12:43:36 +02:00
|
|
|
if (win == NULL || win->xwin == None)
|
2011-01-23 16:14:41 +01:00
|
|
|
return;
|
|
|
|
|
2011-09-11 21:01:24 +02:00
|
|
|
win->fullscreen = !win->fullscreen;
|
2011-01-23 16:14:41 +01:00
|
|
|
|
|
|
|
memset(&ev, 0, sizeof(ev));
|
|
|
|
ev.type = ClientMessage;
|
|
|
|
|
|
|
|
cm = &ev.xclient;
|
|
|
|
cm->window = win->xwin;
|
|
|
|
cm->message_type = XInternAtom(win->env.dpy, "_NET_WM_STATE", False);
|
|
|
|
cm->format = 32;
|
|
|
|
cm->data.l[0] = win->fullscreen;
|
|
|
|
cm->data.l[1] = XInternAtom(win->env.dpy, "_NET_WM_STATE_FULLSCREEN", False);
|
2011-04-19 14:44:26 +02:00
|
|
|
cm->data.l[2] = cm->data.l[3] = 0;
|
2011-01-23 16:14:41 +01:00
|
|
|
|
|
|
|
XSendEvent(win->env.dpy, DefaultRootWindow(win->env.dpy), False,
|
2011-04-14 20:01:11 +02:00
|
|
|
SubstructureNotifyMask | SubstructureRedirectMask, &ev);
|
2011-01-23 16:14:41 +01:00
|
|
|
}
|
|
|
|
|
2013-02-08 22:05:31 +01:00
|
|
|
void win_toggle_bar(win_t *win)
|
|
|
|
{
|
2012-02-15 22:33:39 +01:00
|
|
|
if (win == NULL || win->xwin == None)
|
|
|
|
return;
|
|
|
|
|
2013-01-07 14:30:42 +01:00
|
|
|
if (win->bar.h != 0) {
|
|
|
|
win->h += win->bar.h;
|
|
|
|
win->bar.h = 0;
|
2012-02-15 22:33:39 +01:00
|
|
|
} else {
|
2013-01-07 14:30:42 +01:00
|
|
|
win->bar.h = barheight;
|
|
|
|
win->h -= win->bar.h;
|
2012-02-15 22:33:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-08 22:05:31 +01:00
|
|
|
void win_clear(win_t *win)
|
|
|
|
{
|
2012-10-29 18:25:17 +01:00
|
|
|
int h;
|
2011-01-20 22:02:19 +01:00
|
|
|
win_env_t *e;
|
|
|
|
|
2011-09-29 12:43:36 +02:00
|
|
|
if (win == NULL || win->xwin == None)
|
2011-01-18 20:11:28 +01:00
|
|
|
return;
|
|
|
|
|
2013-01-07 14:30:42 +01:00
|
|
|
h = win->h + win->bar.h;
|
2011-01-20 22:02:19 +01:00
|
|
|
e = &win->env;
|
2011-03-09 11:37:49 +01:00
|
|
|
|
2012-10-29 18:25:17 +01:00
|
|
|
if (win->pm == None)
|
|
|
|
win->pm = XCreatePixmap(e->dpy, win->xwin, win->w, h, e->depth);
|
2011-01-23 16:14:41 +01:00
|
|
|
|
2012-03-02 19:42:14 +01:00
|
|
|
XSetForeground(e->dpy, gc, win->fullscreen ? win->fscol : win->bgcol);
|
2012-10-29 18:25:17 +01:00
|
|
|
XFillRectangle(e->dpy, win->pm, gc, 0, 0, win->w, h);
|
2011-02-17 16:22:54 +01:00
|
|
|
}
|
|
|
|
|
2013-02-08 22:05:31 +01:00
|
|
|
void win_draw_bar(win_t *win)
|
|
|
|
{
|
2013-01-07 14:30:42 +01:00
|
|
|
int len, olen, x, y, w, tw;
|
|
|
|
char rest[3];
|
|
|
|
const char *dots = "...";
|
2012-02-12 19:00:41 +01:00
|
|
|
win_env_t *e;
|
|
|
|
|
2012-10-29 23:23:50 +01:00
|
|
|
if (win == NULL || win->xwin == None || win->pm == None)
|
2012-02-12 19:00:41 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
e = &win->env;
|
|
|
|
y = win->h + font.ascent + V_TEXT_PAD;
|
2013-01-07 14:30:42 +01:00
|
|
|
w = win->w;
|
2012-02-12 19:00:41 +01:00
|
|
|
|
2013-01-07 14:30:42 +01:00
|
|
|
XSetForeground(e->dpy, gc, win->bar.bgcol);
|
|
|
|
XFillRectangle(e->dpy, win->pm, gc, 0, win->h, win->w, win->bar.h);
|
2012-02-12 19:00:41 +01:00
|
|
|
|
2013-01-07 14:30:42 +01:00
|
|
|
XSetForeground(e->dpy, gc, win->bar.fgcol);
|
|
|
|
XSetBackground(e->dpy, gc, win->bar.bgcol);
|
2012-02-12 19:00:41 +01:00
|
|
|
|
2013-01-07 14:30:42 +01:00
|
|
|
if (win->bar.r != NULL) {
|
|
|
|
len = strlen(win->bar.r);
|
|
|
|
if (len > 0) {
|
|
|
|
if ((tw = win_textwidth(win->bar.r, len, true)) > w)
|
|
|
|
return;
|
|
|
|
x = win->w - tw + H_TEXT_PAD;
|
|
|
|
w -= tw;
|
|
|
|
if (font.set)
|
|
|
|
XmbDrawString(e->dpy, win->pm, font.set, gc, x, y, win->bar.r, len);
|
|
|
|
else
|
|
|
|
XDrawString(e->dpy, win->pm, gc, x, y, win->bar.r, len);
|
2012-02-12 19:00:41 +01:00
|
|
|
}
|
2013-01-07 14:30:42 +01:00
|
|
|
}
|
|
|
|
if (win->bar.l != NULL) {
|
|
|
|
olen = len = strlen(win->bar.l);
|
|
|
|
while (len > 0 && (tw = win_textwidth(win->bar.l, len, true)) > w)
|
|
|
|
len--;
|
2012-02-12 19:00:41 +01:00
|
|
|
if (len > 0) {
|
2013-01-07 14:30:42 +01:00
|
|
|
if (len != olen) {
|
|
|
|
w = strlen(dots);
|
|
|
|
if (len <= w)
|
|
|
|
return;
|
|
|
|
memcpy(rest, win->bar.l + len - w, w);
|
|
|
|
memcpy(win->bar.l + len - w, dots, w);
|
|
|
|
}
|
|
|
|
x = H_TEXT_PAD;
|
2012-02-12 19:00:41 +01:00
|
|
|
if (font.set)
|
2013-01-07 14:30:42 +01:00
|
|
|
XmbDrawString(e->dpy, win->pm, font.set, gc, x, y, win->bar.l, len);
|
2012-02-12 19:00:41 +01:00
|
|
|
else
|
2013-01-07 14:30:42 +01:00
|
|
|
XDrawString(e->dpy, win->pm, gc, x, y, win->bar.l, len);
|
|
|
|
if (len != olen)
|
|
|
|
memcpy(win->bar.l + len - w, rest, w);
|
2012-02-12 19:00:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-08 22:05:31 +01:00
|
|
|
void win_draw(win_t *win)
|
|
|
|
{
|
2012-10-29 23:23:50 +01:00
|
|
|
if (win == NULL || win->xwin == None || win->pm == None)
|
2011-03-15 13:55:52 +01:00
|
|
|
return;
|
|
|
|
|
2013-01-07 14:30:42 +01:00
|
|
|
if (win->bar.h > 0)
|
2012-03-16 21:03:49 +01:00
|
|
|
win_draw_bar(win);
|
2012-02-12 19:00:41 +01:00
|
|
|
|
2012-10-29 18:25:17 +01:00
|
|
|
XCopyArea(win->env.dpy, win->pm, win->xwin, gc,
|
2013-01-07 14:30:42 +01:00
|
|
|
0, 0, win->w, win->h + win->bar.h, 0, 0);
|
2011-02-17 16:22:54 +01:00
|
|
|
}
|
|
|
|
|
2011-03-09 11:37:49 +01:00
|
|
|
void win_draw_rect(win_t *win, Pixmap pm, int x, int y, int w, int h,
|
2012-10-29 18:25:17 +01:00
|
|
|
bool fill, int lw, unsigned long col)
|
|
|
|
{
|
2011-02-17 16:22:54 +01:00
|
|
|
XGCValues gcval;
|
|
|
|
|
2011-09-29 12:43:36 +02:00
|
|
|
if (win == NULL || pm == None)
|
2011-02-17 16:22:54 +01:00
|
|
|
return;
|
|
|
|
|
2011-03-09 11:37:49 +01:00
|
|
|
gcval.line_width = lw;
|
|
|
|
gcval.foreground = col;
|
|
|
|
XChangeGC(win->env.dpy, gc, GCForeground | GCLineWidth, &gcval);
|
2011-02-17 16:22:54 +01:00
|
|
|
|
2011-03-09 11:37:49 +01:00
|
|
|
if (fill)
|
|
|
|
XFillRectangle(win->env.dpy, pm, gc, x, y, w, h);
|
2011-02-17 16:22:54 +01:00
|
|
|
else
|
2011-03-09 11:37:49 +01:00
|
|
|
XDrawRectangle(win->env.dpy, pm, gc, x, y, w, h);
|
2011-01-20 21:44:34 +01:00
|
|
|
}
|
|
|
|
|
2013-02-08 22:05:31 +01:00
|
|
|
int win_textwidth(const char *text, unsigned int len, bool with_padding)
|
|
|
|
{
|
2012-02-21 12:49:29 +01:00
|
|
|
XRectangle r;
|
|
|
|
int padding = with_padding ? 2 * H_TEXT_PAD : 0;
|
|
|
|
|
|
|
|
if (font.set) {
|
|
|
|
XmbTextExtents(font.set, text, len, NULL, &r);
|
|
|
|
return r.width + padding;
|
|
|
|
} else {
|
|
|
|
return XTextWidth(font.xfont, text, len) + padding;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-08 22:05:31 +01:00
|
|
|
void win_set_title(win_t *win, const char *title)
|
|
|
|
{
|
2011-09-29 12:43:36 +02:00
|
|
|
if (win == NULL || win->xwin == None)
|
2011-02-04 00:25:57 +01:00
|
|
|
return;
|
|
|
|
|
2011-09-29 12:43:36 +02:00
|
|
|
if (title == NULL)
|
2011-02-04 00:25:57 +01:00
|
|
|
title = "sxiv";
|
|
|
|
|
|
|
|
XStoreName(win->env.dpy, win->xwin, title);
|
|
|
|
XSetIconName(win->env.dpy, win->xwin, title);
|
2011-02-09 09:45:35 +01:00
|
|
|
|
|
|
|
XChangeProperty(win->env.dpy, win->xwin,
|
|
|
|
XInternAtom(win->env.dpy, "_NET_WM_NAME", False),
|
|
|
|
XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
|
|
|
|
PropModeReplace, (unsigned char *) title, strlen(title));
|
|
|
|
XChangeProperty(win->env.dpy, win->xwin,
|
|
|
|
XInternAtom(win->env.dpy, "_NET_WM_ICON_NAME", False),
|
|
|
|
XInternAtom(win->env.dpy, "UTF8_STRING", False), 8,
|
|
|
|
PropModeReplace, (unsigned char *) title, strlen(title));
|
2011-02-04 00:25:57 +01:00
|
|
|
}
|
|
|
|
|
2013-02-08 22:05:31 +01:00
|
|
|
void win_set_bar_info(win_t *win, char *linfo, char *rinfo)
|
|
|
|
{
|
2012-02-12 19:00:41 +01:00
|
|
|
if (win != NULL) {
|
2013-01-07 14:30:42 +01:00
|
|
|
win->bar.l = linfo;
|
|
|
|
win->bar.r = rinfo;
|
2012-02-12 19:00:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-08 22:05:31 +01:00
|
|
|
void win_set_cursor(win_t *win, cursor_t cursor)
|
|
|
|
{
|
2011-09-29 12:43:36 +02:00
|
|
|
if (win == NULL || win->xwin == None)
|
2011-01-29 22:37:40 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
switch (cursor) {
|
2011-02-21 18:26:21 +01:00
|
|
|
case CURSOR_NONE:
|
|
|
|
XDefineCursor(win->env.dpy, win->xwin, cnone);
|
|
|
|
break;
|
2011-01-29 22:37:40 +01:00
|
|
|
case CURSOR_HAND:
|
2011-02-17 11:08:25 +01:00
|
|
|
XDefineCursor(win->env.dpy, win->xwin, chand);
|
|
|
|
break;
|
|
|
|
case CURSOR_WATCH:
|
|
|
|
XDefineCursor(win->env.dpy, win->xwin, cwatch);
|
2011-01-29 22:37:40 +01:00
|
|
|
break;
|
|
|
|
case CURSOR_ARROW:
|
|
|
|
default:
|
2011-02-17 11:08:25 +01:00
|
|
|
XDefineCursor(win->env.dpy, win->xwin, carrow);
|
2011-01-29 22:37:40 +01:00
|
|
|
break;
|
|
|
|
}
|
2011-03-01 18:49:02 +01:00
|
|
|
|
|
|
|
XFlush(win->env.dpy);
|
2011-01-29 22:37:40 +01:00
|
|
|
}
|