From 0e23ea9f6b11273c8a4a3dd6937859afeb80480b Mon Sep 17 00:00:00 2001 From: cxl Date: Sat, 15 Oct 2011 17:17:32 +0000 Subject: [PATCH] reference: low-level Drag & Drop examples git-svn-id: svn://ultimatepp.org/upp/trunk@4025 f0d560ea-af0d-0410-9eb7-867de7ffcac7 --- reference/DragAndDrop1/DragAndDrop1.upp | 11 +++ reference/DragAndDrop1/init | 4 + reference/DragAndDrop1/main.cpp | 91 +++++++++++++++++++++++ reference/DragAndDrop2/DragAndDrop2.upp | 11 +++ reference/DragAndDrop2/init | 4 + reference/DragAndDrop2/main.cpp | 97 +++++++++++++++++++++++++ 6 files changed, 218 insertions(+) create mode 100644 reference/DragAndDrop1/DragAndDrop1.upp create mode 100644 reference/DragAndDrop1/init create mode 100644 reference/DragAndDrop1/main.cpp create mode 100644 reference/DragAndDrop2/DragAndDrop2.upp create mode 100644 reference/DragAndDrop2/init create mode 100644 reference/DragAndDrop2/main.cpp diff --git a/reference/DragAndDrop1/DragAndDrop1.upp b/reference/DragAndDrop1/DragAndDrop1.upp new file mode 100644 index 000000000..5bb0e05b4 --- /dev/null +++ b/reference/DragAndDrop1/DragAndDrop1.upp @@ -0,0 +1,11 @@ +description "Low-level drag and drop operation\377"; + +uses + CtrlLib; + +file + main.cpp; + +mainconfig + "" = "GUI SSE2"; + diff --git a/reference/DragAndDrop1/init b/reference/DragAndDrop1/init new file mode 100644 index 000000000..14bef67fa --- /dev/null +++ b/reference/DragAndDrop1/init @@ -0,0 +1,4 @@ +#ifndef _DragAndDrop1_icpp_init_stub +#define _DragAndDrop1_icpp_init_stub +#include "CtrlLib/init" +#endif diff --git a/reference/DragAndDrop1/main.cpp b/reference/DragAndDrop1/main.cpp new file mode 100644 index 000000000..58e0590eb --- /dev/null +++ b/reference/DragAndDrop1/main.cpp @@ -0,0 +1,91 @@ +#include + +using namespace Upp; + +struct MyAppData { + String text; + Color color; +}; + +struct MyApp : TopWindow { + Point pos; + MyAppData data; + Point dragpos; + + virtual void CancelMode(); + virtual void Paint(Draw& w); + virtual void DragAndDrop(Point p, PasteClip& clip); + virtual void DragLeave(); + virtual void LeftDrag(Point p, dword keyflags); + + MyApp(); +}; + +void MyApp::CancelMode() +{ + dragpos = Null; + Refresh(); +} + +void MyApp::DragAndDrop(Point p, PasteClip& clip) +{ + if(clip.Accept("MyAppData")) { + String bin = clip; + if(bin.GetLength() > sizeof(Color)) { // prudent check + pos = p; + memcpy(&data.color, ~bin, sizeof(Color)); + data.text = bin.Mid(sizeof(Color)); + } + Refresh(); + } + if(AcceptText(clip)) { + pos = p; + data.text = GetString(clip); + } + dragpos = clip.IsAccepted() ? p : Null; + Refresh(); +} + +void MyApp::DragLeave() +{ + dragpos = Null; + Refresh(); +} + +void MyApp::LeftDrag(Point p, dword keyflags) +{ + String bin; + bin.Cat((byte *)&data.color, sizeof(data.color)); + bin.Cat(data.text); + VectorMap d; + d.Add("MyAppData", bin); + Append(d, data.text); + Size sz(128, 64); + ImageDraw iw(sz); + iw.DrawRect(sz, Black()); + iw.Alpha().DrawRect(sz, Black()); + iw.Alpha().DrawText(0, 0, data.text, Courier(14), White()); + DoDragAndDrop(d, iw); +} + +void MyApp::Paint(Draw& w) +{ + w.DrawRect(GetSize(), SColorPaper()); + w.DrawText(pos.x, pos.y, data.text, StdFont(), data.color); + if(!IsNull(dragpos)) + w.DrawRect(RectC(dragpos.x - 1, dragpos.y - 1, 3, 3), LtBlue); +} + +MyApp::MyApp() +{ + data.text = FormatIntRoman(Random(2000)); + data.color = Color(Random() & 127, Random() & 127, Random() & 127); + pos = Point(10, 10); + SetRect(0, 0, 500, 100); + dragpos = Null; +} + +GUI_APP_MAIN +{ + MyApp().Run(); +} diff --git a/reference/DragAndDrop2/DragAndDrop2.upp b/reference/DragAndDrop2/DragAndDrop2.upp new file mode 100644 index 000000000..186b9f3a3 --- /dev/null +++ b/reference/DragAndDrop2/DragAndDrop2.upp @@ -0,0 +1,11 @@ +description "Low-level drag and drop operation, late rendering using GetSelectionData\377"; + +uses + CtrlLib; + +file + main.cpp; + +mainconfig + "" = "GUI SSE2"; + diff --git a/reference/DragAndDrop2/init b/reference/DragAndDrop2/init new file mode 100644 index 000000000..80f0c3254 --- /dev/null +++ b/reference/DragAndDrop2/init @@ -0,0 +1,4 @@ +#ifndef _DragAndDrop2_icpp_init_stub +#define _DragAndDrop2_icpp_init_stub +#include "CtrlLib/init" +#endif diff --git a/reference/DragAndDrop2/main.cpp b/reference/DragAndDrop2/main.cpp new file mode 100644 index 000000000..ddfb0c021 --- /dev/null +++ b/reference/DragAndDrop2/main.cpp @@ -0,0 +1,97 @@ +#include + +using namespace Upp; + +struct MyAppData { + String text; + Color color; +}; + +struct MyApp : TopWindow { + Point pos; + MyAppData data; + Point dragpos; + + virtual void CancelMode(); + virtual void Paint(Draw& w); + virtual void DragAndDrop(Point p, PasteClip& clip); + virtual void DragLeave(); + virtual void LeftDrag(Point p, dword keyflags); + virtual String GetSelectionData(const String& fmt) const; + + MyApp(); +}; + +void MyApp::CancelMode() +{ + dragpos = Null; + Refresh(); +} + +void MyApp::DragAndDrop(Point p, PasteClip& clip) +{ + if(clip.Accept("MyAppData")) { + String bin = clip; + if(bin.GetLength() > sizeof(Color)) { // prudent check + pos = p; + memcpy(&data.color, ~bin, sizeof(Color)); + data.text = bin.Mid(sizeof(Color)); + } + Refresh(); + } + if(AcceptText(clip)) { + pos = p; + data.text = GetString(clip); + } + dragpos = clip.IsAccepted() ? p : Null; + Refresh(); +} + +void MyApp::DragLeave() +{ + dragpos = Null; + Refresh(); +} + +String MyApp::GetSelectionData(const String& fmt) const +{ + if(fmt == "MyAppData") { + String bin; + bin.Cat((byte *)&data.color, sizeof(data.color)); + bin.Cat(data.text); + return bin; + } + return GetTextClip(data.text, fmt); +} + +void MyApp::LeftDrag(Point p, dword keyflags) +{ + Size sz(128, 64); + ImageDraw iw(sz); + iw.DrawRect(sz, Black()); + iw.Alpha().DrawRect(sz, Black()); + iw.Alpha().DrawText(0, 0, data.text, Courier(14), White()); + DoDragAndDrop(String("MyAppData;") + ClipFmtsText(), iw); +} + +void MyApp::Paint(Draw& w) +{ + w.DrawRect(GetSize(), SColorPaper()); + w.DrawText(pos.x, pos.y, data.text, StdFont(), data.color); + if(!IsNull(dragpos)) + w.DrawRect(RectC(dragpos.x - 1, dragpos.y - 1, 3, 3), LtBlue); +} + +MyApp::MyApp() +{ + data.text = FormatIntRoman(Random(2000)); + data.color = Color(Random() & 127, Random() & 127, Random() & 127); + pos = Point(10, 10); + SetRect(0, 0, 500, 100); + dragpos = Null; +} + +GUI_APP_MAIN +{ + MyApp().Run(); +}