view dynamin/gui/windows_clipboard.d @ 103:73060bc3f004

Change license to Boost 1.0 and MPL 2.0.
author Jordan Miner <jminer7@gmail.com>
date Tue, 15 May 2012 22:06:02 -0500
parents aa4efef0f0b1
children acdbb30fee7e
line wrap: on
line source


/*
 * Copyright Jordan Miner
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 */

module dynamin.gui.windows_clipboard;

import dynamin.c.windows;
import Utf = tango.text.convert.Utf;

template ClipboardBackend() {
	void backend_setText(string text) {
		if(!OpenClipboard(msgWnd))
			return;
		EmptyClipboard();
		auto wtext = Utf.toString16(text);
		HGLOBAL hmem = GlobalAlloc(GMEM_MOVEABLE, (wtext.length+1)*wchar.sizeof);
		wchar* data = cast(wchar*)GlobalLock(hmem);
		data[0..wtext.length] = wtext;
		data[wtext.length] = 0;
		GlobalUnlock(hmem);
		SetClipboardData(CF_UNICODETEXT, data);
		CloseClipboard();
	}
	string backend_getText() {
		if(!OpenClipboard(msgWnd))
			return null;
		wchar* data = cast(wchar*)GetClipboardData(CF_UNICODETEXT);
		CloseClipboard();
		if(data is null)
			return null;
		int i = 0;
		while(data[i] != '\0')
			++i;
		if(i == 0)
			return null;
		return Utf.toString(data[0..i]);
	}
	bool backend_containsText() {
		return IsClipboardFormatAvailable(CF_UNICODETEXT) ? true : false;
	}
}

// Windows only has one clipboard
template SelectionBackend() {
	void backend_setText(string text) {
	}
	string backend_getText() {
		return null;
	}
	bool backend_containsText() {
		return false;
	}
}