├── README.md ├── animate ├── animate_container.cpp ├── animate_container.h ├── attribute_animate.h ├── base.cpp ├── base.h ├── def.h ├── frame_animate.cpp ├── frame_animate.h ├── linear_animate.cpp ├── linear_animate.h └── tween.h ├── base ├── def.h ├── dispatcher.h ├── event.h ├── locker.cpp ├── locker.h ├── message_pump.h ├── message_pump.hpp ├── scoped_ptr.h ├── singleton.h ├── task.cpp ├── task.h ├── task_center.h ├── task_center.hpp ├── task_executor.h ├── task_executor.hpp ├── time_ticks.h ├── tuple.h └── type_list.h ├── bin └── win32d │ └── res │ ├── Thumbs.db │ ├── btn_iknow.png │ ├── change_down.png │ ├── change_hover.png │ ├── change_normal.png │ ├── close_down.png │ ├── close_hover.png │ ├── close_normal.png │ ├── default_icon.png │ ├── folder.png │ ├── icon_cloud.png │ ├── icon_exit.png │ ├── icon_setting.png │ ├── input.png │ ├── logo.png │ ├── logout.png │ ├── menu.xml │ ├── menu_bottom_middle.png │ ├── min_down.png │ ├── min_hover.png │ ├── min_normal.png │ └── settings.xml ├── core ├── attribute.h ├── const.h ├── container.cpp ├── container.h ├── context.cpp ├── context.h ├── controls │ ├── break.cpp │ ├── break.h │ ├── button.cpp │ ├── button.h │ ├── div.cpp │ ├── div.h │ ├── hr.cpp │ ├── hr.h │ ├── img.cpp │ ├── img.h │ ├── list.cpp │ ├── list.h │ ├── panel.cpp │ ├── panel.h │ ├── realwnd.cpp │ ├── realwnd.h │ ├── span.cpp │ └── span.h ├── def.h ├── layout.cpp ├── layout.h ├── object.cpp ├── object.h ├── paint.cpp ├── paint.h ├── style.cpp ├── style.h ├── types.h ├── widget.cpp └── widget.h ├── css ├── css.cpp ├── css.h ├── declare.cpp ├── declare.h ├── selector.cpp └── selector.h ├── demos ├── menu │ ├── main.cpp │ ├── maindlg.h │ ├── menudlg.h │ ├── swift.sln │ ├── swift.vcxproj │ └── swift.vcxproj.filters └── test │ ├── main.cpp │ ├── maindlg.h │ ├── swift.sln │ ├── swift.vcxproj │ └── swift.vcxproj.filters ├── swift.vcxproj ├── swift.vcxproj.filters ├── util ├── string.cpp ├── string.h └── tstring.h ├── win ├── asset_loader.cpp ├── asset_loader.h ├── asset_manager.cpp ├── asset_manager.h ├── builder.cpp ├── builder.h ├── dc.cpp ├── dc.h ├── file.cpp ├── file.h ├── font.h ├── gdi_painter.cpp ├── gdi_painter.h ├── image.cpp ├── image.h ├── layered_window.h ├── tooltip.h ├── widget_root.cpp ├── widget_root.h └── window.h └── xml ├── xml.cpp ├── xml.h ├── xmlerror.cpp ├── xmlparser.cpp ├── xmlstr.cpp └── xmlstr.h /README.md: -------------------------------------------------------------------------------- 1 | # smart-ui 2 | -------------------------------------------------------------------------------- /animate/animate_container.cpp: -------------------------------------------------------------------------------- 1 | #include "animate/animate_container.h" 2 | 3 | #include "base/task_center.hpp" 4 | #include "base/time_ticks.h" 5 | 6 | 7 | namespace animate 8 | { 9 | AnimateContainer::AnimateContainer() 10 | : timer_id_(0L) {} 11 | 12 | AnimateContainer::~AnimateContainer() {} 13 | 14 | bool AnimateContainer::Start(Animate* animate) 15 | { 16 | if (animate) 17 | { 18 | PutAnimate(animate); 19 | } 20 | 21 | if (timer_id_ != 0L) 22 | { 23 | return true; 24 | } 25 | 26 | int interval = DEFAULT_TIMER_INTERVAL; 27 | base::Task* delay_task = base::NewMethodTask(this, &AnimateContainer::OnTimer); 28 | if (!delay_task) 29 | { 30 | return false; 31 | } 32 | 33 | timer_id_ = base::Singleton::Instance().PostDelayTask(delay_task, (unsigned)interval, true); 34 | return true; 35 | } 36 | 37 | void AnimateContainer::Stop() 38 | { 39 | if (timer_id_ != 0L) 40 | { 41 | base::Singleton::Instance().DiscardTask(timer_id_); 42 | timer_id_ = 0L; 43 | } 44 | 45 | Clear(); 46 | } 47 | 48 | void AnimateContainer::PutAnimate(Animate* animate) 49 | { 50 | if (animate) 51 | { 52 | unsigned interval = animate->Interval(); 53 | unsigned total = animate->Total(); 54 | if (interval > DEFAULT_TIMER_INTERVAL) 55 | { 56 | interval = interval / DEFAULT_TIMER_INTERVAL * DEFAULT_TIMER_INTERVAL; 57 | } 58 | else 59 | { 60 | interval = DEFAULT_TIMER_INTERVAL; 61 | } 62 | animate->SetInterval(interval); 63 | if (interval > total) 64 | { 65 | animate->SetTotal(total); 66 | } 67 | animates_.insert(animate); 68 | } 69 | } 70 | 71 | void AnimateContainer::OnTimer() 72 | { 73 | _animates::iterator iter = animates_.begin(); 74 | while (iter != animates_.end()) 75 | { 76 | Animate* animate = *iter; 77 | animate->Step(); 78 | if (animate->IsFinished()) 79 | { 80 | iter = animates_.erase(iter); 81 | } 82 | else 83 | { 84 | ++iter; 85 | } 86 | } 87 | } 88 | 89 | void AnimateContainer::Clear() 90 | { 91 | _animates::iterator iter = animates_.begin(); 92 | while (iter != animates_.end()) 93 | { 94 | Animate* animate = *iter; 95 | delete animate; 96 | 97 | ++iter; 98 | } 99 | } 100 | } -------------------------------------------------------------------------------- /animate/animate_container.h: -------------------------------------------------------------------------------- 1 | #ifndef __animate_animate_container_h__ 2 | #define __animate_animate_container_h__ 3 | 4 | #include 5 | #include 6 | 7 | #include "animate/base.h" 8 | #include "animate/def.h" 9 | 10 | namespace animate 11 | { 12 | class AnimateContainer 13 | { 14 | public: 15 | AnimateContainer(); 16 | ~AnimateContainer(); 17 | 18 | bool Start(Animate* animate); 19 | void Stop(); 20 | 21 | protected: 22 | void PutAnimate(Animate* animate); 23 | void OnTimer(); 24 | void Clear(); 25 | 26 | private: 27 | typedef std::set _animates; 28 | 29 | _animates animates_; 30 | LONG timer_id_; 31 | }; 32 | } 33 | 34 | #endif -------------------------------------------------------------------------------- /animate/attribute_animate.h: -------------------------------------------------------------------------------- 1 | #ifndef __animate_attribute_animate_h__ 2 | #define __animate_attribute_animate_h__ 3 | 4 | #include "animate/def.h" 5 | #include "animate/base.h" 6 | 7 | namespace animate 8 | { 9 | class AttributeAnimate : public Animate 10 | { 11 | public: 12 | class Delegate 13 | { 14 | public: 15 | virtual void OnAttributeValueChanged(std::string id, std::string attribute, std::string value) = 0; 16 | }; 17 | 18 | AttributeAnimate() 19 | : delegate_(0) 20 | , type_(AnimateTypeNull) {} 21 | 22 | AttributeAnimate(AnimateType type) 23 | : delegate_(0) 24 | , type_(type) {} 25 | 26 | AttributeAnimate(std::string id, std::string attr_name, AnimateType type = AnimateTypeNull) 27 | : delegate_(0) 28 | , id_(id) 29 | , attribute_(attr_name) 30 | , type_(type) {} 31 | 32 | virtual ~AttributeAnimate() {} 33 | 34 | void SetDelegate(Delegate* delegate) { delegate_ = delegate; } 35 | void SetId(std::string id) { id_ = id; } 36 | void SetAttribute(std::string attribute) { attribute_ = attribute; } 37 | void SetType(AnimateType type) { type_ = type; } 38 | 39 | Delegate* GetDelegate() { return delegate_; } 40 | std::string Id() { return id_; } 41 | std::string Attribute() { return attribute_; } 42 | AnimateType Type() { return type_; } 43 | 44 | private: 45 | Delegate* delegate_; 46 | std::string id_; 47 | std::string attribute_; 48 | AnimateType type_; 49 | }; 50 | } 51 | 52 | #endif -------------------------------------------------------------------------------- /animate/base.cpp: -------------------------------------------------------------------------------- 1 | #include "animate/base.h" 2 | #include "animate/def.h" 3 | 4 | #include 5 | 6 | namespace animate 7 | { 8 | Animate::Animate() 9 | : frame_elapse_(0) 10 | , current_(0) 11 | , interval_(0) 12 | , total_(INFINITE) 13 | , finished_(false) {} 14 | 15 | Animate::~Animate() {} 16 | 17 | void Animate::RegisterObserver(Observer* observer) 18 | { 19 | if (observer) 20 | { 21 | observers_.insert(observer); 22 | } 23 | } 24 | 25 | void Animate::UnRegisterObserver(Observer *observer) 26 | { 27 | for (_observers::iterator iter = observers_.begin(); iter != observers_.end(); ++iter) 28 | { 29 | if (observer == *iter) 30 | { 31 | observers_.erase(iter); 32 | break; 33 | } 34 | } 35 | } 36 | 37 | unsigned Animate::Current() 38 | { 39 | return current_; 40 | } 41 | 42 | unsigned Animate::Interval() 43 | { 44 | return interval_; 45 | } 46 | 47 | unsigned Animate::Total() 48 | { 49 | return total_; 50 | } 51 | 52 | bool Animate::IsFinished() 53 | { 54 | return finished_; 55 | } 56 | 57 | void Animate::SetInterval(unsigned interval) 58 | { 59 | interval_ = interval; 60 | } 61 | 62 | void Animate::SetTotal(unsigned total) 63 | { 64 | total_ = total; 65 | } 66 | 67 | bool Animate::Step() 68 | { 69 | if (IsFinished()) 70 | { 71 | return false; 72 | } 73 | 74 | bool run_step = false; 75 | frame_elapse_ += DEFAULT_TIMER_INTERVAL; 76 | if (frame_elapse_ >= Interval()) 77 | { 78 | frame_elapse_ = 0; 79 | run_step = true; 80 | } 81 | if (Current() + frame_elapse_ >= Total()) 82 | { 83 | run_step = true; 84 | } 85 | 86 | if (run_step) 87 | { 88 | RunStep(); 89 | return true; 90 | } 91 | 92 | return false; 93 | } 94 | 95 | void Animate::RunStep() 96 | { 97 | NotifyObservers(); 98 | 99 | unsigned current = Current(); 100 | unsigned total = Total(); 101 | 102 | if (current == total) 103 | { 104 | SetFinished(); 105 | } 106 | 107 | DoStep(); 108 | 109 | current += Interval(); 110 | if (current >= total) 111 | { 112 | current = total; 113 | } 114 | SetCurrent(current); 115 | } 116 | 117 | void Animate::NotifyObservers() 118 | { 119 | for(_observers::iterator iter = observers_.begin(); 120 | iter != observers_.end(); 121 | ++iter) 122 | { 123 | (*iter)->OnAnimateChanged(this); 124 | } 125 | } 126 | 127 | void Animate::SetCurrent(unsigned current) 128 | { 129 | current_ = current; 130 | } 131 | 132 | void Animate::SetFinished() 133 | { 134 | finished_ = true; 135 | } 136 | } -------------------------------------------------------------------------------- /animate/base.h: -------------------------------------------------------------------------------- 1 | #ifndef __animate_base_h__ 2 | #define __animate_base_h__ 3 | 4 | #include 5 | 6 | namespace animate 7 | { 8 | class Animate 9 | { 10 | public: 11 | class Observer 12 | { 13 | public: 14 | virtual void OnAnimateChanged(Animate* animate) = 0; 15 | }; 16 | 17 | Animate(); 18 | virtual ~Animate(); 19 | 20 | void RegisterObserver(Observer *observer); 21 | void UnRegisterObserver(Observer *observer); 22 | 23 | unsigned Current(); 24 | unsigned Interval(); 25 | unsigned Total(); 26 | bool IsFinished(); 27 | 28 | void SetInterval(unsigned interval); 29 | void SetTotal(unsigned total); 30 | 31 | bool Step(); 32 | 33 | protected: 34 | void NotifyObservers(); 35 | void SetCurrent(unsigned current); 36 | void SetFinished(); 37 | void RunStep(); 38 | 39 | virtual void DoStep() = 0; 40 | 41 | private: 42 | typedef std::set _observers; 43 | 44 | _observers observers_; 45 | 46 | unsigned frame_elapse_; 47 | unsigned current_; 48 | unsigned interval_; 49 | unsigned total_; 50 | 51 | bool finished_; 52 | }; 53 | } 54 | 55 | #endif -------------------------------------------------------------------------------- /animate/def.h: -------------------------------------------------------------------------------- 1 | #ifndef __animate_def_h__ 2 | #define __animate_def_h__ 3 | 4 | namespace animate 5 | { 6 | enum AnimateType 7 | { 8 | AnimateTypeNull, 9 | AnimateTypeLinear, 10 | AnimateTypeFrame, 11 | }; 12 | 13 | enum LinerAnimateStyle 14 | { 15 | LinerAnimateStyleNull, 16 | LinerAnimateStyleLiner, 17 | }; 18 | 19 | static const int DEFAULT_TIMER_INTERVAL = 10U; 20 | } 21 | 22 | #endif -------------------------------------------------------------------------------- /animate/frame_animate.cpp: -------------------------------------------------------------------------------- 1 | #include "animate/frame_animate.h" 2 | 3 | namespace animate 4 | { 5 | AttributeFrameAnimate::AttributeFrameAnimate() 6 | : AttributeAnimate(AnimateTypeFrame) 7 | , count_(0) 8 | , current_count_(0) {} 9 | 10 | AttributeFrameAnimate::AttributeFrameAnimate(int count) 11 | : AttributeAnimate(AnimateTypeFrame) 12 | , count_(count) 13 | , current_count_(0) {} 14 | 15 | AttributeFrameAnimate::AttributeFrameAnimate(std::string id, std::string attr_name, int count) 16 | : AttributeAnimate(id, attr_name, AnimateTypeFrame) 17 | , count_(count) 18 | , current_count_(0) {} 19 | 20 | void AttributeFrameAnimate::SetCount(int count) 21 | { 22 | count_ = count; 23 | } 24 | 25 | int AttributeFrameAnimate::Count() 26 | { 27 | return count_; 28 | } 29 | 30 | void AttributeFrameAnimate::DoStep() 31 | { 32 | int count = Count(); 33 | if (count <= 0) 34 | { 35 | count = 1; 36 | } 37 | 38 | current_count_ = current_count_ % count; 39 | char str_value[16] = {0}; 40 | _itoa_s(current_count_, str_value, 10); 41 | current_count_++; 42 | if (GetDelegate()) 43 | { 44 | GetDelegate()->OnAttributeValueChanged(Id(), Attribute(), std::string(str_value)); 45 | } 46 | } 47 | } -------------------------------------------------------------------------------- /animate/frame_animate.h: -------------------------------------------------------------------------------- 1 | #ifndef __animate_frame_animate_h__ 2 | #define __animate_frame_animate_h__ 3 | 4 | #include "animate/attribute_animate.h" 5 | 6 | namespace animate 7 | { 8 | class AttributeFrameAnimate : public AttributeAnimate 9 | { 10 | public: 11 | AttributeFrameAnimate(); 12 | AttributeFrameAnimate(int count); 13 | AttributeFrameAnimate(std::string id, std::string attr_name, int count); 14 | 15 | void SetCount(int count); 16 | int Count(); 17 | 18 | virtual void DoStep(); 19 | 20 | private: 21 | int count_; 22 | int current_count_; 23 | }; 24 | } 25 | 26 | #endif -------------------------------------------------------------------------------- /animate/linear_animate.cpp: -------------------------------------------------------------------------------- 1 | #include "animate/linear_animate.h" 2 | 3 | namespace animate 4 | { 5 | AttributeLinerAnimate::AttributeLinerAnimate() 6 | : AttributeAnimate(AnimateTypeLinear) 7 | , begin_value_(0) 8 | , end_value_(0) 9 | , liner_style_(LinerAnimateStyleNull) {} 10 | 11 | AttributeLinerAnimate::AttributeLinerAnimate( 12 | std::string id, 13 | std::string attr_name, 14 | int begin_value, 15 | int end_value, 16 | LinerAnimateStyle style) 17 | : AttributeAnimate(id, attr_name, AnimateTypeLinear) 18 | , begin_value_(begin_value) 19 | , end_value_(end_value) 20 | , liner_style_(style) {} 21 | 22 | void AttributeLinerAnimate::SetBeginValue(int begin_value) 23 | { 24 | begin_value_ = begin_value; 25 | } 26 | 27 | int AttributeLinerAnimate::BeginValue() 28 | { 29 | return begin_value_; 30 | } 31 | 32 | void AttributeLinerAnimate::SetEndValue(int end_value) 33 | { 34 | end_value_ = end_value; 35 | } 36 | 37 | int AttributeLinerAnimate::EndValue() 38 | { 39 | return end_value_; 40 | } 41 | 42 | void AttributeLinerAnimate::DoStep() 43 | { 44 | float current = (float)Current(); 45 | float total = (float)Total(); 46 | 47 | int value = 0; 48 | switch(liner_style_) 49 | { 50 | case LinerAnimateStyleLiner: 51 | value = tween::Linear::calculate( 52 | current, 53 | begin_value_, 54 | end_value_ - begin_value_, 55 | total); 56 | break; 57 | 58 | default: 59 | break; 60 | } 61 | 62 | char str_value[16] = {0}; 63 | _itoa_s(value, str_value, 10); 64 | 65 | if (GetDelegate()) 66 | { 67 | GetDelegate()->OnAttributeValueChanged(Id(), Attribute(), std::string(str_value)); 68 | } 69 | } 70 | } -------------------------------------------------------------------------------- /animate/linear_animate.h: -------------------------------------------------------------------------------- 1 | #ifndef __animate_linear_animate_h__ 2 | #define __animate_linear_animate_h__ 3 | 4 | #include 5 | #include 6 | 7 | #include 8 | #include 9 | 10 | #include "animate/def.h" 11 | #include "animate/tween.h" 12 | #include "animate/attribute_animate.h" 13 | 14 | 15 | namespace animate 16 | { 17 | class AttributeLinerAnimate : public AttributeAnimate 18 | { 19 | public: 20 | AttributeLinerAnimate(); 21 | 22 | AttributeLinerAnimate( 23 | std::string id, 24 | std::string attr_name, 25 | int begin_value, 26 | int end_value, 27 | LinerAnimateStyle style); 28 | 29 | void SetBeginValue(int begin_value); 30 | int BeginValue(); 31 | 32 | void SetEndValue(int end_value); 33 | int EndValue(); 34 | 35 | virtual void DoStep(); 36 | 37 | private: 38 | int begin_value_; 39 | int end_value_; 40 | LinerAnimateStyle liner_style_; 41 | }; 42 | } 43 | 44 | #endif -------------------------------------------------------------------------------- /base/def.h: -------------------------------------------------------------------------------- 1 | #ifndef __base_def_h__ 2 | #define __base_def_h__ 3 | 4 | #define DISABLE_COPY_AND_ASSIGN(theclass)\ 5 | theclass(const theclass&);\ 6 | void operator=(const theclass&); 7 | 8 | #endif -------------------------------------------------------------------------------- /base/dispatcher.h: -------------------------------------------------------------------------------- 1 | #ifndef __base_dispatcher_h__ 2 | #define __base_dispatcher_h__ 3 | 4 | #include "base/tuple.h" 5 | 6 | namespace base 7 | { 8 | template 9 | inline void DispatchToMethod(Object* obj, Method method, const Tuple0& arg) 10 | { 11 | (obj->*method)(); 12 | } 13 | 14 | template 16 | inline void DispatchToMethod(Object* obj, Method method, const Tuple1& arg) 17 | { 18 | (obj->*method)(arg.a); 19 | } 20 | 21 | template 23 | inline void DispatchToMethod(Object* obj, Method method, const Tuple2& arg) 24 | { 25 | (obj->*method)(arg.a, arg.b); 26 | } 27 | 28 | template 30 | inline void DispatchToMethod(Object* obj, Method method, const Tuple3& arg) 31 | { 32 | (obj->*method)(arg.a, arg.b, arg.c); 33 | } 34 | 35 | template 38 | inline void DispatchToMethod(Object* obj, Method method, const Tuple4& arg) 39 | { 40 | (obj->*method)(arg.a, arg.b, arg.c, arg.d); 41 | } 42 | 43 | template 46 | inline void DispatchToMethod(Object* obj, Method method, const Tuple5& arg) 47 | { 48 | (obj->*method)(arg.a, arg.b, arg.c, arg.d, arg.e); 49 | } 50 | 51 | template 54 | inline void DispatchToMethod(Object* obj, Method method, const Tuple6& arg) 55 | { 56 | (obj->*method)(arg.a, arg.b, arg.c, arg.d, arg.e, arg.f); 57 | } 58 | 59 | template 63 | inline void DispatchToMethod(Object* obj, Method method, const Tuple7& arg) 64 | { 65 | (obj->*method)(arg.a, arg.b, arg.c, arg.d, arg.e, arg.f, arg.g); 66 | } 67 | 68 | template 72 | inline void DispatchToMethod(Object* obj, Method method, const Tuple8& arg) 73 | { 74 | (obj->*method)(arg.a, arg.b, arg.c, arg.d, arg.e, arg.f, arg.g, arg.h); 75 | } 76 | } 77 | 78 | #endif -------------------------------------------------------------------------------- /base/event.h: -------------------------------------------------------------------------------- 1 | #ifndef __base_event_h__ 2 | #define __base_event_h__ 3 | 4 | #include 5 | #include 6 | 7 | namespace base 8 | { 9 | class Event 10 | { 11 | public: 12 | Event() 13 | { 14 | event_ = ::CreateEvent(0, TRUE, FALSE, NULL); 15 | } 16 | Event(LPCTSTR name) 17 | { 18 | event_ = ::CreateEvent(0, TRUE, FALSE, name); 19 | } 20 | 21 | ~Event() 22 | { 23 | if (event_) 24 | { 25 | ::CloseHandle(event_); 26 | } 27 | } 28 | 29 | void Set() 30 | { 31 | if (event_) 32 | { 33 | ::SetEvent(event_); 34 | } 35 | } 36 | 37 | void Reset() 38 | { 39 | if (event_) 40 | { 41 | ::ResetEvent(event_); 42 | } 43 | } 44 | 45 | DWORD Wait(DWORD timeout = 0) 46 | { 47 | return ::WaitForSingleObject(event_, timeout); 48 | } 49 | 50 | private: 51 | HANDLE event_; 52 | }; 53 | } 54 | 55 | #endif -------------------------------------------------------------------------------- /base/locker.cpp: -------------------------------------------------------------------------------- 1 | #include "base/locker.h" 2 | 3 | namespace base 4 | { 5 | CriticalSectionMutex::CriticalSectionMutex() 6 | { 7 | ::InitializeCriticalSection(&critical_section_); 8 | } 9 | 10 | CriticalSectionMutex::~CriticalSectionMutex() 11 | { 12 | ::DeleteCriticalSection(&critical_section_); 13 | } 14 | 15 | void CriticalSectionMutex::Acquire() 16 | { 17 | ::EnterCriticalSection(&critical_section_); 18 | } 19 | 20 | void CriticalSectionMutex::Release() 21 | { 22 | ::LeaveCriticalSection(&critical_section_); 23 | } 24 | 25 | 26 | SpinMutex::SpinMutex() 27 | : locked_(0L) {} 28 | 29 | SpinMutex::~SpinMutex() {} 30 | 31 | void SpinMutex::Acquire() 32 | { 33 | while (InterlockedExchange(&locked_, 1L) == 1L) 34 | Sleep(0); 35 | } 36 | 37 | void SpinMutex::Release() 38 | { 39 | InterlockedExchange(&locked_, 0L); 40 | } 41 | } -------------------------------------------------------------------------------- /base/locker.h: -------------------------------------------------------------------------------- 1 | #ifndef __base_locker_h__ 2 | #define __base_locker_h__ 3 | 4 | #include 5 | 6 | namespace base 7 | { 8 | class CriticalSectionMutex 9 | { 10 | public: 11 | CriticalSectionMutex(); 12 | ~CriticalSectionMutex(); 13 | 14 | void Acquire(); 15 | void Release(); 16 | 17 | private: 18 | CRITICAL_SECTION critical_section_; 19 | }; 20 | 21 | class SpinMutex 22 | { 23 | public: 24 | SpinMutex(); 25 | ~SpinMutex(); 26 | 27 | void Acquire(); 28 | void Release(); 29 | 30 | private: 31 | LONG locked_; 32 | }; 33 | 34 | class EmptyMutex 35 | { 36 | public: 37 | EmptyMutex() {} 38 | ~EmptyMutex() {} 39 | 40 | void Acquire() {} 41 | void Release() {} 42 | }; 43 | 44 | 45 | template 46 | class ThreadGuard 47 | { 48 | public: 49 | ThreadGuard(Mutex& mutex) 50 | : mutex_(mutex) 51 | { 52 | mutex_.Acquire(); 53 | } 54 | 55 | ~ThreadGuard() 56 | { 57 | mutex_.Release(); 58 | } 59 | 60 | private: 61 | Mutex& mutex_; 62 | }; 63 | } 64 | 65 | #endif -------------------------------------------------------------------------------- /base/message_pump.h: -------------------------------------------------------------------------------- 1 | #ifndef __base_message_pump_h__ 2 | #define __base_message_pump_h__ 3 | 4 | #include 5 | 6 | #include "base/def.h" 7 | 8 | namespace base 9 | { 10 | template 11 | class MessagePump 12 | { 13 | public: 14 | MessagePump(); 15 | ~MessagePump(); 16 | 17 | int Run(Processor* processor); 18 | void Quit(int code); 19 | 20 | bool ScheduleTask(); 21 | bool ScheduleDelayTask(unsigned delay); 22 | 23 | private: 24 | static LRESULT CALLBACK WndProcThunk(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam); 25 | void InitMessageWnd(); 26 | void UnInitMessageWnd(); 27 | 28 | void RunLoop(); 29 | bool ProcessNextWindowMessage(); 30 | bool ProcessWindowMessage(const MSG& msg); 31 | void DispatchWindowMessage(const MSG& msg); 32 | 33 | void WillProcessMessage(const MSG& msg); 34 | void DidProcessMessage(const MSG& msg); 35 | 36 | void HandleTaskMessage(); 37 | void HandleTimerMessage(); 38 | 39 | private: 40 | struct RunState 41 | { 42 | Processor* processor; 43 | 44 | bool should_quit; 45 | int code; 46 | }; 47 | 48 | RunState state_; 49 | HWND message_hwnd_; 50 | LONG have_task_; 51 | LONG have_timer_task_; 52 | 53 | private: 54 | DISABLE_COPY_AND_ASSIGN(MessagePump) 55 | }; 56 | } 57 | 58 | #endif -------------------------------------------------------------------------------- /base/message_pump.hpp: -------------------------------------------------------------------------------- 1 | #ifndef __base_message_pump_hpp__ 2 | #define __base_message_pump_hpp__ 3 | 4 | #include "base/message_pump.h" 5 | #include "util/string.h" 6 | 7 | namespace base 8 | { 9 | static const wchar_t kWndClass[] = L"MessagePumpWindow"; 10 | 11 | static const int WM_TASK = (WM_USER + 1); 12 | 13 | template 14 | MessagePump::MessagePump() 15 | : have_task_(0L) 16 | , have_timer_task_(0L) 17 | { 18 | InitMessageWnd(); 19 | } 20 | 21 | template 22 | MessagePump::~MessagePump() 23 | { 24 | UnInitMessageWnd(); 25 | } 26 | 27 | template 28 | int MessagePump::Run(Processor* processor) 29 | { 30 | state_.processor = processor; 31 | state_.should_quit = false; 32 | state_.code = 0; 33 | 34 | RunLoop(); 35 | 36 | return state_.code; 37 | } 38 | 39 | template 40 | void MessagePump::Quit(int code) 41 | { 42 | PostQuitMessage(code); 43 | state_.should_quit = true; 44 | } 45 | 46 | template 47 | bool MessagePump::ScheduleTask() 48 | { 49 | if (InterlockedExchange(&have_task_, 1L)) 50 | { 51 | return false; 52 | } 53 | 54 | PostMessage(message_hwnd_, WM_TASK, reinterpret_cast(this), 0); 55 | return true; 56 | } 57 | 58 | template 59 | bool MessagePump::ScheduleDelayTask(unsigned delay) 60 | { 61 | if (InterlockedExchange(&have_timer_task_, 1L)) 62 | { 63 | return false; 64 | } 65 | 66 | if (delay < USER_TIMER_MINIMUM) 67 | { 68 | delay = USER_TIMER_MINIMUM; 69 | } 70 | 71 | SetTimer(message_hwnd_, reinterpret_cast(this), delay, NULL); 72 | return true; 73 | } 74 | 75 | template 76 | void MessagePump::InitMessageWnd() 77 | { 78 | HINSTANCE hinst = GetModuleHandle(NULL); 79 | 80 | WNDCLASSEX wc = { 0 }; 81 | wc.cbSize = sizeof(wc); 82 | wc.lpfnWndProc = WndProcThunk; 83 | wc.hInstance = hinst; 84 | wc.lpszClassName = kWndClass; 85 | RegisterClassEx(&wc); 86 | 87 | message_hwnd_ = CreateWindow(kWndClass, 0, 0, 0, 0, 0, 0, HWND_MESSAGE, 0, hinst, 0); 88 | } 89 | 90 | template 91 | void MessagePump::UnInitMessageWnd() 92 | { 93 | DestroyWindow(message_hwnd_); 94 | UnregisterClass(kWndClass, GetModuleHandle(NULL)); 95 | } 96 | 97 | template 98 | LRESULT CALLBACK MessagePump::WndProcThunk(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) 99 | { 100 | switch(message) 101 | { 102 | case WM_TASK: 103 | reinterpret_cast(wparam)->HandleTaskMessage(); 104 | break; 105 | 106 | case WM_TIMER: 107 | reinterpret_cast(wparam)->HandleTimerMessage(); 108 | break; 109 | } 110 | return DefWindowProc(hwnd, message, wparam, lparam); 111 | } 112 | 113 | template 114 | void MessagePump::RunLoop() 115 | { 116 | while (true) 117 | { 118 | bool more_work = ProcessNextWindowMessage(); 119 | if (state_.should_quit) 120 | { 121 | break; 122 | } 123 | 124 | state_.processor->DoTask(); 125 | if (state_.should_quit) 126 | { 127 | break; 128 | } 129 | 130 | unsigned next_delay_time = 0; 131 | state_.processor->DoDelayTask(&next_delay_time); 132 | if (state_.should_quit) 133 | { 134 | break; 135 | } 136 | 137 | if (more_work) 138 | { 139 | continue; 140 | } 141 | 142 | MsgWaitForMultipleObjectsEx(0, NULL, next_delay_time, QS_ALLINPUT, MWMO_INPUTAVAILABLE); 143 | } 144 | } 145 | 146 | template 147 | bool MessagePump::ProcessNextWindowMessage() 148 | { 149 | bool sent_messages_in_queue = false; 150 | DWORD queue_status = GetQueueStatus(QS_SENDMESSAGE); 151 | if (HIWORD(queue_status) & QS_SENDMESSAGE) 152 | { 153 | sent_messages_in_queue = true; 154 | } 155 | 156 | MSG msg; 157 | if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) 158 | { 159 | return ProcessWindowMessage(msg); 160 | } 161 | 162 | return sent_messages_in_queue; 163 | } 164 | 165 | template 166 | bool MessagePump::ProcessWindowMessage(const MSG& msg) 167 | { 168 | if (msg.message == WM_QUIT) 169 | { 170 | state_.should_quit = true; 171 | state_.code = msg.wParam; 172 | return false; 173 | } 174 | 175 | DispatchWindowMessage(msg); 176 | return true; 177 | } 178 | 179 | template 180 | void MessagePump::DispatchWindowMessage(const MSG& msg) 181 | { 182 | WillProcessMessage(msg); 183 | 184 | TranslateMessage(&msg); 185 | DispatchMessage(&msg); 186 | 187 | DidProcessMessage(msg); 188 | } 189 | 190 | template 191 | void MessagePump::WillProcessMessage(const MSG& msg) 192 | { 193 | } 194 | 195 | template 196 | void MessagePump::DidProcessMessage(const MSG& msg) 197 | { 198 | } 199 | 200 | template 201 | void MessagePump::HandleTaskMessage() 202 | { 203 | InterlockedExchange(&have_task_, 0L); 204 | } 205 | 206 | template 207 | void MessagePump::HandleTimerMessage() 208 | { 209 | KillTimer(message_hwnd_, reinterpret_cast(this)); 210 | InterlockedExchange(&have_timer_task_, 0L); 211 | } 212 | } 213 | 214 | #endif -------------------------------------------------------------------------------- /base/scoped_ptr.h: -------------------------------------------------------------------------------- 1 | #ifndef __base_scoped_ptr_h__ 2 | #define __base_scoped_ptr_h__ 3 | 4 | #include "base/def.h" 5 | 6 | namespace base 7 | { 8 | template 9 | class ScopedPtr 10 | { 11 | public: 12 | typedef T element_type; 13 | 14 | explicit ScopedPtr(T* p = 0) 15 | : ptr(p) {} 16 | 17 | ~ScopedPtr() 18 | { 19 | typedef char type_must_be_complete[sizeof(T)]; 20 | delete ptr; 21 | } 22 | 23 | void reset(T* p = 0) 24 | { 25 | typedef char type_must_be_complete[sizeof(T)]; 26 | 27 | if (ptr != p) 28 | { 29 | T* obj = ptr; 30 | ptr = p; 31 | delete obj; 32 | } 33 | } 34 | 35 | T& operator*() const 36 | { 37 | return *ptr; 38 | } 39 | 40 | T* operator->() const 41 | { 42 | return ptr; 43 | } 44 | 45 | T* get() const 46 | { 47 | return ptr; 48 | } 49 | 50 | void swap(ScopedPtr& b) 51 | { 52 | T* tmp = b.ptr; 53 | b.ptr = ptr; 54 | ptr = tmp; 55 | } 56 | 57 | T* release() 58 | { 59 | T* tmp = ptr; 60 | ptr = 0; 61 | return tmp; 62 | } 63 | 64 | T** accept() 65 | { 66 | if (ptr) 67 | { 68 | delete ptr; 69 | ptr = 0; 70 | } 71 | return &ptr; 72 | } 73 | 74 | T** use() 75 | { 76 | return &ptr; 77 | } 78 | 79 | private: 80 | T* ptr; 81 | 82 | private: 83 | DISABLE_COPY_AND_ASSIGN(ScopedPtr) 84 | }; 85 | 86 | 87 | 88 | template 89 | class ScopedArray 90 | { 91 | public: 92 | typedef T element_type; 93 | 94 | explicit ScopedArray(T* p = 0) 95 | : ptr(p) {} 96 | 97 | ~ScopedArray() 98 | { 99 | typedef char type_must_be_complete[sizeof(T)]; 100 | delete[] ptr; 101 | } 102 | 103 | void reset(T* p = 0) 104 | { 105 | typedef char type_must_be_complete[sizeof(T)]; 106 | if (ptr != p) 107 | { 108 | T* arr = ptr; 109 | ptr = p; 110 | delete [] arr; 111 | } 112 | } 113 | 114 | T& operator[](int i) const 115 | { 116 | return ptr[i]; 117 | } 118 | 119 | T* get() const 120 | { 121 | return ptr; 122 | } 123 | 124 | void swap(ScopedArray & b) 125 | { 126 | T* tmp = b.ptr; 127 | b.ptr = ptr; 128 | ptr = tmp; 129 | } 130 | 131 | T* release() 132 | { 133 | T* tmp = ptr; 134 | ptr = 0; 135 | return tmp; 136 | } 137 | 138 | T** accept() 139 | { 140 | if (ptr) 141 | { 142 | delete [] ptr; 143 | ptr = 0; 144 | } 145 | 146 | return &ptr; 147 | } 148 | 149 | private: 150 | T* ptr; 151 | 152 | private: 153 | DISABLE_COPY_AND_ASSIGN(ScopedArray) 154 | }; 155 | } 156 | 157 | #endif -------------------------------------------------------------------------------- /base/singleton.h: -------------------------------------------------------------------------------- 1 | #ifndef __base_singleton_h__ 2 | #define __base_singleton_h__ 3 | 4 | #include "base/locker.h" 5 | 6 | namespace base 7 | { 8 | template 9 | struct DefaultSingletonTraits 10 | { 11 | static T* Create() 12 | { 13 | return new T(); 14 | } 15 | static void Destroy(T* obj) 16 | { 17 | delete obj; 18 | } 19 | 20 | const static bool register_at_exit_ = true; 21 | }; 22 | 23 | template 24 | struct LeakySingletonTraits: public DefaultSingletonTraits 25 | { 26 | static const bool register_at_exit_ = false; 27 | }; 28 | 29 | template, 31 | typename Mutex = CriticalSectionMutex> 32 | class Singleton 33 | { 34 | public: 35 | static T& Instance() 36 | { 37 | if (!instance_) 38 | { 39 | ThreadGuard guard(mutex_); 40 | if (!instance_) 41 | { 42 | instance_ = SingletonTraits::Create(); 43 | if (instance_ && SingletonTraits::register_at_exit_) 44 | { 45 | atexit(DestroySingleton); 46 | } 47 | } 48 | } 49 | return *instance_; 50 | } 51 | 52 | private: 53 | static void DestroySingleton() 54 | { 55 | SingletonTraits::Destroy(instance_); 56 | instance_ = 0; 57 | } 58 | 59 | private: 60 | static T* instance_; 61 | static Mutex mutex_; 62 | 63 | private: 64 | Singleton(){} 65 | ~Singleton(){} 66 | 67 | Singleton(const Singleton&); 68 | Singleton& operator=(const Singleton&); 69 | }; 70 | 71 | template 72 | T* Singleton::instance_ = 0; 73 | 74 | template 75 | Mutex Singleton::mutex_; 76 | } 77 | 78 | #endif -------------------------------------------------------------------------------- /base/task.cpp: -------------------------------------------------------------------------------- 1 | #include "base/task.h" 2 | 3 | namespace base 4 | { 5 | Task::Task() {} 6 | 7 | Task::~Task() {} 8 | } -------------------------------------------------------------------------------- /base/task.h: -------------------------------------------------------------------------------- 1 | #ifndef __base_task_h__ 2 | #define __base_task_h__ 3 | 4 | #include "base/tuple.h" 5 | #include "base/dispatcher.h" 6 | 7 | namespace base 8 | { 9 | class Task 10 | { 11 | public: 12 | Task(); 13 | virtual ~Task(); 14 | 15 | virtual void Run() = 0; 16 | }; 17 | 18 | 19 | 20 | template 21 | class MethodTask : public Task 22 | { 23 | public: 24 | MethodTask(Object *obj, Method method, const Params ¶ms) 25 | : obj_(obj), method_(method), params_(params) 26 | {} 27 | 28 | virtual void Run() 29 | { 30 | DispatchToMethod(obj_, method_, params_); 31 | } 32 | 33 | private: 34 | Object *obj_; 35 | Method method_; 36 | Params params_; 37 | }; 38 | 39 | template 40 | inline Task* NewMethodTask(Object* obj, Method method) 41 | { 42 | return new MethodTask( 43 | obj, method, MakeTuple()); 44 | } 45 | 46 | template 48 | inline Task* NewMethodTask(Object* obj, Method method, 49 | const A& a) 50 | { 51 | return new MethodTask >( 52 | obj, method, MakeTuple(a)); 53 | } 54 | 55 | template 57 | inline Task* NewMethodTask(Object* obj, Method method, 58 | const A& a, const B& b) 59 | { 60 | return new MethodTask >( 61 | obj, method, MakeTuple(a, b)); 62 | } 63 | 64 | template 66 | inline Task* NewMethodTask(Object* obj, Method method, 67 | const A& a, const B& b, const C& c) 68 | { 69 | return new MethodTask >( 70 | obj, method, MakeTuple(a, b, c)); 71 | } 72 | 73 | template 76 | inline Task* NewMethodTask(Object* obj, Method method, 77 | const A& a, const B& b, const C& c, 78 | const D& d) 79 | { 80 | return new MethodTask >( 81 | obj, method, MakeTuple(a, b, c, d)); 82 | } 83 | 84 | template 87 | inline Task* NewMethodTask(Object* obj, Method method, 88 | const A& a, const B& b, const C& c, 89 | const D& d, const E& e) 90 | { 91 | return new MethodTask >( 92 | obj, method, MakeTuple(a, b, c, d, e)); 93 | } 94 | 95 | template 98 | inline Task* NewMethodTask(Object* obj, Method method, 99 | const A& a, const B& b, const C& c, 100 | const D& d, const E& e, const F& f) 101 | { 102 | return new MethodTask >( 103 | obj, method, MakeTuple(a, b, c, d, e, f)); 104 | } 105 | 106 | template 110 | inline Task* NewMethodTask(Object* obj, Method method, 111 | const A& a, const B& b, const C& c, 112 | const D& d, const E& e, const F& f, 113 | const G& g) 114 | { 115 | return new MethodTask >( 116 | obj, method, MakeTuple(a, b, c, d, e, f, g)); 117 | } 118 | 119 | template 123 | inline Task* NewMethodTask(Object* obj, Method method, 124 | const A& a, const B& b, const C& c, 125 | const D& d, const E& e, const F& f, 126 | const G& g, const H& h) 127 | { 128 | return new MethodTask >( 129 | obj, method, MakeTuple(a, b, c, d, e, f, g, h)); 130 | } 131 | } 132 | 133 | #endif -------------------------------------------------------------------------------- /base/task_center.h: -------------------------------------------------------------------------------- 1 | #ifndef __base_task_center_h__ 2 | #define __base_task_center_h__ 3 | 4 | #include 5 | #include 6 | 7 | #include "base/locker.h" 8 | #include "base/task.h" 9 | #include "base/message_pump.hpp" 10 | #include "base/singleton.h" 11 | 12 | namespace base 13 | { 14 | template class Pump> 15 | class TaskCenter 16 | { 17 | public: 18 | template friend class MessagePump; 19 | 20 | TaskCenter(); 21 | ~TaskCenter(); 22 | 23 | int Run(); 24 | bool Quit(int code); 25 | 26 | LONG PostTask(Task* task); 27 | LONG PostDelayTask(Task* task, unsigned delay_time, bool immortal = false); 28 | 29 | void DiscardTask(LONG id); 30 | 31 | private: 32 | void DoTask(); 33 | void DoDelayTask(unsigned* next_delay_time); 34 | 35 | private: 36 | struct PendingTask 37 | { 38 | PendingTask() {} 39 | PendingTask(Task* task, unsigned delay_time, bool immortal, LONG task_id) 40 | : task_(task) 41 | , time_delay_(delay_time) 42 | , immortal_(immortal) 43 | , task_id_(task_id) 44 | , time_post_(TimeTicks::Now()) {} 45 | 46 | ~PendingTask() {} 47 | 48 | bool operator< (const PendingTask& task) const 49 | { 50 | return task.TimeDeal() < this->TimeDeal(); 51 | } 52 | 53 | __int64 TimePost() const { return time_post_; } 54 | __int64 TimeDeal() const { return time_post_ + time_delay_ * TimeTicks::MICROSECONDS_PER_MILLISECOND; } 55 | 56 | Task* task_; 57 | unsigned time_delay_; 58 | bool immortal_; 59 | LONG task_id_; 60 | 61 | __int64 time_post_; 62 | }; 63 | 64 | LONG AddToTaskQueue(Task* task); 65 | LONG AddToDelayTaskQueue(Task* task, unsigned delay_time, bool immortal, LONG id = 0L); 66 | void ReloadDelayTaskQueue(std::queue& tasks); 67 | unsigned GetNextDelayTime(); 68 | 69 | bool DiscardTasks(); 70 | bool DiscardDelayTasks(); 71 | 72 | void RunTask(PendingTask pending_task); 73 | bool IsDiscardedTask(PendingTask& pending_task); 74 | void Discard(PendingTask& pending_task); 75 | 76 | LONG GetState(); 77 | void SetState(LONG state); 78 | 79 | private: 80 | CriticalSectionMutex mutex_; 81 | std::queue task_queue_; 82 | std::priority_queue delay_task_queue_; 83 | std::map discard_queue_; 84 | 85 | enum State 86 | { 87 | STATE_DEFAULT = 0L, 88 | STATE_RUNNING = 1L, 89 | STATE_STOPED = 2L 90 | }; 91 | LONG run_state_; 92 | Pump pump_; 93 | 94 | LONG task_id_; 95 | }; 96 | 97 | typedef TaskCenter TaskCenterUI; 98 | } 99 | 100 | #endif -------------------------------------------------------------------------------- /base/task_executor.h: -------------------------------------------------------------------------------- 1 | #ifndef __base_task_executor_h__ 2 | #define __base_task_executor_h__ 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #include "base/def.h" 10 | #include "base/task.h" 11 | #include "base/locker.h" 12 | #include "base/event.h" 13 | 14 | namespace base 15 | { 16 | struct DefaultThreadFactory 17 | { 18 | typedef unsigned (__stdcall *LPTHREAD_FUNCTION)(void* lpThreadParameter); 19 | static HANDLE CreateThread(LPTHREAD_FUNCTION func, void* param, DWORD flag) 20 | { 21 | if (!func) 22 | { 23 | return 0; 24 | } 25 | 26 | return (HANDLE)_beginthreadex(0, 0, func, param, flag, 0); 27 | } 28 | 29 | static void DestroyThread(HANDLE handle, DWORD timeout) 30 | { 31 | if (!handle) 32 | { 33 | return; 34 | } 35 | 36 | DWORD wait = ::WaitForSingleObject(handle, timeout); 37 | if (wait == WAIT_TIMEOUT) 38 | { 39 | ::TerminateThread(handle, 0); 40 | } 41 | ::CloseHandle(handle); 42 | } 43 | }; 44 | 45 | template > class Container = std::list> 48 | class TaskExecutor 49 | { 50 | public: 51 | static const int DEFAULT_MAX_THREAD_COUNT = 5; 52 | static const int DEFAULT_KEEP_THREAD_ALIVE_TIME = 500; 53 | 54 | enum ThreadState 55 | { 56 | THREAD_CREATED, 57 | THREAD_RUNNING, 58 | THREAD_WAITING, 59 | THREAD_TERMINATED 60 | }; 61 | 62 | enum ThreadPoolState 63 | { 64 | THREAD_POOL_RUNNING, 65 | THREAD_POOL_STOP, 66 | THREAD_POOL_TERMINATED 67 | }; 68 | 69 | TaskExecutor(); 70 | TaskExecutor(int max_count); 71 | TaskExecutor(int max_count, int keep_alive_time); 72 | ~TaskExecutor(); 73 | 74 | bool Execute(Task* task); 75 | bool Cancel(Task* task); 76 | void Clear(); 77 | void Shutdown(DWORD timeout); 78 | bool IsIdle(); 79 | DWORD WaitIdle(DWORD timeout = INFINITE); 80 | 81 | protected: 82 | struct ThreadContext 83 | { 84 | TaskExecutor* executor; 85 | LONG id; 86 | DWORD state; 87 | HANDLE handler; 88 | }; 89 | 90 | void StopAllThread(DWORD timeout); 91 | 92 | void SetThreadPoolState(DWORD state); 93 | DWORD GetThreadPoolState(); 94 | 95 | Task* GetTask(); 96 | 97 | LONG GetNextThreadId(); 98 | void SetThreadState(LONG id, DWORD state); 99 | void StartNewThread(); 100 | void RemoveThread(LONG id); 101 | bool HasIdleThread(); 102 | 103 | static unsigned __stdcall TaskThread(void* param); 104 | void TaskThreadImpl(LONG id); 105 | 106 | private: 107 | int max_count_; 108 | int keep_alive_time_; 109 | LONG current_thread_id_; 110 | LONG state_; 111 | 112 | Mutex thread_mutex_; 113 | std::map thread_pool_; 114 | 115 | Event task_event_; 116 | Container task_list_; 117 | 118 | Event idle_event_; 119 | 120 | private: 121 | DISABLE_COPY_AND_ASSIGN(TaskExecutor) 122 | }; 123 | } 124 | 125 | #endif -------------------------------------------------------------------------------- /base/time_ticks.h: -------------------------------------------------------------------------------- 1 | #ifndef __base_time_ticks_h__ 2 | #define __base_time_ticks_h__ 3 | 4 | #include 5 | #include 6 | 7 | #include "base/singleton.h" 8 | 9 | namespace base 10 | { 11 | class TimeTicks 12 | { 13 | public: 14 | static const __int64 MILLISECONDS_PER_SECOND = 1000; 15 | static const __int64 MICROSECONDS_PER_MILLISECOND = 1000; 16 | static const __int64 MICROSECONDS_PER_SECOND = MICROSECONDS_PER_MILLISECOND * MILLISECONDS_PER_SECOND; 17 | 18 | TimeTicks() {} 19 | 20 | ~TimeTicks() {} 21 | 22 | static __int64 Now() 23 | { 24 | LARGE_INTEGER qfc; 25 | QueryPerformanceCounter(&qfc); 26 | 27 | __int64 frequency = Frequency(); 28 | 29 | qfc.QuadPart *= MICROSECONDS_PER_SECOND; 30 | qfc.QuadPart /= frequency; 31 | 32 | return qfc.QuadPart; 33 | } 34 | 35 | static __int64 Ticks() 36 | { 37 | LARGE_INTEGER qfc; 38 | QueryPerformanceCounter(&qfc); 39 | 40 | return qfc.QuadPart; 41 | } 42 | 43 | static __int64 Frequency() 44 | { 45 | LARGE_INTEGER frequency; 46 | QueryPerformanceFrequency(&frequency); 47 | 48 | return frequency.QuadPart; 49 | } 50 | 51 | static __int64 MicrosecondToMillisecond(__int64 microsecond) 52 | { 53 | double millis = ((double)microsecond / (double)MICROSECONDS_PER_MILLISECOND); 54 | millis = ceil(millis); 55 | return static_cast<__int64>(millis); 56 | } 57 | 58 | static __int64 MillisecondToMicrosecond(__int64 millisecond) 59 | { 60 | return static_cast<__int64>(millisecond * MICROSECONDS_PER_MILLISECOND); 61 | } 62 | }; 63 | } 64 | 65 | #endif -------------------------------------------------------------------------------- /base/tuple.h: -------------------------------------------------------------------------------- 1 | #ifndef __base_tuple_h__ 2 | #define __base_tuple_h__ 3 | 4 | namespace base 5 | { 6 | template 7 | struct TupleTraits 8 | { 9 | typedef P ValueType; 10 | typedef P& RefType; 11 | typedef const P& ParamType; 12 | }; 13 | 14 | template 15 | struct TupleTraits 16 | { 17 | typedef P ValueType; 18 | typedef P& RefType; 19 | typedef P& ParamType; 20 | }; 21 | 22 | 23 | 24 | struct Tuple0 {}; 25 | 26 | template 27 | struct Tuple1 28 | { 29 | public: 30 | typedef A TypeA; 31 | 32 | Tuple1() {} 33 | explicit Tuple1(typename TupleTraits::ParamType a) : a(a) {} 34 | 35 | A a; 36 | }; 37 | 38 | template 39 | struct Tuple2 40 | { 41 | public: 42 | typedef A TypeA; 43 | typedef B TypeB; 44 | 45 | Tuple2() {} 46 | Tuple2(typename TupleTraits::ParamType a, 47 | typename TupleTraits::ParamType b) 48 | : a(a), b(b) {} 49 | 50 | A a; 51 | B b; 52 | }; 53 | 54 | template 55 | struct Tuple3 56 | { 57 | public: 58 | typedef A TypeA; 59 | typedef B TypeB; 60 | typedef C TypeC; 61 | 62 | Tuple3() {} 63 | Tuple3(typename TupleTraits::ParamType a, 64 | typename TupleTraits::ParamType b, 65 | typename TupleTraits::ParamType c) 66 | : a(a), b(b), c(c){} 67 | 68 | A a; 69 | B b; 70 | C c; 71 | }; 72 | 73 | template 75 | struct Tuple4 76 | { 77 | public: 78 | typedef A TypeA; 79 | typedef B TypeB; 80 | typedef C TypeC; 81 | typedef D TypeD; 82 | 83 | Tuple4() {} 84 | Tuple4(typename TupleTraits::ParamType a, 85 | typename TupleTraits::ParamType b, 86 | typename TupleTraits::ParamType c, 87 | typename TupleTraits::ParamType d) 88 | : a(a), b(b), c(c), d(d) {} 89 | 90 | A a; 91 | B b; 92 | C c; 93 | D d; 94 | }; 95 | 96 | template 98 | struct Tuple5 99 | { 100 | public: 101 | typedef A TypeA; 102 | typedef B TypeB; 103 | typedef C TypeC; 104 | typedef D TypeD; 105 | typedef E TypeE; 106 | 107 | Tuple5() {} 108 | Tuple5(typename TupleTraits::ParamType a, 109 | typename TupleTraits::ParamType b, 110 | typename TupleTraits::ParamType c, 111 | typename TupleTraits::ParamType d, 112 | typename TupleTraits::ParamType e) 113 | : a(a), b(b), c(c), d(d), e(e) {} 114 | 115 | A a; 116 | B b; 117 | C c; 118 | D d; 119 | E e; 120 | }; 121 | 122 | template 124 | struct Tuple6 125 | { 126 | public: 127 | typedef A TypeA; 128 | typedef B TypeB; 129 | typedef C TypeC; 130 | typedef D TypeD; 131 | typedef E TypeE; 132 | typedef F TypeF; 133 | 134 | Tuple6() {} 135 | Tuple6(typename TupleTraits::ParamType a, 136 | typename TupleTraits::ParamType b, 137 | typename TupleTraits::ParamType c, 138 | typename TupleTraits::ParamType d, 139 | typename TupleTraits::ParamType e, 140 | typename TupleTraits::ParamType f) 141 | : a(a), b(b), c(c), d(d), e(e), f(f) {} 142 | 143 | A a; 144 | B b; 145 | C c; 146 | D d; 147 | E e; 148 | F f; 149 | }; 150 | 151 | template 154 | struct Tuple7 155 | { 156 | public: 157 | typedef A TypeA; 158 | typedef B TypeB; 159 | typedef C TypeC; 160 | typedef D TypeD; 161 | typedef E TypeE; 162 | typedef F TypeF; 163 | typedef G TypeG; 164 | 165 | Tuple7() {} 166 | Tuple7(typename TupleTraits::ParamType a, 167 | typename TupleTraits::ParamType b, 168 | typename TupleTraits::ParamType c, 169 | typename TupleTraits::ParamType d, 170 | typename TupleTraits::ParamType e, 171 | typename TupleTraits::ParamType f, 172 | typename TupleTraits::ParamType g) 173 | : a(a), b(b), c(c), d(d), e(e), f(f), g(g) {} 174 | 175 | A a; 176 | B b; 177 | C c; 178 | D d; 179 | E e; 180 | F f; 181 | G g; 182 | }; 183 | 184 | template 187 | struct Tuple8 188 | { 189 | public: 190 | typedef A TypeA; 191 | typedef B TypeB; 192 | typedef C TypeC; 193 | typedef D TypeD; 194 | typedef E TypeE; 195 | typedef F TypeF; 196 | typedef G TypeG; 197 | typedef H TypeH; 198 | 199 | Tuple8() {} 200 | Tuple8(typename TupleTraits::ParamType a, 201 | typename TupleTraits::ParamType b, 202 | typename TupleTraits::ParamType c, 203 | typename TupleTraits::ParamType d, 204 | typename TupleTraits::ParamType e, 205 | typename TupleTraits::ParamType f, 206 | typename TupleTraits::ParamType g, 207 | typename TupleTraits::ParamType h) 208 | : a(a), b(b), c(c), d(d), e(e), f(f), g(g), h(h) {} 209 | 210 | A a; 211 | B b; 212 | C c; 213 | D d; 214 | E e; 215 | F f; 216 | G g; 217 | H h; 218 | }; 219 | 220 | 221 | 222 | inline Tuple0 MakeTuple() 223 | { 224 | return Tuple0(); 225 | } 226 | 227 | template 228 | inline Tuple1 MakeTuple(const A& a) 229 | { 230 | return Tuple1(a); 231 | } 232 | 233 | template 234 | inline Tuple2 MakeTuple(const A& a, const B& b) 235 | { 236 | return Tuple2(a, b); 237 | } 238 | 239 | template 240 | inline Tuple3 MakeTuple(const A& a, const B& b, const C& c) 241 | { 242 | return Tuple3(a, b, c); 243 | } 244 | 245 | template 247 | inline Tuple4 MakeTuple(const A& a, const B& b, const C& c, 248 | const D& d) 249 | { 250 | return Tuple4(a, b, c, d); 251 | } 252 | 253 | template 255 | inline Tuple5 MakeTuple(const A& a, const B& b, const C& c, 256 | const D& d, const E& e) 257 | { 258 | return Tuple5(a, b, c, d, e); 259 | } 260 | 261 | template 263 | inline Tuple6 MakeTuple(const A& a, const B& b, const C& c, 264 | const D& d, const E& e, const F& f) 265 | { 266 | return Tuple6(a, b, c, d, e, f); 267 | } 268 | 269 | template 272 | inline Tuple7 MakeTuple(const A& a, const B& b, const C& c, 273 | const D& d, const E& e, const F& f, 274 | const G& g) 275 | { 276 | return Tuple7(a, b, c, d, e, f, g); 277 | } 278 | 279 | template 282 | inline Tuple8 MakeTuple(const A& a, const B& b, const C& c, 283 | const D& d, const E& e, const F& f, 284 | const G& g, const H& h) 285 | { 286 | return Tuple8(a, b, c, d, e, f, g, h); 287 | } 288 | } 289 | 290 | #endif -------------------------------------------------------------------------------- /base/type_list.h: -------------------------------------------------------------------------------- 1 | #ifndef __base_type_list_h__ 2 | #define __base_type_list_h__ 3 | 4 | template 5 | struct TypeList 6 | { 7 | typedef T Head; 8 | typedef U Tail; 9 | }; 10 | 11 | struct NullType 12 | { 13 | }; 14 | 15 | #define TYPELIST1(T1) TypeList 16 | #define TYPELIST2(T1, T2) TypeList 17 | #define TYPELIST3(T1, T2, T3) TypeList 18 | #define TYPELIST4(T1, T2, T3, T4) TypeList 19 | #define TYPELIST5(T1, T2, T3, T4, T5) TypeList 20 | #define TYPELIST6(T1, T2, T3, T4, T5, T6) TypeList 21 | #define TYPELIST7(T1, T2, T3, T4, T5, T6, T7) TypeList 22 | #define TYPELIST8(T1, T2, T3, T4, T5, T6, T7, T8) TypeList 23 | #define TYPELIST9(T1, T2, T3, T4, T5, T6, T7, T8, T9) TypeList 24 | #define TYPELIST10(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10) TypeList 25 | 26 | template 27 | struct TypeAt; 28 | 29 | template 30 | struct TypeAt, 0> 31 | { 32 | typedef Head Result; 33 | }; 34 | 35 | template 36 | struct TypeAt, index> 37 | { 38 | typedef typename TypeAt::Result Result; 39 | }; 40 | 41 | #endif -------------------------------------------------------------------------------- /bin/win32d/res/Thumbs.db: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/myz7656/smart-ui/5b4ac60c52e54f53b68ea11c5840336b4f2b159f/bin/win32d/res/Thumbs.db -------------------------------------------------------------------------------- /bin/win32d/res/btn_iknow.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/myz7656/smart-ui/5b4ac60c52e54f53b68ea11c5840336b4f2b159f/bin/win32d/res/btn_iknow.png -------------------------------------------------------------------------------- /bin/win32d/res/change_down.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/myz7656/smart-ui/5b4ac60c52e54f53b68ea11c5840336b4f2b159f/bin/win32d/res/change_down.png -------------------------------------------------------------------------------- /bin/win32d/res/change_hover.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/myz7656/smart-ui/5b4ac60c52e54f53b68ea11c5840336b4f2b159f/bin/win32d/res/change_hover.png -------------------------------------------------------------------------------- /bin/win32d/res/change_normal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/myz7656/smart-ui/5b4ac60c52e54f53b68ea11c5840336b4f2b159f/bin/win32d/res/change_normal.png -------------------------------------------------------------------------------- /bin/win32d/res/close_down.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/myz7656/smart-ui/5b4ac60c52e54f53b68ea11c5840336b4f2b159f/bin/win32d/res/close_down.png -------------------------------------------------------------------------------- /bin/win32d/res/close_hover.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/myz7656/smart-ui/5b4ac60c52e54f53b68ea11c5840336b4f2b159f/bin/win32d/res/close_hover.png -------------------------------------------------------------------------------- /bin/win32d/res/close_normal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/myz7656/smart-ui/5b4ac60c52e54f53b68ea11c5840336b4f2b159f/bin/win32d/res/close_normal.png -------------------------------------------------------------------------------- /bin/win32d/res/default_icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/myz7656/smart-ui/5b4ac60c52e54f53b68ea11c5840336b4f2b159f/bin/win32d/res/default_icon.png -------------------------------------------------------------------------------- /bin/win32d/res/folder.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/myz7656/smart-ui/5b4ac60c52e54f53b68ea11c5840336b4f2b159f/bin/win32d/res/folder.png -------------------------------------------------------------------------------- /bin/win32d/res/icon_cloud.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/myz7656/smart-ui/5b4ac60c52e54f53b68ea11c5840336b4f2b159f/bin/win32d/res/icon_cloud.png -------------------------------------------------------------------------------- /bin/win32d/res/icon_exit.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/myz7656/smart-ui/5b4ac60c52e54f53b68ea11c5840336b4f2b159f/bin/win32d/res/icon_exit.png -------------------------------------------------------------------------------- /bin/win32d/res/icon_setting.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/myz7656/smart-ui/5b4ac60c52e54f53b68ea11c5840336b4f2b159f/bin/win32d/res/icon_setting.png -------------------------------------------------------------------------------- /bin/win32d/res/input.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/myz7656/smart-ui/5b4ac60c52e54f53b68ea11c5840336b4f2b159f/bin/win32d/res/input.png -------------------------------------------------------------------------------- /bin/win32d/res/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/myz7656/smart-ui/5b4ac60c52e54f53b68ea11c5840336b4f2b159f/bin/win32d/res/logo.png -------------------------------------------------------------------------------- /bin/win32d/res/logout.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/myz7656/smart-ui/5b4ac60c52e54f53b68ea11c5840336b4f2b159f/bin/win32d/res/logout.png -------------------------------------------------------------------------------- /bin/win32d/res/menu.xml: -------------------------------------------------------------------------------- 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 | 4.1G 47 |
48 |
49 | 50 | 51 | -------------------------------------------------------------------------------- /bin/win32d/res/menu_bottom_middle.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/myz7656/smart-ui/5b4ac60c52e54f53b68ea11c5840336b4f2b159f/bin/win32d/res/menu_bottom_middle.png -------------------------------------------------------------------------------- /bin/win32d/res/min_down.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/myz7656/smart-ui/5b4ac60c52e54f53b68ea11c5840336b4f2b159f/bin/win32d/res/min_down.png -------------------------------------------------------------------------------- /bin/win32d/res/min_hover.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/myz7656/smart-ui/5b4ac60c52e54f53b68ea11c5840336b4f2b159f/bin/win32d/res/min_hover.png -------------------------------------------------------------------------------- /bin/win32d/res/min_normal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/myz7656/smart-ui/5b4ac60c52e54f53b68ea11c5840336b4f2b159f/bin/win32d/res/min_normal.png -------------------------------------------------------------------------------- /bin/win32d/res/settings.xml: -------------------------------------------------------------------------------- 1 |  2 | 3 | 32 | 33 | 34 |
35 | 设置存储目录122 36 | 33设置存储目录 37 |
38 | 39 |
40 |
41 | 42 |
43 | 44 |
45 |
46 | 47 |
48 | 49 |
50 |
52 | 更换 53 |
54 |
55 |
58 |
59 |
60 |
61 |
62 |
63 |
64 | 65 |
66 |
67 |
68 |
69 | 70 |
71 |
72 |
73 | 74 | 75 | -------------------------------------------------------------------------------- /core/const.h: -------------------------------------------------------------------------------- 1 | #ifndef __core_const_h__ 2 | #define __core_const_h__ 3 | 4 | namespace core 5 | { 6 | enum 7 | { 8 | ATTRIBUTE_TRAIT_NULL = 0x00000000, 9 | ATTRIBUTE_TRAIT_LAYOUT = 0x00000001, 10 | ATTRIBUTE_TRAIT_PAINT = 0x00000002, 11 | ATTRIBUTE_TRAIT_BOTH = ATTRIBUTE_TRAIT_LAYOUT | ATTRIBUTE_TRAIT_PAINT 12 | }; 13 | 14 | enum 15 | { 16 | BACKGROUND_REPEAT_NULL = 0x00000000, 17 | BACKGROUND_REPEAT_X = 0x00000001, 18 | BACKGROUND_REPEAT_Y = 0x00000002 19 | }; 20 | 21 | enum 22 | { 23 | POSITION_ABSOLUTE = 0x00000001, 24 | POSITION_RELATIVE = 0x00000002 25 | }; 26 | 27 | enum 28 | { 29 | ALIGNMENT_NULL = 0x00000000, 30 | 31 | HORIZONTAL_ALIGNMENT_LEFT = 0x00000001, 32 | HORIZONTAL_ALIGNMENT_CENTER = 0x00000002, 33 | HORIZONTAL_ALIGNMENT_RIGHT = 0x00000004, 34 | 35 | VERTICAL_ALIGNMENT_TOP = 0x00000010, 36 | VERTICAL_ALIGNMENT_MIDDLE = 0x00000020, 37 | VERTICAL_ALIGNMENT_BOTTOM = 0x00000040 38 | }; 39 | 40 | enum 41 | { 42 | FONT_STYLE_NORMAL = 0x00000000, 43 | FONT_STYLE_BOLD = 0x00000001, 44 | FONT_STYLE_ITALIC = 0x00000002, 45 | FONT_STYLE_BOLD_ITALIC = 0x00000003, 46 | FONT_STYLE_UNDERLINE = 0x00000004 47 | }; 48 | 49 | enum 50 | { 51 | BORDER_STYLE_SOLID = 0x00000001, 52 | BORDER_STYLE_DASHED = 0x00000002, 53 | BORDER_STYLE_DOTTED = 0x00000004 54 | }; 55 | 56 | enum 57 | { 58 | POSITION_TYPE_LEFT_TOP = 0x00000001, 59 | POSITION_TYPE_LEFT_BOTTOM = 0x00000002, 60 | POSITION_TYPE_RIGHT_TOP = 0x00000004, 61 | POSITION_TYPE_RIGHT_BOTTOM = 0x00000008 62 | }; 63 | 64 | enum 65 | { 66 | CURSOR_TYPE_ARROW = 0x00000001, 67 | CURSOR_TYPE_HAND = 0x00000002 68 | }; 69 | 70 | enum 71 | { 72 | BOX_MODE_CLASSIC = 0x00000001, 73 | BOX_MODE_MODERN = 0x00000002 74 | }; 75 | 76 | enum 77 | { 78 | ACTION_STATE_NORMAL = 0x00000001, 79 | ACTION_STATE_HOVER = 0x00000002, 80 | ACTION_STATE_DOWN = 0x00000004 81 | }; 82 | 83 | enum 84 | { 85 | SCROLL_BAR_HORIZONTAL = 0x00000001, 86 | SCROLL_BAR_VERTICAL = 0x00000002 87 | }; 88 | } 89 | 90 | 91 | #endif -------------------------------------------------------------------------------- /core/container.h: -------------------------------------------------------------------------------- 1 | #ifndef __core_container_h__ 2 | #define __core_container_h__ 3 | 4 | #include "core/widget.h" 5 | 6 | namespace core 7 | { 8 | class StContainer : public StWidget 9 | { 10 | public: 11 | StContainer(); 12 | ~StContainer(); 13 | 14 | virtual StObject* Clone(); 15 | virtual void Drop(); 16 | 17 | virtual bool IsContainer(); 18 | virtual StWidget* HitTest(int x, int y); 19 | virtual void Move(int x, int y, bool onlyChild); 20 | virtual void SetContext(Context* context); 21 | virtual bool IsFilterWidget(const char* name); 22 | virtual bool IsScroll(); 23 | 24 | 25 | StWidget* FindWidget(const std::string& id); 26 | 27 | StWidget* FirstChild(); 28 | StWidget* LastChild(); 29 | StWidget* FirstChild(const std::string& id); 30 | StWidget* LastChild (const std::string& id); 31 | 32 | void LinkBegin(StWidget *add); 33 | void LinkEnd(StWidget *add); 34 | void InsertBefore(StWidget *add, StWidget *before); 35 | void InsertAfter(StWidget *add, StWidget *after); 36 | void Replace(StWidget *with, StWidget *replace); 37 | void Remove(StWidget *remove); 38 | StWidget* Accept(); 39 | void Clear(); 40 | 41 | protected: 42 | void CopyTo(StContainer* container); 43 | 44 | void DrawChild(StPainter *painter, RECT *clip); 45 | 46 | protected: 47 | BEGIN_MESSAGE_MAP() 48 | MESSAGE_HANDLE(ST_LAYOUT, OnLayout) 49 | MESSAGE_HANDLE(ST_PAINT, OnPaint) 50 | CHAIN_MESSAGE_MAP(StWidget) 51 | END_MESSAGE_MAP() 52 | 53 | LRESULT OnLayout(WPARAM, LPARAM, bool*); 54 | LRESULT OnPaint(WPARAM, LPARAM, bool*); 55 | 56 | protected: 57 | StWidget *first_; 58 | StWidget *last_; 59 | 60 | private: 61 | DISABLE_COPY_AND_ASSIGN(StContainer) 62 | }; 63 | } 64 | 65 | #endif -------------------------------------------------------------------------------- /core/context.cpp: -------------------------------------------------------------------------------- 1 | #include "core/context.h" 2 | 3 | #include "win/widget_root.h" 4 | 5 | namespace core 6 | { 7 | void Context::SetWidgetRoot(win::WidgetRoot* widget_root) 8 | { 9 | widget_root_ = widget_root; 10 | } 11 | 12 | void Context::SetHWND(HWND hwnd) 13 | { 14 | hwnd_ = hwnd; 15 | } 16 | 17 | HWND Context::GetHWND() 18 | { 19 | return hwnd_; 20 | } 21 | 22 | void Context::RemovedWidget(StWidget* widget) 23 | { 24 | if (widget_root_) 25 | { 26 | StWidget* active = widget_root_->Active(); 27 | if (active == widget) 28 | { 29 | widget_root_->SetActive(0); 30 | } 31 | } 32 | } 33 | } -------------------------------------------------------------------------------- /core/context.h: -------------------------------------------------------------------------------- 1 | #ifndef __core_context_h__ 2 | #define __core_context_h__ 3 | 4 | #include 5 | 6 | namespace win 7 | { 8 | class WidgetRoot; 9 | } 10 | 11 | namespace core 12 | { 13 | class StWidget; 14 | class Context 15 | { 16 | public: 17 | Context() 18 | : widget_root_(0) {} 19 | 20 | ~Context() {} 21 | 22 | void SetWidgetRoot(win::WidgetRoot* widget_root); 23 | 24 | void SetHWND(HWND hwnd); 25 | 26 | HWND GetHWND(); 27 | 28 | void RemovedWidget(StWidget* widget); 29 | 30 | private: 31 | HWND hwnd_; 32 | win::WidgetRoot* widget_root_; 33 | }; 34 | } 35 | 36 | #endif -------------------------------------------------------------------------------- /core/controls/break.cpp: -------------------------------------------------------------------------------- 1 | #include "core/controls/break.h" 2 | 3 | namespace core 4 | { 5 | IMPLY_DYNAMIC_CREATE(StBreak, "br") 6 | 7 | 8 | 9 | StBreak::StBreak() {} 10 | 11 | StBreak::~StBreak() {} 12 | 13 | LRESULT StBreak::OnLayout(WPARAM wparam, LPARAM lparam, bool*) 14 | { 15 | LayoutParam* layout_param = (LayoutParam*)wparam; 16 | if (!layout_param) 17 | { 18 | return FALSE; 19 | } 20 | 21 | layout_param->col = layout_param->content.left; 22 | layout_param->row = layout_param->baseline; 23 | return TRUE; 24 | } 25 | 26 | LRESULT StBreak::OnAdjust(WPARAM wparam, LPARAM, bool*) 27 | { 28 | AdjustParam *just = (AdjustParam*)wparam; 29 | if (!just) 30 | { 31 | return FALSE; 32 | } 33 | 34 | just->vertical = true; 35 | 36 | return TRUE; 37 | } 38 | }; -------------------------------------------------------------------------------- /core/controls/break.h: -------------------------------------------------------------------------------- 1 | #ifndef __core_controls_break_h__ 2 | #define __core_controls_break_h__ 3 | 4 | #include "core/def.h" 5 | #include "core/widget.h" 6 | #include 7 | 8 | namespace core 9 | { 10 | class StBreak : public StWidget 11 | { 12 | public: 13 | DECLARE_DYNAMIC_CREATE(StBreak); 14 | 15 | StBreak(); 16 | ~StBreak(); 17 | 18 | protected: 19 | BEGIN_MESSAGE_MAP() 20 | MESSAGE_HANDLE(ST_LAYOUT, OnLayout) 21 | MESSAGE_HANDLE(ST_ADJUST, OnAdjust) 22 | CHAIN_MESSAGE_MAP(StWidget) 23 | END_MESSAGE_MAP() 24 | 25 | LRESULT OnLayout(WPARAM, LPARAM, bool*); 26 | LRESULT OnAdjust(WPARAM, LPARAM, bool*); 27 | 28 | private: 29 | DISABLE_COPY_AND_ASSIGN(StBreak) 30 | }; 31 | }; 32 | #endif -------------------------------------------------------------------------------- /core/controls/button.cpp: -------------------------------------------------------------------------------- 1 | #include "core/controls/button.h" 2 | 3 | namespace core 4 | { 5 | IMPLY_DYNAMIC_CREATE(StGridButton, "grid-button"); 6 | 7 | 8 | 9 | StGridButton::StGridButton() {} 10 | 11 | StGridButton::~StGridButton() {} 12 | 13 | LRESULT StGridButton::OnMouseHover(WPARAM wparam, LPARAM lparam, bool* handled) 14 | { 15 | AddAttribute(FRAME_IMAGE_WIDGET_PROPERTY_INDEX, (int)BS_INDEX_HOVER); 16 | return StFrameGridImage::OnMouseHover(wparam, lparam, handled); 17 | } 18 | 19 | LRESULT StGridButton::OnMouseLeave(WPARAM wparam, LPARAM lparam, bool* handled) 20 | { 21 | AddAttribute(FRAME_IMAGE_WIDGET_PROPERTY_INDEX, (int)BS_INDEX_NORMAL); 22 | return StFrameGridImage::OnMouseLeave(wparam, lparam, handled); 23 | } 24 | 25 | LRESULT StGridButton::OnLButtonDown(WPARAM wparam, LPARAM lparam, bool* handled) 26 | { 27 | AddAttribute(FRAME_IMAGE_WIDGET_PROPERTY_INDEX, (int)BS_INDEX_DOWN); 28 | return StFrameGridImage::OnLButtonDown(wparam, lparam, handled); 29 | } 30 | 31 | LRESULT StGridButton::OnLButtonUp(WPARAM wparam, LPARAM lparam, bool* handled) 32 | { 33 | AddAttribute(FRAME_IMAGE_WIDGET_PROPERTY_INDEX, (int)BS_INDEX_HOVER); 34 | return StFrameGridImage::OnLButtonUp(wparam, lparam, handled); 35 | } 36 | }; -------------------------------------------------------------------------------- /core/controls/button.h: -------------------------------------------------------------------------------- 1 | #ifndef __core_controls_button_h__ 2 | #define __core_controls_button_h__ 3 | 4 | #include "core/controls/img.h" 5 | 6 | namespace core 7 | { 8 | class StGridButton : public StFrameGridImage 9 | { 10 | public: 11 | DECLARE_DYNAMIC_CREATE(StGridButton) 12 | 13 | enum {BS_INDEX_NORMAL = 0, BS_INDEX_HOVER = 1, BS_INDEX_DOWN = 2}; 14 | 15 | StGridButton(); 16 | ~StGridButton(); 17 | 18 | protected: 19 | BEGIN_MESSAGE_MAP() 20 | MESSAGE_HANDLE(ST_MOUSEHOVER, OnMouseHover ) 21 | MESSAGE_HANDLE(ST_MOUSELEAVE, OnMouseLeave ) 22 | MESSAGE_HANDLE(ST_LBUTTONDOWN, OnLButtonDown) 23 | MESSAGE_HANDLE(ST_LBUTTONUP, OnLButtonUp ) 24 | CHAIN_MESSAGE_MAP(StFrameGridImage) 25 | END_MESSAGE_MAP() 26 | 27 | LRESULT OnMouseHover (WPARAM, LPARAM, bool*); 28 | LRESULT OnMouseLeave (WPARAM, LPARAM, bool*); 29 | LRESULT OnLButtonDown(WPARAM, LPARAM, bool*); 30 | LRESULT OnLButtonUp (WPARAM, LPARAM, bool*); 31 | 32 | private: 33 | DISABLE_COPY_AND_ASSIGN(StGridButton) 34 | }; 35 | }; 36 | #endif -------------------------------------------------------------------------------- /core/controls/div.cpp: -------------------------------------------------------------------------------- 1 | #include "core/controls/div.h" 2 | 3 | namespace core 4 | { 5 | IMPLY_DYNAMIC_CREATE(StDivision, "div") 6 | }; -------------------------------------------------------------------------------- /core/controls/div.h: -------------------------------------------------------------------------------- 1 | #ifndef __core_controls_div_h__ 2 | #define __core_controls_div_h__ 3 | 4 | #include "core/def.h" 5 | #include "core/container.h" 6 | 7 | #include 8 | 9 | namespace core 10 | { 11 | class StDivision : public StContainer 12 | { 13 | public: 14 | DECLARE_DYNAMIC_CREATE(StDivision); 15 | 16 | StDivision() {} 17 | ~StDivision() {} 18 | 19 | private: 20 | DISABLE_COPY_AND_ASSIGN(StDivision) 21 | }; 22 | }; 23 | #endif -------------------------------------------------------------------------------- /core/controls/hr.cpp: -------------------------------------------------------------------------------- 1 | #include "core/controls/hr.h" 2 | 3 | #include "win/gdi_painter.h" 4 | 5 | namespace core 6 | { 7 | IMPLY_DYNAMIC_CREATE(StHr, "hr") 8 | 9 | StHr::StHr() 10 | { 11 | SetFilterEvent(true); 12 | } 13 | 14 | StHr::~StHr() {} 15 | 16 | LRESULT StHr::OnPaint(WPARAM wparam, LPARAM lparam, bool*) 17 | { 18 | StPainter* painter = (StPainter*)wparam; 19 | RECT* clip = (RECT*)lparam; 20 | if (!painter || !clip) 21 | { 22 | return FALSE; 23 | } 24 | 25 | DrawLine(painter, clip); 26 | 27 | return TRUE; 28 | } 29 | 30 | void StHr::DrawLine(StPainter* painter, RECT* clip) 31 | { 32 | StStyle& style = GetStyle(ActionState()); 33 | 34 | int border_width = style.BorderWidth(); 35 | if (border_width > 0) 36 | { 37 | DWORD border_style = style.BorderStyle(); 38 | core::Color border_color = style.BorderColor(); 39 | int alpha = Alpha(); 40 | RECT rcContent = layout_rect_.content; 41 | POINT start, stop; 42 | start.x = rcContent.left; 43 | start.y = rcContent.top; 44 | stop.x = (rcContent.right - rcContent.left == border_width) ? start.x : rcContent.right; 45 | stop.y = (rcContent.bottom - rcContent.top == border_width) ? start.y : rcContent.bottom; 46 | painter->DrawLine(start, stop, border_width, 47 | border_color, border_style, alpha); 48 | } 49 | } 50 | 51 | StObject* StHr::Clone() 52 | { 53 | StHr* hr = new StHr; 54 | if (!hr) 55 | { 56 | return 0; 57 | } 58 | 59 | CopyTo(hr); 60 | return hr; 61 | } 62 | 63 | LRESULT StHr::OnCalculate(WPARAM wparam, LPARAM lparam, bool* handled) 64 | { 65 | if (!StWidget::OnCalculate(wparam, lparam, handled)) 66 | { 67 | return FALSE; 68 | } 69 | 70 | BoxParam* box_param = (BoxParam*)lparam; 71 | StStyle& style = LayoutStyle(); 72 | 73 | if (!style.HasAttribute(STYLE_PROPERTY_HEIGHT)) 74 | { 75 | box_param->height = style.BorderWidth(); 76 | } 77 | else if (!style.HasAttribute(STYLE_PROPERTY_WIDTH)) 78 | { 79 | box_param->width = style.BorderWidth(); 80 | } 81 | 82 | return TRUE; 83 | } 84 | } -------------------------------------------------------------------------------- /core/controls/hr.h: -------------------------------------------------------------------------------- 1 | #ifndef __core_controls_hr_h__ 2 | #define __core_controls_hr_h__ 3 | 4 | #include "core/def.h" 5 | #include "core/widget.h" 6 | 7 | #include 8 | 9 | namespace core 10 | { 11 | class StHr : public StWidget 12 | { 13 | public: 14 | DECLARE_DYNAMIC_CREATE(StHr); 15 | 16 | StHr(); 17 | ~StHr(); 18 | 19 | virtual StObject* Clone(); 20 | 21 | protected: 22 | BEGIN_MESSAGE_MAP() 23 | MESSAGE_HANDLE(ST_CALCULATE, OnCalculate) 24 | MESSAGE_HANDLE(ST_PAINT, OnPaint) 25 | CHAIN_MESSAGE_MAP(StWidget) 26 | END_MESSAGE_MAP() 27 | 28 | LRESULT OnCalculate(WPARAM, LPARAM, bool*); 29 | LRESULT OnPaint(WPARAM, LPARAM, bool*); 30 | 31 | void DrawLine(StPainter* painter, RECT* clip); 32 | 33 | private: 34 | DISABLE_COPY_AND_ASSIGN(StHr) 35 | }; 36 | }; 37 | #endif -------------------------------------------------------------------------------- /core/controls/img.h: -------------------------------------------------------------------------------- 1 | #ifndef __core_controls_img_h__ 2 | #define __core_controls_img_h__ 3 | 4 | #include "core/def.h" 5 | #include "core/container.h" 6 | 7 | #include 8 | 9 | namespace core 10 | { 11 | extern const char * IMAGE_WIDGET_PROPERTY_SOURCE; 12 | extern const char * GRID_IMAGE_WIDGET_PROPERTY_GRID; 13 | extern const char * GRID_IMAGE_WIDGET_PROPERTY_LEFT; 14 | extern const char * GRID_IMAGE_WIDGET_PROPERTY_TOP; 15 | extern const char * GRID_IMAGE_WIDGET_PROPERTY_RIGHT; 16 | extern const char * GRID_IMAGE_WIDGET_PROPERTY_BOTTOM; 17 | extern const char * FRAME_IMAGE_WIDGET_PROPERTY_LENGTH; 18 | extern const char * FRAME_IMAGE_WIDGET_PROPERTY_INDEX; 19 | 20 | 21 | 22 | class StImage : public StContainer 23 | { 24 | public: 25 | DECLARE_DYNAMIC_CREATE(StImage) 26 | 27 | StImage(); 28 | ~StImage(); 29 | 30 | virtual StObject* Clone(); 31 | 32 | std::wstring GetSource(); 33 | void SetSource(const std::wstring& file); 34 | 35 | protected: 36 | DECLARE_ATTRIBUTE_MAP() 37 | 38 | BEGIN_MESSAGE_MAP() 39 | MESSAGE_HANDLE(ST_CALCULATE, OnCalculate) 40 | MESSAGE_HANDLE(ST_PAINT, OnPaint) 41 | CHAIN_MESSAGE_MAP(StContainer) 42 | END_MESSAGE_MAP() 43 | 44 | LRESULT OnCalculate(WPARAM, LPARAM, bool*); 45 | LRESULT OnPaint(WPARAM, LPARAM, bool*); 46 | 47 | void CopyTo(StImage* image); 48 | 49 | private: 50 | DISABLE_COPY_AND_ASSIGN(StImage) 51 | }; 52 | 53 | 54 | 55 | class StGridImage : public StImage 56 | { 57 | public: 58 | DECLARE_DYNAMIC_CREATE(StGridImage) 59 | 60 | StGridImage(); 61 | ~StGridImage(); 62 | 63 | int GridLeft(); 64 | int GridTop(); 65 | int GridRight(); 66 | int GridBottom(); 67 | 68 | void SetGridLeft(int left); 69 | void SetGridTop(int top); 70 | void SetGridRight(int right); 71 | void SetGridBottom(int bottom); 72 | 73 | protected: 74 | DECLARE_ATTRIBUTE_MAP() 75 | 76 | BEGIN_MESSAGE_MAP() 77 | MESSAGE_HANDLE(ST_PAINT, OnPaint) 78 | CHAIN_MESSAGE_MAP(StImage) 79 | END_MESSAGE_MAP() 80 | 81 | LRESULT OnPaint(WPARAM, LPARAM, bool*); 82 | 83 | bool ParseGrid(const char * p); 84 | 85 | private: 86 | DISABLE_COPY_AND_ASSIGN(StGridImage) 87 | }; 88 | 89 | 90 | 91 | class StFrameGridImage : public StGridImage 92 | { 93 | public: 94 | DECLARE_DYNAMIC_CREATE(StFrameGridImage) 95 | 96 | StFrameGridImage(); 97 | ~StFrameGridImage(); 98 | 99 | int FrameLength(); 100 | void SetFrameLength(int length); 101 | 102 | int FrameIndex(); 103 | void SetFrameIndex(int index); 104 | 105 | protected: 106 | DECLARE_ATTRIBUTE_MAP() 107 | 108 | BEGIN_MESSAGE_MAP() 109 | MESSAGE_HANDLE(ST_CALCULATE, OnCalculate) 110 | MESSAGE_HANDLE(ST_PAINT, OnPaint) 111 | CHAIN_MESSAGE_MAP(StGridImage) 112 | END_MESSAGE_MAP() 113 | 114 | LRESULT OnCalculate(WPARAM, LPARAM, bool*); 115 | LRESULT OnPaint(WPARAM, LPARAM, bool*); 116 | 117 | private: 118 | DISABLE_COPY_AND_ASSIGN(StFrameGridImage) 119 | }; 120 | }; 121 | #endif -------------------------------------------------------------------------------- /core/controls/list.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/myz7656/smart-ui/5b4ac60c52e54f53b68ea11c5840336b4f2b159f/core/controls/list.h -------------------------------------------------------------------------------- /core/controls/panel.h: -------------------------------------------------------------------------------- 1 | #ifndef __core_panel_h__ 2 | #define __core_panel_h__ 3 | 4 | #include "core/def.h" 5 | #include "core/container.h" 6 | 7 | namespace core 8 | { 9 | extern const char * PANEL_WIDGET_PROPERTY_HSCROLL; 10 | extern const char * PANEL_WIDGET_PROPERTY_VSCROLL; 11 | extern const char * PANEL_WIDGET_PROPERTY_SCROLL_BACKGROUND; 12 | extern const char * PANEL_WIDGET_PROPERTY_SCROLL_BORDER; 13 | extern const char * PANEL_WIDGET_PROPERTY_SCROLL_WIDTH; 14 | 15 | 16 | class StPanel 17 | : public StContainer 18 | { 19 | public: 20 | DECLARE_DYNAMIC_CREATE(StPanel); 21 | 22 | const static int MIN_BAR_SIZE = 20; 23 | const static int DEFAULT_BAR_WIDTH = 10; 24 | 25 | enum Orientation 26 | { 27 | OR_NULL, 28 | OR_HORIZONTAL, 29 | OR_VERTICAL 30 | }; 31 | 32 | StPanel(); 33 | ~StPanel(); 34 | 35 | virtual StWidget* HitTest(int x, int y); 36 | 37 | bool IsHScrollEnable(); 38 | bool IsVScrollEnable(); 39 | core::Color ScrollBackground(); 40 | core::Color ScrollBorder(); 41 | 42 | void EnableHScroll(bool enable); 43 | void EnableVScroll(bool enable); 44 | void SetScrollBackground(core::Color color); 45 | void SetScrollBorder(core::Color color); 46 | 47 | void AddWidget(StWidget* widget); 48 | void RemoveWidget(StWidget* widget); 49 | 50 | protected: 51 | DECLARE_ATTRIBUTE_MAP() 52 | 53 | BEGIN_MESSAGE_MAP() 54 | MESSAGE_HANDLE(ST_LAYOUT, OnLayout) 55 | MESSAGE_HANDLE(ST_PAINT, OnPaint) 56 | MESSAGE_HANDLE(ST_MOUSEHOVER, OnMouseHover) 57 | MESSAGE_HANDLE(ST_MOUSELEAVE, OnMouseLeave) 58 | MESSAGE_HANDLE(ST_LBUTTONDOWN, OnLButtonDown) 59 | MESSAGE_HANDLE(ST_LBUTTONUP, OnLButtonUp) 60 | MESSAGE_HANDLE(ST_MOUSEWHEEL, OnMouseWheel) 61 | CHAIN_MESSAGE_MAP(StContainer) 62 | END_MESSAGE_MAP() 63 | 64 | LRESULT OnLayout(WPARAM, LPARAM, bool*); 65 | LRESULT OnPaint(WPARAM, LPARAM, bool*); 66 | LRESULT OnMouseHover(WPARAM, LPARAM, bool*); 67 | LRESULT OnMouseLeave(WPARAM, LPARAM, bool*); 68 | LRESULT OnLButtonDown(WPARAM, LPARAM, bool*); 69 | LRESULT OnLButtonUp(WPARAM, LPARAM, bool*); 70 | LRESULT OnMouseWheel(WPARAM, LPARAM, bool*); 71 | 72 | int GetTotalWidth(); 73 | int GetTotalHeight(); 74 | int GetRealWidth(); 75 | int GetRealHeight(); 76 | 77 | bool IsPtInHScroll(int x, int y); 78 | bool IsPtInVScroll(int x, int y); 79 | 80 | void UpdateScrollBar(); 81 | void ScrollView(); 82 | 83 | void DrawScroll(StPainter* painter); 84 | RECT GetVScrollRect(); 85 | RECT GetHScrollRect(); 86 | 87 | protected: 88 | int scroll_pos_x_; 89 | int scroll_pos_y_; 90 | 91 | int bar_pos_x_; 92 | int bar_pos_y_; 93 | int bar_size_x_; 94 | int bar_size_y_; 95 | 96 | Orientation orientation_; 97 | int cursor_pos_x_; 98 | int cursor_pos_y_; 99 | 100 | private: 101 | DISABLE_COPY_AND_ASSIGN(StPanel) 102 | }; 103 | } 104 | 105 | #endif -------------------------------------------------------------------------------- /core/controls/realwnd.cpp: -------------------------------------------------------------------------------- 1 | #include "core/controls/realwnd.h" 2 | #include "win/gdi_painter.h" 3 | 4 | namespace core 5 | { 6 | IMPLY_DYNAMIC_CREATE(StRealWnd, "realwnd") 7 | 8 | StRealWnd::StRealWnd() 9 | : hwnd_(NULL) {} 10 | 11 | StRealWnd::~StRealWnd() {} 12 | 13 | LRESULT StRealWnd::OnPaint(WPARAM wparam, LPARAM lparam, bool*) 14 | { 15 | StPainter* painter = (StPainter*)wparam; 16 | RECT* clip = (RECT*)lparam; 17 | if (!painter || !clip) 18 | { 19 | return FALSE; 20 | } 21 | 22 | return TRUE; 23 | } 24 | 25 | 26 | LRESULT StRealWnd::OnLayout(WPARAM wparam, LPARAM lparam, bool* handled) 27 | { 28 | if (!StWidget::OnLayout(wparam, lparam, handled)) 29 | { 30 | return FALSE; 31 | } 32 | 33 | if (GetHWnd() == NULL) 34 | { 35 | return TRUE; 36 | } 37 | 38 | RECT rc = layout_rect_.content; 39 | ::MoveWindow(GetHWnd(), rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, TRUE); 40 | 41 | return TRUE; 42 | } 43 | 44 | void StRealWnd::SetHWnd(HWND hWnd) 45 | { 46 | hwnd_ = hWnd; 47 | } 48 | 49 | HWND StRealWnd::GetHWnd() 50 | { 51 | return hwnd_; 52 | } 53 | 54 | LRESULT StRealWnd::OnShow(WPARAM wparam, LPARAM, bool*) 55 | { 56 | bool visible = bool(wparam); 57 | ShowWindow(GetHWnd(), visible ? SW_SHOW : SW_HIDE); 58 | return TRUE; 59 | } 60 | 61 | void StRealWnd::Move(int x, int y, bool onlyChild) 62 | { 63 | StWidget::Move(x, y, onlyChild); 64 | 65 | if (x != 0 || y != 0) 66 | { 67 | RECT rc = layout_rect_.content; 68 | ::MoveWindow(GetHWnd(), rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, TRUE); 69 | } 70 | } 71 | 72 | } -------------------------------------------------------------------------------- /core/controls/realwnd.h: -------------------------------------------------------------------------------- 1 | #ifndef __core_controls_realwnd_h__ 2 | #define __core_controls_realwnd_h__ 3 | 4 | #include "core/def.h" 5 | #include "core/widget.h" 6 | 7 | #include 8 | 9 | namespace core 10 | { 11 | class StRealWnd : public StWidget 12 | { 13 | public: 14 | DECLARE_DYNAMIC_CREATE(StRealWnd); 15 | 16 | StRealWnd(); 17 | ~StRealWnd(); 18 | 19 | void SetHWnd(HWND hWnd); 20 | HWND GetHWnd(); 21 | 22 | virtual void Move(int x, int y, bool onlyChild); 23 | 24 | protected: 25 | BEGIN_MESSAGE_MAP() 26 | MESSAGE_HANDLE(ST_LAYOUT, OnLayout) 27 | MESSAGE_HANDLE(ST_PAINT, OnPaint) 28 | MESSAGE_HANDLE(ST_SHOW, OnShow) 29 | CHAIN_MESSAGE_MAP(StWidget) 30 | END_MESSAGE_MAP() 31 | 32 | LRESULT OnLayout(WPARAM, LPARAM, bool*); 33 | LRESULT OnPaint(WPARAM, LPARAM, bool*); 34 | LRESULT OnShow(WPARAM, LPARAM, bool*); 35 | 36 | protected: 37 | HWND hwnd_; 38 | 39 | private: 40 | DISABLE_COPY_AND_ASSIGN(StRealWnd) 41 | }; 42 | }; 43 | #endif -------------------------------------------------------------------------------- /core/controls/span.h: -------------------------------------------------------------------------------- 1 | #ifndef __core_controls_span_h__ 2 | #define __core_controls_span_h__ 3 | 4 | #include "core/def.h" 5 | #include "core/widget.h" 6 | 7 | #include 8 | #include 9 | 10 | namespace core 11 | { 12 | extern const char* WIDGET_PROPERTY_INNER_TEXT; 13 | 14 | class StSpan : public StWidget 15 | { 16 | public: 17 | DECLARE_DYNAMIC_CREATE(StSpan); 18 | 19 | StSpan(); 20 | ~StSpan(); 21 | 22 | virtual StObject* Clone(); 23 | virtual StWidget* HitTest(int x, int y); 24 | virtual void Move(int x, int y, bool onlyChild); 25 | 26 | std::wstring InnerText(); 27 | void SetInnerText(const std::wstring& text); 28 | 29 | protected: 30 | DECLARE_ATTRIBUTE_MAP() 31 | 32 | BEGIN_MESSAGE_MAP() 33 | MESSAGE_HANDLE(ST_LAYOUT, OnLayout) 34 | MESSAGE_HANDLE(ST_PAINT, OnPaint) 35 | CHAIN_MESSAGE_MAP(StWidget) 36 | END_MESSAGE_MAP() 37 | 38 | LRESULT OnLayout(WPARAM, LPARAM, bool*); 39 | LRESULT OnPaint(WPARAM, LPARAM, bool*); 40 | 41 | void LayoutLeft(LayoutParam* layout_param, BoxParam& box_param); 42 | void LayoutText(LayoutParam* layout_param); 43 | void LayoutRight(LayoutParam* layout_param, BoxParam& box_param); 44 | 45 | bool BeforeInlineLayout(StWidget& widget, LayoutParam& layout, BoxParam& box); 46 | bool AfterInlineLayout(StWidget& widget, LayoutParam& layout, BoxParam& box); 47 | 48 | void DrawInnerText(StPainter* painter, RECT* clip); 49 | 50 | void CopyTo(StSpan* span); 51 | 52 | protected: 53 | 54 | struct TextToken 55 | { 56 | RECTF rect; 57 | int begin; 58 | int end; 59 | 60 | TextToken() : begin(0), end(0) {} 61 | }; 62 | 63 | typedef std::vector VectorTokens; 64 | VectorTokens text_tokens_; 65 | 66 | private: 67 | DISABLE_COPY_AND_ASSIGN(StSpan) 68 | }; 69 | }; 70 | #endif -------------------------------------------------------------------------------- /core/def.h: -------------------------------------------------------------------------------- 1 | #ifndef __core_def_h__ 2 | #define __core_def_h__ 3 | 4 | #include 5 | 6 | namespace core 7 | { 8 | class StObject; 9 | } 10 | 11 | typedef core::StObject* (__stdcall *DYNAMIC_CREATE_FN)(const char*); 12 | struct RuntimeClass 13 | { 14 | static RuntimeClass* first_class_; 15 | static core::StObject* CreateObject(const char*); 16 | 17 | DYNAMIC_CREATE_FN pfn_create_object_; 18 | RuntimeClass* next_class_; 19 | }; 20 | 21 | struct CLASS_INIT 22 | { 23 | CLASS_INIT(RuntimeClass* new_class) 24 | { 25 | new_class->next_class_ = RuntimeClass::first_class_; 26 | RuntimeClass::first_class_ = new_class; 27 | } 28 | }; 29 | 30 | #define DECLARE_DYNAMIC_CREATE(class_name)\ 31 | public:\ 32 | static RuntimeClass class##class_name;\ 33 | static core::StObject*__stdcall CreateObject(const char*);\ 34 | static bool EqualTag(const std::string& tag); 35 | 36 | #define IMPLY_DYNAMIC_CREATE(class_name, tag_name)\ 37 | RuntimeClass class_name::class##class_name =\ 38 | {\ 39 | class_name::CreateObject,\ 40 | 0\ 41 | };\ 42 | static CLASS_INIT _init_##class_name(&class_name::class##class_name);\ 43 | core::StObject* class_name::CreateObject(const char* value)\ 44 | {\ 45 | if (strcmp(value, tag_name) == 0)\ 46 | return new class_name;\ 47 | \ 48 | return 0;\ 49 | }\ 50 | bool class_name::EqualTag(const std::string& tag)\ 51 | {\ 52 | if (strcmp(tag.c_str(), tag_name) == 0) \ 53 | return true;\ 54 | \ 55 | return false;\ 56 | } 57 | 58 | 59 | #endif -------------------------------------------------------------------------------- /core/layout.h: -------------------------------------------------------------------------------- 1 | #ifndef __core_layout_h__ 2 | #define __core_layout_h__ 3 | 4 | #include "core/types.h" 5 | 6 | namespace core 7 | { 8 | class StWidget; 9 | class StStyle; 10 | 11 | 12 | 13 | class LayoutHelper 14 | { 15 | public: 16 | static DWORD GetPositionType(StStyle& style); 17 | static int GetBaseline(StWidget &widget, bool exclude); 18 | }; 19 | 20 | 21 | 22 | class ILayout 23 | { 24 | public: 25 | virtual bool CalculateBox(StStyle& style, RECT parent, BoxParam* box) = 0; 26 | 27 | virtual bool BeforeBlockLayout(StWidget& widget, LayoutParam& parent, BoxParam& box, LayoutRect* rect) = 0; 28 | virtual bool AfterBlockLayout (StWidget& widget, LayoutParam& self) = 0; 29 | }; 30 | 31 | 32 | 33 | class StreamLayout 34 | : public ILayout 35 | { 36 | public: 37 | bool CalculateBox(StStyle& style, RECT parent, BoxParam* box); 38 | 39 | bool BeforeBlockLayout(StWidget& widget, LayoutParam& parent, BoxParam& box, LayoutRect* rect); 40 | bool AfterBlockLayout (StWidget& widget, LayoutParam& self); 41 | 42 | protected: 43 | bool BeforeAbsoluteBlockLayout(StWidget& widget, LayoutParam& parent, BoxParam& box, LayoutRect* rect); 44 | bool AfterAbsoluteBlockLayout(StWidget& widget, LayoutParam& self); 45 | 46 | bool BeforeRelativeBlockLayout(StWidget& widget, LayoutParam& parent, BoxParam& box, LayoutRect* rect); 47 | bool AfterRelativeBlockLayout(StWidget& widget, LayoutParam& self); 48 | 49 | bool DoAlignment(StWidget& widget, LayoutParam& self); 50 | }; 51 | }; 52 | 53 | #endif -------------------------------------------------------------------------------- /core/object.cpp: -------------------------------------------------------------------------------- 1 | #include "core/def.h" 2 | #include "core/object.h" 3 | 4 | RuntimeClass* RuntimeClass::first_class_ = 0; 5 | 6 | core::StObject* RuntimeClass::CreateObject(const char* id) 7 | { 8 | if (!id || !*id) 9 | { 10 | return 0; 11 | } 12 | 13 | core::StObject* obj = 0; 14 | for (RuntimeClass* next = RuntimeClass::first_class_; next != 0; next = next->next_class_) 15 | { 16 | if (!next->pfn_create_object_) 17 | { 18 | continue; 19 | } 20 | 21 | obj = next->pfn_create_object_(id); 22 | if (obj) 23 | { 24 | break; 25 | } 26 | } 27 | 28 | return obj; 29 | } 30 | 31 | namespace core 32 | { 33 | StObject::~StObject() {} 34 | 35 | KeyAttribute StringToKeyAttribute(const std::string& id) 36 | { 37 | return id; 38 | } 39 | 40 | KeyWidget StringToKeyWidget(const std::string& id) 41 | { 42 | return id; 43 | } 44 | } -------------------------------------------------------------------------------- /core/object.h: -------------------------------------------------------------------------------- 1 | #ifndef __core_object_h__ 2 | #define __core_object_h__ 3 | 4 | #include 5 | 6 | namespace core 7 | { 8 | typedef std::string KeyAttribute; 9 | typedef std::string KeyWidget; 10 | 11 | KeyAttribute StringToKeyAttribute(const std::string& id); 12 | KeyWidget StringToKeyWidget (const std::string& id); 13 | 14 | inline bool KeyAttributeEquals(KeyAttribute src, KeyAttribute dest) 15 | { 16 | return strcmp(src.c_str(), dest.c_str()) == 0; 17 | } 18 | 19 | inline bool KeyWidgetEquals(KeyWidget src, KeyWidget dest) 20 | { 21 | return strcmp(src.c_str(), dest.c_str()) == 0; 22 | } 23 | 24 | class StObject 25 | { 26 | public: 27 | virtual ~StObject() = 0; 28 | virtual StObject* Clone() = 0; 29 | virtual void Drop() = 0; 30 | }; 31 | } 32 | 33 | #endif -------------------------------------------------------------------------------- /core/paint.cpp: -------------------------------------------------------------------------------- 1 | #include "core/paint.h" 2 | #include "core/widget.h" 3 | 4 | namespace core 5 | { 6 | StPainterImpl::StPainterImpl() 7 | : canvas_(0) {} 8 | 9 | StPainterImpl::~StPainterImpl() {} 10 | 11 | void StPainterImpl::Attach(StCanvas* canvas) 12 | { 13 | canvas_ = canvas; 14 | } 15 | 16 | StCanvas* StPainterImpl::Detach() 17 | { 18 | StCanvas* ret = canvas_; 19 | canvas_ = 0; 20 | return ret; 21 | } 22 | 23 | 24 | 25 | StPainter::StPainter() {} 26 | 27 | StPainter::~StPainter() {} 28 | 29 | int StPainter::SetClipRect( 30 | const RECT &rect) 31 | { 32 | StPainterImpl *impl = GetPainterImpl(); 33 | if (impl) 34 | { 35 | return impl->SetClipRect(rect); 36 | } 37 | return 0; 38 | } 39 | 40 | bool StPainter::ClearClipRect( 41 | int clip) 42 | { 43 | StPainterImpl *impl = GetPainterImpl(); 44 | if (impl) 45 | { 46 | return impl->ClearClipRect(clip); 47 | } 48 | 49 | return false; 50 | } 51 | 52 | void StPainter::DrawBorder( 53 | const RECT &rect, 54 | DWORD border_style, 55 | int border_width, 56 | core::Color border_color, 57 | int alpha) 58 | { 59 | StPainterImpl *impl = GetPainterImpl(); 60 | if (impl) 61 | { 62 | RECT rect_draw = rect; 63 | for (int i = 0; i < border_width; i++) 64 | { 65 | impl->DrawRectangle(rect_draw, border_style, border_color, alpha); 66 | rect_draw.left += 1; 67 | rect_draw.top += 1; 68 | rect_draw.right -= 1; 69 | rect_draw.bottom -= 1; 70 | } 71 | } 72 | } 73 | 74 | void StPainter::DrawBackground( 75 | const RECT &rect, 76 | core::Color background_color, 77 | int alpha) 78 | { 79 | StPainterImpl *impl = GetPainterImpl(); 80 | if (impl) 81 | { 82 | impl->FillRectangle(rect, background_color, alpha); 83 | } 84 | } 85 | 86 | void StPainter::CalculateImage( 87 | const std::wstring& file, 88 | int* width, 89 | int* height) 90 | { 91 | StPainterImpl* impl = GetPainterImpl(); 92 | if (impl) 93 | { 94 | impl->CalculateImage(file, width, height); 95 | } 96 | } 97 | 98 | void StPainter::DrawImage( 99 | const std::wstring &file, 100 | const RECT &rect, 101 | int alpha) 102 | { 103 | StPainterImpl *impl = GetPainterImpl(); 104 | if (impl) 105 | { 106 | impl->DrawImage(file, rect, alpha); 107 | } 108 | } 109 | 110 | void StPainter::DrawGridImage( 111 | const std::wstring &file, 112 | const RECT &rect, 113 | const RECT &grid, 114 | int alpha) 115 | { 116 | StPainterImpl *impl = GetPainterImpl(); 117 | if (impl) 118 | { 119 | impl->DrawFrameGridImage(file, rect, grid, 0, 0, alpha); 120 | } 121 | } 122 | 123 | void StPainter::DrawFrameImage( 124 | const std::wstring &file, 125 | const RECT &rect, 126 | int frame_length, 127 | int frame_index, 128 | int alpha) 129 | { 130 | StPainterImpl *impl = GetPainterImpl(); 131 | if (impl) 132 | { 133 | RECT grid = {0}; 134 | impl->DrawFrameGridImage(file, rect, grid, frame_length, frame_index, alpha); 135 | } 136 | } 137 | 138 | void StPainter::DrawFrameGridImage( 139 | const std::wstring &file, 140 | const RECT &rect, 141 | const RECT &grid, 142 | int frame_length, 143 | int frame_index, 144 | int alpha) 145 | { 146 | StPainterImpl *impl = GetPainterImpl(); 147 | if (impl) 148 | { 149 | impl->DrawFrameGridImage(file, rect, grid, frame_length, frame_index, alpha); 150 | } 151 | } 152 | 153 | void StPainter::DrawString( 154 | const std::wstring &text, 155 | const RECTF &rect, 156 | const std::wstring &family, 157 | const core::Unit &size, 158 | DWORD format, 159 | core::Color color, 160 | DWORD align, 161 | int alpha) 162 | { 163 | StPainterImpl *impl = GetPainterImpl(); 164 | if (impl) 165 | { 166 | impl->SetTextFont(family, size, format); 167 | impl->DrawString(text, rect, color, align, alpha); 168 | } 169 | } 170 | 171 | int StPainter::GetCharacterPosition( 172 | const std::wstring &text, 173 | float &width, 174 | const std::wstring &family, 175 | const core::Unit &size, 176 | DWORD format) 177 | { 178 | StPainterImpl *impl = GetPainterImpl(); 179 | if (impl) 180 | { 181 | impl->SetTextFont(family, size, format); 182 | return impl->GetCharacterPosition(text, width); 183 | } 184 | return 0; 185 | } 186 | 187 | void StPainter::DrawLine( 188 | POINT start, 189 | POINT stop, 190 | int nStrokeWidth, 191 | core::Color color, 192 | DWORD style, 193 | int alpha ) 194 | { 195 | StPainterImpl *impl = GetPainterImpl(); 196 | if (impl) 197 | { 198 | impl->DrawLine(start, stop, nStrokeWidth, color, style, alpha); 199 | } 200 | } 201 | 202 | void StPainter::DrawRoundBorder( const RECT& rect, int nRadiusWidth, int nRadiusHeight, DWORD styleBorder, int nBoderWidth, core::Color crBorder, int alpha ) 203 | { 204 | StPainterImpl *impl = GetPainterImpl(); 205 | if (impl) 206 | { 207 | impl->DrawRoundBorder(rect, nRadiusWidth, nRadiusHeight, styleBorder, nBoderWidth, crBorder, alpha); 208 | } 209 | } 210 | 211 | void StPainter::DrawRoundRect( const RECT& rect, int nRadiusWidth, int nRadiusHeight, core::Color crBackground, int alpha ) 212 | { 213 | StPainterImpl *impl = GetPainterImpl(); 214 | if (impl) 215 | { 216 | impl->DrawRoundRect(rect, nRadiusWidth, nRadiusHeight, crBackground, alpha); 217 | } 218 | } 219 | 220 | 221 | } -------------------------------------------------------------------------------- /core/paint.h: -------------------------------------------------------------------------------- 1 | #ifndef __core_paint_h__ 2 | #define __core_paint_h__ 3 | 4 | #include "base/def.h" 5 | #include "core/types.h" 6 | 7 | #include 8 | 9 | namespace core 10 | { 11 | class StCanvas 12 | { 13 | public: 14 | StCanvas() {} 15 | virtual ~StCanvas() {} 16 | }; 17 | 18 | 19 | 20 | class StPainterImpl 21 | { 22 | public: 23 | StPainterImpl(); 24 | virtual ~StPainterImpl(); 25 | 26 | void Attach(StCanvas* canvas); 27 | StCanvas* Detach(); 28 | 29 | virtual int SetClipRect( 30 | const RECT &rect) 31 | { 32 | return 0; 33 | } 34 | 35 | virtual bool ClearClipRect( 36 | int clip) 37 | { 38 | return false; 39 | } 40 | 41 | virtual void DrawRectangle( 42 | const RECT &rect, 43 | DWORD style, 44 | core::Color color, 45 | int alpha) {} 46 | 47 | virtual void FillRectangle( 48 | const RECT &rect, 49 | core::Color color, 50 | int alpha) {} 51 | 52 | virtual void CalculateImage( 53 | const std::wstring &file, 54 | int* width, 55 | int* height) {} 56 | 57 | virtual void DrawImage( 58 | const std::wstring &file, 59 | const RECT &rect, 60 | int alpha) {} 61 | 62 | virtual void DrawFrameGridImage( 63 | const std::wstring &file, 64 | const RECT &rect, 65 | const RECT &grid, 66 | int frame_length, 67 | int frame_index, 68 | int alpha) {} 69 | 70 | virtual void SetTextFont( 71 | const std::wstring &family, 72 | const core::Unit &size, 73 | DWORD format) {} 74 | 75 | virtual void DrawString( 76 | const std::wstring &text, 77 | const RECTF &rect, 78 | core::Color color, 79 | DWORD align, 80 | int alpha) {} 81 | 82 | virtual int GetCharacterPosition( 83 | const std::wstring &text, 84 | float &width) 85 | { 86 | return 0; 87 | } 88 | 89 | virtual void DrawLine( 90 | POINT start, 91 | POINT stop, 92 | int nStrokeWidth, 93 | core::Color color, 94 | DWORD style, 95 | int alpha) {} 96 | 97 | virtual void DrawRoundRect( 98 | const RECT& rect, 99 | int nRadiusWidth, 100 | int nRadiusHeight, 101 | core::Color crBackground, 102 | int alpha) {} 103 | 104 | virtual void DrawRoundBorder( 105 | const RECT& rect, 106 | int nRadiusWidth, 107 | int nRadiusHeight, 108 | DWORD styleBorder, 109 | int nBoderWidth, 110 | core::Color crBorder, 111 | int alpha) {} 112 | 113 | protected: 114 | StCanvas* canvas_; 115 | 116 | private: 117 | DISABLE_COPY_AND_ASSIGN(StPainterImpl) 118 | }; 119 | 120 | 121 | 122 | class StPainter 123 | { 124 | public: 125 | StPainter(); 126 | virtual ~StPainter(); 127 | 128 | virtual int SetClipRect( 129 | const RECT &rect); 130 | 131 | virtual bool ClearClipRect( 132 | int clip); 133 | 134 | virtual void DrawBorder( 135 | const RECT &rect, 136 | DWORD border_style, 137 | int border_width, 138 | core::Color border_color, 139 | int alpha); 140 | 141 | virtual void DrawBackground( 142 | const RECT &rect, 143 | core::Color background_color, 144 | int alpha); 145 | 146 | virtual void CalculateImage( 147 | const std::wstring& file, 148 | int* width, 149 | int* height); 150 | 151 | virtual void DrawImage( 152 | const std::wstring &file, 153 | const RECT &rect, 154 | int alpha); 155 | 156 | virtual void DrawGridImage( 157 | const std::wstring &file, 158 | const RECT &rect, 159 | const RECT &grid, 160 | int alpha); 161 | 162 | virtual void DrawFrameImage( 163 | const std::wstring &file, 164 | const RECT &rect, 165 | int frame_length, 166 | int frame_index, 167 | int alpha); 168 | 169 | virtual void DrawFrameGridImage( 170 | const std::wstring &file, 171 | const RECT &rect, 172 | const RECT &grid, 173 | int frame_length, 174 | int frame_index, 175 | int alpha); 176 | 177 | virtual void DrawString( 178 | const std::wstring &text, 179 | const RECTF &rect, 180 | const std::wstring &family, 181 | const core::Unit &size, 182 | DWORD format, 183 | core::Color color, 184 | DWORD align, 185 | int alpha); 186 | 187 | virtual int GetCharacterPosition( 188 | const std::wstring &text, 189 | float &width, 190 | const std::wstring &family, 191 | const core::Unit &size, 192 | DWORD format); 193 | 194 | virtual void DrawLine( 195 | POINT start, 196 | POINT stop, 197 | int nStrokeWidth, 198 | core::Color color, 199 | DWORD style, 200 | int alpha); 201 | 202 | virtual void DrawRoundBorder( 203 | const RECT& rect, 204 | int nRadiusWidth, 205 | int nRadiusHeight, 206 | DWORD styleBorder, 207 | int nBoderWidth, 208 | core::Color crBorder, 209 | int alpha); 210 | 211 | virtual void DrawRoundRect( 212 | const RECT& rect, 213 | int nRadiusWidth, 214 | int nRadiusHeight, 215 | core::Color crBackground, 216 | int alpha); 217 | 218 | protected: 219 | virtual StPainterImpl* GetPainterImpl() = 0; 220 | 221 | private: 222 | DISABLE_COPY_AND_ASSIGN(StPainter) 223 | }; 224 | } 225 | 226 | #endif -------------------------------------------------------------------------------- /core/style.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/myz7656/smart-ui/5b4ac60c52e54f53b68ea11c5840336b4f2b159f/core/style.cpp -------------------------------------------------------------------------------- /core/style.h: -------------------------------------------------------------------------------- 1 | #ifndef __core_style_h__ 2 | #define __core_style_h__ 3 | 4 | #include "base/def.h" 5 | 6 | #include "core/attribute.h" 7 | #include "core/const.h" 8 | 9 | 10 | namespace core 11 | { 12 | extern const char* STYLE_PROPERTY_BACKGROUND; 13 | extern const char* STYLE_PROPERTY_BACKGROUND_COLOR; 14 | extern const char* STYLE_PROPERTY_BACKGROUND_IMAGE; 15 | extern const char* STYLE_PROPERTY_BACKGROUND_REPEAT; 16 | extern const char* STYLE_PROPERTY_COLOR; 17 | extern const char* STYLE_PROPERTY_LINE_HEIGHT; 18 | extern const char* STYLE_PROPERTY_HORIZONTAL_ALIGN; 19 | extern const char* STYLE_PROPERTY_VERTICAL_ALIGN; 20 | extern const char* STYLE_PROPERTY_TEXT_ALIGN; 21 | extern const char* STYLE_PROPERTY_UNDERLINE; 22 | extern const char* STYLE_PROPERTY_TEXT_INDENT; 23 | extern const char* STYLE_PROPERTY_FONT; 24 | extern const char* STYLE_PROPERTY_FONT_FAMILY; 25 | extern const char* STYLE_PROPERTY_FONT_SIZE; 26 | extern const char* STYLE_PROPERTY_FONT_STYLE; 27 | extern const char* STYLE_PROPERTY_BORDER; 28 | extern const char* STYLE_PROPERTY_BORDER_WIDTH; 29 | extern const char* STYLE_PROPERTY_BORDER_STYLE; 30 | extern const char* STYLE_PROPERTY_BORDER_COLOR; 31 | extern const char* STYLE_PROPERTY_POSITION; 32 | extern const char* STYLE_PROPERTY_MARGIN; 33 | extern const char* STYLE_PROPERTY_MARGIN_LEFT; 34 | extern const char* STYLE_PROPERTY_MARGIN_TOP; 35 | extern const char* STYLE_PROPERTY_MARGIN_RIGHT; 36 | extern const char* STYLE_PROPERTY_MARGIN_BOTTOM; 37 | extern const char* STYLE_PROPERTY_PADDING; 38 | extern const char* STYLE_PROPERTY_PADDING_LEFT; 39 | extern const char* STYLE_PROPERTY_PADDING_TOP; 40 | extern const char* STYLE_PROPERTY_PADDING_RIGHT; 41 | extern const char* STYLE_PROPERTY_PADDING_BOTTOM; 42 | extern const char* STYLE_PROPERTY_LEFT; 43 | extern const char* STYLE_PROPERTY_TOP; 44 | extern const char* STYLE_PROPERTY_RIGHT; 45 | extern const char* STYLE_PROPERTY_BOTTOM; 46 | extern const char* STYLE_PROPERTY_WIDTH; 47 | extern const char* STYLE_PROPERTY_HEIGHT; 48 | extern const char* STYLE_PROPERTY_CURSOR; 49 | extern const char* STYLE_PROPERTY_BOX_MODE; 50 | extern const char* STYLE_PROPERTY_STROKE_WIDTH; 51 | extern const char* STYLE_PROPERTY_BORDER_RADIUS; 52 | extern const char* STYLE_PROPERTY_BORDER_RADIUS_WIDTH; 53 | extern const char* STYLE_PROPERTY_BORDER_RADIUS_HEIGHT; 54 | 55 | 56 | 57 | extern const wchar_t* DEFAULT_FONT_FAMILY; 58 | 59 | 60 | 61 | class StStyle : public StAttributeSet 62 | { 63 | public: 64 | DECLARE_ATTRIBUTE_MAP(); 65 | 66 | enum StyleType 67 | { 68 | STYLE_TAG, 69 | STYLE_CLASS, 70 | STYLE_IDENTIFIER 71 | }; 72 | 73 | StStyle(); 74 | StStyle(const StStyle&); 75 | ~StStyle(); 76 | StStyle& operator=(const StStyle&); 77 | 78 | StObject* Clone(); 79 | void Drop(); 80 | 81 | 82 | StyleType Type() const { return type_; } 83 | void SetType(StyleType type) { type_ = type; } 84 | 85 | const std::string& Name() const { return name_; } 86 | void SetName(const std::string &s) { name_ = s; } 87 | 88 | const DWORD State() const { return state_; } 89 | void SetState(DWORD state) { state_ = state; } 90 | 91 | 92 | core::Color BackgroundColor(); 93 | std::wstring BackgroundImage(); 94 | DWORD BackgroundRepeat(); 95 | core::Color TextColor(); 96 | int LineHeight(); 97 | DWORD HorizontalAlignment(); 98 | DWORD VerticalAlignment(); 99 | DWORD TextAlignment(); 100 | bool IsUnderline(); 101 | int TextIndent(); 102 | std::wstring FontFamily(); 103 | core::Unit FontSize(); 104 | DWORD FontStyle(); 105 | int BorderWidth(); 106 | DWORD BorderStyle(); 107 | core::Color BorderColor(); 108 | DWORD Position(); 109 | core::Unit MarginLeft(); 110 | core::Unit MarginTop(); 111 | core::Unit MarginRight(); 112 | core::Unit MarginBottom(); 113 | core::Unit PaddingLeft(); 114 | core::Unit PaddingTop(); 115 | core::Unit PaddingRight(); 116 | core::Unit PaddingBottom(); 117 | core::Unit Left(); 118 | core::Unit Top(); 119 | core::Unit Right(); 120 | core::Unit Bottom(); 121 | core::Unit Width(); 122 | core::Unit Height(); 123 | DWORD CursorType(); 124 | DWORD BoxMode(); 125 | int BorderRadiusWidth(); 126 | int BorderRadiusHeight(); 127 | 128 | void SetBackgroundColor(const core::Color&); 129 | void SetBackgroundImage(const std::wstring&); 130 | void SetBackgroundRepeat(DWORD); 131 | void SetTextColor(const core::Color&); 132 | void SetLineHeight(int); 133 | void SetHorizontalAlignment(DWORD); 134 | void SetVerticalAlignment(DWORD); 135 | void SetTextAlignment(DWORD); 136 | void SetUnderline(bool); 137 | void SetTextIndent(int); 138 | void SetFontFamily(const std::wstring&); 139 | void SetFontSize(const core::Unit&); 140 | void SetFontStyle(DWORD); 141 | void SetBorderWidth(int); 142 | void SetBorderStyle(DWORD); 143 | void SetBorderColor(const core::Color&); 144 | void SetPosition(DWORD); 145 | void SetMarginLeft(const core::Unit&); 146 | void SetMarginTop(const core::Unit&); 147 | void SetMarginRight(const core::Unit&); 148 | void SetMarginBottom(const core::Unit&); 149 | void SetPaddingLeft(const core::Unit&); 150 | void SetPaddingTop(const core::Unit&); 151 | void SetPaddingRight(const core::Unit&); 152 | void SetPaddingBottom(const core::Unit&); 153 | void SetLeft(const core::Unit&); 154 | void SetTop(const core::Unit&); 155 | void SetRight(const core::Unit&); 156 | void SetBottom(const core::Unit&); 157 | void SetWidth(const core::Unit&); 158 | void SetHeight(const core::Unit&); 159 | void SetCursor(DWORD); 160 | void SetBoxMode(DWORD); 161 | void SetBorderRadiusWidth(int); 162 | void SetBorderRadiusHeight(int); 163 | 164 | const char * ParseValue (const char * p); 165 | void ParseValues(const char * p); 166 | 167 | void Copy(const StStyle& style); 168 | void Inherit(const StStyle& style); 169 | 170 | private: 171 | bool ParseBackground(const char *p); 172 | bool ParseMargin(const char *p); 173 | bool ParsePadding(const char *p); 174 | bool ParseFont(const char *p); 175 | bool ParseBorder(const char *p); 176 | bool ParserBorderRadius(const char *p); 177 | 178 | private: 179 | StyleType type_; 180 | DWORD state_; 181 | std::string name_; 182 | }; 183 | } 184 | 185 | #endif -------------------------------------------------------------------------------- /core/types.h: -------------------------------------------------------------------------------- 1 | #ifndef __core_types_h__ 2 | #define __core_types_h__ 3 | 4 | #include 5 | #include 6 | 7 | namespace core 8 | { 9 | typedef Gdiplus::RectF RECTF; 10 | 11 | inline RECT RECTF2RECT(RECTF& rect_f) 12 | { 13 | RECT rect; 14 | rect.left = (int)(rect_f.GetLeft()); 15 | rect.top = (int)(rect_f.GetTop()); 16 | rect.right = (int)(rect_f.GetRight() + 0.9); 17 | rect.bottom = (int)(rect_f.GetBottom() + 0.9); 18 | 19 | return rect; 20 | } 21 | 22 | inline RECTF RECT2RECTF(RECT& rect) 23 | { 24 | return RECTF((float)rect.left, (float)rect.top, (float)(rect.right - rect.left), (float)(rect.bottom - rect.top)); 25 | } 26 | 27 | struct Color 28 | { 29 | union 30 | { 31 | unsigned long value; 32 | struct rgb 33 | { 34 | byte r; 35 | byte g; 36 | byte b; 37 | byte a; 38 | } rgb_value; 39 | }; 40 | 41 | Color() : value(0) {} 42 | 43 | Color(unsigned long value) : value(value) {} 44 | 45 | operator unsigned long() { return value; } 46 | }; 47 | 48 | 49 | 50 | struct Unit 51 | { 52 | int use; 53 | bool pixel; 54 | 55 | Unit() 56 | : use(0) 57 | , pixel(true) {} 58 | 59 | Unit(int u) 60 | : use(u) 61 | , pixel(true) {} 62 | 63 | Unit(int u, bool p) 64 | : use(u) 65 | , pixel(p) {} 66 | 67 | int operator()(int length) const 68 | { 69 | if (!pixel) 70 | { 71 | return use * length / 100; 72 | } 73 | return use; 74 | } 75 | 76 | Unit & operator=(int u) 77 | { 78 | use = u; 79 | pixel = true; 80 | return *this; 81 | } 82 | }; 83 | 84 | 85 | 86 | struct BoxParam 87 | { 88 | int left; 89 | int top; 90 | int right; 91 | int bottom; 92 | 93 | int margin_left; 94 | int margin_top; 95 | int margin_right; 96 | int margin_bottom; 97 | 98 | int border; 99 | 100 | int padding_left; 101 | int padding_top; 102 | int padding_right; 103 | int padding_bottom; 104 | 105 | int width; 106 | int height; 107 | 108 | BoxParam() 109 | : left(0) 110 | , top(0) 111 | , right(0) 112 | , bottom(0) 113 | , margin_left(0) 114 | , margin_top(0) 115 | , margin_right(0) 116 | , margin_bottom(0) 117 | , border(0) 118 | , padding_left(0) 119 | , padding_top(0) 120 | , padding_right(0) 121 | , padding_bottom(0) 122 | , width(0) 123 | , height(0) {} 124 | }; 125 | 126 | 127 | 128 | struct LayoutParam 129 | { 130 | bool leaf; 131 | RECT content; 132 | int row; 133 | int col; 134 | int baseline; 135 | RECT boundary; 136 | 137 | LayoutParam() 138 | : leaf(false) 139 | , row(0) 140 | , col(0) 141 | , baseline(0) 142 | { 143 | memset(&content, 0, sizeof(RECT)); 144 | memset(&boundary, 0, sizeof(RECT)); 145 | } 146 | 147 | void Offset(int x, int y) 148 | { 149 | OffsetRect(&content, x, y); 150 | OffsetRect(&boundary, x, y); 151 | col += x; 152 | row += y; 153 | baseline += y; 154 | } 155 | }; 156 | 157 | struct AdjustParam 158 | { 159 | int x; 160 | int y; 161 | bool vertical; 162 | 163 | AdjustParam() 164 | : x(0) 165 | , y(0) 166 | , vertical(false) {} 167 | }; 168 | 169 | 170 | 171 | struct LayoutRect 172 | { 173 | RECT boundary; 174 | RECT border; 175 | RECT content; 176 | 177 | LayoutRect() 178 | { 179 | Reset(); 180 | } 181 | 182 | void Reset() 183 | { 184 | memset(&boundary, 0, sizeof(RECT)); 185 | memset(&border, 0, sizeof(RECT)); 186 | memset(&content, 0, sizeof(RECT)); 187 | } 188 | }; 189 | } 190 | 191 | #endif -------------------------------------------------------------------------------- /css/css.cpp: -------------------------------------------------------------------------------- 1 | #include "css/css.h" 2 | 3 | #include "util/string.h" 4 | 5 | 6 | namespace css 7 | { 8 | CssDocument::CssDocument() {} 9 | 10 | CssDocument::~CssDocument() 11 | { 12 | Clear(); 13 | } 14 | 15 | bool CssDocument::Parse(const char* text) 16 | { 17 | if (util::String::IsEmpty(text)) 18 | { 19 | return false; 20 | } 21 | 22 | const unsigned char* p = (const unsigned char*)text; 23 | if (*(p+0) && *(p+0) == 0xefU && 24 | *(p+1) && *(p+1) == 0xbbU && 25 | *(p+2) && *(p+2) == 0xbfU) 26 | { 27 | text += 3; 28 | } 29 | 30 | std::wstring u_text = util::String::UTF8ToUnicode(text); 31 | return RunParser(u_text.c_str()); 32 | } 33 | 34 | bool CssDocument::RunParser(const wchar_t* text) 35 | { 36 | if (util::String::IsEmpty(text)) 37 | { 38 | return false; 39 | } 40 | const wchar_t* p = text; 41 | while (p && *p) 42 | { 43 | Pair pair; 44 | p = pair.selectors.Parse(p); 45 | 46 | if (!p || !*p || *p != L'{') 47 | { 48 | return false; 49 | } 50 | 51 | ++p; 52 | p = pair.declares.Parse(p); 53 | if (!p || !*p || *p != L'}') 54 | { 55 | return false; 56 | } 57 | 58 | if (pair.selectors.HasSelectors() && pair.declares.HasDeclares()) 59 | { 60 | pairs_.push_back(pair); 61 | } 62 | ++p; 63 | } 64 | return true; 65 | } 66 | 67 | bool CssDocument::FindSelectorByTag( 68 | const std::wstring& tag_name, 69 | const std::wstring& pseudo_class, 70 | DeclareSet& declares) 71 | { 72 | bool bRet = false; 73 | 74 | for (std::vector::iterator iter = pairs_.begin(); 75 | iter != pairs_.end(); ++iter) 76 | { 77 | if (iter->selectors.MatchTag(tag_name.c_str(), pseudo_class.c_str())) 78 | { 79 | declares.Push(iter->declares); 80 | bRet = true; 81 | } 82 | } 83 | 84 | return bRet; 85 | } 86 | 87 | bool CssDocument::FindSelectorByClass( 88 | const std::wstring& class_name, 89 | const std::wstring& tag_name, 90 | const std::wstring& pseudo_class, 91 | DeclareSet& declares) 92 | { 93 | bool bRet = false; 94 | 95 | for (std::vector::iterator iter = pairs_.begin(); 96 | iter != pairs_.end(); ++iter) 97 | { 98 | if (iter->selectors.MatchClass(class_name.c_str(), tag_name.c_str(), pseudo_class.c_str())) 99 | { 100 | declares.Push(iter->declares); 101 | bRet = true; 102 | } 103 | } 104 | 105 | return bRet; 106 | } 107 | 108 | bool CssDocument::FindSelectorById( 109 | const std::wstring& id, 110 | const std::wstring& tag_name, 111 | const std::wstring& pseudo_class, 112 | DeclareSet& declares) 113 | { 114 | bool bRet = false; 115 | 116 | for (std::vector::iterator iter = pairs_.begin(); 117 | iter != pairs_.end(); ++iter) 118 | { 119 | if (iter->selectors.MatchId(id.c_str(), tag_name.c_str(), pseudo_class.c_str())) 120 | { 121 | declares.Push(iter->declares); 122 | bRet = true; 123 | } 124 | } 125 | 126 | return bRet; 127 | } 128 | 129 | void CssDocument::Clear() 130 | { 131 | for (std::vector::iterator iter = pairs_.begin(); 132 | iter != pairs_.end(); ++iter) 133 | { 134 | iter->selectors.Clear(); 135 | } 136 | 137 | pairs_.clear(); 138 | } 139 | } -------------------------------------------------------------------------------- /css/css.h: -------------------------------------------------------------------------------- 1 | #ifndef __css_css_h__ 2 | #define __css_css_h__ 3 | 4 | #include "css/selector.h" 5 | #include "css/declare.h" 6 | 7 | namespace css 8 | { 9 | class CssDocument 10 | { 11 | public: 12 | struct Pair 13 | { 14 | SelectorSet selectors; 15 | DeclareSet declares; 16 | }; 17 | 18 | CssDocument(); 19 | ~CssDocument(); 20 | 21 | bool Parse(const char* text); 22 | void Clear(); 23 | 24 | bool FindSelectorByTag( 25 | const std::wstring& tag_name, 26 | const std::wstring& pseudo_class, 27 | DeclareSet& declares); 28 | 29 | bool FindSelectorByClass( 30 | const std::wstring& class_name, 31 | const std::wstring& tag_name, 32 | const std::wstring& pseudo_class, 33 | DeclareSet& declares); 34 | 35 | bool FindSelectorById( 36 | const std::wstring& id, 37 | const std::wstring& tag_name, 38 | const std::wstring& pseudo_class, 39 | DeclareSet& declares); 40 | 41 | private: 42 | bool RunParser(const wchar_t* text); 43 | 44 | private: 45 | std::vector pairs_; 46 | }; 47 | } 48 | 49 | #endif -------------------------------------------------------------------------------- /css/declare.cpp: -------------------------------------------------------------------------------- 1 | #include "css/declare.h" 2 | 3 | namespace css 4 | { 5 | Declare::Declare() {} 6 | 7 | Declare::~Declare() {} 8 | 9 | bool Declare::HasValue() 10 | { 11 | return !property_.empty() && 12 | !value_.empty(); 13 | } 14 | 15 | std::wstring Declare::Property() 16 | { 17 | return property_; 18 | } 19 | 20 | std::wstring Declare::Value() 21 | { 22 | return value_; 23 | } 24 | 25 | const wchar_t* Declare::Parse(const wchar_t* text) 26 | { 27 | bool value = false; 28 | const wchar_t* p = text; 29 | while (p && *p) 30 | { 31 | if (*p == L':') 32 | { 33 | ++p; 34 | value = true; 35 | continue; 36 | } 37 | else if (*p == L';') 38 | { 39 | break; 40 | } 41 | 42 | if (value) 43 | { 44 | value_ += *p; 45 | } 46 | else 47 | { 48 | property_ += *p; 49 | } 50 | ++p; 51 | } 52 | return p; 53 | } 54 | 55 | 56 | 57 | DeclareSet::DeclareSet() {} 58 | 59 | DeclareSet::~DeclareSet() {} 60 | 61 | bool DeclareSet::HasDeclares() 62 | { 63 | return declares_.size() > 0; 64 | } 65 | 66 | const wchar_t* DeclareSet::Parse(const wchar_t* text) 67 | { 68 | const wchar_t* p = text; 69 | while (p && *p) 70 | { 71 | if (iswspace(*p)) 72 | { 73 | ++p; 74 | continue; 75 | } 76 | 77 | if (*p == L'}') 78 | { 79 | break; 80 | } 81 | 82 | Declare declare; 83 | p = declare.Parse(p); 84 | if (!p || !*p || *p != L';') 85 | { 86 | break; 87 | } 88 | ++p; 89 | 90 | if (declare.HasValue()) 91 | { 92 | declares_.push_back(declare); 93 | } 94 | } 95 | return p; 96 | } 97 | 98 | std::vector& DeclareSet::Declares() 99 | { 100 | return declares_; 101 | } 102 | 103 | void DeclareSet::Clear() 104 | { 105 | declares_.clear(); 106 | } 107 | 108 | void DeclareSet::Push(DeclareSet& declare) 109 | { 110 | declares_.insert(declares_.end(), declare.Declares().begin(), declare.Declares().end()); 111 | } 112 | } -------------------------------------------------------------------------------- /css/declare.h: -------------------------------------------------------------------------------- 1 | #ifndef __css_declare_h__ 2 | #define __css_declare_h__ 3 | 4 | #include 5 | #include 6 | 7 | namespace css 8 | { 9 | class Declare 10 | { 11 | public: 12 | Declare(); 13 | ~Declare(); 14 | 15 | bool HasValue(); 16 | 17 | std::wstring Property(); 18 | std::wstring Value(); 19 | 20 | const wchar_t* Parse(const wchar_t* text); 21 | 22 | private: 23 | std::wstring property_; 24 | std::wstring value_; 25 | }; 26 | 27 | 28 | 29 | class DeclareSet 30 | { 31 | public: 32 | DeclareSet(); 33 | ~DeclareSet(); 34 | 35 | bool HasDeclares(); 36 | 37 | const wchar_t* Parse(const wchar_t* text); 38 | 39 | std::vector& Declares(); 40 | 41 | void Clear(); 42 | 43 | void Push(DeclareSet& declare); 44 | 45 | private: 46 | std::vector declares_; 47 | }; 48 | } 49 | 50 | #endif -------------------------------------------------------------------------------- /css/selector.h: -------------------------------------------------------------------------------- 1 | #ifndef __css_selector_h__ 2 | #define __css_selector_h__ 3 | 4 | #include 5 | #include 6 | 7 | #include "base/def.h" 8 | 9 | 10 | namespace css 11 | { 12 | class Selector 13 | { 14 | public: 15 | friend class SelectorDerived; 16 | enum Type 17 | { 18 | TYPE_ID, 19 | TYPE_CLASS, 20 | TYPE_TAG, 21 | }; 22 | 23 | Selector(); 24 | ~Selector(); 25 | 26 | Type GetType(); 27 | 28 | bool Result() { return result_; } 29 | 30 | const wchar_t* Parse(const wchar_t* text); 31 | 32 | bool MatchTag(const wchar_t* tag_name, const wchar_t* pseudo_class); 33 | bool MatchClass(const wchar_t* class_name, const wchar_t* tag_name, const wchar_t* pseudo_class); 34 | bool MatchId(const wchar_t* id_name, const wchar_t* tag_name, const wchar_t* pseudo_class); 35 | bool MatchPseudoClass( const wchar_t* pseudo_class ); 36 | 37 | private: 38 | const wchar_t* ParseTag(const wchar_t* text); 39 | const wchar_t* ParseClass(const wchar_t* text); 40 | const wchar_t* ParseId(const wchar_t* text); 41 | const wchar_t* ParsePseudoClass(const wchar_t* text); 42 | 43 | private: 44 | std::wstring tag_; 45 | std::wstring value_; 46 | std::wstring pseudo_class_; 47 | Type type_; 48 | bool result_; 49 | 50 | Selector* next_; 51 | Selector* prev_; 52 | 53 | private: 54 | DISABLE_COPY_AND_ASSIGN(Selector); 55 | }; 56 | 57 | 58 | 59 | class SelectorDerived 60 | { 61 | public: 62 | SelectorDerived(); 63 | ~SelectorDerived(); 64 | 65 | bool HasChild() { return first_ != 0; } 66 | 67 | const wchar_t* Parse(const wchar_t* text); 68 | 69 | bool MatchTag(const wchar_t* tag_name, const wchar_t* pseudo_class); 70 | bool MatchClass(const wchar_t* class_name, const wchar_t* tag_name, const wchar_t* pseudo_class); 71 | bool MatchId(const wchar_t* id_name, const wchar_t* tag_name, const wchar_t* pseudo_class); 72 | 73 | bool IsDescendantSelector(); 74 | void Clear(); 75 | 76 | private: 77 | const wchar_t* ParseSelector(const wchar_t* text); 78 | void LinkEnd(Selector* add); 79 | void LinkHead(Selector* add); 80 | 81 | friend class SelectorDerivedIterator; 82 | 83 | private: 84 | Selector* first_; 85 | Selector* last_; 86 | }; 87 | 88 | class SelectorSet 89 | { 90 | public: 91 | SelectorSet(); 92 | ~SelectorSet(); 93 | 94 | bool HasSelectors(); 95 | 96 | const wchar_t* Parse(const wchar_t* text); 97 | 98 | bool MatchTag(const wchar_t* tag_name, const wchar_t* pseudo_class); 99 | bool MatchClass(const wchar_t* class_name, const wchar_t* tag_name, const wchar_t* pseudo_class); 100 | bool MatchId(const wchar_t* id_name, const wchar_t* tag_name, const wchar_t* pseudo_class); 101 | 102 | void Clear(); 103 | 104 | private: 105 | std::vector selectors_; 106 | }; 107 | } 108 | 109 | #endif -------------------------------------------------------------------------------- /demos/menu/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "base/task_center.hpp" 4 | #include "menudlg.h" 5 | 6 | int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) 7 | { 8 | CMenuDlg impl; 9 | impl.Parse(L"menu.xml"); 10 | RECT rect = {100, 100, 280, 365}; 11 | impl.Create(NULL, &rect); 12 | impl.ShowWindow(SW_SHOWNORMAL); 13 | impl.UpdateWindow(); 14 | 15 | 16 | base::Singleton::Instance().Run(); 17 | return 0; 18 | } -------------------------------------------------------------------------------- /demos/menu/maindlg.h: -------------------------------------------------------------------------------- 1 | #include "win/window.h" 2 | #include "win/layered_window.h" 3 | #include "core/controls/button.h" 4 | #include 5 | 6 | class CMainDlg 7 | : public win::Window 8 | , public core::IMessageObserver 9 | { 10 | protected: 11 | 12 | BEGIN_WIDGET_MESSAGE_MAP() 13 | WIDGET_LBUTTONUP("button_logout", OnLogout) 14 | WIDGET_LBUTTONUP("button_setting", OnSetting) 15 | WIDGET_LBUTTONUP("button_exit", OnExit) 16 | END_WIDGET_MESSAGE_MAP() 17 | 18 | BEGIN_MSG_MAP(CMainDlg) 19 | MESSAGE_HANDLER( WM_CREATE, OnCreate ) 20 | MESSAGE_HANDLER( WM_DESTROY, OnDestroy ) 21 | CHAIN_MSG_MAP(win::Window) 22 | END_MSG_MAP() 23 | 24 | HRESULT OnCreate( UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL bHandled ) 25 | { 26 | RegisterMessageObserver("button_logout", this); 27 | RegisterMessageObserver("button_setting", this); 28 | RegisterMessageObserver("button_exit", this); 29 | RegisterMessageObserver("my_disk", this); 30 | RegisterMessageObserver("project_disk", this); 31 | RegisterMessageObserver("group_disk", this); 32 | RegisterMessageObserver("website", this); 33 | 34 | return win::Window::OnCreate( uMsg, wParam, lParam, bHandled ); 35 | } 36 | 37 | HRESULT OnDestroy( UINT, WPARAM, LPARAM, BOOL ) 38 | { 39 | ::PostQuitMessage(0); 40 | return TRUE; 41 | } 42 | 43 | void OnLogout(core::StWidget* widget, WPARAM wparam, LPARAM lparam) 44 | { 45 | 46 | } 47 | 48 | void OnSetting(core::StWidget* widget, WPARAM wparam, LPARAM lparam) 49 | { 50 | } 51 | 52 | void OnExit(core::StWidget* widget, WPARAM wparam, LPARAM lparam) 53 | { 54 | DestroyWindow(); 55 | 56 | } 57 | 58 | void RefreshOnDebug() 59 | { 60 | RegisterMessageObserver("button_close", this); 61 | } 62 | }; -------------------------------------------------------------------------------- /demos/menu/menudlg.h: -------------------------------------------------------------------------------- 1 | #include "win/window.h" 2 | #include "win/layered_window.h" 3 | #include "core/controls/button.h" 4 | #include "base/time_ticks.h" 5 | #include 6 | 7 | #include "animate/animate_container.h" 8 | #include "animate/frame_animate.h" 9 | 10 | 11 | class CMenuDlg 12 | : public win::Window 13 | , public core::IMessageObserver 14 | , public animate::AttributeAnimate::Delegate 15 | { 16 | protected: 17 | 18 | BEGIN_WIDGET_MESSAGE_MAP() 19 | WIDGET_LBUTTONUP("button_logout", OnLogout) 20 | WIDGET_LBUTTONUP("button_setting", OnSetting) 21 | WIDGET_LBUTTONUP("button_exit", OnExit) 22 | END_WIDGET_MESSAGE_MAP() 23 | 24 | BEGIN_MSG_MAP(CMainDlg) 25 | MESSAGE_HANDLER( WM_CREATE, OnCreate ) 26 | MESSAGE_HANDLER( WM_DESTROY, OnDestroy ) 27 | CHAIN_MSG_MAP(win::Window) 28 | END_MSG_MAP() 29 | 30 | HRESULT OnCreate( UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL bHandled ) 31 | { 32 | RegisterMessageObserver("button_logout", this); 33 | RegisterMessageObserver("button_setting", this); 34 | RegisterMessageObserver("button_exit", this); 35 | RegisterMessageObserver("my_disk", this); 36 | RegisterMessageObserver("project_disk", this); 37 | RegisterMessageObserver("group_disk", this); 38 | RegisterMessageObserver("website", this); 39 | 40 | return win::Window::OnCreate( uMsg, wParam, lParam, bHandled ); 41 | } 42 | 43 | HRESULT OnDestroy( UINT, WPARAM, LPARAM, BOOL ) 44 | { 45 | ::PostQuitMessage(0); 46 | return TRUE; 47 | } 48 | 49 | void OnLogout(core::StWidget* widget, WPARAM wparam, LPARAM lparam) 50 | { 51 | 52 | } 53 | 54 | void OnSetting(core::StWidget* widget, WPARAM wparam, LPARAM lparam) 55 | { 56 | animate::AttributeFrameAnimate* animate = new animate::AttributeFrameAnimate; 57 | animate->SetId("user_icon"); 58 | animate->SetAttribute("frame-index"); 59 | animate->SetCount(3); 60 | animate->SetInterval(100); 61 | animate->SetTotal(INFINITE); 62 | animate->SetDelegate(this); 63 | 64 | container_.Start(animate); 65 | } 66 | 67 | void OnExit(core::StWidget* widget, WPARAM wparam, LPARAM lparam) 68 | { 69 | DestroyWindow(); 70 | 71 | } 72 | 73 | void RefreshOnDebug() 74 | { 75 | RegisterMessageObserver("button_close", this); 76 | } 77 | 78 | virtual void OnAttributeValueChanged(std::string id, std::string attribute, std::string value) 79 | { 80 | static __int64 last = base::TimeTicks::Now(), count = 0; 81 | count++; 82 | //if (count % 10 == 0) 83 | { 84 | __int64 now = base::TimeTicks::Now(); 85 | __int64 off = now - last; 86 | TCHAR str_value[16] = {0}; 87 | _i64tow_s(off / count, str_value, 16, 10); 88 | ::OutputDebugString(L"OnAttributeValueChanged---"); 89 | ::OutputDebugString(str_value); 90 | ::OutputDebugString(L"\n"); 91 | } 92 | if (id == "user_icon") 93 | { 94 | SetAttribute(id, attribute, value); 95 | } 96 | } 97 | 98 | private: 99 | animate::AnimateContainer container_; 100 | }; -------------------------------------------------------------------------------- /demos/menu/swift.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 2012 4 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "swift", "swift.vcxproj", "{10BC3C99-3085-46C0-89CA-476F4927E460}" 5 | EndProject 6 | Global 7 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 8 | Debug|Win32 = Debug|Win32 9 | Release|Win32 = Release|Win32 10 | EndGlobalSection 11 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 12 | {10BC3C99-3085-46C0-89CA-476F4927E460}.Debug|Win32.ActiveCfg = Debug|Win32 13 | {10BC3C99-3085-46C0-89CA-476F4927E460}.Debug|Win32.Build.0 = Debug|Win32 14 | {10BC3C99-3085-46C0-89CA-476F4927E460}.Release|Win32.ActiveCfg = Release|Win32 15 | {10BC3C99-3085-46C0-89CA-476F4927E460}.Release|Win32.Build.0 = Release|Win32 16 | EndGlobalSection 17 | GlobalSection(SolutionProperties) = preSolution 18 | HideSolutionNode = FALSE 19 | EndGlobalSection 20 | EndGlobal 21 | -------------------------------------------------------------------------------- /demos/test/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "base/task_center.hpp" 4 | #include "maindlg.h" 5 | 6 | int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) 7 | { 8 | CMainDlg impl; 9 | impl.Parse(L"settings.xml"); 10 | RECT rect = {100, 100, 703, 700}; 11 | impl.Create(NULL, &rect); 12 | impl.ShowWindow(SW_SHOWNORMAL); 13 | impl.UpdateWindow(); 14 | 15 | base::Singleton::Instance().Run(); 16 | return 0; 17 | } -------------------------------------------------------------------------------- /demos/test/maindlg.h: -------------------------------------------------------------------------------- 1 | #include "win/window.h" 2 | #include "win/layered_window.h" 3 | #include "core/controls/button.h" 4 | #include 5 | #include "core/controls/realwnd.h" 6 | 7 | class CMainDlg 8 | : public win::Window 9 | , public core::IMessageObserver 10 | { 11 | protected: 12 | 13 | BEGIN_WIDGET_MESSAGE_MAP() 14 | WIDGET_LBUTTONUP("button_close", OnClose) 15 | WIDGET_LBUTTONUP("button_min", OnMin) 16 | WIDGET_LBUTTONUP("button_change", OnChange) 17 | END_WIDGET_MESSAGE_MAP() 18 | 19 | BEGIN_MSG_MAP(CMainDlg) 20 | MESSAGE_HANDLER( WM_CREATE, OnCreate ) 21 | MESSAGE_HANDLER( WM_DESTROY, OnDestroy ) 22 | CHAIN_MSG_MAP(win::Window) 23 | END_MSG_MAP() 24 | 25 | HRESULT OnCreate( UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL bHandled ) 26 | { 27 | RegisterMessageObserver("button_close", this); 28 | RegisterMessageObserver("button_min", this); 29 | RegisterMessageObserver("button_change", this); 30 | 31 | CenterWindow(); 32 | 33 | core::StRealWnd* editWnd = (core::StRealWnd*)FindWidget("editwnd"); 34 | if (editWnd) 35 | { 36 | m_edit.Create(m_hWnd, NULL, NULL, WS_CLIPCHILDREN | WS_CHILD); 37 | editWnd->SetHWnd(m_edit.m_hWnd); 38 | } 39 | 40 | return win::Window::OnCreate( uMsg, wParam, lParam, bHandled ); 41 | } 42 | 43 | HRESULT OnDestroy( UINT, WPARAM, LPARAM, BOOL ) 44 | { 45 | ::PostQuitMessage(0); 46 | return TRUE; 47 | } 48 | 49 | void OnClose(core::StWidget* widget, WPARAM wparam, LPARAM lparam) 50 | { 51 | DestroyWindow(); 52 | } 53 | 54 | void OnMin(core::StWidget* widget, WPARAM wparam, LPARAM lparam) 55 | { 56 | } 57 | 58 | void OnChange(core::StWidget* widget, WPARAM wparam, LPARAM lparam) 59 | { 60 | RECT rect; 61 | ::GetWindowRect(m_hWnd, &rect); 62 | rect.bottom = rect.top + 345; 63 | MoveWindow(&rect); 64 | 65 | SetAttribute("progress_div", "visible", "visible"); 66 | } 67 | 68 | void RefreshOnDebug() 69 | { 70 | RegisterMessageObserver("button_close", this); 71 | } 72 | 73 | CEdit m_edit; 74 | }; -------------------------------------------------------------------------------- /demos/test/swift.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 2012 4 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "swift", "swift.vcxproj", "{10BC3C99-3085-46C0-89CA-476F4927E460}" 5 | EndProject 6 | Global 7 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 8 | Debug|Win32 = Debug|Win32 9 | Release|Win32 = Release|Win32 10 | EndGlobalSection 11 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 12 | {10BC3C99-3085-46C0-89CA-476F4927E460}.Debug|Win32.ActiveCfg = Debug|Win32 13 | {10BC3C99-3085-46C0-89CA-476F4927E460}.Debug|Win32.Build.0 = Debug|Win32 14 | {10BC3C99-3085-46C0-89CA-476F4927E460}.Release|Win32.ActiveCfg = Release|Win32 15 | {10BC3C99-3085-46C0-89CA-476F4927E460}.Release|Win32.Build.0 = Release|Win32 16 | EndGlobalSection 17 | GlobalSection(SolutionProperties) = preSolution 18 | HideSolutionNode = FALSE 19 | EndGlobalSection 20 | EndGlobal 21 | -------------------------------------------------------------------------------- /swift.vcxproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | 14 | {1C452817-E097-45CF-A44A-6196B2CD113A} 15 | swift 16 | 17 | 18 | 19 | StaticLibrary 20 | true 21 | v110 22 | Unicode 23 | Static 24 | 25 | 26 | Application 27 | false 28 | v110 29 | true 30 | MultiByte 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | .;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); 44 | ../lib 45 | ../lib/swift 46 | 47 | 48 | 49 | Level3 50 | Disabled 51 | false 52 | WIN32;NDEBUG;_WINDOWS;WIN_SYSTEM;%(PreprocessorDefinitions) 53 | ../ 54 | 55 | 56 | true 57 | 58 | 59 | 60 | 61 | Level3 62 | MaxSpeed 63 | true 64 | true 65 | true 66 | 67 | 68 | true 69 | true 70 | true 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 | -------------------------------------------------------------------------------- /util/string.cpp: -------------------------------------------------------------------------------- 1 | #include "util/string.h" 2 | #include 3 | 4 | namespace util 5 | { 6 | std::wstring String::UTF8ToUnicode(const char* text) 7 | { 8 | std::wstring str_text; 9 | if (!text || !*text) 10 | { 11 | return str_text; 12 | } 13 | 14 | int len = ::MultiByteToWideChar(CP_UTF8, 0, text, (int)strlen(text), 0, 0); 15 | if (len > 0) 16 | { 17 | wchar_t* wsz_text = new wchar_t[len + 1]; 18 | if (wsz_text && ::MultiByteToWideChar(CP_UTF8, 0, text, (int)strlen(text), wsz_text, len) != 0 ) 19 | { 20 | wsz_text[len] = 0; 21 | str_text = wsz_text; 22 | } 23 | 24 | if (wsz_text) 25 | delete[] wsz_text; 26 | } 27 | 28 | return str_text; 29 | } 30 | std::string String::UnicodeToUTF8(const wchar_t* text) 31 | { 32 | std::string str_text; 33 | if (!text || !*text) 34 | { 35 | return str_text; 36 | } 37 | 38 | int len = ::WideCharToMultiByte(CP_UTF8, 0, text, (int)wcslen(text), 0, 0, 0, 0); 39 | if (len > 0) 40 | { 41 | char* sz_text = new char[len + 1]; 42 | if (sz_text && ::WideCharToMultiByte(CP_UTF8, 0, text, (int)wcslen(text), sz_text, len, 0, 0) != 0) 43 | { 44 | sz_text[len] = 0; 45 | str_text = sz_text; 46 | } 47 | 48 | if (sz_text) 49 | delete[] sz_text; 50 | } 51 | 52 | return str_text; 53 | } 54 | } -------------------------------------------------------------------------------- /util/string.h: -------------------------------------------------------------------------------- 1 | #ifndef __util_string_h__ 2 | #define __util_string_h__ 3 | 4 | #include 5 | #include 6 | 7 | namespace util 8 | { 9 | class String 10 | { 11 | public: 12 | template 13 | static bool IsEmpty(const T* text) 14 | { 15 | if (!text || !*text) 16 | { 17 | return true; 18 | } 19 | return false; 20 | } 21 | 22 | template 23 | static std::string TToHex(T value) 24 | { 25 | std::string s; 26 | char sz[sizeof(T) * 8 / 4 + 1] = {0}; 27 | for (int i = 0; i < sizeof(T) * 8 / 4; i++) 28 | { 29 | unsigned off = sizeof(T) * 8 - 4 * (i + 1); 30 | char p = (char)((value >> off) & 0xF); 31 | if (p <= 9 && p >= 0) 32 | { 33 | p += '0'; 34 | } 35 | else if (p >= 0xA && p <= 0xF) 36 | { 37 | p += 'a'; 38 | p -= 10; 39 | } 40 | 41 | sz[i] = p; 42 | } 43 | 44 | s = sz; 45 | return s; 46 | } 47 | 48 | template 49 | static T HexToT(const char *p, int n) 50 | { 51 | T t = 0; 52 | while (p && *p && n != 0) 53 | { 54 | t <<= 4; 55 | if (*p >= '0' && *p <= '9') 56 | { 57 | t |= (*p - '0'); 58 | } 59 | else if (*p >= 'a' && *p <= 'f') 60 | { 61 | t |= (*p - 'a' + 10); 62 | } 63 | else if (*p >= 'A' && *p <= 'F') 64 | { 65 | t |= (*p - 'A' + 10); 66 | } 67 | else 68 | { 69 | return 0; 70 | } 71 | 72 | p++; 73 | n--; 74 | } 75 | 76 | return t; 77 | } 78 | 79 | static std::wstring UTF8ToUnicode(const char* text); 80 | static std::string UnicodeToUTF8(const wchar_t* text); 81 | }; 82 | } 83 | 84 | #endif -------------------------------------------------------------------------------- /util/tstring.h: -------------------------------------------------------------------------------- 1 | #ifndef __util_t_string_h__ 2 | #define __util_t_string_h__ 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | namespace util 11 | { 12 | template 13 | class TString 14 | { 15 | public: 16 | typedef std::basic_string, std::allocator > value_type; 17 | typedef typename value_type::size_type size_type; 18 | 19 | TString() {} 20 | 21 | TString(const value_type &str) 22 | { 23 | str_ = str; 24 | } 25 | 26 | TString(const TString &obj) 27 | { 28 | str_ = obj.str_; 29 | } 30 | 31 | TString & operator=(const value_type &str) 32 | { 33 | str_ = str; 34 | return *this; 35 | } 36 | 37 | TString & operator=(const TString &obj) 38 | { 39 | if (&obj != this) 40 | { 41 | str_ = obj.str_; 42 | } 43 | 44 | return *this; 45 | } 46 | 47 | public: 48 | const T *C_Str() 49 | { 50 | return str_.c_str(); 51 | } 52 | 53 | void Trim() 54 | { 55 | if (str_.size() > 0) 56 | { 57 | std::basic_string::iterator c; 58 | 59 | for (c = str_.begin(); c != str_.end() && _istspace(*c++); ); 60 | str_.erase(str_.begin(), --c); 61 | 62 | for (c = str_.end(); c != str_.begin() && _istspace(*--c); ); 63 | str_.erase(++c, str_.end()); 64 | } 65 | } 66 | 67 | size_type Tokenize(const value_type &sep, size_type pos, value_type &token) 68 | { 69 | token.clear(); 70 | if (str_.empty() || str_.size() <= pos) 71 | { 72 | return str_.npos; 73 | } 74 | 75 | value_type s = str_.substr(pos); 76 | 77 | size_type p = 0; 78 | size_type find = 0; 79 | while (token.empty()) 80 | { 81 | find = s.find(sep.c_str(), p); 82 | if (find == s.npos) 83 | { 84 | token = s.substr(p); 85 | return s.npos; 86 | } 87 | else 88 | { 89 | token = s.substr(p, find - p); 90 | p += sep.size(); 91 | } 92 | } 93 | 94 | return find + pos; 95 | } 96 | 97 | private: 98 | value_type str_; 99 | }; 100 | } 101 | 102 | #endif -------------------------------------------------------------------------------- /win/asset_loader.cpp: -------------------------------------------------------------------------------- 1 | #include "win/asset_loader.h" 2 | 3 | EXTERN_C IMAGE_DOS_HEADER __ImageBase; 4 | 5 | #define SWFIT_RES_TYPE (L"SWRES") 6 | 7 | namespace win 8 | { 9 | LocalAssetLoader::LocalAssetLoader() 10 | { 11 | Gdiplus::GdiplusStartup(&gdi_token_, &gdiplus_startup_input_, NULL); 12 | Init(); 13 | } 14 | 15 | LocalAssetLoader::~LocalAssetLoader() 16 | { 17 | Gdiplus::GdiplusShutdown(gdi_token_); 18 | } 19 | 20 | void LocalAssetLoader::Init() 21 | { 22 | CString path; 23 | ::GetModuleFileName(NULL, path.GetBufferSetLength(MAX_PATH + 1), MAX_PATH); 24 | path.ReleaseBuffer(); 25 | int pos = path.ReverseFind('\\'); 26 | if (pos != -1) 27 | { 28 | path = path.Left(pos); 29 | } 30 | 31 | workspace_ = (LPCWSTR)path; 32 | workspace_.append(L"\\res\\"); 33 | } 34 | 35 | bool LocalAssetLoader::LoadBinary(const wchar_t* name, byte** buffer, unsigned* size) 36 | { 37 | if (!name || !*name || !buffer) 38 | { 39 | return false; 40 | } 41 | 42 | std::wstring filename = GetFilePath(name); 43 | *buffer = 0; 44 | if (size) 45 | { 46 | *size = 0; 47 | } 48 | 49 | __int64 i64Size = 0; 50 | if (File::GetFileSize(filename.c_str(), &i64Size) && i64Size > 0) 51 | { 52 | DWORD dwReadSize = 0; 53 | *buffer = new byte[(unsigned)i64Size]; 54 | if (*buffer) 55 | { 56 | memset(*buffer, 0, (size_t)i64Size); 57 | dwReadSize = (DWORD)i64Size; 58 | } 59 | else 60 | { 61 | return false; 62 | } 63 | 64 | if (!File::ReadFile(filename.c_str(), *buffer, dwReadSize)) 65 | { 66 | delete[] *buffer; 67 | *buffer = 0; 68 | return false; 69 | } 70 | else 71 | { 72 | if (size) 73 | { 74 | *size = dwReadSize; 75 | } 76 | return true; 77 | } 78 | } 79 | return false; 80 | } 81 | 82 | bool LocalAssetLoader::LoadImage(const wchar_t* name, Image* image) 83 | { 84 | if (!name || !*name || !image) 85 | { 86 | return false; 87 | } 88 | 89 | std::wstring strPath = GetFilePath(name); 90 | 91 | Gdiplus::Image * gdiplus_image = Gdiplus::Image::FromFile(strPath.c_str()); 92 | if (!gdiplus_image) 93 | { 94 | return false; 95 | } 96 | 97 | BOOL created = image->CreateFromImage(gdiplus_image); 98 | delete gdiplus_image; 99 | 100 | return !!created; 101 | } 102 | 103 | std::wstring LocalAssetLoader::GetWorkspace() 104 | { 105 | return workspace_; 106 | } 107 | 108 | std::wstring LocalAssetLoader::GetFilePath(const wchar_t* name) 109 | { 110 | return workspace_ + name; 111 | } 112 | 113 | 114 | EmbedAssetLoader::EmbedAssetLoader() 115 | { 116 | Gdiplus::GdiplusStartup(&gdi_token_, &gdiplus_startup_input_, NULL); 117 | } 118 | 119 | EmbedAssetLoader::~EmbedAssetLoader() 120 | { 121 | Gdiplus::GdiplusShutdown(gdi_token_); 122 | } 123 | 124 | bool EmbedAssetLoader::LoadBinary(const wchar_t* name, byte** buffer, unsigned* size) 125 | { 126 | if (!name || !*name || !buffer) 127 | { 128 | return false; 129 | } 130 | 131 | return LoadResource((HMODULE)&__ImageBase, name, SWFIT_RES_TYPE, buffer, size); 132 | } 133 | 134 | bool EmbedAssetLoader::LoadImage(const wchar_t* name, Image* image) 135 | { 136 | if (!name || !*name || !image) 137 | { 138 | return false; 139 | } 140 | 141 | HMODULE hModule = (HMODULE)&__ImageBase; 142 | HRSRC hRsrc = ::FindResource(hModule, name, SWFIT_RES_TYPE); 143 | if (NULL == hRsrc) 144 | return false; 145 | 146 | DWORD dwSize = ::SizeofResource(hModule, hRsrc); 147 | if (0 == dwSize) 148 | return false; 149 | 150 | HGLOBAL hGlobal = ::LoadResource(hModule, hRsrc); 151 | if (NULL == hGlobal) 152 | return false; 153 | 154 | IStream* pstm = NULL; 155 | CreateStreamOnHGlobal(hGlobal, FALSE, &pstm); 156 | 157 | Gdiplus::Image* gdiplus_image = NULL; 158 | 159 | if (pstm) 160 | { 161 | gdiplus_image = Gdiplus::Image::FromStream(pstm); 162 | pstm->Release(); 163 | } 164 | ::FreeResource(hGlobal); 165 | 166 | BOOL created = false; 167 | if (gdiplus_image) 168 | { 169 | created = image->CreateFromImage(gdiplus_image); 170 | delete gdiplus_image; 171 | } 172 | 173 | return !!created; 174 | } 175 | 176 | bool EmbedAssetLoader::LoadResource(HMODULE module, const wchar_t* name, const wchar_t* type, byte** buffer, unsigned* size) 177 | { 178 | HRSRC hRsrc = ::FindResource(module, name, type); 179 | if (NULL == hRsrc) 180 | return false; 181 | 182 | DWORD dwSize = ::SizeofResource(module, hRsrc); 183 | if (0 == dwSize) 184 | return false; 185 | 186 | HGLOBAL hGlobal = ::LoadResource(module, hRsrc); 187 | if (NULL == hGlobal) 188 | return false; 189 | 190 | LPVOID pBuf = ::LockResource(hGlobal); 191 | if (NULL == pBuf) 192 | return false; 193 | 194 | *buffer = new byte[dwSize]; 195 | if (NULL == *buffer) 196 | { 197 | ::FreeResource(hGlobal); 198 | return false; 199 | } 200 | 201 | memset(*buffer, 0, dwSize); 202 | memcpy(*buffer, pBuf, dwSize); 203 | 204 | if (size) 205 | *size = dwSize; 206 | 207 | ::FreeResource(hGlobal); 208 | return true; 209 | } 210 | } -------------------------------------------------------------------------------- /win/asset_loader.h: -------------------------------------------------------------------------------- 1 | #ifndef __win_asset_loader_h__ 2 | #define __win_asset_loader_h__ 3 | 4 | #include 5 | #include 6 | 7 | #include "win/file.h" 8 | #include "win/image.h" 9 | #include "win/asset_manager.h" 10 | 11 | #include 12 | #include 13 | #include 14 | #pragma comment(lib, "gdiplus.lib") 15 | 16 | namespace win 17 | { 18 | class LocalAssetLoader : public AssetManager::Loader 19 | { 20 | public: 21 | LocalAssetLoader(); 22 | ~LocalAssetLoader(); 23 | 24 | virtual bool LoadBinary(const wchar_t* name, byte** buffer, unsigned* size); 25 | virtual bool LoadImage(const wchar_t* name, Image* image); 26 | 27 | private: 28 | void Init(); 29 | std::wstring GetWorkspace(); 30 | std::wstring GetFilePath(const wchar_t* name); 31 | 32 | private: 33 | Gdiplus::GdiplusStartupInput gdiplus_startup_input_; 34 | ULONG_PTR gdi_token_; 35 | 36 | std::wstring workspace_; 37 | }; 38 | 39 | class EmbedAssetLoader : public AssetManager::Loader 40 | { 41 | public: 42 | EmbedAssetLoader(); 43 | ~EmbedAssetLoader(); 44 | 45 | virtual bool LoadBinary(const wchar_t* name, byte** buffer, unsigned* size); 46 | virtual bool LoadImage(const wchar_t* name, Image* image); 47 | 48 | protected: 49 | bool LoadResource(HMODULE module, const wchar_t* name, const wchar_t* type, byte** buffer, unsigned* size); 50 | 51 | private: 52 | Gdiplus::GdiplusStartupInput gdiplus_startup_input_; 53 | ULONG_PTR gdi_token_; 54 | }; 55 | } 56 | 57 | #endif -------------------------------------------------------------------------------- /win/asset_manager.cpp: -------------------------------------------------------------------------------- 1 | #include "win/asset_manager.h" 2 | #include "win/asset_loader.h" 3 | 4 | namespace win 5 | { 6 | AssetManager::Loader::~Loader() {} 7 | 8 | AssetManager::AssetManager() 9 | { 10 | RegisterDefaultLoader(); 11 | } 12 | 13 | AssetManager::~AssetManager() 14 | { 15 | Clear(); 16 | UnRegisterDefaultLoader(); 17 | } 18 | 19 | void AssetManager::RegisterExternalLoader(Loader* loader) 20 | { 21 | if (loader) 22 | { 23 | loaders_.insert(loader); 24 | } 25 | } 26 | 27 | void AssetManager::UnRegisterExternalLoader(Loader* loader) 28 | { 29 | for (std::set::iterator iter = loaders_.begin(); iter != loaders_.end(); ++iter) 30 | { 31 | Loader* load = *iter; 32 | if (loader == load) 33 | { 34 | loaders_.erase(load); 35 | break; 36 | } 37 | } 38 | } 39 | 40 | bool AssetManager::LoadBinary(const wchar_t* name, byte** buffer, unsigned* size) 41 | { 42 | if (!name || !*name || !buffer) 43 | { 44 | return false; 45 | } 46 | 47 | bool loaded = false; 48 | for (std::set::iterator iter = loaders_.begin(); iter != loaders_.end(); ++iter) 49 | { 50 | Loader* loader = *iter; 51 | if (loader->LoadBinary(name, buffer, size)) 52 | { 53 | loaded = true; 54 | break; 55 | } 56 | } 57 | 58 | return loaded; 59 | } 60 | 61 | bool AssetManager::LoadImage(const wchar_t* name, Image* image) 62 | { 63 | if (!name || !*name || !image) 64 | { 65 | return false; 66 | } 67 | 68 | _image_maps::iterator iter = img_cache_map_.find(std::wstring(name)); 69 | if (iter != img_cache_map_.end()) 70 | { 71 | image->Copy(iter->second); 72 | return true; 73 | } 74 | 75 | bool loaded = false; 76 | for (std::set::iterator iter = loaders_.begin(); iter != loaders_.end(); ++iter) 77 | { 78 | Loader* loader = *iter; 79 | if (loader->LoadImage(name, image)) 80 | { 81 | loaded = true; 82 | break; 83 | } 84 | } 85 | 86 | if (loaded) 87 | { 88 | Image *cache = new Image; 89 | if (cache) 90 | { 91 | cache->Copy(image); 92 | img_cache_map_[std::wstring(name)] = cache; 93 | } 94 | } 95 | return loaded; 96 | } 97 | 98 | void AssetManager::Clear() 99 | { 100 | for (_image_maps::iterator iter = img_cache_map_.begin(); iter != img_cache_map_.end(); ++iter) 101 | { 102 | Image* image = iter->second; 103 | image->Release(); 104 | delete image; 105 | } 106 | 107 | img_cache_map_.clear(); 108 | } 109 | 110 | void AssetManager::RegisterDefaultLoader() 111 | { 112 | local_loader_ = new LocalAssetLoader; 113 | embed_loader_ = new EmbedAssetLoader; 114 | RegisterExternalLoader(local_loader_); 115 | RegisterExternalLoader(embed_loader_); 116 | } 117 | 118 | void AssetManager::UnRegisterDefaultLoader() 119 | { 120 | UnRegisterExternalLoader(local_loader_); 121 | UnRegisterExternalLoader(embed_loader_); 122 | 123 | if (local_loader_) 124 | { 125 | delete local_loader_; 126 | local_loader_ = 0; 127 | } 128 | 129 | if (embed_loader_) 130 | { 131 | delete embed_loader_; 132 | embed_loader_ = 0; 133 | } 134 | } 135 | } -------------------------------------------------------------------------------- /win/asset_manager.h: -------------------------------------------------------------------------------- 1 | #ifndef __win_asset_manager_h__ 2 | #define __win_asset_manager_h__ 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | #include "base/singleton.h" 9 | 10 | #include "win/image.h" 11 | 12 | namespace win 13 | { 14 | class LocalAssetLoader; 15 | class EmbedAssetLoader; 16 | 17 | class AssetManager 18 | { 19 | public: 20 | class Loader 21 | { 22 | public: 23 | virtual ~Loader() = 0; 24 | 25 | virtual bool LoadBinary(const wchar_t* name, byte** buffer, unsigned* size) = 0; 26 | virtual bool LoadImage(const wchar_t* name, Image* image) = 0; 27 | }; 28 | 29 | AssetManager(); 30 | ~AssetManager(); 31 | 32 | void RegisterExternalLoader(Loader* loader); 33 | void UnRegisterExternalLoader(Loader* loader); 34 | 35 | bool LoadBinary(const wchar_t* name, byte** buffer, unsigned* size); 36 | bool LoadImage(const wchar_t* name, Image* image); 37 | void Clear(); 38 | 39 | private: 40 | void RegisterDefaultLoader(); 41 | void UnRegisterDefaultLoader(); 42 | 43 | private: 44 | typedef std::map _image_maps; 45 | 46 | std::set loaders_; 47 | _image_maps img_cache_map_; 48 | 49 | LocalAssetLoader* local_loader_; 50 | EmbedAssetLoader* embed_loader_; 51 | }; 52 | 53 | typedef base::Singleton AssetManagerSingleton; 54 | } 55 | 56 | #endif -------------------------------------------------------------------------------- /win/builder.h: -------------------------------------------------------------------------------- 1 | #ifndef __win_builder_h__ 2 | #define __win_builder_h__ 3 | 4 | #include 5 | 6 | #include "base/def.h" 7 | 8 | #include "xml/xml.h" 9 | #include "css/css.h" 10 | 11 | namespace core 12 | { 13 | class StStyleCache; 14 | class StWidget; 15 | class StContainer; 16 | class StStyle; 17 | } 18 | 19 | namespace css 20 | { 21 | class DeclareSet; 22 | } 23 | 24 | namespace win 25 | { 26 | class Builder 27 | { 28 | public: 29 | Builder() {} 30 | ~Builder() {} 31 | 32 | core::StWidget* Parse(const wchar_t* file); 33 | core::StWidget* ParseText(const wchar_t* text); 34 | 35 | private: 36 | BOOL BuildHeader(xml::XmlElement* root); 37 | core::StWidget* BuildBody(xml::XmlElement* root); 38 | core::StWidget* BuildNode(xml::XmlElement* element, core::StWidget* containter= NULL); 39 | 40 | private: 41 | bool ParseAttribute(xml::XmlElement* element, core::StWidget* widget); 42 | bool BuildChildren(xml::XmlElement *element, core::StContainer* containter); 43 | bool UpdateStyle(core::StWidget* widget, const std::string& value, const std::string& classname, DWORD state); 44 | 45 | void AddDeclareSetToStyle(css::DeclareSet& declare_set, core::StStyle& style); 46 | std::wstring ConverStateToPseudoClass(DWORD state); 47 | 48 | private: 49 | css::CssDocument style_sheet_; 50 | 51 | private: 52 | DISABLE_COPY_AND_ASSIGN(Builder); 53 | }; 54 | } 55 | 56 | #endif -------------------------------------------------------------------------------- /win/dc.cpp: -------------------------------------------------------------------------------- 1 | #include "win/dc.h" 2 | 3 | namespace win 4 | { 5 | WinDC::WinDC() 6 | { 7 | type_ = DEFAULT_DC; 8 | hdc_ = ::GetDC(NULL); 9 | hwnd_ = NULL; 10 | } 11 | 12 | WinDC::WinDC(HWND hwnd) 13 | { 14 | type_ = HANDLER_DC; 15 | hdc_ = ::GetDC(hwnd); 16 | hwnd_ = hwnd; 17 | } 18 | 19 | WinDC::~WinDC() 20 | { 21 | if (type_ == DEFAULT_DC) 22 | { 23 | ::ReleaseDC(NULL, hdc_); 24 | } 25 | else if (type_ == HANDLER_DC) 26 | { 27 | ::ReleaseDC(hwnd_, hdc_); 28 | } 29 | 30 | hwnd_ = 0; 31 | hdc_ = 0; 32 | } 33 | 34 | WinMemoryDC::WinMemoryDC() 35 | { 36 | type_ = DEFAULT_DC; 37 | hdc_ = ::GetDC(NULL); 38 | mem_dc_ = ::CreateCompatibleDC(hdc_); 39 | bitmap_ = NULL; 40 | } 41 | 42 | WinMemoryDC::WinMemoryDC(HDC hdc) 43 | { 44 | type_ = COMPATIBLE_DC; 45 | hdc_ = hdc; 46 | mem_dc_ = ::CreateCompatibleDC(hdc_); 47 | bitmap_ = NULL; 48 | } 49 | 50 | WinMemoryDC::WinMemoryDC(HBITMAP bitmap) 51 | { 52 | type_ = DEFAULT_DC | BITMAP_DC; 53 | hdc_ = ::GetDC(NULL); 54 | mem_dc_ = ::CreateCompatibleDC(hdc_); 55 | bitmap_ = (HBITMAP)::SelectObject(mem_dc_, bitmap); 56 | } 57 | 58 | WinMemoryDC::~WinMemoryDC() 59 | { 60 | if ((type_ & BITMAP_DC) == BITMAP_DC) 61 | { 62 | ::SelectObject(mem_dc_, bitmap_); 63 | } 64 | 65 | ::DeleteDC(mem_dc_); 66 | 67 | if ((type_ & DEFAULT_DC) == DEFAULT_DC) 68 | { 69 | ::ReleaseDC( NULL, hdc_ ); 70 | } 71 | } 72 | } -------------------------------------------------------------------------------- /win/dc.h: -------------------------------------------------------------------------------- 1 | #ifndef __win_dc_h__ 2 | #define __win_dc_h__ 3 | 4 | #include 5 | #include 6 | 7 | #include 8 | #include 9 | 10 | namespace win 11 | { 12 | class WinDC 13 | { 14 | public: 15 | enum Type 16 | { 17 | DEFAULT_DC = 0x00000001, 18 | HANDLER_DC = 0x00000002 19 | }; 20 | 21 | WinDC(); 22 | WinDC(HWND hwnd); 23 | 24 | ~WinDC(); 25 | 26 | operator HDC() { return hdc_; } 27 | 28 | private: 29 | Type type_; 30 | HWND hwnd_; 31 | HDC hdc_; 32 | }; 33 | 34 | class WinMemoryDC 35 | { 36 | public: 37 | enum Type 38 | { 39 | DEFAULT_DC = 0x00000001, 40 | COMPATIBLE_DC = 0x00000002, 41 | BITMAP_DC = 0x00000004 42 | }; 43 | 44 | WinMemoryDC(); 45 | WinMemoryDC(HDC hdc); 46 | WinMemoryDC(HBITMAP bitmap); 47 | 48 | ~WinMemoryDC(); 49 | 50 | operator HDC(){ return mem_dc_; } 51 | 52 | private: 53 | DWORD type_; 54 | HDC mem_dc_; 55 | HDC hdc_; 56 | HBITMAP bitmap_; 57 | }; 58 | } 59 | 60 | #endif -------------------------------------------------------------------------------- /win/file.cpp: -------------------------------------------------------------------------------- 1 | #include "win/file.h" 2 | 3 | namespace win 4 | { 5 | BOOL File::GetFileSize(LPCTSTR lpszFile, __int64 *size) 6 | { 7 | if (!lpszFile || !size) 8 | { 9 | return FALSE; 10 | } 11 | 12 | *size = 0; 13 | 14 | WIN32_FILE_ATTRIBUTE_DATA attr; 15 | if (!GetFileAttributesEx(lpszFile, GetFileExInfoStandard, &attr)) 16 | { 17 | return FALSE; 18 | } 19 | 20 | ULARGE_INTEGER s; 21 | s.HighPart = attr.nFileSizeHigh; 22 | s.LowPart = attr.nFileSizeLow; 23 | *size = s.QuadPart; 24 | 25 | return TRUE; 26 | } 27 | 28 | BOOL File::ReadFile(LPCTSTR lpszFile, BYTE *pBuf, DWORD dwSize) 29 | { 30 | if (!lpszFile || !pBuf) 31 | { 32 | return FALSE; 33 | } 34 | 35 | memset(pBuf, 0, dwSize); 36 | HANDLE hFile = ::CreateFile(lpszFile, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL); 37 | if (hFile == INVALID_HANDLE_VALUE) 38 | { 39 | return FALSE; 40 | } 41 | 42 | DWORD dwRead = 0; 43 | if (::ReadFile(hFile, pBuf, dwSize, &dwRead, NULL) && (dwRead == dwSize)) 44 | { 45 | ::CloseHandle(hFile); 46 | return TRUE; 47 | } 48 | else 49 | { 50 | ::CloseHandle(hFile); 51 | } 52 | 53 | return FALSE; 54 | } 55 | 56 | BOOL File::WriteFile(LPCTSTR lpszFile, BYTE *pBuf, DWORD dwSize) 57 | { 58 | if (!lpszFile || !pBuf) 59 | { 60 | return FALSE; 61 | } 62 | 63 | HANDLE hFile = ::CreateFile(lpszFile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL); 64 | if (hFile == INVALID_HANDLE_VALUE) 65 | { 66 | return FALSE; 67 | } 68 | 69 | DWORD dwWrite = 0; 70 | if (::WriteFile(hFile, pBuf, dwSize, &dwWrite, NULL) && (dwWrite == dwSize)) 71 | { 72 | ::CloseHandle(hFile); 73 | return TRUE; 74 | } 75 | else 76 | { 77 | ::CloseHandle(hFile); 78 | } 79 | 80 | return FALSE; 81 | } 82 | 83 | BOOL File::CreateDirectory(LPCTSTR lpszDirectory) 84 | { 85 | if (!lpszDirectory || _tcslen(lpszDirectory) <= 3) 86 | { 87 | return FALSE; 88 | } 89 | 90 | TCHAR szPath[ MAX_PATH ] = { 0 }; 91 | LPCTSTR lpszTmp = lpszDirectory + 3; 92 | BOOL bRet = FALSE; 93 | while (TRUE) 94 | { 95 | lpszTmp = ::_tcsstr(lpszTmp, _T("\\")); 96 | if (!lpszTmp) 97 | { 98 | bRet = TRUE; 99 | break; 100 | } 101 | else 102 | { 103 | lpszTmp = lpszTmp + 1; 104 | } 105 | 106 | ::memset(szPath, 0, MAX_PATH * sizeof(TCHAR)); 107 | ::memcpy_s(szPath, MAX_PATH - sizeof(TCHAR), lpszDirectory, (lpszTmp - lpszDirectory) * sizeof(TCHAR)); 108 | if (INVALID_FILE_ATTRIBUTES == ::GetFileAttributes(szPath)) 109 | { 110 | bRet = ::CreateDirectory(szPath, NULL); 111 | if (!bRet) 112 | { 113 | break; 114 | } 115 | } 116 | } 117 | 118 | return bRet; 119 | } 120 | 121 | BOOL File::DeleteDirectory(LPCTSTR lpszDirectory) 122 | { 123 | if (ClearDirectory(lpszDirectory)) 124 | { 125 | return ::RemoveDirectory(lpszDirectory); 126 | } 127 | 128 | return FALSE; 129 | } 130 | 131 | BOOL File::ClearDirectory(LPCTSTR lpszDirectory) 132 | { 133 | if (!lpszDirectory) 134 | { 135 | return FALSE; 136 | } 137 | 138 | size_t s = _tcslen(lpszDirectory); 139 | if (*(lpszDirectory + s - 1) != _T('\\')) 140 | { 141 | return FALSE; 142 | } 143 | 144 | TCHAR szPath[ MAX_PATH ] = { 0 }; 145 | _tcscpy_s(szPath, MAX_PATH - sizeof(TCHAR), lpszDirectory); 146 | 147 | TCHAR szTmp[ MAX_PATH ] = { 0 }; 148 | ::_tcscpy_s(szTmp, MAX_PATH - sizeof(TCHAR), lpszDirectory); 149 | ::_tcscat_s(szTmp, MAX_PATH - sizeof(TCHAR), _T("*.*")); 150 | 151 | WIN32_FIND_DATA findData; 152 | HANDLE hFind = ::FindFirstFile(szTmp, &findData); 153 | if (INVALID_HANDLE_VALUE == hFind) 154 | { 155 | return FALSE; 156 | } 157 | 158 | BOOL bRet = TRUE; 159 | do 160 | { 161 | if (_tcscmp(findData.cFileName, _T(".")) == 0 || 162 | _tcscmp(findData.cFileName, _T("..")) == 0) 163 | { 164 | continue; 165 | } 166 | 167 | ::memset(szTmp, 0, MAX_PATH * sizeof(TCHAR)); 168 | ::_tcscpy_s(szTmp, MAX_PATH - sizeof(TCHAR), lpszDirectory); 169 | ::_tcscat_s(szTmp, MAX_PATH - sizeof(TCHAR), findData.cFileName); 170 | 171 | if (FILE_ATTRIBUTE_DIRECTORY == (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) 172 | { 173 | size_t l = _tcslen(szTmp); 174 | if (*(szTmp + l - 1) != _T('\\')) 175 | { 176 | ::_tcscat_s(szTmp, MAX_PATH - sizeof(TCHAR), _T("\\")); 177 | } 178 | 179 | bRet = ClearDirectory(szTmp); 180 | if (!bRet) 181 | { 182 | break; 183 | } 184 | 185 | bRet = ::RemoveDirectory(szTmp); 186 | if (!bRet) 187 | { 188 | break; 189 | } 190 | 191 | continue; 192 | } 193 | 194 | bRet = ::DeleteFile(szTmp); 195 | if (!bRet) 196 | { 197 | break; 198 | } 199 | } while (::FindNextFile(hFind, &findData)); 200 | 201 | ::FindClose(hFind); 202 | return bRet; 203 | } 204 | } -------------------------------------------------------------------------------- /win/file.h: -------------------------------------------------------------------------------- 1 | #ifndef __win_file_h__ 2 | #define __win_file_h__ 3 | 4 | #include 5 | #include 6 | 7 | namespace win 8 | { 9 | class File 10 | { 11 | public: 12 | static BOOL GetFileSize(LPCTSTR lpszFile, __int64 *size); 13 | 14 | static BOOL ReadFile(LPCTSTR lpszFile, BYTE *pBuf, DWORD dwSize); 15 | 16 | static BOOL WriteFile(LPCTSTR lpszFile, BYTE *pBuf, DWORD dwSize); 17 | 18 | static BOOL CreateDirectory(LPCTSTR lpszDirectory); 19 | 20 | static BOOL DeleteDirectory(LPCTSTR lpszDirectory); 21 | 22 | static BOOL ClearDirectory(LPCTSTR lpszDirectory); 23 | }; 24 | } 25 | 26 | #endif -------------------------------------------------------------------------------- /win/font.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/myz7656/smart-ui/5b4ac60c52e54f53b68ea11c5840336b4f2b159f/win/font.h -------------------------------------------------------------------------------- /win/gdi_painter.h: -------------------------------------------------------------------------------- 1 | #ifndef __win_gdi_painter_h__ 2 | #define __win_gdi_painter_h__ 3 | 4 | #include "base/def.h" 5 | #include "core/paint.h" 6 | #include "core/types.h" 7 | 8 | namespace win 9 | { 10 | class HDCCanvas : public core::StCanvas 11 | { 12 | public: 13 | HDCCanvas(); 14 | ~HDCCanvas(); 15 | 16 | void Attatch(HDC hdc); 17 | HDC Detach(); 18 | HDC GetDC(); 19 | 20 | private: 21 | HDC hdc_; 22 | }; 23 | 24 | 25 | 26 | class WinPainterImpl : public core::StPainterImpl 27 | { 28 | public: 29 | WinPainterImpl(BOOL bLayered); 30 | ~WinPainterImpl(); 31 | 32 | public: 33 | virtual int SetClipRect( 34 | const RECT &rect); 35 | 36 | virtual bool ClearClipRect( 37 | int clip); 38 | 39 | virtual void DrawRectangle( 40 | const RECT &rect, 41 | DWORD style, 42 | core::Color color, 43 | int alpha); 44 | 45 | virtual void FillRectangle( 46 | const RECT &rect, 47 | core::Color color, 48 | int alpha); 49 | 50 | virtual void CalculateImage( 51 | const std::wstring &file, 52 | int* width, 53 | int* height); 54 | 55 | virtual void DrawImage( 56 | const std::wstring &file, 57 | const RECT &rect, 58 | int alpha); 59 | 60 | virtual void DrawFrameGridImage( 61 | const std::wstring &file, 62 | const RECT &rect, 63 | const RECT &grid, 64 | int frame_length, 65 | int frame_index, 66 | int alpha); 67 | 68 | virtual void SetTextFont( 69 | const std::wstring &family, 70 | const core::Unit &size, 71 | DWORD format); 72 | 73 | virtual void DrawString( 74 | const std::wstring &text, 75 | const core::RECTF &rect, 76 | core::Color color, 77 | DWORD align, 78 | int alpha); 79 | 80 | virtual int GetCharacterPosition( 81 | const std::wstring &text, 82 | float &width); 83 | 84 | virtual void DrawLine( 85 | POINT start, 86 | POINT stop, 87 | int nStrokeWidth, 88 | core::Color color, 89 | DWORD style, 90 | int alpha); 91 | 92 | virtual void DrawRoundRect( 93 | const RECT& rect, 94 | int nRadiusWidth, 95 | int nRadiusHeight, 96 | core::Color crBackground, 97 | int alpha 98 | ); 99 | 100 | virtual void DrawRoundBorder( 101 | const RECT& rect, 102 | int nRadiusWidth, 103 | int nRadiusHeight, 104 | DWORD styleBorder, 105 | int nBoderWidth, 106 | core::Color crBorder, 107 | int alpha); 108 | 109 | protected: 110 | HDC GetDC(); 111 | 112 | private: 113 | ULONG_PTR gdi_token_; 114 | 115 | std::wstring font_family_; 116 | int font_size_; 117 | DWORD font_style_; 118 | BOOL layered_; 119 | 120 | static const int DEFAULT_FONT_SIZE = 12; 121 | static const int DEFAULT_FONT_RATE = 100; 122 | static const int MAX_ALPHA = 255; 123 | static const int DEFAULT_BLACK_COLOR = RGB(0x01, 0x01, 0x01); 124 | 125 | private: 126 | DISABLE_COPY_AND_ASSIGN(WinPainterImpl) 127 | }; 128 | 129 | 130 | 131 | class WinPainter : public core::StPainter 132 | { 133 | public: 134 | WinPainter(HDCCanvas* canvas, BOOL bLayered= FALSE); 135 | ~WinPainter(); 136 | 137 | protected: 138 | void ReleasePainterImpl(); 139 | core::StPainterImpl* GetPainterImpl(); 140 | 141 | private: 142 | WinPainterImpl* impl_; 143 | HDCCanvas* canvas_; 144 | BOOL layered_; 145 | 146 | private: 147 | DISABLE_COPY_AND_ASSIGN(WinPainter) 148 | }; 149 | } 150 | 151 | #endif -------------------------------------------------------------------------------- /win/image.cpp: -------------------------------------------------------------------------------- 1 | #include "win/image.h" 2 | #include "win/dc.h" 3 | 4 | namespace win 5 | { 6 | Image::Image() 7 | : width_(0) 8 | , height_(0) 9 | , bitmap_(NULL) 10 | , bytes_(NULL) {} 11 | 12 | Image::~Image() {} 13 | 14 | BOOL Image::Copy(Image* image) 15 | { 16 | if (!image) 17 | { 18 | return FALSE; 19 | } 20 | 21 | if(image->bitmap_) 22 | { 23 | width_ = image->width_; 24 | height_ = image->height_; 25 | bitmap_ = image->bitmap_; 26 | bytes_ = image->bytes_; 27 | return TRUE; 28 | } 29 | 30 | return FALSE; 31 | } 32 | 33 | BOOL Image::CreateFromImage(Gdiplus::Image * image) 34 | { 35 | if(!image) 36 | { 37 | return FALSE; 38 | } 39 | 40 | int width = image->GetWidth(); 41 | int height = image->GetHeight(); 42 | 43 | if (!CreateDIBSection(width, height)) 44 | { 45 | return FALSE; 46 | } 47 | 48 | WinMemoryDC mem_dc(bitmap_); 49 | Gdiplus::Graphics g((HDC)mem_dc); 50 | g.DrawImage(image, 51 | Gdiplus::Rect(0, 0, width, height), 52 | 0, 0, width, height, 53 | Gdiplus::UnitPixel ); 54 | 55 | return TRUE; 56 | } 57 | 58 | BOOL Image::CreateDIBSection(int width, int height) 59 | { 60 | if (width <= 0 || height <= 0) 61 | { 62 | return FALSE; 63 | } 64 | 65 | BITMAPINFO bmi; 66 | ZeroMemory(&bmi, sizeof(BITMAPINFO)); 67 | bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); 68 | bmi.bmiHeader.biWidth = width; 69 | bmi.bmiHeader.biHeight = height; 70 | bmi.bmiHeader.biPlanes = 1; 71 | bmi.bmiHeader.biBitCount = 32; 72 | bmi.bmiHeader.biCompression = BI_RGB; 73 | bmi.bmiHeader.biSizeImage = width * height * 4; 74 | 75 | if (bitmap_ != NULL) 76 | { 77 | ::DeleteObject(bitmap_); 78 | } 79 | 80 | width_ = 0; 81 | height_ = 0; 82 | bytes_ = NULL; 83 | bitmap_ = NULL; 84 | 85 | BYTE* bits = NULL; 86 | bitmap_ = ::CreateDIBSection(NULL, (BITMAPINFO *)&bmi, DIB_RGB_COLORS, (VOID**)&bits, NULL, 0); 87 | 88 | if (!bits) 89 | { 90 | return FALSE; 91 | } 92 | 93 | width_ = width; 94 | height_ = height; 95 | bytes_ = bits; 96 | ::memset((void *)bytes_, 0, width_ * height_ * 4); 97 | 98 | return TRUE; 99 | } 100 | 101 | BOOL Image::Release() 102 | { 103 | if (bitmap_) 104 | ::DeleteObject(bitmap_); 105 | 106 | bitmap_ = 0; 107 | width_ = 0; 108 | height_ = 0; 109 | bytes_ = 0; 110 | 111 | return TRUE; 112 | } 113 | 114 | BOOL Image::SetAlpha(BYTE alpha, DWORD color) 115 | { 116 | if (!bytes_) 117 | { 118 | return FALSE; 119 | } 120 | 121 | DWORD *pixel = (DWORD*)bytes_; 122 | int pixel_count = height_ * width_; 123 | 124 | for (int i = 0; i < pixel_count; i++, pixel++) 125 | { 126 | if (*pixel == color) 127 | ((PBYTE)pixel)[3] = alpha; 128 | } 129 | 130 | return TRUE; 131 | } 132 | 133 | BOOL Image::TileBlt(HDC hdc, RECT dest, int alpha) 134 | { 135 | WinMemoryDC MemDC( bitmap_ ); 136 | 137 | int x = dest.left; 138 | int y = dest.top; 139 | int width = dest.right - dest.left; 140 | int height = dest.bottom - dest.top; 141 | 142 | BLENDFUNCTION blend; 143 | blend.BlendOp = AC_SRC_OVER; 144 | blend.BlendFlags = 0; 145 | blend.SourceConstantAlpha = (BYTE)alpha; 146 | blend.AlphaFormat = AC_SRC_ALPHA; 147 | 148 | ::AlphaBlend( hdc, x, y, width, height, MemDC, 0, 0, width_, height_, blend ); 149 | 150 | return TRUE; 151 | } 152 | 153 | BOOL Image::GridBlt(HDC hdc, RECT dest, RECT grid, int alpha) 154 | { 155 | return FrameGridBlt(hdc, dest, grid, 0, 0, alpha); 156 | } 157 | 158 | BOOL Image::FrameBlt(HDC hdc, RECT dest, int length, int index, int alpha) 159 | { 160 | RECT grid = {0}; 161 | return FrameGridBlt(hdc, dest, grid, length, index, alpha); 162 | } 163 | 164 | BOOL Image::FrameGridBlt(HDC hdc, RECT dest, RECT grid, int length, int index, int alpha) 165 | { 166 | WinMemoryDC MemDC(bitmap_); 167 | 168 | BLENDFUNCTION blend; 169 | blend.BlendOp = AC_SRC_OVER; 170 | blend.BlendFlags = 0; 171 | blend.SourceConstantAlpha = (BYTE)alpha; 172 | blend.AlphaFormat = AC_SRC_ALPHA; 173 | 174 | if (length == 0) 175 | { 176 | length = width_; 177 | } 178 | 179 | int count = width_ / length; 180 | index = index % count; 181 | 182 | int left = index * length; 183 | int top = 0; 184 | int right = (index + 1) * length; 185 | int bottom = height_; 186 | 187 | int grid_left = grid.left; 188 | int grid_top = grid.top; 189 | int grid_right = grid.right; 190 | int grid_bottom = grid.bottom; 191 | 192 | if (grid_left > 0 && grid_top > 0) 193 | { 194 | ::AlphaBlend(hdc, 195 | dest.left, dest.top, grid_left, grid_top, 196 | MemDC, 197 | left, top, grid_left, grid_top, 198 | blend); 199 | } 200 | 201 | if (grid_top > 0 && grid_right > 0) 202 | { 203 | ::AlphaBlend(hdc, 204 | dest.right - grid_right, dest.top, grid_right, grid_top, 205 | MemDC, 206 | right - grid_right, top, grid_right, grid_top, 207 | blend); 208 | } 209 | 210 | if (grid_left > 0 && grid_bottom > 0) 211 | { 212 | ::AlphaBlend(hdc, 213 | dest.left, dest.bottom - grid_bottom, grid_left, grid_bottom, 214 | MemDC, 215 | left, bottom - grid_bottom, grid_left, grid_bottom, 216 | blend); 217 | } 218 | 219 | if (grid_right > 0 && grid_bottom > 0) 220 | { 221 | ::AlphaBlend(hdc, 222 | dest.right - grid_right, dest.bottom - grid_bottom, grid_right, grid_bottom, 223 | MemDC, 224 | right - grid_right, bottom - grid_bottom, grid_right, grid_bottom, 225 | blend); 226 | } 227 | 228 | if (grid_left > 0) 229 | { 230 | int heightDest = dest.bottom - dest.top - grid_top - grid_bottom; 231 | int heightImage = bottom - top - grid_top - grid_bottom; 232 | ::AlphaBlend(hdc, 233 | dest.left, dest.top + grid_top, grid_left, heightDest, 234 | MemDC, 235 | left, top + grid_top, grid_left, heightImage, 236 | blend); 237 | } 238 | 239 | if (grid_top > 0) 240 | { 241 | int widthDest = dest.right - dest.left - grid_left - grid_right; 242 | int widthImage = right - left - grid_left - grid_right; 243 | ::AlphaBlend(hdc, 244 | dest.left + grid_left, dest.top, widthDest, grid_top, 245 | MemDC, 246 | left + grid_left, top, widthImage, grid_top, 247 | blend); 248 | } 249 | 250 | if (grid_right > 0) 251 | { 252 | int heightDest = dest.bottom - dest.top - grid_top - grid_bottom; 253 | int heightImage = bottom - top - grid_top - grid_bottom; 254 | ::AlphaBlend(hdc, 255 | dest.right - grid_right, dest.top + grid_top, grid_right, heightDest, 256 | MemDC, 257 | right - grid_right, top + grid_top, grid_right, heightImage, 258 | blend); 259 | } 260 | 261 | if (grid_bottom > 0) 262 | { 263 | int widthDest = dest.right - dest.left - grid_right - grid_left; 264 | int widthImage = right - left - grid_right - grid_left; 265 | ::AlphaBlend(hdc, 266 | dest.left + grid_left, dest.bottom - grid_bottom, widthDest, grid_bottom, 267 | MemDC, 268 | left + grid_left, bottom - grid_bottom, widthImage, grid_bottom, 269 | blend); 270 | } 271 | 272 | if (true) 273 | { 274 | int widthDest = dest.right - dest.left - grid_left - grid_right; 275 | int widthImage = right - left - grid_left - grid_right; 276 | int heightDest = dest.bottom - dest.top - grid_top - grid_bottom; 277 | int heightImage = bottom - top - grid_top - grid_bottom; 278 | ::AlphaBlend(hdc, 279 | dest.left + grid_left, dest.top + grid_top, widthDest, heightDest, 280 | MemDC, 281 | left + grid_left, top + grid_top, widthImage, heightImage, 282 | blend); 283 | } 284 | 285 | return TRUE; 286 | } 287 | } -------------------------------------------------------------------------------- /win/image.h: -------------------------------------------------------------------------------- 1 | #ifndef __win_image_h__ 2 | #define __win_image_h__ 3 | 4 | #include "base/def.h" 5 | 6 | #include 7 | #include 8 | #pragma comment(lib, "gdiplus.lib") 9 | 10 | #include 11 | #pragma comment(lib, "msimg32.lib") 12 | 13 | namespace win 14 | { 15 | class Image 16 | { 17 | public: 18 | Image(); 19 | ~Image(); 20 | 21 | BOOL Copy(Image* image); 22 | BOOL Release(); 23 | 24 | BOOL CreateFromImage(Gdiplus::Image * image); 25 | BOOL CreateDIBSection(int width, int height); 26 | 27 | BOOL SetAlpha(BYTE alpha, DWORD color); 28 | 29 | BOOL TileBlt(HDC hdc, RECT dest, int alpha); 30 | BOOL GridBlt(HDC hdc, RECT dest, RECT grid, int alpha); 31 | BOOL FrameBlt(HDC hdc, RECT dest, int length, int index, int alpha); 32 | BOOL FrameGridBlt(HDC hdc, RECT dest, RECT grid, int length, int index, int alpha); 33 | 34 | int Width() { return width_; } 35 | int Height(){ return height_; } 36 | operator HBITMAP(){ return bitmap_; } 37 | 38 | private: 39 | int width_; 40 | int height_; 41 | HBITMAP bitmap_; 42 | BYTE* bytes_; 43 | 44 | private: 45 | DISABLE_COPY_AND_ASSIGN(Image) 46 | }; 47 | } 48 | 49 | #endif -------------------------------------------------------------------------------- /win/layered_window.h: -------------------------------------------------------------------------------- 1 | #ifndef __win_layered_window_h__ 2 | #define __win_layered_window_h__ 3 | 4 | #include "win/window.h" 5 | 6 | typedef CWinTraits LayeredWinImplTraits; 7 | 8 | namespace win 9 | { 10 | template 11 | class LayeredWindow 12 | : public Window 13 | { 14 | public: 15 | DECLARE_WND_CLASS(L"LayeredWindow") 16 | 17 | LayeredWindow() 18 | : alpha_(255) {} 19 | 20 | ~LayeredWindow() {} 21 | 22 | void SetWindowAlpha(BYTE alpha) 23 | { 24 | alpha_ = alpha; 25 | } 26 | 27 | virtual void UpdateRect(HDC hDC, RECT rcUpdate) 28 | { 29 | RECT rcWnd; 30 | ::GetClientRect(m_hWnd, &rcWnd); 31 | int width = rcWnd.right - rcWnd.left; 32 | int height = rcWnd.bottom - rcWnd.top; 33 | 34 | HDC hMemDC = ::CreateCompatibleDC(hDC); 35 | HBITMAP hBitmap = (HBITMAP)::SelectObject(hMemDC, (HBITMAP)image_cache_); 36 | ::FillRect(hMemDC, &rcUpdate, (HBRUSH)::GetStockObject(BLACK_BRUSH)); 37 | 38 | DrawRect(hMemDC, rcUpdate); 39 | 40 | POINT ptDst = { rcWnd.left, rcWnd.top }; 41 | ::ClientToScreen(m_hWnd, &ptDst); 42 | POINT ptSrc = { 0, 0 }; 43 | SIZE szWnd = { width, height }; 44 | 45 | BLENDFUNCTION blend; 46 | blend.BlendOp = AC_SRC_OVER; 47 | blend.BlendFlags = 0; 48 | blend.SourceConstantAlpha = (BYTE)alpha_; 49 | blend.AlphaFormat = AC_SRC_ALPHA; 50 | ::UpdateLayeredWindow(m_hWnd, hDC, &ptDst, &szWnd, hMemDC, &ptSrc, 0, &blend, ULW_ALPHA); 51 | 52 | ::SelectObject(hMemDC, hBitmap); 53 | ::DeleteDC(hMemDC); 54 | } 55 | 56 | protected: 57 | int alpha_; 58 | }; 59 | } 60 | 61 | #endif -------------------------------------------------------------------------------- /win/tooltip.h: -------------------------------------------------------------------------------- 1 | #ifndef __win_tooltip_h__ 2 | #define __win_tooltip_h__ 3 | 4 | #include 5 | 6 | namespace win 7 | { 8 | class ToolTip 9 | { 10 | public: 11 | ToolTip() 12 | : tip_wnd_(NULL) 13 | { 14 | memset(&tool_info_, 0, sizeof(TOOLINFO)); 15 | tool_info_.cbSize = sizeof(TOOLINFO); 16 | } 17 | 18 | BOOL Create(HWND hParent) 19 | { 20 | tip_wnd_ = ::CreateWindowEx(WS_EX_TOPMOST, TOOLTIPS_CLASS, NULL, 21 | WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP, CW_USEDEFAULT, CW_USEDEFAULT, 22 | CW_USEDEFAULT, CW_USEDEFAULT, hParent, NULL, NULL, NULL); 23 | 24 | tool_info_.hwnd = ::GetParent(tip_wnd_); 25 | tool_info_.uFlags = TTF_IDISHWND; 26 | tool_info_.uId = (UINT_PTR)tool_info_.hwnd; 27 | 28 | ::SendMessage(tip_wnd_, TTM_ADDTOOL, 0, (LPARAM)&tool_info_); 29 | 30 | return TRUE; 31 | } 32 | 33 | int SetMaxTipWidth(int iWidth) 34 | { 35 | return (int) ::SendMessage(tip_wnd_, TTM_SETMAXTIPWIDTH, 0, iWidth); 36 | } 37 | 38 | void Activate(BOOL bActivate) 39 | { 40 | ::SendMessage(tip_wnd_, TTM_ACTIVATE, bActivate, 0L); 41 | } 42 | 43 | BOOL IsWindow() 44 | { 45 | return ::IsWindow(tip_wnd_); 46 | } 47 | 48 | void RelayEvent(LPMSG lpMsg) 49 | { 50 | ::SendMessage(tip_wnd_, TTM_RELAYEVENT, 0, (LPARAM)lpMsg); 51 | } 52 | 53 | void UpdateTipText(LPCTSTR lpszText) 54 | { 55 | std::wstring text; 56 | if (lpszText) 57 | { 58 | text = lpszText; 59 | } 60 | 61 | if (text != str_old_tip_) 62 | { 63 | str_old_tip_ = text; 64 | 65 | tool_info_.lpszText = (LPTSTR)text.c_str(); 66 | ::SendMessage(tip_wnd_, TTM_UPDATETIPTEXT, 0, (LPARAM)&tool_info_); 67 | } 68 | } 69 | 70 | private: 71 | std::wstring str_old_tip_; 72 | HWND tip_wnd_; 73 | TOOLINFO tool_info_; 74 | }; 75 | 76 | } 77 | 78 | #endif -------------------------------------------------------------------------------- /win/widget_root.cpp: -------------------------------------------------------------------------------- 1 | #include "win/widget_root.h" 2 | #include "util/string.h" 3 | 4 | 5 | namespace win 6 | { 7 | WidgetRoot::WidgetRoot() 8 | : body_(0) 9 | , active_(0) 10 | , context_(0) 11 | { 12 | builder_ = new Builder(); 13 | } 14 | 15 | WidgetRoot::~WidgetRoot() 16 | { 17 | if (body_) 18 | { 19 | body_->Drop(); 20 | body_ = 0; 21 | active_ = 0; 22 | } 23 | 24 | if (builder_) 25 | { 26 | delete builder_; 27 | builder_ = 0; 28 | } 29 | } 30 | 31 | BOOL WidgetRoot::ParseXml(LPCTSTR file) 32 | { 33 | if (util::String::IsEmpty(file)) 34 | { 35 | return false; 36 | } 37 | 38 | Builder* builder = GetBuilder(); 39 | if (!builder) 40 | { 41 | return false; 42 | } 43 | 44 | if (body_) 45 | { 46 | body_->Drop(); 47 | body_ = 0; 48 | } 49 | 50 | active_ = 0; 51 | body_ = static_cast(builder->Parse(file)); 52 | body_->SetContext(context_); 53 | 54 | return body_ != 0; 55 | } 56 | 57 | Builder* WidgetRoot::GetBuilder() 58 | { 59 | return builder_; 60 | } 61 | 62 | core::StWidget* WidgetRoot::Body() 63 | { 64 | return body_; 65 | } 66 | 67 | core::StWidget* WidgetRoot::Active() 68 | { 69 | return active_; 70 | } 71 | 72 | void WidgetRoot::SetActive(core::StWidget* active) 73 | { 74 | active_ = active; 75 | } 76 | 77 | void WidgetRoot::SetRuntimeContext(core::Context* context) 78 | { 79 | context_ = context; 80 | if (context_) 81 | { 82 | context_->SetWidgetRoot(this); 83 | } 84 | } 85 | 86 | core::StWidget* WidgetRoot::FindWidget(const std::string& id) 87 | { 88 | if (body_) 89 | { 90 | return body_->FindWidget(id); 91 | } 92 | return 0; 93 | } 94 | 95 | core::Context* WidgetRoot::GetContext() 96 | { 97 | return context_; 98 | } 99 | } -------------------------------------------------------------------------------- /win/widget_root.h: -------------------------------------------------------------------------------- 1 | #ifndef __win_widget_root_h__ 2 | #define __win_widget_root_h__ 3 | 4 | #include "core/widget.h" 5 | #include "core/container.h" 6 | #include "core/style.h" 7 | #include "win/builder.h" 8 | 9 | namespace win 10 | { 11 | class WidgetRoot 12 | { 13 | public: 14 | WidgetRoot(); 15 | ~WidgetRoot(); 16 | 17 | BOOL ParseXml(LPCTSTR file); 18 | 19 | Builder* GetBuilder(); 20 | core::StWidget* Body(); 21 | core::StWidget* Active(); 22 | void SetActive(core::StWidget* active); 23 | 24 | void SetRuntimeContext(core::Context* context); 25 | 26 | core::StWidget* FindWidget(const std::string& id); 27 | core::Context* GetContext(); 28 | 29 | private: 30 | core::StContainer* body_; 31 | core::StWidget* active_; 32 | Builder* builder_; 33 | core::Context* context_; 34 | 35 | private: 36 | DISABLE_COPY_AND_ASSIGN(WidgetRoot) 37 | }; 38 | } 39 | 40 | #endif -------------------------------------------------------------------------------- /xml/xmlerror.cpp: -------------------------------------------------------------------------------- 1 | #include "xml/xml.h" 2 | 3 | namespace xml 4 | { 5 | 6 | 7 | 8 | const char* XmlBase::errorString[ XmlBase::XML_ERROR_STRING_COUNT ] = 9 | { 10 | "No error", 11 | "Error", 12 | "Failed to open file", 13 | "Error parsing Element.", 14 | "Failed to read Element name", 15 | "Error reading Element value.", 16 | "Error reading Attributes.", 17 | "Error: empty tag.", 18 | "Error reading end tag.", 19 | "Error parsing Unknown.", 20 | "Error parsing Comment.", 21 | "Error parsing Declaration.", 22 | "Error document empty.", 23 | "Error null (0) or unexpected EOF found in input stream.", 24 | "Error parsing CDATA.", 25 | "Error when XmlDocument added to document, because XmlDocument can only be at the root.", 26 | }; 27 | 28 | 29 | 30 | } -------------------------------------------------------------------------------- /xml/xmlstr.cpp: -------------------------------------------------------------------------------- 1 | #include "xml/xmlstr.h" 2 | 3 | namespace xml 4 | { 5 | 6 | 7 | 8 | const XmlString::size_type XmlString::npos = static_cast< XmlString::size_type >(-1); 9 | 10 | XmlString::Rep XmlString::nullrep_ = { 0, 0, { '\0' } }; 11 | 12 | void XmlString::reserve (size_type cap) 13 | { 14 | if (cap > capacity()) 15 | { 16 | XmlString tmp; 17 | tmp.init(length(), cap); 18 | memcpy(tmp.start(), data(), length()); 19 | swap(tmp); 20 | } 21 | } 22 | 23 | XmlString& XmlString::assign(const char* str, size_type len) 24 | { 25 | size_type cap = capacity(); 26 | if (len > cap || cap > 3*(len + 8)) 27 | { 28 | XmlString tmp; 29 | tmp.init(len); 30 | memcpy(tmp.start(), str, len); 31 | swap(tmp); 32 | } 33 | else 34 | { 35 | memmove(start(), str, len); 36 | set_size(len); 37 | } 38 | return *this; 39 | } 40 | 41 | XmlString& XmlString::append(const char* str, size_type len) 42 | { 43 | size_type newsize = length() + len; 44 | if (newsize > capacity()) 45 | { 46 | reserve (newsize + capacity()); 47 | } 48 | memmove(finish(), str, len); 49 | set_size(newsize); 50 | return *this; 51 | } 52 | 53 | XmlString operator + (const XmlString & a, const XmlString & b) 54 | { 55 | XmlString tmp; 56 | tmp.reserve(a.length() + b.length()); 57 | tmp += a; 58 | tmp += b; 59 | return tmp; 60 | } 61 | 62 | XmlString operator + (const XmlString & a, const char* b) 63 | { 64 | XmlString tmp; 65 | XmlString::size_type b_len = static_cast( strlen(b) ); 66 | tmp.reserve(a.length() + b_len); 67 | tmp += a; 68 | tmp.append(b, b_len); 69 | return tmp; 70 | } 71 | 72 | XmlString operator + (const char* a, const XmlString & b) 73 | { 74 | XmlString tmp; 75 | XmlString::size_type a_len = static_cast( strlen(a) ); 76 | tmp.reserve(a_len + b.length()); 77 | tmp.append(a, a_len); 78 | tmp += b; 79 | return tmp; 80 | } 81 | 82 | 83 | 84 | } -------------------------------------------------------------------------------- /xml/xmlstr.h: -------------------------------------------------------------------------------- 1 | #ifndef __xml_xml_str_h__ 2 | #define __xml_xml_str_h__ 3 | 4 | #include 5 | #include 6 | 7 | #if defined(_MSC_VER) && (_MSC_VER >= 1200 ) 8 | #define XML_EXPLICIT explicit 9 | #elif defined(__GNUC__) && (__GNUC__ >= 3 ) 10 | #define XML_EXPLICIT explicit 11 | #else 12 | #define XML_EXPLICIT 13 | #endif 14 | 15 | namespace xml 16 | { 17 | 18 | 19 | 20 | class XmlString 21 | { 22 | public : 23 | typedef size_t size_type; 24 | 25 | static const size_type npos; 26 | 27 | XmlString () : rep_(&nullrep_) 28 | { 29 | } 30 | 31 | XmlString ( const XmlString & copy) : rep_(0) 32 | { 33 | init(copy.length()); 34 | memcpy(start(), copy.data(), length()); 35 | } 36 | 37 | XML_EXPLICIT XmlString ( const char * copy) : rep_(0) 38 | { 39 | init( static_cast( strlen(copy) )); 40 | memcpy(start(), copy, length()); 41 | } 42 | 43 | XML_EXPLICIT XmlString ( const char * str, size_type len) : rep_(0) 44 | { 45 | init(len); 46 | memcpy(start(), str, len); 47 | } 48 | 49 | ~XmlString () 50 | { 51 | quit(); 52 | } 53 | 54 | XmlString& operator = (const char * copy) 55 | { 56 | return assign( copy, (size_type)strlen(copy)); 57 | } 58 | 59 | XmlString& operator = (const XmlString & copy) 60 | { 61 | return assign(copy.start(), copy.length()); 62 | } 63 | 64 | XmlString& operator += (const char * suffix) 65 | { 66 | return append(suffix, static_cast( strlen(suffix) )); 67 | } 68 | 69 | XmlString& operator += (char single) 70 | { 71 | return append(&single, 1); 72 | } 73 | 74 | XmlString& operator += (const XmlString & suffix) 75 | { 76 | return append(suffix.data(), suffix.length()); 77 | } 78 | 79 | const char * c_str () const { return rep_->str; } 80 | 81 | const char * data () const { return rep_->str; } 82 | 83 | size_type length () const { return rep_->size; } 84 | 85 | size_type size () const { return rep_->size; } 86 | 87 | bool empty () const { return rep_->size == 0; } 88 | 89 | size_type capacity () const { return rep_->capacity; } 90 | 91 | const char& at (size_type index) const 92 | { 93 | assert( index < length() ); 94 | return rep_->str[ index ]; 95 | } 96 | 97 | char& operator [] (size_type index) const 98 | { 99 | assert( index < length() ); 100 | return rep_->str[ index ]; 101 | } 102 | 103 | size_type find (char lookup) const 104 | { 105 | return find(lookup, 0); 106 | } 107 | 108 | size_type find (char tofind, size_type offset) const 109 | { 110 | if (offset >= length()) return npos; 111 | 112 | for (const char* p = c_str() + offset; *p != '\0'; ++p) 113 | { 114 | if (*p == tofind) return static_cast< size_type >( p - c_str() ); 115 | } 116 | return npos; 117 | } 118 | 119 | void clear () 120 | { 121 | quit(); 122 | init(0,0); 123 | } 124 | 125 | void reserve (size_type cap); 126 | 127 | XmlString& assign (const char* str, size_type len); 128 | 129 | XmlString& append (const char* str, size_type len); 130 | 131 | void swap (XmlString& other) 132 | { 133 | Rep* r = rep_; 134 | rep_ = other.rep_; 135 | other.rep_ = r; 136 | } 137 | 138 | private: 139 | void init(size_type sz) { init(sz, sz); } 140 | void set_size(size_type sz) { rep_->str[ rep_->size = sz ] = '\0'; } 141 | char* start() const { return rep_->str; } 142 | char* finish() const { return rep_->str + rep_->size; } 143 | 144 | struct Rep 145 | { 146 | size_type size, capacity; 147 | char str[1]; 148 | }; 149 | 150 | void init(size_type sz, size_type cap) 151 | { 152 | if (cap) 153 | { 154 | const size_type bytesNeeded = sizeof(Rep) + cap; 155 | const size_type intsNeeded = ( bytesNeeded + sizeof(int) - 1 ) / sizeof( int ); 156 | rep_ = reinterpret_cast( new int[ intsNeeded ] ); 157 | 158 | rep_->str[ rep_->size = sz ] = '\0'; 159 | rep_->capacity = cap; 160 | } 161 | else 162 | { 163 | rep_ = &nullrep_; 164 | } 165 | } 166 | 167 | void quit() 168 | { 169 | if (rep_ != &nullrep_) 170 | { 171 | delete [] ( reinterpret_cast( rep_ ) ); 172 | } 173 | } 174 | 175 | Rep * rep_; 176 | static Rep nullrep_; 177 | } ; 178 | 179 | 180 | inline bool operator == (const XmlString & a, const XmlString & b) 181 | { 182 | return ( a.length() == b.length() ) 183 | && ( strcmp(a.c_str(), b.c_str()) == 0 ); 184 | } 185 | 186 | inline bool operator < (const XmlString & a, const XmlString & b) 187 | { 188 | return strcmp(a.c_str(), b.c_str()) < 0; 189 | } 190 | 191 | inline bool operator != (const XmlString & a, const XmlString & b) { return !(a == b); } 192 | inline bool operator > (const XmlString & a, const XmlString & b) { return b < a; } 193 | inline bool operator <= (const XmlString & a, const XmlString & b) { return !(b < a); } 194 | inline bool operator >= (const XmlString & a, const XmlString & b) { return !(a < b); } 195 | 196 | inline bool operator == (const XmlString & a, const char* b) { return strcmp(a.c_str(), b) == 0; } 197 | inline bool operator == (const char* a, const XmlString & b) { return b == a; } 198 | inline bool operator != (const XmlString & a, const char* b) { return !(a == b); } 199 | inline bool operator != (const char* a, const XmlString & b) { return !(b == a); } 200 | 201 | XmlString operator + (const XmlString & a, const XmlString & b); 202 | XmlString operator + (const XmlString & a, const char* b); 203 | XmlString operator + (const char* a, const XmlString & b); 204 | 205 | 206 | class XmlOutStream : public XmlString 207 | { 208 | public : 209 | XmlOutStream & operator << (const XmlString & in) 210 | { 211 | *this += in; 212 | return *this; 213 | } 214 | 215 | XmlOutStream & operator << (const char * in) 216 | { 217 | *this += in; 218 | return *this; 219 | } 220 | }; 221 | 222 | 223 | 224 | } 225 | #endif --------------------------------------------------------------------------------