xref: /haiku/src/apps/pulse/PulseWindow.cpp (revision 1a76488fc88584bf66b9751d7fb9b6527ac20d87)
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 | B_QUIT_ON_WINDOW_CLOSE)
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->fPrefs);
44 	AddChild(fMiniPulseView);
45 
46 	fMode = pulseapp->fPrefs->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 		BRect r = fNormalPulseView->Bounds();
57 		ResizeTo(r.Width(), r.Height());
58 	}
59 }
60 
61 
62 PulseWindow::~PulseWindow()
63 {
64 	PulseApp *pulseapp = (PulseApp *)be_app;
65 
66 	if (fMode == NORMAL_WINDOW_MODE)
67 		pulseapp->fPrefs->normal_window_rect = Frame();
68 	else if (fMode == MINI_WINDOW_MODE)
69 		pulseapp->fPrefs->mini_window_rect = Frame();
70 }
71 
72 
73 void
74 PulseWindow::MessageReceived(BMessage *message)
75 {
76 	switch (message->what) {
77 		case PV_NORMAL_MODE:
78 		case PV_MINI_MODE:
79 		case PV_DESKBAR_MODE:
80 			SetMode(message->what);
81 			break;
82 		case PRV_NORMAL_FADE_COLORS:
83 		case PRV_NORMAL_CHANGE_COLOR:
84 			fNormalPulseView->UpdateColors(message);
85 			break;
86 		case PRV_MINI_CHANGE_COLOR:
87 			fMiniPulseView->UpdateColors(message);
88 			break;
89 		case PV_PREFERENCES: {
90 			DetachCurrentMessage();
91 			message->AddMessenger("settingsListener", this);
92 			be_app->PostMessage(message);
93 			break;
94 		}
95 		case PV_ABOUT: {
96 			be_app->PostMessage(B_ABOUT_REQUESTED);
97 			break;
98 		}
99 		case PV_QUIT:
100 			PostMessage(B_QUIT_REQUESTED);
101 			break;
102 		case PV_CPU_MENU_ITEM:
103 			// Call the correct version based on whose menu sent the message
104 			if (fMiniPulseView->IsHidden())
105 				fNormalPulseView->ChangeCPUState(message);
106 			else
107 				fMiniPulseView->ChangeCPUState(message);
108 			break;
109 		default:
110 			BWindow::MessageReceived(message);
111 			break;
112 	}
113 }
114 
115 
116 void
117 PulseWindow::SetMode(int newmode)
118 {
119 	PulseApp *pulseapp = (PulseApp *)be_app;
120 
121 	switch (newmode) {
122 		case PV_NORMAL_MODE:
123 		{
124 			if (fMode == MINI_WINDOW_MODE) {
125 				pulseapp->fPrefs->mini_window_rect = Frame();
126 				pulseapp->fPrefs->window_mode = NORMAL_WINDOW_MODE;
127 				pulseapp->fPrefs->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 			BRect r = fNormalPulseView->Bounds();
135 			ResizeTo(r.Width(), r.Height());
136 			MoveTo(pulseapp->fPrefs->normal_window_rect.left,
137 				pulseapp->fPrefs->normal_window_rect.top);
138 			MoveOnScreen(B_MOVE_IF_PARTIALLY_OFFSCREEN);
139 			break;
140 		}
141 
142 		case PV_MINI_MODE:
143 			if (fMode == NORMAL_WINDOW_MODE) {
144 				pulseapp->fPrefs->normal_window_rect = Frame();
145 				pulseapp->fPrefs->window_mode = MINI_WINDOW_MODE;
146 				pulseapp->fPrefs->Save();
147 			}
148 			fNormalPulseView->Hide();
149 			fMiniPulseView->Show();
150 			fMode = MINI_WINDOW_MODE;
151 			SetLook(B_MODAL_WINDOW_LOOK);
152 			SetFeel(B_NORMAL_WINDOW_FEEL);
153 			SetFlags(B_NOT_ZOOMABLE);
154 			SetSizeLimits(GetMinimumViewWidth() - 1, 4096, 2, 4096);
155 			ResizeTo(pulseapp->fPrefs->mini_window_rect.IntegerWidth(),
156 				pulseapp->fPrefs->mini_window_rect.IntegerHeight());
157 			MoveTo(pulseapp->fPrefs->mini_window_rect.left,
158 				pulseapp->fPrefs->mini_window_rect.top);
159 			MoveOnScreen(B_MOVE_IF_PARTIALLY_OFFSCREEN);
160 			break;
161 
162 		case PV_DESKBAR_MODE:
163 			// Do not set window's mode to DESKBAR_MODE because the
164 			// destructor needs to save the correct BRect. ~PulseApp()
165 			// will handle launching the replicant after our prefs are saved.
166 			pulseapp->fPrefs->window_mode = DESKBAR_MODE;
167 			LoadInDeskbar();
168 			break;
169 	}
170 }
171 
172 
173 bool
174 PulseWindow::QuitRequested()
175 {
176 	return true;
177 }
178