xref: /haiku/src/apps/pulse/PulseWindow.cpp (revision 33ee23acbe97607d0779633f46c1269ba91894c8)
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 <Screen.h>
21 #include <TextView.h>
22 
23 #include <stdlib.h>
24 #include <string.h>
25 
26 #undef B_TRANSLATION_CONTEXT
27 #define B_TRANSLATION_CONTEXT "PulseWindow"
28 
29 
30 PulseWindow::PulseWindow(BRect rect)
31 	:
32 	BWindow(rect, B_TRANSLATE_SYSTEM_NAME("Pulse"), B_TITLED_WINDOW,
33 		B_NOT_RESIZABLE | B_NOT_ZOOMABLE)
34 {
35 	SetPulseRate(200000);
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 			PulseApp::ShowAbout(true);
107 			break;
108 		}
109 		case PV_QUIT:
110 			PostMessage(B_QUIT_REQUESTED);
111 			break;
112 		case PV_CPU_MENU_ITEM:
113 			// Call the correct version based on whose menu sent the message
114 			if (fMiniPulseView->IsHidden())
115 				fNormalPulseView->ChangeCPUState(message);
116 			else
117 				fMiniPulseView->ChangeCPUState(message);
118 			break;
119 		default:
120 			BWindow::MessageReceived(message);
121 			break;
122 	}
123 }
124 
125 
126 void
127 PulseWindow::MoveOnScreen()
128 {
129 	// check if the window is on screen, and move it if not
130 	BRect frame = Frame();
131 	BRect screenFrame = BScreen().Frame();
132 
133 	if (frame.left > screenFrame.right)
134 		MoveBy(screenFrame.right - frame.right - 10, 0);
135 	else if (frame.right < 0)
136 		MoveTo(10, frame.top);
137 
138 	if (frame.top > screenFrame.bottom)
139 		MoveBy(0, screenFrame.bottom - frame.bottom - 10);
140 	else if (frame.bottom < 0)
141 		MoveTo(frame.left, 10);
142 }
143 
144 
145 void
146 PulseWindow::SetMode(int newmode)
147 {
148 	PulseApp *pulseapp = (PulseApp *)be_app;
149 
150 	switch (newmode) {
151 		case PV_NORMAL_MODE:
152 			if (fMode == MINI_WINDOW_MODE) {
153 				pulseapp->prefs->mini_window_rect = Frame();
154 				pulseapp->prefs->window_mode = NORMAL_WINDOW_MODE;
155 				pulseapp->prefs->Save();
156 			}
157 			fMiniPulseView->Hide();
158 			fNormalPulseView->Show();
159 			fMode = NORMAL_WINDOW_MODE;
160 			SetType(B_TITLED_WINDOW);
161 			SetFlags(B_NOT_RESIZABLE | B_NOT_ZOOMABLE);
162 			ResizeTo(pulseapp->prefs->normal_window_rect.IntegerWidth(),
163 				pulseapp->prefs->normal_window_rect.IntegerHeight());
164 			MoveTo(pulseapp->prefs->normal_window_rect.left,
165 				pulseapp->prefs->normal_window_rect.top);
166 			MoveOnScreen();
167 			break;
168 
169 		case PV_MINI_MODE:
170 			if (fMode == NORMAL_WINDOW_MODE) {
171 				pulseapp->prefs->normal_window_rect = Frame();
172 				pulseapp->prefs->window_mode = MINI_WINDOW_MODE;
173 				pulseapp->prefs->Save();
174 			}
175 			fNormalPulseView->Hide();
176 			fMiniPulseView->Show();
177 			fMode = MINI_WINDOW_MODE;
178 			SetLook(B_MODAL_WINDOW_LOOK);
179 			SetFeel(B_NORMAL_WINDOW_FEEL);
180 			SetFlags(B_NOT_ZOOMABLE);
181 			SetSizeLimits(GetMinimumViewWidth() - 1, 4096, 2, 4096);
182 			ResizeTo(pulseapp->prefs->mini_window_rect.IntegerWidth(),
183 				pulseapp->prefs->mini_window_rect.IntegerHeight());
184 			MoveTo(pulseapp->prefs->mini_window_rect.left,
185 				pulseapp->prefs->mini_window_rect.top);
186 			MoveOnScreen();
187 			break;
188 
189 		case PV_DESKBAR_MODE:
190 			// Do not set window's mode to DESKBAR_MODE because the
191 			// destructor needs to save the correct BRect. ~PulseApp()
192 			// will handle launching the replicant after our prefs are saved.
193 			pulseapp->prefs->window_mode = DESKBAR_MODE;
194 			PostMessage(B_QUIT_REQUESTED);
195 			break;
196 	}
197 }
198 
199 
200 bool
201 PulseWindow::QuitRequested()
202 {
203 	be_app->PostMessage(B_QUIT_REQUESTED);
204 	return true;
205 }
206