diff --git a/base/win/scoped_gdi_object.h b/base/win/scoped_gdi_object.h index 57b013e2fbaae8..9d8465b25bde5b 100644 --- a/base/win/scoped_gdi_object.h +++ b/base/win/scoped_gdi_object.h @@ -7,64 +7,32 @@ #include -#include "base/basictypes.h" -#include "base/logging.h" +#include "base/scoped_generic.h" namespace base { namespace win { -// Like ScopedHandle but for GDI objects. -template -class ScopedGDIObject { - public: - ScopedGDIObject() : object_(NULL) {} - explicit ScopedGDIObject(T object) : object_(object) {} - - ~ScopedGDIObject() { - Close(); - } - - T Get() { - return object_; - } - - void Set(T object) { - if (object_ && object != object_) - Close(); - object_ = object; - } - - ScopedGDIObject& operator=(T object) { - Set(object); - return *this; - } - - T release() { - T object = object_; - object_ = NULL; - return object; - } +namespace internal { - operator T() { return object_; } - - private: - void Close() { - if (object_) - DeleteObject(object_); - } - - T object_; - DISALLOW_COPY_AND_ASSIGN(ScopedGDIObject); +template +struct ScopedGDIObjectTraits { + static T InvalidValue() { return nullptr; } + static void Free(T object) { DeleteObject(object); } }; // An explicit specialization for HICON because we have to call DestroyIcon() // instead of DeleteObject() for HICON. -template<> -void inline ScopedGDIObject::Close() { - if (object_) - DestroyIcon(object_); +template <> +void inline ScopedGDIObjectTraits::Free(HICON icon) { + DestroyIcon(icon); } +} // namespace internal + +// Like ScopedHandle but for GDI objects. +template +using ScopedGDIObject = ScopedGeneric>; + // Typedefs for some common use cases. typedef ScopedGDIObject ScopedBitmap; typedef ScopedGDIObject ScopedRegion; diff --git a/chrome/browser/ui/views/frame/glass_browser_frame_view.cc b/chrome/browser/ui/views/frame/glass_browser_frame_view.cc index 35cf422c025b57..847dd483b41027 100644 --- a/chrome/browser/ui/views/frame/glass_browser_frame_view.cc +++ b/chrome/browser/ui/views/frame/glass_browser_frame_view.cc @@ -65,13 +65,16 @@ const int kNewTabCaptionMaximizedSpacing = 16; // Converts the |image| to a Windows icon and returns the corresponding HICON // handle. |image| is resized to desired |width| and |height| if needed. -HICON CreateHICONFromSkBitmapSizedTo(const gfx::ImageSkia& image, - int width, - int height) { - if (width == image.width() && height == image.height()) - return IconUtil::CreateHICONFromSkBitmap(*image.bitmap()); - return IconUtil::CreateHICONFromSkBitmap(skia::ImageOperations::Resize( - *image.bitmap(), skia::ImageOperations::RESIZE_BEST, width, height)); +base::win::ScopedHICON CreateHICONFromSkBitmapSizedTo( + const gfx::ImageSkia& image, + int width, + int height) { + return IconUtil::CreateHICONFromSkBitmap( + width == image.width() && height == image.height() + ? *image.bitmap() + : skia::ImageOperations::Resize(*image.bitmap(), + skia::ImageOperations::RESIZE_BEST, + width, height)); } } // namespace @@ -616,6 +619,8 @@ void GlassBrowserFrameView::StopThrobber() { if (throbber_running_) { throbber_running_ = false; + base::win::ScopedHICON previous_small_icon; + base::win::ScopedHICON previous_big_icon; HICON small_icon = nullptr; HICON big_icon = nullptr; @@ -623,10 +628,22 @@ void GlassBrowserFrameView::StopThrobber() { if (browser_view()->ShouldShowWindowIcon()) { gfx::ImageSkia icon = browser_view()->GetWindowIcon(); if (!icon.isNull()) { - small_icon = CreateHICONFromSkBitmapSizedTo( - icon, GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON)); - big_icon = CreateHICONFromSkBitmapSizedTo( - icon, GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON)); + // Keep previous icons alive as long as they are referenced by the HWND. + previous_small_icon = small_window_icon_.Pass(); + previous_big_icon = big_window_icon_.Pass(); + + // Take responsibility for eventually destroying the created icons. + small_window_icon_ = + CreateHICONFromSkBitmapSizedTo(icon, GetSystemMetrics(SM_CXSMICON), + GetSystemMetrics(SM_CYSMICON)) + .Pass(); + big_window_icon_ = + CreateHICONFromSkBitmapSizedTo(icon, GetSystemMetrics(SM_CXICON), + GetSystemMetrics(SM_CYICON)) + .Pass(); + + small_icon = small_window_icon_.get(); + big_icon = big_window_icon_.get(); } } @@ -643,21 +660,13 @@ void GlassBrowserFrameView::StopThrobber() { // This will reset the icon which we set in the throbber code. // WM_SETICON with null icon restores the icon for title bar but not // for taskbar. See http://crbug.com/29996 - HICON previous_small_icon = reinterpret_cast( - SendMessage(views::HWNDForWidget(frame()), WM_SETICON, - static_cast(ICON_SMALL), - reinterpret_cast(small_icon))); - - HICON previous_big_icon = reinterpret_cast( - SendMessage(views::HWNDForWidget(frame()), WM_SETICON, - static_cast(ICON_BIG), - reinterpret_cast(big_icon))); - - if (previous_small_icon) - ::DestroyIcon(previous_small_icon); + SendMessage(views::HWNDForWidget(frame()), WM_SETICON, + static_cast(ICON_SMALL), + reinterpret_cast(small_icon)); - if (previous_big_icon) - ::DestroyIcon(previous_big_icon); + SendMessage(views::HWNDForWidget(frame()), WM_SETICON, + static_cast(ICON_BIG), + reinterpret_cast(big_icon)); } } diff --git a/chrome/browser/ui/views/frame/glass_browser_frame_view.h b/chrome/browser/ui/views/frame/glass_browser_frame_view.h index 3f77ffb9a4d269..3a3d3ec05ab012 100644 --- a/chrome/browser/ui/views/frame/glass_browser_frame_view.h +++ b/chrome/browser/ui/views/frame/glass_browser_frame_view.h @@ -6,6 +6,7 @@ #define CHROME_BROWSER_UI_VIEWS_FRAME_GLASS_BROWSER_FRAME_VIEW_H_ #include "base/compiler_specific.h" +#include "base/win/scoped_gdi_object.h" #include "chrome/browser/ui/views/frame/browser_non_client_frame_view.h" #include "ui/views/controls/button/button.h" #include "ui/views/window/non_client_view.h" @@ -106,6 +107,12 @@ class GlassBrowserFrameView : public BrowserNonClientFrameView, // The bounds of the ClientView. gfx::Rect client_view_bounds_; + // The small icon created from the bitmap image of the window icon. + base::win::ScopedHICON small_window_icon_; + + // The big icon created from the bitmap image of the window icon. + base::win::ScopedHICON big_window_icon_; + // Whether or not the window throbber is currently animating. bool throbber_running_; diff --git a/chrome/browser/ui/views/frame/taskbar_decorator_win.cc b/chrome/browser/ui/views/frame/taskbar_decorator_win.cc index 95267720cb2815..d6328fad663b7a 100644 --- a/chrome/browser/ui/views/frame/taskbar_decorator_win.cc +++ b/chrome/browser/ui/views/frame/taskbar_decorator_win.cc @@ -59,11 +59,11 @@ void SetOverlayIcon(HWND hwnd, scoped_ptr bitmap) { SkCanvas offscreen_canvas(offscreen_bitmap); offscreen_canvas.clear(SK_ColorTRANSPARENT); offscreen_canvas.drawBitmap(sk_icon, 0, kOverlayIconSize - resized_height); - icon.Set(IconUtil::CreateHICONFromSkBitmap(offscreen_bitmap)); - if (!icon.Get()) + icon = IconUtil::CreateHICONFromSkBitmap(offscreen_bitmap).Pass(); + if (!icon.is_valid()) return; } - taskbar->SetOverlayIcon(hwnd, icon, L""); + taskbar->SetOverlayIcon(hwnd, icon.get(), L""); } } // namespace diff --git a/chrome/browser/ui/views/panels/panel_frame_view.cc b/chrome/browser/ui/views/panels/panel_frame_view.cc index dbb5929255ab50..63a2ba92159280 100644 --- a/chrome/browser/ui/views/panels/panel_frame_view.cc +++ b/chrome/browser/ui/views/panels/panel_frame_view.cc @@ -357,18 +357,19 @@ void PanelFrameView::SetWindowCornerStyle(panel::CornerStyle corner_style) { // window region if the region really differs. HWND native_window = views::HWNDForWidget(panel_view_->window()); base::win::ScopedRegion current_region(::CreateRectRgn(0, 0, 0, 0)); - ::GetWindowRgn(native_window, current_region); + ::GetWindowRgn(native_window, current_region.get()); gfx::Path window_mask; GetWindowMask(size(), &window_mask); base::win::ScopedRegion new_region; if (!window_mask.isEmpty()) - new_region.Set(gfx::CreateHRGNFromSkPath(window_mask)); + new_region.reset(gfx::CreateHRGNFromSkPath(window_mask)); const bool has_current_region = current_region != NULL; const bool has_new_region = new_region != NULL; if (has_current_region != has_new_region || - (has_current_region && !::EqualRgn(current_region, new_region))) { + (has_current_region && + !::EqualRgn(current_region.get(), new_region.get()))) { // SetWindowRgn takes ownership of the new_region. ::SetWindowRgn(native_window, new_region.release(), TRUE); } diff --git a/chrome/browser/ui/views/status_icons/status_icon_win.cc b/chrome/browser/ui/views/status_icons/status_icon_win.cc index 105bf487ce5951..0ce341ed3140b2 100644 --- a/chrome/browser/ui/views/status_icons/status_icon_win.cc +++ b/chrome/browser/ui/views/status_icons/status_icon_win.cc @@ -82,7 +82,7 @@ void StatusIconWin::ResetIcon() { InitIconData(&icon_data); icon_data.uFlags = NIF_MESSAGE; icon_data.uCallbackMessage = message_id_; - icon_data.hIcon = icon_.Get(); + icon_data.hIcon = icon_.get(); // If we have an image, then set the NIF_ICON flag, which tells // Shell_NotifyIcon() to set the image for the status icon it creates. if (icon_data.hIcon) @@ -98,8 +98,8 @@ void StatusIconWin::SetImage(const gfx::ImageSkia& image) { NOTIFYICONDATA icon_data; InitIconData(&icon_data); icon_data.uFlags = NIF_ICON; - icon_.Set(IconUtil::CreateHICONFromSkBitmap(*image.bitmap())); - icon_data.hIcon = icon_.Get(); + icon_ = IconUtil::CreateHICONFromSkBitmap(*image.bitmap()).Pass(); + icon_data.hIcon = icon_.get(); BOOL result = Shell_NotifyIcon(NIM_MODIFY, &icon_data); if (!result) LOG(WARNING) << "Error setting status tray icon image"; @@ -131,12 +131,12 @@ void StatusIconWin::DisplayBalloon( base::win::Version win_version = base::win::GetVersion(); if (!icon.isNull() && win_version != base::win::VERSION_PRE_XP) { - balloon_icon_.Set(IconUtil::CreateHICONFromSkBitmap(*icon.bitmap())); + balloon_icon_ = IconUtil::CreateHICONFromSkBitmap(*icon.bitmap()).Pass(); if (win_version >= base::win::VERSION_VISTA) { - icon_data.hBalloonIcon = balloon_icon_.Get(); + icon_data.hBalloonIcon = balloon_icon_.get(); icon_data.dwInfoFlags = NIIF_USER | NIIF_LARGE_ICON; } else { - icon_data.hIcon = balloon_icon_.Get(); + icon_data.hIcon = balloon_icon_.get(); icon_data.uFlags |= NIF_ICON; icon_data.dwInfoFlags = NIIF_USER; } diff --git a/chrome/browser/ui/views/tabs/window_finder_win.cc b/chrome/browser/ui/views/tabs/window_finder_win.cc index 79897ff3861b9d..d0e7b55a88f80d 100644 --- a/chrome/browser/ui/views/tabs/window_finder_win.cc +++ b/chrome/browser/ui/views/tabs/window_finder_win.cc @@ -109,16 +109,16 @@ class TopMostFinder : public BaseWindowFinder { } // hwnd is at the point. Make sure the point is within the windows region. - if (GetWindowRgn(hwnd, tmp_region_.Get()) == ERROR) { + if (GetWindowRgn(hwnd, tmp_region_.get()) == ERROR) { // There's no region on the window and the window contains the point. Stop // iterating. return true; } // The region is relative to the window's rect. - BOOL is_point_in_region = PtInRegion(tmp_region_.Get(), - screen_loc_.x() - r.left, screen_loc_.y() - r.top); - tmp_region_ = CreateRectRgn(0, 0, 0, 0); + BOOL is_point_in_region = PtInRegion( + tmp_region_.get(), screen_loc_.x() - r.left, screen_loc_.y() - r.top); + tmp_region_.reset(CreateRectRgn(0, 0, 0, 0)); // Stop iterating if the region contains the point. return !!is_point_in_region; } diff --git a/content/browser/renderer_host/pepper/pepper_truetype_font_win.cc b/content/browser/renderer_host/pepper/pepper_truetype_font_win.cc index 5d572cda922876..59119cd7d75df9 100644 --- a/content/browser/renderer_host/pepper/pepper_truetype_font_win.cc +++ b/content/browser/renderer_host/pepper/pepper_truetype_font_win.cc @@ -76,25 +76,26 @@ int32_t PepperTrueTypeFontWin::Initialize( } // TODO(bbudge) support widths (extended, condensed). - font_.Set(CreateFont(0 /* height */, - 0 /* width */, - 0 /* escapement */, - 0 /* orientation */, - desc->weight, // our weight enum matches Windows. - (desc->style & PP_TRUETYPEFONTSTYLE_ITALIC) ? 1 : 0, - 0 /* underline */, - 0 /* strikeout */, - desc->charset, // our charset enum matches Windows. - OUT_OUTLINE_PRECIS, // truetype and other outline fonts - CLIP_DEFAULT_PRECIS, - DEFAULT_QUALITY, - pitch_and_family, - base::UTF8ToUTF16(desc->family).c_str())); - if (!font_.Get()) + font_.reset(CreateFont( + 0 /* height */, + 0 /* width */, + 0 /* escapement */, + 0 /* orientation */, + desc->weight, // our weight enum matches Windows. + (desc->style & PP_TRUETYPEFONTSTYLE_ITALIC) ? 1 : 0, + 0 /* underline */, + 0 /* strikeout */, + desc->charset, // our charset enum matches Windows. + OUT_OUTLINE_PRECIS, // truetype and other outline fonts + CLIP_DEFAULT_PRECIS, + DEFAULT_QUALITY, + pitch_and_family, + base::UTF8ToUTF16(desc->family).c_str())); + if (!font_.is_valid()) return PP_ERROR_FAILED; LOGFONT font_desc; - if (!::GetObject(font_.Get(), sizeof(LOGFONT), &font_desc)) + if (!::GetObject(font_.get(), sizeof(LOGFONT), &font_desc)) return PP_ERROR_FAILED; switch (font_desc.lfPitchAndFamily & 0xF0) { // Top 4 bits are family. @@ -125,7 +126,7 @@ int32_t PepperTrueTypeFontWin::Initialize( // doesn't fill in the name field of the LOGFONT structure. base::win::ScopedCreateDC hdc(::CreateCompatibleDC(NULL)); if (hdc.IsValid()) { - base::win::ScopedSelectObject select_object(hdc.Get(), font_.Get()); + base::win::ScopedSelectObject select_object(hdc.Get(), font_.get()); WCHAR name[LF_FACESIZE]; GetTextFace(hdc.Get(), LF_FACESIZE, name); desc->family = base::UTF16ToUTF8(name); @@ -135,14 +136,14 @@ int32_t PepperTrueTypeFontWin::Initialize( } int32_t PepperTrueTypeFontWin::GetTableTags(std::vector* tags) { - if (!font_.Get()) + if (!font_.is_valid()) return PP_ERROR_FAILED; base::win::ScopedCreateDC hdc(::CreateCompatibleDC(NULL)); if (!hdc.IsValid()) return PP_ERROR_FAILED; - base::win::ScopedSelectObject select_object(hdc.Get(), font_.Get()); + base::win::ScopedSelectObject select_object(hdc.Get(), font_.get()); // Get the whole font header. static const DWORD kFontHeaderSize = 12; @@ -181,14 +182,14 @@ int32_t PepperTrueTypeFontWin::GetTable(uint32_t table_tag, int32_t offset, int32_t max_data_length, std::string* data) { - if (!font_.Get()) + if (!font_.is_valid()) return PP_ERROR_FAILED; base::win::ScopedCreateDC hdc(::CreateCompatibleDC(NULL)); if (!hdc.IsValid()) return PP_ERROR_FAILED; - base::win::ScopedSelectObject select_object(hdc.Get(), font_.Get()); + base::win::ScopedSelectObject select_object(hdc.Get(), font_.get()); // Tags are byte swapped on Windows. table_tag = base::ByteSwap(table_tag); diff --git a/content/common/cursors/webcursor_aurawin.cc b/content/common/cursors/webcursor_aurawin.cc index 03509b87828716..b8b4c575c28965 100644 --- a/content/common/cursors/webcursor_aurawin.cc +++ b/content/common/cursors/webcursor_aurawin.cc @@ -30,7 +30,8 @@ ui::PlatformCursor WebCursor::GetPlatformCursor() { custom_size, hotspot, !custom_data.empty() ? &custom_data[0] : NULL, - custom_data.size()); + custom_data.size()) + .release(); return custom_cursor_; } diff --git a/printing/emf_win.cc b/printing/emf_win.cc index 8ee1d843e41bdc..4b00bca7beb2ba 100644 --- a/printing/emf_win.cc +++ b/printing/emf_win.cc @@ -104,12 +104,12 @@ class RasterBitmap { gfx::Rect bitmap_rect(raster_size); gfx::CreateBitmapHeader(raster_size.width(), raster_size.height(), &header_.bmiHeader); - bitmap_.Set(::CreateDIBSection(context_.Get(), &header_, DIB_RGB_COLORS, + bitmap_.reset(CreateDIBSection(context_.Get(), &header_, DIB_RGB_COLORS, &bits, NULL, 0)); - if (!bitmap_) + if (!bitmap_.is_valid()) NOTREACHED() << "Raster bitmap creation for printing failed"; - saved_object_ = ::SelectObject(context_.Get(), bitmap_); + saved_object_ = ::SelectObject(context_.Get(), bitmap_.get()); RECT rect = bitmap_rect.ToRECT(); ::FillRect(context_.Get(), &rect, static_cast(::GetStockObject(WHITE_BRUSH))); diff --git a/printing/image_win.cc b/printing/image_win.cc index 398fb221c4c97a..1584efb1a72be7 100644 --- a/printing/image_win.cc +++ b/printing/image_win.cc @@ -70,8 +70,8 @@ bool Image::LoadMetafile(const Metafile& metafile) { base::win::ScopedBitmap bitmap( ::CreateDIBSection(hdc.Get(), reinterpret_cast(&hdr), 0, reinterpret_cast(&bits), NULL, 0)); - DCHECK(bitmap); - base::win::ScopedSelectObject select_object(hdc.Get(), bitmap); + DCHECK(bitmap.is_valid()); + base::win::ScopedSelectObject select_object(hdc.Get(), bitmap.get()); skia::InitializeDC(hdc.Get()); diff --git a/remoting/host/desktop_shape_tracker_win.cc b/remoting/host/desktop_shape_tracker_win.cc index f3a41e93469638..60a2d2c71bcd70 100644 --- a/remoting/host/desktop_shape_tracker_win.cc +++ b/remoting/host/desktop_shape_tracker_win.cc @@ -6,6 +6,7 @@ #include +#include "base/logging.h" #include "base/memory/scoped_ptr.h" #include "base/win/scoped_gdi_object.h" #include "third_party/webrtc/modules/desktop_capture/desktop_capture_options.h" @@ -63,17 +64,18 @@ void DesktopShapeTrackerWin::RefreshDesktopShape() { } // If the shape has changed, refresh |desktop_shape_|. - if (!EqualRgn(shape_data->desktop_region, old_desktop_region_)) { - old_desktop_region_.Set(shape_data->desktop_region.release()); + if (!EqualRgn(shape_data->desktop_region.get(), old_desktop_region_.get())) { + old_desktop_region_ = shape_data->desktop_region.Pass(); // Determine the size of output buffer required to receive the region. - DWORD bytes_size = GetRegionData(old_desktop_region_, 0, nullptr); + DWORD bytes_size = GetRegionData(old_desktop_region_.get(), 0, nullptr); CHECK(bytes_size != 0); // Fetch the Windows RECTs that comprise the region. std::vector buffer(bytes_size); LPRGNDATA region_data = reinterpret_cast(buffer.data()); - DWORD result = GetRegionData(old_desktop_region_, bytes_size, region_data); + DWORD result = + GetRegionData(old_desktop_region_.get(), bytes_size, region_data); CHECK(result == bytes_size); const LPRECT rects = reinterpret_cast(®ion_data->Buffer[0]); @@ -93,8 +95,8 @@ const webrtc::DesktopRegion& DesktopShapeTrackerWin::desktop_shape() { // static BOOL DesktopShapeTrackerWin::EnumWindowsCallback(HWND window, LPARAM lparam) { EnumDesktopShapeData* data = reinterpret_cast(lparam); - HRGN desktop_region = data->desktop_region.Get(); - HRGN window_region = data->window_region.Get(); + HRGN desktop_region = data->desktop_region.get(); + HRGN window_region = data->window_region.get(); // Is the window visible? if (!IsWindow(window) || !IsWindowVisible(window) || IsIconic(window)) diff --git a/remoting/host/disconnect_window_win.cc b/remoting/host/disconnect_window_win.cc index dff48cec6f4bea..2c9270ea8e143f 100644 --- a/remoting/host/disconnect_window_win.cc +++ b/remoting/host/disconnect_window_win.cc @@ -212,7 +212,7 @@ BOOL DisconnectWindowWin::OnDialogMessage(HWND hwnd, RECT rect; GetClientRect(hwnd_, &rect); { - base::win::ScopedSelectObject border(hdc, border_pen_); + base::win::ScopedSelectObject border(hdc, border_pen_.get()); base::win::ScopedSelectObject brush(hdc, GetStockObject(NULL_BRUSH)); RoundRect(hdc, rect.left, rect.top, rect.right - 1, rect.bottom - 1, kWindowBorderRadius, kWindowBorderRadius); diff --git a/ui/gfx/icon_util.cc b/ui/gfx/icon_util.cc index b75b0c2fde5abd..1497a2908391b7 100644 --- a/ui/gfx/icon_util.cc +++ b/ui/gfx/icon_util.cc @@ -161,14 +161,15 @@ const int IconUtil::kIconDimensions[] = { const size_t IconUtil::kNumIconDimensions = arraysize(kIconDimensions); const size_t IconUtil::kNumIconDimensionsUpToMediumSize = 9; -HICON IconUtil::CreateHICONFromSkBitmap(const SkBitmap& bitmap) { +base::win::ScopedHICON IconUtil::CreateHICONFromSkBitmap( + const SkBitmap& bitmap) { // Only 32 bit ARGB bitmaps are supported. We also try to perform as many // validations as we can on the bitmap. SkAutoLockPixels bitmap_lock(bitmap); if ((bitmap.colorType() != kN32_SkColorType) || (bitmap.width() <= 0) || (bitmap.height() <= 0) || (bitmap.getPixels() == NULL)) - return NULL; + return base::win::ScopedHICON(); // We start by creating a DIB which we'll use later on in order to create // the HICON. We use BITMAPV5HEADER since the bitmap we are about to convert @@ -186,7 +187,7 @@ HICON IconUtil::CreateHICONFromSkBitmap(const SkBitmap& bitmap) { DIB_RGB_COLORS, &bits, NULL, 0); } if (!dib || !bits) - return NULL; + return base::win::ScopedHICON(); memcpy(bits, bitmap.getPixels(), bitmap.width() * bitmap.height() * 4); @@ -225,7 +226,7 @@ HICON IconUtil::CreateHICONFromSkBitmap(const SkBitmap& bitmap) { icon_info.yHotspot = 0; icon_info.hbmMask = mono_bitmap; icon_info.hbmColor = dib; - HICON icon = ::CreateIconIndirect(&icon_info); + base::win::ScopedHICON icon(CreateIconIndirect(&icon_info)); ::DeleteObject(dib); ::DeleteObject(mono_bitmap); return icon; @@ -269,12 +270,11 @@ scoped_ptr IconUtil::CreateImageFamilyFromIconResource( continue; // For everything except the Vista+ 256x256 icons, use |LoadImage()|. - HICON icon_handle = static_cast(LoadImage( + base::win::ScopedHICON icon_handle(static_cast(LoadImage( module, MAKEINTRESOURCE(resource_id), IMAGE_ICON, entry->bWidth, - entry->bHeight, LR_DEFAULTCOLOR | LR_DEFAULTSIZE)); + entry->bHeight, LR_DEFAULTCOLOR | LR_DEFAULTSIZE))); scoped_ptr bitmap( - IconUtil::CreateSkBitmapFromHICON(icon_handle)); - DestroyIcon(icon_handle); + IconUtil::CreateSkBitmapFromHICON(icon_handle.get())); result->Add(gfx::Image::CreateFrom1xBitmap(*bitmap)); } else { // 256x256 icons are stored with width and height set to 0. @@ -313,10 +313,10 @@ SkBitmap* IconUtil::CreateSkBitmapFromHICON(HICON icon) { return new SkBitmap(CreateSkBitmapFromHICONHelper(icon, icon_size)); } -HICON IconUtil::CreateCursorFromDIB(const gfx::Size& icon_size, - const gfx::Point& hotspot, - const void* dib_bits, - size_t dib_size) { +base::win::ScopedHICON IconUtil::CreateCursorFromDIB(const gfx::Size& icon_size, + const gfx::Point& hotspot, + const void* dib_bits, + size_t dib_size) { BITMAPINFO icon_bitmap_info = {}; gfx::CreateBitmapHeader( icon_size.width(), @@ -334,7 +334,7 @@ HICON IconUtil::CreateCursorFromDIB(const gfx::Size& icon_size, 0)); if (dib_size > 0) { SetDIBits(0, - bitmap_handle, + bitmap_handle.get(), 0, icon_size.height(), dib_bits, @@ -343,7 +343,7 @@ HICON IconUtil::CreateCursorFromDIB(const gfx::Size& icon_size, } HBITMAP old_bitmap = reinterpret_cast( - SelectObject(working_dc.Get(), bitmap_handle)); + SelectObject(working_dc.Get(), bitmap_handle.get())); SetBkMode(working_dc.Get(), TRANSPARENT); SelectObject(working_dc.Get(), old_bitmap); @@ -357,10 +357,10 @@ HICON IconUtil::CreateCursorFromDIB(const gfx::Size& icon_size, ii.fIcon = FALSE; ii.xHotspot = hotspot.x(); ii.yHotspot = hotspot.y(); - ii.hbmMask = mask; - ii.hbmColor = bitmap_handle; + ii.hbmMask = mask.get(); + ii.hbmColor = bitmap_handle.get(); - return CreateIconIndirect(&ii); + return base::win::ScopedHICON(CreateIconIndirect(&ii)); } SkBitmap IconUtil::CreateSkBitmapFromHICONHelper(HICON icon, diff --git a/ui/gfx/icon_util.h b/ui/gfx/icon_util.h index b1d0851e7bbefa..deb935fb16505d 100644 --- a/ui/gfx/icon_util.h +++ b/ui/gfx/icon_util.h @@ -11,6 +11,7 @@ #include "base/basictypes.h" #include "base/memory/scoped_ptr.h" +#include "base/win/scoped_gdi_object.h" #include "ui/gfx/geometry/point.h" #include "ui/gfx/geometry/size.h" #include "ui/gfx/gfx_export.h" @@ -42,18 +43,15 @@ class SkBitmap; // ... // // // Convert the bitmap into a Windows HICON -// HICON icon = IconUtil::CreateHICONFromSkBitmap(bitmap); -// if (icon == NULL) { +// base::win::ScopedHICON icon(IconUtil::CreateHICONFromSkBitmap(bitmap)); +// if (!icon.is_valid()) { // // Handle error // ... // } // // // Use the icon with a WM_SETICON message // ::SendMessage(hwnd, WM_SETICON, static_cast(ICON_BIG), -// reinterpret_cast(icon)); -// -// // Destroy the icon when we are done -// ::DestroyIcon(icon); +// reinterpret_cast(icon.get())); // /////////////////////////////////////////////////////////////////////////////// class GFX_EXPORT IconUtil { @@ -86,7 +84,7 @@ class GFX_EXPORT IconUtil { // // The client is responsible for destroying the icon when it is no longer // needed by calling ::DestroyIcon(). - static HICON CreateHICONFromSkBitmap(const SkBitmap& bitmap); + static base::win::ScopedHICON CreateHICONFromSkBitmap(const SkBitmap& bitmap); // Given a valid HICON handle representing an icon, this function converts // the icon into an SkBitmap object containing an ARGB bitmap using the @@ -136,10 +134,10 @@ class GFX_EXPORT IconUtil { // Creates a cursor of the specified size from the DIB passed in. // Returns the cursor on success or NULL on failure. - static HICON CreateCursorFromDIB(const gfx::Size& icon_size, - const gfx::Point& hotspot, - const void* dib_bits, - size_t dib_size); + static base::win::ScopedHICON CreateCursorFromDIB(const gfx::Size& icon_size, + const gfx::Point& hotspot, + const void* dib_bits, + size_t dib_size); private: // The icon format is published in the MSDN but there is no definition of diff --git a/ui/gfx/icon_util_unittest.cc b/ui/gfx/icon_util_unittest.cc index d72c2cc9651279..ffb98649da1b64 100644 --- a/ui/gfx/icon_util_unittest.cc +++ b/ui/gfx/icon_util_unittest.cc @@ -28,6 +28,8 @@ static const char kTempIconFilename[] = "temp_test_icon.ico"; class IconUtilTest : public testing::Test { public: + using ScopedHICON = base::win::ScopedHICON; + void SetUp() override { gfx::RegisterPathProvider(); ASSERT_TRUE(PathService::Get(gfx::DIR_TEST_DATA, &test_data_directory_)); @@ -41,15 +43,16 @@ class IconUtilTest : public testing::Test { // Given a file name for an .ico file and an image dimensions, this // function loads the icon and returns an HICON handle. - HICON LoadIconFromFile(const base::FilePath& filename, - int width, int height) { + ScopedHICON LoadIconFromFile(const base::FilePath& filename, + int width, + int height) { HICON icon = static_cast(LoadImage(NULL, filename.value().c_str(), IMAGE_ICON, width, height, LR_LOADTRANSPARENT | LR_LOADFROMFILE)); - return icon; + return ScopedHICON(icon); } SkBitmap CreateBlackSkBitmap(int width, int height) { @@ -87,11 +90,8 @@ void IconUtilTest::CheckAllIconSizes(const base::FilePath& icon_filename, } // First, use the Windows API to load the icon, a basic validity test. - HICON icon = LoadIconFromFile(icon_filename, kSmallIconWidth, - kSmallIconHeight); - EXPECT_NE(static_cast(NULL), icon); - if (icon != NULL) - ::DestroyIcon(icon); + EXPECT_TRUE(LoadIconFromFile(icon_filename, kSmallIconWidth, kSmallIconHeight) + .is_valid()); // Read the file completely into memory. std::string icon_data; @@ -145,14 +145,13 @@ TEST_F(IconUtilTest, TestIconToBitmapInvalidParameters) { base::FilePath icon_filename = test_data_directory_.AppendASCII(kSmallIconName); gfx::Size icon_size(kSmallIconWidth, kSmallIconHeight); - HICON icon = LoadIconFromFile(icon_filename, - icon_size.width(), - icon_size.height()); - ASSERT_TRUE(icon != NULL); + ScopedHICON icon( + LoadIconFromFile(icon_filename, icon_size.width(), icon_size.height())); + ASSERT_TRUE(icon.is_valid()); // Invalid size parameter. gfx::Size invalid_icon_size(kSmallIconHeight, 0); - EXPECT_EQ(IconUtil::CreateSkBitmapFromHICON(icon, invalid_icon_size), + EXPECT_EQ(IconUtil::CreateSkBitmapFromHICON(icon.get(), invalid_icon_size), static_cast(NULL)); // Invalid icon. @@ -161,38 +160,37 @@ TEST_F(IconUtilTest, TestIconToBitmapInvalidParameters) { // The following code should succeed. scoped_ptr bitmap; - bitmap.reset(IconUtil::CreateSkBitmapFromHICON(icon, icon_size)); + bitmap.reset(IconUtil::CreateSkBitmapFromHICON(icon.get(), icon_size)); EXPECT_NE(bitmap.get(), static_cast(NULL)); - ::DestroyIcon(icon); } // The following test case makes sure IconUtil::CreateHICONFromSkBitmap fails // gracefully when called with invalid input parameters. TEST_F(IconUtilTest, TestBitmapToIconInvalidParameters) { - HICON icon = NULL; + ScopedHICON icon; scoped_ptr bitmap; // Wrong bitmap format. bitmap.reset(new SkBitmap); ASSERT_NE(bitmap.get(), static_cast(NULL)); bitmap->setInfo(SkImageInfo::MakeA8(kSmallIconWidth, kSmallIconHeight)); - icon = IconUtil::CreateHICONFromSkBitmap(*bitmap); - EXPECT_EQ(icon, static_cast(NULL)); + icon = IconUtil::CreateHICONFromSkBitmap(*bitmap).Pass(); + EXPECT_FALSE(icon.is_valid()); // Invalid bitmap size. bitmap.reset(new SkBitmap); ASSERT_NE(bitmap.get(), static_cast(NULL)); bitmap->setInfo(SkImageInfo::MakeN32Premul(0, 0)); - icon = IconUtil::CreateHICONFromSkBitmap(*bitmap); - EXPECT_EQ(icon, static_cast(NULL)); + icon = IconUtil::CreateHICONFromSkBitmap(*bitmap).Pass(); + EXPECT_FALSE(icon.is_valid()); // Valid bitmap configuration but no pixels allocated. bitmap.reset(new SkBitmap); ASSERT_NE(bitmap.get(), static_cast(NULL)); bitmap->setInfo(SkImageInfo::MakeN32Premul(kSmallIconWidth, kSmallIconHeight)); - icon = IconUtil::CreateHICONFromSkBitmap(*bitmap); - EXPECT_TRUE(icon == NULL); + icon = IconUtil::CreateHICONFromSkBitmap(*bitmap).Pass(); + EXPECT_FALSE(icon.is_valid()); } // The following test case makes sure IconUtil::CreateIconFileFromImageFamily @@ -275,30 +273,28 @@ TEST_F(IconUtilTest, TestCreateSkBitmapFromHICON) { base::FilePath small_icon_filename = test_data_directory_.AppendASCII( kSmallIconName); gfx::Size small_icon_size(kSmallIconWidth, kSmallIconHeight); - HICON small_icon = LoadIconFromFile(small_icon_filename, - small_icon_size.width(), - small_icon_size.height()); - ASSERT_NE(small_icon, static_cast(NULL)); - bitmap.reset(IconUtil::CreateSkBitmapFromHICON(small_icon, small_icon_size)); + ScopedHICON small_icon(LoadIconFromFile( + small_icon_filename, small_icon_size.width(), small_icon_size.height())); + ASSERT_TRUE(small_icon.is_valid()); + bitmap.reset( + IconUtil::CreateSkBitmapFromHICON(small_icon.get(), small_icon_size)); ASSERT_NE(bitmap.get(), static_cast(NULL)); EXPECT_EQ(bitmap->width(), small_icon_size.width()); EXPECT_EQ(bitmap->height(), small_icon_size.height()); EXPECT_EQ(bitmap->colorType(), kN32_SkColorType); - ::DestroyIcon(small_icon); base::FilePath large_icon_filename = test_data_directory_.AppendASCII( kLargeIconName); gfx::Size large_icon_size(kLargeIconWidth, kLargeIconHeight); - HICON large_icon = LoadIconFromFile(large_icon_filename, - large_icon_size.width(), - large_icon_size.height()); - ASSERT_NE(large_icon, static_cast(NULL)); - bitmap.reset(IconUtil::CreateSkBitmapFromHICON(large_icon, large_icon_size)); + ScopedHICON large_icon(LoadIconFromFile( + large_icon_filename, large_icon_size.width(), large_icon_size.height())); + ASSERT_TRUE(large_icon.is_valid()); + bitmap.reset( + IconUtil::CreateSkBitmapFromHICON(large_icon.get(), large_icon_size)); ASSERT_NE(bitmap.get(), static_cast(NULL)); EXPECT_EQ(bitmap->width(), large_icon_size.width()); EXPECT_EQ(bitmap->height(), large_icon_size.height()); EXPECT_EQ(bitmap->colorType(), kN32_SkColorType); - ::DestroyIcon(large_icon); } // This test case makes sure that when an HICON is created from an SkBitmap, @@ -306,10 +302,10 @@ TEST_F(IconUtilTest, TestCreateSkBitmapFromHICON) { // dimensions color depth etc. TEST_F(IconUtilTest, TestBasicCreateHICONFromSkBitmap) { SkBitmap bitmap = CreateBlackSkBitmap(kSmallIconWidth, kSmallIconHeight); - HICON icon = IconUtil::CreateHICONFromSkBitmap(bitmap); - EXPECT_NE(icon, static_cast(NULL)); + ScopedHICON icon(IconUtil::CreateHICONFromSkBitmap(bitmap)); + EXPECT_TRUE(icon.is_valid()); ICONINFO icon_info; - ASSERT_TRUE(::GetIconInfo(icon, &icon_info)); + ASSERT_TRUE(GetIconInfo(icon.get(), &icon_info)); EXPECT_TRUE(icon_info.fIcon); // Now that have the icon information, we should obtain the specification of @@ -336,7 +332,6 @@ TEST_F(IconUtilTest, TestBasicCreateHICONFromSkBitmap) { EXPECT_EQ(bitmap_info.bmiHeader.biPlanes, 1); EXPECT_EQ(bitmap_info.bmiHeader.biBitCount, 32); ::ReleaseDC(NULL, hdc); - ::DestroyIcon(icon); } // This test case makes sure that CreateIconFileFromImageFamily creates a diff --git a/ui/gfx/path_win.cc b/ui/gfx/path_win.cc index e6a915b3306968..72f5191ccdf0e5 100644 --- a/ui/gfx/path_win.cc +++ b/ui/gfx/path_win.cc @@ -17,8 +17,9 @@ HRGN CreateHRGNFromSkRegion(const SkRegion& region) { for (SkRegion::Iterator i(region); !i.done(); i.next()) { const SkIRect& rect = i.rect(); - ::SetRectRgn(temp, rect.left(), rect.top(), rect.right(), rect.bottom()); - ::CombineRgn(result, result, temp, RGN_OR); + ::SetRectRgn(temp.get(), + rect.left(), rect.top(), rect.right(), rect.bottom()); + ::CombineRgn(result.get(), result.get(), temp.get(), RGN_OR); } return result.release(); diff --git a/ui/gfx/path_win_unittest.cc b/ui/gfx/path_win_unittest.cc index 1c211d7dacebe5..1e3395cd910aac 100644 --- a/ui/gfx/path_win_unittest.cc +++ b/ui/gfx/path_win_unittest.cc @@ -79,7 +79,7 @@ TEST(CreateHRGNFromSkPathTest, RoundCornerTest) { rrect.setRectXY(SkRect::MakeWH(50, 50), 20, 20); path.addRRect(rrect); base::win::ScopedRegion region(CreateHRGNFromSkPath(path)); - const std::vector& region_rects = GetRectsFromHRGN(region); + const std::vector& region_rects = GetRectsFromHRGN(region.get()); EXPECT_EQ(arraysize(rects), region_rects.size()); for (size_t i = 0; i < arraysize(rects) && i < region_rects.size(); ++i) EXPECT_EQ(rects[i], region_rects[i]); @@ -98,7 +98,7 @@ TEST(CreateHRGNFromSkPathTest, NonContiguousPath) { path.addRect(SkRect::Make(rect)); } base::win::ScopedRegion region(CreateHRGNFromSkPath(path)); - const std::vector& region_rects = GetRectsFromHRGN(region); + const std::vector& region_rects = GetRectsFromHRGN(region.get()); ASSERT_EQ(arraysize(rects), region_rects.size()); for (size_t i = 0; i < arraysize(rects); ++i) EXPECT_EQ(rects[i], region_rects[i]); @@ -109,7 +109,7 @@ TEST(CreateHRGNFromSkPathTest, EmptyPath) { Path path; base::win::ScopedRegion empty_region(::CreateRectRgn(0, 0, 0, 0)); base::win::ScopedRegion region(CreateHRGNFromSkPath(path)); - EXPECT_TRUE(::EqualRgn(empty_region, region)); + EXPECT_TRUE(::EqualRgn(empty_region.get(), region.get())); } } // namespace gfx diff --git a/ui/gfx/platform_font_win.cc b/ui/gfx/platform_font_win.cc index d271f01fb585d9..57606b86c01429 100644 --- a/ui/gfx/platform_font_win.cc +++ b/ui/gfx/platform_font_win.cc @@ -130,7 +130,7 @@ HRESULT FindDirectWriteFontForLOGFONT(IDWriteFactory* factory, // If this succeeds we return the matched font. base::win::ScopedGDIObject font(::CreateFontIndirect(font_info)); base::win::ScopedGetDC screen_dc(NULL); - base::win::ScopedSelectObject scoped_font(screen_dc, font); + base::win::ScopedSelectObject scoped_font(screen_dc, font.get()); base::win::ScopedComPtr font_face; hr = gdi_interop->CreateFontFaceFromHdc(screen_dc, font_face.Receive()); @@ -453,7 +453,7 @@ int PlatformFontWin::GetFontSize(const LOGFONT& font_info) { base::win::ScopedGDIObject font(CreateFontIndirect(&font_info)); TEXTMETRIC font_metrics = {0}; - PlatformFontWin::GetTextMetricsForFont(screen_dc, font, &font_metrics); + PlatformFontWin::GetTextMetricsForFont(screen_dc, font.get(), &font_metrics); return font_metrics.tmAscent; } diff --git a/ui/native_theme/native_theme_win.cc b/ui/native_theme/native_theme_win.cc index 91834768ca11e4..3f8fb05beba67a 100644 --- a/ui/native_theme/native_theme_win.cc +++ b/ui/native_theme/native_theme_win.cc @@ -877,7 +877,7 @@ HRESULT NativeThemeWin::PaintMenuArrow( base::win::ScopedCreateDC mem_dc(CreateCompatibleDC(hdc)); base::win::ScopedBitmap mem_bitmap(CreateCompatibleBitmap(hdc, r.width(), r.height())); - base::win::ScopedSelectObject select_bitmap(mem_dc.Get(), mem_bitmap); + base::win::ScopedSelectObject select_bitmap(mem_dc.Get(), mem_bitmap.get()); // Copy and horizontally mirror the background from hdc into mem_dc. Use // a negative-width source rect, starting at the rightmost pixel. StretchBlt(mem_dc.Get(), 0, 0, r.width(), r.height(), @@ -1604,8 +1604,9 @@ HRESULT NativeThemeWin::PaintTextField(HDC hdc, DrawEdge(hdc, rect, EDGE_SUNKEN, BF_RECT | BF_ADJUST); if (fill_content_area) { - FillRect(hdc, rect, (classic_state & DFCS_INACTIVE) ? - reinterpret_cast(COLOR_BTNFACE + 1) : bg_brush); + FillRect(hdc, rect, (classic_state & DFCS_INACTIVE) + ? reinterpret_cast(COLOR_BTNFACE + 1) + : bg_brush.get()); } return S_OK; } @@ -1625,7 +1626,7 @@ HRESULT NativeThemeWin::PaintTextField(HDC hdc, RECT content_rect; hr = get_theme_content_rect_(handle, hdc, part_id, state_id, rect, &content_rect); - FillRect(hdc, &content_rect, bg_brush); + FillRect(hdc, &content_rect, bg_brush.get()); } return hr; } @@ -1979,7 +1980,8 @@ HRESULT NativeThemeWin::PaintFrameControl(HDC hdc, return E_OUTOFMEMORY; base::win::ScopedCreateDC bitmap_dc(CreateCompatibleDC(NULL)); - base::win::ScopedSelectObject select_bitmap(bitmap_dc.Get(), mask_bitmap); + base::win::ScopedSelectObject select_bitmap(bitmap_dc.Get(), + mask_bitmap.get()); RECT local_rect = { 0, 0, width, height }; DrawFrameControl(bitmap_dc.Get(), &local_rect, type, state); diff --git a/ui/views/controls/menu/menu_config_win.cc b/ui/views/controls/menu/menu_config_win.cc index 0b69567480dfc8..48d520f8148155 100644 --- a/ui/views/controls/menu/menu_config_win.cc +++ b/ui/views/controls/menu/menu_config_win.cc @@ -28,8 +28,8 @@ void MenuConfig::Init() { l10n_util::AdjustUIFont(&(metrics.lfMenuFont)); { base::win::ScopedHFONT new_font(CreateFontIndirect(&metrics.lfMenuFont)); - DLOG_ASSERT(new_font.Get()); - font_list = gfx::FontList(gfx::Font(new_font)); + DLOG_ASSERT(new_font.is_valid()); + font_list = gfx::FontList(gfx::Font(new_font.get())); } NativeTheme::ExtraParams extra; extra.menu_check.is_radio = false; diff --git a/ui/views/widget/native_widget_aura.cc b/ui/views/widget/native_widget_aura.cc index b4044476316153..51be149fc28155 100644 --- a/ui/views/widget/native_widget_aura.cc +++ b/ui/views/widget/native_widget_aura.cc @@ -1167,7 +1167,7 @@ gfx::FontList NativeWidgetPrivate::GetWindowTitleFontList() { base::win::GetNonClientMetrics(&ncm); l10n_util::AdjustUIFont(&(ncm.lfCaptionFont)); base::win::ScopedHFONT caption_font(CreateFontIndirect(&(ncm.lfCaptionFont))); - return gfx::FontList(gfx::Font(caption_font)); + return gfx::FontList(gfx::Font(caption_font.get())); #else return gfx::FontList(); #endif diff --git a/ui/views/win/hwnd_message_handler.cc b/ui/views/win/hwnd_message_handler.cc index 9bfb90fd94e607..c3a194961f3816 100644 --- a/ui/views/win/hwnd_message_handler.cc +++ b/ui/views/win/hwnd_message_handler.cc @@ -558,7 +558,7 @@ void HWNDMessageHandler::CenterWindow(const gfx::Size& size) { } void HWNDMessageHandler::SetRegion(HRGN region) { - custom_window_region_.Set(region); + custom_window_region_.reset(region); ResetWindowRegion(true, true); } @@ -750,7 +750,7 @@ void HWNDMessageHandler::FlashFrame(bool flash) { fwi.cbSize = sizeof(fwi); fwi.hwnd = hwnd(); if (flash) { - fwi.dwFlags = custom_window_region_ ? FLASHW_TRAY : FLASHW_ALL; + fwi.dwFlags = custom_window_region_.is_valid() ? FLASHW_TRAY : FLASHW_ALL; fwi.uCount = 4; fwi.dwTimeout = 0; } else { @@ -818,7 +818,7 @@ void HWNDMessageHandler::FrameTypeChanged() { delegate_->HandleFrameChanged(); InvalidateRect(hwnd(), NULL, FALSE); } else { - if (!custom_window_region_ && !delegate_->IsUsingCustomFrame()) + if (!custom_window_region_.is_valid() && !delegate_->IsUsingCustomFrame()) dwm_transition_desired_ = true; if (!dwm_transition_desired_ || !fullscreen_handler_->fullscreen()) PerformDwmTransition(); @@ -828,23 +828,17 @@ void HWNDMessageHandler::FrameTypeChanged() { void HWNDMessageHandler::SetWindowIcons(const gfx::ImageSkia& window_icon, const gfx::ImageSkia& app_icon) { if (!window_icon.isNull()) { - HICON windows_icon = IconUtil::CreateHICONFromSkBitmap( - *window_icon.bitmap()); - // We need to make sure to destroy the previous icon, otherwise we'll leak - // these GDI objects until we crash! - HICON old_icon = reinterpret_cast( - SendMessage(hwnd(), WM_SETICON, ICON_SMALL, - reinterpret_cast(windows_icon))); - if (old_icon) - DestroyIcon(old_icon); + base::win::ScopedHICON previous_icon = window_icon_.Pass(); + window_icon_ = + IconUtil::CreateHICONFromSkBitmap(*window_icon.bitmap()).Pass(); + SendMessage(hwnd(), WM_SETICON, ICON_SMALL, + reinterpret_cast(window_icon_.get())); } if (!app_icon.isNull()) { - HICON windows_icon = IconUtil::CreateHICONFromSkBitmap(*app_icon.bitmap()); - HICON old_icon = reinterpret_cast( - SendMessage(hwnd(), WM_SETICON, ICON_BIG, - reinterpret_cast(windows_icon))); - if (old_icon) - DestroyIcon(old_icon); + base::win::ScopedHICON previous_icon = app_icon_.Pass(); + app_icon_ = IconUtil::CreateHICONFromSkBitmap(*app_icon.bitmap()).Pass(); + SendMessage(hwnd(), WM_SETICON, ICON_BIG, + reinterpret_cast(app_icon_.get())); } } @@ -1121,7 +1115,8 @@ void HWNDMessageHandler::ResetWindowRegion(bool force, bool redraw) { // automatically makes clicks on transparent pixels fall through, that isn't // the case with WS_EX_COMPOSITED. So, we route WS_EX_COMPOSITED through to // the delegate to allow for a custom hit mask. - if ((window_ex_style() & WS_EX_COMPOSITED) == 0 && !custom_window_region_ && + if ((window_ex_style() & WS_EX_COMPOSITED) == 0 && + !custom_window_region_.is_valid() && (!delegate_->IsUsingCustomFrame() || !delegate_->IsWidgetWindow())) { if (force) SetWindowRgn(hwnd(), NULL, redraw); @@ -1131,14 +1126,14 @@ void HWNDMessageHandler::ResetWindowRegion(bool force, bool redraw) { // Changing the window region is going to force a paint. Only change the // window region if the region really differs. base::win::ScopedRegion current_rgn(CreateRectRgn(0, 0, 0, 0)); - GetWindowRgn(hwnd(), current_rgn); + GetWindowRgn(hwnd(), current_rgn.get()); RECT window_rect; GetWindowRect(hwnd(), &window_rect); base::win::ScopedRegion new_region; - if (custom_window_region_) { - new_region.Set(::CreateRectRgn(0, 0, 0, 0)); - ::CombineRgn(new_region, custom_window_region_.Get(), NULL, RGN_COPY); + if (custom_window_region_.is_valid()) { + new_region.reset(CreateRectRgn(0, 0, 0, 0)); + CombineRgn(new_region.get(), custom_window_region_.get(), NULL, RGN_COPY); } else if (IsMaximized()) { HMONITOR monitor = MonitorFromWindow(hwnd(), MONITOR_DEFAULTTONEAREST); MONITORINFO mi; @@ -1146,20 +1141,20 @@ void HWNDMessageHandler::ResetWindowRegion(bool force, bool redraw) { GetMonitorInfo(monitor, &mi); RECT work_rect = mi.rcWork; OffsetRect(&work_rect, -window_rect.left, -window_rect.top); - new_region.Set(CreateRectRgnIndirect(&work_rect)); + new_region.reset(CreateRectRgnIndirect(&work_rect)); } else { gfx::Path window_mask; delegate_->GetWindowMask(gfx::Size(window_rect.right - window_rect.left, window_rect.bottom - window_rect.top), &window_mask); if (!window_mask.isEmpty()) - new_region.Set(gfx::CreateHRGNFromSkPath(window_mask)); + new_region.reset(gfx::CreateHRGNFromSkPath(window_mask)); } const bool has_current_region = current_rgn != 0; const bool has_new_region = new_region != 0; if (has_current_region != has_new_region || - (has_current_region && !EqualRgn(current_rgn, new_region))) { + (has_current_region && !EqualRgn(current_rgn.get(), new_region.get()))) { // SetWindowRgn takes ownership of the HRGN. SetWindowRgn(hwnd(), new_region.release(), redraw); } @@ -1173,8 +1168,9 @@ void HWNDMessageHandler::UpdateDwmNcRenderingPolicy() { return; DWMNCRENDERINGPOLICY policy = - custom_window_region_ || delegate_->IsUsingCustomFrame() ? - DWMNCRP_DISABLED : DWMNCRP_ENABLED; + custom_window_region_.is_valid() || delegate_->IsUsingCustomFrame() + ? DWMNCRP_DISABLED + : DWMNCRP_ENABLED; DwmSetWindowAttribute(hwnd(), DWMWA_NCRENDERING_POLICY, &policy, sizeof(DWMNCRENDERINGPOLICY)); diff --git a/ui/views/win/hwnd_message_handler.h b/ui/views/win/hwnd_message_handler.h index 15a0ac2084b071..7dcda04b51f300 100644 --- a/ui/views/win/hwnd_message_handler.h +++ b/ui/views/win/hwnd_message_handler.h @@ -523,6 +523,12 @@ class VIEWS_EXPORT HWNDMessageHandler : // so that we can restore it. HCURSOR previous_cursor_; + // The icon created from the bitmap image of the window icon. + base::win::ScopedHICON window_icon_; + + // The icon created from the bitmap image of the app icon. + base::win::ScopedHICON app_icon_; + // Event handling ------------------------------------------------------------ // The flags currently being used with TrackMouseEvent to track mouse