1/* 2 * Copyright 2013 Haiku Inc. All rights reserved. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * John Scipione, jscipione@gmail.com 7 * 8 * Corresponds to: 9 * headers/os/interface/MenuField.h hrev46323 10 * src/kits/interface/MenuField.cpp hrev46323 11 */ 12 13 14/*! 15 \file MenuField.h 16 \ingroup interface 17 \ingroup libbe 18 \brief BMenuField class definition and support structures. 19*/ 20 21 22/*! 23 \class BMenuField 24 \ingroup interface 25 \ingroup libbe 26 \brief A labeled pop-up menu. 27 28 A menu field consists of a label and a menu bar. The label, if used, is 29 located to the left of the menu bar. The frame rectangle is divided 30 in half by default. You can call SetDivider() to change the ratio used by 31 the label and menu bar. 32 33 \image html BMenuField_example.png 34 35 A fixed-size menu field's menu bar width and height are limited 36 by the bounds set by the divider position and \a frame rectangle. 37 38 A variable-size menu field's menu bar is only as wide as it needs to 39 be in order to fit the currently selected menu item, and it's height 40 depends on the user's menu font size preference. The height of the 41 frame rectangle is ignored. 42 43 If a menu field's frame rectangle is less than 20 pixels wide, the width 44 is unbounded, the menu bar grows as wide as it needs to in order to fit 45 the currently selected item. If the frame rectangle is wider than 20 46 pixels then the divider position and the width of the frame rectangle set 47 the maximum menu bar width. 48 49 \remark Layout-enabled menu field's are always fixed-size, however, you can 50 make them act like variable-size menu fields by adding them to a 51 horizontal group followed by glue. 52 53 If you're using the menu field as part of a BLayout you can get better 54 control over the placement of the label and menu bar by splitting the 55 label and menu field into separate BLayoutItem objects using the 56 CreateLabelLayoutItem() and CreateMenuBarLayoutItem() methods. 57 58 You must pass a menu object into the constructor containing the choices 59 the user can select from. The menu is owned by the menu field and its 60 memory will be freed when the menu field is deleted. A BPopUpMenu is 61 typically used instead of a regular BMenu because it opens directly 62 underneath the mouse pointer and is set to radio mode and 63 label-from-marked mode by default, but, this is entirely up to you. 64*/ 65 66 67/*! 68 \fn BMenuField::BMenuField(BRect frame, const char* name, const char* label, 69 BMenu* menu, uint32 resizingMode, uint32 flags) 70 \brief Creates a new variable-size BMenuField object. 71 72 \param frame The \a frame rectangle of the menu field including the label. 73 \param name The \a name of the menu field, internal only, can be \c NULL. 74 \param label The \a label shown to the user, can be blank. 75 \param menu The \a menu of choices shown to the user, typically a BPopUpMenu. 76 \param resizingMode Defines the menu field's behavior when its parent is 77 resized, see BView for details. 78 \param flags The view flags, see BView for details. 79*/ 80 81 82/*! 83 \fn BMenuField::BMenuField(BRect frame, const char* name, const char* label, 84 BMenu* menu, bool fixedSize, uint32 resizingMode, uint32 flags) 85 \brief Creates a new BMenuField object. This constructor allows a you to 86 create either a fixed-size or variable-size menu field. 87 88 \param frame The \a frame rectangle of the menu field including the label. 89 \param name The \a name of the menu field, internal only, can be \c NULL. 90 \param label The \a label shown to the user, can be blank. 91 \param menu The \a menu of choices shown to the user, typically a BPopUpMenu. 92 \param fixedSize \c true for fixed-size, \c false for variable-size. 93 \param resizingMode Defines the menu field's behavior when its parent is 94 resized, see BView for details. 95 \param flags The view flags, see BView for details. 96*/ 97 98 99/*! 100 \fn BMenuField::BMenuField(const char* name, const char* label, BMenu* menu, 101 uint32 flags) 102 \brief Creates a new BMenuField object suitable as part of a BLayout. 103 104 \note This method was not available in BeOS R5. 105 106 \param name The \a name of the menu field, internal only, can be \c NULL. 107 \param label The \a label shown to the user, can be blank. 108 \param menu The \a menu of choices shown to the user, typically a BPopUpMenu. 109 \param flags The view flags, see BView for details. 110*/ 111 112 113/*! 114 \fn BMenuField::BMenuField(const char* label, BMenu* menu, uint32 flags) 115 \brief Creates a new BMenuField object suitable as part of a BLayout. 116 This constructor omits the internal name parameter. 117 118 \note This method was not available in BeOS R5. 119 120 \param label The \a label shown to the user, can be blank. 121 \param menu The \a menu of choices shown to the user, typically a BPopUpMenu. 122 \param flags The view flags, see BView for details. 123*/ 124 125 126/*! 127 \fn BMenuField::BMenuField(BMessage* data) 128 \brief Archive constructor. 129 130 \param data The message \a data to construct the menu field from. 131*/ 132 133 134/*! 135 \fn BMenuField::~BMenuField() 136 \brief Destructor. 137 138 Also frees the memory used by the label and menu. 139*/ 140 141 142/*! 143 \fn BArchivable* BMenuField::Instantiate(BMessage* data) 144 \brief Creates a new BMenuField object from an \a data message. 145 146 \returns A newly created BMenuField object or \c NULL if the message 147 doesn't contain an archived BMenuField. 148*/ 149 150 151/*! 152 \fn status_t BMenuField::Archive(BMessage* data, bool deep) const 153 \brief Archives the the BMenuField object into the \a data message. 154 155 Adds the label, divider, and current state of the BMenuField to the archive. 156 157 \param data A pointer to the BMessage to archive the object into. 158 \param deep Whether or not to archive attached menu and menu items as well. 159 160 \return A status code, \c B_OK if everything went well or an error code 161 otherwise. 162 \retval B_OK The object was archived successfully. 163 \retval B_NO_MEMORY Ran out of memory while archiving the object. 164*/ 165 166 167/*! 168 \fn status_t BMenuField::AllArchived(BMessage* into) const 169 \brief Hook method called when all views have been archived. 170 171 Also archives the label and menu bar. 172 173 \param into Archived data message. 174*/ 175 176 177/*! 178 \fn status_t BMenuField::AllUnarchived(const BMessage* from) 179 \brief Hook method called when all views have been unarchived. 180 181 Also unarchives the label and menu bar. 182 183 \param from Unarchived data message. 184*/ 185 186 187/*! 188 \fn void BMenuField::AttachedToWindow() 189 \brief Hook method called when the menu field is attached to a window. 190 191 Adjusts the background color to match the background color of the parent 192 view and adjusts the height to be the attached menu bar which depends on 193 the user's current menu font setting. 194*/ 195 196 197/*! 198 \fn void BMenuField::AllAttached() 199 \brief Similar to AttachedToWindow() but this method is triggered after 200 all child views have already been attached to a window. 201 202 If the attached menu bar is too narrow it is resized it to fit the menu 203 items. 204*/ 205 206 207/*! 208 \fn void BMenuField::Draw(BRect updateRect) 209 \brief Draws the area of the menu field that intersects \a updateRect. 210 211 The menu field is drawn differently based on whether or not it is the 212 current focus view and whether or not it is enabled. 213 214 \param updateRect The rectangular area to be drawn. 215*/ 216 217 218/*! 219 \fn void BMenuField::FrameResized(float newWidth, float newHeight) 220 \brief Hook method called when the menu bar is resized. 221 222 Adjusts the menu bar size and location. 223 224 \param newWidth The new \a width of the menu field. 225 \param newHeight The new \a height of the menu field. 226*/ 227 228 229/*! 230 \fn void BMenuField::KeyDown(const char* bytes, int32 numBytes) 231 \brief Hook method called when a keyboard key is pressed. 232 233 Provides for keyboard navigation allowing users to open the menu by pressing 234 the space bar, right arrow, or down arrow. 235 236 \param bytes The bytes of the key combination pressed. 237 \param numBytes The number of bytes in \a bytes. 238*/ 239 240 241/*! 242 \fn void BMenuField::MouseDown(BPoint where) 243 \brief Hook method called when a mouse button is pressed. 244 245 Provides the ability to open the menu field menu using the mouse, even if 246 the user clicks on the menu field label. 247 248 \param where The point on the screen where to mouse pointer is when 249 the mouse button is pressed. 250*/ 251 252 253/*! 254 \fn void BMenuField::WindowActivated(bool state) 255 \brief Hook method called when the attached window is activated or 256 deactivated. 257 258 Redraws the focus ring around the menu field when the window is activated 259 and deactivated if it is the window's current focus view. 260 261 \param state \c true if the window becomes activated, \c false if the 262 window becomes deactivated. 263*/ 264 265 266/*! 267 \fn void BMenuField::MakeFocus(bool focused) 268 \brief Makes the view the current focus view of the window or gives up 269 being the focus view of the window. 270 271 Enables or disables keyboard navigation and invalidates the menu field. 272 273 \param focused \a true to set focus, \a false to remove it. 274*/ 275 276 277/*! 278 \fn BMenu* BMenuField::Menu() const 279 \brief Returns a pointer to the menu attached to the menu bar that opens 280 when the user clicks on the menu field. 281*/ 282 283 284/*! 285 \fn BMenuBar* BMenuField::MenuBar() const 286 \brief Returns a pointer to the attached menu bar that contains the pop-up 287 menu. 288*/ 289 290 291/*! 292 \fn BMenuItem* BMenuField::MenuItem() const 293 \brief Returns the first menu item attached to the menu bar containing 294 the pop-up menu. 295*/ 296 297 298/*! 299 \fn void BMenuField::SetLabel(const char* label) 300 \brief Sets the menu field label to \a label. 301 302 \param label The \a label to set to the menu field. 303*/ 304 305 306/*! 307 \fn const char* BMenuField::Label() const 308 \brief Returns the menu field label. 309 310 \return The menu field label or \c NULL if not assigned. 311*/ 312 313 314/*! 315 \fn void BMenuField::SetEnabled(bool on) 316 \brief Enables or disables the menu field. 317 318 \param on \c true to enable the menu field, \c false to disable it. 319*/ 320 321 322/*! 323 \fn bool BMenuField::IsEnabled() const 324 \brief Returns whether or not the menu is enabled. 325 326 \return \c true if the menu is enabled, \c false if disabled. 327*/ 328 329 330/*! 331 \fn void BMenuField::SetAlignment(alignment label) 332 \brief Set the alignment of the menu field label. The default alignment 333 is \c B_ALIGN_LEFT. 334 335 \remark For menu fields that are part of a BLayout consider 336 using BLayoutItem::SetExplicitAlignment() instead. 337 338 \param label The alignment to set, choices include: 339 - \c B_ALIGN_LEFT 340 - \c B_ALIGN_CENTER 341 - \c B_ALIGN_RIGHT 342*/ 343 344 345/*! 346 \fn alignment BMenuField::Alignment() const 347 \brief Returns the label's current alignment. 348 349 \return The label's current alignment constant. 350 351 \see SetAlignment() for more details. 352*/ 353 354 355/*! 356 \fn void BMenuField::SetDivider(float position) 357 \brief Sets the horizontal \a position of the divider that separates the 358 label from the menu bar. 359 360 \remark It is not recommended to use this method for menu fields that are 361 part of a BLayout. Instead split the label and menu bar into 362 separate layout items using CreateLabelLayoutItem() and 363 CreateMenuBarLayoutItem(). This allows you to have better control 364 over the position of the label and menu bar portions of your 365 menu fields. 366 367 \param position The divider \a position to set, should be an integral value. 368*/ 369 370 371/*! 372 \fn float BMenuField::Divider() const 373 \brief Returns the current divider position. 374 375 \return The current divider position as a float. 376 377 \see SetDivider() for more details. 378*/ 379 380 381/*! 382 \fn void BMenuField::ShowPopUpMarker() 383 \brief Shows the pop-up marker located on the right edge of the menu bar. 384 385 \note This method was not available in BeOS R5. 386 387 \sa HidePopUpMarker() 388*/ 389 390 391/*! 392 \fn void BMenuField::HidePopUpMarker() 393 \brief Hides to pop-up marker. 394 395 \note This method was not available in BeOS R5. 396 397 \sa ShowPopUpMarker() 398*/ 399 400 401/*! 402 \fn BLayoutItem* BMenuField::CreateLabelLayoutItem() 403 \brief Returns a pointer to the label layout item. 404 (Layout constructor only) 405 406 \note This method was not available in BeOS R5. 407 408 \sa CreateMenuBarLayoutItem() 409*/ 410 411 412/*! 413 \fn BLayoutItem* BMenuField::CreateMenuBarLayoutItem() 414 \brief Returns a pointer to the menu bar layout item. 415 (Layout constructor only) 416 417 \note This method was not available in BeOS R5. 418 419 \sa CreateLabelLayoutItem() 420*/ 421