rtgui_object.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * File : rtgui_object.h
  3. * This file is part of RT-Thread GUI Engine
  4. * COPYRIGHT (C) 2006 - 2017, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2009-10-04 Bernard first version
  23. */
  24. #ifndef __RTGUI_OBJECT_H__
  25. #define __RTGUI_OBJECT_H__
  26. #include <rtthread.h>
  27. #include <rtgui/rtgui.h>
  28. #ifdef __cplusplus
  29. extern "C" {
  30. #endif
  31. /* rtgui object type */
  32. #define RTGUI_CONTAINER_OF(obj, type, member) \
  33. ((type *)((char *)(obj) - (unsigned long)(&((type *)0)->member)))
  34. /** Casts the function pointer to an rtgui_constructor */
  35. #define RTGUI_CONSTRUCTOR(constructor) ((rtgui_constructor_t)(constructor))
  36. /** Casts the function pointer to an rtgui_constructor */
  37. #define RTGUI_DESTRUCTOR(destructor) ((rtgui_destructor_t)(destructor))
  38. /* pre-definition */
  39. struct rtgui_object;
  40. struct rtgui_app;
  41. typedef struct rtgui_object rtgui_object_t;
  42. typedef void (*rtgui_constructor_t)(rtgui_object_t *object);
  43. typedef void (*rtgui_destructor_t)(rtgui_object_t *object);
  44. /* rtgui type structure */
  45. struct rtgui_type
  46. {
  47. /* type name */
  48. char *name;
  49. /* parent type link */
  50. const struct rtgui_type *parent;
  51. /* constructor and destructor */
  52. rtgui_constructor_t constructor;
  53. rtgui_destructor_t destructor;
  54. /* size of type */
  55. int size;
  56. };
  57. typedef struct rtgui_type rtgui_type_t;
  58. #define RTGUI_TYPE(type) (_rtgui_##type##_get_type())
  59. #define RTGUI_PARENT_TYPE(type) (const struct rtgui_type*)(&_rtgui_##type)
  60. #define DECLARE_CLASS_TYPE(type) \
  61. const rtgui_type_t *_rtgui_##type##_get_type(void); \
  62. extern const struct rtgui_type _rtgui_##type
  63. #define DEFINE_CLASS_TYPE(type, name, parent, constructor, destructor, size) \
  64. const struct rtgui_type _rtgui_##type = { \
  65. name, \
  66. parent, \
  67. RTGUI_CONSTRUCTOR(constructor), \
  68. RTGUI_DESTRUCTOR(destructor), \
  69. size }; \
  70. const rtgui_type_t *_rtgui_##type##_get_type(void) { return &_rtgui_##type; } \
  71. RTM_EXPORT(_rtgui_##type##_get_type)
  72. void rtgui_type_object_construct(const rtgui_type_t *type, rtgui_object_t *object);
  73. void rtgui_type_destructors_call(const rtgui_type_t *type, rtgui_object_t *object);
  74. rt_bool_t rtgui_type_inherits_from(const rtgui_type_t *type, const rtgui_type_t *parent);
  75. const rtgui_type_t *rtgui_type_parent_type_get(const rtgui_type_t *type);
  76. const char *rtgui_type_name_get(const rtgui_type_t *type);
  77. const rtgui_type_t *rtgui_object_object_type_get(rtgui_object_t *object);
  78. #ifdef RTGUI_USING_CAST_CHECK
  79. #define RTGUI_OBJECT_CAST(obj, obj_type, c_type) \
  80. ((c_type *)rtgui_object_check_cast((rtgui_object_t *)(obj), (obj_type), __FUNCTION__, __LINE__))
  81. #else
  82. #define RTGUI_OBJECT_CAST(obj, obj_type, c_type) ((c_type *)(obj))
  83. #endif
  84. #define RTGUI_OBJECT_CHECK_TYPE(_obj, _type) \
  85. (rtgui_type_inherits_from(((rtgui_object_t *)(_obj))->type, (_type)))
  86. DECLARE_CLASS_TYPE(object);
  87. /** Gets the type of an object */
  88. #define RTGUI_OBJECT_TYPE RTGUI_TYPE(object)
  89. /** Casts the object to an rtgui_object_t */
  90. #define RTGUI_OBJECT(obj) (RTGUI_OBJECT_CAST((obj), RTGUI_OBJECT_TYPE, struct rtgui_object))
  91. /** Checks if the object is an rtgui_Object */
  92. #define RTGUI_IS_OBJECT(obj) (RTGUI_OBJECT_CHECK_TYPE((obj), RTGUI_OBJECT_TYPE))
  93. enum rtgui_object_flag
  94. {
  95. RTGUI_OBJECT_FLAG_NONE = 0x0000,
  96. RTGUI_OBJECT_FLAG_STATIC = 0x0001,
  97. RTGUI_OBJECT_FLAG_DISABLED = 0x0002,
  98. /* When an object is created, it's flag is set to valid. When an object is
  99. * deleted, the valid bits will be cleared. */
  100. RTGUI_OBJECT_FLAG_VALID = 0xAB00,
  101. };
  102. /* rtgui base object */
  103. struct rtgui_object
  104. {
  105. /* object type */
  106. const rtgui_type_t *type;
  107. /* the event handler */
  108. rtgui_event_handler_ptr event_handler;
  109. enum rtgui_object_flag flag;
  110. rt_uint32_t id;
  111. };
  112. rtgui_object_t *rtgui_object_create(const rtgui_type_t *object_type);
  113. void rtgui_object_destroy(rtgui_object_t *object);
  114. /* set the event handler of object */
  115. void rtgui_object_set_event_handler(struct rtgui_object *object, rtgui_event_handler_ptr handler);
  116. /* object default event handler */
  117. rt_bool_t rtgui_object_event_handler(struct rtgui_object *object, struct rtgui_event *event);
  118. /* helper micro. widget event handlers could use this. */
  119. #define RTGUI_WIDGET_EVENT_HANDLER_PREPARE \
  120. struct rtgui_widget *widget; \
  121. RT_ASSERT(object != RT_NULL); \
  122. RT_ASSERT(event != RT_NULL); \
  123. widget = RTGUI_WIDGET(object); \
  124. /* supress compiler warning */ \
  125. widget = widget;
  126. /** handle @param event on @param object's own event handler
  127. *
  128. * If the @param object does not have an event handler, which means the object
  129. * does not interested in any event, it will return RT_FALSE. Otherwise, the
  130. * return code of that handler is returned.
  131. */
  132. rt_inline rt_bool_t rtgui_object_handle(struct rtgui_object *object, struct rtgui_event *event)
  133. {
  134. if (object->event_handler)
  135. return object->event_handler(object, event);
  136. return RT_FALSE;
  137. }
  138. rtgui_object_t *rtgui_object_check_cast(rtgui_object_t *object, const rtgui_type_t *type, const char *func, int line);
  139. const rtgui_type_t *rtgui_object_object_type_get(rtgui_object_t *object);
  140. void rtgui_object_set_id(struct rtgui_object *obj, rt_uint32_t id);
  141. rt_uint32_t rtgui_object_get_id(struct rtgui_object *obj);
  142. struct rtgui_object* rtgui_get_object(struct rtgui_app *app, rt_uint32_t id);
  143. struct rtgui_object* rtgui_get_self_object(rt_uint32_t id);
  144. #ifdef __cplusplus
  145. }
  146. #endif
  147. #endif