2 * Copyright 2008-2010, Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
6 * Michael Pfeiffer <laplace@users.sourceforge.net>
10 #include "FileSelectionPage.h"
16 #include <ControlLook.h>
18 #include <RadioButton.h>
19 #include <TextControl.h>
23 #undef B_TRANSLATION_CONTEXT
24 #define B_TRANSLATION_CONTEXT "FileSelectionPage"
27 const uint32 kMsgOpenFilePanel
= 'open';
30 FileSelectionPage::FileSelectionPage(BMessage
* settings
, BRect frame
,
31 const char* name
, const char* description
, file_panel_mode mode
)
33 WizardPageView(settings
, frame
, name
, B_FOLLOW_ALL
,
34 B_WILL_DRAW
| B_FRAME_EVENTS
| B_FULL_UPDATE_ON_RESIZE
),
38 _BuildUI(description
);
42 FileSelectionPage::~FileSelectionPage()
48 FileSelectionPage::FrameResized(float width
, float height
)
50 WizardPageView::FrameResized(width
, height
);
56 FileSelectionPage::AttachedToWindow()
58 fSelect
->SetTarget(this);
63 FileSelectionPage::MessageReceived(BMessage
* message
)
65 switch (message
->what
) {
66 case kMsgOpenFilePanel
:
70 case B_SAVE_REQUESTED
:
71 _SetFileFromFilePanelMessage(message
);
77 WizardPageView::MessageReceived(message
);
83 FileSelectionPage::PageCompleted()
85 fSettings
->ReplaceString("file", fFile
->Text());
89 const float kFileButtonDistance
= 10;
92 FileSelectionPage::_BuildUI(const char* description
)
94 const float kSpacing
= be_control_look
->DefaultItemSpacing();
97 fDescription
= CreateDescription(rect
, "description", description
);
99 MakeHeading(fDescription
);
100 AddChild(fDescription
);
103 fSettings
->FindString("file", &file
);
105 fSelect
= new BButton(rect
, "select",
106 B_TRANSLATE_COMMENT("Select", "Button"),
107 new BMessage(kMsgOpenFilePanel
),
109 fSelect
->ResizeToPreferred();
111 float selectLeft
= rect
.right
- fSelect
->Bounds().Width();
112 rect
.right
= selectLeft
- kSpacing
;
113 fFile
= new BTextControl(rect
, "file",
114 B_TRANSLATE_COMMENT("File:", "Text control label"),
115 file
.String(), new BMessage());
116 fFile
->SetDivider(be_plain_font
->StringWidth(fFile
->Label()) + 5);
119 fSelect
->MoveTo(selectLeft
, 0);
127 FileSelectionPage::_Layout()
129 const float kSpacing
= be_control_look
->DefaultItemSpacing();
130 LayoutDescriptionVertically(fDescription
);
132 float left
= fFile
->Frame().left
;
133 float top
= fDescription
->Frame().bottom
+ kSpacing
;
135 // center "file" text field and "select" button vertically
136 float selectTop
= top
;
138 float fileHeight
= fFile
->Bounds().Height();
139 float selectHeight
= fSelect
->Bounds().Height();
140 if (fileHeight
< selectHeight
) {
141 int delta
= (int)((selectHeight
- fileHeight
+ 1) / 2);
144 int delta
= (int)((fileHeight
- selectHeight
+ 1) / 2);
148 fFile
->MoveTo(left
, fileTop
);
150 float width
= fSelect
->Frame().left
- kSpacing
- left
;
151 fFile
->ResizeTo(width
, fileHeight
);
153 left
= fSelect
->Frame().left
;
154 fSelect
->MoveTo(left
, selectTop
);
159 FileSelectionPage::_OpenFilePanel()
161 if (fFilePanel
!= NULL
)
164 const entry_ref
* directory
= NULL
;
166 BPath
file(fFile
->Text());
169 if (file
.GetParent(&parent
) == B_OK
&&
170 get_ref_for_path(parent
.Path(), &base
) == B_OK
)
173 BMessenger
messenger(this);
174 fFilePanel
= new BFilePanel(fMode
, &messenger
, directory
,
175 B_FILE_NODE
, false, NULL
, NULL
, true);
176 if (fMode
== B_SAVE_PANEL
&& file
.Leaf() != NULL
)
177 fFilePanel
->SetSaveText(file
.Leaf());
183 FileSelectionPage::_SetFileFromFilePanelMessage(BMessage
* message
)
185 if (message
->what
== B_SAVE_REQUESTED
) {
188 message
->FindRef("directory", &directory
);
189 message
->FindString("name", &name
);
190 BPath
path(&directory
);
191 if (path
.Append(name
.String()) == B_OK
)
192 fFile
->SetText(path
.Path());
195 message
->FindRef("refs", &entryRef
);
196 BEntry
entry(&entryRef
);
198 if (entry
.GetPath(&path
) == B_OK
)
199 fFile
->SetText(path
.Path());
205 FileSelectionPage::_FilePanelCanceled()