1/* 2 * Copyright 2009, Oliver Tappe, zooey@hirschkaefer.de. All rights reserved. 3 * Distributed under the terms of the MIT License. 4 */ 5 6 7#include <Application.h> 8#include <Button.h> 9#include <ControlLook.h> 10#include <GroupLayout.h> 11#include <GroupLayoutBuilder.h> 12#include <ScrollView.h> 13#include <String.h> 14#include <TextControl.h> 15#include <TextView.h> 16#include <Window.h> 17 18#include <stdio.h> 19 20 21const static uint32 kMsgAlignLeft = 'alle'; 22const static uint32 kMsgAlignCenter = 'alce'; 23const static uint32 kMsgAlignRight = 'alri'; 24 25 26class Window : public BWindow { 27 public: 28 Window(); 29 30 virtual bool QuitRequested(); 31 virtual void MessageReceived(BMessage *message); 32 33 private: 34 BTextControl* fTextControl; 35 BTextView* fTextView; 36}; 37 38 39// #pragma mark - 40 41 42Window::Window() 43 : BWindow(BRect(100, 100, 800, 500), "TextView-Test", 44 B_TITLED_WINDOW, B_ASYNCHRONOUS_CONTROLS) 45{ 46 fTextControl = new BTextControl("text-contr-O", 47 "a single line of text - (c) Conglom-O", NULL); 48 49 BMessage storage; 50 fTextControl->Archive(&storage); 51 52 BTextControl* unarchived = new BTextControl(&storage); 53 54 BTextControl* disabled = new BTextControl("disabled", 55 "I'm disabled: you can't edit me", NULL); 56 disabled->SetEnabled(false); 57 BTextControl* invalid = new BTextControl("invalid", 58 "I'm invalid: my border is red", NULL); 59 invalid->MarkAsInvalid(true); 60 61 fTextView = new BTextView("text-O"); 62 BScrollView* scrollView = new BScrollView("scroll-O", fTextView, 0, true, 63 true, B_FANCY_BORDER); 64 65 SetLayout(new BGroupLayout(B_HORIZONTAL)); 66 AddChild(BGroupLayoutBuilder(B_VERTICAL, 10) 67 .Add(fTextControl) 68 .Add(unarchived) 69 .Add(disabled) 70 .Add(invalid) 71 .Add(scrollView) 72 .Add(BGroupLayoutBuilder(B_HORIZONTAL, 10) 73 .Add(new BButton("Align Left", new BMessage(kMsgAlignLeft))) 74 .AddGlue() 75 .Add(new BButton("Align Center", new BMessage(kMsgAlignCenter))) 76 .AddGlue() 77 .Add(new BButton("Align Right", new BMessage(kMsgAlignRight))) 78 ) 79 .SetInsets(5, 5, 5, 5) 80 ); 81 82 // generate some lines of content 83 const int32 kLineCount = 10; 84 const int32 kLineNoSize = 6; 85 BString line = ": just some text here - nothing special to see\n"; 86 BString format = BString("%*d") << line; 87 BString content; 88 int32 lineLength = line.Length() + kLineNoSize; 89 int32 contentLength = lineLength * kLineCount; 90 char* currLine = content.LockBuffer(contentLength); 91 if (currLine) { 92 int32 lineNo = 0; 93 for ( ; lineNo < kLineCount; currLine += lineLength) 94 sprintf(currLine, format.String(), kLineNoSize, lineNo++); 95 content.UnlockBuffer(contentLength); 96 } 97 fTextView->SetInsets(2,2,2,2); 98 fTextView->SetText(content.String()); 99} 100 101 102bool 103Window::QuitRequested() 104{ 105 be_app->PostMessage(B_QUIT_REQUESTED); 106 return true; 107} 108 109 110void 111Window::MessageReceived(BMessage *message) 112{ 113 switch (message->what) { 114 case kMsgAlignLeft: 115 fTextControl->SetAlignment(B_ALIGN_LEFT, B_ALIGN_LEFT); 116 fTextView->SetAlignment(B_ALIGN_LEFT); 117 break; 118 119 case kMsgAlignCenter: 120 fTextControl->SetAlignment(B_ALIGN_LEFT, B_ALIGN_CENTER); 121 fTextView->SetAlignment(B_ALIGN_CENTER); 122 break; 123 124 case kMsgAlignRight: 125 fTextControl->SetAlignment(B_ALIGN_LEFT, B_ALIGN_RIGHT); 126 fTextView->SetAlignment(B_ALIGN_RIGHT); 127 break; 128 129 default: 130 BWindow::MessageReceived(message); 131 break; 132 } 133} 134 135 136// #pragma mark - 137 138 139class Application : public BApplication { 140 public: 141 Application(); 142 143 virtual void ReadyToRun(void); 144}; 145 146 147Application::Application() 148 : BApplication("application/x-vnd.haiku-test") 149{ 150} 151 152 153void 154Application::ReadyToRun(void) 155{ 156 BWindow *window = new Window(); 157 window->Show(); 158} 159 160 161// #pragma mark - 162 163 164int 165main(int argc, char **argv) 166{ 167 Application app; 168 169 app.Run(); 170 return 0; 171} 172 173