xref: /haiku/src/apps/deskcalc/CalcWindow.cpp (revision 3c6e2dd68577c34d93e17f19711f6245bf6d0915)
1 /*
2  * Copyright 2006 Haiku, Inc. All Rights Reserved.
3  * Copyright 1997, 1998 R3 Software Ltd. All Rights Reserved.
4  * Distributed under the terms of the MIT License.
5  *
6  * Authors:
7  *		Timothy Wayper <timmy@wunderbear.com>
8  *		Stephan Aßmus <superstippi@gmx.de>
9  */
10 
11 #include "CalcWindow.h"
12 
13 #include <stdlib.h>
14 #include <stdio.h>
15 #include <assert.h>
16 
17 #include <Application.h>
18 #include <Dragger.h>
19 #include <Screen.h>
20 
21 #include "CalcView.h"
22 
23 
24 static const char* kWindowTitle		= "DeskCalc";
25 
26 
27 CalcWindow::CalcWindow(BRect frame, BMessage* settings)
28 	: BWindow(frame, kWindowTitle, B_TITLED_WINDOW, B_ASYNCHRONOUS_CONTROLS)
29 {
30 	// create calculator view with calculator description and
31 	// desktop background color
32 	BScreen screen(this);
33 	rgb_color baseColor = screen.DesktopColor();
34 
35 	SetSizeLimits(100.0, 400.0, 100.0, 400.0);
36 
37 	frame.OffsetTo(B_ORIGIN);
38 	fCalcView = new CalcView(frame, baseColor, settings);
39 
40 	// create replicant dragger
41 	BRect replicantFrame(frame);
42 	replicantFrame.top = replicantFrame.bottom - 7.0f;
43 	replicantFrame.left = replicantFrame.right - 7.0f;
44 	BDragger* dragger = new BDragger(replicantFrame, fCalcView,
45 		B_FOLLOW_RIGHT | B_FOLLOW_BOTTOM);
46 
47 	// attach views
48 	AddChild(fCalcView);
49 	fCalcView->AddChild(dragger);
50 
51 	BRect rect;
52 	if (settings->FindRect("window frame", &rect) == B_OK)
53 		SetFrame(rect);
54 	else
55 		SetFrame(frame, true);
56 }
57 
58 
59 CalcWindow::~CalcWindow()
60 {
61 }
62 
63 
64 void
65 CalcWindow::Show()
66 {
67 	// NOTE: done here because the CalcView
68 	// turns on numlock if the options say so...
69 	// so we want to call MakeFocus() after
70 	// the options have been read for sure...
71 	fCalcView->MakeFocus();
72 	BWindow::Show();
73 }
74 
75 
76 bool
77 CalcWindow::QuitRequested()
78 {
79 	be_app->PostMessage(B_QUIT_REQUESTED);
80 	Hide();
81 
82 	// NOTE: don't quit, since the app needs us
83 	// for saving settings yet...
84 	return false;
85 }
86 
87 
88 status_t
89 CalcWindow::SaveSettings(BMessage* archive) const
90 {
91 	status_t ret = archive->AddRect("window frame", Frame());
92 	if (ret < B_OK)
93 		return ret;
94 
95 	return fCalcView->SaveSettings(archive);
96 }
97 
98 
99 void
100 CalcWindow::SetFrame(BRect frame, bool forceCenter)
101 {
102 	// make sure window frame is on screen (center, if not)
103 	BScreen screen(this);
104 	BRect screenFrame = screen.Frame();
105 	if (forceCenter || !screenFrame.Contains(frame)) {
106 		float left = (screenFrame.Width() - frame.Width()) / 2.0;
107 		float top = (screenFrame.Height() - frame.Height()) / 2.0;
108 		left += screenFrame.left;
109 		top += screenFrame.top;
110 		frame.OffsetTo(left, top);
111 	}
112 
113 	MoveTo(frame.left, frame.top);
114 	ResizeTo(frame.Width(), frame.Height());
115 }
116