diff gtk3/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
line wrap: on
line diff
--- a/gtk3/dw.c	Tue Nov 15 01:07:55 2022 +0000
+++ b/gtk3/dw.c	Tue Nov 15 02:29:56 2022 +0000
@@ -4262,8 +4262,8 @@
    bitmap = dw_bitmap_new(id);
    if(bitmap)
    {
-      dw_window_set_bitmap( bitmap, 0, filename );
-      gtk_container_add (GTK_CONTAINER(tmp), bitmap);
+      dw_window_set_bitmap(bitmap, 0, filename);
+      gtk_container_add(GTK_CONTAINER(tmp), bitmap);
       g_object_set_data(G_OBJECT(tmp), "_dw_bitmap", bitmap);
    }
    gtk_widget_show(tmp);
@@ -4298,7 +4298,7 @@
    if(bitmap)
    {
       dw_window_set_bitmap_from_data(bitmap, 0, data, len);
-      gtk_container_add (GTK_CONTAINER(tmp), bitmap);
+      gtk_container_add(GTK_CONTAINER(tmp), bitmap);
       g_object_set_data(G_OBJECT(tmp), "_dw_bitmap", bitmap);
    }
    gtk_widget_show(tmp);
@@ -4549,16 +4549,20 @@
  *       filename: a path to a file (Bitmap on OS/2 or
  *                 Windows and a pixmap on Unix, pass
  *                 NULL if you use the id param)
- */
-void dw_window_set_bitmap(HWND handle, unsigned long id, const char *filename)
+ * Returns:
+ *        DW_ERROR_NONE on success.
+ *        DW_ERROR_UNKNOWN if the parameters were invalid.
+ *        DW_ERROR_GENERAL if the bitmap was unable to be loaded.
+ */
+int dw_window_set_bitmap(HWND handle, unsigned long id, const char *filename)
 {
    GdkPixbuf *tmp = NULL;
-   int found_ext = 0;
-   int i;
-   int _dw_locked_by_me = FALSE;
+   int i, found_ext = 0;
+   int _dw_locked_by_me = FALSE;
+   int retval = DW_ERROR_UNKNOWN;
 
    if(!id && !filename)
-      return;
+      return retval;
 
    DW_MUTEX_LOCK;
    if(id)
@@ -4567,53 +4571,56 @@
    {
       char *file = alloca(strlen(filename) + 6);
 
-      if (!file)
+      if(!file)
       {
          DW_MUTEX_UNLOCK;
-         return;
+         return DW_ERROR_GENERAL;
       }
 
       strcpy(file, filename);
 
       /* check if we can read from this file (it exists and read permission) */
-      if ( access(file, 04 ) != 0 )
+      if(access(file, 04) != 0)
       {
          /* Try with various extentions */
-         for ( i = 0; i < NUM_EXTS; i++ )
-         {
-            strcpy( file, filename );
-            strcat( file, _dw_image_exts[i] );
-            if ( access( file, 04 ) == 0 )
+         for(i = 0; i < NUM_EXTS; i++)
+         {
+            strcpy(file, filename);
+            strcat(file, _dw_image_exts[i]);
+            if(access( file, 04 ) == 0)
             {
                found_ext = 1;
                break;
             }
          }
-         if ( found_ext == 0 )
+         if(found_ext == 0)
          {
             DW_MUTEX_UNLOCK;
-            return;
-         }
-      }
-      tmp = gdk_pixbuf_new_from_file(file, NULL );
-   }
-
-   if (tmp)
-   {
-      if ( GTK_IS_BUTTON(handle) )
-      {
-         GtkWidget *pixmap = (GtkWidget *)g_object_get_data( G_OBJECT(handle), "_dw_bitmap" );
+            return DW_ERROR_GENERAL;
+         }
+      }
+      tmp = gdk_pixbuf_new_from_file(file, NULL);
+   }
+
+   if(tmp)
+   {
+      if(GTK_IS_BUTTON(handle))
+      {
+         GtkWidget *pixmap = (GtkWidget *)g_object_get_data(G_OBJECT(handle), "_dw_bitmap");
          if(pixmap)
          {
             gtk_image_set_from_pixbuf(GTK_IMAGE(pixmap), tmp);
-         }
-      }
-      else
+	         retval = DW_ERROR_NONE;
+         }
+      }
+      else if(GTK_IS_IMAGE(handle))
       {
          gtk_image_set_from_pixbuf(GTK_IMAGE(handle), tmp);
-      }
-   }
-   DW_MUTEX_UNLOCK;
+         retval = DW_ERROR_NONE;
+      }
+   }
+   DW_MUTEX_UNLOCK;
+   return retval;
 }
 
 /*
@@ -4626,14 +4633,19 @@
  *                 Bitmap on Windows and a pixmap on Unix, pass
  *                 NULL if you use the id param)
  *       len: length of data
- */
-void dw_window_set_bitmap_from_data(HWND handle, unsigned long id, const char *data, int len)
+ * Returns:
+ *        DW_ERROR_NONE on success.
+ *        DW_ERROR_UNKNOWN if the parameters were invalid.
+ *        DW_ERROR_GENERAL if the bitmap was unable to be loaded.
+ */
+int dw_window_set_bitmap_from_data(HWND handle, unsigned long id, const char *data, int len)
 {
    GdkPixbuf *tmp = NULL;
    int _dw_locked_by_me = FALSE;
+   int retval = DW_ERROR_UNKNOWN;
 
    if(!id && !data)
-      return;
+      return retval;
 
    DW_MUTEX_LOCK;
    if(data)
@@ -4654,7 +4666,7 @@
       if(fd == -1 || written != len)
       {
          DW_MUTEX_UNLOCK;
-         return;
+         return DW_ERROR_GENERAL;
       }
 
       tmp = gdk_pixbuf_new_from_file(template, NULL);
@@ -4671,12 +4683,19 @@
          GtkWidget *pixmap = (GtkWidget *)g_object_get_data(G_OBJECT(handle), "_dw_bitmap");
 
          if(pixmap)
+         {
             gtk_image_set_from_pixbuf(GTK_IMAGE(pixmap), tmp);
-      }
-      else
+            retval = DW_ERROR_NONE;
+         }
+      }
+      else if(GTK_IS_IMAGE(handle))
+      {
          gtk_image_set_from_pixbuf(GTK_IMAGE(handle), tmp);
-   }
-   DW_MUTEX_UNLOCK;
+         retval = DW_ERROR_NONE;
+      }
+   }
+   DW_MUTEX_UNLOCK;
+   return retval;
 }
 
 /*