Skip to content
Robert J. Lemmens edited this page Jan 5, 2022 · 1 revision

The contextual

contextual

Description

A 'Contextual' is a menu like construct that pops up when right clicking between certain bounds. You can create one with nk_contextual_begin(...):

  struct nk_rect window_bounds = nk_rect(50, 50, 255, 340);
  if (nk_contextual_begin(ctx, NK_PANEL_CONTEXTUAL, nk_vec2(200, 500), window_bounds)) {
    nk_layout_row_dynamic(ctx, 25, 1);
    nk_contextual_item_label(ctx, "New folder", NK_TEXT_CENTERED);
    nk_contextual_item_label(ctx, "New file", NK_TEXT_CENTERED);
    nk_contextual_end(ctx);
  }

It takes the following parameters:

  • a nk_flags
  • a vec2 for the size of the pop up
  • a rectangle that defines the bounds in which you can open the menu

Example

Lets create a window with a context menu that has a couple of buttons one might usually find when right clicking in a file manager:

static void scratchpad(struct nk_context *ctx, struct media *media) {
  nk_style_set_font(ctx, &media->font_20->handle);
  struct nk_rect window_bounds = nk_rect(250, 250, 255, 340);
  nk_begin(ctx, "Nuklear Contextual example", window_bounds, NK_WINDOW_TITLE | NK_WINDOW_MOVABLE);
  if (nk_contextual_begin(ctx, NK_PANEL_CONTEXTUAL, nk_vec2(200, 500), window_bounds)) {
    nk_layout_row_dynamic(ctx, 25, 1);
    nk_contextual_item_label(ctx, "View", NK_TEXT_CENTERED);
    if (nk_contextual_item_label(ctx, "New file", NK_TEXT_CENTERED)) {
      printf("%s", "Creating a new file!\n");
    }
    if (nk_contextual_item_label(ctx, "New folder", NK_TEXT_CENTERED)) {
      printf("%s", "Creating a new folder!\n");
    }
    nk_contextual_item_label(ctx, "More options...", NK_TEXT_CENTERED);
    nk_contextual_end(ctx);
  }
  nk_end(ctx);
}

Results in:

contextual_gif

As you can see the nk_contextual_item_label(...) components act like buttons.

Helpful functions

There are some handy functions which you can use with a contextual:

  • nk_contextual_close(...) closes the contextual menu. Maybe you have some on-hover or event that requires the menu to close, you can use this function to easily close it (try adding it to the example code above, just above nk_contextual_end and see how to window opens en closes immediately).
  • nk_contextual_item_label for text entries in the context menu.
  • nk_contextual_item_image_label, also shows the passed in nk_image
  • nk_contextual_item_symbol_label, also shows a symbol