Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change API to use string+len pairs instead of null terminated strings #81

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions demo/allegro5/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,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),
Copy link
Member

@dumblob dumblob Dec 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't both demos and examples written in C then use the non-slice API (to not scare people off)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes yes. I only update the header so we can discuss the interface before i go on a thousand line journey.

NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|
NK_WINDOW_CLOSABLE|NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE))
{
Expand All @@ -154,13 +154,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
22 changes: 11 additions & 11 deletions demo/allegro5/nuklear_allegro5.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,22 +98,22 @@ NK_API void nk_allegro5_del_image(struct nk_image* image)
}

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)
{
float width;
char *strcpy;
NkAllegro5Font *font = (NkAllegro5Font*)handle.ptr;
NK_UNUSED(height);
if (!font || !text) {
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!) */
strcpy = malloc(len + 1);
strncpy(strcpy, text, len);
strcpy[len] = '\0';
strcpy = malloc(text.len + 1);
strncpy(strcpy, text.ptr, text.len);
strcpy[text.len] = '\0';
width = al_get_text_width(font->font, strcpy);
free(strcpy);
return width;
Expand Down Expand Up @@ -462,21 +462,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 @@ -232,7 +232,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 @@ -241,25 +241,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 @@ -237,7 +237,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 @@ -246,25 +246,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 @@ -146,7 +146,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 @@ -155,13 +155,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 @@ -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
20 changes: 10 additions & 10 deletions demo/glfw_opengl2/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,35 +122,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 @@ -235,20 +235,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 @@ -140,35 +140,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