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

  1. /*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  2.  
  3.   propview.cpp
  4.   Created: 10/24/95
  5.   Copyright (c) 1995, Borland International
  6.   $Revision:   1.15  $
  7.  
  8. :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
  9. #ifndef __AOEXPCH_H
  10.   #include "aoexpch.h"
  11. #endif
  12. #pragma hdrstop
  13.  
  14. #include "propview.h"
  15. #include <windowsx.h>
  16.  
  17. extern HINSTANCE ghInst;
  18.  
  19. #define TV_FOREGROUND "Foreground"
  20. #define TV_BACKGROUND "Background"
  21.  
  22. static IViewType* g_viewTypeProperty;
  23. /******************************************************************************
  24. *
  25. *
  26. * PropertyView
  27. *
  28. *  This example demonstrates implementation of font and color property for
  29. *  view.
  30. *
  31. ******************************************************************************/
  32.  
  33. class PropertyView {
  34. public:
  35.   PropertyView(HWND hwnd);
  36.   ~PropertyView();
  37.  
  38.   void            Paint();
  39.   void            SetFont(const char* fontName
  40.                           , INT fontSize
  41.                           , LONG lfWeight
  42.                           , BYTE lfItalic
  43.                           , BYTE lfUnderline);
  44.  
  45.   void            SetFClr(COLORREF fClr) { d_fClr = fClr; };
  46.   void            SetBClr(COLORREF bClr) { d_bClr = bClr; };
  47.  
  48.   COLORREF        d_fClr; 
  49.   COLORREF        d_bClr; 
  50.   HFONT          d_hFont;
  51.  
  52. protected:
  53.  
  54.   HWND            d_hwnd;
  55.  
  56. };
  57.  
  58. HWND CreateTextWindow(HINSTANCE hInstance, HWND hwndParent);
  59. #define GetPropertyViewFromHWND(hWnd) ((PropertyView*)GetWindowLong(hWnd, GWL_USERDATA))
  60.  
  61.  
  62. /******************************************************************************
  63. *
  64. *
  65. * PropertyViewClient
  66. *
  67. *
  68. ******************************************************************************/
  69.  
  70. class PropertyViewClient : public IViewClient {
  71. public:
  72.   PropertyViewClient();
  73.   ~PropertyViewClient();
  74.  
  75.   //IUnknown members
  76.   STDMETHODIMP QueryInterface (THIS_ REFIID, LPVOID FAR *);
  77.   STDMETHODIMP_(ULONG) AddRef (THIS) { return m_RefCount++; }
  78.   STDMETHODIMP_(ULONG) Release (THIS)
  79.       {return --m_RefCount == 0 ? (delete this, 0) : m_RefCount; }
  80.  
  81.   virtual HWND  GetHwnd();
  82.   virtual long  GetRestoreDataLen();
  83.   virtual void* GetRestoreData();
  84.   virtual BOOL  CanClose();
  85.  
  86.   virtual unsigned CommandSupported( IPolyString* );
  87.   virtual void ExecuteCommand( IPolyString* );
  88.   virtual HWND Create(IViewParentWnd* wndServer, void * restoreData);
  89.   virtual void PropertyChangeNotify();
  90.  
  91.   virtual void SetFont( IPolyString * propName, 
  92.                         IPolyString * fontName, 
  93.                         int fontSize,
  94.                         FontAttribute attrib );
  95.   virtual void SetColor(IPolyString * propName,
  96.                         DWORD fore, 
  97.                         DWORD back );
  98.  
  99.           void RefreshProperty();
  100. protected:
  101.  
  102.   HWND            d_hwnd;
  103.   IViewParentWnd* d_viewParent;
  104.  
  105. protected:
  106.   // private variables
  107.   ULONG    m_RefCount;
  108. };
  109.  
  110. PropertyViewClient::PropertyViewClient() {
  111.   m_RefCount = 1;
  112.   d_hwnd = 0;
  113.   d_viewParent = 0;
  114. };
  115.  
  116. PropertyViewClient::~PropertyViewClient() {
  117.   if (d_viewParent) {
  118.     d_viewParent->Release();
  119.   }
  120.   if ( IsWindow(d_hwnd) )
  121.     DestroyWindow(d_hwnd);
  122. };
  123.  
  124.  
  125. STDMETHODIMP
  126. PropertyViewClient::QueryInterface (REFIID riid, LPVOID FAR* ppobj)
  127. {
  128.   if ((riid == IID_IUnknown)/* || (riid == IID_PUBLIC_)*/)
  129.   {
  130.     *ppobj = (LPVOID)this;
  131.     AddRef();
  132.     return NOERROR;
  133.   }
  134.  
  135.   *ppobj = NULL;
  136.   return ResultFromScode(E_NOINTERFACE);
  137. }
  138.  
  139. HWND PropertyViewClient::Create(IViewParentWnd* wndServer, void * /*restoreData*/) {
  140.  
  141.   d_viewParent = wndServer;
  142.  
  143.   d_hwnd = CreateTextWindow(ghInst, wndServer->GetHwnd());
  144.  
  145.   //
  146.   // Addon view have to refresh here, since the properties the addon view
  147.   //  register maybe overwritten by what is saved in desktop file
  148.   //
  149.   RefreshProperty();
  150.  
  151.   return d_hwnd;
  152.  
  153. };
  154.  
  155. void PropertyViewClient::RefreshProperty() {
  156.   IPolyString* propName = MakePolyString(TV_FOREGROUND);
  157.   propName->AddRef(); // do an extra addref so it will stick around
  158.  
  159.   propName->AddRef();
  160.   IPolyString* fontName = g_viewTypeProperty->GetFontName(propName);
  161.  
  162.   propName->AddRef();
  163.   int fontSize = g_viewTypeProperty->GetFontSize(propName);
  164.  
  165.   propName->AddRef();
  166.   int attribute = g_viewTypeProperty->GetFontAttribute(propName);
  167.  
  168.   propName->AddRef();
  169.   DWORD fore = g_viewTypeProperty->GetForegroundColor(propName);
  170.  
  171.   propName->AddRef();
  172.   DWORD back = g_viewTypeProperty->GetBackgroundColor(propName);
  173.  
  174.   PropertyView* tv = GetPropertyViewFromHWND(d_hwnd);
  175.  
  176.   tv->SetFont(fontName->GetCstr()
  177.                       , fontSize
  178.                       , (int)attribute & (int)FA_Bold ? FW_BOLD : FW_NORMAL
  179.                       , (BYTE)((int)attribute & (int)FA_Italic ? 1 : 0)
  180.                       , (BYTE)((int)attribute & (int)FA_Underline ? 1 : 0));
  181.  
  182.   propName->Release();
  183.  
  184.   tv->SetFClr(fore);
  185.   tv->SetBClr(back);
  186.  
  187.   InvalidateRect(d_hwnd, NULL, TRUE);
  188.  
  189.   fontName->Release();
  190.  
  191.   propName->Release(); // release the extra addref
  192. };
  193.  
  194.  
  195. HWND PropertyViewClient::GetHwnd()
  196. {
  197.   return d_hwnd;
  198. };
  199.  
  200. long  PropertyViewClient::GetRestoreDataLen()
  201. {
  202.   return 0; 
  203. };
  204.  
  205. //
  206. // Save the data user entered to the destop file
  207. //
  208. void* PropertyViewClient::GetRestoreData()
  209. {
  210.   return NULL;
  211. };
  212.  
  213. BOOL  PropertyViewClient::CanClose()
  214. {
  215.   return 1;
  216. };
  217.  
  218. unsigned PropertyViewClient::CommandSupported(IPolyString* cmdStr)
  219. {
  220.   cmdStr->Release();
  221.   return CMD_UNKNOWN;
  222. }
  223.  
  224.  
  225. void PropertyViewClient::ExecuteCommand(IPolyString* cmdStr)
  226. {
  227.   cmdStr->Release();
  228. }
  229.  
  230. void PropertyViewClient::PropertyChangeNotify() {
  231.   RefreshProperty();
  232. };
  233.  
  234. //
  235. // SetFont() and SetColor() will be obsolete
  236. //
  237. void PropertyViewClient::SetFont( IPolyString * propName, 
  238.                                 IPolyString * fontName, 
  239.                                 int fontSize,
  240.                                 FontAttribute attribute) {
  241.  
  242.   if (stricmp(TV_FOREGROUND, (char*)propName->GetCstr()) == 0) {
  243.  
  244.       PropertyView* tv = GetPropertyViewFromHWND(d_hwnd);
  245.       tv->SetFont(fontName->GetCstr()
  246.                           , fontSize
  247.                           , (int)attribute & (int)FA_Bold ? FW_BOLD : FW_NORMAL
  248.                           , (BYTE)((int)attribute & (int)FA_Italic ? 1 : 0)
  249.                           , (BYTE)((int)attribute & (int)FA_Underline ? 1 : 0));
  250.   } 
  251.   propName->Release();
  252.   fontName->Release();
  253.   InvalidateRect(d_hwnd, NULL, TRUE);
  254. };
  255.  
  256. void PropertyViewClient::SetColor(IPolyString * propName,
  257.                                   DWORD fore, 
  258.                                   DWORD back ) {
  259.   PropertyView* tv = GetPropertyViewFromHWND(d_hwnd);
  260.   if (stricmp(TV_FOREGROUND, (char*)propName->GetCstr()) == 0) {
  261.     tv->SetFClr(fore);
  262.     tv->SetBClr(back);
  263.   }
  264.   propName->Release();
  265.   InvalidateRect(d_hwnd, NULL, TRUE);
  266. };
  267.  
  268. /******************************************************************************
  269. *
  270. *
  271. * PropertyViewFactory
  272. *
  273. *
  274. ******************************************************************************/
  275.  
  276. PropertyViewFactory::PropertyViewFactory(IViewType* viewType) {
  277.   m_RefCount = 1;
  278.  
  279.   //
  280.   // View type is "AddOnPropertyView" and this view
  281.   //  will stay in the editor region
  282.   //
  283.   viewType->Init(this
  284.                 , MakePolyString("AddOnPropertyView")
  285.                 , 0, 0, 813, 358);
  286.   viewType->Release();
  287.  
  288.   //
  289.   //  This is wrong, I know.
  290.   //
  291.   g_viewTypeProperty = viewType;
  292. };
  293.  
  294. STDMETHODIMP
  295. PropertyViewFactory::QueryInterface (REFIID riid, LPVOID FAR* ppobj)
  296. {
  297.   if ((riid == IID_IUnknown)/* || (riid == IID_PUBLIC_)*/)
  298.   {
  299.     *ppobj = (LPVOID)this;
  300.     AddRef();
  301.     return NOERROR;
  302.   }
  303.  
  304.   *ppobj = NULL;
  305.   return ResultFromScode(E_NOINTERFACE);
  306. }
  307.  
  308. IViewClient* PropertyViewFactory::CreateView(IViewParentWnd* wndServer, void * restoreData) {
  309.   PropertyViewClient* viewClient = new PropertyViewClient;
  310.   viewClient->Create(wndServer, restoreData);
  311.   return viewClient;
  312. };
  313.  
  314. void PropertyViewFactory::InitializeProperty(IViewType* viewType) {
  315.  
  316.   IPolyString * propName;
  317.   IPolyString * fontName;
  318.   propName = MakePolyString(TV_FOREGROUND);
  319.   fontName = MakePolyString("Courier");
  320.   fontName->AddRef(); // addref for TV_BACKGROUND
  321.   viewType->InitFontAndColorProperty(propName, 
  322.                                     fontName,
  323.                                     10,
  324.                                     FA_Underline,
  325.                                     GetSysColor(COLOR_WINDOWTEXT),
  326.                                     GetSysColor(COLOR_WINDOW));
  327.  
  328.   propName = MakePolyString(TV_BACKGROUND);
  329.  
  330.   viewType->InitFontAndColorProperty(propName, 
  331.                                     fontName,
  332.                                     10,
  333.                                     FA_Underline,
  334.                                     GetSysColor(COLOR_WINDOWTEXT),
  335.                                     GetSysColor(COLOR_WINDOW));
  336.   viewType->Release();
  337. }
  338.  
  339. //.............................................................................
  340.  
  341. PropertyView::PropertyView(HWND hwnd) {
  342.   d_hwnd = hwnd;
  343.  
  344.   d_hFont = 0;
  345.  
  346.   d_fClr = GetSysColor(COLOR_WINDOWTEXT);
  347.   d_bClr = GetSysColor(COLOR_WINDOW);
  348. };
  349.  
  350. PropertyView::~PropertyView() {
  351.   if ( d_hFont )
  352.     DeleteObject(d_hFont);
  353. }
  354.  
  355. void PropertyView::Paint() {
  356.  
  357.   PAINTSTRUCT ps;
  358.   HDC hdc;
  359.  
  360.   hdc = BeginPaint (d_hwnd, &ps) ;
  361.   /* Set text to white and background to green */
  362.  
  363.   SetTextColor(hdc, d_fClr);
  364.   SetBkColor(hdc, d_bClr);
  365.  
  366.   HFONT hFontOld = (HFONT)SelectObject(hdc, d_hFont);
  367.  
  368.   static char t[] = "Font and Color Property";
  369.  
  370.   RECT rect;
  371.   GetClientRect(d_hwnd, &rect);
  372.   DrawText(hdc, t, -1, &rect, DT_CENTER|DT_VCENTER);
  373.  
  374.   SelectObject(hdc, hFontOld);
  375.  
  376.   EndPaint (d_hwnd, &ps) ;
  377. }
  378.  
  379. void PropertyView::SetFont(const char* fontName
  380.                           , INT fontSize
  381.                           , LONG lfWeight
  382.                           , BYTE lfItalic
  383.                           , BYTE lfUnderline) {
  384.  
  385.   LOGFONT        lf;
  386.   memset(&lf, 0, sizeof( lf ) );
  387.  
  388.  
  389.   strcpy(lf.lfFaceName, fontName);
  390.  
  391.   HDC hdc = GetDC(d_hwnd);
  392.   lf.lfHeight = -MulDiv(fontSize, GetDeviceCaps(hdc, LOGPIXELSY), 72);
  393.   ReleaseDC(d_hwnd, hdc);
  394.  
  395.   lf.lfWeight = lfWeight;
  396.  
  397.   lf.lfItalic = lfItalic;
  398.   lf.lfUnderline = lfUnderline;
  399.   lf.lfStrikeOut = 0;
  400.   lf.lfPitchAndFamily = DEFAULT_PITCH;
  401.  
  402.  
  403.   HFONT hFont = CreateFontIndirect(&lf);
  404.   if (hFont && d_hFont)
  405.     DeleteObject(d_hFont);
  406.  
  407.   d_hFont = hFont;
  408. }
  409.  
  410. LONG APIENTRY PropertyViewWndProc(
  411.   HWND hWnd,                /* window handle                   */
  412.   UINT message,            /* type of message                */
  413.   UINT wParam,              /* additional information         */
  414.   LONG lParam)              /* additional information         */
  415. {
  416.   PropertyView* pv = GetPropertyViewFromHWND(hWnd);
  417.   switch (message)
  418.   {
  419.  
  420.     case WM_CREATE:
  421.     {
  422.       // Create the tree view window and initialize its
  423.       // image list
  424.       pv = new PropertyView(hWnd);
  425.       SetWindowLong(hWnd, GWL_USERDATA, (long)pv);
  426.     }
  427.       break;          
  428.     case WM_PAINT:
  429.     {
  430.       pv->Paint();
  431.       return 0;
  432.     }
  433.     case WM_DESTROY:
  434.     {
  435.       delete pv;
  436.       SetWindowLong(hWnd, GWL_USERDATA, 0L);
  437.     }
  438.     default:
  439.       return (DefWindowProc(hWnd, message, wParam, lParam));
  440.   }
  441.   return (0);
  442. }
  443.  
  444. BOOL RegisterPropertyViewClass(HINSTANCE hInstance)      /* current instance             */
  445. {
  446.   WNDCLASS  wcPropertyView;
  447.  
  448.  
  449.   if ( GetClassInfo(hInstance,              // handle of application instance
  450.                     TEXT("PropertyViewWClass"), // address of class name string
  451.                     &wcPropertyView)            // address of structure for class data
  452.       ) {
  453.     return TRUE;
  454.   }
  455.  
  456.   /* Fill in window class structure with parameters that describe the      */
  457.   /* main window.                                                          */
  458.  
  459.   wcPropertyView.style = 0;                    
  460.   wcPropertyView.lpfnWndProc = (WNDPROC)PropertyViewWndProc; 
  461.   wcPropertyView.cbClsExtra = 0;              
  462.   wcPropertyView.cbWndExtra = 0;              
  463.   wcPropertyView.hInstance = hInstance;
  464.   wcPropertyView.hIcon = NULL;
  465.   wcPropertyView.hCursor = LoadCursor(NULL, IDC_ARROW);
  466.   wcPropertyView.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
  467.   wcPropertyView.lpszMenuName = NULL; 
  468.   wcPropertyView.lpszClassName = TEXT("PropertyViewWClass");
  469.  
  470.   return (RegisterClass(&wcPropertyView));
  471. }
  472.  
  473. /****************************************************************************
  474. *
  475. *   FUNCTION: CreateTextWindow(HINSTANCE hInstance, HWND hwndParent)
  476. *
  477. *   PURPOSE:  Creates a PropertyView window
  478. *
  479. ****************************************************************************/
  480.  
  481. HWND CreateTextWindow(HINSTANCE hInstance, HWND hwndParent) {
  482.  
  483.   RegisterPropertyViewClass(hInstance);
  484.   return CreateWindowEx(
  485.                   WS_EX_CLIENTEDGE,
  486.                   TEXT("PropertyViewWClass"),
  487.                   TEXT(""),
  488.                   WS_CHILD|WS_VISIBLE,
  489.                   CW_USEDEFAULT,
  490.                   CW_USEDEFAULT,
  491.                   CW_USEDEFAULT,
  492.                   CW_USEDEFAULT,
  493.                   hwndParent,
  494.                   NULL,
  495.                   hInstance,
  496.                   NULL);
  497. };
  498.  
  499. //.............................................................................
  500.