- wxCoord sx = m_icons[i].GetWidth(), sy = m_icons[i].GetHeight();
- // requested size
- if( sx == size.x && sy == size.y )
- return m_icons[i];
- // keep track if there is a system-size icon
- if( sx == sysX && sy == sysY )
- sysIcon = &m_icons[i];
+ wxCoord sx = icon.GetWidth(),
+ sy = icon.GetHeight();
+
+ // Exact match ends search immediately in any case.
+ if ( sx == sizeX && sy == sizeY )
+ {
+ iconBest = icon;
+ break;
+ }
+
+ if ( flags & FALLBACK_SYSTEM )
+ {
+ if ( sx == sysX && sy == sysY )
+ {
+ iconBest = icon;
+ bestIsSystem = true;
+ continue;
+ }
+ }
+
+ if ( !bestIsSystem && (flags & FALLBACK_NEAREST_LARGER) )
+ {
+ bool iconLarger = (sx >= sizeX) && (sy >= sizeY);
+ int iconDiff = abs(sx - sizeX) + abs(sy - sizeY);
+
+ // Use current icon as candidate for the best icon, if either:
+ // - we have no candidate yet
+ // - we have no candidate larger than desired size and current icon is
+ // - current icon is closer to desired size than candidate
+ if ( !iconBest.IsOk() ||
+ (!bestIsLarger && iconLarger) ||
+ (iconLarger && (iconDiff < bestDiff)) )
+ {
+ iconBest = icon;
+ bestIsLarger = iconLarger;
+ bestDiff = iconDiff;
+ continue;
+ }
+ }