xref: /haiku/src/apps/terminal/SetTitleDialog.cpp (revision 7a74a5df454197933bc6e80a542102362ee98703)
1 /*
2  * Copyright 2010, Ingo Weinhold, ingo_weinhold@gmx.de.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include "SetTitleDialog.h"
8 
9 #include <Button.h>
10 #include <Catalog.h>
11 #include <LayoutBuilder.h>
12 #include <TextControl.h>
13 
14 
15 static const uint32 kMessageOK				= 'okok';
16 static const uint32 kMessageDefault			= 'dflt';
17 static const uint32 kMessageTitleChanged	= 'chng';
18 
19 
20 #undef B_TRANSLATION_CONTEXT
21 #define B_TRANSLATION_CONTEXT "Terminal SetTitleWindow"
22 
23 
24 // #pragma mark - SetTitleDialog
25 
26 
27 SetTitleDialog::SetTitleDialog(const char* dialogTitle, const char* label,
28 	const char* toolTip)
29 	:
30 	BWindow(BRect(0, 0, 0, 0), dialogTitle, B_BORDERED_WINDOW_LOOK,
31 		B_FLOATING_APP_WINDOW_FEEL,
32 		B_AUTO_UPDATE_SIZE_LIMITS | B_CLOSE_ON_ESCAPE),
33 	fListener(NULL),
34 	fTitle(),
35 	fTitleUserDefined(false)
36 {
37 	BLayoutBuilder::Group<>(this, B_VERTICAL)
38 		.SetInsets(10, 10, 10, 10)
39 		.Add(fTitleTextControl = new BTextControl("title", label, "", NULL))
40 		.AddGroup(B_HORIZONTAL)
41 			.Add(fDefaultButton = new BButton("defaultButton",
42 				B_TRANSLATE("Use default"), new BMessage(kMessageDefault)))
43 			.AddGlue()
44 			.Add(fCancelButton = new BButton("cancelButton",
45 				B_TRANSLATE("Cancel"), new BMessage(B_QUIT_REQUESTED)))
46 			.Add(fOKButton = new BButton("okButton", B_TRANSLATE("OK"),
47 				new BMessage(kMessageOK)));
48 
49 	fTitleTextControl->SetToolTip(toolTip);
50 
51 	fOKButton->MakeDefault(true);
52 
53 	UpdateSizeLimits();
54 		// as a courtesy to our creator, who might want to place us
55 }
56 
57 
58 SetTitleDialog::~SetTitleDialog()
59 {
60 	if (fListener != NULL) {
61 		// reset to old title
62 		fListener->TitleChanged(this, fOldTitle, fOldTitleUserDefined);
63 
64 		Listener* listener = fListener;
65 		fListener = NULL;
66 		listener->SetTitleDialogDone(this);
67 	}
68 }
69 
70 
71 void
72 SetTitleDialog::Go(const BString& title, bool titleUserDefined,
73 	Listener* listener)
74 {
75 	fTitle = fOldTitle = title;
76 	fTitleUserDefined = fOldTitleUserDefined = titleUserDefined;
77 
78 	fDefaultButton->SetEnabled(titleUserDefined);
79 
80 	fTitleTextControl->SetText(fTitle);
81 	fTitleTextControl->SetModificationMessage(
82 		new BMessage(kMessageTitleChanged));
83 	fTitleTextControl->MakeFocus(true);
84 
85 	fListener = listener;
86 
87 	Show();
88 }
89 
90 
91 void
92 SetTitleDialog::Finish()
93 {
94 	if (Listener* listener = fListener) {
95 		fListener = NULL;
96 		listener->SetTitleDialogDone(this);
97 	}
98 
99 	PostMessage(B_QUIT_REQUESTED);
100 }
101 
102 
103 void
104 SetTitleDialog::MessageReceived(BMessage* message)
105 {
106 	switch (message->what) {
107 		case kMessageDefault:
108 			if (fListener != NULL)
109 				fListener->TitleChanged(this, BString(), false);
110 			// We're done now, fall through...
111 
112 		case kMessageOK:
113 			Finish();
114 			break;
115 
116 		case kMessageTitleChanged:
117 			fTitle = fTitleTextControl->Text();
118 			fTitleUserDefined = true;
119 
120 			fDefaultButton->SetEnabled(true);
121 
122 			if (fListener != NULL)
123 				fListener->TitleChanged(this, fTitle, fTitleUserDefined);
124 			break;
125 
126 		default:
127 			BWindow::MessageReceived(message);
128 			break;
129 	}
130 
131 }
132 
133 
134 // #pragma mark - SetTitleDialog
135 
136 
137 SetTitleDialog::Listener::~Listener()
138 {
139 }
140