Mercurial > dwindows
comparison gtk3/dw.c @ 2200:319eeecb411e
GTK3: Linux complains about tmpnam() so switch to mkstemp().
Will commit similar changes for GTK2 once I determine mkstemp is portable
enough for all platforms.
author | bsmith@81767d24-ef19-dc11-ae90-00e081727c95 |
---|---|
date | Tue, 27 Oct 2020 00:15:46 +0000 |
parents | 8ee74cf778cb |
children | ae6626a4331f |
comparison
equal
deleted
inserted
replaced
2199:8ee74cf778cb | 2200:319eeecb411e |
---|---|
1904 } | 1904 } |
1905 } | 1905 } |
1906 return FALSE; | 1906 return FALSE; |
1907 } | 1907 } |
1908 | 1908 |
1909 static GdkPixbuf *_dw_pixbuf_from_data(char *data) | |
1910 { | |
1911 if(data[0] == 'G' && data[1] == 'd' && data[2] == 'k' && data[3] == 'P') | |
1912 return gdk_pixbuf_new_from_inline(-1, (const guint8 *)data, FALSE, NULL); | |
1913 return gdk_pixbuf_new_from_xpm_data((const char **)data); | |
1914 } | |
1915 | |
1909 static GdkPixbuf *_find_pixbuf(HICN icon, unsigned long *userwidth, unsigned long *userheight) | 1916 static GdkPixbuf *_find_pixbuf(HICN icon, unsigned long *userwidth, unsigned long *userheight) |
1910 { | 1917 { |
1911 char *data = NULL; | 1918 char *data = NULL; |
1912 int z, id = GPOINTER_TO_INT(icon); | 1919 int z, id = GPOINTER_TO_INT(icon); |
1913 | 1920 |
1936 } | 1943 } |
1937 } | 1944 } |
1938 | 1945 |
1939 if(data) | 1946 if(data) |
1940 { | 1947 { |
1941 GdkPixbuf *icon_pixbuf; | 1948 GdkPixbuf *icon_pixbuf = _dw_pixbuf_from_data(data); |
1942 | |
1943 if(data[0] == 'G' && data[1] == 'd' && data[2] == 'k' && data[3] == 'P') | |
1944 icon_pixbuf = gdk_pixbuf_new_from_inline(-1, (const guint8 *)data, FALSE, NULL); | |
1945 else | |
1946 icon_pixbuf = gdk_pixbuf_new_from_xpm_data((const char **)data); | |
1947 | 1949 |
1948 if(userwidth) | 1950 if(userwidth) |
1949 *userwidth = gdk_pixbuf_get_width(icon_pixbuf); | 1951 *userwidth = icon_pixbuf ? gdk_pixbuf_get_width(icon_pixbuf) : 0; |
1950 if(userheight) | 1952 if(userheight) |
1951 *userheight = gdk_pixbuf_get_height(icon_pixbuf); | 1953 *userheight = icon_pixbuf ? gdk_pixbuf_get_height(icon_pixbuf) : 0; |
1952 | 1954 |
1953 return icon_pixbuf; | 1955 return icon_pixbuf; |
1954 } | 1956 } |
1955 return NULL; | 1957 return NULL; |
1956 } | 1958 } |
4202 | 4204 |
4203 DW_MUTEX_LOCK; | 4205 DW_MUTEX_LOCK; |
4204 tmp = gtk_button_new(); | 4206 tmp = gtk_button_new(); |
4205 bitmap = dw_bitmap_new(id); | 4207 bitmap = dw_bitmap_new(id); |
4206 | 4208 |
4207 if ( bitmap ) | 4209 if(bitmap) |
4208 { | 4210 { |
4209 dw_window_set_bitmap_from_data(bitmap, 0, data, len); | 4211 dw_window_set_bitmap_from_data(bitmap, 0, data, len); |
4210 gtk_container_add (GTK_CONTAINER(tmp), bitmap); | 4212 gtk_container_add (GTK_CONTAINER(tmp), bitmap); |
4211 g_object_set_data(G_OBJECT(tmp), "_dw_bitmap", bitmap); | 4213 g_object_set_data(G_OBJECT(tmp), "_dw_bitmap", bitmap); |
4212 } | 4214 } |
4538 */ | 4540 */ |
4539 void dw_window_set_bitmap_from_data(HWND handle, unsigned long id, const char *data, int len) | 4541 void dw_window_set_bitmap_from_data(HWND handle, unsigned long id, const char *data, int len) |
4540 { | 4542 { |
4541 GdkPixbuf *tmp = NULL; | 4543 GdkPixbuf *tmp = NULL; |
4542 int _locked_by_me = FALSE; | 4544 int _locked_by_me = FALSE; |
4543 char *file; | 4545 |
4544 FILE *fp; | 4546 if(!id && !data) |
4545 | |
4546 if (!id && !data) | |
4547 return; | 4547 return; |
4548 | 4548 |
4549 DW_MUTEX_LOCK; | 4549 DW_MUTEX_LOCK; |
4550 if(data) | 4550 if(data) |
4551 { | 4551 { |
4552 /* | 4552 /* |
4553 * A real hack; create a temporary file and write the contents | 4553 * A real hack; create a temporary file and write the contents |
4554 * of the data to the file | 4554 * of the data to the file |
4555 */ | 4555 */ |
4556 file = tmpnam( NULL ); | 4556 char template[] = "/tmp/dwpixmapXXXXXX"; |
4557 fp = fopen( file, "wb" ); | 4557 int fd = mkstemp(template); |
4558 if ( fp ) | 4558 |
4559 { | 4559 if(fd) |
4560 fwrite( data, len, 1, fp ); | 4560 { |
4561 fclose( fp ); | 4561 write(fd, data, len); |
4562 close(fd); | |
4562 } | 4563 } |
4563 else | 4564 else |
4564 { | 4565 { |
4565 DW_MUTEX_UNLOCK; | 4566 DW_MUTEX_UNLOCK; |
4566 return; | 4567 return; |
4567 } | 4568 } |
4568 tmp = gdk_pixbuf_new_from_file(file, NULL ); | 4569 |
4570 tmp = gdk_pixbuf_new_from_file(template, NULL); | |
4569 /* remove our temporary file */ | 4571 /* remove our temporary file */ |
4570 unlink (file ); | 4572 unlink(template); |
4571 } | 4573 } |
4572 else if (id) | 4574 else if (id) |
4573 tmp = _find_pixbuf((HICN)id, NULL, NULL); | 4575 tmp = _find_pixbuf((HICN)id, NULL, NULL); |
4574 | 4576 |
4575 if(tmp) | 4577 if(tmp) |
4576 { | 4578 { |
4577 if ( GTK_IS_BUTTON(handle) ) | 4579 if(GTK_IS_BUTTON(handle)) |
4578 { | 4580 { |
4579 GtkWidget *pixmap = (GtkWidget *)g_object_get_data( G_OBJECT(handle), "_dw_bitmap" ); | 4581 GtkWidget *pixmap = (GtkWidget *)g_object_get_data(G_OBJECT(handle), "_dw_bitmap"); |
4582 | |
4580 if(pixmap) | 4583 if(pixmap) |
4581 { | |
4582 gtk_image_set_from_pixbuf(GTK_IMAGE(pixmap), tmp); | 4584 gtk_image_set_from_pixbuf(GTK_IMAGE(pixmap), tmp); |
4583 } | |
4584 } | 4585 } |
4585 else | 4586 else |
4586 { | |
4587 gtk_image_set_from_pixbuf(GTK_IMAGE(handle), tmp); | 4587 gtk_image_set_from_pixbuf(GTK_IMAGE(handle), tmp); |
4588 } | |
4589 } | 4588 } |
4590 DW_MUTEX_UNLOCK; | 4589 DW_MUTEX_UNLOCK; |
4591 } | 4590 } |
4592 | 4591 |
4593 /* | 4592 /* |
6025 * data: Source of data for image. | 6024 * data: Source of data for image. |
6026 * len: length of data | 6025 * len: length of data |
6027 */ | 6026 */ |
6028 HICN API dw_icon_load_from_data(const char *data, int len) | 6027 HICN API dw_icon_load_from_data(const char *data, int len) |
6029 { | 6028 { |
6030 int _locked_by_me = FALSE; | 6029 int fd, _locked_by_me = FALSE; |
6031 char *file; | 6030 char template[] = "/tmp/dwiconXXXXXX"; |
6032 FILE *fp; | |
6033 HICN ret = 0; | 6031 HICN ret = 0; |
6034 | 6032 |
6035 /* | 6033 /* |
6036 * A real hack; create a temporary file and write the contents | 6034 * A real hack; create a temporary file and write the contents |
6037 * of the data to the file | 6035 * of the data to the file |
6038 */ | 6036 */ |
6039 file = tmpnam( NULL ); | 6037 if((fd = mkstemp(template))) |
6040 fp = fopen( file, "wb" ); | 6038 { |
6041 if ( fp ) | 6039 write(fd, data, len); |
6042 { | 6040 close(fd); |
6043 fwrite( data, len, 1, fp ); | |
6044 fclose( fp ); | |
6045 } | 6041 } |
6046 else | 6042 else |
6047 { | |
6048 return 0; | 6043 return 0; |
6049 } | 6044 DW_MUTEX_LOCK; |
6050 DW_MUTEX_LOCK; | 6045 ret = _icon_resize(gdk_pixbuf_new_from_file(template, NULL)); |
6051 ret = _icon_resize(gdk_pixbuf_new_from_file(file, NULL)); | 6046 DW_MUTEX_UNLOCK; |
6052 DW_MUTEX_UNLOCK; | 6047 unlink(template); |
6053 return ret; | 6048 return ret; |
6054 } | 6049 } |
6055 | 6050 |
6056 /* | 6051 /* |
6057 * Frees a loaded resource in OS/2 and Windows. | 6052 * Frees a loaded resource in OS/2 and Windows. |
7840 * Returns: | 7835 * Returns: |
7841 * A handle to a pixmap or NULL on failure. | 7836 * A handle to a pixmap or NULL on failure. |
7842 */ | 7837 */ |
7843 HPIXMAP dw_pixmap_new_from_data(HWND handle, const char *data, int len) | 7838 HPIXMAP dw_pixmap_new_from_data(HWND handle, const char *data, int len) |
7844 { | 7839 { |
7845 int _locked_by_me = FALSE; | 7840 int fd, _locked_by_me = FALSE; |
7846 char *file; | |
7847 FILE *fp; | |
7848 HPIXMAP pixmap; | 7841 HPIXMAP pixmap; |
7849 | 7842 char template[] = "/tmp/dwpixmapXXXXXX"; |
7850 if (!data || !(pixmap = calloc(1,sizeof(struct _hpixmap)))) | 7843 |
7844 if(!data || !(pixmap = calloc(1,sizeof(struct _hpixmap)))) | |
7851 return NULL; | 7845 return NULL; |
7852 | 7846 |
7853 DW_MUTEX_LOCK; | 7847 DW_MUTEX_LOCK; |
7854 /* | 7848 /* |
7855 * A real hack; create a temporary file and write the contents | 7849 * A real hack; create a temporary file and write the contents |
7856 * of the data to the file | 7850 * of the data to the file |
7857 */ | 7851 */ |
7858 file = tmpnam( NULL ); | 7852 if((fd = mkstemp(template))) |
7859 fp = fopen( file, "wb" ); | 7853 { |
7860 if ( fp ) | 7854 write(fd, data, len); |
7861 { | 7855 close(fd); |
7862 fwrite( data, len, 1, fp ); | |
7863 fclose( fp ); | |
7864 } | 7856 } |
7865 else | 7857 else |
7866 { | 7858 { |
7867 DW_MUTEX_UNLOCK; | 7859 DW_MUTEX_UNLOCK; |
7868 return 0; | 7860 return 0; |
7869 } | 7861 } |
7870 pixmap->pixbuf = gdk_pixbuf_new_from_file(file, NULL); | 7862 pixmap->pixbuf = gdk_pixbuf_new_from_file(template, NULL); |
7871 pixmap->image = cairo_image_surface_create_from_png(file); | 7863 pixmap->image = cairo_image_surface_create_from_png(template); |
7872 pixmap->width = gdk_pixbuf_get_width(pixmap->pixbuf); | 7864 pixmap->width = gdk_pixbuf_get_width(pixmap->pixbuf); |
7873 pixmap->height = gdk_pixbuf_get_height(pixmap->pixbuf); | 7865 pixmap->height = gdk_pixbuf_get_height(pixmap->pixbuf); |
7874 /* remove our temporary file */ | 7866 /* remove our temporary file */ |
7875 unlink (file ); | 7867 unlink(template); |
7876 pixmap->handle = handle; | 7868 pixmap->handle = handle; |
7877 DW_MUTEX_UNLOCK; | 7869 DW_MUTEX_UNLOCK; |
7878 return pixmap; | 7870 return pixmap; |
7879 } | 7871 } |
7880 | 7872 |