#include <gtk/gtk.h>
#include <string.h>
void hello(GtkApplication *app, gpointer data);
gchar *prompt(GtkWidget *window);
void getname(GtkEntry *entry, GtkWidget *parent) {
g_signal_emit_by_name(parent, "response", 42);
}
int main(int argc, char *argv[])
{
GtkApplication *app;
int status;
app = gtk_application_new("null.localhost.hello", G_APPLICATION_FLAGS_NONE);
g_signal_connect(app, "activate", G_CALLBACK(hello), NULL);
status = g_application_run(G_APPLICATION(app), argc, argv);
g_object_unref(app);
return status;
}
void hello(GtkApplication *app, gpointer data) {
GtkWidget *win_hello;
GtkWidget *label;
gchar *name = NULL;
gchar hello_str[] = "Hello, ";
gchar *greeting;
win_hello = gtk_application_window_new(app);
gtk_window_set_title(GTK_WINDOW(win_hello), "Hello");
gtk_window_set_default_size(GTK_WINDOW(win_hello), 180, 100);
gtk_container_set_border_width(GTK_CONTAINER(win_hello), 10);
name = prompt(win_hello);
if (name == NULL) {
gtk_application_remove_window(app, GTK_WINDOW(win_hello));
} else {
greeting
= g_malloc
(sizeof(gchar
) * (strlen(name
) + 1) + sizeof(hello_str
)); g_strlcpy(greeting, hello_str, sizeof(hello_str));
g_strlcat
(greeting
, name
, strlen(name
) + sizeof(hello_str
) + 1); g_strlcat(greeting, "!", 1);
label = gtk_label_new(greeting);
gtk_container_add(GTK_CONTAINER(win_hello), label);
gtk_widget_show_all(win_hello);
}
}
gchar *prompt(GtkWidget *window) {
GtkWidget *win_prompt;
GtkWidget *content_area;
GtkWidget *label;
GtkWidget *entry;
GtkWidget *box;
GtkDialogFlags flags = GTK_DIALOG_MODAL;
const gchar *in_data;
gchar *buffer;
gint result;
win_prompt = gtk_dialog_new_with_buttons("Prompt", GTK_WINDOW(window), flags, "OK", GTK_RESPONSE_ACCEPT, "Cancel", GTK_RESPONSE_REJECT, NULL);
content_area = gtk_dialog_get_content_area(GTK_DIALOG(win_prompt));
box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 10);
gtk_container_add(GTK_CONTAINER(content_area), box);
label = gtk_label_new("Your name:");
gtk_box_pack_start(GTK_BOX(box), label, TRUE, FALSE, FALSE);
entry = gtk_entry_new();
g_signal_connect(entry, "activate", G_CALLBACK(getname), win_prompt);
gtk_box_pack_start(GTK_BOX(box), entry, TRUE, FALSE, FALSE);
gtk_widget_show_all(win_prompt);
result = gtk_dialog_run(GTK_DIALOG(win_prompt));
switch(result) {
case GTK_RESPONSE_REJECT:
gtk_widget_destroy(win_prompt);
return NULL;
case GTK_RESPONSE_ACCEPT:
default:
in_data = gtk_entry_get_text(GTK_ENTRY(entry));
buffer = g_strdup(in_data);
gtk_widget_destroy(win_prompt);
return buffer;
}
}
I2luY2x1ZGUgPGd0ay9ndGsuaD4KI2luY2x1ZGUgPHN0cmluZy5oPgoKdm9pZCBoZWxsbyhHdGtBcHBsaWNhdGlvbiAqYXBwLCBncG9pbnRlciBkYXRhKTsKZ2NoYXIgKnByb21wdChHdGtXaWRnZXQgKndpbmRvdyk7CnZvaWQgZ2V0bmFtZShHdGtFbnRyeSAqZW50cnksIEd0a1dpZGdldCAqcGFyZW50KSB7CglnX3NpZ25hbF9lbWl0X2J5X25hbWUocGFyZW50LCAicmVzcG9uc2UiLCA0Mik7Cn0KCmludCBtYWluKGludCBhcmdjLCBjaGFyICphcmd2W10pCnsKCUd0a0FwcGxpY2F0aW9uICphcHA7CglpbnQgc3RhdHVzOwoKCWFwcCA9IGd0a19hcHBsaWNhdGlvbl9uZXcoIm51bGwubG9jYWxob3N0LmhlbGxvIiwgR19BUFBMSUNBVElPTl9GTEFHU19OT05FKTsKCWdfc2lnbmFsX2Nvbm5lY3QoYXBwLCAiYWN0aXZhdGUiLCBHX0NBTExCQUNLKGhlbGxvKSwgTlVMTCk7CglzdGF0dXMgPSBnX2FwcGxpY2F0aW9uX3J1bihHX0FQUExJQ0FUSU9OKGFwcCksIGFyZ2MsIGFyZ3YpOwoJZ19vYmplY3RfdW5yZWYoYXBwKTsKCglyZXR1cm4gc3RhdHVzOwp9Cgp2b2lkIGhlbGxvKEd0a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