changeset 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 12fb7f32f3a9
children a43a3f80ed32
files configure configure.in dw.h gtk/dw.c gtk3/dw.c
diffstat 5 files changed, 144 insertions(+), 83 deletions(-) [+]
line wrap: on
line diff
--- a/configure	Sun Nov 29 04:17:58 2020 +0000
+++ b/configure	Sun Nov 29 05:29:11 2020 +0000
@@ -6268,12 +6268,10 @@
 
   rm -f conf.gdkimlibtest
 
+      CFLAGS="$CFLAGS -DDW_INCLUDE_DEPRECATED_RESOURCES=1"
    else
       GTK_CFLAGS=`$PKG_CFG --cflags $GTK_PACKAGES`
       GTK_LIBS=`$PKG_CFG --libs $GTK_PACKAGES`
-      if test $with_gtk2 = "yes"; then
-         CFLAGS="$CFLAGS -DDW_INCLUDE_DEPRECATED_RESOURCES=1"
-      else
 
 
 
@@ -6465,7 +6463,6 @@
 $as_echo "yes" >&6; }
 	DW_GRESOURCE="enabled"
 fi
-      fi
    fi
 fi
 
--- a/configure.in	Sun Nov 29 04:17:58 2020 +0000
+++ b/configure.in	Sun Nov 29 05:29:11 2020 +0000
@@ -256,14 +256,11 @@
       AM_PATH_GLIB(1.2.0,,AC_MSG_ERROR(AC_MSG_ERROR(Cannot find GLIB: Is glib-config in path?)))
       AM_PATH_GTK(1.2.0,,AC_MSG_ERROR(AC_MSG_ERROR(Cannot find GTK: Is gtk-config in path?)), gthread)
       AM_PATH_GDK_IMLIB(1.9.4, AC_DEFINE(USE_IMLIB), AC_MSG_ERROR(IMLIB not found or too old. Dynamic Windows cannot build without it.))
+      CFLAGS="$CFLAGS -DDW_INCLUDE_DEPRECATED_RESOURCES=1"
    else
       GTK_CFLAGS=`$PKG_CFG --cflags $GTK_PACKAGES`
       GTK_LIBS=`$PKG_CFG --libs $GTK_PACKAGES`
-      if test $with_gtk2 = "yes"; then
-         CFLAGS="$CFLAGS -DDW_INCLUDE_DEPRECATED_RESOURCES=1"
-      else
-         PKG_CHECK_MODULES([GLIB], [glib-2.0 >= 2.32.0], [DW_GRESOURCE="enabled"], [CFLAGS="$CFLAGS -DDW_INCLUDE_DEPRECATED_RESOURCES=1"])
-      fi
+      PKG_CHECK_MODULES([GLIB], [glib-2.0 >= 2.32.0], [DW_GRESOURCE="enabled"], [CFLAGS="$CFLAGS -DDW_INCLUDE_DEPRECATED_RESOURCES=1"])
    fi
 fi
 
--- a/dw.h	Sun Nov 29 04:17:58 2020 +0000
+++ b/dw.h	Sun Nov 29 05:29:11 2020 +0000
@@ -1341,11 +1341,6 @@
 typedef void *HSHM;
 typedef void *HICN;
 
-typedef struct _resource_struct {
-   long resource_max, *resource_id;
-   char **resource_data;
-} DWResources;
-
 /* As of Dynamic Windows 3.1 GResource is default if supported.
  * Using --with-deprecated at configure time will include
  * support for our old home brewed resource system.
@@ -1354,13 +1349,18 @@
  * Test for GResource using: dwindows-config --gresource
  */
 #ifndef DW_INCLUDE_DEPRECATED_RESOURCES
-#if defined(DW_INCLUDE_DEPRECATED) || GTK_MAJOR_VERSION < 3 || !GLIB_CHECK_VERSION(2,32,0)
+#if defined(DW_INCLUDE_DEPRECATED) || GTK_MAJOR_VERSION < 2 || !GLIB_CHECK_VERSION(2,32,0)
 #define DW_INCLUDE_DEPRECATED_RESOURCES 1
 #endif
 #endif
 
 /* Only reference our old style resources if required. */
 #ifdef DW_INCLUDE_DEPRECATED_RESOURCES
+typedef struct _resource_struct {
+   long resource_max, *resource_id;
+   char **resource_data;
+} DWResources;
+
 #if !defined(DW_RESOURCES) || defined(BUILD_DLL)
 static DWResources DW_UNUSED(_resources) = { 0, 0, 0 };
 #else
--- a/gtk/dw.c	Sun Nov 29 04:17:58 2020 +0000
+++ b/gtk/dw.c	Sun Nov 29 05:29:11 2020 +0000
@@ -59,10 +59,15 @@
 #include "gtk/messagebox_information.xpm"
 #include "gtk/messagebox_question.xpm"
 
-/* These are used for resource management */
+/* These are used for legacy resource management 
+ * Starting with Dynamic Windows 3.1 the default 
+ * compiled in resource management is GResource.
+ */
+#ifdef DW_INCLUDE_DEPRECATED_RESOURCES
 #if defined(DW_RESOURCES) && !defined(BUILD_DLL)
 extern DWResources _resources;
 #endif
+#endif
 
 GdkColor _colors[] =
 {
@@ -1845,10 +1850,35 @@
    return NULL;
 }
 
+#if GTK_MAJOR_VERSION > 1
+#ifdef DW_INCLUDE_DEPRECATED_RESOURCES
+static GdkPixbuf *_dw_pixbuf_from_data(char *data)
+{
+   if(data[0] == 'G' && data[1] == 'd' && data[2] == 'k' && data[3] == 'P')
+      return gdk_pixbuf_new_from_inline(-1, (const guint8 *)data, FALSE, NULL);
+   return gdk_pixbuf_new_from_xpm_data((const char **)data);
+}
+#endif
+
+static GdkPixbuf *_dw_pixbuf_from_resource(unsigned int rid)
+{
+#if GLIB_CHECK_VERSION(2,32,0)
+   char resource_path[201] = {0};
+   snprintf(resource_path, 200, "/org/dbsoft/dwindows/%u", rid);
+   return gdk_pixbuf_new_from_resource(resource_path, NULL);
+#else
+   return NULL;
+#endif   
+}
+#endif
+
 static GdkPixmap *_find_pixmap(GdkBitmap **bitmap, HICN icon, HWND handle, unsigned long *userwidth, unsigned long *userheight)
 {
-   char *data = NULL;
-   int z, id = GPOINTER_TO_INT(icon);
+   unsigned int id = GPOINTER_TO_INT(icon);
+#if GTK_MAJOR_VERSION > 1
+   GdkPixbuf *icon_pixbuf = NULL;
+#endif
+   GdkPixmap *icon_pixmap = NULL;
 
    /* Quick dropout for non-handle */
    if(!icon)
@@ -1857,41 +1887,64 @@
    if(id & (1 << 31))
       return _find_private_pixmap(bitmap, GINT_TO_POINTER((id & 0xFFFFFF)), userwidth, userheight);
 
-   for(z=0;z<_resources.resource_max;z++)
-   {
-      if(_resources.resource_id[z] == id)
-      {
-         data = _resources.resource_data[z];
-         break;
-      }
-   }
-
-   if(data)
-   {
-      GdkPixmap *icon_pixmap = NULL;
-#if GTK_MAJOR_VERSION > 1
-      GdkPixbuf *icon_pixbuf;
-
-      if(data[0] == 'G' && data[1] == 'd' && data[2] == 'k' && data[3] == 'P')
-         icon_pixbuf = gdk_pixbuf_new_from_inline(-1, (const guint8 *)data, FALSE, NULL);
-      else
-         icon_pixbuf = gdk_pixbuf_new_from_xpm_data((const char **)data);
-
-      if(userwidth)
-         *userwidth = gdk_pixbuf_get_width(icon_pixbuf);
-      if(userheight)
-         *userheight = gdk_pixbuf_get_height(icon_pixbuf);
-
+#if GTK_MAJOR_VERSION > 1
+   icon_pixbuf = _dw_pixbuf_from_resource(id);
+#endif
+
+#ifdef DW_INCLUDE_DEPRECATED_RESOURCES
+#if GTK_MAJOR_VERSION > 1
+   if(!icon_pixbuf)
+#endif   
+   {
+      char *data = NULL;
+      int z;
+
+      for(z=0;z<_resources.resource_max;z++)
+      {
+         if(_resources.resource_id[z] == id)
+         {
+            data = _resources.resource_data[z];
+            break;
+         }
+      }
+
+      if(data)
+      {
+#if GTK_MAJOR_VERSION > 1
+         icon_pixbuf = _dw_pixbuf_from_data(data);
+#elif defined(USE_IMLIB)
+         gdk_imlib_data_to_pixmap((char **)data, &icon_pixmap, bitmap);
+#else
+         icon_pixmap = gdk_pixmap_create_from_xpm_d(handle->window, bitmap, &_colors[DW_CLR_PALEGRAY], (char **)data);
+#endif
+      }
+   }
+#endif
+#if GTK_MAJOR_VERSION > 1
+   if(icon_pixbuf)
+   {
       gdk_pixbuf_render_pixmap_and_mask(icon_pixbuf, &icon_pixmap, bitmap, 1);
       g_object_unref(icon_pixbuf);
-#elif defined(USE_IMLIB)
-      gdk_imlib_data_to_pixmap((char **)data, &icon_pixmap, bitmap);
-#else
-      icon_pixmap = gdk_pixmap_create_from_xpm_d(handle->window, bitmap, &_colors[DW_CLR_PALEGRAY], (char **)data);
-#endif
-      return icon_pixmap;
-   }
-   return NULL;
+   }
+   if(userwidth)
+      *userwidth = icon_pixbuf ? gdk_pixbuf_get_width(icon_pixbuf) : 0;
+   if(userheight)
+      *userheight = icon_pixbuf ? gdk_pixbuf_get_height(icon_pixbuf) : 0;
+#else
+   if(icon_pixmap)
+   {
+      gint width = 0, height = 0;
+
+#if GTK_CHECK_VERSION(2,24,0)
+      gdk_pixmap_get_size(icon_pixmap, &width, &height);
+#endif
+      if(userwidth)
+         *userwidth = (unsigned long)width;
+      if(userheight)
+         *userheight = (unsigned long)height;         
+   }
+#endif
+   return icon_pixmap;
 }
 
 #if GTK_MAJOR_VERSION > 1
@@ -1906,8 +1959,8 @@
 
 static GdkPixbuf *_find_pixbuf(HICN icon)
 {
-   char *data = NULL;
-   int z, id = GPOINTER_TO_INT(icon);
+   unsigned int id = GPOINTER_TO_INT(icon);
+   GdkPixbuf *icon_pixbuf;
 
    /* Quick dropout for non-handle */
    if(!icon)
@@ -1916,20 +1969,26 @@
    if(id & (1 << 31))
       return _find_private_pixbuf(GINT_TO_POINTER((id & 0xFFFFFF)));
 
-   for(z=0;z<_resources.resource_max;z++)
-   {
-      if(_resources.resource_id[z] == id)
-      {
-         data = _resources.resource_data[z];
-         break;
-      }
-   }
-
-   if(data)
-   {
-      if(data[0] == 'G' && data[1] == 'd' && data[2] == 'k' && data[3] == 'P')
-         return gdk_pixbuf_new_from_inline(-1, (const guint8 *)data, FALSE, NULL);
-      return gdk_pixbuf_new_from_xpm_data((const char **)data);
+   if((icon_pixbuf = _dw_pixbuf_from_resource(id)))
+      return icon_pixbuf;
+   else
+   {
+#ifdef DW_INCLUDE_DEPRECATED_RESOURCES
+      char *data = NULL;
+      int z;
+      
+      for(z=0;z<_resources.resource_max;z++)
+      {
+         if(_resources.resource_id[z] == id)
+         {
+            data = _resources.resource_data[z];
+            break;
+         }
+      }
+
+      if(data)
+         return _dw_pixbuf_from_data(data);
+#endif
    }
    return NULL;
 }
@@ -2010,20 +2069,22 @@
 }
 #endif
 
+static char * _dw_test_xpm[] = {
+   "1 1 1 1",
+   " 	c None",
+   " "};
+   
 /*
  * Initializes the Dynamic Windows engine.
  * Parameters:
  *           newthread: True if this is the only thread.
  *                      False if there is already a message loop running.
  */
-int dw_int_init(DWResources *res, int newthread, int *argc, char **argv[])
-{
-   int z;
-   char *fname;
-   static char * test_xpm[] = {
-      "1 1 1 1",
-      " 	c None",
-      " "};
+#ifdef DW_INCLUDE_DEPRECATED_RESOURCES
+int dw_int_init(DWResources *res, int newthread, int *pargc, char **pargv[])
+{
+   int z, argc = pargc ? *pargc : 0;
+   char *fname, **argv = pargv ? *pargv : NULL;
 
    if(res)
    {
@@ -2031,11 +2092,17 @@
       _resources.resource_id = res->resource_id;
       _resources.resource_data = res->resource_data;
    }
+#else
+int dw_init(int newthread, int argc, char *argv[])
+{
+   int z;
+   char *fname;
+#endif
 
    /* Setup the private data directory */
-   if(argc && argv && *argc > 0 && (*argv)[0])
-   {
-      char *pathcopy = strdup((*argv)[0]);
+   if(argc > 0 && argv[0])
+   {
+      char *pathcopy = strdup(argv[0]);
       char *pos = strrchr(pathcopy, '/');
       char *binname = pathcopy;
 
@@ -2089,7 +2156,7 @@
    gdk_threads_init();
 #endif
 
-   gtk_init(argc, argv);
+   gtk_init(&argc, &argv);
 #ifdef USE_IMLIB
    gdk_imlib_init();
 #endif
@@ -2111,18 +2178,18 @@
     */
 #if GTK_MAJOR_VERSION > 1
    {
-      GdkPixbuf *pixbuf = gdk_pixbuf_new_from_xpm_data((const char **)test_xpm);
+      GdkPixbuf *pixbuf = gdk_pixbuf_new_from_xpm_data((const char **)_dw_test_xpm);
 
       gdk_pixbuf_render_pixmap_and_mask(pixbuf, &_dw_tmppixmap, &_dw_tmpbitmap, 1);
       g_object_unref(pixbuf);
    }
 #elif defined(USE_IMLIB)
-   gdk_imlib_data_to_pixmap((char **)test_xpm, &_dw_tmppixmap, &_dw_tmpbitmap);
+   gdk_imlib_data_to_pixmap((char **)_dw_test_xpm, &_dw_tmppixmap, &_dw_tmpbitmap);
 #else
    {
       GtkWidget *handle = gtk_label_new("");
       gtk_widget_realize(handle);
-      _dw_tmppixmap = gdk_pixmap_create_from_xpm_d(handle->window, &_dw_tmpbitmap, &_colors[DW_CLR_PALEGRAY], (char **)test_xpm);
+      _dw_tmppixmap = gdk_pixmap_create_from_xpm_d(handle->window, &_dw_tmpbitmap, &_colors[DW_CLR_PALEGRAY], (char **)_dw_test_xpm);
       gtk_widget_destroy(handle);
    }
 #endif
--- a/gtk3/dw.c	Sun Nov 29 04:17:58 2020 +0000
+++ b/gtk3/dw.c	Sun Nov 29 05:29:11 2020 +0000
@@ -1924,7 +1924,7 @@
 {
 #if GLIB_CHECK_VERSION(2,32,0)
    char resource_path[201] = {0};
-   snprintf(resource_path, 200, "/org/dbsoft/dwindows/%s/%u", _dw_app_id, rid);
+   snprintf(resource_path, 200, "/org/dbsoft/dwindows/%u", rid);
    return gdk_pixbuf_new_from_resource(resource_path, NULL);
 #else
    return NULL;