xref: /haiku/src/add-ons/translators/ppm/PPMMain.cpp (revision 1deede7388b04dbeec5af85cae7164735ea9e70d)
1 /*
2 	Copyright 1999, Be Incorporated.   All Rights Reserved.
3 	This file may be used under the terms of the Be Sample Code License.
4 */
5 
6 #include <Alert.h>
7 #include <Application.h>
8 #include <Catalog.h>
9 #include <LayoutBuilder.h>
10 #include <Screen.h>
11 #include <TranslatorAddOn.h>
12 #include <View.h>
13 #include <Window.h>
14 
15 
16 #include <stdio.h>
17 
18 #undef B_TRANSLATION_CONTEXT
19 #define B_TRANSLATION_CONTEXT "PPMMain"
20 
21 
22 BPoint get_window_origin();
23 void set_window_origin(BPoint pt);
24 
25 class PPMWindow : public BWindow
26 {
27 public:
28 	PPMWindow(BRect area)
29 		: BWindow(area, B_TRANSLATE("PPM Settings"), B_TITLED_WINDOW,
30 			  B_NOT_RESIZABLE | B_NOT_ZOOMABLE | B_AUTO_UPDATE_SIZE_LIMITS)
31 	{
32 		BLayoutBuilder::Group<>(this, B_HORIZONTAL);
33 	}
34 	~PPMWindow()
35 	{
36 		BPoint pt(0, 0);
37 		ConvertToScreen(&pt);
38 		set_window_origin(pt);
39 		be_app->PostMessage(B_QUIT_REQUESTED);
40 	}
41 };
42 
43 int
44 main()
45 {
46 	BApplication app("application/x-vnd.Haiku-PPMTranslator");
47 	BView* v = NULL;
48 	BRect r(0, 0, 1, 1);
49 	if (MakeConfig(NULL, &v, &r)) {
50 		BAlert* err = new BAlert("Error",
51 			B_TRANSLATE("Something is wrong with the PPMTranslator!"),
52 			B_TRANSLATE("OK"));
53 		err->SetFlags(err->Flags() | B_CLOSE_ON_ESCAPE);
54 		err->Go();
55 		return 1;
56 	}
57 	PPMWindow* w = new PPMWindow(r);
58 	v->ResizeTo(r.Width(), r.Height());
59 	w->AddChild(v);
60 	BPoint o = get_window_origin();
61 	{
62 		BScreen scrn;
63 		BRect f = scrn.Frame();
64 		f.InsetBy(10, 23);
65 		/* if not in a good place, start where the cursor is */
66 		if (!f.Contains(o)) {
67 			uint32 i;
68 			v->GetMouse(&o, &i, false);
69 			o.x -= r.Width() / 2;
70 			o.y -= r.Height() / 2;
71 			/* clamp location to screen */
72 			if (o.x < f.left)
73 				o.x = f.left;
74 			if (o.y < f.top)
75 				o.y = f.top;
76 			if (o.x > f.right)
77 				o.x = f.right;
78 			if (o.y > f.bottom)
79 				o.y = f.bottom;
80 		}
81 	}
82 	w->MoveTo(o);
83 	w->Show();
84 	app.Run();
85 	return 0;
86 }
87