Opera 12.15 Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

OpDocumentEditInput.cpp 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  1. /* -*- Mode: c++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*-
  2. *
  3. * Copyright (C) 1995-2011 Opera Software ASA. All rights reserved.
  4. *
  5. * This file is part of the Opera web browser. It may not be distributed
  6. * under any circumstances.
  7. */
  8. #include "core/pch.h"
  9. #ifdef DOCUMENT_EDIT_SUPPORT
  10. #include "modules/documentedit/OpDocumentEdit.h"
  11. #include "modules/documentedit/OpDocumentEditUtils.h"
  12. #include "modules/doc/html_doc.h"
  13. #include "modules/doc/frm_doc.h"
  14. #include "modules/pi/OpClipboard.h"
  15. #include "modules/dochand/fdelm.h"
  16. #include "modules/dochand/winman.h"
  17. #include "modules/layout/box/box.h"
  18. #include "modules/layout/content/scrollable.h"
  19. #include "modules/util/str.h"
  20. #include "modules/dom/domeventtypes.h"
  21. extern HTML_Element *GetParentListElm(HTML_Element *elm);
  22. void OpDocumentEdit::EditAction(OpInputAction* action)
  23. {
  24. DEBUG_CHECKER(TRUE);
  25. SelectionBoundaryPoint anchor, focus;
  26. if (m_doc->GetHtmlDocument())
  27. m_doc->GetHtmlDocument()->GetSelection(anchor, focus);
  28. HTML_Element* old_caret_helm = m_caret.GetElement();
  29. INT32 old_caret_ofs = m_caret.GetOffset();
  30. #ifdef INTERNAL_SPELLCHECK_SUPPORT
  31. SpellWordInfoObject old_info;
  32. #endif // INTERNAL_SPELLCHECK_SUPPORT
  33. switch (action->GetAction())
  34. {
  35. case OpInputAction::ACTION_NEXT_CHARACTER_SPATIAL:
  36. m_caret.MoveSpatial(TRUE);
  37. break;
  38. case OpInputAction::ACTION_PREVIOUS_CHARACTER_SPATIAL:
  39. m_caret.MoveSpatial(FALSE);
  40. break;
  41. case OpInputAction::ACTION_RANGE_GO_TO_START:
  42. case OpInputAction::ACTION_GO_TO_START:
  43. m_caret.Place(OpWindowCommander::CARET_DOCUMENT_HOME);
  44. break;
  45. case OpInputAction::ACTION_RANGE_GO_TO_LINE_START:
  46. case OpInputAction::ACTION_GO_TO_LINE_START:
  47. m_caret.Place(GetRTL() ? OpWindowCommander::CARET_LINE_END : OpWindowCommander::CARET_LINE_HOME);
  48. break;
  49. case OpInputAction::ACTION_RANGE_GO_TO_END:
  50. case OpInputAction::ACTION_GO_TO_END:
  51. m_caret.Place(OpWindowCommander::CARET_DOCUMENT_END);
  52. break;
  53. case OpInputAction::ACTION_RANGE_GO_TO_LINE_END:
  54. case OpInputAction::ACTION_GO_TO_LINE_END:
  55. m_caret.Place(GetRTL() ? OpWindowCommander::CARET_LINE_HOME : OpWindowCommander::CARET_LINE_END);
  56. break;
  57. case OpInputAction::ACTION_RANGE_PAGE_UP:
  58. case OpInputAction::ACTION_PAGE_UP:
  59. m_caret.Place(OpWindowCommander::CARET_PAGEUP);
  60. break;
  61. case OpInputAction::ACTION_RANGE_PAGE_DOWN:
  62. case OpInputAction::ACTION_PAGE_DOWN:
  63. m_caret.Place(OpWindowCommander::CARET_PAGEDOWN);
  64. break;
  65. case OpInputAction::ACTION_RANGE_NEXT_CHARACTER:
  66. case OpInputAction::ACTION_NEXT_CHARACTER:
  67. if (m_selection.HasContent() && !action->IsRangeAction())
  68. {
  69. TextSelection* text_selection = m_doc->GetTextSelection();
  70. m_caret.Place(text_selection->GetEndSelectionPoint());
  71. }
  72. else
  73. {
  74. // Move action moves in visual order. Range action moves in logical order but reversed if RTL.
  75. BOOL forward = TRUE;
  76. if (/*action->IsRangeAction() && */GetRTL())
  77. forward = !forward;
  78. m_caret.Move(forward, FALSE/*, !action->IsRangeAction()*/);
  79. }
  80. break;
  81. case OpInputAction::ACTION_RANGE_PREVIOUS_CHARACTER:
  82. case OpInputAction::ACTION_PREVIOUS_CHARACTER:
  83. if (m_selection.HasContent() && !action->IsRangeAction())
  84. {
  85. TextSelection* text_selection = m_doc->GetTextSelection();
  86. m_caret.Place(text_selection->GetStartSelectionPoint());
  87. }
  88. else
  89. {
  90. // Move action moves in visual order. Range action moves in logical order but reversed if RTL.
  91. BOOL forward = FALSE;
  92. if (/*action->IsRangeAction() && */GetRTL())
  93. forward = !forward;
  94. m_caret.Move(forward, FALSE/*, !action->IsRangeAction()*/);
  95. }
  96. break;
  97. case OpInputAction::ACTION_RANGE_NEXT_WORD:
  98. case OpInputAction::ACTION_NEXT_WORD:
  99. m_caret.Move(TRUE, TRUE);
  100. break;
  101. case OpInputAction::ACTION_RANGE_PREVIOUS_WORD:
  102. case OpInputAction::ACTION_PREVIOUS_WORD:
  103. m_caret.Move(FALSE, TRUE);
  104. break;
  105. case OpInputAction::ACTION_NEXT_LINE_SPATIAL:
  106. case OpInputAction::ACTION_RANGE_NEXT_LINE:
  107. case OpInputAction::ACTION_NEXT_LINE:
  108. m_caret.Place(OpWindowCommander::CARET_DOWN);
  109. break;
  110. case OpInputAction::ACTION_PREVIOUS_LINE_SPATIAL:
  111. case OpInputAction::ACTION_RANGE_PREVIOUS_LINE:
  112. case OpInputAction::ACTION_PREVIOUS_LINE:
  113. m_caret.Place(OpWindowCommander::CARET_UP);
  114. break;
  115. case OpInputAction::ACTION_DELETE_WORD:
  116. case OpInputAction::ACTION_BACKSPACE_WORD:
  117. case OpInputAction::ACTION_DELETE_TO_END_OF_LINE:
  118. case OpInputAction::ACTION_DELETE:
  119. case OpInputAction::ACTION_BACKSPACE:
  120. if (m_readonly)
  121. break;
  122. if (m_layout_modifier.IsActive())
  123. {
  124. m_layout_modifier.Delete();
  125. break;
  126. }
  127. m_caret.LockUpdatePos(TRUE);
  128. if (!m_selection.HasContent())
  129. {
  130. #ifdef INTERNAL_SPELLCHECK_SUPPORT
  131. old_info.Set(m_caret.GetElement());
  132. #endif // INTERNAL_SPELLCHECK_SUPPORT
  133. m_caret.SetNotRemoveWhenMoveIfUntouched();
  134. switch(action->GetAction())
  135. {
  136. case OpInputAction::ACTION_DELETE_WORD:
  137. m_caret.Move(TRUE, TRUE);
  138. break;
  139. case OpInputAction::ACTION_BACKSPACE_WORD:
  140. m_caret.Move(FALSE, TRUE);
  141. break;
  142. case OpInputAction::ACTION_DELETE_TO_END_OF_LINE:
  143. m_caret.Place(OpWindowCommander::CARET_LINE_END);
  144. break;
  145. case OpInputAction::ACTION_DELETE:
  146. m_caret.Move(TRUE, FALSE);
  147. break;
  148. case OpInputAction::ACTION_BACKSPACE:
  149. m_caret.Move(FALSE, FALSE);
  150. break;
  151. }
  152. HTML_Element* anchor_elm;
  153. int anchor_offset;
  154. TextSelection::ConvertPointToOldStyle(anchor, anchor_elm, anchor_offset);
  155. m_selection.SelectToCaret(anchor_elm, anchor_offset);
  156. }
  157. if (m_selection.HasContent())
  158. {
  159. m_selection.RemoveContent();
  160. #ifdef INTERNAL_SPELLCHECK_SUPPORT
  161. DoSpellWordInfoUpdate(&old_info);
  162. #endif // INTERNAL_SPELLCHECK_SUPPORT
  163. }
  164. else
  165. {
  166. HTML_Element* list = GetParentListElm(m_caret.GetElement());
  167. if (list && action->GetAction() == OpInputAction::ACTION_BACKSPACE && (m_body_is_root || list->IsContentEditable(TRUE)))
  168. {
  169. HTML_Element* new_caret_helm = NULL;
  170. int new_caret_ofs = 0;
  171. GetNearestCaretPos(list, &new_caret_helm, &new_caret_ofs, FALSE, FALSE);
  172. HTML_Element* root = m_doc->GetCaret()->GetContainingElementActual(list);
  173. BeginChange(root, CHANGE_FLAGS_ALLOW_APPEND);
  174. DeleteElement(list);
  175. ReflowAndUpdate();
  176. if (new_caret_helm)
  177. m_caret.Place(new_caret_helm, new_caret_ofs);
  178. else
  179. m_caret.PlaceFirst(root);
  180. EndChange(root);
  181. }
  182. }
  183. m_caret.LockUpdatePos(FALSE);
  184. if (m_listener)
  185. m_listener->OnTextChanged();
  186. break;
  187. case OpInputAction::ACTION_CONVERT_HEX_TO_UNICODE:
  188. {
  189. if (m_caret.GetElement()->Parent()->GetIsPseudoElement())
  190. // Don't let us do something in pseudoelements.
  191. break;
  192. if (!m_caret.IsElementEditable(m_caret.GetElement()))
  193. // Don't let us do something in elements with contentEditable false.
  194. break;
  195. // Extract the text of the current element
  196. OpString string;
  197. int text_len = m_caret.GetElement()->GetTextContentLength() + 1;
  198. if (!string.Reserve(text_len))
  199. break;
  200. if (OpStatus::IsError(m_caret.GetElement()->GetTextContent(string.DataPtr(), text_len)))
  201. break;
  202. int hex_start = 0;
  203. // Find the hex string
  204. if (UnicodePoint charcode = ConvertHexToUnicode(0, m_caret.GetOffset(), hex_start, string.CStr()))
  205. {
  206. // Remove the hex string
  207. m_undo_stack.BeginGroup();
  208. #ifdef INTERNAL_SPELLCHECK_SUPPORT
  209. old_info.Set(m_caret.GetElement());
  210. #endif // INTERNAL_SPELLCHECK_SUPPORT
  211. m_caret.SetNotRemoveWhenMoveIfUntouched();
  212. SelectionBoundaryPoint hex_start_point(old_caret_helm, hex_start);
  213. HTML_Element* hex_start_elm;
  214. int hex_start_offset;
  215. TextSelection::ConvertPointToOldStyle(hex_start_point, hex_start_elm, hex_start_offset);
  216. m_selection.SelectToCaret(hex_start_elm, hex_start_offset);
  217. // m_caret.Set(old_caret_helm, old_caret_ofs);
  218. m_selection.RemoveContent();
  219. #ifdef INTERNAL_SPELLCHECK_SUPPORT
  220. DoSpellWordInfoUpdate(&old_info);
  221. #endif // INTERNAL_SPELLCHECK_SUPPORT
  222. // Insert the new text
  223. uni_char instr[3] = { 0, 0, 0 }; /* ARRAY OK 2011-11-07 peter */
  224. int len = Unicode::WriteUnicodePoint(instr, charcode);
  225. m_caret.LockUpdatePos(TRUE);
  226. #ifdef INTERNAL_SPELLCHECK_SUPPORT
  227. BOOL was_delayed_misspell = FALSE;
  228. #endif // INTERNAL_SPELLCHECK_SUPPORT
  229. BOOL has_content = m_selection.HasContent();
  230. if (GetDoc()->GetCaretPainter()->GetOverstrike() && !has_content)
  231. {
  232. m_caret.Move(TRUE, FALSE);
  233. if (IsFriends(old_caret_helm, m_caret.GetElement()))
  234. m_selection.SelectToCaret(old_caret_helm, old_caret_ofs);
  235. m_caret.Set(old_caret_helm, old_caret_ofs);
  236. }
  237. #ifdef INTERNAL_SPELLCHECK_SUPPORT
  238. else if(!has_content && !m_layout_modifier.IsActive() && m_delay_misspell_word_info)
  239. was_delayed_misspell = TRUE;
  240. #endif // INTERNAL_SPELLCHECK_SUPPORT
  241. InsertText(instr, len, TRUE);
  242. m_undo_stack.EndGroup();
  243. m_caret.LockUpdatePos(FALSE);
  244. #ifdef INTERNAL_SPELLCHECK_SUPPORT
  245. if(m_spell_session)
  246. PossiblyDelayMisspell(was_delayed_misspell);
  247. m_doc_has_changed = FALSE;
  248. #endif // INTERNAL_SPELLCHECK_SUPPORT
  249. if (m_listener)
  250. m_listener->OnTextChanged();
  251. }
  252. break;
  253. }
  254. case OpInputAction::ACTION_TOGGLE_STYLE_BOLD:
  255. execCommand(OP_DOCUMENT_EDIT_COMMAND_BOLD);
  256. if (m_listener)
  257. m_listener->OnTextChanged();
  258. break;
  259. case OpInputAction::ACTION_TOGGLE_STYLE_ITALIC:
  260. execCommand(OP_DOCUMENT_EDIT_COMMAND_ITALIC);
  261. if (m_listener)
  262. m_listener->OnTextChanged();
  263. break;
  264. case OpInputAction::ACTION_TOGGLE_STYLE_UNDERLINE:
  265. execCommand(OP_DOCUMENT_EDIT_COMMAND_UNDERLINE);
  266. if (m_listener)
  267. m_listener->OnTextChanged();
  268. break;
  269. case OpInputAction::ACTION_LOWLEVEL_KEY_PRESSED:
  270. {
  271. if (m_readonly)
  272. break;
  273. OpKey::Code key = action->GetActionKeyCode();
  274. if (!m_caret.GetElement())
  275. // Nowhere to put the input.
  276. break;
  277. if (m_caret.GetElement()->Parent()->GetIsPseudoElement())
  278. // Don't let us do something in pseudoelements.
  279. break;
  280. if (!m_caret.IsElementEditable(m_caret.GetElement()))
  281. // Don't let us do something in elements with contentEditable false.
  282. break;
  283. if (key == OP_KEY_ENTER)
  284. {
  285. BOOL new_paragraph = action->GetShiftKeys() & SHIFTKEY_SHIFT ? FALSE : TRUE;
  286. BOOL break_list = TRUE;
  287. #ifdef DOCUMENT_EDIT_USE_PARAGRAPH_BREAK
  288. break_list = new_paragraph;
  289. #endif // DOCUMENT_EDIT_USE_PARAGRAPH_BREAK
  290. InsertBreak(break_list, new_paragraph);
  291. }
  292. else if (key == OP_KEY_TAB)
  293. {
  294. // FIX: if we don't use wrap_pre, we can't do this! we have to insert nbspaces then.
  295. uni_char instr[2] = { 9, 0 };
  296. InsertText(instr, 1, TRUE);
  297. }
  298. else
  299. {
  300. m_caret.LockUpdatePos(TRUE);
  301. #ifdef INTERNAL_SPELLCHECK_SUPPORT
  302. BOOL was_delayed_misspell = FALSE;
  303. #endif // INTERNAL_SPELLCHECK_SUPPORT
  304. BOOL has_content = m_selection.HasContent();
  305. if (GetDoc()->GetCaretPainter()->GetOverstrike() && !has_content)
  306. {
  307. m_caret.Move(TRUE, FALSE);
  308. if (IsFriends(old_caret_helm, m_caret.GetElement()))
  309. m_selection.SelectToCaret(old_caret_helm, old_caret_ofs);
  310. m_caret.Set(old_caret_helm, old_caret_ofs);
  311. }
  312. #ifdef INTERNAL_SPELLCHECK_SUPPORT
  313. else if(!has_content && !m_layout_modifier.IsActive() && m_delay_misspell_word_info)
  314. was_delayed_misspell = TRUE;
  315. #endif // INTERNAL_SPELLCHECK_SUPPORT
  316. const uni_char *key_value = action->GetKeyValue();
  317. if (key_value)
  318. InsertText(key_value, uni_strlen(key_value), TRUE);
  319. m_caret.LockUpdatePos(FALSE);
  320. #ifdef INTERNAL_SPELLCHECK_SUPPORT
  321. if(m_spell_session)
  322. PossiblyDelayMisspell(was_delayed_misspell);
  323. m_doc_has_changed = FALSE;
  324. #endif // INTERNAL_SPELLCHECK_SUPPORT
  325. }
  326. if (m_listener)
  327. m_listener->OnTextChanged();
  328. break;
  329. }
  330. }
  331. if (action->IsRangeAction() && anchor.GetElement())
  332. {
  333. HTML_Element* anchor_elm;
  334. int anchor_offset;
  335. TextSelection::ConvertPointToOldStyle(anchor, anchor_elm, anchor_offset);
  336. m_selection.SelectToCaret(anchor_elm, anchor_offset);
  337. }
  338. else if (action->IsMoveAction())
  339. m_selection.SelectNothing();
  340. BOOL IsUpDownAction(int action); // widgets/OpMultiEdit.cpp
  341. if (!IsUpDownAction(action->GetAction()))
  342. m_caret.UpdateWantedX();
  343. ScrollIfNeeded();
  344. }
  345. BOOL OpDocumentEdit::OnInputAction(OpInputAction* action)
  346. {
  347. DEBUG_CHECKER(TRUE);
  348. if (!GetDoc())
  349. return FALSE;
  350. CheckLogTreeChanged(TRUE);
  351. switch (action->GetAction())
  352. {
  353. case OpInputAction::ACTION_LOWLEVEL_PREFILTER_ACTION:
  354. {
  355. OpInputAction* child_action = action->GetChildAction();
  356. if (child_action->IsKeyboardInvoked())
  357. {
  358. DOM_EventType key_event = DOM_EVENT_NONE;
  359. switch (child_action->GetAction())
  360. {
  361. case OpInputAction::ACTION_LOWLEVEL_KEY_DOWN:
  362. key_event = ONKEYDOWN;
  363. break;
  364. case OpInputAction::ACTION_LOWLEVEL_KEY_UP:
  365. key_event = ONKEYUP;
  366. break;
  367. case OpInputAction::ACTION_LOWLEVEL_KEY_PRESSED:
  368. key_event = ONKEYPRESS;
  369. break;
  370. }
  371. if (key_event != DOM_EVENT_NONE)
  372. {
  373. OP_BOOLEAN result = FramesDocument::SendDocumentKeyEvent(GetDoc(), NULL, key_event, child_action->GetActionKeyCode(), child_action->GetKeyValue(), child_action->GetPlatformKeyEventData(), child_action->GetShiftKeys(), child_action->GetKeyRepeat(), child_action->GetKeyLocation(), static_cast<unsigned>(DOM_KEYEVENT_DOCUMENTEDIT));
  374. if (OpStatus::IsMemoryError(result))
  375. g_memory_manager->RaiseCondition(OpStatus::ERR_NO_MEMORY);
  376. return (result == OpBoolean::IS_TRUE);
  377. }
  378. }
  379. return FALSE;
  380. }
  381. case OpInputAction::ACTION_GET_ACTION_STATE:
  382. {
  383. OpInputAction* child_action = action->GetChildAction();
  384. OpInputAction::Action action_type = child_action->GetAction();
  385. #ifdef USE_OP_CLIPBOARD
  386. BOOL force_enabling = FALSE;
  387. if (action_type == OpInputAction::ACTION_CUT ||
  388. action_type == OpInputAction::ACTION_COPY ||
  389. action_type == OpInputAction::ACTION_PASTE)
  390. {
  391. HTML_Element* target = m_layout_modifier.IsActive() ? m_layout_modifier.m_helm : (m_selection.HasContent() ? m_selection.GetStartElement() : m_caret.GetElement());
  392. force_enabling = g_clipboard_manager->ForceEnablingClipboardAction(action_type, m_doc, target);
  393. }
  394. #endif // USE_OP_CLIPBOARD
  395. switch (action_type)
  396. {
  397. case OpInputAction::ACTION_UNDO:
  398. child_action->SetEnabled(!m_readonly && queryCommandEnabled(OP_DOCUMENT_EDIT_COMMAND_UNDO));
  399. return TRUE;
  400. case OpInputAction::ACTION_REDO:
  401. child_action->SetEnabled(!m_readonly && queryCommandEnabled(OP_DOCUMENT_EDIT_COMMAND_REDO));
  402. return TRUE;
  403. case OpInputAction::ACTION_TOGGLE_STYLE_BOLD:
  404. child_action->SetEnabled(!m_readonly && !m_plain_text_mode && queryCommandEnabled(OP_DOCUMENT_EDIT_COMMAND_BOLD));
  405. return TRUE;
  406. case OpInputAction::ACTION_TOGGLE_STYLE_ITALIC:
  407. child_action->SetEnabled(!m_readonly && !m_plain_text_mode && queryCommandEnabled(OP_DOCUMENT_EDIT_COMMAND_ITALIC));
  408. return TRUE;
  409. case OpInputAction::ACTION_TOGGLE_STYLE_UNDERLINE:
  410. child_action->SetEnabled(!m_readonly && !m_plain_text_mode && queryCommandEnabled(OP_DOCUMENT_EDIT_COMMAND_UNDERLINE));
  411. return TRUE;
  412. #ifdef USE_OP_CLIPBOARD
  413. case OpInputAction::ACTION_CUT:
  414. child_action->SetEnabled(force_enabling || (!m_readonly && queryCommandEnabled(OP_DOCUMENT_EDIT_COMMAND_CUT)));
  415. return TRUE;
  416. case OpInputAction::ACTION_COPY:
  417. child_action->SetEnabled(force_enabling || queryCommandEnabled(OP_DOCUMENT_EDIT_COMMAND_COPY));
  418. return TRUE;
  419. case OpInputAction::ACTION_PASTE:
  420. child_action->SetEnabled(force_enabling || (!m_readonly && queryCommandEnabled(OP_DOCUMENT_EDIT_COMMAND_PASTE)));
  421. return TRUE;
  422. #endif // USE_OP_CLIPBOARD
  423. }
  424. return FALSE;
  425. }
  426. case OpInputAction::ACTION_LOWLEVEL_KEY_PRESSED:
  427. {
  428. OpKey::Code key = action->GetActionKeyCode();
  429. #if defined(_DOCEDIT_DEBUG) && defined(USE_OP_CLIPBOARD)
  430. if (key == OP_KEY_F10)
  431. {
  432. m_doc->GetLogicalDocument()->GetRoot()->DumpDebugTree();
  433. return TRUE;
  434. }
  435. if (key == OP_KEY_F11)
  436. {
  437. OpString text;
  438. GetTextHTML(text);
  439. OP_NEW_DBG("OpDocumentEdit::OnInputAction()", "docedit");
  440. OP_DBG_LEN(text.CStr(), text.Length());
  441. return TRUE;
  442. }
  443. if (key == OP_KEY_F9)
  444. {
  445. const uni_char* tmp = UNI_L("<table border=1><tr><td>&nbsp;</td><td>&nbsp;</td></tr><tr><td>&nbsp;</td><td>&nbsp;</td></tr></table>");
  446. InsertTextHTML(tmp, uni_strlen(tmp));
  447. return TRUE;
  448. }
  449. #endif // _DOCEDIT_DEBUG && USE_OP_CLIPBOARD */
  450. ShiftKeyState modifiers = action->GetShiftKeys();
  451. const uni_char *key_value = action->GetKeyValue();
  452. BOOL is_key_wanted;
  453. switch (key)
  454. {
  455. #ifdef OP_KEY_ENTER_ENABLED
  456. case OP_KEY_ENTER:
  457. is_key_wanted = (modifiers & (SHIFTKEY_CTRL | SHIFTKEY_ALT | SHIFTKEY_META)) == 0;
  458. break;
  459. #endif // OP_KEY_ENTER_ENABLED
  460. #ifdef OP_KEY_TAB_ENABLED
  461. case OP_KEY_TAB:
  462. is_key_wanted = m_wants_tab && modifiers == SHIFTKEY_NONE;
  463. break;
  464. #endif // OP_KEY_TAB_ENABLED
  465. default:
  466. /* A keypress involving CTRL is ignored, but not if it involves
  467. ALT (or META.) Some key mappings use those modifier combinations
  468. for dead/accented keys (e.g., Ctrl-Alt-Z on a Polish keyboard.) */
  469. if ((modifiers & SHIFTKEY_CTRL) != 0 && (modifiers & (SHIFTKEY_ALT | SHIFTKEY_META)) == 0)
  470. is_key_wanted = FALSE;
  471. else
  472. is_key_wanted = key_value != NULL && key_value[0] >= 32 && key_value[0] != 0x7f;
  473. }
  474. if (m_caret.GetElement() && is_key_wanted)
  475. {
  476. EditAction(action);
  477. return TRUE;
  478. }
  479. return FALSE;
  480. }
  481. case OpInputAction::ACTION_GO_TO_START:
  482. case OpInputAction::ACTION_GO_TO_END:
  483. case OpInputAction::ACTION_GO_TO_LINE_START:
  484. case OpInputAction::ACTION_GO_TO_LINE_END:
  485. case OpInputAction::ACTION_PAGE_UP:
  486. case OpInputAction::ACTION_PAGE_DOWN:
  487. case OpInputAction::ACTION_NEXT_CHARACTER:
  488. case OpInputAction::ACTION_PREVIOUS_CHARACTER:
  489. case OpInputAction::ACTION_NEXT_CHARACTER_SPATIAL:
  490. case OpInputAction::ACTION_PREVIOUS_CHARACTER_SPATIAL:
  491. case OpInputAction::ACTION_NEXT_WORD:
  492. case OpInputAction::ACTION_PREVIOUS_WORD:
  493. case OpInputAction::ACTION_NEXT_LINE:
  494. case OpInputAction::ACTION_PREVIOUS_LINE:
  495. case OpInputAction::ACTION_NEXT_LINE_SPATIAL:
  496. case OpInputAction::ACTION_PREVIOUS_LINE_SPATIAL:
  497. case OpInputAction::ACTION_RANGE_GO_TO_START:
  498. case OpInputAction::ACTION_RANGE_GO_TO_END:
  499. case OpInputAction::ACTION_RANGE_GO_TO_LINE_START:
  500. case OpInputAction::ACTION_RANGE_GO_TO_LINE_END:
  501. case OpInputAction::ACTION_RANGE_PAGE_UP:
  502. case OpInputAction::ACTION_RANGE_PAGE_DOWN:
  503. case OpInputAction::ACTION_RANGE_NEXT_CHARACTER:
  504. case OpInputAction::ACTION_RANGE_PREVIOUS_CHARACTER:
  505. case OpInputAction::ACTION_RANGE_NEXT_WORD:
  506. case OpInputAction::ACTION_RANGE_PREVIOUS_WORD:
  507. case OpInputAction::ACTION_RANGE_NEXT_LINE:
  508. case OpInputAction::ACTION_RANGE_PREVIOUS_LINE:
  509. case OpInputAction::ACTION_DELETE:
  510. case OpInputAction::ACTION_DELETE_WORD:
  511. case OpInputAction::ACTION_DELETE_TO_END_OF_LINE:
  512. case OpInputAction::ACTION_BACKSPACE:
  513. case OpInputAction::ACTION_BACKSPACE_WORD:
  514. case OpInputAction::ACTION_TOGGLE_STYLE_BOLD:
  515. case OpInputAction::ACTION_TOGGLE_STYLE_ITALIC:
  516. case OpInputAction::ACTION_TOGGLE_STYLE_UNDERLINE:
  517. case OpInputAction::ACTION_CONVERT_HEX_TO_UNICODE:
  518. {
  519. if (!m_caret.GetElement())
  520. {
  521. // Code is unfortunately dependant on m_caret.m_helm. Will be fixed in the next redesign/rewrite.
  522. return FALSE;
  523. }
  524. EditAction(action);
  525. return TRUE;
  526. }
  527. case OpInputAction::ACTION_TOGGLE_OVERSTRIKE:
  528. {
  529. if (!m_caret.GetElement())
  530. {
  531. // Code is unfortunately dependant on m_caret.m_helm. Will be fixed in the next redesign/rewrite.
  532. return FALSE;
  533. }
  534. m_caret.SetOverstrike(!GetDoc()->GetCaretPainter()->GetOverstrike());
  535. return TRUE;
  536. }
  537. #ifdef SUPPORT_TEXT_DIRECTION
  538. case OpInputAction::ACTION_CHANGE_DIRECTION_TO_LTR:
  539. case OpInputAction::ACTION_CHANGE_DIRECTION_TO_RTL:
  540. if (!m_caret.GetElement())
  541. {
  542. // Code is unfortunately dependant on m_caret.m_helm. Will be fixed in the next redesign/rewrite.
  543. return FALSE;
  544. }
  545. SetRTL(action->GetAction() == OpInputAction::ACTION_CHANGE_DIRECTION_TO_RTL);
  546. return TRUE;
  547. #endif // SUPPORT_TEXT_DIRECTION
  548. case OpInputAction::ACTION_UNDO:
  549. return OpStatus::IsSuccess(execCommand(OP_DOCUMENT_EDIT_COMMAND_UNDO));
  550. case OpInputAction::ACTION_REDO:
  551. return OpStatus::IsSuccess(execCommand(OP_DOCUMENT_EDIT_COMMAND_REDO));
  552. #ifdef USE_OP_CLIPBOARD
  553. case OpInputAction::ACTION_CUT:
  554. return OpStatus::IsSuccess(execCommand(OP_DOCUMENT_EDIT_COMMAND_CUT));
  555. case OpInputAction::ACTION_COPY:
  556. return OpStatus::IsSuccess(execCommand(OP_DOCUMENT_EDIT_COMMAND_COPY));
  557. case OpInputAction::ACTION_PASTE:
  558. return OpStatus::IsSuccess(execCommand(OP_DOCUMENT_EDIT_COMMAND_PASTE));
  559. case OpInputAction::ACTION_PASTE_MOUSE_SELECTION:
  560. {
  561. if (!m_caret.GetElement())
  562. {
  563. // Code is unfortunately dependant on m_caret.m_helm. Will be fixed in the next redesign/rewrite.
  564. return FALSE;
  565. }
  566. # ifdef _X11_SELECTION_POLICY_
  567. g_clipboard_manager->SetMouseSelectionMode(TRUE);
  568. # endif // _X11_SELECTION_POLICY_
  569. if( g_clipboard_manager->HasText() )
  570. {
  571. if( action->GetActionData() == 1 )
  572. {
  573. Clear();
  574. }
  575. HTML_Element* target = m_selection.HasContent() ? m_selection.GetStartElement() : m_caret.GetElement();
  576. g_clipboard_manager->Paste(this, m_doc, target);
  577. }
  578. # ifdef _X11_SELECTION_POLICY_
  579. g_clipboard_manager->SetMouseSelectionMode(FALSE);
  580. # endif // _X11_SELECTION_POLICY_
  581. return TRUE;
  582. }
  583. #endif // USE_OP_CLIPBOARD
  584. case OpInputAction::ACTION_SELECT_ALL:
  585. return OpStatus::IsSuccess(execCommand(OP_DOCUMENT_EDIT_COMMAND_SELECTALL));
  586. #ifdef DEBUG_DOCUMENT_EDIT
  587. case OpInputAction::ACTION_RELOAD:
  588. case OpInputAction::ACTION_FORCE_RELOAD:
  589. #endif
  590. case OpInputAction::ACTION_CLEAR:
  591. Clear();
  592. return TRUE;
  593. case OpInputAction::ACTION_INSERT:
  594. if (!m_caret.GetElement())
  595. {
  596. // Code is unfortunately dependant on m_caret.m_helm. Will be fixed in the next redesign/rewrite.
  597. return FALSE;
  598. }
  599. InsertText(action->GetActionDataString(), uni_strlen(action->GetActionDataString()));
  600. return TRUE;
  601. case OpInputAction::ACTION_FOCUS_NEXT_WIDGET:
  602. case OpInputAction::ACTION_FOCUS_PREVIOUS_WIDGET:
  603. if (m_caret.GetElement())
  604. {
  605. HTML_Element *ec = GetEditableContainer(m_caret.GetElement());
  606. if (ec && ec->Type() == HE_BODY)
  607. {
  608. FramesDocElm *fd_elm = m_doc->GetDocManager()->GetFrame();
  609. if (fd_elm && fd_elm->IsInlineFrame())
  610. {
  611. // This is a IFRAME so we should focus the parentdocument and call this action on it, so
  612. // focus will be moved within the parentdocument.
  613. fd_elm->GetCurrentDoc()->GetParentDoc()->GetVisualDevice()->SetFocus(FOCUS_REASON_KEYBOARD);
  614. OP_BOOLEAN res = fd_elm->GetCurrentDoc()->GetParentDoc()->OnInputAction(action);
  615. return res == OpBoolean::IS_TRUE;
  616. }
  617. }
  618. }
  619. return FALSE;
  620. case OpInputAction::ACTION_SCROLL_UP:
  621. case OpInputAction::ACTION_SCROLL_DOWN:
  622. case OpInputAction::ACTION_SCROLL_LEFT:
  623. case OpInputAction::ACTION_SCROLL_RIGHT:
  624. if (m_caret.GetElement())
  625. {
  626. // Redirect theese actions to a scrollablecontainer so selectionscroll works.
  627. // The ScrollableContainer is not a parent or child of the DocumentEdits inputcontext.
  628. HTML_Element *ec = GetEditableContainer(m_caret.GetElement());
  629. if (ec && ec->GetLayoutBox())
  630. {
  631. ScrollableArea* sc = ec->GetLayoutBox()->GetScrollable();
  632. if (sc)
  633. return sc->TriggerScrollInputAction(action);
  634. }
  635. }
  636. break;
  637. }
  638. return FALSE;
  639. }
  640. BOOL OpDocumentEdit::GetBoundingRect(OpRect &rect)
  641. {
  642. DEBUG_CHECKER(TRUE);
  643. HTML_Element *ec = GetEditableContainer(m_caret.GetElement());
  644. if (ec && ec->GetLayoutBox())
  645. {
  646. if (OpInputContext* input_context = ec->GetLayoutBox()->GetScrollable())
  647. return input_context->GetBoundingRect(rect);
  648. }
  649. return FALSE;
  650. }
  651. void OpDocumentEdit::OnKeyboardInputGained(OpInputContext* new_input_context, OpInputContext* old_input_context, FOCUS_REASON reason)
  652. {
  653. DEBUG_CHECKER(TRUE);
  654. OnFocus(TRUE, reason);
  655. }
  656. void OpDocumentEdit::OnKeyboardInputLost(OpInputContext* new_input_context, OpInputContext* old_input_context, FOCUS_REASON reason)
  657. {
  658. DEBUG_CHECKER(TRUE);
  659. OnFocus(FALSE, reason);
  660. }
  661. #endif // DOCUMENT_EDIT_SUPPORT