comparison gtk3/dw.c @ 2202:c677d728e143

GTK3: Switch to using GResource for compiled in image resources. Second commit with similar changes coming for GTK2 soon. dwindows-config --gresource will return "enabled" if GResource is supported. GLib 2.32 is required for GResource support, if GLib is older than 2.32 Dynamic Windows will fall back to the old style compiled in resource array.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Sat, 28 Nov 2020 23:47:30 +0000
parents ae6626a4331f
children b86ae2abb3b3
comparison
equal deleted inserted replaced
2201:ae6626a4331f 2202:c677d728e143
54 # else 54 # else
55 # define __func__ "<unknown>" 55 # define __func__ "<unknown>"
56 # endif 56 # endif
57 #endif 57 #endif
58 58
59 /* These are used for resource management */ 59 /* These are used for legacy resource management
60 * Starting with Dynamic Windows 3.1 the default
61 * compiled in resource management is GResource.
62 */
63 #ifdef DW_INCLUDE_DEPRECATED_RESOURCES
60 #if defined(DW_RESOURCES) && !defined(BUILD_DLL) 64 #if defined(DW_RESOURCES) && !defined(BUILD_DLL)
61 extern DWResources _resources; 65 extern DWResources _resources;
66 #endif
62 #endif 67 #endif
63 68
64 /* ff = 255 = 1.0000 69 /* ff = 255 = 1.0000
65 * ee = 238 = 0.9333 70 * ee = 238 = 0.9333
66 * cc = 204 = 0.8000 71 * cc = 204 = 0.8000
1904 } 1909 }
1905 } 1910 }
1906 return FALSE; 1911 return FALSE;
1907 } 1912 }
1908 1913
1914 #ifdef DW_INCLUDE_DEPRECATED_RESOURCES
1909 static GdkPixbuf *_dw_pixbuf_from_data(char *data) 1915 static GdkPixbuf *_dw_pixbuf_from_data(char *data)
1910 { 1916 {
1911 if(data[0] == 'G' && data[1] == 'd' && data[2] == 'k' && data[3] == 'P') 1917 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); 1918 return gdk_pixbuf_new_from_inline(-1, (const guint8 *)data, FALSE, NULL);
1913 return gdk_pixbuf_new_from_xpm_data((const char **)data); 1919 return gdk_pixbuf_new_from_xpm_data((const char **)data);
1914 } 1920 }
1921 #endif
1922
1923 static GdkPixbuf *_dw_pixbuf_from_resource(unsigned int rid)
1924 {
1925 #if GLIB_CHECK_VERSION(2,32,0)
1926 char resource_path[201] = {0};
1927 snprintf(resource_path, 200, "/org/dbsoft/dwindows/%s/%u", _dw_app_id, rid);
1928 return gdk_pixbuf_new_from_resource(resource_path, NULL);
1929 #else
1930 return NULL;
1931 #endif
1932 }
1915 1933
1916 static GdkPixbuf *_find_pixbuf(HICN icon, unsigned long *userwidth, unsigned long *userheight) 1934 static GdkPixbuf *_find_pixbuf(HICN icon, unsigned long *userwidth, unsigned long *userheight)
1917 { 1935 {
1918 char *data = NULL; 1936 unsigned int id = GPOINTER_TO_INT(icon);
1919 int z, id = GPOINTER_TO_INT(icon); 1937 GdkPixbuf *icon_pixbuf = NULL;
1920 1938
1921 /* Quick dropout for non-handle */ 1939 /* Quick dropout for non-handle */
1922 if(!icon) 1940 if(!icon)
1923 return NULL; 1941 return NULL;
1924 1942
1925 if(id > 65535) 1943 if(id > 65535)
1926 { 1944 icon_pixbuf = icon;
1927 GdkPixbuf *icon_pixbuf = icon; 1945 else
1928 1946 icon_pixbuf = _dw_pixbuf_from_resource(id);
1929 if(userwidth) 1947
1930 *userwidth = gdk_pixbuf_get_width(icon_pixbuf); 1948 #ifdef DW_INCLUDE_DEPRECATED_RESOURCES
1931 if(userheight) 1949 if(!icon_pixbuf)
1932 *userheight = gdk_pixbuf_get_height(icon_pixbuf); 1950 {
1933 1951 char *data = NULL;
1934 return icon; 1952 int z;
1935 } 1953
1936 1954 for(z=0;z<_resources.resource_max;z++)
1937 for(z=0;z<_resources.resource_max;z++) 1955 {
1938 { 1956 if(_resources.resource_id[z] == id)
1939 if(_resources.resource_id[z] == id) 1957 {
1940 { 1958 data = _resources.resource_data[z];
1941 data = _resources.resource_data[z]; 1959 break;
1942 break; 1960 }
1943 } 1961 }
1944 } 1962
1945 1963 if(data)
1946 if(data) 1964 icon_pixbuf = _dw_pixbuf_from_data(data);
1947 { 1965 }
1948 GdkPixbuf *icon_pixbuf = _dw_pixbuf_from_data(data); 1966 #endif
1949 1967 if(userwidth)
1950 if(userwidth) 1968 *userwidth = icon_pixbuf ? gdk_pixbuf_get_width(icon_pixbuf) : 0;
1951 *userwidth = icon_pixbuf ? gdk_pixbuf_get_width(icon_pixbuf) : 0; 1969 if(userheight)
1952 if(userheight) 1970 *userheight = icon_pixbuf ? gdk_pixbuf_get_height(icon_pixbuf) : 0;
1953 *userheight = icon_pixbuf ? gdk_pixbuf_get_height(icon_pixbuf) : 0; 1971
1954 1972 return icon_pixbuf;
1955 return icon_pixbuf;
1956 }
1957 return NULL;
1958 } 1973 }
1959 1974
1960 #if GLIB_CHECK_VERSION(2,40,0) 1975 #if GLIB_CHECK_VERSION(2,40,0)
1961 /* Handle system notification click callbacks */ 1976 /* Handle system notification click callbacks */
1962 static void _dw_notification_handler(GSimpleAction *action, GVariant *param, gpointer user_data) 1977 static void _dw_notification_handler(GSimpleAction *action, GVariant *param, gpointer user_data)
1991 * Initializes the Dynamic Windows engine. 2006 * Initializes the Dynamic Windows engine.
1992 * Parameters: 2007 * Parameters:
1993 * newthread: True if this is the only thread. 2008 * newthread: True if this is the only thread.
1994 * False if there is already a message loop running. 2009 * False if there is already a message loop running.
1995 */ 2010 */
1996 int dw_int_init(DWResources *res, int newthread, int *argc, char **argv[]) 2011 #ifdef DW_INCLUDE_DEPRECATED_RESOURCES
1997 { 2012 int dw_int_init(DWResources *res, int newthread, int *pargc, char **pargv[])
2013 {
2014 int argc = pargc ? *pargc : 0;
2015 char **argv = pargv ? *pargv : NULL;
2016
1998 if(res) 2017 if(res)
1999 { 2018 {
2000 _resources.resource_max = res->resource_max; 2019 _resources.resource_max = res->resource_max;
2001 _resources.resource_id = res->resource_id; 2020 _resources.resource_id = res->resource_id;
2002 _resources.resource_data = res->resource_data; 2021 _resources.resource_data = res->resource_data;
2003 } 2022 }
2004 2023 #else
2024 int dw_init(int newthread, int argc, char *argv[])
2025 {
2026 #endif
2005 /* Setup the private data directory */ 2027 /* Setup the private data directory */
2006 if(argc && argv && *argc > 0 && (*argv)[0]) 2028 if(argc > 0 && argv[0])
2007 { 2029 {
2008 char *pathcopy = strdup((*argv)[0]); 2030 char *pathcopy = strdup(argv[0]);
2009 char *pos = strrchr(pathcopy, '/'); 2031 char *pos = strrchr(pathcopy, '/');
2010 char *binname = pathcopy; 2032 char *binname = pathcopy;
2011 2033
2012 /* If we have a / then... 2034 /* If we have a / then...
2013 * the binary name should be at the end. 2035 * the binary name should be at the end.
2052 _dw_gdk_threads_enter = dlsym(RTLD_DEFAULT, "gdk_threads_enter"); 2074 _dw_gdk_threads_enter = dlsym(RTLD_DEFAULT, "gdk_threads_enter");
2053 _dw_gdk_threads_leave = dlsym(RTLD_DEFAULT, "gdk_threads_leave"); 2075 _dw_gdk_threads_leave = dlsym(RTLD_DEFAULT, "gdk_threads_leave");
2054 2076
2055 _dw_gdk_threads_init(); 2077 _dw_gdk_threads_init();
2056 2078
2057 gtk_init(argc, argv); 2079 gtk_init(&argc, &argv);
2058 2080
2059 pthread_key_create(&_dw_fg_color_key, NULL); 2081 pthread_key_create(&_dw_fg_color_key, NULL);
2060 pthread_key_create(&_dw_bg_color_key, NULL); 2082 pthread_key_create(&_dw_bg_color_key, NULL);
2061 pthread_key_create(&_dw_mutex_key, NULL); 2083 pthread_key_create(&_dw_mutex_key, NULL);
2062 2084