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