16ec25bbaSAdrien Destugues/* 26ec25bbaSAdrien Destugues * Copyright 2010, Haiku inc. 36ec25bbaSAdrien Destugues * Distributed under the terms of the MIT Licence. 46ec25bbaSAdrien Destugues * 56ec25bbaSAdrien Destugues * Documentation by: 66ec25bbaSAdrien Destugues * Clark Gaeble 76ec25bbaSAdrien Destugues * Adrien Destugues <pulkomandy@pulkomandy.ath.cx> 8*a33f8fbdSAdrien Destugues * John Scipione <jscipione@gmail.com> 96ec25bbaSAdrien Destugues * Corresponds to: 10*a33f8fbdSAdrien Destugues * /trunk/headers/os/interface/Box.h rev 42274 11*a33f8fbdSAdrien Destugues * /trunk/src/kits/interface/Box.cpp rev 42274 12*a33f8fbdSAdrien Destugues 136ec25bbaSAdrien Destugues 146ec25bbaSAdrien Destugues/*! 156ec25bbaSAdrien Destugues \file Box.h 166ec25bbaSAdrien Destugues \brief Defines the BBox class 176ec25bbaSAdrien Destugues*/ 186ec25bbaSAdrien Destugues 19*a33f8fbdSAdrien Destugues 20*a33f8fbdSAdrien Destugues/*! 21*a33f8fbdSAdrien Destugues \class BBox 226ec25bbaSAdrien Destugues \ingroup interface 23*a33f8fbdSAdrien Destugues \brief The BBox class is used to draw a square box in a window with an 24*a33f8fbdSAdrien Destugues optional label to group related subviews. 256ec25bbaSAdrien Destugues 26*a33f8fbdSAdrien Destugues A BBox is an organizational interface element used to group related views 27*a33f8fbdSAdrien Destugues together visually. A basic BBox looks like this: 286ec25bbaSAdrien Destugues 29*a33f8fbdSAdrien Destugues \image html B_FANCY_BORDER.png 30*a33f8fbdSAdrien Destugues 31*a33f8fbdSAdrien Destugues A box's label can either be text or it can be another control such 32*a33f8fbdSAdrien Destugues as a checkbox or dropdown box. See SetLabel() for more details on setting 33*a33f8fbdSAdrien Destugues the label on a BBox. 346ec25bbaSAdrien Destugues*/ 356ec25bbaSAdrien Destugues 366ec25bbaSAdrien Destugues 37*a33f8fbdSAdrien Destugues/*! 38*a33f8fbdSAdrien Destugues \fn BBox::BBox(BRect frame, const char *name = NULL, 39*a33f8fbdSAdrien Destugues uint32 resizingMode = B_FOLLOW_LEFT | B_FOLLOW_TOP, 40*a33f8fbdSAdrien Destugues uint32 flags = B_WILL_DRAW | B_FRAME_EVENTS | B_NAVIGABLE_JUMP, 41*a33f8fbdSAdrien Destugues border_style border = B_FANCY_BORDER) 42*a33f8fbdSAdrien Destugues \brief Constructs a BBox from a set of dimensions. 436ec25bbaSAdrien Destugues 44*a33f8fbdSAdrien Destugues \note This is the only constructor that can be used if the BBox is to be 45*a33f8fbdSAdrien Destugues inserted in a window that doesn't use the layout system. 46*a33f8fbdSAdrien Destugues 47*a33f8fbdSAdrien Destugues \param frame The bounds of the BBox. 48*a33f8fbdSAdrien Destugues \param name The name of the BBox. 49*a33f8fbdSAdrien Destugues \param resizingMode Defines the behavior of the BBox as the parent view 5001c2a7f3SAdrien Destugues resizes. 51*a33f8fbdSAdrien Destugues \param flags Behavior flags for the BBox. See BView for details. 52*a33f8fbdSAdrien Destugues \param border The border_style of the BBox. 536ec25bbaSAdrien Destugues*/ 546ec25bbaSAdrien Destugues 556ec25bbaSAdrien Destugues 56*a33f8fbdSAdrien Destugues/*! 57*a33f8fbdSAdrien Destugues \fn BBox::BBox(const char* name, 58*a33f8fbdSAdrien Destugues uint32 flags = B_WILL_DRAW | B_FRAME_EVENTS | B_NAVIGABLE_JUMP, 59*a33f8fbdSAdrien Destugues border_style border = B_FANCY_BORDER, BView* child = NULL) 60*a33f8fbdSAdrien Destugues \brief Constructs a named BBox with dimensions defined automatically by the 6101c2a7f3SAdrien Destugues Layout Kit. 626ec25bbaSAdrien Destugues 63*a33f8fbdSAdrien Destugues \param name The name of the BBox. 64*a33f8fbdSAdrien Destugues \param flags Behavior flags for the BBox. See BView for details. 65*a33f8fbdSAdrien Destugues \param border The border_style of the BBox. 66*a33f8fbdSAdrien Destugues \param child Adds an initial child to the BBox. See the Layout Kit for 67*a33f8fbdSAdrien Destugues details. 686ec25bbaSAdrien Destugues*/ 696ec25bbaSAdrien Destugues 706ec25bbaSAdrien Destugues 71*a33f8fbdSAdrien Destugues/*! 72*a33f8fbdSAdrien Destugues \fn BBox::BBox(border_style border, BView* child) 73*a33f8fbdSAdrien Destugues \brief Constructs an anonymous BBox, with a defined border style and 74*a33f8fbdSAdrien Destugues a child. 756ec25bbaSAdrien Destugues 76*a33f8fbdSAdrien Destugues There can only be a single child view in the BBox. This view can, however, 77*a33f8fbdSAdrien Destugues act as a nesting container if you need more things to show inside the BBox. 786ec25bbaSAdrien Destugues*/ 796ec25bbaSAdrien Destugues 806ec25bbaSAdrien Destugues 81*a33f8fbdSAdrien Destugues/*! 82*a33f8fbdSAdrien Destugues \fn BBox::BBox(BMessage* archive) 83*a33f8fbdSAdrien Destugues \brief For archive restoration, allows a BBox to be constructed from an 84*a33f8fbdSAdrien Destugues \a archive message. 856ec25bbaSAdrien Destugues 86*a33f8fbdSAdrien Destugues This method is usually not called directly. If you want to build a BBox 87*a33f8fbdSAdrien Destugues from a message then you should call Instantiate() which can handle errors 88*a33f8fbdSAdrien Destugues properly. 896ec25bbaSAdrien Destugues 90*a33f8fbdSAdrien Destugues If the \a archive is a deep one, the BBox will also unarchive all 91*a33f8fbdSAdrien Destugues of its children recursively. 926ec25bbaSAdrien Destugues 93*a33f8fbdSAdrien Destugues \param archive The \a archive to restore from. 946ec25bbaSAdrien Destugues*/ 956ec25bbaSAdrien Destugues 966ec25bbaSAdrien Destugues 97*a33f8fbdSAdrien Destugues/*! 98*a33f8fbdSAdrien Destugues \fn BBox::~BBox() 99*a33f8fbdSAdrien Destugues \brief Destructor method. 1006ec25bbaSAdrien Destugues 101*a33f8fbdSAdrien Destugues Calling the destructor will also free the memory used by the box's label 102*a33f8fbdSAdrien Destugues if it has one. 1036ec25bbaSAdrien Destugues*/ 1046ec25bbaSAdrien Destugues 1056ec25bbaSAdrien Destugues 106*a33f8fbdSAdrien Destugues/*! 107*a33f8fbdSAdrien Destugues \fn static BArchivable* BBox::Instantiate(BMessage* archive) 108*a33f8fbdSAdrien Destugues \brief Creates a new BBox from an \a archive. 1096ec25bbaSAdrien Destugues 110*a33f8fbdSAdrien Destugues If the message is a valid BBox then an instance of BBox created from the 111*a33f8fbdSAdrien Destugues passed in \a archive will be returned. Otherwise this method will 112*a33f8fbdSAdrien Destugues return \c NULL. 113*a33f8fbdSAdrien Destugues 114*a33f8fbdSAdrien Destugues \param archive The \a archive message. 115*a33f8fbdSAdrien Destugues 116*a33f8fbdSAdrien Destugues \returns An instance of BBox if the \a archive is valid or \c NULL. 117*a33f8fbdSAdrien Destugues*/ 118*a33f8fbdSAdrien Destugues 119*a33f8fbdSAdrien Destugues 120*a33f8fbdSAdrien Destugues/*! 121*a33f8fbdSAdrien Destugues \fn virtual status_t BBox::Archive(BMessage* archive, 122*a33f8fbdSAdrien Destugues bool deep = true) const; 123*a33f8fbdSAdrien Destugues \brief Archives the BBox into \a archive. 124*a33f8fbdSAdrien Destugues 125*a33f8fbdSAdrien Destugues \param archive The target \a archive which the BBox data will go 126*a33f8fbdSAdrien Destugues into. 1276ec25bbaSAdrien Destugues \param deep Whether or not to recursively archive the children. 128*a33f8fbdSAdrien Destugues \returns A status flag indicating if the archive operation was successful. 129*a33f8fbdSAdrien Destugues 130*a33f8fbdSAdrien Destugues \retval B_OK The archive operation was successful. 131*a33f8fbdSAdrien Destugues \retval B_BAD_VALUE The archive operation failed. 1326ec25bbaSAdrien Destugues*/ 1336ec25bbaSAdrien Destugues 1346ec25bbaSAdrien Destugues 135*a33f8fbdSAdrien Destugues/*! 136*a33f8fbdSAdrien Destugues \fn virtual void BBox::SetBorder(border_style border) 1376ec25bbaSAdrien Destugues \brief Sets the border style. 13801c2a7f3SAdrien Destugues 139*a33f8fbdSAdrien Destugues Possible values are \c B_PLAIN_BORDER (a single 1-pixel line border), 140*a33f8fbdSAdrien Destugues \c B_FANCY_BORDER (the default, beveled look), and \c B_NO_BORDER, which 141*a33f8fbdSAdrien Destugues is used to make an invisible box. See border_style for more details. 1426ec25bbaSAdrien Destugues*/ 1436ec25bbaSAdrien Destugues 1446ec25bbaSAdrien Destugues 145*a33f8fbdSAdrien Destugues/*! 146*a33f8fbdSAdrien Destugues \fn border_style BBox::Border() const 147*a33f8fbdSAdrien Destugues \brief Gets the current border_style of a BBox. 148*a33f8fbdSAdrien Destugues 149*a33f8fbdSAdrien Destugues \returns The border_style flag that is currently set to the BBox. 1506ec25bbaSAdrien Destugues*/ 1516ec25bbaSAdrien Destugues 1526ec25bbaSAdrien Destugues 153*a33f8fbdSAdrien Destugues/*! 154*a33f8fbdSAdrien Destugues \fn float BBox::TopBorderOffset() 155*a33f8fbdSAdrien Destugues \brief Gets the distance from the very top of the BBox to the top border 156*a33f8fbdSAdrien Destugues line in pixels as a \c float. 157*a33f8fbdSAdrien Destugues 158*a33f8fbdSAdrien Destugues \warning This method is not part of the BeOS R5 API and is not yet 159*a33f8fbdSAdrien Destugues finalized. 16001c2a7f3SAdrien Destugues 16101c2a7f3SAdrien Destugues The distance may vary depending on the text or view used as label, and the 162*a33f8fbdSAdrien Destugues font settings. The border is drawn center aligned with the label. You can 163*a33f8fbdSAdrien Destugues use this value to line up two boxes visually if one has a label and the 164*a33f8fbdSAdrien Destugues other does not. 16501c2a7f3SAdrien Destugues 166*a33f8fbdSAdrien Destugues \returns The distance offset of the BBox as a \c float. 1676ec25bbaSAdrien Destugues*/ 1686ec25bbaSAdrien Destugues 1696ec25bbaSAdrien Destugues 170*a33f8fbdSAdrien Destugues/*! 171*a33f8fbdSAdrien Destugues \fn BRect BBox::InnerFrame() 172*a33f8fbdSAdrien Destugues \brief Gets the rectangle just inside the border of the BBox as a BRect. 173*a33f8fbdSAdrien Destugues 174*a33f8fbdSAdrien Destugues \warning This method is not part of the BeOS R5 API and is not yet 175*a33f8fbdSAdrien Destugues finalized. 176*a33f8fbdSAdrien Destugues 177*a33f8fbdSAdrien Destugues \returns A BRect of the dimensions of the box's inside border. 1786ec25bbaSAdrien Destugues*/ 1796ec25bbaSAdrien Destugues 1806ec25bbaSAdrien Destugues 181*a33f8fbdSAdrien Destugues/*! 182*a33f8fbdSAdrien Destugues \fn void BBox::SetLabel(const char* string) 183*a33f8fbdSAdrien Destugues \brief Sets the box's label text. 1846ec25bbaSAdrien Destugues 185*a33f8fbdSAdrien Destugues Below is an example of a BBox with a simple text label: 186*a33f8fbdSAdrien Destugues 187*a33f8fbdSAdrien Destugues \image html BBox_example.png 188*a33f8fbdSAdrien Destugues 189*a33f8fbdSAdrien Destugues The code to create a BBox with a text label looks like this: 190*a33f8fbdSAdrien Destugues 191*a33f8fbdSAdrien Destugues \code 192*a33f8fbdSAdrien DestuguesfIconBox = new BBox("Icon Box"); 193*a33f8fbdSAdrien DestuguesfIconBox->SetLabel("Icon"); 194*a33f8fbdSAdrien Destugues \endcode 195*a33f8fbdSAdrien Destugues 196*a33f8fbdSAdrien Destugues \param string The label text string to set as the box's title. 1976ec25bbaSAdrien Destugues*/ 1986ec25bbaSAdrien Destugues 1996ec25bbaSAdrien Destugues 200*a33f8fbdSAdrien Destugues/*! 201*a33f8fbdSAdrien Destugues \fn status_t BBox::SetLabel(BView* viewLabel) 2026ec25bbaSAdrien Destugues \brief Sets the label from a pre-existing BView. 2036ec25bbaSAdrien Destugues 204*a33f8fbdSAdrien Destugues This version of SetLabel() allows building a BBox with a control as a 205*a33f8fbdSAdrien Destugues label widget. You can pass in any type of BView derived control for this 206*a33f8fbdSAdrien Destugues such as a BPopupMenu or BCheckBox. 207*a33f8fbdSAdrien Destugues 208*a33f8fbdSAdrien Destugues An example of a BBox with a BCheckBox control attached is shown below: 209*a33f8fbdSAdrien Destugues 210*a33f8fbdSAdrien Destugues \image html BBox_with_checkbox.png 211*a33f8fbdSAdrien Destugues 212*a33f8fbdSAdrien Destugues The code to create such a BBox looks like this: 213*a33f8fbdSAdrien Destugues 214*a33f8fbdSAdrien Destugues \code 215*a33f8fbdSAdrien DestuguesfVirtualMemoryEnabledCheckBox = new BCheckBox("Virtual memory check box", 216*a33f8fbdSAdrien Destugues "Enable virtual memory", new BMessage(kVirtualMemoryEnabled)); 217*a33f8fbdSAdrien Destugues 218*a33f8fbdSAdrien DestuguesBBox* fVirtualMemoryBox = new BBox("Virtual memory box"); 219*a33f8fbdSAdrien DestuguesfVirtualMemoryBox->SetLabel(fVirtualMemoryEnabledCheckBox); 220*a33f8fbdSAdrien Destugues \endcode 221*a33f8fbdSAdrien Destugues 222*a33f8fbdSAdrien Destugues \param viewLabel A BView. 223*a33f8fbdSAdrien Destugues \returns \c B_OK 2246ec25bbaSAdrien Destugues*/ 2256ec25bbaSAdrien Destugues 2266ec25bbaSAdrien Destugues 227*a33f8fbdSAdrien Destugues/*! 228*a33f8fbdSAdrien Destugues \fn const char* BBox::Label() const 2296ec25bbaSAdrien Destugues \brief Gets the label's text. 2306ec25bbaSAdrien Destugues 23101c2a7f3SAdrien Destugues This only works if the label was set as text. If you set another view as the 23201c2a7f3SAdrien Destugues label, you have to get its text by other means, likely starting with 23301c2a7f3SAdrien Destugues LabelView. 234*a33f8fbdSAdrien Destugues 235*a33f8fbdSAdrien Destugues \returns The label text of the BBox as a <tt>const char*</tt> if the BBox 236*a33f8fbdSAdrien Destugues has a text label or \c NULL otherwise. 2376ec25bbaSAdrien Destugues*/ 2386ec25bbaSAdrien Destugues 2396ec25bbaSAdrien Destugues 240*a33f8fbdSAdrien Destugues/*! 241*a33f8fbdSAdrien Destugues \fn BView* BBox::LabelView() const 2426ec25bbaSAdrien Destugues \brief Gets the BView representing the label. 2436ec25bbaSAdrien Destugues*/ 2446ec25bbaSAdrien Destugues 2456ec25bbaSAdrien Destugues 246*a33f8fbdSAdrien Destugues/*! 247*a33f8fbdSAdrien Destugues \fn virtual void BBox::Draw(BRect updateRect) 248*a33f8fbdSAdrien Destugues \brief Draws onto the parent window the part of the BBox that intersects 2496ec25bbaSAdrien Destugues the dirty area. 2506ec25bbaSAdrien Destugues 251*a33f8fbdSAdrien Destugues This is an hook method called by the interface kit. You don't have to call 252*a33f8fbdSAdrien Destugues it yourself. If you need to force redrawing of (part of) the BBox, consider 25301c2a7f3SAdrien Destugues using Invalidate instead. 25401c2a7f3SAdrien Destugues 2556ec25bbaSAdrien Destugues \param updateRect The area that needs to be redrawn. Note the box may draw 25601c2a7f3SAdrien Destugues more around the rectangle. 2576ec25bbaSAdrien Destugues*/ 2586ec25bbaSAdrien Destugues 2596ec25bbaSAdrien Destugues 260*a33f8fbdSAdrien Destugues/*! 261*a33f8fbdSAdrien Destugues \fn virtual void BBox::AttachedToWindow() 262*a33f8fbdSAdrien Destugues \brief Hook method called when the BBox is attached to a window. 2636ec25bbaSAdrien Destugues 264*a33f8fbdSAdrien Destugues This method sets the box's background color to the background of the 265*a33f8fbdSAdrien Destugues parent view. 2666ec25bbaSAdrien Destugues 267*a33f8fbdSAdrien Destugues If you are using the layout system, the BBox is also resized according to 268*a33f8fbdSAdrien Destugues the layout of the parent view. 2696ec25bbaSAdrien Destugues*/ 2706ec25bbaSAdrien Destugues 2716ec25bbaSAdrien Destugues 272*a33f8fbdSAdrien Destugues/*! 273*a33f8fbdSAdrien Destugues \fn virtual void BBox::FrameResized(float width, float height) 274*a33f8fbdSAdrien Destugues \brief Called when the BBox needs to change its size. 2756ec25bbaSAdrien Destugues 276*a33f8fbdSAdrien Destugues This method may be called either because the window in which the BBox is 2776ec25bbaSAdrien Destugues was resized, or because the window layout was otherwise altered. 2786ec25bbaSAdrien Destugues 279*a33f8fbdSAdrien Destugues It recomputes the layout of the BBox (including label and contents) and 280*a33f8fbdSAdrien Destugues makes it redraw as necessary. 2816ec25bbaSAdrien Destugues*/ 2826ec25bbaSAdrien Destugues 2836ec25bbaSAdrien Destugues 284*a33f8fbdSAdrien Destugues/*! 285*a33f8fbdSAdrien Destugues \fn virtual void BBox::ResizeToPreferred() 286*a33f8fbdSAdrien Destugues \brief Resizes the BBox to its preferred dimensions. 2876ec25bbaSAdrien Destugues 28801c2a7f3SAdrien Destugues This only works in the non-layout mode, as it forces the resizing. 2896ec25bbaSAdrien Destugues*/ 2906ec25bbaSAdrien Destugues 2916ec25bbaSAdrien Destugues 292*a33f8fbdSAdrien Destugues/*! 293*a33f8fbdSAdrien Destugues \fn virtual void BBox::GetPreferredSize(float* _width, float* _height) 294*a33f8fbdSAdrien Destugues \brief Gets the dimensions that the BBox would prefer to be. 2956ec25bbaSAdrien Destugues 2966ec25bbaSAdrien Destugues The size is computed from the children sizes, unless it was explicitly set 297*a33f8fbdSAdrien Destugues for the BBox (which can be done only if the BBox is configured to 298*a33f8fbdSAdrien Destugues use the Layout Kit). 2996ec25bbaSAdrien Destugues 300*a33f8fbdSAdrien Destugues \note Either the \a _width or \a _height parameter may be set to \c NULL 301*a33f8fbdSAdrien Destugues if you only want to get the other one. 30201c2a7f3SAdrien Destugues 303*a33f8fbdSAdrien Destugues \param[out] _width The width of the preferred size is placed in here. 304*a33f8fbdSAdrien Destugues \param[out] _height The height of the preferred size is placed in here. 3056ec25bbaSAdrien Destugues*/ 3066ec25bbaSAdrien Destugues 3076ec25bbaSAdrien Destugues 308*a33f8fbdSAdrien Destugues/*! 309*a33f8fbdSAdrien Destugues \fn virtual BSize BBox::MinSize() 310*a33f8fbdSAdrien Destugues \brief Gets the minimum possible size of the BBox. 3116ec25bbaSAdrien Destugues 312*a33f8fbdSAdrien Destugues Drawing the BBox at this size ensures the label and the child view are 31301c2a7f3SAdrien Destugues visible. Going smaller means something may get invisible on screen for lack 31401c2a7f3SAdrien Destugues of space. 3156ec25bbaSAdrien Destugues*/ 3166ec25bbaSAdrien Destugues 3176ec25bbaSAdrien Destugues 318*a33f8fbdSAdrien Destugues/*! 319*a33f8fbdSAdrien Destugues \fn virtual BSize BBox::MaxSize() 320*a33f8fbdSAdrien Destugues \brief Gets the maximum possible size of the BBox. 3216ec25bbaSAdrien Destugues 32201c2a7f3SAdrien Destugues The maximum size depends on the child view's one. 323*a33f8fbdSAdrien Destugues 324*a33f8fbdSAdrien Destugues \returns A BSize of the maximum possible size of the BBox. 3256ec25bbaSAdrien Destugues*/ 3266ec25bbaSAdrien Destugues 3276ec25bbaSAdrien Destugues 328*a33f8fbdSAdrien Destugues/*! 329*a33f8fbdSAdrien Destugues \fn virtual BSize BBox::PreferredSize() 3306ec25bbaSAdrien Destugues \brief Returns the box's preferred size. 3316ec25bbaSAdrien Destugues 3326ec25bbaSAdrien Destugues This is the same as GetPreferredSize, but using the more convenient BSize 3336ec25bbaSAdrien Destugues struct. 334*a33f8fbdSAdrien Destugues 335*a33f8fbdSAdrien Destugues \returns A BSize of the minimum possible size of the BBox. 3366ec25bbaSAdrien Destugues*/ 3376ec25bbaSAdrien Destugues 3386ec25bbaSAdrien Destugues 339*a33f8fbdSAdrien Destugues/*! 340*a33f8fbdSAdrien Destugues \fn virtual void BBox::DoLayout() 341*a33f8fbdSAdrien Destugues \brief Lays out the BBox. Moves everything into its appropriate position. 3426ec25bbaSAdrien Destugues 343*a33f8fbdSAdrien Destugues This only works if the BBox uses the layout system from the Layout Kit, 344*a33f8fbdSAdrien Destugues i.e. it was created with one of the BRect-less constructors. 34501c2a7f3SAdrien Destugues 346*a33f8fbdSAdrien Destugues Once the size of the BBox is known, from layouting of the parent views, 347*a33f8fbdSAdrien Destugues this method is called so the BBox can adjust the position and size of the 348*a33f8fbdSAdrien Destugues label, eventually truncating the text if there is not enough space. The 349*a33f8fbdSAdrien Destugues exact border positions are also computed, then the child view is also 350*a33f8fbdSAdrien Destugues layouted if its size constraints changed. 3516ec25bbaSAdrien Destugues*/ 352