xref: /haiku/src/apps/deskcalc/CalcWindow.cpp (revision 7749d0bb0c358a3279b1b9cc76d8376e900130a5)
1 /*
2  * Copyright 2006-2011 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 <Catalog.h>
19 #include <Dragger.h>
20 #include <Screen.h>
21 
22 #include "CalcView.h"
23 
24 
25 #undef B_TRANSLATE_CONTEXT
26 #define B_TRANSLATE_CONTEXT "Window"
27 
28 
29 CalcWindow::CalcWindow(BRect frame, BMessage* settings)
30 	:
31 	BWindow(frame, B_TRANSLATE_SYSTEM_NAME("DeskCalc"), B_TITLED_WINDOW,
32 		B_ASYNCHRONOUS_CONTROLS | B_NOT_ANCHORED_ON_ACTIVATE)
33 {
34 	// create calculator view with calculator description and
35 	// desktop background color
36 	BScreen screen(this);
37 	rgb_color baseColor = screen.DesktopColor();
38 
39 	SetSizeLimits(100.0, 400.0, 100.0, 400.0);
40 
41 	frame.OffsetTo(B_ORIGIN);
42 	fCalcView = new CalcView(frame, baseColor, settings);
43 
44 	// create replicant dragger
45 	BRect replicantFrame(frame);
46 	replicantFrame.top = replicantFrame.bottom - 7.0f;
47 	replicantFrame.left = replicantFrame.right - 7.0f;
48 	BDragger* dragger = new BDragger(replicantFrame, fCalcView,
49 		B_FOLLOW_RIGHT | B_FOLLOW_BOTTOM);
50 
51 	// attach views
52 	AddChild(fCalcView);
53 	fCalcView->AddChild(dragger);
54 
55 	BRect rect;
56 	if (settings->FindRect("window frame", &rect) == B_OK)
57 		SetFrame(rect);
58 	else
59 		SetFrame(frame, true);
60 }
61 
62 
63 CalcWindow::~CalcWindow()
64 {
65 }
66 
67 
68 void
69 CalcWindow::Show()
70 {
71 	// NOTE: done here because the CalcView
72 	// turns on numlock if the options say so...
73 	// so we want to call MakeFocus() after
74 	// the options have been read for sure...
75 	fCalcView->MakeFocus();
76 	BWindow::Show();
77 }
78 
79 
80 bool
81 CalcWindow::QuitRequested()
82 {
83 	be_app->PostMessage(B_QUIT_REQUESTED);
84 	Hide();
85 
86 	// NOTE: don't quit, since the app needs us
87 	// for saving settings yet...
88 	return false;
89 }
90 
91 
92 status_t
93 CalcWindow::SaveSettings(BMessage* archive) const
94 {
95 	status_t ret = archive->AddRect("window frame", Frame());
96 	if (ret < B_OK)
97 		return ret;
98 
99 	return fCalcView->SaveSettings(archive);
100 }
101 
102 
103 void
104 CalcWindow::SetFrame(BRect frame, bool forceCenter)
105 {
106 	// make sure window frame is on screen (center, if not)
107 	BScreen screen(this);
108 	BRect screenFrame = screen.Frame();
109 	if (forceCenter || !screenFrame.Contains(frame)) {
110 		float left = (screenFrame.Width() - frame.Width()) / 2.0;
111 		float top = (screenFrame.Height() - frame.Height()) / 2.0;
112 		left += screenFrame.left;
113 		top += screenFrame.top;
114 		frame.OffsetTo(left, top);
115 	}
116 
117 	MoveTo(frame.left, frame.top);
118 	ResizeTo(frame.Width(), frame.Height());
119 }
120