view dwt/browser/WebBrowser.d @ 156:969e7de37c3d default tip

Fixes to get dwt to work with dmd and ldc
author Jacob Carlborg <doob@me.com>
date Wed, 08 Jul 2009 21:56:44 +0200
parents 38807a925e24
children
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:
 *      John Reimer <terminal.node@gmail.com>
 *******************************************************************************/
module dwt.browser.WebBrowser;

import dwt.dwthelper.utils;

import dwt.DWT;
import dwt.widgets.Composite;

import dwt.browser.Browser;
import dwt.browser.CloseWindowListener;
import dwt.browser.LocationListener;
import dwt.browser.OpenWindowListener;
import dwt.browser.ProgressListener;
import dwt.browser.StatusTextListener;
import dwt.browser.TitleListener;
import dwt.browser.VisibilityWindowListener;

abstract class WebBrowser {
    Browser browser;
    CloseWindowListener[] closeWindowListeners;
    LocationListener[] locationListeners;
    OpenWindowListener[] openWindowListeners;
    ProgressListener[] progressListeners;
    StatusTextListener[] statusTextListeners;
    TitleListener[] titleListeners;
    VisibilityWindowListener[] visibilityWindowListeners;

    static Runnable MozillaClearSessions;
    static Runnable NativeClearSessions;

    /* Key Mappings */
    static const int [][] KeyTable = [
        /* Keyboard and Mouse Masks */
        [18,    DWT.ALT],
        [16,    DWT.SHIFT],
        [17,    DWT.CONTROL],
        [224,   DWT.COMMAND],

        /* Literal Keys */
        [65,    'a'],
        [66,    'b'],
        [67,    'c'],
        [68,    'd'],
        [69,    'e'],
        [70,    'f'],
        [71,    'g'],
        [72,    'h'],
        [73,    'i'],
        [74,    'j'],
        [75,    'k'],
        [76,    'l'],
        [77,    'm'],
        [78,    'n'],
        [79,    'o'],
        [80,    'p'],
        [81,    'q'],
        [82,    'r'],
        [83,    's'],
        [84,    't'],
        [85,    'u'],
        [86,    'v'],
        [87,    'w'],
        [88,    'x'],
        [89,    'y'],
        [90,    'z'],
        [48,    '0'],
        [49,    '1'],
        [50,    '2'],
        [51,    '3'],
        [52,    '4'],
        [53,    '5'],
        [54,    '6'],
        [55,    '7'],
        [56,    '8'],
        [57,    '9'],
        [32,    ' '],
        [59,    ';'],
        [61,    '='],
        [188,   ','],
        [190,   '.'],
        [191,   '/'],
        [219,   '['],
        [221,   ']'],
        [222,   '\''],
        [192,   '`'],
        [220,   '\\'],
        [108,   '|'],

        /* Non-Numeric Keypad Keys */
        [37,    DWT.ARROW_LEFT],
        [39,    DWT.ARROW_RIGHT],
        [38,    DWT.ARROW_UP],
        [40,    DWT.ARROW_DOWN],
        [45,    DWT.INSERT],
        [36,    DWT.HOME],
        [35,    DWT.END],
        [46,    DWT.DEL],
        [33,    DWT.PAGE_UP],
        [34,    DWT.PAGE_DOWN],

        /* Virtual and Ascii Keys */
        [8,     DWT.BS],
        [13,    DWT.CR],
        [9,     DWT.TAB],
        [27,    DWT.ESC],
        [12,    DWT.DEL],

        /* Functions Keys */
        [112,   DWT.F1],
        [113,   DWT.F2],
        [114,   DWT.F3],
        [115,   DWT.F4],
        [116,   DWT.F5],
        [117,   DWT.F6],
        [118,   DWT.F7],
        [119,   DWT.F8],
        [120,   DWT.F9],
        [121,   DWT.F10],
        [122,   DWT.F11],
        [123,   DWT.F12],
        [124,   DWT.F13],
        [125,   DWT.F14],
        [126,   DWT.F15],
        [127,   0],
        [128,   0],
        [129,   0],
        [130,   0],
        [131,   0],
        [132,   0],
        [133,   0],
        [134,   0],
        [135,   0],

        /* Numeric Keypad Keys */
        [96,    DWT.KEYPAD_0],
        [97,    DWT.KEYPAD_1],
        [98,    DWT.KEYPAD_2],
        [99,    DWT.KEYPAD_3],
        [100,   DWT.KEYPAD_4],
        [101,   DWT.KEYPAD_5],
        [102,   DWT.KEYPAD_6],
        [103,   DWT.KEYPAD_7],
        [104,   DWT.KEYPAD_8],
        [105,   DWT.KEYPAD_9],
        [14,    DWT.KEYPAD_CR],
        [107,   DWT.KEYPAD_ADD],
        [109,   DWT.KEYPAD_SUBTRACT],
        [106,   DWT.KEYPAD_MULTIPLY],
        [111,   DWT.KEYPAD_DIVIDE],
        [110,   DWT.KEYPAD_DECIMAL],

        /* Other keys */
        [20,    DWT.CAPS_LOCK],
        [144,   DWT.NUM_LOCK],
        [145,   DWT.SCROLL_LOCK],
        [44,    DWT.PRINT_SCREEN],
        [6,     DWT.HELP],
        [19,    DWT.PAUSE],
        [3,     DWT.BREAK],

        /* Safari-specific */
        [186,   ';'],
        [187,   '='],
        [189,   '-'],
    ];

public void addCloseWindowListener (CloseWindowListener listener) {
    //CloseWindowListener[] newCloseWindowListeners = new CloseWindowListener[closeWindowListeners.length + 1];
    //System.arraycopy(closeWindowListeners, 0, newCloseWindowListeners, 0, closeWindowListeners.length);
    //closeWindowListeners = newCloseWindowListeners;
    closeWindowListeners ~= listener;
}

public void addLocationListener (LocationListener listener) {
    //LocationListener[] newLocationListeners = new LocationListener[locationListeners.length + 1];
    //System.arraycopy(locationListeners, 0, newLocationListeners, 0, locationListeners.length);
    //locationListeners = newLocationListeners;
    locationListeners ~= listener;
}

public void addOpenWindowListener (OpenWindowListener listener) {
    //OpenWindowListener[] newOpenWindowListeners = new OpenWindowListener[openWindowListeners.length + 1];
    //System.arraycopy(openWindowListeners, 0, newOpenWindowListeners, 0, openWindowListeners.length);
    //openWindowListeners = newOpenWindowListeners;
    openWindowListeners ~= listener;
}

public void addProgressListener (ProgressListener listener) {
    //ProgressListener[] newProgressListeners = new ProgressListener[progressListeners.length + 1];
    //System.arraycopy(progressListeners, 0, newProgressListeners, 0, progressListeners.length);
    //progressListeners = newProgressListeners;
    progressListeners ~= listener;
}

public void addStatusTextListener (StatusTextListener listener) {
    //StatusTextListener[] newStatusTextListeners = new StatusTextListener[statusTextListeners.length + 1];
    //System.arraycopy(statusTextListeners, 0, newStatusTextListeners, 0, statusTextListeners.length);
    //statusTextListeners = newStatusTextListeners;
    statusTextListeners ~= listener;
}

public void addTitleListener (TitleListener listener) {
    //TitleListener[] newTitleListeners = new TitleListener[titleListeners.length + 1];
    //System.arraycopy(titleListeners, 0, newTitleListeners, 0, titleListeners.length);
    //titleListeners = newTitleListeners;
    titleListeners ~= listener;
}

public void addVisibilityWindowListener (VisibilityWindowListener listener) {
    //VisibilityWindowListener[] newVisibilityWindowListeners = new VisibilityWindowListener[visibilityWindowListeners.length + 1];
    //System.arraycopy(visibilityWindowListeners, 0, newVisibilityWindowListeners, 0, visibilityWindowListeners.length);
    //visibilityWindowListeners = newVisibilityWindowListeners;
    visibilityWindowListeners ~= listener;
}

public abstract bool back ();

public static void clearSessions () {
    if (NativeClearSessions !is null) NativeClearSessions.run ();
    if (MozillaClearSessions !is null) MozillaClearSessions.run ();
}

public abstract void create (Composite parent, int style);

public abstract bool execute (String script);

public abstract bool forward ();

public abstract String getText ();

public abstract String getUrl ();

public Object getWebBrowser () {
    return null;
}

public abstract bool isBackEnabled ();

public bool isFocusControl () {
    return false;
}

public abstract bool isForwardEnabled ();

public abstract void refresh ();

public void removeCloseWindowListener (CloseWindowListener listener) {
    if (closeWindowListeners.length is 0) return;
    int index = -1;
    for (int i = 0; i < closeWindowListeners.length; i++) {
        if (listener is closeWindowListeners[i]){
            index = i;
            break;
        }
    }
    if (index is -1) return;
    if (closeWindowListeners.length is 1) {
        closeWindowListeners = new CloseWindowListener[0];
        return;
    }
    //CloseWindowListener[] newCloseWindowListeners = new CloseWindowListener[closeWindowListeners.length - 1];
    //System.arraycopy (closeWindowListeners, 0, newCloseWindowListeners, 0, index);
    //System.arraycopy (closeWindowListeners, index + 1, newCloseWindowListeners, index, closeWindowListeners.length - index - 1);
    closeWindowListeners = closeWindowListeners[0..index] ~ closeWindowListeners[index+1..$];
}

public void removeLocationListener (LocationListener listener) {
    if (locationListeners.length is 0) return;
    int index = -1;
    for (int i = 0; i < locationListeners.length; i++) {
        if (listener is locationListeners[i]){
            index = i;
            break;
        }
    }
    if (index is -1) return;
    if (locationListeners.length is 1) {
        locationListeners = new LocationListener[0];
        return;
    }
    //LocationListener[] newLocationListeners = new LocationListener[locationListeners.length - 1];
    //System.arraycopy (locationListeners, 0, newLocationListeners, 0, index);
    //System.arraycopy (locationListeners, index + 1, newLocationListeners, index, locationListeners.length - index - 1);
    locationListeners = locationListeners[0..index] ~ locationListeners[index+1..$];
}

public void removeOpenWindowListener (OpenWindowListener listener) {
    if (openWindowListeners.length is 0) return;
    int index = -1;
    for (int i = 0; i < openWindowListeners.length; i++) {
        if (listener is openWindowListeners[i]){
            index = i;
            break;
        }
    }
    if (index is -1) return;
    if (openWindowListeners.length is 1) {
        openWindowListeners = new OpenWindowListener[0];
        return;
    }
    //OpenWindowListener[] newOpenWindowListeners = new OpenWindowListener[openWindowListeners.length - 1];
    //System.arraycopy (openWindowListeners, 0, newOpenWindowListeners, 0, index);
    //System.arraycopy (openWindowListeners, index + 1, newOpenWindowListeners, index, openWindowListeners.length - index - 1);
    openWindowListeners = openWindowListeners[0..index] ~ openWindowListeners[index+1..$];
}

public void removeProgressListener (ProgressListener listener) {
    if (progressListeners.length is 0) return;
    int index = -1;
    for (int i = 0; i < progressListeners.length; i++) {
        if (listener is progressListeners[i]){
            index = i;
            break;
        }
    }
    if (index is -1) return;
    if (progressListeners.length is 1) {
        progressListeners = new ProgressListener[0];
        return;
    }
    //ProgressListener[] newProgressListeners = new ProgressListener[progressListeners.length - 1];
    //System.arraycopy (progressListeners, 0, newProgressListeners, 0, index);
    //System.arraycopy (progressListeners, index + 1, newProgressListeners, index, progressListeners.length - index - 1);
    progressListeners = progressListeners[0..index] ~ progressListeners[index+1..$];
}

public void removeStatusTextListener (StatusTextListener listener) {
    if (statusTextListeners.length is 0) return;
    int index = -1;
    for (int i = 0; i < statusTextListeners.length; i++) {
        if (listener is statusTextListeners[i]){
            index = i;
            break;
        }
    }
    if (index is -1) return;
    if (statusTextListeners.length is 1) {
        statusTextListeners = new StatusTextListener[0];
        return;
    }
    //StatusTextListener[] newStatusTextListeners = new StatusTextListener[statusTextListeners.length - 1];
    //System.arraycopy (statusTextListeners, 0, newStatusTextListeners, 0, index);
    //System.arraycopy (statusTextListeners, index + 1, newStatusTextListeners, index, statusTextListeners.length - index - 1);
    statusTextListeners = statusTextListeners[0..index] ~ statusTextListeners[index+1..$];
}

public void removeTitleListener (TitleListener listener) {
    if (titleListeners.length is 0) return;
    int index = -1;
    for (int i = 0; i < titleListeners.length; i++) {
        if (listener is titleListeners[i]){
            index = i;
            break;
        }
    }
    if (index is -1) return;
    if (titleListeners.length is 1) {
        titleListeners = new TitleListener[0];
        return;
    }
    TitleListener[] newTitleListeners = new TitleListener[titleListeners.length - 1];
    //System.arraycopy (titleListeners, 0, newTitleListeners, 0, index);
    //System.arraycopy (titleListeners, index + 1, newTitleListeners, index, titleListeners.length - index - 1);
    titleListeners = titleListeners[0..index] ~ titleListeners[index+1..$];
}

public void removeVisibilityWindowListener (VisibilityWindowListener listener) {
    if (visibilityWindowListeners.length is 0) return;
    int index = -1;
    for (int i = 0; i < visibilityWindowListeners.length; i++) {
        if (listener is visibilityWindowListeners[i]){
            index = i;
            break;
        }
    }
    if (index is -1) return;
    if (visibilityWindowListeners.length is 1) {
        visibilityWindowListeners = new VisibilityWindowListener[0];
        return;
    }
    //VisibilityWindowListener[] newVisibilityWindowListeners = new VisibilityWindowListener[visibilityWindowListeners.length - 1];
    //System.arraycopy (visibilityWindowListeners, 0, newVisibilityWindowListeners, 0, index);
    //System.arraycopy (visibilityWindowListeners, index + 1, newVisibilityWindowListeners, index, visibilityWindowListeners.length - index - 1);
    visibilityWindowListeners = visibilityWindowListeners[0..index] ~ visibilityWindowListeners[index+1..$];
}

public void setBrowser (Browser browser) {
    this.browser = browser;
}

public abstract bool setText (String html);

public abstract bool setUrl (String url);

public abstract void stop ();

int translateKey (int key) {
    for (int i = 0; i < KeyTable.length; i++) {
        if (KeyTable[i][0] is key) return KeyTable[i][1];
    }
    return 0;
}
}