1 /* 2 * Copyright 2006-2012 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 * Stephan Aßmus, superstippi@gmx.de 8 * John Scipione, jscipione@gmail.com 9 * Timothy Wayper, timmy@wunderbear.com 10 */ 11 12 13 #include "CalcWindow.h" 14 15 #include <stdlib.h> 16 #include <stdio.h> 17 #include <assert.h> 18 19 #include <Application.h> 20 #include <Catalog.h> 21 #include <Dragger.h> 22 #include <Screen.h> 23 24 #include "CalcApplication.h" 25 #include "CalcOptions.h" 26 #include "CalcView.h" 27 28 29 #undef B_TRANSLATION_CONTEXT 30 #define B_TRANSLATION_CONTEXT "Window" 31 32 33 CalcWindow::CalcWindow(BRect frame, BMessage* settings) 34 : 35 BWindow(frame, kAppName, B_TITLED_WINDOW, B_ASYNCHRONOUS_CONTROLS 36 | B_NOT_ANCHORED_ON_ACTIVATE) 37 { 38 // create calculator view with calculator description and 39 // desktop background color 40 BScreen screen(this); 41 rgb_color baseColor = screen.DesktopColor(); 42 43 // Size Limits are defined in CalcView.h 44 SetSizeLimits(kMinimumWidthBasic, kMaximumWidthBasic, 45 kMinimumHeightBasic, kMaximumHeightBasic); 46 47 frame.OffsetTo(B_ORIGIN); 48 fCalcView = new CalcView(Frame(), baseColor, settings); 49 50 // create replicant dragger 51 BRect replicantFrame(frame); 52 replicantFrame.top = replicantFrame.bottom - 7.0f; 53 replicantFrame.left = replicantFrame.right - 7.0f; 54 BDragger* dragger = new BDragger(replicantFrame, fCalcView, 55 B_FOLLOW_RIGHT | B_FOLLOW_BOTTOM); 56 57 // attach views 58 AddChild(fCalcView); 59 fCalcView->AddChild(dragger); 60 61 BRect rect; 62 if (settings->FindRect("window frame", &rect) == B_OK) { 63 SetFrame(rect); 64 MoveOnScreen(B_MOVE_IF_PARTIALLY_OFFSCREEN); 65 } else 66 SetFrame(frame, true); 67 68 // Add shortcut keys to menu options 69 AddShortcut('0', B_COMMAND_KEY, 70 new BMessage(MSG_OPTIONS_KEYPAD_MODE_COMPACT)); 71 AddShortcut('1', B_COMMAND_KEY, 72 new BMessage(MSG_OPTIONS_KEYPAD_MODE_BASIC)); 73 AddShortcut('2', B_COMMAND_KEY, 74 new BMessage(MSG_OPTIONS_KEYPAD_MODE_SCIENTIFIC)); 75 } 76 77 78 CalcWindow::~CalcWindow() 79 { 80 } 81 82 83 void 84 CalcWindow::MessageReceived(BMessage* message) 85 { 86 switch (message->what) { 87 case MSG_OPTIONS_AUTO_NUM_LOCK: 88 fCalcView->ToggleAutoNumlock(); 89 break; 90 91 case MSG_OPTIONS_ANGLE_MODE_RADIAN: 92 fCalcView->SetDegreeMode(false); 93 return; 94 95 case MSG_OPTIONS_ANGLE_MODE_DEGREE: 96 fCalcView->SetDegreeMode(true); 97 return; 98 99 case MSG_OPTIONS_KEYPAD_MODE_COMPACT: 100 fCalcView->SetKeypadMode(KEYPAD_MODE_COMPACT); 101 break; 102 103 case MSG_OPTIONS_KEYPAD_MODE_BASIC: 104 fCalcView->SetKeypadMode(KEYPAD_MODE_BASIC); 105 break; 106 107 case MSG_OPTIONS_KEYPAD_MODE_SCIENTIFIC: 108 fCalcView->SetKeypadMode(KEYPAD_MODE_SCIENTIFIC); 109 break; 110 111 default: 112 BWindow::MessageReceived(message); 113 break; 114 } 115 } 116 117 118 void 119 CalcWindow::Show() 120 { 121 // NOTE: done here because the CalcView 122 // turns on numlock if the options say so... 123 // so we want to call MakeFocus() after 124 // the options have been read for sure... 125 fCalcView->MakeFocus(); 126 BWindow::Show(); 127 } 128 129 130 bool 131 CalcWindow::QuitRequested() 132 { 133 be_app->PostMessage(B_QUIT_REQUESTED); 134 Hide(); 135 136 // NOTE: don't quit, since the app needs us 137 // for saving settings yet... 138 return false; 139 } 140 141 142 status_t 143 CalcWindow::SaveSettings(BMessage* archive) const 144 { 145 status_t ret = archive->AddRect("window frame", Frame()); 146 if (ret < B_OK) 147 return ret; 148 149 return fCalcView->SaveSettings(archive); 150 } 151 152 153 void 154 CalcWindow::SetFrame(BRect frame, bool forceCenter) 155 { 156 // make sure window frame is on screen (center, if not) 157 BScreen screen(this); 158 BRect screenFrame = screen.Frame(); 159 if (forceCenter || !screenFrame.Contains(frame)) { 160 float left = (screenFrame.Width() - frame.Width()) / 2.0; 161 float top = (screenFrame.Height() - frame.Height()) / 2.0; 162 left += screenFrame.left; 163 top += screenFrame.top; 164 frame.OffsetTo(left, top); 165 } 166 167 MoveTo(frame.left, frame.top); 168 ResizeTo(frame.Width(), frame.Height()); 169 } 170