comparison gtk/dw.c @ 2204:b86ae2abb3b3

GTK: Added GTK2 support for GResource embedded images. Removed the App ID from the resource namespace. I assume the namespace is per process and this should be sufficient, but can revisit adding the application name or application ID to the resource namespace later.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Sun, 29 Nov 2020 05:29:11 +0000
parents ae6626a4331f
children a43a3f80ed32
comparison
equal deleted inserted replaced
2203:12fb7f32f3a9 2204:b86ae2abb3b3
57 #include "gtk/messagebox_error.xpm" 57 #include "gtk/messagebox_error.xpm"
58 #include "gtk/messagebox_warning.xpm" 58 #include "gtk/messagebox_warning.xpm"
59 #include "gtk/messagebox_information.xpm" 59 #include "gtk/messagebox_information.xpm"
60 #include "gtk/messagebox_question.xpm" 60 #include "gtk/messagebox_question.xpm"
61 61
62 /* These are used for resource management */ 62 /* These are used for legacy resource management
63 * Starting with Dynamic Windows 3.1 the default
64 * compiled in resource management is GResource.
65 */
66 #ifdef DW_INCLUDE_DEPRECATED_RESOURCES
63 #if defined(DW_RESOURCES) && !defined(BUILD_DLL) 67 #if defined(DW_RESOURCES) && !defined(BUILD_DLL)
64 extern DWResources _resources; 68 extern DWResources _resources;
69 #endif
65 #endif 70 #endif
66 71
67 GdkColor _colors[] = 72 GdkColor _colors[] =
68 { 73 {
69 { 0, 0x0000, 0x0000, 0x0000 }, /* 0 black */ 74 { 0, 0x0000, 0x0000, 0x0000 }, /* 0 black */
1843 return _PixmapArray[id].pixmap; 1848 return _PixmapArray[id].pixmap;
1844 } 1849 }
1845 return NULL; 1850 return NULL;
1846 } 1851 }
1847 1852
1853 #if GTK_MAJOR_VERSION > 1
1854 #ifdef DW_INCLUDE_DEPRECATED_RESOURCES
1855 static GdkPixbuf *_dw_pixbuf_from_data(char *data)
1856 {
1857 if(data[0] == 'G' && data[1] == 'd' && data[2] == 'k' && data[3] == 'P')
1858 return gdk_pixbuf_new_from_inline(-1, (const guint8 *)data, FALSE, NULL);
1859 return gdk_pixbuf_new_from_xpm_data((const char **)data);
1860 }
1861 #endif
1862
1863 static GdkPixbuf *_dw_pixbuf_from_resource(unsigned int rid)
1864 {
1865 #if GLIB_CHECK_VERSION(2,32,0)
1866 char resource_path[201] = {0};
1867 snprintf(resource_path, 200, "/org/dbsoft/dwindows/%u", rid);
1868 return gdk_pixbuf_new_from_resource(resource_path, NULL);
1869 #else
1870 return NULL;
1871 #endif
1872 }
1873 #endif
1874
1848 static GdkPixmap *_find_pixmap(GdkBitmap **bitmap, HICN icon, HWND handle, unsigned long *userwidth, unsigned long *userheight) 1875 static GdkPixmap *_find_pixmap(GdkBitmap **bitmap, HICN icon, HWND handle, unsigned long *userwidth, unsigned long *userheight)
1849 { 1876 {
1850 char *data = NULL; 1877 unsigned int id = GPOINTER_TO_INT(icon);
1851 int z, id = GPOINTER_TO_INT(icon); 1878 #if GTK_MAJOR_VERSION > 1
1879 GdkPixbuf *icon_pixbuf = NULL;
1880 #endif
1881 GdkPixmap *icon_pixmap = NULL;
1852 1882
1853 /* Quick dropout for non-handle */ 1883 /* Quick dropout for non-handle */
1854 if(!icon) 1884 if(!icon)
1855 return NULL; 1885 return NULL;
1856 1886
1857 if(id & (1 << 31)) 1887 if(id & (1 << 31))
1858 return _find_private_pixmap(bitmap, GINT_TO_POINTER((id & 0xFFFFFF)), userwidth, userheight); 1888 return _find_private_pixmap(bitmap, GINT_TO_POINTER((id & 0xFFFFFF)), userwidth, userheight);
1859 1889
1860 for(z=0;z<_resources.resource_max;z++)
1861 {
1862 if(_resources.resource_id[z] == id)
1863 {
1864 data = _resources.resource_data[z];
1865 break;
1866 }
1867 }
1868
1869 if(data)
1870 {
1871 GdkPixmap *icon_pixmap = NULL;
1872 #if GTK_MAJOR_VERSION > 1 1890 #if GTK_MAJOR_VERSION > 1
1873 GdkPixbuf *icon_pixbuf; 1891 icon_pixbuf = _dw_pixbuf_from_resource(id);
1874 1892 #endif
1875 if(data[0] == 'G' && data[1] == 'd' && data[2] == 'k' && data[3] == 'P') 1893
1876 icon_pixbuf = gdk_pixbuf_new_from_inline(-1, (const guint8 *)data, FALSE, NULL); 1894 #ifdef DW_INCLUDE_DEPRECATED_RESOURCES
1877 else 1895 #if GTK_MAJOR_VERSION > 1
1878 icon_pixbuf = gdk_pixbuf_new_from_xpm_data((const char **)data); 1896 if(!icon_pixbuf)
1879 1897 #endif
1880 if(userwidth) 1898 {
1881 *userwidth = gdk_pixbuf_get_width(icon_pixbuf); 1899 char *data = NULL;
1882 if(userheight) 1900 int z;
1883 *userheight = gdk_pixbuf_get_height(icon_pixbuf); 1901
1884 1902 for(z=0;z<_resources.resource_max;z++)
1903 {
1904 if(_resources.resource_id[z] == id)
1905 {
1906 data = _resources.resource_data[z];
1907 break;
1908 }
1909 }
1910
1911 if(data)
1912 {
1913 #if GTK_MAJOR_VERSION > 1
1914 icon_pixbuf = _dw_pixbuf_from_data(data);
1915 #elif defined(USE_IMLIB)
1916 gdk_imlib_data_to_pixmap((char **)data, &icon_pixmap, bitmap);
1917 #else
1918 icon_pixmap = gdk_pixmap_create_from_xpm_d(handle->window, bitmap, &_colors[DW_CLR_PALEGRAY], (char **)data);
1919 #endif
1920 }
1921 }
1922 #endif
1923 #if GTK_MAJOR_VERSION > 1
1924 if(icon_pixbuf)
1925 {
1885 gdk_pixbuf_render_pixmap_and_mask(icon_pixbuf, &icon_pixmap, bitmap, 1); 1926 gdk_pixbuf_render_pixmap_and_mask(icon_pixbuf, &icon_pixmap, bitmap, 1);
1886 g_object_unref(icon_pixbuf); 1927 g_object_unref(icon_pixbuf);
1887 #elif defined(USE_IMLIB) 1928 }
1888 gdk_imlib_data_to_pixmap((char **)data, &icon_pixmap, bitmap); 1929 if(userwidth)
1930 *userwidth = icon_pixbuf ? gdk_pixbuf_get_width(icon_pixbuf) : 0;
1931 if(userheight)
1932 *userheight = icon_pixbuf ? gdk_pixbuf_get_height(icon_pixbuf) : 0;
1889 #else 1933 #else
1890 icon_pixmap = gdk_pixmap_create_from_xpm_d(handle->window, bitmap, &_colors[DW_CLR_PALEGRAY], (char **)data); 1934 if(icon_pixmap)
1891 #endif 1935 {
1892 return icon_pixmap; 1936 gint width = 0, height = 0;
1893 } 1937
1894 return NULL; 1938 #if GTK_CHECK_VERSION(2,24,0)
1939 gdk_pixmap_get_size(icon_pixmap, &width, &height);
1940 #endif
1941 if(userwidth)
1942 *userwidth = (unsigned long)width;
1943 if(userheight)
1944 *userheight = (unsigned long)height;
1945 }
1946 #endif
1947 return icon_pixmap;
1895 } 1948 }
1896 1949
1897 #if GTK_MAJOR_VERSION > 1 1950 #if GTK_MAJOR_VERSION > 1
1898 static GdkPixbuf *_find_private_pixbuf(HICN icon) 1951 static GdkPixbuf *_find_private_pixbuf(HICN icon)
1899 { 1952 {
1904 return NULL; 1957 return NULL;
1905 } 1958 }
1906 1959
1907 static GdkPixbuf *_find_pixbuf(HICN icon) 1960 static GdkPixbuf *_find_pixbuf(HICN icon)
1908 { 1961 {
1909 char *data = NULL; 1962 unsigned int id = GPOINTER_TO_INT(icon);
1910 int z, id = GPOINTER_TO_INT(icon); 1963 GdkPixbuf *icon_pixbuf;
1911 1964
1912 /* Quick dropout for non-handle */ 1965 /* Quick dropout for non-handle */
1913 if(!icon) 1966 if(!icon)
1914 return NULL; 1967 return NULL;
1915 1968
1916 if(id & (1 << 31)) 1969 if(id & (1 << 31))
1917 return _find_private_pixbuf(GINT_TO_POINTER((id & 0xFFFFFF))); 1970 return _find_private_pixbuf(GINT_TO_POINTER((id & 0xFFFFFF)));
1918 1971
1919 for(z=0;z<_resources.resource_max;z++) 1972 if((icon_pixbuf = _dw_pixbuf_from_resource(id)))
1920 { 1973 return icon_pixbuf;
1921 if(_resources.resource_id[z] == id) 1974 else
1922 { 1975 {
1923 data = _resources.resource_data[z]; 1976 #ifdef DW_INCLUDE_DEPRECATED_RESOURCES
1924 break; 1977 char *data = NULL;
1925 } 1978 int z;
1926 } 1979
1927 1980 for(z=0;z<_resources.resource_max;z++)
1928 if(data) 1981 {
1929 { 1982 if(_resources.resource_id[z] == id)
1930 if(data[0] == 'G' && data[1] == 'd' && data[2] == 'k' && data[3] == 'P') 1983 {
1931 return gdk_pixbuf_new_from_inline(-1, (const guint8 *)data, FALSE, NULL); 1984 data = _resources.resource_data[z];
1932 return gdk_pixbuf_new_from_xpm_data((const char **)data); 1985 break;
1986 }
1987 }
1988
1989 if(data)
1990 return _dw_pixbuf_from_data(data);
1991 #endif
1933 } 1992 }
1934 return NULL; 1993 return NULL;
1935 } 1994 }
1936 #endif 1995 #endif
1937 1996
2008 * quell the warning and can be used at a later point if needed. 2067 * quell the warning and can be used at a later point if needed.
2009 */ 2068 */
2010 } 2069 }
2011 #endif 2070 #endif
2012 2071
2072 static char * _dw_test_xpm[] = {
2073 "1 1 1 1",
2074 " c None",
2075 " "};
2076
2013 /* 2077 /*
2014 * Initializes the Dynamic Windows engine. 2078 * Initializes the Dynamic Windows engine.
2015 * Parameters: 2079 * Parameters:
2016 * newthread: True if this is the only thread. 2080 * newthread: True if this is the only thread.
2017 * False if there is already a message loop running. 2081 * False if there is already a message loop running.
2018 */ 2082 */
2019 int dw_int_init(DWResources *res, int newthread, int *argc, char **argv[]) 2083 #ifdef DW_INCLUDE_DEPRECATED_RESOURCES
2020 { 2084 int dw_int_init(DWResources *res, int newthread, int *pargc, char **pargv[])
2021 int z; 2085 {
2022 char *fname; 2086 int z, argc = pargc ? *pargc : 0;
2023 static char * test_xpm[] = { 2087 char *fname, **argv = pargv ? *pargv : NULL;
2024 "1 1 1 1",
2025 " c None",
2026 " "};
2027 2088
2028 if(res) 2089 if(res)
2029 { 2090 {
2030 _resources.resource_max = res->resource_max; 2091 _resources.resource_max = res->resource_max;
2031 _resources.resource_id = res->resource_id; 2092 _resources.resource_id = res->resource_id;
2032 _resources.resource_data = res->resource_data; 2093 _resources.resource_data = res->resource_data;
2033 } 2094 }
2095 #else
2096 int dw_init(int newthread, int argc, char *argv[])
2097 {
2098 int z;
2099 char *fname;
2100 #endif
2034 2101
2035 /* Setup the private data directory */ 2102 /* Setup the private data directory */
2036 if(argc && argv && *argc > 0 && (*argv)[0]) 2103 if(argc > 0 && argv[0])
2037 { 2104 {
2038 char *pathcopy = strdup((*argv)[0]); 2105 char *pathcopy = strdup(argv[0]);
2039 char *pos = strrchr(pathcopy, '/'); 2106 char *pos = strrchr(pathcopy, '/');
2040 char *binname = pathcopy; 2107 char *binname = pathcopy;
2041 2108
2042 /* If we have a / then... 2109 /* If we have a / then...
2043 * the binary name should be at the end. 2110 * the binary name should be at the end.
2087 gdk_threads_set_lock_functions(G_CALLBACK(_dw_gdk_lock_enter), G_CALLBACK(_dw_gdk_lock_leave)); 2154 gdk_threads_set_lock_functions(G_CALLBACK(_dw_gdk_lock_enter), G_CALLBACK(_dw_gdk_lock_leave));
2088 2155
2089 gdk_threads_init(); 2156 gdk_threads_init();
2090 #endif 2157 #endif
2091 2158
2092 gtk_init(argc, argv); 2159 gtk_init(&argc, &argv);
2093 #ifdef USE_IMLIB 2160 #ifdef USE_IMLIB
2094 gdk_imlib_init(); 2161 gdk_imlib_init();
2095 #endif 2162 #endif
2096 /* Add colors to the system colormap */ 2163 /* Add colors to the system colormap */
2097 _dw_cmap = gdk_colormap_get_system(); 2164 _dw_cmap = gdk_colormap_get_system();
2109 /* Create place holder pixmap/bitmap when one is needed by the API... 2176 /* Create place holder pixmap/bitmap when one is needed by the API...
2110 * but we don't really want to display anything. 2177 * but we don't really want to display anything.
2111 */ 2178 */
2112 #if GTK_MAJOR_VERSION > 1 2179 #if GTK_MAJOR_VERSION > 1
2113 { 2180 {
2114 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_xpm_data((const char **)test_xpm); 2181 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_xpm_data((const char **)_dw_test_xpm);
2115 2182
2116 gdk_pixbuf_render_pixmap_and_mask(pixbuf, &_dw_tmppixmap, &_dw_tmpbitmap, 1); 2183 gdk_pixbuf_render_pixmap_and_mask(pixbuf, &_dw_tmppixmap, &_dw_tmpbitmap, 1);
2117 g_object_unref(pixbuf); 2184 g_object_unref(pixbuf);
2118 } 2185 }
2119 #elif defined(USE_IMLIB) 2186 #elif defined(USE_IMLIB)
2120 gdk_imlib_data_to_pixmap((char **)test_xpm, &_dw_tmppixmap, &_dw_tmpbitmap); 2187 gdk_imlib_data_to_pixmap((char **)_dw_test_xpm, &_dw_tmppixmap, &_dw_tmpbitmap);
2121 #else 2188 #else
2122 { 2189 {
2123 GtkWidget *handle = gtk_label_new(""); 2190 GtkWidget *handle = gtk_label_new("");
2124 gtk_widget_realize(handle); 2191 gtk_widget_realize(handle);
2125 _dw_tmppixmap = gdk_pixmap_create_from_xpm_d(handle->window, &_dw_tmpbitmap, &_colors[DW_CLR_PALEGRAY], (char **)test_xpm); 2192 _dw_tmppixmap = gdk_pixmap_create_from_xpm_d(handle->window, &_dw_tmpbitmap, &_colors[DW_CLR_PALEGRAY], (char **)_dw_test_xpm);
2126 gtk_widget_destroy(handle); 2193 gtk_widget_destroy(handle);
2127 } 2194 }
2128 #endif 2195 #endif
2129 2196
2130 /* 2197 /*