1 /* 2 * Copyright 2006, 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 // load_settings 22 status_t 23 load_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 // save_settings 45 status_t 46 save_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 | B_ERASE_FILE); 57 if ((ret = file.InitCheck()) == B_OK) { 58 ret = message->Flatten(&file); 59 file.Unset(); 60 } 61 } 62 } 63 } 64 return ret; 65 } 66 67 // stroke_frame 68 void 69 stroke_frame(BView* v, BRect r, rgb_color left, rgb_color top, 70 rgb_color right, rgb_color bottom) 71 { 72 if (v && r.IsValid()) { 73 v->BeginLineArray(4); 74 v->AddLine(BPoint(r.left, r.bottom), 75 BPoint(r.left, r.top), left); 76 v->AddLine(BPoint(r.left + 1.0, r.top), 77 BPoint(r.right, r.top), top); 78 v->AddLine(BPoint(r.right, r.top + 1.0), 79 BPoint(r.right, r.bottom), right); 80 v->AddLine(BPoint(r.right - 1.0, r.bottom), 81 BPoint(r.left + 1.0, r.bottom), bottom); 82 v->EndLineArray(); 83 } 84 } 85 86 // make_sure_frame_is_on_screen 87 bool 88 make_sure_frame_is_on_screen(BRect& frame, BWindow* window) 89 { 90 BScreen* screen = window ? new BScreen(window) : new BScreen(B_MAIN_SCREEN_ID); 91 bool success = false; 92 if (frame.IsValid() && screen->IsValid()) { 93 BRect screenFrame = screen->Frame(); 94 if (!screenFrame.Contains(frame)) { 95 // make sure frame fits in the screen 96 if (frame.Width() > screenFrame.Width()) 97 frame.right -= frame.Width() - screenFrame.Width() + 10.0; 98 if (frame.Height() > screenFrame.Height()) 99 frame.bottom -= frame.Height() - screenFrame.Height() + 30.0; 100 // frame is now at the most the size of the screen 101 if (frame.right > screenFrame.right) 102 frame.OffsetBy(-(frame.right - screenFrame.right), 0.0); 103 if (frame.bottom > screenFrame.bottom) 104 frame.OffsetBy(0.0, -(frame.bottom - screenFrame.bottom)); 105 if (frame.left < screenFrame.left) 106 frame.OffsetBy((screenFrame.left - frame.left), 0.0); 107 if (frame.top < screenFrame.top) 108 frame.OffsetBy(0.0, (screenFrame.top - frame.top)); 109 } 110 success = true; 111 } 112 delete screen; 113 return success; 114 } 115 116