From 01c27ecb2f7c4773ae1370be352708c4a4a662f2 Mon Sep 17 00:00:00 2001 From: cxl Date: Sun, 28 Jan 2018 10:35:54 +0000 Subject: [PATCH] .reference git-svn-id: svn://ultimatepp.org/upp/trunk@11740 f0d560ea-af0d-0410-9eb7-867de7ffcac7 --- .../ArrayCtrlScrollSync.upp | 11 +++ reference/ArrayCtrlScrollSync/main.cpp | 70 +++++++++++++++++++ reference/CoWorkLoop/CoWorkLoop.cpp | 18 ++--- 3 files changed, 87 insertions(+), 12 deletions(-) create mode 100644 reference/ArrayCtrlScrollSync/ArrayCtrlScrollSync.upp create mode 100644 reference/ArrayCtrlScrollSync/main.cpp diff --git a/reference/ArrayCtrlScrollSync/ArrayCtrlScrollSync.upp b/reference/ArrayCtrlScrollSync/ArrayCtrlScrollSync.upp new file mode 100644 index 000000000..772e581f0 --- /dev/null +++ b/reference/ArrayCtrlScrollSync/ArrayCtrlScrollSync.upp @@ -0,0 +1,11 @@ +description "Demosntrates how to synchronize ArrayCtrl scroll position with LineEdit\377"; + +uses + CtrlLib; + +file + main.cpp; + +mainconfig + "" = "GUI"; + diff --git a/reference/ArrayCtrlScrollSync/main.cpp b/reference/ArrayCtrlScrollSync/main.cpp new file mode 100644 index 000000000..423957480 --- /dev/null +++ b/reference/ArrayCtrlScrollSync/main.cpp @@ -0,0 +1,70 @@ +#include + +using namespace Upp; + +struct MyApp : TopWindow { + LineEdit editor; + ArrayCtrl list; + Splitter splitter; + int dosel = 0; + + MyApp(); +}; + +MyApp::MyApp() +{ + Add(splitter.SizePos()); + splitter.Horz(list, editor); + list.AddRowNumColumn().ConvertBy([=](const Value& v)->Value { + int ii = v; + if(ii >= editor.GetLineCount()) + return Null; + return AsString(ii) << " " << FormatIntRoman(ii, true); + }); + list.NoHeader().NoGrid().HideSb(); + list.NoWantFocus(); + + String text; + for(int i = 0; i < 1000; i++) + text << "Line " << AsString(i) << "\n"; + editor <<= text; + + int cy = editor.GetFontSize().cy; + list.SetLineCy(cy); + + editor.WhenAction = editor.WhenScroll = [=] { + list.SetVirtualCount(editor.GetLineCount() + 2); // unfortunately, we need 2 more lines to cover LineEdit end of text + list.ScrollTo(editor.GetScrollPos().y * cy); + }; + editor.WhenSel = [=] { + if(dosel) // avoid infinite recursion + return; + dosel++; + list.SetCursor(editor.GetCursorLine()); + list.WhenAction(); + dosel--; + }; + + list.WhenScroll = [=] { + editor.SetScrollPos(Point(0, list.GetScroll() / cy)); + }; + list.WhenSel = [=] { + if(dosel) // avoid infinite recursion + return; + dosel++; + int i = list.GetCursor(); + if(i >= 0 && i <= editor.GetLength()) { + editor.SetCursor(editor.GetPos(i)); + editor.WhenAction(); + } + dosel--; + }; + + editor.WhenAction(); // set initial scroll position in the list + editor.WhenSel(); // set initial cursor position in the list +} + +GUI_APP_MAIN +{ + MyApp().Run(); +} diff --git a/reference/CoWorkLoop/CoWorkLoop.cpp b/reference/CoWorkLoop/CoWorkLoop.cpp index e6e4e33e6..b7eba5fab 100644 --- a/reference/CoWorkLoop/CoWorkLoop.cpp +++ b/reference/CoWorkLoop/CoWorkLoop.cpp @@ -2,20 +2,11 @@ using namespace Upp; -force_inline double Sum(const String& s) -{ - CParser p(s); - double sum = 0; - while(!p.IsEof()) - sum += p.ReadDouble(); - return sum; -} - CONSOLE_APP_MAIN { SeedRandom(0); Vector data; - for(int i = 0; i < 10000000; i++) { + for(int i = 0; i < 1000; i++) { int n = Random(7); data.Add(); for(int j = 0; j < n; j++) @@ -27,8 +18,11 @@ CONSOLE_APP_MAIN co * [&] { double m = 0; int i; - while((i = co.Next()) < data.GetCount()) - m += Sum(data[i]); + while((i = co.Next()) < data.GetCount()) { + CParser p(data[i]); + while(!p.IsEof()) + m += p.ReadDouble(); + } CoWork::FinLock(); sum += m; };