home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / PROPSHT.PAK / PROPSHTX.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  6.9 KB  |  300 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // Copyright (c) 1995, 1995 by Borland International, All Rights Reserved
  4. //
  5. // Illustrates usage of PropertySheets and PropertyPages
  6. //----------------------------------------------------------------------------
  7. #include <owl/pch.h>
  8. #if !defined(OWL_PROPSHT_H)
  9. # include <owl/propsht.h>
  10. #endif
  11. #if !defined(OWL_COMMCTRL_H)
  12. # include <owl/commctrl.h>
  13. #endif
  14. #if !defined(OWL_CHOOSECO_H)
  15. # include <owl/chooseco.h>
  16. #endif
  17. #include <stdio.h>
  18. #include "propsht.h"
  19.  
  20. //
  21. // class TWinAttrDlg
  22. // ~~~~~ ~~~~~~~~~~~
  23. class TWinAttrDlg : public TPropertyPage {
  24.   public:
  25.     TWinAttrDlg(TPropertySheet* parent);
  26.  
  27.     // Overriden virtuals
  28.     //
  29.     void              SetupWindow();
  30.     int               Apply(TNotify&);
  31.  
  32.     // Event handlers
  33.     //
  34.     void              BkColorClick();
  35.     void              EnChange();
  36.  
  37.     TColor            BkColor;
  38.     TAPointer<char>   Caption;
  39.  
  40.   DECLARE_RESPONSE_TABLE(TWinAttrDlg);
  41. };
  42.  
  43. TWinAttrDlg::TWinAttrDlg(TPropertySheet* parent)
  44.             :TPropertyPage(parent, IDD_WINATTR, "WinAttrDlg", IDI_COLOR)
  45. {
  46. }
  47.  
  48. DEFINE_RESPONSE_TABLE1(TWinAttrDlg, TPropertyPage)
  49.     EV_BN_CLICKED(IDC_BKCOLOR, BkColorClick),
  50.     EV_EN_CHANGE(IDC_TEXTEDIT, EnChange),
  51. END_RESPONSE_TABLE;
  52.  
  53. void
  54. TWinAttrDlg::SetupWindow()
  55. {
  56.   TPropertyPage::SetupWindow();
  57.  
  58.   // Grab client window's caption
  59.   //
  60.   Caption = new char[_MAX_PATH];
  61.   GetApplication()->GetMainWindow()->GetWindowText(Caption, _MAX_PATH);
  62.   SetDlgItemText(IDC_TEXTEDIT, Caption);
  63.  
  64.   // Client's color is the default window color [??]
  65.   //
  66.   BkColor = TColor::SysWindow;
  67. }
  68.  
  69. void
  70. TWinAttrDlg::BkColorClick()
  71. {
  72.   TChooseColorDialog::TData colorData;
  73.   TAPointer<TColor> custColors = new TColor[16];
  74.   colorData.Flags = CC_RGBINIT;
  75.   colorData.Color = BkColor;
  76.   colorData.CustColors = custColors;
  77.   if (TChooseColorDialog(this, colorData).Execute() == IDOK) {
  78.     if (colorData.Color != BkColor) {
  79.       BkColor = colorData.Color;
  80.       GetSheet()->PageChanged(*this);
  81.     }
  82.   }
  83. }
  84.  
  85. void
  86. TWinAttrDlg::EnChange()
  87. {
  88.   char text[_MAX_PATH];
  89.   text[0] = 0;
  90.   GetDlgItemText(IDC_TEXTEDIT, text, sizeof(text));
  91.   if (strcmp(text, Caption))
  92.     GetSheet()->PageChanged(*this);
  93. }
  94.  
  95. int               
  96. TWinAttrDlg::Apply(TNotify& not)
  97. {
  98.   // Set color
  99.   //
  100.   GetApplication()->GetMainWindow()->GetClientWindow()->SetBkgndColor(BkColor);
  101.   GetApplication()->GetMainWindow()->GetClientWindow()->Invalidate();
  102.  
  103.   // And caption
  104.   //
  105.   char text[_MAX_PATH];
  106.   text[0] = 0;
  107.   GetDlgItemText(IDC_TEXTEDIT, text, sizeof(text));
  108.   if (strcmp(text, Caption)) {
  109.     GetApplication()->GetMainWindow()->SetWindowText(text);
  110.     Caption = strnewdup(text);
  111.   }
  112.  
  113.   // These changes are not 'Cancellable'
  114.   //
  115.   GetSheet()->CancelToClose();
  116.  
  117.   return TPropertyPage::Apply(not);
  118. }
  119.  
  120. //----------------------------------------------------------------------------
  121.  
  122. //
  123. // class TTestPage
  124. // ~~~~~ ~~~~~~~~~
  125. class TTestPage : public TPropertyPage {
  126.   public:
  127.     TTestPage(TPropertySheet* parent);
  128.  
  129.     // Event handlers
  130.     //
  131.     void EvCommandAndId(uint id);
  132.  
  133.     // Override virtuals of TPropertyPage
  134.     //
  135.     int   SetActive(TNotify&);    // PSN_SETACTIVE
  136.     bool  KillActive(TNotify&);   // PSN_KILLACTIVE
  137.     int   Apply(TNotify&);        // PSN_APPLY
  138.     void  Reset(TNotify&);        // PSN_RESET
  139.     void  Help(TNotify&);         // PSN_HELP
  140.     bool  QueryCancel(TNotify&);  // PSN_QUERYCANCEL
  141.  
  142.     DECLARE_RESPONSE_TABLE(TTestPage);
  143. };
  144.  
  145. TTestPage::TTestPage(TPropertySheet* parent)
  146.           :TPropertyPage(parent, IDD_SAMPLEPAGE, "TestPage")
  147. {
  148. }
  149.  
  150. DEFINE_RESPONSE_TABLE1(TTestPage, TPropertyPage)
  151.  
  152.   // Handlers of control specific to this page/dialog
  153.   //
  154.   EV_COMMAND_AND_ID(IDC_SIM_APPLY,        EvCommandAndId),
  155.   EV_COMMAND_AND_ID(IDC_SIM_XCELTOCLOSE,  EvCommandAndId),
  156.   EV_COMMAND_AND_ID(IDC_SIM_CHANGED,      EvCommandAndId),
  157.   EV_COMMAND_AND_ID(IDC_SIM_UNCHANGED,    EvCommandAndId),
  158.  
  159.   EV_COMMAND_AND_ID(IDC_ADDPAGE,          EvCommandAndId),
  160.   EV_COMMAND_AND_ID(IDC_REMPAGE,          EvCommandAndId),
  161.   EV_COMMAND_AND_ID(IDC_SELPAGE,          EvCommandAndId),
  162.   EV_COMMAND_AND_ID(IDC_PAGEINFO,         EvCommandAndId),
  163. END_RESPONSE_TABLE;
  164.  
  165. void
  166. TTestPage::EvCommandAndId(uint id)
  167. {
  168.   switch(id) {
  169.     case IDC_SIM_APPLY:
  170.          GetSheet()->Apply();
  171.          break;
  172.  
  173.     case IDC_SIM_XCELTOCLOSE:
  174.          GetSheet()->CancelToClose();
  175.          break;
  176.  
  177.     case IDC_SIM_CHANGED:
  178.          GetSheet()->PageChanged(*this);
  179.          break;
  180.  
  181.     case IDC_SIM_UNCHANGED:
  182.          GetSheet()->PageUnchanged(*this);
  183.          break;
  184.   }
  185. }
  186.  
  187. int   
  188. TTestPage::SetActive(TNotify& not)    
  189. {
  190.   return TPropertyPage::SetActive(not);
  191. }
  192.  
  193. bool  
  194. TTestPage::KillActive(TNotify& not)
  195. {
  196.   return TPropertyPage::KillActive(not);
  197. }
  198.  
  199. int   
  200. TTestPage::Apply(TNotify& not)
  201. {
  202.   return TPropertyPage::Apply(not);
  203. }
  204.  
  205. void  
  206. TTestPage::Reset(TNotify& not)
  207. {
  208.   TPropertyPage::Reset(not);
  209. }
  210.  
  211. void  
  212. TTestPage::Help(TNotify& not)
  213. {
  214.   TPropertyPage::Help(not);
  215.   MessageBox("Invoke WinHelp() here", GetModule()->GetName(), MB_OK);
  216. }
  217.  
  218. bool  
  219. TTestPage::QueryCancel(TNotify& not)
  220. {
  221.   return TPropertyPage::QueryCancel(not);
  222. }
  223.  
  224. //----------------------------------------------------------------------------
  225.  
  226. //
  227. // class TClientWindow
  228. // ~~~~~ ~~~~~~~~~~~~~
  229. class TClientWindow : public TWindow {
  230.  
  231.   public:
  232.     TClientWindow(TWindow* parent= 0);
  233.  
  234.   protected:
  235.     // Message Handlers
  236.     //
  237.     void        Properties(bool modal);
  238.     void        PropertiesModal()    { Properties(true);  }
  239.     void        PropertiesModeless() { Properties(false); }
  240.  
  241.   DECLARE_RESPONSE_TABLE(TClientWindow);
  242. };
  243.  
  244. DEFINE_RESPONSE_TABLE1(TClientWindow, TWindow)
  245.   EV_COMMAND(CM_PROPERTY, PropertiesModal),
  246.   EV_COMMAND(CM_PROPMODELESS, PropertiesModeless),
  247. END_RESPONSE_TABLE;
  248.  
  249.  
  250. TClientWindow::TClientWindow(TWindow* parent)
  251. :
  252.   TWindow(parent)
  253. {
  254.   Attr.Style |= (WS_CLIPSIBLINGS|WS_CLIPCHILDREN);
  255. }
  256.  
  257. void
  258. TClientWindow::Properties(bool modal)
  259. {
  260.   // Allocate a PropertySheet object
  261.   //
  262.   TPropertySheet* ps = new TPropertySheet(this, "Options", 0, false, 
  263.                                 PSH_HASHELP);
  264.  
  265.   // Allocate PropertyPage objects using the sheet as 'parent' parameters
  266.   //
  267.   new TWinAttrDlg(ps);
  268.   (new TTestPage(ps))->SetFlags(PSP_HASHELP);
  269.   
  270.   if (modal)
  271.     ps->Execute();
  272.   else
  273.     ps->Create();
  274. }
  275.  
  276. //----------------------------------------------------------------------------
  277.  
  278. //
  279. // class TSampleApp
  280. // ~~~~~ ~~~~~~~~~~
  281. class TSampleApp : public TApplication {
  282.   public:
  283.     void    InitMainWindow();
  284. };
  285.  
  286. void
  287. TSampleApp::InitMainWindow()
  288. {
  289.   EnableCtl3d();
  290.   SetName("Property Sheet");
  291.   SetMainWindow(new TFrameWindow(0, GetName(), new TClientWindow()));
  292.   GetMainWindow()->AssignMenu(IDM_APPMENU);
  293. }
  294.  
  295. int
  296. OwlMain(int /*argc*/, char*/*argv*/[])
  297. {
  298.   return TSampleApp().Run();
  299. }
  300.