Julien Danjou wrote:
> At 1229325688 time_t, Leon Winter wrote:
>> +        if icon.width > tonumber(data.h) then
>> +            icon = 
>> icon:crop_and_scale(0,0,icon.height,icon.width,data.h,data.h)
> 
> Pictures may not be square.
> 

Patch attached.

Anyway this saves us only 2 SLOC, but also the
mouse-over/leave/click-handler for the former imagebox.

Regards,
Leon Winter

>From cf899b01d0707fea89d5a475caa59908f2ce4ca2 Mon Sep 17 00:00:00 2001
From: Leon Winter <[email protected]>
Date: Thu, 18 Dec 2008 18:02:54 +0100
Subject: [PATCH] awful.menu: use bg_image instead of an imagebox

---
 lib/awful/menu.lua.in |   38 ++++++++++++++++++--------------------
 1 files changed, 18 insertions(+), 20 deletions(-)

diff --git a/lib/awful/menu.lua.in b/lib/awful/menu.lua.in
index 6a19df1..95666c7 100644
--- a/lib/awful/menu.lua.in
+++ b/lib/awful/menu.lua.in
@@ -17,6 +17,7 @@ local capi = { screen = screen, mouse = mouse, client = client }
 local util = require("awful.util")
 local tags = require("awful.tag")
 local awbeautiful = require("beautiful")
+local tonumber = tonumber
 
 --- Menu module for awful
 module("awful.menu")
@@ -113,31 +114,28 @@ local function add_item(data, num, item_info)
         button({}, 3, function () hide(data) end)
     }
 
-    -- Create the item icon widget
-    local icon
-    if item_info[3] then
-        icon = widget({ type = "imagebox",  align = "left" })
-        if type(item_info[3]) == "string" then
-            icon.image = image(item_info[3])
-        else
-            icon.image = item_info[3]
-        end
-    else
-        icon = widget({type = "textbox", align = "left" })
-        icon.width = data.h
-    end
-
-    icon:buttons(bindings)
-
-    function icon.mouse_enter() mouse_enter(item, data.theme) end
-    function icon.mouse_leave() mouse_leave(item, data.theme) end
-
     -- Create the item label widget
     local label = widget({
         type = "textbox",
         align = "flex"
     })
     label.text = " " .. item_info[1]
+    label:margin{left = data.h}
+    -- Set icon if needed
+    if item_info[3] then
+        local icon = type(item_info[3]) == "string" and image(item_info[3]) or item_info[3]
+        if icon.width > tonumber(data.h) or icon.height > tonumber(data.h) then
+            local width, height
+            if ((data.h/icon.height) * icon.width) > tonumber(data.h) then
+                width, height = data.h,(tonumber(data.h)/icon.width)*icon.height 
+            else
+                width, height = (tonumber(data.h)/icon.height)*icon.width,data.h
+            end
+            icon = icon:crop_and_scale(0,0,icon.width,icon.height,width,height)
+        end
+        label.bg_image = icon
+    end
+
     label:buttons(bindings)
 
     function label.mouse_enter() mouse_enter(item, data.theme) end
@@ -155,7 +153,7 @@ local function add_item(data, num, item_info)
     end
 
     -- Add widgets to the wibox
-    item.widgets = { icon, label, submenu }
+    item.widgets = { label, submenu }
 
     item.ontop = true
 
-- 
1.5.6.5


Reply via email to