rtgui_object.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * File : rtgui_object.h
  3. * This file is part of RTGUI in RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2009-10-04 Bernard first version
  13. */
  14. #ifndef __RTGUI_OBJECT_H__
  15. #define __RTGUI_OBJECT_H__
  16. #include <rtthread.h>
  17. #include <rtgui/rtgui.h>
  18. #ifdef __cplusplus
  19. extern "C" {
  20. #endif
  21. /* rtgui object type */
  22. #define RTGUI_CONTAINER_OF(obj, type, member) \
  23. ((type *)((char *)(obj) - (unsigned long)(&((type *)0)->member)))
  24. /** Casts the function pointer to an rtgui_constructor */
  25. #define RTGUI_CONSTRUCTOR(constructor) ((rtgui_constructor_t)(constructor))
  26. /** Casts the function pointer to an rtgui_constructor */
  27. #define RTGUI_DESTRUCTOR(destructor) ((rtgui_destructor_t)(destructor))
  28. /* pre-definition */
  29. struct rtgui_object;
  30. typedef struct rtgui_object rtgui_object_t;
  31. typedef void (*rtgui_constructor_t)(rtgui_object_t *object);
  32. typedef void (*rtgui_destructor_t)(rtgui_object_t *object);
  33. /* rtgui type structure */
  34. struct rtgui_type
  35. {
  36. /* type name */
  37. char *name;
  38. /* parent type link */
  39. struct rtgui_type *parent;
  40. /* constructor and destructor */
  41. rtgui_constructor_t constructor;
  42. rtgui_destructor_t destructor;
  43. /* size of type */
  44. int size;
  45. };
  46. typedef struct rtgui_type rtgui_type_t;
  47. #define RTGUI_TYPE(type) (struct rtgui_type*)&(_rtgui_##type)
  48. #define DECLARE_CLASS_TYPE(type) extern const struct rtgui_type _rtgui_##type
  49. #define DEFINE_CLASS_TYPE(type, name, parent, constructor, destructor, size) \
  50. const struct rtgui_type _rtgui_##type = { \
  51. name, \
  52. parent, \
  53. RTGUI_CONSTRUCTOR(constructor), \
  54. RTGUI_DESTRUCTOR(destructor), \
  55. size }
  56. void rtgui_type_object_construct(const rtgui_type_t *type, rtgui_object_t *object);
  57. void rtgui_type_destructors_call(const rtgui_type_t *type, rtgui_object_t *object);
  58. rt_bool_t rtgui_type_inherits_from(const rtgui_type_t *type, const rtgui_type_t *parent);
  59. const rtgui_type_t *rtgui_type_parent_type_get(const rtgui_type_t *type);
  60. const char *rtgui_type_name_get(const rtgui_type_t *type);
  61. const rtgui_type_t *rtgui_object_object_type_get(rtgui_object_t *object);
  62. #ifdef RTGUI_USING_CAST_CHECK
  63. #define RTGUI_OBJECT_CAST(obj, obj_type, c_type) \
  64. ((c_type *)rtgui_object_check_cast((rtgui_object_t *)(obj), (obj_type), __FUNCTION__, __LINE__))
  65. #else
  66. #define RTGUI_OBJECT_CAST(obj, obj_type, c_type) ((c_type *)(obj))
  67. #endif
  68. #define RTGUI_OBJECT_CHECK_TYPE(_obj, _type) \
  69. (rtgui_type_inherits_from(((rtgui_object_t *)(_obj))->type, (_type)))
  70. DECLARE_CLASS_TYPE(object);
  71. /** Gets the type of an object */
  72. #define RTGUI_OBJECT_TYPE RTGUI_TYPE(object)
  73. /** Casts the object to an rtgui_object_t */
  74. #define RTGUI_OBJECT(obj) (RTGUI_OBJECT_CAST((obj), RTGUI_OBJECT_TYPE, struct rtgui_object))
  75. /** Checks if the object is an rtgui_Object */
  76. #define RTGUI_IS_OBJECT(obj) (RTGUI_OBJECT_CHECK_TYPE((obj), RTGUI_OBJECT_TYPE))
  77. enum rtgui_object_flag
  78. {
  79. RTGUI_OBJECT_FLAG_NONE = 0x00,
  80. RTGUI_OBJECT_FLAG_STATIC = 0x01,
  81. RTGUI_OBJECT_FLAG_DISABLED = 0x02
  82. };
  83. /* rtgui base object */
  84. struct rtgui_object
  85. {
  86. /* object type */
  87. const rtgui_type_t *type;
  88. /* the event handler */
  89. rtgui_event_handler_ptr event_handler;
  90. enum rtgui_object_flag flag;
  91. };
  92. rtgui_object_t *rtgui_object_create(rtgui_type_t *object_type);
  93. void rtgui_object_destroy(rtgui_object_t *object);
  94. /* set the event handler of object */
  95. void rtgui_object_set_event_handler(struct rtgui_object *object, rtgui_event_handler_ptr handler);
  96. /* object default event handler */
  97. rt_bool_t rtgui_object_event_handler(struct rtgui_object *object, struct rtgui_event *event);
  98. /* helper micro. widget event handlers could use this. */
  99. #define RTGUI_WIDGET_EVENT_HANDLER_PREPARE \
  100. struct rtgui_widget *widget; \
  101. RT_ASSERT(object != RT_NULL); \
  102. RT_ASSERT(event != RT_NULL); \
  103. widget = RTGUI_WIDGET(object); \
  104. /* supress compiler warning */ \
  105. widget = widget;
  106. /** handle @param event on @param object's own event handler
  107. *
  108. * If the @param object does not have an event handler, which means the object
  109. * does not interested in any event, it will return RT_FALSE. Otherwise, the
  110. * return code of that handler is returned.
  111. */
  112. rt_inline rt_bool_t rtgui_object_handle(struct rtgui_object *object, struct rtgui_event *event)
  113. {
  114. if (object->event_handler)
  115. return object->event_handler(object, event);
  116. return RT_FALSE;
  117. }
  118. rtgui_object_t *rtgui_object_check_cast(rtgui_object_t *object, rtgui_type_t *type, const char *func, int line);
  119. rtgui_type_t *rtk_object_object_type_get(rtgui_object_t *object);
  120. #ifdef __cplusplus
  121. }
  122. #endif
  123. #endif