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