comparison dwtexamples/sleak/SleakExample.d @ 35:11cb40a34da1

SleakExample
author Frank Benoit <benoit@tionex.de>
date Sat, 08 Mar 2008 22:56:24 +0100
parents
children 56ff46d37645
comparison
equal deleted inserted replaced
34:7c3196948c7f 35:11cb40a34da1
1 /*
2 * Copyright (c) 2000, 2002 IBM Corp. All rights reserved.
3 * This file is made available under the terms of the Common Public License v1.0
4 * which accompanies this distribution, and is available at
5 * http://www.eclipse.org/legal/cpl-v10.html
6 *
7 * Port to the D programming language
8 * Frank Benoit <benoit@tionex.de>
9 */
10 module dwtexamples.sleak.SleakExample;
11
12 import dwt.program.Program;
13 import dwt.graphics.ImageData;
14 import dwt.graphics.Image;
15 import dwt.layout.FillLayout;
16 import dwt.events.PaintListener;
17 import dwt.events.PaintEvent;
18 import dwt.events.SelectionAdapter;
19 import dwt.events.SelectionEvent;
20 import tango.io.Stdout;
21 version(JIVE){
22 import jive.stacktrace;
23 }
24
25 void main() {
26 Display display;
27 Shell shell;
28 List list;
29 Canvas canvas;
30 Image image;
31
32 version( all ){
33 DeviceData data = new DeviceData();
34 data.tracking = true;
35 display = new Display(data);
36 Sleak sleak = new Sleak();
37 sleak.open();
38 }
39 else{
40 display = new Display();
41 }
42
43 shell = new Shell(display);
44 shell.setLayout(new FillLayout());
45 list = new List(shell, DWT.BORDER | DWT.SINGLE | DWT.V_SCROLL | DWT.H_SCROLL);
46 list.setItems(Program.getExtensions());
47 canvas = new Canvas(shell, DWT.BORDER);
48 canvas.addPaintListener(new class() PaintListener {
49 public void paintControl(PaintEvent e) {
50 if (image !is null) {
51 e.gc.drawImage(image, 0, 0);
52 }
53 }
54 });
55 list.addSelectionListener(new class() SelectionAdapter {
56 public void widgetSelected(SelectionEvent e) {
57 image = null; // potentially leak one image
58 char[][] selection = list.getSelection();
59 if (selection.length !is 0) {
60 Program program = Program.findProgram(selection[0]);
61 if (program !is null) {
62 ImageData imageData = program.getImageData();
63 if (imageData !is null) {
64 if (image !is null) image.dispose();
65 image = new Image(display, imageData);
66 }
67 }
68 }
69 canvas.redraw();
70 }
71 });
72 shell.setSize(shell.computeSize(DWT.DEFAULT, 200));
73 shell.open();
74 while (!shell.isDisposed()) {
75 if (!display.readAndDispatch())
76 display.sleep();
77 }
78 }