tb_editfield.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  1. // ================================================================================
  2. // == This file is a part of Turbo Badger. (C) 2011-2014, Emil Segerås ==
  3. // == See tb_core.h for more information. ==
  4. // ================================================================================
  5. #include "tb_editfield.h"
  6. #include "tb_select.h"
  7. #include "tb_menu_window.h"
  8. #include "tb_system.h"
  9. #include "tb_language.h"
  10. #include "tb_style_edit_content.h"
  11. #include "tb_widgets_reader.h"
  12. #include "tb_widget_skin_condition_context.h"
  13. #include "tb_font_renderer.h"
  14. #include "tb_skin_util.h"
  15. namespace tb {
  16. const int CARET_BLINK_TIME = 500;
  17. const int SELECTION_SCROLL_DELAY = 1000/30;
  18. /** Get the delta that should be scrolled if dragging the pointer outside the range min-max */
  19. int GetSelectionScrollSpeed(int pointerpos, int min, int max)
  20. {
  21. int d = 0;
  22. if (pointerpos < min)
  23. d = pointerpos - min;
  24. else if (pointerpos > max)
  25. d = pointerpos - max;
  26. d *= d;
  27. d /= 40;
  28. return (pointerpos < min) ? -d : d;
  29. }
  30. TBEditField::TBEditField()
  31. : m_edit_type(EDIT_TYPE_TEXT)
  32. , m_adapt_to_content_size(false)
  33. , m_virtual_width(250)
  34. {
  35. SetIsFocusable(true);
  36. SetWantLongClick(true);
  37. AddChild(&m_scrollbar_x);
  38. AddChild(&m_scrollbar_y);
  39. AddChild(&m_root);
  40. m_root.SetGravity(WIDGET_GRAVITY_ALL);
  41. m_scrollbar_x.SetGravity(WIDGET_GRAVITY_BOTTOM | WIDGET_GRAVITY_LEFT_RIGHT);
  42. m_scrollbar_y.SetGravity(WIDGET_GRAVITY_RIGHT | WIDGET_GRAVITY_TOP_BOTTOM);
  43. m_scrollbar_y.SetAxis(AXIS_Y);
  44. int scrollbar_y_w = m_scrollbar_y.GetPreferredSize().pref_w;
  45. int scrollbar_x_h = m_scrollbar_x.GetPreferredSize().pref_h;
  46. m_scrollbar_x.SetRect(TBRect(0, - scrollbar_x_h, - scrollbar_y_w, scrollbar_x_h));
  47. m_scrollbar_y.SetRect(TBRect(- scrollbar_y_w, 0, scrollbar_y_w, 0));
  48. m_scrollbar_x.SetOpacity(0);
  49. m_scrollbar_y.SetOpacity(0);
  50. SetSkinBg(TBIDC("TBEditField"), WIDGET_INVOKE_INFO_NO_CALLBACKS);
  51. m_style_edit.SetListener(this);
  52. m_root.SetRect(GetVisibleRect());
  53. m_placeholder.SetTextAlign(TB_TEXT_ALIGN_LEFT);
  54. m_content_factory.editfield = this;
  55. m_style_edit.SetContentFactory(&m_content_factory);
  56. }
  57. TBEditField::~TBEditField()
  58. {
  59. RemoveChild(&m_root);
  60. RemoveChild(&m_scrollbar_y);
  61. RemoveChild(&m_scrollbar_x);
  62. }
  63. TBRect TBEditField::GetVisibleRect()
  64. {
  65. TBRect rect = GetPaddingRect();
  66. if (m_scrollbar_y.GetOpacity())
  67. rect.w -= m_scrollbar_y.GetRect().w;
  68. if (m_scrollbar_x.GetOpacity())
  69. rect.h -= m_scrollbar_x.GetRect().h;
  70. return rect;
  71. }
  72. void TBEditField::UpdateScrollbarVisibility(bool multiline)
  73. {
  74. bool enable_vertical = multiline && !m_adapt_to_content_size;
  75. m_scrollbar_y.SetOpacity(enable_vertical ? 1.f : 0.f);
  76. m_root.SetRect(GetVisibleRect());
  77. }
  78. void TBEditField::SetAdaptToContentSize(bool adapt)
  79. {
  80. if (m_adapt_to_content_size == adapt)
  81. return;
  82. m_adapt_to_content_size = adapt;
  83. UpdateScrollbarVisibility(GetMultiline());
  84. }
  85. void TBEditField::SetVirtualWidth(int virtual_width)
  86. {
  87. if (m_virtual_width == virtual_width)
  88. return;
  89. m_virtual_width = virtual_width;
  90. if (m_adapt_to_content_size && m_style_edit.packed.wrapping)
  91. InvalidateLayout(INVALIDATE_LAYOUT_RECURSIVE);
  92. }
  93. void TBEditField::SetMultiline(bool multiline)
  94. {
  95. if (multiline == GetMultiline())
  96. return;
  97. UpdateScrollbarVisibility(multiline);
  98. m_style_edit.SetMultiline(multiline);
  99. SetWrapping(multiline);
  100. InvalidateSkinStates();
  101. TBWidget::Invalidate();
  102. }
  103. void TBEditField::SetStyling(bool styling)
  104. {
  105. m_style_edit.SetStyling(styling);
  106. }
  107. void TBEditField::SetReadOnly(bool readonly)
  108. {
  109. if (readonly == GetReadOnly())
  110. return;
  111. m_style_edit.SetReadOnly(readonly);
  112. InvalidateSkinStates();
  113. TBWidget::Invalidate();
  114. }
  115. void TBEditField::SetWrapping(bool wrapping)
  116. {
  117. if (wrapping == GetWrapping())
  118. return;
  119. m_style_edit.SetWrapping(wrapping);
  120. // Invalidate the layout when the wrap mode change and we should adapt our size to it
  121. if (m_adapt_to_content_size)
  122. InvalidateLayout(INVALIDATE_LAYOUT_RECURSIVE);
  123. }
  124. void TBEditField::SetEditType(EDIT_TYPE type)
  125. {
  126. if (m_edit_type == type)
  127. return;
  128. m_edit_type = type;
  129. m_style_edit.SetPassword(type == EDIT_TYPE_PASSWORD);
  130. InvalidateSkinStates();
  131. TBWidget::Invalidate();
  132. }
  133. bool TBEditField::GetCustomSkinCondition(const TBSkinCondition::CONDITION_INFO &info)
  134. {
  135. if (info.custom_prop == TBIDC("edit-type"))
  136. {
  137. switch (m_edit_type)
  138. {
  139. case EDIT_TYPE_TEXT: return info.value == TBIDC("text");
  140. case EDIT_TYPE_SEARCH: return info.value == TBIDC("search");
  141. case EDIT_TYPE_PASSWORD: return info.value == TBIDC("password");
  142. case EDIT_TYPE_EMAIL: return info.value == TBIDC("email");
  143. case EDIT_TYPE_PHONE: return info.value == TBIDC("phone");
  144. case EDIT_TYPE_URL: return info.value == TBIDC("url");
  145. case EDIT_TYPE_NUMBER: return info.value == TBIDC("number");
  146. };
  147. }
  148. else if (info.custom_prop == TBIDC("multiline"))
  149. return !((uint32)info.value) == !GetMultiline();
  150. else if (info.custom_prop == TBIDC("readonly"))
  151. return !((uint32)info.value) == !GetReadOnly();
  152. return false;
  153. }
  154. void TBEditField::ScrollTo(int x, int y)
  155. {
  156. int old_x = m_scrollbar_x.GetValue();
  157. int old_y = m_scrollbar_y.GetValue();
  158. m_style_edit.SetScrollPos(x, y);
  159. if (old_x != m_scrollbar_x.GetValue() ||
  160. old_y != m_scrollbar_y.GetValue())
  161. TBWidget::Invalidate();
  162. }
  163. TBWidget::ScrollInfo TBEditField::GetScrollInfo()
  164. {
  165. ScrollInfo info;
  166. info.min_x = static_cast<int>(m_scrollbar_x.GetMinValue());
  167. info.min_y = static_cast<int>(m_scrollbar_y.GetMinValue());
  168. info.max_x = static_cast<int>(m_scrollbar_x.GetMaxValue());
  169. info.max_y = static_cast<int>(m_scrollbar_y.GetMaxValue());
  170. info.x = m_scrollbar_x.GetValue();
  171. info.y = m_scrollbar_y.GetValue();
  172. return info;
  173. }
  174. bool TBEditField::OnEvent(const TBWidgetEvent &ev)
  175. {
  176. if (ev.type == EVENT_TYPE_CHANGED && ev.target == &m_scrollbar_x)
  177. {
  178. m_style_edit.SetScrollPos(m_scrollbar_x.GetValue(), m_style_edit.scroll_y);
  179. OnScroll(m_scrollbar_x.GetValue(), m_style_edit.scroll_y);
  180. TBWidget::OnEvent(ev);
  181. return true;
  182. }
  183. else if (ev.type == EVENT_TYPE_CHANGED && ev.target == &m_scrollbar_y)
  184. {
  185. m_style_edit.SetScrollPos(m_style_edit.scroll_x, m_scrollbar_y.GetValue());
  186. OnScroll(m_style_edit.scroll_x, m_scrollbar_y.GetValue());
  187. TBWidget::OnEvent(ev);
  188. return true;
  189. }
  190. else if (ev.type == EVENT_TYPE_CHANGED)
  191. {
  192. TBWidget::OnEvent(ev);
  193. }
  194. else if (ev.type == EVENT_TYPE_WHEEL && ev.modifierkeys == TB_MODIFIER_NONE)
  195. {
  196. int old_val = m_scrollbar_y.GetValue();
  197. m_scrollbar_y.SetValue(old_val + ev.delta_y * TBSystem::GetPixelsPerLine());
  198. return m_scrollbar_y.GetValue() != old_val;
  199. }
  200. else if (ev.type == EVENT_TYPE_POINTER_DOWN && ev.target == this)
  201. {
  202. TBRect padding_rect = GetPaddingRect();
  203. if (m_style_edit.MouseDown(
  204. TBPoint(ev.target_x - padding_rect.x, ev.target_y - padding_rect.y),
  205. 1, ev.count, ev.modifierkeys, ev.touch))
  206. {
  207. // Post a message to start selection scroll
  208. PostMessageDelayed(TBIDC("selscroll"), nullptr, SELECTION_SCROLL_DELAY);
  209. return true;
  210. }
  211. }
  212. else if (ev.type == EVENT_TYPE_POINTER_MOVE && ev.target == this)
  213. {
  214. TBRect padding_rect = GetPaddingRect();
  215. return m_style_edit.MouseMove(TBPoint(ev.target_x - padding_rect.x, ev.target_y - padding_rect.y));
  216. }
  217. else if (ev.type == EVENT_TYPE_POINTER_UP && ev.target == this)
  218. {
  219. TBRect padding_rect = GetPaddingRect();
  220. return m_style_edit.MouseUp(TBPoint(ev.target_x - padding_rect.x, ev.target_y - padding_rect.y),
  221. 1, ev.modifierkeys, ev.touch);
  222. }
  223. else if (ev.type == EVENT_TYPE_KEY_DOWN)
  224. {
  225. TBWidget::OnEvent(ev);
  226. return m_style_edit.KeyDown(ev.key, ev.special_key, ev.modifierkeys);
  227. }
  228. else if (ev.type == EVENT_TYPE_KEY_UP)
  229. {
  230. TBWidget::OnEvent(ev);
  231. return true;
  232. }
  233. else if ((ev.type == EVENT_TYPE_CLICK && ev.target->GetID() == TBIDC("popupmenu")) ||
  234. (ev.type == EVENT_TYPE_SHORTCUT))
  235. {
  236. if (ev.ref_id == TBIDC("cut") && !m_style_edit.packed.read_only)
  237. m_style_edit.Cut();
  238. else if (ev.ref_id == TBIDC("copy"))
  239. m_style_edit.Copy();
  240. else if (ev.ref_id == TBIDC("paste") && !m_style_edit.packed.read_only)
  241. m_style_edit.Paste();
  242. else if (ev.ref_id == TBIDC("delete") && !m_style_edit.packed.read_only)
  243. m_style_edit.Delete();
  244. else if (ev.ref_id == TBIDC("undo") && !m_style_edit.packed.read_only)
  245. m_style_edit.Undo();
  246. else if (ev.ref_id == TBIDC("redo") && !m_style_edit.packed.read_only)
  247. m_style_edit.Redo();
  248. else if (ev.ref_id == TBIDC("selectall"))
  249. m_style_edit.selection.SelectAll();
  250. else
  251. return false;
  252. return true;
  253. }
  254. else if ((ev.type == EVENT_TYPE_CONTEXT_MENU || ev.type == EVENT_TYPE_RIGHT_POINTER_UP) && ev.target == this)
  255. {
  256. TBPoint pos_in_root(ev.target_x, ev.target_y);
  257. //ev.target->ConvertToRoot(pos_in_root.x, pos_in_root.y);
  258. if (TBMenuWindow *menu = new TBMenuWindow(ev.target, TBIDC("popupmenu")))
  259. {
  260. TBGenericStringItemSource *source = menu->GetList()->GetDefaultSource();
  261. source->AddItem(new TBGenericStringItem(g_tb_lng->GetString(TBIDC("cut")), TBIDC("cut")));
  262. source->AddItem(new TBGenericStringItem(g_tb_lng->GetString(TBIDC("copy")), TBIDC("copy")));
  263. source->AddItem(new TBGenericStringItem(g_tb_lng->GetString(TBIDC("paste")), TBIDC("paste")));
  264. source->AddItem(new TBGenericStringItem(g_tb_lng->GetString(TBIDC("delete")), TBIDC("delete")));
  265. source->AddItem(new TBGenericStringItem("-"));
  266. source->AddItem(new TBGenericStringItem(g_tb_lng->GetString(TBIDC("selectall")), TBIDC("selectall")));
  267. menu->Show(source, TBPopupAlignment(pos_in_root), -1);
  268. }
  269. return true;
  270. }
  271. return false;
  272. }
  273. void TBEditField::OnPaint(const PaintProps &paint_props)
  274. {
  275. TBRect visible_rect = GetVisibleRect();
  276. bool clip = m_scrollbar_x.CanScroll() || m_scrollbar_y.CanScroll();
  277. TBRect old_clip;
  278. if (clip)
  279. old_clip = g_renderer->SetClipRect(visible_rect, true);
  280. int trans_x = visible_rect.x, trans_y = visible_rect.y;
  281. g_renderer->Translate(trans_x, trans_y);
  282. // Draw text content, caret etc.
  283. visible_rect.x = visible_rect.y = 0;
  284. m_style_edit.Paint(visible_rect, GetCalculatedFontDescription(), paint_props.text_color);
  285. // If empty, draw placeholder text with some opacity.
  286. if (m_style_edit.IsEmpty())
  287. {
  288. float old_opacity = g_renderer->GetOpacity();
  289. g_renderer->SetOpacity(old_opacity * g_tb_skin->GetDefaultPlaceholderOpacity());
  290. TBRect placeholder_rect(visible_rect.x, visible_rect.y, visible_rect.w, GetFont()->GetHeight());
  291. m_placeholder.Paint(this, placeholder_rect, paint_props.text_color);
  292. g_renderer->SetOpacity(old_opacity);
  293. }
  294. g_renderer->Translate(-trans_x, -trans_y);
  295. if (clip)
  296. g_renderer->SetClipRect(old_clip, false);
  297. }
  298. void TBEditField::OnPaintChildren(const PaintProps &paint_props)
  299. {
  300. TBWidget::OnPaintChildren(paint_props);
  301. // Draw fadeout skin at the needed edges.
  302. DrawEdgeFadeout(GetVisibleRect(),
  303. TBIDC("TBEditField.fadeout_x"),
  304. TBIDC("TBEditField.fadeout_y"),
  305. m_scrollbar_x.GetValue(),
  306. m_scrollbar_y.GetValue(),
  307. (int)(m_scrollbar_x.GetMaxValue() - m_scrollbar_x.GetValueDouble()),
  308. (int)(m_scrollbar_y.GetMaxValue() - m_scrollbar_y.GetValueDouble()));
  309. }
  310. void TBEditField::OnAdded()
  311. {
  312. m_style_edit.SetFont(GetCalculatedFontDescription());
  313. }
  314. void TBEditField::OnFontChanged()
  315. {
  316. m_style_edit.SetFont(GetCalculatedFontDescription());
  317. }
  318. void TBEditField::OnFocusChanged(bool focused)
  319. {
  320. m_style_edit.Focus(focused);
  321. if (focused)
  322. {
  323. if (!m_style_edit.packed.styling_on)
  324. GetText(m_initial_edit_text);
  325. }
  326. else
  327. {
  328. if (!m_style_edit.packed.styling_on)
  329. {
  330. TBStr curText;
  331. GetText(curText);
  332. if (!curText.Equals(m_initial_edit_text))
  333. {
  334. TBWidgetEvent ev(EVENT_TYPE_CUSTOM);
  335. ev.ref_id = TBIDC("edit_complete");
  336. // forward to delegate
  337. TBWidget::OnEvent(ev);
  338. }
  339. }
  340. }
  341. TBWidget::OnFocusChanged(focused);
  342. }
  343. void TBEditField::OnResized(int old_w, int old_h)
  344. {
  345. // Make the scrollbars move
  346. TBWidget::OnResized(old_w, old_h);
  347. TBRect visible_rect = GetVisibleRect();
  348. m_style_edit.SetLayoutSize(visible_rect.w, visible_rect.h, false);
  349. UpdateScrollbars();
  350. }
  351. PreferredSize TBEditField::OnCalculatePreferredContentSize(const SizeConstraints &constraints)
  352. {
  353. int font_height = GetFont()->GetHeight();
  354. PreferredSize ps;
  355. if (m_adapt_to_content_size)
  356. {
  357. int old_layout_width = m_style_edit.layout_width;
  358. int old_layout_height = m_style_edit.layout_height;
  359. if (m_style_edit.packed.wrapping)
  360. {
  361. // If we have wrapping enabled, we have to set a virtual width and format the text
  362. // so we can get the actual content width with a constant result every time.
  363. // If the layouter does not respect our size constraints in the end, we may
  364. // get a completly different content height due to different wrapping.
  365. // To fix that, we need to layout in 2 passes.
  366. // A hacky fix is to do something we probably shouldn't: use the old layout width
  367. // as virtual width for the new.
  368. //int layout_width = old_layout_width > 0 ? MAX(old_layout_width, m_virtual_width) : m_virtual_width;
  369. int layout_width = m_virtual_width;
  370. if (constraints.available_w != SizeConstraints::NO_RESTRICTION)
  371. {
  372. layout_width = constraints.available_w;
  373. if (TBSkinElement *bg_skin = GetSkinBgElement())
  374. layout_width -= bg_skin->padding_left + bg_skin->padding_right;
  375. }
  376. m_style_edit.SetLayoutSize(layout_width, old_layout_height, true);
  377. ps.size_dependency = SIZE_DEP_HEIGHT_DEPEND_ON_WIDTH;
  378. }
  379. int width = m_style_edit.GetContentWidth();
  380. int height = m_style_edit.GetContentHeight();
  381. if (m_style_edit.packed.wrapping)
  382. m_style_edit.SetLayoutSize(old_layout_width, old_layout_height, true);
  383. height = MAX(height, font_height);
  384. ps.min_w = ps.pref_w /*= ps.max_w*/ = width; // should go with the hack above.
  385. //ps.min_w = ps.pref_w = ps.max_w = width;
  386. ps.min_h = ps.pref_h = ps.max_h = height;
  387. }
  388. else
  389. {
  390. ps.pref_h = ps.min_h = font_height;
  391. if (m_style_edit.packed.multiline_on)
  392. {
  393. ps.pref_w = font_height * 10;
  394. ps.pref_h = font_height * 5;
  395. }
  396. else
  397. ps.max_h = ps.pref_h;
  398. }
  399. return ps;
  400. }
  401. void TBEditField::OnMessageReceived(TBMessage *msg)
  402. {
  403. if (msg->message == TBIDC("blink"))
  404. {
  405. m_style_edit.caret.on = !m_style_edit.caret.on;
  406. m_style_edit.caret.Invalidate();
  407. // Post another blink message so we blink again.
  408. PostMessageDelayed(TBIDC("blink"), nullptr, CARET_BLINK_TIME);
  409. }
  410. else if (msg->message == TBIDC("selscroll") && captured_widget == this)
  411. {
  412. // Get scroll speed from where mouse is relative to the padding rect.
  413. TBRect padding_rect = GetVisibleRect().Shrink(2, 2);
  414. int dx = GetSelectionScrollSpeed(pointer_move_widget_x, padding_rect.x, padding_rect.x + padding_rect.w);
  415. int dy = GetSelectionScrollSpeed(pointer_move_widget_y, padding_rect.y, padding_rect.y + padding_rect.h);
  416. m_scrollbar_x.SetValue(m_scrollbar_x.GetValue() + dx);
  417. m_scrollbar_y.SetValue(m_scrollbar_y.GetValue() + dy);
  418. // Handle mouse move at the new scroll position, so selection is updated
  419. if (dx || dy)
  420. m_style_edit.MouseMove(TBPoint(pointer_move_widget_x, pointer_move_widget_y));
  421. // Post another setscroll message so we continue scrolling if we still should.
  422. if (m_style_edit.select_state)
  423. PostMessageDelayed(TBIDC("selscroll"), nullptr, SELECTION_SCROLL_DELAY);
  424. }
  425. }
  426. void TBEditField::OnChange()
  427. {
  428. // Invalidate the layout when the content change and we should adapt our size to it
  429. if (m_adapt_to_content_size)
  430. InvalidateLayout(INVALIDATE_LAYOUT_RECURSIVE);
  431. TBWidgetEvent ev(EVENT_TYPE_CHANGED);
  432. InvokeEvent(ev);
  433. }
  434. bool TBEditField::OnEnter()
  435. {
  436. return false;
  437. }
  438. void TBEditField::Invalidate(const TBRect &rect)
  439. {
  440. TBWidget::Invalidate();
  441. }
  442. void TBEditField::DrawString(int32 x, int32 y, TBFontFace *font, const TBColor &color, const char *str, int32 len)
  443. {
  444. font->DrawString(x, y, color, str, len);
  445. }
  446. void TBEditField::DrawRect(const TBRect &rect, const TBColor &color)
  447. {
  448. g_renderer->DrawRect(rect, color);
  449. }
  450. void TBEditField::DrawRectFill(const TBRect &rect, const TBColor &color)
  451. {
  452. g_renderer->DrawRectFill(rect, color);
  453. }
  454. void TBEditField::DrawTextSelectionBg(const TBRect &rect)
  455. {
  456. TBWidgetSkinConditionContext context(this);
  457. g_tb_skin->PaintSkin(rect, TBIDC("TBEditField.selection"), static_cast<SKIN_STATE>(GetAutoState()), context);
  458. }
  459. void TBEditField::DrawContentSelectionFg(const TBRect &rect)
  460. {
  461. TBWidgetSkinConditionContext context(this);
  462. g_tb_skin->PaintSkin(rect, TBIDC("TBEditField.selection"), static_cast<SKIN_STATE>(GetAutoState()), context);
  463. }
  464. void TBEditField::DrawCaret(const TBRect &rect)
  465. {
  466. if (GetIsFocused() && !m_style_edit.packed.read_only)
  467. DrawTextSelectionBg(rect);
  468. }
  469. void TBEditField::Scroll(int32 dx, int32 dy)
  470. {
  471. TBWidget::Invalidate();
  472. m_scrollbar_x.SetValue(m_style_edit.scroll_x);
  473. m_scrollbar_y.SetValue(m_style_edit.scroll_y);
  474. }
  475. void TBEditField::UpdateScrollbars()
  476. {
  477. int32 w = m_style_edit.layout_width;
  478. int32 h = m_style_edit.layout_height;
  479. m_scrollbar_x.SetLimits(0, m_style_edit.GetContentWidth() - w, w);
  480. m_scrollbar_y.SetLimits(0, m_style_edit.GetContentHeight() - h, h);
  481. }
  482. void TBEditField::CaretBlinkStart()
  483. {
  484. // Post the delayed blink message if we don't already have one
  485. if (!GetMessageByID(TBIDC("blink")))
  486. PostMessageDelayed(TBIDC("blink"), nullptr, CARET_BLINK_TIME);
  487. }
  488. void TBEditField::CaretBlinkStop()
  489. {
  490. // Remove the blink message if we have one
  491. if (TBMessage *msg = GetMessageByID(TBIDC("blink")))
  492. DeleteMessage(msg);
  493. }
  494. // == TBEditFieldScrollRoot =======================================================================
  495. void TBEditFieldScrollRoot::OnPaintChildren(const PaintProps &paint_props)
  496. {
  497. // Avoid setting clipping (can be expensive) if we have no children to paint anyway.
  498. if (!GetFirstChild())
  499. return;
  500. // Clip children
  501. TBRect old_clip_rect = g_renderer->SetClipRect(GetPaddingRect(), true);
  502. TBWidget::OnPaintChildren(paint_props);
  503. g_renderer->SetClipRect(old_clip_rect, false);
  504. }
  505. void TBEditFieldScrollRoot::GetChildTranslation(int &x, int &y) const
  506. {
  507. TBEditField *edit_field = static_cast<TBEditField *>(GetParent());
  508. x = (int) -edit_field->GetStyleEdit()->scroll_x;
  509. y = (int) -edit_field->GetStyleEdit()->scroll_y;
  510. }
  511. WIDGET_HIT_STATUS TBEditFieldScrollRoot::GetHitStatus(int x, int y)
  512. {
  513. // Return no hit on this widget, but maybe on any of the children.
  514. if (TBWidget::GetHitStatus(x, y) && GetWidgetAt(x, y, false))
  515. return WIDGET_HIT_STATUS_HIT;
  516. return WIDGET_HIT_STATUS_NO_HIT;
  517. }
  518. // == TBTextFragmentContentWidget =================================================================
  519. class TBTextFragmentContentWidget : public TBTextFragmentContent
  520. {
  521. public:
  522. TBTextFragmentContentWidget(TBWidget *parent, TBWidget *widget);
  523. virtual ~TBTextFragmentContentWidget();
  524. virtual void UpdatePos(int x, int y);
  525. virtual int32 GetWidth(TBFontFace *font, TBTextFragment *fragment);
  526. virtual int32 GetHeight(TBFontFace *font, TBTextFragment *fragment);
  527. virtual int32 GetBaseline(TBFontFace *font, TBTextFragment *fragment);
  528. private:
  529. TBWidget *m_widget;
  530. };
  531. TBTextFragmentContentWidget::TBTextFragmentContentWidget(TBWidget *parent, TBWidget *widget)
  532. : m_widget(widget)
  533. {
  534. parent->GetContentRoot()->AddChild(widget);
  535. }
  536. TBTextFragmentContentWidget::~TBTextFragmentContentWidget()
  537. {
  538. m_widget->GetParent()->RemoveChild(m_widget);
  539. delete m_widget;
  540. }
  541. void TBTextFragmentContentWidget::UpdatePos(int x, int y)
  542. {
  543. m_widget->SetRect(TBRect(x, y, GetWidth(nullptr, nullptr), GetHeight(nullptr, nullptr)));
  544. }
  545. int32 TBTextFragmentContentWidget::GetWidth(TBFontFace *font, TBTextFragment *fragment)
  546. {
  547. return m_widget->GetRect().w ? m_widget->GetRect().w : m_widget->GetPreferredSize().pref_w;
  548. }
  549. int32 TBTextFragmentContentWidget::GetHeight(TBFontFace *font, TBTextFragment *fragment)
  550. {
  551. return m_widget->GetRect().h ? m_widget->GetRect().h : m_widget->GetPreferredSize().pref_h;
  552. }
  553. int32 TBTextFragmentContentWidget::GetBaseline(TBFontFace *font, TBTextFragment *fragment)
  554. {
  555. int height = GetHeight(font, fragment);
  556. return (height + fragment->block->CalculateBaseline(font)) / 2;
  557. }
  558. // == TBEditFieldContentFactory ===================================================================
  559. int TBEditFieldContentFactory::GetContent(const char *text)
  560. {
  561. return TBTextFragmentContentFactory::GetContent(text);
  562. }
  563. TBTextFragmentContent *TBEditFieldContentFactory::CreateFragmentContent(const char *text, int text_len)
  564. {
  565. if (strncmp(text, "<widget ", MIN(text_len, 8)) == 0)
  566. {
  567. // Create a wrapper for the generated widget.
  568. // Its size will adapt to the content.
  569. if (TBWidget *widget = new TBWidget())
  570. {
  571. if (TBTextFragmentContentWidget *cw = new TBTextFragmentContentWidget(editfield, widget))
  572. {
  573. g_widgets_reader->LoadData(widget, text + 8, text_len - 9);
  574. return cw;
  575. }
  576. delete widget;
  577. }
  578. }
  579. return TBTextFragmentContentFactory::CreateFragmentContent(text, text_len);
  580. }
  581. }; // namespace tb