xref: /haiku/src/apps/diskusage/MainWindow.cpp (revision 959ff00ddee8411dabb09211f3bfbd52d87229da)
1 /*
2  * Copyright (c) 2008 Stephan Aßmus <superstippi@gmx.de>. All rights reserved.
3  * Distributed under the terms of the MIT/X11 license.
4  *
5  * Copyright (c) 1999 Mike Steed. You are free to use and distribute this software
6  * as long as it is accompanied by it's documentation and this copyright notice.
7  * The software comes with no warranty, etc.
8  */
9 #include "MainWindow.h"
10 
11 #include <Application.h>
12 #include <Node.h>
13 #include <Roster.h>
14 #include <Screen.h>
15 
16 #include "Common.h"
17 #include "ControlsView.h"
18 #include "PieView.h"
19 #include "StatusView.h"
20 
21 
22 const float kMinWinSize = 275.0;
23 
24 
25 MainWindow::MainWindow(BRect pieRect)
26 	: BWindow(pieRect, "DiskUsage", B_TITLED_WINDOW,
27 		B_ASYNCHRONOUS_CONTROLS | B_QUIT_ON_WINDOW_CLOSE)
28 {
29 	BRect r = Bounds();
30 	r.bottom = r.top + 1.0;
31 	fControlsView = new ControlsView(r);
32 	float cvHeight = fControlsView->Bounds().Height();
33 	AddChild(fControlsView);
34 
35 	r = Bounds();
36 	r.top = r.bottom - 1.0;
37 	fStatusView = new StatusView(r);
38 	float svHeight = fStatusView->Bounds().Height();
39 
40 	float winHeight = pieRect.Height() + cvHeight + svHeight;
41 	fStatusView->MoveTo(0.0, winHeight - svHeight);
42 	ResizeTo(r.Width(), winHeight);
43 
44 	AddChild(fStatusView);
45 
46 	r = fControlsView->Frame();
47 	r.top = r.bottom + 1.0;
48 	r.bottom = fStatusView->Frame().top - 1.0;
49 	fPieView = new PieView(r, this);
50 	AddChild(fPieView);
51 
52 	Show();
53 
54 	// Note: The following code is semi-broken because BScreen::Frame()
55 	// returns incorrect dimensions for the G200 in 1152x864 mode.  I reported
56 	// this bug, and Be said it's not a bug -- the Matrox driver actually uses
57 	// a resolution of 1152x900 in that mode.  Oh well.
58 	Lock();
59 	float extraHeight = fControlsView->Bounds().Height() + fStatusView->Bounds().Height();
60 	float minHeight = kMinWinSize + extraHeight;
61 	float maxHeight = BScreen(this).Frame().Height();
62 	float maxWidth = maxHeight - extraHeight;
63 	Unlock();
64 
65 	SetSizeLimits(kMinWinSize, maxWidth, minHeight, maxHeight);
66 }
67 
68 
69 MainWindow::~MainWindow()
70 {
71 }
72 
73 
74 void
75 MainWindow::MessageReceived(BMessage* message)
76 {
77 	switch (message->what) {
78 		case kBtnRescan:
79 		case kMenuSelectVol:
80 		case B_REFS_RECEIVED:
81 			fPieView->MessageReceived(message);
82 			break;
83 
84 		case kBtnHelp:
85 			be_roster->Launch(&kHelpFileRef);
86 			break;
87 
88 		default:
89 			BWindow::MessageReceived(message);
90 			break;
91 	}
92 }
93 
94 
95 void
96 MainWindow::Zoom(BPoint origin, float width, float height)
97 {
98 	width = Frame().Width();
99 	height = Frame().Height();
100 	if (_FixAspectRatio(&width, &height))
101 		SetZoomLimits(width, height);
102 
103 	BWindow::Zoom(Frame().LeftTop(), width, height);
104 }
105 
106 
107 void
108 MainWindow::FrameResized(float width, float height)
109 {
110 	if (_FixAspectRatio(&width, &height))
111 		ResizeTo(width, height);
112 }
113 
114 
115 bool
116 MainWindow::QuitRequested()
117 {
118 	be_app->PostMessage(B_QUIT_REQUESTED);
119 	return true;
120 }
121 
122 
123 // #pragma mark -
124 
125 
126 void
127 MainWindow::ShowInfo(const FileInfo* info)
128 {
129 	fStatusView->Show(info);
130 }
131 
132 
133 void
134 MainWindow::SetRescanEnabled(bool enabled)
135 {
136 	fControlsView->SetRescanEnabled(enabled);
137 }
138 
139 
140 BVolume*
141 MainWindow::FindDeviceFor(dev_t device, bool invoke)
142 {
143 	return fControlsView->FindDeviceFor(device, invoke);
144 }
145 
146 
147 // #pragma mark -
148 
149 
150 bool
151 MainWindow::_FixAspectRatio(float* width, float* height)
152 {
153 	float ctrlViewHeight = fControlsView->Bounds().Height();
154 	float statusViewHeight = fStatusView->Bounds().Height();
155 
156 	float newPieSize = *height - ctrlViewHeight - statusViewHeight;
157 	if (*width < newPieSize)
158 		newPieSize = *width;
159 
160 	float newWidth = newPieSize;
161 	float newHeight = newPieSize + ctrlViewHeight + statusViewHeight;
162 	if (*width != newWidth || *height != newHeight) {
163 		*width = newWidth;
164 		*height = newHeight;
165 		return true;
166 	}
167 
168 	return false;
169 }
170 
171 
172