xref: /haiku/src/apps/terminal/SetTitleDialog.cpp (revision 13581b3d2a71545960b98fefebc5225b5bf29072)
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 	fOldTitleUserDefined(false),
36 	fTitleUserDefined(false)
37 {
38 	BLayoutBuilder::Group<>(this, B_VERTICAL)
39 		.SetInsets(10, 10, 10, 10)
40 		.Add(fTitleTextControl = new BTextControl("title", label, "", NULL))
41 		.AddGroup(B_HORIZONTAL)
42 			.Add(fDefaultButton = new BButton("defaultButton",
43 				B_TRANSLATE("Use default"), new BMessage(kMessageDefault)))
44 			.AddGlue()
45 			.Add(fCancelButton = new BButton("cancelButton",
46 				B_TRANSLATE("Cancel"), new BMessage(B_QUIT_REQUESTED)))
47 			.Add(fOKButton = new BButton("okButton", B_TRANSLATE("OK"),
48 				new BMessage(kMessageOK)));
49 
50 	fTitleTextControl->SetToolTip(toolTip);
51 
52 	fOKButton->MakeDefault(true);
53 
54 	UpdateSizeLimits();
55 		// as a courtesy to our creator, who might want to place us
56 }
57 
58 
59 SetTitleDialog::~SetTitleDialog()
60 {
61 	if (fListener != NULL) {
62 		// reset to old title
63 		fListener->TitleChanged(this, fOldTitle, fOldTitleUserDefined);
64 
65 		Listener* listener = fListener;
66 		fListener = NULL;
67 		listener->SetTitleDialogDone(this);
68 	}
69 }
70 
71 
72 void
73 SetTitleDialog::Go(const BString& title, bool titleUserDefined,
74 	Listener* listener)
75 {
76 	fTitle = fOldTitle = title;
77 	fTitleUserDefined = fOldTitleUserDefined = titleUserDefined;
78 
79 	fDefaultButton->SetEnabled(titleUserDefined);
80 
81 	fTitleTextControl->SetText(fTitle);
82 	fTitleTextControl->SetModificationMessage(
83 		new BMessage(kMessageTitleChanged));
84 	fTitleTextControl->MakeFocus(true);
85 
86 	fListener = listener;
87 
88 	Show();
89 }
90 
91 
92 void
93 SetTitleDialog::Finish()
94 {
95 	if (Listener* listener = fListener) {
96 		fListener = NULL;
97 		listener->SetTitleDialogDone(this);
98 	}
99 
100 	PostMessage(B_QUIT_REQUESTED);
101 }
102 
103 
104 void
105 SetTitleDialog::MessageReceived(BMessage* message)
106 {
107 	switch (message->what) {
108 		case kMessageDefault:
109 			if (fListener != NULL)
110 				fListener->TitleChanged(this, BString(), false);
111 			// We're done now, fall through...
112 
113 		case kMessageOK:
114 			Finish();
115 			break;
116 
117 		case kMessageTitleChanged:
118 			fTitle = fTitleTextControl->Text();
119 			fTitleUserDefined = true;
120 
121 			fDefaultButton->SetEnabled(true);
122 
123 			if (fListener != NULL)
124 				fListener->TitleChanged(this, fTitle, fTitleUserDefined);
125 			break;
126 
127 		default:
128 			BWindow::MessageReceived(message);
129 			break;
130 	}
131 
132 }
133 
134 
135 // #pragma mark - SetTitleDialog
136 
137 
138 SetTitleDialog::Listener::~Listener()
139 {
140 }
141