1 /* 2 * Copyright 2006, Ingo Weinhold <bonefish@cs.tu-berlin.de>. 3 * All rights reserved. Distributed under the terms of the MIT License. 4 */ 5 6 #include <SplitView.h> 7 8 #include <stdio.h> 9 10 #include <Archivable.h> 11 #include <ControlLook.h> 12 #include <Cursor.h> 13 14 #include "SplitLayout.h" 15 16 17 BSplitView::BSplitView(enum orientation orientation, float spacing) 18 : 19 BView(NULL, 20 B_WILL_DRAW | B_FULL_UPDATE_ON_RESIZE | B_INVALIDATE_AFTER_LAYOUT, 21 fSplitLayout = new BSplitLayout(orientation, spacing)) 22 { 23 SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR)); 24 } 25 26 27 BSplitView::BSplitView(BMessage* from) 28 : 29 BView(BUnarchiver::PrepareArchive(from)), 30 fSplitLayout(NULL) 31 { 32 BUnarchiver(from).Finish(); 33 } 34 35 36 BSplitView::~BSplitView() 37 { 38 } 39 40 41 void 42 BSplitView::SetInsets(float left, float top, float right, float bottom) 43 { 44 left = BControlLook::ComposeSpacing(left); 45 top = BControlLook::ComposeSpacing(top); 46 right = BControlLook::ComposeSpacing(right); 47 bottom = BControlLook::ComposeSpacing(bottom); 48 49 fSplitLayout->SetInsets(left, top, right, bottom); 50 } 51 52 53 void 54 BSplitView::SetInsets(float horizontal, float vertical) 55 { 56 horizontal = BControlLook::ComposeSpacing(horizontal); 57 vertical = BControlLook::ComposeSpacing(vertical); 58 fSplitLayout->SetInsets(horizontal, vertical, horizontal, vertical); 59 } 60 61 62 void 63 BSplitView::SetInsets(float insets) 64 { 65 insets = BControlLook::ComposeSpacing(insets); 66 fSplitLayout->SetInsets(insets, insets, insets, insets); 67 } 68 69 70 void 71 BSplitView::GetInsets(float* left, float* top, float* right, 72 float* bottom) const 73 { 74 fSplitLayout->GetInsets(left, top, right, bottom); 75 } 76 77 78 float 79 BSplitView::Spacing() const 80 { 81 return fSplitLayout->Spacing(); 82 } 83 84 85 void 86 BSplitView::SetSpacing(float spacing) 87 { 88 fSplitLayout->SetSpacing(spacing); 89 } 90 91 92 orientation 93 BSplitView::Orientation() const 94 { 95 return fSplitLayout->Orientation(); 96 } 97 98 99 void 100 BSplitView::SetOrientation(enum orientation orientation) 101 { 102 fSplitLayout->SetOrientation(orientation); 103 } 104 105 106 float 107 BSplitView::SplitterSize() const 108 { 109 return fSplitLayout->SplitterSize(); 110 } 111 112 113 void 114 BSplitView::SetSplitterSize(float size) 115 { 116 fSplitLayout->SetSplitterSize(size); 117 } 118 119 120 float 121 BSplitView::ItemWeight(int32 index) const 122 { 123 return fSplitLayout->ItemWeight(index); 124 } 125 126 127 float 128 BSplitView::ItemWeight(BLayoutItem* item) const 129 { 130 return fSplitLayout->ItemWeight(item); 131 } 132 133 134 void 135 BSplitView::SetItemWeight(int32 index, float weight, bool invalidateLayout) 136 { 137 fSplitLayout->SetItemWeight(index, weight, invalidateLayout); 138 } 139 140 141 void 142 BSplitView::SetItemWeight(BLayoutItem* item, float weight) 143 { 144 fSplitLayout->SetItemWeight(item, weight); 145 } 146 147 148 void 149 BSplitView::SetCollapsible(bool collapsible) 150 { 151 fSplitLayout->SetCollapsible(collapsible); 152 } 153 154 155 void 156 BSplitView::SetCollapsible(int32 index, bool collapsible) 157 { 158 fSplitLayout->SetCollapsible(index, collapsible); 159 } 160 161 162 void 163 BSplitView::SetCollapsible(int32 first, int32 last, bool collapsible) 164 { 165 fSplitLayout->SetCollapsible(first, last, collapsible); 166 } 167 168 169 void 170 BSplitView::AddChild(BView* child, BView* sibling) 171 { 172 BView::AddChild(child, sibling); 173 } 174 175 176 bool 177 BSplitView::AddChild(BView* child, float weight) 178 { 179 return fSplitLayout->AddView(child, weight); 180 } 181 182 183 bool 184 BSplitView::AddChild(int32 index, BView* child, float weight) 185 { 186 return fSplitLayout->AddView(index, child, weight); 187 } 188 189 190 bool 191 BSplitView::AddChild(BLayoutItem* child) 192 { 193 return fSplitLayout->AddItem(child); 194 } 195 196 197 bool 198 BSplitView::AddChild(BLayoutItem* child, float weight) 199 { 200 return fSplitLayout->AddItem(child, weight); 201 } 202 203 204 bool 205 BSplitView::AddChild(int32 index, BLayoutItem* child, float weight) 206 { 207 return fSplitLayout->AddItem(index, child, weight); 208 } 209 210 211 void 212 BSplitView::Draw(BRect updateRect) 213 { 214 // draw the splitters 215 int32 draggedSplitterIndex = fSplitLayout->DraggedSplitter(); 216 int32 count = fSplitLayout->CountItems(); 217 for (int32 i = 0; i < count - 1; i++) { 218 BRect frame = fSplitLayout->SplitterItemFrame(i); 219 DrawSplitter(frame, updateRect, Orientation(), 220 draggedSplitterIndex == i); 221 } 222 } 223 224 225 void 226 BSplitView::MouseDown(BPoint where) 227 { 228 SetMouseEventMask(B_POINTER_EVENTS, 229 B_LOCK_WINDOW_FOCUS | B_SUSPEND_VIEW_FOCUS); 230 231 if (fSplitLayout->StartDraggingSplitter(where)) 232 Invalidate(); 233 } 234 235 236 void 237 BSplitView::MouseUp(BPoint where) 238 { 239 if (fSplitLayout->StopDraggingSplitter()) { 240 Relayout(); 241 Invalidate(); 242 } 243 } 244 245 246 void 247 BSplitView::MouseMoved(BPoint where, uint32 transit, const BMessage* message) 248 { 249 BCursor cursor(B_CURSOR_ID_SYSTEM_DEFAULT); 250 251 int32 splitterIndex = fSplitLayout->DraggedSplitter(); 252 253 if (splitterIndex >= 0 || fSplitLayout->IsAboveSplitter(where)) { 254 if (Orientation() == B_VERTICAL) 255 cursor = BCursor(B_CURSOR_ID_RESIZE_NORTH_SOUTH); 256 else 257 cursor = BCursor(B_CURSOR_ID_RESIZE_EAST_WEST); 258 } 259 260 if (splitterIndex >= 0) { 261 BRect oldFrame = fSplitLayout->SplitterItemFrame(splitterIndex); 262 if (fSplitLayout->DragSplitter(where)) { 263 Invalidate(oldFrame); 264 Invalidate(fSplitLayout->SplitterItemFrame(splitterIndex)); 265 } 266 } 267 268 SetViewCursor(&cursor, true); 269 } 270 271 272 void 273 BSplitView::SetLayout(BLayout* layout) 274 { 275 // not allowed 276 } 277 278 279 status_t 280 BSplitView::Archive(BMessage* into, bool deep) const 281 { 282 return BView::Archive(into, deep); 283 } 284 285 286 status_t 287 BSplitView::AllUnarchived(const BMessage* from) 288 { 289 status_t err = BView::AllUnarchived(from); 290 if (err == B_OK) { 291 fSplitLayout = dynamic_cast<BSplitLayout*>(GetLayout()); 292 if (!fSplitLayout && GetLayout()) 293 return B_BAD_TYPE; 294 else if (!fSplitLayout) 295 return B_ERROR; 296 } 297 return err; 298 } 299 300 301 BArchivable* 302 BSplitView::Instantiate(BMessage* from) 303 { 304 if (validate_instantiation(from, "BSplitView")) 305 return new BSplitView(from); 306 return NULL; 307 } 308 309 310 void 311 BSplitView::DrawSplitter(BRect frame, const BRect& updateRect, 312 enum orientation orientation, bool pressed) 313 { 314 _DrawDefaultSplitter(this, frame, updateRect, orientation, pressed); 315 } 316 317 318 void 319 BSplitView::_DrawDefaultSplitter(BView* view, BRect frame, 320 const BRect& updateRect, enum orientation orientation, bool pressed) 321 { 322 uint32 flags = pressed ? BControlLook::B_ACTIVATED : 0; 323 be_control_look->DrawSplitter(view, frame, updateRect, view->ViewColor(), 324 orientation, flags, 0); 325 } 326