sashtest.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: sashtest.cpp
  3. // Purpose: Layout/sash sample
  4. // Author: Julian Smart
  5. // Modified by:
  6. // Created: 04/01/98
  7. // Copyright: (c) Julian Smart
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. // For compilers that support precompilation, includes "wx/wx.h".
  11. #include "wx/wxprec.h"
  12. #ifdef __BORLANDC__
  13. #pragma hdrstop
  14. #endif
  15. #ifndef WX_PRECOMP
  16. #include "wx/wx.h"
  17. #include "wx/mdi.h"
  18. #endif
  19. #include "wx/toolbar.h"
  20. #include "wx/laywin.h"
  21. #include "sashtest.h"
  22. MyFrame *frame = NULL;
  23. wxList my_children;
  24. IMPLEMENT_APP(MyApp)
  25. // For drawing lines in a canvas
  26. long xpos = -1;
  27. long ypos = -1;
  28. int winNumber = 1;
  29. // Initialise this in OnInit, not statically
  30. bool MyApp::OnInit(void)
  31. {
  32. if ( !wxApp::OnInit() )
  33. return false;
  34. // Create the main frame window
  35. frame = new MyFrame(NULL, wxID_ANY, wxT("Sash Demo"), wxPoint(0, 0), wxSize(500, 400),
  36. wxDEFAULT_FRAME_STYLE |
  37. wxNO_FULL_REPAINT_ON_RESIZE |
  38. wxHSCROLL | wxVSCROLL);
  39. // Give it an icon (this is ignored in MDI mode: uses resources)
  40. #ifdef __WXMSW__
  41. frame->SetIcon(wxIcon(wxT("sashtest_icn")));
  42. #endif
  43. // Make a menubar
  44. wxMenu *file_menu = new wxMenu;
  45. file_menu->Append(SASHTEST_NEW_WINDOW, wxT("&New window"));
  46. file_menu->Append(SASHTEST_TOGGLE_WINDOW, wxT("&Toggle window"));
  47. file_menu->Append(SASHTEST_QUIT, wxT("&Exit"));
  48. wxMenu *help_menu = new wxMenu;
  49. help_menu->Append(SASHTEST_ABOUT, wxT("&About"));
  50. wxMenuBar *menu_bar = new wxMenuBar;
  51. menu_bar->Append(file_menu, wxT("&File"));
  52. menu_bar->Append(help_menu, wxT("&Help"));
  53. // Associate the menu bar with the frame
  54. frame->SetMenuBar(menu_bar);
  55. #if wxUSE_STATUSBAR
  56. frame->CreateStatusBar();
  57. #endif // wxUSE_STATUSBAR
  58. frame->Show(true);
  59. return true;
  60. }
  61. wxBEGIN_EVENT_TABLE(MyFrame, wxMDIParentFrame)
  62. EVT_MENU(SASHTEST_ABOUT, MyFrame::OnAbout)
  63. EVT_MENU(SASHTEST_NEW_WINDOW, MyFrame::OnNewWindow)
  64. EVT_SIZE(MyFrame::OnSize)
  65. EVT_MENU(SASHTEST_QUIT, MyFrame::OnQuit)
  66. EVT_MENU(SASHTEST_TOGGLE_WINDOW, MyFrame::OnToggleWindow)
  67. EVT_SASH_DRAGGED_RANGE(ID_WINDOW_TOP, ID_WINDOW_BOTTOM, MyFrame::OnSashDrag)
  68. wxEND_EVENT_TABLE()
  69. // Define my frame constructor
  70. MyFrame::MyFrame(wxWindow *parent, const wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size,
  71. const long style):
  72. wxMDIParentFrame(parent, id, title, pos, size, style)
  73. {
  74. // Create some dummy layout windows
  75. // A window like a toolbar
  76. wxSashLayoutWindow* win =
  77. new wxSashLayoutWindow(this, ID_WINDOW_TOP,
  78. wxDefaultPosition, wxSize(200, 30),
  79. wxNO_BORDER | wxSW_3D | wxCLIP_CHILDREN);
  80. win->SetDefaultSize(wxSize(1000, 30));
  81. win->SetOrientation(wxLAYOUT_HORIZONTAL);
  82. win->SetAlignment(wxLAYOUT_TOP);
  83. win->SetBackgroundColour(*wxRED);
  84. win->SetSashVisible(wxSASH_BOTTOM, true);
  85. m_topWindow = win;
  86. // A window like a statusbar
  87. win = new wxSashLayoutWindow(this, ID_WINDOW_BOTTOM,
  88. wxDefaultPosition, wxSize(200, 30),
  89. wxNO_BORDER | wxSW_3D | wxCLIP_CHILDREN);
  90. win->SetDefaultSize(wxSize(1000, 30));
  91. win->SetOrientation(wxLAYOUT_HORIZONTAL);
  92. win->SetAlignment(wxLAYOUT_BOTTOM);
  93. win->SetBackgroundColour(*wxBLUE);
  94. win->SetSashVisible(wxSASH_TOP, true);
  95. m_bottomWindow = win;
  96. // A window to the left of the client window
  97. win = new wxSashLayoutWindow(this, ID_WINDOW_LEFT1,
  98. wxDefaultPosition, wxSize(200, 30),
  99. wxNO_BORDER | wxSW_3D | wxCLIP_CHILDREN);
  100. win->SetDefaultSize(wxSize(120, 1000));
  101. win->SetOrientation(wxLAYOUT_VERTICAL);
  102. win->SetAlignment(wxLAYOUT_LEFT);
  103. win->SetBackgroundColour(*wxGREEN);
  104. win->SetSashVisible(wxSASH_RIGHT, true);
  105. win->SetExtraBorderSize(10);
  106. wxTextCtrl* textWindow = new wxTextCtrl(win, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize,
  107. wxTE_MULTILINE|wxSUNKEN_BORDER);
  108. // wxTE_MULTILINE|wxNO_BORDER);
  109. textWindow->SetValue(wxT("A help window"));
  110. m_leftWindow1 = win;
  111. // Another window to the left of the client window
  112. win = new wxSashLayoutWindow(this, ID_WINDOW_LEFT2,
  113. wxDefaultPosition, wxSize(200, 30),
  114. wxNO_BORDER | wxSW_3D | wxCLIP_CHILDREN);
  115. win->SetDefaultSize(wxSize(120, 1000));
  116. win->SetOrientation(wxLAYOUT_VERTICAL);
  117. win->SetAlignment(wxLAYOUT_LEFT);
  118. win->SetBackgroundColour(wxColour(0, 255, 255));
  119. win->SetSashVisible(wxSASH_RIGHT, true);
  120. m_leftWindow2 = win;
  121. }
  122. void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
  123. {
  124. Close(true);
  125. }
  126. void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
  127. {
  128. (void)wxMessageBox(wxT("wxWidgets 2.0 Sash Demo\nAuthor: Julian Smart (c) 1998"), wxT("About Sash Demo"));
  129. }
  130. void MyFrame::OnToggleWindow(wxCommandEvent& WXUNUSED(event))
  131. {
  132. if (m_leftWindow1->IsShown())
  133. {
  134. m_leftWindow1->Show(false);
  135. }
  136. else
  137. {
  138. m_leftWindow1->Show(true);
  139. }
  140. #if wxUSE_MDI_ARCHITECTURE
  141. wxLayoutAlgorithm layout;
  142. layout.LayoutMDIFrame(this);
  143. #endif // wxUSE_MDI_ARCHITECTURE
  144. }
  145. void MyFrame::OnSashDrag(wxSashEvent& event)
  146. {
  147. if (event.GetDragStatus() == wxSASH_STATUS_OUT_OF_RANGE)
  148. return;
  149. switch (event.GetId())
  150. {
  151. case ID_WINDOW_TOP:
  152. {
  153. m_topWindow->SetDefaultSize(wxSize(1000, event.GetDragRect().height));
  154. break;
  155. }
  156. case ID_WINDOW_LEFT1:
  157. {
  158. m_leftWindow1->SetDefaultSize(wxSize(event.GetDragRect().width, 1000));
  159. break;
  160. }
  161. case ID_WINDOW_LEFT2:
  162. {
  163. m_leftWindow2->SetDefaultSize(wxSize(event.GetDragRect().width, 1000));
  164. break;
  165. }
  166. case ID_WINDOW_BOTTOM:
  167. {
  168. m_bottomWindow->SetDefaultSize(wxSize(1000, event.GetDragRect().height));
  169. break;
  170. }
  171. }
  172. #if wxUSE_MDI_ARCHITECTURE
  173. wxLayoutAlgorithm layout;
  174. layout.LayoutMDIFrame(this);
  175. #endif // wxUSE_MDI_ARCHITECTURE
  176. // Leaves bits of itself behind sometimes
  177. GetClientWindow()->Refresh();
  178. }
  179. void MyFrame::OnNewWindow(wxCommandEvent& WXUNUSED(event))
  180. {
  181. // Make another frame, containing a canvas
  182. MyChild *subframe = new MyChild(frame, wxT("Canvas Frame"),
  183. wxPoint(10, 10), wxSize(300, 300),
  184. wxDEFAULT_FRAME_STYLE |
  185. wxNO_FULL_REPAINT_ON_RESIZE);
  186. subframe->SetTitle(wxString::Format(wxT("Canvas Frame %d"), winNumber));
  187. winNumber ++;
  188. // Give it an icon (this is ignored in MDI mode: uses resources)
  189. #ifdef __WXMSW__
  190. subframe->SetIcon(wxIcon(wxT("sashtest_icn")));
  191. #endif
  192. #if wxUSE_STATUSBAR
  193. // Give it a status line
  194. subframe->CreateStatusBar();
  195. #endif // wxUSE_STATUSBAR
  196. // Make a menubar
  197. wxMenu *file_menu = new wxMenu;
  198. file_menu->Append(SASHTEST_NEW_WINDOW, wxT("&New window"));
  199. file_menu->Append(SASHTEST_CHILD_QUIT, wxT("&Close child"));
  200. file_menu->Append(SASHTEST_QUIT, wxT("&Exit"));
  201. wxMenu *option_menu = new wxMenu;
  202. // Dummy option
  203. option_menu->Append(SASHTEST_REFRESH, wxT("&Refresh picture"));
  204. wxMenu *help_menu = new wxMenu;
  205. help_menu->Append(SASHTEST_ABOUT, wxT("&About"));
  206. wxMenuBar *menu_bar = new wxMenuBar;
  207. menu_bar->Append(file_menu, wxT("&File"));
  208. menu_bar->Append(option_menu, wxT("&Options"));
  209. menu_bar->Append(help_menu, wxT("&Help"));
  210. // Associate the menu bar with the frame
  211. subframe->SetMenuBar(menu_bar);
  212. int width, height;
  213. subframe->GetClientSize(&width, &height);
  214. MyCanvas *canvas = new MyCanvas(subframe, wxPoint(0, 0), wxSize(width, height));
  215. canvas->SetCursor(wxCursor(wxCURSOR_PENCIL));
  216. subframe->canvas = canvas;
  217. // Give it scrollbars
  218. canvas->SetScrollbars(20, 20, 50, 50);
  219. subframe->Show(true);
  220. }
  221. wxBEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
  222. EVT_MOUSE_EVENTS(MyCanvas::OnEvent)
  223. wxEND_EVENT_TABLE()
  224. // Define a constructor for my canvas
  225. MyCanvas::MyCanvas(wxWindow *parent, const wxPoint& pos, const wxSize& size)
  226. : wxScrolledWindow(parent, wxID_ANY, pos, size,
  227. wxSUNKEN_BORDER | wxNO_FULL_REPAINT_ON_RESIZE)
  228. {
  229. SetBackgroundColour(* wxWHITE);
  230. }
  231. // Define the repainting behaviour
  232. void MyCanvas::OnDraw(wxDC& dc)
  233. {
  234. dc.SetFont(*wxSWISS_FONT);
  235. dc.SetPen(*wxGREEN_PEN);
  236. dc.DrawLine(0, 0, 200, 200);
  237. dc.DrawLine(200, 0, 0, 200);
  238. dc.SetBrush(*wxCYAN_BRUSH);
  239. dc.SetPen(*wxRED_PEN);
  240. dc.DrawRectangle(100, 100, 100, 50);
  241. dc.DrawRoundedRectangle(150, 150, 100, 50, 20);
  242. dc.DrawEllipse(250, 250, 100, 50);
  243. #if wxUSE_SPLINES
  244. dc.DrawSpline(50, 200, 50, 100, 200, 10);
  245. #endif // wxUSE_SPLINES
  246. dc.DrawLine(50, 230, 200, 230);
  247. dc.DrawText(wxT("This is a test string"), 50, 230);
  248. wxPoint points[3];
  249. points[0].x = 200; points[0].y = 300;
  250. points[1].x = 100; points[1].y = 400;
  251. points[2].x = 300; points[2].y = 400;
  252. dc.DrawPolygon(3, points);
  253. }
  254. // This implements a tiny doodling program! Drag the mouse using
  255. // the left button.
  256. void MyCanvas::OnEvent(wxMouseEvent& event)
  257. {
  258. wxClientDC dc(this);
  259. PrepareDC(dc);
  260. wxPoint pt(event.GetLogicalPosition(dc));
  261. if (xpos > -1 && ypos > -1 && event.Dragging())
  262. {
  263. dc.SetPen(*wxBLACK_PEN);
  264. dc.DrawLine(xpos, ypos, pt.x, pt.y);
  265. }
  266. xpos = pt.x;
  267. ypos = pt.y;
  268. }
  269. void MyFrame::OnSize(wxSizeEvent& WXUNUSED(event))
  270. {
  271. #if wxUSE_MDI_ARCHITECTURE
  272. wxLayoutAlgorithm layout;
  273. layout.LayoutMDIFrame(this);
  274. #endif // wxUSE_MDI_ARCHITECTURE
  275. }
  276. // Note that SASHTEST_NEW_WINDOW and SASHTEST_ABOUT commands get passed
  277. // to the parent window for processing, so no need to
  278. // duplicate event handlers here.
  279. wxBEGIN_EVENT_TABLE(MyChild, wxMDIChildFrame)
  280. EVT_MENU(SASHTEST_CHILD_QUIT, MyChild::OnQuit)
  281. wxEND_EVENT_TABLE()
  282. MyChild::MyChild(wxMDIParentFrame *parent, const wxString& title, const wxPoint& pos, const wxSize& size,
  283. const long style):
  284. wxMDIChildFrame(parent, wxID_ANY, title, pos, size, style)
  285. {
  286. canvas = NULL;
  287. my_children.Append(this);
  288. }
  289. MyChild::~MyChild(void)
  290. {
  291. my_children.DeleteObject(this);
  292. }
  293. void MyChild::OnQuit(wxCommandEvent& WXUNUSED(event))
  294. {
  295. Close(true);
  296. }
  297. void MyChild::OnActivate(wxActivateEvent& event)
  298. {
  299. if (event.GetActive() && canvas)
  300. canvas->SetFocus();
  301. }