xref: /haiku/src/apps/haikudepot/ui_generic/support.cpp (revision 90c3b9bf9fe633e4c6a59d42c11c3f523183c553)
1 /*
2  * Copyright 2006, 2011, 2013 Stephan Aßmus <superstippi@gmx.de>
3  * All rights reserved. Distributed under the terms of the MIT License.
4  */
5 
6 #include "support.h"
7 
8 #include <algorithm>
9 #include <stdio.h>
10 #include <string.h>
11 
12 #include <Application.h>
13 #include <Directory.h>
14 #include <File.h>
15 #include <FindDirectory.h>
16 #include <Font.h>
17 #include <Path.h>
18 #include <Resources.h>
19 #include <Roster.h>
20 #include <Screen.h>
21 #include <View.h>
22 
23 
24 status_t
25 load_settings(BMessage* message, const char* fileName, const char* folder)
26 {
27 	if (message == NULL || fileName == NULL || fileName[0] == '\0')
28 		return B_BAD_VALUE;
29 
30 	BPath path;
31 	status_t ret = find_directory(B_USER_SETTINGS_DIRECTORY, &path);
32 	if (ret != B_OK)
33 		return ret;
34 
35 	// passing folder is optional
36 	if (folder != NULL)
37 		ret = path.Append(folder);
38 
39 	if (ret == B_OK && (ret = path.Append(fileName)) == B_OK ) {
40 		BFile file(path.Path(), B_READ_ONLY);
41 		ret = file.InitCheck();
42 		if (ret == B_OK)
43 			ret = message->Unflatten(&file);
44 	}
45 
46 	return ret;
47 }
48 
49 
50 status_t
51 save_settings(const BMessage* message, const char* fileName, const char* folder)
52 {
53 	if (message == NULL || fileName == NULL || fileName[0] == '\0')
54 		return B_BAD_VALUE;
55 
56 	BPath path;
57 	status_t ret = find_directory(B_USER_SETTINGS_DIRECTORY, &path);
58 	if (ret != B_OK)
59 		return ret;
60 
61 	// passing folder is optional
62 	if (folder != NULL)
63 		ret = path.Append(folder);
64 
65 	if (ret == B_OK)
66 		ret = create_directory(path.Path(), 0777);
67 
68 	if (ret == B_OK)
69 		ret = path.Append(fileName);
70 
71 	if (ret == B_OK) {
72 		BFile file(path.Path(), B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE);
73 		ret = file.InitCheck();
74 		if (ret == B_OK)
75 			ret = message->Flatten(&file);
76 	}
77 
78 	return ret;
79 }
80 
81 
82 bool
83 make_sure_frame_is_on_screen(BRect& frame, float borderWidth,
84 	float tabHeight, BWindow* window)
85 {
86 	if (!frame.IsValid())
87 		return false;
88 
89 	BScreen* screen = window != NULL ? new BScreen(window)
90 		: new BScreen(B_MAIN_SCREEN_ID);
91 
92 	if (!screen->IsValid()) {
93 		delete screen;
94 		return false;
95 	}
96 
97 	BRect screenFrame = screen->Frame();
98 
99 	// Validate borderWidth and tabHeight
100 	if (borderWidth < 0.0f)
101 		borderWidth = 0.0f;
102 	else
103 		borderWidth = std::min(borderWidth, floorf(screenFrame.Width() / 4.0f));
104 
105 	if (tabHeight < 0.0f)
106 		tabHeight = 0.0f;
107 	else
108 		tabHeight = std::min(tabHeight, floorf(screenFrame.Height() / 4.0f));
109 
110 	// Account for window border and tab. It doesn't matter much if the
111 	// decorator frame is wider, just as long as the user can grab a
112 	// border to move the window
113 	screenFrame.InsetBy(borderWidth, borderWidth);
114 	screenFrame.top += tabHeight;
115 
116 	if (!screenFrame.Contains(frame)) {
117 		// make sure frame fits in the screen
118 		if (frame.Width() > screenFrame.Width())
119 			frame.right -= frame.Width() - screenFrame.Width();
120 		if (frame.Height() > screenFrame.Height())
121 			frame.bottom -= frame.Height() - screenFrame.Height();
122 
123 		// frame is now at the most the size of the screen
124 		if (frame.right > screenFrame.right)
125 			frame.OffsetBy(-(frame.right - screenFrame.right), 0.0);
126 		if (frame.bottom > screenFrame.bottom)
127 			frame.OffsetBy(0.0, -(frame.bottom - screenFrame.bottom));
128 		if (frame.left < screenFrame.left)
129 			frame.OffsetBy((screenFrame.left - frame.left), 0.0);
130 		if (frame.top < screenFrame.top)
131 			frame.OffsetBy(0.0, (screenFrame.top - frame.top));
132 	}
133 
134 	delete screen;
135 	return true;
136 }
137 
138 
139 status_t
140 get_app_resources(BResources& resources)
141 {
142 	app_info info;
143 	status_t status = be_app->GetAppInfo(&info);
144 	if (status != B_OK)
145 		return status;
146 
147 	return resources.SetTo(&info.ref);
148 }
149 
150 
151 void
152 set_small_font(BView* view)
153 {
154 	BFont font;
155 	view->GetFont(&font);
156 	font.SetSize(ceilf(font.Size() * 0.8));
157 	view->SetFont(&font);
158 }
159 
160 
161 
162 
163