Skip to content

Commit

Permalink
Change API to use string+len pairs instead of nullterm strings
Browse files Browse the repository at this point in the history
  • Loading branch information
Hejsil committed May 18, 2020
1 parent 20c757d commit ae431f9
Show file tree
Hide file tree
Showing 73 changed files with 2,427 additions and 3,108 deletions.
13 changes: 5 additions & 8 deletions demo/allegro5/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@
* DEMO
*
* ===============================================================*/
static void error_callback(int e, const char *d)
{printf("Error %d: %s\n", e, d);}

int main(void)
{
/* Platform */
Expand Down Expand Up @@ -121,7 +118,7 @@ int main(void)
nk_input_end(ctx);

/* GUI */
if (nk_begin(ctx, "Demo", nk_rect(50, 50, 200, 200),
if (nk_begin(ctx, nk_slicez("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))
{
Expand All @@ -130,13 +127,13 @@ int main(void)
static int property = 20;

nk_layout_row_static(ctx, 30, 80, 1);
if (nk_button_label(ctx, "button"))
if (nk_button_label(ctx, nk_slicez("button")))
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;
if (nk_option_label(ctx, nk_slicez("easy"), op == EASY)) op = EASY;
if (nk_option_label(ctx, nk_slicez("hard"), op == HARD)) op = HARD;
nk_layout_row_dynamic(ctx, 22, 1);
nk_property_int(ctx, "Compression:", 0, &property, 100, 10, 1);
nk_property_int(ctx, nk_slicez("Compression:"), 0, &property, 100, 10, 1);
}
nk_end(ctx);

Expand Down
24 changes: 13 additions & 11 deletions demo/allegro5/nuklear_allegro5.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,19 +92,21 @@ nk_allegro5_font_create_from_file(const char *file_name, int font_size, int flag
}

static float
nk_allegro5_font_get_text_width(nk_handle handle, float height, const char *text, int len)
nk_allegro5_font_get_text_width(nk_handle handle, float height, struct nk_slice text)
{
NkAllegro5Font *font = (NkAllegro5Font*)handle.ptr;
if (!font || !text) {
(void)height;

if (!font || !text.ptr || !text.len) {
return 0;
}
/* We must copy into a new buffer with exact length null-terminated
as nuklear uses variable size buffers and al_get_text_width doesn't
accept a length, it infers length from null-termination
(which is unsafe API design by allegro devs!) */
char strcpy[len+1];
strncpy((char*)&strcpy, text, len);
strcpy[len] = '\0';
char strcpy[text.len+1];
strncpy((char*)&strcpy, text.ptr, text.len);
strcpy[text.len] = '\0';
return al_get_text_width(font->font, strcpy);
}

Expand Down Expand Up @@ -402,21 +404,21 @@ NK_INTERN void
nk_allegro5_clipboard_paste(nk_handle usr, struct nk_text_edit *edit)
{
char *text = al_get_clipboard_text(allegro5.dsp);
if (text) nk_textedit_paste(edit, text, nk_strlen(text));
if (text) nk_textedit_paste(edit, nk_slicez(text));
(void)usr;
al_free(text);
}

NK_INTERN void
nk_allegro5_clipboard_copy(nk_handle usr, const char *text, int len)
nk_allegro5_clipboard_copy(nk_handle usr, struct nk_slice text)
{
char *str = 0;
(void)usr;
if (!len) return;
str = (char*)malloc((size_t)len+1);
if (!text.len) return;
str = (char*)malloc((size_t)text.len+1);
if (!str) return;
memcpy(str, text, (size_t)len);
str[len] = '\0';
memcpy(str, text.ptr, (size_t)text.len);
str[text.len] = '\0';
al_set_clipboard_text(allegro5.dsp, str);
free(str);
}
Expand Down
12 changes: 6 additions & 6 deletions demo/calculator.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
static void
calculator(struct nk_context *ctx)
{
if (nk_begin(ctx, "Calculator", nk_rect(10, 10, 180, 250),
if (nk_begin(ctx, nk_slicez("Calculator"), nk_rect(10, 10, 180, 250),
NK_WINDOW_BORDER|NK_WINDOW_NO_SCROLLBAR|NK_WINDOW_MOVABLE))
{
static int set = 0, prev = 0, op = 0;
Expand All @@ -24,19 +24,19 @@ calculator(struct nk_context *ctx)
for (i = 0; i < 16; ++i) {
if (i >= 12 && i < 15) {
if (i > 12) continue;
if (nk_button_label(ctx, "C")) {
if (nk_button_label(ctx, nk_slicez("C"))) {
a = b = op = 0; current = &a; set = 0;
} if (nk_button_label(ctx, "0")) {
} if (nk_button_label(ctx, nk_slicez("0"))) {
*current = *current*10.0f; set = 0;
} if (nk_button_label(ctx, "=")) {
} if (nk_button_label(ctx, nk_slicez("="))) {
solve = 1; prev = op; op = 0;
}
} else if (((i+1) % 4)) {
if (nk_button_text(ctx, &numbers[(i/4)*3+i%4], 1)) {
if (nk_button_label(ctx, nk_slice(&numbers[(i/4)*3+i%4], 1))) {
*current = *current * 10.0f + numbers[(i/4)*3+i%4] - '0';
set = 0;
}
} else if (nk_button_text(ctx, &ops[i/4], 1)) {
} else if (nk_button_label(ctx, nk_slice(&ops[i/4], 1))) {
if (!set) {
if (current != &b) {
current = &b;
Expand Down
20 changes: 10 additions & 10 deletions demo/d3d11/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ int main(void)
nk_input_end(ctx);

/* GUI */
if (nk_begin(ctx, "Demo", nk_rect(50, 50, 230, 250),
if (nk_begin(ctx, nk_slicez("Demo"), nk_rect(50, 50, 230, 250),
NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|
NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE))
{
Expand All @@ -236,25 +236,25 @@ int main(void)
static int property = 20;

nk_layout_row_static(ctx, 30, 80, 1);
if (nk_button_label(ctx, "button"))
if (nk_button_label(ctx, nk_slicez("button")))
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;
if (nk_option_label(ctx, nk_slicez("easy"), op == EASY)) op = EASY;
if (nk_option_label(ctx, nk_slicez("hard"), op == HARD)) op = HARD;
nk_layout_row_dynamic(ctx, 22, 1);
nk_property_int(ctx, "Compression:", 0, &property, 100, 10, 1);
nk_property_int(ctx, nk_slicez("Compression:"), 0, &property, 100, 10, 1);

nk_layout_row_dynamic(ctx, 20, 1);
nk_label(ctx, "background:", NK_TEXT_LEFT);
nk_label(ctx, nk_slicez("background:"), NK_TEXT_LEFT);
nk_layout_row_dynamic(ctx, 25, 1);
if (nk_combo_begin_color(ctx, nk_rgb_cf(bg), nk_vec2(nk_widget_width(ctx),400))) {
nk_layout_row_dynamic(ctx, 120, 1);
bg = nk_color_picker(ctx, bg, NK_RGBA);
nk_layout_row_dynamic(ctx, 25, 1);
bg.r = nk_propertyf(ctx, "#R:", 0, bg.r, 1.0f, 0.01f,0.005f);
bg.g = nk_propertyf(ctx, "#G:", 0, bg.g, 1.0f, 0.01f,0.005f);
bg.b = nk_propertyf(ctx, "#B:", 0, bg.b, 1.0f, 0.01f,0.005f);
bg.a = nk_propertyf(ctx, "#A:", 0, bg.a, 1.0f, 0.01f,0.005f);
bg.r = nk_propertyf(ctx, nk_slicez("#R:"), 0, bg.r, 1.0f, 0.01f,0.005f);
bg.g = nk_propertyf(ctx, nk_slicez("#G:"), 0, bg.g, 1.0f, 0.01f,0.005f);
bg.b = nk_propertyf(ctx, nk_slicez("#B:"), 0, bg.b, 1.0f, 0.01f,0.005f);
bg.a = nk_propertyf(ctx, nk_slicez("#A:"), 0, bg.a, 1.0f, 0.01f,0.005f);
nk_combo_end(ctx);
}
}
Expand Down
20 changes: 10 additions & 10 deletions demo/d3d9/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ int main(void)
nk_input_end(ctx);

/* GUI */
if (nk_begin(ctx, "Demo", nk_rect(50, 50, 230, 250),
if (nk_begin(ctx, nk_slicez("Demo"), nk_rect(50, 50, 230, 250),
NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|
NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE))
{
Expand All @@ -240,25 +240,25 @@ int main(void)
static int property = 20;

nk_layout_row_static(ctx, 30, 80, 1);
if (nk_button_label(ctx, "button"))
if (nk_button_label(ctx, nk_slicez("button")))
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;
if (nk_option_label(ctx, nk_slicez("easy"), op == EASY)) op = EASY;
if (nk_option_label(ctx, nk_slicez("hard"), op == HARD)) op = HARD;
nk_layout_row_dynamic(ctx, 22, 1);
nk_property_int(ctx, "Compression:", 0, &property, 100, 10, 1);
nk_property_int(ctx, nk_slicez("Compression:"), 0, &property, 100, 10, 1);

nk_layout_row_dynamic(ctx, 20, 1);
nk_label(ctx, "background:", NK_TEXT_LEFT);
nk_label(ctx, nk_slicez("background:"), NK_TEXT_LEFT);
nk_layout_row_dynamic(ctx, 25, 1);
if (nk_combo_begin_color(ctx, nk_rgb_cf(bg), nk_vec2(nk_widget_width(ctx),400))) {
nk_layout_row_dynamic(ctx, 120, 1);
bg = nk_color_picker(ctx, bg, NK_RGBA);
nk_layout_row_dynamic(ctx, 25, 1);
bg.r = nk_propertyf(ctx, "#R:", 0, bg.r, 1.0f, 0.01f,0.005f);
bg.g = nk_propertyf(ctx, "#G:", 0, bg.g, 1.0f, 0.01f,0.005f);
bg.b = nk_propertyf(ctx, "#B:", 0, bg.b, 1.0f, 0.01f,0.005f);
bg.a = nk_propertyf(ctx, "#A:", 0, bg.a, 1.0f, 0.01f,0.005f);
bg.r = nk_propertyf(ctx, nk_slicez("#R:"), 0, bg.r, 1.0f, 0.01f,0.005f);
bg.g = nk_propertyf(ctx, nk_slicez("#G:"), 0, bg.g, 1.0f, 0.01f,0.005f);
bg.b = nk_propertyf(ctx, nk_slicez("#B:"), 0, bg.b, 1.0f, 0.01f,0.005f);
bg.a = nk_propertyf(ctx, nk_slicez("#A:"), 0, bg.a, 1.0f, 0.01f,0.005f);
nk_combo_end(ctx);
}
}
Expand Down
10 changes: 5 additions & 5 deletions demo/gdi/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ int main(void)
nk_input_end(ctx);

/* GUI */
if (nk_begin(ctx, "Demo", nk_rect(50, 50, 200, 200),
if (nk_begin(ctx, nk_slicez("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))
{
Expand All @@ -150,13 +150,13 @@ int main(void)
static int property = 20;

nk_layout_row_static(ctx, 30, 80, 1);
if (nk_button_label(ctx, "button"))
if (nk_button_label(ctx, nk_slicez("button")))
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;
if (nk_option_label(ctx, nk_slicez("easy"), op == EASY)) op = EASY;
if (nk_option_label(ctx, nk_slicez("hard"), op == HARD)) op = HARD;
nk_layout_row_dynamic(ctx, 22, 1);
nk_property_int(ctx, "Compression:", 0, &property, 100, 10, 1);
nk_property_int(ctx, nk_slicez("Compression:"), 0, &property, 100, 10, 1);
}
nk_end(ctx);

Expand Down
10 changes: 5 additions & 5 deletions demo/gdip/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ int main(void)
nk_input_end(ctx);

/* GUI */
if (nk_begin(ctx, "Demo", nk_rect(50, 50, 200, 200),
if (nk_begin(ctx, nk_slicez("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))
{
Expand All @@ -145,13 +145,13 @@ int main(void)
static int property = 20;

nk_layout_row_static(ctx, 30, 80, 1);
if (nk_button_label(ctx, "button"))
if (nk_button_label(ctx, nk_slicez("button")))
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;
if (nk_option_label(ctx, nk_slicez("easy"), op == EASY)) op = EASY;
if (nk_option_label(ctx, nk_slicez("hard"), op == HARD)) op = HARD;
nk_layout_row_dynamic(ctx, 22, 1);
nk_property_int(ctx, "Compression:", 0, &property, 100, 10, 1);
nk_property_int(ctx, nk_slicez("Compression:"), 0, &property, 100, 10, 1);
}
nk_end(ctx);

Expand Down
20 changes: 10 additions & 10 deletions demo/glfw_opengl2/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,35 +117,35 @@ int main(void)
nk_glfw3_new_frame();

/* GUI */
if (nk_begin(ctx, "Demo", nk_rect(50, 50, 230, 250),
if (nk_begin(ctx, nk_slicez("Demo"), nk_rect(50, 50, 230, 250),
NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|
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"))
if (nk_button_label(ctx, nk_slicez("button")))
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;
if (nk_option_label(ctx, nk_slicez("easy"), op == EASY)) op = EASY;
if (nk_option_label(ctx, nk_slicez("hard"), op == HARD)) op = HARD;

nk_layout_row_dynamic(ctx, 25, 1);
nk_property_int(ctx, "Compression:", 0, &property, 100, 10, 1);
nk_property_int(ctx, nk_slicez("Compression:"), 0, &property, 100, 10, 1);

nk_layout_row_dynamic(ctx, 20, 1);
nk_label(ctx, "background:", NK_TEXT_LEFT);
nk_label(ctx, nk_slicez("background:"), NK_TEXT_LEFT);
nk_layout_row_dynamic(ctx, 25, 1);
if (nk_combo_begin_color(ctx, nk_rgb_cf(bg), nk_vec2(nk_widget_width(ctx),400))) {
nk_layout_row_dynamic(ctx, 120, 1);
bg = nk_color_picker(ctx, bg, NK_RGBA);
nk_layout_row_dynamic(ctx, 25, 1);
bg.r = nk_propertyf(ctx, "#R:", 0, bg.r, 1.0f, 0.01f,0.005f);
bg.g = nk_propertyf(ctx, "#G:", 0, bg.g, 1.0f, 0.01f,0.005f);
bg.b = nk_propertyf(ctx, "#B:", 0, bg.b, 1.0f, 0.01f,0.005f);
bg.a = nk_propertyf(ctx, "#A:", 0, bg.a, 1.0f, 0.01f,0.005f);
bg.r = nk_propertyf(ctx, nk_slicez("#R:"), 0, bg.r, 1.0f, 0.01f,0.005f);
bg.g = nk_propertyf(ctx, nk_slicez("#G:"), 0, bg.g, 1.0f, 0.01f,0.005f);
bg.b = nk_propertyf(ctx, nk_slicez("#B:"), 0, bg.b, 1.0f, 0.01f,0.005f);
bg.a = nk_propertyf(ctx, nk_slicez("#A:"), 0, bg.a, 1.0f, 0.01f,0.005f);
nk_combo_end(ctx);
}
}
Expand Down
12 changes: 6 additions & 6 deletions demo/glfw_opengl2/nuklear_glfw_gl2.h
Original file line number Diff line number Diff line change
Expand Up @@ -234,20 +234,20 @@ NK_INTERN void
nk_glfw3_clipboard_paste(nk_handle usr, struct nk_text_edit *edit)
{
const char *text = glfwGetClipboardString(glfw.win);
if (text) nk_textedit_paste(edit, text, nk_strlen(text));
if (text) nk_textedit_paste(edit, nk_slicez(text));
(void)usr;
}

NK_INTERN void
nk_glfw3_clipboard_copy(nk_handle usr, const char *text, int len)
nk_glfw3_clipboard_copy(nk_handle usr, struct nk_slice text)
{
char *str = 0;
(void)usr;
if (!len) return;
str = (char*)malloc((size_t)len+1);
if (!text.len) return;
str = (char*)malloc(text.len+1);
if (!str) return;
memcpy(str, text, (size_t)len);
str[len] = '\0';
memcpy(str, text.ptr, text.len);
str[text.len] = '\0';
glfwSetClipboardString(glfw.win, str);
free(str);
}
Expand Down
20 changes: 10 additions & 10 deletions demo/glfw_opengl3/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,35 +135,35 @@ int main(void)
nk_glfw3_new_frame(&glfw);

/* GUI */
if (nk_begin(ctx, "Demo", nk_rect(50, 50, 230, 250),
if (nk_begin(ctx, nk_slicez("Demo"), nk_rect(50, 50, 230, 250),
NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|
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"))
if (nk_button_label(ctx, nk_slicez("button")))
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;
if (nk_option_label(ctx, nk_slicez("easy"), op == EASY)) op = EASY;
if (nk_option_label(ctx, nk_slicez("hard"), op == HARD)) op = HARD;

nk_layout_row_dynamic(ctx, 25, 1);
nk_property_int(ctx, "Compression:", 0, &property, 100, 10, 1);
nk_property_int(ctx, nk_slicez("Compression:"), 0, &property, 100, 10, 1);

nk_layout_row_dynamic(ctx, 20, 1);
nk_label(ctx, "background:", NK_TEXT_LEFT);
nk_label(ctx, nk_slicez("background:"), NK_TEXT_LEFT);
nk_layout_row_dynamic(ctx, 25, 1);
if (nk_combo_begin_color(ctx, nk_rgb_cf(bg), nk_vec2(nk_widget_width(ctx),400))) {
nk_layout_row_dynamic(ctx, 120, 1);
bg = nk_color_picker(ctx, bg, NK_RGBA);
nk_layout_row_dynamic(ctx, 25, 1);
bg.r = nk_propertyf(ctx, "#R:", 0, bg.r, 1.0f, 0.01f,0.005f);
bg.g = nk_propertyf(ctx, "#G:", 0, bg.g, 1.0f, 0.01f,0.005f);
bg.b = nk_propertyf(ctx, "#B:", 0, bg.b, 1.0f, 0.01f,0.005f);
bg.a = nk_propertyf(ctx, "#A:", 0, bg.a, 1.0f, 0.01f,0.005f);
bg.r = nk_propertyf(ctx, nk_slicez("#R:"), 0, bg.r, 1.0f, 0.01f,0.005f);
bg.g = nk_propertyf(ctx, nk_slicez("#G:"), 0, bg.g, 1.0f, 0.01f,0.005f);
bg.b = nk_propertyf(ctx, nk_slicez("#B:"), 0, bg.b, 1.0f, 0.01f,0.005f);
bg.a = nk_propertyf(ctx, nk_slicez("#A:"), 0, bg.a, 1.0f, 0.01f,0.005f);
nk_combo_end(ctx);
}
}
Expand Down
Loading

0 comments on commit ae431f9

Please sign in to comment.