From cb98ef23a65baee59871ea6761267309baa7a7e0 Mon Sep 17 00:00:00 2001 From: TheBrokenRail Date: Wed, 11 May 2022 21:34:29 -0400 Subject: [PATCH] Add Back --text-info --- CMakeLists.txt | 2 +- src/main.c | 3 + src/option.c | 84 ++++++++++++++++++ src/option.h | 8 +- src/text.c | 228 +++++++++++++++++++++++++++++++++++++++++++++++++ src/zenity.h | 10 ++- src/zenity.ui | 141 ++++++++++++++++++++++++++++-- 7 files changed, 465 insertions(+), 11 deletions(-) create mode 100644 src/text.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 0cee64d..8d8e21f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -33,7 +33,7 @@ add_custom_command( ARGS "--target" "${CMAKE_CURRENT_BINARY_DIR}/.gresource.c" "--generate-source" "--sourcedir" "${CMAKE_CURRENT_SOURCE_DIR}/src" "${CMAKE_CURRENT_SOURCE_DIR}/src/.gresource.xml" DEPENDS "src/.gresource.xml" "src/zenity.ui" ) -add_executable(zenity src/entry.c src/tree.c src/option.c src/util.c src/main.c "${CMAKE_CURRENT_BINARY_DIR}/.gresource.c") +add_executable(zenity src/entry.c src/tree.c src/option.c src/text.c src/util.c src/main.c "${CMAKE_CURRENT_BINARY_DIR}/.gresource.c") target_compile_definitions(zenity PRIVATE "-DG_LOG_DOMAIN=\"Zenity\"") # Install diff --git a/src/main.c b/src/main.c index ea9db42..401ede5 100644 --- a/src/main.c +++ b/src/main.c @@ -54,6 +54,9 @@ main (gint argc, gchar **argv) { results->tree_data->data = (const gchar **) argv + 1; zenity_tree (results->data, results->tree_data); break; + case MODE_TEXTINFO: + zenity_text (results->data, results->text_data); + break; case MODE_LAST: g_printerr ("You must specify a dialog type. See 'zenity " "--help' for details\n"); diff --git a/src/option.c b/src/option.c index 39481f6..9e82da7 100644 --- a/src/option.c +++ b/src/option.c @@ -61,6 +61,10 @@ static gboolean zenity_list_hide_header; static gboolean zenity_list_imagelist; static gboolean zenity_list_mid_search; +/* Text Dialog Options */ +static gboolean zenity_text_active; +static gchar *zenity_text_font; + static GOptionEntry general_options[] = {{"title", '\0', 0, @@ -253,6 +257,43 @@ static GOptionEntry list_options[] = {{"list", NULL}, {NULL}}; +static GOptionEntry text_options[] = {{"text-info", + '\0', + G_OPTION_FLAG_IN_MAIN, + G_OPTION_ARG_NONE, + &zenity_text_active, + "Display text information dialog", + NULL}, + {"text", + '\0', + G_OPTION_FLAG_NOALIAS, + G_OPTION_ARG_STRING, + &zenity_general_dialog_text, + "Set the dialog text", + "TEXT"}, + {"filename", + '\0', + G_OPTION_FLAG_NOALIAS, + G_OPTION_ARG_FILENAME, + &zenity_general_uri, + "Open file", + "FILENAME"}, + {"font", + '\0', + 0, + G_OPTION_ARG_STRING, + &zenity_text_font, + "Set the text font", + "TEXT"}, + {"no-wrap", + '\0', + G_OPTION_FLAG_NOALIAS, + G_OPTION_ARG_NONE, + &zenity_general_dialog_no_wrap, + "Do not enable text wrapping", + NULL}, + {NULL}}; + static ZenityParsingOptions *results; static GOptionContext *ctx; @@ -265,6 +306,7 @@ zenity_option_init (void) { results->mode = MODE_LAST; results->data = g_new0 (ZenityData, 1); results->entry_data = g_new0 (ZenityEntryData, 1); + results->text_data = g_new0 (ZenityTextData, 1); results->tree_data = g_new0 (ZenityTreeData, 1); } @@ -294,6 +336,9 @@ zenity_option_free (void) { if (zenity_list_hide_column) g_free (zenity_list_hide_column); + if (zenity_text_font) + g_free (zenity_text_font); + g_option_context_free (ctx); } @@ -376,6 +421,14 @@ zenity_list_pre_callback (GOptionContext *context, GOptionGroup *group, return TRUE; } +static gboolean +zenity_text_pre_callback (GOptionContext *context, GOptionGroup *group, + gpointer data, GError **error) { + zenity_text_active = FALSE; + zenity_text_font = NULL; + return TRUE; +} + /* Post parse callbacks assign the option values to parsing result and makes some post condition tests */ @@ -498,6 +551,25 @@ zenity_list_post_callback (GOptionContext *context, GOptionGroup *group, return TRUE; } +static gboolean +zenity_text_post_callback (GOptionContext *context, GOptionGroup *group, + gpointer data, GError **error) { + zenity_option_set_dialog_mode (zenity_text_active, MODE_TEXTINFO); + + if (results->mode == MODE_TEXTINFO) { + results->text_data->dialog_text = zenity_general_dialog_text; + results->text_data->uri = zenity_general_uri; + results->text_data->no_wrap = zenity_general_dialog_no_wrap; + results->text_data->font = zenity_text_font; + } else { + if (zenity_text_font) + zenity_option_error ( + zenity_option_get_name (text_options, &zenity_text_font), + ERROR_SUPPORT); + } + return TRUE; +} + static GOptionContext * zenity_create_context (void) { GOptionContext *tmp_ctx; @@ -532,6 +604,18 @@ zenity_create_context (void) { g_option_group_set_error_hook (a_group, zenity_option_error_callback); g_option_context_add_group (tmp_ctx, a_group); + /* Adds text option entries */ + a_group = g_option_group_new ("text-info", + "Text information options", + "Show text information options", + NULL, + NULL); + g_option_group_add_entries (a_group, text_options); + g_option_group_set_parse_hooks ( + a_group, zenity_text_pre_callback, zenity_text_post_callback); + g_option_group_set_error_hook (a_group, zenity_option_error_callback); + g_option_context_add_group (tmp_ctx, a_group); + /* Adds gtk option entries */ a_group = gtk_get_option_group (TRUE); g_option_context_add_group (tmp_ctx, a_group); diff --git a/src/option.h b/src/option.h index 052699d..d7f788b 100644 --- a/src/option.h +++ b/src/option.h @@ -30,7 +30,12 @@ #include #endif -typedef enum { MODE_ENTRY, MODE_LIST, MODE_LAST } ZenityDialogMode; +typedef enum { + MODE_ENTRY, + MODE_LIST, + MODE_TEXTINFO, + MODE_LAST +} ZenityDialogMode; typedef enum { ERROR_SYNTAX, @@ -44,6 +49,7 @@ typedef struct { ZenityData *data; ZenityEntryData *entry_data; + ZenityTextData *text_data; ZenityTreeData *tree_data; } ZenityParsingOptions; diff --git a/src/text.c b/src/text.c new file mode 100644 index 0000000..bde43ad --- /dev/null +++ b/src/text.c @@ -0,0 +1,228 @@ +/* + * text.c + * + * Copyright (C) 2002 Sun Microsystems, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + * + * Authors: Glynn Foster + */ + +#include "util.h" +#include "zenity.h" +#include + +static ZenityTextData *zen_text_data; + +static void zenity_text_dialog_response ( + GtkWidget *widget, int response, gpointer data); + +static gboolean +zenity_text_handle_stdin ( + GIOChannel *channel, GIOCondition condition, gpointer data) { + static GtkTextBuffer *buffer; + static GtkTextView *text_view; + gchar buf[1024]; + + gsize len; + + text_view = GTK_TEXT_VIEW (data); + buffer = gtk_text_view_get_buffer (text_view); + + if ((condition & G_IO_IN) || (condition & (G_IO_IN | G_IO_HUP))) { + GError *error = NULL; + gint status; + + while (channel->is_readable != TRUE) + ; + + do { + status = g_io_channel_read_chars (channel, buf, 1024, &len, &error); + + while (gtk_events_pending ()) + gtk_main_iteration (); + + } while (status == G_IO_STATUS_AGAIN); + + if (status != G_IO_STATUS_NORMAL) { + if (error) { + g_warning ("zenity_text_handle_stdin () : %s", error->message); + g_error_free (error); + error = NULL; + } + return FALSE; + } + + if (len > 0) { + GtkTextIter end; + gchar *utftext; + gsize localelen; + gsize utflen; + + gtk_text_buffer_get_end_iter (buffer, &end); + + if (!g_utf8_validate (buf, len, NULL)) { + utftext = g_convert_with_fallback (buf, + len, + "UTF-8", + "ISO-8859-1", + NULL, + &localelen, + &utflen, + NULL); + gtk_text_buffer_insert (buffer, &end, utftext, utflen); + g_free (utftext); + } else { + gtk_text_buffer_insert (buffer, &end, buf, len); + } + } + } + + return TRUE; +} + +static void +zenity_text_fill_entries_from_stdin (GtkTextView *text_view) { + GIOChannel *channel; + + channel = g_io_channel_unix_new (0); + g_io_channel_set_encoding (channel, "UTF-8", NULL); + g_io_channel_set_flags (channel, G_IO_FLAG_NONBLOCK, NULL); + g_io_add_watch ( + channel, G_IO_IN | G_IO_HUP, zenity_text_handle_stdin, text_view); +} + +void +zenity_text (ZenityData *data, ZenityTextData *text_data) { + GtkBuilder *builder; + GtkWidget *dialog; + GtkWidget *ok_button; + + GObject *text_view; + GtkTextBuffer *text_buffer; + + GObject *text_label; + + zen_text_data = text_data; + builder = + zenity_util_load_ui_file ("zenity_text_dialog", "textbuffer1", NULL); + + if (builder == NULL) { + data->exit_code = zenity_util_return_exit_code (ZENITY_ERROR); + return; + } + + gtk_builder_connect_signals (builder, NULL); + + dialog = + GTK_WIDGET (gtk_builder_get_object (builder, "zenity_text_dialog")); + + ok_button = GTK_WIDGET ( + gtk_builder_get_object (builder, "zenity_text_close_button")); + + g_signal_connect (G_OBJECT (dialog), + "response", + G_CALLBACK (zenity_text_dialog_response), + data); + + if (data->dialog_title) + gtk_window_set_title (GTK_WINDOW (dialog), data->dialog_title); + + gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_CLOSE); + + text_buffer = gtk_text_buffer_new (NULL); + text_view = gtk_builder_get_object (builder, "zenity_text_view"); + gtk_text_view_set_buffer (GTK_TEXT_VIEW (text_view), text_buffer); + + if (text_data->no_wrap) + gtk_text_view_set_wrap_mode (GTK_TEXT_VIEW (text_view), GTK_WRAP_NONE); + + G_GNUC_BEGIN_IGNORE_DEPRECATIONS + if (text_data->font) { + PangoFontDescription *fontDesc = + pango_font_description_from_string (text_data->font); + gtk_widget_override_font (GTK_WIDGET (text_view), fontDesc); + } + G_GNUC_END_IGNORE_DEPRECATIONS + + if (text_data->uri) + zenity_util_fill_file_buffer (text_buffer, text_data->uri); + else + zenity_text_fill_entries_from_stdin (GTK_TEXT_VIEW (text_view)); + + if (data->extra_label) { + gint i = 0; + while (data->extra_label[i] != NULL) { + gtk_dialog_add_button ( + GTK_DIALOG (dialog), data->extra_label[i], i); + i++; + } + } + + if (data->ok_label) { + gtk_button_set_label (GTK_BUTTON (ok_button), data->ok_label); + } + + if (data->width > -1 || data->height > -1) + gtk_window_set_default_size ( + GTK_WINDOW (dialog), data->width, data->height); + else + gtk_window_set_default_size (GTK_WINDOW (dialog), 300, 400); + + if (data->modal) + gtk_window_set_modal (GTK_WINDOW (dialog), TRUE); + + text_label = gtk_builder_get_object (builder, "zenity_text_label"); + + if (text_data->dialog_text) + gtk_label_set_text_with_mnemonic ( + GTK_LABEL (text_label), g_strcompress (text_data->dialog_text)); + + zenity_util_show_dialog (dialog); + + g_object_unref (builder); + + if (data->timeout_delay > 0) { + g_timeout_add_seconds (data->timeout_delay, + (GSourceFunc) zenity_util_timeout_handle, + dialog); + } + + gtk_main (); +} + +static void +zenity_text_dialog_response (GtkWidget *widget, int response, gpointer data) { + ZenityData *zen_data = data; + + switch (response) { + case GTK_RESPONSE_CLOSE: + zen_data->exit_code = zenity_util_return_exit_code (ZENITY_OK); + break; + + case ZENITY_TIMEOUT: + zen_data->exit_code = zenity_util_return_exit_code (ZENITY_TIMEOUT); + break; + + default: + if (zen_data->extra_label && + response < g_strv_length (zen_data->extra_label)) + printf ("%s\n", zen_data->extra_label[response]); + zenity_util_exit_code_with_data (ZENITY_ESC, zen_data); + break; + } + gtk_main_quit (); +} diff --git a/src/zenity.h b/src/zenity.h index b6b824d..6502616 100644 --- a/src/zenity.h +++ b/src/zenity.h @@ -15,7 +15,6 @@ typedef struct { gint exit_code; gint timeout_delay; gboolean modal; - guintptr attach; } ZenityData; typedef enum { @@ -35,6 +34,14 @@ typedef struct { const gchar **data; } ZenityEntryData; +typedef struct { + gchar *dialog_text; + gchar *uri; + gboolean no_wrap; + gchar *font; + GtkTextBuffer *buffer; +} ZenityTextData; + typedef struct { gchar *dialog_text; GSList *columns; @@ -52,6 +59,7 @@ typedef struct { } ZenityTreeData; void zenity_entry (ZenityData *data, ZenityEntryData *entry_data); +void zenity_text (ZenityData *data, ZenityTextData *text_data); void zenity_tree (ZenityData *data, ZenityTreeData *tree_data); G_END_DECLS diff --git a/src/zenity.ui b/src/zenity.ui index 0678983..04e37e5 100644 --- a/src/zenity.ui +++ b/src/zenity.ui @@ -5,7 +5,7 @@ False 5 - Add a new entry + Add a new entry center dialog @@ -22,7 +22,7 @@ end - Cancel + Cancel True True True @@ -36,7 +36,7 @@ - OK + OK True True True @@ -72,7 +72,7 @@ True False - _Enter new text: + _Enter new text: True 0 @@ -109,7 +109,7 @@ False 5 - Select items from the list + Select items from the list center 300 196 @@ -126,7 +126,7 @@ end - Cancel + Cancel True True True @@ -140,7 +140,7 @@ - OK + OK True True True @@ -171,7 +171,7 @@ True False - Select items from the list below. + Select items from the list below. 0 @@ -217,4 +217,129 @@ zenity_tree_ok_button + + + False + 5 + Text View + center + 300 + 200 + dialog + + + + True + False + 2 + + + True + False + end + + + OK + True + True + True + True + right + + + + False + False + 0 + + + + + False + True + end + 1 + + + + + True + False + 6 + + + True + False + vertical + 6 + + + True + False + _Text information: + True + 0 + + + False + False + 0 + + + + + + + + True + True + 0 + + + + + + + True + False + 5 + vertical + + + True + True + etched-in + + + True + True + 2 + 2 + False + word + 2 + 2 + textbuffer1 + + + + + True + True + 0 + + + + + True + True + 2 + + + + + + zenity_text_close_button + +