Merge 'remotes/trunk'
[0ad.git] / source / gui / CText.cpp
blob5a2d6be813648181997ec3bc659eddaa2f311802
1 /* Copyright (C) 2016 Wildfire Games.
2 * This file is part of 0 A.D.
4 * 0 A.D. is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
9 * 0 A.D. is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
18 #include "precompiled.h"
20 #include "CText.h"
22 #include "CGUIScrollBarVertical.h"
23 #include "GUI.h"
25 #include "lib/ogl.h"
27 CText::CText()
29 AddSetting(GUIST_float, "buffer_zone");
30 AddSetting(GUIST_CGUIString, "caption");
31 AddSetting(GUIST_int, "cell_id");
32 AddSetting(GUIST_bool, "clip");
33 AddSetting(GUIST_CStrW, "font");
34 AddSetting(GUIST_bool, "scrollbar");
35 AddSetting(GUIST_CStr, "scrollbar_style");
36 AddSetting(GUIST_bool, "scroll_bottom");
37 AddSetting(GUIST_bool, "scroll_top");
38 AddSetting(GUIST_CGUISpriteInstance, "sprite");
39 AddSetting(GUIST_EAlign, "text_align");
40 AddSetting(GUIST_EVAlign, "text_valign");
41 AddSetting(GUIST_CColor, "textcolor");
42 AddSetting(GUIST_CColor, "textcolor_disabled");
43 AddSetting(GUIST_CStrW, "tooltip");
44 AddSetting(GUIST_CStr, "tooltip_style");
46 // Private settings
47 AddSetting(GUIST_CStrW, "_icon_tooltip");
48 AddSetting(GUIST_CStr, "_icon_tooltip_style");
50 //GUI<bool>::SetSetting(this, "ghost", true);
51 GUI<bool>::SetSetting(this, "scrollbar", false);
52 GUI<bool>::SetSetting(this, "clip", true);
54 // Add scroll-bar
55 CGUIScrollBarVertical* bar = new CGUIScrollBarVertical();
56 bar->SetRightAligned(true);
57 AddScrollBar(bar);
59 // Add text
60 AddText(new SGUIText());
63 CText::~CText()
67 void CText::SetupText()
69 if (!GetGUI())
70 return;
72 ENSURE(m_GeneratedTexts.size()>=1);
74 CStrW font;
75 if (GUI<CStrW>::GetSetting(this, "font", font) != PSRETURN_OK || font.empty())
76 // Use the default if none is specified
77 // TODO Gee: (2004-08-14) Don't define standard like this. Do it with the default style.
78 font = L"default";
80 CGUIString caption;
81 bool scrollbar;
82 GUI<CGUIString>::GetSetting(this, "caption", caption);
83 GUI<bool>::GetSetting(this, "scrollbar", scrollbar);
85 float width = m_CachedActualSize.GetWidth();
86 // remove scrollbar if applicable
87 if (scrollbar && GetScrollBar(0).GetStyle())
88 width -= GetScrollBar(0).GetStyle()->m_Width;
91 float buffer_zone = 0.f;
92 GUI<float>::GetSetting(this, "buffer_zone", buffer_zone);
93 *m_GeneratedTexts[0] = GetGUI()->GenerateText(caption, font, width, buffer_zone, this);
95 if (!scrollbar)
96 CalculateTextPosition(m_CachedActualSize, m_TextPos, *m_GeneratedTexts[0]);
98 // Setup scrollbar
99 if (scrollbar)
101 bool scroll_top = false, scroll_bottom = false;
102 GUI<bool>::GetSetting(this, "scroll_bottom", scroll_bottom);
103 GUI<bool>::GetSetting(this, "scroll_top", scroll_top);
105 // If we are currently scrolled to the bottom of the text,
106 // then add more lines of text, update the scrollbar so we
107 // stick to the bottom.
108 // (Use 1.5px delta so this triggers the first time caption is set)
109 bool bottom = false;
110 if (scroll_bottom && GetScrollBar(0).GetPos() > GetScrollBar(0).GetMaxPos() - 1.5f)
111 bottom = true;
113 GetScrollBar(0).SetScrollRange(m_GeneratedTexts[0]->m_Size.cy);
114 GetScrollBar(0).SetScrollSpace(m_CachedActualSize.GetHeight());
116 GetScrollBar(0).SetX(m_CachedActualSize.right);
117 GetScrollBar(0).SetY(m_CachedActualSize.top);
118 GetScrollBar(0).SetZ(GetBufferedZ());
119 GetScrollBar(0).SetLength(m_CachedActualSize.bottom - m_CachedActualSize.top);
121 if (bottom)
122 GetScrollBar(0).SetPos(GetScrollBar(0).GetMaxPos());
123 if (scroll_top)
124 GetScrollBar(0).SetPos(0.0f);
128 void CText::HandleMessage(SGUIMessage& Message)
130 IGUIScrollBarOwner::HandleMessage(Message);
131 //IGUITextOwner::HandleMessage(Message); <== placed it after the switch instead!
133 switch (Message.type)
135 case GUIM_SETTINGS_UPDATED:
136 if (Message.value == "scrollbar")
137 SetupText();
139 // Update scrollbar
140 if (Message.value == "scrollbar_style")
142 CStr scrollbar_style;
143 GUI<CStr>::GetSetting(this, Message.value, scrollbar_style);
145 GetScrollBar(0).SetScrollBarStyle(scrollbar_style);
147 SetupText();
150 break;
152 case GUIM_MOUSE_WHEEL_DOWN:
154 GetScrollBar(0).ScrollPlus();
155 // Since the scroll was changed, let's simulate a mouse movement
156 // to check if scrollbar now is hovered
157 SGUIMessage msg(GUIM_MOUSE_MOTION);
158 HandleMessage(msg);
159 break;
161 case GUIM_MOUSE_WHEEL_UP:
163 GetScrollBar(0).ScrollMinus();
164 // Since the scroll was changed, let's simulate a mouse movement
165 // to check if scrollbar now is hovered
166 SGUIMessage msg(GUIM_MOUSE_MOTION);
167 HandleMessage(msg);
168 break;
170 case GUIM_LOAD:
172 GetScrollBar(0).SetX(m_CachedActualSize.right);
173 GetScrollBar(0).SetY(m_CachedActualSize.top);
174 GetScrollBar(0).SetZ(GetBufferedZ());
175 GetScrollBar(0).SetLength(m_CachedActualSize.bottom - m_CachedActualSize.top);
177 CStr scrollbar_style;
178 GUI<CStr>::GetSetting(this, "scrollbar_style", scrollbar_style);
179 GetScrollBar(0).SetScrollBarStyle(scrollbar_style);
180 break;
183 default:
184 break;
187 IGUITextOwner::HandleMessage(Message);
190 void CText::Draw()
192 float bz = GetBufferedZ();
194 // First call draw on ScrollBarOwner
195 bool scrollbar;
196 GUI<bool>::GetSetting(this, "scrollbar", scrollbar);
198 if (scrollbar)
199 // Draw scrollbar
200 IGUIScrollBarOwner::Draw();
202 if (!GetGUI())
203 return;
205 CGUISpriteInstance* sprite;
206 int cell_id;
207 bool clip;
208 GUI<CGUISpriteInstance>::GetSettingPointer(this, "sprite", sprite);
209 GUI<int>::GetSetting(this, "cell_id", cell_id);
210 GUI<bool>::GetSetting(this, "clip", clip);
212 GetGUI()->DrawSprite(*sprite, cell_id, bz, m_CachedActualSize);
214 float scroll = 0.f;
215 if (scrollbar)
216 scroll = GetScrollBar(0).GetPos();
218 // Clipping area (we'll have to subtract the scrollbar)
219 CRect cliparea;
220 if (clip)
222 cliparea = m_CachedActualSize;
224 if (scrollbar)
226 // subtract scrollbar from cliparea
227 if (cliparea.right > GetScrollBar(0).GetOuterRect().left &&
228 cliparea.right <= GetScrollBar(0).GetOuterRect().right)
229 cliparea.right = GetScrollBar(0).GetOuterRect().left;
231 if (cliparea.left >= GetScrollBar(0).GetOuterRect().left &&
232 cliparea.left < GetScrollBar(0).GetOuterRect().right)
233 cliparea.left = GetScrollBar(0).GetOuterRect().right;
237 bool enabled;
238 GUI<bool>::GetSetting(this, "enabled", enabled);
240 CColor color;
241 GUI<CColor>::GetSetting(this, enabled ? "textcolor" : "textcolor_disabled", color);
243 if (scrollbar)
244 DrawText(0, color, m_CachedActualSize.TopLeft() - CPos(0.f, scroll), bz+0.1f, cliparea);
245 else
246 DrawText(0, color, m_TextPos, bz+0.1f, cliparea);
249 bool CText::MouseOverIcon()
251 for (SGUIText* const& guitext : m_GeneratedTexts)
252 for (const SGUIText::SSpriteCall& spritecall : guitext->m_SpriteCalls)
254 // Check mouse over sprite
255 if (!spritecall.m_Area.PointInside(GetMousePos() - m_CachedActualSize.TopLeft()))
256 continue;
258 // If tooltip exists, set the property
259 if (!spritecall.m_Tooltip.empty())
261 SetSetting("_icon_tooltip_style", spritecall.m_TooltipStyle);
262 SetSetting("_icon_tooltip", spritecall.m_Tooltip);
265 return true;
268 return false;