1/* BMailFileConfigView - a file configuration view for filters
2**
3** Copyright 2001 Dr. Zoidberg Enterprises. All rights reserved.
4*/
5
6
7#include <stdio.h>
8
9#include <Button.h>
10#include <Catalog.h>
11#include <Message.h>
12#include <Path.h>
13#include <String.h>
14#include <TextControl.h>
15
16#include <FileConfigView.h>
17
18
19class _EXPORT BFileControl;
20class _EXPORT BMailFileConfigView;
21
22
23#undef B_TRANSLATION_CONTEXT
24#define B_TRANSLATION_CONTEXT "MailKit"
25
26
27const uint32 kMsgSelectButton = 'fsel';
28
29
30BFileControl::BFileControl(BRect rect, const char* name, const char* label,
31	const char *pathOfFile,uint32 flavors)
32	:
33	BView(rect, name, B_FOLLOW_LEFT | B_FOLLOW_TOP, 0)
34{
35	SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
36
37	// determine font height
38	font_height fontHeight;
39	GetFontHeight(&fontHeight);
40	float itemHeight = (int32)(fontHeight.ascent + fontHeight.descent
41		+ fontHeight.leading) + 13;
42	BString selectString = B_TRANSLATE("Select" B_UTF8_ELLIPSIS);
43	float labelWidth = StringWidth(selectString) + 20;
44	rect = Bounds();
45	rect.right -= labelWidth;
46	rect.top = 4;
47	rect.bottom = itemHeight + 2;
48	fText = new BTextControl(rect,"file_path", label, pathOfFile, NULL);
49	if (label)
50		fText->SetDivider(fText->StringWidth(label) + 6);
51	AddChild(fText);
52
53	fButton = new BButton(BRect(0, 0, 1, 1), "select_file", selectString,
54		new BMessage(kMsgSelectButton));
55	fButton->ResizeToPreferred();
56	fButton->MoveBy(rect.right + 6,
57		(rect.Height() - fButton->Frame().Height()) / 2);
58	AddChild(fButton);
59
60	fPanel = new BFilePanel(B_OPEN_PANEL, NULL, NULL, flavors, false);
61
62	ResizeToPreferred();
63}
64
65
66BFileControl::~BFileControl()
67{
68	delete fPanel;
69}
70
71
72void
73BFileControl::AttachedToWindow()
74{
75	fButton->SetTarget(this);
76
77	BMessenger messenger(this);
78	if (messenger.IsValid())
79		fPanel->SetTarget(messenger);
80}
81
82
83void
84BFileControl::MessageReceived(BMessage* msg)
85{
86	switch (msg->what)
87	{
88		case kMsgSelectButton:
89		{
90			fPanel->Hide();
91			//fPanel->Window()->SetTitle(title);
92
93			BPath path(fText->Text());
94			if (path.InitCheck() >= B_OK)
95				if (path.GetParent(&path) >= B_OK)
96					fPanel->SetPanelDirectory(path.Path());
97
98			fPanel->Show();
99			break;
100		}
101		case B_REFS_RECEIVED:
102		{
103			entry_ref ref;
104			if (msg->FindRef("refs", &ref) >= B_OK)
105			{
106				BEntry entry(&ref);
107				if (entry.InitCheck() >= B_OK)
108				{
109					BPath path;
110					entry.GetPath(&path);
111
112					fText->SetText(path.Path());
113				}
114			}
115			break;
116		}
117		default:
118			BView::MessageReceived(msg);
119			break;
120	}
121}
122
123
124void
125BFileControl::SetText(const char* pathOfFile)
126{
127	fText->SetText(pathOfFile);
128}
129
130
131const char*
132BFileControl::Text() const
133{
134	return fText->Text();
135}
136
137
138void
139BFileControl::SetEnabled(bool enabled)
140{
141	fText->SetEnabled(enabled);
142	fButton->SetEnabled(enabled);
143}
144
145
146void
147BFileControl::GetPreferredSize(float* width, float* height)
148{
149	*width = fButton->Frame().right + 5;
150	*height = fText->Bounds().Height() + 8;
151}
152
153
154//--------------------------------------------------------------------------
155//	#pragma mark -
156
157BMailFileConfigView::BMailFileConfigView(const char* label, const char*name,
158	bool useMeta, const char* defaultPath, uint32 flavors)
159	:
160	BFileControl(BRect(5, 0, 255, 10), name, label, defaultPath, flavors),
161	fUseMeta(useMeta),
162	fName(name)
163{
164}
165
166
167void
168BMailFileConfigView::SetTo(const BMessage* archive, BMessage* meta)
169{
170	fMeta = meta;
171	BString path = (fUseMeta ? meta : archive)->FindString(fName);
172
173	if (path != "")
174		SetText(path.String());
175}
176
177
178status_t
179BMailFileConfigView::Archive(BMessage* into, bool /*deep*/) const
180{
181	const char* path = Text();
182	BMessage* archive = fUseMeta ? fMeta : into;
183
184	if (archive->ReplaceString(fName,path) != B_OK)
185		archive->AddString(fName,path);
186
187	return B_OK;
188}
189