1 #ifdef __GNUG__
2 // #pragma implementation
3 #endif
4
5 // For compilers that support precompilation, includes "wx/wx.h".
6 #include <wx/wxprec.h>
7
8 #ifdef __BORLANDC__
9 #pragma hdrstop
10 #endif
11
12 #ifndef WX_PRECOMP
13 #include <wx/wx.h>
14 #endif
15
16 #include <wx/bitmap.h>
17 #include <wx/image.h>
18 #include <wx/dcbuffer.h>
19
20 #include "NassiView.h"
21 #include "NassiDiagramWindow.h"
22 #include "NassiDropTarget.h"
23 //#include "DataObject.h"
24 #include "commands.h"
25 #include "HooverDrawlet.h"
26
27
28 //IMPLEMENT_DYNAMIC_CLASS(NassiDiagramWindow, wxScrolledWindow)
BEGIN_EVENT_TABLE(NassiDiagramWindow,wxScrolledWindow)29 BEGIN_EVENT_TABLE(NassiDiagramWindow, wxScrolledWindow)
30 EVT_MOUSEWHEEL(NassiDiagramWindow::OnMouseWheel)
31 EVT_RIGHT_DOWN(NassiDiagramWindow::OnMouseRightDown)
32 EVT_RIGHT_UP(NassiDiagramWindow::OnMouseRightUp)
33 EVT_LEFT_DOWN(NassiDiagramWindow::OnMouseLeftDown)
34 EVT_LEFT_UP(NassiDiagramWindow::OnMouseLeftUp)
35 EVT_MOTION(NassiDiagramWindow::OnMouseMove)
36
37 EVT_LEAVE_WINDOW(NassiDiagramWindow::OnEnter)
38 EVT_ENTER_WINDOW(NassiDiagramWindow::OnLeave)
39
40 EVT_PAINT(NassiDiagramWindow::OnPaint)
41 EVT_ERASE_BACKGROUND(NassiDiagramWindow::OnErase)
42 EVT_KEY_DOWN(NassiDiagramWindow::OnKeyDown)
43 EVT_CHAR(NassiDiagramWindow::OnChar)
44 //EVT_CHAR_HOOK(NassiDiagramWindow::OnChar)
45 EVT_SET_FOCUS(NassiDiagramWindow::OnSetFocus)
46 EVT_KILL_FOCUS(NassiDiagramWindow::OnKillFocus)
47
48 END_EVENT_TABLE()
49
50 // Define a constructor for my window
51 //MyDiagramWindow::MyDiagramWindow(wxView *v, wxMDIChildFrame *frame, const wxPoint& pos, const wxSize& size, long style):
52 // wxScrolledWindow(frame, wxID_ANY, pos, size, style)
53 NassiDiagramWindow::NassiDiagramWindow(wxWindow *parent, NassiView *view):
54 wxScrolledWindow(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxSUNKEN_BORDER | wxWANTS_CHARS ),
55 //dndpt(-1, -1),
56 m_view(view),
57 m_hd(0)
58 {
59 SetDropTarget( new NassiDropTarget(this, view) );
60
61 SetCursor(wxCursor(wxCURSOR_ARROW));
62 SetScrollRate(5, 5);
63
64 wxClientDC dc(this);
65 dc.SetFont(m_view->GetSourceFont());
66 SetCaret( new wxCaret(this, 10,10) );//1, dc.GetCharHeight()
67 SetFocus();
68 }
69
~NassiDiagramWindow()70 NassiDiagramWindow::~NassiDiagramWindow()
71 {
72 if ( m_hd )
73 delete m_hd;
74 // is not needed?
75 // if ( caret )
76 // delete caret;
77 }
78
OnMouseWheel(wxMouseEvent & event)79 void NassiDiagramWindow::OnMouseWheel(wxMouseEvent& event)
80 {
81 wxClientDC dc(this);
82 PrepareDC(dc);
83 dc.SetFont(m_view->GetCommentFont());
84 wxCoord /*dx,*/ dy;
85 //dx = dc.GetCharWidth();
86 dy = dc.GetCharHeight();
87
88 if ( m_hd )
89 {
90 m_hd->Draw(dc);
91 delete m_hd;
92 m_hd = 0;
93 }
94 if ( ! event.IsPageScroll() )
95 {
96 wxInt32 nWheelRotation = event.GetWheelRotation();
97 if ( !event.ControlDown() )
98 {
99 wxInt32 x, y;
100 GetViewStart(&x, &y);
101 if (nWheelRotation < 0)
102 y += dy/4;
103 else
104 y -= dy/4;
105 Scroll(x, y);
106 }
107 else
108 {
109 if (nWheelRotation < 0)
110 m_view->ZoomIn();
111 else
112 m_view->ZoomOut();
113 }
114 }
115 }
116
OnErase(wxEraseEvent &)117 void NassiDiagramWindow::OnErase(wxEraseEvent & /*event*/){}
118
OnPaint(wxPaintEvent &)119 void NassiDiagramWindow::OnPaint(wxPaintEvent & /*event*/)
120 {
121 wxBufferedPaintDC dc(this);
122 DoPrepareDC(dc);
123 PaintBackground(dc);
124 Draw(dc);
125 }
126
PaintBackground(wxDC & dc)127 void NassiDiagramWindow::PaintBackground(wxDC &dc)
128 {
129 wxColour backgroundColour = GetBackgroundColour();
130 if ( !backgroundColour.Ok())
131 backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
132
133 dc.SetBrush(wxBrush(backgroundColour));
134 dc.SetPen(wxPen(backgroundColour, 1));
135 wxRect windowRect(wxPoint(0,0), GetClientSize());
136 CalcUnscrolledPosition(windowRect.x, windowRect.y, &windowRect.x, &windowRect.y);
137 dc.DrawRectangle(windowRect);
138 dc.SetBrush(wxNullBrush);
139 dc.SetPen(wxNullPen);
140 }
141
Draw(wxDC & dc)142 void NassiDiagramWindow::Draw(wxDC& dc)
143 {
144 m_view->DrawDiagram(&dc);
145
146 if ( m_hd ) m_hd->Draw(dc);
147 }
148
OnEnter(wxMouseEvent &)149 void NassiDiagramWindow::OnEnter(wxMouseEvent & /*event*/)
150 {
151 wxClientDC dc(this);
152 DoPrepareDC(dc);
153
154 if ( m_hd )
155 m_hd->Draw(dc);
156 }
157
OnLeave(wxMouseEvent &)158 void NassiDiagramWindow::OnLeave(wxMouseEvent & /*event*/)
159 {
160 wxClientDC dc(this);
161 DoPrepareDC(dc);
162
163 if ( m_hd )
164 m_hd->UnDraw(dc);
165 }
166
OnChar(wxKeyEvent & event)167 void NassiDiagramWindow::OnChar(wxKeyEvent & event)
168 {
169 m_view->OnChar(event);
170 }
171
OnKeyDown(wxKeyEvent & event)172 void NassiDiagramWindow::OnKeyDown(wxKeyEvent &event)
173 {
174 m_view->OnKeyDown(event);
175 //event.Skip();
176 }
177
OnMouseLeftDown(wxMouseEvent & event)178 void NassiDiagramWindow::OnMouseLeftDown(wxMouseEvent &event)
179 {
180 wxClientDC dc(this);
181 DoPrepareDC(dc);
182 RemoveDrawlet(dc);
183 wxPoint pos = event.GetLogicalPosition(dc);
184 m_view->OnMouseLeftDown(event, pos);
185
186 this->SetFocus();
187 }
188
OnMouseLeftUp(wxMouseEvent & event)189 void NassiDiagramWindow::OnMouseLeftUp(wxMouseEvent &event)
190 {
191 wxClientDC dc(this);
192 DoPrepareDC(dc);
193 RemoveDrawlet(dc);
194
195 wxPoint pos = event.GetLogicalPosition(dc);
196 m_view->OnMouseLeftUp(event, pos);
197
198 this->SetFocus();
199 }
200
OnMouseRightDown(wxMouseEvent & event)201 void NassiDiagramWindow::OnMouseRightDown(wxMouseEvent &event)
202 {
203 wxClientDC dc(this);
204 DoPrepareDC(dc);
205 RemoveDrawlet(dc);
206
207 wxPoint pos = event.GetLogicalPosition(dc);
208 m_view->OnMouseRightDown(event, pos);
209
210 this->SetFocus();
211 }
212
OnMouseRightUp(wxMouseEvent & event)213 void NassiDiagramWindow::OnMouseRightUp(wxMouseEvent& event)
214 {
215 wxClientDC dc(this);
216 DoPrepareDC(dc);
217 RemoveDrawlet(dc);
218
219 wxPoint pos = event.GetLogicalPosition(dc);
220 m_view->OnMouseRightUp(event, pos);
221
222 this->SetFocus();
223 }
224
OnMouseMove(wxMouseEvent & event)225 void NassiDiagramWindow::OnMouseMove(wxMouseEvent &event)
226 {
227 wxClientDC dc(this);
228 DoPrepareDC(dc);
229
230 RemoveDrawlet(dc);
231
232 wxPoint pos = event.GetLogicalPosition(dc);
233 m_hd = m_view->OnMouseMove(event, pos);
234
235 if ( m_hd && !m_hd->Draw(dc) )
236 {
237 delete m_hd;
238 m_hd = 0;
239 }
240 }
241
RemoveDrawlet(wxDC & dc)242 void NassiDiagramWindow::RemoveDrawlet(wxDC &dc)
243 {
244 if ( m_hd )
245 {
246 m_hd->UnDraw(dc);
247 delete m_hd;
248 m_hd = 0;
249 }
250 }
251
OnSetFocus(wxFocusEvent &)252 void NassiDiagramWindow::OnSetFocus(wxFocusEvent & /*event*/){}
253
OnKillFocus(wxFocusEvent &)254 void NassiDiagramWindow::OnKillFocus(wxFocusEvent & /*event*/)
255 {
256 wxClientDC dc(this);
257 DoPrepareDC(dc);
258
259 RemoveDrawlet(dc);
260 }
261
OnDrop(const wxPoint & pt,NassiBrick * brick,wxString strc,wxString strs,wxDragResult def)262 wxDragResult NassiDiagramWindow::OnDrop(const wxPoint &pt, NassiBrick *brick, wxString strc, wxString strs, wxDragResult def )
263 {
264 wxClientDC dc(this);
265 DoPrepareDC(dc);
266 wxCoord xx, yy;
267 CalcUnscrolledPosition( pt.x, pt.y, &xx, &yy);
268
269 wxPoint pos(xx, yy);
270
271 RemoveDrawlet(dc);
272
273 return m_view->OnDrop(pos, brick, strc, strs, def);
274 }
275
OnDragOver(const wxPoint & pt,wxDragResult def,bool HasNoBricks)276 wxDragResult NassiDiagramWindow::OnDragOver(const wxPoint &pt, wxDragResult def, bool HasNoBricks)
277 {
278 wxClientDC dc(this);
279 DoPrepareDC(dc);
280 wxCoord xx, yy;
281 CalcUnscrolledPosition( pt.x, pt.y, &xx, &yy);
282 wxPoint pos(xx, yy);
283 RemoveDrawlet(dc);
284
285 m_hd = m_view->OnDragOver(pos, def, HasNoBricks);
286
287 if ( m_hd && !m_hd->Draw(dc) )
288 {
289 delete m_hd;
290 m_hd = 0;
291 }
292
293 return def;
294 }
295
OnDragLeave()296 void NassiDiagramWindow::OnDragLeave()
297 {
298 m_view->OnDragLeave();
299 }
300
OnDragEnter()301 void NassiDiagramWindow::OnDragEnter()
302 {
303 wxClientDC dc(this);
304 DoPrepareDC(dc);
305
306 RemoveDrawlet(dc);
307
308 m_view->OnDragEnter();
309 }
310
311