125648ff0SStephan Aßmus/*
2e2b113d4SStephan Aßmus * Copyright 2006, 2011 Haiku.
325648ff0SStephan Aßmus * Distributed under the terms of the MIT License.
425648ff0SStephan Aßmus *
525648ff0SStephan Aßmus * Authors:
625648ff0SStephan Aßmus *		Stephan A��mus <superstippi@gmx.de>
725648ff0SStephan Aßmus */
825648ff0SStephan Aßmus
925648ff0SStephan Aßmus#include "support.h"
1025648ff0SStephan Aßmus
1125648ff0SStephan Aßmus#include <stdio.h>
1225648ff0SStephan Aßmus#include <string.h>
1325648ff0SStephan Aßmus
1425648ff0SStephan Aßmus#include <Directory.h>
1525648ff0SStephan Aßmus#include <File.h>
1625648ff0SStephan Aßmus#include <FindDirectory.h>
1725648ff0SStephan Aßmus#include <Path.h>
1825648ff0SStephan Aßmus#include <Screen.h>
1925648ff0SStephan Aßmus#include <View.h>
2025648ff0SStephan Aßmus
21e2b113d4SStephan Aßmus
2225648ff0SStephan Aßmusstatus_t
2325648ff0SStephan Aßmusload_settings(BMessage* message, const char* fileName, const char* folder)
2425648ff0SStephan Aßmus{
2525648ff0SStephan Aßmus	status_t ret = B_BAD_VALUE;
2625648ff0SStephan Aßmus	if (message) {
2725648ff0SStephan Aßmus		BPath path;
2825648ff0SStephan Aßmus		if ((ret = find_directory(B_USER_SETTINGS_DIRECTORY, &path)) == B_OK) {
2925648ff0SStephan Aßmus			// passing folder is optional
3025648ff0SStephan Aßmus			if (folder)
3125648ff0SStephan Aßmus				ret = path.Append( folder );
3225648ff0SStephan Aßmus			if (ret == B_OK && (ret = path.Append(fileName)) == B_OK ) {
3325648ff0SStephan Aßmus				BFile file(path.Path(), B_READ_ONLY);
3425648ff0SStephan Aßmus				if ((ret = file.InitCheck()) == B_OK) {
3525648ff0SStephan Aßmus					ret = message->Unflatten(&file);
3625648ff0SStephan Aßmus					file.Unset();
3725648ff0SStephan Aßmus				}
3825648ff0SStephan Aßmus			}
3925648ff0SStephan Aßmus		}
4025648ff0SStephan Aßmus	}
4125648ff0SStephan Aßmus	return ret;
4225648ff0SStephan Aßmus}
4325648ff0SStephan Aßmus
44e2b113d4SStephan Aßmus
4525648ff0SStephan Aßmusstatus_t
4625648ff0SStephan Aßmussave_settings(BMessage* message, const char* fileName, const char* folder)
4725648ff0SStephan Aßmus{
4825648ff0SStephan Aßmus	status_t ret = B_BAD_VALUE;
4925648ff0SStephan Aßmus	if (message) {
5025648ff0SStephan Aßmus		BPath path;
5125648ff0SStephan Aßmus		if ((ret = find_directory(B_USER_SETTINGS_DIRECTORY, &path)) == B_OK) {
5225648ff0SStephan Aßmus			// passing folder is optional
5325648ff0SStephan Aßmus			if (folder && (ret = path.Append(folder)) == B_OK)
5425648ff0SStephan Aßmus				ret = create_directory(path.Path(), 0777);
5525648ff0SStephan Aßmus			if (ret == B_OK && (ret = path.Append(fileName)) == B_OK) {
56e2b113d4SStephan Aßmus				BFile file(path.Path(), B_WRITE_ONLY | B_CREATE_FILE
57e2b113d4SStephan Aßmus					| B_ERASE_FILE);
5825648ff0SStephan Aßmus				if ((ret = file.InitCheck()) == B_OK) {
5925648ff0SStephan Aßmus					ret = message->Flatten(&file);
6025648ff0SStephan Aßmus					file.Unset();
6125648ff0SStephan Aßmus				}
6225648ff0SStephan Aßmus			}
6325648ff0SStephan Aßmus		}
6425648ff0SStephan Aßmus	}
6525648ff0SStephan Aßmus	return ret;
6625648ff0SStephan Aßmus}
6725648ff0SStephan Aßmus
68e2b113d4SStephan Aßmus
6925648ff0SStephan Aßmusvoid
70e2b113d4SStephan Aßmusstroke_frame(BView* v, BRect r, rgb_color left, rgb_color top, rgb_color right,
71e2b113d4SStephan Aßmus	rgb_color bottom)
7225648ff0SStephan Aßmus{
7325648ff0SStephan Aßmus	if (v && r.IsValid()) {
7425648ff0SStephan Aßmus		v->BeginLineArray(4);
7525648ff0SStephan Aßmus			v->AddLine(BPoint(r.left, r.bottom),
7625648ff0SStephan Aßmus					   BPoint(r.left, r.top), left);
7725648ff0SStephan Aßmus			v->AddLine(BPoint(r.left + 1.0, r.top),
7825648ff0SStephan Aßmus					   BPoint(r.right, r.top), top);
7925648ff0SStephan Aßmus			v->AddLine(BPoint(r.right, r.top + 1.0),
8025648ff0SStephan Aßmus					   BPoint(r.right, r.bottom), right);
8125648ff0SStephan Aßmus			v->AddLine(BPoint(r.right - 1.0, r.bottom),
8225648ff0SStephan Aßmus					   BPoint(r.left + 1.0, r.bottom), bottom);
8325648ff0SStephan Aßmus		v->EndLineArray();
8425648ff0SStephan Aßmus	}
8525648ff0SStephan Aßmus}
8625648ff0SStephan Aßmus
87e2b113d4SStephan Aßmus
8825648ff0SStephan Aßmusbool
89a9c2d6b3SStephan Aßmusmake_sure_frame_is_on_screen(BRect& frame, BWindow* window)
9025648ff0SStephan Aßmus{
91e2b113d4SStephan Aßmus	BScreen* screen = window != NULL ? new BScreen(window)
92e2b113d4SStephan Aßmus		: new BScreen(B_MAIN_SCREEN_ID);
93e2b113d4SStephan Aßmus
9425648ff0SStephan Aßmus	bool success = false;
9525648ff0SStephan Aßmus	if (frame.IsValid() && screen->IsValid()) {
9625648ff0SStephan Aßmus		BRect screenFrame = screen->Frame();
9725648ff0SStephan Aßmus		if (!screenFrame.Contains(frame)) {
9825648ff0SStephan Aßmus			// make sure frame fits in the screen
9925648ff0SStephan Aßmus			if (frame.Width() > screenFrame.Width())
10025648ff0SStephan Aßmus				frame.right -= frame.Width() - screenFrame.Width() + 10.0;
10125648ff0SStephan Aßmus			if (frame.Height() > screenFrame.Height())
10225648ff0SStephan Aßmus				frame.bottom -= frame.Height() - screenFrame.Height() + 30.0;
10325648ff0SStephan Aßmus			// frame is now at the most the size of the screen
10425648ff0SStephan Aßmus			if (frame.right > screenFrame.right)
10525648ff0SStephan Aßmus				frame.OffsetBy(-(frame.right - screenFrame.right), 0.0);
10625648ff0SStephan Aßmus			if (frame.bottom > screenFrame.bottom)
10725648ff0SStephan Aßmus				frame.OffsetBy(0.0, -(frame.bottom - screenFrame.bottom));
10825648ff0SStephan Aßmus			if (frame.left < screenFrame.left)
10925648ff0SStephan Aßmus				frame.OffsetBy((screenFrame.left - frame.left), 0.0);
11025648ff0SStephan Aßmus			if (frame.top < screenFrame.top)
11125648ff0SStephan Aßmus				frame.OffsetBy(0.0, (screenFrame.top - frame.top));
11225648ff0SStephan Aßmus		}
11325648ff0SStephan Aßmus		success = true;
11425648ff0SStephan Aßmus	}
11525648ff0SStephan Aßmus	delete screen;
11625648ff0SStephan Aßmus	return success;
11725648ff0SStephan Aßmus}
11825648ff0SStephan Aßmus
119