llsearchcombobox.cpp
上传用户:king477883
上传日期:2021-03-01
资源大小:9553k
文件大小:7k
源码类别:

游戏引擎

开发平台:

C++ Builder

  1. /**
  2.  * @file llsearchcombobox.cpp
  3.  * @brief Search Combobox implementation
  4.  *
  5.  * $LicenseInfo:firstyear=2009&license=viewergpl$
  6.  * 
  7.  * Copyright (c) 2009-2010, Linden Research, Inc.
  8.  * 
  9.  * Second Life Viewer Source Code
  10.  * The source code in this file ("Source Code") is provided by Linden Lab
  11.  * to you under the terms of the GNU General Public License, version 2.0
  12.  * ("GPL"), unless you have obtained a separate licensing agreement
  13.  * ("Other License"), formally executed by you and Linden Lab.  Terms of
  14.  * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15.  * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16.  * 
  17.  * There are special exceptions to the terms and conditions of the GPL as
  18.  * it is applied to this Source Code. View the full text of the exception
  19.  * in the file doc/FLOSS-exception.txt in this software distribution, or
  20.  * online at
  21.  * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22.  * 
  23.  * By copying, modifying or distributing this software, you acknowledge
  24.  * that you have read and understood your obligations described above,
  25.  * and agree to abide by those obligations.
  26.  * 
  27.  * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28.  * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29.  * COMPLETENESS OR PERFORMANCE.
  30.  * $/LicenseInfo$
  31.  */
  32. #include "llviewerprecompiledheaders.h"
  33. #include "llsearchcombobox.h"
  34. #include "llkeyboard.h"
  35. #include "lltrans.h"  // for LLTrans::getString()
  36. #include "lluictrlfactory.h"
  37. static LLDefaultChildRegistry::Register<LLSearchComboBox> r1("search_combo_box");
  38. class LLSearchHistoryBuilder
  39. {
  40. public:
  41. LLSearchHistoryBuilder(LLSearchComboBox* combo_box, const std::string& filter);
  42. virtual void buildSearchHistory();
  43. virtual ~LLSearchHistoryBuilder(){}
  44. protected:
  45. virtual bool filterSearchHistory();
  46. LLSearchComboBox* mComboBox;
  47. std::string mFilter;
  48. LLSearchHistory::search_history_list_t mFilteredSearchHistory;
  49. };
  50. LLSearchComboBox::Params::Params()
  51. : search_button("search_button")
  52. , dropdown_button_visible("dropdown_button_visible", false)
  53. {
  54. }
  55. LLSearchComboBox::LLSearchComboBox(const Params&p)
  56. : LLComboBox(p)
  57. {
  58. S32 btn_top = p.search_button.top_pad + p.search_button.rect.height;
  59. S32 btn_right = p.search_button.rect.width + p.search_button.left_pad;
  60. LLRect search_btn_rect(p.search_button.left_pad, btn_top, btn_right, p.search_button.top_pad);
  61. LLButton::Params button_params(p.search_button);
  62. button_params.name(std::string("search_btn"));
  63. button_params.rect(search_btn_rect) ;
  64. button_params.follows.flags(FOLLOWS_LEFT|FOLLOWS_TOP);
  65. button_params.tab_stop(false);
  66. button_params.click_callback.function(boost::bind(&LLSearchComboBox::onSelectionCommit, this));
  67. mSearchButton = LLUICtrlFactory::create<LLButton>(button_params);
  68. mTextEntry->addChild(mSearchButton);
  69. mTextEntry->setPassDelete(TRUE);
  70. setButtonVisible(p.dropdown_button_visible);
  71. mTextEntry->setCommitCallback(boost::bind(&LLComboBox::onTextCommit, this, _2));
  72. mTextEntry->setKeystrokeCallback(boost::bind(&LLComboBox::onTextEntry, this, _1), NULL);
  73. setCommitCallback(boost::bind(&LLSearchComboBox::onSelectionCommit, this));
  74. setPrearrangeCallback(boost::bind(&LLSearchComboBox::onSearchPrearrange, this, _2));
  75. mSearchButton->setCommitCallback(boost::bind(&LLSearchComboBox::onTextCommit, this, _2));
  76. }
  77. void LLSearchComboBox::rebuildSearchHistory(const std::string& filter)
  78. {
  79. LLSearchHistoryBuilder builder(this, filter);
  80. builder.buildSearchHistory();
  81. }
  82. void LLSearchComboBox::onSearchPrearrange(const LLSD& data)
  83. {
  84. std::string filter = data.asString();
  85. rebuildSearchHistory(filter);
  86. mList->mouseOverHighlightNthItem(-1); // Clear highlight on the last selected item.
  87. }
  88. void LLSearchComboBox::onTextEntry(LLLineEditor* line_editor)
  89. {
  90. KEY key = gKeyboard->currentKey();
  91. if (line_editor->getText().empty())
  92. {
  93. prearrangeList(); // resets filter
  94. hideList();
  95. }
  96. // Typing? (moving cursor should not affect showing the list)
  97. else if (key != KEY_LEFT && key != KEY_RIGHT && key != KEY_HOME && key != KEY_END)
  98. {
  99. prearrangeList(line_editor->getText());
  100. if (mList->getItemCount() != 0)
  101. {
  102. showList();
  103. focusTextEntry();
  104. }
  105. else
  106. {
  107. // Hide the list if it's empty.
  108. hideList();
  109. }
  110. }
  111. LLComboBox::onTextEntry(line_editor);
  112. }
  113. void LLSearchComboBox::focusTextEntry()
  114. {
  115. // We can't use "mTextEntry->setFocus(TRUE)" instead because
  116. // if the "select_on_focus" parameter is true it places the cursor
  117. // at the beginning (after selecting text), thus screwing up updateSelection().
  118. if (mTextEntry)
  119. {
  120. gFocusMgr.setKeyboardFocus(mTextEntry);
  121. }
  122. }
  123. void LLSearchComboBox::hideList()
  124. {
  125. LLComboBox::hideList();
  126. if (mTextEntry && hasFocus())
  127. focusTextEntry();
  128. }
  129. LLSearchComboBox::~LLSearchComboBox()
  130. {
  131. }
  132. void LLSearchComboBox::onSelectionCommit()
  133. {
  134. std::string search_query = getSimple();
  135. LLStringUtil::trim(search_query);
  136. // Order of add() and mTextEntry->setText does matter because add() will select first item 
  137. // in drop down list and its label will be copied to text box rewriting mTextEntry->setText() call
  138. if(!search_query.empty())
  139. {
  140. remove(search_query);
  141. add(search_query, ADD_TOP);
  142. }
  143. mTextEntry->setText(search_query);
  144. setControlValue(search_query);
  145. }
  146. BOOL LLSearchComboBox::remove(const std::string& name)
  147. {
  148. BOOL found = mList->selectItemByLabel(name, FALSE);
  149. if (found)
  150. {
  151. LLScrollListItem* item = mList->getFirstSelected();
  152. if (item)
  153. {
  154. LLComboBox::remove(mList->getItemIndex(item));
  155. }
  156. }
  157. return found;
  158. }
  159. void LLSearchComboBox::clearHistory()
  160. {
  161. removeall();
  162. setTextEntry(LLStringUtil::null);
  163. }
  164. BOOL LLSearchComboBox::handleKeyHere(KEY key,MASK mask )
  165. {
  166. if(mTextEntry->hasFocus() && MASK_NONE == mask && KEY_DOWN == key)
  167. {
  168. S32 first = 0;
  169. S32 size = 0;
  170. // get entered text (without auto-complete part)
  171. mTextEntry->getSelectionRange(&first, &size);
  172. std::string search_query = mTextEntry->getText();
  173. search_query.erase(first, size);
  174. onSearchPrearrange(search_query);
  175. }
  176. return LLComboBox::handleKeyHere(key, mask);
  177. }
  178. LLSearchHistoryBuilder::LLSearchHistoryBuilder(LLSearchComboBox* combo_box, const std::string& filter)
  179. : mComboBox(combo_box)
  180. , mFilter(filter)
  181. {
  182. }
  183. bool LLSearchHistoryBuilder::filterSearchHistory()
  184. {
  185. // *TODO: an STL algorithm would look nicer
  186. mFilteredSearchHistory.clear();
  187. std::string filter_copy = mFilter;
  188. LLStringUtil::toLower(filter_copy);
  189. LLSearchHistory::search_history_list_t history = 
  190. LLSearchHistory::getInstance()->getSearchHistoryList();
  191. LLSearchHistory::search_history_list_t::const_iterator it = history.begin();
  192. for ( ; it != history.end(); ++it)
  193. {
  194. std::string search_query = (*it).search_query;
  195. LLStringUtil::toLower(search_query);
  196. if (search_query.find(filter_copy) != std::string::npos)
  197. mFilteredSearchHistory.push_back(*it);
  198. }
  199. return mFilteredSearchHistory.size();
  200. }
  201. void LLSearchHistoryBuilder::buildSearchHistory()
  202. {
  203. mFilteredSearchHistory.clear();
  204. LLSearchHistory::search_history_list_t filtered_items;
  205. LLSearchHistory::search_history_list_t* itemsp = NULL;
  206. LLSearchHistory* sh = LLSearchHistory::getInstance();
  207. if (mFilter.empty())
  208. {
  209. itemsp = &sh->getSearchHistoryList();
  210. }
  211. else
  212. {
  213. filterSearchHistory();
  214. itemsp = &mFilteredSearchHistory;
  215. itemsp->sort();
  216. }
  217. mComboBox->removeall();
  218. LLSearchHistory::search_history_list_t::const_iterator it = itemsp->begin();
  219. for ( ; it != itemsp->end(); it++)
  220. {
  221. LLSearchHistory::LLSearchHistoryItem item = *it;
  222. mComboBox->add(item.search_query);
  223. }
  224. }