aboutsummaryrefslogtreecommitdiff
path: root/c/layout.h
blob: 29647e241e5c7b378c7f65e5ea57264c19b4fee2 (plain)
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
#ifndef LAYOUT_H
#define LAYOUT_H

#include <windows.h>

#include "datalistview.h"
#include "pl.h"
#include "win.h"

/* Given main window's width and height, set appropriate positions and
 * sizes for child windows. */
void UpdateLayout(int w = 0, int h = 0);

/* Dragger objects implement draggable portions of the client area,
 * such as the split between two list views. HandleLButtonDown and
 * HandleSetCursor are called by relevant window procedures upon
 * WM_(NC)LBUTTONDOWN and WM_SETCURSOR. */

struct Dragger
{
	bool HandleLButtonDown();
	bool HandleSetCursor();
protected:
	bool IsDown() const;
	bool IsDouble(const long time, const POINT& pt);
	virtual bool InDragArea(int x, int y) const;
	/* Perform drag, resizing relevant windows. */
	virtual void Drag(int x, int y) const;
	/* Reset dragger to automatic position. */
	virtual void Reset() const;
	/* Called after drag, when mouse button is released. */
	virtual void Done() const;
private:
	bool m_bActive = false;
	long m_time0 = 0;
	POINT m_pt0 = {0, 0};
};

/* DlvDragger implements the draggable split between the data list
 * view and the episode list view. */

struct DlvDragger : public Dragger
{
private:
	bool InDragArea(int x, int y) const override;
	void Drag(int x, int y) const override;
	void Reset() const override;
	void Done() const override;
};

/* Below follows the implementation of the non-virtual member
 * functions of Dragger, on which derived objects rely. */

inline bool Dragger::IsDouble(const long time, const POINT& pt)
{
	const bool dbl = time-m_time0 <= static_cast<long>(GetDoubleClickTime())
	&& abs(pt.x-m_pt0.x) <= Metric<SM_CXDOUBLECLK>
	&& abs(pt.y-m_pt0.y) <= Metric<SM_CYDOUBLECLK>;
	m_time0 = time;
	m_pt0 = std::move(pt);
	return dbl;
}

inline bool Dragger::IsDown() const
{
	return GetKeyState(VK_LBUTTON) & 0x8000;
}

inline bool Dragger::HandleLButtonDown()
{
	extern HWND g_hWnd;
	POINT pt;
	Require(GetRelativeCursorPos(g_hWnd, &pt));
	if (!InDragArea(pt.x, pt.y)) return false;

	if (IsDouble(GetMessageTime(), pt)) {
		m_bActive = false;
		Reset();
	} else
		m_bActive = true;

	return m_bActive;
}

inline bool Dragger::HandleSetCursor()
{
	extern HWND g_hWnd;
	POINT pt;
	Require(GetRelativeCursorPos(g_hWnd, &pt));

	extern HCURSOR g_hcSizeNs;
	bool r = true;
	if (InDragArea(pt.x, pt.y))
		SetCursor(g_hcSizeNs);
	else
		r = false;
	if (!m_bActive)
		return r;
	Drag(pt.x, pt.y);
	if (!IsDown()) {
		m_bActive = false;
		Done();
	}
	return r;
}

#endif