Nilorea Library gui dictionary search using n_gui widgets.
#define WIDTH 800
#define HEIGHT 800
#define ALLEGRO_UNSTABLE 1
#include <allegro5/allegro_ttf.h>
}
}
(void)widget_id;
}
(void)widget_id;
if (!selected) return;
if (item_text) {
}
}
(void)widget_id;
}
}
char* entry_key = (char*)node->ptr;
}
}
}
char buf[16384] = "";
size_t offset = 0;
int written = snprintf(buf + offset, sizeof(buf) - offset, "%s : %s\n", def->type, def->definition);
if (written > 0) offset += (size_t)written;
if (offset >= sizeof(buf) - 1) break;
}
} else {
}
}
int main(
int argc,
char* argv[]) {
if (!al_init()) {
n_abort(
"Could not init Allegro.\n");
}
if (!al_init_image_addon()) {
n_abort(
"Unable to initialize image addon\n");
}
if (!al_init_primitives_addon()) {
n_abort(
"Unable to initialize primitives addon\n");
}
if (!al_init_font_addon()) {
n_abort(
"Unable to initialize font addon\n");
}
if (!al_init_ttf_addon()) {
n_abort(
"Unable to initialize ttf_font addon\n");
}
if (!al_install_keyboard()) {
n_abort(
"Unable to initialize keyboard handler\n");
}
if (!al_install_mouse()) {
n_abort(
"Unable to initialize mouse handler\n");
}
ALLEGRO_EVENT_QUEUE* event_queue = NULL;
event_queue = al_create_event_queue();
if (!event_queue) {
fprintf(stderr, "failed to create event_queue!\n");
return -1;
}
char ver_str[128] = "";
while ((
getoptret = getopt(argc, argv,
"hvV:L:")) != EOF) {
case 'h':
n_log(
LOG_NOTICE,
"\n %s -h help -v version -V LOG_LEVEL (NOLOG,INFO,NOTICE,ERR,DEBUG) -L OPT_LOG_FILE\n", argv[0]);
exit(TRUE);
case 'v':
sprintf(ver_str, "%s %s", __DATE__, __TIME__);
exit(TRUE);
break;
case 'V':
if (!strncmp("INFO", optarg, 6)) {
} else {
if (!strncmp("NOTICE", optarg, 7)) {
} else {
if (!strncmp("ERR", optarg, 5)) {
} else {
if (!strncmp("DEBUG", optarg, 5)) {
} else {
exit(FALSE);
}
}
}
}
break;
case 'L':
break;
case '?': {
switch (optopt) {
case 'V':
n_log(
LOG_ERR,
"\nPlease specify a log level after -V. \nAvailable values: NOLOG,VERBOSE,NOTICE,ERROR,DEBUG\n\n");
break;
case 'L':
default:
break;
}
}
default:
n_log(
LOG_ERR,
"\n %s -h help -v version -V DEBUGLEVEL (NOLOG,VERBOSE,NOTICE,ERROR,DEBUG) -L logfile\n", argv[0]);
exit(FALSE);
}
}
double fps = 60.0;
double fps_delta_time = 1.0 / fps;
al_set_new_display_flags(ALLEGRO_OPENGL | ALLEGRO_WINDOWED);
n_abort(
"Unable to create display\n");
}
al_set_window_title(
display, argv[0]);
al_set_new_bitmap_flags(ALLEGRO_VIDEO_BITMAP);
enum APP_KEYS {
KEY_ESC
};
al_register_event_source(event_queue, al_get_display_event_source(
display));
al_register_event_source(event_queue, al_get_timer_event_source(
fps_timer));
al_register_event_source(event_queue, al_get_keyboard_event_source());
al_register_event_source(event_queue, al_get_mouse_event_source());
ALLEGRO_FONT* font = NULL;
font = al_load_font("DATAS/2Dumb.ttf", 18, 0);
if (!font) {
n_log(
LOG_ERR,
"Unable to load DATAS/2Dumb.ttf, using builtin font");
font = al_create_builtin_font();
}
int do_draw = 0;
FILE* dict_file = fopen("DATAS/dictionary.csv", "r");
if (!dict_file) {
n_abort(
"Unable to open DATAS/dictionary.csv\n");
}
char read_buf[16384] = "";
char* entry_key = NULL;
char* type = NULL;
char* definition = NULL;
while (fgets(read_buf, 16384, dict_file)) {
entry_def->
type = strdup(type);
} else {
entry->key = strdup(entry_key);
entry_def->
type = strdup(type);
}
}
}
fclose(dict_file);
al_clear_keyboard_state(NULL);
al_flush_event_queue(event_queue);
do {
ALLEGRO_EVENT ev;
al_wait_for_event(event_queue, &ev);
(void)gui_handled;
if (ev.type == ALLEGRO_EVENT_KEY_DOWN) {
switch (ev.keyboard.keycode) {
case ALLEGRO_KEY_ESCAPE:
break;
default:
break;
}
} else if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
} else if (ev.type == ALLEGRO_EVENT_DISPLAY_RESIZE) {
(
float)al_get_display_height(
display));
} else if (ev.type == ALLEGRO_EVENT_DISPLAY_SWITCH_IN || ev.type == ALLEGRO_EVENT_DISPLAY_SWITCH_OUT) {
al_flush_event_queue(event_queue);
}
if (ev.type == ALLEGRO_EVENT_TIMER) {
do_draw = 1;
}
if (do_draw && al_is_event_queue_empty(event_queue)) {
al_set_target_bitmap(al_get_backbuffer(
display));
al_clear_to_color(al_map_rgba(20, 20, 25, 255));
al_flip_display();
do_draw = 0;
}
al_destroy_event_queue(event_queue);
al_destroy_font(font);
al_uninstall_system();
return 0;
}
ALLEGRO_TIMER * fps_timer
ALLEGRO_TIMER * logic_timer
ALLEGRO_DISPLAY * display
char * definition
content of definition
static int search_textarea_id
void free_entry(void *entry_ptr)
LIST * definitions
list of DICTIONARY_DEFINITION for that entry
void on_search_change(int widget_id, const char *text, void *user_data)
static int definition_win_id
void on_clear_click(int widget_id, void *user_data)
void on_completion_select(int widget_id, int index, int selected, void *user_data)
static int status_label_id
static int definition_label_id
char * key
key of the entry
char * type
type of definition (verb, noun,...)
static void update_completion(N_GUI_CTX *gui, const char *text)
static int completion_listbox_id
void free_entry_def(void *entry_def)
static size_t max_results
static void update_definitions(N_GUI_CTX *gui, const char *word)
static HASH_TABLE * dictionary
static char last_search[4096]
dictionary definition for DICTIONARY_ENTRY
#define FreeNoLog(__ptr)
Free Handler without log.
#define Malloc(__ptr, __struct, __size)
Malloc Handler to get errors and set to 0.
#define _str(__PTR)
define true
void n_abort(char const *format,...)
abort program with a text
void n_gui_set_display_size(N_GUI_CTX *ctx, float w, float h)
Set the display (viewport) size for global scrollbar computation.
#define N_GUI_ALIGN_LEFT
left aligned text
void n_gui_textarea_set_text(N_GUI_CTX *ctx, int widget_id, const char *text)
set the text content of a textarea widget
void n_gui_label_set_text(N_GUI_CTX *ctx, int widget_id, const char *text)
set the text of a label widget
int n_gui_add_listbox(N_GUI_CTX *ctx, int window_id, float x, float y, float w, float h, int selection_mode, void(*on_select)(int, int, int, void *), void *user_data)
Add a listbox widget.
int n_gui_process_event(N_GUI_CTX *ctx, ALLEGRO_EVENT event)
Process an allegro event through the GUI system.
int n_gui_add_label(N_GUI_CTX *ctx, int window_id, const char *text, float x, float y, float w, float h, int align)
Add a static text label.
#define N_GUI_WIN_FIXED_POSITION
disable window dragging (default:enable)
int n_gui_listbox_add_item(N_GUI_CTX *ctx, int widget_id, const char *text)
add an item to a listbox widget
void n_gui_set_virtual_size(N_GUI_CTX *ctx, float w, float h)
Set the virtual canvas size for resolution-independent scaling.
#define N_GUI_SELECT_SINGLE
single item selection
void n_gui_draw(N_GUI_CTX *ctx)
Draw all visible windows and their widgets.
int n_gui_add_window(N_GUI_CTX *ctx, const char *title, float x, float y, float w, float h)
Add a new pseudo-window to the context.
N_GUI_CTX * n_gui_new_ctx(ALLEGRO_FONT *default_font)
Create a new GUI context.
const char * n_gui_listbox_get_item_text(N_GUI_CTX *ctx, int widget_id, int index)
get the text of a listbox item
void n_gui_listbox_clear(N_GUI_CTX *ctx, int widget_id)
remove all items from a listbox widget
void n_gui_destroy_ctx(N_GUI_CTX **ctx)
Destroy a GUI context and all its windows/widgets.
#define N_GUI_SHAPE_ROUNDED
rounded rectangle shape
void n_gui_window_set_flags(N_GUI_CTX *ctx, int window_id, int flags)
Set feature flags on a window.
#define N_GUI_ALIGN_JUSTIFIED
justified text (spread words to fill width)
int n_gui_add_textarea(N_GUI_CTX *ctx, int window_id, float x, float y, float w, float h, int multiline, size_t char_limit, void(*on_change)(int, const char *, void *), void *user_data)
Add a text area widget.
int n_gui_add_button(N_GUI_CTX *ctx, int window_id, const char *label, float x, float y, float w, float h, int shape, void(*on_click)(int, void *), void *user_data)
Add a button widget to a window.
The top-level GUI context that holds all windows.
int ht_get_ptr(HASH_TABLE *table, const char *key, void **val)
get pointer at 'key' from 'table'
int destroy_ht(HASH_TABLE **table)
empty a table and destroy it
LIST * ht_get_completion_list(HASH_TABLE *table, const char *keybud, size_t max_results)
get next matching keys in table tree
int ht_put_ptr(HASH_TABLE *table, const char *key, void *ptr, void(*destructor)(void *ptr), void *(*duplicator)(void *ptr))
put an arbitrary pointer value with given key in the targeted hash table
HASH_TABLE * new_ht_trie(size_t alphabet_length, size_t alphabet_offset)
create a TRIE hash table with the alphabet_size, each key value beeing decreased by alphabet_offset
structure of a hash table
int list_push(LIST *list, void *ptr, void(*destructor)(void *ptr))
Add a pointer to the end of the list.
#define list_foreach(__ITEM_, __LIST_)
ForEach macro helper, safe for node removal during iteration.
int list_destroy(LIST **list)
Empty and Free a list container.
LIST * new_generic_list(size_t max_items)
Initialiaze a generic list container to max_items pointers.
#define MAX_LIST_ITEMS
flag to pass to new_generic_list for the maximum possible number of item in a list
Structure of a generic LIST container.
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
#define LOG_DEBUG
debug-level messages
#define LOG_ERR
error conditions
int set_log_file(char *file)
Set the logging to a file instead of stderr.
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
#define LOG_NOTICE
normal but significant condition
#define LOG_INFO
informational
PCRE2_UCHAR8 ** match_list
populated match list (NULL-terminated) after npcre_match_capture() Allocated by PCRE2 via pcre2_subst...
N_PCRE * npcre_new(char *str, int flags)
From pcre doc, the flag bits are: PCRE_ANCHORED Force pattern anchoring PCRE_AUTO_CALLOUT Compile aut...
int npcre_match_capture(char *str, N_PCRE *pcre)
Return TRUE if str matches regexp, and make captures.
int npcre_clean_match(N_PCRE *pcre)
clean the match list of the last capture, if any
int npcre_delete(N_PCRE **pcre)
Free a N_PCRE pointer.
Common headers and low-level functions & define.
GUI system: buttons, sliders, text areas, checkboxes, scrollbars, dropdown menus, windows.
Hash functions and table.
List structures and definitions.
PCRE helpers for regex matching.
N_STR and string function declaration.