Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1 | // Copyright 2014 PDFium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | |
| 7 | #include "xfa/fxfa/app/xfa_fftext.h" |
| 8 | |
Dan Sinclair | 398a43d | 2016-03-23 15:51:01 -0400 | [diff] [blame] | 9 | #include "xfa/fwl/core/fwl_widgetdef.h" |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 10 | #include "xfa/fxfa/app/xfa_ffdraw.h" |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 11 | #include "xfa/fxfa/app/xfa_textlayout.h" |
dsinclair | 7222ea6 | 2016-04-06 14:33:07 -0700 | [diff] [blame] | 12 | #include "xfa/fxfa/include/xfa_ffapp.h" |
| 13 | #include "xfa/fxfa/include/xfa_ffdoc.h" |
| 14 | #include "xfa/fxfa/include/xfa_ffpageview.h" |
| 15 | #include "xfa/fxfa/include/xfa_ffwidget.h" |
Dan Sinclair | 811b8a4 | 2016-03-17 08:59:42 -0400 | [diff] [blame] | 16 | #include "xfa/fxgraphics/include/cfx_graphics.h" |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 17 | |
| 18 | CXFA_FFText::CXFA_FFText(CXFA_FFPageView* pPageView, CXFA_WidgetAcc* pDataAcc) |
| 19 | : CXFA_FFDraw(pPageView, pDataAcc) {} |
| 20 | CXFA_FFText::~CXFA_FFText() {} |
| 21 | void CXFA_FFText::RenderWidget(CFX_Graphics* pGS, |
| 22 | CFX_Matrix* pMatrix, |
dsinclair | 9d6ca99 | 2016-06-16 10:51:56 -0700 | [diff] [blame] | 23 | uint32_t dwStatus) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 24 | if (!IsMatchVisibleStatus(dwStatus)) { |
| 25 | return; |
| 26 | } |
| 27 | { |
| 28 | CFX_Matrix mtRotate; |
| 29 | GetRotateMatrix(mtRotate); |
| 30 | if (pMatrix) { |
| 31 | mtRotate.Concat(*pMatrix); |
| 32 | } |
| 33 | CXFA_FFWidget::RenderWidget(pGS, &mtRotate, dwStatus); |
| 34 | CXFA_TextLayout* pTextLayout = m_pDataAcc->GetTextLayout(); |
| 35 | if (pTextLayout) { |
| 36 | CFX_RenderDevice* pRenderDevice = pGS->GetRenderDevice(); |
| 37 | CFX_RectF rtText; |
| 38 | GetRectWithoutRotate(rtText); |
| 39 | if (CXFA_Margin mgWidget = m_pDataAcc->GetMargin()) { |
| 40 | CXFA_LayoutItem* pItem = this; |
dsinclair | 85d1f2c | 2016-06-23 12:40:16 -0700 | [diff] [blame] | 41 | if (!pItem->GetPrev() && !pItem->GetNext()) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 42 | XFA_RectWidthoutMargin(rtText, mgWidget); |
| 43 | } else { |
| 44 | FX_FLOAT fLeftInset, fRightInset, fTopInset = 0, fBottomInset = 0; |
| 45 | mgWidget.GetLeftInset(fLeftInset); |
| 46 | mgWidget.GetRightInset(fRightInset); |
dsinclair | 85d1f2c | 2016-06-23 12:40:16 -0700 | [diff] [blame] | 47 | if (!pItem->GetPrev()) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 48 | mgWidget.GetTopInset(fTopInset); |
dsinclair | 85d1f2c | 2016-06-23 12:40:16 -0700 | [diff] [blame] | 49 | } else if (!pItem->GetNext()) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 50 | mgWidget.GetBottomInset(fBottomInset); |
| 51 | } |
| 52 | rtText.Deflate(fLeftInset, fTopInset, fRightInset, fBottomInset); |
| 53 | } |
| 54 | } |
| 55 | CFX_Matrix mt; |
| 56 | mt.Set(1, 0, 0, 1, rtText.left, rtText.top); |
| 57 | CFX_RectF rtClip = rtText; |
| 58 | mtRotate.TransformRect(rtClip); |
| 59 | mt.Concat(mtRotate); |
| 60 | pTextLayout->DrawString(pRenderDevice, mt, rtClip, GetIndex()); |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | FX_BOOL CXFA_FFText::IsLoaded() { |
| 65 | CXFA_TextLayout* pTextLayout = m_pDataAcc->GetTextLayout(); |
| 66 | return pTextLayout && !pTextLayout->m_bHasBlock; |
| 67 | } |
| 68 | FX_BOOL CXFA_FFText::PerformLayout() { |
| 69 | CXFA_FFDraw::PerformLayout(); |
| 70 | CXFA_TextLayout* pTextLayout = m_pDataAcc->GetTextLayout(); |
| 71 | if (!pTextLayout) { |
| 72 | return FALSE; |
| 73 | } |
| 74 | if (!pTextLayout->m_bHasBlock) { |
| 75 | return TRUE; |
| 76 | } |
| 77 | pTextLayout->m_Blocks.RemoveAll(); |
| 78 | CXFA_LayoutItem* pItem = this; |
dsinclair | 85d1f2c | 2016-06-23 12:40:16 -0700 | [diff] [blame] | 79 | if (!pItem->GetPrev() && !pItem->GetNext()) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 80 | return TRUE; |
| 81 | } |
| 82 | pItem = pItem->GetFirst(); |
| 83 | while (pItem) { |
| 84 | CFX_RectF rtText; |
| 85 | pItem->GetRect(rtText); |
| 86 | if (CXFA_Margin mgWidget = m_pDataAcc->GetMargin()) { |
dsinclair | 85d1f2c | 2016-06-23 12:40:16 -0700 | [diff] [blame] | 87 | if (!pItem->GetPrev()) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 88 | FX_FLOAT fTopInset; |
| 89 | mgWidget.GetTopInset(fTopInset); |
| 90 | rtText.height -= fTopInset; |
dsinclair | 85d1f2c | 2016-06-23 12:40:16 -0700 | [diff] [blame] | 91 | } else if (!pItem->GetNext()) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 92 | FX_FLOAT fBottomInset; |
| 93 | mgWidget.GetBottomInset(fBottomInset); |
| 94 | rtText.height -= fBottomInset; |
| 95 | } |
| 96 | } |
| 97 | pTextLayout->ItemBlocks(rtText, pItem->GetIndex()); |
| 98 | pItem = pItem->GetNext(); |
| 99 | } |
| 100 | pTextLayout->m_bHasBlock = FALSE; |
| 101 | return TRUE; |
| 102 | } |
tsepez | 736f28a | 2016-03-25 14:19:51 -0700 | [diff] [blame] | 103 | FX_BOOL CXFA_FFText::OnLButtonDown(uint32_t dwFlags, FX_FLOAT fx, FX_FLOAT fy) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 104 | CFX_RectF rtBox; |
| 105 | GetRectWithoutRotate(rtBox); |
| 106 | if (!rtBox.Contains(fx, fy)) { |
| 107 | return FALSE; |
| 108 | } |
| 109 | const FX_WCHAR* wsURLContent = GetLinkURLAtPoint(fx, fy); |
dsinclair | 85d1f2c | 2016-06-23 12:40:16 -0700 | [diff] [blame] | 110 | if (!wsURLContent) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 111 | return FALSE; |
| 112 | } |
| 113 | SetButtonDown(TRUE); |
| 114 | return TRUE; |
| 115 | } |
tsepez | 736f28a | 2016-03-25 14:19:51 -0700 | [diff] [blame] | 116 | FX_BOOL CXFA_FFText::OnMouseMove(uint32_t dwFlags, FX_FLOAT fx, FX_FLOAT fy) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 117 | CFX_RectF rtBox; |
| 118 | GetRectWithoutRotate(rtBox); |
| 119 | if (!rtBox.Contains(fx, fy)) { |
| 120 | return FALSE; |
| 121 | } |
| 122 | const FX_WCHAR* wsURLContent = GetLinkURLAtPoint(fx, fy); |
dsinclair | 85d1f2c | 2016-06-23 12:40:16 -0700 | [diff] [blame] | 123 | if (!wsURLContent) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 124 | return FALSE; |
| 125 | } |
| 126 | return TRUE; |
| 127 | } |
tsepez | 736f28a | 2016-03-25 14:19:51 -0700 | [diff] [blame] | 128 | FX_BOOL CXFA_FFText::OnLButtonUp(uint32_t dwFlags, FX_FLOAT fx, FX_FLOAT fy) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 129 | if (!IsButtonDown()) { |
| 130 | return FALSE; |
| 131 | } |
| 132 | SetButtonDown(FALSE); |
| 133 | const FX_WCHAR* wsURLContent = GetLinkURLAtPoint(fx, fy); |
dsinclair | 85d1f2c | 2016-06-23 12:40:16 -0700 | [diff] [blame] | 134 | if (!wsURLContent) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 135 | return FALSE; |
| 136 | } |
| 137 | CXFA_FFDoc* pDoc = GetDoc(); |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame^] | 138 | pDoc->GetDocEnvironment()->GotoURL(pDoc, wsURLContent); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 139 | return TRUE; |
| 140 | } |
dsinclair | 89fcde8 | 2016-05-03 13:00:25 -0700 | [diff] [blame] | 141 | FWL_WidgetHit CXFA_FFText::OnHitTest(FX_FLOAT fx, FX_FLOAT fy) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 142 | CFX_RectF rtBox; |
| 143 | GetRectWithoutRotate(rtBox); |
dsinclair | 89fcde8 | 2016-05-03 13:00:25 -0700 | [diff] [blame] | 144 | if (!rtBox.Contains(fx, fy)) |
| 145 | return FWL_WidgetHit::Unknown; |
| 146 | if (!GetLinkURLAtPoint(fx, fy)) |
| 147 | return FWL_WidgetHit::Unknown; |
| 148 | return FWL_WidgetHit::HyperLink; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 149 | } |
| 150 | const FX_WCHAR* CXFA_FFText::GetLinkURLAtPoint(FX_FLOAT fx, FX_FLOAT fy) { |
| 151 | CXFA_TextLayout* pTextLayout = m_pDataAcc->GetTextLayout(); |
dsinclair | 85d1f2c | 2016-06-23 12:40:16 -0700 | [diff] [blame] | 152 | if (!pTextLayout) { |
| 153 | return nullptr; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 154 | } |
| 155 | FX_FLOAT x(fx), y(fy); |
| 156 | FWLToClient(x, y); |
| 157 | const CXFA_PieceLineArray* pPieceLines = pTextLayout->GetPieceLines(); |
| 158 | int32_t iCount = pPieceLines->GetSize(); |
| 159 | for (int32_t i = 0; i < iCount; i++) { |
| 160 | CXFA_PieceLine* pPieceLine = pPieceLines->GetAt(i); |
| 161 | int32_t iPieces = pPieceLine->m_textPieces.GetSize(); |
| 162 | for (int32_t j = 0; j < iPieces; j++) { |
| 163 | XFA_TextPiece* pPiece = pPieceLine->m_textPieces.GetAt(j); |
| 164 | if (pPiece->pLinkData && pPiece->rtPiece.Contains(x, y)) { |
| 165 | return pPiece->pLinkData->GetLinkURL(); |
| 166 | } |
| 167 | } |
| 168 | } |
dsinclair | 85d1f2c | 2016-06-23 12:40:16 -0700 | [diff] [blame] | 169 | return nullptr; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 170 | } |
| 171 | void CXFA_FFText::FWLToClient(FX_FLOAT& fx, FX_FLOAT& fy) { |
| 172 | CFX_RectF rtWidget; |
| 173 | GetRectWithoutRotate(rtWidget); |
| 174 | fx -= rtWidget.left; |
| 175 | fy -= rtWidget.top; |
| 176 | } |