2020-02-03 04:54:55 -05:00
# pragma once
# include "PascalStr.h"
# include "PLWidgets.h"
2020-03-08 23:14:53 -04:00
# include "Vec2i.h"
struct DrawSurface ;
2020-02-03 04:54:55 -05:00
namespace PortabilityLayer
{
class EditboxWidget final : public WidgetSpec < EditboxWidget >
{
public :
2020-09-12 22:29:57 -04:00
typedef bool ( * CharacterFilterCallback_t ) ( void * context , uint8_t character ) ;
2020-02-03 04:54:55 -05:00
EditboxWidget ( const WidgetBasicState & state ) ;
~ EditboxWidget ( ) ;
2020-05-18 02:03:17 -04:00
bool Init ( const WidgetBasicState & state , const void * additionalData ) override ;
2020-02-03 04:54:55 -05:00
void DrawControl ( DrawSurface * surface ) override ;
void SetString ( const PLPasStr & str ) override ;
PLPasStr GetString ( ) const override ;
2020-02-16 20:55:47 -05:00
void GainFocus ( ) override ;
void LoseFocus ( ) override ;
2020-09-12 22:29:57 -04:00
WidgetHandleState_t ProcessEvent ( void * captureContext , const TimeTaggedVOSEvent & evt ) override ;
2020-02-16 20:55:47 -05:00
2020-03-01 17:01:35 -05:00
Rect GetExpandedRect ( ) const override ;
2020-10-10 02:42:06 -04:00
bool HandlesTickEvents ( ) const override ;
2020-02-16 20:55:47 -05:00
2020-02-16 21:57:02 -05:00
void SetSelection ( size_t startChar , size_t endChar ) ;
2020-03-08 23:14:53 -04:00
void SetMultiLine ( bool isMultiLine ) ;
2020-09-12 22:29:57 -04:00
void SetCharacterFilter ( void * context , CharacterFilterCallback_t callback ) ;
void SetCapacity ( size_t capacity ) ;
2020-02-03 04:54:55 -05:00
private :
2020-02-16 20:55:47 -05:00
static const unsigned int kCaratBlinkRate = 20 ;
2020-07-03 04:00:25 -04:00
static const unsigned int kMouseScrollRate = 20 ;
2020-02-16 20:55:47 -05:00
2020-03-08 23:14:53 -04:00
enum CaratSelectionAnchor
{
CaratSelectionAnchor_Start ,
CaratSelectionAnchor_End
} ;
2020-02-16 20:55:47 -05:00
void OnTick ( ) override ;
void Redraw ( ) ;
void HandleCharacter ( uint8_t keyChar , const uint32_t numRepeatsRequested ) ;
void HandleBackspace ( const uint32_t numRepeatsRequested ) ;
void HandleForwardDelete ( const uint32_t numRepeatsRequested ) ;
2020-03-08 23:14:53 -04:00
void HandleUpArrow ( const uint32_t numRepeatsRequested , bool shiftHeld ) ;
void HandleDownArrow ( const uint32_t numRepeatsRequested , bool shiftHeld ) ;
void HandleLeftArrow ( const uint32_t numRepeatsRequested , bool shiftHeld ) ;
void HandleRightArrow ( const uint32_t numRepeatsRequested , bool shiftHeld ) ;
2020-07-03 04:08:09 -04:00
void HandleHome ( bool shiftHeld ) ;
void HandleEnd ( bool shiftHeld ) ;
2020-03-08 23:14:53 -04:00
size_t FindVerticalMovementCaratPos ( const Vec2i & desiredPos , bool & isOutOfRange ) const ;
void HandleKeyMoveCarat ( size_t newPos , bool shiftHeld ) ;
2020-03-09 01:40:47 -04:00
WidgetHandleState_t HandleDragSelection ( const TimeTaggedVOSEvent & evt ) ;
2020-05-21 05:01:16 -04:00
void DrawSelection ( DrawSurface * surface , const Vec2i & basePoint , PortabilityLayer : : RenderedFont * font ) const ;
2020-03-08 23:14:53 -04:00
2020-07-03 04:00:25 -04:00
Vec2i ResolveCaratPos ( PortabilityLayer : : RenderedFont * rfont ) const ;
2020-03-08 23:14:53 -04:00
Vec2i ResolveBasePoint ( ) const ;
size_t ResolveCaratChar ( ) const ;
2020-07-03 04:00:25 -04:00
void AdjustScrollToCarat ( ) ;
void AdjustScrollToTextBounds ( ) ;
2020-03-08 23:14:53 -04:00
PortabilityLayer : : FontFamily * GetFontFamily ( ) const ;
PortabilityLayer : : RenderedFont * GetRenderedFont ( ) const ;
2020-02-03 04:54:55 -05:00
uint8_t * m_chars ;
size_t m_capacity ;
size_t m_length ;
size_t m_selStartChar ;
size_t m_selEndChar ;
2020-07-03 04:00:25 -04:00
CaratSelectionAnchor m_caratSelectionAnchor ; // Where the carat is attached to the selection range
2020-03-08 23:14:53 -04:00
2020-07-03 04:00:25 -04:00
Vec2i m_caratScrollPosition ; // Ideal position of the carat in the editbox, but not necessarily its actual location (i.e. may be in the middle of a glyph)
2020-10-10 02:42:06 -04:00
bool m_caratScrollLocked ; // If true, the vertical position
2020-07-03 04:00:25 -04:00
Vec2i m_scrollOffset ;
2020-02-16 20:55:47 -05:00
bool m_hasFocus ;
2020-03-08 23:14:53 -04:00
bool m_isMultiLine ;
2020-03-09 01:40:47 -04:00
bool m_isDraggingSelection ;
size_t m_dragSelectionStartChar ;
2020-02-16 20:55:47 -05:00
uint16_t m_caratTimer ;
2020-09-12 22:29:57 -04:00
CharacterFilterCallback_t m_characterFilter ;
void * m_characterFilterContext ;
2020-02-03 04:54:55 -05:00
} ;
}