1 /*
2 * Copyright 2008, Stephan Aßmus <superstippi@gmx.de>.
3 * Copyright 2007, Ingo Weinhold <bonefish@cs.tu-berlin.de>.
4 * All rights reserved. Distributed under the terms of the MIT License.
5 */
6
7 #include "TextControlTest.h"
8
9 #include <stdio.h>
10
11 #include <TextControl.h>
12
13 #include "CheckBox.h"
14 #include "GroupView.h"
15
16
17 enum {
18 MSG_CHANGE_LABEL_TEXT = 'chlt',
19 MSG_CHANGE_LABEL_FONT = 'chlf',
20 MSG_CHANGE_INVALID = 'chiv'
21 };
22
23
24 // constructor
TextControlTest()25 TextControlTest::TextControlTest()
26 :
27 ControlTest("TextControl"),
28 fLongTextCheckBox(NULL),
29 fBigFontCheckBox(NULL),
30 fInvalidCheckBox(NULL),
31 fDefaultFont(NULL),
32 fBigFont(NULL)
33 {
34 fTextControl = new BTextControl("Label", "Some Text", NULL);
35
36 SetView(fTextControl);
37 }
38
39
40 // destructor
~TextControlTest()41 TextControlTest::~TextControlTest()
42 {
43 delete fDefaultFont;
44 delete fBigFont;
45 }
46
47
48 // CreateTest
49 Test*
CreateTest()50 TextControlTest::CreateTest()
51 {
52 return new TextControlTest;
53 }
54
55
56 // ActivateTest
57 void
ActivateTest(View * controls)58 TextControlTest::ActivateTest(View* controls)
59 {
60 GroupView* group = new GroupView(B_VERTICAL);
61 group->SetFrame(controls->Bounds());
62 group->SetSpacing(0, 8);
63 controls->AddChild(group);
64
65 // BMenuField sets its background color to that of its parent in
66 // AttachedToWindow(). Override.
67 fTextControl->SetViewUIColor(B_PANEL_BACKGROUND_COLOR);
68 fTextControl->SetLowUIColor(B_PANEL_BACKGROUND_COLOR);
69
70 // long text
71 fLongTextCheckBox = new LabeledCheckBox("Long label text",
72 new BMessage(MSG_CHANGE_LABEL_TEXT), this);
73 group->AddChild(fLongTextCheckBox);
74
75 // big font
76 fBigFontCheckBox = new LabeledCheckBox("Big label font",
77 new BMessage(MSG_CHANGE_LABEL_FONT), this);
78 group->AddChild(fBigFontCheckBox);
79
80 fInvalidCheckBox = new LabeledCheckBox("Invalid Input", new BMessage(MSG_CHANGE_INVALID), this);
81 group->AddChild(fInvalidCheckBox);
82
83 _UpdateLabelText();
84 _UpdateLabelFont();
85
86 group->AddChild(new Glue());
87 }
88
89
90 // DectivateTest
91 void
DectivateTest()92 TextControlTest::DectivateTest()
93 {
94 }
95
96
97 // MessageReceived
98 void
MessageReceived(BMessage * message)99 TextControlTest::MessageReceived(BMessage* message)
100 {
101 switch (message->what) {
102 case MSG_CHANGE_LABEL_TEXT:
103 _UpdateLabelText();
104 break;
105 case MSG_CHANGE_LABEL_FONT:
106 _UpdateLabelFont();
107 break;
108 case MSG_CHANGE_INVALID:
109 _UpdateInvalid();
110 break;
111 default:
112 Test::MessageReceived(message);
113 break;
114 }
115 }
116
117
118 void
_UpdateInvalid()119 TextControlTest::_UpdateInvalid()
120 {
121 if (!fInvalidCheckBox)
122 return;
123
124 if (fInvalidCheckBox->IsSelected())
125 fTextControl->MarkAsInvalid(true);
126 else
127 fTextControl->MarkAsInvalid(false);
128 }
129
130
131 // _UpdateLabelText
132 void
_UpdateLabelText()133 TextControlTest::_UpdateLabelText()
134 {
135 if (!fLongTextCheckBox || !fTextControl)
136 return;
137
138 fTextControl->SetLabel(fLongTextCheckBox->IsSelected()
139 ? "Pretty long text control label"
140 : "Short label");
141 }
142
143
144 // _UpdateLabelFont
145 void
_UpdateLabelFont()146 TextControlTest::_UpdateLabelFont()
147 {
148 if (!fBigFontCheckBox || !fTextControl || !fTextControl->Window())
149 return;
150
151 // get default font lazily
152 if (!fDefaultFont) {
153 fDefaultFont = new BFont;
154 fTextControl->GetFont(fDefaultFont);
155
156 fBigFont = new BFont(fDefaultFont);
157 fBigFont->SetSize(20);
158 }
159
160 // set font
161 fTextControl->SetFont(fBigFontCheckBox->IsSelected()
162 ? fBigFont : fDefaultFont);
163 fTextControl->InvalidateLayout();
164 fTextControl->Invalidate();
165 }
166