FontComboBox.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /* ==========================================================================
  2. File : FontComboBox.cpp
  3. Class : CFontComboBox
  4. Author : Johan Rosengren, Abstrakt Mekanik AB
  5. Iain Clarke
  6. Date : 2005-05-06
  7. Purpose : CFontComboBox is derived from "CComboBox" and is an
  8. autosizing no-frills combobox for display of the fonts
  9. installed in the system..
  10. Description : Simpel derived class with members to fill the box, to
  11. autosize the dropdown and select an entry by name.
  12. Usage : Create as any combobox, and call "FillCombo" to fill
  13. the control with the names of the fonts installed in
  14. the system. Call "SelectFontName" to select a font by
  15. name.
  16. ========================================================================*/
  17. #include "stdafx.h"
  18. #include "FontComboBox.h"
  19. #ifdef _DEBUG
  20. #define new DEBUG_NEW
  21. #undef THIS_FILE
  22. static char THIS_FILE[] = __FILE__;
  23. #endif
  24. // Enumeration callback function for
  25. // installed fonts
  26. BOOL CALLBACK EnumFontProc( LPLOGFONT lplf, LPTEXTMETRIC /*lptm*/, DWORD /*dwType*/, LPARAM lpData )
  27. {
  28. CFontComboBox *caller = reinterpret_cast< CFontComboBox* > ( lpData );
  29. caller->AddString( lplf->lfFaceName );
  30. CClientDC dc( caller );
  31. dc.SelectStockObject( ANSI_VAR_FONT );
  32. // Add a "0" for the margin.
  33. CSize sz = dc.GetTextExtent( CString( lplf->lfFaceName ) + CString( "0" ) );
  34. caller->SetMaxWidth( max( caller->GetMaxWidth(), sz.cx ) );
  35. return TRUE;
  36. }
  37. /////////////////////////////////////////////////////////////////////////////
  38. // CFontComboBox
  39. CFontComboBox::CFontComboBox()
  40. /* ============================================================
  41. Function : CFontComboBox::CFontComboBox
  42. Description : ctor
  43. Access : Public
  44. Return : void
  45. Parameters : none
  46. Usage :
  47. ============================================================*/
  48. {
  49. m_maxWidth = 0;
  50. }
  51. CFontComboBox::~CFontComboBox()
  52. /* ============================================================
  53. Function : CFontComboBox::~CFontComboBox
  54. Description : dtor
  55. Access : Public
  56. Return : void
  57. Parameters : none
  58. Usage :
  59. ============================================================*/
  60. {
  61. }
  62. BEGIN_MESSAGE_MAP(CFontComboBox, CComboBox)
  63. //{{AFX_MSG_MAP(CFontComboBox)
  64. ON_CONTROL_REFLECT(CBN_DROPDOWN, OnDropdown)
  65. //}}AFX_MSG_MAP
  66. END_MESSAGE_MAP()
  67. /////////////////////////////////////////////////////////////////////////////
  68. // CFontComboBox message handlers
  69. void CFontComboBox::OnDropdown()
  70. /* ============================================================
  71. Function : CFontComboBox::OnDropdown
  72. Description : Sets the dropped down width of the combo
  73. control to the max width of the string in
  74. the list
  75. Access : Protected
  76. Return : void
  77. Parameters : none
  78. Usage : Called from MFC
  79. ============================================================*/
  80. {
  81. int scroll = ::GetSystemMetrics(SM_CXVSCROLL);
  82. SetDroppedWidth( m_maxWidth + scroll );
  83. }
  84. /////////////////////////////////////////////////////////////////////////////
  85. // CFontComboBox implementation
  86. void CFontComboBox::FillCombo()
  87. /* ============================================================
  88. Function : CFontComboBox::FillCombo
  89. Description : Fills the combo with the font names from
  90. the system
  91. Access : Public
  92. Return : void
  93. Parameters : none
  94. Usage : Call to fill the combo with the installed
  95. fonts.
  96. ============================================================*/
  97. {
  98. CClientDC dc( this );
  99. ::EnumFonts( dc, 0, ( FONTENUMPROC ) EnumFontProc, ( LPARAM ) this );
  100. }
  101. void CFontComboBox::SetMaxWidth( int maxWidth )
  102. /* ============================================================
  103. Function : CFontComboBox::SetMaxWidth
  104. Description : Sets the "m_maxWidth" member.
  105. Access : Public
  106. Return : void
  107. Parameters : int maxWidth - New width of the drop
  108. down list
  109. Usage : Call to set a max width for the strings in
  110. the list, used to set the width of the drop
  111. down list. Called from the font enum
  112. callback.
  113. ============================================================*/
  114. {
  115. m_maxWidth = maxWidth;
  116. }
  117. int CFontComboBox::GetMaxWidth() const
  118. /* ============================================================
  119. Function : CFontComboBox::GetMaxWidth
  120. Description : Returns the "m_maxWidth" member of the
  121. control.
  122. Access : Public
  123. Return : int - Width of the longest string, in
  124. pixels.
  125. Parameters : none
  126. Usage : Call to get the width necessary to display
  127. the longest string in the combo drop down.
  128. ============================================================*/
  129. {
  130. return m_maxWidth;
  131. }
  132. void CFontComboBox::SelectFontName( const CString& font )
  133. /* ============================================================
  134. Function : CFontComboBox::SelectFontName
  135. Description : Selects "font" in the list, if it
  136. exists.
  137. Access : Public
  138. Return : void
  139. Parameters : const CString& font - Name of the font to
  140. select.
  141. Usage : Call to set/change the selection of the
  142. combo.
  143. ============================================================*/
  144. {
  145. int max = GetCount();
  146. for( int t = 0 ; t < max ; t++ )
  147. {
  148. CString data;
  149. GetLBText( t, data );
  150. if( data == font )
  151. {
  152. SetCurSel( t );
  153. return;
  154. }
  155. }
  156. }