xref: /haiku/src/apps/pulse/PulseWindow.cpp (revision 0a7d4d9991b743aed5404bfff09f2a659da0a4ff)
152a38012Sejakowatz //****************************************************************************************
252a38012Sejakowatz //
352a38012Sejakowatz //	File:		PulseWindow.cpp
452a38012Sejakowatz //
552a38012Sejakowatz //	Written by:	Daniel Switkin
652a38012Sejakowatz //
752a38012Sejakowatz //	Copyright 1999, Be Incorporated
852a38012Sejakowatz //
952a38012Sejakowatz //****************************************************************************************
1052a38012Sejakowatz 
1180f47570SAxel Dörfler 
1252a38012Sejakowatz #include "PulseWindow.h"
1352a38012Sejakowatz #include "PulseApp.h"
1452a38012Sejakowatz #include "Common.h"
1552a38012Sejakowatz #include "DeskbarPulseView.h"
16*0a7d4d99SAxel Dörfler 
17*0a7d4d99SAxel Dörfler #include <Alert.h>
18*0a7d4d99SAxel Dörfler #include <Deskbar.h>
19*0a7d4d99SAxel Dörfler #include <Screen.h>
2080f47570SAxel Dörfler 
2152a38012Sejakowatz #include <stdlib.h>
2252a38012Sejakowatz #include <string.h>
2352a38012Sejakowatz 
2452a38012Sejakowatz 
2580f47570SAxel Dörfler PulseWindow::PulseWindow(BRect rect) :
2680f47570SAxel Dörfler 	BWindow(rect, "Pulse", B_TITLED_WINDOW, B_NOT_RESIZABLE | B_NOT_ZOOMABLE)
2780f47570SAxel Dörfler {
2852a38012Sejakowatz 	SetPulseRate(200000);
2952a38012Sejakowatz 
3052a38012Sejakowatz 	PulseApp *pulseapp = (PulseApp *)be_app;
3152a38012Sejakowatz 	BRect bounds = Bounds();
3280f47570SAxel Dörfler 	fNormalPulseView = new NormalPulseView(bounds);
3380f47570SAxel Dörfler 	AddChild(fNormalPulseView);
3452a38012Sejakowatz 
3580f47570SAxel Dörfler 	fMiniPulseView = new MiniPulseView(bounds, "MiniPulseView", pulseapp->prefs);
3680f47570SAxel Dörfler 	AddChild(fMiniPulseView);
3752a38012Sejakowatz 
3880f47570SAxel Dörfler 	fMode = pulseapp->prefs->window_mode;
3980f47570SAxel Dörfler 	if (fMode == MINI_WINDOW_MODE) {
4052a38012Sejakowatz 		SetLook(B_MODAL_WINDOW_LOOK);
4152a38012Sejakowatz 		SetFeel(B_NORMAL_WINDOW_FEEL);
4252a38012Sejakowatz 		SetFlags(B_NOT_ZOOMABLE);
4380f47570SAxel Dörfler 		fNormalPulseView->Hide();
4452a38012Sejakowatz 		SetSizeLimits(GetMinimumViewWidth() - 1, 4096, 2, 4096);
4552a38012Sejakowatz 		ResizeTo(rect.Width(), rect.Height());
4680f47570SAxel Dörfler 	} else
4780f47570SAxel Dörfler 		fMiniPulseView->Hide();
4852a38012Sejakowatz 
4980f47570SAxel Dörfler 	fPrefsWindow = NULL;
5052a38012Sejakowatz }
5152a38012Sejakowatz 
5280f47570SAxel Dörfler 
5380f47570SAxel Dörfler PulseWindow::~PulseWindow()
5480f47570SAxel Dörfler {
5580f47570SAxel Dörfler 	PulseApp *pulseapp = (PulseApp *)be_app;
5680f47570SAxel Dörfler 
5780f47570SAxel Dörfler 	if (fMode == NORMAL_WINDOW_MODE)
5880f47570SAxel Dörfler 		pulseapp->prefs->normal_window_rect = Frame();
5980f47570SAxel Dörfler 	else if (fMode == MINI_WINDOW_MODE)
6080f47570SAxel Dörfler 		pulseapp->prefs->mini_window_rect = Frame();
6180f47570SAxel Dörfler }
6280f47570SAxel Dörfler 
6380f47570SAxel Dörfler 
6480f47570SAxel Dörfler void
6580f47570SAxel Dörfler PulseWindow::MessageReceived(BMessage *message)
6680f47570SAxel Dörfler {
6752a38012Sejakowatz 	switch (message->what) {
6852a38012Sejakowatz 		case PV_NORMAL_MODE:
6952a38012Sejakowatz 		case PV_MINI_MODE:
7052a38012Sejakowatz 		case PV_DESKBAR_MODE:
7152a38012Sejakowatz 			SetMode(message->what);
7252a38012Sejakowatz 			break;
7352a38012Sejakowatz 		case PRV_NORMAL_FADE_COLORS:
7452a38012Sejakowatz 		case PRV_NORMAL_CHANGE_COLOR:
7580f47570SAxel Dörfler 			fNormalPulseView->UpdateColors(message);
7652a38012Sejakowatz 			break;
7752a38012Sejakowatz 		case PRV_MINI_CHANGE_COLOR:
7880f47570SAxel Dörfler 			fMiniPulseView->UpdateColors(message);
7952a38012Sejakowatz 			break;
8052a38012Sejakowatz 		case PRV_QUIT:
8180f47570SAxel Dörfler 			fPrefsWindow = NULL;
8252a38012Sejakowatz 			break;
8352a38012Sejakowatz 		case PV_PREFERENCES: {
8452a38012Sejakowatz 			// If the window is already open, bring it to the front
8580f47570SAxel Dörfler 			if (fPrefsWindow != NULL) {
8680f47570SAxel Dörfler 				fPrefsWindow->Activate(true);
8752a38012Sejakowatz 				break;
8852a38012Sejakowatz 			}
8952a38012Sejakowatz 			// Otherwise launch a new preferences window
9052a38012Sejakowatz 			PulseApp *pulseapp = (PulseApp *)be_app;
9180f47570SAxel Dörfler 			fPrefsWindow = new PrefsWindow(pulseapp->prefs->prefs_window_rect,
9252a38012Sejakowatz 				"Pulse Preferences", new BMessenger(this), pulseapp->prefs);
9380f47570SAxel Dörfler 			fPrefsWindow->Show();
9452a38012Sejakowatz 			break;
9552a38012Sejakowatz 		}
9652a38012Sejakowatz 		case PV_ABOUT: {
9752a38012Sejakowatz 			BAlert *alert = new BAlert("Info", "Pulse\n\nBy David Ramsey and Arve Hjønnevåg\nRevised by Daniel Switkin", "OK");
9852a38012Sejakowatz 			// Use the asynchronous version so we don't block the window's thread
9952a38012Sejakowatz 			alert->Go(NULL);
10052a38012Sejakowatz 			break;
10152a38012Sejakowatz 		}
10252a38012Sejakowatz 		case PV_QUIT:
10352a38012Sejakowatz 			PostMessage(B_QUIT_REQUESTED);
10452a38012Sejakowatz 			break;
10552a38012Sejakowatz 		case PV_CPU_MENU_ITEM:
10652a38012Sejakowatz 			// Call the correct version based on whose menu sent the message
10780f47570SAxel Dörfler 			if (fMiniPulseView->IsHidden())
10880f47570SAxel Dörfler 				fNormalPulseView->ChangeCPUState(message);
10980f47570SAxel Dörfler 			else
11080f47570SAxel Dörfler 				fMiniPulseView->ChangeCPUState(message);
11152a38012Sejakowatz 			break;
11252a38012Sejakowatz 		default:
11352a38012Sejakowatz 			BWindow::MessageReceived(message);
11452a38012Sejakowatz 			break;
11552a38012Sejakowatz 	}
11652a38012Sejakowatz }
11752a38012Sejakowatz 
11880f47570SAxel Dörfler 
11980f47570SAxel Dörfler void
120*0a7d4d99SAxel Dörfler PulseWindow::MoveOnScreen()
121*0a7d4d99SAxel Dörfler {
122*0a7d4d99SAxel Dörfler 	// check if the window is on screen, and move it if not
123*0a7d4d99SAxel Dörfler 	BRect frame = Frame();
124*0a7d4d99SAxel Dörfler 	BRect screenFrame = BScreen().Frame();
125*0a7d4d99SAxel Dörfler 
126*0a7d4d99SAxel Dörfler 	if (frame.left > screenFrame.right)
127*0a7d4d99SAxel Dörfler 		MoveBy(screenFrame.right - frame.right - 10, 0);
128*0a7d4d99SAxel Dörfler 	else if (frame.right < 0)
129*0a7d4d99SAxel Dörfler 		MoveTo(10, frame.top);
130*0a7d4d99SAxel Dörfler 
131*0a7d4d99SAxel Dörfler 	if (frame.top > screenFrame.bottom)
132*0a7d4d99SAxel Dörfler 		MoveBy(0, screenFrame.bottom - frame.bottom - 10);
133*0a7d4d99SAxel Dörfler 	else if (frame.bottom < 0)
134*0a7d4d99SAxel Dörfler 		MoveTo(frame.left, 10);
135*0a7d4d99SAxel Dörfler }
136*0a7d4d99SAxel Dörfler 
137*0a7d4d99SAxel Dörfler 
138*0a7d4d99SAxel Dörfler void
13980f47570SAxel Dörfler PulseWindow::SetMode(int newmode)
14080f47570SAxel Dörfler {
14152a38012Sejakowatz 	PulseApp *pulseapp = (PulseApp *)be_app;
142*0a7d4d99SAxel Dörfler 
14352a38012Sejakowatz 	switch (newmode) {
14452a38012Sejakowatz 		case PV_NORMAL_MODE:
14580f47570SAxel Dörfler 			if (fMode == MINI_WINDOW_MODE) {
14652a38012Sejakowatz 				pulseapp->prefs->mini_window_rect = Frame();
14752a38012Sejakowatz 				pulseapp->prefs->window_mode = NORMAL_WINDOW_MODE;
14852a38012Sejakowatz 				pulseapp->prefs->Save();
14952a38012Sejakowatz 			}
15080f47570SAxel Dörfler 			fMiniPulseView->Hide();
15180f47570SAxel Dörfler 			fNormalPulseView->Show();
15280f47570SAxel Dörfler 			fMode = NORMAL_WINDOW_MODE;
15352a38012Sejakowatz 			SetType(B_TITLED_WINDOW);
15452a38012Sejakowatz 			SetFlags(B_NOT_RESIZABLE | B_NOT_ZOOMABLE);
15552a38012Sejakowatz 			ResizeTo(pulseapp->prefs->normal_window_rect.IntegerWidth(),
15652a38012Sejakowatz 				pulseapp->prefs->normal_window_rect.IntegerHeight());
15752a38012Sejakowatz 			MoveTo(pulseapp->prefs->normal_window_rect.left,
15852a38012Sejakowatz 				pulseapp->prefs->normal_window_rect.top);
159*0a7d4d99SAxel Dörfler 			MoveOnScreen();
16052a38012Sejakowatz 			break;
161*0a7d4d99SAxel Dörfler 
16252a38012Sejakowatz 		case PV_MINI_MODE:
16380f47570SAxel Dörfler 			if (fMode == NORMAL_WINDOW_MODE) {
16452a38012Sejakowatz 				pulseapp->prefs->normal_window_rect = Frame();
16552a38012Sejakowatz 				pulseapp->prefs->window_mode = MINI_WINDOW_MODE;
16652a38012Sejakowatz 				pulseapp->prefs->Save();
16752a38012Sejakowatz 			}
16880f47570SAxel Dörfler 			fNormalPulseView->Hide();
16980f47570SAxel Dörfler 			fMiniPulseView->Show();
17080f47570SAxel Dörfler 			fMode = MINI_WINDOW_MODE;
17152a38012Sejakowatz 			SetLook(B_MODAL_WINDOW_LOOK);
17252a38012Sejakowatz 			SetFeel(B_NORMAL_WINDOW_FEEL);
17352a38012Sejakowatz 			SetFlags(B_NOT_ZOOMABLE);
17452a38012Sejakowatz 			SetSizeLimits(GetMinimumViewWidth() - 1, 4096, 2, 4096);
17552a38012Sejakowatz 			ResizeTo(pulseapp->prefs->mini_window_rect.IntegerWidth(),
17652a38012Sejakowatz 				pulseapp->prefs->mini_window_rect.IntegerHeight());
17752a38012Sejakowatz 			MoveTo(pulseapp->prefs->mini_window_rect.left,
17852a38012Sejakowatz 				pulseapp->prefs->mini_window_rect.top);
179*0a7d4d99SAxel Dörfler 			MoveOnScreen();
18052a38012Sejakowatz 			break;
181*0a7d4d99SAxel Dörfler 
18252a38012Sejakowatz 		case PV_DESKBAR_MODE:
18352a38012Sejakowatz 			// Do not set window's mode to DESKBAR_MODE because the
18452a38012Sejakowatz 			// destructor needs to save the correct BRect. ~PulseApp()
18552a38012Sejakowatz 			// will handle launching the replicant after our prefs are saved.
18652a38012Sejakowatz 			pulseapp->prefs->window_mode = DESKBAR_MODE;
18752a38012Sejakowatz 			PostMessage(B_QUIT_REQUESTED);
18852a38012Sejakowatz 			break;
18952a38012Sejakowatz 	}
19052a38012Sejakowatz }
19152a38012Sejakowatz 
19252a38012Sejakowatz 
19380f47570SAxel Dörfler bool
19480f47570SAxel Dörfler PulseWindow::QuitRequested()
19580f47570SAxel Dörfler {
19652a38012Sejakowatz 	be_app->PostMessage(B_QUIT_REQUESTED);
19752a38012Sejakowatz 	return true;
19852a38012Sejakowatz }
199