1a5b50d1fSIngo Weinhold/*
2a5b50d1fSIngo Weinhold * Copyright 2007, Ingo Weinhold <bonefish@cs.tu-berlin.de>.
3a5b50d1fSIngo Weinhold * All rights reserved. Distributed under the terms of the MIT License.
4a5b50d1fSIngo Weinhold */
5a5b50d1fSIngo Weinhold
6a5b50d1fSIngo Weinhold#include "ViewContainer.h"
7a5b50d1fSIngo Weinhold
8a5b50d1fSIngo Weinhold#include <Message.h>
9a5b50d1fSIngo Weinhold#include <Window.h>
10a5b50d1fSIngo Weinhold
11a5b50d1fSIngo Weinhold
12a5b50d1fSIngo Weinhold// internal messages
13a5b50d1fSIngo Weinholdenum {
14a5b50d1fSIngo Weinhold	MSG_LAYOUT_CONTAINER			= 'layc',
15a5b50d1fSIngo Weinhold};
16a5b50d1fSIngo Weinhold
17a5b50d1fSIngo Weinhold
18a5b50d1fSIngo WeinholdViewContainer::ViewContainer(BRect frame)
19a5b50d1fSIngo Weinhold	: BView(frame, "view container", B_FOLLOW_NONE, B_WILL_DRAW),
20a5b50d1fSIngo Weinhold	  View(frame.OffsetToCopy(B_ORIGIN)),
21a5b50d1fSIngo Weinhold	  fMouseFocus(NULL)
22a5b50d1fSIngo Weinhold{
23a5b50d1fSIngo Weinhold	BView::SetViewColor(B_TRANSPARENT_32_BIT);
24a5b50d1fSIngo Weinhold	_AddedToContainer(this);
25a5b50d1fSIngo Weinhold}
26a5b50d1fSIngo Weinhold
27a5b50d1fSIngo Weinhold
28a5b50d1fSIngo Weinholdvoid
29a5b50d1fSIngo WeinholdViewContainer::MessageReceived(BMessage* message)
30a5b50d1fSIngo Weinhold{
31a5b50d1fSIngo Weinhold	switch (message->what) {
32a5b50d1fSIngo Weinhold		case MSG_LAYOUT_CONTAINER:
33a5b50d1fSIngo Weinhold			View::Layout();
34a5b50d1fSIngo Weinhold			break;
35a5b50d1fSIngo Weinhold		default:
36a5b50d1fSIngo Weinhold			BView::MessageReceived(message);
37a5b50d1fSIngo Weinhold			break;
38a5b50d1fSIngo Weinhold	}
39a5b50d1fSIngo Weinhold}
40a5b50d1fSIngo Weinhold
41a5b50d1fSIngo Weinhold
42a5b50d1fSIngo Weinholdvoid
43a5b50d1fSIngo WeinholdViewContainer::AllAttached()
44a5b50d1fSIngo Weinhold{
45a5b50d1fSIngo Weinhold	Window()->PostMessage(MSG_LAYOUT_CONTAINER, this);
46a5b50d1fSIngo Weinhold}
47a5b50d1fSIngo Weinhold
48a5b50d1fSIngo Weinhold
49a5b50d1fSIngo Weinholdvoid
50a5b50d1fSIngo WeinholdViewContainer::Draw(BRect updateRect)
51a5b50d1fSIngo Weinhold{
52a5b50d1fSIngo Weinhold	View::_Draw(this, updateRect);
53a5b50d1fSIngo Weinhold}
54a5b50d1fSIngo Weinhold
55a5b50d1fSIngo Weinhold
56a5b50d1fSIngo Weinholdvoid
57a5b50d1fSIngo WeinholdViewContainer::MouseDown(BPoint where)
58a5b50d1fSIngo Weinhold{
59a5b50d1fSIngo Weinhold	// get mouse buttons and modifiers
60a5b50d1fSIngo Weinhold	uint32 buttons;
61a5b50d1fSIngo Weinhold	int32 modifiers;
62a5b50d1fSIngo Weinhold	_GetButtonsAndModifiers(buttons, modifiers);
63a5b50d1fSIngo Weinhold
64a5b50d1fSIngo Weinhold	// get mouse focus
65a5b50d1fSIngo Weinhold	if (!fMouseFocus && (buttons & B_PRIMARY_MOUSE_BUTTON)) {
66a5b50d1fSIngo Weinhold		fMouseFocus = AncestorAt(where);
67a5b50d1fSIngo Weinhold		if (fMouseFocus)
68a5b50d1fSIngo Weinhold			SetMouseEventMask(B_POINTER_EVENTS);
69a5b50d1fSIngo Weinhold	}
70a5b50d1fSIngo Weinhold
71a5b50d1fSIngo Weinhold	// call hook
72a5b50d1fSIngo Weinhold	if (fMouseFocus) {
73a5b50d1fSIngo Weinhold		fMouseFocus->MouseDown(fMouseFocus->ConvertFromContainer(where),
74a5b50d1fSIngo Weinhold			buttons, modifiers);
75a5b50d1fSIngo Weinhold	}
76a5b50d1fSIngo Weinhold}
77a5b50d1fSIngo Weinhold
78a5b50d1fSIngo Weinhold
79a5b50d1fSIngo Weinholdvoid
80a5b50d1fSIngo WeinholdViewContainer::MouseUp(BPoint where)
81a5b50d1fSIngo Weinhold{
82a5b50d1fSIngo Weinhold	if (!fMouseFocus)
83a5b50d1fSIngo Weinhold		return;
84a5b50d1fSIngo Weinhold
85a5b50d1fSIngo Weinhold	// get mouse buttons and modifiers
86a5b50d1fSIngo Weinhold	uint32 buttons;
87a5b50d1fSIngo Weinhold	int32 modifiers;
88a5b50d1fSIngo Weinhold	_GetButtonsAndModifiers(buttons, modifiers);
89a5b50d1fSIngo Weinhold
90a5b50d1fSIngo Weinhold	// call hook
91a5b50d1fSIngo Weinhold	if (fMouseFocus) {
92a5b50d1fSIngo Weinhold		fMouseFocus->MouseUp(fMouseFocus->ConvertFromContainer(where),
93a5b50d1fSIngo Weinhold			buttons, modifiers);
94a5b50d1fSIngo Weinhold	}
95a5b50d1fSIngo Weinhold
96a5b50d1fSIngo Weinhold	// unset the mouse focus when the primary button has been released
97a5b50d1fSIngo Weinhold	if (!(buttons & B_PRIMARY_MOUSE_BUTTON))
98a5b50d1fSIngo Weinhold		fMouseFocus = NULL;
99a5b50d1fSIngo Weinhold}
100a5b50d1fSIngo Weinhold
101a5b50d1fSIngo Weinhold
102a5b50d1fSIngo Weinholdvoid
103a5b50d1fSIngo WeinholdViewContainer::MouseMoved(BPoint where, uint32 code, const BMessage* message)
104a5b50d1fSIngo Weinhold{
105a5b50d1fSIngo Weinhold	if (!fMouseFocus)
106a5b50d1fSIngo Weinhold		return;
107a5b50d1fSIngo Weinhold
108a5b50d1fSIngo Weinhold	// get mouse buttons and modifiers
109a5b50d1fSIngo Weinhold	uint32 buttons;
110a5b50d1fSIngo Weinhold	int32 modifiers;
111a5b50d1fSIngo Weinhold	_GetButtonsAndModifiers(buttons, modifiers);
112a5b50d1fSIngo Weinhold
113a5b50d1fSIngo Weinhold	// call hook
114a5b50d1fSIngo Weinhold	if (fMouseFocus) {
115a5b50d1fSIngo Weinhold		fMouseFocus->MouseMoved(fMouseFocus->ConvertFromContainer(where),
116a5b50d1fSIngo Weinhold			buttons, modifiers);
117a5b50d1fSIngo Weinhold	}
118a5b50d1fSIngo Weinhold}
119a5b50d1fSIngo Weinhold
120a5b50d1fSIngo Weinhold
121691d242bSIngo Weinholdvoid
122691d242bSIngo WeinholdViewContainer::InvalidateLayout(bool descendants)
123691d242bSIngo Weinhold{
124691d242bSIngo Weinhold	BView::InvalidateLayout(descendants);
125691d242bSIngo Weinhold}
126691d242bSIngo Weinhold
127691d242bSIngo Weinhold
128a5b50d1fSIngo Weinholdvoid
129a5b50d1fSIngo WeinholdViewContainer::InvalidateLayout()
130a5b50d1fSIngo Weinhold{
131a5b50d1fSIngo Weinhold	if (View::IsLayoutValid()) {
132a5b50d1fSIngo Weinhold		View::InvalidateLayout();
133a5b50d1fSIngo Weinhold
134a5b50d1fSIngo Weinhold		// trigger asynchronous re-layout
135a5b50d1fSIngo Weinhold		if (Window())
136a5b50d1fSIngo Weinhold			Window()->PostMessage(MSG_LAYOUT_CONTAINER, this);
137a5b50d1fSIngo Weinhold	}
138a5b50d1fSIngo Weinhold}
139a5b50d1fSIngo Weinhold
140a5b50d1fSIngo Weinhold
141a5b50d1fSIngo Weinholdvoid
142a5b50d1fSIngo WeinholdViewContainer::Draw(BView* container, BRect updateRect)
143a5b50d1fSIngo Weinhold{
144a5b50d1fSIngo Weinhold}
145a5b50d1fSIngo Weinhold
146a5b50d1fSIngo Weinhold
147a5b50d1fSIngo Weinholdvoid
148a5b50d1fSIngo WeinholdViewContainer::MouseDown(BPoint where, uint32 buttons, int32 modifiers)
149a5b50d1fSIngo Weinhold{
150a5b50d1fSIngo Weinhold}
151a5b50d1fSIngo Weinhold
152a5b50d1fSIngo Weinhold
153a5b50d1fSIngo Weinholdvoid
154a5b50d1fSIngo WeinholdViewContainer::MouseUp(BPoint where, uint32 buttons, int32 modifiers)
155a5b50d1fSIngo Weinhold{
156a5b50d1fSIngo Weinhold}
157a5b50d1fSIngo Weinhold
158a5b50d1fSIngo Weinhold
159a5b50d1fSIngo Weinholdvoid
160a5b50d1fSIngo WeinholdViewContainer::MouseMoved(BPoint where, uint32 buttons, int32 modifiers)
161a5b50d1fSIngo Weinhold{
162a5b50d1fSIngo Weinhold}
163a5b50d1fSIngo Weinhold
164a5b50d1fSIngo Weinhold
165a5b50d1fSIngo Weinholdvoid
166a5b50d1fSIngo WeinholdViewContainer::_GetButtonsAndModifiers(uint32& buttons, int32& modifiers)
167a5b50d1fSIngo Weinhold{
168a5b50d1fSIngo Weinhold	buttons = 0;
169a5b50d1fSIngo Weinhold	modifiers = 0;
170a5b50d1fSIngo Weinhold
171a5b50d1fSIngo Weinhold	if (BMessage* message = Window()->CurrentMessage()) {
172a5b50d1fSIngo Weinhold		if (message->FindInt32("buttons", (int32*)&buttons) != B_OK)
173a5b50d1fSIngo Weinhold			buttons = 0;
174a5b50d1fSIngo Weinhold		if (message->FindInt32("modifiers", modifiers) != B_OK)
175a5b50d1fSIngo Weinhold			modifiers = 0;
176a5b50d1fSIngo Weinhold	}
177a5b50d1fSIngo Weinhold}
178