1 /*
2 * Copyright 2007, Ingo Weinhold <bonefish@cs.tu-berlin.de>.
3 * All rights reserved. Distributed under the terms of the MIT License.
4 */
5
6 #include "MenuFieldTest.h"
7
8 #include <stdio.h>
9
10 #include <Menu.h>
11 #include <MenuField.h>
12 #include <MenuItem.h>
13
14 #include "CheckBox.h"
15 #include "GroupView.h"
16
17
18 enum {
19 MSG_CHANGE_LABEL_TEXT = 'chlt',
20 MSG_CHANGE_LABEL_FONT = 'chlf',
21 MSG_CHANGE_MENU_TEXT = 'chmt'
22 };
23
24
25 // constructor
MenuFieldTest()26 MenuFieldTest::MenuFieldTest()
27 : ControlTest("MenuField"),
28 fLongLabelTextCheckBox(NULL),
29 fLongMenuTextCheckBox(NULL),
30 fBigFontCheckBox(NULL),
31 fDefaultFont(NULL),
32 fBigFont(NULL)
33 {
34 BMenu* menu = new BMenu("The Menu");
35
36 // add a few items
37 for (int32 i = 0; i < 10; i++) {
38 BString itemText("Menu item ");
39 itemText << i;
40 menu->AddItem(new BMenuItem(itemText.String(), NULL));
41 }
42
43 fMenuField = new BMenuField("", menu);
44
45 SetView(fMenuField);
46 }
47
48
49 // destructor
~MenuFieldTest()50 MenuFieldTest::~MenuFieldTest()
51 {
52 delete fDefaultFont;
53 delete fBigFont;
54 }
55
56
57 // CreateTest
58 Test*
CreateTest()59 MenuFieldTest::CreateTest()
60 {
61 return new MenuFieldTest;
62 }
63
64
65 // ActivateTest
66 void
ActivateTest(View * controls)67 MenuFieldTest::ActivateTest(View* controls)
68 {
69 GroupView* group = new GroupView(B_VERTICAL);
70 group->SetFrame(controls->Bounds());
71 group->SetSpacing(0, 8);
72 controls->AddChild(group);
73
74 // BMenuField sets its background color to that of its parent in
75 // AttachedToWindow(). Override.
76 rgb_color background = ui_color(B_PANEL_BACKGROUND_COLOR);
77 fMenuField->SetViewColor(background);
78 fMenuField->SetLowColor(background);
79
80 // long label text
81 fLongLabelTextCheckBox = new LabeledCheckBox("Long label text",
82 new BMessage(MSG_CHANGE_LABEL_TEXT), this);
83 group->AddChild(fLongLabelTextCheckBox);
84
85 // long menu text
86 fLongMenuTextCheckBox = new LabeledCheckBox("Long menu text",
87 new BMessage(MSG_CHANGE_MENU_TEXT), this);
88 group->AddChild(fLongMenuTextCheckBox);
89
90 // big font
91 fBigFontCheckBox = new LabeledCheckBox("Big label font",
92 new BMessage(MSG_CHANGE_LABEL_FONT), this);
93 group->AddChild(fBigFontCheckBox);
94
95 _UpdateLabelText();
96 _UpdateLabelFont();
97
98 group->AddChild(new Glue());
99 }
100
101
102 // DectivateTest
103 void
DectivateTest()104 MenuFieldTest::DectivateTest()
105 {
106 }
107
108
109 // MessageReceived
110 void
MessageReceived(BMessage * message)111 MenuFieldTest::MessageReceived(BMessage* message)
112 {
113 switch (message->what) {
114 case MSG_CHANGE_LABEL_TEXT:
115 _UpdateLabelText();
116 break;
117 case MSG_CHANGE_MENU_TEXT:
118 _UpdateMenuText();
119 break;
120 case MSG_CHANGE_LABEL_FONT:
121 _UpdateLabelFont();
122 break;
123 default:
124 Test::MessageReceived(message);
125 break;
126 }
127 }
128
129
130 // _UpdateLabelText
131 void
_UpdateLabelText()132 MenuFieldTest::_UpdateLabelText()
133 {
134 if (!fLongLabelTextCheckBox || !fMenuField)
135 return;
136
137 fMenuField->SetLabel(fLongLabelTextCheckBox->IsSelected()
138 ? "Pretty long menu field label"
139 : "Short label");
140 }
141
142
143 // _UpdateMenuText
144 void
_UpdateMenuText()145 MenuFieldTest::_UpdateMenuText()
146 {
147 if (!fLongMenuTextCheckBox || !fMenuField)
148 return;
149
150 fMenuField->Menu()->Superitem()->SetLabel(
151 fLongMenuTextCheckBox->IsSelected()
152 ? "Pretty long menu field text"
153 : "Short text");
154 }
155
156
157 // _UpdateLabelFont
158 void
_UpdateLabelFont()159 MenuFieldTest::_UpdateLabelFont()
160 {
161 if (!fBigFontCheckBox || !fMenuField || !fMenuField->Window())
162 return;
163
164 // get default font lazily
165 if (!fDefaultFont) {
166 fDefaultFont = new BFont;
167 fMenuField->GetFont(fDefaultFont);
168
169 fBigFont = new BFont(fDefaultFont);
170 fBigFont->SetSize(20);
171 }
172
173 // set font
174 fMenuField->SetFont(fBigFontCheckBox->IsSelected()
175 ? fBigFont : fDefaultFont);
176 fMenuField->InvalidateLayout();
177 fMenuField->Invalidate();
178 }
179