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

added support for combo from an array of struct #725

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
32 changes: 31 additions & 1 deletion demo/common/overview.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ overview(struct nk_context *ctx)

#ifdef INCLUDE_STYLE
/* styles */
static const char* themes[] = {"Black", "White", "Red", "Blue", "Dark", "Dracula",
static const char* themes[] = {"Black", "White", "Red", "Blue", "Dark", "Dracula",
"Catppucin Latte", "Catppucin Frappe", "Catppucin Macchiato", "Catppucin Mocha"};
static int current_theme = 0;
#endif
Expand Down Expand Up @@ -584,6 +584,36 @@ overview(struct nk_context *ctx)
nk_combo_end(ctx);
}
}
{
/* Combobox from array of struct */
static int sel=0;
struct student {
int id;
char* name;
int age;
char* major;
};
Comment on lines +588 to +595
Copy link
Contributor

Choose a reason for hiding this comment

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

May want to declare the struct outside of the code block.

Copy link
Contributor

Choose a reason for hiding this comment

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

Declare or define? What would be the reasoning g for this? I have definitely seen structure definitions with local scope.

But then again. I don't think that is a c89 capability.


static struct student students[4] = {
{0, "----", 0, "----"},
{1, "Mike", 20, "CS"},
{2, "Jim", 19, "Maths"},
{3, "Julia", 20, "Biology"}
};
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe for static definitions, those may want to be outside as well. In addition, using this format may make it easier to read...

static struct student students[4] = {
  {
    .id = 0,
    .name = "---"
    .age = 0,
    .major = "---"
  },
  ...

In addition, what is the first ---- entry for?

Copy link
Author

@gooosedev gooosedev Dec 8, 2024

Choose a reason for hiding this comment

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

the "----" string it was so there was a blank entry in the list. but i think i'll edit the function to check if the name is NULL, if so it could uses default value.

--- a/src/nuklear_combo.c
+++ b/src/nuklear_combo.c
@@ -1,6 +1,5 @@
 #include "nuklear.h"
 #include "nuklear_internal.h"
-
 /* ==============================================================
  *
  *                          COMBO
@@ -845,6 +844,8 @@ NK_API int nk_combo_from_struct_array(struct nk_context *ctx, const void *items,
         nk_layout_row_dynamic(ctx, (float)item_height, 1);
         for (i = 0; i < count; ++i) {
             name = *(char**)((char*)items + stride);
+            if (!name)
+                name = "";
             if (nk_combo_item_label(ctx, name, NK_TEXT_LEFT))
                 selected = i;
             items = (char*)items + item_sz;

just for clarification, when you say "those may want to be outside as well", you mean moving the array definition outside the overview function or the code block for the combo_from_struct_array example ? I'm not sure i get it correctly.

sel = nk_combo_from_struct_array(ctx, &students, 4,
sizeof(struct student),
offsetof(struct student, name),
sel, 15, nk_vec2(200,200));
if(sel>0){
nk_layout_row_static(ctx, 30,300, 2);
nk_labelf(ctx, NK_TEXT_LEFT, "id:%d | name:%s | age:%d | major:%s",
students[sel].id,
students[sel].name,
students[sel].age,
students[sel].major
);
}
}

nk_tree_pop(ctx);
}
Expand Down
38 changes: 37 additions & 1 deletion nuklear.h
Original file line number Diff line number Diff line change
Expand Up @@ -3765,7 +3765,8 @@ NK_API int nk_combo(struct nk_context*, const char *const *items, int count, int
NK_API int nk_combo_separator(struct nk_context*, const char *items_separated_by_separator, int separator, int selected, int count, int item_height, struct nk_vec2 size);
NK_API int nk_combo_string(struct nk_context*, const char *items_separated_by_zeros, int selected, int count, int item_height, struct nk_vec2 size);
NK_API int nk_combo_callback(struct nk_context*, void(*item_getter)(void*, int, const char**), void *userdata, int selected, int count, int item_height, struct nk_vec2 size);
NK_API void nk_combobox(struct nk_context*, const char *const *items, int count, int *selected, int item_height, struct nk_vec2 size);
NK_API int nk_combo_from_struct_array(struct nk_context *ctx, const void *items, int count, int item_sz, int stride, int selected, int item_height, struct nk_vec2 size);
NK_API void nk_combobox(struct nk_context*, const char **items, int count, int *selected, int item_height, struct nk_vec2 size);
NK_API void nk_combobox_string(struct nk_context*, const char *items_separated_by_zeros, int *selected, int count, int item_height, struct nk_vec2 size);
NK_API void nk_combobox_separator(struct nk_context*, const char *items_separated_by_separator, int separator, int *selected, int count, int item_height, struct nk_vec2 size);
NK_API void nk_combobox_callback(struct nk_context*, void(*item_getter)(void*, int, const char**), void*, int *selected, int count, int item_height, struct nk_vec2 size);
Expand Down Expand Up @@ -30504,6 +30505,41 @@ nk_combo_callback(struct nk_context *ctx, void(*item_getter)(void*, int, const c
nk_combo_end(ctx);
} return selected;
}


NK_API int nk_combo_from_struct_array(struct nk_context *ctx, const void *items, int count, int item_sz, int stride, int selected, int item_height, struct nk_vec2 size)
{
int i = 0;
int max_height;
struct nk_vec2 item_spacing;
struct nk_vec2 window_padding;
char* sel;
char* name;
NK_ASSERT(ctx);
NK_ASSERT(items);
NK_ASSERT(ctx->current);
if (!ctx || !items ||!count)
return selected;

item_spacing = ctx->style.window.spacing;
window_padding = nk_panel_get_padding(&ctx->style, ctx->current->layout->type);
max_height = count * item_height + count * (int)item_spacing.y;
max_height += (int)item_spacing.y * 2 + (int)window_padding.y * 2;
size.y = NK_MIN(size.y, (float)max_height);
sel = *(char**)((char*)items + (item_sz*selected) + stride);
if (nk_combo_begin_label(ctx, sel, size)) {
nk_layout_row_dynamic(ctx, (float)item_height, 1);
for (i = 0; i < count; ++i) {
name = *(char**)((char*)items + stride);
if (nk_combo_item_label(ctx, name, NK_TEXT_LEFT))
selected = i;
items = (char*)items + item_sz;
}
nk_combo_end(ctx);
}
return selected;
}

NK_API void
nk_combobox(struct nk_context *ctx, const char *const *items, int count,
int *selected, int item_height, struct nk_vec2 size)
Expand Down
3 changes: 2 additions & 1 deletion src/nuklear.h
Original file line number Diff line number Diff line change
Expand Up @@ -3543,7 +3543,8 @@ NK_API int nk_combo(struct nk_context*, const char *const *items, int count, int
NK_API int nk_combo_separator(struct nk_context*, const char *items_separated_by_separator, int separator, int selected, int count, int item_height, struct nk_vec2 size);
NK_API int nk_combo_string(struct nk_context*, const char *items_separated_by_zeros, int selected, int count, int item_height, struct nk_vec2 size);
NK_API int nk_combo_callback(struct nk_context*, void(*item_getter)(void*, int, const char**), void *userdata, int selected, int count, int item_height, struct nk_vec2 size);
NK_API void nk_combobox(struct nk_context*, const char *const *items, int count, int *selected, int item_height, struct nk_vec2 size);
NK_API int nk_combo_from_struct_array(struct nk_context *ctx, const void *items, int count, int item_sz, int stride, int selected, int item_height, struct nk_vec2 size);
NK_API void nk_combobox(struct nk_context*, const char **items, int count, int *selected, int item_height, struct nk_vec2 size);
NK_API void nk_combobox_string(struct nk_context*, const char *items_separated_by_zeros, int *selected, int count, int item_height, struct nk_vec2 size);
NK_API void nk_combobox_separator(struct nk_context*, const char *items_separated_by_separator, int separator, int *selected, int count, int item_height, struct nk_vec2 size);
NK_API void nk_combobox_callback(struct nk_context*, void(*item_getter)(void*, int, const char**), void*, int *selected, int count, int item_height, struct nk_vec2 size);
Expand Down
35 changes: 35 additions & 0 deletions src/nuklear_combo.c
Original file line number Diff line number Diff line change
Expand Up @@ -819,6 +819,41 @@ nk_combo_callback(struct nk_context *ctx, void(*item_getter)(void*, int, const c
nk_combo_end(ctx);
} return selected;
}


NK_API int nk_combo_from_struct_array(struct nk_context *ctx, const void *items, int count, int item_sz, int stride, int selected, int item_height, struct nk_vec2 size)
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you add some API documentation? Define each parameter, and give examples where possible. Thanks.

{
int i = 0;
int max_height;
struct nk_vec2 item_spacing;
struct nk_vec2 window_padding;
char* sel;
char* name;
NK_ASSERT(ctx);
NK_ASSERT(items);
NK_ASSERT(ctx->current);
if (!ctx || !items ||!count)
return selected;

item_spacing = ctx->style.window.spacing;
window_padding = nk_panel_get_padding(&ctx->style, ctx->current->layout->type);
max_height = count * item_height + count * (int)item_spacing.y;
max_height += (int)item_spacing.y * 2 + (int)window_padding.y * 2;
size.y = NK_MIN(size.y, (float)max_height);
sel = *(char**)((char*)items + (item_sz*selected) + stride);
if (nk_combo_begin_label(ctx, sel, size)) {
nk_layout_row_dynamic(ctx, (float)item_height, 1);
for (i = 0; i < count; ++i) {
name = *(char**)((char*)items + stride);
if (nk_combo_item_label(ctx, name, NK_TEXT_LEFT))
selected = i;
items = (char*)items + item_sz;
}
nk_combo_end(ctx);
}
return selected;
}

NK_API void
nk_combobox(struct nk_context *ctx, const char *const *items, int count,
int *selected, int item_height, struct nk_vec2 size)
Expand Down
Loading