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