xref: /haiku/src/apps/showimage/ShowImageWindow.cpp (revision 7eca09874bd34b14ac63e3f28ec455fab810cc77)
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 
5103839579SMatthew Wilber ShowImageWindow::ShowImageWindow(const entry_ref *pref)
52cdb89ebbSMatthew Wilber 	: BWindow(BRect(50, 50, 350, 250), "", B_DOCUMENT_WINDOW, 0)
5313afa642SPhil Greenway {
54*7eca0987SMichael Pfeiffer 	fpSavePanel = NULL;
55*7eca0987SMichael Pfeiffer 	fpRef = NULL;
56*7eca0987SMichael Pfeiffer 
57*7eca0987SMichael Pfeiffer 	// add shortcuts
58*7eca0987SMichael Pfeiffer 	AddShortcut(B_UP_ARROW, B_COMMAND_KEY, new BMessage(MSG_FILE_PREV));
59*7eca0987SMichael Pfeiffer 	AddShortcut(B_DOWN_ARROW, B_COMMAND_KEY, new BMessage(MSG_FILE_NEXT));
60fc0a275bSMatthew Wilber 
6113afa642SPhil Greenway 	// create menu bar
62*7eca0987SMichael Pfeiffer 	fpBar = new BMenuBar(BRect(0, 0, Bounds().right, 20), "menu_bar");
63*7eca0987SMichael Pfeiffer 	LoadMenus(fpBar);
64*7eca0987SMichael Pfeiffer 	AddChild(fpBar);
6513afa642SPhil Greenway 
6613afa642SPhil Greenway 	BRect viewFrame = Bounds();
67*7eca0987SMichael Pfeiffer 	viewFrame.top		= fpBar->Bounds().bottom + 1;
6813afa642SPhil Greenway 	viewFrame.right		-= B_V_SCROLL_BAR_WIDTH;
6913afa642SPhil Greenway 	viewFrame.bottom	-= B_H_SCROLL_BAR_HEIGHT;
7013afa642SPhil Greenway 
7113afa642SPhil Greenway 	// create the image view
72*7eca0987SMichael Pfeiffer 	fpImageView = new ShowImageView(viewFrame, "image_view", B_FOLLOW_ALL,
73*7eca0987SMichael Pfeiffer 		B_WILL_DRAW | B_FRAME_EVENTS | B_FULL_UPDATE_ON_RESIZE | B_PULSE_NEEDED);
7413afa642SPhil Greenway 	// wrap a scroll view around the view
75*7eca0987SMichael Pfeiffer 	BScrollView *pscrollView = new BScrollView("image_scroller", fpImageView,
76cdb89ebbSMatthew Wilber 		B_FOLLOW_ALL, 0, false, false, B_PLAIN_BORDER);
77cdb89ebbSMatthew Wilber 	AddChild(pscrollView);
7813afa642SPhil Greenway 
7907caaa8eSMatthew Wilber 	const int32 kstatusWidth = 190;
80cdb89ebbSMatthew Wilber 	BRect rect;
8113afa642SPhil Greenway 	rect = Bounds();
8213afa642SPhil Greenway 	rect.top	= viewFrame.bottom + 1;
8307caaa8eSMatthew Wilber 	rect.left 	= viewFrame.left + kstatusWidth;
8413afa642SPhil Greenway 	rect.right	= viewFrame.right;
85cdb89ebbSMatthew Wilber 	BScrollBar *phscroll;
86*7eca0987SMichael Pfeiffer 	phscroll = new BScrollBar(rect, "hscroll", fpImageView, 0, 150,
87cdb89ebbSMatthew Wilber 		B_HORIZONTAL);
88cdb89ebbSMatthew Wilber 	AddChild(phscroll);
8913afa642SPhil Greenway 
9013afa642SPhil Greenway 	rect.left = 0;
9107caaa8eSMatthew Wilber 	rect.right = kstatusWidth - 1;
92*7eca0987SMichael Pfeiffer 	fpStatusView = new ShowImageStatusView(rect, "status_view", B_FOLLOW_BOTTOM,
93cdb89ebbSMatthew Wilber 		B_WILL_DRAW);
94*7eca0987SMichael Pfeiffer 	fpStatusView->SetViewColor(ui_color(B_MENU_BACKGROUND_COLOR));
95*7eca0987SMichael Pfeiffer 	AddChild(fpStatusView);
9613afa642SPhil Greenway 
9713afa642SPhil Greenway 	rect = Bounds();
9813afa642SPhil Greenway 	rect.top    = viewFrame.top;
9913afa642SPhil Greenway 	rect.left 	= viewFrame.right + 1;
10013afa642SPhil Greenway 	rect.bottom	= viewFrame.bottom;
101cdb89ebbSMatthew Wilber 	BScrollBar *pvscroll;
102*7eca0987SMichael Pfeiffer 	pvscroll = new BScrollBar(rect, "vscroll", fpImageView, 0, 150, B_VERTICAL);
103cdb89ebbSMatthew Wilber 	AddChild(pvscroll);
10413afa642SPhil Greenway 
10503839579SMatthew Wilber 	SetSizeLimits(250, 100000, 100, 100000);
10613afa642SPhil Greenway 
10713afa642SPhil Greenway 	// finish creating window
108cdb89ebbSMatthew Wilber 	SetRef(pref);
10913afa642SPhil Greenway 	UpdateTitle();
11013afa642SPhil Greenway 
111*7eca0987SMichael Pfeiffer 	fpImageView->SetImage(pref);
112*7eca0987SMichael Pfeiffer 
113*7eca0987SMichael Pfeiffer 	SetPulseRate(100000); // every 1/10 second; ShowImageView needs it for marching ants
11403839579SMatthew Wilber 
11513afa642SPhil Greenway 	Show();
11613afa642SPhil Greenway }
11713afa642SPhil Greenway 
11813afa642SPhil Greenway ShowImageWindow::~ShowImageWindow()
11913afa642SPhil Greenway {
120*7eca0987SMichael Pfeiffer 	delete fpRef;
12113afa642SPhil Greenway }
12213afa642SPhil Greenway 
123cdb89ebbSMatthew Wilber status_t
124cdb89ebbSMatthew Wilber ShowImageWindow::InitCheck()
12513afa642SPhil Greenway {
126*7eca0987SMichael Pfeiffer 	if (!fpRef || !fpImageView)
12713afa642SPhil Greenway 		return B_ERROR;
128cdb89ebbSMatthew Wilber 	else
12913afa642SPhil Greenway 		return B_OK;
13013afa642SPhil Greenway }
131cdb89ebbSMatthew Wilber 
132cdb89ebbSMatthew Wilber void
133cdb89ebbSMatthew Wilber ShowImageWindow::SetRef(const entry_ref *pref)
134cdb89ebbSMatthew Wilber {
135*7eca0987SMichael Pfeiffer 	if (!fpRef)
136*7eca0987SMichael Pfeiffer 		fpRef = new entry_ref(*pref);
137cdb89ebbSMatthew Wilber 	else
138*7eca0987SMichael Pfeiffer 		*fpRef = *pref;
13913afa642SPhil Greenway }
14013afa642SPhil Greenway 
141cdb89ebbSMatthew Wilber void
142cdb89ebbSMatthew Wilber ShowImageWindow::UpdateTitle()
14313afa642SPhil Greenway {
144*7eca0987SMichael Pfeiffer 	BEntry entry(fpRef);
14513afa642SPhil Greenway 	if (entry.InitCheck() == B_OK) {
14613afa642SPhil Greenway 		BPath path;
14713afa642SPhil Greenway 		entry.GetPath(&path);
148cdb89ebbSMatthew Wilber 		if (path.InitCheck() == B_OK)
14913afa642SPhil Greenway 			SetTitle(path.Path());
15013afa642SPhil Greenway 	}
15113afa642SPhil Greenway }
15213afa642SPhil Greenway 
153cdb89ebbSMatthew Wilber void
154cdb89ebbSMatthew Wilber ShowImageWindow::LoadMenus(BMenuBar *pbar)
15513afa642SPhil Greenway {
156cdb89ebbSMatthew Wilber 	BMenu *pmenu = new BMenu("File");
157cdb89ebbSMatthew Wilber 	AddItemMenu(pmenu, "Open", MSG_FILE_OPEN, 'O', 0, 'A', true);
158cdb89ebbSMatthew Wilber 	pmenu->AddSeparatorItem();
159cdb89ebbSMatthew Wilber 	BMenu *pmenuSaveAs = new BMenu("Save As...", B_ITEMS_IN_COLUMN);
160cdb89ebbSMatthew Wilber 	BTranslationUtils::AddTranslationItems(pmenuSaveAs, B_TRANSLATOR_BITMAP);
161fc0a275bSMatthew Wilber 		// Fill Save As submenu with all types that can be converted
162fc0a275bSMatthew Wilber 		// to from the Be bitmap image format
163cdb89ebbSMatthew Wilber 	pmenu->AddItem(pmenuSaveAs);
164cdb89ebbSMatthew Wilber 	AddItemMenu(pmenu, "Close", MSG_CLOSE, 'W', 0, 'W', true);
165cdb89ebbSMatthew Wilber 	pmenu->AddSeparatorItem();
166cdb89ebbSMatthew Wilber 	AddItemMenu(pmenu, "About ShowImage...", B_ABOUT_REQUESTED, 0, 0, 'A', true);
167cdb89ebbSMatthew Wilber 	pmenu->AddSeparatorItem();
168cdb89ebbSMatthew Wilber 	AddItemMenu(pmenu, "Quit", B_QUIT_REQUESTED, 'Q', 0, 'A', true);
169cdb89ebbSMatthew Wilber 	pbar->AddItem(pmenu);
17013afa642SPhil Greenway 
171cdb89ebbSMatthew Wilber 	pmenu = new BMenu("Edit");
172cdb89ebbSMatthew Wilber 	AddItemMenu(pmenu, "Undo", B_UNDO, 'Z', 0, 'W', false);
173cdb89ebbSMatthew Wilber 	pmenu->AddSeparatorItem();
174cdb89ebbSMatthew Wilber 	AddItemMenu(pmenu, "Cut", B_CUT, 'X', 0, 'W', false);
175cdb89ebbSMatthew Wilber 	AddItemMenu(pmenu, "Copy", B_COPY, 'C', 0, 'W', false);
176cdb89ebbSMatthew Wilber 	AddItemMenu(pmenu, "Paste", B_PASTE, 'V', 0, 'W', false);
177cdb89ebbSMatthew Wilber 	AddItemMenu(pmenu, "Clear", MSG_CLEAR_SELECT, 0, 0, 'W', false);
178cdb89ebbSMatthew Wilber 	pmenu->AddSeparatorItem();
179cdb89ebbSMatthew Wilber 	AddItemMenu(pmenu, "Select All", MSG_SELECT_ALL, 'A', 0, 'W', false);
180cdb89ebbSMatthew Wilber 	pbar->AddItem(pmenu);
18113afa642SPhil Greenway 
182*7eca0987SMichael Pfeiffer 	pmenu = fpPageMenu = new BMenu("Page");
18303839579SMatthew Wilber 	AddItemMenu(pmenu, "First", MSG_PAGE_FIRST, 'F', 0, 'W', true);
18403839579SMatthew Wilber 	AddItemMenu(pmenu, "Last", MSG_PAGE_LAST, 'L', 0, 'W', true);
18503839579SMatthew Wilber 	AddItemMenu(pmenu, "Next", MSG_PAGE_NEXT, 'N', 0, 'W', true);
18603839579SMatthew Wilber 	AddItemMenu(pmenu, "Previous", MSG_PAGE_PREV, 'P', 0, 'W', true);
18703839579SMatthew Wilber 	pbar->AddItem(pmenu);
18803839579SMatthew Wilber 
189cdb89ebbSMatthew Wilber 	pmenu = new BMenu("Image");
190cdb89ebbSMatthew Wilber 	AddItemMenu(pmenu, "Dither Image", MSG_DITHER_IMAGE, 0, 0, 'W', true);
191*7eca0987SMichael Pfeiffer 	AddItemMenu(pmenu, "Fit To Window Size", MSG_FIT_TO_WINDOW_SIZE, 0, 0, 'W', true);
192cdb89ebbSMatthew Wilber 	pbar->AddItem(pmenu);
19313afa642SPhil Greenway }
19413afa642SPhil Greenway 
195cdb89ebbSMatthew Wilber BMenuItem *
196cdb89ebbSMatthew Wilber ShowImageWindow::AddItemMenu(BMenu *pmenu, char *caption, long unsigned int msg,
197cdb89ebbSMatthew Wilber 	char shortcut, uint32 modifier, char target, bool benabled)
198cdb89ebbSMatthew Wilber {
199cdb89ebbSMatthew Wilber 	BMenuItem* pitem;
200cdb89ebbSMatthew Wilber 	pitem = new BMenuItem(caption, new BMessage(msg), shortcut);
20113afa642SPhil Greenway 
20213afa642SPhil Greenway 	if (target == 'A')
203cdb89ebbSMatthew Wilber 	   pitem->SetTarget(be_app);
20413afa642SPhil Greenway 
205cdb89ebbSMatthew Wilber 	pitem->SetEnabled(benabled);
206cdb89ebbSMatthew Wilber 	pmenu->AddItem(pitem);
20713afa642SPhil Greenway 
208cdb89ebbSMatthew Wilber 	return pitem;
20913afa642SPhil Greenway }
21013afa642SPhil Greenway 
211cdb89ebbSMatthew Wilber void
212cdb89ebbSMatthew Wilber ShowImageWindow::WindowRedimension(BBitmap *pbitmap)
21313afa642SPhil Greenway {
21413afa642SPhil Greenway 	// set the window's min & max size limits
21513afa642SPhil Greenway 	// based on document's data bounds
216cdb89ebbSMatthew Wilber 	float maxWidth = pbitmap->Bounds().Width() + B_V_SCROLL_BAR_WIDTH;
217*7eca0987SMichael Pfeiffer 	float maxHeight = pbitmap->Bounds().Height() + fpBar->Frame().Height() +
218cdb89ebbSMatthew Wilber 		B_H_SCROLL_BAR_HEIGHT + 1;
21913afa642SPhil Greenway 	float minWidth = min(maxWidth, 100.0f);
22013afa642SPhil Greenway 	float minHeight = min(maxHeight, 100.0f);
22113afa642SPhil Greenway 
22213afa642SPhil Greenway 	// adjust the window's current size based on new min/max values
22313afa642SPhil Greenway 	float curWidth = Bounds().Width();
22413afa642SPhil Greenway 	float curHeight = Bounds().Height();
225cdb89ebbSMatthew Wilber 	if (curWidth < minWidth)
22613afa642SPhil Greenway 		curWidth = minWidth;
227cdb89ebbSMatthew Wilber 	else if (curWidth > maxWidth)
22813afa642SPhil Greenway 		curWidth = maxWidth;
229cdb89ebbSMatthew Wilber 
230cdb89ebbSMatthew Wilber 	if (curHeight < minHeight)
23113afa642SPhil Greenway 		curHeight = minHeight;
232cdb89ebbSMatthew Wilber 	else if (curHeight > maxHeight)
23313afa642SPhil Greenway 		curHeight = maxHeight;
234cdb89ebbSMatthew Wilber 
235cdb89ebbSMatthew Wilber 	if (minWidth < 250)
23613afa642SPhil Greenway 		minWidth = 250;
237cdb89ebbSMatthew Wilber 
23813afa642SPhil Greenway 	SetSizeLimits(minWidth, maxWidth, minHeight, maxHeight);
23913afa642SPhil Greenway 	ResizeTo(curWidth, curHeight);
24013afa642SPhil Greenway }
24113afa642SPhil Greenway 
242cdb89ebbSMatthew Wilber void
243cdb89ebbSMatthew Wilber ShowImageWindow::FrameResized(float width, float height)
24413afa642SPhil Greenway {
24513afa642SPhil Greenway }
24613afa642SPhil Greenway 
247*7eca0987SMichael Pfeiffer bool
248*7eca0987SMichael Pfeiffer ShowImageWindow::ToggleMenuItem(uint32 what)
249*7eca0987SMichael Pfeiffer {
250*7eca0987SMichael Pfeiffer 	BMenuItem *item;
251*7eca0987SMichael Pfeiffer 	bool marked = false;
252*7eca0987SMichael Pfeiffer 	item = fpBar->FindItem(what);
253*7eca0987SMichael Pfeiffer 	if (item != NULL) {
254*7eca0987SMichael Pfeiffer 		marked = !item->IsMarked();
255*7eca0987SMichael Pfeiffer 		item->SetMarked(marked);
256*7eca0987SMichael Pfeiffer 	}
257*7eca0987SMichael Pfeiffer 	return marked;
258*7eca0987SMichael Pfeiffer }
259*7eca0987SMichael Pfeiffer 
260cdb89ebbSMatthew Wilber void
261cdb89ebbSMatthew Wilber ShowImageWindow::MessageReceived(BMessage *pmsg)
26213afa642SPhil Greenway {
263cdb89ebbSMatthew Wilber 	switch (pmsg->what) {
264fc0a275bSMatthew Wilber 		case MSG_OUTPUT_TYPE:
265fc0a275bSMatthew Wilber 			// User clicked Save As then choose an output format
266*7eca0987SMichael Pfeiffer 			if (!fpSavePanel)
267cdb89ebbSMatthew Wilber 				// If user doesn't already have a save panel open
268cdb89ebbSMatthew Wilber 				SaveAs(pmsg);
26913afa642SPhil Greenway 			break;
270fc0a275bSMatthew Wilber 
271fc0a275bSMatthew Wilber 		case MSG_SAVE_PANEL:
272fc0a275bSMatthew Wilber 			// User specified where to save the output image
273cdb89ebbSMatthew Wilber 			SaveToFile(pmsg);
274fc0a275bSMatthew Wilber 			break;
275fc0a275bSMatthew Wilber 
27607caaa8eSMatthew Wilber 		case MSG_CLOSE:
277cdb89ebbSMatthew Wilber 			if (CanQuit())
27807caaa8eSMatthew Wilber 				Quit();
27907caaa8eSMatthew Wilber 			break;
28007caaa8eSMatthew Wilber 
281cdb89ebbSMatthew Wilber 		case B_CANCEL:
282*7eca0987SMichael Pfeiffer 			delete fpSavePanel;
283*7eca0987SMichael Pfeiffer 			fpSavePanel = NULL;
284cdb89ebbSMatthew Wilber 			break;
285cdb89ebbSMatthew Wilber 
28603839579SMatthew Wilber 		case MSG_UPDATE_STATUS:
28703839579SMatthew Wilber 		{
288*7eca0987SMichael Pfeiffer 			bool benable = (fpImageView->PageCount() > 1) ? true : false;
289*7eca0987SMichael Pfeiffer 			if (fpPageMenu->IsEnabled() != benable)
2901d592070SMatthew Wilber 				// Only call this function if the state is changing
2911d592070SMatthew Wilber 				// to avoid flickering
292*7eca0987SMichael Pfeiffer 				fpPageMenu->SetEnabled(benable);
2931d592070SMatthew Wilber 
29403839579SMatthew Wilber 			BString str;
29503839579SMatthew Wilber 			if (pmsg->FindString("status", &str) == B_OK)
296*7eca0987SMichael Pfeiffer 				fpStatusView->SetText(str);
297*7eca0987SMichael Pfeiffer 
298*7eca0987SMichael Pfeiffer 			entry_ref ref;
299*7eca0987SMichael Pfeiffer 			if (pmsg->FindRef("ref", &ref) == B_OK) {
300*7eca0987SMichael Pfeiffer 				SetRef(&ref);
301*7eca0987SMichael Pfeiffer 				UpdateTitle();
302*7eca0987SMichael Pfeiffer 			}
30303839579SMatthew Wilber 			break;
30403839579SMatthew Wilber 		}
30503839579SMatthew Wilber 
30613afa642SPhil Greenway 		case B_UNDO:
30713afa642SPhil Greenway 			break;
30813afa642SPhil Greenway 		case B_CUT:
30913afa642SPhil Greenway 			break;
31013afa642SPhil Greenway 		case B_COPY:
31113afa642SPhil Greenway 			break;
31213afa642SPhil Greenway 		case B_PASTE:
31313afa642SPhil Greenway 			break;
31413afa642SPhil Greenway 		case MSG_CLEAR_SELECT:
31513afa642SPhil Greenway 			break;
31613afa642SPhil Greenway 		case MSG_SELECT_ALL:
31713afa642SPhil Greenway 			break;
31813afa642SPhil Greenway 
31903839579SMatthew Wilber 		case MSG_PAGE_FIRST:
320*7eca0987SMichael Pfeiffer 			fpImageView->FirstPage();
32103839579SMatthew Wilber 			break;
32203839579SMatthew Wilber 
32303839579SMatthew Wilber 		case MSG_PAGE_LAST:
324*7eca0987SMichael Pfeiffer 			fpImageView->LastPage();
32503839579SMatthew Wilber 			break;
32603839579SMatthew Wilber 
32703839579SMatthew Wilber 		case MSG_PAGE_NEXT:
328*7eca0987SMichael Pfeiffer 			fpImageView->NextPage();
32903839579SMatthew Wilber 			break;
33003839579SMatthew Wilber 
33103839579SMatthew Wilber 		case MSG_PAGE_PREV:
332*7eca0987SMichael Pfeiffer 			fpImageView->PrevPage();
33303839579SMatthew Wilber 			break;
33403839579SMatthew Wilber 
335cdb89ebbSMatthew Wilber 		case MSG_DITHER_IMAGE:
336*7eca0987SMichael Pfeiffer 			ToggleMenuItem(pmsg->what);
337*7eca0987SMichael Pfeiffer 			break;
338*7eca0987SMichael Pfeiffer 
339*7eca0987SMichael Pfeiffer 		case MSG_FIT_TO_WINDOW_SIZE:
340*7eca0987SMichael Pfeiffer 			{
341*7eca0987SMichael Pfeiffer 				bool resize;
342*7eca0987SMichael Pfeiffer 				resize = ToggleMenuItem(pmsg->what);
343*7eca0987SMichael Pfeiffer 				fpImageView->ResizeToViewBounds(resize);
344*7eca0987SMichael Pfeiffer 			}
345*7eca0987SMichael Pfeiffer 			break;
346*7eca0987SMichael Pfeiffer 
347*7eca0987SMichael Pfeiffer 		case MSG_FILE_PREV:
348*7eca0987SMichael Pfeiffer 			fpImageView->PrevFile();
349*7eca0987SMichael Pfeiffer 			break;
350*7eca0987SMichael Pfeiffer 
351*7eca0987SMichael Pfeiffer 		case MSG_FILE_NEXT:
352*7eca0987SMichael Pfeiffer 			fpImageView->NextFile();
35313afa642SPhil Greenway 			break;
35413afa642SPhil Greenway 
35513afa642SPhil Greenway 		default:
356cdb89ebbSMatthew Wilber 			BWindow::MessageReceived(pmsg);
35713afa642SPhil Greenway 			break;
35813afa642SPhil Greenway 	}
35913afa642SPhil Greenway }
360fc0a275bSMatthew Wilber 
361fc0a275bSMatthew Wilber void
362fc0a275bSMatthew Wilber ShowImageWindow::SaveAs(BMessage *pmsg)
363fc0a275bSMatthew Wilber {
364cdb89ebbSMatthew Wilber 	// Read the translator and output type the user chose
365cdb89ebbSMatthew Wilber 	translator_id outTranslator;
366cdb89ebbSMatthew Wilber 	uint32 outType;
367cdb89ebbSMatthew Wilber 	if (pmsg->FindInt32(TRANSLATOR_FLD,
368cdb89ebbSMatthew Wilber 		reinterpret_cast<int32 *>(&outTranslator)) != B_OK)
369fc0a275bSMatthew Wilber 		return;
370cdb89ebbSMatthew Wilber 	if (pmsg->FindInt32(TYPE_FLD,
371cdb89ebbSMatthew Wilber 		reinterpret_cast<int32 *>(&outType)) != B_OK)
372fc0a275bSMatthew Wilber 		return;
373fc0a275bSMatthew Wilber 
374cdb89ebbSMatthew Wilber 	// Add the chosen translator and output type to the
375cdb89ebbSMatthew Wilber 	// message that the save panel will send back
376cdb89ebbSMatthew Wilber 	BMessage *ppanelMsg = new BMessage(MSG_SAVE_PANEL);
377cdb89ebbSMatthew Wilber 	ppanelMsg->AddInt32(TRANSLATOR_FLD, outTranslator);
378cdb89ebbSMatthew Wilber 	ppanelMsg->AddInt32(TYPE_FLD, outType);
379cdb89ebbSMatthew Wilber 
380cdb89ebbSMatthew Wilber 	// Create save panel and show it
381*7eca0987SMichael Pfeiffer 	fpSavePanel = new BFilePanel(B_SAVE_PANEL, new BMessenger(this), NULL, 0,
382cdb89ebbSMatthew Wilber 		false, ppanelMsg);
383*7eca0987SMichael Pfeiffer 	if (!fpSavePanel)
384fc0a275bSMatthew Wilber 		return;
385*7eca0987SMichael Pfeiffer 	fpSavePanel->Window()->SetWorkspaces(B_CURRENT_WORKSPACE);
386*7eca0987SMichael Pfeiffer 	fpSavePanel->Show();
387fc0a275bSMatthew Wilber }
388fc0a275bSMatthew Wilber 
389fc0a275bSMatthew Wilber void
390fc0a275bSMatthew Wilber ShowImageWindow::SaveToFile(BMessage *pmsg)
391fc0a275bSMatthew Wilber {
392cdb89ebbSMatthew Wilber 	// Read in where the file should be saved
393fc0a275bSMatthew Wilber 	entry_ref dirref;
394fc0a275bSMatthew Wilber 	if (pmsg->FindRef("directory", &dirref) != B_OK)
395fc0a275bSMatthew Wilber 		return;
396fc0a275bSMatthew Wilber 	const char *filename;
397fc0a275bSMatthew Wilber 	if (pmsg->FindString("name", &filename) != B_OK)
398fc0a275bSMatthew Wilber 		return;
399fc0a275bSMatthew Wilber 
400cdb89ebbSMatthew Wilber 	// Read in the translator and type to be used
401cdb89ebbSMatthew Wilber 	// to save the output image
402cdb89ebbSMatthew Wilber 	translator_id outTranslator;
403cdb89ebbSMatthew Wilber 	uint32 outType;
404cdb89ebbSMatthew Wilber 	if (pmsg->FindInt32(TRANSLATOR_FLD,
405cdb89ebbSMatthew Wilber 		reinterpret_cast<int32 *>(&outTranslator)) != B_OK)
406cdb89ebbSMatthew Wilber 		return;
407cdb89ebbSMatthew Wilber 	if (pmsg->FindInt32(TYPE_FLD,
408cdb89ebbSMatthew Wilber 		reinterpret_cast<int32 *>(&outType)) != B_OK)
409cdb89ebbSMatthew Wilber 		return;
410cdb89ebbSMatthew Wilber 
411cdb89ebbSMatthew Wilber 	// Create the output file
412fc0a275bSMatthew Wilber 	BDirectory dir(&dirref);
413fc0a275bSMatthew Wilber 	BFile file(&dir, filename, B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE);
414fc0a275bSMatthew Wilber 	if (file.InitCheck() != B_OK)
415fc0a275bSMatthew Wilber 		return;
416fc0a275bSMatthew Wilber 
417cdb89ebbSMatthew Wilber 	// Translate the image and write it out to the output file
418*7eca0987SMichael Pfeiffer 	BBitmapStream stream(fpImageView->GetBitmap());
419fc0a275bSMatthew Wilber 	BTranslatorRoster *proster = BTranslatorRoster::Default();
420cdb89ebbSMatthew Wilber 	if (proster->Translate(outTranslator, &stream, NULL,
421cdb89ebbSMatthew Wilber 		&file, outType) != B_OK) {
422fc0a275bSMatthew Wilber 		BAlert *palert = new BAlert(NULL, "Error writing image file.", "Ok");
423fc0a275bSMatthew Wilber 		palert->Go();
424fc0a275bSMatthew Wilber 	}
425fc0a275bSMatthew Wilber 
426fc0a275bSMatthew Wilber 	BBitmap *pout = NULL;
427fc0a275bSMatthew Wilber 	stream.DetachBitmap(&pout);
428fc0a275bSMatthew Wilber 		// bitmap used by stream still belongs to the view,
429fc0a275bSMatthew Wilber 		// detach so it doesn't get deleted
430fc0a275bSMatthew Wilber }
431fc0a275bSMatthew Wilber 
432cdb89ebbSMatthew Wilber bool
433cdb89ebbSMatthew Wilber ShowImageWindow::CanQuit()
434cdb89ebbSMatthew Wilber {
435*7eca0987SMichael Pfeiffer 	if (fpSavePanel)
436cdb89ebbSMatthew Wilber 		// Don't allow this window to be closed if a save panel is open
437cdb89ebbSMatthew Wilber 		return false;
438cdb89ebbSMatthew Wilber 	else
439cdb89ebbSMatthew Wilber 		return true;
440cdb89ebbSMatthew Wilber }
441cdb89ebbSMatthew Wilber 
442cdb89ebbSMatthew Wilber bool
443cdb89ebbSMatthew Wilber ShowImageWindow::QuitRequested()
444cdb89ebbSMatthew Wilber {
445cdb89ebbSMatthew Wilber 	return CanQuit();
446cdb89ebbSMatthew Wilber }
447cdb89ebbSMatthew Wilber 
44807caaa8eSMatthew Wilber void
44907caaa8eSMatthew Wilber ShowImageWindow::Quit()
45007caaa8eSMatthew Wilber {
45107caaa8eSMatthew Wilber 	// tell the app to forget about this window
45207caaa8eSMatthew Wilber 	be_app->PostMessage(MSG_WINDOW_QUIT);
45307caaa8eSMatthew Wilber 	BWindow::Quit();
45407caaa8eSMatthew Wilber }
455fc0a275bSMatthew Wilber 
456