2 * Copyright 2008-2013 Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT license.
6 * Stephan Aßmus <superstippi@gmx.de>
7 * Axel Dörfler, axeld@pinc-software.de.
8 * Karsten Heimrich. <host.haiku@gmx.de>
12 #include "AbstractParametersPanel.h"
14 #include <driver_settings.h>
19 #include <DiskSystemAddOn.h>
20 #include <DiskSystemAddOnManager.h>
21 #include <GroupLayout.h>
22 #include <MessageFilter.h>
26 #undef B_TRANSLATION_CONTEXT
27 #define B_TRANSLATION_CONTEXT "AbstractParametersPanel"
30 static const uint32 kMsgOk
= 'okok';
31 static const uint32 kParameterChanged
= 'pmch';
34 class AbstractParametersPanel::EscapeFilter
: public BMessageFilter
{
36 EscapeFilter(AbstractParametersPanel
* target
)
38 BMessageFilter(B_ANY_DELIVERY
, B_ANY_SOURCE
),
43 virtual ~EscapeFilter()
47 virtual filter_result
Filter(BMessage
* message
, BHandler
** target
)
49 filter_result result
= B_DISPATCH_MESSAGE
;
50 switch (message
->what
) {
52 case B_UNMAPPED_KEY_DOWN
: {
54 if (message
->FindInt32("raw_char", (int32
*)&key
) >= B_OK
) {
55 if (key
== B_ESCAPE
) {
56 result
= B_SKIP_MESSAGE
;
69 AbstractParametersPanel
* fPanel
;
76 AbstractParametersPanel::AbstractParametersPanel(BWindow
* window
)
78 BWindow(BRect(300.0, 200.0, 600.0, 300.0), 0, B_MODAL_WINDOW_LOOK
,
79 B_MODAL_SUBSET_WINDOW_FEEL
,
80 B_ASYNCHRONOUS_CONTROLS
| B_AUTO_UPDATE_SIZE_LIMITS
),
81 fOkButton(new BButton(B_TRANSLATE("OK"), new BMessage(kMsgOk
))),
82 fReturnStatus(B_CANCELED
),
84 fEscapeFilter(new EscapeFilter(this)),
85 fExitSemaphore(create_sem(0, "AbstractParametersPanel exit")),
88 AddCommonFilter(fEscapeFilter
);
93 AbstractParametersPanel::~AbstractParametersPanel()
95 RemoveCommonFilter(fEscapeFilter
);
98 delete_sem(fExitSemaphore
);
100 if (fOkButton
->Window() == NULL
)
106 AbstractParametersPanel::QuitRequested()
108 release_sem(fExitSemaphore
);
114 AbstractParametersPanel::MessageReceived(BMessage
* message
)
116 switch (message
->what
) {
122 fReturnStatus
= B_OK
;
123 release_sem(fExitSemaphore
);
126 case kParameterChanged
:
127 fOkButton
->SetEnabled(fEditor
->ValidateParameters());
131 BWindow::MessageReceived(message
);
137 AbstractParametersPanel::Go(BString
& parameters
)
140 return Go(parameters
, storage
);
145 AbstractParametersPanel::Cancel()
147 fReturnStatus
= B_CANCELED
;
148 release_sem(fExitSemaphore
);
153 AbstractParametersPanel::Init(B_PARAMETER_EDITOR_TYPE type
,
154 const BString
& diskSystem
, BPartition
* partition
)
156 // Create partition parameter editor
158 status_t status
= B_ERROR
;
159 if (diskSystem
.IsEmpty()) {
160 status
= partition
->GetParameterEditor(type
, &fEditor
);
162 DiskSystemAddOnManager
* manager
= DiskSystemAddOnManager::Default();
163 BDiskSystemAddOn
* addOn
= manager
->GetAddOn(diskSystem
);
166 manager
->PutAddOn(addOn
);
168 status
= addOn
->GetParameterEditor(type
, &fEditor
);
171 if (status
!= B_OK
&& status
!= B_NOT_SUPPORTED
)
172 fReturnStatus
= status
;
176 BLayoutBuilder::Group
<> builder
= BLayoutBuilder::Group
<>(this,
180 AddControls(builder
, NULL
);
182 AddControls(builder
, fEditor
->View());
184 builder
.AddGroup(B_HORIZONTAL
, B_USE_DEFAULT_SPACING
)
186 .Add(new BButton(B_TRANSLATE("Cancel"), new BMessage(B_CANCEL
)))
189 .SetInsets(B_USE_DEFAULT_SPACING
);
191 SetDefaultButton(fOkButton
);
193 if (fEditor
!= NULL
) {
194 fEditor
->SetTo(partition
);
195 fEditor
->SetModificationMessage(new BMessage(kParameterChanged
));
201 AbstractParametersPanel::Go(BString
& parameters
, BMessage
& storage
)
203 // Without an editor, we cannot change anything, anyway
204 if (fEditor
== NULL
&& NeedsEditor()) {
206 if (ValidWithoutEditor() && fReturnStatus
== B_CANCELED
)
207 fReturnStatus
= B_OK
;
212 // run the window thread, to get an initial layout of the controls
218 // center the panel above the parent window
219 CenterIn(fWindow
->Frame());
224 // block this thread now, but keep the window repainting
226 status_t status
= acquire_sem_etc(fExitSemaphore
, 1,
227 B_CAN_INTERRUPT
| B_RELATIVE_TIMEOUT
, 50000);
228 if (status
!= B_TIMED_OUT
&& status
!= B_INTERRUPTED
)
230 fWindow
->UpdateIfNeeded();
236 if (fReturnStatus
== B_OK
) {
237 if (fEditor
!= NULL
&& fEditor
->ValidateParameters()) {
238 status_t status
= fEditor
->GetParameters(parameters
);
240 fReturnStatus
= status
;
242 if (fReturnStatus
== B_OK
)
243 fReturnStatus
= ParametersReceived(parameters
, storage
);
247 status_t status
= fReturnStatus
;
250 // NOTE: this object is toast now!
257 AbstractParametersPanel::NeedsEditor() const
264 AbstractParametersPanel::ValidWithoutEditor() const
270 AbstractParametersPanel::ParametersReceived(const BString
& parameters
,
278 AbstractParametersPanel::AddControls(BLayoutBuilder::Group
<>& builder
,
281 if (editorView
!= NULL
)
282 builder
.Add(editorView
);