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