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