object.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. #if defined(Hiro_Object)
  2. mObject::mObject() {
  3. Application::initialize();
  4. }
  5. mObject::~mObject() {
  6. }
  7. auto mObject::allocate() -> pObject* {
  8. return new pObject(*this);
  9. }
  10. auto mObject::construct() -> void {
  11. if(delegate) return;
  12. delegate = allocate();
  13. signal(construct);
  14. }
  15. auto mObject::destruct() -> void {
  16. if(!delegate) return;
  17. signal(destruct);
  18. delete delegate;
  19. delegate = nullptr;
  20. }
  21. //
  22. //used to test if returned items "exist" from eg Window::sizable(), ListView::selected(), etc.
  23. mObject::operator bool() const {
  24. return parent() || !abstract();
  25. }
  26. //this is used to control dynamic allocation of pObject delegates
  27. //an mObject is abstract only if it has no top-level object (eg a Button not attached to any Window)
  28. //if the mObject is not abstract, the pObject delegate is allocated immediately
  29. //otherwise, the pObject is not allocated until it is attached to a non-abstract parent
  30. auto mObject::abstract() const -> bool {
  31. #if defined(Hiro_Group)
  32. if(dynamic_cast<const mGroup*>(this)) return false;
  33. #endif
  34. #if defined(Hiro_Timer)
  35. if(dynamic_cast<const mTimer*>(this)) return false;
  36. #endif
  37. #if defined(Hiro_Window)
  38. if(dynamic_cast<const mWindow*>(this)) return false;
  39. #endif
  40. #if defined(Hiro_PopupMenu)
  41. if(dynamic_cast<const mPopupMenu*>(this)) return false;
  42. #endif
  43. if(auto object = parent()) return object->abstract();
  44. return true;
  45. }
  46. auto mObject::adjustOffset(signed displacement) -> type& {
  47. state.offset += displacement;
  48. return *this;
  49. }
  50. auto mObject::enabled(bool recursive) const -> bool {
  51. if(!recursive || !state.enabled) return state.enabled;
  52. if(auto object = parent()) return object->enabled(true);
  53. return true;
  54. }
  55. auto mObject::focused() const -> bool {
  56. return signal(focused);
  57. }
  58. auto mObject::font(bool recursive) const -> Font {
  59. if(!recursive || state.font) return state.font;
  60. if(auto object = parent()) return object->font(true);
  61. return Application::font();
  62. }
  63. auto mObject::group() const -> Group {
  64. return {};
  65. }
  66. auto mObject::offset() const -> signed {
  67. return state.offset;
  68. }
  69. auto mObject::parent() const -> mObject* {
  70. return state.parent;
  71. }
  72. #if defined(Hiro_ComboButton)
  73. auto mObject::parentComboButton(bool recursive) const -> mComboButton* {
  74. if(auto comboButton = dynamic_cast<mComboButton*>(parent())) return comboButton;
  75. if(recursive) {
  76. if(auto object = parent()) return object->parentComboButton(true);
  77. }
  78. return nullptr;
  79. }
  80. #endif
  81. #if defined(Hiro_ComboEdit)
  82. auto mObject::parentComboEdit(bool recursive) const -> mComboEdit* {
  83. if(auto comboEdit = dynamic_cast<mComboEdit*>(parent())) return comboEdit;
  84. if(recursive) {
  85. if(auto object = parent()) return object->parentComboEdit(true);
  86. }
  87. return nullptr;
  88. }
  89. #endif
  90. #if defined(Hiro_Frame)
  91. auto mObject::parentFrame(bool recursive) const -> mFrame* {
  92. if(auto frame = dynamic_cast<mFrame*>(parent())) return frame;
  93. if(recursive) {
  94. if(auto object = parent()) return object->parentFrame(true);
  95. }
  96. return nullptr;
  97. }
  98. #endif
  99. #if defined(Hiro_IconView)
  100. auto mObject::parentIconView(bool recursive) const -> mIconView* {
  101. if(auto iconView = dynamic_cast<mIconView*>(parent())) return iconView;
  102. if(recursive) {
  103. if(auto object = parent()) return object->parentIconView(true);
  104. }
  105. return nullptr;
  106. }
  107. #endif
  108. #if defined(Hiro_Menu)
  109. auto mObject::parentMenu(bool recursive) const -> mMenu* {
  110. if(auto menu = dynamic_cast<mMenu*>(parent())) return menu;
  111. if(recursive) {
  112. if(auto object = parent()) return object->parentMenu(true);
  113. }
  114. return nullptr;
  115. }
  116. #endif
  117. #if defined(Hiro_MenuBar)
  118. auto mObject::parentMenuBar(bool recursive) const -> mMenuBar* {
  119. if(auto menuBar = dynamic_cast<mMenuBar*>(parent())) return menuBar;
  120. if(recursive) {
  121. if(auto object = parent()) return object->parentMenuBar(true);
  122. }
  123. return nullptr;
  124. }
  125. #endif
  126. #if defined(Hiro_PopupMenu)
  127. auto mObject::parentPopupMenu(bool recursive) const -> mPopupMenu* {
  128. if(auto popupMenu = dynamic_cast<mPopupMenu*>(parent())) return popupMenu;
  129. if(recursive) {
  130. if(auto object = parent()) return object->parentPopupMenu(true);
  131. }
  132. return nullptr;
  133. }
  134. #endif
  135. #if defined(Hiro_Sizable)
  136. auto mObject::parentSizable(bool recursive) const -> mSizable* {
  137. if(auto sizable = dynamic_cast<mSizable*>(parent())) return sizable;
  138. if(recursive) {
  139. if(auto object = parent()) return object->parentSizable(true);
  140. }
  141. return nullptr;
  142. }
  143. #endif
  144. #if defined(Hiro_TabFrame)
  145. auto mObject::parentTabFrame(bool recursive) const -> mTabFrame* {
  146. if(auto tabFrame = dynamic_cast<mTabFrame*>(parent())) return tabFrame;
  147. if(recursive) {
  148. if(auto object = parent()) return object->parentTabFrame(true);
  149. }
  150. return nullptr;
  151. }
  152. #endif
  153. #if defined(Hiro_TabFrame)
  154. auto mObject::parentTabFrameItem(bool recursive) const -> mTabFrameItem* {
  155. if(auto tabFrameItem = dynamic_cast<mTabFrameItem*>(parent())) return tabFrameItem;
  156. if(recursive) {
  157. if(auto object = parent()) return object->parentTabFrameItem(true);
  158. }
  159. return nullptr;
  160. }
  161. #endif
  162. #if defined(Hiro_TableView)
  163. auto mObject::parentTableView(bool recursive) const -> mTableView* {
  164. if(auto tableView = dynamic_cast<mTableView*>(parent())) return tableView;
  165. if(recursive) {
  166. if(auto object = parent()) return object->parentTableView(true);
  167. }
  168. return nullptr;
  169. }
  170. #endif
  171. #if defined(Hiro_TableView)
  172. auto mObject::parentTableViewItem(bool recursive) const -> mTableViewItem* {
  173. if(auto tableViewItem = dynamic_cast<mTableViewItem*>(parent())) return tableViewItem;
  174. if(recursive) {
  175. if(auto object = parent()) return object->parentTableViewItem(true);
  176. }
  177. return nullptr;
  178. }
  179. #endif
  180. #if defined(Hiro_TreeView)
  181. auto mObject::parentTreeView(bool recursive) const -> mTreeView* {
  182. if(auto treeView = dynamic_cast<mTreeView*>(parent())) return treeView;
  183. if(recursive) {
  184. if(auto object = parent()) return object->parentTreeView(true);
  185. }
  186. return nullptr;
  187. }
  188. #endif
  189. #if defined(Hiro_TreeView)
  190. auto mObject::parentTreeViewItem(bool recursive) const -> mTreeViewItem* {
  191. if(auto treeViewItem = dynamic_cast<mTreeViewItem*>(parent())) return treeViewItem;
  192. if(recursive) {
  193. if(auto object = parent()) return object->parentTreeViewItem(true);
  194. }
  195. return nullptr;
  196. }
  197. #endif
  198. #if defined(Hiro_Widget)
  199. auto mObject::parentWidget(bool recursive) const -> mWidget* {
  200. if(auto widget = dynamic_cast<mWidget*>(parent())) return widget;
  201. if(recursive) {
  202. if(auto object = parent()) return object->parentWidget(true);
  203. }
  204. return nullptr;
  205. }
  206. #endif
  207. #if defined(Hiro_Window)
  208. auto mObject::parentWindow(bool recursive) const -> mWindow* {
  209. if(auto window = dynamic_cast<mWindow*>(parent())) return window;
  210. if(recursive) {
  211. if(auto object = parent()) return object->parentWindow(true);
  212. }
  213. return nullptr;
  214. }
  215. #endif
  216. auto mObject::remove() -> type& {
  217. signal(remove);
  218. return *this;
  219. }
  220. auto mObject::reset() -> type& {
  221. signal(reset);
  222. return *this;
  223. }
  224. auto mObject::setEnabled(bool enabled) -> type& {
  225. state.enabled = enabled;
  226. signal(setEnabled, this->enabled(true));
  227. return *this;
  228. }
  229. auto mObject::setFocused() -> type& {
  230. signal(setFocused);
  231. return *this;
  232. }
  233. auto mObject::setFont(const Font& font) -> type& {
  234. state.font = font;
  235. signal(setFont, this->font(true));
  236. return *this;
  237. }
  238. auto mObject::setGroup(sGroup group) -> type& {
  239. return *this;
  240. }
  241. auto mObject::setParent(mObject* parent, int offset) -> type& {
  242. destruct();
  243. state.parent = parent;
  244. state.offset = offset;
  245. if(!abstract()) construct();
  246. return *this;
  247. }
  248. auto mObject::setVisible(bool visible) -> type& {
  249. state.visible = visible;
  250. signal(setVisible, this->visible(true));
  251. return *this;
  252. }
  253. auto mObject::visible(bool recursive) const -> bool {
  254. if(!recursive || !state.visible) return state.visible;
  255. if(auto object = parent()) return object->visible(true);
  256. return true;
  257. }
  258. #endif