view dwt/browser/MozillaDelegate.d @ 36:db5a898b2119

Fixed a lot of compile errors
author Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
date Tue, 07 Oct 2008 12:56:18 +0200
parents 5b53d338c709
children d8635bb48c7c
line wrap: on
line source

/*******************************************************************************
 * Copyright (c) 2003, 2007 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:
 *     Jacob Carlborg <jacob.carlborg@gmail.com>
 *******************************************************************************/
module dwt.browser.MozillaDelegate;

import dwt.DWT;
import dwt.browser.Browser;
import dwt.dwthelper.string;
import dwt.dwthelper.utils;
import dwt.widgets.Display;
import dwt.widgets.Event;
import dwt.widgets.Listener;

class MozillaDelegate {
    Browser browser;
    Listener listener;
    bool hasFocus;

    this (Browser browser) {
        super();
        this.browser = browser;
    }

    static Browser findBrowser (int handle) {
        Display display = Display.getCurrent();
        return cast(Browser) display.findWidget(handle);
    }

    static char[] mbcsToWcs (String codePage, byte[] buffer) {
        //  int encoding = OS.CFStringGetSystemEncoding ();
        //  int cfString = OS.CFStringCreateWithBytes (OS.kCFAllocatorDefault, buffer, buffer.length, encoding, false);
        //  char[] chars = null;
        //  if (cfString !is 0) {
        //      int length = OS.CFStringGetLength (cfString);
        //      chars = new char [length];
        //      if (length !is 0) {
        //          CFRange range = new CFRange ();
        //          range.length = length;
        //          OS.CFStringGetCharacters (cfString, range, chars);
        //      }
        //      OS.CFRelease (cfString);
        //  }
        //  return chars;

        //return new String(buffer).toCharArray(); commented by Jacob Carlborg
        return cast(char[]) buffer.dup; // FIXME
    }

    static byte[] wcsToMbcs (String codePage, String str, bool terminate) {
        //  char[] chars = new char [String.length()];
        //  String.getChars (0, chars.length, chars, 0);
        //  int cfString = OS.CFStringCreateWithCharacters (OS.kCFAllocatorDefault, chars, chars.length);
        //  byte[] buffer = null;
        //  if (cfString !is 0) {
        //      CFRange range = new CFRange ();
        //      range.length = chars.length;
        //      int encoding = OS.CFStringGetSystemEncoding ();
        //      int[] size = new int[1];
        //      int numChars = OS.CFStringGetBytes (cfString, range, encoding, cast(byte)'?', true, null, 0, size);
        //      buffer = new byte [size[0] + (terminate ? 1 : 0)];
        //      if (numChars !is 0) {
        //          numChars = OS.CFStringGetBytes (cfString, range, encoding, cast(byte)'?', true, buffer, size[0], size);
        //      }
        //      OS.CFRelease (cfString);
        //  }
        //  return buffer;
        if (terminate)
            str ~= "\0";

        //return str.getBytes(); commented by Jacob Carlborg
        return cast(byte[]) str.dup; // FIXME
    }

    int getHandle () {
        return browser.view.id;
    }

    String getLibraryName () {
        return "libxpcom.dylib"; //$NON-NLS-1$
    }

    String getDWTInitLibraryName () {
        return "swt-xulrunner"; //$NON-NLS-1$
    }

    void handleFocus () {
        if (hasFocus)
            return;
        hasFocus = true;
        (cast(Mozilla) browser.webBrowser).Activate();
        browser.setFocus();
        listener = new class Listener {
            public void handleEvent (Event event) {
                if (event.widget == browser)
                    return;

                (cast(Mozilla) browser.webBrowser).Deactivate();
                hasFocus = false;
                browser.getDisplay().removeFilter(DWT.FocusIn, this);
                browser.getShell().removeListener(DWT.Deactivate, this);
                listener = null;
            }

        };
        browser.getDisplay().addFilter(DWT.FocusIn, listener);
        browser.getShell().addListener(DWT.Deactivate, listener);
    }

    void handleMouseDown () {
    }

    bool hookEnterExit () {
        return true;
    }

    void init_ () {
    }

    bool needsSpinup () {
        return false;
    }

    void onDispose (int embedHandle) {
        if (listener !is null) {
            browser.getDisplay().removeFilter(DWT.FocusIn, listener);
            browser.getShell().removeListener(DWT.Deactivate, listener);
            listener = null;
        }
        browser = null;
    }

    void setSize (int embedHandle, int width, int height) {
        // TODO
    }

}