Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <gtk/gtk.h>
- //we will be swapping text1 and text2 between the text in the window and the window title
- //I learned that I have to declare these arrays above any function will use them.
- //Is there a "global" variable type? Is that what "extern" is for?
- char text1[50] = "I'm a pretty princess";
- char text2[50] = "The rain in Spain";
- static void killwindow (GtkWidget *window, gpointer data)
- {
- //killwindow is our "exit" callback hooked to the "X"
- g_message("Ending Program\n");
- gtk_main_quit();
- }
- static void titlechange (GtkWidget *window, GParamSpec *paramspec, gpointer data)
- {
- g_message("The Title has changed\n");
- }
- //swaptext basically just asks to see if the window title is already text2
- //if it is, it switches text1 and text2, if not, vice versa.
- //There is probably a far more graceful way of doing this.
- static void swaptext (GtkWidget *window, GdkEvent *event, gpointer data)
- {
- if (g_ascii_strcasecmp (gtk_window_get_title(GTK_WINDOW (window)),text2) == 0)
- {
- g_object_set (window, "title", text1, NULL);
- g_object_set (data, "label", text2, NULL);
- //gtk_window_set_title (GTK_WINDOW (window), text1);
- //gtk_label_set_text (GTK_LABEL (data), text2);
- }
- else
- {
- g_object_set (window, "title", text2, NULL);
- g_object_set (data, "label", text1, NULL);
- //gtk_window_set_title (GTK_WINDOW (window), text2);
- //gtk_label_set_text (GTK_LABEL (data), text1);
- }
- //g_message("Text was swapped.\n");
- }
- int main (int argc, char *argv[])
- {
- gtk_init (&argc, &argv);
- //our main window and text, respectively
- GtkWidget *window, *label;
- window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
- //don't forget to use the casting function GTK_WINDOW(window) or you'll get errors
- g_object_set (window, "height-request", 200, "width-request", 500, "title", text2, "resizable", TRUE, NULL);
- //gtk_window_set_default_size (GTK_WINDOW (window), 500, 200);
- //gtk_window_set_title (GTK_WINDOW (window), text2);
- //gtk_window_set_resizable (GTK_WINDOW (window), TRUE);
- label = gtk_label_new (text1);
- g_object_set (label, "selectable", TRUE, NULL);
- //gtk_label_set_selectable (GTK_LABEL (label), TRUE);
- gtk_container_add (GTK_CONTAINER(window), label);
- g_signal_connect (G_OBJECT (window), "destroy", G_CALLBACK (killwindow), NULL);
- g_signal_connect (G_OBJECT (window), "key_press_event", G_CALLBACK (swaptext), label);
- g_signal_connect (G_OBJECT (window), "notify::title", G_CALLBACK(titlechange), NULL);
- gtk_widget_show_all (window);
- gtk_main ();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement