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