home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / wxos2233.zip / wxOS2-2_3_3.zip / wxWindows-2.3.3 / contrib / samples / gizmos / dynsash_switch / dynsash_switch.cpp next >
C/C++ Source or Header  |  2002-03-07  |  8KB  |  234 lines

  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name:        dynsash_switch.cpp
  3. // Purpose:     Test custom scrollbar handling of wxDynamicSashWindow by
  4. //              creating a dynamic sash window where the client scrolls a
  5. //              a subwindow of the client window by responding to scroll
  6. //              events itself
  7. // Author:      Matt Kimball
  8. // Modified by:
  9. // Created:     7/15/2001
  10. // RCS-ID:      $Id: dynsash_switch.cpp,v 1.3 2002/03/07 10:06:22 JS Exp $
  11. // Copyright:   (c) 2001 Matt Kimball
  12. // Licence:     wxWindows licence
  13. /////////////////////////////////////////////////////////////////////////////
  14.  
  15. // For compilers that support precompilation, includes "wx/wx.h".
  16. #include "wx/wxprec.h"
  17.  
  18. #ifdef __BORLANDC__
  19.     #pragma hdrstop
  20. #endif
  21.  
  22. // for all others, include the necessary headers (this file is usually all you
  23. // need because it includes almost all "standard" wxWindows headers)
  24. #ifndef WX_PRECOMP
  25.     #include "wx/wx.h"
  26. #endif
  27.  
  28. #include <wx/app.h>
  29. #include <wx/frame.h>
  30. #include <wx/choice.h>
  31. #include <wx/dcclient.h>
  32. #include <wx/gizmos/dynamicsash.h>
  33. #include <wx/layout.h>
  34. #include <wx/scrolbar.h>
  35.  
  36. class SwitchDemo : public wxApp {
  37. public:
  38.     bool OnInit();
  39. };
  40.  
  41.  
  42. class SwitchView : public wxWindow {
  43. public:
  44.     SwitchView(wxDynamicSashWindow *parent);
  45.  
  46.     wxSize DoGetBestSize() const;
  47.  
  48. private:
  49.     void OnSize(wxSizeEvent& event);
  50.     void OnPaint(wxPaintEvent& event);
  51.     void OnChoice(wxCommandEvent& event);
  52.     void OnScroll(wxScrollEvent& event);
  53.     void OnFocus(wxFocusEvent& event);
  54.     void OnErase(wxEraseEvent& event);
  55.     void OnSplit(wxDynamicSashSplitEvent& event);
  56.     void OnUnify(wxDynamicSashUnifyEvent& event);
  57.  
  58.     wxDynamicSashWindow *m_dyn_sash;
  59.     wxWindow *m_bar;
  60.     wxChoice *m_choice;
  61.     wxWindow *m_view;
  62. };
  63.  
  64. IMPLEMENT_APP(SwitchDemo)
  65.  
  66.  
  67. SwitchView::SwitchView(wxDynamicSashWindow *win) {
  68.     Create(win, -1);
  69.  
  70.     m_dyn_sash = win;
  71.  
  72.     m_bar = new wxWindow(this, -1, wxDefaultPosition, wxDefaultSize, wxRAISED_BORDER, "bar");
  73.     m_choice = new wxChoice(m_bar, -1);
  74.     m_choice->SetEventHandler(this);
  75.     m_view = new wxWindow(this, -1, wxDefaultPosition, wxDefaultSize, 0, "view");
  76.     m_view->SetBackgroundColour(*wxWHITE);
  77.     m_view->SetEventHandler(this);
  78.  
  79.     m_choice->Append("Triangle");
  80.     m_choice->Append("Square");
  81.     m_choice->SetSelection(0);
  82.  
  83.     wxLayoutConstraints *layout;
  84.  
  85.     layout = new wxLayoutConstraints();
  86.     layout->left.Absolute(0);
  87.     layout->top.Absolute(0);
  88.     layout->height.Absolute(36);
  89.     layout->width.SameAs(this, wxWidth);
  90.     m_bar->SetConstraints(layout);
  91.  
  92.     layout = new wxLayoutConstraints();
  93.     layout->left.Absolute(3);
  94.     layout->width.AsIs();
  95.     layout->height.AsIs();
  96.     layout->top.Absolute(3);
  97.     m_choice->SetConstraints(layout);
  98.  
  99.     layout = new wxLayoutConstraints();
  100.     layout->left.Absolute(0);
  101.     layout->width.SameAs(this, wxWidth);
  102.     layout->top.Below(m_bar);
  103.     layout->bottom.SameAs(this, wxBottom);
  104.     m_view->SetConstraints(layout);
  105.  
  106.     wxScrollBar *hscroll = m_dyn_sash->GetHScrollBar(this);
  107.     wxScrollBar *vscroll = m_dyn_sash->GetVScrollBar(this);
  108.  
  109.     hscroll->SetEventHandler(this);
  110.     vscroll->SetEventHandler(this);
  111.  
  112.     Connect(GetId(), wxEVT_SIZE, (wxObjectEventFunction)&SwitchView::OnSize);
  113.     Connect(m_choice->GetId(), wxEVT_COMMAND_CHOICE_SELECTED, (wxObjectEventFunction)&SwitchView::OnChoice);
  114.     Connect(m_view->GetId(), wxEVT_PAINT, (wxObjectEventFunction)&SwitchView::OnPaint);
  115.  
  116.     Connect(-1, wxEVT_SET_FOCUS, (wxObjectEventFunction)&SwitchView::OnFocus);
  117.     Connect(-1, wxEVT_SCROLL_TOP, (wxObjectEventFunction)&SwitchView::OnScroll);
  118.     Connect(-1, wxEVT_SCROLL_BOTTOM, (wxObjectEventFunction)&SwitchView::OnScroll);
  119.     Connect(-1, wxEVT_SCROLL_LINEUP, (wxObjectEventFunction)&SwitchView::OnScroll);
  120.     Connect(-1, wxEVT_SCROLL_LINEDOWN, (wxObjectEventFunction)&SwitchView::OnScroll);
  121.     Connect(-1, wxEVT_SCROLL_PAGEUP, (wxObjectEventFunction)&SwitchView::OnScroll);
  122.     Connect(-1, wxEVT_SCROLL_PAGEDOWN, (wxObjectEventFunction)&SwitchView::OnScroll);
  123.     Connect(-1, wxEVT_SCROLL_THUMBTRACK, (wxObjectEventFunction)&SwitchView::OnScroll);
  124.     Connect(-1, wxEVT_SCROLL_THUMBRELEASE, (wxObjectEventFunction)&SwitchView::OnScroll);
  125.     Connect(-1, wxEVT_ERASE_BACKGROUND, (wxObjectEventFunction)&SwitchView::OnErase);
  126.  
  127.     Connect(-1, wxEVT_DYNAMIC_SASH_SPLIT, (wxObjectEventFunction)&SwitchView::OnSplit);
  128.     Connect(-1, wxEVT_DYNAMIC_SASH_UNIFY, (wxObjectEventFunction)&SwitchView::OnUnify);
  129. }
  130.  
  131. wxSize SwitchView::DoGetBestSize() const {
  132.     return wxSize(64, 64);
  133. }
  134.  
  135. void SwitchView::OnSize(wxSizeEvent& event) {
  136.     Layout();
  137.  
  138.     wxScrollBar *hscroll = m_dyn_sash->GetHScrollBar(this);
  139.     wxScrollBar *vscroll = m_dyn_sash->GetVScrollBar(this);
  140.  
  141.     if (hscroll && vscroll) {
  142.         int hpos = hscroll->GetThumbPosition();
  143.         int vpos = vscroll->GetThumbPosition();
  144.  
  145.         wxSize size = m_view->GetSize();
  146.         if (hpos + size.GetWidth() > 300)
  147.             hpos = 300 - size.GetWidth();
  148.         if (vpos + size.GetHeight() > 300)
  149.             vpos = 300 - size.GetHeight();
  150.  
  151.         hscroll->SetScrollbar(hpos, size.GetWidth(), 300, size.GetWidth());
  152.         vscroll->SetScrollbar(vpos, size.GetHeight(), 300, size.GetHeight());
  153.     }
  154. }
  155.  
  156. void SwitchView::OnPaint(wxPaintEvent& event) {
  157.     wxPaintDC dc(m_view);
  158.  
  159.     wxScrollBar *hscroll = m_dyn_sash->GetHScrollBar(this);
  160.     wxScrollBar *vscroll = m_dyn_sash->GetVScrollBar(this);
  161.  
  162.     dc.Clear();
  163.     dc.SetDeviceOrigin(-hscroll->GetThumbPosition(), -vscroll->GetThumbPosition());
  164.  
  165.     if (m_choice->GetSelection()) {
  166.         dc.DrawLine(20, 20, 280, 20);
  167.         dc.DrawLine(280, 20, 280, 280);
  168.         dc.DrawLine(280, 280, 20, 280);
  169.         dc.DrawLine(20, 280, 20, 20);
  170.     } else {
  171.         dc.DrawLine(150, 20, 280, 280);
  172.         dc.DrawLine(280, 280, 20, 280);
  173.         dc.DrawLine(20, 280, 150, 20);
  174.     }
  175. }
  176.  
  177. void SwitchView::OnErase(wxEraseEvent& event) {
  178.     // Do nothing
  179. }
  180.  
  181. void SwitchView::OnSplit(wxDynamicSashSplitEvent& event) {
  182.     SwitchView *view = new SwitchView(m_dyn_sash);
  183.     view->m_choice->SetSelection(m_choice->GetSelection());
  184.  
  185.     wxScrollBar *hscroll = m_dyn_sash->GetHScrollBar(this);
  186.     wxScrollBar *vscroll = m_dyn_sash->GetVScrollBar(this);
  187.  
  188.     hscroll->SetEventHandler(this);
  189.     vscroll->SetEventHandler(this);
  190. }
  191.  
  192. void SwitchView::OnUnify(wxDynamicSashUnifyEvent& event) {
  193.     wxScrollBar *hscroll = m_dyn_sash->GetHScrollBar(this);
  194.     wxScrollBar *vscroll = m_dyn_sash->GetVScrollBar(this);
  195.  
  196.     hscroll->SetEventHandler(this);
  197.     vscroll->SetEventHandler(this);
  198. }
  199.  
  200. void SwitchView::OnChoice(wxCommandEvent& event) {
  201.     m_view->Refresh();
  202. }
  203.  
  204. void SwitchView::OnScroll(wxScrollEvent& event) {
  205.     m_view->Refresh();
  206. }
  207.  
  208. void SwitchView::OnFocus(wxFocusEvent& event) {
  209.     wxScrollBar *hscroll = m_dyn_sash->GetHScrollBar(this);
  210.     wxScrollBar *vscroll = m_dyn_sash->GetVScrollBar(this);
  211.  
  212.     if (event.m_eventObject == hscroll || event.m_eventObject == vscroll) {
  213.         m_view->SetFocus();
  214.     } else {
  215.         event.Skip();
  216.     }
  217. }
  218.  
  219.  
  220.  
  221. bool SwitchDemo::OnInit() {
  222.     wxFrame *frame;
  223.     wxDynamicSashWindow *dyn;
  224.  
  225.     frame = new wxFrame(NULL, -1, "Dynamic Sash Window Switch Demo");
  226.     dyn = new wxDynamicSashWindow(frame, -1, wxDefaultPosition, wxDefaultSize, wxCLIP_CHILDREN);
  227.     new SwitchView(dyn);
  228.  
  229.     frame->SetSize(480, 480);
  230.     frame->Show();
  231.  
  232.     return TRUE;
  233. }
  234.