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, 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 void LayoutItems(int32 index); 198 void ComputeLayout(int32 index, bool bestFit, bool moveItems, 199 float* width, float* height); 200 BRect Bump(BRect current, BPoint extent, int32 index) const; 201 BPoint ItemLocInRect(BRect frame) const; 202 BRect CalcFrame(BPoint where, bool *scrollOn); 203 bool ScrollMenu(BRect bounds, BPoint loc, bool *fast); 204 void ScrollIntoView(BMenuItem *item); 205 206 void DrawItems(BRect updateRect); 207 int State(BMenuItem **item = NULL) const; 208 void InvokeItem(BMenuItem *item, bool now = false); 209 210 bool OverSuper(BPoint loc); 211 bool OverSubmenu(BMenuItem *item, BPoint loc); 212 BMenuWindow *MenuWindow(); 213 void DeleteMenuWindow(); 214 BMenuItem *HitTestItems(BPoint where, BPoint slop = B_ORIGIN) const; 215 BRect Superbounds() const; 216 void CacheFontInfo(); 217 218 void ItemMarked(BMenuItem *item); 219 void Install(BWindow *target); 220 void Uninstall(); 221 void SelectItem( BMenuItem *m, 222 uint32 showSubmenu = 0, 223 bool selectFirstItem = false); 224 BMenuItem *CurrentSelection() const; 225 bool SelectNextItem(BMenuItem *item, bool forward); 226 BMenuItem *NextItem(BMenuItem *item, bool forward) const; 227 bool IsItemVisible(BMenuItem *item) const; 228 void SetIgnoreHidden(bool on); 229 void SetStickyMode(bool on); 230 bool IsStickyMode() const; 231 void CalcTriggers(); 232 const char *ChooseTrigger(const char *title, BList *chars); 233 void UpdateWindowViewSize(bool upWind = true); 234 bool IsStickyPrefOn(); 235 void RedrawAfterSticky(BRect bounds); 236 bool OkToProceed(BMenuItem *); 237 238 status_t ParseMsg(BMessage *msg, int32 *sindex, BMessage *spec, 239 int32 *form, const char **prop, 240 BMenu **tmenu, BMenuItem **titem, int32 *user_data, 241 BMessage *reply) const; 242 243 status_t DoMenuMsg(BMenuItem **next, BMenu *tar, BMessage *m, 244 BMessage *r, BMessage *spec, int32 f) const; 245 status_t DoMenuItemMsg(BMenuItem **next, BMenu *tar, BMessage *m, 246 BMessage *r, BMessage *spec, int32 f) const; 247 248 status_t DoEnabledMsg(BMenuItem *ti, BMenu *tm, BMessage *m, 249 BMessage *r) const; 250 status_t DoLabelMsg(BMenuItem *ti, BMenu *tm, BMessage *m, 251 BMessage *r) const; 252 status_t DoMarkMsg(BMenuItem *ti, BMenu *tm, BMessage *m, 253 BMessage *r) const; 254 status_t DoDeleteMsg(BMenuItem *ti, BMenu *tm, BMessage *m, 255 BMessage *r) const; 256 status_t DoCreateMsg(BMenuItem *ti, BMenu *tm, BMessage *m, 257 BMessage *r, bool menu) const; 258 259 static menu_info sMenuInfo; 260 static bool sSwapped; 261 262 BMenuItem *fChosenItem; 263 BList fItems; 264 BRect fPad; 265 BMenuItem *fSelected; 266 BMenuWindow *fCachedMenuWindow; 267 BMenu *fSuper; 268 BMenuItem *fSuperitem; 269 BRect fSuperbounds; 270 float fAscent; 271 float fDescent; 272 float fFontHeight; 273 uint32 fState; 274 menu_layout fLayout; 275 BRect *fExtraRect; 276 float fMaxContentWidth; 277 BPoint *fInitMatrixSize; 278 _ExtraMenuData_ *fExtraMenuData; // !! 279 280 uint32 _reserved[2]; 281 282 char fTrigger; 283 bool fResizeToFit; 284 bool fUseCachedMenuLayout; 285 bool fEnabled; 286 bool fDynamicName; 287 bool fRadioMode; 288 bool fTrackNewBounds; 289 bool fStickyMode; 290 bool fIgnoreHidden; 291 bool fTriggerEnabled; 292 bool fRedrawAfterSticky; 293 bool fAttachAborted; 294 }; 295 296 /*-------------------------------------------------------------*/ 297 /*-------------------------------------------------------------*/ 298 299 #endif /* _MENU_H */ 300