1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
|
#include <gtk/gtk.h>
#include <stdlib.h>
#include <stdbool.h>
enum
{
COL_PIXBUF = 0,
COL_URI,
NUM_COLS
} ;
static void destroy(GtkWidget *widget ,gpointer pointer) ;
static gboolean delete_event(GtkWidget *widget,GdkEvent *event,gpointer pointer) ;
static void at_exit_handler(void) ;
static GError *error = NULL ;
static GFileInfo *g_file_info = NULL ;
void get_icon_async(GObject *source_object, GAsyncResult *res, gpointer user_data) {
g_file_info = g_file_query_info_finish( (GFile *) source_object, res, &error);
if (error != NULL) {
g_warning("%s\n", error->message) ;
}
return ;
}
static GIcon *get_icon_from_file(const char *uri) {
g_print("uri: %s\n", uri) ;
gchar *filepath = g_filename_from_uri((const gchar *) uri, NULL, NULL) ;
GFile *g_file = g_file_new_for_path(filepath) ;
g_print("= %p\n%s\n", g_file, g_file_get_path(g_file)) ;
GFileInfo *g_file_info = g_file_query_info(g_file, G_FILE_ATTRIBUTE_STANDARD_ICON, G_FILE_QUERY_INFO_NONE, NULL, &error) ;
//g_file_query_info_async(g_file, G_FILE_ATTRIBUTE_STANDARD_ICON, G_FILE_QUERY_INFO_NONE, G_PRIORITY_DEFAULT, NULL, (GAsyncReadyCallback) get_icon_async, NULL);
if (error != NULL) {
g_warning("%s\n", error->message) ;
g_clear_error(&error) ;
}
GIcon *icon = g_file_info_get_icon(g_file_info) ;
return icon ;
}
void drag_data_received(GtkWidget *wgt, GdkDragContext *context, int x, int y, GtkSelectionData *seldata, guint info, guint time, gpointer userdata) {
GtkTreeModel *model;
GtkTreeIter iter;
gchar *uri = (gchar *) gtk_selection_data_get_text(seldata) ; // This function return a guchar.
GIcon *icon = get_icon_from_file(uri) ;
g_print("GIcon: %p\n", icon) ;
GtkWidget *image = gtk_image_new_from_gicon(icon, GTK_ICON_SIZE_DND) ;
model = GTK_TREE_MODEL(userdata);
gtk_list_store_append(GTK_LIST_STORE(model), &iter);
gtk_list_store_set(GTK_LIST_STORE(model), &iter, COL_PIXBUF, gtk_image_get_pixbuf(GTK_IMAGE(image)), COL_URI, uri, -1) ;
gtk_widget_destroy(image) ;
g_free(uri) ;
}
static GtkListStore *create_liststore(void) {
GtkTreeIter iter ;
GdkPixbuf *icon = NULL ;
GtkListStore *store = gtk_list_store_new(2, GDK_TYPE_PIXBUF, G_TYPE_STRING);
icon = gdk_pixbuf_new_from_file("/set/an/image/path/it/will/work", &error);
if (error != NULL) {
/** FIXME: fallback needed ! **/
g_warning ("Could not load icon: %s\n", error->message);
g_clear_error(&error);
}
gtk_list_store_append(store, &iter);
gtk_list_store_set(store, &iter, COL_PIXBUF, icon, COL_URI, "URI", -1);
return store;
}
static GtkWidget *create_treeview(void) {
GtkTreeViewColumn *col;
GtkCellRenderer *renderer;
GtkWidget *view;
GtkListStore *store = create_liststore();
view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
col = gtk_tree_view_column_new();
gtk_tree_view_column_set_title(col, "Title");
renderer = gtk_cell_renderer_pixbuf_new();
gtk_tree_view_column_pack_start(col, renderer, FALSE);
gtk_tree_view_column_set_attributes(col, renderer,
"pixbuf", COL_PIXBUF,
NULL);
renderer = gtk_cell_renderer_text_new();
gtk_tree_view_column_pack_start(col, renderer, TRUE);
gtk_tree_view_column_set_attributes(col, renderer,
"text", COL_URI,
NULL);
gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);
gtk_widget_show_all(view);
/* Make tree view a destination for Drag'n'Drop */
enum
{
TARGET_STRING,
TARGET_URL
};
static GtkTargetEntry targetentries[] =
{
{ "STRING", 0, TARGET_STRING },
{ "text/plain", 0, TARGET_STRING },
{ "text/uri-list", 0, TARGET_URL },
};
gtk_drag_dest_set(view, GTK_DEST_DEFAULT_ALL, targetentries, 3,
GDK_ACTION_COPY|GDK_ACTION_MOVE|GDK_ACTION_LINK);
g_signal_connect(view, "drag_data_received", G_CALLBACK(drag_data_received), store);
return view;
}
int main(int argc, char *argv[]) {
g_set_application_name("Destroyer") ;
const char *app_id = "destroyer.app.mrcyberfighter" ; /** @Hint: For using Gtk notification you will have create a *.desktop file named has the id (prgname.org in this case) and getting a dbus connection. ; **/
if ( ! g_application_id_is_valid(app_id) ) {
fprintf(stderr, "Wrong app id\n") ;
exit(EXIT_FAILURE) ;
}
int app_flags = G_APPLICATION_NON_UNIQUE | G_APPLICATION_SEND_ENVIRONMENT | G_APPLICATION_HANDLES_OPEN ;
GtkApplication *app = gtk_application_new(app_id, app_flags) ;
bool registered = g_application_register(G_APPLICATION(app), NULL, &error) ;
if (error != NULL || (! registered)) {
fprintf(stderr,"Cannot register app: %s\n", error->message) ;
g_clear_error(&error) ;
exit(EXIT_FAILURE) ;
}
GtkWidget *window = gtk_application_window_new(app) ;
g_signal_connect_after(G_OBJECT(window), "delete-event", G_CALLBACK(delete_event), NULL) ;
g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(destroy), NULL) ;
gtk_window_set_default_size(GTK_WINDOW(window), 800, 600);
GtkWidget *scrollable_window = gtk_scrolled_window_new(NULL, NULL);
GtkWidget *treeview = create_treeview();
gtk_container_add(GTK_CONTAINER(scrollable_window), treeview) ;
gtk_container_add(GTK_CONTAINER(window), scrollable_window) ;
gtk_widget_show_all(window) ;
int status = g_application_run(G_APPLICATION(app), argc, argv);
g_object_unref(app) ;
return status ;
}
static void destroy(GtkWidget *widget, gpointer pointer) {
exit(EXIT_SUCCESS) ;
return ;
}
static gboolean delete_event(GtkWidget *widget,GdkEvent *event,gpointer pointer) {
return FALSE ;
} |
Partager