comparison gtk4/dw.c @ 2315:112595f9a645

GTK4: Remove unnecessary cruft and move pixmap functions into thread safety.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Tue, 16 Feb 2021 05:09:26 +0000
parents eda7642d80df
children 1f85311baa65
comparison
equal deleted inserted replaced
2314:eda7642d80df 2315:112595f9a645
356 }; 356 };
357 357
358 /* 358 /*
359 * List those icons that have transparency first 359 * List those icons that have transparency first
360 */ 360 */
361 #define NUM_EXTS 9 361 static char *_dw_image_exts[] =
362 char *_dw_image_exts[NUM_EXTS+1] =
363 { 362 {
364 ".xpm", 363 ".xpm",
365 ".png", 364 ".png",
366 ".ico", 365 ".ico",
367 ".icns", 366 ".icns",
385 pthread_key_t _dw_bg_color_key; 384 pthread_key_t _dw_bg_color_key;
386 pthread_key_t _dw_event_key; 385 pthread_key_t _dw_event_key;
387 386
388 static int _dw_ignore_expand = 0; 387 static int _dw_ignore_expand = 0;
389 static pthread_t _dw_thread = (pthread_t)-1; 388 static pthread_t _dw_thread = (pthread_t)-1;
390
391 #define DEFAULT_SIZE_WIDTH 12
392 #define DEFAULT_SIZE_HEIGHT 6
393 #define DEFAULT_TITLEBAR_HEIGHT 22
394 389
395 #define _DW_TREE_TYPE_CONTAINER 1 390 #define _DW_TREE_TYPE_CONTAINER 1
396 #define _DW_TREE_TYPE_TREE 2 391 #define _DW_TREE_TYPE_TREE 2
397 #define _DW_TREE_TYPE_LISTBOX 3 392 #define _DW_TREE_TYPE_LISTBOX 3
398 #define _DW_TREE_TYPE_COMBOBOX 4 393 #define _DW_TREE_TYPE_COMBOBOX 4
1852 } 1847 }
1853 1848
1854 /* Convert DW style font to CSS syntax (or Pango for older versions): 1849 /* Convert DW style font to CSS syntax (or Pango for older versions):
1855 * font: font-style font-variant font-weight font-size/line-height font-family 1850 * font: font-style font-variant font-weight font-size/line-height font-family
1856 */ 1851 */
1857 char *_convert_font(const char *font) 1852 char *_dw_convert_font(const char *font)
1858 { 1853 {
1859 char *newfont = NULL; 1854 char *newfont = NULL;
1860 1855
1861 if(font) 1856 if(font)
1862 { 1857 {
1953 DW_FUNCTION_ADD_PARAM2(handle, fontname) 1948 DW_FUNCTION_ADD_PARAM2(handle, fontname)
1954 DW_FUNCTION_RETURN(dw_window_set_font, int) 1949 DW_FUNCTION_RETURN(dw_window_set_font, int)
1955 DW_FUNCTION_RESTORE_PARAM2(handle, HWND, fontname, const char *) 1950 DW_FUNCTION_RESTORE_PARAM2(handle, HWND, fontname, const char *)
1956 { 1951 {
1957 GtkWidget *handle2 = handle; 1952 GtkWidget *handle2 = handle;
1958 char *font = _convert_font(fontname); 1953 char *font = _dw_convert_font(fontname);
1959 gpointer data; 1954 gpointer data;
1960 int retval = DW_ERROR_NONE; 1955 int retval = DW_ERROR_NONE;
1961 1956
1962 if(GTK_IS_SCROLLED_WINDOW(handle)) 1957 if(GTK_IS_SCROLLED_WINDOW(handle))
1963 { 1958 {
3561 DW_FUNCTION_ADD_PARAM3(handle, id, filename) 3556 DW_FUNCTION_ADD_PARAM3(handle, id, filename)
3562 DW_FUNCTION_NO_RETURN(dw_window_set_bitmap) 3557 DW_FUNCTION_NO_RETURN(dw_window_set_bitmap)
3563 DW_FUNCTION_RESTORE_PARAM3(handle, HWND, id, ULONG, filename, const char *) 3558 DW_FUNCTION_RESTORE_PARAM3(handle, HWND, id, ULONG, filename, const char *)
3564 { 3559 {
3565 GdkPixbuf *tmp = NULL; 3560 GdkPixbuf *tmp = NULL;
3566 int found_ext = 0;
3567 int i;
3568 3561
3569 if(id) 3562 if(id)
3570 tmp = _dw_find_pixbuf((HICN)id, NULL, NULL); 3563 tmp = _dw_find_pixbuf((HICN)id, NULL, NULL);
3571 else 3564 else
3572 { 3565 {
3575 strcpy(file, filename); 3568 strcpy(file, filename);
3576 3569
3577 /* check if we can read from this file (it exists and read permission) */ 3570 /* check if we can read from this file (it exists and read permission) */
3578 if(access(file, 04) != 0) 3571 if(access(file, 04) != 0)
3579 { 3572 {
3573 int i = 0;
3574
3580 /* Try with various extentions */ 3575 /* Try with various extentions */
3581 for(i=0; i<NUM_EXTS; i++) 3576 while(_dw_image_exts[i] && !tmp)
3582 { 3577 {
3583 strcpy(file, filename); 3578 strcpy(file, filename);
3584 strcat(file, _dw_image_exts[i]); 3579 strcat(file, _dw_image_exts[i]);
3585 if(access(file, 04) == 0) 3580 if(access(file, 04) == 0)
3586 { 3581 tmp = gdk_pixbuf_new_from_file(file, NULL);
3587 found_ext = 1; 3582 i++;
3588 break;
3589 }
3590 } 3583 }
3591 } 3584 }
3592 if(found_ext) 3585 else
3593 tmp = gdk_pixbuf_new_from_file(file, NULL); 3586 tmp = gdk_pixbuf_new_from_file(file, NULL);
3594 } 3587 }
3595 3588
3596 if(tmp) 3589 if(tmp)
3597 { 3590 {
5077 { 5070 {
5078 return (HICN)id; 5071 return (HICN)id;
5079 } 5072 }
5080 5073
5081 /* Internal function to keep HICNs from getting too big */ 5074 /* Internal function to keep HICNs from getting too big */
5082 GdkPixbuf *_icon_resize(GdkPixbuf *ret) 5075 GdkPixbuf *_dw_icon_resize(GdkPixbuf *ret)
5083 { 5076 {
5084 if(ret) 5077 if(ret)
5085 { 5078 {
5086 int pwidth = gdk_pixbuf_get_width(ret); 5079 int pwidth = gdk_pixbuf_get_width(ret);
5087 int pheight = gdk_pixbuf_get_height(ret); 5080 int pheight = gdk_pixbuf_get_height(ret);
5103 * DW pick the appropriate file extension. 5096 * DW pick the appropriate file extension.
5104 * (ICO on OS/2 or Windows, XPM on Unix) 5097 * (ICO on OS/2 or Windows, XPM on Unix)
5105 */ 5098 */
5106 HICN API dw_icon_load_from_file(const char *filename) 5099 HICN API dw_icon_load_from_file(const char *filename)
5107 { 5100 {
5108 char *file = alloca(strlen(filename) + 6); 5101 HICN retval = 0;
5109 int i, found_ext = 0; 5102
5110 5103 if(filename && *filename)
5111 if (!file) 5104 {
5112 return 0; 5105 char *file = alloca(strlen(filename) + 6);
5113 5106
5114 strcpy(file, filename); 5107 strcpy(file, filename);
5115 5108
5116 /* check if we can read from this file (it exists and read permission) */ 5109 /* check if we can read from this file (it exists and read permission) */
5117 if (access(file, 04) != 0) 5110 if(access(file, 04) != 0)
5118 { 5111 {
5119 /* Try with various extentions */ 5112 int i = 0;
5120 for ( i = 0; i < NUM_EXTS; i++ ) 5113
5121 { 5114 /* Try with various extentions */
5122 strcpy( file, filename ); 5115 while(_dw_image_exts[i] && !retval)
5123 strcat( file, _dw_image_exts[i] );
5124 if ( access( file, 04 ) == 0 )
5125 { 5116 {
5126 found_ext = 1; 5117 strcpy(file, filename);
5127 break; 5118 strcat(file, _dw_image_exts[i]);
5119 if(access(file, 04) == 0)
5120 retval = _dw_icon_resize(gdk_pixbuf_new_from_file(file, NULL));
5121 i++;
5128 } 5122 }
5129 } 5123 }
5130 if ( found_ext == 0 ) 5124 else
5131 { 5125 retval = _dw_icon_resize(gdk_pixbuf_new_from_file(file, NULL));
5132 return 0; 5126 }
5133 } 5127 return retval;
5134 }
5135
5136 return _icon_resize(gdk_pixbuf_new_from_file(file, NULL));
5137 } 5128 }
5138 5129
5139 /* 5130 /*
5140 * Obtains an icon from data. 5131 * Obtains an icon from data.
5141 * Parameters: 5132 * Parameters:
5158 close(fd); 5149 close(fd);
5159 } 5150 }
5160 /* Bail if we couldn't write full file */ 5151 /* Bail if we couldn't write full file */
5161 if(fd == -1 || written != len) 5152 if(fd == -1 || written != len)
5162 return 0; 5153 return 0;
5163 ret = _icon_resize(gdk_pixbuf_new_from_file(template, NULL)); 5154 ret = _dw_icon_resize(gdk_pixbuf_new_from_file(template, NULL));
5164 unlink(template); 5155 unlink(template);
5165 return ret; 5156 return ret;
5166 } 5157 }
5167 5158
5168 /* 5159 /*
6732 * height: Height of the pixmap in pixels. 6723 * height: Height of the pixmap in pixels.
6733 * depth: Color depth of the pixmap. 6724 * depth: Color depth of the pixmap.
6734 * Returns: 6725 * Returns:
6735 * A handle to a pixmap or NULL on failure. 6726 * A handle to a pixmap or NULL on failure.
6736 */ 6727 */
6737 HPIXMAP dw_pixmap_new(HWND handle, unsigned long width, unsigned long height, int depth) 6728 DW_FUNCTION_DEFINITION(dw_pixmap_new, HPIXMAP, HWND handle, unsigned long width, unsigned long height, DW_UNUSED(int depth))
6738 { 6729 DW_FUNCTION_ADD_PARAM4(handle, width, height, depth)
6739 HPIXMAP pixmap; 6730 DW_FUNCTION_RETURN(dw_pixmap_new, HPIXMAP)
6740 6731 DW_FUNCTION_RESTORE_PARAM4(handle, HWND, width, unsigned long, height, unsigned long, DW_UNUSED(depth), int)
6741 if (!(pixmap = calloc(1,sizeof(struct _hpixmap)))) 6732 {
6742 return NULL; 6733 HPIXMAP pixmap = 0;
6743 6734
6744 if (!depth) 6735 if((pixmap = calloc(1,sizeof(struct _hpixmap))))
6745 depth = -1; 6736 {
6746 6737 pixmap->width = width;
6747 pixmap->width = width; pixmap->height = height; 6738 pixmap->height = height;
6748 6739 pixmap->handle = handle;
6749 6740 /* Depth needs to be divided by 3... but for the RGB colorspace...
6750 pixmap->handle = handle; 6741 * only 8 bits per sample is allowed, so to avoid issues just pass 8 for now.
6751 /* Depth needs to be divided by 3... but for the RGB colorspace... 6742 */
6752 * only 8 bits per sample is allowed, so to avoid issues just pass 8 for now. 6743 pixmap->pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, width, height);
6753 */ 6744 pixmap->image = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
6754 pixmap->pixbuf = gdk_pixbuf_new( GDK_COLORSPACE_RGB, FALSE, 8, width, height ); 6745 }
6755 pixmap->image = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height); 6746 DW_FUNCTION_RETURN_THIS(pixmap);
6756 return pixmap;
6757 } 6747 }
6758 6748
6759 /* 6749 /*
6760 * Creates a pixmap from a file. 6750 * Creates a pixmap from a file.
6761 * Parameters: 6751 * Parameters:
6764 * DW pick the appropriate file extension. 6754 * DW pick the appropriate file extension.
6765 * (BMP on OS/2 or Windows, XPM on Unix) 6755 * (BMP on OS/2 or Windows, XPM on Unix)
6766 * Returns: 6756 * Returns:
6767 * A handle to a pixmap or NULL on failure. 6757 * A handle to a pixmap or NULL on failure.
6768 */ 6758 */
6769 HPIXMAP dw_pixmap_new_from_file(HWND handle, const char *filename) 6759 DW_FUNCTION_DEFINITION(dw_pixmap_new_from_file, HPIXMAP, HWND handle, const char *filename)
6770 { 6760 DW_FUNCTION_ADD_PARAM2(handle, filename)
6771 HPIXMAP pixmap; 6761 DW_FUNCTION_RETURN(dw_pixmap_new_from_file, HPIXMAP)
6772 char *file = alloca(strlen(filename) + 6); 6762 DW_FUNCTION_RESTORE_PARAM2(handle, HWND, filename, const char *)
6773 int found_ext = 0; 6763 {
6774 int i; 6764 HPIXMAP pixmap = 0;
6775 6765
6776 if (!file || !(pixmap = calloc(1,sizeof(struct _hpixmap)))) 6766 if(filename && *filename && (pixmap = calloc(1,sizeof(struct _hpixmap))))
6777 return NULL; 6767 {
6778 6768 char *file = alloca(strlen(filename) + 6);
6779 strcpy(file, filename); 6769
6780 6770 strcpy(file, filename);
6781 /* check if we can read from this file (it exists and read permission) */ 6771
6782 if(access(file, 04) != 0) 6772 /* check if we can read from this file (it exists and read permission) */
6783 { 6773 if(access(file, 04) != 0)
6784 /* Try with various extentions */ 6774 {
6785 for ( i = 0; i < NUM_EXTS; i++ ) 6775 int i = 0;
6786 { 6776
6787 strcpy( file, filename ); 6777 /* Try with various extentions */
6788 strcat( file, _dw_image_exts[i] ); 6778 while(_dw_image_exts[i] && !pixmap->pixbuf)
6789 if ( access( file, 04 ) == 0 )
6790 { 6779 {
6791 found_ext = 1; 6780 strcpy(file, filename);
6792 break; 6781 strcat(file, _dw_image_exts[i]);
6782 if(access(file, 04 ) == 0)
6783 pixmap->pixbuf = gdk_pixbuf_new_from_file(file, NULL);
6784 i++;
6793 } 6785 }
6794 } 6786 }
6795 if ( found_ext == 0 ) 6787 else
6788 pixmap->pixbuf = gdk_pixbuf_new_from_file(file, NULL);
6789
6790 if(pixmap->pixbuf)
6791 {
6792 pixmap->image = cairo_image_surface_create_from_png(file);
6793 pixmap->width = gdk_pixbuf_get_width(pixmap->pixbuf);
6794 pixmap->height = gdk_pixbuf_get_height(pixmap->pixbuf);
6795 pixmap->handle = handle;
6796 }
6797 else
6796 { 6798 {
6797 free(pixmap); 6799 free(pixmap);
6798 return NULL; 6800 pixmap = 0;
6799 } 6801 }
6800 } 6802 }
6801 6803 DW_FUNCTION_RETURN_THIS(pixmap);
6802 pixmap->pixbuf = gdk_pixbuf_new_from_file(file, NULL);
6803 pixmap->image = cairo_image_surface_create_from_png(file);
6804 pixmap->width = gdk_pixbuf_get_width(pixmap->pixbuf);
6805 pixmap->height = gdk_pixbuf_get_height(pixmap->pixbuf);
6806 pixmap->handle = handle;
6807 return pixmap;
6808 } 6804 }
6809 6805
6810 /* 6806 /*
6811 * Creates a pixmap from data 6807 * Creates a pixmap from data
6812 * Parameters: 6808 * Parameters:
6815 * DW pick the appropriate file extension. 6811 * DW pick the appropriate file extension.
6816 * (BMP on OS/2 or Windows, XPM on Unix) 6812 * (BMP on OS/2 or Windows, XPM on Unix)
6817 * Returns: 6813 * Returns:
6818 * A handle to a pixmap or NULL on failure. 6814 * A handle to a pixmap or NULL on failure.
6819 */ 6815 */
6820 HPIXMAP dw_pixmap_new_from_data(HWND handle, const char *data, int len) 6816 DW_FUNCTION_DEFINITION(dw_pixmap_new_from_data, HPIXMAP, HWND handle, const char *data, int len)
6821 { 6817 DW_FUNCTION_ADD_PARAM3(handle, data, len)
6822 int fd, written = -1; 6818 DW_FUNCTION_RETURN(dw_pixmap_new_from_data, HPIXMAP)
6823 HPIXMAP pixmap; 6819 DW_FUNCTION_RESTORE_PARAM3(handle, HWND, data, const char *, len, int)
6824 char template[] = "/tmp/dwpixmapXXXXXX"; 6820 {
6825 6821 HPIXMAP pixmap = 0;
6826 if(!data || !(pixmap = calloc(1,sizeof(struct _hpixmap)))) 6822
6827 return NULL; 6823 if(data && len > 0 && (pixmap = calloc(1,sizeof(struct _hpixmap))))
6828 6824 {
6829 /* 6825 int fd, written = -1;
6830 * A real hack; create a temporary file and write the contents 6826 char template[] = "/tmp/dwpixmapXXXXXX";
6831 * of the data to the file 6827
6832 */ 6828 /*
6833 if((fd = mkstemp(template)) != -1) 6829 * A real hack; create a temporary file and write the contents
6834 { 6830 * of the data to the file
6835 written = write(fd, data, len); 6831 */
6836 close(fd); 6832 if((fd = mkstemp(template)) != -1)
6837 } 6833 {
6838 /* Bail if we couldn't write full file */ 6834 written = write(fd, data, len);
6839 if(fd == -1 || written != len) 6835 close(fd);
6840 return 0; 6836 }
6841 pixmap->pixbuf = gdk_pixbuf_new_from_file(template, NULL); 6837 /* Bail if we couldn't write full file */
6842 pixmap->image = cairo_image_surface_create_from_png(template); 6838 if(fd != -1 && written == len)
6843 pixmap->width = gdk_pixbuf_get_width(pixmap->pixbuf); 6839 {
6844 pixmap->height = gdk_pixbuf_get_height(pixmap->pixbuf); 6840 pixmap->pixbuf = gdk_pixbuf_new_from_file(template, NULL);
6845 /* remove our temporary file */ 6841 pixmap->image = cairo_image_surface_create_from_png(template);
6846 unlink(template); 6842 pixmap->width = gdk_pixbuf_get_width(pixmap->pixbuf);
6847 pixmap->handle = handle; 6843 pixmap->height = gdk_pixbuf_get_height(pixmap->pixbuf);
6848 return pixmap; 6844 /* remove our temporary file */
6845 unlink(template);
6846 pixmap->handle = handle;
6847 }
6848 }
6849 DW_FUNCTION_RETURN_THIS(pixmap);
6849 } 6850 }
6850 6851
6851 /* 6852 /*
6852 * Sets the transparent color for a pixmap 6853 * Sets the transparent color for a pixmap
6853 * Parameters: 6854 * Parameters:
6867 * handle: Window handle the pixmap is associated with. 6868 * handle: Window handle the pixmap is associated with.
6868 * id: Resource ID associated with requested pixmap. 6869 * id: Resource ID associated with requested pixmap.
6869 * Returns: 6870 * Returns:
6870 * A handle to a pixmap or NULL on failure. 6871 * A handle to a pixmap or NULL on failure.
6871 */ 6872 */
6872 HPIXMAP dw_pixmap_grab(HWND handle, ULONG id) 6873 DW_FUNCTION_DEFINITION(dw_pixmap_grab, HPIXMAP, HWND handle, ULONG id)
6873 { 6874 DW_FUNCTION_ADD_PARAM2(handle, id)
6874 HPIXMAP pixmap; 6875 DW_FUNCTION_RETURN(dw_pixmap_grab, HPIXMAP)
6875 6876 DW_FUNCTION_RESTORE_PARAM2(handle, HWND, id, ULONG)
6876 if (!(pixmap = calloc(1,sizeof(struct _hpixmap)))) 6877 {
6877 return NULL; 6878 HPIXMAP pixmap = 0;
6878 6879
6879 pixmap->pixbuf = gdk_pixbuf_copy(_dw_find_pixbuf((HICN)id, &pixmap->width, &pixmap->height)); 6880 if((pixmap = calloc(1,sizeof(struct _hpixmap))))
6880 return pixmap; 6881 {
6882 pixmap->pixbuf = gdk_pixbuf_copy(_dw_find_pixbuf((HICN)id, &pixmap->width, &pixmap->height));
6883 pixmap->handle = handle;
6884 }
6885 DW_FUNCTION_RETURN_THIS(pixmap);
6881 } 6886 }
6882 6887
6883 /* Call this after drawing to the screen to make sure 6888 /* Call this after drawing to the screen to make sure
6884 * anything you have drawn is visible. 6889 * anything you have drawn is visible.
6885 */ 6890 */
6902 { 6907 {
6903 if(pixmap) 6908 if(pixmap)
6904 { 6909 {
6905 char *oldfont = pixmap->font; 6910 char *oldfont = pixmap->font;
6906 6911
6907 pixmap->font = _convert_font(fontname); 6912 pixmap->font = _dw_convert_font(fontname);
6908 6913
6909 if(oldfont) 6914 if(oldfont)
6910 free(oldfont); 6915 free(oldfont);
6911 return DW_ERROR_NONE; 6916 return DW_ERROR_NONE;
6912 } 6917 }