xref: /haiku/src/tests/kits/interface/layout/widget_layout_test/tests/ControlTest.cpp (revision aa9303f41ec73feef2a0b0d0e95b037427761c08)
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 "ControlTest.h"
7 
8 #include <stdio.h>
9 
10 #include <Control.h>
11 #include <Font.h>
12 #include <Message.h>
13 
14 #include "CheckBox.h"
15 #include "GroupView.h"
16 #include "StringView.h"
17 
18 
19 
20 // constructor
ControlTest(const char * name)21 ControlTest::ControlTest(const char* name)
22 	: Test(name, NULL),
23 	  fControl(NULL),
24 	  fLongTextCheckBox(NULL),
25 	  fBigFontCheckBox(NULL),
26 	  fDefaultFont(NULL),
27 	  fBigFont(NULL)
28 {
29 }
30 
31 
32 // destructor
~ControlTest()33 ControlTest::~ControlTest()
34 {
35 	delete fDefaultFont;
36 	delete fBigFont;
37 }
38 
39 
40 // SetView
41 void
SetView(BView * view)42 ControlTest::SetView(BView* view)
43 {
44 	Test::SetView(view);
45 	fControl = dynamic_cast<BControl*>(view);
46 }
47 
48 
49 // ActivateTest
50 void
ActivateTest(View * controls)51 ControlTest::ActivateTest(View* controls)
52 {
53 	GroupView* group = new GroupView(B_VERTICAL);
54 	group->SetFrame(controls->Bounds());
55 	group->SetSpacing(0, 8);
56 	controls->AddChild(group);
57 
58 	// long text
59 	fLongTextCheckBox = new LabeledCheckBox("Long label text",
60 		new BMessage(MSG_CHANGE_CONTROL_TEXT), this);
61 	group->AddChild(fLongTextCheckBox);
62 
63 	// big font
64 	fBigFontCheckBox = new LabeledCheckBox("Big label font",
65 		new BMessage(MSG_CHANGE_CONTROL_FONT), this);
66 	group->AddChild(fBigFontCheckBox);
67 
68 	UpdateControlText();
69 	UpdateControlFont();
70 }
71 
72 
73 // DectivateTest
74 void
DectivateTest()75 ControlTest::DectivateTest()
76 {
77 }
78 
79 
80 // MessageReceived
81 void
MessageReceived(BMessage * message)82 ControlTest::MessageReceived(BMessage* message)
83 {
84 	switch (message->what) {
85 		case MSG_CHANGE_CONTROL_TEXT:
86 			UpdateControlText();
87 			break;
88 		case MSG_CHANGE_CONTROL_FONT:
89 			UpdateControlFont();
90 			break;
91 		default:
92 			Test::MessageReceived(message);
93 			break;
94 	}
95 }
96 
97 
98 // UpdateControlText
99 void
UpdateControlText()100 ControlTest::UpdateControlText()
101 {
102 	if (!fLongTextCheckBox || !fControl)
103 		return;
104 
105 	fControl->SetLabel(fLongTextCheckBox->IsSelected()
106 		? "Very long label for a simple control"
107 		: "Short label");
108 }
109 
110 
111 // UpdateControlFont
112 void
UpdateControlFont()113 ControlTest::UpdateControlFont()
114 {
115 	if (!fBigFontCheckBox || !fControl || !fControl->Window())
116 		return;
117 
118 	// get default font lazily
119 	if (!fDefaultFont) {
120 		fDefaultFont = new BFont;
121 		fControl->GetFont(fDefaultFont);
122 
123 		fBigFont = new BFont(fDefaultFont);
124 		fBigFont->SetSize(20);
125 	}
126 
127 	// set font
128 	fControl->SetFont(fBigFontCheckBox->IsSelected()
129 		? fBigFont : fDefaultFont);
130 	fControl->InvalidateLayout();
131 	fControl->Invalidate();
132 }
133