1 /* 2 * This file is part of gtkD. 3 * 4 * gtkD is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU Lesser General Public License 6 * as published by the Free Software Foundation; either version 3 7 * of the License, or (at your option) any later version, with 8 * some exceptions, please read the COPYING file. 9 * 10 * gtkD is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU Lesser General Public License for more details. 14 * 15 * You should have received a copy of the GNU Lesser General Public License 16 * along with gtkD; if not, write to the Free Software 17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA 18 */ 19 20 // generated automatically - do not change 21 // find conversion definition on APILookup.txt 22 // implement new conversion functionalities on the wrap.utils pakage 23 24 25 module gdk.Texture; 26 27 private import gdk.PaintableIF; 28 private import gdk.PaintableT; 29 private import gdk.c.functions; 30 public import gdk.c.types; 31 private import gdkpixbuf.Pixbuf; 32 private import gio.FileIF; 33 private import glib.ConstructionException; 34 private import glib.ErrorG; 35 private import glib.GException; 36 private import glib.Str; 37 private import gobject.ObjectG; 38 39 40 /** 41 * `GdkTexture` is the basic element used to refer to pixel data. 42 * 43 * It is primarily meant for pixel data that will not change over 44 * multiple frames, and will be used for a long time. 45 * 46 * There are various ways to create `GdkTexture` objects from a 47 * `GdkPixbuf`, or a Cairo surface, or other pixel data. 48 * 49 * The ownership of the pixel data is transferred to the `GdkTexture` 50 * instance; you can only make a copy of it, via 51 * [method@Gdk.Texture.download]. 52 * 53 * `GdkTexture` is an immutable object: That means you cannot change 54 * anything about it other than increasing the reference count via 55 * g_object_ref(). 56 */ 57 public class Texture : ObjectG, PaintableIF 58 { 59 /** the main Gtk struct */ 60 protected GdkTexture* gdkTexture; 61 62 /** Get the main Gtk struct */ 63 public GdkTexture* getTextureStruct(bool transferOwnership = false) 64 { 65 if (transferOwnership) 66 ownedRef = false; 67 return gdkTexture; 68 } 69 70 /** the main Gtk struct as a void* */ 71 protected override void* getStruct() 72 { 73 return cast(void*)gdkTexture; 74 } 75 76 /** 77 * Sets our main struct and passes it to the parent class. 78 */ 79 public this (GdkTexture* gdkTexture, bool ownedRef = false) 80 { 81 this.gdkTexture = gdkTexture; 82 super(cast(GObject*)gdkTexture, ownedRef); 83 } 84 85 // add the Paintable capabilities 86 mixin PaintableT!(GdkTexture); 87 88 89 /** */ 90 public static GType getType() 91 { 92 return gdk_texture_get_type(); 93 } 94 95 /** 96 * Creates a new texture object representing the `GdkPixbuf`. 97 * 98 * Params: 99 * pixbuf = a `GdkPixbuf` 100 * 101 * Returns: a new `GdkTexture` 102 * 103 * Throws: ConstructionException GTK+ fails to create the object. 104 */ 105 public this(Pixbuf pixbuf) 106 { 107 auto __p = gdk_texture_new_for_pixbuf((pixbuf is null) ? null : pixbuf.getPixbufStruct()); 108 109 if(__p is null) 110 { 111 throw new ConstructionException("null returned by new_for_pixbuf"); 112 } 113 114 this(cast(GdkTexture*) __p, true); 115 } 116 117 /** 118 * Creates a new texture by loading an image from a file. 119 * 120 * The file format is detected automatically. The supported formats 121 * are PNG and JPEG, though more formats might be available. 122 * 123 * If %NULL is returned, then @error will be set. 124 * 125 * Params: 126 * file = `GFile` to load 127 * 128 * Returns: A newly-created `GdkTexture` or %NULL if an error occurred. 129 * 130 * Throws: GException on failure. 131 * Throws: ConstructionException GTK+ fails to create the object. 132 */ 133 public this(FileIF file) 134 { 135 GError* err = null; 136 137 auto __p = gdk_texture_new_from_file((file is null) ? null : file.getFileStruct(), &err); 138 139 if (err !is null) 140 { 141 throw new GException( new ErrorG(err) ); 142 } 143 144 if(__p is null) 145 { 146 throw new ConstructionException("null returned by new_from_file"); 147 } 148 149 this(cast(GdkTexture*) __p, true); 150 } 151 152 /** 153 * Creates a new texture by loading an image from a resource. 154 * 155 * The file format is detected automatically. The supported formats 156 * are PNG and JPEG, though more formats might be available. 157 * 158 * It is a fatal error if @resource_path does not specify a valid 159 * image resource and the program will abort if that happens. 160 * If you are unsure about the validity of a resource, use 161 * [ctor@Gdk.Texture.new_from_file] to load it. 162 * 163 * Params: 164 * resourcePath = the path of the resource file 165 * 166 * Returns: A newly-created `GdkTexture` 167 * 168 * Throws: ConstructionException GTK+ fails to create the object. 169 */ 170 public this(string resourcePath) 171 { 172 auto __p = gdk_texture_new_from_resource(Str.toStringz(resourcePath)); 173 174 if(__p is null) 175 { 176 throw new ConstructionException("null returned by new_from_resource"); 177 } 178 179 this(cast(GdkTexture*) __p, true); 180 } 181 182 /** 183 * Downloads the @texture into local memory. 184 * 185 * This may be an expensive operation, as the actual texture data 186 * may reside on a GPU or on a remote display server. 187 * 188 * The data format of the downloaded data is equivalent to 189 * %CAIRO_FORMAT_ARGB32, so every downloaded pixel requires 190 * 4 bytes of memory. 191 * 192 * Downloading a texture into a Cairo image surface: 193 * ```c 194 * surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 195 * gdk_texture_get_width (texture), 196 * gdk_texture_get_height (texture)); 197 * gdk_texture_download (texture, 198 * cairo_image_surface_get_data (surface), 199 * cairo_image_surface_get_stride (surface)); 200 * cairo_surface_mark_dirty (surface); 201 * ``` 202 * 203 * Params: 204 * data = pointer to enough memory to be filled with the 205 * downloaded data of @texture 206 * stride = rowstride in bytes 207 */ 208 public void download(char[] data, size_t stride) 209 { 210 gdk_texture_download(gdkTexture, data.ptr, stride); 211 } 212 213 /** 214 * Returns the height of the @texture, in pixels. 215 * 216 * Returns: the height of the `GdkTexture` 217 */ 218 public int getHeight() 219 { 220 return gdk_texture_get_height(gdkTexture); 221 } 222 223 /** 224 * Returns the width of @texture, in pixels. 225 * 226 * Returns: the width of the `GdkTexture` 227 */ 228 public int getWidth() 229 { 230 return gdk_texture_get_width(gdkTexture); 231 } 232 233 /** 234 * Store the given @texture to the @filename as a PNG file. 235 * 236 * This is a utility function intended for debugging and testing. 237 * If you want more control over formats, proper error handling or 238 * want to store to a `GFile` or other location, you might want to 239 * look into using the gdk-pixbuf library. 240 * 241 * Params: 242 * filename = the filename to store to 243 * 244 * Returns: %TRUE if saving succeeded, %FALSE on failure. 245 */ 246 public bool saveToPng(string filename) 247 { 248 return gdk_texture_save_to_png(gdkTexture, Str.toStringz(filename)) != 0; 249 } 250 }