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.     g_message("Ending Program\n");
  15.     gtk_main_quit();
  16. }
  17.  
  18. static void titlechange (GtkWidget *window, GParamSpec *paramspec, gpointer data)
  19. {
  20.     g_message("The Title has changed\n");
  21. }
  22.  
  23. //swaptext basically just asks to see if the window title is already text2
  24. //if it is, it switches text1 and text2, if not, vice versa.
  25. //There is probably a far more graceful way of doing this.
  26. static void swaptext (GtkWidget *window, GdkEvent *event, gpointer data)
  27. {
  28.     if (g_ascii_strcasecmp (gtk_window_get_title(GTK_WINDOW (window)),text2) == 0)
  29.     {
  30.         g_object_set (window, "title", text1, NULL);
  31.         g_object_set (data, "label", text2, NULL);
  32.  
  33.         //gtk_window_set_title (GTK_WINDOW (window), text1);
  34.         //gtk_label_set_text (GTK_LABEL (data), text2);
  35.     }
  36.  
  37.     else
  38.     {
  39.         g_object_set (window, "title", text2, NULL);
  40.         g_object_set (data, "label", text1, NULL);
  41.  
  42.         //gtk_window_set_title (GTK_WINDOW (window), text2);
  43.         //gtk_label_set_text (GTK_LABEL (data), text1);
  44.     }
  45.  
  46.     //g_message("Text was swapped.\n");
  47. }
  48.  
  49. int main (int argc, char *argv[])
  50. {
  51.  
  52.     gtk_init (&argc, &argv);
  53.  
  54.   //our main window and text, respectively
  55.     GtkWidget *window, *label;
  56.  
  57.     window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  58.     //don't forget to use the casting function GTK_WINDOW(window) or you'll get errors
  59.     g_object_set (window, "height-request", 200, "width-request", 500, "title", text2, "resizable", TRUE, NULL);
  60.     //gtk_window_set_default_size (GTK_WINDOW (window), 500, 200);
  61.     //gtk_window_set_title (GTK_WINDOW (window), text2);
  62.     //gtk_window_set_resizable (GTK_WINDOW (window), TRUE);
  63.  
  64.     label = gtk_label_new (text1);
  65.     g_object_set (label, "selectable", TRUE, NULL);
  66.     //gtk_label_set_selectable (GTK_LABEL (label), TRUE);
  67.  
  68.     gtk_container_add (GTK_CONTAINER(window), label);
  69.  
  70.     g_signal_connect (G_OBJECT (window), "destroy", G_CALLBACK (killwindow), NULL);
  71.     g_signal_connect (G_OBJECT (window), "key_press_event", G_CALLBACK (swaptext), label);
  72.     g_signal_connect (G_OBJECT (window), "notify::title", G_CALLBACK(titlechange), NULL);
  73.  
  74.     gtk_widget_show_all (window);
  75.  
  76.     gtk_main ();
  77.     return 0;
  78. }