xref: /haiku/src/apps/cortex/TipManager/TipWindow.cpp (revision fce4895d1884da5ae6fb299d23c735c598e690b1)
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 // TipWindow.cpp
33 
34 #include "TipWindow.h"
35 #include "TipView.h"
36 
37 #include <Debug.h>
38 
39 __USE_CORTEX_NAMESPACE
40 
41 // -------------------------------------------------------- //
42 // *** dtor/ctors
43 // -------------------------------------------------------- //
44 
45 TipWindow::~TipWindow() {}
46 TipWindow::TipWindow(
47 	const char*							text) :
48 	BWindow(
49 		BRect(0,0,0,0),
50 		"TipWindow",
51 		B_NO_BORDER_WINDOW_LOOK,
52 		B_FLOATING_ALL_WINDOW_FEEL,
53 		B_NOT_MOVABLE|B_AVOID_FOCUS/*,
54 		B_ALL_WORKSPACES*/),
55 	// the TipView is created on demand
56 	m_tipView(0) {
57 
58 	if(text)
59 		m_text = text;
60 }
61 
62 
63 // -------------------------------------------------------- //
64 // *** operations (LOCK REQUIRED)
65 // -------------------------------------------------------- //
66 
67 const char* TipWindow::text() const {
68 	return m_text.Length() ?
69 		m_text.String() :
70 		0;
71 }
72 
73 void TipWindow::setText(
74 	const char*							text) {
75 
76 	if(!m_tipView)
77 		_createTipView();
78 
79 	m_text = text;
80 	m_tipView->setText(text);
81 
82 	// size to fit view
83 	float width, height;
84 	m_tipView->GetPreferredSize(&width, &height);
85 	m_tipView->ResizeTo(width, height);
86 	ResizeTo(width, height);
87 
88 	m_tipView->Invalidate();
89 }
90 
91 // -------------------------------------------------------- //
92 // *** hooks
93 // -------------------------------------------------------- //
94 
95 // override to substitute your own view class
96 TipView* TipWindow::createTipView() {
97 	return new TipView;
98 }
99 
100 // -------------------------------------------------------- //
101 // *** BWindow
102 // -------------------------------------------------------- //
103 
104 // initializes the tip view
105 void TipWindow::Show() {
106 
107 	// initialize the tip view if necessary
108 	if(!m_tipView)
109 		_createTipView();
110 
111 	_inherited::Show();
112 }
113 
114 // remove tip view? +++++
115 void TipWindow::Hide() {
116 	_inherited::Hide();
117 }
118 
119 
120 // hides the window when the user switches workspaces
121 // +++++ should it be restored when the user switches back?
122 void TipWindow::WorkspaceActivated(
123 	int32										workspace,
124 	bool										active) {
125 
126 	// don't confuse the user
127 	if(!IsHidden())
128 		Hide();
129 
130 	_inherited::WorkspaceActivated(workspace, active);
131 }
132 
133 // -------------------------------------------------------- //
134 // implementation
135 // -------------------------------------------------------- //
136 
137 void TipWindow::_createTipView() {
138 	if(m_tipView)
139 		_destroyTipView();
140 	m_tipView = createTipView();
141 	ASSERT(m_tipView);
142 
143 	AddChild(m_tipView);
144 
145 	if(m_text.Length())
146 		m_tipView->setText(m_text.String());
147 	else
148 		m_tipView->setText("(no info)");
149 }
150 
151 void TipWindow::_destroyTipView() {
152 	if(!m_tipView)
153 		return;
154 	RemoveChild(m_tipView);
155 	delete m_tipView;
156 	m_tipView = 0;
157 }
158 
159 // END -- TipWindow.cpp --
160