summaryrefslogtreecommitdiff
path: root/gui/menus.inc
blob: 511f790db97c0bc0cbc4817686b17fc1795dc75c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
{
    fpGUI  -  Free Pascal GUI Library
    
    Menu class declarations
    
    Copyright (C) 2000 - 2006 See the file AUTHORS.txt, included in this
    distribution, for details of the copyright.

    See the file COPYING.modifiedLGPL, included in this distribution,
    for details about redistributing fpGUI.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
}

{%mainunit fpgui.pp}

{
  All menu and menu item implementations
}

{$IFDEF read_interface}

  TPopupMenu = class;
  TMenuBar = class;

  { TMenuItem }

  TMenuItem = class(TCustomPanel)
  private
    FHotKeyDef: string;
    FSeparator: boolean;
    FSubMenu: TPopupMenu;
    function    GetSubMenu: TPopupMenu;
    procedure   InternalShowPopupMenu;
  protected
    procedure   Paint(Canvas: TFCanvas); override;
    function    ProcessEvent(Event: TEventObj): Boolean; override;
    procedure   Click; override;
  public
    constructor Create(const pText: string; pOwner: TComponent); overload;
    destructor  Destroy; override;
    property    SubMenu: TPopupMenu read GetSubMenu;
  published
    property    Separator: boolean read FSeparator write FSeparator;
    property    HotKeyDef: string read FHotKeyDef write FHotKeyDef;
    property    Text;
    property    Visible;
    property    Enabled;
  end;
  
  
  { TPopupMenu }

  TPopupMenu = class(TPopupWindow)
  private
    FMenu: TMenuBar;
  public
    constructor Create(AOwner: TComponent); override;
    destructor  Destroy; override;
    function    AddMenu(const pTitle: string): TMenuItem;
    function    AddMenu(const pTitle: string; const pHotKeyDef: string; pHandlerProc: TNotifyEvent): TMenuItem;
  end;


  { TMenuBar }

  TMenuBar = class(TCustomBoxLayout)
  private
  protected
  public
    constructor Create(AOwner: TComponent); override;
    function    AddMenu(const pTitle: string): TMenuItem;
    function    AddMenu(const pTitle: string; const pHotKeyDef: string; pHandlerProc: TNotifyEvent): TMenuItem;
  published
  end;

{$ENDIF read_interface}



{$IFDEF read_implementation}

{ TMenuItem }

function TMenuItem.GetSubMenu: TPopupMenu;
begin
  if not Assigned(FSubMenu) then
    FSubMenu := TPopupMenu.Create(self);
  Result := FSubMenu;
end;

procedure TMenuItem.InternalShowPopupMenu;
begin
  if Assigned(FSubMenu) and FSubMenu.Visible then
  begin
    FSubMenu.Close;
    Exit; //==>
  end;

  if not Assigned(FSubMenu) then
  begin
    FSubMenu := TPopupMenu.Create(Self);
  end;

  FSubMenu.SetPosition(ClientToScreen(Point(0, Height)));
  FSubMenu.Show;
  FSubMenu.Wnd.SetMinMaxClientSize(MaxSize, MaxSize);
end;

procedure TMenuItem.Paint(Canvas: TFCanvas);
begin
  if (wsClicked in WidgetState) or (wsMouseInside in WidgetState) then
    FBevelStyle := bsRaised
//  else if (wsClicked in WidgetState) then
//    FBevelStyle := bsLowered
  else
    FBevelStyle := bsPlain;

  inherited Paint(Canvas);
end;

function TMenuItem.ProcessEvent(Event: TEventObj): Boolean;
begin
  {$IFDEF DEBUG}
  if Event.InheritsFrom(TMouseEnterEventObj) then
    writeln(Format('MouseEnter for %s:%s', [Text, Classname]))
  else if Event.InheritsFrom(TMouseLeaveEventObj) then
    writeln(Format('MouseLeave for %s:%s', [Text, Classname]));
  {$ENDIF}
  
  if Event.InheritsFrom(TMouseEnterEventObj) then
  begin
    Include(WidgetState, wsMouseInside);
    Redraw;
    result := True;
  end
  else if Event.InheritsFrom(TMouseLeaveEventObj) then
  begin
    Exclude(WidgetState, wsMouseInside);
    Redraw;
    result := True;
  end
  else
    result := inherited ProcessEvent(Event);
end;

procedure TMenuItem.Click;
begin
  if (wsMouseInside in WidgetState) and Assigned(FSubMenu) then
  begin
    InternalShowPopupMenu;
  end
  else
    inherited Click;

  if FindForm is TPopupMenu then
    TPopupMenu(FindForm).Close;
end;

constructor TMenuItem.Create(const pText: string; pOwner: TComponent);
begin
  inherited Create(pText, pOwner);
  WidgetStyle := WidgetStyle + [wsCaptureMouse, wsClickable, wsOpaque];
  FBevelStyle := bsPlain;
end;

destructor TMenuItem.Destroy;
begin
  if Assigned(FSubMenu) then
    FSubMenu.Free;
  inherited Destroy;
end;

{ TPopupMenu }

constructor TPopupMenu.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  WidgetStyle := WidgetStyle + [wsCaptureMouse, wsClickable, wsOpaque];
  BorderWidth := 1;
  Color       := clBlack;
  Name        := '#MenuPopup';

  FMenu := TMenuBar.Create(self);
  FMenu.Name        := '#VBoxMenu';
  FMenu.Orientation := Vertical;
  FMenu.Spacing     := 0;
  InsertChild(FMenu);
end;

destructor TPopupMenu.Destroy;
begin
  FMenu.Free;
  inherited Destroy;
end;

function TPopupMenu.AddMenu(const pTitle: string): TMenuItem;
begin
  Result := FMenu.AddMenu(pTitle);
end;

function TPopupMenu.AddMenu(const pTitle: string; const pHotKeyDef: string;
  pHandlerProc: TNotifyEvent): TMenuItem;
begin
  Result := FMenu.AddMenu(pTitle, photKeyDef, pHandlerProc);
end;

{ TMenuBar }

constructor TMenuBar.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  WidgetStyle := WidgetStyle + [wsCaptureMouse, wsClickable, wsOpaque];
  FCanExpandHeight := False;
  Spacing := 0;
end;

function TMenuBar.AddMenu(const pTitle: string): TMenuItem;
begin
  Result := TMenuItem.Create(pTitle, self);
  InsertChild(Result);
end;


function TMenuBar.AddMenu(const pTitle: string; const pHotKeyDef: string;
  pHandlerProc: TNotifyEvent): TMenuItem;
begin
  Result := AddMenu(pTitle);
  if pTitle <> '-' then
  begin
    Result.Text       := pTitle;
    Result.HotKeyDef  := pHotKeyDef;
    Result.OnClick    := pHandlerProc;
  end
  else
    Result.Separator  := True;
end;


{$ENDIF read_implementation}