home *** CD-ROM | disk | FTP | other *** search
/ Prima Shareware 3 / DuCom_Prima-Shareware-3_cd1.bin / PROGRAMO / C / RICHCT / FORMATBR.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-03  |  13.8 KB  |  478 lines

  1. /*  Project richctrl
  2.     DHB Software
  3.     Copyright ⌐ 1996. All Rights Reserved.
  4.  
  5.     SUBSYSTEM:    richctrl.apx Application
  6.     FILE:         formatbr.cpp
  7.     AUTHOR:       David H. Borg
  8.  
  9.  
  10.     OVERVIEW
  11.     ========
  12.     Source file for implementation of FormatBar (TWindow).
  13. */
  14.  
  15. #include <owl\owlpch.h>
  16. #pragma hdrstop
  17.  
  18. #include "formatbr.h"
  19.  
  20.  
  21. struct FontInfoRec {
  22.   TFont* Font;     // Logical font object
  23.   int    Height;   // Height of logical font in pixels
  24.   int    Width;    // Width of name of the font in pixels
  25.   char   Name[LF_FACESIZE];  // Name of this font
  26. };
  27.  
  28.  
  29. // local variables used by EnumerateFonts callback function
  30. static int         FontUsers = 0;
  31. static FontInfoRec FontInfo[MaxNumFonts];
  32. static int         NumFonts;            // Number of system fonts available
  33. static TDC*        TheDC = 0;
  34.  
  35. // EnumerateFont is a call back function.  It receives information
  36. //  about system fonts.  It creates an example of each font by calling
  37. //  CreateFont. When MaxNumFonts have been processed, 0 is returned
  38. //  notifying windows to stop sending information, otherwise 1 is
  39. //  returned telling windows to send more information if available
  40. //
  41. #if defined(__WIN32__)
  42. int __stdcall
  43. #else
  44. int far pascal _export
  45. #endif
  46. EnumerateFont(LOGFONT far* logFont, TEXTMETRIC far*, int dwType, LPARAM)
  47. {
  48.     if( dwType == TRUETYPE_FONTTYPE){
  49.         // Create the font described by logFont
  50.         //
  51.         FontInfo[NumFonts].Font = new TFont(logFont);
  52.  
  53.         // Save the height of the font for positioning when drawing in the window
  54.         //
  55.         FontInfo[NumFonts].Height = logFont->lfHeight;
  56.  
  57.         // Save the name of the font for drawing in the window
  58.         //
  59.         strcpy(FontInfo[NumFonts].Name, logFont->lfFaceName);
  60.         TheDC->SelectObject(*FontInfo[NumFonts].Font);
  61.  
  62.         TSize extent(0,0);
  63.         TheDC->GetTextExtent(logFont->lfFaceName, strlen(logFont->lfFaceName),
  64.                            extent);
  65.         FontInfo[NumFonts].Width = extent.cx;
  66.         TheDC->RestoreFont();
  67.         NumFonts++;
  68.     }
  69.  
  70.     return NumFonts >= MaxNumFonts ?
  71.         0 :      // Don't send any more information, the array is full
  72.         1;       // Send more information if available
  73. }
  74.  
  75. // Collect all of the system fonts
  76. //
  77. void
  78. GetFontInfo()
  79. {
  80.   if (FontUsers == 0) {
  81.     TheDC = new TScreenDC;
  82.     NumFonts = 0;
  83.  
  84.     // Create an instance of the call back function.  This allows
  85.     // our program to refer to an exported function.  Otherwise the
  86.     // Data segment will not be correct. This is a no-op in 32bit.
  87.     //
  88.     TProcInstance enumProc((FARPROC)EnumerateFont);
  89.  
  90.     // Gather information about all fonts that are allowable in our DC
  91.     //
  92.     EnumFonts(*TheDC, 0, (OLDFONTENUMPROC)(FARPROC)enumProc, 0);
  93.  
  94.     delete TheDC;
  95.     TheDC = 0;
  96.   }
  97.   FontUsers++;
  98. }
  99.  
  100. // Release font information
  101. //
  102. void
  103. ReleaseFontInfo()
  104. {
  105.   FontUsers--;
  106.   if (FontUsers == 0)
  107.     for (int i = 0; i < NumFonts; i++) {
  108.       delete FontInfo[i].Font;
  109.       FontInfo[i].Font = 0;
  110.     }
  111. }
  112.  
  113.  
  114. // support the following point sizes
  115. int pointSizes[] = {8, 9, 10, 11, 12, 14, 16, 18, 20, 22, 24, 26, 28, 36, 48, 72 };
  116. int nPointSizes = sizeof( pointSizes) / sizeof( pointSizes[0]);
  117.  
  118.  
  119. // A command enabler for the toolbar. This makes the toolbar automatically
  120. // enable and disable the buttons, just like the OWL TControlBar.
  121. class FormatBarButtonEnabler : public TCommandEnabler {
  122.     public:
  123.         FormatBarButtonEnabler(HWND hWndReceiver, FormatBar* fb, uint id)
  124.                 : TCommandEnabler( id, hWndReceiver) {
  125.             formatBar = fb;
  126.         }
  127.  
  128.     // override member functions of TCommandEnabler
  129.     void  Enable (bool enable);
  130.     void  SetText (const char far* text);
  131.     void  SetCheck (int state);
  132.  
  133.     protected:
  134.         FormatBar*  formatBar;
  135. };
  136.  
  137.  
  138. void FormatBarButtonEnabler::Enable(bool enable)
  139. {
  140.     TCommandEnabler::Enable(enable);
  141.     formatBar->SendMessage( TB_ENABLEBUTTON, Id, enable);
  142. }
  143.  
  144.                                               
  145. void FormatBarButtonEnabler::SetText(const char far* text)
  146. {
  147.     // implement later, do nothing for now
  148.  
  149.     formatBar->SendDlgItemMessage( Id, CB_SELECTSTRING, (WPARAM)-1, (LPARAM)text);
  150. }
  151.  
  152.  
  153. void FormatBarButtonEnabler::SetCheck(int state)
  154. {
  155.     formatBar->SendMessage( TB_CHECKBUTTON, Id, state);
  156. }
  157.  
  158.  
  159. // Initialization parameters for fbButton array. Include one line per button.
  160. static TBBUTTON fbButton[] = {
  161.     { 0, 0, 0, TBSTYLE_SEP, {0,0}, 0L, 0},
  162.     { 0, 0, 0, TBSTYLE_SEP, {0,0}, 0L, 0},
  163.     { 0, 0, 0, TBSTYLE_SEP, {0,0}, 0L, 0},
  164.     { 0, 0, 0, TBSTYLE_SEP, {0,0}, 0L, 0},
  165.     { 0, 0, 0, TBSTYLE_SEP, {0,0}, 0L, 0},
  166.     { 0, 0, 0, TBSTYLE_SEP, {0,0}, 0L, 0},
  167.     { 0, 0, 0, TBSTYLE_SEP, {0,0}, 0L, 0},
  168.     { 0, 0, 0, TBSTYLE_SEP, {0,0}, 0L, 0},
  169.     { 0, 0, 0, TBSTYLE_SEP, {0,0}, 0L, 0},
  170.     { 0, 0, 0, TBSTYLE_SEP, {0,0}, 0L, 0},
  171.     { 0, 0, 0, TBSTYLE_SEP, {0,0}, 0L, 0},
  172.     { 0, 0, 0, TBSTYLE_SEP, {0,0}, 0L, 0},
  173.     { 0, 0, 0, TBSTYLE_SEP, {0,0}, 0L, 0},
  174.     { 0, 0, 0, TBSTYLE_SEP, {0,0}, 0L, 0},
  175.     { 0, 0, 0, TBSTYLE_SEP, {0,0}, 0L, 0},
  176.     { 0, 0, 0, TBSTYLE_SEP, {0,0}, 0L, 0},
  177.     { 0, 0, 0, TBSTYLE_SEP, {0,0}, 0L, 0},
  178.     { 0, 0, 0, TBSTYLE_SEP, {0,0}, 0L, 0},
  179.     { 0, 0, 0, TBSTYLE_SEP, {0,0}, 0L, 0},
  180.     { 0, 0, 0, TBSTYLE_SEP, {0,0}, 0L, 0},
  181.     { 0, 0, 0, TBSTYLE_SEP, {0,0}, 0L, 0},
  182.     { 0, 0, 0, TBSTYLE_SEP, {0,0}, 0L, 0},
  183.     { 0, 0, 0, TBSTYLE_SEP, {0,0}, 0L, 0},
  184.     { 0, 0, 0, TBSTYLE_SEP, {0,0}, 0L, 0},
  185.     { 0, 0, 0, TBSTYLE_SEP, {0,0}, 0L, 0},
  186.     { 0, 0, 0, TBSTYLE_SEP, {0,0}, 0L, 0},
  187.     { 0, 0, 0, TBSTYLE_SEP, {0,0}, 0L, 0},
  188.     { ID_BOLD, CM_FORMATBOLD, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0,0}, 0L, 0},
  189.     { ID_ITALIC, CM_FORMATITALIC, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0,0}, 0L, 0},
  190.     { ID_UNDERLINE, CM_FORMATUNDERLINE, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0,0}, 0L, 0},
  191.     { 0, 0, TBSTATE_ENABLED, TBSTYLE_SEP, {0,0}, 0L, 0},
  192.     { ID_ALIGNLEFT, CM_FORMATLEFT, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0,0}, 0L, 0},
  193.     { ID_ALIGNCENTER, CM_FORMATCENTER, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0,0}, 0L, 0},
  194.     { ID_ALIGNRIGHT, CM_FORMATRIGHT, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0,0}, 0L, 0},
  195.     };  // end of initfbbuttontemplate
  196.  
  197. const int formatBarItems = sizeof( fbButton) / sizeof( fbButton[0]);
  198. const int nFormatItems = 6;            // there are 12 view bitmap items
  199. const int firstFormatButton = 27;    // first view button in structure
  200.  
  201.  
  202. //
  203. // Build a response table for all messages/commands handled
  204. // by the application.
  205. //
  206. DEFINE_RESPONSE_TABLE1(FormatBar, TWindow)
  207. //{{FormatBarRSP_TBL_BEGIN}}
  208.     EV_NOTIFY_AT_CHILD(BN_CLICKED, BnClicked),
  209.     EV_CBN_SELCHANGE(IDC_FONTFACE, CBNSelChangeFontFace),
  210.     EV_CBN_SELCHANGE(IDC_FONTSIZE, CBNSelChangeFontSize),
  211.     EV_CBN_CLOSEUP(IDC_FONTFACE, CBNCloseUpFontFace),
  212.     EV_CBN_CLOSEUP(IDC_FONTSIZE, CBNCloseUpFontSize),
  213.     EV_MESSAGE( WM_NOTIFY, EvCommonControlNotify),
  214.     EV_WM_PAINT,
  215. //{{FormatBarRSP_TBL_END}}
  216. END_RESPONSE_TABLE;
  217.  
  218.  
  219. //{{FormatBar Implementation}}
  220.  
  221.  
  222. // the tab control is derived from TWindow with specific Attributes
  223. FormatBar::FormatBar (TWindow* parent, int id, int w, int h, TModule* module):
  224.     TWindow(parent, 0, module)
  225. {
  226.     Attr.Id = id;
  227.     Attr.X = 0;
  228.     Attr.Y = 0;
  229.     Attr.W = w;
  230.     Attr.H = h;
  231.     Attr.ExStyle = 0;
  232.     Attr.Style = WS_CHILD | WS_VISIBLE | TBSTYLE_TOOLTIPS | CCS_NOMOVEY;
  233.  
  234.     fontType = new ToolComboBox( this, IDC_FONTFACE, 0, 3, 152, 250, CBS_SORT | CBS_HASSTRINGS | CBS_DROPDOWNLIST, 0);
  235.     fontSize = new ToolComboBox( this, IDC_FONTSIZE, 158, 3, 50, 248, CBS_HASSTRINGS | CBS_DROPDOWNLIST, 0);
  236.  
  237.     GetFontInfo();
  238. }
  239.  
  240.  
  241. FormatBar::FormatBar (TWindow* parent, const char far* title, TModule* module):
  242.     TWindow(parent, title, module)
  243. {
  244.     // INSERT>> Your constructor code here.
  245.  
  246. }
  247.  
  248.  
  249. FormatBar::~FormatBar ()
  250. {
  251.     Destroy();
  252.  
  253.     // INSERT>> Your destructor code here.
  254.  
  255.     ReleaseFontInfo();
  256. }
  257.  
  258.  
  259. char far* FormatBar::GetClassName ()
  260. {
  261.     // return the Windows 95 Toolbar common control class name
  262.     // this is what makes the window a tool bar
  263.     return TOOLBARCLASSNAME;
  264. }
  265.  
  266.  
  267. void FormatBar::EvPaint ()
  268. {
  269.     // Let Common Control paint the window, don't call TWindow::EvPaint()
  270.     DefaultProcessing();
  271. }
  272.  
  273.  
  274. void FormatBar::SetupWindow ()
  275. {
  276.     TWindow::SetupWindow();
  277.  
  278.     // INSERT>> Your code here.
  279.  
  280.     SetupFormatBar();
  281.     FillComboBoxes();
  282. }
  283.  
  284.  
  285. // Create the toolbar control and add the buttons and commands.
  286. void FormatBar::SetupFormatBar ()
  287. {
  288.     HWND hWndToolTip;    // tool tip window handle
  289.     TOOLINFO toolInfo;    // tool tip info structure
  290.  
  291.     // Create the formatbar control and add the buttons, commands, and text.
  292.     TColor colors( TColor::LtGray);
  293.     COLORMAP mapSysColor;
  294.  
  295.     // Map custom button bitmaps background color to system button face color.
  296.     mapSysColor.from = COLORREF( colors);
  297.     mapSysColor.to = ::GetSysColor( COLOR_BTNFACE);
  298.     CreateMappedBitmap( GetApplication()->GetInstance(), IDB_FORMAT, 0, &mapSysColor, nFormatItems);
  299.  
  300.     // Tell the control the size of button structure.
  301.     SendMessage( TB_BUTTONSTRUCTSIZE, (WPARAM) sizeof(TBBUTTON), 0);
  302.  
  303.     // Initialize structures that identify the bitmaps.
  304.     fbFormatBmp.hInst = GetApplication()->GetInstance();
  305.     fbFormatBmp.nID = IDB_FORMAT;
  306.  
  307.     // initialize toolbar with small bitmaps
  308.     fmtBitmap = SendMessage( TB_ADDBITMAP, (WPARAM)nFormatItems, (LPARAM)&fbFormatBmp);
  309.  
  310.     // Add the standard bitmap base number to the format offsets from the template.
  311.     // note: this is not needed for the standard bitmaps if added first.
  312.     for( int j = firstFormatButton; j < formatBarItems; j++){
  313.         fbButton[j].iBitmap += fmtBitmap;
  314.     }
  315.  
  316.     // Send the initialized buttons structure to the common controls dll.
  317.     SendMessage( TB_ADDBUTTONS, (WPARAM)formatBarItems, (LPARAM)(LPTBBUTTON)fbButton);
  318.  
  319.     // get tooltip window handle
  320.     hWndToolTip = (HWND)SendMessage( TB_GETTOOLTIPS, 0, 0);
  321.  
  322.     if (hWndToolTip)
  323.     {
  324.         // initialize the toolinfo structure
  325.         toolInfo.cbSize = sizeof(toolInfo);
  326.         toolInfo.uFlags = TTF_IDISHWND | TTF_CENTERTIP;
  327.         toolInfo.lpszText = (LPSTR)IDC_FONTFACE;
  328.         toolInfo.hwnd = fontType->HWindow;
  329.         toolInfo.uId = (UINT)fontType->HWindow;
  330.         toolInfo.hinst = GetApplication()->GetInstance();
  331.  
  332.         // add tooltips for the font type combo box
  333.         ::SendMessage( hWndToolTip, TTM_ADDTOOL, 0, (LPARAM)&toolInfo);
  334.  
  335.         toolInfo.lpszText = (LPSTR)IDC_FONTSIZE;
  336.         toolInfo.hwnd = fontSize->HWindow;
  337.         toolInfo.uId = (UINT)fontSize->HWindow;
  338.  
  339.         // add tooltips for the font size combo box
  340.         ::SendMessage( hWndToolTip, TTM_ADDTOOL, 0, (LPARAM)&toolInfo);
  341.     }
  342.  
  343.     // Set the fonts for the combo boxes the same as the toolbar.
  344.     fontType->SetWindowFont( GetWindowFont(), false);
  345.     fontSize->SetWindowFont( GetWindowFont(), false);
  346.  
  347.     // Automatically size of the toolbar to the button size.
  348.     SendMessage( TB_AUTOSIZE, 0, 0);
  349. }
  350.  
  351.  
  352. void FormatBar::FillComboBoxes ()
  353. {
  354.     char buf[255];
  355.  
  356.     for (int i = 0; i < NumFonts; i++) {
  357.         fontType->AddString( FontInfo[i].Name);
  358.     }
  359.     fontType->SetSelString( "Times", -1);
  360.  
  361.  
  362.     // Fill the point size combo box with the list of possible point sizes
  363.     for( i = 0; i < nPointSizes; i++)
  364.     {
  365.         wsprintf( buf, "%d", pointSizes[i]);
  366.         fontSize->AddString( buf);
  367.     }
  368.     fontSize->SetSelString( "12", -1);
  369. }
  370.  
  371.  
  372. // This function handles the messages sent by Windows 95 common controls.
  373. LRESULT FormatBar::EvCommonControlNotify( WPARAM /*wParam*/, LPARAM lParam)
  374. {
  375.     LRESULT retVal = false;
  376.     NMHDR FAR *pNmhdr = (NMHDR FAR *)lParam;
  377.     LPTBNOTIFY lpTbNotify = (LPTBNOTIFY)lParam;
  378.  
  379.     switch( pNmhdr->code){
  380.         case TTN_NEEDTEXT:         // process tool tip control message
  381.             {
  382.                 LPTOOLTIPTEXT lpText;
  383.  
  384.                 lpText = (LPTOOLTIPTEXT)lParam;
  385.                 lpText->hinst = GetModule()->GetInstance();
  386.                 lpText->lpszText = MAKEINTRESOURCE(lpText->hdr.idFrom);
  387.             }
  388.             break;
  389.         case TBN_GETBUTTONINFO:    // Customize Toolbar dialog nees this info
  390.             if (lpTbNotify->iItem < formatBarItems) {
  391.                 lpTbNotify->tbButton = fbButton[lpTbNotify->iItem];
  392.                 retVal = true;
  393.             }
  394.             break;
  395.         case TBN_QUERYINSERT:    // Can Customize Dialog insert?
  396.             retVal = true;
  397.             break;
  398.         case TBN_QUERYDELETE:    // Can Customize Dialog delete?
  399.             retVal = true;
  400.             break;
  401.         default:
  402.             break;
  403.     }
  404.     return retVal;
  405. }
  406.  
  407.  
  408. // Pressing a toolbar button causes a BN_CLICKED message to be sent.
  409. // Re-transmit the BN_CLICKED Id as CM_... message to simulate menu selections.
  410. void FormatBar::BnClicked()
  411. {
  412.     TCurrentEvent& currentEvent = GetCurrentEvent();
  413.  
  414.     // Post button Id to simulate a menu command.
  415.     Parent->PostMessage( WM_COMMAND, LOWORD(currentEvent.WParam));
  416. }
  417.  
  418.  
  419. // Selecting a toolbar combobox item causes a LBN_SELCHANGE message to be sent.
  420. // Send a windows message corresponding to the action to be taken.
  421. void FormatBar::CBNSelChangeFontFace()
  422. {
  423.     static char buf[255];
  424.  
  425.     fontType->GetSelString( buf, sizeof(buf));
  426.     fontType->InformFocus( WM_CHANGEFONTFACE, (WPARAM)0, (LPARAM)buf);
  427. }
  428.  
  429.  
  430. // Selecting a toolbar combobox item causes a LBN_SELCHANGE message to be sent.
  431. // Send a windows message corresponding to the action to be taken.
  432. void FormatBar::CBNSelChangeFontSize()
  433. {
  434.     char buf[255];
  435.     int size;
  436.  
  437.     fontSize->GetSelString( buf, sizeof(buf));
  438.     size = atoi(buf);
  439.     fontSize->InformFocus( WM_CHANGEFONTSIZE, (WPARAM)size, (LPARAM)0);
  440. }
  441.  
  442.  
  443. // Done selecting combobox, reset focus window
  444. void FormatBar::CBNCloseUpFontFace()
  445. {
  446.     fontType->RetWndFocus();
  447. }
  448.  
  449.  
  450. // Done selecting combobox, reset focus window
  451. void FormatBar::CBNCloseUpFontSize()
  452. {
  453.     fontSize->RetWndFocus();
  454. }
  455.  
  456.  
  457. // IdleAction takes care of button enabling through FormatBarButtonEnabler.
  458. bool FormatBar::IdleAction (long idleCount)
  459. {
  460.     if (idleCount == 0) {
  461.         for( int i=0; i < formatBarItems; i++){
  462.             if( fbButton[i].fsStyle != TBSTYLE_BUTTON)
  463.                 continue;
  464.             // Must use SendMessage here since a ptr to a temp is passed
  465.             Parent->SendMessage( WM_COMMAND_ENABLE, 0,
  466.                 (LPARAM)&FormatBarButtonEnabler( Parent->HWindow, this,
  467.                 fbButton[i].idCommand));
  468.         }
  469.         // send enable messages for the custom comboboxes
  470.         Parent->SendMessage( WM_COMMAND_ENABLE, 0,
  471.             (LPARAM)&FormatBarButtonEnabler( Parent->HWindow, this,    IDC_FONTFACE));
  472.         Parent->SendMessage( WM_COMMAND_ENABLE, 0,
  473.             (LPARAM)&FormatBarButtonEnabler( Parent->HWindow, this, IDC_FONTSIZE));
  474.     }
  475.     return TWindow::IdleAction(idleCount);
  476. }
  477.  
  478.