TBSelectItemSource *UISelectItemSource::GetTBItemSource() { // caller's responsibility to clean up TBGenericStringItemSource* src = new TBGenericStringItemSource(); for (List<SharedPtr<UISelectItem> >::Iterator itr = items_.Begin(); itr != items_.End(); itr++) { tb::TBGenericStringItem* tbitem = (*itr)->GetTBItem(); src->AddItem(tbitem); } return src; }
bool DemoApplication::Init() { if (!Application::Init()) return false; // Block new animations during Init. TBAnimationBlocker anim_blocker; // Run unit tests int num_failed_tests = TBRunTests(); // TBSelectList and TBSelectDropdown widgets have a default item source that are fed with any items // specified in the resource files. But it is also possible to set any source which can save memory // and improve performance. Then you don't have to populate each instance with its own set of items, // for widgets that occur many times in a UI, always with the same items. // Here we prepare the name source, that is used in a few places. for (int i = 0; boy_names[i]; i++) advanced_source.AddItem(new AdvancedItem(boy_names[i++], TBIDC("boy_item"), true)); for (int i = 0; girl_names[i]; i++) advanced_source.AddItem(new AdvancedItem(girl_names[i++], TBIDC("girl_item"), false)); for (int i = 0; girl_names[i]; i++) name_source.AddItem(new TBGenericStringItem(girl_names[i++], TBIDC("girl_item"))); for (int i = 0; boy_names[i]; i++) name_source.AddItem(new TBGenericStringItem(boy_names[i++], TBIDC("boy_item"))); advanced_source.SetSort(TB_SORT_ASCENDING); name_source.SetSort(TB_SORT_ASCENDING); // Prepare a source with submenus (with eternal recursion) so we can test sub menu support. popup_menu_source.AddItem(new TBGenericStringItem("Option 1", TBIDC("opt 1"))); popup_menu_source.AddItem(new TBGenericStringItem("Option 2", TBIDC("opt 2"))); popup_menu_source.AddItem(new TBGenericStringItem("-")); popup_menu_source.AddItem(new TBGenericStringItem("Same submenu", &popup_menu_source)); popup_menu_source.AddItem(new TBGenericStringItem("Long submenu", &name_source)); // Give the first item a skin image popup_menu_source.GetItem(0)->SetSkinImage(TBIDC("Icon16")); new MainWindow(); new EditWindow; new ListWindow(&name_source); new AdvancedListWindow(&advanced_source); new TabContainerWindow(); if (num_failed_tests) { TBStr text; text.SetFormatted("There is %d failed tests!\nCheck the output for details.", num_failed_tests); TBMessageWindow *msg_win = new TBMessageWindow(GetRoot(), TBIDC("")); msg_win->Show("Testing results", text); } return true; }
bool TBEditField::OnEvent(const TBWidgetEvent &ev) { if (ev.type == EVENT_TYPE_CHANGED && ev.target == &m_scrollbar_x) { m_style_edit.SetScrollPos(m_scrollbar_x.GetValue(), m_style_edit.scroll_y); OnScroll(m_scrollbar_x.GetValue(), m_style_edit.scroll_y); return true; } else if (ev.type == EVENT_TYPE_CHANGED && ev.target == &m_scrollbar_y) { m_style_edit.SetScrollPos(m_style_edit.scroll_x, m_scrollbar_y.GetValue()); OnScroll(m_style_edit.scroll_x, m_scrollbar_y.GetValue()); return true; } else if (ev.type == EVENT_TYPE_WHEEL && ev.modifierkeys == TB_MODIFIER_NONE) { int old_val = m_scrollbar_y.GetValue(); m_scrollbar_y.SetValue(old_val + ev.delta_y * TBSystem::GetPixelsPerLine()); return m_scrollbar_y.GetValue() != old_val; } else if (ev.type == EVENT_TYPE_POINTER_DOWN && ev.target == this) { TBRect padding_rect = GetPaddingRect(); if (m_style_edit.MouseDown( TBPoint(ev.target_x - padding_rect.x, ev.target_y - padding_rect.y), 1, ev.count, TB_MODIFIER_NONE, ev.touch)) { // Post a message to start selection scroll PostMessageDelayed(TBIDC("selscroll"), nullptr, SELECTION_SCROLL_DELAY); return true; } } else if (ev.type == EVENT_TYPE_POINTER_MOVE && ev.target == this) { TBRect padding_rect = GetPaddingRect(); return m_style_edit.MouseMove(TBPoint(ev.target_x - padding_rect.x, ev.target_y - padding_rect.y)); } else if (ev.type == EVENT_TYPE_POINTER_UP && ev.target == this) { TBRect padding_rect = GetPaddingRect(); return m_style_edit.MouseUp(TBPoint(ev.target_x - padding_rect.x, ev.target_y - padding_rect.y), 1, TB_MODIFIER_NONE, ev.touch); } else if (ev.type == EVENT_TYPE_KEY_DOWN) { return m_style_edit.KeyDown(ev.key, ev.special_key, ev.modifierkeys); } else if (ev.type == EVENT_TYPE_KEY_UP) { return true; } else if ((ev.type == EVENT_TYPE_CLICK && ev.target->GetID() == TBIDC("popupmenu")) || (ev.type == EVENT_TYPE_SHORTCUT)) { if (ev.ref_id == TBIDC("cut") && !m_style_edit.packed.read_only) m_style_edit.Cut(); else if (ev.ref_id == TBIDC("copy")) m_style_edit.Copy(); else if (ev.ref_id == TBIDC("paste") && !m_style_edit.packed.read_only) m_style_edit.Paste(); else if (ev.ref_id == TBIDC("delete") && !m_style_edit.packed.read_only) m_style_edit.Delete(); else if (ev.ref_id == TBIDC("undo") && !m_style_edit.packed.read_only) m_style_edit.Undo(); else if (ev.ref_id == TBIDC("redo") && !m_style_edit.packed.read_only) m_style_edit.Redo(); else if (ev.ref_id == TBIDC("selectall")) m_style_edit.selection.SelectAll(); else return false; return true; } else if (ev.type == EVENT_TYPE_CONTEXT_MENU && ev.target == this) { TBPoint pos_in_root(ev.target_x, ev.target_y); ev.target->ConvertToRoot(pos_in_root.x, pos_in_root.y); if (TBMenuWindow *menu = new TBMenuWindow(ev.target, TBIDC("popupmenu"))) { TBGenericStringItemSource *source = menu->GetList()->GetDefaultSource(); source->AddItem(new TBGenericStringItem(g_tb_lng->GetString(TBIDC("cut")), TBIDC("cut"))); source->AddItem(new TBGenericStringItem(g_tb_lng->GetString(TBIDC("copy")), TBIDC("copy"))); source->AddItem(new TBGenericStringItem(g_tb_lng->GetString(TBIDC("paste")), TBIDC("paste"))); source->AddItem(new TBGenericStringItem(g_tb_lng->GetString(TBIDC("delete")), TBIDC("delete"))); source->AddItem(new TBGenericStringItem("-")); source->AddItem(new TBGenericStringItem(g_tb_lng->GetString(TBIDC("selectall")), TBIDC("selectall"))); menu->Show(source, TBPopupAlignment(pos_in_root), -1); } return true; } return false; }
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); }