Mercurial > dwindows
comparison gtk/dw.c @ 2854:8a5131cbbe93
GTK2/3/4: Add return values to dw_window_set_bitmap(_from_data).
Also similar to the other platforms, no longer install XPMs to gtk/.
GTK4 is currently untested, if it doesn't work, may be a follow-up.
Also fix a GCC fortify warning with strncpy().
author | bsmith@81767d24-ef19-dc11-ae90-00e081727c95 |
---|---|
date | Tue, 15 Nov 2022 02:29:56 +0000 |
parents | ee1cfa7d645e |
children | 86286f528adf |
comparison
equal
deleted
inserted
replaced
2853:c250764b2f32 | 2854:8a5131cbbe93 |
---|---|
4607 bitmap = dw_bitmap_new(id); | 4607 bitmap = dw_bitmap_new(id); |
4608 | 4608 |
4609 if(bitmap) | 4609 if(bitmap) |
4610 { | 4610 { |
4611 dw_window_set_bitmap(bitmap, id, NULL); | 4611 dw_window_set_bitmap(bitmap, id, NULL); |
4612 gtk_container_add (GTK_CONTAINER(tmp), bitmap); | 4612 gtk_container_add(GTK_CONTAINER(tmp), bitmap); |
4613 gtk_object_set_data(GTK_OBJECT(tmp), "_dw_bitmap", bitmap); | 4613 gtk_object_set_data(GTK_OBJECT(tmp), "_dw_bitmap", bitmap); |
4614 } | 4614 } |
4615 gtk_widget_show(tmp); | 4615 gtk_widget_show(tmp); |
4616 _create_tooltip(tmp, text); | 4616 _create_tooltip(tmp, text); |
4617 gtk_object_set_data(GTK_OBJECT(tmp), "_dw_id", GINT_TO_POINTER(id)); | 4617 gtk_object_set_data(GTK_OBJECT(tmp), "_dw_id", GINT_TO_POINTER(id)); |
4638 DW_MUTEX_LOCK; | 4638 DW_MUTEX_LOCK; |
4639 /* Create a new button */ | 4639 /* Create a new button */ |
4640 tmp = gtk_button_new(); | 4640 tmp = gtk_button_new(); |
4641 /* Now on to the image stuff */ | 4641 /* Now on to the image stuff */ |
4642 bitmap = dw_bitmap_new(id); | 4642 bitmap = dw_bitmap_new(id); |
4643 if ( bitmap ) | 4643 if(bitmap) |
4644 { | 4644 { |
4645 dw_window_set_bitmap( bitmap, 0, filename ); | 4645 dw_window_set_bitmap(bitmap, 0, filename); |
4646 gtk_container_add (GTK_CONTAINER(tmp), bitmap); | 4646 gtk_container_add (GTK_CONTAINER(tmp), bitmap); |
4647 gtk_object_set_data(GTK_OBJECT(tmp), "_dw_bitmap", bitmap); | 4647 gtk_object_set_data(GTK_OBJECT(tmp), "_dw_bitmap", bitmap); |
4648 } | 4648 } |
4649 | 4649 |
4650 /* Pack and show all our widgets */ | 4650 /* Pack and show all our widgets */ |
4672 | 4672 |
4673 DW_MUTEX_LOCK; | 4673 DW_MUTEX_LOCK; |
4674 tmp = gtk_button_new(); | 4674 tmp = gtk_button_new(); |
4675 bitmap = dw_bitmap_new(id); | 4675 bitmap = dw_bitmap_new(id); |
4676 | 4676 |
4677 if ( bitmap ) | 4677 if(bitmap) |
4678 { | 4678 { |
4679 dw_window_set_bitmap_from_data(bitmap, 0, data, len); | 4679 dw_window_set_bitmap_from_data(bitmap, 0, data, len); |
4680 gtk_container_add (GTK_CONTAINER(tmp), bitmap); | 4680 gtk_container_add(GTK_CONTAINER(tmp), bitmap); |
4681 gtk_object_set_data(GTK_OBJECT(tmp), "_dw_bitmap", bitmap); | 4681 gtk_object_set_data(GTK_OBJECT(tmp), "_dw_bitmap", bitmap); |
4682 } | 4682 } |
4683 gtk_widget_show(tmp); | 4683 gtk_widget_show(tmp); |
4684 _create_tooltip(tmp, text); | 4684 _create_tooltip(tmp, text); |
4685 gtk_object_set_data(GTK_OBJECT(tmp), "_dw_id", GINT_TO_POINTER(id)); | 4685 gtk_object_set_data(GTK_OBJECT(tmp), "_dw_id", GINT_TO_POINTER(id)); |
4907 * id: An ID to be used to specify the icon, | 4907 * id: An ID to be used to specify the icon, |
4908 * (pass 0 if you use the filename param) | 4908 * (pass 0 if you use the filename param) |
4909 * filename: a path to a file (Bitmap on OS/2 or | 4909 * filename: a path to a file (Bitmap on OS/2 or |
4910 * Windows and a pixmap on Unix, pass | 4910 * Windows and a pixmap on Unix, pass |
4911 * NULL if you use the id param) | 4911 * NULL if you use the id param) |
4912 */ | 4912 * Returns: |
4913 void dw_window_set_bitmap(HWND handle, unsigned long id, const char *filename) | 4913 * DW_ERROR_NONE on success. |
4914 * DW_ERROR_UNKNOWN if the parameters were invalid. | |
4915 * DW_ERROR_GENERAL if the bitmap was unable to be loaded. | |
4916 */ | |
4917 int dw_window_set_bitmap(HWND handle, unsigned long id, const char *filename) | |
4914 { | 4918 { |
4915 #if GTK_MAJOR_VERSION > 1 | 4919 #if GTK_MAJOR_VERSION > 1 |
4916 GdkPixbuf *pixbuf = NULL; | 4920 GdkPixbuf *pixbuf = NULL; |
4917 #else | 4921 #else |
4918 GdkBitmap *bitmap = NULL; | 4922 GdkBitmap *bitmap = NULL; |
4919 GdkPixmap *tmp = NULL; | 4923 GdkPixmap *tmp = NULL; |
4920 #endif | 4924 #endif |
4921 int found_ext = 0; | 4925 int i, found_ext = 0; |
4922 int i; | 4926 int _dw_locked_by_me = FALSE; |
4923 int _dw_locked_by_me = FALSE; | 4927 int retval = DW_ERROR_UNKNOWN; |
4924 | 4928 |
4925 if(!id && !filename) | 4929 if(!id && !filename) |
4926 return; | 4930 return retval; |
4927 | 4931 |
4928 DW_MUTEX_LOCK; | 4932 DW_MUTEX_LOCK; |
4929 if(id) | 4933 if(id) |
4930 #if GTK_MAJOR_VERSION > 1 | 4934 #if GTK_MAJOR_VERSION > 1 |
4931 pixbuf = _dw_find_pixbuf((HICN)id); | 4935 pixbuf = _dw_find_pixbuf((HICN)id); |
4937 char *file = alloca(strlen(filename) + 6); | 4941 char *file = alloca(strlen(filename) + 6); |
4938 #if defined(USE_IMLIB) | 4942 #if defined(USE_IMLIB) |
4939 GdkImlibImage *image; | 4943 GdkImlibImage *image; |
4940 #endif | 4944 #endif |
4941 | 4945 |
4942 if (!file) | 4946 if(!file) |
4943 { | 4947 { |
4944 DW_MUTEX_UNLOCK; | 4948 DW_MUTEX_UNLOCK; |
4945 return; | 4949 return DW_ERROR_GENERAL; |
4946 } | 4950 } |
4947 | 4951 |
4948 strcpy(file, filename); | 4952 strcpy(file, filename); |
4949 | 4953 |
4950 /* check if we can read from this file (it exists and read permission) */ | 4954 /* check if we can read from this file (it exists and read permission) */ |
4951 if ( access(file, 04 ) != 0 ) | 4955 if(access(file, 04 ) != 0) |
4952 { | 4956 { |
4953 /* Try with various extentions */ | 4957 /* Try with various extentions */ |
4954 for ( i = 0; i < NUM_EXTS; i++ ) | 4958 for(i = 0; i < NUM_EXTS; i++) |
4955 { | 4959 { |
4956 strcpy( file, filename ); | 4960 strcpy(file, filename); |
4957 strcat( file, _dw_image_exts[i] ); | 4961 strcat(file, _dw_image_exts[i]); |
4958 if ( access( file, 04 ) == 0 ) | 4962 if(access( file, 04 ) == 0) |
4959 { | 4963 { |
4960 found_ext = 1; | 4964 found_ext = 1; |
4961 break; | 4965 break; |
4962 } | 4966 } |
4963 } | 4967 } |
4964 if ( found_ext == 0 ) | 4968 if(found_ext == 0) |
4965 { | 4969 { |
4966 DW_MUTEX_UNLOCK; | 4970 DW_MUTEX_UNLOCK; |
4967 return; | 4971 return DW_ERROR_GENERAL; |
4968 } | 4972 } |
4969 } | 4973 } |
4970 #if GTK_MAJOR_VERSION > 1 | 4974 #if GTK_MAJOR_VERSION > 1 |
4971 pixbuf = gdk_pixbuf_new_from_file(file, NULL ); | 4975 pixbuf = gdk_pixbuf_new_from_file(file, NULL); |
4972 #elif defined(USE_IMLIB) | 4976 #elif defined(USE_IMLIB) |
4973 image = gdk_imlib_load_image(file); | 4977 image = gdk_imlib_load_image(file); |
4974 gdk_imlib_render(image, image->rgb_width, image->rgb_height); | 4978 gdk_imlib_render(image, image->rgb_width, image->rgb_height); |
4975 tmp = gdk_imlib_copy_image(image); | 4979 tmp = gdk_imlib_copy_image(image); |
4976 bitmap = gdk_imlib_copy_mask(image); | 4980 bitmap = gdk_imlib_copy_mask(image); |
4979 tmp = gdk_pixmap_create_from_xpm(handle->window, &bitmap, &_dw_colors[DW_CLR_PALEGRAY], file); | 4983 tmp = gdk_pixmap_create_from_xpm(handle->window, &bitmap, &_dw_colors[DW_CLR_PALEGRAY], file); |
4980 #endif | 4984 #endif |
4981 } | 4985 } |
4982 | 4986 |
4983 #if GTK_MAJOR_VERSION > 1 | 4987 #if GTK_MAJOR_VERSION > 1 |
4984 if (pixbuf) | 4988 if(pixbuf) |
4985 #else | 4989 #else |
4986 if (tmp) | 4990 if(tmp) |
4987 #endif | 4991 #endif |
4988 { | 4992 { |
4989 if ( GTK_IS_BUTTON(handle) ) | 4993 if(GTK_IS_BUTTON(handle)) |
4990 { | 4994 { |
4991 #if GTK_MAJOR_VERSION > 1 | 4995 #if GTK_MAJOR_VERSION > 1 |
4992 GtkWidget *pixmap = (GtkWidget *)gtk_object_get_data( GTK_OBJECT(handle), "_dw_bitmap" ); | 4996 GtkWidget *pixmap = (GtkWidget *)gtk_object_get_data(GTK_OBJECT(handle), "_dw_bitmap"); |
4993 if(pixmap) | 4997 if(pixmap) |
4994 { | 4998 { |
4995 gtk_image_set_from_pixbuf(GTK_IMAGE(pixmap), pixbuf); | 4999 gtk_image_set_from_pixbuf(GTK_IMAGE(pixmap), pixbuf); |
5000 retval = DW_ERROR_NONE; | |
4996 } | 5001 } |
4997 #else | 5002 #else |
4998 GtkWidget *pixmap = GTK_BUTTON(handle)->child; | 5003 GtkWidget *pixmap = GTK_BUTTON(handle)->child; |
4999 gtk_pixmap_set(GTK_PIXMAP(pixmap), tmp, bitmap); | 5004 gtk_pixmap_set(GTK_PIXMAP(pixmap), tmp, bitmap); |
5000 #endif | 5005 retval = DW_ERROR_NONE; |
5001 } | 5006 #endif |
5002 else | 5007 } |
5003 { | |
5004 #if GTK_MAJOR_VERSION > 1 | 5008 #if GTK_MAJOR_VERSION > 1 |
5009 else if(GTK_IS_IMAGE(handle)) | |
5010 { | |
5005 gtk_image_set_from_pixbuf(GTK_IMAGE(handle), pixbuf); | 5011 gtk_image_set_from_pixbuf(GTK_IMAGE(handle), pixbuf); |
5006 #else | 5012 #else |
5013 else if(GTK_IS_PIXMAP(handle)) | |
5014 { | |
5007 gtk_pixmap_set(GTK_PIXMAP(handle), tmp, bitmap); | 5015 gtk_pixmap_set(GTK_PIXMAP(handle), tmp, bitmap); |
5008 #endif | 5016 #endif |
5017 retval = DW_ERROR_NONE; | |
5009 } | 5018 } |
5010 } | 5019 } |
5011 #if GTK_MAJOR_VERSION > 1 | 5020 #if GTK_MAJOR_VERSION > 1 |
5012 if(pixbuf) | 5021 if(pixbuf) |
5013 g_object_unref(pixbuf); | 5022 g_object_unref(pixbuf); |
5014 #endif | 5023 #endif |
5015 DW_MUTEX_UNLOCK; | 5024 DW_MUTEX_UNLOCK; |
5025 return retval; | |
5016 } | 5026 } |
5017 | 5027 |
5018 /* | 5028 /* |
5019 * Sets the bitmap used for a given static window. | 5029 * Sets the bitmap used for a given static window. |
5020 * Parameters: | 5030 * Parameters: |
5023 * (pass 0 if you use the filename param) | 5033 * (pass 0 if you use the filename param) |
5024 * data: the image data | 5034 * data: the image data |
5025 * Bitmap on Windows and a pixmap on Unix, pass | 5035 * Bitmap on Windows and a pixmap on Unix, pass |
5026 * NULL if you use the id param) | 5036 * NULL if you use the id param) |
5027 * len: length of data | 5037 * len: length of data |
5028 */ | 5038 * Returns: |
5029 void dw_window_set_bitmap_from_data(HWND handle, unsigned long id, const char *data, int len) | 5039 * DW_ERROR_NONE on success. |
5040 * DW_ERROR_UNKNOWN if the parameters were invalid. | |
5041 * DW_ERROR_GENERAL if the bitmap was unable to be loaded. | |
5042 */ | |
5043 int dw_window_set_bitmap_from_data(HWND handle, unsigned long id, const char *data, int len) | |
5030 { | 5044 { |
5031 #if GTK_MAJOR_VERSION > 1 | 5045 #if GTK_MAJOR_VERSION > 1 |
5032 GdkPixbuf *pixbuf = NULL; | 5046 GdkPixbuf *pixbuf = NULL; |
5033 #else | 5047 #else |
5034 GdkBitmap *bitmap = NULL; | 5048 GdkBitmap *bitmap = NULL; |
5035 GdkPixmap *tmp = NULL; | 5049 GdkPixmap *tmp = NULL; |
5036 #endif | 5050 #endif |
5037 int _dw_locked_by_me = FALSE; | 5051 int _dw_locked_by_me = FALSE; |
5052 int retval = DW_ERROR_UNKNOWN; | |
5038 | 5053 |
5039 if(!id && !data) | 5054 if(!id && !data) |
5040 return; | 5055 return retval; |
5041 | 5056 |
5042 DW_MUTEX_LOCK; | 5057 DW_MUTEX_LOCK; |
5043 if(data) | 5058 if(data) |
5044 { | 5059 { |
5045 #if defined(USE_IMLIB) | 5060 #if defined(USE_IMLIB) |
5059 } | 5074 } |
5060 /* Bail if we couldn't write full file */ | 5075 /* Bail if we couldn't write full file */ |
5061 if(fd == -1 || written != len) | 5076 if(fd == -1 || written != len) |
5062 { | 5077 { |
5063 DW_MUTEX_UNLOCK; | 5078 DW_MUTEX_UNLOCK; |
5064 return; | 5079 return DW_ERROR_GENERAL; |
5065 } | 5080 } |
5066 #if GTK_MAJOR_VERSION > 1 | 5081 #if GTK_MAJOR_VERSION > 1 |
5067 pixbuf = gdk_pixbuf_new_from_file(template, NULL); | 5082 pixbuf = gdk_pixbuf_new_from_file(template, NULL); |
5068 #elif defined(USE_IMLIB) | 5083 #elif defined(USE_IMLIB) |
5069 image = gdk_imlib_load_image(template); | 5084 image = gdk_imlib_load_image(template); |
5075 tmp = gdk_pixmap_create_from_xpm_d(handle->window, &bitmap, &_dw_colors[DW_CLR_PALEGRAY], mydata); | 5090 tmp = gdk_pixmap_create_from_xpm_d(handle->window, &bitmap, &_dw_colors[DW_CLR_PALEGRAY], mydata); |
5076 #endif | 5091 #endif |
5077 /* remove our temporary file */ | 5092 /* remove our temporary file */ |
5078 unlink(template); | 5093 unlink(template); |
5079 } | 5094 } |
5080 else if (id) | 5095 else if(id) |
5081 #if GTK_MAJOR_VERSION > 1 | 5096 #if GTK_MAJOR_VERSION > 1 |
5082 pixbuf = _dw_find_pixbuf((HICN)id); | 5097 pixbuf = _dw_find_pixbuf((HICN)id); |
5083 #else | 5098 #else |
5084 tmp = _dw_find_pixmap(&bitmap, (HICN)id, handle, NULL, NULL); | 5099 tmp = _dw_find_pixmap(&bitmap, (HICN)id, handle, NULL, NULL); |
5085 #endif | 5100 #endif |
5086 | 5101 |
5087 #if GTK_MAJOR_VERSION > 1 | 5102 #if GTK_MAJOR_VERSION > 1 |
5088 if (pixbuf) | 5103 if(pixbuf) |
5089 #else | 5104 #else |
5090 if (tmp) | 5105 if(tmp) |
5091 #endif | 5106 #endif |
5092 { | 5107 { |
5093 if ( GTK_IS_BUTTON(handle) ) | 5108 if(GTK_IS_BUTTON(handle)) |
5094 { | 5109 { |
5095 #if GTK_MAJOR_VERSION > 1 | 5110 #if GTK_MAJOR_VERSION > 1 |
5096 GtkWidget *pixmap = (GtkWidget *)gtk_object_get_data( GTK_OBJECT(handle), "_dw_bitmap" ); | 5111 GtkWidget *pixmap = (GtkWidget *)gtk_object_get_data( GTK_OBJECT(handle), "_dw_bitmap" ); |
5097 if(pixmap) | 5112 if(pixmap) |
5098 { | 5113 { |
5099 gtk_image_set_from_pixbuf(GTK_IMAGE(pixmap), pixbuf); | 5114 gtk_image_set_from_pixbuf(GTK_IMAGE(pixmap), pixbuf); |
5115 retval = DW_ERROR_NONE; | |
5100 } | 5116 } |
5101 #else | 5117 #else |
5102 GtkWidget *pixmap = GTK_BUTTON(handle)->child; | 5118 GtkWidget *pixmap = GTK_BUTTON(handle)->child; |
5103 gtk_pixmap_set(GTK_PIXMAP(pixmap), tmp, bitmap); | 5119 gtk_pixmap_set(GTK_PIXMAP(pixmap), tmp, bitmap); |
5104 #endif | 5120 retval = DW_ERROR_NONE; |
5105 } | 5121 #endif |
5106 else | 5122 } |
5107 { | |
5108 #if GTK_MAJOR_VERSION > 1 | 5123 #if GTK_MAJOR_VERSION > 1 |
5124 else if(GTK_IS_IMAGE(handle)) | |
5125 { | |
5109 gtk_image_set_from_pixbuf(GTK_IMAGE(handle), pixbuf); | 5126 gtk_image_set_from_pixbuf(GTK_IMAGE(handle), pixbuf); |
5110 #else | 5127 #else |
5128 else if(GTK_IS_PIXMAP(handle)) | |
5129 { | |
5111 gtk_pixmap_set(GTK_PIXMAP(handle), tmp, bitmap); | 5130 gtk_pixmap_set(GTK_PIXMAP(handle), tmp, bitmap); |
5112 #endif | 5131 #endif |
5132 retval = DW_ERROR_NONE; | |
5113 } | 5133 } |
5114 } | 5134 } |
5115 #if GTK_MAJOR_VERSION > 1 | 5135 #if GTK_MAJOR_VERSION > 1 |
5116 if(pixbuf) | 5136 if(pixbuf) |
5117 g_object_unref(pixbuf); | 5137 g_object_unref(pixbuf); |
5118 #endif | 5138 #endif |
5119 DW_MUTEX_UNLOCK; | 5139 DW_MUTEX_UNLOCK; |
5140 return retval; | |
5120 } | 5141 } |
5121 | 5142 |
5122 /* | 5143 /* |
5123 * Sets the text used for a given window. | 5144 * Sets the text used for a given window. |
5124 * Parameters: | 5145 * Parameters: |