xref: /haiku/src/add-ons/translators/shared/TranslatorWindow.cpp (revision b289aaf66bbf6e173aa90fa194fc256965f1b34d)
1 /*****************************************************************************/
2 // TranslatorWindow
3 // Written by Michael Wilber, OBOS 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 OpenBeOS 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 // Constructor
39 //
40 // Sets up the BWindow for holding a Translator's BView object
41 //
42 // Preconditions:
43 //
44 // Parameters: area,	The bounds of the window
45 //
46 // Postconditions:
47 //
48 // Returns:
49 // ---------------------------------------------------------------
50 TranslatorWindow::TranslatorWindow(BRect area, const char *title)
51 	:	BWindow(area, title, B_TITLED_WINDOW,
52 			B_NOT_RESIZABLE | B_NOT_ZOOMABLE | B_AUTO_UPDATE_SIZE_LIMITS)
53 {
54 	SetLayout(new BGroupLayout(B_HORIZONTAL));
55 		// Set the layout on layout window
56 		// Do nothing for a non-layout window
57 }
58 
59 // ---------------------------------------------------------------
60 // Destructor
61 //
62 // Posts a quit message so that the application is close properly
63 //
64 // Preconditions:
65 //
66 // Parameters:
67 //
68 // Postconditions:
69 //
70 // Returns:
71 // ---------------------------------------------------------------
72 TranslatorWindow::~TranslatorWindow()
73 {
74 	be_app->PostMessage(B_QUIT_REQUESTED);
75 }
76 
77 status_t
78 LaunchTranslatorWindow(BTranslator *translator, const char *title, BRect rect)
79 {
80 	BView *view = NULL;
81 	if (translator->MakeConfigurationView(NULL, &view, &rect)) {
82 		BAlert *err = new BAlert("Error",
83 			"Unable to create the view.", "OK");
84 		err->Go();
85 		return B_ERROR;
86 	}
87 	// release the translator even though I never really used it anyway
88 	translator->Release();
89 	translator = NULL;
90 
91 	TranslatorWindow *wnd = new TranslatorWindow(rect, title);
92 	wnd->AddChild(view);
93 	BPoint wndpt = B_ORIGIN;
94 	{
95 		BScreen scrn;
96 		BRect frame = scrn.Frame();
97 		frame.InsetBy(10, 23);
98 		// if the point is outside of the screen frame,
99 		// use the mouse location to find a better point
100 		if (!frame.Contains(wndpt)) {
101 			uint32 dummy;
102 			view->GetMouse(&wndpt, &dummy, false);
103 			wndpt.x -= rect.Width() / 2;
104 			wndpt.y -= rect.Height() / 2;
105 			// clamp location to screen
106 			if (wndpt.x < frame.left)
107 				wndpt.x = frame.left;
108 			if (wndpt.y < frame.top)
109 				wndpt.y = frame.top;
110 			if (wndpt.x > frame.right)
111 				wndpt.x = frame.right;
112 			if (wndpt.y > frame.bottom)
113 				wndpt.y = frame.bottom;
114 		}
115 	}
116 	wnd->MoveTo(wndpt);
117 	wnd->Show();
118 
119 	return B_OK;
120 }
121 
122