xref: /haiku/src/apps/launchbox/App.cpp (revision a381c8a06378de22ff08adf4282b4e3f7e50d250)
1 /*
2  * Copyright 2006, Haiku.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Stephan Aßmus <superstippi@gmx.de>
7  */
8 
9 
10 #include "App.h"
11 
12 #include <stdio.h>
13 
14 #include <AboutWindow.h>
15 #include <Entry.h>
16 #include <Message.h>
17 #include <String.h>
18 
19 #include "support.h"
20 
21 #include "MainWindow.h"
22 
23 
24 // constructor
25 App::App()
26 	: BApplication("application/x-vnd.Haiku-LaunchBox")
27 {
28 }
29 
30 // destructor
31 App::~App()
32 {
33 }
34 
35 // QuitRequested
36 bool
37 App::QuitRequested()
38 {
39 	BMessage settings('sett');
40 	for (int32 i = 0; BWindow* window = WindowAt(i); i++) {
41 		if (MainWindow* padWindow = dynamic_cast<MainWindow*>(window)) {
42 			BMessage* windowSettings = padWindow->Settings();
43 			if (windowSettings && padWindow->Lock()) {
44 				padWindow->SaveSettings(windowSettings);
45 				padWindow->Unlock();
46 				settings.AddMessage("window", windowSettings);
47 			}
48 		}
49 	}
50 	save_settings(&settings, "main_settings", "LaunchBox");
51 	return true;
52 }
53 
54 // ReadyToRun
55 void
56 App::ReadyToRun()
57 {
58 	bool windowAdded = false;
59 	BRect frame(50.0, 50.0, 65.0, 100.0);
60 
61 	BMessage settings('sett');
62 	status_t status = load_settings(&settings, "main_settings", "LaunchBox");
63 	if (status >= B_OK) {
64 		BMessage windowMessage;
65 		for (int32 i = 0; settings.FindMessage("window", i, &windowMessage) >= B_OK; i++) {
66 			BString name("Pad ");
67 			name << i + 1;
68 			BMessage* windowSettings = new BMessage(windowMessage);
69 			MainWindow* window = new MainWindow(name.String(), frame, windowSettings);
70 			window->Show();
71 			windowAdded = true;
72 			frame.OffsetBy(10.0, 10.0);
73 			windowMessage.MakeEmpty();
74 		}
75 	}
76 
77 	if (!windowAdded) {
78 		MainWindow* window = new MainWindow("Pad 1", frame, true);
79 		window->Show();
80 	}
81 }
82 
83 // MessageReceived
84 void
85 App::MessageReceived(BMessage* message)
86 {
87 	switch (message->what) {
88 		case MSG_ADD_WINDOW: {
89 			BMessage* settings = new BMessage('sett');
90 			bool wasCloned = message->FindMessage("window", settings) == B_OK;
91 			BString name("Pad ");
92 			name << CountWindows() + 1;
93 			MainWindow* window = new MainWindow(name.String(),
94 				BRect(50.0, 50.0, 65.0, 100.0), settings);
95 			if (wasCloned)
96 				window->MoveBy(10, 10);
97 			window->Show();
98 			break;
99 		}
100 		default:
101 			BApplication::MessageReceived(message);
102 			break;
103 	}
104 }
105 
106 // AboutRequested
107 void
108 App::AboutRequested()
109 {
110 	const char* authors[2];
111 	authors[0] = "Stephan Aßmus (aka stippi)";
112 	authors[1] = NULL;
113 	(new BAboutWindow("LaunchBox", 2004, authors))->Show();
114 }
115