1d08caee0SIngo Weinhold/*
2d08caee0SIngo Weinhold * Copyright 2007, Ingo Weinhold <bonefish@cs.tu-berlin.de>.
3d08caee0SIngo Weinhold * All rights reserved. Distributed under the terms of the MIT License.
4d08caee0SIngo Weinhold */
5d08caee0SIngo Weinhold
6d08caee0SIngo Weinhold#include "MenuBarTest.h"
7d08caee0SIngo Weinhold
8d08caee0SIngo Weinhold#include <MenuBar.h>
9d08caee0SIngo Weinhold#include <MenuItem.h>
10d08caee0SIngo Weinhold#include <Message.h>
11d08caee0SIngo Weinhold
12d08caee0SIngo Weinhold#include "CheckBox.h"
13d08caee0SIngo Weinhold
14d08caee0SIngo Weinhold
15d08caee0SIngo Weinholdenum {
16d08caee0SIngo Weinhold	MSG_THIRD_ITEM			= '3rdi',
1786dab989SStephan Aßmus	MSG_CHILD_MENU			= 'chmn',
18d08caee0SIngo Weinhold	MSG_CHANGE_ITEM_TEXT	= 'chit'
19d08caee0SIngo Weinhold};
20d08caee0SIngo Weinhold
21d08caee0SIngo Weinhold
22d08caee0SIngo WeinholdMenuBarTest::MenuBarTest()
23d08caee0SIngo Weinhold	: Test("MenuBar", NULL),
24d08caee0SIngo Weinhold	  fMenuBar(new BMenuBar("The Menu"))
25d08caee0SIngo Weinhold
26d08caee0SIngo Weinhold{
27d08caee0SIngo Weinhold	SetView(fMenuBar);
28d08caee0SIngo Weinhold
29d08caee0SIngo Weinhold	// add a few items
30d08caee0SIngo Weinhold	fMenuBar->AddItem(fFirstItem = new BMenuItem("Menu item 1", NULL));
31d08caee0SIngo Weinhold	fMenuBar->AddItem(new BMenuItem("Menu item 2", NULL));
32d08caee0SIngo Weinhold	fThirdItem = new BMenuItem("Menu item 3", NULL);
3386dab989SStephan Aßmus	fChildMenu = new BMenu("Child menu");
34d08caee0SIngo Weinhold}
35d08caee0SIngo Weinhold
36d08caee0SIngo Weinhold
37d08caee0SIngo WeinholdTest*
38d08caee0SIngo WeinholdMenuBarTest::CreateTest()
39d08caee0SIngo Weinhold{
40d08caee0SIngo Weinhold	return new MenuBarTest;
41d08caee0SIngo Weinhold}
42d08caee0SIngo Weinhold
43d08caee0SIngo Weinhold
44d08caee0SIngo Weinhold// ActivateTest
45d08caee0SIngo Weinholdvoid
46d08caee0SIngo WeinholdMenuBarTest::ActivateTest(View* controls)
47d08caee0SIngo Weinhold{
48d08caee0SIngo Weinhold	GroupView* group = new GroupView(B_VERTICAL);
49d08caee0SIngo Weinhold	group->SetFrame(controls->Bounds());
50d08caee0SIngo Weinhold	group->SetSpacing(0, 8);
51d08caee0SIngo Weinhold	controls->AddChild(group);
52d08caee0SIngo Weinhold
53d08caee0SIngo Weinhold	// third item
54d08caee0SIngo Weinhold	fThirdItemCheckBox = new LabeledCheckBox("Third item",
55d08caee0SIngo Weinhold		new BMessage(MSG_THIRD_ITEM), this);
56d08caee0SIngo Weinhold	group->AddChild(fThirdItemCheckBox);
57d08caee0SIngo Weinhold
5886dab989SStephan Aßmus	// child menu
5986dab989SStephan Aßmus	fChildMenuCheckBox = new LabeledCheckBox("Child menu",
6086dab989SStephan Aßmus		new BMessage(MSG_CHILD_MENU), this);
6186dab989SStephan Aßmus	group->AddChild(fChildMenuCheckBox);
6286dab989SStephan Aßmus
63d08caee0SIngo Weinhold	// long text
64d08caee0SIngo Weinhold	fLongTextCheckBox = new LabeledCheckBox("Long label text",
65d08caee0SIngo Weinhold		new BMessage(MSG_CHANGE_ITEM_TEXT), this);
66d08caee0SIngo Weinhold	group->AddChild(fLongTextCheckBox);
67d08caee0SIngo Weinhold
68d08caee0SIngo Weinhold	group->AddChild(new Glue());
69d08caee0SIngo Weinhold
70d08caee0SIngo Weinhold	UpdateThirdItem();
71d08caee0SIngo Weinhold	UpdateLongText();
72d08caee0SIngo Weinhold}
73d08caee0SIngo Weinhold
74d08caee0SIngo Weinhold
75d08caee0SIngo Weinhold// DectivateTest
76d08caee0SIngo Weinholdvoid
77d08caee0SIngo WeinholdMenuBarTest::DectivateTest()
78d08caee0SIngo Weinhold{
79d08caee0SIngo Weinhold}
80d08caee0SIngo Weinhold
81d08caee0SIngo Weinhold
82d08caee0SIngo Weinhold// MessageReceived
83d08caee0SIngo Weinholdvoid
84d08caee0SIngo WeinholdMenuBarTest::MessageReceived(BMessage* message)
85d08caee0SIngo Weinhold{
86d08caee0SIngo Weinhold	switch (message->what) {
87d08caee0SIngo Weinhold		case MSG_THIRD_ITEM:
88d08caee0SIngo Weinhold			UpdateThirdItem();
89d08caee0SIngo Weinhold			break;
9086dab989SStephan Aßmus		case MSG_CHILD_MENU:
9186dab989SStephan Aßmus			UpdateChildMenu();
9286dab989SStephan Aßmus			break;
93d08caee0SIngo Weinhold		case MSG_CHANGE_ITEM_TEXT:
94d08caee0SIngo Weinhold			UpdateLongText();
95d08caee0SIngo Weinhold			break;
96d08caee0SIngo Weinhold		default:
97d08caee0SIngo Weinhold			Test::MessageReceived(message);
98d08caee0SIngo Weinhold			break;
99d08caee0SIngo Weinhold	}
100d08caee0SIngo Weinhold}
101d08caee0SIngo Weinhold
102d08caee0SIngo Weinhold
103d08caee0SIngo Weinhold// UpdateThirdItem
104d08caee0SIngo Weinholdvoid
105d08caee0SIngo WeinholdMenuBarTest::UpdateThirdItem()
106d08caee0SIngo Weinhold{
107d08caee0SIngo Weinhold	if (!fThirdItemCheckBox || !fMenuBar)
108d08caee0SIngo Weinhold		return;
109d08caee0SIngo Weinhold
110d08caee0SIngo Weinhold	if (fThirdItemCheckBox->IsSelected() == (fThirdItem->Menu() != NULL))
111d08caee0SIngo Weinhold		return;
112d08caee0SIngo Weinhold
113d08caee0SIngo Weinhold	if (fThirdItemCheckBox->IsSelected())
114d08caee0SIngo Weinhold		fMenuBar->AddItem(fThirdItem);
115d08caee0SIngo Weinhold	else
116d08caee0SIngo Weinhold		fMenuBar->RemoveItem(fThirdItem);
117d08caee0SIngo Weinhold}
118d08caee0SIngo Weinhold
119d08caee0SIngo Weinhold
12086dab989SStephan Aßmus// UpdateChildMenu
12186dab989SStephan Aßmusvoid
12286dab989SStephan AßmusMenuBarTest::UpdateChildMenu()
12386dab989SStephan Aßmus{
12486dab989SStephan Aßmus	if (!fChildMenuCheckBox || !fMenuBar)
12586dab989SStephan Aßmus		return;
12686dab989SStephan Aßmus
12786dab989SStephan Aßmus	if (fChildMenuCheckBox->IsSelected() == (fChildMenu->Supermenu() != NULL))
12886dab989SStephan Aßmus		return;
12986dab989SStephan Aßmus
13086dab989SStephan Aßmus	if (fChildMenuCheckBox->IsSelected())
13186dab989SStephan Aßmus		fMenuBar->AddItem(fChildMenu);
13286dab989SStephan Aßmus	else
13386dab989SStephan Aßmus		fMenuBar->RemoveItem(fChildMenu);
13486dab989SStephan Aßmus}
13586dab989SStephan Aßmus
13686dab989SStephan Aßmus
137d08caee0SIngo Weinhold// 	UpdateLongText
138d08caee0SIngo Weinholdvoid
139d08caee0SIngo WeinholdMenuBarTest::UpdateLongText()
140d08caee0SIngo Weinhold{
141d08caee0SIngo Weinhold	if (!fLongTextCheckBox || !fMenuBar)
142d08caee0SIngo Weinhold		return;
143d08caee0SIngo Weinhold
144d08caee0SIngo Weinhold	fFirstItem->SetLabel(fLongTextCheckBox->IsSelected()
145d08caee0SIngo Weinhold		? "Very long text for a menu item"
146d08caee0SIngo Weinhold		: "Menu item 1");
147d08caee0SIngo Weinhold}
148