Welcome to mirror list, hosted at ThFree Co, Russian Federation.

SearchForm.cpp « src « Sloynik « bada_sloynik - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 705559c937d61421fea1a2ad2bfc82f682c6b3af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#include "SearchForm.h"
#include "ArticleForm.h"
#include "../../../words/sloynik_engine.hpp"
#include <FApp.h>
#include <locale>


using namespace Osp::Base;
using namespace Osp::Ui;
using namespace Osp::Ui::Controls;

namespace
{
  sl::StrFn::Str const * StrCreate(char const * utf8Data, uint32_t size)
  {
    return reinterpret_cast<sl::StrFn::Str *>(new string(utf8Data, size));
  }

  void StrDestroy(sl::StrFn::Str const * s)
  {
    delete reinterpret_cast<string const *>(s);
  }

  int StrSecondaryCompare(void *,
      sl::StrFn::Str const * pa, sl::StrFn::Str const * pb)
  {
    string const & a = *reinterpret_cast<string const *>(pa);
    string const & b = *reinterpret_cast<string const *>(pb);
    return a == b ? 0 : (a < b ? -1 : 1);
  }

  int StrPrimaryCompare(void *,
      sl::StrFn::Str const * pa, sl::StrFn::Str const * pb)
  {
    string s1(*reinterpret_cast<string const *>(pa));
    string s2(*reinterpret_cast<string const *>(pb));
    std::use_facet<std::ctype<char> >(
        std::locale()).tolower(&s1[0], &s1[0] + s1.size());
    std::use_facet<std::ctype<char> >(
        std::locale()).tolower(&s2[0], &s2[0] + s2.size());
    return s1 == s2 ? 0 : (s1 < s2 ? -1 : 1);
  }
}


SearchForm::SearchForm(void) : m_pEngine(NULL), m_pArticleForm(NULL)
{
}

SearchForm::~SearchForm(void)
{
  // TODO: delete m_pEngine; delete m_pArticleForm;
}

bool SearchForm::Initialize()
{
	// Construct an XML form
	Construct(L"IDF_SEARCHFORM");

	return true;
}

result SearchForm::OnInitializing(void)
{
	result r = E_SUCCESS;

	// TODO: Add your initialization code here

  string const dictionaryPath = "/Home/wordnet.slf";
  string const indexPath = "/Home/index";
  string const tempPath = "/Home/index_tmp";
  vector<pair<string, uint64_t> > dictionaries;
  dictionaries.push_back(make_pair(dictionaryPath, 1ULL));
  sl::StrFn strFn;
  strFn.Create = StrCreate;
  strFn.Destroy = StrDestroy;
  strFn.PrimaryCompare = StrPrimaryCompare;
  strFn.SecondaryCompare = StrSecondaryCompare;
  strFn.m_pData = NULL;
  strFn.m_PrimaryCompareId = 1;
  strFn.m_SecondaryCompareId = 2;
  m_pEngine = new sl::SloynikEngine(indexPath, tempPath, strFn, dictionaries);


	m_pCustomListItemFormat = new CustomListItemFormat();
  m_pCustomListItemFormat->Construct();
  m_pCustomListItemFormat->AddElement(TEXT_ID,
      Osp::Graphics::Rectangle(0, 0, GetWidth(), ITEM_HEIGHT));


	m_pSearchField =
	    static_cast<EditField *>(GetControl(L"IDPC_SEARCH_EDIT", true));
	m_pResultsList =
	    static_cast<SlidableList *>(GetControl(L"IDPC_RESULTS_LIST", true));

	m_pSearchField->AddTextEventListener(*this);
	m_pSearchField->AddActionEventListener(*this);
	m_pResultsList->AddSlidableListEventListener(*this);
	m_pResultsList->AddCustomItemEventListener(*this);

	m_pSearchField->SetOverlayKeypadCommandButton(
	    COMMAND_BUTTON_POSITION_LEFT, L"Clear", ID_CLEAR_SEARCH_FIELD);


	// m_pResultsList->AddSlidableListEventListener(*this);
	/*
	// Get a button via resource ID
	__pButtonOk = static_cast<Button *>(GetControl(L"IDC_BUTTON_OK"));
	if (__pButtonOk != null)
	{
		__pButtonOk->SetActionId(ID_BUTTON_OK);
		__pButtonOk->AddActionEventListener(*this);
	}
	*/

	return r;
}

result
SearchForm::OnTerminating(void)
{
	result r = E_SUCCESS;

	// TODO: Add your termination code here

	return r;
}

void
SearchForm::OnActionPerformed(const Osp::Ui::Control& source, int actionId)
{
	switch(actionId)
	{
	case ID_CLEAR_SEARCH_FIELD:
		{
			m_pSearchField->SetText(L"");
			m_pResultsList->ScrollToTop();
			m_pResultsList->RequestRedraw();
		}
		break;
	default:
		break;
	}
}

void SearchForm::OnTextValueChanged(Osp::Ui::Control const &)
{
  sl::SloynikEngine::SearchResult searchResult;
  String text = m_pSearchField->GetText();
  ByteBuffer * buf = Utility::StringUtil::StringToUtf8N(text);
  if (buf)
  {
    char const * utf8Str = reinterpret_cast<char const *>(buf->GetPointer());
    m_pEngine->Search(utf8Str, searchResult);
    delete buf;
    m_pResultsList->ScrollToTop(searchResult.m_FirstMatched);
    m_pResultsList->RequestRedraw();
  }
}

void SearchForm::OnTextValueChangeCanceled(Osp::Ui::Control const &)
{
}

void SearchForm::OnListPropertyRequested(const Osp::Ui::Control&)
{
  uint32_t const itemCount = m_pEngine ? m_pEngine->WordCount() : 0;
  m_pResultsList->SetItemCountAndHeight(itemCount, itemCount * ITEM_HEIGHT);
}

void SearchForm::OnLoadToTopRequested(
    const Osp::Ui::Control&, int index, int numItems)
{
  for (int i = index; i > index - numItems; i--)
    m_pResultsList->LoadItemToTop(*CreateListItem(i), i + 1);
}

void SearchForm::OnLoadToBottomRequested(
    const Osp::Ui::Control&, int index, int numItems)
{
  for (int i = index; i < index + numItems; i++)
    m_pResultsList->LoadItemToBottom(*CreateListItem(i), i + 1);
}

void SearchForm::OnUnloadItemRequested(const Osp::Ui::Control&, int)
{
  // TODO: OnUnloadItemRequested
}

CustomListItem * SearchForm::CreateListItem(uint32_t id)
{
  sl::SloynikEngine::WordInfo info;
  m_pEngine->GetWordInfo(id, info);
  String text;
  Utility::StringUtil::Utf8ToString(info.m_Word.c_str(), text);

  CustomListItem * pItem = new CustomListItem();
  pItem->Construct(ITEM_HEIGHT);
  pItem->SetItemFormat(*m_pCustomListItemFormat);
  pItem->SetElement(TEXT_ID, text);
  return pItem;
}

void SearchForm::OnItemStateChanged(const Osp::Ui::Control & source, int index,
    int itemId, int /*elementId*/, Osp::Ui::ItemStatus status)
{
  this->OnItemStateChanged(source, index, itemId, status);
}

void SearchForm::OnItemStateChanged(const Osp::Ui::Control &source, int index,
    int itemId, Osp::Ui::ItemStatus status)
{
  Frame * pFrame =
      Osp::App::Application::GetInstance()->GetAppFrame()->GetFrame();
  if (!m_pArticleForm)
  {
    m_pArticleForm = new ArticleForm();
    m_pArticleForm->m_pSearchForm = this;
    m_pArticleForm->Initialize();
    pFrame->AddControl(*m_pArticleForm);
  }

  sl::SloynikEngine::WordInfo info;
  m_pEngine->GetWordInfo(index, info);
  Utility::StringUtil::Utf8ToString(info.m_Word.c_str(), m_pArticleForm->m_Name);

  sl::SloynikEngine::ArticleData data;
  m_pEngine->GetArticleData(index, data);
  data.m_HTML.push_back(0);
  delete m_pArticleForm->m_pBuffer;
  m_pArticleForm->m_pBuffer = new ByteBuffer;
  m_pArticleForm->m_pBuffer->Construct(data.m_HTML.size());
  m_pArticleForm->m_pBuffer->SetArray(
      reinterpret_cast<byte const *>(&data.m_HTML[0]), 0, data.m_HTML.size());

  m_pArticleForm->MyInit();
  pFrame->SetCurrentForm(*m_pArticleForm);
  m_pArticleForm->Draw();
  m_pArticleForm->Show();
}