1

It appears that in GTK you hold a reference to an object, for example like an GtkEntry, but you hold it with a pointer to a GtkWidget. For example

GtkWidget* pointer = gtk_entry_new();

Then when you want to do something like set the text of that entry, you have to call a function and do something with that pointer.

gtk_entry_set_text(GTK_ENTRY(pointer), "hello");

A few questions:

  1. What is the "GTK_ENTRY()" thing? Is it a function call or typecast?
  2. Why is it in all Capital letters, why not do something like (GtkEntry*)pointer?
  3. Why even do this? Why not return a GtkEntry pointer when you create a new Entry?
Faruk
  • 312
  • 3
  • 13
Matthew
  • 3,886
  • 7
  • 47
  • 84

1 Answers1

3

GTK_ENTRY is a macro, behaving like a function. It casts its argument to GtkEntry*, but it may - depending on macros like NDEBUG - do additional check to verify that provided argument may be cast.

It is in all CAPITAL because that is the general convention in C for macros.

The function gtk_entry_new returns GtkWidget* and not GtkEntry* more functions in GTK operates on GtkWidget*, and C language does not provide inheritance so the conversion cannot be automatic.

Wojtek Surowka
  • 20,535
  • 4
  • 44
  • 51
  • What is better to use, a macro rather than a conversion like (GtkEntry*)pointer – Matthew Aug 26 '14 at 20:07
  • 1
    Macro is better because it is safer - it performs the check. Just the cast would perform the conversion blindly and some errors would not be visible immediately. This is how GTK emulates classes and inheritance. – Wojtek Surowka Aug 26 '14 at 20:11
  • How come GTK says it has ancestry but there is no inheritance in C? – Matthew Aug 26 '14 at 22:15
  • 1
    GTK emulates it. More details at http://www.gtk.org/tutorial1.2/gtk_tut-22.html#ss22.2 – Wojtek Surowka Aug 26 '14 at 22:51