2017-01-15 23:04:51 +03:00
|
|
|
/* nuklear - v1.32.0 - public domain */
|
2016-04-11 02:34:59 +03:00
|
|
|
#include <assert.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <string.h>
|
2016-04-28 15:38:44 +03:00
|
|
|
#include <limits.h>
|
2016-04-30 18:09:01 +03:00
|
|
|
#include <math.h>
|
2016-04-11 02:34:59 +03:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <unistd.h>
|
2016-04-30 18:09:01 +03:00
|
|
|
#include <time.h>
|
2016-04-11 02:34:59 +03:00
|
|
|
|
|
|
|
#define NK_INCLUDE_FIXED_TYPES
|
2016-04-28 15:38:44 +03:00
|
|
|
#define NK_INCLUDE_STANDARD_IO
|
2016-08-06 18:44:00 +03:00
|
|
|
#define NK_INCLUDE_STANDARD_VARARGS
|
2016-04-11 02:34:59 +03:00
|
|
|
#define NK_INCLUDE_DEFAULT_ALLOCATOR
|
2016-04-30 18:09:01 +03:00
|
|
|
#define NK_IMPLEMENTATION
|
|
|
|
#define NK_XLIB_IMPLEMENTATION
|
|
|
|
#include "../../nuklear.h"
|
2016-04-11 02:34:59 +03:00
|
|
|
#include "nuklear_xlib.h"
|
2016-04-30 18:09:01 +03:00
|
|
|
|
|
|
|
#define DTIME 20
|
|
|
|
#define WINDOW_WIDTH 800
|
|
|
|
#define WINDOW_HEIGHT 600
|
2016-04-11 02:34:59 +03:00
|
|
|
|
|
|
|
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;
|
2017-01-23 21:33:58 +03:00
|
|
|
Atom wm_delete_window;
|
2016-04-11 02:34:59 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
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 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));
|
|
|
|
}
|
|
|
|
|
2016-04-28 15:38:44 +03:00
|
|
|
/* ===============================================================
|
|
|
|
*
|
|
|
|
* EXAMPLE
|
|
|
|
*
|
|
|
|
* ===============================================================*/
|
2018-01-01 19:20:55 +03:00
|
|
|
/* This are some code examples to provide a small overview of what can be
|
|
|
|
* done with this library. To try out an example uncomment the defines */
|
|
|
|
/*#define INCLUDE_ALL */
|
|
|
|
/*#define INCLUDE_STYLE */
|
|
|
|
/*#define INCLUDE_CALCULATOR */
|
2021-12-16 18:16:33 +03:00
|
|
|
/*#define INCLUDE_CANVAS */
|
2018-01-01 19:20:55 +03:00
|
|
|
/*#define INCLUDE_OVERVIEW */
|
|
|
|
/*#define INCLUDE_NODE_EDITOR */
|
|
|
|
|
|
|
|
#ifdef INCLUDE_ALL
|
|
|
|
#define INCLUDE_STYLE
|
|
|
|
#define INCLUDE_CALCULATOR
|
2021-12-16 18:16:33 +03:00
|
|
|
#define INCLUDE_CANVAS
|
2018-01-01 19:20:55 +03:00
|
|
|
#define INCLUDE_OVERVIEW
|
|
|
|
#define INCLUDE_NODE_EDITOR
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef INCLUDE_STYLE
|
2022-03-14 22:46:44 +03:00
|
|
|
#include "../../demo/common/style.c"
|
2018-01-01 19:20:55 +03:00
|
|
|
#endif
|
|
|
|
#ifdef INCLUDE_CALCULATOR
|
2022-03-14 22:46:44 +03:00
|
|
|
#include "../../demo/common/calculator.c"
|
2018-01-01 19:20:55 +03:00
|
|
|
#endif
|
2021-12-16 18:16:33 +03:00
|
|
|
#ifdef INCLUDE_CANVAS
|
2022-03-14 22:46:44 +03:00
|
|
|
#include "../../demo/common/canvas.c"
|
2021-12-16 18:16:33 +03:00
|
|
|
#endif
|
2018-01-01 19:20:55 +03:00
|
|
|
#ifdef INCLUDE_OVERVIEW
|
2022-03-14 22:46:44 +03:00
|
|
|
#include "../../demo/common/overview.c"
|
2018-01-01 19:20:55 +03:00
|
|
|
#endif
|
|
|
|
#ifdef INCLUDE_NODE_EDITOR
|
2022-03-14 22:46:44 +03:00
|
|
|
#include "../../demo/common/node_editor.c"
|
2018-01-01 19:20:55 +03:00
|
|
|
#endif
|
2016-04-28 15:38:44 +03:00
|
|
|
|
|
|
|
/* ===============================================================
|
|
|
|
*
|
|
|
|
* DEMO
|
|
|
|
*
|
|
|
|
* ===============================================================*/
|
2016-04-11 02:34:59 +03:00
|
|
|
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);
|
2016-04-22 22:54:05 +03:00
|
|
|
if (!xw.dpy) die("Could not open a display; perhaps $DISPLAY is not set?");
|
2016-04-11 02:34:59 +03:00
|
|
|
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);
|
2017-03-18 20:09:54 +03:00
|
|
|
|
2016-04-11 02:34:59 +03:00
|
|
|
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);
|
2017-03-18 20:09:54 +03:00
|
|
|
|
2016-04-11 02:34:59 +03:00
|
|
|
XStoreName(xw.dpy, xw.win, "X11");
|
|
|
|
XMapWindow(xw.dpy, xw.win);
|
2017-01-23 21:33:58 +03:00
|
|
|
xw.wm_delete_window = XInternAtom(xw.dpy, "WM_DELETE_WINDOW", False);
|
|
|
|
XSetWMProtocols(xw.dpy, xw.win, &xw.wm_delete_window, 1);
|
2016-04-11 02:34:59 +03:00
|
|
|
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);
|
2016-04-28 15:38:44 +03:00
|
|
|
|
2016-04-11 02:34:59 +03:00
|
|
|
while (running)
|
|
|
|
{
|
|
|
|
/* Input */
|
|
|
|
XEvent evt;
|
|
|
|
started = timestamp();
|
|
|
|
nk_input_begin(ctx);
|
2017-01-23 21:33:58 +03:00
|
|
|
while (XPending(xw.dpy)) {
|
|
|
|
XNextEvent(xw.dpy, &evt);
|
|
|
|
if (evt.type == ClientMessage) goto cleanup;
|
2016-04-11 02:34:59 +03:00
|
|
|
if (XFilterEvent(&evt, xw.win)) continue;
|
|
|
|
nk_xlib_handle_event(xw.dpy, xw.screen, xw.win, &evt);
|
|
|
|
}
|
|
|
|
nk_input_end(ctx);
|
|
|
|
|
|
|
|
/* GUI */
|
2016-10-30 00:23:46 +03:00
|
|
|
if (nk_begin(ctx, "Demo", nk_rect(50, 50, 200, 200),
|
2016-04-11 02:34:59 +03:00
|
|
|
NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|
|
2017-01-23 21:33:58 +03:00
|
|
|
NK_WINDOW_CLOSABLE|NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE))
|
2016-04-11 02:34:59 +03:00
|
|
|
{
|
|
|
|
enum {EASY, HARD};
|
|
|
|
static int op = EASY;
|
|
|
|
static int property = 20;
|
|
|
|
|
|
|
|
nk_layout_row_static(ctx, 30, 80, 1);
|
2016-07-15 19:04:50 +03:00
|
|
|
if (nk_button_label(ctx, "button"))
|
2016-04-11 02:34:59 +03:00
|
|
|
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;
|
2016-04-21 11:54:01 +03:00
|
|
|
nk_layout_row_dynamic(ctx, 25, 1);
|
2016-04-11 02:34:59 +03:00
|
|
|
nk_property_int(ctx, "Compression:", 0, &property, 100, 10, 1);
|
|
|
|
}
|
2016-10-30 00:23:46 +03:00
|
|
|
nk_end(ctx);
|
2017-04-06 10:51:50 +03:00
|
|
|
if (nk_window_is_hidden(ctx, "Demo")) break;
|
2016-04-11 02:34:59 +03:00
|
|
|
|
2016-04-28 15:38:44 +03:00
|
|
|
/* -------------- EXAMPLES ---------------- */
|
2018-01-01 19:20:55 +03:00
|
|
|
#ifdef INCLUDE_CALCULATOR
|
|
|
|
calculator(ctx);
|
|
|
|
#endif
|
2021-12-16 18:16:33 +03:00
|
|
|
#ifdef INCLUDE_CANVAS
|
|
|
|
canvas(ctx);
|
|
|
|
#endif
|
2018-01-01 19:20:55 +03:00
|
|
|
#ifdef INCLUDE_OVERVIEW
|
|
|
|
overview(ctx);
|
|
|
|
#endif
|
|
|
|
#ifdef INCLUDE_NODE_EDITOR
|
|
|
|
node_editor(ctx);
|
|
|
|
#endif
|
2016-04-28 15:38:44 +03:00
|
|
|
/* ----------------------------------------- */
|
|
|
|
|
2016-04-11 02:34:59 +03:00
|
|
|
/* 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);
|
|
|
|
}
|
|
|
|
|
2017-01-23 21:33:58 +03:00
|
|
|
cleanup:
|
2016-04-11 02:34:59 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|