Skip to content
Robert J. Lemmens edited this page Jan 5, 2022 · 3 revisions

The combobox

Combobox1

Description

A combobox can be used to display a list of selectable items. You can create one with nk_combobox(...):

  static const char *my_options[] = {"Option 1", "Option 2"};   // The options we want to display
  static int selected_item_index = 0;                           // Selected item index
  struct nk_vec2 size = {100, 100};                             // Size of the dropdown that displays all our items
  nk_combobox(ctx, my_options, 2, &selected_item_index, 20, size);

It takes a couple of parameters:

  • a 2d char array with your items
  • item count
  • a reference to an integer, this will be filled with the index of the selected item
  • the height of a single row
  • the size of the dropdown box. If the items dont fit a scrollbar will appear in the dropdown.

Examples:

heres an example of a combobox and a text item that displays the selected item:

static void scratchpad(struct nk_context *ctx) {
  nk_style_set_font(ctx, &media->font_20->handle);
  static const char *my_options[] = {"Option 1", "Option 2", "Option 3", "Option 4"}; // The options we want to display
  static int selected_item_index = 0;    // Selected item index
  struct nk_vec2 size = {100, 60}; // Size of the dropdown combobox
  nk_begin(ctx, "Nuklear Combobox example", nk_rect(50,50, 255, 340), NK_WINDOW_TITLE | NK_WINDOW_MOVABLE);
    nk_layout_row_dynamic(ctx, 20, 1);
    nk_combobox(ctx, my_options, 4, &selected_item_index, 20, size);  // draw the combobox
    char buffer[20];
    sprintf(buffer, "Selected %d", selected_item_index);
    nk_text(ctx, buffer, strlen(buffer), NK_TEXT_LEFT); // display our selected index
  nk_end(ctx);
}

Results in: Combobox

Clone this wiki locally