tb_editfield.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  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. return true;
  181. }
  182. else if (ev.type == EVENT_TYPE_CHANGED && ev.target == &m_scrollbar_y)
  183. {
  184. m_style_edit.SetScrollPos(m_style_edit.scroll_x, m_scrollbar_y.GetValue());
  185. OnScroll(m_style_edit.scroll_x, m_scrollbar_y.GetValue());
  186. return true;
  187. }
  188. else if (ev.type == EVENT_TYPE_WHEEL && ev.modifierkeys == TB_MODIFIER_NONE)
  189. {
  190. int old_val = m_scrollbar_y.GetValue();
  191. m_scrollbar_y.SetValue(old_val + ev.delta_y * TBSystem::GetPixelsPerLine());
  192. return m_scrollbar_y.GetValue() != old_val;
  193. }
  194. else if (ev.type == EVENT_TYPE_POINTER_DOWN && ev.target == this)
  195. {
  196. TBRect padding_rect = GetPaddingRect();
  197. if (m_style_edit.MouseDown(
  198. TBPoint(ev.target_x - padding_rect.x, ev.target_y - padding_rect.y),
  199. 1, ev.count, ev.modifierkeys, ev.touch))
  200. {
  201. // Post a message to start selection scroll
  202. PostMessageDelayed(TBIDC("selscroll"), nullptr, SELECTION_SCROLL_DELAY);
  203. return true;
  204. }
  205. }
  206. else if (ev.type == EVENT_TYPE_POINTER_MOVE && ev.target == this)
  207. {
  208. TBRect padding_rect = GetPaddingRect();
  209. return m_style_edit.MouseMove(TBPoint(ev.target_x - padding_rect.x, ev.target_y - padding_rect.y));
  210. }
  211. else if (ev.type == EVENT_TYPE_POINTER_UP && ev.target == this)
  212. {
  213. TBRect padding_rect = GetPaddingRect();
  214. return m_style_edit.MouseUp(TBPoint(ev.target_x - padding_rect.x, ev.target_y - padding_rect.y),
  215. 1, ev.modifierkeys, ev.touch);
  216. }
  217. else if (ev.type == EVENT_TYPE_KEY_DOWN)
  218. {
  219. return m_style_edit.KeyDown(ev.key, ev.special_key, ev.modifierkeys);
  220. }
  221. else if (ev.type == EVENT_TYPE_KEY_UP)
  222. {
  223. return true;
  224. }
  225. else if ((ev.type == EVENT_TYPE_CLICK && ev.target->GetID() == TBIDC("popupmenu")) ||
  226. (ev.type == EVENT_TYPE_SHORTCUT))
  227. {
  228. if (ev.ref_id == TBIDC("cut") && !m_style_edit.packed.read_only)
  229. m_style_edit.Cut();
  230. else if (ev.ref_id == TBIDC("copy"))
  231. m_style_edit.Copy();
  232. else if (ev.ref_id == TBIDC("paste") && !m_style_edit.packed.read_only)
  233. m_style_edit.Paste();
  234. else if (ev.ref_id == TBIDC("delete") && !m_style_edit.packed.read_only)
  235. m_style_edit.Delete();
  236. else if (ev.ref_id == TBIDC("undo") && !m_style_edit.packed.read_only)
  237. m_style_edit.Undo();
  238. else if (ev.ref_id == TBIDC("redo") && !m_style_edit.packed.read_only)
  239. m_style_edit.Redo();
  240. else if (ev.ref_id == TBIDC("selectall"))
  241. m_style_edit.selection.SelectAll();
  242. else
  243. return false;
  244. return true;
  245. }
  246. else if ((ev.type == EVENT_TYPE_CONTEXT_MENU || ev.type == EVENT_TYPE_RIGHT_POINTER_UP) && ev.target == this)
  247. {
  248. TBPoint pos_in_root(ev.target_x, ev.target_y);
  249. //ev.target->ConvertToRoot(pos_in_root.x, pos_in_root.y);
  250. if (TBMenuWindow *menu = new TBMenuWindow(ev.target, TBIDC("popupmenu")))
  251. {
  252. TBGenericStringItemSource *source = menu->GetList()->GetDefaultSource();
  253. source->AddItem(new TBGenericStringItem(g_tb_lng->GetString(TBIDC("cut")), TBIDC("cut")));
  254. source->AddItem(new TBGenericStringItem(g_tb_lng->GetString(TBIDC("copy")), TBIDC("copy")));
  255. source->AddItem(new TBGenericStringItem(g_tb_lng->GetString(TBIDC("paste")), TBIDC("paste")));
  256. source->AddItem(new TBGenericStringItem(g_tb_lng->GetString(TBIDC("delete")), TBIDC("delete")));
  257. source->AddItem(new TBGenericStringItem("-"));
  258. source->AddItem(new TBGenericStringItem(g_tb_lng->GetString(TBIDC("selectall")), TBIDC("selectall")));
  259. menu->Show(source, TBPopupAlignment(pos_in_root), -1);
  260. }
  261. return true;
  262. }
  263. return false;
  264. }
  265. void TBEditField::OnPaint(const PaintProps &paint_props)
  266. {
  267. TBRect visible_rect = GetVisibleRect();
  268. bool clip = m_scrollbar_x.CanScroll() || m_scrollbar_y.CanScroll();
  269. TBRect old_clip;
  270. if (clip)
  271. old_clip = g_renderer->SetClipRect(visible_rect, true);
  272. int trans_x = visible_rect.x, trans_y = visible_rect.y;
  273. g_renderer->Translate(trans_x, trans_y);
  274. // Draw text content, caret etc.
  275. visible_rect.x = visible_rect.y = 0;
  276. m_style_edit.Paint(visible_rect, GetCalculatedFontDescription(), paint_props.text_color);
  277. // If empty, draw placeholder text with some opacity.
  278. if (m_style_edit.IsEmpty())
  279. {
  280. float old_opacity = g_renderer->GetOpacity();
  281. g_renderer->SetOpacity(old_opacity * g_tb_skin->GetDefaultPlaceholderOpacity());
  282. TBRect placeholder_rect(visible_rect.x, visible_rect.y, visible_rect.w, GetFont()->GetHeight());
  283. m_placeholder.Paint(this, placeholder_rect, paint_props.text_color);
  284. g_renderer->SetOpacity(old_opacity);
  285. }
  286. g_renderer->Translate(-trans_x, -trans_y);
  287. if (clip)
  288. g_renderer->SetClipRect(old_clip, false);
  289. }
  290. void TBEditField::OnPaintChildren(const PaintProps &paint_props)
  291. {
  292. TBWidget::OnPaintChildren(paint_props);
  293. // Draw fadeout skin at the needed edges.
  294. DrawEdgeFadeout(GetVisibleRect(),
  295. TBIDC("TBEditField.fadeout_x"),
  296. TBIDC("TBEditField.fadeout_y"),
  297. m_scrollbar_x.GetValue(),
  298. m_scrollbar_y.GetValue(),
  299. (int)(m_scrollbar_x.GetMaxValue() - m_scrollbar_x.GetValueDouble()),
  300. (int)(m_scrollbar_y.GetMaxValue() - m_scrollbar_y.GetValueDouble()));
  301. }
  302. void TBEditField::OnAdded()
  303. {
  304. m_style_edit.SetFont(GetCalculatedFontDescription());
  305. }
  306. void TBEditField::OnFontChanged()
  307. {
  308. m_style_edit.SetFont(GetCalculatedFontDescription());
  309. }
  310. void TBEditField::OnFocusChanged(bool focused)
  311. {
  312. m_style_edit.Focus(focused);
  313. }
  314. void TBEditField::OnResized(int old_w, int old_h)
  315. {
  316. // Make the scrollbars move
  317. TBWidget::OnResized(old_w, old_h);
  318. TBRect visible_rect = GetVisibleRect();
  319. m_style_edit.SetLayoutSize(visible_rect.w, visible_rect.h, false);
  320. UpdateScrollbars();
  321. }
  322. PreferredSize TBEditField::OnCalculatePreferredContentSize(const SizeConstraints &constraints)
  323. {
  324. int font_height = GetFont()->GetHeight();
  325. PreferredSize ps;
  326. if (m_adapt_to_content_size)
  327. {
  328. int old_layout_width = m_style_edit.layout_width;
  329. int old_layout_height = m_style_edit.layout_height;
  330. if (m_style_edit.packed.wrapping)
  331. {
  332. // If we have wrapping enabled, we have to set a virtual width and format the text
  333. // so we can get the actual content width with a constant result every time.
  334. // If the layouter does not respect our size constraints in the end, we may
  335. // get a completly different content height due to different wrapping.
  336. // To fix that, we need to layout in 2 passes.
  337. // A hacky fix is to do something we probably shouldn't: use the old layout width
  338. // as virtual width for the new.
  339. //int layout_width = old_layout_width > 0 ? MAX(old_layout_width, m_virtual_width) : m_virtual_width;
  340. int layout_width = m_virtual_width;
  341. if (constraints.available_w != SizeConstraints::NO_RESTRICTION)
  342. {
  343. layout_width = constraints.available_w;
  344. if (TBSkinElement *bg_skin = GetSkinBgElement())
  345. layout_width -= bg_skin->padding_left + bg_skin->padding_right;
  346. }
  347. m_style_edit.SetLayoutSize(layout_width, old_layout_height, true);
  348. ps.size_dependency = SIZE_DEP_HEIGHT_DEPEND_ON_WIDTH;
  349. }
  350. int width = m_style_edit.GetContentWidth();
  351. int height = m_style_edit.GetContentHeight();
  352. if (m_style_edit.packed.wrapping)
  353. m_style_edit.SetLayoutSize(old_layout_width, old_layout_height, true);
  354. height = MAX(height, font_height);
  355. ps.min_w = ps.pref_w /*= ps.max_w*/ = width; // should go with the hack above.
  356. //ps.min_w = ps.pref_w = ps.max_w = width;
  357. ps.min_h = ps.pref_h = ps.max_h = height;
  358. }
  359. else
  360. {
  361. ps.pref_h = ps.min_h = font_height;
  362. if (m_style_edit.packed.multiline_on)
  363. {
  364. ps.pref_w = font_height * 10;
  365. ps.pref_h = font_height * 5;
  366. }
  367. else
  368. ps.max_h = ps.pref_h;
  369. }
  370. return ps;
  371. }
  372. void TBEditField::OnMessageReceived(TBMessage *msg)
  373. {
  374. if (msg->message == TBIDC("blink"))
  375. {
  376. m_style_edit.caret.on = !m_style_edit.caret.on;
  377. m_style_edit.caret.Invalidate();
  378. // Post another blink message so we blink again.
  379. PostMessageDelayed(TBIDC("blink"), nullptr, CARET_BLINK_TIME);
  380. }
  381. else if (msg->message == TBIDC("selscroll") && captured_widget == this)
  382. {
  383. // Get scroll speed from where mouse is relative to the padding rect.
  384. TBRect padding_rect = GetVisibleRect().Shrink(2, 2);
  385. int dx = GetSelectionScrollSpeed(pointer_move_widget_x, padding_rect.x, padding_rect.x + padding_rect.w);
  386. int dy = GetSelectionScrollSpeed(pointer_move_widget_y, padding_rect.y, padding_rect.y + padding_rect.h);
  387. m_scrollbar_x.SetValue(m_scrollbar_x.GetValue() + dx);
  388. m_scrollbar_y.SetValue(m_scrollbar_y.GetValue() + dy);
  389. // Handle mouse move at the new scroll position, so selection is updated
  390. if (dx || dy)
  391. m_style_edit.MouseMove(TBPoint(pointer_move_widget_x, pointer_move_widget_y));
  392. // Post another setscroll message so we continue scrolling if we still should.
  393. if (m_style_edit.select_state)
  394. PostMessageDelayed(TBIDC("selscroll"), nullptr, SELECTION_SCROLL_DELAY);
  395. }
  396. }
  397. void TBEditField::OnChange()
  398. {
  399. // Invalidate the layout when the content change and we should adapt our size to it
  400. if (m_adapt_to_content_size)
  401. InvalidateLayout(INVALIDATE_LAYOUT_RECURSIVE);
  402. TBWidgetEvent ev(EVENT_TYPE_CHANGED);
  403. InvokeEvent(ev);
  404. }
  405. bool TBEditField::OnEnter()
  406. {
  407. return false;
  408. }
  409. void TBEditField::Invalidate(const TBRect &rect)
  410. {
  411. TBWidget::Invalidate();
  412. }
  413. void TBEditField::DrawString(int32 x, int32 y, TBFontFace *font, const TBColor &color, const char *str, int32 len)
  414. {
  415. font->DrawString(x, y, color, str, len);
  416. }
  417. void TBEditField::DrawRect(const TBRect &rect, const TBColor &color)
  418. {
  419. g_renderer->DrawRect(rect, color);
  420. }
  421. void TBEditField::DrawRectFill(const TBRect &rect, const TBColor &color)
  422. {
  423. g_renderer->DrawRectFill(rect, color);
  424. }
  425. void TBEditField::DrawTextSelectionBg(const TBRect &rect)
  426. {
  427. TBWidgetSkinConditionContext context(this);
  428. g_tb_skin->PaintSkin(rect, TBIDC("TBEditField.selection"), static_cast<SKIN_STATE>(GetAutoState()), context);
  429. }
  430. void TBEditField::DrawContentSelectionFg(const TBRect &rect)
  431. {
  432. TBWidgetSkinConditionContext context(this);
  433. g_tb_skin->PaintSkin(rect, TBIDC("TBEditField.selection"), static_cast<SKIN_STATE>(GetAutoState()), context);
  434. }
  435. void TBEditField::DrawCaret(const TBRect &rect)
  436. {
  437. if (GetIsFocused() && !m_style_edit.packed.read_only)
  438. DrawTextSelectionBg(rect);
  439. }
  440. void TBEditField::Scroll(int32 dx, int32 dy)
  441. {
  442. TBWidget::Invalidate();
  443. m_scrollbar_x.SetValue(m_style_edit.scroll_x);
  444. m_scrollbar_y.SetValue(m_style_edit.scroll_y);
  445. }
  446. void TBEditField::UpdateScrollbars()
  447. {
  448. int32 w = m_style_edit.layout_width;
  449. int32 h = m_style_edit.layout_height;
  450. m_scrollbar_x.SetLimits(0, m_style_edit.GetContentWidth() - w, w);
  451. m_scrollbar_y.SetLimits(0, m_style_edit.GetContentHeight() - h, h);
  452. }
  453. void TBEditField::CaretBlinkStart()
  454. {
  455. // Post the delayed blink message if we don't already have one
  456. if (!GetMessageByID(TBIDC("blink")))
  457. PostMessageDelayed(TBIDC("blink"), nullptr, CARET_BLINK_TIME);
  458. }
  459. void TBEditField::CaretBlinkStop()
  460. {
  461. // Remove the blink message if we have one
  462. if (TBMessage *msg = GetMessageByID(TBIDC("blink")))
  463. DeleteMessage(msg);
  464. }
  465. // == TBEditFieldScrollRoot =======================================================================
  466. void TBEditFieldScrollRoot::OnPaintChildren(const PaintProps &paint_props)
  467. {
  468. // Avoid setting clipping (can be expensive) if we have no children to paint anyway.
  469. if (!GetFirstChild())
  470. return;
  471. // Clip children
  472. TBRect old_clip_rect = g_renderer->SetClipRect(GetPaddingRect(), true);
  473. TBWidget::OnPaintChildren(paint_props);
  474. g_renderer->SetClipRect(old_clip_rect, false);
  475. }
  476. void TBEditFieldScrollRoot::GetChildTranslation(int &x, int &y) const
  477. {
  478. TBEditField *edit_field = static_cast<TBEditField *>(GetParent());
  479. x = (int) -edit_field->GetStyleEdit()->scroll_x;
  480. y = (int) -edit_field->GetStyleEdit()->scroll_y;
  481. }
  482. WIDGET_HIT_STATUS TBEditFieldScrollRoot::GetHitStatus(int x, int y)
  483. {
  484. // Return no hit on this widget, but maybe on any of the children.
  485. if (TBWidget::GetHitStatus(x, y) && GetWidgetAt(x, y, false))
  486. return WIDGET_HIT_STATUS_HIT;
  487. return WIDGET_HIT_STATUS_NO_HIT;
  488. }
  489. // == TBTextFragmentContentWidget =================================================================
  490. class TBTextFragmentContentWidget : public TBTextFragmentContent
  491. {
  492. public:
  493. TBTextFragmentContentWidget(TBWidget *parent, TBWidget *widget);
  494. virtual ~TBTextFragmentContentWidget();
  495. virtual void UpdatePos(int x, int y);
  496. virtual int32 GetWidth(TBFontFace *font, TBTextFragment *fragment);
  497. virtual int32 GetHeight(TBFontFace *font, TBTextFragment *fragment);
  498. virtual int32 GetBaseline(TBFontFace *font, TBTextFragment *fragment);
  499. private:
  500. TBWidget *m_widget;
  501. };
  502. TBTextFragmentContentWidget::TBTextFragmentContentWidget(TBWidget *parent, TBWidget *widget)
  503. : m_widget(widget)
  504. {
  505. parent->GetContentRoot()->AddChild(widget);
  506. }
  507. TBTextFragmentContentWidget::~TBTextFragmentContentWidget()
  508. {
  509. m_widget->GetParent()->RemoveChild(m_widget);
  510. delete m_widget;
  511. }
  512. void TBTextFragmentContentWidget::UpdatePos(int x, int y)
  513. {
  514. m_widget->SetRect(TBRect(x, y, GetWidth(nullptr, nullptr), GetHeight(nullptr, nullptr)));
  515. }
  516. int32 TBTextFragmentContentWidget::GetWidth(TBFontFace *font, TBTextFragment *fragment)
  517. {
  518. return m_widget->GetRect().w ? m_widget->GetRect().w : m_widget->GetPreferredSize().pref_w;
  519. }
  520. int32 TBTextFragmentContentWidget::GetHeight(TBFontFace *font, TBTextFragment *fragment)
  521. {
  522. return m_widget->GetRect().h ? m_widget->GetRect().h : m_widget->GetPreferredSize().pref_h;
  523. }
  524. int32 TBTextFragmentContentWidget::GetBaseline(TBFontFace *font, TBTextFragment *fragment)
  525. {
  526. int height = GetHeight(font, fragment);
  527. return (height + fragment->block->CalculateBaseline(font)) / 2;
  528. }
  529. // == TBEditFieldContentFactory ===================================================================
  530. int TBEditFieldContentFactory::GetContent(const char *text)
  531. {
  532. return TBTextFragmentContentFactory::GetContent(text);
  533. }
  534. TBTextFragmentContent *TBEditFieldContentFactory::CreateFragmentContent(const char *text, int text_len)
  535. {
  536. if (strncmp(text, "<widget ", MIN(text_len, 8)) == 0)
  537. {
  538. // Create a wrapper for the generated widget.
  539. // Its size will adapt to the content.
  540. if (TBWidget *widget = new TBWidget())
  541. {
  542. if (TBTextFragmentContentWidget *cw = new TBTextFragmentContentWidget(editfield, widget))
  543. {
  544. g_widgets_reader->LoadData(widget, text + 8, text_len - 9);
  545. return cw;
  546. }
  547. delete widget;
  548. }
  549. }
  550. return TBTextFragmentContentFactory::CreateFragmentContent(text, text_len);
  551. }
  552. }; // namespace tb