IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

GTK+ avec C & C++ Discussion :

treeview, drag-n-drop et pixbuf selon le filetype dragged-in.


Sujet :

GTK+ avec C & C++

  1. #1
    Membre expérimenté
    Avatar de Luke spywoker
    Homme Profil pro
    Etudiant informatique autodidacte
    Inscrit en
    Juin 2010
    Messages
    1 077
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Etudiant informatique autodidacte

    Informations forums :
    Inscription : Juin 2010
    Messages : 1 077
    Points : 1 742
    Points
    1 742
    Par défaut treeview, drag-n-drop et pixbuf selon le filetype dragged-in.
    Bonjours,

    j'ai implémenter un GtkTreeview avec comme model un GtkListstore qui ne comprend que 2 types de colonnes:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    gtk_list_store_new(2, GDK_TYPE_PIXBUF, G_TYPE_STRING);
    Mon problème étant que je n'arrive pas a obtenir la **GIcon** correspondante selon ce que l'on fait glisser (ajouter) au GtkTreeview.

    J'ai essayer de plusieurs manière allant jusqu'a ne récuprer que le mimetype sans succès...

    Mais du code (simplifier) vaut mieux qu'un long discours:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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 ;
    }
    Merci pour vos réponses.
    Pour faire tes armes:
    Use du présent pour construire ton futur sinon use de ce que tu as appris auparavant.
    Et sois toujours bien armé avant de te lancer.
    Le hasard ne sourit qu'aux gens préparés...
    Site: Website programmation international (www.open-source-projects.net)
    Site: Website imagerie 3D (www.3dreaming-imaging.net)
    Testez aux moins pendant une semaine l'éditeur avec terminaux intégrées it-edit Vous l'adopterai sûrement !
    FUN is HARD WORK !!!

  2. #2
    Membre régulier Avatar de Persistant
    Homme Profil pro
    Administrateur de base de données
    Inscrit en
    Septembre 2016
    Messages
    50
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Administrateur de base de données

    Informations forums :
    Inscription : Septembre 2016
    Messages : 50
    Points : 73
    Points
    73
    Par défaut
    Salut,

    Je n'ai pas de solution. Mais je remarque un drôle de comportement sur mon ordi.

    Dans la fonction get_icon_from_file si je met en dur le filepath j'obtiens bien un GIcon
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
      g_print("uri: %s\n", uri) ;// output "file:///home/spywoker/test.txt"
      gchar *filepath = g_filename_from_uri((const gchar *) uri, NULL, NULL) ;
      g_print("filepath: %s\n", filepath) ;// output "/home/spywoker/test.txt"
    GFile *g_file = g_file_new_for_path(filepath) ;// Pas OK
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    GFile *g_file = g_file_new_for_path("/home/spywoker/test.txt") ;// OK

  3. #3
    Membre expérimenté
    Avatar de Luke spywoker
    Homme Profil pro
    Etudiant informatique autodidacte
    Inscrit en
    Juin 2010
    Messages
    1 077
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Etudiant informatique autodidacte

    Informations forums :
    Inscription : Juin 2010
    Messages : 1 077
    Points : 1 742
    Points
    1 742
    Par défaut
    Merci, je vais tester de mettre en dur malgré que cela ne serve a rien car le but est de glisser dedans n'importe quel fichier ou dossier donc dynamiquement.

    Je voulais obtenir l'icône par défaut du fichier ou dossier qui est drag-n-drop dans mon interface grace a GFileInfo.

    Le comportement que j'espèrais était de faire glisser un fichier ou dossier dans mon interface et d'afficher aussi l'icône.

    ----

    Ma lib gio ne déconne pas ; Car j'ai créer un "File Manager" qui emploie la même méthode et qui marche.

    ---

    Je pense que c'est plutôt la faute de mon gestionnaire de drag-n-drop qui est mal implémenter.

    Car j'obtiens les uri et filepath mais pas la GIcon du mimetype associer du fichier ou dossier glisser en dedans.

    Le problème est que:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    g_file_query_info(g_file, G_FILE_ATTRIBUTE_STANDARD_ICON, G_FILE_QUERY_INFO_NONE, NULL, &error) ;
    retourne NULL.

    Et error->message dit que le fichier n'existe pas...???

    Merci encore cordialement pour ta réponse.

    P.S: Quand a l'icone dans le titre du GtkTreeview j'arrive a la mettre car elle est en dur dans le code.
    Pour faire tes armes:
    Use du présent pour construire ton futur sinon use de ce que tu as appris auparavant.
    Et sois toujours bien armé avant de te lancer.
    Le hasard ne sourit qu'aux gens préparés...
    Site: Website programmation international (www.open-source-projects.net)
    Site: Website imagerie 3D (www.3dreaming-imaging.net)
    Testez aux moins pendant une semaine l'éditeur avec terminaux intégrées it-edit Vous l'adopterai sûrement !
    FUN is HARD WORK !!!

  4. #4
    Nouveau membre du Club
    Homme Profil pro
    Inscrit en
    Juin 2004
    Messages
    53
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Hérault (Languedoc Roussillon)

    Informations forums :
    Inscription : Juin 2004
    Messages : 53
    Points : 35
    Points
    35
    Par défaut
    Bonsoir.

    Je ne sai s pas si ça va aider par contre je sais que je risque de devoir y passer par les gtktreeview

    J'ai trouvé ces exemples, si ça peut aider

    https://developer.gnome.org/gtkmm-tu...amples.html.en

    Salutations

  5. #5
    Membre régulier Avatar de Persistant
    Homme Profil pro
    Administrateur de base de données
    Inscrit en
    Septembre 2016
    Messages
    50
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Administrateur de base de données

    Informations forums :
    Inscription : Septembre 2016
    Messages : 50
    Points : 73
    Points
    73
    Par défaut
    Et ici quelques test pour g_file_query_info et voir comment s'en servir.

    Je m'en suis inspiré pour faire un test. Et j'obtiens toute les infos (icon, mimetype, ...) d'un fichier text.
    Code Affichage
    Code C : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    #include <glib-object.h>
    #include <gio/gio.h>
    #include <string.h>
     
     
    static gboolean writable = FALSE;
    static gboolean filesystem = FALSE;
    static char *attributes = NULL;
    static gboolean nofollow_symlinks = FALSE;
     
    #define _(v) v
     
     
    const char *
    file_type_to_string (GFileType type)
    {
      switch (type)
        {
        case G_FILE_TYPE_UNKNOWN:
          return "unknown";
        case G_FILE_TYPE_REGULAR:
          return "regular";
        case G_FILE_TYPE_DIRECTORY:
          return "directory";
        case G_FILE_TYPE_SYMBOLIC_LINK:
          return "symlink";
        case G_FILE_TYPE_SPECIAL:
          return "special";
        case G_FILE_TYPE_SHORTCUT:
          return "shortcut";
        case G_FILE_TYPE_MOUNTABLE:
          return "mountable";
        default:
          return "invalid type";
        }
    }
     
    void
    print_error (const gchar *format, ...)
    {
      gchar *message;
      va_list args;
     
      va_start (args, format);
      message = g_strdup_vprintf (format, args);
      va_end (args);
     
      g_printerr ("gio: %s\n", message);
      g_free (message);
    }
     
    void
    print_file_error (GFile *file, const gchar *message)
    {
      gchar *uri;
     
      uri = g_file_get_uri (file);
      print_error ("%s: %s", uri, message);
      g_free (uri);
    }
     
     
    static char *
    escape_string (const char *in)
    {
      GString *str;
      static char *hex_digits = "0123456789abcdef";
      unsigned char c;
     
     
      str = g_string_new ("");
     
      while ((c = *in++) != 0)
        {
          if (c >= 32 && c <= 126 && c != '\\')
            g_string_append_c (str, c);
          else
            {
              g_string_append (str, "\\x");
              g_string_append_c (str, hex_digits[(c >> 4) & 0xf]);
              g_string_append_c (str, hex_digits[c & 0xf]);
            }
        }
     
      return g_string_free (str, FALSE);
    }
     
    static void
    show_attributes (GFileInfo *info)
    {
      char **attributes;
      char *s;
      int i;
     
      attributes = g_file_info_list_attributes (info, NULL);
     
      g_print (_("attributes:\n"));
      for (i = 0; attributes[i] != NULL; i++)
        {
          /* list the icons in order rather than displaying "GThemedIcon:0x8df7200" */
          if (strcmp (attributes[i], "standard::icon") == 0 ||
              strcmp (attributes[i], "standard::symbolic-icon") == 0)
            {
              GIcon *icon;
              int j;
              const char * const *names = NULL;
     
              if (strcmp (attributes[i], "standard::symbolic-icon") == 0)
                icon = g_file_info_get_symbolic_icon (info);
              else
                icon = g_file_info_get_icon (info);
     
              /* only look up names if GThemedIcon */
              if (G_IS_THEMED_ICON(icon))
                {
                  names = g_themed_icon_get_names (G_THEMED_ICON (icon));
                  g_print ("  %s: ", attributes[i]);
                  for (j = 0; names[j] != NULL; j++)
                    g_print ("%s%s", names[j], (names[j+1] == NULL)?"":", ");
                  g_print ("\n");
                }
              else
                {
                  s = g_file_info_get_attribute_as_string (info, attributes[i]);
                  g_print ("  %s: %s\n", attributes[i], s);
                  g_free (s);
                }
            }
          else
            {
              s = g_file_info_get_attribute_as_string (info, attributes[i]);
              g_print ("  %s: %s\n", attributes[i], s);
              g_free (s);
            }
        }
      g_strfreev (attributes);
    }
     
    static void
    show_info (GFile *file, GFileInfo *info)
    {
      const char *name, *type;
      char *escaped, *uri;
      goffset size;
     
      name = g_file_info_get_display_name (info);
      if (name)
        /* Translators: This is a noun and represents and attribute of a file */
        g_print (_("display name: %s\n"), name);
     
      name = g_file_info_get_edit_name (info);
      if (name)
        /* Translators: This is a noun and represents and attribute of a file */
        g_print (_("edit name: %s\n"), name);
     
      name = g_file_info_get_name (info);
      if (name)
        {
          escaped = escape_string (name);
          g_print (_("name: %s\n"), escaped);
          g_free (escaped);
        }
     
      if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_TYPE))
        {
          type = file_type_to_string (g_file_info_get_file_type (info));
          g_print (_("type: %s\n"), type);
        }
     
      if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
        {
          size = g_file_info_get_size (info);
          g_print (_("size: "));
          g_print (" %"G_GUINT64_FORMAT"\n", (guint64)size);
        }
     
      if (g_file_info_get_is_hidden (info))
        g_print (_("hidden\n"));
     
      uri = g_file_get_uri (file);
      g_print (_("uri: %s\n"), uri);
      g_free (uri);
     
      show_attributes (info);
    }
     
    static gboolean
    query_info (GFile *file)
    {
      GFileQueryInfoFlags flags;
      GFileInfo *info;
      GError *error;
     
      if (file == NULL)
        return FALSE;
     
      if (attributes == NULL)
        attributes = "*";
     
      flags = 0;
      if (nofollow_symlinks)
        flags |= G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS;
     
      error = NULL;
      if (filesystem)
        info = g_file_query_filesystem_info (file, attributes, NULL, &error);
      else
        info = g_file_query_info (file, attributes, flags, NULL, &error);
     
      if (info == NULL)
        {
          print_file_error (file, error->message);
          g_error_free (error);
          return FALSE;
        }
     
      if (filesystem)
        show_attributes (info);
      else
        show_info (file, info);
     
      g_object_unref (info);
     
      return TRUE;
    }
     
     
    int main(int argc, char *argv[]) {
     
      GError *error=NULL;
      GFile *g_file = g_file_new_for_path("/home/spywoker/test.txt") ;
      query_info(g_file);
     
      return 0;
    }
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    display name: test.txt
    edit name: test.txt
    name: test.txt
    type: regular
    size:  14
    uri: file:///home/spywoker/test.txt
    attributes:
      standard::type: 1
      standard::name: test.txt
      standard::display-name: test.txt
      standard::edit-name: test.txt
      standard::copy-name: test.txt
      standard::icon: text-plain, text-x-generic
      standard::content-type: text/plain
      standard::fast-content-type: text/plain
      standard::size: 14
      standard::allocated-size: 4096
      standard::symbolic-icon: text-plain-symbolic, text-x-generic-symbolic, text-plain, text-x-generic
      etag::value: 1503141705:371309
      id::file: l2053:6446136
      id::filesystem: l2053
      access::can-read: TRUE
      access::can-write: TRUE
      access::can-execute: FALSE
      access::can-delete: TRUE
      access::can-trash: TRUE
      access::can-rename: TRUE
      time::modified: 1503141705
      time::modified-usec: 371309
      time::access: 1503141705
      time::access-usec: 371309
      time::changed: 1503141705
      time::changed-usec: 439308
      unix::device: 2053
      unix::inode: 6446136
      unix::mode: 33204
      unix::nlink: 1
      unix::uid: 1000
      unix::gid: 1000
      unix::rdev: 0
      unix::block-size: 4096
      unix::blocks: 8
      owner::user: spywoker
      owner::user-real: spywoker
      owner::group: spywoker
      metadata::gedit-encoding: UTF-8
      metadata::gedit-position: 13
      metadata::nautilus-icon-position: 
      metadata::screen:


    Je n'ai pas trouvé comment enregistrer le GIcon dans un fchier pour vérifier que c'est la bonne image alors j'ai créer un GtkWindow pour l'afficher.
    Code Affichage
    Code C : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    #include <gtk/gtk.h>
     
    static void
    activate (GtkApplication* app,
              gpointer        user_data)
    {
      GtkWidget *window;
     
      window = gtk_application_window_new (app);
      gtk_window_set_title (GTK_WINDOW (window), "Window");
      gtk_window_set_default_size (GTK_WINDOW (window), 200, 200);
     
      GError *error=NULL;
      GFile *g_file = g_file_new_for_path("/home/spywoker/test.txt") ;
      GFileInfo *info = g_file_query_info (g_file,
        G_FILE_ATTRIBUTE_STANDARD_ICON,
        G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
        NULL,
        &error);
      GIcon *icon = g_file_info_get_icon (info);
     
      GtkWidget *image = gtk_image_new_from_gicon(icon, GTK_ICON_SIZE_DND) ;
      gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(image));
     
     
      gtk_widget_show_all (window);
    }
     
    int
    main (int    argc,
          char **argv)
    {
      GtkApplication *app;
      int status;
     
      app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE);
      g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
      status = g_application_run (G_APPLICATION (app), argc, argv);
      g_object_unref (app);
     
      return status;
    }
    Nom : spywoker.png
Affichages : 442
Taille : 76,8 Ko



    Pour compiler je fais un make
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    all: main.c
    	gcc -g main.c -o main `pkg-config --cflags --libs gtk+-3.0`
    A toi d'adapter ;-)

    PS: Si quelqu'un sais comment enregistrer un png depuis un GIcon je suis preneur

  6. #6
    Membre régulier Avatar de Persistant
    Homme Profil pro
    Administrateur de base de données
    Inscrit en
    Septembre 2016
    Messages
    50
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Administrateur de base de données

    Informations forums :
    Inscription : Septembre 2016
    Messages : 50
    Points : 73
    Points
    73
    Par défaut
    J'ai "corrigé deux erreurs.
    • GtkListStore *store = gtk_list_store_new(2, G_TYPE_ICON, G_TYPE_STRING); au lieu de GDK_TYPE_PIXBUF
    • list = g_uri_list_extract_uris((const gchar *)gtk_selection_data_get_data(seldata)); sans quoi l'icon n'est pas trouvé, et un saut de ligne apparait dans la colonne text


    Code C : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    #include <gtk/gtk.h>
    #include <stdlib.h>
    #include <stdbool.h>
     
    enum
    {
      COL_ICON = 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 gchar *filename) {
     
      GFile *g_file = g_file_new_for_path(filename) ;
      GFileInfo *info = g_file_query_info (g_file, G_FILE_ATTRIBUTE_STANDARD_ICON, G_FILE_QUERY_INFO_NONE, NULL, &error);
      GIcon *icon = g_file_info_get_icon (info);
      if (error != NULL) {
        g_warning("%s\n", error->message) ;
        g_clear_error(&error) ;
        return NULL;// or display "break" icon
      }
     
      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 **list;
      gchar *filename;
      gint i = 0;
      list = g_uri_list_extract_uris((const gchar *)gtk_selection_data_get_data(seldata));
      if (list) {
        while (list[i] != NULL) {
          filename = g_filename_from_uri(list[i], NULL, NULL);
     
    GIcon *icon = get_icon_from_file(filename) ;
    model = GTK_TREE_MODEL(userdata);
    gtk_list_store_append(GTK_LIST_STORE(model), &iter);
    gtk_list_store_set(GTK_LIST_STORE(model), &iter, COL_ICON, icon, COL_URI, filename, -1) ;
     
          i++;
        }
        g_strfreev(list);
      }
    }
     
     
    static GtkListStore *create_liststore(void) {
     
         GError    *error   = NULL;
     
        GtkTreeIter    iter           ;
        GIcon     *icon   = NULL  ;
     
        GtkListStore *store = gtk_list_store_new(2, G_TYPE_ICON, G_TYPE_STRING);
     
        GFile *g_file = g_file_new_for_path("/home/spywoker/test.txt") ;
        GFileInfo *info = g_file_query_info (g_file, G_FILE_ATTRIBUTE_STANDARD_ICON, G_FILE_QUERY_INFO_NONE, NULL, &error);
        icon = g_file_info_get_icon (info);
     
        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_ICON, 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();
        g_object_set (G_OBJECT (renderer), "stock-size", GTK_ICON_SIZE_DIALOG/*GTK_ICON_SIZE_LARGE_TOOLBAR*/, NULL);
        gtk_tree_view_column_pack_start(col, renderer, FALSE);
        gtk_tree_view_column_set_attributes(col, renderer,
                                            "gicon", COL_ICON,
                                            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 ;
    }

    Espérant t'avoir aidé...

    PS: La doc semble dire que tu peux mettre un GIcon ou un GdkPixbuf via "icon-name" au lieu de "gicon" mais j'ai pas très bien compris...
    Et, Je ne crois pas que tu puisse faire un gtk_image_get_pixbuf() sur un widget qui n'a pas encore été affiché.

  7. #7
    Membre expérimenté
    Avatar de Luke spywoker
    Homme Profil pro
    Etudiant informatique autodidacte
    Inscrit en
    Juin 2010
    Messages
    1 077
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Etudiant informatique autodidacte

    Informations forums :
    Inscription : Juin 2010
    Messages : 1 077
    Points : 1 742
    Points
    1 742
    Par défaut Grand merci Persistant !!!
    Grand merci Persistant,

    ton code fonctionne a merveille, j'ai pas eu le temps de m'occuper en ce moment car je suis très pris dans une affaires personnelle a régler.

    ---

    Et, Je ne crois pas que tu puisse faire un gtk_image_get_pixbuf() sur un widget qui n'a pas encore été affiché.
    Normal car il faut que l'image soit de type GDK_PIXBUF sinon gtk_image_get_pixbuf() ne marche pas comme dit dans la doc.

    ---

    A tous seigneurs tout honneur Persistant a régler un problème que je n'ai su régler.

    Bravo pour l'effort !!!

    Je te renverrai l'ascenseur un de ces 4.

    Milles fois merci encore Persistant.

    Luke Spywoker.

    P.S: Milieux septembre je pourrai m'occuper car mon affaire personnelle sera régler.
    Pour faire tes armes:
    Use du présent pour construire ton futur sinon use de ce que tu as appris auparavant.
    Et sois toujours bien armé avant de te lancer.
    Le hasard ne sourit qu'aux gens préparés...
    Site: Website programmation international (www.open-source-projects.net)
    Site: Website imagerie 3D (www.3dreaming-imaging.net)
    Testez aux moins pendant une semaine l'éditeur avec terminaux intégrées it-edit Vous l'adopterai sûrement !
    FUN is HARD WORK !!!

  8. #8
    Membre régulier Avatar de Persistant
    Homme Profil pro
    Administrateur de base de données
    Inscrit en
    Septembre 2016
    Messages
    50
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Administrateur de base de données

    Informations forums :
    Inscription : Septembre 2016
    Messages : 50
    Points : 73
    Points
    73
    Par défaut
    Nom : kdo.jpg
Affichages : 406
Taille : 35,1 Ko

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [VB.net] Drag and drop dans une Treeview
    Par gégécap dans le forum Windows Forms
    Réponses: 2
    Dernier message: 19/10/2006, 10h05
  2. Treeview et drag'n'drop
    Par mad0308 dans le forum Delphi
    Réponses: 2
    Dernier message: 26/06/2006, 21h58
  3. Treeview: drag'n'drop à la windows explorer
    Par Gaadek dans le forum Delphi
    Réponses: 2
    Dernier message: 15/06/2006, 11h22
  4. [Débutant(e)][VB.NET] Drag and drop entre 2 treeviews
    Par - Manuella Leray - dans le forum Windows Forms
    Réponses: 8
    Dernier message: 13/10/2005, 15h54
  5. [VB.NET] Microsoft TreeView drag and drop ?
    Par bigtoof dans le forum ASP.NET
    Réponses: 7
    Dernier message: 24/05/2004, 14h50

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo