Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
FlavorPackage.h
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#ifndef _Stroika_Frameworks_Led_FlavorPackage_h_
5#define _Stroika_Frameworks_Led_FlavorPackage_h_ 1
6
7#include "Stroika/Frameworks/StroikaPreComp.h"
8
9/*
10@MODULE: FlavorPackage
11@DESCRIPTION:
12 <p>This module defines the packaged up contents of serialized data. This data is usually used to exchange
13 in drag and drop, and clipboard operations. But its sometimes used/saved for other purposes, like UNDO and
14 hidable text.
15 */
16
17#include <map>
18#include <vector>
19
21
22#include "Command.h"
23#include "TextImager.h"
24
25namespace Stroika::Frameworks::Led {
26
29
30 class ReaderFlavorPackage;
31 class WriterFlavorPackage;
32
33 /**
34 * <p>Helper class for implementing externalizing. Can be subclassed to add new formats.
35 * Call it with a @'WriterFlavorPackage', and a range to copy from, and the externalizing will be done.</p>
36 * <p>See also @'FlavorPackageInternalizer'.</p>
37 */
38 class FlavorPackageExternalizer : public virtual MarkerOwner {
39 private:
40 using inherited = MarkerOwner;
41
42 public:
43 FlavorPackageExternalizer (TextStore& ts);
44
45 public:
46 virtual TextStore* PeekAtTextStore () const override;
47
48 public:
49 virtual void ExternalizeFlavors (WriterFlavorPackage& flavorPackage, size_t from, size_t to);
50 virtual void ExternalizeBestFlavor (WriterFlavorPackage& flavorPackage, size_t from, size_t to);
51 virtual void ExternalizeFlavor_TEXT (WriterFlavorPackage& flavorPackage, size_t from, size_t to);
52
53 private:
54 TextStore& fTextStore;
55 };
56
57 /**
58 * <p>Helper class for implementing internalizing. Can be subclassed to add new formats.
59 * Call it with a @'ReaderFlavorPackage', and a range to insert it into, and the internalizing will be done.</p>
60 * <p>See also @'FlavorPackageExternalizer'.</p>
61 */
62 class FlavorPackageInternalizer : public virtual MarkerOwner {
63 private:
64 using inherited = MarkerOwner;
65
66 public:
67 FlavorPackageInternalizer (TextStore& ts);
68
69 public:
70 virtual TextStore* PeekAtTextStore () const override;
71
72 public:
73 virtual bool InternalizeBestFlavor (ReaderFlavorPackage& flavorPackage, size_t from, size_t to);
74 nonvirtual bool InternalizeFlavor_TEXT (ReaderFlavorPackage& flavorPackage, size_t from, size_t to);
75 virtual bool InternalizeFlavor_FILE (ReaderFlavorPackage& flavorPackage, size_t from, size_t to);
76 virtual bool InternalizeFlavor_FILEData (filesystem::path fileName, Led_ClipFormat* suggestedClipFormat,
77 optional<CodePage> suggestedCodePage, size_t from, size_t to);
78 virtual void InternalizeFlavor_FILEGuessFormatsFromName (filesystem::path fileName, Led_ClipFormat* suggestedClipFormat,
79 optional<CodePage> suggestedCodePage);
80 virtual void InternalizeFlavor_FILEGuessFormatsFromStartOfData (Led_ClipFormat* suggestedClipFormat, optional<CodePage> suggestedCodePage,
81 const byte* fileStart, const byte* fileEnd);
82 virtual bool InternalizeFlavor_FILEDataRawBytes (Led_ClipFormat* suggestedClipFormat, optional<CodePage> suggestedCodePage,
83 size_t from, size_t to, const void* rawBytes, size_t nRawBytes);
84
85 private:
86 TextStore& fTextStore;
87 };
88
89 /**
90 * Abstraction wrapping both Drag&Drop packages, and clipboard access. Used by @'FlavorPackageInternalizer'.</p>
91 */
93 public:
94 nonvirtual bool GetFlavorAvailable_TEXT () const;
95 virtual bool GetFlavorAvailable (Led_ClipFormat clipFormat) const = 0;
96
97 public:
98 /**
99 * Return an upper bound on the size of the given flavor element. Try to get the right size, but
100 * the OS frequently makes this impossible. Just garuantee that when you do a ReaderFlavorPackage::ReadFlavorData
101 * you get the right size, and that is smaller or equal to what this returns.</p>
102 */
103 virtual size_t GetFlavorSize (Led_ClipFormat clipFormat) const = 0;
104
105 public:
106 /*
107 @METHOD: ReaderFlavorPackage::ReadFlavorData
108 @DESCRIPTION: <p>Return the data of a given clip format, copied into the passed in buffer. The caller must allocate/free
109 the buffer. An upper bound on the size needed for the buffer can be retrieved with @'ReaderFlavorPackage::GetFlavorSize'.</p>
110 */
111 virtual size_t ReadFlavorData (Led_ClipFormat clipFormat, size_t bufSize, void* buf) const = 0;
112 };
113
114#if qStroika_Frameworks_Led_SupportClipboard
115 /**
116 * <p>NB: On windows - it is REQUIRED the ClipboardFlavorPackage objects only be
117 * created in the context of Open/Close clipboard operations (for example done in
118 * OnPasteCommand_Before/OnPasteCommand_After - so typically no problem).</p>
119 * <p>NB: For X-Windows, the clip data is just stored in the global variable ReaderClipboardFlavorPackage::sPrivateClipData.</p>
120 */
121 class ReaderClipboardFlavorPackage : public ReaderFlavorPackage {
122 public:
123 virtual bool GetFlavorAvailable (Led_ClipFormat clipFormat) const override;
124 virtual size_t GetFlavorSize (Led_ClipFormat clipFormat) const override;
125 virtual size_t ReadFlavorData (Led_ClipFormat clipFormat, size_t bufSize, void* buf) const override;
126#if qStroika_FeatureSupported_XWindows
127 public:
128 static map<Led_ClipFormat, vector<char>> sPrivateClipData;
129#endif
130 };
131#endif
132
133 /**
134 * <p>Abstraction wrapping both Drag&Drop packages, and clipboard access. Used by @'FlavorPackageExternalizer'.</p>
135 */
137 public:
138 virtual void AddFlavorData (Led_ClipFormat clipFormat, size_t bufSize, const void* buf) = 0;
139 };
140
141#if qStroika_Frameworks_Led_SupportClipboard
142 /**
143 * <p>NB: On windows - it is REQUIRED the ClipboardFlavorPackage objects only be
144 * created in the context of Open/Close clipboard operations (for example done in
145 * OnCopyCommand_Before/OnCopyCommand_After - so typically no problem).</p>
146 * <p>See also @'ReaderClipboardFlavorPackage'.</p>
147 */
148 class WriterClipboardFlavorPackage : public WriterFlavorPackage {
149 public:
150 virtual void AddFlavorData (Led_ClipFormat clipFormat, size_t bufSize, const void* buf) override;
151 };
152#endif
153
154 /**
155 * <p>Dual purpose, store-and-reuse package. Useful for undo.</p>
156 */
158 public:
161
162 // ReaderFlavorPackage
163 public:
164 virtual bool GetFlavorAvailable (Led_ClipFormat clipFormat) const override;
165 virtual size_t GetFlavorSize (Led_ClipFormat clipFormat) const override;
166 virtual size_t ReadFlavorData (Led_ClipFormat clipFormat, size_t bufSize, void* buf) const override;
167
168 // WriterFlavorPackage
169 public:
170 virtual void AddFlavorData (Led_ClipFormat clipFormat, size_t bufSize, const void* buf) override;
171
172 private:
173 struct PackageRecord {
174 Led_ClipFormat fFormat;
175 vector<char> fData;
176 };
177 vector<PackageRecord> fPackages;
178 };
179
180}
181
182/*
183 ********************************************************************************
184 ***************************** Implementation Details ***************************
185 ********************************************************************************
186 */
187#include "FlavorPackage.inl"
188
189#endif /*_Stroika_Frameworks_Led_FlavorPackage_h_*/
virtual size_t GetFlavorSize(Led_ClipFormat clipFormat) const override
virtual size_t GetFlavorSize(Led_ClipFormat clipFormat) const =0
conditional_t< qTargetPlatformSDKUseswchar_t, wchar_t, char > SDKChar
Definition SDKChar.h:71