1 2 /* 3 * ast-model.h 4 * 5 * Copyright (C) 2010 Christopher Li. 6 * 7 */ 8 9 #ifndef _ast_model_h_ 10 #define _ast_model_h_ 11 12 #include <stdint.h> 13 #include <gtk/gtk.h> 14 #include "lib.h" 15 16 #define AST_TYPE_NODE (ast_get_type ()) 17 #define AST_NODE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), AST_TYPE_NODE, AstNode)) 18 #define AST_NODE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), AST_TYPE_NODE, AstNodeClass)) 19 #define AST_IS_NODE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), AST_TYPE_NODE)) 20 #define AST_IS_NODE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), AST_TYPE_NODE)) 21 #define AST_NODE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), AST_TYPE_NODE, AstNodeClass)) 22 23 enum 24 { 25 AST_COL_RECORD = 0, 26 AST_COL_NAME, 27 AST_N_COLUMNS, 28 } ; 29 30 31 typedef struct AstNode AstNode; 32 typedef struct AstNodeClass AstNodeClass; 33 34 35 36 /* AstNode: this structure contains everything we need for our 37 * model implementation. You can add extra fields to 38 * this structure, e.g. hashtables to quickly lookup 39 * rows or whatever else you might need, but it is 40 * crucial that 'parent' is the first member of the 41 * structure. */ 42 43 struct AstNode 44 { 45 GObject base; /* this MUST be the first member */ 46 47 AstNode *parent; 48 int index; 49 const gchar *text; 50 void (*inspect)(struct AstNode* node); 51 void *ptr; 52 GArray *childnodes; 53 gint stamp; 54 }; 55 56 57 58 /* AstNodeClass: more boilerplate GObject stuff */ 59 60 struct AstNodeClass 61 { 62 GObjectClass base_class; 63 }; 64 65 66 GType ast_get_type(void); 67 AstNode* ast_new(AstNode *parent, int index, const char *prefix, void *ptr, void (*expand)(AstNode*)); 68 69 70 static inline 71 AstNode* ast_append_child(AstNode *parent, const char *text, 72 void *ptr, void (*inspect)(AstNode*)) 73 { 74 if (ptr) { 75 AstNode *child = ast_new(parent, parent->childnodes->len, 76 text, ptr, inspect); 77 g_array_append_val(parent->childnodes, child); 78 return child; 79 } 80 return NULL; 81 } 82 83 static inline 84 void ast_append_attribute(AstNode *parent, const char *text) 85 { 86 AstNode *child = ast_new(parent, parent->childnodes->len, text, NULL, NULL); 87 g_array_append_val(parent->childnodes, child); 88 } 89 90 #endif /* _ast_h_*/ 91