#include #ifdef __GLASGOW_HASKELL__ //#include "unicode_stub.h" #endif /* __GLASGOW_HASKELL__ */ #include #include #include "config.h" GtkWidget *window; GtkWidget *input; GtkWidget *box; GtkWidget *scwin; GtkWidget *flowbox; GtkEntryBuffer *input_buffer; char *labels; GtkWidget **buttons; bool inited = false; extern char *searchCharacterByName (const char *); void on_click (GtkWidget *widget, gpointer data) { gdk_clipboard_set_text (gtk_widget_get_primary_clipboard (widget), (char *)data); } void on_input (GtkText *text, gpointer user_data) { if (!inited) { scwin = gtk_scrolled_window_new (); flowbox = gtk_flow_box_new (); gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (scwin), flowbox); gtk_box_append (GTK_BOX (box), scwin); } else { flowbox = gtk_flow_box_new (); gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (scwin), flowbox); } inited = true; graphene_rect_t box_bounds; if (gtk_widget_compute_bounds (box, window, &box_bounds)) { graphene_rect_t text_bounds; if (gtk_widget_compute_bounds (GTK_WIDGET (text), window, &text_bounds)) { int height = box_bounds.size.height - text_bounds.size.height - 10; gtk_scrolled_window_set_max_content_height (GTK_SCROLLED_WINDOW (scwin), height); gtk_scrolled_window_set_min_content_height (GTK_SCROLLED_WINDOW (scwin), height); } } gsize buf_size = gtk_entry_buffer_get_bytes (input_buffer); const char *buffer_contents = gtk_entry_buffer_get_text (input_buffer); char *results; results = searchCharacterByName (buffer_contents); size_t l = strlen (results); if (l == 0) { return; } wchar_t symbols[l]; int i = 0; int o = 0; int o_ = 0; mbstowcs (symbols, results, l); if (buttons) { buttons = realloc (buttons, l * sizeof (GtkWidget *)); } else { buttons = malloc (l * sizeof (GtkWidget *)); } memset (buttons, 0, l * sizeof (GtkWidget *)); if (labels) { labels = realloc (labels, 5*l); } else { labels = malloc (5*l); } memset (labels, 0, 5*l); while (symbols[i]) { const wchar_t hold[] = { symbols[i], 0 }; o_ += wcstombs (&labels[o], hold, 4) +1; buttons[i] = gtk_button_new_with_label (&labels[o]); g_signal_connect (buttons[i], "clicked", G_CALLBACK (on_click), &labels[o]); gtk_flow_box_append ((GtkFlowBox *)flowbox, buttons[i]); ++i; o = o_; } } void activate (GtkApplication *app, gpointer user_data) { window = gtk_application_window_new (app); gtk_window_set_title (GTK_WINDOW (window), "pickme"); gtk_window_set_default_size (GTK_WINDOW (window), 200, 200); input_buffer = gtk_entry_buffer_new (NULL, -1); input = gtk_text_new_with_buffer (input_buffer); g_signal_connect (input, "activate", G_CALLBACK (on_input), NULL); box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 10); gtk_box_set_homogeneous (GTK_BOX (box), FALSE); gtk_box_append (GTK_BOX (box), input); gtk_window_set_child (GTK_WINDOW (window), box); gtk_window_present (GTK_WINDOW (window)); } void shutdown (GtkApplication *app, gpointer user_data) { if (!inited) { free (buttons); free (labels); } } int main (int argc, char **argv) { hs_init (&argc, &argv); GtkApplication *app; int res; app = gtk_application_new (APP_ID, G_APPLICATION_DEFAULT_FLAGS); g_signal_connect (app, "activate", G_CALLBACK (activate), NULL); g_signal_connect (app, "shutdown", G_CALLBACK (shutdown), NULL); res = g_application_run (G_APPLICATION (app), argc, argv); g_object_unref (app); return res; }