214 lines
4.9 KiB
C++

/**
*******************************************************************************
* @file ewol/widget/Button.cpp
* @brief ewol Button widget system (Sources)
* @author Edouard DUPIN
* @date 07/11/2011
* @par Project
* ewol
*
* @par Copyright
* Copyright 2011 Edouard DUPIN, all right reserved
*
* This software is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY.
*
* Licence summary :
* You can modify and redistribute the sources code and binaries.
* You can send me the bug-fix
*
* Term of the licence in in the file licence.txt.
*
*******************************************************************************
*/
#include <ewol/widget/Button.h>
#include <ewol/OObject.h>
#include <ewol/WidgetManager.h>
extern const char * const ewolEventButtonPressed = "ewol Button Pressed";
extern const char * const ewolEventButtonEnter = "ewol Button Enter";
extern const char * const ewolEventButtonLeave = "ewol Button Leave";
#undef __class__
#define __class__ "ewol::Button"
void ewol::Button::Init(void)
{
AddEventId(ewolEventButtonPressed);
AddEventId(ewolEventButtonEnter);
AddEventId(ewolEventButtonLeave);
m_alignement = ewol::TEXT_ALIGN_CENTER;
#ifdef __PLATFORM__Android
m_padding.y = 12;
m_padding.x = 12;
#else
m_padding.y = 4;
m_padding.x = 4;
#endif
m_textColorFg.red = 0.0;
m_textColorFg.green = 0.0;
m_textColorFg.blue = 0.0;
m_textColorFg.alpha = 1.0;
m_textColorBg.red = 0.0;
m_textColorBg.green = 0.0;
m_textColorBg.blue = 0.0;
m_textColorBg.alpha = 0.25;
SetCanHaveFocus(true);
}
ewol::Button::Button(void)
{
m_label = "No Label";
Init();
}
ewol::Button::Button(etk::UString newLabel)
{
m_label = newLabel;
Init();
}
ewol::Button::~Button(void)
{
}
void ewol::Button::SetPadding(coord2D_ts newPadding)
{
m_padding = newPadding;
}
bool ewol::Button::CalculateMinSize(void)
{
int32_t fontId = GetDefaultFontId();
int32_t minWidth = ewol::GetWidth(fontId, m_label);
int32_t minHeight = ewol::GetHeight(fontId);
m_minSize.x = m_padding.x*2 + minWidth;
m_minSize.y = m_padding.y*2 + minHeight;
MarkToReedraw();
return true;
}
void ewol::Button::SetLabel(etk::UString newLabel)
{
m_label = newLabel;
}
void ewol::Button::SetValue(bool val)
{
}
void ewol::Button::SetAlignement(textAlignement_te typeAlign)
{
m_alignement = typeAlign;
MarkToReedraw();
}
bool ewol::Button::GetValue(void)
{
return false;
}
void ewol::Button::OnRegenerateDisplay(void)
{
if (true == NeedRedraw()) {
// clean the object list ...
ClearOObjectList();
int32_t tmpSizeX = m_minSize.x;
int32_t tmpSizeY = m_minSize.y;
int32_t tmpOriginX = (m_size.x - m_minSize.x) / 2;
int32_t tmpOriginY = (m_size.y - m_minSize.y) / 2;
// no change for the text orogin :
int32_t tmpTextOriginX = (m_size.x - m_minSize.x) / 2 + m_padding.x;
int32_t tmpTextOriginY = (m_size.y - m_minSize.y) / 2 + m_padding.y;
if (true==m_userFillX) {
tmpSizeX = m_size.x;
tmpOriginX = 0;
if (m_alignement == ewol::TEXT_ALIGN_LEFT) {
tmpTextOriginX = m_padding.x;
}
}
if (true==m_userFillY) {
tmpSizeY = m_size.y;
tmpOriginY = 0;
}
tmpOriginX += m_padding.x;
tmpOriginY += m_padding.y;
tmpSizeX -= 2*m_padding.x;
tmpSizeY -= 2*m_padding.y;
ewol::OObject2DText * tmpText = new ewol::OObject2DText("", -1, m_textColorFg);
/*
int32_t fontId = GetDefaultFontId();
int32_t fontHeight = ewol::GetHeight(fontId);
int32_t fontWidth = ewol::GetWidth(fontId, m_label.c_str());
*/
coord2D_ts textPos;
textPos.x = tmpTextOriginX;
textPos.y = tmpTextOriginY;
clipping_ts drawClipping;
drawClipping.x = m_padding.x;
drawClipping.y = m_padding.y;
drawClipping.w = m_size.x - 2*m_padding.x;
drawClipping.h = m_size.y - 2*m_padding.y;
tmpText->Text(textPos, drawClipping, m_label);
ewol::OObject2DColored * tmpOObjects = new ewol::OObject2DColored;
tmpOObjects->SetColor(m_textColorBg);
tmpOriginX -= m_padding.x/2;
tmpOriginY -= m_padding.y/2;
tmpSizeX += m_padding.x/1;
tmpSizeY += m_padding.y/1;
tmpOObjects->Rectangle( tmpOriginX, tmpOriginY, tmpSizeX, tmpSizeY);
AddOObject(tmpOObjects);
AddOObject(tmpText);
}
}
bool ewol::Button::OnEventInput(int32_t IdInput, eventInputType_te typeEvent, etkFloat_t x, etkFloat_t y)
{
//EWOL_DEBUG("Event on BT ...");
if (1 == IdInput) {
if( ewol::EVENT_INPUT_TYPE_SINGLE == typeEvent
|| ewol::EVENT_INPUT_TYPE_DOUBLE == typeEvent
|| ewol::EVENT_INPUT_TYPE_TRIPLE == typeEvent) {
// nothing to do ...
GenEventInputExternal(ewolEventButtonPressed, x, y);
MarkToReedraw();
return true;
}
}
return false;
}
bool ewol::Button::OnEventKb(ewol::eventKbType_te typeEvent, uniChar_t unicodeData)
{
//EWOL_DEBUG("BT PRESSED : \"" << UTF8_data << "\" size=" << strlen(UTF8_data));
if( typeEvent == ewol::EVENT_KB_TYPE_DOWN
&& unicodeData == '\r') {
GenEventInputExternal(ewolEventButtonEnter, -1, -1);
}
return false;
}