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