Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. #include <stdlib.h>
  2. #include <gtk/gtk.h>
  3.  
  4.  
  5. //we will be swapping text1 and text2 between the text in the window and the window title
  6. //I learned that I have to declare these arrays above any function will use them.
  7. //Is there a "global" variable type?  Is that what "extern" is for?
  8. char text1[50] = "I'm a pretty princess";
  9. char text2[50] = "The rain in Spain";
  10.  
  11. static void killwindow (GtkWidget *window, gpointer data)
  12. {
  13.     //killwindow is our "exit" callback hooked to the "X"
  14.     gtk_main_quit();
  15. }
  16.  
  17. //swaptext basically just asks to see if the window title is already text2
  18. //if it is, it switches text1 and text2, if not, vice versa.
  19. //There is probably a far more graceful way of doing this.
  20. static void swaptext(GtkWidget *window, GdkEvent *event, gpointer data)
  21. {
  22.     if (g_ascii_strcasecmp (gtk_window_get_title(GTK_WINDOW (window)),text2) == 0)
  23.     {
  24.         gtk_window_set_title (GTK_WINDOW (window), text1);
  25.         gtk_label_set_text (GTK_LABEL (data), text2);
  26.     }
  27.  
  28.     else
  29.     {
  30.         gtk_window_set_title (GTK_WINDOW (window), text2);
  31.         gtk_label_set_text (GTK_LABEL (data), text1);
  32.     }
  33. }
  34.  
  35. int main (int argc, char *argv[])
  36. {
  37.  
  38.     gtk_init (&argc, &argv);
  39.  
  40.   //our main window and text, respectively
  41.     GtkWidget *window, *label;
  42.  
  43.     window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  44.     //don't forget to use the casting function GTK_WINDOW(window) or you'll get errors
  45.     gtk_window_set_default_size (GTK_WINDOW (window), 500, 200);
  46.     gtk_window_set_title (GTK_WINDOW (window), text2);
  47.     gtk_window_set_resizable (GTK_WINDOW (window), TRUE);
  48.  
  49.     label = gtk_label_new (text1);
  50.     gtk_label_set_selectable (GTK_LABEL (label), TRUE);
  51.  
  52.     gtk_container_add (GTK_CONTAINER(window), label);
  53.  
  54.     g_signal_connect (G_OBJECT (window), "destroy", G_CALLBACK (killwindow), NULL);
  55.     g_signal_connect (G_OBJECT (window), "key_press_event", G_CALLBACK (swaptext), label);
  56.  
  57.     gtk_widget_show_all (window);
  58.  
  59.     gtk_main ();
  60.     return 0;
  61. }