1 /* 2 Open Tracker License 3 4 Terms and Conditions 5 6 Copyright (c) 1991-2000, Be Incorporated. All rights reserved. 7 8 Permission is hereby granted, free of charge, to any person obtaining a copy of 9 this software and associated documentation files (the "Software"), to deal in 10 the Software without restriction, including without limitation the rights to 11 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 12 of the Software, and to permit persons to whom the Software is furnished to do 13 so, subject to the following conditions: 14 15 The above copyright notice and this permission notice applies to all licensees 16 and shall be included in all copies or substantial portions of the Software. 17 18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF TITLE, MERCHANTABILITY, 20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 21 BE INCORPORATED BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 22 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION 23 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 25 Except as contained in this notice, the name of Be Incorporated shall not be 26 used in advertising or otherwise to promote the sale, use or other dealings in 27 this Software without prior written authorization from Be Incorporated. 28 29 Tracker(TM), Be(R), BeOS(R), and BeIA(TM) are trademarks or registered trademarks 30 of Be Incorporated in the United States and other countries. Other brand product 31 names are registered trademarks or trademarks of their respective holders. 32 All rights reserved. 33 */ 34 35 // defines the status area drawn in the bottom left corner of a Tracker window 36 37 #include <Button.h> 38 #include <Screen.h> 39 40 #include "OverrideAlert.h" 41 42 OverrideAlert::OverrideAlert(const char *title, const char *text, 43 const char *button1, uint32 modifiers1, 44 const char *button2, uint32 modifiers2, 45 const char *button3, uint32 modifiers3, 46 button_width width, alert_type type) 47 : BAlert(title, text, button1, button2, button3, width, type), 48 fCurModifiers(0) 49 { 50 fButtonModifiers[0] = modifiers1; 51 fButtonModifiers[1] = modifiers2; 52 fButtonModifiers[2] = modifiers3; 53 UpdateButtons(modifiers(), true); 54 55 BPoint where = OverPosition(Frame().Width(), Frame().Height()); 56 MoveTo(where.x, where.y); 57 } 58 59 OverrideAlert::OverrideAlert(const char *title, const char *text, 60 const char *button1, uint32 modifiers1, 61 const char *button2, uint32 modifiers2, 62 const char *button3, uint32 modifiers3, 63 button_width width, button_spacing spacing, alert_type type) 64 : BAlert(title, text, button1, button2, button3, width, spacing, type), 65 fCurModifiers(0) 66 { 67 fButtonModifiers[0] = modifiers1; 68 fButtonModifiers[1] = modifiers2; 69 fButtonModifiers[2] = modifiers3; 70 UpdateButtons(modifiers(), true); 71 72 BPoint where = OverPosition(Frame().Width(), Frame().Height()); 73 MoveTo(where.x, where.y); 74 } 75 76 OverrideAlert::~OverrideAlert() 77 { 78 } 79 80 void 81 OverrideAlert::DispatchMessage(BMessage *message, BHandler *handler) 82 { 83 if (message->what == B_KEY_DOWN || message->what == B_KEY_UP 84 || message->what == B_UNMAPPED_KEY_DOWN 85 || message->what == B_UNMAPPED_KEY_UP) { 86 uint32 modifiers; 87 if (message->FindInt32("modifiers", (int32 *)&modifiers) == B_OK) 88 UpdateButtons(modifiers); 89 } 90 BAlert::DispatchMessage(message, handler); 91 } 92 93 BPoint 94 OverrideAlert::OverPosition(float width, float height) 95 { 96 // This positions the alert window like a normal alert, put 97 // places it on top of the calling window if possible. 98 99 BWindow *window = dynamic_cast<BWindow *>(BLooper::LooperForThread(find_thread(NULL))); 100 BRect screenFrame; 101 BRect desirableRect; 102 screenFrame = BScreen(window).Frame(); 103 104 if (window) { 105 // If we found a window associated with this calling thread, 106 // place alert over that window so that the first button is 107 // on top of it. This allows name editing confirmations to 108 // work with focus follows mouse -- when the alert goes away, 109 // the underlying window will still have focus. 110 111 desirableRect = window->Frame(); 112 float midX = (desirableRect.left + desirableRect.right) / 2.0f; 113 float midY = (desirableRect.top * 3.0f + desirableRect.bottom) / 4.0f; 114 115 desirableRect.left = midX - ceilf(width / 2.0f); 116 desirableRect.right = desirableRect.left+width; 117 desirableRect.top = midY - ceilf(height / 3.0f); 118 desirableRect.bottom = desirableRect.top + height; 119 120 } else { 121 // Otherwise, just place alert in center of screen. 122 123 desirableRect = screenFrame; 124 float midX = (desirableRect.left + desirableRect.right) / 2.0f; 125 float midY = (desirableRect.top * 3.0f + desirableRect.bottom) / 4.0f; 126 127 desirableRect.left = midX - ceilf(width / 2.0f); 128 desirableRect.right = desirableRect.left + width; 129 desirableRect.top = midY - ceilf(height / 3.0f); 130 desirableRect.bottom = desirableRect.top + height; 131 } 132 133 return desirableRect.LeftTop(); 134 } 135 136 void 137 OverrideAlert::UpdateButtons(uint32 modifiers, bool force) 138 { 139 if (modifiers == fCurModifiers && !force) 140 return; 141 142 fCurModifiers = modifiers; 143 for (int32 i = 0; i < 3; i++) { 144 BButton *button = ButtonAt(i); 145 if (button) 146 button->SetEnabled(((fButtonModifiers[i] & fCurModifiers) == fButtonModifiers[i])); 147 } 148 } 149