menu.h (2462B)
1 #ifndef WMENU_MENU_H 2 #define WMENU_MENU_H 3 4 #include <cairo/cairo.h> 5 #include <stdbool.h> 6 #include <sys/types.h> 7 #include <xkbcommon/xkbcommon.h> 8 #include <wayland-client.h> 9 10 struct menu; 11 typedef void (*menu_callback)(struct menu *menu, char *text, bool exit); 12 13 // A menu item. 14 struct item { 15 char *text; 16 int width; 17 struct item *prev_match; // previous matching item 18 struct item *next_match; // next matching item 19 struct page *page; // the page holding this item 20 }; 21 22 // A page of menu items. 23 struct page { 24 struct item *first; // first item in the page 25 struct item *last; // last item in the page 26 struct page *prev; // previous page 27 struct page *next; // next page 28 }; 29 30 // Menu state. 31 struct menu { 32 // Whether the menu appears at the bottom of the screen 33 bool bottom; 34 // The function used to match menu items 35 int (*strncmp)(const char *, const char *, size_t); 36 // Whether the input is a password 37 bool passwd; 38 // The font used to display the menu 39 char *font; 40 // The number of lines to list items vertically 41 int lines; 42 // The name of the output to display on 43 char *output_name; 44 // The prompt displayed to the left of the input field 45 char *prompt; 46 // Normal colors 47 uint32_t normalbg, normalfg; 48 // Prompt colors 49 uint32_t promptbg, promptfg; 50 // Selection colors 51 uint32_t selectionbg, selectionfg; 52 53 struct wl_context *context; 54 55 // 1x1 surface used estimate text sizes with pango 56 cairo_surface_t *test_surface; 57 cairo_t *test_cairo; 58 59 int width; 60 int height; 61 int line_height; 62 int padding; 63 int inputw; 64 int promptw; 65 int left_arrow; 66 int right_arrow; 67 bool rendered; 68 69 char input[BUFSIZ]; 70 size_t cursor; 71 72 struct item *items; // array of all items 73 size_t item_count; 74 struct item *matches; // list of matching items 75 struct item *matches_end; // last matching item 76 struct item *sel; // selected item 77 struct page *pages; // list of pages 78 79 menu_callback callback; 80 bool exit; 81 bool failure; 82 }; 83 84 struct menu *menu_create(menu_callback callback); 85 void menu_destroy(struct menu *menu); 86 void menu_getopts(struct menu *menu, int argc, char *argv[]); 87 void menu_add_item(struct menu *menu, char *text); 88 void menu_sort_and_deduplicate(struct menu *menu); 89 void menu_invalidate(struct menu *menu); 90 void menu_render_items(struct menu *menu); 91 void menu_paste(struct menu *menu, const char *text, ssize_t len); 92 void menu_keypress(struct menu *menu, enum wl_keyboard_key_state key_state, 93 xkb_keysym_t sym); 94 95 #endif