OWLNext    7.0
Borland's Object Windows Library for the modern age
Loading...
Searching...
No Matches
icon.cpp
Go to the documentation of this file.
1//----------------------------------------------------------------------------
2// ObjectWindows
3// Copyright (c) 1992, 1996 by Borland International, All Rights Reserved
4//
5/// \file
6/// Implementation of class TIcon, a GDI Icon object encapsulation
7//----------------------------------------------------------------------------
8#include <owl/pch.h>
9#include <owl/gdiobjec.h>
10#include <owl/shellitm.h>
11
12namespace owl {
13
15DIAG_DECLARE_GROUP(OwlGDI); // General GDI diagnostic group
16
17//
18/// Creates a TIcon object and sets the Handle data member to the given borrowed
19/// handle. The ShouldDelete data member defaults to false, ensuring that the
20/// borrowed handle will not be deleted when the C++ object is destroyed.
21//
27
28//
29/// Creates a copy of the given icon object.
30//
32{
33 Handle = ::CopyIcon(icon);
34 CheckValid();
35}
36
37//
38/// Creates an icon object from the given resource.
39//
41{
42 Handle = ::LoadIcon(instance, resId);
43 ShouldDelete = (instance != nullptr);
44 CheckValid();
45}
46
47//
48/// Creates an icon object from the given resource file.
49//
51{
53
54 if (reinterpret_cast<INT_PTR>(Handle) == 1)
55 Handle = nullptr;
56 CheckValid();
57}
58
59//
60/// Creates an icon object with the given values.
61//
63 const void * andBits, const void * xorBits)
64{
65 Handle = ::CreateIcon(instance, size.cx, size.cy,
67 static_cast<const uint8 *>(andBits), static_cast<const uint8 *>(xorBits));
68 CheckValid();
69}
70
71//
72/// Creates an icon object of the given size from the bits found in the resBits
73/// buffer.
74//
76{
77 Handle = CreateIconFromResource((PBYTE)resBits, resSize, true, 0x00030000);
78 CheckValid();
79}
80
81//
82/// Creates an icon object with the given ICONINFO information.
83//
85{
86 WARN(!iconInfo.fIcon, "TIcon constructor called with ICONINFO::fIcon == false"); // Turn this into a precondition?
87 ICONINFO i = iconInfo; // Make a clone, since CreateIconIndirect is not const-correct.
89 CheckValid();
90}
91
92#if defined(OWL5_COMPAT)
93
94//
95/// Creates an icon object with the given ICONINFO information.
96/// This overload is deprecated. Use the overload that takes a reference instead.
97//
99{
100 //IconInfo->fIcon = true; // assume the user setup the struct OK
102 CheckValid();
103}
104
105#endif
106
107//
108/// Destroys the icon and frees any memory that the icon occupied.
109//
111{
112 if (ShouldDelete && Handle)
113 ::DestroyIcon(static_cast<HICON>(Handle));
114}
115
116//
117/// Retrieves information about this icon.
118/// Throws TXGdi on failure.
119//
120auto TIcon::GetInfo() const -> TInfo
121{
122 auto i = GetIconInfoEx();
123 CHECK(i.fIcon != FALSE);
124 i.szModName[MAX_PATH - 1] = _T('\0'); // Ensure termination.
125 i.szResName[MAX_PATH - 1] = _T('\0'); // Ensure termination.
126 return TInfo
127 {
128 TPoint{static_cast<int>(i.xHotspot), static_cast<int>(i.yHotspot)},
129 TBitmap{i.hbmMask, AutoDelete}, // Takes ownership.
130 TBitmap{i.hbmColor, AutoDelete}, // Takes ownership.
131 i.wResID,
132 tstring{&i.szModName[0]},
133 tstring{&i.szResName[0]}
134 };
135}
136
137//
138/// Retrieves information about this icon.
139/// Wrapper for the Windows API function of the same name. Throws TXGdi on failure.
140///
141/// \note GetIconInfo creates bitmaps for the hbmMask and hbmColor members of ICONINFO. The calling
142/// application must manage these bitmaps and delete them when they are no longer necessary (using
143/// the Windows API function ::DeleteObject explicitly, or by passing ownership of the handles to
144/// TBitmap with the parameter `autoDelete` set to TAutoDelete::AutoDelete).
145///
146/// \sa http://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-geticoninfo
147//
149{
150 auto i = ICONINFO{};
151 const auto r = ::GetIconInfo(GetHandle(), &i);
152 if (!r || i.fIcon == FALSE) throw TXGdi(IDS_GDIFAILURE, GetHandle());
153 return i;
154}
155
156//
157/// Retrieves information about this icon.
158/// Wrapper for the Windows API function of the same name. Throws TXGdi on failure.
159///
160/// \note GetIconInfoEx creates bitmaps for the hbmMask and hbmColor members of ICONINFOEX. The calling
161/// application must manage these bitmaps and delete them when they are no longer necessary (using
162/// the Windows API function ::DeleteObject explicitly, or by passing ownership of the handles to
163/// TBitmap with the parameter `autoDelete` set to TAutoDelete::AutoDelete).
164///
165/// \sa http://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-geticoninfoexa
166//
168{
169 auto i = ICONINFOEX{sizeof(ICONINFOEX)};
170 const auto r = ::GetIconInfoEx(GetHandle(), &i);
171 if (!r || i.fIcon == FALSE) throw TXGdi(IDS_GDIFAILURE, GetHandle());
172 return i;
173}
174
175} // OWL namespace
176/* ========================================================================== */
177
#define CHECK(condition)
Definition checks.h:239
#define WARN(condition, message)
Definition checks.h:273
#define DIAG_DECLARE_GROUP(group)
Definition checks.h:404
TBitmap is the GDI bitmap class derived from TGdiObject.
Definition gdiobjec.h:510
Root and abstract class for Windows object wrappers.
Definition gdibase.h:79
void CheckValid(uint resId=IDS_GDIFAILURE)
Definition gdibase.cpp:49
bool ShouldDelete
Should object delete GDI handle in dtor?
Definition gdibase.h:82
HANDLE Handle
GDI handle of this object.
Definition gdibase.h:81
TIcon, derived from TGdiObject, represents the GDI object icon class.
Definition gdiobjec.h:670
auto GetIconInfo() const -> ICONINFO
Retrieves information about this icon.
Definition icon.cpp:148
TIcon(HICON handle, TAutoDelete autoDelete=NoAutoDelete)
Creates a TIcon object and sets the Handle data member to the given borrowed handle.
Definition icon.cpp:22
auto GetIconInfoEx() const -> ICONINFOEX
Retrieves information about this icon.
Definition icon.cpp:167
auto GetInfo() const -> TInfo
Retrieves information about this icon.
Definition icon.cpp:120
~TIcon()
Destroys the icon and frees any memory that the icon occupied.
Definition icon.cpp:110
HICON GetHandle() const
Returns the handle of the icon with type HICON.
Definition gdiobjec.h:1574
TPoint is a support class, derived from tagPOINT.
Definition geometry.h:87
static HICON ExtractIcon(HINSTANCE, LPCTSTR, UINT)
Invokes 'ExtractIcon' indirectly.
Definition shellitm.cpp:114
The tagSIZE struct is defined as.
Definition geometry.h:234
Describes an exception resulting from GDI failures such as creating too many TWindow device contexts ...
Definition gdibase.h:52
#define MAX_PATH
Definition cygwin.h:98
#define _T(x)
Definition cygwin.h:51
Definition of abstract GDI object class and derived classes.
TAutoDelete
Flag for Handle ctors to control Handle deletion in dtor.
Definition gdibase.h:70
@ AutoDelete
Definition gdibase.h:70
Object Windows Library (OWLNext Core)
Definition animctrl.h:22
unsigned char uint8
Definition number.h:32
unsigned long uint32
Definition number.h:34
OWL_DIAGINFO
Definition animctrl.cpp:14
std::string tstring
Definition defs.h:79
Definitions of Win95 Shell Clases: TShellItem, TShellItemIterator, TPidl, TShellMalloc.