File: PropertyFontSourceControl.cpp

package info (click to toggle)
mygui 3.2.2%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 36,224 kB
  • sloc: cpp: 118,031; ansic: 30,202; xml: 15,544; cs: 12,602; tcl: 776; python: 417; makefile: 34
file content (139 lines) | stat: -rw-r--r-- 3,694 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*!
	@file
	@author		Albert Semenov
	@date		08/2010
*/

#include "Precompiled.h"
#include "PropertyFontSourceControl.h"
#include "FactoryManager.h"

namespace tools
{
	namespace
	{

		const char* const fileTypesData[] = { "*.ttf", "*.ttc", "*.otf", "*.pfa", "*.pfb", "*.fon", "*.fnt" };

		const MyGUI::VectorString fileTypes(fileTypesData, fileTypesData + sizeof fileTypesData / sizeof *fileTypesData);

		struct StrCmpI : public std::binary_function<std::string, std::string, bool>
		{
			result_type operator()(const first_argument_type& _a, const second_argument_type& _b)
			{
				size_t aLength = _a.length(), bLength = _b.length(), length = (std::min)(aLength, bLength);
				first_argument_type::const_iterator aIter = _a.begin();
				second_argument_type::const_iterator bIter = _b.begin();

				while (length-- > 0)
				{
					int aUpper = toupper(*aIter++), bUpper = toupper(*bIter++);

					if (aUpper != bUpper)
						return aUpper < bUpper;
				}

				return aLength < bLength;
			}
		};

	}

	FACTORY_ITEM_ATTRIBUTE(PropertyFontSourceControl)

	PropertyFontSourceControl::PropertyFontSourceControl() :
		mName(nullptr),
		mComboBox(nullptr)
	{
	}

	PropertyFontSourceControl::~PropertyFontSourceControl()
	{
		mComboBox->eventComboChangePosition -= MyGUI::newDelegate(this, &PropertyFontSourceControl::notifyComboChangePosition);
	}

	void PropertyFontSourceControl::OnInitialise(Control* _parent, MyGUI::Widget* _place, const std::string& _layoutName)
	{
		PropertyControl::OnInitialise(_parent, _place, "PropertyComboBoxControl.layout");

		assignWidget(mName, "Name", false);
		assignWidget(mComboBox, "ComboBox");

		fillResources();

		for (MyGUI::VectorString::const_iterator item = mResources.begin(); item != mResources.end(); ++item)
			mComboBox->addItem((*item));

		mComboBox->beginToItemFirst();

		mComboBox->eventComboChangePosition += MyGUI::newDelegate(this, &PropertyFontSourceControl::notifyComboChangePosition);
	}

	void PropertyFontSourceControl::updateCaption()
	{
		PropertyPtr proper = getProperty();
		if (proper != nullptr)
			mName->setCaption(proper->getType()->getName());
	}

	void PropertyFontSourceControl::updateProperty()
	{
		PropertyPtr proper = getProperty();
		if (proper != nullptr)
		{
			mComboBox->setEnabled(!proper->getType()->getReadOnly());

			size_t index = getComboIndex(proper->getValue());
			mComboBox->setIndexSelected(index);
		}
		else
		{
			mComboBox->setIndexSelected(MyGUI::ITEM_NONE);
			mComboBox->setEnabled(false);
		}
	}

	void PropertyFontSourceControl::notifyComboChangePosition(MyGUI::ComboBox* _sender, size_t _index)
	{
		PropertyPtr proper = getProperty();
		if (proper != nullptr)
		{
			std::string value = _index != MyGUI::ITEM_NONE ? mComboBox->getItemNameAt(_index) : "";
			executeAction(value);
		}
	}

	size_t PropertyFontSourceControl::getComboIndex(const MyGUI::UString& _name)
	{
		size_t result = MyGUI::ITEM_NONE;

		size_t count = mComboBox->getItemCount();
		for (size_t index = 0; index < count; ++index)
		{
			if (mComboBox->getItemNameAt(index) == _name)
			{
				result = index;
				break;
			}
		}

		return result;
	}

	void PropertyFontSourceControl::fillResources()
	{
		mResources.clear();

		std::set<std::string, StrCmpI> allFilenames;

		for (MyGUI::VectorString::const_iterator fileType = fileTypes.begin(); fileType != fileTypes.end(); ++fileType)
		{
			const MyGUI::VectorString& filenames = MyGUI::DataManager::getInstance().getDataListNames(*fileType);
			allFilenames.insert(filenames.begin(), filenames.end());
		}

		for (std::set<std::string, StrCmpI>::const_iterator iter = allFilenames.begin(); iter != allFilenames.end(); ++iter)
			mResources.push_back(*iter);
	}

}