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.

OpDocumentEditLayoutModifier.cpp 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /* -*- Mode: c++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*-
  2. *
  3. * Copyright (C) 1995-2005 Opera Software AS. 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/layout/box/box.h"
  13. #include "modules/layout/content/scrollable.h"
  14. #include "modules/layout/layout_workplace.h"
  15. OpDocumentEditLayoutModifier::OpDocumentEditLayoutModifier(OpDocumentEdit* edit)
  16. : m_edit(edit)
  17. , m_helm(NULL)
  18. {
  19. DEBUG_CHECKER_CONSTRUCTOR();
  20. }
  21. OpDocumentEditLayoutModifier::~OpDocumentEditLayoutModifier()
  22. {
  23. DEBUG_CHECKER(TRUE);
  24. }
  25. void OpDocumentEditLayoutModifier::Paint(VisualDevice* vis_dev)
  26. {
  27. DEBUG_CHECKER(TRUE);
  28. if (!m_helm)
  29. return;
  30. vis_dev->SetColor(g_op_ui_info->GetSystemColor(OP_SYSTEM_COLOR_TEXT_SELECTED));
  31. vis_dev->RectangleOut(m_rect.x, m_rect.y, m_rect.width, m_rect.height);
  32. vis_dev->SetColor(g_op_ui_info->GetSystemColor(OP_SYSTEM_COLOR_BACKGROUND_SELECTED));
  33. vis_dev->RectangleOut(m_rect.x, m_rect.y, m_rect.width, m_rect.height, CSS_VALUE_dashed);
  34. /*
  35. OpRect c[3];
  36. const int csz = 6;
  37. c[0].Set(m_rect.x + m_rect.width - csz, m_rect.y + m_rect.height/2 - csz, csz, csz);
  38. c[1].Set(m_rect.x + m_rect.width - csz, m_rect.y + m_rect.height - csz, csz, csz);
  39. c[2].Set(m_rect.x + m_rect.width/2 - csz, m_rect.y + m_rect.height - csz, csz, csz);
  40. for(int i = 0; i < 3; i++)
  41. {
  42. vis_dev->SetColor(OP_RGB(255, 255, 255));
  43. vis_dev->FillRect(c[i]);
  44. vis_dev->SetColor(g_op_ui_info->GetSystemColor(OP_SYSTEM_COLOR_BACKGROUND_SELECTED));
  45. vis_dev->RectangleOut(c[i].x, c[i].y, c[i].width, c[i].height);
  46. }*/
  47. }
  48. CursorType OpDocumentEditLayoutModifier::GetCursorType(HTML_Element* helm, int x, int y)
  49. {
  50. DEBUG_CHECKER(TRUE);
  51. if (IsLayoutModifiable(helm))
  52. return CURSOR_DEFAULT_ARROW;
  53. if (helm->GetLayoutBox())
  54. if (ScrollableArea* sc = helm->GetLayoutBox()->GetScrollable())
  55. if (sc->IsOverScrollbar(OpPoint(x, y)))
  56. // We still want default cursor on the scrollbars.
  57. return CURSOR_AUTO;
  58. return CURSOR_TEXT;
  59. }
  60. BOOL OpDocumentEditLayoutModifier::IsLayoutModifiable(HTML_Element* helm)
  61. {
  62. DEBUG_CHECKER(TRUE);
  63. Box* box = helm->GetLayoutBox();
  64. if (!box || !box->GetContent())
  65. return FALSE;
  66. if (helm->Type() == HE_DOC_ROOT || helm->Type() == HE_BODY)
  67. return FALSE;
  68. if (!(/*box->GetContent()->IsReplaced()*/ m_edit->IsReplacedElement(helm) ||
  69. box->IsAbsolutePositionedBox() || box->IsFixedPositionedBox() ||
  70. box->GetTableContent() || helm->Type() == HE_HR || helm->Type() == HE_BUTTON))
  71. return FALSE;
  72. HTML_Element* ec = m_edit->GetEditableContainer(helm);
  73. if (helm == ec)
  74. return FALSE;
  75. return ec ? TRUE : FALSE;
  76. }
  77. BOOL OpDocumentEditLayoutModifier::HandleMouseEvent(HTML_Element* helm, DOM_EventType event, int x, long y, MouseButton button)
  78. {
  79. DEBUG_CHECKER(TRUE);
  80. if (event != ONMOUSEDOWN)
  81. return FALSE;
  82. Unactivate();
  83. if (!IsLayoutModifiable(helm))
  84. return FALSE;
  85. OldStyleTextSelectionPoint before, after;
  86. before.SetLogicalPosition(helm, 0);
  87. after.SetLogicalPosition(helm, 1);
  88. m_edit->m_selection.Select(&before, &after);
  89. m_helm = helm;
  90. UpdateRect();
  91. // Some other place where focus is set is not called when we handle mouseevent here.
  92. m_edit->SetFocus(FOCUS_REASON_MOUSE);
  93. return TRUE;
  94. }
  95. void OpDocumentEditLayoutModifier::UpdateRect()
  96. {
  97. DEBUG_CHECKER(TRUE);
  98. AffinePos ctm;
  99. RECT tmprect;
  100. m_edit->GetContentRectOfElement(m_helm, ctm, tmprect, ENCLOSING_BOX);
  101. VisualDevice* vd = m_edit->GetDoc()->GetVisualDevice();
  102. vd->Update(m_rect.x, m_rect.y, m_rect.width, m_rect.height);
  103. m_rect = OpRect(&tmprect);
  104. ctm.Apply(m_rect);
  105. vd->Update(m_rect.x, m_rect.y, m_rect.width, m_rect.height);
  106. }
  107. void OpDocumentEditLayoutModifier::Delete()
  108. {
  109. DEBUG_CHECKER(TRUE);
  110. if (!m_helm)
  111. return;
  112. HTML_Element* helm = m_helm;
  113. Unactivate();
  114. HTML_Element* new_caret_helm = NULL;
  115. int new_caret_ofs = 0;
  116. m_edit->GetNearestCaretPos(helm, &new_caret_helm, &new_caret_ofs, FALSE, FALSE);
  117. HTML_Element* containing_element = m_edit->GetDoc()->GetCaret()->GetContainingElementActual(helm);
  118. m_edit->BeginChange(containing_element);
  119. m_edit->m_caret.Set(NULL, 0);
  120. m_edit->DeleteElement(helm);
  121. m_edit->ReflowAndUpdate();
  122. if (new_caret_helm)
  123. m_edit->m_caret.Set(new_caret_helm, new_caret_ofs);
  124. else
  125. {
  126. /* Since no appropriate element was found for the caret, use the containing element as
  127. * edit_root so an empty placeholder element can be inserted. */
  128. m_edit->m_caret.PlaceFirst(containing_element);
  129. }
  130. m_edit->EndChange(containing_element, TIDY_LEVEL_NORMAL);
  131. }
  132. void OpDocumentEditLayoutModifier::Unactivate()
  133. {
  134. DEBUG_CHECKER(TRUE);
  135. if (!m_helm)
  136. return;
  137. m_helm = NULL;
  138. VisualDevice* vd = m_edit->GetDoc()->GetVisualDevice();
  139. vd->Update(m_rect.x, m_rect.y, m_rect.width, m_rect.height);
  140. }
  141. #endif // DOCUMENT_EDIT_SUPPORT