Add Back --text-info

This commit is contained in:
TheBrokenRail 2022-05-11 21:34:29 -04:00
parent 3dbcdbb34a
commit cb98ef23a6
7 changed files with 465 additions and 11 deletions

View File

@ -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

View File

@ -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");

View File

@ -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);

View File

@ -30,7 +30,12 @@
#include <locale.h>
#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;

228
src/text.c Normal file
View File

@ -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 <glynn.foster@sun.com>
*/
#include "util.h"
#include "zenity.h"
#include <gio/gio.h>
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 ();
}

View File

@ -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

View File

@ -5,7 +5,7 @@
<object class="GtkDialog" id="zenity_entry_dialog">
<property name="can_focus">False</property>
<property name="border_width">5</property>
<property name="title" translatable="yes">Add a new entry</property>
<property name="title">Add a new entry</property>
<property name="window_position">center</property>
<property name="type_hint">dialog</property>
<signal name="destroy" handler="gtk_main_quit" swapped="no"/>
@ -22,7 +22,7 @@
<property name="layout_style">end</property>
<child>
<object class="GtkButton" id="zenity_entry_cancel_button">
<property name="label" translatable="yes">Cancel</property>
<property name="label">Cancel</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="can_default">True</property>
@ -36,7 +36,7 @@
</child>
<child>
<object class="GtkButton" id="zenity_entry_ok_button">
<property name="label" translatable="yes">OK</property>
<property name="label">OK</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="can_default">True</property>
@ -72,7 +72,7 @@
<object class="GtkLabel" id="zenity_entry_text">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">_Enter new text:</property>
<property name="label">_Enter new text:</property>
<property name="use_underline">True</property>
<property name="xalign">0</property>
</object>
@ -109,7 +109,7 @@
<object class="GtkDialog" id="zenity_tree_dialog">
<property name="can_focus">False</property>
<property name="border_width">5</property>
<property name="title" translatable="yes">Select items from the list</property>
<property name="title">Select items from the list</property>
<property name="window_position">center</property>
<property name="default_width">300</property>
<property name="default_height">196</property>
@ -126,7 +126,7 @@
<property name="layout_style">end</property>
<child>
<object class="GtkButton" id="zenity_tree_cancel_button">
<property name="label" translatable="yes">Cancel</property>
<property name="label">Cancel</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="can_default">True</property>
@ -140,7 +140,7 @@
</child>
<child>
<object class="GtkButton" id="zenity_tree_ok_button">
<property name="label" translatable="yes">OK</property>
<property name="label">OK</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="can_default">True</property>
@ -171,7 +171,7 @@
<object class="GtkLabel" id="zenity_tree_text">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Select items from the list below.</property>
<property name="label">Select items from the list below.</property>
<property name="xalign">0</property>
</object>
<packing>
@ -217,4 +217,129 @@
<action-widget response="-5">zenity_tree_ok_button</action-widget>
</action-widgets>
</object>
<object class="GtkTextBuffer" id="textbuffer1"/>
<object class="GtkDialog" id="zenity_text_dialog">
<property name="can_focus">False</property>
<property name="border_width">5</property>
<property name="title">Text View</property>
<property name="window_position">center</property>
<property name="default_width">300</property>
<property name="default_height">200</property>
<property name="type_hint">dialog</property>
<signal name="destroy" handler="gtk_main_quit" swapped="no"/>
<child internal-child="vbox">
<object class="GtkBox" id="dialog-vbox5">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="spacing">2</property>
<child internal-child="action_area">
<object class="GtkButtonBox" id="dialog-action_area5">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="layout_style">end</property>
<child>
<object class="GtkButton" id="zenity_text_close_button">
<property name="label">OK</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="can_default">True</property>
<property name="receives_default">True</property>
<property name="image_position">right</property>
<accelerator key="Return" signal="clicked" modifiers="GDK_CONTROL_MASK"/>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">0</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="pack_type">end</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkBox" id="vbox30">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="border_width">6</property>
<child>
<object class="GtkBox" id="vbox40">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<property name="spacing">6</property>
<child>
<object class="GtkLabel" id="zenity_text_label">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label">_Text information:</property>
<property name="use_underline">True</property>
<property name="xalign">0</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">0</property>
</packing>
</child>
<child>
<placeholder/>
</child>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
</object>
</child>
<child>
<object class="GtkBox" id="vbox5">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="border_width">5</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkScrolledWindow" id="zenity_text_scrolled_window">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="shadow_type">etched-in</property>
<child>
<object class="GtkTextView" id="zenity_text_view">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="pixels_above_lines">2</property>
<property name="pixels_below_lines">2</property>
<property name="editable">False</property>
<property name="wrap_mode">word</property>
<property name="left_margin">2</property>
<property name="right_margin">2</property>
<property name="buffer">textbuffer1</property>
</object>
</child>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">2</property>
</packing>
</child>
</object>
</child>
<action-widgets>
<action-widget response="-7">zenity_text_close_button</action-widget>
</action-widgets>
</object>
</interface>