1/*
2 * Copyright 2006, 2011 Haiku.
3 * Distributed under the terms of the MIT License.
4 *
5 * Authors:
6 *		Stephan Aßmus <superstippi@gmx.de>
7 */
8
9#include "support.h"
10
11#include <stdio.h>
12#include <string.h>
13
14#include <Directory.h>
15#include <File.h>
16#include <FindDirectory.h>
17#include <Path.h>
18#include <Screen.h>
19#include <View.h>
20
21
22status_t
23load_settings(BMessage* message, const char* fileName, const char* folder)
24{
25	status_t ret = B_BAD_VALUE;
26	if (message) {
27		BPath path;
28		if ((ret = find_directory(B_USER_SETTINGS_DIRECTORY, &path)) == B_OK) {
29			// passing folder is optional
30			if (folder)
31				ret = path.Append( folder );
32			if (ret == B_OK && (ret = path.Append(fileName)) == B_OK ) {
33				BFile file(path.Path(), B_READ_ONLY);
34				if ((ret = file.InitCheck()) == B_OK) {
35					ret = message->Unflatten(&file);
36					file.Unset();
37				}
38			}
39		}
40	}
41	return ret;
42}
43
44
45status_t
46save_settings(BMessage* message, const char* fileName, const char* folder)
47{
48	status_t ret = B_BAD_VALUE;
49	if (message) {
50		BPath path;
51		if ((ret = find_directory(B_USER_SETTINGS_DIRECTORY, &path)) == B_OK) {
52			// passing folder is optional
53			if (folder && (ret = path.Append(folder)) == B_OK)
54				ret = create_directory(path.Path(), 0777);
55			if (ret == B_OK && (ret = path.Append(fileName)) == B_OK) {
56				BFile file(path.Path(), B_WRITE_ONLY | B_CREATE_FILE
57					| B_ERASE_FILE);
58				if ((ret = file.InitCheck()) == B_OK) {
59					ret = message->Flatten(&file);
60					file.Unset();
61				}
62			}
63		}
64	}
65	return ret;
66}
67
68
69void
70stroke_frame(BView* v, BRect r, rgb_color left, rgb_color top, rgb_color right,
71	rgb_color bottom)
72{
73	if (v && r.IsValid()) {
74		v->BeginLineArray(4);
75			v->AddLine(BPoint(r.left, r.bottom),
76					   BPoint(r.left, r.top), left);
77			v->AddLine(BPoint(r.left + 1.0, r.top),
78					   BPoint(r.right, r.top), top);
79			v->AddLine(BPoint(r.right, r.top + 1.0),
80					   BPoint(r.right, r.bottom), right);
81			v->AddLine(BPoint(r.right - 1.0, r.bottom),
82					   BPoint(r.left + 1.0, r.bottom), bottom);
83		v->EndLineArray();
84	}
85}
86
87
88bool
89make_sure_frame_is_on_screen(BRect& frame, BWindow* window)
90{
91	BScreen* screen = window != NULL ? new BScreen(window)
92		: new BScreen(B_MAIN_SCREEN_ID);
93
94	bool success = false;
95	if (frame.IsValid() && screen->IsValid()) {
96		BRect screenFrame = screen->Frame();
97		if (!screenFrame.Contains(frame)) {
98			// make sure frame fits in the screen
99			if (frame.Width() > screenFrame.Width())
100				frame.right -= frame.Width() - screenFrame.Width() + 10.0;
101			if (frame.Height() > screenFrame.Height())
102				frame.bottom -= frame.Height() - screenFrame.Height() + 30.0;
103			// frame is now at the most the size of the screen
104			if (frame.right > screenFrame.right)
105				frame.OffsetBy(-(frame.right - screenFrame.right), 0.0);
106			if (frame.bottom > screenFrame.bottom)
107				frame.OffsetBy(0.0, -(frame.bottom - screenFrame.bottom));
108			if (frame.left < screenFrame.left)
109				frame.OffsetBy((screenFrame.left - frame.left), 0.0);
110			if (frame.top < screenFrame.top)
111				frame.OffsetBy(0.0, (screenFrame.top - frame.top));
112		}
113		success = true;
114	}
115	delete screen;
116	return success;
117}
118
119