How to build Gtk thingies in perl

How to define an enumerate of Gtk in perl

Let's start from the eariest one. An enumerate is nothing but an array of constants in C. It's usually just an integer in C. In Perl-Gtk, the enumerate is mapped to a string instead of an integer. By this way, we can use 'name' instead of the magic integer number when we use enumerate in perl. The gluing machenism will translate the 'name' into integer values for you transparently. However, you still can use integer number directly for performance.

For example, when you want to use gtk_tree_set_view_mode() in perl. You can render the second arguments by using ways.

Example 1. Using enumerate


Internally, this mapping from perl to C is implemented by a hash table and the reverse mapping is done by using the get_type_enum_get_vals.

In the latter case, get_type_enum_get_vals will return a list of (value,name,nick_name) tuples. We travel these tuples until we found a match. By this way, the mapping between 'name' and integer value is supplied by gtk itself. We don't need to build such table inside perl. It means that we can use a generic routine to di it. This routine, named as newSvDefEnumHash is implemented in Gtk/MiscTypes.

In the former case,SvDefEnumHash, which defined in Gtk/MiscTypes, is used to translate 'name' into integer values. In addition, this function will try to interpret the content of perl variables as integer if it can not find a match in the list returned by gtk_type_enum_get_vals. It means that you can use


It's equivalent to the previous example. This function can check if the value '0' is valid for this enumerate also.

Both SvDefEnumHash and newSvDefEnumhash will not be used directly. It is called through the 'typemap'. In Perl-Gtk, the typemap is usually generated from DEFL definitions of enumrate. The syntax is like,

Example 2. Example of enumerate in DEFL

(define-enum GdkEventType
  (nothing GDK_NOTHING)
  (delete GDK_DELETE)
  (destroy GDK_DESTROY)
  (expose GDK_EXPOSE)
  (motion-notify GDK_MOTION_NOTIFY)
  (button-press GDK_BUTTON_PRESS)
  (2button-press GDK_2BUTTON_PRESS)
  (3button-press GDK_3BUTTON_PRESS)
  (button-release GDK_BUTTON_RELEASE)
  (key-press GDK_KEY_PRESS)
  (key-release GDK_KEY_RELEASE)
  (enter-notify GDK_ENTER_NOTIFY)
  (leave-notify GDK_LEAVE_NOTIFY)
  (focus-change GDK_FOCUS_CHANGE)
  (configure GDK_CONFIGURE)
  (map GDK_MAP)
  (unmap GDK_UNMAP)
  (property-notify GDK_PROPERTY_NOTIFY)
  (selection-clear GDK_SELECTION_CLEAR)
  (selection-request GDK_SELECTION_REQUEST)
  (selection-notify GDK_SELECTION_NOTIFY)
  (other-event GDK_OTHER_EVENT))

However, only the first two elements will be used in the current implementation. The mapping between names and constant values will not be used. The actual mapping is done by using the method described above. In fact, you can use (define-enum GdkEventType).