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 17enum { 18 MSG_CHANGE_LABEL_TEXT = 'chlt', 19 MSG_CHANGE_LABEL_FONT = 'chlf' 20}; 21 22 23// constructor 24TextControlTest::TextControlTest() 25 : ControlTest("TextControl"), 26 fLongTextCheckBox(NULL), 27 fBigFontCheckBox(NULL), 28 fDefaultFont(NULL), 29 fBigFont(NULL) 30{ 31 fTextControl = new BTextControl("Label", "Some Text", NULL); 32 33 SetView(fTextControl); 34} 35 36 37// destructor 38TextControlTest::~TextControlTest() 39{ 40 delete fDefaultFont; 41 delete fBigFont; 42} 43 44 45// CreateTest 46Test* 47TextControlTest::CreateTest() 48{ 49 return new TextControlTest; 50} 51 52 53// ActivateTest 54void 55TextControlTest::ActivateTest(View* controls) 56{ 57 GroupView* group = new GroupView(B_VERTICAL); 58 group->SetFrame(controls->Bounds()); 59 group->SetSpacing(0, 8); 60 controls->AddChild(group); 61 62 // BMenuField sets its background color to that of its parent in 63 // AttachedToWindow(). Override. 64 rgb_color background = ui_color(B_PANEL_BACKGROUND_COLOR); 65 fTextControl->SetViewColor(background); 66 fTextControl->SetLowColor(background); 67 68 // long text 69 fLongTextCheckBox = new LabeledCheckBox("Long label text", 70 new BMessage(MSG_CHANGE_LABEL_TEXT), this); 71 group->AddChild(fLongTextCheckBox); 72 73 // big font 74 fBigFontCheckBox = new LabeledCheckBox("Big label font", 75 new BMessage(MSG_CHANGE_LABEL_FONT), this); 76 group->AddChild(fBigFontCheckBox); 77 78 _UpdateLabelText(); 79 _UpdateLabelFont(); 80 81 group->AddChild(new Glue()); 82} 83 84 85// DectivateTest 86void 87TextControlTest::DectivateTest() 88{ 89} 90 91 92// MessageReceived 93void 94TextControlTest::MessageReceived(BMessage* message) 95{ 96 switch (message->what) { 97 case MSG_CHANGE_LABEL_TEXT: 98 _UpdateLabelText(); 99 break; 100 case MSG_CHANGE_LABEL_FONT: 101 _UpdateLabelFont(); 102 break; 103 default: 104 Test::MessageReceived(message); 105 break; 106 } 107} 108 109 110// _UpdateLabelText 111void 112TextControlTest::_UpdateLabelText() 113{ 114 if (!fLongTextCheckBox || !fTextControl) 115 return; 116 117 fTextControl->SetLabel(fLongTextCheckBox->IsSelected() 118 ? "Pretty long text control label" 119 : "Short label"); 120} 121 122 123// _UpdateLabelFont 124void 125TextControlTest::_UpdateLabelFont() 126{ 127 if (!fBigFontCheckBox || !fTextControl || !fTextControl->Window()) 128 return; 129 130 // get default font lazily 131 if (!fDefaultFont) { 132 fDefaultFont = new BFont; 133 fTextControl->GetFont(fDefaultFont); 134 135 fBigFont = new BFont(fDefaultFont); 136 fBigFont->SetSize(20); 137 } 138 139 // set font 140 fTextControl->SetFont(fBigFontCheckBox->IsSelected() 141 ? fBigFont : fDefaultFont); 142 fTextControl->InvalidateLayout(); 143 fTextControl->Invalidate(); 144} 145