/// <summary> /// The common logic of the two versions of AddString. /// </summary> private ViewBuilder AddStringClientRun(StringClientRun run, Expression <Func <string> > fetchString, Func <string> fetcher) { ParaBox para = InsertParaOrRun(run); // Try to hook an event for notification of changes to the property. var mexp = (fetchString.Body as MemberExpression); var argExp = Expression.Lambda <Func <object> >(mexp.Expression); Type type = mexp.Member.DeclaringType; string name = mexp.Member.Name; EventInfo einfo = type.GetEvent(name + "Changed"); if (einfo != null) { var target = argExp.Compile().Invoke(); var stringHookup = new StringHookup(target, fetcher, hookup => einfo.AddEventHandler(target, new EventHandler <EventArgs>(hookup.StringPropChanged)), hookup => einfo.RemoveEventHandler(target, new EventHandler <EventArgs>(hookup.StringPropChanged)), para); AddHookupToRun(run, stringHookup); var propInfo = type.GetProperty(name); if (propInfo.CanWrite) { stringHookup.Writer = newVal => propInfo.SetValue(target, newVal, null); } } return(this); }
public void InsertCharInEmptyLine() { string contents = ""; var engine = new FakeRenderEngine() { Ws = 34, SegmentHeight = 13 }; var factory = new FakeRendererFactory(); factory.SetRenderer(34, engine); var styles = new AssembledStyles().WithWs(34); var clientRuns = new List <IClientRun>(); var run = new StringClientRun(contents, styles); clientRuns.Add(run); var data1 = new MockData1(34, 35); data1.SimpleThree = contents; var source = new TextSource(clientRuns, null); var para = new ParaBox(styles, source); var hookup = new StringHookup(this, () => data1.SimpleThree, hook => data1.SimpleThreeChanged += hook.StringPropChanged, hook => data1.SimpleThreeChanged -= hook.StringPropChanged, para); hookup.Writer = newVal => data1.SimpleThree = newVal; run.Hookup = hookup; var root = new RootBox(styles); var block = new BlockBox(styles, Color.Red, 20000, 10000); root.AddBox(block); root.AddBox(para); var layoutArgs = MakeLayoutInfo(Int32.MaxValue / 2, m_gm.VwGraphics, factory); root.Layout(layoutArgs); Assert.That(root.Height, Is.EqualTo(13 + block.Height)); Assert.That(para.Width, Is.EqualTo(FakeRenderEngine.SimulatedWidth(contents))); Assert.That(root.Width, Is.EqualTo(block.Width)); int simulatedWidth = FakeRenderEngine.SimulatedWidth("x"); Assert.That(root.Width, Is.GreaterThan(para.Width + simulatedWidth)); PaintTransform ptrans = new PaintTransform(2, 4, 96, 100, 0, 10, 120, 128); MockSite site = new MockSite(); site.m_transform = ptrans; site.m_vwGraphics = m_gm.VwGraphics; root.Site = site; var oldRootWidth = root.Width; var ip = root.SelectAtEnd(); ip.InsertText("x"); Assert.That(root.Height, Is.EqualTo(13 + block.Height)); Assert.That(root.Width, Is.EqualTo(oldRootWidth)); Assert.That(para.Width, Is.EqualTo(simulatedWidth)); var expectedInvalidate = new Rectangle(-RootBox.InvalidateMargin, -RootBox.InvalidateMargin + block.Height, simulatedWidth + RootBox.InvalidateMargin * 2, 13 + 2 * RootBox.InvalidateMargin); Assert.That(site.RectsInvalidatedInRoot, Has.Member(expectedInvalidate)); }
private MockData1 HookDataToClientRun(ParaBox para, string contents1, int runIndex) { var data1 = new MockData1(ParaBuilderTests.s_simpleStringWs, ParaBuilderTests.s_simpleStringWs); data1.SimpleThree = contents1; var hookup = new StringHookup(data1, () => data1.SimpleThree, hook => data1.SimpleThreeChanged += hook.StringPropChanged, hook => data1.SimpleThreeChanged -= hook.StringPropChanged, para); hookup.Writer = newVal => data1.SimpleThree = newVal; hookup.ClientRunIndex = runIndex; (para.Source.ClientRuns[runIndex] as StringClientRun).Hookup = hookup; return(data1); }
// Add to the root a text paragraph which reflects the SimpleText property. private void AddSimpleTextPara(AssembledStyles styles, int ws, RootBox root) { var items = new List <ClientRun>(); var run = new StringClientRun("This is the day that the Lord has made. We will rejoice and be glad in it", styles.WithWs(ws)); items.Add(run); TextSource source = new TextSource(items); ParaBox para = new ParaBox(styles, source); var hookup = new StringHookup(this, () => this.SimpleText, hook => SimpleTextChanged += hook.StringPropChanged, hook => SimpleTextChanged -= hook.StringPropChanged, para); hookup.Writer = newVal => SimpleText = newVal; run.Hookup = hookup; root.AddBox(para); }
public void HookupSimpleString() { MockData1 data1 = new MockData1(m_wsFrn, m_wsEng); data1.SimpleThree = "foo"; MockParaBox mockPara = new MockParaBox(); var mlHook = new StringHookup(data1, () => data1.SimpleThree, hookup => data1.SimpleThreeChanged += hookup.StringPropChanged, hookup => data1.SimpleThreeChanged -= hookup.StringPropChanged, mockPara); mlHook.ClientRunIndex = 5; data1.SimpleThree = "bar"; data1.RaiseSimpleThreeChanged(); Assert.AreEqual(5, mockPara.TheIndex, "Should have fired the event and notified the correct index"); Assert.AreEqual("bar", mockPara.TheString, "Should have informed para of new string"); mlHook.Dispose(); }
public void InsertGrowsPara() { string contents = "This is the day."; var engine = new FakeRenderEngine() { Ws = 34, SegmentHeight = 13 }; var factory = new FakeRendererFactory(); factory.SetRenderer(34, engine); var styles = new AssembledStyles().WithWs(34); var clientRuns = new List <IClientRun>(); var run = new StringClientRun(contents, styles); clientRuns.Add(run); var data1 = new MockData1(34, 35); data1.SimpleThree = contents; var source = new TextSource(clientRuns, null); var para = new ParaBox(styles, source); var hookup = new StringHookup(this, () => data1.SimpleThree, hook => data1.SimpleThreeChanged += hook.StringPropChanged, hook => data1.SimpleThreeChanged -= hook.StringPropChanged, para); hookup.Writer = newVal => data1.SimpleThree = newVal; run.Hookup = hookup; var extraBox = new BlockBox(styles, Color.Red, 50, 72000); var root = new RootBoxFdo(styles); root.SizeChanged += root_SizeChanged; root.AddBox(para); root.AddBox(extraBox); var layoutArgs = MakeLayoutInfo(Int32.MaxValue / 2, m_gm.VwGraphics, factory); root.Layout(layoutArgs); Assert.IsTrue(m_sizeChangedCalled); Assert.That(root.Height, Is.EqualTo(13 + 96)); Assert.That(root.Width, Is.EqualTo(FakeRenderEngine.SimulatedWidth(contents))); int widthThisIsThe = FakeRenderEngine.SimulatedWidth("This is the"); layoutArgs = MakeLayoutInfo(widthThisIsThe + 2, m_gm.VwGraphics, factory); root.Layout(layoutArgs); Assert.That(root.Height, Is.EqualTo(26 + 96), "two line para is twice the height"); Assert.That(root.Width, Is.EqualTo(widthThisIsThe + 2), "two-line para occupies full available width"); Assert.That(extraBox.Top, Is.EqualTo(26)); PaintTransform ptrans = new PaintTransform(2, 4, 96, 100, 0, 10, 120, 128); MockSite site = new MockSite(); site.m_transform = ptrans; site.m_vwGraphics = m_gm.VwGraphics; root.Site = site; m_sizeChangedCalled = false; var ip = para.SelectAtEnd(); ip.InsertText(" We will be"); Assert.That(para.Height, Is.EqualTo(39), "inserted text makes para a line higher"); Assert.That(root.Height, Is.EqualTo(39 + 96), "root grows when para does"); Assert.That(root.Width, Is.EqualTo(widthThisIsThe + 2), "three-line para occupies full available width"); Assert.That(extraBox.Top, Is.EqualTo(39)); Assert.IsTrue(m_sizeChangedCalled); }