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 16 BToolTip::BToolTip() 17 { 18 _InitData(); 19 } 20 21 22 BToolTip::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 34 BToolTip::~BToolTip() 35 { 36 } 37 38 39 status_t 40 BToolTip::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 52 void 53 BToolTip::SetSticky(bool enable) 54 { 55 fIsSticky = enable; 56 } 57 58 59 bool 60 BToolTip::IsSticky() const 61 { 62 return fIsSticky; 63 } 64 65 66 void 67 BToolTip::SetMouseRelativeLocation(BPoint location) 68 { 69 fRelativeLocation = location; 70 } 71 72 73 BPoint 74 BToolTip::MouseRelativeLocation() const 75 { 76 return fRelativeLocation; 77 } 78 79 80 void 81 BToolTip::SetAlignment(BAlignment alignment) 82 { 83 fAlignment = alignment; 84 } 85 86 87 BAlignment 88 BToolTip::Alignment() const 89 { 90 return fAlignment; 91 } 92 93 94 void 95 BToolTip::AttachedToWindow() 96 { 97 } 98 99 100 void 101 BToolTip::DetachedFromWindow() 102 { 103 } 104 105 106 bool 107 BToolTip::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 129 void 130 BToolTip::Unlock() 131 { 132 if (fLockedLooper) 133 View()->UnlockLooper(); 134 else 135 BToolTipManager::Manager()->Unlock(); 136 } 137 138 139 void 140 BToolTip::_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 151 BTextToolTip::BTextToolTip(const char* text) 152 { 153 _InitData(text); 154 } 155 156 157 BTextToolTip::BTextToolTip(BMessage* archive) 158 { 159 // TODO! 160 } 161 162 163 BTextToolTip::~BTextToolTip() 164 { 165 delete fTextView; 166 } 167 168 169 /*static*/ BTextToolTip* 170 BTextToolTip::Instantiate(BMessage* archive) 171 { 172 if (!validate_instantiation(archive, "BTextToolTip")) 173 return NULL; 174 175 return new(std::nothrow) BTextToolTip(archive); 176 } 177 178 179 status_t 180 BTextToolTip::Archive(BMessage* archive, bool deep) const 181 { 182 status_t status = BToolTip::Archive(archive, deep); 183 // TODO! 184 185 return status; 186 } 187 188 189 BView* 190 BTextToolTip::View() const 191 { 192 return fTextView; 193 } 194 195 196 const char* 197 BTextToolTip::Text() const 198 { 199 return fTextView->Text(); 200 } 201 202 203 void 204 BTextToolTip::SetText(const char* text) 205 { 206 if (!Lock()) 207 return; 208 209 fTextView->SetText(text); 210 fTextView->InvalidateLayout(); 211 212 Unlock(); 213 } 214 215 216 void 217 BTextToolTip::_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