1 //------------------------------------------------------------------------------ 2 // Copyright (c) 2001-2002, OpenBeOS 3 // 4 // Permission is hereby granted, free of charge, to any person obtaining a 5 // copy of this software and associated documentation files (the "Software"), 6 // to deal in the Software without restriction, including without limitation 7 // the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 // and/or sell copies of the Software, and to permit persons to whom the 9 // Software is furnished to do so, subject to the following conditions: 10 // 11 // The above copyright notice and this permission notice shall be included in 12 // all copies or substantial portions of the Software. 13 // 14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20 // DEALINGS IN THE SOFTWARE. 21 // 22 // File Name: StringView.cpp 23 // Author: Frans van Nispen (xlr8@tref.nl) 24 // Description: BStringView draw a non-editable text string 25 //------------------------------------------------------------------------------ 26 27 // Standard Includes ----------------------------------------------------------- 28 #include <stdio.h> 29 #include <stdlib.h> 30 #include <string.h> 31 32 // System Includes ------------------------------------------------------------- 33 #include <Message.h> 34 #include <StringView.h> 35 #include <View.h> 36 #include <Window.h> 37 38 // Project Includes ------------------------------------------------------------ 39 40 // Local Includes -------------------------------------------------------------- 41 42 // Local Defines --------------------------------------------------------------- 43 44 // Globals --------------------------------------------------------------------- 45 46 47 //------------------------------------------------------------------------------ 48 BStringView::BStringView(BRect frame, const char* name, const char* text, 49 uint32 resizeMask, uint32 flags) 50 : BView(frame, name, resizeMask, flags) 51 { 52 fText = strdup(text); 53 fAlign = B_ALIGN_LEFT; 54 } 55 //------------------------------------------------------------------------------ 56 BStringView::BStringView(BMessage* data) 57 : BView(data) 58 { 59 const char* text; 60 61 if (data->FindInt32("_aligne",(int32&)fAlign) != B_OK) { 62 fAlign = B_ALIGN_LEFT; 63 } 64 65 if (data->FindString("_text",&text) != B_OK) { 66 text = NULL; 67 } 68 69 SetText(text); 70 } 71 //------------------------------------------------------------------------------ 72 BArchivable* BStringView::Instantiate(BMessage* data) 73 { 74 if (!validate_instantiation(data,"BStringView")) { 75 return NULL; 76 } 77 78 return new BStringView(data); 79 } 80 //------------------------------------------------------------------------------ 81 status_t BStringView::Archive(BMessage* data, bool deep) const 82 { 83 BView::Archive(data, deep); 84 85 if (fText) { 86 data->AddString("_text",fText); 87 } 88 89 data->AddInt32("_align", fAlign); 90 91 return B_OK; 92 } 93 //------------------------------------------------------------------------------ 94 BStringView::~BStringView() 95 { 96 free(fText); 97 } 98 //------------------------------------------------------------------------------ 99 void BStringView::SetText(const char* text) 100 { 101 free(fText); 102 fText = strdup(text); 103 Invalidate(); 104 } 105 //------------------------------------------------------------------------------ 106 const char* BStringView::Text() const 107 { 108 return fText; 109 } 110 //------------------------------------------------------------------------------ 111 void BStringView::SetAlignment(alignment flag) 112 { 113 fAlign = flag; 114 Invalidate(); 115 } 116 //------------------------------------------------------------------------------ 117 alignment BStringView::Alignment() const 118 { 119 return fAlign; 120 } 121 //------------------------------------------------------------------------------ 122 void BStringView::AttachedToWindow() 123 { 124 if (Parent()) { 125 SetViewColor(Parent()->ViewColor()); 126 } 127 } 128 //------------------------------------------------------------------------------ 129 void BStringView::Draw(BRect bounds) 130 { 131 SetLowColor(ViewColor()); 132 BFont font; 133 GetFont(&font); 134 font_height fh; 135 font.GetHeight(&fh); 136 137 float y = Bounds().bottom - ceil(fh.descent); 138 float x; 139 switch (fAlign) 140 { 141 case B_ALIGN_RIGHT: 142 x = Bounds().Width() - font.StringWidth(fText) - 2.0f; 143 break; 144 145 case B_ALIGN_CENTER: 146 x = (Bounds().Width() - font.StringWidth(fText))/2.0f; 147 break; 148 149 default: 150 x = 2.0f; 151 break; 152 } 153 154 DrawString( fText, BPoint(x,y) ); 155 } 156 //------------------------------------------------------------------------------ 157 void BStringView::ResizeToPreferred() 158 { 159 float w, h; 160 GetPreferredSize(&w, &h); 161 BView::ResizeTo(w, h); 162 } 163 //------------------------------------------------------------------------------ 164 void BStringView::GetPreferredSize(float* width, float* height) 165 { 166 BFont font; 167 GetFont(&font); 168 font_height fh; 169 font.GetHeight(&fh); 170 171 *height = ceil(fh.ascent + fh.descent + fh.leading) + 2.0f; 172 *width = 4.0f + ceil(font.StringWidth(fText)); 173 } 174 //------------------------------------------------------------------------------ 175 void BStringView::MessageReceived(BMessage* msg) 176 { 177 BView::MessageReceived(msg); 178 } 179 //------------------------------------------------------------------------------ 180 void BStringView::MouseDown(BPoint pt) 181 { 182 BView::MouseDown(pt); 183 } 184 //------------------------------------------------------------------------------ 185 void BStringView::MouseUp(BPoint pt) 186 { 187 BView::MouseUp(pt); 188 } 189 //------------------------------------------------------------------------------ 190 void BStringView::MouseMoved(BPoint pt, uint32 code, const BMessage* msg) 191 { 192 BView::MouseMoved(pt, code, msg); 193 } 194 //------------------------------------------------------------------------------ 195 void BStringView::DetachedFromWindow() 196 { 197 BView::DetachedFromWindow(); 198 } 199 //------------------------------------------------------------------------------ 200 void BStringView::FrameMoved(BPoint newPosition) 201 { 202 BView::FrameMoved(newPosition); 203 } 204 //------------------------------------------------------------------------------ 205 void BStringView::FrameResized(float newWidth, float newHeight) 206 { 207 BView::FrameResized(newWidth, newHeight); 208 } 209 //------------------------------------------------------------------------------ 210 BHandler* BStringView::ResolveSpecifier(BMessage* msg, int32 index, 211 BMessage* specifier, int32 form, 212 const char* property) 213 { 214 return NULL; 215 } 216 //------------------------------------------------------------------------------ 217 void BStringView::MakeFocus(bool state) 218 { 219 BView::MakeFocus(state); 220 } 221 //------------------------------------------------------------------------------ 222 void BStringView::AllAttached() 223 { 224 BView::AllAttached(); 225 } 226 //------------------------------------------------------------------------------ 227 void BStringView::AllDetached() 228 { 229 BView::AllDetached(); 230 } 231 //------------------------------------------------------------------------------ 232 status_t BStringView::GetSupportedSuites(BMessage* data) 233 { 234 return B_OK; 235 } 236 //------------------------------------------------------------------------------ 237 status_t BStringView::Perform(perform_code d, void* arg) 238 { 239 return B_ERROR; 240 } 241 //------------------------------------------------------------------------------ 242 void BStringView::_ReservedStringView1() 243 { 244 } 245 //------------------------------------------------------------------------------ 246 void BStringView::_ReservedStringView2() 247 { 248 } 249 //------------------------------------------------------------------------------ 250 void BStringView::_ReservedStringView3() 251 { 252 } 253 //------------------------------------------------------------------------------ 254 BStringView& BStringView::operator=(const BStringView&) 255 { 256 // Assignment not allowed 257 return *this; 258 } 259 //------------------------------------------------------------------------------ 260 261