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