1/* 2 * Copyright 2009, Axel Dörfler, axeld@pinc-software.de. 3 * Distributed under the terms of the MIT License. 4 */ 5 6 7#include <ToolTip.h> 8 9#include <new> 10 11#include <Message.h> 12#include <TextView.h> 13#include <ToolTipManager.h> 14 15 16BToolTip::BToolTip() 17{ 18 _InitData(); 19} 20 21 22BToolTip::BToolTip(BMessage* archive) 23{ 24 _InitData(); 25 26 bool sticky; 27 if (archive->FindBool("sticky", &sticky) == B_OK) 28 fIsSticky = sticky; 29 30 // TODO! 31} 32 33 34BToolTip::~BToolTip() 35{ 36} 37 38 39status_t 40BToolTip::Archive(BMessage* archive, bool deep) const 41{ 42 status_t status = BArchivable::Archive(archive, deep); 43 44 if (fIsSticky) 45 status = archive->AddBool("sticky", fIsSticky); 46 47 // TODO! 48 return status; 49} 50 51 52void 53BToolTip::SetSticky(bool enable) 54{ 55 fIsSticky = enable; 56} 57 58 59bool 60BToolTip::IsSticky() const 61{ 62 return fIsSticky; 63} 64 65 66void 67BToolTip::SetMouseRelativeLocation(BPoint location) 68{ 69 fRelativeLocation = location; 70} 71 72 73BPoint 74BToolTip::MouseRelativeLocation() const 75{ 76 return fRelativeLocation; 77} 78 79 80void 81BToolTip::SetAlignment(BAlignment alignment) 82{ 83 fAlignment = alignment; 84} 85 86 87BAlignment 88BToolTip::Alignment() const 89{ 90 return fAlignment; 91} 92 93 94void 95BToolTip::AttachedToWindow() 96{ 97} 98 99 100void 101BToolTip::DetachedFromWindow() 102{ 103} 104 105 106bool 107BToolTip::Lock() 108{ 109 bool lockedLooper; 110 while (true) { 111 lockedLooper = View()->LockLooper(); 112 if (!lockedLooper) { 113 BToolTipManager* manager = BToolTipManager::Manager(); 114 manager->Lock(); 115 116 if (View()->Window() != NULL) { 117 manager->Unlock(); 118 continue; 119 } 120 } 121 break; 122 } 123 124 fLockedLooper = lockedLooper; 125 return true; 126} 127 128 129void 130BToolTip::Unlock() 131{ 132 if (fLockedLooper) 133 View()->UnlockLooper(); 134 else 135 BToolTipManager::Manager()->Unlock(); 136} 137 138 139void 140BToolTip::_InitData() 141{ 142 fIsSticky = false; 143 fRelativeLocation = BPoint(20, 20); 144 fAlignment = BAlignment(B_ALIGN_RIGHT, B_ALIGN_BOTTOM); 145} 146 147 148// #pragma mark - 149 150 151BTextToolTip::BTextToolTip(const char* text) 152{ 153 _InitData(text); 154} 155 156 157BTextToolTip::BTextToolTip(BMessage* archive) 158{ 159 // TODO! 160} 161 162 163BTextToolTip::~BTextToolTip() 164{ 165 delete fTextView; 166} 167 168 169/*static*/ BTextToolTip* 170BTextToolTip::Instantiate(BMessage* archive) 171{ 172 if (!validate_instantiation(archive, "BTextToolTip")) 173 return NULL; 174 175 return new(std::nothrow) BTextToolTip(archive); 176} 177 178 179status_t 180BTextToolTip::Archive(BMessage* archive, bool deep) const 181{ 182 status_t status = BToolTip::Archive(archive, deep); 183 // TODO! 184 185 return status; 186} 187 188 189BView* 190BTextToolTip::View() const 191{ 192 return fTextView; 193} 194 195 196const char* 197BTextToolTip::Text() const 198{ 199 return fTextView->Text(); 200} 201 202 203void 204BTextToolTip::SetText(const char* text) 205{ 206 if (!Lock()) 207 return; 208 209 fTextView->SetText(text); 210 fTextView->InvalidateLayout(); 211 212 Unlock(); 213} 214 215 216void 217BTextToolTip::_InitData(const char* text) 218{ 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); 226} 227 228