xref: /haiku/src/kits/tracker/OverrideAlert.cpp (revision 1026b0a1a76dc88927bb8175c470f638dc5464ee)
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 
38 #include <Button.h>
39 #include <Screen.h>
40 
41 #include "OverrideAlert.h"
42 
43 
44 OverrideAlert::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 
62 OverrideAlert::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 
80 OverrideAlert::~OverrideAlert()
81 {
82 }
83 
84 
85 void
86 OverrideAlert::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 
99 BPoint
100 OverrideAlert::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 
144 void
145 OverrideAlert::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