xref: /haiku/src/apps/showimage/ShowImageView.h (revision ed6250c95736c0b55da79d6e9dd01369532260c0)
1 /*
2  * Copyright 2003-2007, 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 FindNextImageByDir(entry_ref *in_current, entry_ref *out_image,
171 				bool next, bool rewind);
172 		bool FindNextImage(entry_ref *in_current, entry_ref *out_image,
173 				bool next, bool rewind);
174 		bool ShowNextImage(bool next, bool rewind);
175 		bool FirstFile();
176 		void ConstrainToImage(BPoint &point);
177 		void ConstrainToImage(BRect &rect);
178 		BBitmap* CopyFromRect(BRect srcRect);
179 		BBitmap* CopySelection(uchar alpha = 255, bool imageSize = true);
180 		bool AddSupportedTypes(BMessage* msg, BBitmap* bitmap);
181 		void BeginDrag(BPoint sourcePoint);
182 		void SendInMessage(BMessage* msg, BBitmap* bitmap, translation_format* format);
183 		bool OutputFormatForType(BBitmap* bitmap, const char* type,
184 				translation_format* format);
185 		void HandleDrop(BMessage* msg);
186 		void MoveImage();
187 		uint32 GetMouseButtons();
188 		void UpdateSelectionRect(BPoint point, bool final);
189 		void DrawBorder(BRect border);
190 		void LayoutCaption(BFont &font, BPoint &textPos, BRect &background);
191 		void DrawCaption();
192 		void UpdateCaption();
193 		void DrawSelectionBox();
194 		Scaler* GetScaler(BRect rect);
195 		void DrawImage(BRect rect);
196 		float LimitToRange(float v, orientation o, bool absolute);
197 		void ScrollRestricted(float x, float y, bool absolute);
198 		void ScrollRestrictedTo(float x, float y);
199 		void ScrollRestrictedBy(float x, float y);
200 		void MouseWheelChanged(BMessage* msg);
201 		void ShowPopUpMenu(BPoint screen);
202 		void SettingsSetBool(const char* name, bool value);
203 		void SetIcon(bool clear, icon_size which);
204 		void ToggleSlideShow();
205 		void ExitFullScreen();
206 
207 		BMessenger fTrackerMessenger; // of the window that this was launched from
208 		entry_ref fCurrentRef;		// of the image
209 		bool fDither;				// dither the image
210 		int32 fDocumentIndex;		// of the image in the file
211 		int32 fDocumentCount;		// number of images in the file
212 		BBitmap *fBitmap;			// the original image
213 		BBitmap *fDisplayBitmap;
214 			// the image to be displayed
215 			// (== fBitmap if the bitmap can be displayed as is)
216 		BBitmap *fSelBitmap;		// the bitmap in the selection
217 		float fZoom;				// factor to be used to display the image
218 		bool fScaleBilinear;		// use bilinear scaling?
219 		Scaler* fScaler;			// holds the scaled image if bilinear scaling is enabled
220 		bool fShrinkToBounds;
221 		bool fZoomToBounds;
222 		bool fShrinkOrZoomToBounds;
223 		bool fFullScreen;			// is the image displayed fullscreen?
224 		float fLeft;				// the origin of the image in the view
225 		float fTop;
226 		bool fMovesImage;			// is the image being moved with the mouse
227 		bool fMakesSelection;		// is a selection being made
228 		BPoint fFirstPoint;			// first point in image space of selection
229 		bool fAnimateSelection;		// marching ants
230 		bool fHasSelection;			// is fSelectionRect valid
231 		BRect fSelectionRect;		// the current location of the selection rectangle
232 		BRect fCopyFromRect;
233 			// the portion of the background bitmap the selection is made from
234 		pattern fPatternUp, fPatternDown, fPatternLeft, fPatternRight;
235 
236 		bool fSlideShow;			// is slide show enabled?
237 		int fSlideShowDelay;		// in pulse rate units
238 		int fSlideShowCountDown;	// shows next image if it reaches zero
239 #if DELAYED_SCALING
240 		int fScalingCountDown;
241 #endif
242 
243 		bool fShowCaption;			// display caption?
244 		BString fCaption;			// caption text
245 
246 		BString fImageType; 		// Type of image, for use in status bar and caption
247 
248 		bool fInverted;
249 
250 		bool fShowingPopUpMenu;
251 
252 		int fHideCursorCountDown;	// Hides the cursor when it reaches zero
253 		bool fIsActiveWin;			// Is the parent window the active window?
254 
255 		ProgressWindow* fProgressWindow;
256 
257 		enum image_orientation fImageOrientation;
258 		static enum image_orientation fTransformation[
259 			ImageProcessor::kNumberOfAffineTransformations][kNumberOfOrientations];
260 };
261 
262 #endif	// SHOW_IMAGE_VIEW_H
263