//============================================================================= //============================================================================= void UTBRendererBatcher::LoadDefaultResources() { g_tb_lng->Load("resources/language/lng_en.tb.txt"); // Load the default skin, and override skin that contains the graphics specific to the demo. g_tb_skin->Load("resources/default_skin/skin.tb.txt", "demo01/skin/skin.tb.txt"); // **README** // - define TB_FONT_RENDERER_FREETYPE in tb_config.h for non-demo #ifdef TB_FONT_RENDERER_TBBF void register_tbbf_font_renderer(); register_tbbf_font_renderer(); #endif #ifdef TB_FONT_RENDERER_STB void register_stb_font_renderer(); register_stb_font_renderer(); #endif #ifdef TB_FONT_RENDERER_FREETYPE void register_freetype_font_renderer(); register_freetype_font_renderer(); #endif // Add fonts we can use to the font manager. #if defined(TB_FONT_RENDERER_STB) || defined(TB_FONT_RENDERER_FREETYPE) g_font_manager->AddFontInfo("resources/vera.ttf", "Vera"); #endif #ifdef TB_FONT_RENDERER_TBBF g_font_manager->AddFontInfo("resources/default_font/segoe_white_with_shadow.tb.txt", "Segoe"); g_font_manager->AddFontInfo("fonts/neon.tb.txt", "Neon"); g_font_manager->AddFontInfo("fonts/orangutang.tb.txt", "Orangutang"); g_font_manager->AddFontInfo("fonts/orange.tb.txt", "Orange"); #endif // Set the default font description for widgets to one of the fonts we just added TBFontDescription fd; #ifdef TB_FONT_RENDERER_TBBF fd.SetID(TBIDC("Segoe")); #else fd.SetID(TBIDC("Vera")); #endif fd.SetSize(g_tb_skin->GetDimensionConverter()->DpToPx(14)); g_font_manager->SetDefaultFontDescription(fd); // Create the font now. TBFontFace *font = g_font_manager->CreateFontFace(g_font_manager->GetDefaultFontDescription()); // Render some glyphs in one go now since we know we are going to use them. It would work fine // without this since glyphs are rendered when needed, but with some extra updating of the glyph bitmap. if ( font ) { font->RenderGlyphs(" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~•·åäöÅÄÖ"); } root_.SetSkinBg(TBIDC("background")); TBWidgetsAnimationManager::Init(); }
void Init(unsigned int width, unsigned int height) { renderer = new TBRendererGL(); tb_core_init(renderer, "language/lng_en.tb.txt"); root = new AppRoot(); Resize(width, height); // Start listening to keyboard focus TBWidgetListener::AddGlobalListener(&edit_listener); // Load the default skin, and override skin that contains the graphics specific to the demo. g_tb_skin->Load("skin/skin.tb.txt", "demo_skin/skin.tb.txt"); // Register tbbf font renderer void register_tbbf_font_renderer(); register_tbbf_font_renderer(); // Add a font to the font manager. g_font_manager->AddFontInfo("font/segoe_white_with_shadow.tb.txt", "Segoe"); // Set the default font description for widgets to one of the fonts we just added TBFontDescription fd; fd.SetID(TBIDC("Segoe")); fd.SetSize(g_tb_skin->GetDimensionConverter()->DpToPx(14)); g_font_manager->SetDefaultFontDescription(fd); // Create the font now. TBFontFace *font = g_font_manager->CreateFontFace(g_font_manager->GetDefaultFontDescription()); // Give the root widget a background skin root->SetSkinBg("background_solid"); g_widgets_reader->LoadFile(root, "layout/main_layout.tb.txt"); }
MenubarItemWidget::MenubarItemWidget(MenubarItem *item, MenubarItemSource *source, TBSelectItemViewer *source_viewer, int index) : m_source(source) , m_source_viewer(source_viewer) , m_index(index) { SetSkinBg(TBIDC("TBSelectItem")); SetLayoutDistribution(LAYOUT_DISTRIBUTION_GRAVITY); SetLayoutDistributionPosition(LAYOUT_DISTRIBUTION_POSITION_LEFT_TOP); SetPaintOverflowFadeout(false); TBWidget* root = GetContentRoot(); TBFontDescription fd; fd.SetID(TBIDC("Vera")); fd.SetSize(12); TBTextField* text = new TBTextField(); text->SetIgnoreInput(true); text->SetText(item->str); text->SetFontDescription(fd); root->AddChild(text); if (item->shortcut_.Length()) { TBWidget* spacer = new TBWidget(); spacer->SetIgnoreInput(true); spacer->SetGravity(WIDGET_GRAVITY_LEFT_RIGHT); root->AddChild(spacer); TBTextField* shortcut = new TBTextField(); shortcut->SetIgnoreInput(true); shortcut->SetText(item->shortcut_.CString()); shortcut->SetFontDescription(fd); shortcut->SetGravity(WIDGET_GRAVITY_RIGHT); root->AddChild(shortcut); } }
int app_main() { application = new DemoApplication(); ApplicationBackend *application_backend = ApplicationBackend::Create(application, 1280, 700, "Demo"); if (!application_backend) return 1; tb_core_init(application_backend->GetRenderer(), "resources/language/lng_en.tb.txt"); // Load the default skin, and override skin that contains the graphics specific to the demo. g_tb_skin->Load("resources/default_skin/skin.tb.txt", "Demo/demo01/skin/skin.tb.txt"); // Register font renderers. #ifdef TB_FONT_RENDERER_TBBF void register_tbbf_font_renderer(); register_tbbf_font_renderer(); #endif #ifdef TB_FONT_RENDERER_STB void register_stb_font_renderer(); register_stb_font_renderer(); #endif #ifdef TB_FONT_RENDERER_FREETYPE void register_freetype_font_renderer(); register_freetype_font_renderer(); #endif // Add fonts we can use to the font manager. #if defined(TB_FONT_RENDERER_STB) || defined(TB_FONT_RENDERER_FREETYPE) g_font_manager->AddFontInfo("resources/vera.ttf", "Vera"); #endif #ifdef TB_FONT_RENDERER_TBBF g_font_manager->AddFontInfo("resources/default_font/segoe_white_with_shadow.tb.txt", "Segoe"); g_font_manager->AddFontInfo("Demo/fonts/neon.tb.txt", "Neon"); g_font_manager->AddFontInfo("Demo/fonts/orangutang.tb.txt", "Orangutang"); g_font_manager->AddFontInfo("Demo/fonts/orange.tb.txt", "Orange"); #endif // Set the default font description for widgets to one of the fonts we just added TBFontDescription fd; #ifdef TB_FONT_RENDERER_TBBF fd.SetID(TBIDC("Segoe")); #else fd.SetID(TBIDC("Vera")); #endif fd.SetSize(g_tb_skin->GetDimensionConverter()->DpToPx(14)); g_font_manager->SetDefaultFontDescription(fd); // Create the font now. TBFontFace *font = g_font_manager->CreateFontFace(g_font_manager->GetDefaultFontDescription()); // Render some glyphs in one go now since we know we are going to use them. It would work fine // without this since glyphs are rendered when needed, but with some extra updating of the glyph bitmap. if (font) font->RenderGlyphs(" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~•·åäöÅÄÖ"); // Give the root widget a background skin application->GetRoot()->SetSkinBg(TBIDC("background")); application->Init(); application->Run(); application->ShutDown(); delete application; return 0; }
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); }
bool TBWidgetsReader::CreateWidget(TBWidget *target, TBNode *node, WIDGET_Z add_child_z) { CREATE_INFO info = { this, target->GetContentRoot(), node }; // Find a widget creator from the node name TBWidgetFactory *wc = nullptr; for (wc = factories.GetFirst(); wc; wc = wc->GetNext()) if (strcmp(node->GetName(), wc->name) == 0) break; // Create the widget TBWidget *new_widget = wc ? wc->Create(&info) : nullptr; if (!new_widget) return false; // Read generic properties SetIDFromNode(new_widget->GetID(), node->GetNode("id")); SetIDFromNode(new_widget->GetGroupID(), node->GetNode("group-id")); if (wc->sync_type == TBValue::TYPE_FLOAT) new_widget->SetValueDouble(node->GetValueFloat("value", 0)); else new_widget->SetValue(node->GetValueInt("value", 0)); if (TBNode *data_node = node->GetNode("data")) new_widget->data.Copy(data_node->GetValue()); new_widget->SetIsGroupRoot(node->GetValueInt("is-group-root", new_widget->GetIsGroupRoot()) ? true : false); new_widget->SetIsFocusable(node->GetValueInt("is-focusable", new_widget->GetIsFocusable()) ? true : false); new_widget->SetWantLongClick(node->GetValueInt("want-long-click", new_widget->GetWantLongClick()) ? true : false); new_widget->SetIgnoreInput(node->GetValueInt("ignore-input", new_widget->GetIgnoreInput()) ? true : false); new_widget->SetOpacity(node->GetValueFloat("opacity", new_widget->GetOpacity())); if (const char *text = node->GetValueString("text", nullptr)) new_widget->SetText(text); if (const char *connection = node->GetValueStringRaw("connection", nullptr)) { // If we already have a widget value with this name, just connect to it and the widget will // adjust its value to it. Otherwise create a new widget value, and give it the value we // got from the resource. if (TBWidgetValue *value = g_value_group.GetValue(connection)) new_widget->Connect(value); else if (TBWidgetValue *value = g_value_group.CreateValueIfNeeded(connection, wc->sync_type)) { value->SetFromWidget(new_widget); new_widget->Connect(value); } } if (const char *gravity = node->GetValueString("gravity", nullptr)) { WIDGET_GRAVITY g = WIDGET_GRAVITY_NONE; if (strstr(gravity, "left")) g |= WIDGET_GRAVITY_LEFT; if (strstr(gravity, "top")) g |= WIDGET_GRAVITY_TOP; if (strstr(gravity, "right")) g |= WIDGET_GRAVITY_RIGHT; if (strstr(gravity, "bottom")) g |= WIDGET_GRAVITY_BOTTOM; if (strstr(gravity, "all")) g |= WIDGET_GRAVITY_ALL; if (!(g & WIDGET_GRAVITY_LEFT_RIGHT)) g |= WIDGET_GRAVITY_LEFT; if (!(g & WIDGET_GRAVITY_TOP_BOTTOM)) g |= WIDGET_GRAVITY_TOP; new_widget->SetGravity(g); } if (const char *state = node->GetValueString("state", nullptr)) { if (strstr(state, "disabled")) new_widget->SetState(WIDGET_STATE_DISABLED, true); } if (const char *skin = node->GetValueString("skin", nullptr)) { new_widget->SetSkinBg(skin); } if (TBNode *lp = node->GetNode("lp")) { LayoutParams layout_params; if (new_widget->GetLayoutParams()) layout_params = *new_widget->GetLayoutParams(); const TBDimensionConverter *dc = g_tb_skin->GetDimensionConverter(); if (const char *str = lp->GetValueString("width", nullptr)) layout_params.SetWidth(dc->GetPxFromString(str, LayoutParams::UNSPECIFIED)); if (const char *str = lp->GetValueString("height", nullptr)) layout_params.SetHeight(dc->GetPxFromString(str, LayoutParams::UNSPECIFIED)); if (const char *str = lp->GetValueString("min-width", nullptr)) layout_params.min_w = dc->GetPxFromString(str, LayoutParams::UNSPECIFIED); if (const char *str = lp->GetValueString("max-width", nullptr)) layout_params.max_w = dc->GetPxFromString(str, LayoutParams::UNSPECIFIED); if (const char *str = lp->GetValueString("pref-width", nullptr)) layout_params.pref_w = dc->GetPxFromString(str, LayoutParams::UNSPECIFIED); if (const char *str = lp->GetValueString("min-height", nullptr)) layout_params.min_h = dc->GetPxFromString(str, LayoutParams::UNSPECIFIED); if (const char *str = lp->GetValueString("max-height", nullptr)) layout_params.max_h = dc->GetPxFromString(str, LayoutParams::UNSPECIFIED); if (const char *str = lp->GetValueString("pref-height", nullptr)) layout_params.pref_h = dc->GetPxFromString(str, LayoutParams::UNSPECIFIED); new_widget->SetLayoutParams(layout_params); } target->GetContentRoot()->OnInflateChild(new_widget); // Add the new widget to the hiearchy target->GetContentRoot()->AddChild(new_widget, add_child_z); // Read the font now when the widget is in the hiearchy so inheritance works. if (TBNode *font = node->GetNode("font")) { TBFontDescription fd = new_widget->GetCalculatedFontDescription(); if (const char *size = font->GetValueString("size", nullptr)) { int new_size = g_tb_skin->GetDimensionConverter()->GetPxFromString(size, fd.GetSize()); fd.SetSize(new_size); } if (const char *name = font->GetValueString("name", nullptr)) fd.SetID(name); new_widget->SetFontDescription(fd); } // Iterate through all nodes and create widgets for (TBNode *n = node->GetFirstChild(); n; n = n->GetNext()) CreateWidget(new_widget, n, wc->add_child_z); if (node->GetValueInt("autofocus", 0)) new_widget->SetFocus(WIDGET_FOCUS_REASON_UNKNOWN); return true; }
ListViewItemWidget::ListViewItemWidget(ListViewItem *item, ListViewItemSource *source, TBSelectItemViewer *sourceviewer, int index) : source_(source) , sourceviewer_(sourceviewer) , index_(index) , item_(item) , expandBox_(0) , textField_(0) , icon_(0) { SetLayoutDistribution(LAYOUT_DISTRIBUTION_GRAVITY); SetLayoutDistributionPosition(LAYOUT_DISTRIBUTION_POSITION_LEFT_TOP); SetPaintOverflowFadeout(false); item_->widget_ = this; for (int i = 0; i < item->depth_; i++) { LayoutParams lp; lp.SetWidth(6); lp.SetHeight(4); TBWidget* spacer = new TBWidget(); spacer->SetLayoutParams(lp); GetContentRoot()->AddChild(spacer); } if (item_->children_.Size()) { expandBox_ = new TBCheckBox(); expandBox_->SetSkinBg(TBIDC("TBCheckBox.uilistview")); expandBox_->SetValue(item_->GetExpanded()); expandBox_->SetID(item->id); GetContentRoot()->AddChild(expandBox_); } else { LayoutParams lp; lp.SetWidth(12); lp.SetHeight(4); TBWidget* spacer = new TBWidget(); spacer->SetLayoutParams(lp); GetContentRoot()->AddChild(spacer); } if (item->icon_.Length()) { icon_ = new TBSkinImage(TBIDC(item->icon_.CString())); icon_->SetIgnoreInput(true); GetContentRoot()->AddChild(icon_); } TBFontDescription fd; fd.SetID(TBIDC("Vera")); fd.SetSize(11); TBTextField* tfield = textField_ = new TBTextField(); tfield->SetIgnoreInput(true); tfield->SetSkinBg(item->textSkin_.Length() ? TBIDC(item->textSkin_.CString()) : TBIDC("Folder")); tfield->SetText(item->str); tfield->SetFontDescription(fd); SetSkinBg(TBIDC("TBSelectItem")); GetContentRoot()->AddChild(tfield); SetID(item->id); }