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::Lock(); 114 115 if (View()->Window() != NULL) { 116 BToolTipManager::Unlock(); 117 continue; 118 } 119 } 120 break; 121 } 122 123 fLockedLooper = lockedLooper; 124 return true; 125 } 126 127 128 void 129 BToolTip::Unlock() 130 { 131 if (fLockedLooper) 132 View()->UnlockLooper(); 133 else 134 BToolTipManager::Unlock(); 135 } 136 137 138 void 139 BToolTip::_InitData() 140 { 141 fIsSticky = false; 142 fRelativeLocation = BPoint(20, 20); 143 fAlignment = BAlignment(B_ALIGN_RIGHT, B_ALIGN_BOTTOM); 144 } 145 146 147 // #pragma mark - 148 149 150 BTextToolTip::BTextToolTip(const char* text) 151 { 152 _InitData(text); 153 } 154 155 156 BTextToolTip::BTextToolTip(BMessage* archive) 157 { 158 // TODO! 159 } 160 161 162 BTextToolTip::~BTextToolTip() 163 { 164 delete fTextView; 165 } 166 167 168 /*static*/ BTextToolTip* 169 BTextToolTip::Instantiate(BMessage* archive) 170 { 171 if (!validate_instantiation(archive, "BTextToolTip")) 172 return NULL; 173 174 return new(std::nothrow) BTextToolTip(archive); 175 } 176 177 178 status_t 179 BTextToolTip::Archive(BMessage* archive, bool deep) const 180 { 181 status_t status = BToolTip::Archive(archive, deep); 182 // TODO! 183 184 return status; 185 } 186 187 188 BView* 189 BTextToolTip::View() const 190 { 191 return fTextView; 192 } 193 194 195 const char* 196 BTextToolTip::Text() const 197 { 198 return fTextView->Text(); 199 } 200 201 202 void 203 BTextToolTip::SetText(const char* text) 204 { 205 if (!Lock()) 206 return; 207 208 fTextView->SetText(text); 209 210 Unlock(); 211 } 212 213 214 void 215 BTextToolTip::_InitData(const char* text) 216 { 217 fTextView = new BTextView("tool tip text"); 218 fTextView->SetText(text); 219 fTextView->MakeEditable(false); 220 fTextView->SetViewColor(ui_color(B_TOOL_TIP_BACKGROUND_COLOR)); 221 rgb_color color = ui_color(B_TOOL_TIP_TEXT_COLOR); 222 fTextView->SetFontAndColor(NULL, 0, &color); 223 fTextView->SetWordWrap(false); 224 } 225 226