rtgui_object.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. struct rtgui_app;
  31. typedef struct rtgui_object rtgui_object_t;
  32. typedef void (*rtgui_constructor_t)(rtgui_object_t *object);
  33. typedef void (*rtgui_destructor_t)(rtgui_object_t *object);
  34. /* rtgui type structure */
  35. struct rtgui_type
  36. {
  37. /* type name */
  38. char *name;
  39. /* parent type link */
  40. const struct rtgui_type *parent;
  41. /* constructor and destructor */
  42. rtgui_constructor_t constructor;
  43. rtgui_destructor_t destructor;
  44. /* size of type */
  45. int size;
  46. };
  47. typedef struct rtgui_type rtgui_type_t;
  48. #define RTGUI_TYPE(type) (_rtgui_##type##_get_type())
  49. #define RTGUI_PARENT_TYPE(type) (const struct rtgui_type*)(&_rtgui_##type)
  50. #define DECLARE_CLASS_TYPE(type) \
  51. const rtgui_type_t *_rtgui_##type##_get_type(void); \
  52. extern const struct rtgui_type _rtgui_##type
  53. #define DEFINE_CLASS_TYPE(type, name, parent, constructor, destructor, size) \
  54. const struct rtgui_type _rtgui_##type = { \
  55. name, \
  56. parent, \
  57. RTGUI_CONSTRUCTOR(constructor), \
  58. RTGUI_DESTRUCTOR(destructor), \
  59. size }; \
  60. const rtgui_type_t *_rtgui_##type##_get_type(void) { return &_rtgui_##type; } \
  61. RTM_EXPORT(_rtgui_##type##_get_type)
  62. void rtgui_type_object_construct(const rtgui_type_t *type, rtgui_object_t *object);
  63. void rtgui_type_destructors_call(const rtgui_type_t *type, rtgui_object_t *object);
  64. rt_bool_t rtgui_type_inherits_from(const rtgui_type_t *type, const rtgui_type_t *parent);
  65. const rtgui_type_t *rtgui_type_parent_type_get(const rtgui_type_t *type);
  66. const char *rtgui_type_name_get(const rtgui_type_t *type);
  67. const rtgui_type_t *rtgui_object_object_type_get(rtgui_object_t *object);
  68. #ifdef RTGUI_USING_CAST_CHECK
  69. #define RTGUI_OBJECT_CAST(obj, obj_type, c_type) \
  70. ((c_type *)rtgui_object_check_cast((rtgui_object_t *)(obj), (obj_type), __FUNCTION__, __LINE__))
  71. #else
  72. #define RTGUI_OBJECT_CAST(obj, obj_type, c_type) ((c_type *)(obj))
  73. #endif
  74. #define RTGUI_OBJECT_CHECK_TYPE(_obj, _type) \
  75. (rtgui_type_inherits_from(((rtgui_object_t *)(_obj))->type, (_type)))
  76. DECLARE_CLASS_TYPE(object);
  77. /** Gets the type of an object */
  78. #define RTGUI_OBJECT_TYPE RTGUI_TYPE(object)
  79. /** Casts the object to an rtgui_object_t */
  80. #define RTGUI_OBJECT(obj) (RTGUI_OBJECT_CAST((obj), RTGUI_OBJECT_TYPE, struct rtgui_object))
  81. /** Checks if the object is an rtgui_Object */
  82. #define RTGUI_IS_OBJECT(obj) (RTGUI_OBJECT_CHECK_TYPE((obj), RTGUI_OBJECT_TYPE))
  83. enum rtgui_object_flag
  84. {
  85. RTGUI_OBJECT_FLAG_NONE = 0x0000,
  86. RTGUI_OBJECT_FLAG_STATIC = 0x0001,
  87. RTGUI_OBJECT_FLAG_DISABLED = 0x0002,
  88. /* When an object is created, it's flag is set to valid. When an object is
  89. * deleted, the valid bits will be cleared. */
  90. RTGUI_OBJECT_FLAG_VALID = 0xAB00,
  91. };
  92. /* rtgui base object */
  93. struct rtgui_object
  94. {
  95. /* object type */
  96. const rtgui_type_t *type;
  97. /* the event handler */
  98. rtgui_event_handler_ptr event_handler;
  99. enum rtgui_object_flag flag;
  100. rt_uint32_t id;
  101. };
  102. rtgui_object_t *rtgui_object_create(const rtgui_type_t *object_type);
  103. void rtgui_object_destroy(rtgui_object_t *object);
  104. /* set the event handler of object */
  105. void rtgui_object_set_event_handler(struct rtgui_object *object, rtgui_event_handler_ptr handler);
  106. /* object default event handler */
  107. rt_bool_t rtgui_object_event_handler(struct rtgui_object *object, struct rtgui_event *event);
  108. /* helper micro. widget event handlers could use this. */
  109. #define RTGUI_WIDGET_EVENT_HANDLER_PREPARE \
  110. struct rtgui_widget *widget; \
  111. RT_ASSERT(object != RT_NULL); \
  112. RT_ASSERT(event != RT_NULL); \
  113. widget = RTGUI_WIDGET(object); \
  114. /* supress compiler warning */ \
  115. widget = widget;
  116. /** handle @param event on @param object's own event handler
  117. *
  118. * If the @param object does not have an event handler, which means the object
  119. * does not interested in any event, it will return RT_FALSE. Otherwise, the
  120. * return code of that handler is returned.
  121. */
  122. rt_inline rt_bool_t rtgui_object_handle(struct rtgui_object *object, struct rtgui_event *event)
  123. {
  124. if (object->event_handler)
  125. return object->event_handler(object, event);
  126. return RT_FALSE;
  127. }
  128. rtgui_object_t *rtgui_object_check_cast(rtgui_object_t *object, const rtgui_type_t *type, const char *func, int line);
  129. const rtgui_type_t *rtgui_object_object_type_get(rtgui_object_t *object);
  130. void rtgui_object_set_id(struct rtgui_object *obj, rt_uint32_t id);
  131. rt_uint32_t rtgui_object_get_id(struct rtgui_object *obj);
  132. struct rtgui_object* rtgui_get_object(struct rtgui_app *app, rt_uint32_t id);
  133. struct rtgui_object* rtgui_get_self_object(rt_uint32_t id);
  134. #ifdef __cplusplus
  135. }
  136. #endif
  137. #endif