xref: /haiku/src/apps/showimage/ShowImageWindow.cpp (revision 03839579020b2f3c91671fbaa4abf7d260624935)
1cdb89ebbSMatthew Wilber /*****************************************************************************/
2cdb89ebbSMatthew Wilber // ShowImageWindow
3cdb89ebbSMatthew Wilber // Written by Fernando Francisco de Oliveira, Michael Wilber
4cdb89ebbSMatthew Wilber //
5cdb89ebbSMatthew Wilber // ShowImageWindow.cpp
6cdb89ebbSMatthew Wilber //
7cdb89ebbSMatthew Wilber //
8cdb89ebbSMatthew Wilber // Copyright (c) 2003 OpenBeOS Project
9cdb89ebbSMatthew Wilber //
10cdb89ebbSMatthew Wilber // Permission is hereby granted, free of charge, to any person obtaining a
11cdb89ebbSMatthew Wilber // copy of this software and associated documentation files (the "Software"),
12cdb89ebbSMatthew Wilber // to deal in the Software without restriction, including without limitation
13cdb89ebbSMatthew Wilber // the rights to use, copy, modify, merge, publish, distribute, sublicense,
14cdb89ebbSMatthew Wilber // and/or sell copies of the Software, and to permit persons to whom the
15cdb89ebbSMatthew Wilber // Software is furnished to do so, subject to the following conditions:
16cdb89ebbSMatthew Wilber //
17cdb89ebbSMatthew Wilber // The above copyright notice and this permission notice shall be included
18cdb89ebbSMatthew Wilber // in all copies or substantial portions of the Software.
19cdb89ebbSMatthew Wilber //
20cdb89ebbSMatthew Wilber // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21cdb89ebbSMatthew Wilber // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22cdb89ebbSMatthew Wilber // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23cdb89ebbSMatthew Wilber // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24cdb89ebbSMatthew Wilber // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25cdb89ebbSMatthew Wilber // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26cdb89ebbSMatthew Wilber // DEALINGS IN THE SOFTWARE.
27cdb89ebbSMatthew Wilber /*****************************************************************************/
2813afa642SPhil Greenway 
2913afa642SPhil Greenway #include <algobase.h>
3007caaa8eSMatthew Wilber #include <stdio.h>
3113afa642SPhil Greenway #include <Application.h>
3213afa642SPhil Greenway #include <Bitmap.h>
33fc0a275bSMatthew Wilber #include <BitmapStream.h>
3413afa642SPhil Greenway #include <Entry.h>
35fc0a275bSMatthew Wilber #include <File.h>
3613afa642SPhil Greenway #include <Menu.h>
3713afa642SPhil Greenway #include <MenuBar.h>
3813afa642SPhil Greenway #include <MenuItem.h>
3913afa642SPhil Greenway #include <Path.h>
4013afa642SPhil Greenway #include <ScrollView.h>
4113afa642SPhil Greenway #include <TranslationUtils.h>
4213afa642SPhil Greenway #include <TranslatorRoster.h>
4313afa642SPhil Greenway #include <Alert.h>
4407caaa8eSMatthew Wilber #include <SupportDefs.h>
4513afa642SPhil Greenway 
4613afa642SPhil Greenway #include "ShowImageConstants.h"
4713afa642SPhil Greenway #include "ShowImageWindow.h"
4813afa642SPhil Greenway #include "ShowImageView.h"
4913afa642SPhil Greenway #include "ShowImageStatusView.h"
5013afa642SPhil Greenway 
51*03839579SMatthew Wilber ShowImageWindow::ShowImageWindow(const entry_ref *pref)
52cdb89ebbSMatthew Wilber 	: BWindow(BRect(50, 50, 350, 250), "", B_DOCUMENT_WINDOW, 0)
5313afa642SPhil Greenway {
54fc0a275bSMatthew Wilber 	fpsavePanel = NULL;
55cdb89ebbSMatthew Wilber 	fpref = NULL;
56fc0a275bSMatthew Wilber 
5713afa642SPhil Greenway 	// create menu bar
58cdb89ebbSMatthew Wilber 	fpbar = new BMenuBar(BRect(0, 0, Bounds().right, 20), "menu_bar");
59cdb89ebbSMatthew Wilber 	LoadMenus(fpbar);
60cdb89ebbSMatthew Wilber 	AddChild(fpbar);
6113afa642SPhil Greenway 
6213afa642SPhil Greenway 	BRect viewFrame = Bounds();
63cdb89ebbSMatthew Wilber 	viewFrame.top		= fpbar->Bounds().bottom + 1;
6413afa642SPhil Greenway 	viewFrame.right		-= B_V_SCROLL_BAR_WIDTH;
6513afa642SPhil Greenway 	viewFrame.bottom	-= B_H_SCROLL_BAR_HEIGHT;
6613afa642SPhil Greenway 
6713afa642SPhil Greenway 	// create the image view
68cdb89ebbSMatthew Wilber 	fpimageView = new ShowImageView(viewFrame, "image_view", B_FOLLOW_ALL,
6913afa642SPhil Greenway 		B_WILL_DRAW | B_FRAME_EVENTS | B_PULSE_NEEDED);
7013afa642SPhil Greenway 	// wrap a scroll view around the view
71cdb89ebbSMatthew Wilber 	BScrollView *pscrollView = new BScrollView("image_scroller", fpimageView,
72cdb89ebbSMatthew Wilber 		B_FOLLOW_ALL, 0, false, false, B_PLAIN_BORDER);
73cdb89ebbSMatthew Wilber 	AddChild(pscrollView);
7413afa642SPhil Greenway 
7507caaa8eSMatthew Wilber 	const int32 kstatusWidth = 190;
76cdb89ebbSMatthew Wilber 	BRect rect;
7713afa642SPhil Greenway 	rect = Bounds();
7813afa642SPhil Greenway 	rect.top	= viewFrame.bottom + 1;
7907caaa8eSMatthew Wilber 	rect.left 	= viewFrame.left + kstatusWidth;
8013afa642SPhil Greenway 	rect.right	= viewFrame.right;
81cdb89ebbSMatthew Wilber 	BScrollBar *phscroll;
82cdb89ebbSMatthew Wilber 	phscroll = new BScrollBar(rect, "hscroll", fpimageView, 0, 150,
83cdb89ebbSMatthew Wilber 		B_HORIZONTAL);
84cdb89ebbSMatthew Wilber 	AddChild(phscroll);
8513afa642SPhil Greenway 
8613afa642SPhil Greenway 	rect.left = 0;
8707caaa8eSMatthew Wilber 	rect.right = kstatusWidth - 1;
88*03839579SMatthew Wilber 	fpstatusView = new ShowImageStatusView(rect, "status_view", B_FOLLOW_BOTTOM,
89cdb89ebbSMatthew Wilber 		B_WILL_DRAW);
90*03839579SMatthew Wilber 	fpstatusView->SetViewColor(ui_color(B_MENU_BACKGROUND_COLOR));
91*03839579SMatthew Wilber 	AddChild(fpstatusView);
9213afa642SPhil Greenway 
9313afa642SPhil Greenway 	rect = Bounds();
9413afa642SPhil Greenway 	rect.top    = viewFrame.top;
9513afa642SPhil Greenway 	rect.left 	= viewFrame.right + 1;
9613afa642SPhil Greenway 	rect.bottom	= viewFrame.bottom;
97cdb89ebbSMatthew Wilber 	BScrollBar *pvscroll;
98cdb89ebbSMatthew Wilber 	pvscroll = new BScrollBar(rect, "vscroll", fpimageView, 0, 150, B_VERTICAL);
99cdb89ebbSMatthew Wilber 	AddChild(pvscroll);
10013afa642SPhil Greenway 
101*03839579SMatthew Wilber 	SetSizeLimits(250, 100000, 100, 100000);
10213afa642SPhil Greenway 
10313afa642SPhil Greenway 	// finish creating window
104cdb89ebbSMatthew Wilber 	SetRef(pref);
10513afa642SPhil Greenway 	UpdateTitle();
10613afa642SPhil Greenway 
107*03839579SMatthew Wilber 	fpimageView->SetImage(pref);
108*03839579SMatthew Wilber 
10913afa642SPhil Greenway 	Show();
11013afa642SPhil Greenway }
11113afa642SPhil Greenway 
11213afa642SPhil Greenway ShowImageWindow::~ShowImageWindow()
11313afa642SPhil Greenway {
114cdb89ebbSMatthew Wilber 	delete fpref;
11513afa642SPhil Greenway }
11613afa642SPhil Greenway 
117cdb89ebbSMatthew Wilber status_t
118cdb89ebbSMatthew Wilber ShowImageWindow::InitCheck()
11913afa642SPhil Greenway {
120cdb89ebbSMatthew Wilber 	if (!fpref || !fpimageView)
12113afa642SPhil Greenway 		return B_ERROR;
122cdb89ebbSMatthew Wilber 	else
12313afa642SPhil Greenway 		return B_OK;
12413afa642SPhil Greenway }
125cdb89ebbSMatthew Wilber 
126cdb89ebbSMatthew Wilber void
127cdb89ebbSMatthew Wilber ShowImageWindow::SetRef(const entry_ref *pref)
128cdb89ebbSMatthew Wilber {
129cdb89ebbSMatthew Wilber 	if (!fpref)
130cdb89ebbSMatthew Wilber 		fpref = new entry_ref(*pref);
131cdb89ebbSMatthew Wilber 	else
132cdb89ebbSMatthew Wilber 		*fpref = *pref;
13313afa642SPhil Greenway }
13413afa642SPhil Greenway 
135cdb89ebbSMatthew Wilber void
136cdb89ebbSMatthew Wilber ShowImageWindow::UpdateTitle()
13713afa642SPhil Greenway {
138cdb89ebbSMatthew Wilber 	BEntry entry(fpref);
13913afa642SPhil Greenway 	if (entry.InitCheck() == B_OK) {
14013afa642SPhil Greenway 		BPath path;
14113afa642SPhil Greenway 		entry.GetPath(&path);
142cdb89ebbSMatthew Wilber 		if (path.InitCheck() == B_OK)
14313afa642SPhil Greenway 			SetTitle(path.Path());
14413afa642SPhil Greenway 	}
14513afa642SPhil Greenway }
14613afa642SPhil Greenway 
147cdb89ebbSMatthew Wilber void
148cdb89ebbSMatthew Wilber ShowImageWindow::LoadMenus(BMenuBar *pbar)
14913afa642SPhil Greenway {
150cdb89ebbSMatthew Wilber 	BMenu *pmenu = new BMenu("File");
151cdb89ebbSMatthew Wilber 	AddItemMenu(pmenu, "Open", MSG_FILE_OPEN, 'O', 0, 'A', true);
152cdb89ebbSMatthew Wilber 	pmenu->AddSeparatorItem();
153cdb89ebbSMatthew Wilber 	BMenu *pmenuSaveAs = new BMenu("Save As...", B_ITEMS_IN_COLUMN);
154cdb89ebbSMatthew Wilber 	BTranslationUtils::AddTranslationItems(pmenuSaveAs, B_TRANSLATOR_BITMAP);
155fc0a275bSMatthew Wilber 		// Fill Save As submenu with all types that can be converted
156fc0a275bSMatthew Wilber 		// to from the Be bitmap image format
157cdb89ebbSMatthew Wilber 	pmenu->AddItem(pmenuSaveAs);
158cdb89ebbSMatthew Wilber 	AddItemMenu(pmenu, "Close", MSG_CLOSE, 'W', 0, 'W', true);
159cdb89ebbSMatthew Wilber 	pmenu->AddSeparatorItem();
160cdb89ebbSMatthew Wilber 	AddItemMenu(pmenu, "About ShowImage...", B_ABOUT_REQUESTED, 0, 0, 'A', true);
161cdb89ebbSMatthew Wilber 	pmenu->AddSeparatorItem();
162cdb89ebbSMatthew Wilber 	AddItemMenu(pmenu, "Quit", B_QUIT_REQUESTED, 'Q', 0, 'A', true);
163cdb89ebbSMatthew Wilber 	pbar->AddItem(pmenu);
16413afa642SPhil Greenway 
165cdb89ebbSMatthew Wilber 	pmenu = new BMenu("Edit");
166cdb89ebbSMatthew Wilber 	AddItemMenu(pmenu, "Undo", B_UNDO, 'Z', 0, 'W', false);
167cdb89ebbSMatthew Wilber 	pmenu->AddSeparatorItem();
168cdb89ebbSMatthew Wilber 	AddItemMenu(pmenu, "Cut", B_CUT, 'X', 0, 'W', false);
169cdb89ebbSMatthew Wilber 	AddItemMenu(pmenu, "Copy", B_COPY, 'C', 0, 'W', false);
170cdb89ebbSMatthew Wilber 	AddItemMenu(pmenu, "Paste", B_PASTE, 'V', 0, 'W', false);
171cdb89ebbSMatthew Wilber 	AddItemMenu(pmenu, "Clear", MSG_CLEAR_SELECT, 0, 0, 'W', false);
172cdb89ebbSMatthew Wilber 	pmenu->AddSeparatorItem();
173cdb89ebbSMatthew Wilber 	AddItemMenu(pmenu, "Select All", MSG_SELECT_ALL, 'A', 0, 'W', false);
174cdb89ebbSMatthew Wilber 	pbar->AddItem(pmenu);
17513afa642SPhil Greenway 
176*03839579SMatthew Wilber 	pmenu = new BMenu("Page");
177*03839579SMatthew Wilber 	AddItemMenu(pmenu, "First", MSG_PAGE_FIRST, 'F', 0, 'W', true);
178*03839579SMatthew Wilber 	AddItemMenu(pmenu, "Last", MSG_PAGE_LAST, 'L', 0, 'W', true);
179*03839579SMatthew Wilber 	AddItemMenu(pmenu, "Next", MSG_PAGE_NEXT, 'N', 0, 'W', true);
180*03839579SMatthew Wilber 	AddItemMenu(pmenu, "Previous", MSG_PAGE_PREV, 'P', 0, 'W', true);
181*03839579SMatthew Wilber 	pbar->AddItem(pmenu);
182*03839579SMatthew Wilber 
183cdb89ebbSMatthew Wilber 	pmenu = new BMenu("Image");
184cdb89ebbSMatthew Wilber 	AddItemMenu(pmenu, "Dither Image", MSG_DITHER_IMAGE, 0, 0, 'W', true);
185cdb89ebbSMatthew Wilber 	pbar->AddItem(pmenu);
18613afa642SPhil Greenway }
18713afa642SPhil Greenway 
188cdb89ebbSMatthew Wilber BMenuItem *
189cdb89ebbSMatthew Wilber ShowImageWindow::AddItemMenu(BMenu *pmenu, char *caption, long unsigned int msg,
190cdb89ebbSMatthew Wilber 	char shortcut, uint32 modifier, char target, bool benabled)
191cdb89ebbSMatthew Wilber {
192cdb89ebbSMatthew Wilber 	BMenuItem* pitem;
193cdb89ebbSMatthew Wilber 	pitem = new BMenuItem(caption, new BMessage(msg), shortcut);
19413afa642SPhil Greenway 
19513afa642SPhil Greenway 	if (target == 'A')
196cdb89ebbSMatthew Wilber 	   pitem->SetTarget(be_app);
19713afa642SPhil Greenway 
198cdb89ebbSMatthew Wilber 	pitem->SetEnabled(benabled);
199cdb89ebbSMatthew Wilber 	pmenu->AddItem(pitem);
20013afa642SPhil Greenway 
201cdb89ebbSMatthew Wilber 	return pitem;
20213afa642SPhil Greenway }
20313afa642SPhil Greenway 
204cdb89ebbSMatthew Wilber void
205cdb89ebbSMatthew Wilber ShowImageWindow::WindowRedimension(BBitmap *pbitmap)
20613afa642SPhil Greenway {
20713afa642SPhil Greenway 	// set the window's min & max size limits
20813afa642SPhil Greenway 	// based on document's data bounds
209cdb89ebbSMatthew Wilber 	float maxWidth = pbitmap->Bounds().Width() + B_V_SCROLL_BAR_WIDTH;
210cdb89ebbSMatthew Wilber 	float maxHeight = pbitmap->Bounds().Height() + fpbar->Frame().Height() +
211cdb89ebbSMatthew Wilber 		B_H_SCROLL_BAR_HEIGHT + 1;
21213afa642SPhil Greenway 	float minWidth = min(maxWidth, 100.0f);
21313afa642SPhil Greenway 	float minHeight = min(maxHeight, 100.0f);
21413afa642SPhil Greenway 
21513afa642SPhil Greenway 	// adjust the window's current size based on new min/max values
21613afa642SPhil Greenway 	float curWidth = Bounds().Width();
21713afa642SPhil Greenway 	float curHeight = Bounds().Height();
218cdb89ebbSMatthew Wilber 	if (curWidth < minWidth)
21913afa642SPhil Greenway 		curWidth = minWidth;
220cdb89ebbSMatthew Wilber 	else if (curWidth > maxWidth)
22113afa642SPhil Greenway 		curWidth = maxWidth;
222cdb89ebbSMatthew Wilber 
223cdb89ebbSMatthew Wilber 	if (curHeight < minHeight)
22413afa642SPhil Greenway 		curHeight = minHeight;
225cdb89ebbSMatthew Wilber 	else if (curHeight > maxHeight)
22613afa642SPhil Greenway 		curHeight = maxHeight;
227cdb89ebbSMatthew Wilber 
228cdb89ebbSMatthew Wilber 	if (minWidth < 250)
22913afa642SPhil Greenway 		minWidth = 250;
230cdb89ebbSMatthew Wilber 
23113afa642SPhil Greenway 	SetSizeLimits(minWidth, maxWidth, minHeight, maxHeight);
23213afa642SPhil Greenway 	ResizeTo(curWidth, curHeight);
23313afa642SPhil Greenway }
23413afa642SPhil Greenway 
235cdb89ebbSMatthew Wilber void
236cdb89ebbSMatthew Wilber ShowImageWindow::FrameResized(float width, float height)
23713afa642SPhil Greenway {
23813afa642SPhil Greenway }
23913afa642SPhil Greenway 
240cdb89ebbSMatthew Wilber void
241cdb89ebbSMatthew Wilber ShowImageWindow::MessageReceived(BMessage *pmsg)
24213afa642SPhil Greenway {
243cdb89ebbSMatthew Wilber 	switch (pmsg->what) {
244fc0a275bSMatthew Wilber 		case MSG_OUTPUT_TYPE:
245fc0a275bSMatthew Wilber 			// User clicked Save As then choose an output format
246cdb89ebbSMatthew Wilber 			if (!fpsavePanel)
247cdb89ebbSMatthew Wilber 				// If user doesn't already have a save panel open
248cdb89ebbSMatthew Wilber 				SaveAs(pmsg);
24913afa642SPhil Greenway 			break;
250fc0a275bSMatthew Wilber 
251fc0a275bSMatthew Wilber 		case MSG_SAVE_PANEL:
252fc0a275bSMatthew Wilber 			// User specified where to save the output image
253cdb89ebbSMatthew Wilber 			SaveToFile(pmsg);
254fc0a275bSMatthew Wilber 			break;
255fc0a275bSMatthew Wilber 
25607caaa8eSMatthew Wilber 		case MSG_CLOSE:
257cdb89ebbSMatthew Wilber 			if (CanQuit())
25807caaa8eSMatthew Wilber 				Quit();
25907caaa8eSMatthew Wilber 			break;
26007caaa8eSMatthew Wilber 
261cdb89ebbSMatthew Wilber 		case B_CANCEL:
262cdb89ebbSMatthew Wilber 			delete fpsavePanel;
263cdb89ebbSMatthew Wilber 			fpsavePanel = NULL;
264cdb89ebbSMatthew Wilber 			break;
265cdb89ebbSMatthew Wilber 
266*03839579SMatthew Wilber 		case MSG_UPDATE_STATUS:
267*03839579SMatthew Wilber 		{
268*03839579SMatthew Wilber 			BString str;
269*03839579SMatthew Wilber 			if (pmsg->FindString("status", &str) == B_OK)
270*03839579SMatthew Wilber 				fpstatusView->SetText(str);
271*03839579SMatthew Wilber 			break;
272*03839579SMatthew Wilber 		}
273*03839579SMatthew Wilber 
27413afa642SPhil Greenway 		case B_UNDO:
27513afa642SPhil Greenway 			break;
27613afa642SPhil Greenway 		case B_CUT:
27713afa642SPhil Greenway 			break;
27813afa642SPhil Greenway 		case B_COPY:
27913afa642SPhil Greenway 			break;
28013afa642SPhil Greenway 		case B_PASTE:
28113afa642SPhil Greenway 			break;
28213afa642SPhil Greenway 		case MSG_CLEAR_SELECT:
28313afa642SPhil Greenway 			break;
28413afa642SPhil Greenway 		case MSG_SELECT_ALL:
28513afa642SPhil Greenway 			break;
28613afa642SPhil Greenway 
287*03839579SMatthew Wilber 		case MSG_PAGE_FIRST:
288*03839579SMatthew Wilber 			fpimageView->FirstPage();
289*03839579SMatthew Wilber 			break;
290*03839579SMatthew Wilber 
291*03839579SMatthew Wilber 		case MSG_PAGE_LAST:
292*03839579SMatthew Wilber 			fpimageView->LastPage();
293*03839579SMatthew Wilber 			break;
294*03839579SMatthew Wilber 
295*03839579SMatthew Wilber 		case MSG_PAGE_NEXT:
296*03839579SMatthew Wilber 			fpimageView->NextPage();
297*03839579SMatthew Wilber 			break;
298*03839579SMatthew Wilber 
299*03839579SMatthew Wilber 		case MSG_PAGE_PREV:
300*03839579SMatthew Wilber 			fpimageView->PrevPage();
301*03839579SMatthew Wilber 			break;
302*03839579SMatthew Wilber 
303cdb89ebbSMatthew Wilber 		case MSG_DITHER_IMAGE:
304cdb89ebbSMatthew Wilber 		     BMenuItem *pmenuDither;
305cdb89ebbSMatthew Wilber 	    	 pmenuDither = fpbar->FindItem(pmsg->what);
306cdb89ebbSMatthew Wilber 		     pmenuDither->SetMarked(!pmenuDither->IsMarked());
30713afa642SPhil Greenway 		     break;
30813afa642SPhil Greenway 
30913afa642SPhil Greenway 		default:
310cdb89ebbSMatthew Wilber 			BWindow::MessageReceived(pmsg);
31113afa642SPhil Greenway 			break;
31213afa642SPhil Greenway 	}
31313afa642SPhil Greenway }
314fc0a275bSMatthew Wilber 
315fc0a275bSMatthew Wilber void
316fc0a275bSMatthew Wilber ShowImageWindow::SaveAs(BMessage *pmsg)
317fc0a275bSMatthew Wilber {
318cdb89ebbSMatthew Wilber 	// Read the translator and output type the user chose
319cdb89ebbSMatthew Wilber 	translator_id outTranslator;
320cdb89ebbSMatthew Wilber 	uint32 outType;
321cdb89ebbSMatthew Wilber 	if (pmsg->FindInt32(TRANSLATOR_FLD,
322cdb89ebbSMatthew Wilber 		reinterpret_cast<int32 *>(&outTranslator)) != B_OK)
323fc0a275bSMatthew Wilber 		return;
324cdb89ebbSMatthew Wilber 	if (pmsg->FindInt32(TYPE_FLD,
325cdb89ebbSMatthew Wilber 		reinterpret_cast<int32 *>(&outType)) != B_OK)
326fc0a275bSMatthew Wilber 		return;
327fc0a275bSMatthew Wilber 
328cdb89ebbSMatthew Wilber 	// Add the chosen translator and output type to the
329cdb89ebbSMatthew Wilber 	// message that the save panel will send back
330cdb89ebbSMatthew Wilber 	BMessage *ppanelMsg = new BMessage(MSG_SAVE_PANEL);
331cdb89ebbSMatthew Wilber 	ppanelMsg->AddInt32(TRANSLATOR_FLD, outTranslator);
332cdb89ebbSMatthew Wilber 	ppanelMsg->AddInt32(TYPE_FLD, outType);
333cdb89ebbSMatthew Wilber 
334cdb89ebbSMatthew Wilber 	// Create save panel and show it
335fc0a275bSMatthew Wilber 	fpsavePanel = new BFilePanel(B_SAVE_PANEL, new BMessenger(this), NULL, 0,
336cdb89ebbSMatthew Wilber 		false, ppanelMsg);
337fc0a275bSMatthew Wilber 	if (!fpsavePanel)
338fc0a275bSMatthew Wilber 		return;
339fc0a275bSMatthew Wilber 	fpsavePanel->Window()->SetWorkspaces(B_CURRENT_WORKSPACE);
340fc0a275bSMatthew Wilber 	fpsavePanel->Show();
341fc0a275bSMatthew Wilber }
342fc0a275bSMatthew Wilber 
343fc0a275bSMatthew Wilber void
344fc0a275bSMatthew Wilber ShowImageWindow::SaveToFile(BMessage *pmsg)
345fc0a275bSMatthew Wilber {
346cdb89ebbSMatthew Wilber 	// Read in where the file should be saved
347fc0a275bSMatthew Wilber 	entry_ref dirref;
348fc0a275bSMatthew Wilber 	if (pmsg->FindRef("directory", &dirref) != B_OK)
349fc0a275bSMatthew Wilber 		return;
350fc0a275bSMatthew Wilber 	const char *filename;
351fc0a275bSMatthew Wilber 	if (pmsg->FindString("name", &filename) != B_OK)
352fc0a275bSMatthew Wilber 		return;
353fc0a275bSMatthew Wilber 
354cdb89ebbSMatthew Wilber 	// Read in the translator and type to be used
355cdb89ebbSMatthew Wilber 	// to save the output image
356cdb89ebbSMatthew Wilber 	translator_id outTranslator;
357cdb89ebbSMatthew Wilber 	uint32 outType;
358cdb89ebbSMatthew Wilber 	if (pmsg->FindInt32(TRANSLATOR_FLD,
359cdb89ebbSMatthew Wilber 		reinterpret_cast<int32 *>(&outTranslator)) != B_OK)
360cdb89ebbSMatthew Wilber 		return;
361cdb89ebbSMatthew Wilber 	if (pmsg->FindInt32(TYPE_FLD,
362cdb89ebbSMatthew Wilber 		reinterpret_cast<int32 *>(&outType)) != B_OK)
363cdb89ebbSMatthew Wilber 		return;
364cdb89ebbSMatthew Wilber 
365cdb89ebbSMatthew Wilber 	// Create the output file
366fc0a275bSMatthew Wilber 	BDirectory dir(&dirref);
367fc0a275bSMatthew Wilber 	BFile file(&dir, filename, B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE);
368fc0a275bSMatthew Wilber 	if (file.InitCheck() != B_OK)
369fc0a275bSMatthew Wilber 		return;
370fc0a275bSMatthew Wilber 
371cdb89ebbSMatthew Wilber 	// Translate the image and write it out to the output file
372cdb89ebbSMatthew Wilber 	BBitmapStream stream(fpimageView->GetBitmap());
373fc0a275bSMatthew Wilber 	BTranslatorRoster *proster = BTranslatorRoster::Default();
374cdb89ebbSMatthew Wilber 	if (proster->Translate(outTranslator, &stream, NULL,
375cdb89ebbSMatthew Wilber 		&file, outType) != B_OK) {
376fc0a275bSMatthew Wilber 		BAlert *palert = new BAlert(NULL, "Error writing image file.", "Ok");
377fc0a275bSMatthew Wilber 		palert->Go();
378fc0a275bSMatthew Wilber 	}
379fc0a275bSMatthew Wilber 
380fc0a275bSMatthew Wilber 	BBitmap *pout = NULL;
381fc0a275bSMatthew Wilber 	stream.DetachBitmap(&pout);
382fc0a275bSMatthew Wilber 		// bitmap used by stream still belongs to the view,
383fc0a275bSMatthew Wilber 		// detach so it doesn't get deleted
384fc0a275bSMatthew Wilber }
385fc0a275bSMatthew Wilber 
386cdb89ebbSMatthew Wilber bool
387cdb89ebbSMatthew Wilber ShowImageWindow::CanQuit()
388cdb89ebbSMatthew Wilber {
389cdb89ebbSMatthew Wilber 	if (fpsavePanel)
390cdb89ebbSMatthew Wilber 		// Don't allow this window to be closed if a save panel is open
391cdb89ebbSMatthew Wilber 		return false;
392cdb89ebbSMatthew Wilber 	else
393cdb89ebbSMatthew Wilber 		return true;
394cdb89ebbSMatthew Wilber }
395cdb89ebbSMatthew Wilber 
396cdb89ebbSMatthew Wilber bool
397cdb89ebbSMatthew Wilber ShowImageWindow::QuitRequested()
398cdb89ebbSMatthew Wilber {
399cdb89ebbSMatthew Wilber 	return CanQuit();
400cdb89ebbSMatthew Wilber }
401cdb89ebbSMatthew Wilber 
40207caaa8eSMatthew Wilber void
40307caaa8eSMatthew Wilber ShowImageWindow::Quit()
40407caaa8eSMatthew Wilber {
40507caaa8eSMatthew Wilber 	// tell the app to forget about this window
40607caaa8eSMatthew Wilber 	be_app->PostMessage(MSG_WINDOW_QUIT);
40707caaa8eSMatthew Wilber 	BWindow::Quit();
40807caaa8eSMatthew Wilber }
409fc0a275bSMatthew Wilber 
410