xref: /haiku/src/tools/translation/inspector/ImageWindow.cpp (revision 56abf4aa37ee075d3a2f5a1fad32b6eb9cb33c2c)
1 /*****************************************************************************/
2 // ImageWindow
3 // Written by Michael Wilber, OBOS Translation Kit Team
4 //
5 // ImageWindow.cpp
6 //
7 // BWindow class for displaying an image.  Uses ImageView class for its
8 // view.
9 //
10 //
11 // Copyright (c) 2003 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 "ImageWindow.h"
33 #include "Constants.h"
34 #include <Application.h>
35 #include <Catalog.h>
36 #include <Locale.h>
37 #include <MenuBar.h>
38 #include <MenuItem.h>
39 #include <Menu.h>
40 #include <ScrollView.h>
41 #include <Alert.h>
42 
43 #undef B_TRANSLATION_CONTEXT
44 #define B_TRANSLATION_CONTEXT "ImageWindow"
45 
46 
47 ImageWindow::ImageWindow(BRect rect, const char *name)
48 	: BWindow(rect, name, B_DOCUMENT_WINDOW, 0)
49 {
50 	// Setup menu bar
51 	BRect rctbar(0, 0, 100, 10);
52 	BMenuBar *pbar = new BMenuBar(rctbar, "MenuBar");
53 
54 	BMenu *pmnufile = new BMenu(B_TRANSLATE("File"));
55 	BMenuItem *pitmopen = new BMenuItem(B_TRANSLATE("Open" B_UTF8_ELLIPSIS),
56 		new BMessage(M_OPEN_IMAGE), 'O', 0);
57 
58 	BMenuItem *pitmsave = new BMenuItem(B_TRANSLATE("Save" B_UTF8_ELLIPSIS),
59 		new BMessage(M_SAVE_IMAGE), 'S', 0);
60 
61 	BMenuItem *pitmquit = new BMenuItem(B_TRANSLATE("Quit"),
62 		new BMessage(B_QUIT_REQUESTED), 'Q', 0);
63 
64 	pmnufile->AddItem(pitmopen);
65 	pmnufile->AddItem(pitmsave);
66 	pmnufile->AddSeparatorItem();
67 	pmnufile->AddItem(pitmquit);
68 	pbar->AddItem(pmnufile);
69 
70 	BMenu *pmnuview = new BMenu(B_TRANSLATE("View"));
71 	BMenuItem *pitmfirst = new BMenuItem(B_TRANSLATE("First Page"),
72 		new BMessage(M_VIEW_FIRST_PAGE), 'F', 0);
73 
74 	BMenuItem *pitmlast = new BMenuItem(B_TRANSLATE("Last Page"),
75 		new BMessage(M_VIEW_LAST_PAGE), 'L', 0);
76 
77 	BMenuItem *pitmnext = new BMenuItem(B_TRANSLATE("Next Page"),
78 		new BMessage(M_VIEW_NEXT_PAGE), 'N', 0);
79 
80 	BMenuItem *pitmprev = new BMenuItem(B_TRANSLATE("Previous Page"),
81 		new BMessage(M_VIEW_PREV_PAGE), 'P', 0);
82 
83 	pmnuview->AddItem(pitmfirst);
84 	pmnuview->AddItem(pitmlast);
85 	pmnuview->AddItem(pitmnext);
86 	pmnuview->AddItem(pitmprev);
87 	pbar->AddItem(pmnuview);
88 
89 
90 	BMenu *pmnuwindow = new BMenu(B_TRANSLATE("Window"));
91 	BMenuItem *pitmactives = new BMenuItem(B_TRANSLATE("Active Translators"),
92 		new BMessage(M_ACTIVE_TRANSLATORS_WINDOW), 'T', 0);
93 	pitmactives->SetTarget(be_app);
94 
95 	BMenuItem *pitminfo = new BMenuItem(B_TRANSLATE("Info"),
96 		new BMessage(M_INFO_WINDOW), 'I', 0);
97 	pitminfo->SetTarget(be_app);
98 
99 	pmnuwindow->AddItem(pitmactives);
100 	pmnuwindow->AddItem(pitminfo);
101 	pbar->AddItem(pmnuwindow);
102 
103 	AddChild(pbar);
104 
105 	// Setup image view
106 	BRect rctview = Bounds();
107 	rctview.top = pbar->Frame().bottom + 1;
108 	rctview.right -= B_V_SCROLL_BAR_WIDTH;
109 	rctview.bottom -= B_H_SCROLL_BAR_HEIGHT;
110 
111 	fpimageView = new ImageView(rctview, "ImageView");
112 	AddChild(new BScrollView("ImageScroll", fpimageView,
113 		B_FOLLOW_ALL_SIDES, 0, true, true));
114 
115 	// Setup file open panel
116 	BMessenger messenger(this);
117 	BMessage message(M_OPEN_FILE_PANEL);
118 	fpopenPanel = new BFilePanel(B_OPEN_PANEL, &messenger, NULL, 0L, false,
119 		&message, NULL, false, true);
120 
121 	SetSizeLimits(200, 10000, 150, 10000);
122 }
123 
124 ImageWindow::~ImageWindow()
125 {
126 	delete fpopenPanel;
127 	fpopenPanel = NULL;
128 }
129 
130 void
131 ImageWindow::MessageReceived(BMessage *pmsg)
132 {
133 	switch (pmsg->what) {
134 		case M_OPEN_IMAGE:
135 			fpopenPanel->Window()->SetWorkspaces(B_CURRENT_WORKSPACE);
136 			fpopenPanel->Show();
137 			break;
138 
139 		case M_SAVE_IMAGE:
140 			if (fpimageView->HasImage()) {
141 				BAlert *palert = new BAlert(NULL,
142 					B_TRANSLATE("Save feature not implemented yet."),
143 					B_TRANSLATE("Bummer"));
144 				palert->Go();
145 			} else {
146 				BAlert *palert = new BAlert(NULL,
147 					B_TRANSLATE("No image available to save."),
148 					B_TRANSLATE("OK"));
149 				palert->Go();
150 			}
151 			break;
152 
153 		case M_OPEN_FILE_PANEL:
154 		case B_SIMPLE_DATA:
155 			fpimageView->SetImage(pmsg);
156 			break;
157 
158 		case M_VIEW_FIRST_PAGE:
159 			fpimageView->FirstPage();
160 			break;
161 		case M_VIEW_LAST_PAGE:
162 			fpimageView->LastPage();
163 			break;
164 		case M_VIEW_NEXT_PAGE:
165 			fpimageView->NextPage();
166 			break;
167 		case M_VIEW_PREV_PAGE:
168 			fpimageView->PrevPage();
169 			break;
170 
171 		case B_CANCEL:
172 			break;
173 
174 		default:
175 			BWindow::MessageReceived(pmsg);
176 			break;
177 	}
178 }
179 
180 bool
181 ImageWindow::QuitRequested()
182 {
183 	be_app->PostMessage(B_QUIT_REQUESTED);
184 	return true;
185 }
186 
187