Fixed PR styling issues

This commit is contained in:
vurtun 2017-03-18 18:09:54 +01:00
parent e7ae9bfff5
commit 6bb00890fb
2 changed files with 156 additions and 181 deletions

View File

@ -87,7 +87,7 @@ sleep_for(long t)
* EXAMPLE
*
* ===============================================================*/
/* This are some code examples to provide a small overview of what can be
/* These are some code examples to provide a small overview of what can be
* done with this library. To try out an example uncomment the include
* and the corresponding function. */
/*#include "../style.c"*/
@ -113,11 +113,11 @@ main(void)
memset(&xw, 0, sizeof xw);
xw.dpy = XOpenDisplay(NULL);
if (!xw.dpy) die("Could not open a display; perhaps $DISPLAY is not set?");
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 |
@ -127,11 +127,11 @@ main(void)
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);
xw.wm_delete_window = XInternAtom(xw.dpy, "WM_DELETE_WINDOW", False);
XSetWMProtocols(xw.dpy, xw.win, &xw.wm_delete_window, 1);
XGetWindowAttributes(xw.dpy, xw.win, &xw.attr);
xw.width = (unsigned int)xw.attr.width;
xw.height = (unsigned int)xw.attr.height;
@ -145,6 +145,7 @@ main(void)
/*set_style(ctx, THEME_RED);*/
/*set_style(ctx, THEME_BLUE);*/
/*set_style(ctx, THEME_DARK);*/
while (running)
{
/* Input */

View File

@ -1,7 +1,7 @@
/*
* Nuklear - v1.32.0 - public domain
* no warrenty implied; use at your own risk.
* authored from 2015-2016 by Micha Mettke
* authored from 2015-2017 by Micha Mettke
*/
/*
* ==============================================================
@ -14,19 +14,21 @@
#define NK_XLIB_H_
#include <X11/Xlib.h>
/* Font */
typedef struct XFont XFont;
NK_API struct nk_context* nk_xlib_init(XFont*, Display*, int scrn, Window root, unsigned w, unsigned h);
NK_API int nk_xlib_handle_event(Display*, int scrn, Window, XEvent*);
NK_API void nk_xlib_render(Drawable screen, struct nk_color clear);
NK_API void nk_xlib_shutdown(void);
NK_API void nk_xlib_set_font(XFont*);
NK_API void nk_xlib_push_font(XFont*);
NK_API void nk_xlib_paste(nk_handle, struct nk_text_edit*);
NK_API void nk_xlib_copy(nk_handle, const char*, int len);
/* Font */
NK_API XFont* nk_xfont_create(Display *dpy, const char *name);
NK_API void nk_xfont_del(Display *dpy, XFont *font);
NK_API struct nk_context* nk_xlib_init(XFont *font, Display *dpy, int screen, Window root, unsigned int w, unsigned int h);
NK_API int nk_xlib_handle_event(Display *dpy, int screen, Window win, XEvent *evt);
NK_API void nk_xlib_render(Drawable screen, struct nk_color clear);
NK_API void nk_xlib_shutdown(void);
NK_API void nk_xlib_set_font(XFont *font);
NK_API void nk_xlib_push_font(XFont *xfont);
NK_API void nk_xlib_paste(nk_handle handle, struct nk_text_edit* edit);
NK_API void nk_xlib_copy(nk_handle handle, const char* str, int len);
#endif
/*
* ==============================================================
@ -60,6 +62,15 @@ struct XSurface {
unsigned int w, h;
};
static struct {
char *clipboard_data;
int clipboard_len;
struct nk_text_edit* clipboard_target;
Atom xa_clipboard;
Atom xa_targets;
Atom xa_text;
Atom xa_utf8_string;
struct nk_context ctx;
struct XSurface *surf;
Cursor cursor;
@ -74,15 +85,6 @@ static struct {
#define MAX(a,b) ((a) < (b) ? (b) : (a))
#endif
static char* clipboard_data = NULL;
static int clipboard_len = 0;
static struct nk_text_edit* clipboard_target = NULL;
static Atom xa_clipboard;
static Atom xa_targets;
static Atom xa_text;
static Atom xa_utf8_string;
static unsigned long
nk_color_from_byte(const nk_byte *c)
{
@ -149,28 +151,26 @@ nk_xsurf_stroke_rect(XSurface* surf, short x, short y, unsigned short w,
unsigned long c = nk_color_from_byte(&col.r);
XSetForeground(surf->dpy, surf->gc, c);
XSetLineAttributes(surf->dpy, surf->gc, line_thickness, LineSolid, CapButt, JoinMiter);
if (r == 0) {
XDrawRectangle(surf->dpy, surf->drawable, surf->gc, x, y, w, h);
} else {
short xc = x + r;
short yc = y + r;
short wc = (short)(w - 2 * r);
short hc = (short)(h - 2 * r);
if (r == 0) {XDrawRectangle(surf->dpy, surf->drawable, surf->gc, x, y, w, h);return;}
XDrawLine(surf->dpy, surf->drawable, surf->gc, xc, y, xc+wc, y);
XDrawLine(surf->dpy, surf->drawable, surf->gc, x+w, yc, x+w, yc+hc);
XDrawLine(surf->dpy, surf->drawable, surf->gc, xc, y+h, xc+wc, y+h);
XDrawLine(surf->dpy, surf->drawable, surf->gc, x, yc, x, yc+hc);
{short xc = x + r;
short yc = y + r;
short wc = (short)(w - 2 * r);
short hc = (short)(h - 2 * r);
XDrawArc(surf->dpy, surf->drawable, surf->gc, xc + wc - r, y,
(unsigned)r*2, (unsigned)r*2, 0 * 64, 90 * 64);
XDrawArc(surf->dpy, surf->drawable, surf->gc, x, y,
(unsigned)r*2, (unsigned)r*2, 90 * 64, 90 * 64);
XDrawArc(surf->dpy, surf->drawable, surf->gc, x, yc + hc - r,
(unsigned)r*2, (unsigned)2*r, 180 * 64, 90 * 64);
XDrawArc(surf->dpy, surf->drawable, surf->gc, xc + wc - r, yc + hc - r,
(unsigned)r*2, (unsigned)2*r, -90 * 64, 90 * 64);
}
XDrawLine(surf->dpy, surf->drawable, surf->gc, xc, y, xc+wc, y);
XDrawLine(surf->dpy, surf->drawable, surf->gc, x+w, yc, x+w, yc+hc);
XDrawLine(surf->dpy, surf->drawable, surf->gc, xc, y+h, xc+wc, y+h);
XDrawLine(surf->dpy, surf->drawable, surf->gc, x, yc, x, yc+hc);
XDrawArc(surf->dpy, surf->drawable, surf->gc, xc + wc - r, y,
(unsigned)r*2, (unsigned)r*2, 0 * 64, 90 * 64);
XDrawArc(surf->dpy, surf->drawable, surf->gc, x, y,
(unsigned)r*2, (unsigned)r*2, 90 * 64, 90 * 64);
XDrawArc(surf->dpy, surf->drawable, surf->gc, x, yc + hc - r,
(unsigned)r*2, (unsigned)2*r, 180 * 64, 90 * 64);
XDrawArc(surf->dpy, surf->drawable, surf->gc, xc + wc - r, yc + hc - r,
(unsigned)r*2, (unsigned)2*r, -90 * 64, 90 * 64);}
XSetLineAttributes(surf->dpy, surf->gc, 1, LineSolid, CapButt, JoinMiter);
}
@ -180,53 +180,51 @@ nk_xsurf_fill_rect(XSurface* surf, short x, short y, unsigned short w,
{
unsigned long c = nk_color_from_byte(&col.r);
XSetForeground(surf->dpy, surf->gc, c);
if (r == 0) {
XFillRectangle(surf->dpy, surf->drawable, surf->gc, x, y, w, h);
} else {
short xc = x + r;
short yc = y + r;
short wc = (short)(w - 2 * r);
short hc = (short)(h - 2 * r);
if (r == 0) {XFillRectangle(surf->dpy, surf->drawable, surf->gc, x, y, w, h); return;}
XPoint pnts[12];
pnts[0].x = x;
pnts[0].y = yc;
pnts[1].x = xc;
pnts[1].y = yc;
pnts[2].x = xc;
pnts[2].y = y;
{short xc = x + r;
short yc = y + r;
short wc = (short)(w - 2 * r);
short hc = (short)(h - 2 * r);
pnts[3].x = xc + wc;
pnts[3].y = y;
pnts[4].x = xc + wc;
pnts[4].y = yc;
pnts[5].x = x + w;
pnts[5].y = yc;
XPoint pnts[12];
pnts[0].x = x;
pnts[0].y = yc;
pnts[1].x = xc;
pnts[1].y = yc;
pnts[2].x = xc;
pnts[2].y = y;
pnts[6].x = x + w;
pnts[6].y = yc + hc;
pnts[7].x = xc + wc;
pnts[7].y = yc + hc;
pnts[8].x = xc + wc;
pnts[8].y = y + h;
pnts[3].x = xc + wc;
pnts[3].y = y;
pnts[4].x = xc + wc;
pnts[4].y = yc;
pnts[5].x = x + w;
pnts[5].y = yc;
pnts[9].x = xc;
pnts[9].y = y + h;
pnts[10].x = xc;
pnts[10].y = yc + hc;
pnts[11].x = x;
pnts[11].y = yc + hc;
pnts[6].x = x + w;
pnts[6].y = yc + hc;
pnts[7].x = xc + wc;
pnts[7].y = yc + hc;
pnts[8].x = xc + wc;
pnts[8].y = y + h;
XFillPolygon(surf->dpy, surf->drawable, surf->gc, pnts, 12, Convex, CoordModeOrigin);
XFillArc(surf->dpy, surf->drawable, surf->gc, xc + wc - r, y,
(unsigned)r*2, (unsigned)r*2, 0 * 64, 90 * 64);
XFillArc(surf->dpy, surf->drawable, surf->gc, x, y,
(unsigned)r*2, (unsigned)r*2, 90 * 64, 90 * 64);
XFillArc(surf->dpy, surf->drawable, surf->gc, x, yc + hc - r,
(unsigned)r*2, (unsigned)2*r, 180 * 64, 90 * 64);
XFillArc(surf->dpy, surf->drawable, surf->gc, xc + wc - r, yc + hc - r,
(unsigned)r*2, (unsigned)2*r, -90 * 64, 90 * 64);
}
pnts[9].x = xc;
pnts[9].y = y + h;
pnts[10].x = xc;
pnts[10].y = yc + hc;
pnts[11].x = x;
pnts[11].y = yc + hc;
XFillPolygon(surf->dpy, surf->drawable, surf->gc, pnts, 12, Convex, CoordModeOrigin);
XFillArc(surf->dpy, surf->drawable, surf->gc, xc + wc - r, y,
(unsigned)r*2, (unsigned)r*2, 0 * 64, 90 * 64);
XFillArc(surf->dpy, surf->drawable, surf->gc, x, y,
(unsigned)r*2, (unsigned)r*2, 90 * 64, 90 * 64);
XFillArc(surf->dpy, surf->drawable, surf->gc, x, yc + hc - r,
(unsigned)r*2, (unsigned)2*r, 180 * 64, 90 * 64);
XFillArc(surf->dpy, surf->drawable, surf->gc, xc + wc - r, yc + hc - r,
(unsigned)r*2, (unsigned)2*r, -90 * 64, 90 * 64);}
}
static void
@ -264,7 +262,7 @@ nk_xsurf_fill_polygon(XSurface *surf, const struct nk_vec2i *pnts, int count,
struct nk_color col)
{
int i = 0;
#define MAX_POINTS 64
#define MAX_POINTS 128
XPoint xpnts[MAX_POINTS];
unsigned long c = nk_color_from_byte(&col.r);
XSetForeground(surf->dpy, surf->gc, c);
@ -369,8 +367,7 @@ nk_xsurf_draw_text(XSurface *surf, short x, short y, unsigned short w, unsigned
XSetForeground(surf->dpy, surf->gc, fg);
if(font->set)
XmbDrawString(surf->dpy,surf->drawable,font->set,surf->gc,tx,ty,(const char*)text,(int)len);
else
XDrawString(surf->dpy, surf->drawable, surf->gc, tx, ty, (const char*)text, (int)len);
else XDrawString(surf->dpy, surf->drawable, surf->gc, tx, ty, (const char*)text, (int)len);
}
static void
@ -406,7 +403,6 @@ nk_xfont_create(Display *dpy, const char *name)
fprintf(stderr, "missing fontset: %s\n", missing[n]);
XFreeStringList(missing);
}
if(font->set) {
XFontStruct **xfonts;
char **font_names;
@ -473,10 +469,10 @@ nk_xlib_init(XFont *xfont, Display *dpy, int screen, Window root,
if (!XSupportsLocale()) return 0;
if (!XSetLocaleModifiers("@im=none")) return 0;
xa_clipboard = XInternAtom(dpy, "CLIPBOARD", False);
xa_targets = XInternAtom(dpy, "TARGETS", False);
xa_text = XInternAtom(dpy, "TEXT", False);
xa_utf8_string = XInternAtom(dpy, "UTF8_STRING", False);
xlib.xa_clipboard = XInternAtom(dpy, "CLIPBOARD", False);
xlib.xa_targets = XInternAtom(dpy, "TARGETS", False);
xlib.xa_text = XInternAtom(dpy, "TEXT", False);
xlib.xa_utf8_string = XInternAtom(dpy, "UTF8_STRING", False);
/* create invisible cursor */
{static XColor dummy; char data[1] = {0};
@ -513,31 +509,29 @@ nk_xlib_push_font(XFont *xfont)
NK_API void
nk_xlib_paste(nk_handle handle, struct nk_text_edit* edit)
{
(void)handle; /*Unused*/
/* Paste in X is asynchronous, so can not use a temporary text edit */
NK_ASSERT(edit != &xlib.ctx.text_edit && "Paste not supported for temporary editors");
clipboard_target = edit;
/* Request the contents of the primary buffer */
XConvertSelection(xlib.dpy, XA_PRIMARY, XA_STRING, XA_PRIMARY, xlib.root, CurrentTime);
NK_UNUSED(handle);
/* Paste in X is asynchronous, so can not use a temporary text edit */
NK_ASSERT(edit != &xlib.ctx.text_edit && "Paste not supported for temporary editors");
xlib.clipboard_target = edit;
/* Request the contents of the primary buffer */
XConvertSelection(xlib.dpy, XA_PRIMARY, XA_STRING, XA_PRIMARY, xlib.root, CurrentTime);
}
NK_API void
nk_xlib_copy(nk_handle handle, const char* str, int len)
{
(void)handle; /*Unused*/
free(clipboard_data);
clipboard_len = 0;
clipboard_data = malloc(len);
if (clipboard_data)
{
memcpy(clipboard_data, str, len);
clipboard_len = len;
XSetSelectionOwner(xlib.dpy, XA_PRIMARY, xlib.root, CurrentTime);
XSetSelectionOwner(xlib.dpy, xa_clipboard, xlib.root, CurrentTime);
}
NK_UNUSED(handle);
free(xlib.clipboard_data);
xlib.clipboard_len = 0;
xlib.clipboard_data = malloc((size_t)len);
if (xlib.clipboard_data) {
memcpy(xlib.clipboard_data, str, (size_t)len);
xlib.clipboard_len = len;
XSetSelectionOwner(xlib.dpy, XA_PRIMARY, xlib.root, CurrentTime);
XSetSelectionOwner(xlib.dpy, xlib.xa_clipboard, xlib.root, CurrentTime);
}
}
NK_API int
nk_xlib_handle_event(Display *dpy, int screen, Window win, XEvent *evt)
{
@ -650,82 +644,60 @@ nk_xlib_handle_event(Display *dpy, int screen, Window win, XEvent *evt)
XRefreshKeyboardMapping(&evt->xmapping);
return 1;
} else if (evt->type == SelectionClear) {
free(clipboard_data);
clipboard_data = NULL;
clipboard_len = 0;
return 1;
free(xlib.clipboard_data);
xlib.clipboard_data = NULL;
xlib.clipboard_len = 0;
return 1;
} else if (evt->type == SelectionRequest) {
XEvent reply;
reply.xselection.type = SelectionNotify;
reply.xselection.requestor = evt->xselectionrequest.requestor;
reply.xselection.selection = evt->xselectionrequest.selection;
reply.xselection.target = evt->xselectionrequest.target;
reply.xselection.property = None; /* Default refuse */
reply.xselection.time = evt->xselectionrequest.time;
XEvent reply;
reply.xselection.type = SelectionNotify;
reply.xselection.requestor = evt->xselectionrequest.requestor;
reply.xselection.selection = evt->xselectionrequest.selection;
reply.xselection.target = evt->xselectionrequest.target;
reply.xselection.property = None; /* Default refuse */
reply.xselection.time = evt->xselectionrequest.time;
if (reply.xselection.target == xa_targets)
{
Atom target_list[4];
target_list[0] = xa_targets;
target_list[1] = xa_text;
target_list[2] = xa_utf8_string;
target_list[3] = XA_STRING;
if (reply.xselection.target == xlib.xa_targets) {
Atom target_list[4];
target_list[0] = xlib.xa_targets;
target_list[1] = xlib.xa_text;
target_list[2] = xlib.xa_utf8_string;
target_list[3] = XA_STRING;
reply.xselection.property = evt->xselectionrequest.property;
XChangeProperty(
evt->xselection.display,
evt->xselectionrequest.requestor,
reply.xselection.property,
XA_ATOM, 32, PropModeReplace,
(unsigned char*)&target_list, 4);
}
else if (clipboard_data && (
reply.xselection.target == xa_text ||
reply.xselection.target == xa_utf8_string ||
reply.xselection.target == XA_STRING
))
{
reply.xselection.property = evt->xselectionrequest.property;
XChangeProperty(
evt->xselection.display,
evt->xselectionrequest.requestor,
reply.xselection.property,
reply.xselection.target, 8, PropModeReplace,
(unsigned char*)clipboard_data, clipboard_len);
}
XSendEvent(evt->xselection.display, evt->xselectionrequest.requestor, True, 0, &reply);
XFlush(evt->xselection.display);
return 1;
} else if (evt->type == SelectionNotify && clipboard_target)
{
if ((evt->xselection.target == XA_STRING) ||
(evt->xselection.target == xa_utf8_string) ||
(evt->xselection.target == xa_text))
{
Atom actual_type;
int actual_format;
unsigned long pos = 0, length, remaining;
unsigned char* data = NULL;
do
{
XGetWindowProperty(dpy, win, XA_PRIMARY, pos, 1024, False,
AnyPropertyType, &actual_type, &actual_format, &length, &remaining, &data);
if (length && data)
{
nk_textedit_text(clipboard_target, (char*)data, length);
}
if (data != NULL)
XFree(data);
pos += (length * actual_format) / 32;
reply.xselection.property = evt->xselectionrequest.property;
XChangeProperty(evt->xselection.display,evt->xselectionrequest.requestor,
reply.xselection.property, XA_ATOM, 32, PropModeReplace,
(unsigned char*)&target_list, 4);
} else if (xlib.clipboard_data && (reply.xselection.target == xlib.xa_text ||
reply.xselection.target == xlib.xa_utf8_string || reply.xselection.target == XA_STRING)) {
reply.xselection.property = evt->xselectionrequest.property;
XChangeProperty(evt->xselection.display,evt->xselectionrequest.requestor,
reply.xselection.property, reply.xselection.target, 8, PropModeReplace,
(unsigned char*)xlib.clipboard_data, xlib.clipboard_len);
}
while (remaining != 0);
}
return 1;
}
XSendEvent(evt->xselection.display, evt->xselectionrequest.requestor, True, 0, &reply);
XFlush(evt->xselection.display);
return 1;
} else if (evt->type == SelectionNotify && xlib.clipboard_target) {
if ((evt->xselection.target != XA_STRING) &&
(evt->xselection.target != xlib.xa_utf8_string) &&
(evt->xselection.target != xlib.xa_text))
return 1;
{Atom actual_type;
int actual_format;
unsigned long pos = 0, len, remain;
unsigned char* data = 0;
do {
XGetWindowProperty(dpy, win, XA_PRIMARY, (int)pos, 1024, False,
AnyPropertyType, &actual_type, &actual_format, &len, &remain, &data);
if (len && data)
nk_textedit_text(xlib.clipboard_target, (char*)data, (int)len);
if (data != 0) XFree(data);
pos += (len * (unsigned long)actual_format) / 32;
} while (remain != 0);}
return 1;
}
return 0;
}
@ -815,6 +787,7 @@ nk_xlib_render(Drawable screen, struct nk_color clear)
case NK_COMMAND_IMAGE:
case NK_COMMAND_ARC:
case NK_COMMAND_ARC_FILLED:
case NK_COMMAND_CUSTOM:
default: break;
}
}
@ -822,3 +795,4 @@ nk_xlib_render(Drawable screen, struct nk_color clear)
nk_xsurf_blit(screen, surf, surf->w, surf->h);
}
#endif