view.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: samples/docview/view.cpp
  3. // Purpose: View classes implementation
  4. // Author: Julian Smart
  5. // Modified by: Vadim Zeitlin: merge with the MDI version and general cleanup
  6. // Created: 04/01/98
  7. // Copyright: (c) 1998 Julian Smart
  8. // (c) 2008 Vadim Zeitlin
  9. // Licence: wxWindows licence
  10. /////////////////////////////////////////////////////////////////////////////
  11. // For compilers that support precompilation, includes "wx/wx.h".
  12. #include "wx/wxprec.h"
  13. #ifdef __BORLANDC__
  14. #pragma hdrstop
  15. #endif
  16. #ifndef WX_PRECOMP
  17. #include "wx/wx.h"
  18. #endif
  19. #if !wxUSE_DOC_VIEW_ARCHITECTURE
  20. #error You must set wxUSE_DOC_VIEW_ARCHITECTURE to 1 in setup.h!
  21. #endif
  22. #include "docview.h"
  23. #include "doc.h"
  24. #include "view.h"
  25. // ----------------------------------------------------------------------------
  26. // DrawingView implementation
  27. // ----------------------------------------------------------------------------
  28. IMPLEMENT_DYNAMIC_CLASS(DrawingView, wxView)
  29. wxBEGIN_EVENT_TABLE(DrawingView, wxView)
  30. EVT_MENU(wxID_CUT, DrawingView::OnCut)
  31. wxEND_EVENT_TABLE()
  32. // What to do when a view is created. Creates actual
  33. // windows for displaying the view.
  34. bool DrawingView::OnCreate(wxDocument *doc, long flags)
  35. {
  36. if ( !wxView::OnCreate(doc, flags) )
  37. return false;
  38. MyApp& app = wxGetApp();
  39. if ( app.GetMode() != MyApp::Mode_Single )
  40. {
  41. // create a new window and canvas inside it
  42. wxFrame* frame = app.CreateChildFrame(this, true);
  43. wxASSERT(frame == GetFrame());
  44. m_canvas = new MyCanvas(this);
  45. frame->Show();
  46. }
  47. else // single document mode
  48. {
  49. // reuse the existing window and canvas
  50. m_canvas = app.GetMainWindowCanvas();
  51. m_canvas->SetView(this);
  52. // Initialize the edit menu Undo and Redo items
  53. doc->GetCommandProcessor()->SetEditMenu(app.GetMainWindowEditMenu());
  54. doc->GetCommandProcessor()->Initialize();
  55. }
  56. return true;
  57. }
  58. // Sneakily gets used for default print/preview as well as drawing on the
  59. // screen.
  60. void DrawingView::OnDraw(wxDC *dc)
  61. {
  62. dc->SetPen(*wxBLACK_PEN);
  63. // simply draw all lines of all segments
  64. const DoodleSegments& segments = GetDocument()->GetSegments();
  65. for ( DoodleSegments::const_iterator i = segments.begin();
  66. i != segments.end();
  67. ++i )
  68. {
  69. const DoodleLines& lines = i->GetLines();
  70. for ( DoodleLines::const_iterator j = lines.begin();
  71. j != lines.end();
  72. ++j )
  73. {
  74. const DoodleLine& line = *j;
  75. dc->DrawLine(line.x1, line.y1, line.x2, line.y2);
  76. }
  77. }
  78. }
  79. DrawingDocument* DrawingView::GetDocument()
  80. {
  81. return wxStaticCast(wxView::GetDocument(), DrawingDocument);
  82. }
  83. void DrawingView::OnUpdate(wxView* sender, wxObject* hint)
  84. {
  85. wxView::OnUpdate(sender, hint);
  86. if ( m_canvas )
  87. m_canvas->Refresh();
  88. }
  89. // Clean up windows used for displaying the view.
  90. bool DrawingView::OnClose(bool deleteWindow)
  91. {
  92. if ( !wxView::OnClose(deleteWindow) )
  93. return false;
  94. Activate(false);
  95. // Clear the canvas in single-window mode in which it stays alive
  96. if ( wxGetApp().GetMode() == MyApp::Mode_Single )
  97. {
  98. m_canvas->ClearBackground();
  99. m_canvas->ResetView();
  100. m_canvas = NULL;
  101. if (GetFrame())
  102. wxStaticCast(GetFrame(), wxFrame)->SetTitle(wxTheApp->GetAppDisplayName());
  103. }
  104. else // not single window mode
  105. {
  106. if ( deleteWindow )
  107. {
  108. GetFrame()->Destroy();
  109. SetFrame(NULL);
  110. }
  111. }
  112. return true;
  113. }
  114. void DrawingView::OnCut(wxCommandEvent& WXUNUSED(event) )
  115. {
  116. DrawingDocument * const doc = GetDocument();
  117. doc->GetCommandProcessor()->Submit(new DrawingRemoveSegmentCommand(doc));
  118. }
  119. // ----------------------------------------------------------------------------
  120. // TextEditView implementation
  121. // ----------------------------------------------------------------------------
  122. IMPLEMENT_DYNAMIC_CLASS(TextEditView, wxView)
  123. wxBEGIN_EVENT_TABLE(TextEditView, wxView)
  124. EVT_MENU(wxID_COPY, TextEditView::OnCopy)
  125. EVT_MENU(wxID_PASTE, TextEditView::OnPaste)
  126. EVT_MENU(wxID_SELECTALL, TextEditView::OnSelectAll)
  127. wxEND_EVENT_TABLE()
  128. bool TextEditView::OnCreate(wxDocument *doc, long flags)
  129. {
  130. if ( !wxView::OnCreate(doc, flags) )
  131. return false;
  132. wxFrame* frame = wxGetApp().CreateChildFrame(this, false);
  133. wxASSERT(frame == GetFrame());
  134. m_text = new wxTextCtrl(frame, wxID_ANY, "",
  135. wxDefaultPosition, wxDefaultSize,
  136. wxTE_MULTILINE);
  137. frame->Show();
  138. return true;
  139. }
  140. void TextEditView::OnDraw(wxDC *WXUNUSED(dc))
  141. {
  142. // nothing to do here, wxTextCtrl draws itself
  143. }
  144. bool TextEditView::OnClose(bool deleteWindow)
  145. {
  146. if ( !wxView::OnClose(deleteWindow) )
  147. return false;
  148. Activate(false);
  149. if ( wxGetApp().GetMode() == MyApp::Mode_Single )
  150. {
  151. m_text->Clear();
  152. }
  153. else // not single window mode
  154. {
  155. if ( deleteWindow )
  156. {
  157. GetFrame()->Destroy();
  158. SetFrame(NULL);
  159. }
  160. }
  161. return true;
  162. }
  163. // ----------------------------------------------------------------------------
  164. // MyCanvas implementation
  165. // ----------------------------------------------------------------------------
  166. wxBEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
  167. EVT_MOUSE_EVENTS(MyCanvas::OnMouseEvent)
  168. wxEND_EVENT_TABLE()
  169. // Define a constructor for my canvas
  170. MyCanvas::MyCanvas(wxView *view, wxWindow *parent)
  171. : wxScrolledWindow(parent ? parent : view->GetFrame())
  172. {
  173. m_view = view;
  174. m_currentSegment = NULL;
  175. m_lastMousePos = wxDefaultPosition;
  176. SetCursor(wxCursor(wxCURSOR_PENCIL));
  177. // this is completely arbitrary and is done just for illustration purposes
  178. SetVirtualSize(1000, 1000);
  179. SetScrollRate(20, 20);
  180. SetBackgroundColour(*wxWHITE);
  181. }
  182. MyCanvas::~MyCanvas()
  183. {
  184. delete m_currentSegment;
  185. }
  186. // Define the repainting behaviour
  187. void MyCanvas::OnDraw(wxDC& dc)
  188. {
  189. if ( m_view )
  190. m_view->OnDraw(& dc);
  191. }
  192. // This implements a tiny doodling program. Drag the mouse using the left
  193. // button.
  194. void MyCanvas::OnMouseEvent(wxMouseEvent& event)
  195. {
  196. if ( !m_view )
  197. return;
  198. wxClientDC dc(this);
  199. PrepareDC(dc);
  200. dc.SetPen(*wxBLACK_PEN);
  201. const wxPoint pt(event.GetLogicalPosition(dc));
  202. // is this the end of the current segment?
  203. if ( m_currentSegment && event.LeftUp() )
  204. {
  205. if ( !m_currentSegment->IsEmpty() )
  206. {
  207. // We've got a valid segment on mouse left up, so store it.
  208. DrawingDocument * const
  209. doc = wxStaticCast(m_view->GetDocument(), DrawingDocument);
  210. doc->GetCommandProcessor()->Submit(
  211. new DrawingAddSegmentCommand(doc, *m_currentSegment));
  212. doc->Modify(true);
  213. }
  214. wxDELETE(m_currentSegment);
  215. }
  216. // is this the start of a new segment?
  217. if ( m_lastMousePos != wxDefaultPosition && event.Dragging() )
  218. {
  219. if ( !m_currentSegment )
  220. m_currentSegment = new DoodleSegment;
  221. m_currentSegment->AddLine(m_lastMousePos, pt);
  222. dc.DrawLine(m_lastMousePos, pt);
  223. }
  224. m_lastMousePos = pt;
  225. }
  226. // ----------------------------------------------------------------------------
  227. // ImageCanvas implementation
  228. // ----------------------------------------------------------------------------
  229. // Define a constructor for my canvas
  230. ImageCanvas::ImageCanvas(wxView* view)
  231. : wxScrolledWindow(view->GetFrame())
  232. {
  233. m_view = view;
  234. SetScrollRate( 10, 10 );
  235. }
  236. // Define the repainting behaviour
  237. void ImageCanvas::OnDraw(wxDC& dc)
  238. {
  239. if ( m_view )
  240. m_view->OnDraw(& dc);
  241. }
  242. // ----------------------------------------------------------------------------
  243. // ImageView implementation
  244. // ----------------------------------------------------------------------------
  245. IMPLEMENT_DYNAMIC_CLASS(ImageView, wxView)
  246. ImageDocument* ImageView::GetDocument()
  247. {
  248. return wxStaticCast(wxView::GetDocument(), ImageDocument);
  249. }
  250. bool ImageView::OnCreate(wxDocument* doc, long flags)
  251. {
  252. if ( !wxView::OnCreate(doc, flags) )
  253. return false;
  254. wxFrame* frame = wxGetApp().CreateChildFrame(this, false);
  255. wxASSERT(frame == GetFrame());
  256. m_canvas = new ImageCanvas(this);
  257. frame->Show();
  258. return true;
  259. }
  260. void ImageView::OnUpdate(wxView* sender, wxObject* hint)
  261. {
  262. wxView::OnUpdate(sender, hint);
  263. wxImage image = GetDocument()->GetImage();
  264. if ( image.IsOk() )
  265. {
  266. m_canvas->SetVirtualSize(image.GetWidth(), image.GetHeight());
  267. }
  268. }
  269. void ImageView::OnDraw(wxDC* dc)
  270. {
  271. wxImage image = GetDocument()->GetImage();
  272. if ( image.IsOk() )
  273. {
  274. dc->DrawBitmap(wxBitmap(image), 0, 0, true /* use mask */);
  275. }
  276. }
  277. bool ImageView::OnClose(bool deleteWindow)
  278. {
  279. if ( !wxView::OnClose(deleteWindow) )
  280. return false;
  281. Activate(false);
  282. if ( wxGetApp().GetMode() == MyApp::Mode_Single )
  283. {
  284. GetDocument()->DeleteContents();
  285. }
  286. else // not single window mode
  287. {
  288. if ( deleteWindow )
  289. {
  290. GetFrame()->Destroy();
  291. SetFrame(NULL);
  292. }
  293. }
  294. return true;
  295. }
  296. // ----------------------------------------------------------------------------
  297. // ImageDetailsView
  298. // ----------------------------------------------------------------------------
  299. ImageDetailsView::ImageDetailsView(ImageDetailsDocument *doc)
  300. : wxView()
  301. {
  302. SetDocument(doc);
  303. m_frame = wxGetApp().CreateChildFrame(this, false);
  304. m_frame->SetTitle("Image Details");
  305. wxPanel * const panel = new wxPanel(m_frame);
  306. wxFlexGridSizer * const sizer = new wxFlexGridSizer(2, wxSize(5, 5));
  307. const wxSizerFlags
  308. flags = wxSizerFlags().Align(wxALIGN_CENTRE_VERTICAL).Border();
  309. sizer->Add(new wxStaticText(panel, wxID_ANY, "Image &file:"), flags);
  310. sizer->Add(new wxStaticText(panel, wxID_ANY, doc->GetFilename()), flags);
  311. sizer->Add(new wxStaticText(panel, wxID_ANY, "Image &type:"), flags);
  312. wxString typeStr;
  313. switch ( doc->GetType() )
  314. {
  315. case wxBITMAP_TYPE_PNG:
  316. typeStr = "PNG";
  317. break;
  318. case wxBITMAP_TYPE_JPEG:
  319. typeStr = "JPEG";
  320. break;
  321. default:
  322. typeStr = "Unknown";
  323. }
  324. sizer->Add(new wxStaticText(panel, wxID_ANY, typeStr), flags);
  325. sizer->Add(new wxStaticText(panel, wxID_ANY, "Image &size:"), flags);
  326. wxSize size = doc->GetSize();
  327. sizer->Add(new wxStaticText(panel, wxID_ANY,
  328. wxString::Format("%d*%d", size.x, size.y)),
  329. flags);
  330. sizer->Add(new wxStaticText(panel, wxID_ANY, "Number of unique &colours:"),
  331. flags);
  332. sizer->Add(new wxStaticText(panel, wxID_ANY,
  333. wxString::Format("%lu", doc->GetNumColours())),
  334. flags);
  335. sizer->Add(new wxStaticText(panel, wxID_ANY, "Uses &alpha:"), flags);
  336. sizer->Add(new wxStaticText(panel, wxID_ANY,
  337. doc->HasAlpha() ? "Yes" : "No"), flags);
  338. panel->SetSizer(sizer);
  339. m_frame->SetClientSize(panel->GetBestSize());
  340. m_frame->Show(true);
  341. }
  342. void ImageDetailsView::OnDraw(wxDC * WXUNUSED(dc))
  343. {
  344. // nothing to do here, we use controls to show our information
  345. }
  346. bool ImageDetailsView::OnClose(bool deleteWindow)
  347. {
  348. if ( wxGetApp().GetMode() != MyApp::Mode_Single && deleteWindow )
  349. {
  350. delete m_frame;
  351. m_frame = NULL;
  352. }
  353. return true;
  354. }