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