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 "../../include/pdfwindow/PDFWindow.h"
8 #include "../../include/pdfwindow/PWL_Wnd.h"
9 #include "../../include/pdfwindow/PWL_Label.h"
10 #include "../../include/pdfwindow/PWL_Utils.h"
11 
12 /* ---------------------------- CPWL_Label ------------------------------ */
13 
CPWL_Label()14 CPWL_Label::CPWL_Label() : m_pEdit(NULL)
15 {
16 	m_pEdit = IFX_Edit::NewEdit();
17 
18 	ASSERT(m_pEdit != NULL);
19 }
20 
~CPWL_Label()21 CPWL_Label::~CPWL_Label()
22 {
23 	IFX_Edit::DelEdit(m_pEdit);
24 }
25 
GetClassName() const26 CFX_ByteString CPWL_Label::GetClassName() const
27 {
28 	return "CPWL_Label";
29 }
30 
OnCreated()31 void CPWL_Label::OnCreated()
32 {
33 	SetParamByFlag();
34 	SetFontSize(this->GetCreationParam().fFontSize);
35 
36 	m_pEdit->SetFontMap(this->GetFontMap());
37 	m_pEdit->Initialize();
38 
39 	if (HasFlag(PES_TEXTOVERFLOW))
40 	{
41 		SetClipRect(CPDF_Rect(0.0f,0.0f,0.0f,0.0f));
42 		m_pEdit->SetTextOverflow(TRUE);
43 	}
44 }
45 
SetText(FX_LPCWSTR csText)46 void CPWL_Label::SetText(FX_LPCWSTR csText)
47 {
48 	m_pEdit->SetText(csText);
49 }
50 
RePosChildWnd()51 void CPWL_Label::RePosChildWnd()
52 {
53 	m_pEdit->SetPlateRect(GetClientRect());
54 }
55 
SetFontSize(FX_FLOAT fFontSize)56 void CPWL_Label::SetFontSize(FX_FLOAT fFontSize)
57 {
58 	m_pEdit->SetFontSize(fFontSize);
59 }
60 
GetFontSize() const61 FX_FLOAT CPWL_Label::GetFontSize() const
62 {
63 	return m_pEdit->GetFontSize();
64 }
65 
SetParamByFlag()66 void CPWL_Label::SetParamByFlag()
67 {
68 	if (HasFlag(PES_LEFT))
69 	{
70 		m_pEdit->SetAlignmentH(0);
71 	}
72 	else if (HasFlag(PES_MIDDLE))
73 	{
74 		m_pEdit->SetAlignmentH(1);
75 	}
76 	else if (HasFlag(PES_RIGHT))
77 	{
78 		m_pEdit->SetAlignmentH(2);
79 	}
80 	else
81 	{
82 		m_pEdit->SetAlignmentH(0);
83 	}
84 
85 	if (HasFlag(PES_TOP))
86 	{
87 		m_pEdit->SetAlignmentV(0);
88 	}
89 	else if (HasFlag(PES_CENTER))
90 	{
91 		m_pEdit->SetAlignmentV(1);
92 	}
93 	else if (HasFlag(PES_BOTTOM))
94 	{
95 		m_pEdit->SetAlignmentV(2);
96 	}
97 	else
98 	{
99 		m_pEdit->SetAlignmentV(0);
100 	}
101 
102 	if (HasFlag(PES_PASSWORD))
103 	{
104 		m_pEdit->SetPasswordChar('*');
105 	}
106 
107 	m_pEdit->SetMultiLine(HasFlag(PES_MULTILINE));
108 	m_pEdit->SetAutoReturn(HasFlag(PES_AUTORETURN));
109 	m_pEdit->SetAutoFontSize(HasFlag(PWS_AUTOFONTSIZE));
110 	m_pEdit->SetAutoScroll(HasFlag(PES_AUTOSCROLL));
111 }
112 
DrawThisAppearance(CFX_RenderDevice * pDevice,CPDF_Matrix * pUser2Device)113 void CPWL_Label::DrawThisAppearance(CFX_RenderDevice* pDevice, CPDF_Matrix* pUser2Device)
114 {
115 	CPWL_Wnd::DrawThisAppearance(pDevice,pUser2Device);
116 
117 	GetClientRect();
118 
119 	CPDF_Rect rcClip;
120 	CPVT_WordRange wrRange = m_pEdit->GetVisibleWordRange();
121 	CPVT_WordRange* pRange = NULL;
122 
123 	if (!HasFlag(PES_TEXTOVERFLOW))
124 	{
125 		rcClip = GetClientRect();
126 		pRange = &wrRange;
127 	}
128 IFX_SystemHandler* pSysHandler = GetSystemHandler();
129 	IFX_Edit::DrawEdit(pDevice, pUser2Device, m_pEdit,
130 		CPWL_Utils::PWLColorToFXColor(GetTextColor(), this->GetTransparency()),
131 		CPWL_Utils::PWLColorToFXColor(GetTextStrokeColor(), this->GetTransparency()),
132 		rcClip, CPDF_Point(0.0f,0.0f), pRange,pSysHandler, NULL);
133 }
134 
SetHorzScale(FX_INT32 nHorzScale)135 void CPWL_Label::SetHorzScale(FX_INT32 nHorzScale)
136 {
137 	m_pEdit->SetHorzScale(nHorzScale);
138 }
139 
SetCharSpace(FX_FLOAT fCharSpace)140 void CPWL_Label::SetCharSpace(FX_FLOAT fCharSpace)
141 {
142 	m_pEdit->SetCharSpace(fCharSpace);
143 }
144 
GetContentRect() const145 CPDF_Rect CPWL_Label::GetContentRect() const
146 {
147 	return m_pEdit->GetContentRect();
148 }
149 
GetThisAppearanceStream(CFX_ByteTextBuf & sAppStream)150 void CPWL_Label::GetThisAppearanceStream(CFX_ByteTextBuf & sAppStream)
151 {
152 	CPWL_Wnd::GetThisAppearanceStream(sAppStream);
153 
154 	sAppStream << GetTextAppearanceStream(CPDF_Point(0.0f, 0.0f));
155 }
156 
GetTextAppearanceStream(const CPDF_Point & ptOffset) const157 CFX_ByteString CPWL_Label::GetTextAppearanceStream(const CPDF_Point & ptOffset) const
158 {
159 	CFX_ByteTextBuf sRet;
160 	CFX_ByteString sEdit = CPWL_Utils::GetEditAppStream(m_pEdit,ptOffset);
161 
162 	if (sEdit.GetLength() > 0)
163 	{
164 		sRet << "BT\n" << CPWL_Utils::GetColorAppStream(GetTextColor()) << sEdit << "ET\n";
165 	}
166 
167 	return sRet.GetByteString();
168 }
169 
GetText() const170 CFX_WideString CPWL_Label::GetText() const
171 {
172 	return m_pEdit->GetText();
173 }
174 
SetLimitChar(FX_INT32 nLimitChar)175 void CPWL_Label::SetLimitChar(FX_INT32 nLimitChar)
176 {
177 	m_pEdit->SetLimitChar(nLimitChar);
178 }
179 
GetTotalWords()180 FX_INT32 CPWL_Label::GetTotalWords()
181 {
182 	if (m_pEdit)
183 		return m_pEdit->GetTotalWords();
184 
185 	return 0;
186 }
187 
188