PEBL 2.2
Psychology Experiment Building Language - Cross-platform psychological experiment development system
sdl/PlatformTextBox.h
Go to the documentation of this file.
1//* -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*- */
3// Name: src/platforms/sdl/PlatformTextBox.h
4// Purpose: Contains platform-specific interface for text boxes
5// Author: Shane T. Mueller, Ph.D.
6// Copyright: (c) 2003-2026 Shane T. Mueller <smueller@obereed.net>
7// License: GPL 2
8//
9//
10//
11// This file is part of the PEBL project.
12//
13// PEBL is free software; you can redistribute it and/or modify
14// it under the terms of the GNU General Public License as published by
15// the Free Software Foundation; either version 2 of the License, or
16// (at your option) any later version.
17//
18// PEBL is distributed in the hope that it will be useful,
19// but WITHOUT ANY WARRANTY; without even the implied warranty of
20// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21// GNU General Public License for more details.
22//
23// You should have received a copy of the GNU General Public License
24// along with PEBL; if not, write to the Free Software
25// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27#ifndef __PLATFORMTEXTBOX_H__
28#define __PLATFORMTEXTBOX_H__
29
30#include "PlatformWidget.h"
31#include "PlatformFont.h"
32#include "../../objects/PTextBox.h"
33
34#ifdef PEBL_OSX
35#include "SDL.h"
36#include "SDL_ttf.h"
37#else
38#include "SDL.h"
39#include "SDL_ttf.h"
40#endif
41
42#include "../../utility/rc_ptrs.h"
43#include "../../base/PEBLObject.h"
44#include "../../utility/FormatParser.h"
45
46#include <vector>
47#include <map>
48#include <tuple>
49
53
54
55class PlatformTextBox: virtual public PlatformWidget, virtual public PTextBox
56{
57public:
58 PlatformTextBox(std::string text, counted_ptr<PEBLObjectBase> font, int width, int height);
59
61 PlatformTextBox(const PlatformTextBox & textbox);
62
63 virtual ~PlatformTextBox();
64
65
66 virtual bool RenderText();
67
68 virtual bool SetProperty(std::string, Variant v);
69 virtual Variant GetProperty(std::string name)const;
70 //These need to be overridden because the text needs to be re-rendered when they are called.
71 virtual void SetFont(counted_ptr<PEBLObjectBase> font);
72 virtual void SetText(std::string text);
73 virtual void SetHeight(int h);
74 virtual void SetWidth(int w);
75
76 //This needs to be overridden so that it returns a PlatformFont.
77 // Return adaptive font if it exists, otherwise return original
79 return mAdaptiveFontObject.get() ? mAdaptiveFontObject : mFontObject;
80 }
81
82
83 virtual bool Draw();
84 virtual void HandleKeyPress(int keycode, int modkeys, Uint16 unicode);
85 virtual void HandleTextInput(std::string input);
86 virtual std::string ObjectName()const{return "PlatformTextBox";} ;
87 virtual int FindCursorPosition(long int x, long int y);
88 virtual void SetEditable(bool val);
90protected:
91
92 virtual std::ostream & SendToStream(std::ostream& out) const;
93
94 std::vector<int> mBreaks;
95
96private:
97 // Helper to get PlatformFont pointer from counted_ptr when needed
98 // Returns adaptive font if it exists, otherwise returns original font
99 PlatformFont* GetPlatformFont() const {
100 if (mAdaptiveFontObject.get()) {
101 return dynamic_cast<PlatformFont*>(mAdaptiveFontObject.get());
102 }
103 return dynamic_cast<PlatformFont*>(mFontObject.get());
104 }
105
106 void FindBreaks();
107 int FindNextLineBreak(unsigned int curposition);
108
109 // Size-aware line breaking for formatted text
110 void FindBreaksFormatted(const std::vector<FormatParser::FormatSegment>& segments);
111 void FindBreaksInSegment(const FormatParser::FormatSegment& seg,
112 PlatformFont* segFont,
113 int& currentLineWidth,
114 int& currentLineMaxHeight,
115 std::string& currentLineText,
116 int& strippedTextPos,
117 int& totalHeight);
118
119 void DrawCursor();
120
121 counted_ptr<PEBLObjectBase> mFontObject;
122 counted_ptr<PEBLObjectBase> mAdaptiveFontObject; // Holds adapted font separately to avoid destructor cascade
123 std::vector<counted_ptr<PEBLObjectBase>> mIntermediateFonts; // Keeps intermediate adaptive fonts alive
124 bool mIsUTF8;
125 std::string mStrippedText; // For formatted mode: stores text with tags stripped (used for line breaking)
126
127};
128
129
130
131#endif
This class is the basic generic text box.
Definition PTextBox.h:40
Validator platform textbox - no rendering, used only for compilation.
virtual void SetHeight(int h)
virtual counted_ptr< PEBLObjectBase > GetFont() const
virtual int FindCursorPosition(long int x, long int y)
virtual void SetFont(counted_ptr< PEBLObjectBase > font)
virtual void HandleKeyPress(int keycode, int modkeys, Uint16 unicode)
virtual void SetText(std::string text)
virtual void HandleTextInput(std::string input)
PlatformTextBox(std::string text, counted_ptr< PEBLObjectBase > font, int width, int height)
virtual void SetEditable(bool val)
virtual bool Draw()
This method initiates everything needed to display the main window
virtual Variant GetProperty(std::string name) const
virtual ~PlatformTextBox()
Standard Destructor.
virtual std::string ObjectName() const
virtual bool RenderText()
std::vector< int > mBreaks
virtual bool SetProperty(std::string, Variant v)
virtual std::ostream & SendToStream(std::ostream &out) const
An inheritable printing class used by PEBLObjectBase::operator<<.
virtual void SetWidth(int w)
X * get() const
Definition rc_ptrs.h:110