forked from alecmus/lecui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
form_common.h
651 lines (550 loc) · 22.7 KB
/
form_common.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
//
// form_common.h - common form functions
//
// lecui user interface library, part of the liblec library
// Copyright (c) 2019 Alec Musasa (alecmus at live dot com)
//
// Released under the MIT license. For full details see the
// file LICENSE.txt
//
#pragma once
#include "form.h"
#include <Windows.h>
#include <d2d1.h>
#include <dwrite.h>
#include <wincodec.h>
#include <iostream>
#include <sstream>
namespace liblec {
namespace lecui {
static inline void log(const std::string& string) {
#if defined(_DEBUG)
std::string _string = "-->" + string + "\n";
printf(_string.c_str());
OutputDebugStringA(_string.c_str());
#endif
}
template<class Interface>
static inline void safe_release(Interface** pp_interface_to_release) {
if (*pp_interface_to_release != nullptr) {
(*pp_interface_to_release)->Release();
(*pp_interface_to_release) = nullptr;
}
}
/// <summary>Adjust a rectangle to a given DPI scale.</summary>
/// <param name="rc">The coordinates of the rectangle under 96dpi.</param>
/// <param name="DPIScale">The ratio of the current dpi to 96dpi.</param>
/// <remarks>DPIScale is 1.0 when the current setting is 96dpi.</remarks>
static inline void scale_RECT(RECT& rc, const float& DPIScale) {
int iUnscaledW = rc.right - rc.left;
int iUnscaledH = rc.bottom - rc.top;
rc.left = int(.5f + rc.left * DPIScale);
rc.top = int(.5f + rc.top * DPIScale);
rc.right = rc.left + int(.5f + iUnscaledW * DPIScale);
rc.bottom = rc.top + int(.5f + iUnscaledH * DPIScale);
}
/// <summary>Adjust a rectangle to a given DPI scale.</summary>
/// <param name="rc">The coordinates of the rectangle under 96dpi.</param>
/// <param name="DPIScale">The ratio of the current dpi to 96dpi.</param>
/// <remarks>DPIScale is 1.0 when the current setting is 96dpi.</remarks>
static inline void scale_RECT(D2D1_RECT_F& rc, const float& DPIScale) {
float iUnscaledW = rc.right - rc.left;
float iUnscaledH = rc.bottom - rc.top;
rc.left = rc.left * DPIScale;
rc.top = rc.top * DPIScale;
rc.right = rc.left + iUnscaledW * DPIScale;
rc.bottom = rc.top + iUnscaledH * DPIScale;
}
/// <summary>Adjust a rectangle to a scale of 96dpi.</summary>
/// <param name="rc">The coordinates of the rectangle under the current dpi.</param>
/// <param name="DPIScale">The ratio of the current dpi to 96dpi.</param>
/// <remarks>DPIScale is 1.0 when the current setting is 96dpi.</remarks>
static inline void unscale_RECT(RECT& rc, const float& DPIScale) {
int iScaledW = rc.right - rc.left;
int iScaledH = rc.bottom - rc.top;
rc.left = int(.5f + rc.left / DPIScale);
rc.top = int(.5f + rc.top / DPIScale);
rc.right = rc.left + int(.5f + iScaledW / DPIScale);
rc.bottom = rc.top + int(.5f + iScaledH / DPIScale);
}
/// <summary>Adjust a rectangle to a scale of 96dpi.</summary>
/// <param name="rc">The coordinates of the rectangle under the current dpi.</param>
/// <param name="DPIScale">The ratio of the current dpi to 96dpi.</param>
/// <remarks>DPIScale is 1.0 when the current setting is 96dpi.</remarks>
static inline void unscale_RECT(D2D1_RECT_F& rc, const float& DPIScale) {
float iScaledW = rc.right - rc.left;
float iScaledH = rc.bottom - rc.top;
rc.left = rc.left / DPIScale;
rc.top = rc.top / DPIScale;
rc.right = rc.left + iScaledW / DPIScale;
rc.bottom = rc.top + iScaledH / DPIScale;
}
static inline std::wstring convert_string(const std::string& input) {
int s_length = (int)input.length() + 1;
int len = MultiByteToWideChar(CP_ACP, 0, input.c_str(), s_length, 0, 0);
wchar_t* buffer = new wchar_t[len];
MultiByteToWideChar(CP_ACP, 0, input.c_str(), s_length, buffer, len);
std::wstring output(buffer);
delete[] buffer;
return output;
}
static inline std::string convert_string(const std::wstring& input) {
int s_length = (int)input.length() + 1;
int len = WideCharToMultiByte(CP_ACP, 0, input.c_str(), s_length, 0, 0, 0, 0);
char* buffer = new char[len];
WideCharToMultiByte(CP_ACP, 0, input.c_str(), s_length, buffer, len, 0, 0);
std::string output(buffer);
delete[] buffer;
return output;
}
static inline D2D1::ColorF convert_color(const color& _color) {
return D2D1::ColorF(static_cast<float>(_color.get_red()) / 255.f,
static_cast<float>(_color.get_green()) / 255.f,
static_cast<float>(_color.get_blue()) / 255.f,
static_cast<float>(_color.get_alpha()) / 255.f
);
}
static inline float convert_fontsize_to_dip(const float& points) {
return points * 96.f / 72.f;
}
/// <summary>position rect_subject within rect_reference,
/// perc_h% horizontally and perc_v% vertically</summary>
static inline void pos_rect(const D2D1_RECT_F& rect_reference,
D2D1_RECT_F& rect_subject, const float& perc_h, const float& perc_v) {
D2D1_RECT_F rect_original = rect_subject;
const auto delta_x = (rect_reference.right - rect_reference.left) - (rect_original.right - rect_original.left);
rect_subject.left = rect_reference.left + (perc_h * delta_x) / 100.f;
const auto delta_y = (rect_reference.bottom - rect_reference.top) - (rect_original.bottom - rect_original.top);
rect_subject.top = rect_reference.top + (perc_v * delta_y) / 100.f;
rect_subject.right = rect_subject.left + (rect_original.right - rect_original.left);
rect_subject.bottom = rect_subject.top + (rect_original.bottom - rect_original.top);
}
/// <summary>position rect_subject within rect_reference,
/// perc_h% horizontally and perc_v% vertically</summary>
static inline void pos_rect(const lecui::rect& rect_reference,
lecui::rect& rect_subject, const float& perc_h, const float& perc_v) {
lecui::rect rect_original = rect_subject;
const auto delta_x = (rect_reference.get_right() - rect_reference.get_left()) - (rect_original.right() - rect_original.left());
rect_subject.left() = rect_reference.get_left() + (perc_h * delta_x) / 100.f;
const auto delta_y = (rect_reference.get_bottom() - rect_reference.get_top()) - (rect_original.bottom() - rect_original.top());
rect_subject.top(rect_reference.get_top() + (perc_v * delta_y) / 100.f);
rect_subject.right(rect_subject.left() + (rect_original.right() - rect_original.left()));
rect_subject.bottom(rect_subject.top() + (rect_original.bottom() - rect_original.top()));
}
/// <summary>position rect_subject within rect_reference,
/// perc_h% horizontally and perc_v% vertically</summary>
template <class t>
static inline void pos_rect(const t& rect_reference,
t& rect_subject, const float& perc_h, const float& perc_v) {
t rect_original = rect_subject;
const auto delta_x = (rect_reference.right - rect_reference.left) - (rect_original.right - rect_original.left);
rect_subject.left = rect_reference.left + (perc_h * delta_x) / 100.f;
const auto delta_y = (rect_reference.bottom - rect_reference.top) - (rect_original.bottom - rect_original.top);
rect_subject.top = rect_reference.top + (perc_v * delta_y) / 100.f;
rect_subject.right = rect_subject.left + (rect_original.right - rect_original.left);
rect_subject.bottom = rect_subject.top + (rect_original.bottom - rect_original.top);
}
/// <summary>position D within C to reflect how B is positioned in A.</summary>
static inline void position_h_scrollbar(const D2D1_RECT_F& rectA,
const D2D1_RECT_F& rectB, const D2D1_RECT_F& rectC, D2D1_RECT_F& rectD) {
const float A_width = rectA.right - rectA.left;
const float B_width = rectB.right - rectB.left;
const float C_width = rectC.right - rectC.left;
// if both A and B have 0.f width set to 1.f
// if A has width 0.f but not B set to 0.f
const float width_ratio = A_width == 0.f ? (B_width == 0.f ? 1.f : 0.f) : B_width / A_width;
const float width_d = C_width * width_ratio;
rectD.left = 0.f;
rectD.right = rectD.left + width_d;
rectD.top = rectC.top;
rectD.bottom = rectC.bottom;
// compute how far B is along A
auto perc_b_along_a = [&]() {
const float start_pos = rectA.left + ((rectB.right - rectB.left) / 2.f);
const float end_pos = rectA.right - ((rectB.right - rectB.left) / 2.f);
const float center_B = rectB.left + ((rectB.right - rectB.left) / 2.f);
if (end_pos == start_pos)
return 100.f;
else
return 100.f * (center_B - start_pos) / (end_pos - start_pos);
};
// position C within D
pos_rect(rectC, rectD, perc_b_along_a(), 0.f);
}
/// <summary>position D within C to reflect how B is positioned in A.</summary>
static inline void position_v_scrollbar(const D2D1_RECT_F& rectA,
const D2D1_RECT_F& rectB, const D2D1_RECT_F& rectC, D2D1_RECT_F& rectD) {
const float A_height = rectA.bottom - rectA.top;
const float B_height = rectB.bottom - rectB.top;
const float C_height = rectC.bottom - rectC.top;
// if both A and B have 0.f height set to 1.f
// if A has height 0.f but not B set to 0.f
const float height_ratio = A_height == 0.f ? (B_height == 0.f ? 1.f : 0.f) : B_height / A_height;
const float height_d = C_height * height_ratio;
rectD.top = 0.f;
rectD.bottom = rectD.top + height_d;
rectD.left = rectC.left;
rectD.right = rectC.right;
// compute how far B is along A
auto perc_b_along_a = [&]() {
const float start_pos = rectA.top + ((rectB.bottom - rectB.top) / 2.f);
const float end_pos = rectA.bottom - ((rectB.bottom - rectB.top) / 2.f);
const float center_B = rectB.top + ((rectB.bottom - rectB.top) / 2.f);
if (end_pos == start_pos)
return 100.f;
else
return 100.f * (center_B - start_pos) / (end_pos - start_pos);
};
// position C within D
pos_rect(rectC, rectD, 0.f, perc_b_along_a());
}
static inline D2D1_RECT_F convert_rect(const rect& rect) {
D2D1_RECT_F _rect;
_rect.left = rect.get_left();
_rect.top = rect.get_top();
_rect.right = rect.get_right();
_rect.bottom = rect.get_bottom();
return _rect;
}
static inline rect convert_rect(const D2D1_RECT_F& rect) {
lecui::rect _rect;
_rect.left(rect.left);
_rect.top(rect.top);
_rect.right(rect.right);
_rect.bottom(rect.bottom);
return _rect;
}
/// <summary>Rounding off class.</summary>
class round_off {
public:
/// <summary>Round-off a double to a string.</summary>
/// <param name="d">The double to round-off.</param>
/// <param name="precision">The number of decimal places to round it off to.</param>
/// <returns>The rounded-off value, as a string.</returns>
static std::string to_string(const double& d, int precision) {
std::stringstream ss;
ss << std::fixed;
ss.precision(precision);
ss << d;
return ss.str();
}
/// <summary>Round-off a double to another double.</summary>
/// <param name="d">The double to round-off.</param>
/// <param name="precision">The number of decimal places to round it off to.</param>
/// <returns>The rounded-off value.</returns>
static double to_double(const double& d, int precision) {
int y = (int)d;
double z = d - (double)y;
double m = pow(10.0, (double)precision);
double q = z * m;
double r = round(q);
return static_cast<double>(y) + (1.0 / m) * r;
}
/// <summary>Round-off a float to another float.</summary>
/// <param name="d">The float to round-off.</param>
/// <param name="precision">The number of decimal places to round it off to.</param>
/// <returns>The rounded-off value.</returns>
static float to_float(const float& f, int precision) {
int y = (int)f;
float z = f - (float)y;
float m = pow(10.f, (float)precision);
float q = z * m;
float r = round(q);
return static_cast<float>(y) + (1.f / m) * r;
}
};
static inline void make_single_line(IDWriteFactory* p_directwrite_factory,
IDWriteTextFormat* p_text_format) {
// to-do: verify this function's correctness
DWRITE_TRIMMING trimming;
trimming.granularity = DWRITE_TRIMMING_GRANULARITY_CHARACTER;
trimming.delimiter = 0;
trimming.delimiterCount = 0;
IDWriteInlineObject* trimmingSign = nullptr;
p_directwrite_factory->CreateEllipsisTrimmingSign(p_text_format, &trimmingSign);
p_text_format->SetTrimming(&trimming, trimmingSign);
}
template <typename T>
static inline T smallest(T a, T b) {
return (((a) < (b)) ? (a) : (b));
}
template <typename T>
static inline T largest(T a, T b) {
return (((a) > (b)) ? (a) : (b));
}
template <class t>
static inline void swap(t& left, t& right) {
t temp = left;
left = right;
right = temp;
}
static inline color lighten_color(const color& clr_in,
unsigned short percentage) {
color color = clr_in;
color.red(clr_in.get_red() + (((255 - clr_in.get_red()) * percentage) / 100));
color.green(clr_in.get_green() + (((255 - clr_in.get_green()) * percentage) / 100));
color.blue(clr_in.get_blue() + (((255 - clr_in.get_blue()) * percentage) / 100));
return color;
}
static inline color darken_color(const color& clr_in,
unsigned short percentage) {
color color = clr_in;
color.red(((clr_in.get_red() - 0) * (100 - percentage)) / 100);
color.green(((clr_in.get_green() - 0) * (100 - percentage)) / 100);
color.blue(((clr_in.get_blue() - 0) * (100 - percentage)) / 100);
return color;
}
class auto_clip {
public:
auto_clip(bool render,
ID2D1HwndRenderTarget* p_render_target,
const D2D1_RECT_F& rect,
float content_margin) :
_render(render),
_p_render_target(p_render_target),
_rect({ rect.left - content_margin, rect.top - content_margin,
rect.right + content_margin, rect.bottom + content_margin }) {
if (render)
_p_render_target->PushAxisAlignedClip(_rect,
D2D1_ANTIALIAS_MODE_PER_PRIMITIVE);
}
~auto_clip() {
if (_render)
_p_render_target->PopAxisAlignedClip();
}
private:
bool _render;
ID2D1HwndRenderTarget* _p_render_target;
D2D1_RECT_F _rect;
};
/// <summary>Fit a rectangle within another.</summary>
/// <param name="rect_container">The container rectangle.</param>
/// <param name="enlarge_if_smaller">Enlarge if it's smaller than the container.</param>
/// <param name="keep_aspect_ratio">Whether to keep the source image's aspect ratio.</param>
/// <param name="center">Whether to center the rectangle in the container.</param>
/// <param name="rect"></param>
static inline void fit_rect(const D2D1_RECT_F rect_container, D2D1_RECT_F& rect,
bool enlarge_if_smaller, bool keep_aspect_ratio, bool center) {
auto width = rect_container.right - rect_container.left;
auto height = rect_container.bottom - rect_container.top;
// deduce old height and ratio
const auto old_height = rect.bottom - rect.top;
const auto old_width = rect.right - rect.left;
const auto ratio = old_width / old_height;
if (enlarge_if_smaller == false) {
if (old_width < width && old_height < height) {
// both sides of the rectangle are smaller than the container, preserve size
width = old_width;
height = old_height;
}
}
// save target dimensions
const auto control_w = width;
const auto control_h = height;
if (ratio == 1) {
if (keep_aspect_ratio) {
if (width > height)
width = height; // landscape
else
height = width; // portrait
}
}
else {
if (keep_aspect_ratio) {
// adjust either new width or height to keep aspect ratio
if (old_width > old_height) {
// old width is greater than old height
// adjust new height using new width to keep aspect ratio
height = width / ratio;
if (height > control_h) {
// new width is greater than target width, adjust it accordingly
height = control_h;
width = height * ratio;
}
}
else {
// old height is greater than old width
// adjust new width using new height to keep aspect ratio
width = height * ratio;
if (width > control_w) {
// new width is greater than target width, adjust it accordingly
width = control_w;
height = width / ratio;
}
}
}
}
rect.left = 0.f;
rect.top = 0.f;
if (center) {
rect.left = ((rect_container.right - rect_container.left) - width) / 2.f;
rect.top = ((rect_container.bottom - rect_container.top) - height) / 2.f;
}
rect.left += rect_container.left;
rect.top += rect_container.top;
rect.right = rect.left + width;
rect.bottom = rect.top + height;
}
static inline void create_bitmap(ID2D1RenderTarget* p_render_target,
IWICImagingFactory* p_IWICFactory, IWICBitmapDecoder* p_decoder,
ID2D1Bitmap** pp_bitmap, size target_size,
bool enlarge_if_smaller, bool keep_aspect_ratio, image_quality quality) {
IWICBitmapFrameDecode* pSource = nullptr;
IWICFormatConverter* pConverter = nullptr;
IWICBitmapScaler* pScaler = nullptr;
// Create the initial frame.
HRESULT hr = p_decoder->GetFrame(0, &pSource);
if (SUCCEEDED(hr)) {
// Convert the image format to 32bppPBGRA
// (DXGI_FORMAT_B8G8R8A8_UNORM + D2D1_ALPHA_MODE_PREMULTIPLIED).
hr = p_IWICFactory->CreateFormatConverter(&pConverter);
}
if (SUCCEEDED(hr)) {
UINT32 original_width = 0;
UINT32 original_height = 0;
hr = pSource->GetSize(&original_width, &original_height);
if (SUCCEEDED(hr)) {
D2D1_RECT_F rect_container = { 0, 0, 0, 0 };
rect_container.right = rect_container.left + target_size.width();
rect_container.bottom = rect_container.top + target_size.height();
D2D1_RECT_F rect = { 0, 0, 0, 0 };
rect.right = rect.left + static_cast<float>(original_width);
rect.bottom = rect.top + static_cast<float>(original_height);
fit_rect(rect_container, rect, enlarge_if_smaller, keep_aspect_ratio, false);
auto new_width = static_cast<UINT>(rect.right - rect.left);
auto new_height = static_cast<UINT>(rect.bottom - rect.top);
if (new_width != original_width || new_height != original_height) {
// scale image
hr = p_IWICFactory->CreateBitmapScaler(&pScaler);
if (SUCCEEDED(hr)) {
WICBitmapInterpolationMode mode;
switch (quality) {
case image_quality::low:
mode = WICBitmapInterpolationModeNearestNeighbor;
break;
case image_quality::high:
mode = WICBitmapInterpolationModeHighQualityCubic;
break;
case image_quality::medium:
default:
mode = WICBitmapInterpolationModeLinear;
break;
}
hr = pScaler->Initialize(pSource, new_width, new_height, mode);
}
if (SUCCEEDED(hr)) {
hr = pConverter->Initialize(pScaler, GUID_WICPixelFormat32bppPBGRA,
WICBitmapDitherTypeNone, nullptr, 0.f, WICBitmapPaletteTypeMedianCut);
}
}
else {
// don't scale image
hr = pConverter->Initialize(pSource, GUID_WICPixelFormat32bppPBGRA,
WICBitmapDitherTypeNone, nullptr, 0.f, WICBitmapPaletteTypeMedianCut);
}
}
}
if (SUCCEEDED(hr)) {
// Create a Direct2D bitmap from the WIC bitmap.
hr = p_render_target->CreateBitmapFromWicBitmap(pConverter, nullptr, pp_bitmap);
}
safe_release(&pScaler);
safe_release(&pConverter);
safe_release(&pSource);
}
static inline HRESULT load_bitmap_resource(ID2D1RenderTarget* p_render_target,
IWICImagingFactory* p_IWICFactory, HINSTANCE h_inst, int id_image,
std::string resource_type, ID2D1Bitmap** pp_bitmap,
size target_size, bool enlarge_if_smaller, bool keep_aspect_ratio, image_quality quality) {
IWICBitmapDecoder* p_decoder = nullptr;
IWICStream* p_stream = nullptr;
HRSRC image_resource_handle = NULL;
HGLOBAL image_resource_data_handle = NULL;
void* p_image_file = nullptr;
DWORD image_file_size = 0;
// Locate the resource.
image_resource_handle = FindResourceA(h_inst, MAKEINTRESOURCEA(id_image), resource_type.c_str());
HRESULT hr = image_resource_handle ? S_OK : E_FAIL;
if (SUCCEEDED(hr)) {
// Load the resource.
image_resource_data_handle = LoadResource(h_inst, image_resource_handle);
hr = image_resource_data_handle ? S_OK : E_FAIL;
}
if (SUCCEEDED(hr)) {
// Lock it to get a system memory pointer.
p_image_file = LockResource(image_resource_data_handle);
hr = p_image_file ? S_OK : E_FAIL;
}
if (SUCCEEDED(hr)) {
// Calculate the size.
image_file_size = SizeofResource(h_inst, image_resource_handle);
hr = image_file_size ? S_OK : E_FAIL;
}
if (SUCCEEDED(hr)) {
// Create a WIC stream to map onto the memory.
hr = p_IWICFactory->CreateStream(&p_stream);
}
if (SUCCEEDED(hr)) {
// Initialize the stream with the memory pointer and size.
hr = p_stream->InitializeFromMemory(reinterpret_cast<BYTE*>(p_image_file), image_file_size);
}
if (SUCCEEDED(hr)) {
// Create a decoder for the stream.
hr = p_IWICFactory->CreateDecoderFromStream(p_stream, nullptr,
WICDecodeMetadataCacheOnLoad, &p_decoder);
}
if (SUCCEEDED(hr))
create_bitmap(p_render_target, p_IWICFactory, p_decoder,
pp_bitmap, target_size, enlarge_if_smaller, keep_aspect_ratio, quality);
safe_release(&p_decoder);
safe_release(&p_stream);
return hr;
}
static inline HRESULT load_bitmap_file(ID2D1RenderTarget* p_render_target,
IWICImagingFactory* p_IWICFactory, PCWSTR uri, ID2D1Bitmap** pp_bitmap,
size target_size, bool enlarge_if_smaller, bool keep_aspect_ratio, image_quality quality) {
IWICBitmapDecoder* p_decoder = nullptr;
HRESULT hr = p_IWICFactory->CreateDecoderFromFilename(uri, nullptr, GENERIC_READ,
WICDecodeMetadataCacheOnLoad, &p_decoder);
if (SUCCEEDED(hr))
create_bitmap(p_render_target, p_IWICFactory, p_decoder,
pp_bitmap, target_size, enlarge_if_smaller, keep_aspect_ratio, quality);
safe_release(&p_decoder);
return hr;
}
static inline float get_process_dpi() {
// make the process DPI aware, if it isn't already
if (!IsProcessDPIAware()) {
if (!SetProcessDPIAware())
log("This program is not DPI aware. As a result, UI elements may not be clear.");
}
// capture current DPI scale
HDC hdc_screen = GetDC(NULL);
auto _dpi_scale = (float)GetDeviceCaps(hdc_screen, LOGPIXELSY) / 96.0f;
ReleaseDC(NULL, hdc_screen);
return _dpi_scale;
}
static inline void trim(std::string& s) {
auto trim_left = [](std::string& s) {
s.erase(s.begin(), std::find_if(s.begin(), s.end(),
[](unsigned char ch) { return !std::isspace(ch); }));
};
auto trim_right = [](std::string& s) {
s.erase(std::find_if(s.rbegin(), s.rend(),
[](unsigned char ch) { return !std::isspace(ch); }).base(), s.end());
};
trim_left(s);
trim_right(s);
}
/// <summary>Check if two vectors are equal.</summary>
/// <typeparam name="T">The typename.</typeparam>
/// <param name="v1">The first vector.</param>
/// <param name="v2">The second vector.</param>
/// <returns>Returns true if the vectors are the same, else false.</returns>
template<typename T>
static inline bool is_equal(std::vector<T>& v1, std::vector<T>& v2) {
return (v1.size() == v2.size() &&
std::equal(v1.begin(), v1.end(), v2.begin()));
}
}
}