1/*
2 * Copyright 2015 Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
4 *
5 * Authors:
6 * 		Josef Gajdusek
7 */
8
9
10#include "EditWindow.h"
11
12#include <math.h>
13
14#include <Button.h>
15#include <LayoutBuilder.h>
16#include <TextControl.h>
17#include <String.h>
18#include <StringView.h>
19
20#include "ShortcutsWindow.h"
21
22
23EditWindow::EditWindow(const char* placeholder, uint32 flags)
24	:
25	BWindow(BRect(0, 0, 0, 0), "", B_MODAL_WINDOW, flags)
26{
27	fTextControl = new BTextControl("", placeholder, NULL);
28
29	BButton* okButton = new BButton("OK", new BMessage(B_CONTROL_MODIFIED));
30	okButton->SetExplicitAlignment(BAlignment(B_ALIGN_RIGHT, B_ALIGN_TOP));
31	SetDefaultButton(okButton);
32
33	BLayoutBuilder::Group<>(this, B_VERTICAL)
34		.SetInsets(B_USE_WINDOW_INSETS)
35		.Add(fTextControl)
36		.Add(okButton);
37}
38
39
40void
41EditWindow::MessageReceived(BMessage* message)
42{
43	switch (message->what) {
44		case B_CONTROL_MODIFIED:
45			delete_sem(fSem);
46			break;
47		default:
48			BWindow::MessageReceived(message);
49			break;
50	}
51}
52
53
54BString
55EditWindow::Go()
56{
57	fSem = create_sem(0, "EditSem");
58	if (fSem < B_OK) {
59		Quit();
60		return "";
61	}
62
63	BSize psize = GetLayout()->PreferredSize();
64	ResizeTo(max_c(be_plain_font->StringWidth(fTextControl->Text()) * 1.5,
65				psize.Width()),
66		psize.Height());
67	Show();
68	CenterOnScreen();
69
70	acquire_sem(fSem);
71	BString result = fTextControl->Text();
72	if (Lock())
73		Quit();
74
75	return result;
76}
77