1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
|
/*!
@file
@author Generate utility by Albert Semenov
@date 01/2009
@module
*/
#pragma once
#include <MyGUI.h>
#include "Config.h"
#include "NativePtrHolder.h"
#include "WidgetHolder.h"
#include "Types.h"
#include "Delegates.h"
namespace MyGUI
{
namespace Managed
{
public ref class BaseWidget abstract
{
public:
BaseWidget() : mNative(0), mIsWrap(true) { }
internal:
BaseWidget( MyGUI::Widget* _native )
{
if (_native == nullptr) return;
mNative = _native;
mParent = getMangedParent(mNative);
if (mParent == nullptr) mRoots.Add(this);
else mParent->mChilds.Add(this);
WidgetHolder sender = this;
mNative->setUserData(sender);
mIsWrap = true;
}
~BaseWidget()
{
if (mNative != 0)
{
DestroyChilds();
if (mParent == nullptr)
{
mRoots.Remove(this);
}
else
{
mParent->mChilds.Remove(this);
mParent = nullptr;
}
if ( ! mIsWrap )
MyGUI::WidgetManager::getInstance().destroyWidget(mNative);
mNative = 0;
}
}
internal:
void CreateWidget( BaseWidget ^ _parent, MyGUI::WidgetStyle _style, const std::string& _skin, const MyGUI::IntCoord& _coord, MyGUI::Align _align, const std::string& _layer, const std::string& _name )
{
if (_parent == nullptr)
{
mNative = MyGUI::Gui::getInstance().createWidgetT(
getClassTypeName(),
_skin,
_coord,
_align,
_layer,
_name);
mRoots.Add(this);
}
else
{
mNative = _parent->mNative->createWidgetT(
getClassTypeName(),
_skin,
_coord,
_align,
_name);
mParent = _parent;
mParent->mChilds.Add(this);
}
WidgetHolder sender = this;
mNative->setUserData(sender);
mIsWrap = false;
}
void DestroyChilds()
{
while (mChilds.Count > 0)
{
BaseWidget ^ child = mChilds[0];
delete child;
child = nullptr;
}
}
BaseWidget ^ getMangedParent(MyGUI::Widget* _widget)
{
MyGUI::Widget* parent = _widget->getParent();
while (parent != nullptr)
{
WidgetHolder* obj = parent->getUserData< WidgetHolder >(false);
if (obj != nullptr) return obj->toObject();
parent = parent->getParent();
}
return nullptr;
}
public:
generic <typename WidgetType> where WidgetType : ref class
WidgetType CreateWidget(System::String ^ _skin, IntCoord _coord, Align _align, System::String ^ _name)
{
BaseWidget ^ child = (BaseWidget ^ )(System::Activator::CreateInstance<WidgetType>());
child->CreateWidget(this, MyGUI::WidgetStyle::Child, Convert<const std::string&>::From(_skin), Convert<const MyGUI::IntCoord&>::From(_coord), Convert<MyGUI::Align>::From(_align), "", Convert<const std::string&>::From(_name));
return (WidgetType)child;
}
generic <typename WidgetType> where WidgetType : ref class
WidgetType CreateWidget(System::String ^ _skin, IntCoord _coord, Align _align)
{
BaseWidget ^ child = (BaseWidget ^ )(System::Activator::CreateInstance<WidgetType>());
child->CreateWidget(this, MyGUI::WidgetStyle::Child, Convert<const std::string&>::From(_skin), Convert<const MyGUI::IntCoord&>::From(_coord), Convert<MyGUI::Align>::From(_align), "", "");
return (WidgetType)child;
}
generic <typename WidgetType> where WidgetType : ref class
WidgetType CreateWidget(WidgetStyle _style, System::String ^ _skin, IntCoord _coord, Align _align, System::String ^ _layer, System::String ^ _name)
{
BaseWidget ^ child = (BaseWidget ^ )(System::Activator::CreateInstance<WidgetType>());
child->CreateWidget(this, Convert<MyGUI::WidgetStyle>::From(_style), Convert<const std::string&>::From(_skin), Convert<const MyGUI::IntCoord&>::From(_coord), Convert<MyGUI::Align>::From(_align), Convert<const std::string&>::From(_layer), Convert<const std::string&>::From(_name));
return (WidgetType)child;
}
BaseWidget ^ CreateWidgetT(System::Type ^ _type, WidgetStyle _style, System::String ^ _skin, IntCoord _coord, Align _align, System::String ^ _layer, System::String ^ _name)
{
System::Reflection::ConstructorInfo ^ ci = _type->GetConstructor(gcnew cli::array < System::Type ^ > (0));
BaseWidget ^ child = (BaseWidget ^ )ci->Invoke(nullptr);
child->CreateWidget(this, Convert<MyGUI::WidgetStyle>::From(_style), Convert<const std::string&>::From(_skin), Convert<const MyGUI::IntCoord&>::From(_coord), Convert<MyGUI::Align>::From(_align), Convert<const std::string&>::From(_layer), Convert<const std::string&>::From(_name));
return child;
}
internal:
virtual const std::string& getClassTypeName() = 0;
MyGUI::Widget* GetNativePtr()
{
return mNative;
}
public:
System::IntPtr GetNativeIntPtr()
{
return System::IntPtr(mNative);
}
property System::Object ^ UserData
{
System::Object ^ get( )
{
MMYGUI_CHECK_NATIVE(mNative);
return mUserData;
}
void set(System::Object ^ _value)
{
MMYGUI_CHECK_NATIVE(mNative);
mUserData = _value;
}
}
internal:
MyGUI::Widget* mNative;
private:
bool mIsWrap;
BaseWidget ^ mParent;
System::Collections::Generic::List < BaseWidget ^ > mChilds;
System::Object ^ mUserData;
static System::Collections::Generic::List < BaseWidget ^ > mRoots;
};
} // namespace Managed
} // namespace MyGUI
|