// example-label.c // gcc -Wall `pkg-config --cflags --libs gtk+-2.0` -o label-demo example-label.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**); int main (int argc, char** argv) { GtkWidget* mwin; // Main Window GtkWidget* vbox; // gonna pack widgets in this // wake the beast gtk_init (&argc, &argv); // 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); // create a layout box, look this up vbox = gtk_vbox_new (FALSE, 2); // add it to the main window gtk_container_add ((GtkContainer*)mwin, vbox); // make some label widgets GtkWidget* label[3]; 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); // 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); } 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); }