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