// TBWidgetListener
    virtual bool OnWidgetInvokeEvent(TBWidget *widget, const TBWidgetEvent &ev)
    {
        // Skip these events for now
        if (ev.IsPointerEvent())
            return false;

        // Always ignore activity in this window (or we might get endless recursion)
        if (TBWindow *window = widget->GetParentWindow())
            if (TBSafeCast<DebugSettingsWindow>(window))
                return false;

        TBTempBuffer buf;
        buf.AppendString(GetEventTypeStr(ev.type));
        buf.AppendString(" (");
        buf.AppendString(widget->GetClassName());
        buf.AppendString(")");

        buf.AppendString(" id: ");
        buf.AppendString(GetIDString(ev.target->GetID()));

        if (ev.ref_id)
        {
            buf.AppendString(", ref_id: ");
            buf.AppendString(GetIDString(ev.ref_id));
        }

        if (ev.type == EVENT_TYPE_CHANGED)
        {
            TBStr extra, text;
            if (ev.target->GetText(text) && text.Length() > 24)
                sprintf(text.CStr() + 20, "...");
            extra.SetFormatted(", value: %.2f (\"%s\")", ev.target->GetValueDouble(), text.CStr());
            buf.AppendString(extra);
        }
        buf.AppendString("\n");

        // Append the line to the output textfield
        TBStyleEdit *se = output->GetStyleEdit();
        se->selection.SelectNothing();
        se->AppendText(buf.GetData(), TB_ALL_TO_TERMINATION, true);
        se->ScrollIfNeeded(false, true);

        // Remove lines from the top if we exceed the height limit.
        const int height_limit = 2000;
        int current_height = se->GetContentHeight();
        if (current_height > height_limit)
        {
            se->caret.Place(TBPoint(0, current_height - height_limit));
            se->selection.SelectToCaret(se->blocks.GetFirst(), 0);
            se->Delete();
        }
        return false;
    }
Beispiel #2
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 TBMessageWindow::Show(const char *title, const char *message, TBMessageWindowSettings *settings)
{
	TBWidget *target = m_target.Get();
	if (!target)
		return false;

	TBMessageWindowSettings default_settings;
	if (!settings)
		settings = &default_settings;

	TBWidget *root = target->GetParentRoot();

	const char *source =	"TBLayout: axis: y, distribution: available\n"
							"	TBLayout: distribution: available, size: available\n"
							"		TBSkinImage: id: 2\n"
							"		TBEditField: multiline: 1, readonly: 1, id: 1\n"
							"	TBLayout: distribution-position: right bottom, id: 3\n";
	if (!g_widgets_reader->LoadData(GetContentRoot(), source))
		return false;

	SetText(title);

	GetWidgetByIDAndType<TBSkinImage>(2)->SetSkinBg(settings->icon_skin);

	TBEditField *editfield = GetWidgetByIDAndType<TBEditField>(1);
	editfield->SetStyling(settings->styling);
	editfield->SetText(message);
	editfield->SetSkinBg("");

	// Create buttons
	if (settings->msg == TB_MSG_OK)
	{
		AddButton("TBMessageWindow.ok", true);
	}
	else if (settings->msg == TB_MSG_OK_CANCEL)
	{
		AddButton("TBMessageWindow.ok", true);
		AddButton("TBMessageWindow.cancel", false);
	}
	else if (settings->msg == TB_MSG_YES_NO)
	{
		AddButton("TBMessageWindow.yes", true);
		AddButton("TBMessageWindow.no", false);
	}

	// Size to fit content. This will use the default size of the textfield.
	ResizeToFitContent();
	TBRect rect = GetRect();

	// Get how much we overflow the textfield has given the current width, and grow our height to show all we can.
	// FIX: It would be better to use adapt-to-content on the editfield to achieve the most optimal size.
	// At least when we do full blown multi pass size checking.
	rect.h += editfield->GetStyleEdit()->GetOverflowY();

	// Create background dimmer
	if (settings->dimmer)
	{
		if (TBDimmer *dimmer = new TBDimmer)
		{
			root->AddChild(dimmer);
			m_dimmer.Set(dimmer);
		}
	}

	// Center and size to the new height
	TBRect bounds(0, 0, root->GetRect().w, root->GetRect().h);
	SetRect(rect.CenterIn(bounds).MoveIn(bounds).Clip(bounds));
	root->AddChild(this);
	return true;
}