view dwt/dnd/TextTransfer.d @ 255:5a30aa9820f3

removed tango.stdc.stringz imports and allow null for arrays and string arguments.
author Frank Benoit <benoit@tionex.de>
date Sun, 15 Jun 2008 22:32:20 +0200
parents 380bad9f6852
children c0d810de7093
line wrap: on
line source

/*******************************************************************************
 * Copyright (c) 2000, 2006 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 * Port to the D programming language:
 *     Frank Benoit <benoit@tionex.de>
 *******************************************************************************/
module dwt.dnd.TextTransfer;

import dwt.internal.Converter;
import dwt.internal.gtk.OS;
import dwt.dwthelper.utils;
import dwt.dnd.ByteArrayTransfer;
import dwt.dnd.TransferData;
import dwt.dnd.DND;


/**
 * The class <code>TextTransfer</code> provides a platform specific mechanism
 * for converting plain text represented as a java <code>String</code>
 * to a platform specific representation of the data and vice versa.
 *
 * <p>An example of a java <code>String</code> containing plain text is shown
 * below:</p>
 *
 * <code><pre>
 *     String textData = "Hello World";
 * </code></pre>
 *
 * @see Transfer
 */
public class TextTransfer : ByteArrayTransfer {

    private static TextTransfer _instance;
    private static const String COMPOUND_TEXT = "COMPOUND_TEXT"; //$NON-NLS-1$
    private static const String UTF8_STRING = "UTF8_STRING"; //$NON-NLS-1$
    private static const String STRING = "STRING"; //$NON-NLS-1$
    private static const int COMPOUND_TEXT_ID;
    private static const int UTF8_STRING_ID;
    private static const int STRING_ID;

static this(){
    COMPOUND_TEXT_ID = registerType(COMPOUND_TEXT);
    UTF8_STRING_ID = registerType(UTF8_STRING);
    STRING_ID = registerType(STRING);
    _instance = new TextTransfer();
}

private this() {}

/**
 * Returns the singleton instance of the TextTransfer class.
 *
 * @return the singleton instance of the TextTransfer class
 */
public static TextTransfer getInstance () {
    return _instance;
}

/**
 * This implementation of <code>javaToNative</code> converts plain text
 * represented by a java <code>String</code> to a platform specific representation.
 *
 * @param object a java <code>String</code> containing text
 * @param transferData an empty <code>TransferData</code> object; this object
 *  will be filled in on return with the platform specific format of the data
 *
 * @see Transfer#javaToNative
 */
override public void javaToNative (Object object, TransferData transferData) {
    transferData.result = 0;
    if (!checkText(object) || !isSupportedType(transferData)) {
        DND.error(DND.ERROR_INVALID_DATA);
    }
    String string = (cast(ArrayWrapperString)object).array;
    char* utf8 = toStringz(string);
    if  (transferData.type is cast(void*) COMPOUND_TEXT_ID) {
        void* encoding;
        int format;
        char* ctext;
        int length;
        bool result = cast(bool) OS.gdk_utf8_to_compound_text(utf8, &encoding, &format, &ctext, &length);
        if (!result) return;
        transferData.type = encoding;
        transferData.format = format;
        transferData.length = length;
        transferData.pValue = ctext;
        transferData.result = 1;
    }
    if (transferData.type is cast(void*)UTF8_STRING_ID) {
        char* pValue = cast(char*)OS.g_malloc(string.length+1);
        if (pValue is  null) return;
        pValue[ 0 .. string.length ] = string;
        pValue[ string.length ] = '\0';
        transferData.type = cast(void*)UTF8_STRING_ID;
        transferData.format = 8;
        transferData.length = string.length;
        transferData.pValue = pValue;
        transferData.result = 1;
    }
    if (transferData.type is cast(void*)STRING_ID) {
        auto string_target = OS.gdk_utf8_to_string_target(utf8);
        if (string_target is  null) return;
        transferData.type = cast(void*)STRING_ID;
        transferData.format = 8;
        transferData.length = tango.stdc.string.strlen(string_target);
        transferData.pValue = string_target;
        transferData.result = 1;
    }
}

/**
 * This implementation of <code>nativeToJava</code> converts a platform specific
 * representation of plain text to a java <code>String</code>.
 *
 * @param transferData the platform specific representation of the data to be converted
 * @return a java <code>String</code> containing text if the conversion was successful; otherwise null
 *
 * @see Transfer#nativeToJava
 */
override public Object nativeToJava(TransferData transferData){
    if (!isSupportedType(transferData) ||  transferData.pValue is null) return null;
    char** list;
    int count = OS.gdk_text_property_to_utf8_list(transferData.type, transferData.format, transferData.pValue, transferData.length, &list);
    if (count is 0) return null;
    String utf8 = fromStringz( list[0] ).dup;
    OS.g_strfreev(list);
    return new ArrayWrapperString( utf8 );
}

override protected int[] getTypeIds() {
    return [UTF8_STRING_ID, COMPOUND_TEXT_ID, STRING_ID];
}

override protected String[] getTypeNames() {
    return [UTF8_STRING, COMPOUND_TEXT, STRING];
}

bool checkText(Object object) {
    if( object is null ) return false;
    ArrayWrapperString astr = cast(ArrayWrapperString)object;
    if( astr is null ) return false;
    return astr.array.length > 0;
}

protected override bool validate(Object object) {
    return checkText(object);
}
}