xref: /haiku/src/apps/deskcalc/CalcWindow.cpp (revision cfc3fa87da824bdf593eb8b817a83b6376e77935)
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)
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 	frame.OffsetTo(B_ORIGIN);
36 	fCalcView = new CalcView(frame, baseColor);
37 
38 	// create replicant dragger
39 	frame.top = frame.bottom - 7.0f;
40 	frame.left = frame.right - 7.0f;
41 	BDragger* dragger = new BDragger(frame, fCalcView,
42 		B_FOLLOW_RIGHT | B_FOLLOW_BOTTOM);
43 
44 	// attach views
45 	AddChild(fCalcView);
46 	fCalcView->AddChild(dragger);
47 
48 	SetSizeLimits(50.0, 400.0, 22.0, 400.0);
49 }
50 
51 
52 CalcWindow::~CalcWindow()
53 {
54 }
55 
56 
57 void
58 CalcWindow::Show()
59 {
60 	// NOTE: done here because the CalcView
61 	// turns on numlock if the options say so...
62 	// so we want to call MakeFocus() after
63 	// the options have been read for sure...
64 	fCalcView->MakeFocus();
65 	BWindow::Show();
66 }
67 
68 
69 bool
70 CalcWindow::QuitRequested()
71 {
72 	be_app->PostMessage(B_QUIT_REQUESTED);
73 	Hide();
74 
75 	// NOTE: don't quit, since the app needs us
76 	// for saving settings yet...
77 	return false;
78 }
79 
80 
81 bool
82 CalcWindow::LoadSettings(BMessage* archive)
83 {
84 	status_t ret = fCalcView->LoadSettings(archive);
85 
86 	if (ret < B_OK)
87 		return false;
88 
89 	BRect frame;
90 	ret = archive->FindRect("window frame", &frame);
91 	if (ret < B_OK)
92 		return false;
93 
94 	SetFrame(frame);
95 
96 	return true;
97 }
98 
99 
100 status_t
101 CalcWindow::SaveSettings(BMessage* archive) const
102 {
103 	status_t ret = archive->AddRect("window frame", Frame());
104 	if (ret < B_OK)
105 		return ret;
106 
107 	return fCalcView->SaveSettings(archive);
108 }
109 
110 
111 void
112 CalcWindow::SetFrame(BRect frame, bool forceCenter)
113 {
114 	// make sure window frame is on screen (center, if not)
115 	BScreen screen(this);
116 	BRect screenFrame = screen.Frame();
117 	if (forceCenter || !screenFrame.Contains(frame)) {
118 		float left = (screenFrame.Width() - frame.Width()) / 2.0;
119 		float top = (screenFrame.Height() - frame.Height()) / 2.0;
120 		left += screenFrame.left;
121 		top += screenFrame.top;
122 		frame.OffsetTo(left, top);
123 	}
124 
125 	MoveTo(frame.left, frame.top);
126 	ResizeTo(frame.Width(), frame.Height());
127 }
128