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