xref: /haiku/src/bin/screen_blanker/PasswordWindow.cpp (revision 020cbad9d40235a2c50a81a42d69912a5ff8fbc4)
1 /*
2  * Copyright 2003-2007, Haiku, Inc. All Rights Reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Michael Phipps
7  *		Jérôme Duval, jerome.duval@free.fr
8  *		Julun <host.haiku@gmx.de>
9  */
10 
11 
12 #include "PasswordWindow.h"
13 
14 
15 #include <Application.h>
16 #include <Box.h>
17 #include <Button.h>
18 #include <Screen.h>
19 
20 
21 PasswordWindow::PasswordWindow()
22 	: BWindow(BRect(100, 100, 400, 230), "Enter Password", B_NO_BORDER_WINDOW_LOOK,
23 		B_FLOATING_ALL_WINDOW_FEEL,
24 		B_NOT_MOVABLE | B_NOT_CLOSABLE |B_NOT_ZOOMABLE | B_NOT_MINIMIZABLE
25 		| B_NOT_RESIZABLE | B_ASYNCHRONOUS_CONTROLS , B_ALL_WORKSPACES)
26 {
27 	BView* topView = new BView(Bounds(), "topView", B_FOLLOW_ALL, B_WILL_DRAW);
28 	topView->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
29 	AddChild(topView);
30 
31 	BRect bounds(Bounds());
32 	bounds.InsetBy(10.0, 10.0);
33 
34 	BBox *customBox = new BBox(bounds, "customBox", B_FOLLOW_NONE);
35 	topView->AddChild(customBox);
36 	customBox->SetLabel("Unlock screen saver");
37 
38 	bounds.top += 10.0;
39 	fPassword = new BTextControl(bounds, "password", "Enter password:",
40 		"VeryLongPasswordPossible", B_FOLLOW_NONE);
41 	customBox->AddChild(fPassword);
42 	fPassword->MakeFocus(true);
43 	fPassword->ResizeToPreferred();
44 	fPassword->TextView()->HideTyping(true);
45 	fPassword->SetDivider(be_plain_font->StringWidth("Enter password:") + 5.0);
46 
47 	BButton* button = new BButton(BRect(), "unlock", "Unlock",
48 		new BMessage(kMsgUnlock), B_FOLLOW_NONE);
49 	customBox->AddChild(button);
50 	button->MakeDefault(true);
51 	button->ResizeToPreferred();
52 	button->SetTarget(NULL, be_app);
53 
54 	BRect frame = fPassword->Frame();
55 	button->MoveTo(frame.right - button->Bounds().Width(), frame.bottom + 10.0);
56 	customBox->ResizeTo(frame.right + 10.0,	button->Frame().bottom + 10.0);
57 
58 	frame = customBox->Frame();
59 	ResizeTo(frame.right + 10.0, frame.bottom + 10.0);
60 
61 	BScreen screen(this);
62 	MoveTo(screen.Frame().left + (screen.Frame().Width() - Bounds().Width()) / 2,
63 		screen.Frame().top + (screen.Frame().Height() - Bounds().Height()) / 2);
64 }
65 
66 
67 void
68 PasswordWindow::SetPassword(const char* text)
69 {
70 	if (Lock()) {
71 		fPassword->SetText(text);
72 		fPassword->MakeFocus(true);
73 		Unlock();
74 	}
75 }
76