1/*****************************************************************************/ 2// TranslatorWindow 3// Written by Michael Wilber, Haiku Translation Kit Team 4// 5// TranslatorWindow.cpp 6// 7// This BWindow based object is used to hold the Translator's BView object 8// when the user runs the Translator as an application. 9// 10// 11// Copyright (c) 2004 Haiku Project 12// 13// Permission is hereby granted, free of charge, to any person obtaining a 14// copy of this software and associated documentation files (the "Software"), 15// to deal in the Software without restriction, including without limitation 16// the rights to use, copy, modify, merge, publish, distribute, sublicense, 17// and/or sell copies of the Software, and to permit persons to whom the 18// Software is furnished to do so, subject to the following conditions: 19// 20// The above copyright notice and this permission notice shall be included 21// in all copies or substantial portions of the Software. 22// 23// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 24// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 25// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 26// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 27// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 28// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 29// DEALINGS IN THE SOFTWARE. 30/*****************************************************************************/ 31 32#include <Screen.h> 33#include <Alert.h> 34#include <GroupLayout.h> 35#include "TranslatorWindow.h" 36 37 38#undef B_TRANSLATION_CONTEXT 39#define B_TRANSLATION_CONTEXT "TranslatorWindow" 40 41 42// --------------------------------------------------------------- 43// Constructor 44// 45// Sets up the BWindow for holding a Translator's BView object 46// 47// Preconditions: 48// 49// Parameters: area, The bounds of the window 50// 51// Postconditions: 52// 53// Returns: 54// --------------------------------------------------------------- 55TranslatorWindow::TranslatorWindow(BRect area, const char *title) 56 : BWindow(area, title, B_TITLED_WINDOW, 57 B_NOT_RESIZABLE | B_NOT_ZOOMABLE | B_AUTO_UPDATE_SIZE_LIMITS) 58{ 59 SetLayout(new BGroupLayout(B_HORIZONTAL)); 60 // Set the layout on layout window 61 // Do nothing for a non-layout window 62} 63 64 65// --------------------------------------------------------------- 66// Destructor 67// 68// Posts a quit message so that the application is close properly 69// 70// Preconditions: 71// 72// Parameters: 73// 74// Postconditions: 75// 76// Returns: 77// --------------------------------------------------------------- 78TranslatorWindow::~TranslatorWindow() 79{ 80 be_app->PostMessage(B_QUIT_REQUESTED); 81} 82 83 84status_t 85LaunchTranslatorWindow(BTranslator *translator, const char *title, BRect rect) 86{ 87 BView *view = NULL; 88 if (translator->MakeConfigurationView(NULL, &view, &rect)) { 89 BAlert *err = new BAlert(B_TRANSLATE("Error"), 90 B_TRANSLATE("Unable to create the view."), B_TRANSLATE("OK")); 91 err->SetFlags(err->Flags() | B_CLOSE_ON_ESCAPE); 92 err->Go(); 93 return B_ERROR; 94 } 95 // release the translator even though I never really used it anyway 96 translator->Release(); 97 translator = NULL; 98 99 TranslatorWindow *wnd = new TranslatorWindow(rect, title); 100 wnd->AddChild(view); 101 BPoint wndpt = B_ORIGIN; 102 { 103 BScreen scrn; 104 BRect frame = scrn.Frame(); 105 frame.InsetBy(10, 23); 106 // if the point is outside of the screen frame, 107 // use the mouse location to find a better point 108 if (!frame.Contains(wndpt)) { 109 uint32 dummy; 110 view->GetMouse(&wndpt, &dummy, false); 111 wndpt.x -= rect.Width() / 2; 112 wndpt.y -= rect.Height() / 2; 113 // clamp location to screen 114 if (wndpt.x < frame.left) 115 wndpt.x = frame.left; 116 if (wndpt.y < frame.top) 117 wndpt.y = frame.top; 118 if (wndpt.x > frame.right) 119 wndpt.x = frame.right; 120 if (wndpt.y > frame.bottom) 121 wndpt.y = frame.bottom; 122 } 123 } 124 wnd->MoveTo(wndpt); 125 wnd->Show(); 126 127 return B_OK; 128} 129 130