ui/winui/icons.cpp

changeset 0
2483f517c562
equal deleted inserted replaced
-1:000000000000 0:2483f517c562
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2017 Olaf Wintermann. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include "pch.h"
30
31 #include "icons.h"
32
33 #include <stdlib.h>
34 #include <string.h>
35
36 #include "util.h"
37
38 #include <Windows.h>
39 #include <Shellapi.h>
40
41
42 using namespace winrt;
43 using namespace Microsoft::UI::Xaml;
44 using namespace Microsoft::UI::Xaml::Controls;
45 using namespace Windows::UI::Xaml::Interop;
46 using namespace winrt::Windows::Foundation;
47 using namespace winrt::Microsoft::UI::Xaml::Controls::Primitives;
48 using namespace winrt::Microsoft::UI::Xaml::Media::Imaging;
49 //using namespace Windows::Storage::Streams;
50
51 static UiIcon* sys_folder_icon16;
52 static UiIcon* sys_file_icon16;
53
54 static UiIcon* sys_folder_icon32;
55 static UiIcon* sys_file_icon32;
56
57 std::unordered_map<std::string, Symbol> ui_symbol_icons = {
58 {"Accept", Symbol::Accept },
59 {"Account", Symbol::Account },
60 {"Add", Symbol::Add },
61 {"AddFriend", Symbol::AddFriend },
62 {"Admin", Symbol::Admin },
63 {"AlignCenter", Symbol::AlignCenter },
64 {"AlignLeft", Symbol::AlignLeft },
65 {"AlignRight", Symbol::AlignRight },
66 {"AllApps", Symbol::AllApps },
67 {"Attach", Symbol::Attach },
68 {"AttachCamera", Symbol::AttachCamera },
69 {"Audio", Symbol::Audio },
70 {"Back", Symbol::Back },
71 {"BackToWindow", Symbol::BackToWindow },
72 {"BlockContact", Symbol::BlockContact },
73 {"Bold", Symbol::Bold },
74 {"Bookmarks", Symbol::Bookmarks },
75 {"BrowsePhotos", Symbol::BrowsePhotos },
76 {"Bullets", Symbol::Bullets },
77 {"Calculator", Symbol::Calculator },
78 {"Calendar", Symbol::Calendar },
79 {"CalendarDay", Symbol::CalendarDay },
80 {"CalendarReply", Symbol::CalendarReply },
81 {"CalendarWeek", Symbol::CalendarWeek },
82 {"Camera", Symbol::Camera },
83 {"Cancel", Symbol::Cancel },
84 {"Caption", Symbol::Caption },
85 {"CellPhone", Symbol::CellPhone },
86 {"Character", Symbol::Character },
87 {"Clear", Symbol::Clear },
88 {"ClearSelection", Symbol::ClearSelection },
89 {"Clock", Symbol::Clock },
90 {"ClosedCaption", Symbol::ClosedCaption },
91 {"ClosePane", Symbol::ClosePane },
92 {"Comment", Symbol::Comment },
93 {"Contact", Symbol::Contact },
94 {"Contact2", Symbol::Contact2 },
95 {"ContactInfo", Symbol::ContactInfo },
96 {"ContactPresence", Symbol::ContactPresence },
97 {"Copy", Symbol::Copy },
98 {"Crop", Symbol::Crop },
99 {"Cut", Symbol::Cut },
100 {"Delete", Symbol::Delete },
101 {"Directions", Symbol::Directions },
102 {"DisableUpdates", Symbol::DisableUpdates },
103 {"DisconnectDrive", Symbol::DisconnectDrive },
104 {"Dislike", Symbol::Dislike },
105 {"DockBottom", Symbol::DockBottom },
106 {"DockLeft", Symbol::DockLeft },
107 {"DockRight", Symbol::DockRight },
108 {"Document", Symbol::Document },
109 {"Download", Symbol::Download },
110 {"Edit", Symbol::Edit },
111 {"Emoji", Symbol::Emoji },
112 {"Emoji2", Symbol::Emoji2 },
113 {"Favorite", Symbol::Favorite },
114 {"Filter", Symbol::Filter },
115 {"Find", Symbol::Find },
116 {"Flag", Symbol::Flag },
117 {"Folder", Symbol::Folder },
118 {"Font", Symbol::Font },
119 {"FontColor", Symbol::FontColor },
120 {"FontDecrease", Symbol::FontDecrease },
121 {"FontIncrease", Symbol::FontIncrease },
122 {"FontSize", Symbol::FontSize },
123 {"Forward", Symbol::Forward },
124 {"FourBars", Symbol::FourBars },
125 {"FullScreen", Symbol::FullScreen },
126 {"GlobalNavigationButton", Symbol::GlobalNavigationButton },
127 {"Globe", Symbol::Globe },
128 {"Go", Symbol::Go },
129 {"GoToStart", Symbol::GoToStart },
130 {"GoToToday", Symbol::GoToToday },
131 {"HangUp", Symbol::HangUp },
132 {"Help", Symbol::Help },
133 {"HideBcc", Symbol::HideBcc },
134 {"Highlight", Symbol::Highlight },
135 {"Home", Symbol::Home },
136 {"Import", Symbol::Import },
137 {"ImportAll", Symbol::ImportAll },
138 {"Important", Symbol::Important },
139 {"Italic", Symbol::Italic },
140 {"Keyboard", Symbol::Keyboard },
141 {"LeaveChat", Symbol::LeaveChat },
142 {"Library", Symbol::Library },
143 {"Like", Symbol::Like },
144 {"LikeDislike", Symbol::LikeDislike },
145 {"Link", Symbol::Link },
146 {"List", Symbol::List },
147 {"Mail", Symbol::Mail },
148 {"MailFilled", Symbol::MailFilled },
149 {"MailForward", Symbol::MailForward },
150 {"MailReply", Symbol::MailReply },
151 {"MailReplyAll", Symbol::MailReplyAll },
152 {"Manage", Symbol::Manage },
153 {"Map", Symbol::Map },
154 {"MapDrive", Symbol::MapDrive },
155 {"MapPin", Symbol::MapPin },
156 {"Memo", Symbol::Memo },
157 {"Message", Symbol::Message },
158 {"Microphone", Symbol::Microphone },
159 {"More", Symbol::More },
160 {"MoveToFolder", Symbol::MoveToFolder },
161 {"MusicInfo", Symbol::MusicInfo },
162 {"Mute", Symbol::Mute },
163 {"NewFolder", Symbol::NewFolder },
164 {"NewWindow", Symbol::NewWindow },
165 {"Next", Symbol::Next },
166 {"OneBar", Symbol::OneBar },
167 {"OpenFile", Symbol::OpenFile },
168 {"OpenLocal", Symbol::OpenLocal },
169 {"OpenPane", Symbol::OpenPane },
170 {"OpenWith", Symbol::OpenWith },
171 {"Orientation", Symbol::Orientation },
172 {"OtherUser", Symbol::OtherUser },
173 {"OutlineStar", Symbol::OutlineStar },
174 {"Page", Symbol::Page },
175 {"Page2", Symbol::Page2 },
176 {"Paste", Symbol::Paste },
177 {"Pause", Symbol::Pause },
178 {"People", Symbol::People },
179 {"Permissions", Symbol::Permissions },
180 {"Phone", Symbol::Phone },
181 {"PhoneBook", Symbol::PhoneBook },
182 {"Pictures", Symbol::Pictures },
183 {"Pin", Symbol::Pin },
184 {"Placeholder", Symbol::Placeholder },
185 {"Play", Symbol::Play },
186 {"PostUpdate", Symbol::PostUpdate },
187 {"Preview", Symbol::Preview },
188 {"PreviewLink", Symbol::PreviewLink },
189 {"Previous", Symbol::Previous },
190 {"Print", Symbol::Print },
191 {"Priority", Symbol::Priority },
192 {"ProtectedDocument", Symbol::ProtectedDocument },
193 {"Read", Symbol::Read },
194 {"Redo", Symbol::Redo },
195 {"Refresh", Symbol::Refresh },
196 {"Remote", Symbol::Remote },
197 {"Remove", Symbol::Remove },
198 {"Rename", Symbol::Rename },
199 {"Repair", Symbol::Repair },
200 {"RepeatAll", Symbol::RepeatAll },
201 {"RepeatOne", Symbol::RepeatOne },
202 {"ReportHacked", Symbol::ReportHacked },
203 {"ReShare", Symbol::ReShare },
204 {"Rotate", Symbol::Rotate },
205 {"RotateCamera", Symbol::RotateCamera },
206 {"Save", Symbol::Save },
207 {"SaveLocal", Symbol::SaveLocal },
208 {"Scan", Symbol::Scan },
209 {"SelectAll", Symbol::SelectAll },
210 {"Send", Symbol::Send },
211 {"SetLockScreen", Symbol::SetLockScreen },
212 {"SetTile", Symbol::SetTile },
213 {"Setting", Symbol::Setting },
214 {"Share", Symbol::Share },
215 {"Shop", Symbol::Shop },
216 {"ShowBcc", Symbol::ShowBcc },
217 {"ShowResults", Symbol::ShowResults },
218 {"Shuffle", Symbol::Shuffle },
219 {"SlideShow", Symbol::SlideShow },
220 {"SolidStar", Symbol::SolidStar },
221 {"Sort", Symbol::Sort },
222 {"Stop", Symbol::Stop },
223 {"StopSlideShow", Symbol::StopSlideShow },
224 {"Street", Symbol::Street },
225 {"Switch", Symbol::Switch },
226 {"SwitchApps", Symbol::SwitchApps },
227 {"Sync", Symbol::Sync },
228 {"SyncFolder", Symbol::SyncFolder },
229 {"Tag", Symbol::Tag },
230 {"Target", Symbol::Target },
231 {"ThreeBars", Symbol::ThreeBars },
232 {"TouchPointer", Symbol::TouchPointer },
233 {"Trim", Symbol::Trim },
234 {"TwoBars", Symbol::TwoBars },
235 {"TwoPage", Symbol::TwoPage },
236 {"Underline", Symbol::Underline },
237 {"Undo", Symbol::Undo },
238 {"UnFavorite", Symbol::UnFavorite },
239 {"UnPin", Symbol::UnPin },
240 {"UnSyncFolder", Symbol::UnSyncFolder },
241 {"Up", Symbol::Up },
242 {"Upload", Symbol::Upload },
243 {"Video", Symbol::Video },
244 {"VideoChat", Symbol::VideoChat },
245 {"View", Symbol::View },
246 {"ViewAll", Symbol::ViewAll },
247 {"Volume", Symbol::Volume },
248 {"WebCam", Symbol::WebCam },
249 {"World", Symbol::World },
250 {"XboxOneConsole", Symbol::XboxOneConsole },
251 {"ZeroBars", Symbol::ZeroBars },
252 {"Zoom", Symbol::Zoom },
253 {"ZoomIn", Symbol::ZoomIn },
254 {"ZoomOut", Symbol::ZoomOut }
255 };
256
257 winrt::Microsoft::UI::Xaml::Controls::IconElement ui_get_icon(const char* name) {
258 if (ui_symbol_icons.find(name) == ui_symbol_icons.end()) {
259 SymbolIcon no_icon = { nullptr };
260 return no_icon;
261 }
262
263 Symbol symbol = ui_symbol_icons[name];
264 SymbolIcon icon = SymbolIcon(symbol);
265 return icon;
266 }
267
268
269 // symbol icon implementation
270 UiSymbolIcon::UiSymbolIcon(winrt::Microsoft::UI::Xaml::Controls::Symbol sym) {
271 symbol = sym;
272 }
273
274 UiSymbolIcon::~UiSymbolIcon() {
275
276 }
277
278 winrt::Microsoft::UI::Xaml::Controls::IconElement UiSymbolIcon::getIcon() {
279 return SymbolIcon(symbol);
280 }
281
282 // image icon implementation
283 UiImageIcon::UiImageIcon(const char* uristr) {
284 wchar_t* wuri = str2wstr(uristr, nullptr);
285 Windows::Foundation::Uri uri{ wuri };
286 this->uri = uri;
287 free(wuri);
288 }
289
290 UiImageIcon::~UiImageIcon() {
291
292 }
293
294 winrt::Microsoft::UI::Xaml::Controls::IconElement UiImageIcon::getIcon() {
295 BitmapIcon icon = BitmapIcon();
296 icon.UriSource(uri);
297 ImageIcon img = ImageIcon();
298 img.Source();
299 return icon;
300 }
301
302 // bitmap icon implementation
303 UiBitmapIcon::UiBitmapIcon(winrt::Microsoft::UI::Xaml::Media::Imaging::BitmapSource bitmap) {
304 this->bitmap = bitmap;
305 }
306
307 UiBitmapIcon::~UiBitmapIcon() {
308
309 }
310
311 winrt::Microsoft::UI::Xaml::Controls::IconElement UiBitmapIcon::getIcon() {
312 ImageIcon icon = ImageIcon();
313 icon.Source(bitmap);
314 return icon;
315 }
316
317 UIEXPORT UiIcon* ui_icon(const char* name, size_t size) {
318 Symbol symbol = ui_symbol_icons[name];
319 UiSymbolIcon* icon = new UiSymbolIcon(symbol);
320 return icon;
321 }
322
323
324 UIEXPORT UiIcon* ui_imageicon(const char* file) {
325 return new UiImageIcon(file);
326 }
327
328 UIEXPORT void ui_icon_free(UiIcon* icon) {
329 delete icon;
330 }
331
332
333 struct __declspec(uuid("905a0fef-bc53-11df-8c49-001e4fc686da")) IBufferByteAccess : ::IUnknown
334 {
335 virtual HRESULT __stdcall Buffer(uint8_t** value) = 0;
336 };
337
338
339
340 winrt::Microsoft::UI::Xaml::Media::Imaging::WriteableBitmap ui_dllicon2bitmap(const char* dll, int iconindex, bool large) {
341 WriteableBitmap wbitmap = { nullptr };
342
343 // get the icon from the dll
344 HICON hicon_small;
345 HICON hicon_large;
346 if (ExtractIconExA(dll, iconindex, &hicon_large, &hicon_small, 1) > 0) {
347 HICON hicon = large ? hicon_large : hicon_small;
348
349 // convert icon to (gdi) bitmap
350 ICONINFO info;
351 info.hbmColor = nullptr;
352 info.hbmMask = nullptr;
353 if (GetIconInfo(hicon, &info)) {
354 BITMAP bitmap;
355 if (GetObjectW(info.hbmColor, sizeof(BITMAP), &bitmap) != 0) {
356 size_t bitmap_size = bitmap.bmWidthBytes * bitmap.bmHeight;
357 char *bitmap_data = (char*)malloc(bitmap_size);
358
359 // get the pixel data
360 if (GetBitmapBits(info.hbmColor, bitmap_size, bitmap_data) != 0) {
361 WriteableBitmap wb = WriteableBitmap(bitmap.bmWidth, bitmap.bmHeight);
362 void *wb_data = wb.PixelBuffer().data();
363 memcpy(wb_data, bitmap_data, bitmap_size);
364 wbitmap = wb;
365 }
366 free(bitmap_data);
367 }
368 if (info.hbmMask) {
369 DeleteObject(info.hbmMask);
370 }
371 if (info.hbmColor) {
372 DeleteObject(info.hbmColor);
373 }
374 }
375
376 DestroyIcon(hicon_small);
377 DestroyIcon(hicon_large);
378 }
379
380 return wbitmap;
381 }
382
383 UiIcon* ui_dllicon(const char* dll, int iconindex, bool large) {
384 WriteableBitmap wbitmap = ui_dllicon2bitmap(dll, iconindex, large);
385 return new UiBitmapIcon(wbitmap);
386 }
387
388 UIEXPORT UiIcon* ui_foldericon(size_t size) {
389 bool large = true;
390 UiIcon** sys_folder_icon = &sys_folder_icon32;
391 if (size <= 24) {
392 large = false;
393 sys_folder_icon = &sys_folder_icon16;
394 }
395
396 if (*sys_folder_icon) {
397 return *sys_folder_icon;
398 }
399
400 UiIcon* icon = ui_dllicon("shell32.dll", 3, large);
401 *sys_folder_icon = icon;
402 return icon;
403 }
404
405 UIEXPORT UiIcon* ui_fileicon(size_t size) {
406 bool large = true;
407 UiIcon** sys_folder_icon = &sys_file_icon32;
408 if (size <= 24) {
409 large = false;
410 sys_folder_icon = &sys_file_icon16;
411 }
412
413 if (*sys_folder_icon) {
414 return *sys_folder_icon;
415 }
416
417 UiIcon* icon = ui_dllicon("shell32.dll", 0, large);
418 *sys_folder_icon = icon;
419 return icon;
420 }

mercurial