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