static void ugtk_openavatarthread(void *UNUSED(args)) { void *dialog = utoxGTK_file_chooser_dialog_new((const char *)S(SELECT_AVATAR_TITLE), NULL, GTK_FILE_CHOOSER_ACTION_OPEN, "_Cancel", GTK_RESPONSE_CANCEL, "_Open", GTK_RESPONSE_ACCEPT, NULL); void *filter = utoxGTK_file_filter_new(); utoxGTK_file_filter_add_mime_type(filter, "image/jpeg"); utoxGTK_file_filter_add_mime_type(filter, "image/png"); utoxGTK_file_filter_add_mime_type(filter, "image/bmp"); utoxGTK_file_filter_add_mime_type(filter, "image/gif"); utoxGTK_file_chooser_set_filter(dialog, filter); void *preview = utoxGTK_image_new(); utoxGTK_file_chooser_set_preview_widget(dialog, preview); utoxGTK_signal_connect_data(dialog, "update-preview", update_image_preview, preview, NULL, 0); while (utoxGTK_dialog_run(dialog) == GTK_RESPONSE_ACCEPT) { char *filename = utoxGTK_file_chooser_get_filename(dialog); int size; int width, height, bpp; uint8_t *img = stbi_load(filename, &width, &height, &bpp, 0); uint8_t *file_data = stbi_write_png_to_mem(img, 0, width, height, bpp, &size); free(img); utoxGTK_free(filename); if (!file_data) { void *message_dialog = utoxGTK_message_dialog_new(dialog, 0, 1, 2, (const char *)S(CANT_FIND_FILE_OR_EMPTY)); utoxGTK_dialog_run(message_dialog); utoxGTK_widget_destroy(message_dialog); } else if (size > UTOX_AVATAR_MAX_DATA_LENGTH) { free(file_data); char size_str[16]; int len = sprint_humanread_bytes(size_str, sizeof(size_str), UTOX_AVATAR_MAX_DATA_LENGTH); char err_str[265] = { 0 }; snprintf((char *)err_str, 265, "%s%.*s (%ikb loaded)", S(AVATAR_TOO_LARGE_MAX_SIZE_IS), len, size_str, (size / 1024)); void *message_dialog = utoxGTK_message_dialog_new(dialog, 0, 1, 2, err_str); utoxGTK_dialog_run(message_dialog); utoxGTK_widget_destroy(message_dialog); } else { postmessage_utox(SELF_AVATAR_SET, size, 0, file_data); break; } } utoxGTK_widget_destroy(dialog); while (utoxGTK_events_pending()) { utoxGTK_main_iteration(); } utoxGTK_open = false; }
static void gtk_openavatarthread(void *UNUSED(args)) { void *dialog = gtk_file_chooser_dialog_new((const char *)S(SELECT_AVATAR_TITLE), NULL, 0, "gtk-cancel", -6, "gtk-open", -3, NULL); void *filter = gtk_file_filter_new(); gtk_file_filter_add_mime_type(filter, "image/png"); gtk_file_chooser_set_filter(dialog, filter); while (gtk_dialog_run(dialog) == -3) { // -3 means user selected an image char *filename = gtk_file_chooser_get_filename(dialog); uint32_t size; void *file_data = file_raw(filename, &size); g_free_utox(filename); if (!file_data) { void *message_dialog = gtk_message_dialog_new(dialog, 0, 1, 2, (const char *)S(CANT_FIND_FILE_OR_EMPTY)); gtk_dialog_run(message_dialog); gtk_widget_destroy(message_dialog); } else if (size > UTOX_AVATAR_MAX_DATA_LENGTH) { free(file_data); char_t size_str[16]; int len = sprint_humanread_bytes(size_str, sizeof(size_str), UTOX_AVATAR_MAX_DATA_LENGTH); void *message_dialog = gtk_message_dialog_new(dialog, 0, 1, 2, "%s%.*s.", S(AVATAR_TOO_LARGE_MAX_SIZE_IS), len, size_str); gtk_dialog_run(message_dialog); gtk_widget_destroy(message_dialog); } else { postmessage(SELF_AVATAR_SET, size, 0, file_data); break; } } gtk_widget_destroy(dialog); while(gtk_events_pending()) { gtk_main_iteration(); } gtk_open = 0; }