d7a9f14c1c
Fixed window movement for windows without header and some small wrong calculation inside the panel layout code. In addition I added another example implementing a simple calculator.
167 lines
4.4 KiB
C
167 lines
4.4 KiB
C
/* nuklear - v1.00 - public domain */
|
|
#include <assert.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <stdarg.h>
|
|
#include <string.h>
|
|
#include <time.h>
|
|
#include <sys/time.h>
|
|
#include <unistd.h>
|
|
#include <math.h>
|
|
|
|
#include <X11/Xlib.h>
|
|
#include <X11/Xutil.h>
|
|
#include <X11/Xresource.h>
|
|
|
|
#define DTIME 20
|
|
#define WINDOW_WIDTH 800
|
|
#define WINDOW_HEIGHT 600
|
|
|
|
#define NK_INCLUDE_FIXED_TYPES
|
|
#define NK_INCLUDE_DEFAULT_ALLOCATOR
|
|
#include "nuklear_xlib.h"
|
|
#include "nuklear_xlib.c"
|
|
|
|
typedef struct XWindow XWindow;
|
|
struct XWindow {
|
|
Display *dpy;
|
|
Window root;
|
|
Visual *vis;
|
|
Colormap cmap;
|
|
XWindowAttributes attr;
|
|
XSetWindowAttributes swa;
|
|
Window win;
|
|
int screen;
|
|
XFont *font;
|
|
unsigned int width;
|
|
unsigned int height;
|
|
};
|
|
|
|
static void
|
|
die(const char *fmt, ...)
|
|
{
|
|
va_list ap;
|
|
va_start(ap, fmt);
|
|
vfprintf(stderr, fmt, ap);
|
|
va_end(ap);
|
|
fputs("\n", stderr);
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
static void*
|
|
xcalloc(size_t siz, size_t n)
|
|
{
|
|
void *ptr = calloc(siz, n);
|
|
if (!ptr) die("Out of memory\n");
|
|
return ptr;
|
|
}
|
|
|
|
static long
|
|
timestamp(void)
|
|
{
|
|
struct timeval tv;
|
|
if (gettimeofday(&tv, NULL) < 0) return 0;
|
|
return (long)((long)tv.tv_sec * 1000 + (long)tv.tv_usec/1000);
|
|
}
|
|
|
|
static void
|
|
sleep_for(long t)
|
|
{
|
|
struct timespec req;
|
|
const time_t sec = (int)(t/1000);
|
|
const long ms = t - (sec * 1000);
|
|
req.tv_sec = sec;
|
|
req.tv_nsec = ms * 1000000L;
|
|
while(-1 == nanosleep(&req, &req));
|
|
}
|
|
|
|
int
|
|
main(void)
|
|
{
|
|
long dt;
|
|
long started;
|
|
int running = 1;
|
|
XWindow xw;
|
|
struct nk_context *ctx;
|
|
|
|
/* X11 */
|
|
memset(&xw, 0, sizeof xw);
|
|
xw.dpy = XOpenDisplay(NULL);
|
|
xw.root = DefaultRootWindow(xw.dpy);
|
|
xw.screen = XDefaultScreen(xw.dpy);
|
|
xw.vis = XDefaultVisual(xw.dpy, xw.screen);
|
|
xw.cmap = XCreateColormap(xw.dpy,xw.root,xw.vis,AllocNone);
|
|
xw.swa.colormap = xw.cmap;
|
|
xw.swa.event_mask =
|
|
ExposureMask | KeyPressMask | KeyReleaseMask |
|
|
ButtonPress | ButtonReleaseMask| ButtonMotionMask |
|
|
Button1MotionMask | Button3MotionMask | Button4MotionMask | Button5MotionMask|
|
|
PointerMotionMask | KeymapStateMask;
|
|
xw.win = XCreateWindow(xw.dpy, xw.root, 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT, 0,
|
|
XDefaultDepth(xw.dpy, xw.screen), InputOutput,
|
|
xw.vis, CWEventMask | CWColormap, &xw.swa);
|
|
XStoreName(xw.dpy, xw.win, "X11");
|
|
XMapWindow(xw.dpy, xw.win);
|
|
XGetWindowAttributes(xw.dpy, xw.win, &xw.attr);
|
|
xw.width = (unsigned int)xw.attr.width;
|
|
xw.height = (unsigned int)xw.attr.height;
|
|
|
|
|
|
/* GUI */
|
|
xw.font = nk_xfont_create(xw.dpy, "fixed");
|
|
ctx = nk_xlib_init(xw.font, xw.dpy, xw.screen, xw.win, xw.width, xw.height);
|
|
while (running)
|
|
{
|
|
/* Input */
|
|
XEvent evt;
|
|
started = timestamp();
|
|
nk_input_begin(ctx);
|
|
while (XCheckWindowEvent(xw.dpy, xw.win, xw.swa.event_mask, &evt)){
|
|
if (XFilterEvent(&evt, xw.win)) continue;
|
|
nk_xlib_handle_event(xw.dpy, xw.screen, xw.win, &evt);
|
|
}
|
|
nk_input_end(ctx);
|
|
|
|
/* GUI */
|
|
{struct nk_panel layout;
|
|
if (nk_begin(ctx, &layout, "Demo", nk_rect(50, 50, 200, 200),
|
|
NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|
|
|
NK_WINDOW_CLOSABLE|NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE))
|
|
{
|
|
enum {EASY, HARD};
|
|
static int op = EASY;
|
|
static int property = 20;
|
|
|
|
nk_layout_row_static(ctx, 30, 80, 1);
|
|
if (nk_button_label(ctx, "button", NK_BUTTON_DEFAULT))
|
|
fprintf(stdout, "button pressed\n");
|
|
nk_layout_row_dynamic(ctx, 30, 2);
|
|
if (nk_option_label(ctx, "easy", op == EASY)) op = EASY;
|
|
if (nk_option_label(ctx, "hard", op == HARD)) op = HARD;
|
|
nk_layout_row_dynamic(ctx, 22, 1);
|
|
nk_property_int(ctx, "Compression:", 0, &property, 100, 10, 1);
|
|
}
|
|
nk_end(ctx);}
|
|
if (nk_window_is_closed(ctx, "Demo")) break;
|
|
|
|
/* Draw */
|
|
XClearWindow(xw.dpy, xw.win);
|
|
nk_xlib_render(xw.win, nk_rgb(30,30,30));
|
|
XFlush(xw.dpy);
|
|
|
|
/* Timing */
|
|
dt = timestamp() - started;
|
|
if (dt < DTIME)
|
|
sleep_for(DTIME - dt);
|
|
}
|
|
|
|
nk_xfont_del(xw.dpy, xw.font);
|
|
nk_xlib_shutdown();
|
|
XUnmapWindow(xw.dpy, xw.win);
|
|
XFreeColormap(xw.dpy, xw.cmap);
|
|
XDestroyWindow(xw.dpy, xw.win);
|
|
XCloseDisplay(xw.dpy);
|
|
return 0;
|
|
}
|
|
|