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