1 /* 2 * Copyright 2007-2013 Haiku, Inc. All rights reserved. 3 * Distributed under the terms of the MIT License. 4 */ 5 #ifndef _MENU_H 6 #define _MENU_H 7 8 9 #include <InterfaceDefs.h> 10 #include <List.h> 11 #include <View.h> 12 13 14 class BMenu; 15 class BMenuBar; 16 class BMenuItem; 17 18 19 namespace BPrivate { 20 class BMenuWindow; 21 class ExtraMenuData; 22 class TriggerList; 23 class MenuPrivate; 24 } 25 26 enum menu_layout { 27 B_ITEMS_IN_ROW = 0, 28 B_ITEMS_IN_COLUMN, 29 B_ITEMS_IN_MATRIX 30 }; 31 32 struct menu_info { 33 float font_size; 34 font_family f_family; 35 font_style f_style; 36 rgb_color background_color; 37 int32 separator; 38 bool click_to_open; 39 bool triggers_always_shown; 40 }; 41 42 status_t get_menu_info(menu_info* info); 43 status_t set_menu_info(menu_info* info); 44 45 typedef bool (*menu_tracking_hook)(BMenu* menu, void* state); 46 47 48 class BMenu : public BView { 49 public: 50 BMenu(const char* name, 51 menu_layout layout = B_ITEMS_IN_COLUMN); 52 BMenu(const char* name, float width, 53 float height); 54 BMenu(BMessage* archive); 55 56 virtual ~BMenu(); 57 58 static BArchivable* Instantiate(BMessage* archive); 59 virtual status_t Archive(BMessage* archive, 60 bool deep = true) const; 61 62 virtual void AttachedToWindow(); 63 virtual void DetachedFromWindow(); 64 virtual void AllAttached(); 65 virtual void AllDetached(); 66 67 virtual void Draw(BRect updateRect); 68 69 virtual void MessageReceived(BMessage* message); 70 virtual void KeyDown(const char* bytes, int32 numBytes); 71 72 virtual BSize MinSize(); 73 virtual BSize MaxSize(); 74 virtual BSize PreferredSize(); 75 virtual void GetPreferredSize(float* _width, 76 float* _height); 77 virtual void ResizeToPreferred(); 78 virtual void DoLayout(); 79 virtual void FrameMoved(BPoint where); 80 virtual void FrameResized(float width, float height); 81 82 void InvalidateLayout(); 83 84 virtual void MakeFocus(bool focus = true); 85 86 bool AddItem(BMenuItem* item); 87 bool AddItem(BMenuItem* item, int32 index); 88 bool AddItem(BMenuItem* item, BRect frame); 89 bool AddItem(BMenu* menu); 90 bool AddItem(BMenu* menu, int32 index); 91 bool AddItem(BMenu* menu, BRect frame); 92 bool AddList(BList* list, int32 index); 93 94 bool AddSeparatorItem(); 95 96 bool RemoveItem(BMenuItem* item); 97 BMenuItem* RemoveItem(int32 index); 98 bool RemoveItems(int32 index, int32 count, 99 bool deleteItems = false); 100 bool RemoveItem(BMenu* menu); 101 102 BMenuItem* ItemAt(int32 index) const; 103 BMenu* SubmenuAt(int32 index) const; 104 int32 CountItems() const; 105 int32 IndexOf(BMenuItem* item) const; 106 int32 IndexOf(BMenu* menu) const; 107 BMenuItem* FindItem(uint32 command) const; 108 BMenuItem* FindItem(const char* name) const; 109 110 virtual status_t SetTargetForItems(BHandler* target); 111 virtual status_t SetTargetForItems(BMessenger messenger); 112 virtual void SetEnabled(bool enable); 113 virtual void SetRadioMode(bool on); 114 virtual void SetTriggersEnabled(bool enable); 115 virtual void SetMaxContentWidth(float maxWidth); 116 117 void SetLabelFromMarked(bool on); 118 bool IsLabelFromMarked(); 119 bool IsEnabled() const; 120 bool IsRadioMode() const; 121 bool AreTriggersEnabled() const; 122 bool IsRedrawAfterSticky() const; 123 float MaxContentWidth() const; 124 125 BMenuItem* FindMarked(); 126 int32 FindMarkedIndex(); 127 128 BMenu* Supermenu() const; 129 BMenuItem* Superitem() const; 130 131 132 virtual BHandler* ResolveSpecifier(BMessage* message, 133 int32 index, BMessage* specifier, 134 int32 form, const char* property); 135 virtual status_t GetSupportedSuites(BMessage* data); 136 137 virtual status_t Perform(perform_code d, void* arg); 138 139 protected: 140 BMenu(BRect frame, const char* name, 141 uint32 resizeMask, uint32 flags, 142 menu_layout layout, bool resizeToFit); 143 144 virtual void LayoutInvalidated(bool descendants); 145 146 virtual BPoint ScreenLocation(); 147 148 void SetItemMargins(float left, float top, 149 float right, float bottom); 150 void GetItemMargins(float* _left, float* _top, 151 float* _right, float* _bottom) const; 152 153 menu_layout Layout() const; 154 155 virtual void Show(); 156 void Show(bool selectFirstItem); 157 void Hide(); 158 BMenuItem* Track(bool startOpened = false, 159 BRect* specialRect = NULL); 160 161 public: 162 enum add_state { 163 B_INITIAL_ADD, 164 B_PROCESSING, 165 B_ABORT 166 }; 167 virtual bool AddDynamicItem(add_state state); 168 virtual void DrawBackground(BRect updateRect); 169 170 void SetTrackingHook(menu_tracking_hook hook, 171 void* state); 172 173 private: 174 friend class BMenuBar; 175 friend class BSeparatorItem; 176 friend class BPrivate::MenuPrivate; 177 friend status_t _init_interface_kit_(); 178 friend status_t set_menu_info(menu_info* info); 179 friend status_t get_menu_info(menu_info* info); 180 181 struct LayoutData; 182 183 virtual void _ReservedMenu3(); 184 virtual void _ReservedMenu4(); 185 virtual void _ReservedMenu5(); 186 virtual void _ReservedMenu6(); 187 188 BMenu& operator=(const BMenu& other); 189 190 void _InitData(BMessage* archive); 191 bool _Show(bool selectFirstItem = false, 192 bool keyDown = false); 193 void _Hide(); 194 BMenuItem* _Track(int* action, long start = -1); 195 196 void _UpdateNavigationArea(BPoint position, 197 BRect& navAreaRectAbove, 198 BRect& navAreaBelow); 199 200 void _UpdateStateOpenSelect(BMenuItem* item, 201 BPoint position, BRect& navAreaRectAbove, 202 BRect& navAreaBelow, 203 bigtime_t& selectedTime, 204 bigtime_t& navigationAreaTime); 205 void _UpdateStateClose(BMenuItem* item, 206 const BPoint& where, 207 const uint32& buttons); 208 209 bool _AddItem(BMenuItem* item, int32 index); 210 bool _RemoveItems(int32 index, int32 count, 211 BMenuItem* item, bool deleteItems = false); 212 bool _RelayoutIfNeeded(); 213 void _LayoutItems(int32 index); 214 BSize _ValidatePreferredSize(); 215 void _ComputeLayout(int32 index, bool bestFit, 216 bool moveItems, float* width, 217 float* height); 218 void _ComputeColumnLayout(int32 index, bool bestFit, 219 bool moveItems, BRect* override, BRect& outRect); 220 void _ComputeRowLayout(int32 index, bool bestFit, 221 bool moveItems, BRect& outRect); 222 void _ComputeMatrixLayout(BRect& outRect); 223 224 BRect _CalcFrame(BPoint where, bool* scrollOn); 225 226 protected: 227 void DrawItems(BRect updateRect); 228 229 private: 230 bool _OverSuper(BPoint loc); 231 bool _OverSubmenu(BMenuItem* item, BPoint loc); 232 BPrivate::BMenuWindow* _MenuWindow(); 233 void _DeleteMenuWindow(); 234 BMenuItem* _HitTestItems(BPoint where, 235 BPoint slop = B_ORIGIN) const; 236 BRect _Superbounds() const; 237 void _CacheFontInfo(); 238 239 void _ItemMarked(BMenuItem* item); 240 void _Install(BWindow* target); 241 void _Uninstall(); 242 void _SelectItem(BMenuItem* item, 243 bool showSubmenu = true, 244 bool selectFirstItem = false, 245 bool keyDown = false); 246 bool _SelectNextItem(BMenuItem* item, bool forward); 247 BMenuItem* _NextItem(BMenuItem* item, bool forward) const; 248 void _SetIgnoreHidden(bool ignoreHidden) 249 { fIgnoreHidden = ignoreHidden; } 250 void _SetStickyMode(bool on); 251 bool _IsStickyMode() const; 252 253 // Methods to get the current modifier keycode 254 void _GetShiftKey(uint32 &value) const; 255 void _GetControlKey(uint32 &value) const; 256 void _GetCommandKey(uint32 &value) const; 257 void _GetOptionKey(uint32 &value) const; 258 void _GetMenuKey(uint32 &value) const; 259 260 void _CalcTriggers(); 261 bool _ChooseTrigger(const char* title, int32& index, 262 uint32& trigger, 263 BPrivate::TriggerList& triggers); 264 void _UpdateWindowViewSize(const bool &updatePosition); 265 bool _AddDynamicItems(bool keyDown = false); 266 bool _OkToProceed(BMenuItem* item, 267 bool keyDown = false); 268 269 bool _CustomTrackingWantsToQuit(); 270 271 int _State(BMenuItem** _item = NULL) const; 272 void _InvokeItem(BMenuItem* item, bool now = false); 273 void _QuitTracking(bool onlyThis = true); 274 275 static menu_info sMenuInfo; 276 277 // Variables to keep track of what code is currently assigned to 278 // each modifier key 279 static uint32 sShiftKey; 280 static uint32 sControlKey; 281 static uint32 sOptionKey; 282 static uint32 sCommandKey; 283 static uint32 sMenuKey; 284 285 BMenuItem* fChosenItem; 286 BList fItems; 287 BRect fPad; 288 BMenuItem* fSelected; 289 BPrivate::BMenuWindow* fCachedMenuWindow; 290 BMenu* fSuper; 291 BMenuItem* fSuperitem; 292 BRect fSuperbounds; 293 float fAscent; 294 float fDescent; 295 float fFontHeight; 296 uint32 fState; 297 menu_layout fLayout; 298 BRect* fExtraRect; 299 float fMaxContentWidth; 300 BPoint* fInitMatrixSize; 301 BPrivate::ExtraMenuData* fExtraMenuData; 302 303 LayoutData* fLayoutData; 304 305 int32 _reserved; 306 307 char fTrigger; 308 bool fResizeToFit; 309 bool fUseCachedMenuLayout; 310 bool fEnabled; 311 bool fDynamicName; 312 bool fRadioMode; 313 bool fTrackNewBounds; 314 bool fStickyMode; 315 bool fIgnoreHidden; 316 bool fTriggerEnabled; 317 bool fHasSubmenus; 318 bool fAttachAborted; 319 }; 320 321 #endif // _MENU_H 322