18d9b6ef4SClemens Zeidler/*
28d9b6ef4SClemens Zeidler * Copyright 2007-2008, Christof Lutteroth, lutteroth@cs.auckland.ac.nz
38d9b6ef4SClemens Zeidler * Copyright 2007-2008, James Kim, jkim202@ec.auckland.ac.nz
48d9b6ef4SClemens Zeidler * Copyright 2010, Clemens Zeidler <haiku@clemens-zeidler.de>
58d9b6ef4SClemens Zeidler * Distributed under the terms of the MIT License.
68d9b6ef4SClemens Zeidler */
78d9b6ef4SClemens Zeidler
8ea607d6fSAxel Dörfler#include <Application.h>
9ea607d6fSAxel Dörfler#include <Button.h>
104199a81aSAlex Wilson#include <GroupLayout.h>
11ea607d6fSAxel Dörfler#include <List.h>
124199a81aSAlex Wilson#include <StringView.h>
13ea607d6fSAxel Dörfler#include <Window.h>
14ea607d6fSAxel Dörfler
15ea607d6fSAxel Dörfler// include this for ALM
168d9b6ef4SClemens Zeidler#include "ALMLayout.h"
17ea607d6fSAxel Dörfler
18ea607d6fSAxel Dörfler
194199a81aSAlex Wilsonconst uint32 kMsgClone = 'clne';
204199a81aSAlex Wilson
214199a81aSAlex Wilson
22ea607d6fSAxel Dörflerclass HelloWorldWindow : public BWindow {
23ea607d6fSAxel Dörflerpublic:
24ea607d6fSAxel Dörfler	HelloWorldWindow(BRect frame)
25f5dc380dSClemens Zeidler		:
26f5dc380dSClemens Zeidler		BWindow(frame, "ALM Hello World", B_TITLED_WINDOW,
27f5dc380dSClemens Zeidler			B_QUIT_ON_WINDOW_CLOSE)
28ea607d6fSAxel Dörfler	{
29ea607d6fSAxel Dörfler		button1 = new BButton("Hello World!");
30ea607d6fSAxel Dörfler
31ea607d6fSAxel Dörfler		// create a new BALMLayout and use  it for this window
32ea607d6fSAxel Dörfler		fLayout = new BALMLayout();
334199a81aSAlex Wilson		BView* view = new BView("alm view", 0, fLayout);
344199a81aSAlex Wilson		fLayout->SetExplicitAlignment(BAlignment(B_ALIGN_USE_FULL_WIDTH,
354199a81aSAlex Wilson			B_ALIGN_USE_FULL_HEIGHT));
364199a81aSAlex Wilson		SetLayout(new BGroupLayout(B_VERTICAL));
374199a81aSAlex Wilson		AddChild(view);
38ea607d6fSAxel Dörfler
39ea607d6fSAxel Dörfler		// add an area containing the button
40ea607d6fSAxel Dörfler		// use the borders of the layout as the borders for the area
419576365dSClemens Zeidler		fLayout->AddView(button1, fLayout->Left(), fLayout->Top(),
429576365dSClemens Zeidler			fLayout->Right(), fLayout->Bottom());
43c87bbc39SClemens Zeidler		button1->SetExplicitMinSize(BSize(0, 50));
44c87bbc39SClemens Zeidler		button1->SetExplicitMaxSize(BSize(500, 500));
4506f960fcSClemens Zeidler		button1->SetExplicitAlignment(BAlignment(B_ALIGN_USE_FULL_WIDTH,
4606f960fcSClemens Zeidler			B_ALIGN_USE_FULL_HEIGHT));
47c87bbc39SClemens Zeidler
48c87bbc39SClemens Zeidler		// test size limits
49c87bbc39SClemens Zeidler		BSize min = fLayout->MinSize();
50c87bbc39SClemens Zeidler		BSize max = fLayout->MaxSize();
51c87bbc39SClemens Zeidler		SetSizeLimits(min.Width(), max.Width(), min.Height(), max.Height());
524199a81aSAlex Wilson
534199a81aSAlex Wilson		AddShortcut('c', B_COMMAND_KEY, new BMessage(kMsgClone));
544199a81aSAlex Wilson	}
554199a81aSAlex Wilson
564199a81aSAlex Wilson	void MessageReceived(BMessage* message)
574199a81aSAlex Wilson	{
584199a81aSAlex Wilson		switch (message->what) {
594199a81aSAlex Wilson			case kMsgClone:
604199a81aSAlex Wilson			{
614199a81aSAlex Wilson				BView* view = fLayout->View();
624199a81aSAlex Wilson				BMessage archive;
634199a81aSAlex Wilson				view->Archive(&archive, true);
644199a81aSAlex Wilson				BWindow* window = new BWindow(BRect(30, 30, 100, 100),
654199a81aSAlex Wilson					"ALM HelloWorld Clone", B_TITLED_WINDOW,
664199a81aSAlex Wilson					B_QUIT_ON_WINDOW_CLOSE | B_AUTO_UPDATE_SIZE_LIMITS);
674199a81aSAlex Wilson				window->SetLayout(new BGroupLayout(B_VERTICAL));
684199a81aSAlex Wilson				BView* clone;
694199a81aSAlex Wilson				status_t err = BUnarchiver::InstantiateObject(&archive, clone);
704199a81aSAlex Wilson				if (err != B_OK)
714199a81aSAlex Wilson					window->AddChild(new BStringView("", "An error occurred!"));
724199a81aSAlex Wilson				else
734199a81aSAlex Wilson					window->AddChild(clone);
744199a81aSAlex Wilson				window->Show();
754199a81aSAlex Wilson
764199a81aSAlex Wilson				break;
774199a81aSAlex Wilson			}
784199a81aSAlex Wilson			default:
794199a81aSAlex Wilson				BWindow::MessageReceived(message);
804199a81aSAlex Wilson		}
81ea607d6fSAxel Dörfler	}
82ea607d6fSAxel Dörfler
83ea607d6fSAxel Dörflerprivate:
844199a81aSAlex Wilson
85ea607d6fSAxel Dörfler	BALMLayout* fLayout;
86ea607d6fSAxel Dörfler	BButton* button1;
87ea607d6fSAxel Dörfler};
88ea607d6fSAxel Dörfler
89ea607d6fSAxel Dörfler
90ea607d6fSAxel Dörflerclass HelloWorld : public BApplication {
91ea607d6fSAxel Dörflerpublic:
92ea607d6fSAxel Dörfler	HelloWorld()
93f5dc380dSClemens Zeidler		:
94f5dc380dSClemens Zeidler		BApplication("application/x-vnd.haiku.HelloWorld")
95ea607d6fSAxel Dörfler	{
96ea607d6fSAxel Dörfler		BRect frameRect;
97ea607d6fSAxel Dörfler		frameRect.Set(100, 100, 300, 300);
98ea607d6fSAxel Dörfler		HelloWorldWindow* window = new HelloWorldWindow(frameRect);
99ea607d6fSAxel Dörfler		window->Show();
100ea607d6fSAxel Dörfler	}
101ea607d6fSAxel Dörfler};
102ea607d6fSAxel Dörfler
103ea607d6fSAxel Dörfler
104ea607d6fSAxel Dörflerint
105ea607d6fSAxel Dörflermain()
106ea607d6fSAxel Dörfler{
107ea607d6fSAxel Dörfler	HelloWorld app;
108ea607d6fSAxel Dörfler	app.Run();
109ea607d6fSAxel Dörfler	return 0;
110ea607d6fSAxel Dörfler}
111