1/*
2 * Copyright 2015 Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
4 *
5 * Authors:
6 *		Axel Dörfler, <axeld@pinc-software.de>
7 */
8
9
10#include "IPAddressControl.h"
11
12#include <NetworkAddress.h>
13
14
15static const uint32 kMsgModified = 'txmd';
16
17
18IPAddressControl::IPAddressControl(int family, const char* label,
19	const char* name)
20	:
21	BTextControl(name, label, "", NULL),
22	fFamily(family),
23	fAllowEmpty(true)
24{
25	SetModificationMessage(new BMessage(kMsgModified));
26}
27
28
29IPAddressControl::~IPAddressControl()
30{
31}
32
33
34bool
35IPAddressControl::AllowEmpty() const
36{
37	return fAllowEmpty;
38}
39
40
41void
42IPAddressControl::SetAllowEmpty(bool empty)
43{
44	fAllowEmpty = empty;
45}
46
47
48void
49IPAddressControl::AttachedToWindow()
50{
51	BTextControl::AttachedToWindow();
52	SetTarget(this);
53	_UpdateMark();
54}
55
56
57void
58IPAddressControl::MessageReceived(BMessage* message)
59{
60	switch (message->what) {
61		case kMsgModified:
62			_UpdateMark();
63			break;
64
65		default:
66			BTextControl::MessageReceived(message);
67			break;
68	}
69}
70
71
72void
73IPAddressControl::_UpdateMark()
74{
75	if (TextLength() == 0) {
76		MarkAsInvalid(!fAllowEmpty);
77		return;
78	}
79
80	BNetworkAddress address;
81	bool success = address.SetTo(fFamily, Text(), (char*)NULL,
82		B_NO_ADDRESS_RESOLUTION) == B_OK;
83
84	MarkAsInvalid(!success);
85}
86