// example-label2.c // gcc -Wall `pkg-config --cflags --libs gtk+-2.0` -o label2-demo example-label2.c // This is the only header that you need for the basic gtk library #include #include // This is used by the "drag-and-drop" interface #define NDTYPE 1 // Number of "Drop Types" static GtkTargetEntry dtype[NDTYPE] = { // Drop Types { "text/uri-list", 0, 0 } }; // prototype for "drag-and-drop" function void DropFiles(GtkWidget*, GdkDragContext*, int, int, GtkSelectionData*, int, int, GtkLabel**); // prototype for "file selector" function void SelectFile(GtkWidget*, GtkLabel**); // prototype for "about dialog" function void AboutDialog(GtkWidget*); int main (int argc, char** argv) { GtkWidget* mwin; // Main Window GtkWidget* vbox; // gonna pack widgets in this GdkPixbuf* icon; // Hello, this is new! // wake the beast gtk_init (&argc, &argv); // load the Apply icon from the default theme, look this up icon = gtk_icon_theme_load_icon (gtk_icon_theme_get_default(), "gtk-apply", 48, GTK_ICON_LOOKUP_FORCE_SIZE, NULL); // the size "48" will be used in the About Dialog if(icon) { // this sets the icon in the title bar and About Dialog gtk_window_set_default_icon (icon); // we no longer need the pixbuf g_object_unref (icon); } // create the window mwin = gtk_window_new (GTK_WINDOW_TOPLEVEL); // obvious API functions gtk_window_set_title ((GtkWindow*)mwin, "Label-Demo"); gtk_container_set_border_width ((GtkContainer*)mwin, 2); // center on screen gtk_window_set_position ((GtkWindow*)mwin, GTK_WIN_POS_CENTER); GtkWidget *wvbox, *hbox, *bling; // we now add these first wvbox = gtk_vbox_new (FALSE, 2); gtk_container_add ((GtkContainer*)mwin, wvbox); hbox = gtk_hbox_new (FALSE, 0); gtk_box_pack_start ((GtkBox*)wvbox, hbox, TRUE, TRUE, 0); bling = gtk_label_new ("" " Rabid Examples "); gtk_box_pack_start ((GtkBox*)hbox, bling, FALSE, FALSE, 0); gtk_label_set_use_markup ((GtkLabel*)bling, TRUE); gtk_label_set_angle ((GtkLabel*)bling, 90.0); // GtkLabel is derived from GtkMisc gtk_misc_set_padding ((GtkMisc*)bling, 4, 0); // create a layout box, look this up vbox = gtk_vbox_new (FALSE, 2); // the vbox is now packed into the hbox // and will expand to fill the window gtk_box_pack_start ((GtkBox*)hbox, vbox, TRUE, TRUE, 2); // make some label widgets GtkWidget* label[4]; label[0] = gtk_label_new ("Drag A File To This Window"); // pack it into the box, look this up too gtk_box_pack_start ((GtkBox*)vbox, label[0], FALSE, FALSE, 2); label[1] = gtk_label_new (NULL); gtk_box_pack_start ((GtkBox*)vbox, label[1], FALSE, FALSE, 2); label[2] = gtk_label_new (NULL); gtk_box_pack_start ((GtkBox*)vbox, label[2], FALSE, FALSE, 2); // make this one fancy gtk_label_set_selectable ((GtkLabel*)label[2], TRUE); // this example adds an extra label label[3] = gtk_label_new (NULL); gtk_box_pack_start ((GtkBox*)vbox, label[3], FALSE, FALSE, 2); gtk_label_set_selectable ((GtkLabel*)label[3], TRUE); // add some buttons GtkWidget *bbox, *btn; // the buttonbox makes all the child widgets the same size bbox = gtk_hbutton_box_new (); // bbox is packed into wvbox gtk_box_pack_start ((GtkBox*)wvbox, bbox, FALSE, FALSE, 0); // make it pretty gtk_button_box_set_layout ((GtkButtonBox*)bbox, GTK_BUTTONBOX_CENTER); // buttonbox ignores the "gtk_box_pack" parameters // so we just use the container defaults btn = gtk_button_new_with_mnemonic ("_Select"); gtk_container_add ((GtkContainer*)bbox, btn); // call the function when the User clicks the button g_signal_connect (btn, "clicked", G_CALLBACK(SelectFile), label); // bbox has aaumed the reference to btn, so we can reuse the variable name btn = gtk_button_new_with_mnemonic ("_About"); gtk_container_add ((GtkContainer*)bbox, btn); g_signal_connect (btn, "clicked", G_CALLBACK(AboutDialog), NULL); btn = gtk_button_new_with_mnemonic ("_Quit"); gtk_container_add ((GtkContainer*)bbox, btn); g_signal_connect (btn, "clicked", G_CALLBACK(gtk_main_quit), NULL); // allow drag-and-drop on the main window gtk_drag_dest_set (mwin, GTK_DEST_DEFAULT_ALL, dtype, NDTYPE, GDK_ACTION_COPY); // do something with the labels when it happens g_signal_connect (mwin, "drag_data_received", G_CALLBACK(DropFiles), label); // exit main loop when the window is closed g_signal_connect (mwin, "delete-event", G_CALLBACK(gtk_main_quit), NULL); // show all the stuff added to the window gtk_widget_show_all (mwin); gtk_main (); // run the main loop return 0; // it's all good! } void GetInfo(char* sFile, GtkLabel** label) { char *sBase, *sSize, *sDate; struct stat sb; stat(sFile, &sb); // look up info sBase = g_path_get_basename (sFile); gtk_label_set_text (label[0], sBase); g_free (sBase); // this is a useful function from GLib Miscellaneous Utility Functions sSize = g_format_size_full (sb.st_size, G_FORMAT_SIZE_LONG_FORMAT | G_FORMAT_SIZE_IEC_UNITS); gtk_label_set_text (label[1], sSize); g_free (sSize); // show the date in a human-readable format struct tm* lt = localtime(&sb.st_mtime); // add some Pango markup to this one sDate = g_strdup_printf ("Modified: %02i/%02i/%i", lt->tm_mon+1, lt->tm_mday, lt->tm_year-100); gtk_label_set_text (label[2], sDate); gtk_label_set_use_markup ((GtkLabel*)label[2], TRUE); g_free (sDate); char* sPath = g_path_get_dirname (sFile); gtk_label_set_text (label[3], sPath); g_free (sPath); } void DropFiles(GtkWidget* w, GdkDragContext* context, int x, int y, GtkSelectionData* data, int info, int time, GtkLabel** label) { // User dropped files, do something! char **sDrop, *sFile; // get the list of uris dropped on the window sDrop = gtk_selection_data_get_uris (data); // we need the filename of the first file sFile = g_filename_from_uri (sDrop[0], NULL, NULL); // we are done with this stuff g_strfreev (sDrop); gtk_drag_finish (context, TRUE, FALSE, time); GetInfo(sFile, label); g_free (sFile); } void SelectFile(GtkWidget* w, GtkLabel** label) { GtkWidget *fcd, *btn, *img; fcd = gtk_file_chooser_dialog_new ("Select File", NULL, GTK_FILE_CHOOSER_ACTION_OPEN, NULL, NULL); // we prefer not to use STOCK buttons btn = gtk_dialog_add_button ((GtkDialog*)fcd, "_Okay", GTK_RESPONSE_OK); img = gtk_image_new_from_icon_name ("gtk-ok", GTK_ICON_SIZE_BUTTON); gtk_button_set_image ((GtkButton*)btn, img); btn = gtk_dialog_add_button ((GtkDialog*)fcd, "_Cancel", GTK_RESPONSE_CANCEL); img = gtk_image_new_from_icon_name ("gtk-cancel", GTK_ICON_SIZE_BUTTON); gtk_button_set_image ((GtkButton*)btn, img); if(gtk_dialog_run ((GtkDialog*)fcd) == GTK_RESPONSE_OK) { // we're only interested if the User clicks "Okay" char* sFile; sFile = gtk_file_chooser_get_filename ((GtkFileChooser*)fcd); GetInfo(sFile, label); g_free (sFile); } gtk_widget_destroy (fcd); } void AboutDialog(GtkWidget* w) { // look this up char* sURL = "http://kotex.iwarp.com/code"; GtkWidget* about; about = gtk_about_dialog_new (); gtk_about_dialog_set_version ((GtkAboutDialog*)about, "Version 2.0"); gtk_about_dialog_set_website ((GtkAboutDialog*)about, sURL); gtk_about_dialog_set_website_label ((GtkAboutDialog*)about, "Rabid GTK Examples"); gtk_dialog_run ((GtkDialog*)about); gtk_widget_destroy (about); }