CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14

Threaded View

  1. #1
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,867

    Trying to get my head around GTK+ casting

    GTK+ contains a widget known as a FileChooserButton. When pressed, it launches a dialog box allowing the user to select a file. After the dialog box closes, the chosen file's name appears in the button and the programmer can obtain a path to the chosen file using gtk_file_chooser_get_filename(). This requires a pointer to GtkFileChooser. Note that GtkFileChooser is not the same thing as GtkFileChooserButton. GtkFileChooser is essentially a collection of helper functions for dealing with the chosen file. Here's the header file for the button:-

    Code:
    #if defined(GTK_DISABLE_SINGLE_INCLUDES) && !defined (__GTK_H_INSIDE__) && !defined (GTK_COMPILATION)
    #error "Only <gtk/gtk.h> can be included directly."
    #endif
    
    #ifndef __GTK_FILE_CHOOSER_BUTTON_H__
    #define __GTK_FILE_CHOOSER_BUTTON_H__
    
    #include <gtk/gtkhbox.h>
    #include <gtk/gtkfilechooser.h>
    
    G_BEGIN_DECLS
    
    #define GTK_TYPE_FILE_CHOOSER_BUTTON            (gtk_file_chooser_button_get_type ())
    #define GTK_FILE_CHOOSER_BUTTON(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_FILE_CHOOSER_BUTTON, GtkFileChooserButton))
    #define GTK_FILE_CHOOSER_BUTTON_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_CHOOSER_BUTTON, GtkFileChooserButtonClass))
    #define GTK_IS_FILE_CHOOSER_BUTTON(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_FILE_CHOOSER_BUTTON))
    #define GTK_IS_FILE_CHOOSER_BUTTON_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_CHOOSER_BUTTON))
    #define GTK_FILE_CHOOSER_BUTTON_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_CHOOSER_BUTTON, GtkFileChooserButtonClass))
    
    typedef struct _GtkFileChooserButton        GtkFileChooserButton;
    typedef struct _GtkFileChooserButtonPrivate GtkFileChooserButtonPrivate;
    typedef struct _GtkFileChooserButtonClass   GtkFileChooserButtonClass;
    
    struct _GtkFileChooserButton
    {
      /*< private >*/
      GtkHBox parent;
    
      GtkFileChooserButtonPrivate *GSEAL (priv);
    };
    
    struct _GtkFileChooserButtonClass
    {
      /*< private >*/
      GtkHBoxClass parent_class;
    
      void (* file_set) (GtkFileChooserButton *fc);
    
      void (*__gtk_reserved1);
      void (*__gtk_reserved2);
      void (*__gtk_reserved3);
      void (*__gtk_reserved4);
      void (*__gtk_reserved5);
      void (*__gtk_reserved6);
      void (*__gtk_reserved7);
    };
    
    
    GType                 gtk_file_chooser_button_get_type         (void) G_GNUC_CONST;
    GtkWidget *           gtk_file_chooser_button_new              (const gchar          *title,
    								GtkFileChooserAction  action);
    
    #ifndef GTK_DISABLE_DEPRECATED
    GtkWidget *           gtk_file_chooser_button_new_with_backend (const gchar          *title,
    								GtkFileChooserAction  action,
    								const gchar          *backend);
    #endif /* GTK_DISABLE_DEPRECATED */
    
    GtkWidget *           gtk_file_chooser_button_new_with_dialog  (GtkWidget            *dialog);
    G_CONST_RETURN gchar *gtk_file_chooser_button_get_title        (GtkFileChooserButton *button);
    void                  gtk_file_chooser_button_set_title        (GtkFileChooserButton *button,
    								const gchar          *title);
    gint                  gtk_file_chooser_button_get_width_chars  (GtkFileChooserButton *button);
    void                  gtk_file_chooser_button_set_width_chars  (GtkFileChooserButton *button,
    								gint                  n_chars);
    gboolean              gtk_file_chooser_button_get_focus_on_click (GtkFileChooserButton *button);
    void                  gtk_file_chooser_button_set_focus_on_click (GtkFileChooserButton *button,
                                                                      gboolean              focus_on_click);
    
    G_END_DECLS
    
    #endif /* !__GTK_FILE_CHOOSER_BUTTON_H__ */
    and here's a cut-down version of the header file for GtkFileChooser (showing function gtk_file_chooser_get_filename() at the end):-

    Code:
    #if defined(GTK_DISABLE_SINGLE_INCLUDES) && !defined (__GTK_H_INSIDE__) && !defined (GTK_COMPILATION)
    #error "Only <gtk/gtk.h> can be included directly."
    #endif
    
    #ifndef __GTK_FILE_CHOOSER_H__
    #define __GTK_FILE_CHOOSER_H__
    
    #include <gtk/gtkfilefilter.h>
    #include <gtk/gtkwidget.h>
    
    G_BEGIN_DECLS
    
    #define GTK_TYPE_FILE_CHOOSER             (gtk_file_chooser_get_type ())
    #define GTK_FILE_CHOOSER(obj)             (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_FILE_CHOOSER, GtkFileChooser))
    #define GTK_IS_FILE_CHOOSER(obj)          (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_FILE_CHOOSER))
    
    typedef struct _GtkFileChooser      GtkFileChooser;
    
    typedef enum
    {
      GTK_FILE_CHOOSER_ACTION_OPEN,
      GTK_FILE_CHOOSER_ACTION_SAVE,
      GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
      GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER
    } GtkFileChooserAction;
    
    typedef enum
    {
      GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM,
      GTK_FILE_CHOOSER_CONFIRMATION_ACCEPT_FILENAME,
      GTK_FILE_CHOOSER_CONFIRMATION_SELECT_AGAIN
    } GtkFileChooserConfirmation;
    
    GType gtk_file_chooser_get_type (void) G_GNUC_CONST;
    
    /* GError enumeration for GtkFileChooser */
    /**
     * GTK_FILE_CHOOSER_ERROR:
     *
     * Used to get the #GError quark for #GtkFileChooser errors.
     */
    #define GTK_FILE_CHOOSER_ERROR (gtk_file_chooser_error_quark ())
    
    typedef enum {
      GTK_FILE_CHOOSER_ERROR_NONEXISTENT,
      GTK_FILE_CHOOSER_ERROR_BAD_FILENAME,
      GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
      GTK_FILE_CHOOSER_ERROR_INCOMPLETE_HOSTNAME
    } GtkFileChooserError;
    
    GQuark gtk_file_chooser_error_quark (void);
    
    /* Filename manipulation */
    gchar *  gtk_file_chooser_get_filename       (GtkFileChooser *chooser);
    
    /* ... and all the other function declarationss */
    
    G_END_DECLS
    
    #endif /* __GTK_FILE_CHOOSER_H__ */
    The programmer creates a button and uses it like this:-

    Code:
    GtkWidget* file_chooser_button = gtk_file_chooser_button_new ("Choose a file", GTK_FILE_CHOOSER_ACTION_OPEN);
    
    // Then - after the user has chosen his file:-
    
    gchar* chosen_file = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER(file_chooser_button));
    Thanks for persevering so far! Here's the bit I don't understand.... GTK_FILE_CHOOSER() effectively casts a GtkFileChooserButton* to a GtkFileChooser*. But looking at the two header files I can't see anything that would allow one object to be cast to the other. And yet it does work! What am I failing to see
    Last edited by John E; May 4th, 2011 at 05:38 AM.
    "A problem well stated is a problem half solved.” - Charles F. Kettering

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured