Česky
Kamil Dudka

GED 2006 (C++)

File detail

Name:Downloaddocwnd.h [Download]
Location: ged2006 > src
Size:6.8 KB
Last modification:2007-08-29 02:16

Source code

/*
 * File: docwnd.h
 * Project: GED - bitmap editor (ICP)
 * Author: Kamil Dudka, xdudka00
 * Team: xdudka00, xfilak01, xhefka00, xhradi08
 * Created: 2006-04-01
 */
 
#ifndef DOCWND_H
#define DOCWND_H
 
#include <string>
 
#include <FL/Fl_Window.H>
#include <FL/Fl_Scroll.H>
#include <FL/Fl_Box.H>
#include <FL/Fl_Image.H>
 
#include "image.h"
#include "draw.h"
 
class DocMenu;
class Fl_Scroll;
class MyFl_Image;
class PaintArea;
class StatusBar;
class Cmd;
 
/**
 * Document window class.
 */
class DocWnd: public Fl_Window {
public:
	/**
	 * Create Document window. This does not mean that window is shown.
	 * Use Fl_Window::show() method to do this.
	 * \param img Pointer to Image object to work with.
	 */
	DocWnd (Image *img);
 
	/**
	 * Virtual destructor is needed for collaboration with Document class.
	 */
	virtual ~DocWnd ();
 
	/**
	 * Save current image. Image will have the same name if already has one.
	 * \return Return true if really saved. Saving can be also canceled by user.
	 * See program documentation for more information.
	 */
	bool save ();
 
	/**
	 * Save current image with new name. User will be prompted for new name.
	 * \return Return true if really saved. Saving can be also canceled by user.
	 * See program documentation for more information.
	 */
	bool saveAs ();
 
	/**
	 * Close current document window. User will be asked for saving current document
	 * if document was changed since last saving.
	 * \return Return true if really closed. Closing can be also canceled by user.
	 * See program documentation for more information.
	 */
	bool tryClose ();
 
	/**
	 * Undo last change. This can throw CmdHistory::ErrUnderflow exception.
	 * See CmdHistory documentation for more information.
	 */
	void undo ();
 
	/**
	 * Undo last undo. This can throw CmdHistory::ErrUnderflow exception.
	 * See CmdHistory documentation for more information.
	 */
	void redo ();
 
	/**
	 * Set selected area to whole image.
	 */
	void unSelect ();
 
	/**
	 * Apply filter "grayscale"
	 */
	void filterGray ();
 
	/**
	 * Apply filter "invert"
	 */
	void filterInvert ();
 
	/**
	 * Apply plugin to current image.
	 * \param plugin Plugin name (identification)
	 * \param szArgs Arguments passed to plugin
	 */
	void applyPlugin (std::string plugin, const char *szArgs=0);
 
	/**
	 * Start macro loading
	 */
	void macLoad ();
 
	/**
	 * Stop macro loading and create new macro. This macro is immediately saved to configuration file.
	 * \param szName New macro name. Macro is overwrited whithout prompt if already exist.
	 */
	void macCreate (const char *szName);
 
	/**
	 * Apply macro on current image.
	 * \param macroName Name of macro to apply.
	 */
	void applyMacro (std::string macroName);
 
	/**
	 * Set status bar text. Call DocWnd::redraw() method to apply this change.
	 * \param szMsg New text to view in status bar.
	 */
	void setStatusMsg (const char *szMsg);	
 
	/**
	 * Set status bar text. Call DocWnd::redraw() method to apply this change.
	 * \param msg New text to view in status bar.
	 */
	void setStatusMsg (const std::string &msg) { setStatusMsg (msg.c_str()); }
 
	/**
	 * Redraw document window
	 */
	virtual void redraw ();
 
	/**
	 * Low level draw of status bar. Do not use since you really know what you are doing.
	 */
	void drawStatus ();
 
private:
	Image *_img;
	DocMenu *_menu;
	Fl_Scroll *_scroll;
	PaintArea *_paint;
	StatusBar *_statusBar;
	std::string _statusMsg;
};
 
/**
 * Paint area widget
 */
class PaintArea: public Fl_Box {
public:
	/**
	 * Create paint area widget. Note that widget size is same as image size.
	 * \param at Widget position.
	 * \param img Pointer to Image object to use for this paint area.
	 */
	PaintArea (Point at, Image *img);
	virtual ~PaintArea ();
 
	/**
	 * Low level draw of paint area. Do not use since you really know what you are doing.
	 */
	virtual void draw ();
 
	/**
	 * Redraw paint area. Use this to apply changes made on image.
	 */
	virtual void redraw ();
 
	/**
	 * Paint area event handling. See FLTK documentation for more information.
	 * \param event Events enumeration.
	 * \return Return 1 if event is handled by PaintArea.
	 * Return Fl_Widget::handle(event) if event is not handled.
	 */
	virtual int handle (int event);
 
private:
	void drawStart ();			///< Create draw command (if needed)
	void drawData ();			///< Put data into draw command (if needed)
	void drawEnd ();			///< Complete draw command (called on FL_RELEASE)
 
	void feedBackInit ();			///< Initialize draw feedback
	void feedBack ();			///< Draw feedback during draw
 
	Image *_img;
	MyFl_Image *_flImg;
	DrawContext _dc;
	Cmd *_currentCmd;
 
	int _selX1, _selX2, _selY1, _selY2;
	int _iBrushWidth;
	int _relX1, _relX2, _relY1, _relY2;
	int _absX1, _absX2, _absY1, _absY2;
	bool _bInRange;
};
 
/**
 * Status bar widget
 */
class StatusBar: public Fl_Box {
public:
	/**
	 * Create status bar widget
	 * \param x Widget position.
	 * \param y Widget position.
	 * \param width Widget width.
	 * \param height Widget height.
	 * \param szText Status bar initially text.
	 */
	StatusBar (int x, int y, int width, int height, const char *szText);
 
	/**
	 * Needed for low-level status draw.
	 */
	friend void DocWnd::drawStatus ();
};
 
/**
 * User friendly scroll area derived from FLTK scroll area.
 */
class MyFl_Scroll: public Fl_Scroll {
public:
	/**
	 * Create scroll area
	 * \param x Scroll area position.
	 * \param y Scroll area position.
	 * \param width Scroll area width.
	 * \param height Scroll area height.
	 */
	MyFl_Scroll (int x, int y, int width, int height):
		Fl_Scroll (x, y, width, height)
	{
	}
 
	/**
	 * This virtual method is called on widget resize.
	 * It can change scrollers position if widget si resized.
	 * \param x New widget position.
	 * \param y New widget position.
	 * \param width New widget width.
	 * \param height New widget height.
	 */
	virtual void resize (int x, int y, int width, int height)
	{
		int xPos = xposition();
		int yPos = yposition();
		int dw = width - w();
		int dh = height - h();
 
		if (dw>0)
			xPos -= dw;
		if (dh>0)
			yPos -= dh;
 
		if (xPos<0)
			xPos=0;
		if (yPos<0)
			yPos=0;
 
		Fl_Scroll::resize (x, y, width, height);
		position (xPos, yPos);
		redraw ();
	}
};
 
/**
 * Fl_Image is needed for FLTK binding with FrameBuffer class
 */
class MyFl_Image: public Fl_RGB_Image {
public:
	/**
	 * Create image object.
	 * \param fb Reference to frame buffer to use to load image.
	 */
	MyFl_Image (FrameBuffer &fb):
		Fl_RGB_Image (
				reinterpret_cast<const unsigned char *>(fb.pBuff()),
				fb.size().width, fb.size().height, sizeof(Pixel)),
		_fb(fb)
	{
	}
 
	/**
	 * Virtual method called to update image data.
	 * See FLTK documentation for more information.
	 */
	virtual void uncache ()
	{
		w (_fb.size().width);
		h (_fb.size().height);
		array = reinterpret_cast<const unsigned char *>(_fb.pBuff());
		data (reinterpret_cast<const char **>(&array), sizeof(Pixel));
		Fl_RGB_Image::uncache ();
	}
 
private:
	FrameBuffer &_fb;
};
 
#endif