#include <Alert.h>
#include <Box.h>
#include <Catalog.h>
#include <ControlLook.h>
#include <LayoutBuilder.h>
#include <Locale.h>
#include <MenuItem.h>
#include <PopUpMenu.h>
#include <WindowPrivate.h>
#include "AutoLock.h"
#include "ContainerWindow.h"
#include "Commands.h"
#include "Screen.h"
#include "SelectionWindow.h"
const uint32 kSelectButtonPressed = 'sbpr';
#undef B_TRANSLATION_CONTEXT
#define B_TRANSLATION_CONTEXT "SelectionWindow"
SelectionWindow::SelectionWindow(BContainerWindow* window)
:
BWindow(BRect(0, 0, 270, 0), B_TRANSLATE("Select"), B_TITLED_WINDOW,
B_NOT_ZOOMABLE | B_NOT_MINIMIZABLE | B_NOT_V_RESIZABLE
| B_NO_WORKSPACE_ACTIVATION | B_ASYNCHRONOUS_CONTROLS
| B_NOT_ANCHORED_ON_ACTIVATE | B_AUTO_UPDATE_SIZE_LIMITS
| B_CLOSE_ON_ESCAPE),
fParentWindow(window)
{
if (window->Feel() & kDesktopWindowFeel) {
SetFeel(B_NORMAL_WINDOW_FEEL);
}
AddToSubset(fParentWindow);
BMenu* menu = new BPopUpMenu("popup");
menu->AddItem(new BMenuItem(B_TRANSLATE("starts with"), NULL));
menu->AddItem(new BMenuItem(B_TRANSLATE("ends with"), NULL));
menu->AddItem(new BMenuItem(B_TRANSLATE("contains"), NULL));
menu->AddItem(new BMenuItem(B_TRANSLATE("matches wildcard expression"),
NULL));
menu->AddItem(new BMenuItem(B_TRANSLATE("matches regular expression"),
NULL));
menu->SetLabelFromMarked(true);
menu->ItemAt(3)->SetMarked(true);
fMatchingTypeMenuField = new BMenuField("name", B_TRANSLATE("Name"), menu);
fExpressionTextControl = new BTextControl("expression", NULL, "", NULL);
fInverseCheckBox = new BCheckBox("inverse", B_TRANSLATE("Invert"), NULL);
fInverseCheckBox->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, B_SIZE_UNSET));
fIgnoreCaseCheckBox = new BCheckBox(
"ignore", B_TRANSLATE("Ignore case"), NULL);
fIgnoreCaseCheckBox->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, B_SIZE_UNSET));
fIgnoreCaseCheckBox->SetValue(1);
fSelectButton = new BButton("select", B_TRANSLATE("Select"),
new BMessage(kSelectButtonPressed));
fSelectButton->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, B_SIZE_UNSET));
fSelectButton->MakeDefault(true);
BLayoutBuilder::Group<>(this, B_VERTICAL, B_USE_HALF_ITEM_SPACING)
.SetInsets(B_USE_WINDOW_SPACING)
.Add(fMatchingTypeMenuField)
.Add(fExpressionTextControl)
.AddGroup(B_HORIZONTAL)
.Add(fInverseCheckBox)
.Add(fIgnoreCaseCheckBox)
.AddGlue(100.0)
.Add(fSelectButton)
.End()
.End();
Run();
Lock();
MoveCloseToMouse();
fExpressionTextControl->MakeFocus(true);
Unlock();
}
void
SelectionWindow::MessageReceived(BMessage* message)
{
switch (message->what) {
case kSelectButtonPressed:
{
Hide();
BMessage* selectionInfo = new BMessage(kSelectMatchingEntries);
selectionInfo->AddInt32("ExpressionType", ExpressionType());
BString expression;
Expression(expression);
selectionInfo->AddString("Expression", expression.String());
selectionInfo->AddBool("InvertSelection", Invert());
selectionInfo->AddBool("IgnoreCase", IgnoreCase());
fParentWindow->PostMessage(selectionInfo);
break;
}
default:
_inherited::MessageReceived(message);
break;
}
}
bool
SelectionWindow::QuitRequested()
{
Hide();
return false;
}
void
SelectionWindow::MoveCloseToMouse()
{
uint32 buttons;
BPoint mousePosition;
ChildAt((int32)0)->GetMouse(&mousePosition, &buttons);
ConvertToScreen(&mousePosition);
BPoint windowPosition = BPoint(mousePosition.x - Frame().Width() / 2,
mousePosition.y - Frame().Height() / 2);
BScreen screen;
windowPosition.x
= MAX(20, MIN(screen.Frame().right - 20 - Frame().Width(),
windowPosition.x));
windowPosition.y = MAX(20,
MIN(screen.Frame().bottom - 20 - Frame().Height(), windowPosition.y));
MoveTo(windowPosition);
SetWorkspaces(1UL << current_workspace());
}
TrackerStringExpressionType
SelectionWindow::ExpressionType() const
{
if (!fMatchingTypeMenuField->LockLooper())
return kNone;
BMenuItem* item = fMatchingTypeMenuField->Menu()->FindMarked();
if (item == NULL) {
fMatchingTypeMenuField->UnlockLooper();
return kNone;
}
int32 index = fMatchingTypeMenuField->Menu()->IndexOf(item);
fMatchingTypeMenuField->UnlockLooper();
if (index < kStartsWith || index > kRegexpMatch)
return kNone;
TrackerStringExpressionType typeArray[] = { kStartsWith, kEndsWith,
kContains, kGlobMatch, kRegexpMatch};
return typeArray[index];
}
void
SelectionWindow::Expression(BString &result) const
{
if (!fExpressionTextControl->LockLooper())
return;
result = fExpressionTextControl->Text();
fExpressionTextControl->UnlockLooper();
}
bool
SelectionWindow::IgnoreCase() const
{
if (!fIgnoreCaseCheckBox->LockLooper()) {
return true;
}
bool ignore = fIgnoreCaseCheckBox->Value() != 0;
fIgnoreCaseCheckBox->UnlockLooper();
return ignore;
}
bool
SelectionWindow::Invert() const
{
if (!fInverseCheckBox->LockLooper()) {
return false;
}
bool inverse = fInverseCheckBox->Value() != 0;
fInverseCheckBox->UnlockLooper();
return inverse;
}