1 /******************************************************************************* 2 / 3 / File: Menu.h 4 / 5 / Description: BMenu display a menu of selectable items. 6 / 7 / Copyright 1994-98, Be Incorporated, All Rights Reserved 8 / 9 *******************************************************************************/ 10 11 #ifndef _MENU_H 12 #define _MENU_H 13 14 #include <BeBuild.h> 15 #include <InterfaceDefs.h> 16 #include <List.h> 17 #include <View.h> 18 19 /*----------------------------------------------------------------*/ 20 /*----- Menu decalrations and structures -------------------------*/ 21 22 class BMenuItem; 23 class BMenuBar; 24 25 namespace BPrivate { 26 class BMenuWindow; 27 } 28 29 class _ExtraMenuData_; 30 31 enum menu_layout { 32 B_ITEMS_IN_ROW = 0, 33 B_ITEMS_IN_COLUMN, 34 B_ITEMS_IN_MATRIX 35 }; 36 37 struct menu_info { 38 float font_size; 39 font_family f_family; 40 font_style f_style; 41 rgb_color background_color; 42 int32 separator; 43 bool click_to_open; 44 bool triggers_always_shown; 45 }; 46 47 _IMPEXP_BE status_t set_menu_info(menu_info *info); 48 _IMPEXP_BE status_t get_menu_info(menu_info *info); 49 50 typedef bool (* menu_tracking_hook )(BMenu *, void *); 51 52 /*----------------------------------------------------------------*/ 53 /*----- BMenu class ----------------------------------------------*/ 54 55 class BMenu : public BView 56 { 57 public: 58 BMenu( const char *title, 59 menu_layout layout = B_ITEMS_IN_COLUMN); 60 BMenu(const char *title, float width, float height); 61 virtual ~BMenu(); 62 63 BMenu(BMessage *data); 64 static BArchivable *Instantiate(BMessage *data); 65 virtual status_t Archive(BMessage *data, bool deep = true) const; 66 67 virtual void AttachedToWindow(); 68 virtual void DetachedFromWindow(); 69 70 bool AddItem(BMenuItem *item); 71 bool AddItem(BMenuItem *item, int32 index); 72 bool AddItem(BMenuItem *item, BRect frame); 73 bool AddItem(BMenu *menu); 74 bool AddItem(BMenu *menu, int32 index); 75 bool AddItem(BMenu *menu, BRect frame); 76 bool AddList(BList *list, int32 index); 77 bool AddSeparatorItem(); 78 bool RemoveItem(BMenuItem *item); 79 BMenuItem *RemoveItem(int32 index); 80 bool RemoveItems(int32 index, 81 int32 count, 82 bool del = false); 83 bool RemoveItem(BMenu *menu); 84 85 BMenuItem *ItemAt(int32 index) const; 86 BMenu *SubmenuAt(int32 index) const; 87 int32 CountItems() const; 88 int32 IndexOf(BMenuItem *item) const; 89 int32 IndexOf(BMenu *menu) const; 90 BMenuItem *FindItem(uint32 command) const; 91 BMenuItem *FindItem(const char *name) const; 92 93 virtual status_t SetTargetForItems(BHandler *target); 94 virtual status_t SetTargetForItems(BMessenger messenger); 95 virtual void SetEnabled(bool state); 96 virtual void SetRadioMode(bool state); 97 virtual void SetTriggersEnabled(bool state); 98 virtual void SetMaxContentWidth(float max); 99 100 void SetLabelFromMarked(bool on); 101 bool IsLabelFromMarked(); 102 bool IsEnabled() const; 103 bool IsRadioMode() const; 104 bool AreTriggersEnabled() const; 105 bool IsRedrawAfterSticky() const; 106 float MaxContentWidth() const; 107 108 BMenuItem *FindMarked(); 109 110 BMenu *Supermenu() const; 111 BMenuItem *Superitem() const; 112 113 virtual void MessageReceived(BMessage *msg); 114 virtual void KeyDown(const char *bytes, int32 numBytes); 115 virtual void Draw(BRect updateRect); 116 virtual void GetPreferredSize(float *width, float *height); 117 virtual void ResizeToPreferred(); 118 virtual void FrameMoved(BPoint new_position); 119 virtual void FrameResized(float new_width, float new_height); 120 void InvalidateLayout(); 121 122 virtual BHandler *ResolveSpecifier(BMessage *msg, 123 int32 index, 124 BMessage *specifier, 125 int32 form, 126 const char *property); 127 virtual status_t GetSupportedSuites(BMessage *data); 128 129 virtual status_t Perform(perform_code d, void *arg); 130 131 virtual void MakeFocus(bool state = true); 132 virtual void AllAttached(); 133 virtual void AllDetached(); 134 135 protected: 136 137 BMenu( BRect frame, 138 const char *viewName, 139 uint32 resizeMask, 140 uint32 flags, 141 menu_layout layout, 142 bool resizeToFit); 143 144 virtual BPoint ScreenLocation(); 145 146 void SetItemMargins( float left, 147 float top, 148 float right, 149 float bottom); 150 void GetItemMargins( float *left, 151 float *top, 152 float *right, 153 float *bottom) const; 154 155 menu_layout Layout() const; 156 157 virtual void Show(); 158 void Show(bool selectFirstItem); 159 void Hide(); 160 BMenuItem *Track( bool start_opened = false, 161 BRect *special_rect = NULL); 162 163 public: 164 enum add_state { 165 B_INITIAL_ADD, 166 B_PROCESSING, 167 B_ABORT 168 }; 169 virtual bool AddDynamicItem(add_state s); 170 virtual void DrawBackground(BRect update); 171 172 void SetTrackingHook(menu_tracking_hook func, void *state); 173 174 /*----- Private or reserved -----------------------------------------*/ 175 private: 176 friend class BWindow; 177 friend class BMenuBar; 178 friend class BMenuItem; 179 friend status_t _init_interface_kit_(); 180 friend status_t set_menu_info(menu_info *); 181 friend status_t get_menu_info(menu_info *); 182 183 virtual void _ReservedMenu3(); 184 virtual void _ReservedMenu4(); 185 virtual void _ReservedMenu5(); 186 virtual void _ReservedMenu6(); 187 188 BMenu &operator=(const BMenu &); 189 190 void InitData(BMessage *data = NULL); 191 bool _show(bool selectFirstItem = false); 192 void _hide(); 193 BMenuItem *_track(int *action, long start = -1); 194 195 void _UpdateStateOpenSelect(BMenuItem *item, bigtime_t &openTime, bigtime_t &closeTime); 196 void _UpdateStateClose(BMenuItem *item, const BPoint &where, const uint32 &buttons); 197 198 bool _AddItem(BMenuItem *item, int32 index); 199 bool RemoveItems(int32 index, 200 int32 count, 201 BMenuItem *item, 202 bool del = false); 203 bool RelayoutIfNeeded(); 204 void LayoutItems(int32 index); 205 void ComputeLayout(int32 index, bool bestFit, bool moveItems, 206 float* width, float* height); 207 BRect Bump(BRect current, BPoint extent, int32 index) const; 208 BPoint ItemLocInRect(BRect frame) const; 209 BRect CalcFrame(BPoint where, bool *scrollOn); 210 bool ScrollMenu(BRect bounds, BPoint loc, bool *fast); 211 void ScrollIntoView(BMenuItem *item); 212 213 void DrawItems(BRect updateRect); 214 int State(BMenuItem **item = NULL) const; 215 void InvokeItem(BMenuItem *item, bool now = false); 216 217 bool OverSuper(BPoint loc); 218 bool OverSubmenu(BMenuItem *item, BPoint loc); 219 BPrivate::BMenuWindow* MenuWindow(); 220 void DeleteMenuWindow(); 221 BMenuItem *HitTestItems(BPoint where, BPoint slop = B_ORIGIN) const; 222 BRect Superbounds() const; 223 void CacheFontInfo(); 224 225 void ItemMarked(BMenuItem *item); 226 void Install(BWindow *target); 227 void Uninstall(); 228 void _SelectItem(BMenuItem* item, bool showSubmenu = true, 229 bool selectFirstItem = false); 230 BMenuItem *CurrentSelection() const; 231 bool SelectNextItem(BMenuItem *item, bool forward); 232 BMenuItem *NextItem(BMenuItem *item, bool forward) const; 233 bool IsItemVisible(BMenuItem *item) const; 234 void SetIgnoreHidden(bool on); 235 void SetStickyMode(bool on); 236 bool IsStickyMode() const; 237 void CalcTriggers(); 238 const char *ChooseTrigger(const char *title, BList *chars); 239 void UpdateWindowViewSize(bool upWind = true); 240 bool IsStickyPrefOn(); 241 void RedrawAfterSticky(BRect bounds); 242 bool OkToProceed(BMenuItem *); 243 244 bool CustomTrackingWantsToQuit(); 245 246 void QuitTracking(); 247 248 249 status_t ParseMsg(BMessage *msg, int32 *sindex, BMessage *spec, 250 int32 *form, const char **prop, 251 BMenu **tmenu, BMenuItem **titem, int32 *user_data, 252 BMessage *reply) const; 253 254 status_t DoMenuMsg(BMenuItem **next, BMenu *tar, BMessage *m, 255 BMessage *r, BMessage *spec, int32 f) const; 256 status_t DoMenuItemMsg(BMenuItem **next, BMenu *tar, BMessage *m, 257 BMessage *r, BMessage *spec, int32 f) const; 258 259 status_t DoEnabledMsg(BMenuItem *ti, BMenu *tm, BMessage *m, 260 BMessage *r) const; 261 status_t DoLabelMsg(BMenuItem *ti, BMenu *tm, BMessage *m, 262 BMessage *r) const; 263 status_t DoMarkMsg(BMenuItem *ti, BMenu *tm, BMessage *m, 264 BMessage *r) const; 265 status_t DoDeleteMsg(BMenuItem *ti, BMenu *tm, BMessage *m, 266 BMessage *r) const; 267 status_t DoCreateMsg(BMenuItem *ti, BMenu *tm, BMessage *m, 268 BMessage *r, bool menu) const; 269 270 static menu_info sMenuInfo; 271 static bool sAltAsCommandKey; 272 273 BMenuItem *fChosenItem; 274 BList fItems; 275 BRect fPad; 276 BMenuItem *fSelected; 277 BPrivate::BMenuWindow* fCachedMenuWindow; 278 BMenu *fSuper; 279 BMenuItem *fSuperitem; 280 BRect fSuperbounds; 281 float fAscent; 282 float fDescent; 283 float fFontHeight; 284 uint32 fState; 285 menu_layout fLayout; 286 BRect *fExtraRect; 287 float fMaxContentWidth; 288 BPoint *fInitMatrixSize; 289 _ExtraMenuData_ *fExtraMenuData; 290 291 uint32 _reserved[2]; 292 293 char fTrigger; 294 bool fResizeToFit; 295 bool fUseCachedMenuLayout; 296 bool fEnabled; 297 bool fDynamicName; 298 bool fRadioMode; 299 bool fTrackNewBounds; 300 bool fStickyMode; 301 bool fIgnoreHidden; 302 bool fTriggerEnabled; 303 bool fRedrawAfterSticky; 304 bool fAttachAborted; 305 }; 306 307 /*-------------------------------------------------------------*/ 308 /*-------------------------------------------------------------*/ 309 310 #endif /* _MENU_H */ 311