home *** CD-ROM | disk | FTP | other *** search
- /* Project richctrl
- DHB Software
- Copyright ⌐ 1996. All Rights Reserved.
-
- SUBSYSTEM: richctrl.apx Application
- FILE: formatbr.cpp
- AUTHOR: David H. Borg
-
-
- OVERVIEW
- ========
- Source file for implementation of FormatBar (TWindow).
- */
-
- #include <owl\owlpch.h>
- #pragma hdrstop
-
- #include "formatbr.h"
-
-
- struct FontInfoRec {
- TFont* Font; // Logical font object
- int Height; // Height of logical font in pixels
- int Width; // Width of name of the font in pixels
- char Name[LF_FACESIZE]; // Name of this font
- };
-
-
- // local variables used by EnumerateFonts callback function
- static int FontUsers = 0;
- static FontInfoRec FontInfo[MaxNumFonts];
- static int NumFonts; // Number of system fonts available
- static TDC* TheDC = 0;
-
- // EnumerateFont is a call back function. It receives information
- // about system fonts. It creates an example of each font by calling
- // CreateFont. When MaxNumFonts have been processed, 0 is returned
- // notifying windows to stop sending information, otherwise 1 is
- // returned telling windows to send more information if available
- //
- #if defined(__WIN32__)
- int __stdcall
- #else
- int far pascal _export
- #endif
- EnumerateFont(LOGFONT far* logFont, TEXTMETRIC far*, int dwType, LPARAM)
- {
- if( dwType == TRUETYPE_FONTTYPE){
- // Create the font described by logFont
- //
- FontInfo[NumFonts].Font = new TFont(logFont);
-
- // Save the height of the font for positioning when drawing in the window
- //
- FontInfo[NumFonts].Height = logFont->lfHeight;
-
- // Save the name of the font for drawing in the window
- //
- strcpy(FontInfo[NumFonts].Name, logFont->lfFaceName);
- TheDC->SelectObject(*FontInfo[NumFonts].Font);
-
- TSize extent(0,0);
- TheDC->GetTextExtent(logFont->lfFaceName, strlen(logFont->lfFaceName),
- extent);
- FontInfo[NumFonts].Width = extent.cx;
- TheDC->RestoreFont();
- NumFonts++;
- }
-
- return NumFonts >= MaxNumFonts ?
- 0 : // Don't send any more information, the array is full
- 1; // Send more information if available
- }
-
- // Collect all of the system fonts
- //
- void
- GetFontInfo()
- {
- if (FontUsers == 0) {
- TheDC = new TScreenDC;
- NumFonts = 0;
-
- // Create an instance of the call back function. This allows
- // our program to refer to an exported function. Otherwise the
- // Data segment will not be correct. This is a no-op in 32bit.
- //
- TProcInstance enumProc((FARPROC)EnumerateFont);
-
- // Gather information about all fonts that are allowable in our DC
- //
- EnumFonts(*TheDC, 0, (OLDFONTENUMPROC)(FARPROC)enumProc, 0);
-
- delete TheDC;
- TheDC = 0;
- }
- FontUsers++;
- }
-
- // Release font information
- //
- void
- ReleaseFontInfo()
- {
- FontUsers--;
- if (FontUsers == 0)
- for (int i = 0; i < NumFonts; i++) {
- delete FontInfo[i].Font;
- FontInfo[i].Font = 0;
- }
- }
-
-
- // support the following point sizes
- int pointSizes[] = {8, 9, 10, 11, 12, 14, 16, 18, 20, 22, 24, 26, 28, 36, 48, 72 };
- int nPointSizes = sizeof( pointSizes) / sizeof( pointSizes[0]);
-
-
- // A command enabler for the toolbar. This makes the toolbar automatically
- // enable and disable the buttons, just like the OWL TControlBar.
- class FormatBarButtonEnabler : public TCommandEnabler {
- public:
- FormatBarButtonEnabler(HWND hWndReceiver, FormatBar* fb, uint id)
- : TCommandEnabler( id, hWndReceiver) {
- formatBar = fb;
- }
-
- // override member functions of TCommandEnabler
- void Enable (bool enable);
- void SetText (const char far* text);
- void SetCheck (int state);
-
- protected:
- FormatBar* formatBar;
- };
-
-
- void FormatBarButtonEnabler::Enable(bool enable)
- {
- TCommandEnabler::Enable(enable);
- formatBar->SendMessage( TB_ENABLEBUTTON, Id, enable);
- }
-
-
- void FormatBarButtonEnabler::SetText(const char far* text)
- {
- // implement later, do nothing for now
-
- formatBar->SendDlgItemMessage( Id, CB_SELECTSTRING, (WPARAM)-1, (LPARAM)text);
- }
-
-
- void FormatBarButtonEnabler::SetCheck(int state)
- {
- formatBar->SendMessage( TB_CHECKBUTTON, Id, state);
- }
-
-
- // Initialization parameters for fbButton array. Include one line per button.
- static TBBUTTON fbButton[] = {
- { 0, 0, 0, TBSTYLE_SEP, {0,0}, 0L, 0},
- { 0, 0, 0, TBSTYLE_SEP, {0,0}, 0L, 0},
- { 0, 0, 0, TBSTYLE_SEP, {0,0}, 0L, 0},
- { 0, 0, 0, TBSTYLE_SEP, {0,0}, 0L, 0},
- { 0, 0, 0, TBSTYLE_SEP, {0,0}, 0L, 0},
- { 0, 0, 0, TBSTYLE_SEP, {0,0}, 0L, 0},
- { 0, 0, 0, TBSTYLE_SEP, {0,0}, 0L, 0},
- { 0, 0, 0, TBSTYLE_SEP, {0,0}, 0L, 0},
- { 0, 0, 0, TBSTYLE_SEP, {0,0}, 0L, 0},
- { 0, 0, 0, TBSTYLE_SEP, {0,0}, 0L, 0},
- { 0, 0, 0, TBSTYLE_SEP, {0,0}, 0L, 0},
- { 0, 0, 0, TBSTYLE_SEP, {0,0}, 0L, 0},
- { 0, 0, 0, TBSTYLE_SEP, {0,0}, 0L, 0},
- { 0, 0, 0, TBSTYLE_SEP, {0,0}, 0L, 0},
- { 0, 0, 0, TBSTYLE_SEP, {0,0}, 0L, 0},
- { 0, 0, 0, TBSTYLE_SEP, {0,0}, 0L, 0},
- { 0, 0, 0, TBSTYLE_SEP, {0,0}, 0L, 0},
- { 0, 0, 0, TBSTYLE_SEP, {0,0}, 0L, 0},
- { 0, 0, 0, TBSTYLE_SEP, {0,0}, 0L, 0},
- { 0, 0, 0, TBSTYLE_SEP, {0,0}, 0L, 0},
- { 0, 0, 0, TBSTYLE_SEP, {0,0}, 0L, 0},
- { 0, 0, 0, TBSTYLE_SEP, {0,0}, 0L, 0},
- { 0, 0, 0, TBSTYLE_SEP, {0,0}, 0L, 0},
- { 0, 0, 0, TBSTYLE_SEP, {0,0}, 0L, 0},
- { 0, 0, 0, TBSTYLE_SEP, {0,0}, 0L, 0},
- { 0, 0, 0, TBSTYLE_SEP, {0,0}, 0L, 0},
- { 0, 0, 0, TBSTYLE_SEP, {0,0}, 0L, 0},
- { ID_BOLD, CM_FORMATBOLD, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0,0}, 0L, 0},
- { ID_ITALIC, CM_FORMATITALIC, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0,0}, 0L, 0},
- { ID_UNDERLINE, CM_FORMATUNDERLINE, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0,0}, 0L, 0},
- { 0, 0, TBSTATE_ENABLED, TBSTYLE_SEP, {0,0}, 0L, 0},
- { ID_ALIGNLEFT, CM_FORMATLEFT, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0,0}, 0L, 0},
- { ID_ALIGNCENTER, CM_FORMATCENTER, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0,0}, 0L, 0},
- { ID_ALIGNRIGHT, CM_FORMATRIGHT, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0,0}, 0L, 0},
- }; // end of initfbbuttontemplate
-
- const int formatBarItems = sizeof( fbButton) / sizeof( fbButton[0]);
- const int nFormatItems = 6; // there are 12 view bitmap items
- const int firstFormatButton = 27; // first view button in structure
-
-
- //
- // Build a response table for all messages/commands handled
- // by the application.
- //
- DEFINE_RESPONSE_TABLE1(FormatBar, TWindow)
- //{{FormatBarRSP_TBL_BEGIN}}
- EV_NOTIFY_AT_CHILD(BN_CLICKED, BnClicked),
- EV_CBN_SELCHANGE(IDC_FONTFACE, CBNSelChangeFontFace),
- EV_CBN_SELCHANGE(IDC_FONTSIZE, CBNSelChangeFontSize),
- EV_CBN_CLOSEUP(IDC_FONTFACE, CBNCloseUpFontFace),
- EV_CBN_CLOSEUP(IDC_FONTSIZE, CBNCloseUpFontSize),
- EV_MESSAGE( WM_NOTIFY, EvCommonControlNotify),
- EV_WM_PAINT,
- //{{FormatBarRSP_TBL_END}}
- END_RESPONSE_TABLE;
-
-
- //{{FormatBar Implementation}}
-
-
- // the tab control is derived from TWindow with specific Attributes
- FormatBar::FormatBar (TWindow* parent, int id, int w, int h, TModule* module):
- TWindow(parent, 0, module)
- {
- Attr.Id = id;
- Attr.X = 0;
- Attr.Y = 0;
- Attr.W = w;
- Attr.H = h;
- Attr.ExStyle = 0;
- Attr.Style = WS_CHILD | WS_VISIBLE | TBSTYLE_TOOLTIPS | CCS_NOMOVEY;
-
- fontType = new ToolComboBox( this, IDC_FONTFACE, 0, 3, 152, 250, CBS_SORT | CBS_HASSTRINGS | CBS_DROPDOWNLIST, 0);
- fontSize = new ToolComboBox( this, IDC_FONTSIZE, 158, 3, 50, 248, CBS_HASSTRINGS | CBS_DROPDOWNLIST, 0);
-
- GetFontInfo();
- }
-
-
- FormatBar::FormatBar (TWindow* parent, const char far* title, TModule* module):
- TWindow(parent, title, module)
- {
- // INSERT>> Your constructor code here.
-
- }
-
-
- FormatBar::~FormatBar ()
- {
- Destroy();
-
- // INSERT>> Your destructor code here.
-
- ReleaseFontInfo();
- }
-
-
- char far* FormatBar::GetClassName ()
- {
- // return the Windows 95 Toolbar common control class name
- // this is what makes the window a tool bar
- return TOOLBARCLASSNAME;
- }
-
-
- void FormatBar::EvPaint ()
- {
- // Let Common Control paint the window, don't call TWindow::EvPaint()
- DefaultProcessing();
- }
-
-
- void FormatBar::SetupWindow ()
- {
- TWindow::SetupWindow();
-
- // INSERT>> Your code here.
-
- SetupFormatBar();
- FillComboBoxes();
- }
-
-
- // Create the toolbar control and add the buttons and commands.
- void FormatBar::SetupFormatBar ()
- {
- HWND hWndToolTip; // tool tip window handle
- TOOLINFO toolInfo; // tool tip info structure
-
- // Create the formatbar control and add the buttons, commands, and text.
- TColor colors( TColor::LtGray);
- COLORMAP mapSysColor;
-
- // Map custom button bitmaps background color to system button face color.
- mapSysColor.from = COLORREF( colors);
- mapSysColor.to = ::GetSysColor( COLOR_BTNFACE);
- CreateMappedBitmap( GetApplication()->GetInstance(), IDB_FORMAT, 0, &mapSysColor, nFormatItems);
-
- // Tell the control the size of button structure.
- SendMessage( TB_BUTTONSTRUCTSIZE, (WPARAM) sizeof(TBBUTTON), 0);
-
- // Initialize structures that identify the bitmaps.
- fbFormatBmp.hInst = GetApplication()->GetInstance();
- fbFormatBmp.nID = IDB_FORMAT;
-
- // initialize toolbar with small bitmaps
- fmtBitmap = SendMessage( TB_ADDBITMAP, (WPARAM)nFormatItems, (LPARAM)&fbFormatBmp);
-
- // Add the standard bitmap base number to the format offsets from the template.
- // note: this is not needed for the standard bitmaps if added first.
- for( int j = firstFormatButton; j < formatBarItems; j++){
- fbButton[j].iBitmap += fmtBitmap;
- }
-
- // Send the initialized buttons structure to the common controls dll.
- SendMessage( TB_ADDBUTTONS, (WPARAM)formatBarItems, (LPARAM)(LPTBBUTTON)fbButton);
-
- // get tooltip window handle
- hWndToolTip = (HWND)SendMessage( TB_GETTOOLTIPS, 0, 0);
-
- if (hWndToolTip)
- {
- // initialize the toolinfo structure
- toolInfo.cbSize = sizeof(toolInfo);
- toolInfo.uFlags = TTF_IDISHWND | TTF_CENTERTIP;
- toolInfo.lpszText = (LPSTR)IDC_FONTFACE;
- toolInfo.hwnd = fontType->HWindow;
- toolInfo.uId = (UINT)fontType->HWindow;
- toolInfo.hinst = GetApplication()->GetInstance();
-
- // add tooltips for the font type combo box
- ::SendMessage( hWndToolTip, TTM_ADDTOOL, 0, (LPARAM)&toolInfo);
-
- toolInfo.lpszText = (LPSTR)IDC_FONTSIZE;
- toolInfo.hwnd = fontSize->HWindow;
- toolInfo.uId = (UINT)fontSize->HWindow;
-
- // add tooltips for the font size combo box
- ::SendMessage( hWndToolTip, TTM_ADDTOOL, 0, (LPARAM)&toolInfo);
- }
-
- // Set the fonts for the combo boxes the same as the toolbar.
- fontType->SetWindowFont( GetWindowFont(), false);
- fontSize->SetWindowFont( GetWindowFont(), false);
-
- // Automatically size of the toolbar to the button size.
- SendMessage( TB_AUTOSIZE, 0, 0);
- }
-
-
- void FormatBar::FillComboBoxes ()
- {
- char buf[255];
-
- for (int i = 0; i < NumFonts; i++) {
- fontType->AddString( FontInfo[i].Name);
- }
- fontType->SetSelString( "Times", -1);
-
-
- // Fill the point size combo box with the list of possible point sizes
- for( i = 0; i < nPointSizes; i++)
- {
- wsprintf( buf, "%d", pointSizes[i]);
- fontSize->AddString( buf);
- }
- fontSize->SetSelString( "12", -1);
- }
-
-
- // This function handles the messages sent by Windows 95 common controls.
- LRESULT FormatBar::EvCommonControlNotify( WPARAM /*wParam*/, LPARAM lParam)
- {
- LRESULT retVal = false;
- NMHDR FAR *pNmhdr = (NMHDR FAR *)lParam;
- LPTBNOTIFY lpTbNotify = (LPTBNOTIFY)lParam;
-
- switch( pNmhdr->code){
- case TTN_NEEDTEXT: // process tool tip control message
- {
- LPTOOLTIPTEXT lpText;
-
- lpText = (LPTOOLTIPTEXT)lParam;
- lpText->hinst = GetModule()->GetInstance();
- lpText->lpszText = MAKEINTRESOURCE(lpText->hdr.idFrom);
- }
- break;
- case TBN_GETBUTTONINFO: // Customize Toolbar dialog nees this info
- if (lpTbNotify->iItem < formatBarItems) {
- lpTbNotify->tbButton = fbButton[lpTbNotify->iItem];
- retVal = true;
- }
- break;
- case TBN_QUERYINSERT: // Can Customize Dialog insert?
- retVal = true;
- break;
- case TBN_QUERYDELETE: // Can Customize Dialog delete?
- retVal = true;
- break;
- default:
- break;
- }
- return retVal;
- }
-
-
- // Pressing a toolbar button causes a BN_CLICKED message to be sent.
- // Re-transmit the BN_CLICKED Id as CM_... message to simulate menu selections.
- void FormatBar::BnClicked()
- {
- TCurrentEvent& currentEvent = GetCurrentEvent();
-
- // Post button Id to simulate a menu command.
- Parent->PostMessage( WM_COMMAND, LOWORD(currentEvent.WParam));
- }
-
-
- // Selecting a toolbar combobox item causes a LBN_SELCHANGE message to be sent.
- // Send a windows message corresponding to the action to be taken.
- void FormatBar::CBNSelChangeFontFace()
- {
- static char buf[255];
-
- fontType->GetSelString( buf, sizeof(buf));
- fontType->InformFocus( WM_CHANGEFONTFACE, (WPARAM)0, (LPARAM)buf);
- }
-
-
- // Selecting a toolbar combobox item causes a LBN_SELCHANGE message to be sent.
- // Send a windows message corresponding to the action to be taken.
- void FormatBar::CBNSelChangeFontSize()
- {
- char buf[255];
- int size;
-
- fontSize->GetSelString( buf, sizeof(buf));
- size = atoi(buf);
- fontSize->InformFocus( WM_CHANGEFONTSIZE, (WPARAM)size, (LPARAM)0);
- }
-
-
- // Done selecting combobox, reset focus window
- void FormatBar::CBNCloseUpFontFace()
- {
- fontType->RetWndFocus();
- }
-
-
- // Done selecting combobox, reset focus window
- void FormatBar::CBNCloseUpFontSize()
- {
- fontSize->RetWndFocus();
- }
-
-
- // IdleAction takes care of button enabling through FormatBarButtonEnabler.
- bool FormatBar::IdleAction (long idleCount)
- {
- if (idleCount == 0) {
- for( int i=0; i < formatBarItems; i++){
- if( fbButton[i].fsStyle != TBSTYLE_BUTTON)
- continue;
- // Must use SendMessage here since a ptr to a temp is passed
- Parent->SendMessage( WM_COMMAND_ENABLE, 0,
- (LPARAM)&FormatBarButtonEnabler( Parent->HWindow, this,
- fbButton[i].idCommand));
- }
- // send enable messages for the custom comboboxes
- Parent->SendMessage( WM_COMMAND_ENABLE, 0,
- (LPARAM)&FormatBarButtonEnabler( Parent->HWindow, this, IDC_FONTFACE));
- Parent->SendMessage( WM_COMMAND_ENABLE, 0,
- (LPARAM)&FormatBarButtonEnabler( Parent->HWindow, this, IDC_FONTSIZE));
- }
- return TWindow::IdleAction(idleCount);
- }
-
-