xref: /haiku/src/apps/pulse/PulseWindow.cpp (revision 1214ef1b2100f2b3299fc9d8d6142e46f70a4c3f)
1 //****************************************************************************************
2 //
3 //	File:		PulseWindow.cpp
4 //
5 //	Written by:	Daniel Switkin
6 //
7 //	Copyright 1999, Be Incorporated
8 //
9 //****************************************************************************************
10 
11 
12 #include "PulseWindow.h"
13 #include "PulseApp.h"
14 #include "Common.h"
15 #include "DeskbarPulseView.h"
16 
17 #include <Alert.h>
18 #include <Deskbar.h>
19 #include <Screen.h>
20 
21 #include <stdlib.h>
22 #include <string.h>
23 
24 
25 PulseWindow::PulseWindow(BRect rect) :
26 	BWindow(rect, "Pulse", B_TITLED_WINDOW, B_NOT_RESIZABLE | B_NOT_ZOOMABLE)
27 {
28 	SetPulseRate(200000);
29 
30 	PulseApp *pulseapp = (PulseApp *)be_app;
31 	BRect bounds = Bounds();
32 	fNormalPulseView = new NormalPulseView(bounds);
33 	AddChild(fNormalPulseView);
34 
35 	fMiniPulseView = new MiniPulseView(bounds, "MiniPulseView", pulseapp->prefs);
36 	AddChild(fMiniPulseView);
37 
38 	fMode = pulseapp->prefs->window_mode;
39 	if (fMode == MINI_WINDOW_MODE) {
40 		SetLook(B_MODAL_WINDOW_LOOK);
41 		SetFeel(B_NORMAL_WINDOW_FEEL);
42 		SetFlags(B_NOT_ZOOMABLE);
43 		fNormalPulseView->Hide();
44 		SetSizeLimits(GetMinimumViewWidth() - 1, 4096, 2, 4096);
45 		ResizeTo(rect.Width(), rect.Height());
46 	} else
47 		fMiniPulseView->Hide();
48 
49 	fPrefsWindow = NULL;
50 }
51 
52 
53 PulseWindow::~PulseWindow()
54 {
55 	PulseApp *pulseapp = (PulseApp *)be_app;
56 
57 	if (fMode == NORMAL_WINDOW_MODE)
58 		pulseapp->prefs->normal_window_rect = Frame();
59 	else if (fMode == MINI_WINDOW_MODE)
60 		pulseapp->prefs->mini_window_rect = Frame();
61 }
62 
63 
64 void
65 PulseWindow::MessageReceived(BMessage *message)
66 {
67 	switch (message->what) {
68 		case PV_NORMAL_MODE:
69 		case PV_MINI_MODE:
70 		case PV_DESKBAR_MODE:
71 			SetMode(message->what);
72 			break;
73 		case PRV_NORMAL_FADE_COLORS:
74 		case PRV_NORMAL_CHANGE_COLOR:
75 			fNormalPulseView->UpdateColors(message);
76 			break;
77 		case PRV_MINI_CHANGE_COLOR:
78 			fMiniPulseView->UpdateColors(message);
79 			break;
80 		case PRV_QUIT:
81 			fPrefsWindow = NULL;
82 			break;
83 		case PV_PREFERENCES: {
84 			// If the window is already open, bring it to the front
85 			if (fPrefsWindow != NULL) {
86 				fPrefsWindow->Activate(true);
87 				break;
88 			}
89 			// Otherwise launch a new preferences window
90 			PulseApp *pulseapp = (PulseApp *)be_app;
91 			fPrefsWindow = new PrefsWindow(pulseapp->prefs->prefs_window_rect,
92 				"Pulse Preferences", new BMessenger(this), pulseapp->prefs);
93 			fPrefsWindow->Show();
94 			break;
95 		}
96 		case PV_ABOUT: {
97 			BAlert *alert = new BAlert("Info", "Pulse\n\nBy David Ramsey and Arve Hjønnevåg\nRevised by Daniel Switkin", "OK");
98 			// Use the asynchronous version so we don't block the window's thread
99 			alert->Go(NULL);
100 			break;
101 		}
102 		case PV_QUIT:
103 			PostMessage(B_QUIT_REQUESTED);
104 			break;
105 		case PV_CPU_MENU_ITEM:
106 			// Call the correct version based on whose menu sent the message
107 			if (fMiniPulseView->IsHidden())
108 				fNormalPulseView->ChangeCPUState(message);
109 			else
110 				fMiniPulseView->ChangeCPUState(message);
111 			break;
112 		default:
113 			BWindow::MessageReceived(message);
114 			break;
115 	}
116 }
117 
118 
119 void
120 PulseWindow::MoveOnScreen()
121 {
122 	// check if the window is on screen, and move it if not
123 	BRect frame = Frame();
124 	BRect screenFrame = BScreen().Frame();
125 
126 	if (frame.left > screenFrame.right)
127 		MoveBy(screenFrame.right - frame.right - 10, 0);
128 	else if (frame.right < 0)
129 		MoveTo(10, frame.top);
130 
131 	if (frame.top > screenFrame.bottom)
132 		MoveBy(0, screenFrame.bottom - frame.bottom - 10);
133 	else if (frame.bottom < 0)
134 		MoveTo(frame.left, 10);
135 }
136 
137 
138 void
139 PulseWindow::SetMode(int newmode)
140 {
141 	PulseApp *pulseapp = (PulseApp *)be_app;
142 
143 	switch (newmode) {
144 		case PV_NORMAL_MODE:
145 			if (fMode == MINI_WINDOW_MODE) {
146 				pulseapp->prefs->mini_window_rect = Frame();
147 				pulseapp->prefs->window_mode = NORMAL_WINDOW_MODE;
148 				pulseapp->prefs->Save();
149 			}
150 			fMiniPulseView->Hide();
151 			fNormalPulseView->Show();
152 			fMode = NORMAL_WINDOW_MODE;
153 			SetType(B_TITLED_WINDOW);
154 			SetFlags(B_NOT_RESIZABLE | B_NOT_ZOOMABLE);
155 			ResizeTo(pulseapp->prefs->normal_window_rect.IntegerWidth(),
156 				pulseapp->prefs->normal_window_rect.IntegerHeight());
157 			MoveTo(pulseapp->prefs->normal_window_rect.left,
158 				pulseapp->prefs->normal_window_rect.top);
159 			MoveOnScreen();
160 			break;
161 
162 		case PV_MINI_MODE:
163 			if (fMode == NORMAL_WINDOW_MODE) {
164 				pulseapp->prefs->normal_window_rect = Frame();
165 				pulseapp->prefs->window_mode = MINI_WINDOW_MODE;
166 				pulseapp->prefs->Save();
167 			}
168 			fNormalPulseView->Hide();
169 			fMiniPulseView->Show();
170 			fMode = MINI_WINDOW_MODE;
171 			SetLook(B_MODAL_WINDOW_LOOK);
172 			SetFeel(B_NORMAL_WINDOW_FEEL);
173 			SetFlags(B_NOT_ZOOMABLE);
174 			SetSizeLimits(GetMinimumViewWidth() - 1, 4096, 2, 4096);
175 			ResizeTo(pulseapp->prefs->mini_window_rect.IntegerWidth(),
176 				pulseapp->prefs->mini_window_rect.IntegerHeight());
177 			MoveTo(pulseapp->prefs->mini_window_rect.left,
178 				pulseapp->prefs->mini_window_rect.top);
179 			MoveOnScreen();
180 			break;
181 
182 		case PV_DESKBAR_MODE:
183 			// Do not set window's mode to DESKBAR_MODE because the
184 			// destructor needs to save the correct BRect. ~PulseApp()
185 			// will handle launching the replicant after our prefs are saved.
186 			pulseapp->prefs->window_mode = DESKBAR_MODE;
187 			PostMessage(B_QUIT_REQUESTED);
188 			break;
189 	}
190 }
191 
192 
193 bool
194 PulseWindow::QuitRequested()
195 {
196 	be_app->PostMessage(B_QUIT_REQUESTED);
197 	return true;
198 }
199