void UIEditField::SetTextAlign(TEXT_ALIGN align) { if (!widget_) return; // safe cast? TBEditField* w = (TBEditField*) widget_; switch (align) { case TEXT_ALIGN_CENTER: w->SetTextAlign(TB_TEXT_ALIGN_CENTER); break; case TEXT_ALIGN_LEFT: w->SetTextAlign(TB_TEXT_ALIGN_LEFT); break; case TEXT_ALIGN_RIGHT: w->SetTextAlign(TB_TEXT_ALIGN_RIGHT); break; } }
virtual bool OnEvent(const TBWidgetEvent &ev) { if (ev.type == EVENT_TYPE_CLICK) { TBEditField *edit = GetWidgetByIDAndType<TBEditField>(TBIDC("editfield")); if (!edit) return false; if (ev.target->GetID() == TBIDC("clear")) { edit->SetText(""); return true; } else if (ev.target->GetID() == TBIDC("undo")) { edit->GetStyleEdit()->Undo(); return true; } else if (ev.target->GetID() == TBIDC("redo")) { edit->GetStyleEdit()->Redo(); return true; } else if (ev.target->GetID() == TBIDC("menu")) { static TBGenericStringItemSource source; if (!source.GetNumItems()) { source.AddItem(new TBGenericStringItem("Default font", TBIDC("default font"))); source.AddItem(new TBGenericStringItem("Default font (larger)", TBIDC("large font"))); source.AddItem(new TBGenericStringItem("RGB font (Neon)", TBIDC("rgb font Neon"))); source.AddItem(new TBGenericStringItem("RGB font (Orangutang)", TBIDC("rgb font Orangutang"))); source.AddItem(new TBGenericStringItem("RGB font (Orange)", TBIDC("rgb font Orange"))); source.AddItem(new TBGenericStringItem("-")); source.AddItem(new TBGenericStringItem("Glyph cache stresstest (CJK)", TBIDC("CJK"))); source.AddItem(new TBGenericStringItem("-")); source.AddItem(new TBGenericStringItem("Toggle wrapping", TBIDC("toggle wrapping"))); source.AddItem(new TBGenericStringItem("-")); source.AddItem(new TBGenericStringItem("Align left", TBIDC("align left"))); source.AddItem(new TBGenericStringItem("Align center", TBIDC("align center"))); source.AddItem(new TBGenericStringItem("Align right", TBIDC("align right"))); } if (TBMenuWindow *menu = new TBMenuWindow(ev.target, TBIDC("popup_menu"))) menu->Show(&source, TBPopupAlignment()); return true; } else if (ev.target->GetID() == TBIDC("popup_menu")) { if (ev.ref_id == TBIDC("default font")) edit->SetFontDescription(TBFontDescription()); else if (ev.ref_id == TBIDC("large font")) { TBFontDescription fd = g_font_manager->GetDefaultFontDescription(); fd.SetSize(28); edit->SetFontDescription(fd); } else if (ev.ref_id == TBIDC("rgb font Neon")) { TBFontDescription fd = edit->GetCalculatedFontDescription(); fd.SetID(TBIDC("Neon")); edit->SetFontDescription(fd); } else if (ev.ref_id == TBIDC("rgb font Orangutang")) { TBFontDescription fd = edit->GetCalculatedFontDescription(); fd.SetID(TBIDC("Orangutang")); edit->SetFontDescription(fd); } else if (ev.ref_id == TBIDC("rgb font Orange")) { TBFontDescription fd = edit->GetCalculatedFontDescription(); fd.SetID(TBIDC("Orange")); edit->SetFontDescription(fd); } else if (ev.ref_id == TBIDC("CJK")) { TBTempBuffer buf; for (int i = 0, cp = 0x4E00; cp <= 0x9FCC; cp++, i++) { char utf8[8]; int len = utf8::encode(cp, utf8); buf.Append(utf8, len); if (i % 64 == 63) buf.Append("\n", 1); } edit->GetStyleEdit()->SetText(buf.GetData(), buf.GetAppendPos()); } else if (ev.ref_id == TBIDC("toggle wrapping")) edit->SetWrapping(!edit->GetWrapping()); else if (ev.ref_id == TBIDC("align left")) edit->SetTextAlign(TB_TEXT_ALIGN_LEFT); else if (ev.ref_id == TBIDC("align center")) edit->SetTextAlign(TB_TEXT_ALIGN_CENTER); else if (ev.ref_id == TBIDC("align right")) edit->SetTextAlign(TB_TEXT_ALIGN_RIGHT); return true; } } return DemoWindow::OnEvent(ev); }