1 //------------------------------------------------------------------------------
2 // emFilePanel.h
3 //
4 // Copyright (C) 2004-2008,2010,2016-2017 Oliver Hamann.
5 //
6 // Homepage: http://eaglemode.sourceforge.net/
7 //
8 // This program is free software: you can redistribute it and/or modify it under
9 // the terms of the GNU General Public License version 3 as published by the
10 // Free Software Foundation.
11 //
12 // This program is distributed in the hope that it will be useful, but WITHOUT
13 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 // FOR A PARTICULAR PURPOSE. See the GNU General Public License version 3 for
15 // more details.
16 //
17 // You should have received a copy of the GNU General Public License version 3
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
19 //------------------------------------------------------------------------------
20 
21 #ifndef emFilePanel_h
22 #define emFilePanel_h
23 
24 #ifndef emPanel_h
25 #include <emCore/emPanel.h>
26 #endif
27 
28 #ifndef emFileModel_h
29 #include <emCore/emFileModel.h>
30 #endif
31 
32 
33 //==============================================================================
34 //================================ emFilePanel =================================
35 //==============================================================================
36 
37 class emFilePanel : public emPanel {
38 
39 public:
40 
41 	// Base class for a panel with which the user can view or edit a file
42 	// that is interfaced with an emFileModel. Internally, an object of this
43 	// class manages an emFileModelClient. The memory limit and priority of
44 	// that client is set and updated from the panel properties. In
45 	// addition, a virtual file state is provided (a more correct name would
46 	// be: "virtual file model state"). This is similar to the file state of
47 	// the model, but with some extensions (see GetVirFileState()). The
48 	// panel shows information about that virtual file state. A derived
49 	// class should overload the Paint method for showing the file contents
50 	// when the virtual file state is good (loaded or unsaved).
51 
52 	emFilePanel(
53 		ParentArg parent, const emString & name,
54 		emFileModel * fileModel=NULL, bool updateFileModel=true
55 	);
56 		// Constructor.
57 		// Arguments:
58 		//   parent    - Parent for this panel (emPanel or emView).
59 		//   name      - The name for this panel.
60 		//   fileModel - See SetFileModel.
61 		//   updateFileModel - See SetFileModel.
62 
63 	virtual ~emFilePanel();
64 		// Destructor.
65 
66 	emFileModel * GetFileModel() const;
67 		// Get the file model, NULL if none.
68 
69 	virtual void SetFileModel(emFileModel * fileModel,
70 	                          bool updateFileModel=true);
71 		// Set the file model, NULL for none. If updateFileModel==true,
72 		// fileModel->Update() is called.
73 
74 	void SetCustomError(const emString & message);
75 	void ClearCustomError();
76 	emString GetCustomError() const;
77 		// Set, clear or get a custom error message. If set, the message
78 		// is shown by the default implementation of Paint.
79 
80 	const emSignal & GetVirFileStateSignal() const;
81 		// Signaled when the virtual file state has changed.
82 
83 	enum VirtualFileState {
84 		VFS_WAITING       = emFileModel::FS_WAITING,
85 		VFS_LOADING       = emFileModel::FS_LOADING,
86 		VFS_LOADED        = emFileModel::FS_LOADED,
87 		VFS_UNSAVED       = emFileModel::FS_UNSAVED,
88 		VFS_SAVING        = emFileModel::FS_SAVING,
89 		VFS_TOO_COSTLY    = emFileModel::FS_TOO_COSTLY,
90 		VFS_LOAD_ERROR    = emFileModel::FS_LOAD_ERROR,
91 		VFS_SAVE_ERROR    = emFileModel::FS_SAVE_ERROR,
92 		VFS_NO_FILE_MODEL = emFileModel::FS_MAX_VAL+1,
93 		VFS_CUSTOM_ERROR  = emFileModel::FS_MAX_VAL+2
94 	};
95 	VirtualFileState GetVirFileState() const;
96 		// Get the virtual file state. This is like the file state of
97 		// the model (GetFileMode()->GetFileState()), but:
98 		// - There is the additional state VFS_CUSTOM_ERROR. It is set
99 		//   when a custom error has been reported through
100 		//   SetCustomError(...).
101 		// - There is the additional state VFS_NO_FILE_MODEL. It is set
102 		//   when GetFileModel()==NULL.
103 		// - If the memory limit of this panel is smaller than the
104 		//   memory need of the model, the virtual file state is forced
105 		//   to VFS_TOO_COSTLY even when the model is in loaded state.
106 		//   (Otherwise the show state of the panel could depend on the
107 		//   show state of another panel - too ugly)
108 
109 	bool IsVFSGood() const;
110 		// This is a short-cut for:
111 		//  (GetVirFileState()==VFS_LOADED ||
112 		//   GetVirFileState()==VFS_UNSAVED)
113 		// It means that the file model data can safely be shown and
114 		// modified.
115 
116 	virtual emString GetIconFileName() const;
117 
118 	virtual bool IsContentReady(bool * pReadying=NULL) const;
119 
120 protected:
121 
122 	virtual bool Cycle();
123 
124 	virtual void Notice(NoticeFlags flags);
125 
126 	virtual bool IsOpaque() const;
127 
128 	virtual void Paint(const emPainter & painter, emColor canvasColor) const;
129 		// Paints some info about the virtual file state including any
130 		// error messages. Should be overloaded for showing the file
131 		// model data when IsVFSGood()==true. InvalidatePainting is
132 		// called automatically on each change of the virtual file
133 		// state.
134 
135 	virtual bool IsHopeForSeeking() const;
136 		// Returns true if VFS_WAITING, VFS_LOADING or VFS_SAVING.
137 
138 private:
139 
140 	emFileModelClient FileModelClient;
141 	emString * CustomError;
142 	emSignal VirFileStateSignal;
143 };
144 
GetFileModel()145 inline emFileModel * emFilePanel::GetFileModel() const
146 {
147 	return FileModelClient.GetModel();
148 }
149 
GetVirFileStateSignal()150 inline const emSignal & emFilePanel::GetVirFileStateSignal() const
151 {
152 	return VirFileStateSignal;
153 }
154 
155 
156 #endif
157