xref: /haiku/src/apps/showimage/ShowImageView.h (revision 1acbe440b8dd798953bec31d18ee589aa3f71b73)
1 /*
2  * Copyright 2003-2006, 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 ShowImageView : public BView {
40 	public:
41 		ShowImageView(BRect rect, const char *name, uint32 resizingMode,
42 			uint32 flags);
43 		virtual ~ShowImageView();
44 
45 		virtual void AttachedToWindow();
46 		virtual void Draw(BRect updateRect);
47 		virtual void FrameResized(float width, float height);
48 		virtual void MouseDown(BPoint point);
49 		virtual void MouseMoved(BPoint point, uint32 state, const BMessage *message);
50 		virtual void MouseUp(BPoint point);
51 		virtual void KeyDown(const char *bytes, int32 numBytes);
52 		virtual void Pulse();
53 
54 		virtual void MessageReceived(BMessage *message);
55 		virtual void WindowActivated(bool active);
56 
57 		void SetTrackerMessenger(const BMessenger& trackerMessenger);
58 		status_t SetImage(const entry_ref *ref);
59 		const entry_ref* Image() const { return &fCurrentRef; }
60 
61 		void SaveToFile(BDirectory* dir, const char* name, BBitmap* bitmap,
62 				const translation_format* format);
63 		void SetDither(bool dither);
64 		bool GetDither() const { return fDither; }
65 		void SetShowCaption(bool show);
66 		void SetShrinkToBounds(bool enable);
67 		bool GetShrinkToBounds() const { return fShrinkToBounds; }
68 		void SetZoomToBounds(bool enable);
69 		bool GetZoomToBounds() const { return fZoomToBounds; }
70 		void SetFullScreen(bool fullScreen);
71 		BBitmap *GetBitmap();
72 		void GetName(BString *name);
73 		void GetPath(BString *name);
74 		void SetScaleBilinear(bool b);
75 		bool GetScaleBilinear() { return fScaleBilinear; }
76 
77 		void FixupScrollBar(orientation o, float bitmapLength, float viewLength);
78 		void FixupScrollBars();
79 
80 		int32 CurrentPage();
81 		int32 PageCount();
82 
83 		void Undo();
84 		void Cut();
85 		void Paste();
86 		void SelectAll();
87 		void ClearSelection();
88 
89 		void CopySelectionToClipboard();
90 
91 		void FirstPage();
92 		void LastPage();
93 		void NextPage();
94 		void PrevPage();
95 		void GoToPage(int32 page);
96 		bool NextFile();
97 		bool PrevFile();
98 		bool HasNextFile();
99 		bool HasPrevFile();
100 		void SetSlideShowDelay(float seconds);
101 		float GetSlideShowDelay() const { return fSlideShowDelay / 10.0; }
102 		bool SlideShowStarted() const { return fSlideShow; }
103 		void StartSlideShow();
104 		void StopSlideShow();
105 		void SetZoom(float zoom);
106 		void ZoomIn();
107 		void ZoomOut();
108 
109 		// Image manipulation
110 		void Rotate(int degree); // 90 and 270 only
111 		void Flip(bool vertical);
112 		void Invert();
113 		void ResizeImage(int w, int h);
114 
115 		void SetIcon(bool clear);
116 
117 	private:
118 		ShowImageUndo fUndo;
119 		enum image_orientation {
120 			k0,    // 0
121 			k90,   // 1
122 			k180,  // 2
123 			k270,  // 3
124 			k0V,   // 4
125 			k90V,  // 5
126 			k0H,   // 6
127 			k270V, // 7
128 			kNumberOfOrientations,
129 		};
130 		void InitPatterns();
131 		void RotatePatterns();
132 		void RemoveSelection(bool bToClipboard);
133 		bool HasSelection() { return fHasSelection; }
134 		void SetHasSelection(bool bHasSelection);
135 		void AnimateSelection(bool a);
136 		void SendMessageToWindow(BMessage *message);
137 		void SendMessageToWindow(uint32 code);
138 		void Notify();
139 		void UpdateStatusText();
140 		void AddToRecentDocuments();
141 		void AddWhiteRect(BRect &rect);
142 		void GetMergeRects(BBitmap *merge, BRect selection, BRect &srcBits, BRect &destRect);
143 		void GetSelMergeRects(BRect &srcBits, BRect &destRect);
144 		status_t SetSelection(const entry_ref *pref, BPoint point);
145 		status_t PasteBitmap(BBitmap *bitmap, BPoint point);
146 		void MergeWithBitmap(BBitmap *merge, BRect selection);
147 		void MergeSelection();
148 		void DeleteScaler();
149 		void DeleteBitmap();
150 		void DeleteSelBitmap();
151 		int32 BytesPerPixel(color_space cs) const;
152 		void CopyPixel(uchar* dest, int32 destX, int32 destY, int32 destBPR,
153 				uchar* src, int32 x, int32 y, int32 bpr, int32 bpp);
154 		void InvertPixel(int32 x, int32 y, uchar* dest, int32 destBPR, uchar* src,
155 				int32 bpr, int32 bpp);
156 		void DoImageOperation(enum ImageProcessor::operation op, bool quiet = false);
157 		void UserDoImageOperation(enum ImageProcessor::operation op, bool quiet = false);
158 		BRect AlignBitmap();
159 		void Setup(BRect r);
160 		BPoint ImageToView(BPoint p) const;
161 		BPoint ViewToImage(BPoint p) const;
162 		BRect ImageToView(BRect r) const;
163 		bool IsImage(const entry_ref* pref);
164 		static int CompareEntries(const void* a, const void* b);
165 		void FreeEntries(BList* entries);
166 		void SetTrackerSelectionToCurrent();
167 		bool FindNextImageByDir(entry_ref *in_current, entry_ref *out_image,
168 				bool next, bool rewind);
169 		bool FindNextImage(entry_ref *in_current, entry_ref *out_image,
170 				bool next, bool rewind);
171 		bool ShowNextImage(bool next, bool rewind);
172 		bool FirstFile();
173 		void ConstrainToImage(BPoint &point);
174 		void ConstrainToImage(BRect &rect);
175 		BBitmap* CopyFromRect(BRect srcRect);
176 		BBitmap* CopySelection(uchar alpha = 255, bool imageSize = true);
177 		bool AddSupportedTypes(BMessage* msg, BBitmap* bitmap);
178 		void BeginDrag(BPoint sourcePoint);
179 		void SendInMessage(BMessage* msg, BBitmap* bitmap, translation_format* format);
180 		bool OutputFormatForType(BBitmap* bitmap, const char* type,
181 				translation_format* format);
182 		void HandleDrop(BMessage* msg);
183 		void MoveImage();
184 		uint32 GetMouseButtons();
185 		void UpdateSelectionRect(BPoint point, bool final);
186 		void DrawBorder(BRect border);
187 		void LayoutCaption(BFont &font, BPoint &textPos, BRect &background);
188 		void DrawCaption();
189 		void UpdateCaption();
190 		void DrawSelectionBox();
191 		Scaler* GetScaler(BRect rect);
192 		void DrawImage(BRect rect);
193 		float LimitToRange(float v, orientation o, bool absolute);
194 		void ScrollRestricted(float x, float y, bool absolute);
195 		void ScrollRestrictedTo(float x, float y);
196 		void ScrollRestrictedBy(float x, float y);
197 		void MouseWheelChanged(BMessage* msg);
198 		void ShowPopUpMenu(BPoint screen);
199 		void SettingsSetBool(const char* name, bool value);
200 		void SetIcon(bool clear, icon_size which);
201 		void ToggleSlideShow();
202 		void ExitFullScreen();
203 
204 		BMessenger fTrackerMessenger; // of the window that this was launched from
205 		entry_ref fCurrentRef;		// of the image
206 		bool fDither;				// dither the image
207 		int32 fDocumentIndex;		// of the image in the file
208 		int32 fDocumentCount;		// number of images in the file
209 		BBitmap *fBitmap;			// the original image
210 		BBitmap *fDisplayBitmap;
211 			// the image to be displayed
212 			// (== fBitmap if the bitmap can be displayed as is)
213 		BBitmap *fSelBitmap;		// the bitmap in the selection
214 		float fZoom;				// factor to be used to display the image
215 		bool fScaleBilinear;		// use bilinear scaling?
216 		Scaler* fScaler;			// holds the scaled image if bilinear scaling is enabled
217 		bool fShrinkToBounds;
218 		bool fZoomToBounds;
219 		bool fShrinkOrZoomToBounds;
220 		bool fFullScreen;			// is the image displayed fullscreen?
221 		float fLeft;				// the origin of the image in the view
222 		float fTop;
223 		bool fMovesImage;			// is the image being moved with the mouse
224 		bool fMakesSelection;		// is a selection being made
225 		BPoint fFirstPoint;			// first point in image space of selection
226 		bool fAnimateSelection;		// marching ants
227 		bool fHasSelection;			// is fSelectionRect valid
228 		BRect fSelectionRect;		// the current location of the selection rectangle
229 		BRect fCopyFromRect;
230 			// the portion of the background bitmap the selection is made from
231 		pattern fPatternUp, fPatternDown, fPatternLeft, fPatternRight;
232 
233 		bool fSlideShow;			// is slide show enabled?
234 		int fSlideShowDelay;		// in pulse rate units
235 		int fSlideShowCountDown;	// shows next image if it reaches zero
236 #if DELAYED_SCALING
237 		int fScalingCountDown;
238 #endif
239 
240 		bool fShowCaption;			// display caption?
241 		BString fCaption;			// caption text
242 
243 		BString fImageType; 		// Type of image, for use in status bar and caption
244 
245 		bool fInverted;
246 
247 		bool fShowingPopUpMenu;
248 
249 		int fHideCursorCountDown;	// Hides the cursor when it reaches zero
250 		bool fIsActiveWin;			// Is the parent window the active window?
251 
252 		enum image_orientation fImageOrientation;
253 		static enum image_orientation fTransformation[
254 			ImageProcessor::kNumberOfAffineTransformations][kNumberOfOrientations];
255 };
256 
257 #endif	// SHOW_IMAGE_VIEW_H
258