xref: /haiku/src/apps/cortex/DormantNodeView/DormantNodeWindow.cpp (revision 17889a8c70dbb3d59c1412f6431968753c767bab)
1 /*
2  * Copyright (c) 1999-2000, Eric Moon.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions, and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions, and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
23  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
27  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 
32 // DormantNodeWindow.cpp
33 // e.moon 2jun99
34 
35 #include "DormantNodeWindow.h"
36 // DormantNodeView
37 #include "DormantNodeView.h"
38 
39 #include "RouteWindow.h"
40 
41 // Application Kit
42 #include <Application.h>
43 // Locale Kit
44 #undef B_CATALOG
45 #define B_CATALOG (&sCatalog)
46 #include <Catalog.h>
47 // Interface Kit
48 #include <Screen.h>
49 #include <ScrollBar.h>
50 
51 #undef B_TRANSLATION_CONTEXT
52 #define B_TRANSLATION_CONTEXT "CortexDormantNodeWindow"
53 
54 __USE_CORTEX_NAMESPACE
55 
56 #include <Debug.h>
57 #define D_ALLOC(x)	//PRINT (x)		// ctor/dtor
58 #define D_HOOK(x) //PRINT (x)		// BWindow impl.
59 #define D_MESSAGE(x) //PRINT (x)	// MessageReceived()
60 #define D_INTERNAL(x) //PRINT (x)	// internal operations
61 
62 static BCatalog sCatalog("x-vnd.Cortex.DormantNodeView");
63 
64 // -------------------------------------------------------- //
65 // constants
66 // -------------------------------------------------------- //
67 
68 // this should be a bit more sophisticated :)
69 const BRect DormantNodeWindow::s_initFrame(500.0, 350.0, 640.0, 480.0);
70 
71 // -------------------------------------------------------- //
72 // ctor/dtor
73 // -------------------------------------------------------- //
74 
75 DormantNodeWindow::DormantNodeWindow(
76 	BWindow* parent)
77 	: BWindow(s_initFrame,
78 			  B_TRANSLATE("Media add-ons"),
79 			  B_FLOATING_WINDOW_LOOK,
80 			  B_FLOATING_SUBSET_WINDOW_FEEL,
81 			  B_WILL_ACCEPT_FIRST_CLICK|B_AVOID_FOCUS|B_ASYNCHRONOUS_CONTROLS),
82 	  m_parent(parent),
83 	  m_zoomed(false),
84 	  m_zooming(false) {
85 	D_ALLOC(("DormantNodeWindow::DormantNodeWindow()\n"));
86 
87 	ASSERT(m_parent);
88 	AddToSubset(m_parent);
89 
90 	// Create the ListView
91 	BRect r = Bounds();
92 	r.right -= B_V_SCROLL_BAR_WIDTH;
93 	m_listView = new DormantNodeView(r, "Dormant Node ListView", B_FOLLOW_ALL_SIDES);
94 
95 	// Add the vertical ScrollBar
96 	r.left = r.right + 1.0;
97 	r.right = r.left + B_V_SCROLL_BAR_WIDTH;
98 	r.InsetBy(0.0, -1.0);
99 	AddChild(new BScrollBar(r, "", m_listView, 0.0, 0.0, B_VERTICAL));
100 
101 	// Add the ListView
102 	AddChild(m_listView);
103 	_constrainToScreen();
104 }
105 
106 DormantNodeWindow::~DormantNodeWindow() {
107 	D_ALLOC(("DormantNodeWindow::~DormantNodeWindow()\n"));
108 
109 }
110 
111 // -------------------------------------------------------- //
112 // BWindow impl.
113 // -------------------------------------------------------- //
114 
115 bool DormantNodeWindow::QuitRequested() {
116 	D_HOOK(("DormantNodeWindow::QuitRequested()\n"));
117 
118 	// [e.moon 29nov99] the RouteWindow is now responsible for
119 	// closing me
120 	m_parent->PostMessage(RouteWindow::M_TOGGLE_DORMANT_NODE_WINDOW);
121 	return false;
122 }
123 
124 void DormantNodeWindow::Zoom(
125 	BPoint origin,
126 	float width,
127 	float height) {
128 	D_HOOK(("DormantNodeWindow::Zoom()\n"));
129 
130 	m_zooming = true;
131 
132 	BScreen screen(this);
133 	if (!screen.Frame().Contains(Frame())) {
134 		m_zoomed = false;
135 	}
136 
137 	if (!m_zoomed) {
138 		// resize to the ideal size
139 		m_manualSize = Bounds();
140 		m_listView->GetPreferredSize(&width, &height);
141 		ResizeTo(width + B_V_SCROLL_BAR_WIDTH, height);
142 		m_zoomed = true;
143 		_constrainToScreen();
144 	}
145 	else {
146 		// resize to the most recent manual size
147 		ResizeTo(m_manualSize.Width(), m_manualSize.Height());
148 		m_zoomed = false;
149 	}
150 }
151 
152 // -------------------------------------------------------- //
153 // internal operations
154 // -------------------------------------------------------- //
155 
156 void DormantNodeWindow::_constrainToScreen() {
157 	D_INTERNAL(("DormantNodeWindow::_constrainToScreen()\n"));
158 
159 	BScreen screen(this);
160 	BRect screenRect = screen.Frame();
161 	BRect windowRect = Frame();
162 
163 	// if the window is outside the screen rect
164 	// move it to the default position
165 	if (!screenRect.Intersects(windowRect)) {
166 		windowRect.OffsetTo(screenRect.LeftTop());
167 		MoveTo(windowRect.LeftTop());
168 		windowRect = Frame();
169 	}
170 
171 	// if the window is larger than the screen rect
172 	// resize it to fit at each side
173 	if (!screenRect.Contains(windowRect)) {
174 		if (windowRect.left < screenRect.left) {
175 			windowRect.left = screenRect.left + 5.0;
176 			MoveTo(windowRect.LeftTop());
177 			windowRect = Frame();
178 		}
179 		if (windowRect.top < screenRect.top) {
180 			windowRect.top = screenRect.top + 5.0;
181 			MoveTo(windowRect.LeftTop());
182 			windowRect = Frame();
183 		}
184 		if (windowRect.right > screenRect.right) {
185 			windowRect.right = screenRect.right - 5.0;
186 		}
187 		if (windowRect.bottom > screenRect.bottom) {
188 			windowRect.bottom = screenRect.bottom - 5.0;
189 		}
190 		ResizeTo(windowRect.Width(), windowRect.Height());
191 	}
192 }
193 
194 // END -- DormantNodeWindow.cpp --
195