xref: /haiku/src/preferences/shortcuts/EditWindow.cpp (revision 583c561cb259ab00c3621b9446268ab68ef6b6e5)
1149ce46dSAdrien Destugues /*
2149ce46dSAdrien Destugues  * Copyright 2015 Haiku, Inc. All rights reserved.
3149ce46dSAdrien Destugues  * Distributed under the terms of the MIT License.
4149ce46dSAdrien Destugues  *
5149ce46dSAdrien Destugues  * Authors:
6149ce46dSAdrien Destugues  * 		Josef Gajdusek
7149ce46dSAdrien Destugues  */
8149ce46dSAdrien Destugues 
9149ce46dSAdrien Destugues 
10149ce46dSAdrien Destugues #include "EditWindow.h"
11149ce46dSAdrien Destugues 
12149ce46dSAdrien Destugues #include <math.h>
13149ce46dSAdrien Destugues 
14149ce46dSAdrien Destugues #include <Button.h>
15149ce46dSAdrien Destugues #include <LayoutBuilder.h>
16149ce46dSAdrien Destugues #include <TextControl.h>
17149ce46dSAdrien Destugues #include <String.h>
18149ce46dSAdrien Destugues #include <StringView.h>
19149ce46dSAdrien Destugues 
20149ce46dSAdrien Destugues #include "ShortcutsWindow.h"
21149ce46dSAdrien Destugues 
22149ce46dSAdrien Destugues 
EditWindow(const char * placeholder,uint32 flags)23149ce46dSAdrien Destugues EditWindow::EditWindow(const char* placeholder, uint32 flags)
24149ce46dSAdrien Destugues 	:
25149ce46dSAdrien Destugues 	BWindow(BRect(0, 0, 0, 0), "", B_MODAL_WINDOW, flags)
26149ce46dSAdrien Destugues {
27149ce46dSAdrien Destugues 	fTextControl = new BTextControl("", placeholder, NULL);
28149ce46dSAdrien Destugues 
29*583c561cSHumdinger 	BButton* okButton = new BButton("OK", new BMessage(B_CONTROL_MODIFIED));
30149ce46dSAdrien Destugues 	okButton->SetExplicitAlignment(BAlignment(B_ALIGN_RIGHT, B_ALIGN_TOP));
31149ce46dSAdrien Destugues 	SetDefaultButton(okButton);
32149ce46dSAdrien Destugues 
33149ce46dSAdrien Destugues 	BLayoutBuilder::Group<>(this, B_VERTICAL)
34149ce46dSAdrien Destugues 		.SetInsets(B_USE_WINDOW_INSETS)
35149ce46dSAdrien Destugues 		.Add(fTextControl)
36149ce46dSAdrien Destugues 		.Add(okButton);
37149ce46dSAdrien Destugues }
38149ce46dSAdrien Destugues 
39149ce46dSAdrien Destugues 
40149ce46dSAdrien Destugues void
MessageReceived(BMessage * message)41149ce46dSAdrien Destugues EditWindow::MessageReceived(BMessage* message)
42149ce46dSAdrien Destugues {
43149ce46dSAdrien Destugues 	switch (message->what) {
44149ce46dSAdrien Destugues 		case B_CONTROL_MODIFIED:
45149ce46dSAdrien Destugues 			delete_sem(fSem);
46149ce46dSAdrien Destugues 			break;
47149ce46dSAdrien Destugues 		default:
48149ce46dSAdrien Destugues 			BWindow::MessageReceived(message);
49149ce46dSAdrien Destugues 			break;
50149ce46dSAdrien Destugues 	}
51149ce46dSAdrien Destugues }
52149ce46dSAdrien Destugues 
53149ce46dSAdrien Destugues 
54149ce46dSAdrien Destugues BString
Go()55149ce46dSAdrien Destugues EditWindow::Go()
56149ce46dSAdrien Destugues {
57149ce46dSAdrien Destugues 	fSem = create_sem(0, "EditSem");
58149ce46dSAdrien Destugues 	if (fSem < B_OK) {
59149ce46dSAdrien Destugues 		Quit();
60149ce46dSAdrien Destugues 		return "";
61149ce46dSAdrien Destugues 	}
62149ce46dSAdrien Destugues 
63149ce46dSAdrien Destugues 	BSize psize = GetLayout()->PreferredSize();
64149ce46dSAdrien Destugues 	ResizeTo(max_c(be_plain_font->StringWidth(fTextControl->Text()) * 1.5,
65149ce46dSAdrien Destugues 				psize.Width()),
66149ce46dSAdrien Destugues 		psize.Height());
67149ce46dSAdrien Destugues 	Show();
68149ce46dSAdrien Destugues 	CenterOnScreen();
69149ce46dSAdrien Destugues 
70149ce46dSAdrien Destugues 	acquire_sem(fSem);
71abed891dSMichael Lotz 	BString result = fTextControl->Text();
72149ce46dSAdrien Destugues 	if (Lock())
73149ce46dSAdrien Destugues 		Quit();
74abed891dSMichael Lotz 
75abed891dSMichael Lotz 	return result;
76149ce46dSAdrien Destugues }
77