From 2cb9ff97ed512f2ab9d80cef82a996e1ef6acfe7 Mon Sep 17 00:00:00 2001
From: David Maciejak <david.maciejak@gmail.com>
Date: Fri, 7 Mar 2014 08:39:28 +0800
Subject: [PATCH] Scale image to fit in preview panel

---
 WINGs/WINGs/WINGs.h        |    2 +-
 WINGs/wpixmap.c            |   16 +++++++++++++---
 WPrefs.app/MouseSettings.c |    2 +-
 WPrefs.app/WPrefs.c        |    4 ++--
 src/dialog.c               |    9 ++++-----
 src/dockedapp.c            |    2 +-
 6 files changed, 22 insertions(+), 13 deletions(-)

diff --git a/WINGs/WINGs/WINGs.h b/WINGs/WINGs/WINGs.h
index d3b4f28..aa95844 100644
--- a/WINGs/WINGs/WINGs.h
+++ b/WINGs/WINGs/WINGs.h
@@ -839,7 +839,7 @@ WMPixmap* WMCreateBlendedPixmapFromRImage(WMScreen *scrPtr, RImage *image,
                                           const RColor *color);
 
 WMPixmap* WMCreateBlendedPixmapFromFile(WMScreen *scrPtr, const char *fileName,
-                                        const RColor *color);
+                                        const RColor *color, unsigned wHeight, unsigned wWidth);
 
 void WMDrawPixmap(WMPixmap *pixmap, Drawable d, int x, int y);
 
diff --git a/WINGs/wpixmap.c b/WINGs/wpixmap.c
index 55c457b..5511a13 100644
--- a/WINGs/wpixmap.c
+++ b/WINGs/wpixmap.c
@@ -117,7 +117,8 @@ WMPixmap *WMCreateBlendedPixmapFromRImage(WMScreen * scrPtr, RImage * image, con
 	return pixPtr;
 }
 
-WMPixmap *WMCreateBlendedPixmapFromFile(WMScreen * scrPtr, const char *fileName, const RColor * color)
+WMPixmap *WMCreateBlendedPixmapFromFile(WMScreen * scrPtr, const char *fileName, const RColor * color,
+					unsigned wHeight, unsigned wWidth)
 {
 	WMPixmap *pixPtr;
 	RImage *image;
@@ -126,10 +127,19 @@ WMPixmap *WMCreateBlendedPixmapFromFile(WMScreen * scrPtr, const char *fileName,
 	if (!image)
 		return NULL;
 
-	RCombineImageWithColor(image, color);
+	/* scale it if requested to fit in the widget */
+	if (wHeight > 0 && wWidth > 0 && (image->height > wHeight || image->width > wWidth)) {
+		RImage *tmp;
+		tmp = image;
+		if (image->width > image->height)
+			image = RScaleImage(tmp, wWidth, image->height * wHeight / image->width);
+		else
+			image = RScaleImage(tmp, image->width * wWidth / image->height, wHeight);
+		RReleaseImage(tmp);
+	}
 
+	RCombineImageWithColor(image, color);
 	pixPtr = WMCreatePixmapFromRImage(scrPtr, image, 0);
-
 	RReleaseImage(image);
 
 	return pixPtr;
diff --git a/WPrefs.app/MouseSettings.c b/WPrefs.app/MouseSettings.c
index f83ec3a..7978f72 100644
--- a/WPrefs.app/MouseSettings.c
+++ b/WPrefs.app/MouseSettings.c
@@ -446,7 +446,7 @@ static void createPanel(Panel * p)
 	WMSetLabelImagePosition(panel->speedL, WIPImageOnly);
 	path = LocateImage(SPEED_ICON_FILE);
 	if (path) {
-		icon = WMCreateBlendedPixmapFromFile(scr, path, &color);
+		icon = WMCreateBlendedPixmapFromFile(scr, path, &color, 0, 0);
 		if (icon) {
 			WMSetLabelImage(panel->speedL, icon);
 			WMReleasePixmap(icon);
diff --git a/WPrefs.app/WPrefs.c b/WPrefs.app/WPrefs.c
index 31192aa..b80e099 100644
--- a/WPrefs.app/WPrefs.c
+++ b/WPrefs.app/WPrefs.c
@@ -456,7 +456,7 @@ void SetButtonAlphaImage(WMScreen *scr, WMButton *bPtr, const char *file)
 	color.blue = 0xae;
 	color.alpha = 0;
 	if (iconPath) {
-		icon = WMCreateBlendedPixmapFromFile(scr, iconPath, &color);
+		icon = WMCreateBlendedPixmapFromFile(scr, iconPath, &color, 0, 0);
 		if (!icon)
 			wwarning(_("could not load icon file %s"), iconPath);
 	} else {
@@ -470,7 +470,7 @@ void SetButtonAlphaImage(WMScreen *scr, WMButton *bPtr, const char *file)
 	color.blue = 0xff;
 	color.alpha = 0;
 	if (iconPath) {
-		icon = WMCreateBlendedPixmapFromFile(scr, iconPath, &color);
+		icon = WMCreateBlendedPixmapFromFile(scr, iconPath, &color, 0, 0);
 		if (!icon)
 			wwarning(_("could not load icon file %s"), iconPath);
 	} else {
diff --git a/src/dialog.c b/src/dialog.c
index 69fd31d..5218866 100644
--- a/src/dialog.c
+++ b/src/dialog.c
@@ -619,7 +619,8 @@ static void setViewedImage(IconPanel *panel, const char *file)
 	color.green = 0xaa;
 	color.blue = 0xae;
 	color.alpha = 0;
-	pixmap = WMCreateBlendedPixmapFromFile(WMWidgetScreen(panel->win), file, &color);
+	pixmap = WMCreateBlendedPixmapFromFile(WMWidgetScreen(panel->win), file, &color, 75, 75);
+
 	if (!pixmap) {
 		WMSetButtonEnabled(panel->okButton, False);
 
@@ -649,9 +650,7 @@ static void listCallback(void *self, void *data)
 		path = item->text;
 
 		WMSetTextFieldText(panel->fileField, path);
-
 		WMSetLabelImage(panel->iconView, NULL);
-
 		WMSetButtonEnabled(panel->okButton, False);
 
 		WMClearList(panel->iconList);
@@ -742,7 +741,7 @@ static void drawIconProc(WMList * lPtr, int index, Drawable d, char *text, int s
 	color.blue = WMBlueComponentOfColor(back) >> 8;
 	color.alpha = WMGetColorAlpha(back) >> 8;
 
-	pixmap = WMCreateBlendedPixmapFromFile(wmscr, file, &color);
+	pixmap = WMCreateBlendedPixmapFromFile(wmscr, file, &color, 0, 0);
 	wfree(file);
 
 	if (!pixmap) {
@@ -1524,7 +1523,7 @@ static WMPixmap *getWindowMakerIconImage(WMScreen *scr)
 		gray.blue = 0xae;
 		gray.alpha = 0;
 
-		pix = WMCreateBlendedPixmapFromFile(scr, path, &gray);
+		pix = WMCreateBlendedPixmapFromFile(scr, path, &gray, 0, 0);
 		wfree(path);
 	}
 
diff --git a/src/dockedapp.c b/src/dockedapp.c
index 19d55bb..502c830 100644
--- a/src/dockedapp.c
+++ b/src/dockedapp.c
@@ -103,7 +103,7 @@ static void updateSettingsPanelIcon(AppSettingsPanel * panel)
 			color.green = 0xaa;
 			color.blue = 0xae;
 			color.alpha = 0;
-			pixmap = WMCreateBlendedPixmapFromFile(WMWidgetScreen(panel->win), path, &color);
+			pixmap = WMCreateBlendedPixmapFromFile(WMWidgetScreen(panel->win), path, &color, 64, 64);
 			if (!pixmap) {
 				WMSetLabelImage(panel->iconLabel, NULL);
 			} else {
-- 
1.7.10.4

