Fix Clipboard about text data, but Bitmap is not yet.

This commit is contained in:
haru-s 2023-03-01 11:59:01 +09:00
parent fa984dcd11
commit e839be61a3
3 changed files with 67 additions and 156 deletions

View file

@ -58,15 +58,10 @@ class MainForm : Form
_copy.click ~= (Control c, EventArgs e) _copy.click ~= (Control c, EventArgs e)
{ {
_picturebox.image = null; _picturebox.image = null;
static if (1) // BUG: workaround static if (1)
{ Clipboard.setString(_textbox.text);
Clipboard.setString(_textbox.text, false); else // same as
}
else
{
// TODO: Don't work
Clipboard.setData(DataFormats.stringFormat, new Data(_textbox.text)); Clipboard.setData(DataFormats.stringFormat, new Data(_textbox.text));
}
}; };
_copyBitmap = new Button(); _copyBitmap = new Button();
@ -87,7 +82,7 @@ class MainForm : Form
} }
else else
{ {
Clipboard.setImage(bitmap, false); // TODO: Don't work Clipboard.setImage(bitmap); // TODO: Don't work
} }
_picturebox.image = bitmap; _picturebox.image = bitmap;
}; };

View file

@ -25,110 +25,46 @@ static:
/// Returns a data object that represents the entire contents of the Clipboard. /// Returns a data object that represents the entire contents of the Clipboard.
dfl.data.IDataObject getDataObject() dfl.data.IDataObject getDataObject()
{ {
dfl.internal.wincom.IDataObject comdobj; dfl.internal.wincom.IDataObject comDataObject;
if(S_OK != OleGetClipboard(&comdobj)) if(S_OK != OleGetClipboard(&comDataObject))
throw new DflException("Unable to obtain clipboard data object"); throw new DflException("Unable to obtain clipboard data object");
if(comdobj is _comd) if(comDataObject is _comDataObject)
return _dd; return _dflDataObject;
_comd = comdobj; _comDataObject = comDataObject;
return _dd = new ComToDdataObject(comdobj); return _dflDataObject = new ComToDdataObject(comDataObject);
} }
/// Places a specified data object on the system Clipboard and accepts a Boolean parameter /// Places a specified data object on the system Clipboard and accepts a Boolean parameter
/// that indicates whether the data object should be left on the Clipboard /// that indicates whether the data object should be left on the Clipboard
/// when the application exits. /// when the application exits.
void setDataObject(Data obj, bool persist = false) void setDataObject(dfl.data.IDataObject dataObj, bool persist = false)
{ {
// First, clears data on clipboard. // First, clears data on clipboard.
if(S_OK != OleSetClipboard(null)) if(S_OK != OleSetClipboard(null))
goto err_set; goto err_set;
_comd = null; _dflDataObject = dataObj;
_dd = null; _comDataObject = new DtoComDataObject(_dflDataObject);
_objref = null;
if(S_OK != OleSetClipboard(_comDataObject))
goto err_set;
if(obj.info) if(persist)
{ OleFlushClipboard();
if(cast(TypeInfo_Class)obj.info)
{
Object foo;
foo = obj.getObject();
if(obj.info == typeid(Bitmap))
{
DataObject bar = new DataObject;
_dd = bar;
_objref = bar;
_dd.setData(DataFormats.bitmap, obj);
}
else if(cast(dfl.data.IDataObject)foo)
{
_dd = cast(dfl.data.IDataObject)foo;
_objref = foo;
}
else
{
// Can't set any old class object.
throw new DflException("Unknown data object");
}
}
else if(obj.info == typeid(dfl.data.IDataObject))
{
_dd = obj.getIDataObject();
_objref = cast(Object)_dd;
}
else if(cast(TypeInfo_Interface)obj.info)
{
// Can't set any old interface.
throw new DflException("Unknown data object");
}
else
{
DataObject foo = new DataObject;
_dd = foo;
_objref = foo;
_dd.setData(obj); // Same as _dd.setData(DataFormats.getFormat(obj.info).name, obj);
}
assert(_dd !is null);
_comd = new DtoComDataObject(_dd);
if(S_OK != OleSetClipboard(_comd))
{
_comd = null;
//delete dd;
_dd = null;
goto err_set;
}
if(persist)
OleFlushClipboard();
}
else
{
_dd = null;
if(S_OK != OleSetClipboard(null))
goto err_set;
}
return; return;
err_set: err_set:
throw new DflException("Unable to set clipboard data"); throw new DflException("Unable to set clipboard data");
} }
/// ditto
void setDataObject(dfl.data.IDataObject obj, bool persist = false)
{
setDataObject(new Data(obj), persist);
}
/// Retrieves data in a specified format from the Clipboard. /// Retrieves data in a specified format from the Clipboard.
Data getData(Dstring fmt) Data getData(Dstring fmt)
{ {
dfl.data.IDataObject ido = getDataObject(); dfl.data.IDataObject dataObj = getDataObject();
Dstring normalizedFormatName = DataFormats.getFormat(fmt).name; Dstring normalizedFormatName = DataFormats.getFormat(fmt).name;
if (ido.getDataPresent(normalizedFormatName)) if (dataObj.getDataPresent(normalizedFormatName))
return ido.getData(normalizedFormatName); return dataObj.getData(normalizedFormatName);
return null; return null;
} }
@ -138,135 +74,132 @@ static:
dfl.data.IDataObject dataObj = new DataObject; dfl.data.IDataObject dataObj = new DataObject;
Dstring normalizedFormatName = DataFormats.getFormat(fmt).name; Dstring normalizedFormatName = DataFormats.getFormat(fmt).name;
dataObj.setData(normalizedFormatName, obj); dataObj.setData(normalizedFormatName, obj);
setDataObject(dataObj, true);
// TODO: Why do not work to call setDataObject()?
if(S_OK != OleSetClipboard(new DtoComDataObject(dataObj)))
throw new DflException("OleSetClipboard failure");
} }
/// Queries the Clipboard for the presence of data in a specified data format. /// Queries the Clipboard for the presence of data in a specified data format.
bool containsData(Dstring fmt) bool containsData(Dstring fmt)
{ {
dfl.data.IDataObject ido = getDataObject(); dfl.data.IDataObject dataObj = getDataObject();
Dstring normalizedFormatName = DataFormats.getFormat(fmt).name; Dstring normalizedFormatName = DataFormats.getFormat(fmt).name;
return ido.getDataPresent(normalizedFormatName); return dataObj.getDataPresent(normalizedFormatName);
} }
/// Stores UTF-8 text data on the Clipboard. /// Stores UTF-8 text data on the Clipboard.
void setString(Dstring str, bool persist = false) void setString(Dstring str)
{ {
setDataObject(new Data(str), persist); setData(DataFormats.stringFormat, new Data(str));
} }
/// Returns a string containing the UTF-8 text data on the Clipboard. /// Returns a string containing the UTF-8 text data on the Clipboard.
Dstring getString() Dstring getString()
{ {
dfl.data.IDataObject ido = getDataObject(); dfl.data.IDataObject dataObj = getDataObject();
if(ido.getDataPresent(DataFormats.utf8)) if(dataObj.getDataPresent(DataFormats.utf8))
return ido.getData(DataFormats.utf8).getString(); return dataObj.getData(DataFormats.utf8).getString();
return null; return null;
} }
/// Queries the Clipboard for the presence of data in the UTF-8 text format. /// Queries the Clipboard for the presence of data in the UTF-8 text format.
bool containsString() bool containsString()
{ {
dfl.data.IDataObject ido = getDataObject(); dfl.data.IDataObject dataObj = getDataObject();
return ido.getDataPresent(DataFormats.stringFormat); return dataObj.getDataPresent(DataFormats.stringFormat);
} }
/// Stores UnicodeText data on the Clipboard. /// Stores UnicodeText data on the Clipboard.
// Unicode text. // Unicode text.
void setUnicodeText(Dwstring unicodeText, bool persist = false) void setUnicodeText(Dwstring unicodeText)
{ {
setDataObject(new Data(unicodeText), persist); setData(DataFormats.unicodeText, new Data(unicodeText));
} }
/// Returns a string containing the UnicodeText data on the Clipboard. /// Returns a string containing the UnicodeText data on the Clipboard.
Dwstring getUnicodeText() Dwstring getUnicodeText()
{ {
dfl.data.IDataObject ido = getDataObject(); dfl.data.IDataObject dataObj = getDataObject();
if(ido.getDataPresent(DataFormats.unicodeText)) if(dataObj.getDataPresent(DataFormats.unicodeText))
return ido.getData(DataFormats.unicodeText).getUnicodeText(); return dataObj.getData(DataFormats.unicodeText).getUnicodeText();
return null; return null;
} }
/// Queries the Clipboard for the presence of data in the UnicodeText format. /// Queries the Clipboard for the presence of data in the UnicodeText format.
bool containsUnicodeText() bool containsUnicodeText()
{ {
dfl.data.IDataObject ido = getDataObject(); dfl.data.IDataObject dataObj = getDataObject();
return ido.getDataPresent(DataFormats.unicodeText); return dataObj.getDataPresent(DataFormats.unicodeText);
} }
/// Stores (Ansi)Text data on the Clipboard. /// Stores (Ansi)Text data on the Clipboard.
// ANSI text. // ANSI text.
void setText(ubyte[] ansiText, bool persist = false) void setText(ubyte[] ansiText)
{ {
setDataObject(new Data(ansiText), persist); setData(DataFormats.text, new Data(ansiText));
} }
/// Returns a string containing the (Ansi)Text data on the Clipboard. /// Returns a string containing the (Ansi)Text data on the Clipboard.
ubyte[] getText() ubyte[] getText()
{ {
dfl.data.IDataObject ido = getDataObject(); dfl.data.IDataObject dataObj = getDataObject();
if(ido.getDataPresent(DataFormats.text)) if(dataObj.getDataPresent(DataFormats.text))
return ido.getData(DataFormats.text).getText(); return dataObj.getData(DataFormats.text).getText();
return null; return null;
} }
/// Queries the Clipboard for the presence of data in the (Ansi)Text format. /// Queries the Clipboard for the presence of data in the (Ansi)Text format.
bool containsText() bool containsText()
{ {
dfl.data.IDataObject ido = getDataObject(); dfl.data.IDataObject dataObj = getDataObject();
return ido.getDataPresent(DataFormats.text); return dataObj.getDataPresent(DataFormats.text);
} }
/// Stores FileDrop data on the Clipboard. The dropped file list is specified as a string collection. /// Stores FileDrop data on the Clipboard. The dropped file list is specified as a string collection.
void setFileDropList(string[] fileDropList, bool persist = false) void setFileDropList(string[] fileDropList)
{ {
setDataObject(new Data(fileDropList), persist); setData(DataFormats.fileDrop, new Data(fileDropList));
} }
/// Returns a string collection that contains a list of dropped files available on the Clipboard. /// Returns a string collection that contains a list of dropped files available on the Clipboard.
string[] getFileDropList() string[] getFileDropList()
{ {
dfl.data.IDataObject ido = getDataObject(); dfl.data.IDataObject dataObj = getDataObject();
if(ido.getDataPresent(DataFormats.fileDrop)) if(dataObj.getDataPresent(DataFormats.fileDrop))
return ido.getData(DataFormats.fileDrop).getStrings(); return dataObj.getData(DataFormats.fileDrop).getStrings();
return null; return null;
} }
/// Queries the Clipboard for the presence of data in the FileDrop data format. /// Queries the Clipboard for the presence of data in the FileDrop data format.
bool containsFileDropList() bool containsFileDropList()
{ {
dfl.data.IDataObject ido = getDataObject(); dfl.data.IDataObject dataObj = getDataObject();
return ido.getDataPresent(DataFormats.fileDrop); return dataObj.getDataPresent(DataFormats.fileDrop);
} }
/// Stores Bitmap data on the Clipboard. /// Stores Bitmap data on the Clipboard.
void setImage(Image image, bool persist = false) void setImage(Image image)
{ {
setDataObject(new Data(image), persist); setData(DataFormats.bitmap, new Data(image));
} }
/// Returns a Image object from the Clipboard that contains data in the Bitmap format. /// Returns a Image object from the Clipboard that contains data in the Bitmap format.
Image getImage() Image getImage()
{ {
dfl.data.IDataObject ido = getDataObject(); dfl.data.IDataObject dataObj = getDataObject();
if(ido.getDataPresent(DataFormats.bitmap)) if(dataObj.getDataPresent(DataFormats.bitmap))
return ido.getData(DataFormats.bitmap).getImage(); return dataObj.getData(DataFormats.bitmap).getImage();
return null; return null;
} }
/// Queries the Clipboard for the presence of data in the Bitmap data format. /// Queries the Clipboard for the presence of data in the Bitmap data format.
bool containsImage() bool containsImage()
{ {
dfl.data.IDataObject ido = getDataObject(); dfl.data.IDataObject dataObj = getDataObject();
return ido.getDataPresent(DataFormats.bitmap); return dataObj.getDataPresent(DataFormats.bitmap);
} }
@ -276,9 +209,8 @@ static:
if (S_OK != OleSetClipboard(null)) if (S_OK != OleSetClipboard(null))
throw new DflException("Unable to clear clipboard data"); throw new DflException("Unable to clear clipboard data");
_comd = null; _comDataObject = null;
_dd = null; _dflDataObject = null;
_objref = null;
} }
@ -292,7 +224,6 @@ static:
private: private:
dfl.internal.wincom.IDataObject _comd; dfl.internal.wincom.IDataObject _comDataObject;
dfl.data.IDataObject _dd; dfl.data.IDataObject _dflDataObject;
Object _objref; // Prevent dd from being garbage collected!
} }

View file

@ -39,7 +39,7 @@ class DataFormats // docmain
} }
package: package:
int _id; int _id;
Dstring _name; Dstring _name;
@ -731,7 +731,7 @@ class Data // docmain
} }
private: private:
TypeInfo _info; TypeInfo _info;
InnerValues _innerValues; InnerValues _innerValues;
@ -740,7 +740,7 @@ class Data // docmain
Data dataValue; Data dataValue;
Object objectValue; Object objectValue;
Dstring dstringValue; Dstring dstringValue;
Dstring[] dstringsValue; // For drop files. Dstring[] dstringsValue; // For FileDrop
Ddstring ddstringValue; Ddstring ddstringValue;
Dwstring dwstringValue; Dwstring dwstringValue;
uint uintValue; uint uintValue;
@ -781,7 +781,6 @@ interface IDataObject // docmain
/// ///
Dstring[] getFormats(); Dstring[] getFormats();
//Dstring[] getFormats(bool onlyNative);
/// ///
void setData(Data obj); void setData(Data obj);
@ -856,13 +855,6 @@ class DataObject: dfl.data.IDataObject // docmain
} }
// TODO: remove...
deprecated final Dstring[] getFormats(bool onlyNative)
{
return getFormats();
}
/// Stores pair of format and data. /// Stores pair of format and data.
/// When -replace- is true, stores new data with as a pair of preexist format. /// When -replace- is true, stores new data with as a pair of preexist format.
// Concrete implementation. // Concrete implementation.
@ -1304,13 +1296,6 @@ class ComToDdataObject: dfl.data.IDataObject // package
} }
// TO-DO: remove...
deprecated final Dstring[] getFormats(bool onlyNative)
{
return getFormats();
}
private void _setData(int id, Data obj) private void _setData(int id, Data obj)
{ {
/+ /+