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 | |
dsinclair | 447b1f3 | 2016-12-08 10:06:32 -0800 | [diff] [blame] | 9 | #include "xfa/fwl/fwl_widgetdef.h" |
| 10 | #include "xfa/fwl/fwl_widgethit.h" |
Dan Sinclair | 1f5d498 | 2017-01-10 16:37:32 -0500 | [diff] [blame] | 11 | #include "xfa/fxfa/app/cxfa_linkuserdata.h" |
| 12 | #include "xfa/fxfa/app/cxfa_pieceline.h" |
| 13 | #include "xfa/fxfa/app/cxfa_textlayout.h" |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 14 | #include "xfa/fxfa/app/xfa_ffdraw.h" |
Dan Sinclair | 1f5d498 | 2017-01-10 16:37:32 -0500 | [diff] [blame] | 15 | #include "xfa/fxfa/app/xfa_textpiece.h" |
dsinclair | 5b49309 | 2016-09-29 20:20:24 -0700 | [diff] [blame] | 16 | #include "xfa/fxfa/xfa_ffapp.h" |
| 17 | #include "xfa/fxfa/xfa_ffdoc.h" |
| 18 | #include "xfa/fxfa/xfa_ffpageview.h" |
| 19 | #include "xfa/fxfa/xfa_ffwidget.h" |
dsinclair | 202ad72 | 2016-09-29 17:41:42 -0700 | [diff] [blame] | 20 | #include "xfa/fxgraphics/cfx_graphics.h" |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 21 | |
dan sinclair | aaf0bdc | 2017-02-04 10:16:21 -0500 | [diff] [blame^] | 22 | CXFA_FFText::CXFA_FFText(CXFA_WidgetAcc* pDataAcc) : CXFA_FFDraw(pDataAcc) {} |
| 23 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 24 | CXFA_FFText::~CXFA_FFText() {} |
dan sinclair | aaf0bdc | 2017-02-04 10:16:21 -0500 | [diff] [blame^] | 25 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 26 | void CXFA_FFText::RenderWidget(CFX_Graphics* pGS, |
| 27 | CFX_Matrix* pMatrix, |
dsinclair | 9d6ca99 | 2016-06-16 10:51:56 -0700 | [diff] [blame] | 28 | uint32_t dwStatus) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 29 | if (!IsMatchVisibleStatus(dwStatus)) { |
| 30 | return; |
| 31 | } |
| 32 | { |
| 33 | CFX_Matrix mtRotate; |
| 34 | GetRotateMatrix(mtRotate); |
| 35 | if (pMatrix) { |
| 36 | mtRotate.Concat(*pMatrix); |
| 37 | } |
| 38 | CXFA_FFWidget::RenderWidget(pGS, &mtRotate, dwStatus); |
| 39 | CXFA_TextLayout* pTextLayout = m_pDataAcc->GetTextLayout(); |
| 40 | if (pTextLayout) { |
| 41 | CFX_RenderDevice* pRenderDevice = pGS->GetRenderDevice(); |
| 42 | CFX_RectF rtText; |
| 43 | GetRectWithoutRotate(rtText); |
| 44 | if (CXFA_Margin mgWidget = m_pDataAcc->GetMargin()) { |
| 45 | CXFA_LayoutItem* pItem = this; |
dsinclair | 85d1f2c | 2016-06-23 12:40:16 -0700 | [diff] [blame] | 46 | if (!pItem->GetPrev() && !pItem->GetNext()) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 47 | XFA_RectWidthoutMargin(rtText, mgWidget); |
| 48 | } else { |
| 49 | FX_FLOAT fLeftInset, fRightInset, fTopInset = 0, fBottomInset = 0; |
| 50 | mgWidget.GetLeftInset(fLeftInset); |
| 51 | mgWidget.GetRightInset(fRightInset); |
dsinclair | 85d1f2c | 2016-06-23 12:40:16 -0700 | [diff] [blame] | 52 | if (!pItem->GetPrev()) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 53 | mgWidget.GetTopInset(fTopInset); |
dsinclair | 85d1f2c | 2016-06-23 12:40:16 -0700 | [diff] [blame] | 54 | } else if (!pItem->GetNext()) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 55 | mgWidget.GetBottomInset(fBottomInset); |
| 56 | } |
| 57 | rtText.Deflate(fLeftInset, fTopInset, fRightInset, fBottomInset); |
| 58 | } |
| 59 | } |
| 60 | CFX_Matrix mt; |
| 61 | mt.Set(1, 0, 0, 1, rtText.left, rtText.top); |
| 62 | CFX_RectF rtClip = rtText; |
| 63 | mtRotate.TransformRect(rtClip); |
| 64 | mt.Concat(mtRotate); |
| 65 | pTextLayout->DrawString(pRenderDevice, mt, rtClip, GetIndex()); |
| 66 | } |
| 67 | } |
| 68 | } |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 69 | bool CXFA_FFText::IsLoaded() { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 70 | CXFA_TextLayout* pTextLayout = m_pDataAcc->GetTextLayout(); |
| 71 | return pTextLayout && !pTextLayout->m_bHasBlock; |
| 72 | } |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 73 | bool CXFA_FFText::PerformLayout() { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 74 | CXFA_FFDraw::PerformLayout(); |
| 75 | CXFA_TextLayout* pTextLayout = m_pDataAcc->GetTextLayout(); |
| 76 | if (!pTextLayout) { |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 77 | return false; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 78 | } |
| 79 | if (!pTextLayout->m_bHasBlock) { |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 80 | return true; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 81 | } |
| 82 | pTextLayout->m_Blocks.RemoveAll(); |
| 83 | CXFA_LayoutItem* pItem = this; |
dsinclair | 85d1f2c | 2016-06-23 12:40:16 -0700 | [diff] [blame] | 84 | if (!pItem->GetPrev() && !pItem->GetNext()) { |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 85 | return true; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 86 | } |
| 87 | pItem = pItem->GetFirst(); |
| 88 | while (pItem) { |
| 89 | CFX_RectF rtText; |
| 90 | pItem->GetRect(rtText); |
| 91 | if (CXFA_Margin mgWidget = m_pDataAcc->GetMargin()) { |
dsinclair | 85d1f2c | 2016-06-23 12:40:16 -0700 | [diff] [blame] | 92 | if (!pItem->GetPrev()) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 93 | FX_FLOAT fTopInset; |
| 94 | mgWidget.GetTopInset(fTopInset); |
| 95 | rtText.height -= fTopInset; |
dsinclair | 85d1f2c | 2016-06-23 12:40:16 -0700 | [diff] [blame] | 96 | } else if (!pItem->GetNext()) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 97 | FX_FLOAT fBottomInset; |
| 98 | mgWidget.GetBottomInset(fBottomInset); |
| 99 | rtText.height -= fBottomInset; |
| 100 | } |
| 101 | } |
| 102 | pTextLayout->ItemBlocks(rtText, pItem->GetIndex()); |
| 103 | pItem = pItem->GetNext(); |
| 104 | } |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 105 | pTextLayout->m_bHasBlock = false; |
| 106 | return true; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 107 | } |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 108 | 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] | 109 | CFX_RectF rtBox; |
| 110 | GetRectWithoutRotate(rtBox); |
| 111 | if (!rtBox.Contains(fx, fy)) { |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 112 | return false; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 113 | } |
| 114 | const FX_WCHAR* wsURLContent = GetLinkURLAtPoint(fx, fy); |
dsinclair | 85d1f2c | 2016-06-23 12:40:16 -0700 | [diff] [blame] | 115 | if (!wsURLContent) { |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 116 | return false; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 117 | } |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 118 | SetButtonDown(true); |
| 119 | return true; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 120 | } |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 121 | 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] | 122 | CFX_RectF rtBox; |
| 123 | GetRectWithoutRotate(rtBox); |
| 124 | if (!rtBox.Contains(fx, fy)) { |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 125 | return false; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 126 | } |
| 127 | const FX_WCHAR* wsURLContent = GetLinkURLAtPoint(fx, fy); |
dsinclair | 85d1f2c | 2016-06-23 12:40:16 -0700 | [diff] [blame] | 128 | if (!wsURLContent) { |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 129 | return false; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 130 | } |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 131 | return true; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 132 | } |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 133 | 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] | 134 | if (!IsButtonDown()) { |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 135 | return false; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 136 | } |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 137 | SetButtonDown(false); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 138 | const FX_WCHAR* wsURLContent = GetLinkURLAtPoint(fx, fy); |
dsinclair | 85d1f2c | 2016-06-23 12:40:16 -0700 | [diff] [blame] | 139 | if (!wsURLContent) { |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 140 | return false; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 141 | } |
| 142 | CXFA_FFDoc* pDoc = GetDoc(); |
dsinclair | 577ad2c | 2016-09-22 10:20:43 -0700 | [diff] [blame] | 143 | pDoc->GetDocEnvironment()->GotoURL(pDoc, wsURLContent); |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 144 | return true; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 145 | } |
dsinclair | 89fcde8 | 2016-05-03 13:00:25 -0700 | [diff] [blame] | 146 | FWL_WidgetHit CXFA_FFText::OnHitTest(FX_FLOAT fx, FX_FLOAT fy) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 147 | CFX_RectF rtBox; |
| 148 | GetRectWithoutRotate(rtBox); |
dsinclair | 89fcde8 | 2016-05-03 13:00:25 -0700 | [diff] [blame] | 149 | if (!rtBox.Contains(fx, fy)) |
| 150 | return FWL_WidgetHit::Unknown; |
| 151 | if (!GetLinkURLAtPoint(fx, fy)) |
| 152 | return FWL_WidgetHit::Unknown; |
| 153 | return FWL_WidgetHit::HyperLink; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 154 | } |
| 155 | const FX_WCHAR* CXFA_FFText::GetLinkURLAtPoint(FX_FLOAT fx, FX_FLOAT fy) { |
| 156 | CXFA_TextLayout* pTextLayout = m_pDataAcc->GetTextLayout(); |
tsepez | 783a7e0 | 2017-01-17 11:05:57 -0800 | [diff] [blame] | 157 | if (!pTextLayout) |
dsinclair | 85d1f2c | 2016-06-23 12:40:16 -0700 | [diff] [blame] | 158 | return nullptr; |
tsepez | 783a7e0 | 2017-01-17 11:05:57 -0800 | [diff] [blame] | 159 | |
| 160 | FX_FLOAT x(fx); |
| 161 | FX_FLOAT y(fy); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 162 | FWLToClient(x, y); |
tsepez | 783a7e0 | 2017-01-17 11:05:57 -0800 | [diff] [blame] | 163 | |
| 164 | for (const auto& pPieceLine : *pTextLayout->GetPieceLines()) { |
| 165 | for (const auto& pPiece : pPieceLine->m_textPieces) { |
| 166 | if (pPiece->pLinkData && pPiece->rtPiece.Contains(x, y)) |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 167 | return pPiece->pLinkData->GetLinkURL(); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 168 | } |
| 169 | } |
dsinclair | 85d1f2c | 2016-06-23 12:40:16 -0700 | [diff] [blame] | 170 | return nullptr; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 171 | } |
| 172 | void CXFA_FFText::FWLToClient(FX_FLOAT& fx, FX_FLOAT& fy) { |
| 173 | CFX_RectF rtWidget; |
| 174 | GetRectWithoutRotate(rtWidget); |
| 175 | fx -= rtWidget.left; |
| 176 | fy -= rtWidget.top; |
| 177 | } |