Skip to content

Instantly share code, notes, and snippets.

@mattn
Created May 2, 2009 16:25
Show Gist options
  • Save mattn/105615 to your computer and use it in GitHub Desktop.
Save mattn/105615 to your computer and use it in GitHub Desktop.
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#include <X11/Xlocale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int
main(int argc, char *argv[])
{
Display *dpy;
Window root, win;
XEvent evt;
GC gc;
unsigned long black, white;
int width, height, x, y, pixbyte, i, j;
XWindowAttributes attr;
XFontSet font;
XFontSetExtents *extents;
char **missing_charset_list_return;
int missing_charset_count_return;
char *def_string_return;
int loop = 1;
XRectangle inkrect, logrect;
Atom _NET_WM_NAME;
Atom _NET_WM_ICON_NAME;
Atom UTF8_STRING;
Atom WM_DELETE;
Atom MESSAGE;
const char *str = "ハローワールド";
const char *strs[] = {"ハローワールド", NULL};
XTextProperty windowname;
XClassHint classhints;
XWMHints wmhints;
setlocale(LC_ALL, "");
dpy = XOpenDisplay(NULL);
WM_DELETE = XInternAtom(dpy, "WM_DELETE_WINDOW", 0);
_NET_WM_NAME = XInternAtom(dpy, "_NET_WM_NAME", 0);
_NET_WM_ICON_NAME = XInternAtom(dpy, "_NET_WM_ICON_NAME", 0);
UTF8_STRING = XInternAtom(dpy, "UTF8_STRING", 0);
black = BlackPixel(dpy, 0);
white = WhitePixel(dpy, 0);
x = 100;
y = 50;
width = 150;
height = 50;
root = RootWindow(dpy, 0);
win = XCreateSimpleWindow(dpy, root, 100, 50, width, height, 0, 0, white);
XChangeProperty(dpy, win, _NET_WM_NAME, UTF8_STRING, 8, 0, str, strlen(str));
XChangeProperty(dpy, win, XA_WM_NAME, XA_STRING, 8, 0, str, strlen(str));
XChangeProperty(dpy, win, _NET_WM_ICON_NAME, UTF8_STRING, 8, 0, str, strlen(str));
XChangeProperty(dpy, win, XA_WM_ICON_NAME, XA_STRING, 8, 0, str, strlen(str));
XSelectInput(dpy, win,
ButtonPressMask | ButtonReleaseMask | ExposureMask | StructureNotifyMask);
font =
XCreateFontSet(dpy, "-misc-*-medium-r-normal--14-*-*-*-*-*-*",
&missing_charset_list_return, &missing_charset_count_return,
&def_string_return);
gc = XCreateGC(dpy, win, 0, 0);
XSetBackground(dpy, gc, white);
XSetForeground(dpy, gc, black);
XMapRaised(dpy, win);
while (loop)
{
XFlush(dpy);
if (XEventsQueued(dpy, QueuedAfterFlush) != 0)
{
XNextEvent(dpy, &evt);
switch (evt.type)
{
case ClientMessage:
MESSAGE = evt.xclient.message_type;
loop = 0;
break;
case Expose:
XGetWindowAttributes(dpy, win, &attr);
XSetForeground(dpy, gc, black);
Xutf8TextExtents(font, str, strlen(str), &inkrect, &logrect);
extents = XExtentsOfFontSet(font);
Xutf8DrawString(dpy, win, font, gc,
(attr.width - logrect.width) / 2,
(attr.height - logrect.height) / 2 - extents->max_logical_extent.y,
str, strlen(str));
XFlush(dpy);
break;
case ButtonPress:
printf("ButtonPress\n");
printf("x = %d, y = %d\n", evt.xbutton.x, evt.xbutton.y);
break;
case ButtonRelease:
printf("ButtonRelease\n");
XGetWindowAttributes(dpy, win, &attr);
printf("x = %d, y = %d\n", evt.xbutton.x, evt.xbutton.y);
if (evt.xbutton.x > 0 && evt.xbutton.x < attr.width
&& evt.xbutton.y > 0 && evt.xbutton.y < attr.height)
loop = 0;
break;
}
}
}
XFreeFontSet(dpy, font);
XDestroyWindow(dpy, win);
XCloseDisplay(dpy);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment