1/*
2 * Copyright 2007-2008, Christof Lutteroth, lutteroth@cs.auckland.ac.nz
3 * Copyright 2007-2008, James Kim, jkim202@ec.auckland.ac.nz
4 * Copyright 2010, Clemens Zeidler <haiku@clemens-zeidler.de>
5 * Distributed under the terms of the MIT License.
6 */
7
8#include <Application.h>
9#include <Button.h>
10#include <GroupLayout.h>
11#include <List.h>
12#include <StringView.h>
13#include <Window.h>
14
15// include this for ALM
16#include "ALMLayout.h"
17
18
19const uint32 kMsgClone = 'clne';
20
21
22class HelloWorldWindow : public BWindow {
23public:
24	HelloWorldWindow(BRect frame)
25		:
26		BWindow(frame, "ALM Hello World", B_TITLED_WINDOW,
27			B_QUIT_ON_WINDOW_CLOSE)
28	{
29		button1 = new BButton("Hello World!");
30
31		// create a new BALMLayout and use  it for this window
32		fLayout = new BALMLayout();
33		BView* view = new BView("alm view", 0, fLayout);
34		fLayout->SetExplicitAlignment(BAlignment(B_ALIGN_USE_FULL_WIDTH,
35			B_ALIGN_USE_FULL_HEIGHT));
36		SetLayout(new BGroupLayout(B_VERTICAL));
37		AddChild(view);
38
39		// add an area containing the button
40		// use the borders of the layout as the borders for the area
41		fLayout->AddView(button1, fLayout->Left(), fLayout->Top(),
42			fLayout->Right(), fLayout->Bottom());
43		button1->SetExplicitMinSize(BSize(0, 50));
44		button1->SetExplicitMaxSize(BSize(500, 500));
45		button1->SetExplicitAlignment(BAlignment(B_ALIGN_USE_FULL_WIDTH,
46			B_ALIGN_USE_FULL_HEIGHT));
47
48		// test size limits
49		BSize min = fLayout->MinSize();
50		BSize max = fLayout->MaxSize();
51		SetSizeLimits(min.Width(), max.Width(), min.Height(), max.Height());
52
53		AddShortcut('c', B_COMMAND_KEY, new BMessage(kMsgClone));
54	}
55
56	void MessageReceived(BMessage* message)
57	{
58		switch (message->what) {
59			case kMsgClone:
60			{
61				BView* view = fLayout->View();
62				BMessage archive;
63				view->Archive(&archive, true);
64				BWindow* window = new BWindow(BRect(30, 30, 100, 100),
65					"ALM HelloWorld Clone", B_TITLED_WINDOW,
66					B_QUIT_ON_WINDOW_CLOSE | B_AUTO_UPDATE_SIZE_LIMITS);
67				window->SetLayout(new BGroupLayout(B_VERTICAL));
68				BView* clone;
69				status_t err = BUnarchiver::InstantiateObject(&archive, clone);
70				if (err != B_OK)
71					window->AddChild(new BStringView("", "An error occurred!"));
72				else
73					window->AddChild(clone);
74				window->Show();
75
76				break;
77			}
78			default:
79				BWindow::MessageReceived(message);
80		}
81	}
82
83private:
84
85	BALMLayout* fLayout;
86	BButton* button1;
87};
88
89
90class HelloWorld : public BApplication {
91public:
92	HelloWorld()
93		:
94		BApplication("application/x-vnd.haiku.HelloWorld")
95	{
96		BRect frameRect;
97		frameRect.Set(100, 100, 300, 300);
98		HelloWorldWindow* window = new HelloWorldWindow(frameRect);
99		window->Show();
100	}
101};
102
103
104int
105main()
106{
107	HelloWorld app;
108	app.Run();
109	return 0;
110}
111