xref: /haiku/src/kits/tracker/OverrideAlert.cpp (revision d06cbe081b7ea043aea2012359744091de6d604d)
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 	:
50 	BAlert(title, text, button1, button2, button3, width, type),
51 	fCurModifiers(0)
52 {
53 	fButtonModifiers[0] = modifiers1;
54 	fButtonModifiers[1] = modifiers2;
55 	fButtonModifiers[2] = modifiers3;
56 	UpdateButtons(modifiers(), true);
57 
58 	BPoint where = OverPosition(Frame().Width(), Frame().Height());
59 	MoveTo(where.x, where.y);
60 }
61 
62 
63 OverrideAlert::OverrideAlert(const char* title, const char* text,
64 	const char* button1, uint32 modifiers1,
65 	const char* button2, uint32 modifiers2,
66 	const char* button3, uint32 modifiers3,
67 	button_width width, button_spacing spacing, alert_type type)
68 	:
69 	BAlert(title, text, button1, button2, button3, width, spacing, type),
70 	fCurModifiers(0)
71 {
72 	fButtonModifiers[0] = modifiers1;
73 	fButtonModifiers[1] = modifiers2;
74 	fButtonModifiers[2] = modifiers3;
75 	UpdateButtons(modifiers(), true);
76 
77 	BPoint where = OverPosition(Frame().Width(), Frame().Height());
78 	MoveTo(where.x, where.y);
79 }
80 
81 
82 OverrideAlert::~OverrideAlert()
83 {
84 }
85 
86 
87 void
88 OverrideAlert::DispatchMessage(BMessage* message, BHandler* handler)
89 {
90 	if (message->what == B_KEY_DOWN || message->what == B_KEY_UP
91 		|| message->what == B_UNMAPPED_KEY_DOWN
92 		|| message->what == B_UNMAPPED_KEY_UP) {
93 		uint32 modifiers;
94 		if (message->FindInt32("modifiers", (int32*)&modifiers) == B_OK)
95 			UpdateButtons(modifiers);
96 	}
97 	BAlert::DispatchMessage(message, handler);
98 }
99 
100 
101 BPoint
102 OverrideAlert::OverPosition(float width, float height)
103 {
104 	// This positions the alert window like a normal alert, put
105 	// places it on top of the calling window if possible.
106 
107 	BWindow* window
108 		= dynamic_cast<BWindow*>(BLooper::LooperForThread(find_thread(NULL)));
109 	BRect screenFrame;
110 	BRect desirableRect;
111 	screenFrame = BScreen(window).Frame();
112 
113 	if (window != NULL) {
114 		// If we found a window associated with this calling thread,
115 		// place alert over that window so that the first button is
116 		// on top of it.  This allows name editing confirmations to
117 		// work with focus follows mouse -- when the alert goes away,
118 		// the underlying window will still have focus.
119 
120 		desirableRect = window->Frame();
121 		float midX = (desirableRect.left + desirableRect.right) / 2.0f;
122 		float midY = (desirableRect.top * 3.0f + desirableRect.bottom) / 4.0f;
123 
124 		desirableRect.left = midX - ceilf(width / 2.0f);
125 		desirableRect.right = desirableRect.left+width;
126 		desirableRect.top = midY - ceilf(height / 3.0f);
127 		desirableRect.bottom = desirableRect.top + height;
128 	} else {
129 		// Otherwise, just place alert in center of screen.
130 
131 		desirableRect = screenFrame;
132 		float midX = (desirableRect.left + desirableRect.right) / 2.0f;
133 		float midY = (desirableRect.top * 3.0f + desirableRect.bottom) / 4.0f;
134 
135 		desirableRect.left = midX - ceilf(width / 2.0f);
136 		desirableRect.right = desirableRect.left + width;
137 		desirableRect.top = midY - ceilf(height / 3.0f);
138 		desirableRect.bottom = desirableRect.top + height;
139 	}
140 
141 	return desirableRect.LeftTop();
142 }
143 
144 
145 void
146 OverrideAlert::UpdateButtons(uint32 modifiers, bool force)
147 {
148 	if (modifiers == fCurModifiers && !force)
149 		return;
150 
151 	fCurModifiers = modifiers;
152 	for (int32 i = 0; i < 3; i++) {
153 		BButton* button = ButtonAt(i);
154 		if (button != NULL) {
155 			button->SetEnabled(((fButtonModifiers[i] & fCurModifiers)
156 				== fButtonModifiers[i]));
157 		}
158 	}
159 }
160