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)
{
_picturebox.image = null;
static if (1) // BUG: workaround
{
Clipboard.setString(_textbox.text, false);
}
else
{
// TODO: Don't work
static if (1)
Clipboard.setString(_textbox.text);
else // same as
Clipboard.setData(DataFormats.stringFormat, new Data(_textbox.text));
}
};
_copyBitmap = new Button();
@ -87,7 +82,7 @@ class MainForm : Form
}
else
{
Clipboard.setImage(bitmap, false); // TODO: Don't work
Clipboard.setImage(bitmap); // TODO: Don't work
}
_picturebox.image = bitmap;
};

View file

@ -25,110 +25,46 @@ static:
/// Returns a data object that represents the entire contents of the Clipboard.
dfl.data.IDataObject getDataObject()
{
dfl.internal.wincom.IDataObject comdobj;
if(S_OK != OleGetClipboard(&comdobj))
dfl.internal.wincom.IDataObject comDataObject;
if(S_OK != OleGetClipboard(&comDataObject))
throw new DflException("Unable to obtain clipboard data object");
if(comdobj is _comd)
return _dd;
_comd = comdobj;
return _dd = new ComToDdataObject(comdobj);
if(comDataObject is _comDataObject)
return _dflDataObject;
_comDataObject = comDataObject;
return _dflDataObject = new ComToDdataObject(comDataObject);
}
/// 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
/// 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.
if(S_OK != OleSetClipboard(null))
goto err_set;
_comd = null;
_dd = null;
_objref = null;
_dflDataObject = dataObj;
_comDataObject = new DtoComDataObject(_dflDataObject);
if(obj.info)
{
if(cast(TypeInfo_Class)obj.info)
{
Object foo;
foo = obj.getObject();
if(S_OK != OleSetClipboard(_comDataObject))
goto err_set;
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;
}
if(persist)
OleFlushClipboard();
return;
err_set:
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.
Data getData(Dstring fmt)
{
dfl.data.IDataObject ido = getDataObject();
dfl.data.IDataObject dataObj = getDataObject();
Dstring normalizedFormatName = DataFormats.getFormat(fmt).name;
if (ido.getDataPresent(normalizedFormatName))
return ido.getData(normalizedFormatName);
if (dataObj.getDataPresent(normalizedFormatName))
return dataObj.getData(normalizedFormatName);
return null;
}
@ -138,135 +74,132 @@ static:
dfl.data.IDataObject dataObj = new DataObject;
Dstring normalizedFormatName = DataFormats.getFormat(fmt).name;
dataObj.setData(normalizedFormatName, obj);
// TODO: Why do not work to call setDataObject()?
if(S_OK != OleSetClipboard(new DtoComDataObject(dataObj)))
throw new DflException("OleSetClipboard failure");
setDataObject(dataObj, true);
}
/// Queries the Clipboard for the presence of data in a specified data format.
bool containsData(Dstring fmt)
{
dfl.data.IDataObject ido = getDataObject();
dfl.data.IDataObject dataObj = getDataObject();
Dstring normalizedFormatName = DataFormats.getFormat(fmt).name;
return ido.getDataPresent(normalizedFormatName);
return dataObj.getDataPresent(normalizedFormatName);
}
/// 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.
Dstring getString()
{
dfl.data.IDataObject ido = getDataObject();
if(ido.getDataPresent(DataFormats.utf8))
return ido.getData(DataFormats.utf8).getString();
dfl.data.IDataObject dataObj = getDataObject();
if(dataObj.getDataPresent(DataFormats.utf8))
return dataObj.getData(DataFormats.utf8).getString();
return null;
}
/// Queries the Clipboard for the presence of data in the UTF-8 text format.
bool containsString()
{
dfl.data.IDataObject ido = getDataObject();
return ido.getDataPresent(DataFormats.stringFormat);
dfl.data.IDataObject dataObj = getDataObject();
return dataObj.getDataPresent(DataFormats.stringFormat);
}
/// Stores UnicodeText data on the Clipboard.
// 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.
Dwstring getUnicodeText()
{
dfl.data.IDataObject ido = getDataObject();
if(ido.getDataPresent(DataFormats.unicodeText))
return ido.getData(DataFormats.unicodeText).getUnicodeText();
dfl.data.IDataObject dataObj = getDataObject();
if(dataObj.getDataPresent(DataFormats.unicodeText))
return dataObj.getData(DataFormats.unicodeText).getUnicodeText();
return null;
}
/// Queries the Clipboard for the presence of data in the UnicodeText format.
bool containsUnicodeText()
{
dfl.data.IDataObject ido = getDataObject();
return ido.getDataPresent(DataFormats.unicodeText);
dfl.data.IDataObject dataObj = getDataObject();
return dataObj.getDataPresent(DataFormats.unicodeText);
}
/// Stores (Ansi)Text data on the Clipboard.
// 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.
ubyte[] getText()
{
dfl.data.IDataObject ido = getDataObject();
if(ido.getDataPresent(DataFormats.text))
return ido.getData(DataFormats.text).getText();
dfl.data.IDataObject dataObj = getDataObject();
if(dataObj.getDataPresent(DataFormats.text))
return dataObj.getData(DataFormats.text).getText();
return null;
}
/// Queries the Clipboard for the presence of data in the (Ansi)Text format.
bool containsText()
{
dfl.data.IDataObject ido = getDataObject();
return ido.getDataPresent(DataFormats.text);
dfl.data.IDataObject dataObj = getDataObject();
return dataObj.getDataPresent(DataFormats.text);
}
/// 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.
string[] getFileDropList()
{
dfl.data.IDataObject ido = getDataObject();
if(ido.getDataPresent(DataFormats.fileDrop))
return ido.getData(DataFormats.fileDrop).getStrings();
dfl.data.IDataObject dataObj = getDataObject();
if(dataObj.getDataPresent(DataFormats.fileDrop))
return dataObj.getData(DataFormats.fileDrop).getStrings();
return null;
}
/// Queries the Clipboard for the presence of data in the FileDrop data format.
bool containsFileDropList()
{
dfl.data.IDataObject ido = getDataObject();
return ido.getDataPresent(DataFormats.fileDrop);
dfl.data.IDataObject dataObj = getDataObject();
return dataObj.getDataPresent(DataFormats.fileDrop);
}
/// 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.
Image getImage()
{
dfl.data.IDataObject ido = getDataObject();
if(ido.getDataPresent(DataFormats.bitmap))
return ido.getData(DataFormats.bitmap).getImage();
dfl.data.IDataObject dataObj = getDataObject();
if(dataObj.getDataPresent(DataFormats.bitmap))
return dataObj.getData(DataFormats.bitmap).getImage();
return null;
}
/// Queries the Clipboard for the presence of data in the Bitmap data format.
bool containsImage()
{
dfl.data.IDataObject ido = getDataObject();
return ido.getDataPresent(DataFormats.bitmap);
dfl.data.IDataObject dataObj = getDataObject();
return dataObj.getDataPresent(DataFormats.bitmap);
}
@ -276,9 +209,8 @@ static:
if (S_OK != OleSetClipboard(null))
throw new DflException("Unable to clear clipboard data");
_comd = null;
_dd = null;
_objref = null;
_comDataObject = null;
_dflDataObject = null;
}
@ -292,7 +224,6 @@ static:
private:
dfl.internal.wincom.IDataObject _comd;
dfl.data.IDataObject _dd;
Object _objref; // Prevent dd from being garbage collected!
dfl.internal.wincom.IDataObject _comDataObject;
dfl.data.IDataObject _dflDataObject;
}

View file

@ -39,7 +39,7 @@ class DataFormats // docmain
}
package:
package:
int _id;
Dstring _name;
@ -731,7 +731,7 @@ class Data // docmain
}
private:
private:
TypeInfo _info;
InnerValues _innerValues;
@ -740,7 +740,7 @@ class Data // docmain
Data dataValue;
Object objectValue;
Dstring dstringValue;
Dstring[] dstringsValue; // For drop files.
Dstring[] dstringsValue; // For FileDrop
Ddstring ddstringValue;
Dwstring dwstringValue;
uint uintValue;
@ -781,7 +781,6 @@ interface IDataObject // docmain
///
Dstring[] getFormats();
//Dstring[] getFormats(bool onlyNative);
///
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.
/// When -replace- is true, stores new data with as a pair of preexist format.
// 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)
{
/+