17cb395c4SStephan Aßmus/*
29f5d4ecdSJohn Scipione * Copyright 2006-2012 Haiku, Inc. All Rights Reserved.
37cb395c4SStephan Aßmus * Copyright 1997, 1998 R3 Software Ltd. All Rights Reserved.
47cb395c4SStephan Aßmus * Distributed under the terms of the MIT License.
57cb395c4SStephan Aßmus *
67cb395c4SStephan Aßmus * Authors:
7b98de092SJohn Scipione *		Stephan A��mus, superstippi@gmx.de
8b98de092SJohn Scipione *		John Scipione, jscipione@gmail.com
9b98de092SJohn Scipione *		Timothy Wayper, timmy@wunderbear.com
107cb395c4SStephan Aßmus */
117cb395c4SStephan Aßmus
129f5d4ecdSJohn Scipione
137cb395c4SStephan Aßmus#include "CalcWindow.h"
147cb395c4SStephan Aßmus
157cb395c4SStephan Aßmus#include <stdlib.h>
167cb395c4SStephan Aßmus#include <stdio.h>
177cb395c4SStephan Aßmus#include <assert.h>
187cb395c4SStephan Aßmus
197cb395c4SStephan Aßmus#include <Application.h>
20cd7548f5SJonas Sundström#include <Catalog.h>
217cb395c4SStephan Aßmus#include <Dragger.h>
227cb395c4SStephan Aßmus#include <Screen.h>
237cb395c4SStephan Aßmus
240046f444SJohn Scipione#include "CalcApplication.h"
2560ba75c5SJohn Scipione#include "CalcOptions.h"
267cb395c4SStephan Aßmus#include "CalcView.h"
277cb395c4SStephan Aßmus
287cb395c4SStephan Aßmus
29546208a5SOliver Tappe#undef B_TRANSLATION_CONTEXT
30546208a5SOliver Tappe#define B_TRANSLATION_CONTEXT "Window"
317cb395c4SStephan Aßmus
327cb395c4SStephan Aßmus
33accec9a7SAlexandre DecknerCalcWindow::CalcWindow(BRect frame, BMessage* settings)
34560ff447SJonas Sundström	:
350046f444SJohn Scipione	BWindow(frame, kAppName, B_TITLED_WINDOW, B_ASYNCHRONOUS_CONTROLS
360046f444SJohn Scipione		| B_NOT_ANCHORED_ON_ACTIVATE)
377cb395c4SStephan Aßmus{
387cb395c4SStephan Aßmus	// create calculator view with calculator description and
397cb395c4SStephan Aßmus	// desktop background color
407cb395c4SStephan Aßmus	BScreen screen(this);
417cb395c4SStephan Aßmus	rgb_color baseColor = screen.DesktopColor();
427cb395c4SStephan Aßmus
4360ba75c5SJohn Scipione	// Size Limits are defined in CalcView.h
4460ba75c5SJohn Scipione	SetSizeLimits(kMinimumWidthBasic, kMaximumWidthBasic,
4529fbfe31SJohn Scipione		kMinimumHeightBasic, kMaximumHeightBasic);
46bc3229d6SJérôme Duval
477cb395c4SStephan Aßmus	frame.OffsetTo(B_ORIGIN);
482c856023SKacper Kasper	fCalcView = new CalcView(Frame(), baseColor, settings);
49accec9a7SAlexandre Deckner
507cb395c4SStephan Aßmus	// create replicant dragger
511c0f89a7SStephan Aßmus	BRect replicantFrame(frame);
521c0f89a7SStephan Aßmus	replicantFrame.top = replicantFrame.bottom - 7.0f;
531c0f89a7SStephan Aßmus	replicantFrame.left = replicantFrame.right - 7.0f;
541c0f89a7SStephan Aßmus	BDragger* dragger = new BDragger(replicantFrame, fCalcView,
557cb395c4SStephan Aßmus		B_FOLLOW_RIGHT | B_FOLLOW_BOTTOM);
56accec9a7SAlexandre Deckner
577cb395c4SStephan Aßmus	// attach views
587cb395c4SStephan Aßmus	AddChild(fCalcView);
597cb395c4SStephan Aßmus	fCalcView->AddChild(dragger);
601f84dba7SStephan Aßmus
61bc3229d6SJérôme Duval	BRect rect;
62bc3229d6SJérôme Duval	if (settings->FindRect("window frame", &rect) == B_OK)
63bc3229d6SJérôme Duval		SetFrame(rect);
64bc3229d6SJérôme Duval	else
65bc3229d6SJérôme Duval		SetFrame(frame, true);
6660ba75c5SJohn Scipione
6760ba75c5SJohn Scipione	// Add shortcut keys to menu options
6860ba75c5SJohn Scipione	AddShortcut('0', B_COMMAND_KEY,
6960ba75c5SJohn Scipione		new BMessage(MSG_OPTIONS_KEYPAD_MODE_COMPACT));
7060ba75c5SJohn Scipione	AddShortcut('1', B_COMMAND_KEY,
7160ba75c5SJohn Scipione		new BMessage(MSG_OPTIONS_KEYPAD_MODE_BASIC));
7260ba75c5SJohn Scipione	AddShortcut('2', B_COMMAND_KEY,
7360ba75c5SJohn Scipione		new BMessage(MSG_OPTIONS_KEYPAD_MODE_SCIENTIFIC));
747cb395c4SStephan Aßmus}
757cb395c4SStephan Aßmus
767cb395c4SStephan Aßmus
777cb395c4SStephan AßmusCalcWindow::~CalcWindow()
787cb395c4SStephan Aßmus{
797cb395c4SStephan Aßmus}
807cb395c4SStephan Aßmus
817cb395c4SStephan Aßmus
8260ba75c5SJohn Scipionevoid
8363c55f02SJohn ScipioneCalcWindow::MessageReceived(BMessage* message)
8460ba75c5SJohn Scipione{
8563c55f02SJohn Scipione	switch (message->what) {
8660ba75c5SJohn Scipione		case MSG_OPTIONS_AUTO_NUM_LOCK:
8760ba75c5SJohn Scipione			fCalcView->ToggleAutoNumlock();
8860ba75c5SJohn Scipione			break;
8960ba75c5SJohn Scipione
9060ba75c5SJohn Scipione		case MSG_OPTIONS_AUDIO_FEEDBACK:
9160ba75c5SJohn Scipione			fCalcView->ToggleAudioFeedback();
9260ba75c5SJohn Scipione			break;
9360ba75c5SJohn Scipione
94fa37d94dSJohn Scipione		case MSG_OPTIONS_ANGLE_MODE_RADIAN:
95fa37d94dSJohn Scipione			fCalcView->SetDegreeMode(false);
96fa37d94dSJohn Scipione			return;
97fa37d94dSJohn Scipione
98fa37d94dSJohn Scipione		case MSG_OPTIONS_ANGLE_MODE_DEGREE:
99fa37d94dSJohn Scipione			fCalcView->SetDegreeMode(true);
100fa37d94dSJohn Scipione			return;
1018ffd0477SJohn Scipione
10260ba75c5SJohn Scipione		case MSG_OPTIONS_KEYPAD_MODE_COMPACT:
10360ba75c5SJohn Scipione			fCalcView->SetKeypadMode(KEYPAD_MODE_COMPACT);
10460ba75c5SJohn Scipione			break;
10560ba75c5SJohn Scipione
10660ba75c5SJohn Scipione		case MSG_OPTIONS_KEYPAD_MODE_BASIC:
10760ba75c5SJohn Scipione			fCalcView->SetKeypadMode(KEYPAD_MODE_BASIC);
10860ba75c5SJohn Scipione			break;
10960ba75c5SJohn Scipione
11060ba75c5SJohn Scipione		case MSG_OPTIONS_KEYPAD_MODE_SCIENTIFIC:
11160ba75c5SJohn Scipione			fCalcView->SetKeypadMode(KEYPAD_MODE_SCIENTIFIC);
11260ba75c5SJohn Scipione			break;
11360ba75c5SJohn Scipione
11460ba75c5SJohn Scipione		default:
11563c55f02SJohn Scipione			BWindow::MessageReceived(message);
11660ba75c5SJohn Scipione			break;
11760ba75c5SJohn Scipione	}
11860ba75c5SJohn Scipione}
11960ba75c5SJohn Scipione
12060ba75c5SJohn Scipione
1217cb395c4SStephan Aßmusvoid
1227cb395c4SStephan AßmusCalcWindow::Show()
1237cb395c4SStephan Aßmus{
1247cb395c4SStephan Aßmus	// NOTE: done here because the CalcView
1257cb395c4SStephan Aßmus	// turns on numlock if the options say so...
1267cb395c4SStephan Aßmus	// so we want to call MakeFocus() after
1277cb395c4SStephan Aßmus	// the options have been read for sure...
1287cb395c4SStephan Aßmus	fCalcView->MakeFocus();
1297cb395c4SStephan Aßmus	BWindow::Show();
1307cb395c4SStephan Aßmus}
1317cb395c4SStephan Aßmus
1327cb395c4SStephan Aßmus
1337cb395c4SStephan Aßmusbool
1347cb395c4SStephan AßmusCalcWindow::QuitRequested()
1357cb395c4SStephan Aßmus{
1367cb395c4SStephan Aßmus	be_app->PostMessage(B_QUIT_REQUESTED);
1377cb395c4SStephan Aßmus	Hide();
1387cb395c4SStephan Aßmus
1397cb395c4SStephan Aßmus	// NOTE: don't quit, since the app needs us
1407cb395c4SStephan Aßmus	// for saving settings yet...
1417cb395c4SStephan Aßmus	return false;
1427cb395c4SStephan Aßmus}
1437cb395c4SStephan Aßmus
1447cb395c4SStephan Aßmus
1457cb395c4SStephan Aßmusstatus_t
1467cb395c4SStephan AßmusCalcWindow::SaveSettings(BMessage* archive) const
1477cb395c4SStephan Aßmus{
1487cb395c4SStephan Aßmus	status_t ret = archive->AddRect("window frame", Frame());
1497cb395c4SStephan Aßmus	if (ret < B_OK)
1507cb395c4SStephan Aßmus		return ret;
1517cb395c4SStephan Aßmus
1527cb395c4SStephan Aßmus	return fCalcView->SaveSettings(archive);
1537cb395c4SStephan Aßmus}
1547cb395c4SStephan Aßmus
1557cb395c4SStephan Aßmus
1567cb395c4SStephan Aßmusvoid
1577cb395c4SStephan AßmusCalcWindow::SetFrame(BRect frame, bool forceCenter)
1587cb395c4SStephan Aßmus{
1597cb395c4SStephan Aßmus	// make sure window frame is on screen (center, if not)
1607cb395c4SStephan Aßmus	BScreen screen(this);
1617cb395c4SStephan Aßmus	BRect screenFrame = screen.Frame();
1627cb395c4SStephan Aßmus	if (forceCenter || !screenFrame.Contains(frame)) {
1637cb395c4SStephan Aßmus		float left = (screenFrame.Width() - frame.Width()) / 2.0;
1647cb395c4SStephan Aßmus		float top = (screenFrame.Height() - frame.Height()) / 2.0;
1657cb395c4SStephan Aßmus		left += screenFrame.left;
1667cb395c4SStephan Aßmus		top += screenFrame.top;
1677cb395c4SStephan Aßmus		frame.OffsetTo(left, top);
1687cb395c4SStephan Aßmus	}
1697cb395c4SStephan Aßmus
1707cb395c4SStephan Aßmus	MoveTo(frame.left, frame.top);
1717cb395c4SStephan Aßmus	ResizeTo(frame.Width(), frame.Height());
1727cb395c4SStephan Aßmus}
173