vfs: check userland buffers before reading them.
[haiku.git] / src / kits / interface / ToolTip.cpp
blobf5a4873f1bd148dda5857e7597f579133bfc9b5a
1 /*
2 * Copyright 2009, Axel Dörfler, axeld@pinc-software.de.
3 * Distributed under the terms of the MIT License.
4 */
7 #include <ToolTip.h>
9 #include <new>
11 #include <Message.h>
12 #include <TextView.h>
13 #include <ToolTipManager.h>
16 BToolTip::BToolTip()
18 _InitData();
22 BToolTip::BToolTip(BMessage* archive)
24 _InitData();
26 bool sticky;
27 if (archive->FindBool("sticky", &sticky) == B_OK)
28 fIsSticky = sticky;
30 // TODO!
34 BToolTip::~BToolTip()
39 status_t
40 BToolTip::Archive(BMessage* archive, bool deep) const
42 status_t status = BArchivable::Archive(archive, deep);
44 if (fIsSticky)
45 status = archive->AddBool("sticky", fIsSticky);
47 // TODO!
48 return status;
52 void
53 BToolTip::SetSticky(bool enable)
55 fIsSticky = enable;
59 bool
60 BToolTip::IsSticky() const
62 return fIsSticky;
66 void
67 BToolTip::SetMouseRelativeLocation(BPoint location)
69 fRelativeLocation = location;
73 BPoint
74 BToolTip::MouseRelativeLocation() const
76 return fRelativeLocation;
80 void
81 BToolTip::SetAlignment(BAlignment alignment)
83 fAlignment = alignment;
87 BAlignment
88 BToolTip::Alignment() const
90 return fAlignment;
94 void
95 BToolTip::AttachedToWindow()
100 void
101 BToolTip::DetachedFromWindow()
106 bool
107 BToolTip::Lock()
109 bool lockedLooper;
110 while (true) {
111 lockedLooper = View()->LockLooper();
112 if (!lockedLooper) {
113 BToolTipManager* manager = BToolTipManager::Manager();
114 manager->Lock();
116 if (View()->Window() != NULL) {
117 manager->Unlock();
118 continue;
121 break;
124 fLockedLooper = lockedLooper;
125 return true;
129 void
130 BToolTip::Unlock()
132 if (fLockedLooper)
133 View()->UnlockLooper();
134 else
135 BToolTipManager::Manager()->Unlock();
139 void
140 BToolTip::_InitData()
142 fIsSticky = false;
143 fRelativeLocation = BPoint(20, 20);
144 fAlignment = BAlignment(B_ALIGN_RIGHT, B_ALIGN_BOTTOM);
148 // #pragma mark -
151 BTextToolTip::BTextToolTip(const char* text)
153 _InitData(text);
157 BTextToolTip::BTextToolTip(BMessage* archive)
159 // TODO!
163 BTextToolTip::~BTextToolTip()
165 delete fTextView;
169 /*static*/ BTextToolTip*
170 BTextToolTip::Instantiate(BMessage* archive)
172 if (!validate_instantiation(archive, "BTextToolTip"))
173 return NULL;
175 return new(std::nothrow) BTextToolTip(archive);
179 status_t
180 BTextToolTip::Archive(BMessage* archive, bool deep) const
182 status_t status = BToolTip::Archive(archive, deep);
183 // TODO!
185 return status;
189 BView*
190 BTextToolTip::View() const
192 return fTextView;
196 const char*
197 BTextToolTip::Text() const
199 return fTextView->Text();
203 void
204 BTextToolTip::SetText(const char* text)
206 if (!Lock())
207 return;
209 fTextView->SetText(text);
210 fTextView->InvalidateLayout();
212 Unlock();
216 void
217 BTextToolTip::_InitData(const char* text)
219 fTextView = new BTextView("tool tip text");
220 fTextView->SetText(text);
221 fTextView->MakeEditable(false);
222 fTextView->SetViewUIColor(B_TOOL_TIP_BACKGROUND_COLOR);
223 rgb_color color = ui_color(B_TOOL_TIP_TEXT_COLOR);
224 fTextView->SetFontAndColor(NULL, 0, &color);
225 fTextView->SetWordWrap(false);