-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWnd.cpp
More file actions
658 lines (568 loc) · 16.7 KB
/
Copy pathWnd.cpp
File metadata and controls
658 lines (568 loc) · 16.7 KB
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
652
653
654
655
656
657
#include "Wnd.h"
#include "Backplate.h"
#include "Util.h"
#include <algorithm>
namespace FD2D
{
// Note: LayoutRect() returns coordinates in client coordinate system (Backplate client area)
// Since all LayoutRects are in the same client coordinate system, no conversion is needed
// Parent Wnd and Child Wnd both receive coordinates in client/Layout coordinate system
Wnd::Wnd()
{
}
Wnd::Wnd(const std::wstring& name)
: m_name(name)
{
}
void Wnd::SetLayoutRect(const D2D1_RECT_F& rect)
{
m_layoutDesired = rect;
m_layoutRect = rect;
m_desired = { rect.right - rect.left, rect.bottom - rect.top };
}
void Wnd::SetAnchors(bool anchorLeft, bool anchorTop, bool anchorRight, bool anchorBottom)
{
m_anchorLeft = anchorLeft;
m_anchorTop = anchorTop;
m_anchorRight = anchorRight;
m_anchorBottom = anchorBottom;
}
const D2D1_RECT_F& Wnd::LayoutRect() const
{
return m_layoutRect;
}
Size Wnd::Measure(Size available)
{
// A base Wnd with no children has size 0
if (m_childrenOrdered.empty())
{
m_desired = { 0.0f, 0.0f };
return m_desired;
}
// If there are children, use the maximum size among them
Size maxSize {};
for (auto& child : m_childrenOrdered)
{
if (child)
{
Size childSize = child->Measure(available);
maxSize.w = (std::max)(maxSize.w, childSize.w);
maxSize.h = (std::max)(maxSize.h, childSize.h);
}
}
m_desired = { maxSize.w + 2 * m_margin, maxSize.h + 2 * m_margin };
return m_desired;
}
Size Wnd::MinSize() const
{
// Default: if no children, no intrinsic minimum.
if (m_childrenOrdered.empty())
{
return { 0.0f, 0.0f };
}
Size maxSize {};
for (const auto& child : m_childrenOrdered)
{
if (child)
{
Size childMin = child->MinSize();
maxSize.w = (std::max)(maxSize.w, childMin.w);
maxSize.h = (std::max)(maxSize.h, childMin.h);
}
}
// Include this node's margin/padding.
maxSize.w += 2.0f * m_margin + 2.0f * m_padding;
maxSize.h += 2.0f * m_margin + 2.0f * m_padding;
return maxSize;
}
void Wnd::Arrange(Rect finalRect)
{
Rect inset = Inset(finalRect, m_margin);
m_bounds = inset;
m_layoutRect = ToD2D(inset);
Rect childArea = Inset(inset, m_padding);
for (auto& child : m_childrenOrdered)
{
if (child)
{
child->Arrange(childArea);
}
}
}
void Wnd::SetContentMargin(float uniform)
{
SetContentMargin(Thickness(uniform));
}
void Wnd::SetContentMargin(float horizontal, float vertical)
{
SetContentMargin(Thickness(horizontal, vertical));
}
void Wnd::SetContentMargin(const Thickness& margin)
{
m_contentMargin = margin;
NotifyContentLayoutChanged();
}
void Wnd::SetContentAlign(AlignH horizontal, AlignV vertical)
{
m_contentAlignH = horizontal;
m_contentAlignV = vertical;
NotifyContentLayoutChanged();
}
void Wnd::SetContentAlignH(AlignH horizontal)
{
m_contentAlignH = horizontal;
NotifyContentLayoutChanged();
}
void Wnd::SetContentAlignV(AlignV vertical)
{
m_contentAlignV = vertical;
NotifyContentLayoutChanged();
}
Rect Wnd::BoundsRect() const
{
return FromD2D(m_layoutRect);
}
Rect Wnd::ContentRectFor(const Size& contentSize) const
{
return ContentRectFor(BoundsRect(), contentSize);
}
Rect Wnd::ContentRectFor(const Rect& bounds, const Size& contentSize) const
{
return LayoutContent(bounds, contentSize, m_contentMargin, m_contentAlignH, m_contentAlignV);
}
void Wnd::NotifyContentLayoutChanged()
{
if (m_backplate)
{
m_backplate->RequestLayout();
}
Invalidate();
}
void Wnd::SetName(const std::wstring& name)
{
m_name = name;
}
const std::wstring& Wnd::Name() const
{
return m_name;
}
bool Wnd::AddChild(const std::shared_ptr<Wnd>& child)
{
if (!child)
{
return false;
}
const std::wstring& childName = child->Name();
if (childName.empty())
{
return false;
}
if (m_children.find(childName) != m_children.end())
{
return false;
}
m_children.emplace(childName, child);
m_childrenOrdered.push_back(child);
if (m_backplate != nullptr)
{
child->OnAttached(*m_backplate);
}
return true;
}
bool Wnd::RemoveChild(const std::wstring& childName)
{
if (childName.empty())
{
return false;
}
auto it = m_children.find(childName);
if (it == m_children.end())
{
return false;
}
std::shared_ptr<Wnd> child = it->second;
if (child && m_backplate != nullptr)
{
child->OnDetached();
}
m_children.erase(it);
for (auto vit = m_childrenOrdered.begin(); vit != m_childrenOrdered.end(); ++vit)
{
if (*vit && (*vit)->Name() == childName)
{
m_childrenOrdered.erase(vit);
break;
}
}
return true;
}
void Wnd::ClearChildren()
{
if (m_backplate != nullptr)
{
for (auto& child : m_childrenOrdered)
{
if (child)
{
child->OnDetached();
}
}
}
m_children.clear();
m_childrenOrdered.clear();
}
bool Wnd::ReorderChildren(const std::vector<std::wstring>& childNamesInOrder)
{
std::unordered_map<std::wstring, bool> seen;
seen.reserve(childNamesInOrder.size());
std::vector<std::shared_ptr<Wnd>> newOrder;
newOrder.reserve(childNamesInOrder.size());
for (const auto& childName : childNamesInOrder)
{
if (childName.empty())
{
return false;
}
if (seen.find(childName) != seen.end())
{
return false;
}
seen.emplace(childName, true);
auto it = m_children.find(childName);
if (it == m_children.end())
{
return false;
}
newOrder.push_back(it->second);
}
m_childrenOrdered = std::move(newOrder);
return true;
}
const std::unordered_map<std::wstring, std::shared_ptr<Wnd>>& Wnd::Children() const
{
return m_children;
}
const std::vector<std::shared_ptr<Wnd>>& Wnd::ChildrenInOrder() const
{
return m_childrenOrdered;
}
void Wnd::OnAttached(Backplate& backplate)
{
m_backplate = &backplate;
for (auto& child : m_childrenOrdered)
{
if (child)
{
child->OnAttached(backplate);
}
}
}
void Wnd::OnDetached()
{
if (m_backplate != nullptr)
{
m_backplate->ClearFocusIf(this);
}
for (auto& child : m_childrenOrdered)
{
if (child)
{
child->OnDetached();
}
}
m_backplate = nullptr;
}
void Wnd::OnGraphicsInvalidated(GraphicsInvalidationReason reason, const GraphicsGeneration& generation)
{
for (auto& child : m_childrenOrdered)
{
if (child)
{
child->OnGraphicsInvalidated(reason, generation);
}
}
}
void Wnd::OnRender(ID2D1RenderTarget* target)
{
UNREFERENCED_PARAMETER(target);
for (auto& child : m_childrenOrdered)
{
if (child)
{
child->OnRender(target);
}
}
}
void Wnd::RenderOverlayTree(ID2D1RenderTarget* target, OverlayLayer layer)
{
RenderChildOverlays(target, layer);
OnRenderOverlay(target, layer);
}
void Wnd::RenderChildOverlays(ID2D1RenderTarget* target, OverlayLayer layer)
{
for (auto& child : m_childrenOrdered)
{
if (child)
{
child->RenderOverlayTree(target, layer);
}
}
}
void Wnd::OnRenderOverlay(ID2D1RenderTarget* target, OverlayLayer layer)
{
UNREFERENCED_PARAMETER(target);
UNREFERENCED_PARAMETER(layer);
}
void Wnd::OnRenderD3D(ID3D11DeviceContext* context)
{
UNREFERENCED_PARAMETER(context);
for (auto& child : m_childrenOrdered)
{
if (child)
{
child->OnRenderD3D(context);
}
}
}
bool Wnd::RouteOverlayInput(const InputEvent& event, OverlayLayer layer)
{
if (IsOverlayActive(layer) && OnOverlayInput(event, layer))
{
return true;
}
return RouteChildOverlayInput(event, layer);
}
bool Wnd::RouteChildOverlayInput(const InputEvent& event, OverlayLayer layer)
{
for (auto it = m_childrenOrdered.rbegin(); it != m_childrenOrdered.rend(); ++it)
{
const auto& child = *it;
if (!child)
{
continue;
}
if (child->RouteOverlayInput(event, layer))
{
return true;
}
}
return false;
}
bool Wnd::HasActiveOverlayInTree(OverlayLayer layer) const
{
if (IsOverlayActive(layer))
{
return true;
}
for (const auto& child : m_childrenOrdered)
{
if (child && child->HasActiveOverlayInTree(layer))
{
return true;
}
}
return false;
}
bool Wnd::IsOverlayActive(OverlayLayer layer) const
{
UNREFERENCED_PARAMETER(layer);
return false;
}
bool Wnd::OnOverlayInput(const InputEvent& event, OverlayLayer layer)
{
UNREFERENCED_PARAMETER(event);
UNREFERENCED_PARAMETER(layer);
return false;
}
bool Wnd::OnInputEvent(const InputEvent& event)
{
// Mouse input should behave like hit-testing: topmost child first, stop at first handled.
// Note: Coordinates are already in client/Layout coordinate system (converted by Backplate)
// All LayoutRects are in the same client coordinate system, so no conversion needed
if (Util::IsMouseInputEventType(event.type))
{
// For wheel input, route based on cursor position so the pane under the mouse receives it
// even if another control currently owns focus.
if (event.type == InputEventType::MouseWheel || event.type == InputEventType::MouseHWheel)
{
if (!event.hasPoint)
{
return false;
}
for (auto it = m_childrenOrdered.rbegin(); it != m_childrenOrdered.rend(); ++it)
{
const auto& child = *it;
if (!child)
{
continue;
}
if (!Util::RectContainsPoint(child->LayoutRect(), event.point))
{
continue;
}
if (child->OnInputEvent(event))
{
return true;
}
}
return false;
}
for (auto it = m_childrenOrdered.rbegin(); it != m_childrenOrdered.rend(); ++it)
{
const auto& child = *it;
if (child && child->OnInputEvent(event))
{
return true;
}
}
return false;
}
// Non-mouse messages: broadcast (layout/animation timers etc).
bool handled = false;
for (auto& child : m_childrenOrdered)
{
if (child && child->OnInputEvent(event))
{
handled = true;
}
}
return handled;
}
bool Wnd::OnCommandEvent(const CommandEvent& event)
{
bool handled = false;
for (auto& child : m_childrenOrdered)
{
if (child && child->OnCommandEvent(event))
{
handled = true;
}
}
return handled;
}
bool Wnd::OnFileDropPaths(const std::vector<std::wstring>& paths, const POINT& clientPt)
{
return !paths.empty() && OnFileDrop(paths.front(), clientPt);
}
bool Wnd::OnFileDrop(const std::wstring& path, const POINT& clientPt)
{
// Default behavior: hit-test children (topmost first) and forward.
for (auto it = m_childrenOrdered.rbegin(); it != m_childrenOrdered.rend(); ++it)
{
const auto& child = *it;
if (!child)
{
continue;
}
if (!Util::RectContainsPoint(child->LayoutRect(), clientPt))
{
continue;
}
if (child->OnFileDrop(path, clientPt))
{
return true;
}
}
return false;
}
bool Wnd::OnFileDrag(const std::wstring& path, const POINT& clientPt, FileDragVisual& outVisual)
{
UNREFERENCED_PARAMETER(path);
// Default behavior: hit-test children (topmost first) and forward.
for (auto it = m_childrenOrdered.rbegin(); it != m_childrenOrdered.rend(); ++it)
{
const auto& child = *it;
if (!child)
{
continue;
}
if (!Util::RectContainsPoint(child->LayoutRect(), clientPt))
{
continue;
}
if (child->OnFileDrag(path, clientPt, outVisual))
{
return true;
}
}
outVisual = FileDragVisual::None;
return false;
}
void Wnd::OnFileDragLeave()
{
// Default behavior: broadcast to children so any overlay state can be cleared.
for (auto& child : m_childrenOrdered)
{
if (child)
{
child->OnFileDragLeave();
}
}
}
void Wnd::RequestFocus()
{
if (m_backplate != nullptr)
{
m_backplate->SetFocusedWnd(this);
}
}
bool Wnd::HasFocus() const
{
return m_backplate != nullptr && m_backplate->FocusedWnd() == this;
}
Wnd* Wnd::HitTestDeepest(const POINT& pt)
{
const D2D1_RECT_F& r = m_layoutRect;
const float x = static_cast<float>(pt.x);
const float y = static_cast<float>(pt.y);
if (x < r.left || x > r.right || y < r.top || y > r.bottom)
{
return nullptr;
}
// Reverse (topmost-first) so an overlapping later child wins.
for (auto it = m_childrenOrdered.rbegin(); it != m_childrenOrdered.rend(); ++it)
{
if (*it)
{
if (Wnd* hit = (*it)->HitTestDeepest(pt))
{
return hit;
}
}
}
return this;
}
Backplate* Wnd::BackplateRef() const
{
return m_backplate;
}
void Wnd::Invalidate() const
{
if (m_backplate == nullptr)
{
return;
}
HWND hwnd = m_backplate->Window();
if (hwnd == nullptr || !IsWindow(hwnd))
{
return;
}
// Use InvalidateRect (post WM_PAINT) if we are inside a resize, already
// inside Render(), or a caller is batching several state changes into one
// render (IsDeferringRender(), e.g. drag-hover overlay updates during a
// single OLE DragOver). Calling Render() re-entrantly would set
// m_renderRequested=true and cause the do-while render loop to spin
// once per Invalidate() call, leading to multi-second stalls when
// many components (e.g. loading spinners) call Invalidate() during
// a single frame.
if (m_backplate->IsInSizeMove() || m_backplate->IsRendering() || m_backplate->IsDeferringRender())
{
InvalidateRect(hwnd, nullptr, FALSE);
}
else
{
m_backplate->NoteRenderTrigger(Backplate::RenderTrigger::Invalidate);
m_backplate->Render();
}
}
}