xref: /haiku/src/apps/showimage/ShowImageView.h (revision 62f5ba006a08b0df30631375878effaf67ae5dbc)
1 /*
2  * Copyright 2003-2009, Haiku, Inc. All Rights Reserved.
3  * Copyright 2004-2005 yellowTAB GmbH. All Rights Reserverd.
4  * Copyright 2006 Bernd Korz. All Rights Reserved
5  * Distributed under the terms of the MIT License.
6  *
7  * Authors:
8  *		Fernando Francisco de Oliveira
9  *		Michael Wilber
10  *		Michael Pfeiffer
11  *		yellowTAB GmbH
12  *		Bernd Korz
13  */
14 #ifndef SHOW_IMAGE_VIEW_H
15 #define SHOW_IMAGE_VIEW_H
16 
17 
18 #include "Filter.h"
19 #include "ShowImageUndo.h"
20 
21 #include <Bitmap.h>
22 #include <Entry.h>
23 #include <NodeInfo.h>
24 #include <String.h>
25 #include <TranslatorRoster.h>
26 #include <View.h>
27 
28 
29 // delay scaling operation, so that a sequence of zoom in/out operations works smoother
30 #define DELAYED_SCALING 1
31 // the delay time in 1/10 seconds
32 #define SCALING_DELAY_TIME 3
33 // width of the black border stroked arround the bitmap
34 #define PEN_SIZE 1.0f
35 
36 // the delay time for hiding the cursor in 1/10 seconds (the pulse rate)
37 #define HIDE_CURSOR_DELAY_TIME 20
38 
39 class ProgressWindow;
40 
41 class ShowImageView : public BView {
42 	public:
43 		ShowImageView(BRect rect, const char *name, uint32 resizingMode,
44 			uint32 flags);
45 		virtual ~ShowImageView();
46 
47 		virtual void AttachedToWindow();
48 		virtual void DetachedFromWindow();
49 		virtual void Draw(BRect updateRect);
50 		virtual void FrameResized(float width, float height);
51 		virtual void MouseDown(BPoint point);
52 		virtual void MouseMoved(BPoint point, uint32 state, const BMessage *message);
53 		virtual void MouseUp(BPoint point);
54 		virtual void KeyDown(const char *bytes, int32 numBytes);
55 		virtual void Pulse();
56 
57 		virtual void MessageReceived(BMessage *message);
58 		virtual void WindowActivated(bool active);
59 
60 		void SetTrackerMessenger(const BMessenger& trackerMessenger);
61 		status_t SetImage(const entry_ref *ref);
62 		const entry_ref* Image() const { return &fCurrentRef; }
63 
64 		void SaveToFile(BDirectory* dir, const char* name, BBitmap* bitmap,
65 				const translation_format* format);
66 		void SetDither(bool dither);
67 		bool GetDither() const { return fDither; }
68 		void SetShowCaption(bool show);
69 		void SetShrinkToBounds(bool enable);
70 		bool GetShrinkToBounds() const { return fShrinkToBounds; }
71 		void SetZoomToBounds(bool enable);
72 		bool GetZoomToBounds() const { return fZoomToBounds; }
73 		void SetFullScreen(bool fullScreen);
74 		BBitmap *GetBitmap();
75 		void GetName(BString *name);
76 		void GetPath(BString *name);
77 		void SetScaleBilinear(bool b);
78 		bool GetScaleBilinear() { return fScaleBilinear; }
79 
80 		void FixupScrollBar(orientation o, float bitmapLength, float viewLength);
81 		void FixupScrollBars();
82 
83 		int32 CurrentPage();
84 		int32 PageCount();
85 
86 		void Undo();
87 		void Cut();
88 		void Paste();
89 		void SelectAll();
90 		void ClearSelection();
91 
92 		void CopySelectionToClipboard();
93 
94 		void FirstPage();
95 		void LastPage();
96 		void NextPage();
97 		void PrevPage();
98 		void GoToPage(int32 page);
99 		bool NextFile();
100 		bool PrevFile();
101 		bool HasNextFile();
102 		bool HasPrevFile();
103 		void SetSlideShowDelay(float seconds);
104 		float GetSlideShowDelay() const { return fSlideShowDelay / 10.0; }
105 		bool SlideShowStarted() const { return fSlideShow; }
106 		void StartSlideShow();
107 		void StopSlideShow();
108 		void SetZoom(float zoom);
109 		void ZoomIn();
110 		void ZoomOut();
111 
112 		// Image manipulation
113 		void Rotate(int degree); // 90 and 270 only
114 		void Flip(bool vertical);
115 		void Invert();
116 		void ResizeImage(int w, int h);
117 
118 		void SetIcon(bool clear);
119 
120 	private:
121 		ShowImageUndo fUndo;
122 		enum image_orientation {
123 			k0,    // 0
124 			k90,   // 1
125 			k180,  // 2
126 			k270,  // 3
127 			k0V,   // 4
128 			k90V,  // 5
129 			k0H,   // 6
130 			k270V, // 7
131 			kNumberOfOrientations,
132 		};
133 		void _InitPatterns();
134 		void _RotatePatterns();
135 		void _RemoveSelection(bool bToClipboard);
136 		bool _HasSelection() { return fHasSelection; }
137 		void _SetHasSelection(bool bHasSelection);
138 		void _AnimateSelection(bool a);
139 		void _SendMessageToWindow(BMessage *message);
140 		void _SendMessageToWindow(uint32 code);
141 		void _Notify();
142 		void _UpdateStatusText();
143 		void _AddToRecentDocuments();
144 		void _AddWhiteRect(BRect &rect);
145 		void _GetMergeRects(BBitmap *merge, BRect selection, BRect &srcBits, BRect &destRect);
146 		void _GetSelMergeRects(BRect &srcBits, BRect &destRect);
147 		status_t _SetSelection(const entry_ref *pref, BPoint point);
148 		status_t _PasteBitmap(BBitmap *bitmap, BPoint point);
149 		void _MergeWithBitmap(BBitmap *merge, BRect selection);
150 		void _MergeSelection();
151 		void _DeleteScaler();
152 		void _DeleteBitmap();
153 		void _DeleteSelBitmap();
154 		int32 _BytesPerPixel(color_space cs) const;
155 		void _CopyPixel(uchar* dest, int32 destX, int32 destY, int32 destBPR,
156 				uchar* src, int32 x, int32 y, int32 bpr, int32 bpp);
157 		void _InvertPixel(int32 x, int32 y, uchar* dest, int32 destBPR, uchar* src,
158 				int32 bpr, int32 bpp);
159 		void _DoImageOperation(enum ImageProcessor::operation op, bool quiet = false);
160 		void _UserDoImageOperation(enum ImageProcessor::operation op, bool quiet = false);
161 		BRect _AlignBitmap();
162 		void _Setup(BRect r);
163 		BPoint _ImageToView(BPoint p) const;
164 		BPoint _ViewToImage(BPoint p) const;
165 		BRect _ImageToView(BRect r) const;
166 		bool _IsImage(const entry_ref* pref);
167 		static int _CompareEntries(const void* a, const void* b);
168 		void _FreeEntries(BList* entries);
169 		void _SetTrackerSelectionToCurrent();
170 		bool _FindNextImage(entry_ref *in_current, entry_ref *out_image,
171 				bool next, bool rewind);
172 		bool _ShowNextImage(bool next, bool rewind);
173 		bool _FirstFile();
174 		void _ConstrainToImage(BPoint &point);
175 		void _ConstrainToImage(BRect &rect);
176 		BBitmap* _CopyFromRect(BRect srcRect);
177 		BBitmap* _CopySelection(uchar alpha = 255, bool imageSize = true);
178 		bool _AddSupportedTypes(BMessage* msg, BBitmap* bitmap);
179 		void _BeginDrag(BPoint sourcePoint);
180 		void _SendInMessage(BMessage* msg, BBitmap* bitmap, translation_format* format);
181 		bool _OutputFormatForType(BBitmap* bitmap, const char* type,
182 				translation_format* format);
183 		void _HandleDrop(BMessage* msg);
184 		void _MoveImage();
185 		uint32 _GetMouseButtons();
186 		void _UpdateSelectionRect(BPoint point, bool final);
187 		void _DrawBorder(BRect border);
188 		void _LayoutCaption(BFont &font, BPoint &textPos, BRect &background);
189 		void _DrawCaption();
190 		void _UpdateCaption();
191 		void _DrawSelectionBox();
192 		Scaler* _GetScaler(BRect rect);
193 		void _DrawImage(BRect rect);
194 		float _LimitToRange(float v, orientation o, bool absolute);
195 		void _ScrollRestricted(float x, float y, bool absolute);
196 		void _ScrollRestrictedTo(float x, float y);
197 		void _ScrollRestrictedBy(float x, float y);
198 		void _MouseWheelChanged(BMessage* msg);
199 		void _ShowPopUpMenu(BPoint screen);
200 		void _SettingsSetBool(const char* name, bool value);
201 		void _SetIcon(bool clear, icon_size which);
202 		void _ToggleSlideShow();
203 		void _ExitFullScreen();
204 
205 		BMessenger fTrackerMessenger; // of the window that this was launched from
206 		entry_ref fCurrentRef;		// of the image
207 		bool fDither;				// dither the image
208 		int32 fDocumentIndex;		// of the image in the file
209 		int32 fDocumentCount;		// number of images in the file
210 		BBitmap *fBitmap;			// the original image
211 		BBitmap *fDisplayBitmap;
212 			// the image to be displayed
213 			// (== fBitmap if the bitmap can be displayed as is)
214 		BBitmap *fSelBitmap;		// the bitmap in the selection
215 		float fZoom;				// factor to be used to display the image
216 		bool fScaleBilinear;		// use bilinear scaling?
217 		Scaler* fScaler;			// holds the scaled image if bilinear scaling is enabled
218 		bool fShrinkToBounds;
219 		bool fZoomToBounds;
220 		bool fShrinkOrZoomToBounds;
221 		bool fFullScreen;			// is the image displayed fullscreen?
222 		float fLeft;				// the origin of the image in the view
223 		float fTop;
224 		bool fMovesImage;			// is the image being moved with the mouse
225 		bool fMakesSelection;		// is a selection being made
226 		BPoint fFirstPoint;			// first point in image space of selection
227 		bool fAnimateSelection;		// marching ants
228 		bool fHasSelection;			// is fSelectionRect valid
229 		BRect fSelectionRect;		// the current location of the selection rectangle
230 		BRect fCopyFromRect;
231 			// the portion of the background bitmap the selection is made from
232 		pattern fPatternUp, fPatternDown, fPatternLeft, fPatternRight;
233 
234 		bool fSlideShow;			// is slide show enabled?
235 		int fSlideShowDelay;		// in pulse rate units
236 		int fSlideShowCountDown;	// shows next image if it reaches zero
237 #if DELAYED_SCALING
238 		int fScalingCountDown;
239 #endif
240 
241 		bool fShowCaption;			// display caption?
242 		BString fCaption;			// caption text
243 
244 		BString fImageType; 		// Type of image, for use in status bar and caption
245 		BString fImageMime;
246 
247 		bool fInverted;
248 
249 		bool fShowingPopUpMenu;
250 
251 		int fHideCursorCountDown;	// Hides the cursor when it reaches zero
252 		bool fIsActiveWin;			// Is the parent window the active window?
253 
254 		ProgressWindow* fProgressWindow;
255 
256 		enum image_orientation fImageOrientation;
257 		static enum image_orientation fTransformation[
258 			ImageProcessor::kNumberOfAffineTransformations][kNumberOfOrientations];
259 };
260 
261 #endif	// SHOW_IMAGE_VIEW_H
262