1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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  *     Frank Benoit <benoit@tionex.de>
12  *******************************************************************************/
13 module org.eclipse.swt.widgets.ImageList;
14 
15 import java.lang.all;
16 
17 
18 import org.eclipse.swt.internal.gtk.OS;
19 import org.eclipse.swt.graphics.Image;
20 import org.eclipse.swt.widgets.Display;
21 
22 
23 class ImageList {
24     GdkPixbuf* [] pixbufs;
25     int width = -1, height = -1;
26     Image [] images;
27 
28 public this() {
29     images = new Image [4];
30     pixbufs = new GdkPixbuf*[4];
31 }
32 
33 public int add (Image image) {
34     int index = 0;
35     while (index < images.length) {
36         if (images [index] !is null) {
37             if (images [index].isDisposed ()) {
38                 OS.g_object_unref (pixbufs [index]);
39                 images [index] = null;
40                 pixbufs [index] = null;
41             }
42         }
43         if (images [index] is null) break;
44         index++;
45     }
46     if (index is images.length) {
47         Image [] newImages = new Image [images.length + 4];
48         System.arraycopy (images, 0, newImages, 0, images.length);
49         images = newImages;
50         pixbufs.length = pixbufs.length + 4;
51     }
52     set (index, image);
53     return index;
54 }
55 
56 public void dispose () {
57     if (pixbufs is null) return;
58     for (int index=0; index<pixbufs.length; index++) {
59         if (pixbufs [index] !is null) OS.g_object_unref (pixbufs [index]);
60     }
61     images = null;
62     pixbufs = null;
63 }
64 
65 public Image get (int index) {
66     return images [index];
67 }
68 
69 GdkPixbuf* getPixbuf (int index) {
70     return pixbufs [index];
71 }
72 
73 public int indexOf (Image image) {
74     if (image is null) return -1;
75     for (int index=0; index<images.length; index++) {
76         if (image is images [index]) return index;
77     }
78     return -1;
79 }
80 
81 int indexOf (GdkPixbuf* pixbuf) {
82     if (pixbuf is null) return -1;
83     for (int index=0; index<images.length; index++) {
84         if (pixbuf is pixbufs [index]) return index;
85     }
86     return -1;
87 }
88 
89 public bool isDisposed () {
90     return images is null;
91 }
92 
93 public void put (int index, Image image) {
94     ptrdiff_t count = images.length;
95     if (!(0 <= index && index < count)) return;
96     if (image !is null) {
97         set (index, image);
98     } else {
99         images [index] = null;
100         if (pixbufs [index] !is null) OS.g_object_unref (pixbufs [index]);
101         pixbufs [index] = null;
102     }
103 }
104 
105 public void remove (Image image) {
106     if (image is null) return;
107     for (int index=0; index<images.length; index++) {
108         if (image is images [index]){
109             OS.g_object_unref (pixbufs [index]);
110             images [index] = null;
111             pixbufs [index] = null;
112         }
113     }
114 }
115 
116 void set (int index, Image image) {
117     int w, h;
118     OS.gdk_drawable_get_size (image.pixmap, &w, &h);
119     auto pixbuf = Display.createPixbuf (image);
120     if (width is -1 || height is -1) {
121         width = w;
122         height = h;
123     }
124     if (w !is width || h !is height) {
125         auto scaledPixbuf = OS.gdk_pixbuf_scale_simple(pixbuf, width, height, OS.GDK_INTERP_BILINEAR);
126         OS.g_object_unref (pixbuf);
127         pixbuf = scaledPixbuf;
128     }
129     auto oldPixbuf = pixbufs [index];
130     if (oldPixbuf !is null) {
131         if (images [index] is image) {
132             OS.gdk_pixbuf_copy_area (pixbuf, 0, 0, width, height, oldPixbuf, 0, 0);
133             OS.g_object_unref (pixbuf);
134             pixbuf = oldPixbuf;
135         } else {
136             OS.g_object_unref (oldPixbuf);
137         }
138     }
139     pixbufs [index] = pixbuf;
140     images [index] = image;
141 }
142 
143 public int size () {
144     int result = 0;
145     for (int index=0; index<images.length; index++) {
146         if (images [index] !is null) {
147             if (images [index].isDisposed ()) {
148                 OS.g_object_unref (pixbufs [index]);
149                 images [index] = null;
150                 pixbufs [index] = null;
151             }
152             if (images [index] !is null) result++;
153         }
154     }
155     return result;
156 }
157 
158 }