1// LAF OS Library
2// Copyright (C) 2020 Igara Studio S.A.
3// Copyright (C) 2017 David Capello
4//
5// This file is released under the terms of the MIT license.
6// Read LICENSE.txt for more information.
7
8#ifndef OS_MENUS_H_INCLUDED
9#define OS_MENUS_H_INCLUDED
10#pragma once
11
12#include "os/keys.h"
13#include "os/ref.h"
14#include "os/shortcut.h"
15
16#include <functional>
17#include <string>
18
19namespace os {
20 class MenuItem;
21
22 struct MenuItemInfo {
23 enum Type {
24 Normal,
25 Separator
26 };
27
28 enum Action {
29 UserDefined,
30
31 // macOS standard commands
32 Hide,
33 HideOthers,
34 ShowAll,
35 Quit,
36 Minimize,
37 Zoom,
38 };
39
40 Type type;
41 Action action;
42 std::string text;
43 Shortcut shortcut;
44 std::function<void()> execute;
45 std::function<void(os::MenuItem*)> validate;
46
47 explicit MenuItemInfo(const Type type = Normal,
48 const Action action = UserDefined)
49 : type(type)
50 , action(action) {
51 }
52
53 explicit MenuItemInfo(const std::string& text,
54 const Action action = UserDefined)
55 : type(Normal)
56 , action(action)
57 , text(text) {
58 }
59 };
60
61 class Menu;
62 class MenuItem;
63 class Menus;
64
65 using MenuItemRef = Ref<MenuItem>;
66 using MenuRef = Ref<Menu>;
67 using MenusRef = Ref<Menus>;
68
69 class MenuItem : public RefCount {
70 public:
71 virtual ~MenuItem() { }
72 virtual void setText(const std::string& text) = 0;
73 virtual void setSubmenu(const MenuRef& submenu) = 0;
74 virtual void setEnabled(bool state) = 0;
75 virtual void setChecked(bool state) = 0;
76 virtual void setShortcut(const Shortcut& shortcut) = 0;
77 };
78
79 class Menu : public RefCount {
80 public:
81 virtual ~Menu() { }
82 virtual void addItem(const MenuItemRef& item) = 0;
83 virtual void insertItem(const int index, const MenuItemRef& item) = 0;
84 virtual void removeItem(const MenuItemRef& item) = 0;
85 };
86
87 class Menus : public RefCount {
88 public:
89 virtual ~Menus() { }
90 virtual MenuRef makeMenu() = 0;
91 virtual MenuItemRef makeMenuItem(const MenuItemInfo& info) = 0;
92 virtual void setAppMenu(const MenuRef& menu) = 0;
93 };
94
95} // namespace os
96
97#endif
98