// FIXME: leaks
result RichTextPanel::Construct(const Dimension& dim, const String & text) {
	result r = E_SUCCESS;
	TextElement* textElement;
	int width, height;
	Color color = Color::GetColor(COLOR_ID_WHITE);
	Font font;

	Dimension textDim;
	int textActualLen;

	enrichedText = new EnrichedText();
	r = enrichedText->Construct(dim);
	TryCatch(r == E_SUCCESS, , "Failed Construct EnrichedText");

	r = font.Construct(FONT_STYLE_PLAIN, TEXT_FONT_SIZE);
	TryCatch(r == E_SUCCESS, , "Failed Construct Font");

	textElement = new TextElement();
	r = textElement->Construct(text);
	TryCatch(r == E_SUCCESS, , "Failed TextElement");
	r = textElement->SetTextColor(color);
	TryCatch(r == E_SUCCESS, , "Failed SetTextColor");
	r = textElement->SetFont(font);
	TryCatch(r == E_SUCCESS, , "Failed SetFont");

	r = enrichedText->Add(*textElement);
	TryCatch(r == E_SUCCESS, , "Failed Add textElement");


	height = enrichedText->GetTotalLineHeight();

	r = enrichedText->GetTextExtent(0, text.GetLength(), textDim, textActualLen);
	TryCatch(r == E_SUCCESS, , "Failed GetTextExtent");
	width = dim.width < textDim.width ? dim.width : textDim.width;

	r = Panel::Construct(Rectangle(0, 0, width, height));
	TryCatch(r == E_SUCCESS, , "Failed Construct RichTextPanel parent");

	TryCatch(GetLastResult() == E_SUCCESS, r = GetLastResult(), "Failed GetSize");
	AppLog("Size received: %d x %d", width, height);

	r = SetSize(width, height);
	TryCatch(r == E_SUCCESS, , "Failed SetSize");

//	SetBackgroundColor(color);
	TryCatch(GetLastResult() == E_SUCCESS, r = GetLastResult(), "Failed SetBackgroundColor");

	return r;

	CATCH:
	    AppLogException("$${Function:Construct} is failed.", GetErrorMessage(r));

	    if (textElement)
	    	delete textElement;

	    return r;
}
Esempio n. 2
0
	virtual result DrawButton(Canvas& canvas)
	{
		canvas.SetBackgroundColor(Color(0, 0, 0, 0));
		canvas.Clear();

		if (_state == BUTTON_STATE_NORMAL)
		{
			canvas.FillRoundRectangle(_pallete[BUTTON_COLOR_BG_NORMAL], Rectangle(0, 0 , _bounds.width, _bounds.height), Dimension(5, 5));
		}
		else
		{
			canvas.FillRoundRectangle(_pallete[BUTTON_COLOR_BG_PRESSED], Rectangle(0, 0 , _bounds.width, _bounds.height), Dimension(5, 5));
		}

        Font font;
        font.Construct(FONT_STYLE_PLAIN, 18);
        canvas.SetFont(font);

        if (_text != L"")
        {
        	EnrichedText enriched;
        	enriched.Construct(Dimension(_bounds.width, _bounds.height));
        	enriched.SetVerticalAlignment(TEXT_ALIGNMENT_MIDDLE);
        	enriched.SetHorizontalAlignment(TEXT_ALIGNMENT_CENTER);

        	TextElement element;
        	element.Construct(_text);
    		if (_state == BUTTON_STATE_NORMAL)
    		{
    			element.SetTextColor(_pallete[BUTTON_COLOR_FG_NORMAL]);
    		}
    		else
    		{
    			element.SetTextColor(_pallete[BUTTON_COLOR_FG_PRESSED]);
    		}
    		enriched.Add(element);
    		canvas.DrawText(Point(0, 0), enriched);
    		enriched.RemoveAll(false);
        }

		return E_SUCCESS;
	}
Esempio n. 3
0
void DrawingClass::FDrawTextToCanvas(Osp::Graphics::Canvas * oncanvas, Osp::Graphics::Rectangle destdim, String text, int fontsize, int fontstyle, bool wordwrap, Osp::Graphics::Color fontcolor, Osp::Graphics::TextHorizontalAlignment halign, Osp::Graphics::TextVerticalAlignment valign) {
	Font font_;
	font_.Construct(fontstyle, fontsize);
	EnrichedText texteel;
	texteel.Construct(Dimension(destdim.width, destdim.height));
	texteel.SetHorizontalAlignment(halign);
	texteel.SetVerticalAlignment(valign);
	if (wordwrap) {
		texteel.SetTextWrapStyle(TEXT_WRAP_WORD_WRAP);
	}
	TextElement textel;
	textel.Construct(text);
	textel.SetTextColor(fontcolor);
	textel.SetFont(font_);
	texteel.Add(textel);
	oncanvas->DrawText(Point(destdim.x,destdim.y), texteel);
}
 result DrawElement(const Osp::Graphics::Canvas& canvas, const Osp::Graphics::Rectangle& rect, CustomListItemStatus itemStatus)
 {
     result r = E_SUCCESS;

     Canvas* pCanvas = const_cast<Canvas*>(&canvas);

     Font textfont;
     textfont.Construct(Osp::Graphics::FONT_STYLE_PLAIN, 28);

     EnrichedText texteel;
     texteel.Construct(Dimension(rect.width, rect.height));
   	 texteel.SetHorizontalAlignment(Osp::Graphics::TEXT_ALIGNMENT_LEFT);
     texteel.SetVerticalAlignment(Osp::Graphics::TEXT_ALIGNMENT_MIDDLE);
     texteel.SetTextWrapStyle(TEXT_WRAP_CHARACTER_WRAP);
     texteel.SetTextAbbreviationEnabled(true);
     TextElement textelcol;
     textelcol.Construct(text);
     textelcol.SetTextColor(Color::COLOR_WHITE);
     textelcol.SetFont(textfont);
     texteel.Add(textelcol);
     pCanvas->DrawText(Point(0,0), texteel);

     return r;
 }
Esempio n. 5
0
    bool drawText(const char * pszText, Dimension& dimensions, CCImage::ETextAlign alignment, const char * fontName = NULL, int fontSize = 0)
    {
        bool nRet = false;
        do
        {
            CC_BREAK_IF(pszText == NULL || strlen(pszText) <= 0);
            // text
            Osp::Base::String strText(pszText);
            // Set a font to the TextElement
            Font font;
            bool bUseDefaultFont = true;
            if (fontName != NULL && strlen(fontName) > 0)
            {
                String strFonName(fontName);
                if (strFonName.EndsWith(".ttf") || strFonName.EndsWith(".TTF"))
                {
                    bUseDefaultFont = false;
                    const char* pFullFontPath = CCFileUtils::fullPathFromRelativePath(fontName);
                    font.Construct(pFullFontPath, FONT_STYLE_PLAIN, fontSize);
                }
                else
                {
                    IList* pSystemFontList = Font::GetSystemFontListN();
                    if (pSystemFontList != NULL)
                    {
                        IEnumerator* pEnum = pSystemFontList->GetEnumeratorN();
                        Object* pObj = null;
                        while (pEnum->MoveNext() == E_SUCCESS)
                        {
                            pObj = pEnum->GetCurrent();
                            String* pStrName = static_cast<String*>(pObj);
                            if (pStrName->Equals(strFonName, false))
                            {
                                bUseDefaultFont = false;
                                font.Construct(*pStrName, FONT_STYLE_PLAIN, fontSize);
                                break;
                            }
                        }

                        delete pEnum;

                        pSystemFontList->RemoveAll(true);
                        delete pSystemFontList;
                    }
                }
            }

            if (bUseDefaultFont)
            {
                font.Construct(FONT_STYLE_PLAIN, fontSize);
            }

            // calculate text size
            if (dimensions.width <= 0)
            {
                Dimension dim;
                font.GetTextExtent(strText, strText.GetLength(), dim);
                dimensions.width = dim.width;
                dimensions.height = dim.height;
            }

            CC_BREAK_IF(dimensions.width <= 0);

            // Create an EnrichedText
            m_pEnrichedText = new EnrichedText();
            m_pEnrichedText->Construct(Dimension(dimensions.width, 10));

            switch (alignment)
            {
            case CCImage::kAlignCenter:
                m_pEnrichedText->SetHorizontalAlignment(TEXT_ALIGNMENT_CENTER);
                m_pEnrichedText->SetVerticalAlignment(TEXT_ALIGNMENT_MIDDLE);
                break;
            case CCImage::kAlignTop:
                m_pEnrichedText->SetHorizontalAlignment(TEXT_ALIGNMENT_CENTER);
                m_pEnrichedText->SetVerticalAlignment(TEXT_ALIGNMENT_TOP);
                break;
            case CCImage::kAlignTopRight:
                m_pEnrichedText->SetHorizontalAlignment(TEXT_ALIGNMENT_RIGHT);
                m_pEnrichedText->SetVerticalAlignment(TEXT_ALIGNMENT_TOP);
                break;
            case CCImage::kAlignRight:
                m_pEnrichedText->SetHorizontalAlignment(TEXT_ALIGNMENT_RIGHT);
                m_pEnrichedText->SetVerticalAlignment(TEXT_ALIGNMENT_MIDDLE);
                break;
            case CCImage::kAlignBottomRight:
                m_pEnrichedText->SetHorizontalAlignment(TEXT_ALIGNMENT_RIGHT);
                m_pEnrichedText->SetVerticalAlignment(TEXT_ALIGNMENT_BOTTOM);
                break;
            case CCImage::kAlignBottom:
                m_pEnrichedText->SetHorizontalAlignment(TEXT_ALIGNMENT_CENTER);
                m_pEnrichedText->SetVerticalAlignment(TEXT_ALIGNMENT_BOTTOM);
                break;
            case CCImage::kAlignBottomLeft:
                m_pEnrichedText->SetHorizontalAlignment(TEXT_ALIGNMENT_LEFT);
                m_pEnrichedText->SetVerticalAlignment(TEXT_ALIGNMENT_BOTTOM);
                break;
            case CCImage::kAlignLeft:
                m_pEnrichedText->SetHorizontalAlignment(TEXT_ALIGNMENT_LEFT);
                m_pEnrichedText->SetVerticalAlignment(TEXT_ALIGNMENT_MIDDLE);
                break;
            case CCImage::kAlignTopLeft:
                m_pEnrichedText->SetHorizontalAlignment(TEXT_ALIGNMENT_LEFT);
                m_pEnrichedText->SetVerticalAlignment(TEXT_ALIGNMENT_TOP);
                break;
            default:
                m_pEnrichedText->SetHorizontalAlignment(TEXT_ALIGNMENT_CENTER);
                m_pEnrichedText->SetVerticalAlignment(TEXT_ALIGNMENT_MIDDLE);
                break;
            }
            // Set attributes of the EnrichedText
            m_pEnrichedText->SetTextWrapStyle(TEXT_WRAP_CHARACTER_WRAP);
            m_pEnrichedText->SetTextAbbreviationEnabled(true);

            // Create a TextElement
            TextElement* pTextElement = new TextElement();
            pTextElement->Construct(strText);
            // After Adding, set attributes of the TextElement
            pTextElement->SetTextColor(Color::COLOR_WHITE);
            pTextElement->SetFont(font);
            // Add the TextElement to the EnrichedText
            m_pEnrichedText->Add(*pTextElement);

            m_pEnrichedText->Refresh();
            dimensions.height = m_pEnrichedText->GetTotalLineHeight();
            m_pEnrichedText->SetSize(dimensions.width, dimensions.height);

            CC_SAFE_DELETE(m_pCanvas);
            m_pCanvas = new Canvas();
            m_pCanvas->Construct(Rectangle(0, 0, dimensions.width, dimensions.height));
            m_pCanvas->DrawText(Point(0, 0), *m_pEnrichedText);

            m_pEnrichedText->RemoveAllTextElements(true);
            CC_SAFE_DELETE(m_pEnrichedText);

            nRet = true;
        } while (0);
        return nRet;
    }