fork download
  1. #include <gtk/gtk.h>
  2. #include <string.h>
  3.  
  4. void hello(GtkApplication *app, gpointer data);
  5. gchar *prompt(GtkWidget *window);
  6. void getname(GtkEntry *entry, GtkWidget *parent) {
  7. g_signal_emit_by_name(parent, "response", 42);
  8. }
  9.  
  10. int main(int argc, char *argv[])
  11. {
  12. GtkApplication *app;
  13. int status;
  14.  
  15. app = gtk_application_new("null.localhost.hello", G_APPLICATION_FLAGS_NONE);
  16. g_signal_connect(app, "activate", G_CALLBACK(hello), NULL);
  17. status = g_application_run(G_APPLICATION(app), argc, argv);
  18. g_object_unref(app);
  19.  
  20. return status;
  21. }
  22.  
  23. void hello(GtkApplication *app, gpointer data) {
  24. GtkWidget *win_hello;
  25. GtkWidget *label;
  26. gchar *name = NULL;
  27. gchar hello_str[] = "Hello, ";
  28. gchar *greeting;
  29.  
  30. win_hello = gtk_application_window_new(app);
  31. gtk_window_set_title(GTK_WINDOW(win_hello), "Hello");
  32. gtk_window_set_default_size(GTK_WINDOW(win_hello), 180, 100);
  33. gtk_container_set_border_width(GTK_CONTAINER(win_hello), 10);
  34.  
  35. name = prompt(win_hello);
  36. if (name == NULL) {
  37. gtk_application_remove_window(app, GTK_WINDOW(win_hello));
  38. } else {
  39. greeting = g_malloc(sizeof(gchar) * (strlen(name) + 1) + sizeof(hello_str));
  40. g_strlcpy(greeting, hello_str, sizeof(hello_str));
  41. g_strlcat(greeting, name, strlen(name) + sizeof(hello_str) + 1);
  42. g_strlcat(greeting, "!", 1);
  43. label = gtk_label_new(greeting);
  44. gtk_container_add(GTK_CONTAINER(win_hello), label);
  45.  
  46. gtk_widget_show_all(win_hello);
  47. }
  48. }
  49.  
  50. gchar *prompt(GtkWidget *window) {
  51. GtkWidget *win_prompt;
  52. GtkWidget *content_area;
  53. GtkWidget *label;
  54. GtkWidget *entry;
  55. GtkWidget *box;
  56. GtkDialogFlags flags = GTK_DIALOG_MODAL;
  57. const gchar *in_data;
  58. gchar *buffer;
  59. gint result;
  60.  
  61. win_prompt = gtk_dialog_new_with_buttons("Prompt", GTK_WINDOW(window), flags, "OK", GTK_RESPONSE_ACCEPT, "Cancel", GTK_RESPONSE_REJECT, NULL);
  62. content_area = gtk_dialog_get_content_area(GTK_DIALOG(win_prompt));
  63.  
  64. box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 10);
  65. gtk_container_add(GTK_CONTAINER(content_area), box);
  66.  
  67. label = gtk_label_new("Your name:");
  68. gtk_box_pack_start(GTK_BOX(box), label, TRUE, FALSE, FALSE);
  69.  
  70. entry = gtk_entry_new();
  71. g_signal_connect(entry, "activate", G_CALLBACK(getname), win_prompt);
  72. gtk_box_pack_start(GTK_BOX(box), entry, TRUE, FALSE, FALSE);
  73.  
  74. gtk_widget_show_all(win_prompt);
  75.  
  76. result = gtk_dialog_run(GTK_DIALOG(win_prompt));
  77.  
  78. switch(result) {
  79. case GTK_RESPONSE_REJECT:
  80. gtk_widget_destroy(win_prompt);
  81. return NULL;
  82. case GTK_RESPONSE_ACCEPT:
  83. default:
  84. in_data = gtk_entry_get_text(GTK_ENTRY(entry));
  85. buffer = g_strdup(in_data);
  86. gtk_widget_destroy(win_prompt);
  87. return buffer;
  88. }
  89. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c:1:21: fatal error: gtk/gtk.h: No such file or directory
compilation terminated.
stdout
Standard output is empty