1 /******************************************************************************* 2 * Copyright (c) 2003, 2007 IBM Corporation and others. 3 * All rights reserved. This program and the accompanying materials 4 * are made available under the terms of the Eclipse Public License v1.0 5 * which accompanies this distribution, and is available at 6 * http://www.eclipse.org/legal/epl-v10.html 7 * 8 * Contributors: 9 * IBM Corporation - initial API and implementation 10 * Port to the D programming language: 11 * John Reimer <terminal.node@gmail.com> 12 *******************************************************************************/ 13 module org.eclipse.swt.browser.WebBrowser; 14 15 import java.lang.all; 16 17 import org.eclipse.swt.SWT; 18 import org.eclipse.swt.widgets.Composite; 19 20 import org.eclipse.swt.browser.Browser; 21 import org.eclipse.swt.browser.CloseWindowListener; 22 import org.eclipse.swt.browser.LocationListener; 23 import org.eclipse.swt.browser.OpenWindowListener; 24 import org.eclipse.swt.browser.ProgressListener; 25 import org.eclipse.swt.browser.StatusTextListener; 26 import org.eclipse.swt.browser.TitleListener; 27 import org.eclipse.swt.browser.VisibilityWindowListener; 28 29 abstract class WebBrowser { 30 Browser browser; 31 CloseWindowListener[] closeWindowListeners; 32 LocationListener[] locationListeners; 33 OpenWindowListener[] openWindowListeners; 34 ProgressListener[] progressListeners; 35 StatusTextListener[] statusTextListeners; 36 TitleListener[] titleListeners; 37 VisibilityWindowListener[] visibilityWindowListeners; 38 39 static Runnable MozillaClearSessions; 40 static Runnable NativeClearSessions; 41 42 /* Key Mappings */ 43 static const int [][] KeyTable = [ 44 /* Keyboard and Mouse Masks */ 45 [18, SWT.ALT], 46 [16, SWT.SHIFT], 47 [17, SWT.CONTROL], 48 [224, SWT.COMMAND], 49 50 /* Literal Keys */ 51 [65, 'a'], 52 [66, 'b'], 53 [67, 'c'], 54 [68, 'd'], 55 [69, 'e'], 56 [70, 'f'], 57 [71, 'g'], 58 [72, 'h'], 59 [73, 'i'], 60 [74, 'j'], 61 [75, 'k'], 62 [76, 'l'], 63 [77, 'm'], 64 [78, 'n'], 65 [79, 'o'], 66 [80, 'p'], 67 [81, 'q'], 68 [82, 'r'], 69 [83, 's'], 70 [84, 't'], 71 [85, 'u'], 72 [86, 'v'], 73 [87, 'w'], 74 [88, 'x'], 75 [89, 'y'], 76 [90, 'z'], 77 [48, '0'], 78 [49, '1'], 79 [50, '2'], 80 [51, '3'], 81 [52, '4'], 82 [53, '5'], 83 [54, '6'], 84 [55, '7'], 85 [56, '8'], 86 [57, '9'], 87 [32, ' '], 88 [59, ';'], 89 [61, '='], 90 [188, ','], 91 [190, '.'], 92 [191, '/'], 93 [219, '['], 94 [221, ']'], 95 [222, '\''], 96 [192, '`'], 97 [220, '\\'], 98 [108, '|'], 99 100 /* Non-Numeric Keypad Keys */ 101 [37, SWT.ARROW_LEFT], 102 [39, SWT.ARROW_RIGHT], 103 [38, SWT.ARROW_UP], 104 [40, SWT.ARROW_DOWN], 105 [45, SWT.INSERT], 106 [36, SWT.HOME], 107 [35, SWT.END], 108 [46, SWT.DEL], 109 [33, SWT.PAGE_UP], 110 [34, SWT.PAGE_DOWN], 111 112 /* Virtual and Ascii Keys */ 113 [8, SWT.BS], 114 [13, SWT.CR], 115 [9, SWT.TAB], 116 [27, SWT.ESC], 117 [12, SWT.DEL], 118 119 /* Functions Keys */ 120 [112, SWT.F1], 121 [113, SWT.F2], 122 [114, SWT.F3], 123 [115, SWT.F4], 124 [116, SWT.F5], 125 [117, SWT.F6], 126 [118, SWT.F7], 127 [119, SWT.F8], 128 [120, SWT.F9], 129 [121, SWT.F10], 130 [122, SWT.F11], 131 [123, SWT.F12], 132 [124, SWT.F13], 133 [125, SWT.F14], 134 [126, SWT.F15], 135 [127, 0], 136 [128, 0], 137 [129, 0], 138 [130, 0], 139 [131, 0], 140 [132, 0], 141 [133, 0], 142 [134, 0], 143 [135, 0], 144 145 /* Numeric Keypad Keys */ 146 [96, SWT.KEYPAD_0], 147 [97, SWT.KEYPAD_1], 148 [98, SWT.KEYPAD_2], 149 [99, SWT.KEYPAD_3], 150 [100, SWT.KEYPAD_4], 151 [101, SWT.KEYPAD_5], 152 [102, SWT.KEYPAD_6], 153 [103, SWT.KEYPAD_7], 154 [104, SWT.KEYPAD_8], 155 [105, SWT.KEYPAD_9], 156 [14, SWT.KEYPAD_CR], 157 [107, SWT.KEYPAD_ADD], 158 [109, SWT.KEYPAD_SUBTRACT], 159 [106, SWT.KEYPAD_MULTIPLY], 160 [111, SWT.KEYPAD_DIVIDE], 161 [110, SWT.KEYPAD_DECIMAL], 162 163 /* Other keys */ 164 [20, SWT.CAPS_LOCK], 165 [144, SWT.NUM_LOCK], 166 [145, SWT.SCROLL_LOCK], 167 [44, SWT.PRINT_SCREEN], 168 [6, SWT.HELP], 169 [19, SWT.PAUSE], 170 [3, SWT.BREAK], 171 172 /* Safari-specific */ 173 [186, ';'], 174 [187, '='], 175 [189, '-'], 176 ]; 177 178 public void addCloseWindowListener (CloseWindowListener listener) { 179 //CloseWindowListener[] newCloseWindowListeners = new CloseWindowListener[closeWindowListeners.length + 1]; 180 //System.arraycopy(closeWindowListeners, 0, newCloseWindowListeners, 0, closeWindowListeners.length); 181 //closeWindowListeners = newCloseWindowListeners; 182 closeWindowListeners ~= listener; 183 } 184 185 public void addLocationListener (LocationListener listener) { 186 //LocationListener[] newLocationListeners = new LocationListener[locationListeners.length + 1]; 187 //System.arraycopy(locationListeners, 0, newLocationListeners, 0, locationListeners.length); 188 //locationListeners = newLocationListeners; 189 locationListeners ~= listener; 190 } 191 192 public void addOpenWindowListener (OpenWindowListener listener) { 193 //OpenWindowListener[] newOpenWindowListeners = new OpenWindowListener[openWindowListeners.length + 1]; 194 //System.arraycopy(openWindowListeners, 0, newOpenWindowListeners, 0, openWindowListeners.length); 195 //openWindowListeners = newOpenWindowListeners; 196 openWindowListeners ~= listener; 197 } 198 199 public void addProgressListener (ProgressListener listener) { 200 //ProgressListener[] newProgressListeners = new ProgressListener[progressListeners.length + 1]; 201 //System.arraycopy(progressListeners, 0, newProgressListeners, 0, progressListeners.length); 202 //progressListeners = newProgressListeners; 203 progressListeners ~= listener; 204 } 205 206 public void addStatusTextListener (StatusTextListener listener) { 207 //StatusTextListener[] newStatusTextListeners = new StatusTextListener[statusTextListeners.length + 1]; 208 //System.arraycopy(statusTextListeners, 0, newStatusTextListeners, 0, statusTextListeners.length); 209 //statusTextListeners = newStatusTextListeners; 210 statusTextListeners ~= listener; 211 } 212 213 public void addTitleListener (TitleListener listener) { 214 //TitleListener[] newTitleListeners = new TitleListener[titleListeners.length + 1]; 215 //System.arraycopy(titleListeners, 0, newTitleListeners, 0, titleListeners.length); 216 //titleListeners = newTitleListeners; 217 titleListeners ~= listener; 218 } 219 220 public void addVisibilityWindowListener (VisibilityWindowListener listener) { 221 //VisibilityWindowListener[] newVisibilityWindowListeners = new VisibilityWindowListener[visibilityWindowListeners.length + 1]; 222 //System.arraycopy(visibilityWindowListeners, 0, newVisibilityWindowListeners, 0, visibilityWindowListeners.length); 223 //visibilityWindowListeners = newVisibilityWindowListeners; 224 visibilityWindowListeners ~= listener; 225 } 226 227 public abstract bool back (); 228 229 public static void clearSessions () { 230 if (NativeClearSessions !is null) NativeClearSessions.run (); 231 if (MozillaClearSessions !is null) MozillaClearSessions.run (); 232 } 233 234 public abstract void create (Composite parent, int style); 235 236 public abstract bool execute (String script); 237 238 public abstract bool forward (); 239 240 public abstract String getText (); 241 242 public abstract String getUrl (); 243 244 public Object getWebBrowser () { 245 return null; 246 } 247 248 public abstract bool isBackEnabled (); 249 250 public bool isFocusControl () { 251 return false; 252 } 253 254 public abstract bool isForwardEnabled (); 255 256 public abstract void refresh (); 257 258 public void removeCloseWindowListener (CloseWindowListener listener) { 259 if (closeWindowListeners.length is 0) return; 260 int index = -1; 261 for (int i = 0; i < closeWindowListeners.length; i++) { 262 if (listener is closeWindowListeners[i]){ 263 index = i; 264 break; 265 } 266 } 267 if (index is -1) return; 268 if (closeWindowListeners.length is 1) { 269 closeWindowListeners = new CloseWindowListener[0]; 270 return; 271 } 272 //CloseWindowListener[] newCloseWindowListeners = new CloseWindowListener[closeWindowListeners.length - 1]; 273 //System.arraycopy (closeWindowListeners, 0, newCloseWindowListeners, 0, index); 274 //System.arraycopy (closeWindowListeners, index + 1, newCloseWindowListeners, index, closeWindowListeners.length - index - 1); 275 closeWindowListeners = closeWindowListeners[0..index] ~ closeWindowListeners[index+1..$]; 276 } 277 278 public void removeLocationListener (LocationListener listener) { 279 if (locationListeners.length is 0) return; 280 int index = -1; 281 for (int i = 0; i < locationListeners.length; i++) { 282 if (listener is locationListeners[i]){ 283 index = i; 284 break; 285 } 286 } 287 if (index is -1) return; 288 if (locationListeners.length is 1) { 289 locationListeners = new LocationListener[0]; 290 return; 291 } 292 //LocationListener[] newLocationListeners = new LocationListener[locationListeners.length - 1]; 293 //System.arraycopy (locationListeners, 0, newLocationListeners, 0, index); 294 //System.arraycopy (locationListeners, index + 1, newLocationListeners, index, locationListeners.length - index - 1); 295 locationListeners = locationListeners[0..index] ~ locationListeners[index+1..$]; 296 } 297 298 public void removeOpenWindowListener (OpenWindowListener listener) { 299 if (openWindowListeners.length is 0) return; 300 int index = -1; 301 for (int i = 0; i < openWindowListeners.length; i++) { 302 if (listener is openWindowListeners[i]){ 303 index = i; 304 break; 305 } 306 } 307 if (index is -1) return; 308 if (openWindowListeners.length is 1) { 309 openWindowListeners = new OpenWindowListener[0]; 310 return; 311 } 312 //OpenWindowListener[] newOpenWindowListeners = new OpenWindowListener[openWindowListeners.length - 1]; 313 //System.arraycopy (openWindowListeners, 0, newOpenWindowListeners, 0, index); 314 //System.arraycopy (openWindowListeners, index + 1, newOpenWindowListeners, index, openWindowListeners.length - index - 1); 315 openWindowListeners = openWindowListeners[0..index] ~ openWindowListeners[index+1..$]; 316 } 317 318 public void removeProgressListener (ProgressListener listener) { 319 if (progressListeners.length is 0) return; 320 int index = -1; 321 for (int i = 0; i < progressListeners.length; i++) { 322 if (listener is progressListeners[i]){ 323 index = i; 324 break; 325 } 326 } 327 if (index is -1) return; 328 if (progressListeners.length is 1) { 329 progressListeners = new ProgressListener[0]; 330 return; 331 } 332 //ProgressListener[] newProgressListeners = new ProgressListener[progressListeners.length - 1]; 333 //System.arraycopy (progressListeners, 0, newProgressListeners, 0, index); 334 //System.arraycopy (progressListeners, index + 1, newProgressListeners, index, progressListeners.length - index - 1); 335 progressListeners = progressListeners[0..index] ~ progressListeners[index+1..$]; 336 } 337 338 public void removeStatusTextListener (StatusTextListener listener) { 339 if (statusTextListeners.length is 0) return; 340 int index = -1; 341 for (int i = 0; i < statusTextListeners.length; i++) { 342 if (listener is statusTextListeners[i]){ 343 index = i; 344 break; 345 } 346 } 347 if (index is -1) return; 348 if (statusTextListeners.length is 1) { 349 statusTextListeners = new StatusTextListener[0]; 350 return; 351 } 352 //StatusTextListener[] newStatusTextListeners = new StatusTextListener[statusTextListeners.length - 1]; 353 //System.arraycopy (statusTextListeners, 0, newStatusTextListeners, 0, index); 354 //System.arraycopy (statusTextListeners, index + 1, newStatusTextListeners, index, statusTextListeners.length - index - 1); 355 statusTextListeners = statusTextListeners[0..index] ~ statusTextListeners[index+1..$]; 356 } 357 358 public void removeTitleListener (TitleListener listener) { 359 if (titleListeners.length is 0) return; 360 int index = -1; 361 for (int i = 0; i < titleListeners.length; i++) { 362 if (listener is titleListeners[i]){ 363 index = i; 364 break; 365 } 366 } 367 if (index is -1) return; 368 if (titleListeners.length is 1) { 369 titleListeners = new TitleListener[0]; 370 return; 371 } 372 TitleListener[] newTitleListeners = new TitleListener[titleListeners.length - 1]; 373 //System.arraycopy (titleListeners, 0, newTitleListeners, 0, index); 374 //System.arraycopy (titleListeners, index + 1, newTitleListeners, index, titleListeners.length - index - 1); 375 titleListeners = titleListeners[0..index] ~ titleListeners[index+1..$]; 376 } 377 378 public void removeVisibilityWindowListener (VisibilityWindowListener listener) { 379 if (visibilityWindowListeners.length is 0) return; 380 int index = -1; 381 for (int i = 0; i < visibilityWindowListeners.length; i++) { 382 if (listener is visibilityWindowListeners[i]){ 383 index = i; 384 break; 385 } 386 } 387 if (index is -1) return; 388 if (visibilityWindowListeners.length is 1) { 389 visibilityWindowListeners = new VisibilityWindowListener[0]; 390 return; 391 } 392 //VisibilityWindowListener[] newVisibilityWindowListeners = new VisibilityWindowListener[visibilityWindowListeners.length - 1]; 393 //System.arraycopy (visibilityWindowListeners, 0, newVisibilityWindowListeners, 0, index); 394 //System.arraycopy (visibilityWindowListeners, index + 1, newVisibilityWindowListeners, index, visibilityWindowListeners.length - index - 1); 395 visibilityWindowListeners = visibilityWindowListeners[0..index] ~ visibilityWindowListeners[index+1..$]; 396 } 397 398 public void setBrowser (Browser browser) { 399 this.browser = browser; 400 } 401 402 public abstract bool setText (String html); 403 404 public abstract bool setUrl (String url); 405 406 public abstract void stop (); 407 408 int translateKey (int key) { 409 for (int i = 0; i < KeyTable.length; i++) { 410 if (KeyTable[i][0] is key) return KeyTable[i][1]; 411 } 412 return 0; 413 } 414 }