RRECToolbar.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /* ==========================================================================
  2. File : RRECToolbar.cpp
  3. Class : CRRECToolbar
  4. Author : Johan Rosengren, Abstrakt Mekanik AB
  5. Iain Clarke
  6. Date : 2004-05-07
  7. Purpose : This class encapsulates a toolbar that can be used with
  8. "CRulerRichEditCtrl". The class is derived from "CToolBarCtrl",
  9. and manages a formatting toolbar
  10. Description : A "CToolBarCtrl"-derived class. Reads a toolbar resource
  11. with the ID "TOOLBAR_CONTROL" and adds combo controls for
  12. font name and -size, as well as a color picker at the
  13. positions "FONT_NAME_POS", "FONT_SIZE_POS" and
  14. "FONT_COLOR_POS" respectively.
  15. Usage : Created by the rich edit mini-editor.
  16. ========================================================================*/
  17. #include "stdafx.h"
  18. #include "RRECToolbar.h"
  19. #include "ids.h"
  20. #include <tchar.h>
  21. #ifdef _DEBUG
  22. #define new DEBUG_NEW
  23. #undef THIS_FILE
  24. static char THIS_FILE[] = __FILE__;
  25. #endif
  26. extern UINT urm_SETCURRENTFONTNAME;
  27. extern UINT urm_SETCURRENTFONTSIZE;
  28. extern UINT urm_SETCURRENTFONTCOLOR;
  29. /////////////////////////////////////////////////////////////////////////////
  30. // CRRECToolbar
  31. CRRECToolbar::CRRECToolbar()
  32. /* ============================================================
  33. Function : CRRECToolbar::CRRECToolbar
  34. Description : ctor
  35. Access : Public
  36. Return : void
  37. Parameters : none
  38. Usage :
  39. ============================================================*/
  40. {
  41. }
  42. CRRECToolbar::~CRRECToolbar()
  43. /* ============================================================
  44. Function : CRRECToolbar::~CRRECToolbar
  45. Description : dtor
  46. Access : Public
  47. Return : void
  48. Parameters : none
  49. Usage :
  50. ============================================================*/
  51. {
  52. }
  53. BOOL CRRECToolbar::Create( CWnd* parent, CRect& rc )
  54. /* ============================================================
  55. Function : CRRECToolbar::Create
  56. Description : Creates the toolbar control
  57. Access : Public
  58. Return : BOOL - "TRUE" if success
  59. Parameters : CWnd* parent - Parent editor
  60. CRect& rc - Rectangle to place
  61. toolbar in.
  62. Usage : Called from the parent editor
  63. ============================================================*/
  64. {
  65. BOOL result = FALSE;
  66. HINSTANCE hInstance = AfxFindResourceHandle( MAKEINTRESOURCE( TOOLBAR_CONTROL ), RT_TOOLBAR );
  67. if(!hInstance)
  68. return FALSE;
  69. HRSRC hRsrc = ::FindResource( hInstance, MAKEINTRESOURCE( TOOLBAR_CONTROL ), RT_TOOLBAR );
  70. if( !hRsrc )
  71. return FALSE;
  72. HGLOBAL hGlobal = LoadResource( hInstance, hRsrc );
  73. if (hGlobal == NULL)
  74. return FALSE;
  75. CToolBarData* pData = ( CToolBarData* ) LockResource( hGlobal );
  76. if (pData == NULL)
  77. return FALSE;
  78. ASSERT( pData->wVersion == 1 );
  79. TBBUTTON tb, tbSep;
  80. memset ( &tb, 0, sizeof( tb ) );
  81. memset ( &tbSep, 0, sizeof( tbSep ) );
  82. result = CToolBarCtrl::Create(WS_VISIBLE|WS_CHILD, rc, parent, TOOLBAR_CONTROL);
  83. if( result )
  84. {
  85. SetButtonStructSize( sizeof ( tb ) );
  86. CSize sz ( pData->wWidth, pData->wHeight );
  87. SetBitmapSize( sz );
  88. sz.cx += 4;
  89. sz.cy += 4;
  90. SetButtonSize( sz );
  91. // Loop through adding buttons.
  92. tb.fsState = TBSTATE_ENABLED;
  93. tb.fsStyle = TBSTYLE_BUTTON;
  94. tb.iString = -1;
  95. tb.iBitmap = 0;
  96. tbSep.iString = -1;
  97. tbSep.fsStyle = TBSTYLE_SEP;
  98. for( WORD w = 0; w < pData->wItemCount; w++ )
  99. {
  100. if ( pData->items()[ w ] == 0 )
  101. AddButtons( 1, &tbSep );
  102. else
  103. {
  104. tb.idCommand = pData->items()[ w ];
  105. AddButtons( 1, &tb );
  106. tb.iBitmap++;
  107. }
  108. }
  109. HBITMAP hBitmap = (HBITMAP) ::LoadImage( hInstance, MAKEINTRESOURCE( TOOLBAR_CONTROL ), IMAGE_BITMAP, 0,0, LR_LOADMAP3DCOLORS );
  110. if( !hBitmap )
  111. return FALSE;
  112. BITMAP bm;
  113. memset( &bm, 0, sizeof ( bm ) );
  114. ::GetObject( hBitmap, sizeof ( bm ), &bm );
  115. AddBitmap( bm.bmWidth / pData->wWidth, CBitmap::FromHandle ( hBitmap ) );
  116. UnlockResource( hGlobal );
  117. FreeResource( hGlobal );
  118. /////////////////////////////////////
  119. // Map in combo boxes
  120. //
  121. CRect rect;
  122. TBBUTTONINFO tbi;
  123. tbi.cbSize = sizeof( TBBUTTONINFO );
  124. tbi.cx = FONT_COMBO_WIDTH;
  125. tbi.dwMask = TBIF_SIZE | 0x80000000; // By index
  126. SetButtonInfo( FONT_NAME_POS, &tbi );
  127. GetItemRect( FONT_NAME_POS, &rect );
  128. rect.bottom += COMBO_HEIGHT;
  129. // The font name combo
  130. if( m_font.Create( WS_CHILD |
  131. WS_VSCROLL |
  132. WS_VISIBLE |
  133. CBS_AUTOHSCROLL |
  134. CBS_DROPDOWN |
  135. CBS_SORT |
  136. CBS_HASSTRINGS,
  137. rect, this, DROPDOWN_FONT ) )
  138. {
  139. m_font.SetFont( CFont::FromHandle( ( HFONT ) ::GetStockObject( ANSI_VAR_FONT ) ) );
  140. m_font.FillCombo();
  141. tbi.cx = COMBO_WIDTH;
  142. SetButtonInfo( FONT_SIZE_POS, &tbi );
  143. GetItemRect( FONT_SIZE_POS, &rect );
  144. rect.bottom += COMBO_HEIGHT;
  145. // The font size combo
  146. if( m_size.Create( WS_CHILD |
  147. WS_VISIBLE |
  148. CBS_AUTOHSCROLL |
  149. CBS_DROPDOWNLIST |
  150. CBS_HASSTRINGS,
  151. rect, this, DROPDOWN_SIZE ) )
  152. {
  153. m_size.SetFont( CFont::FromHandle( ( HFONT ) ::GetStockObject( ANSI_VAR_FONT ) ) );
  154. m_size.FillCombo();
  155. CString color;
  156. CString defaultText;
  157. CString customText;
  158. color.LoadString( STRING_COLOR );
  159. defaultText.LoadString( STRING_DEFAULT );
  160. customText.LoadString( STRING_CUSTOM );
  161. tbi.cx = COLOR_WIDTH;
  162. SetButtonInfo( FONT_COLOR_POS, &tbi );
  163. GetItemRect( FONT_COLOR_POS, &rect );
  164. // The color picker
  165. if( m_color.Create( color,
  166. WS_VISIBLE|
  167. WS_CHILD,
  168. rect, this, BUTTON_COLOR ) )
  169. {
  170. m_color.SetDefaultText( defaultText );
  171. m_color.SetCustomText( customText );
  172. m_color.SetSelectionMode( CP_MODE_TEXT );
  173. m_color.SetBkColour( RGB( 255, 255, 255 ) );
  174. m_color.SetFont( CFont::FromHandle( ( HFONT ) ::GetStockObject( ANSI_VAR_FONT ) ) );
  175. result = TRUE;
  176. }
  177. }
  178. }
  179. }
  180. return result;
  181. }
  182. BEGIN_MESSAGE_MAP(CRRECToolbar, CToolBarCtrl)
  183. //{{AFX_MSG_MAP(CRRECToolbar)
  184. // NOTE - the ClassWizard will add and remove mapping macros here.
  185. ON_CBN_SELCHANGE(DROPDOWN_FONT, OnSelchangeFont)
  186. ON_CBN_SELCHANGE(DROPDOWN_SIZE, OnSelchangeSize)
  187. ON_MESSAGE(CPN_SELENDOK, OnColorButton)
  188. //}}AFX_MSG_MAP
  189. END_MESSAGE_MAP()
  190. /////////////////////////////////////////////////////////////////////////////
  191. // CRRECToolbar message handlers
  192. void CRRECToolbar::OnSelchangeFont()
  193. /* ============================================================
  194. Function : CRRECToolbar::OnSelchangeFont
  195. Description : Changes the font of the selected text in
  196. the editor.
  197. Access : Protected
  198. Return : void
  199. Parameters : none
  200. Usage : Called from MFC when the selection changes
  201. in the font name combo.
  202. ============================================================*/
  203. {
  204. CString font;
  205. int index = m_font.GetCurSel();
  206. if( index != CB_ERR )
  207. {
  208. m_font.GetLBText( index, font );
  209. GetParent()->SendMessage( urm_SETCURRENTFONTNAME, ( WPARAM ) ( LPCTSTR ) font, 0 );
  210. }
  211. }
  212. void CRRECToolbar::OnSelchangeSize()
  213. /* ============================================================
  214. Function : CRRECToolbar::OnSelchangeSize
  215. Description : Changes the size of the selected text in
  216. the editor.
  217. Access : Protected
  218. Return : void
  219. Parameters : none
  220. Usage : Called from MFC when the selection changes
  221. in the font size combo.
  222. ============================================================*/
  223. {
  224. int size = 0;
  225. int index = m_size.GetCurSel();
  226. if( index != CB_ERR )
  227. {
  228. CString sz;
  229. m_size.GetLBText( index, sz );
  230. size = _ttoi( ( LPCTSTR ) sz );
  231. GetParent()->SendMessage( urm_SETCURRENTFONTSIZE, 0, ( LPARAM ) size );
  232. }
  233. }
  234. LRESULT CRRECToolbar::OnColorButton( WPARAM, LPARAM )
  235. /* ============================================================
  236. Function : CRRECToolbar::OnColorButton
  237. Description : Mapped to the color picker defined
  238. "CPN_SELENDOK" message, sent when the color
  239. is changed in the picker.
  240. Access : Protected
  241. Return : LRESULT - Not used
  242. Parameters : WPARAM - Not used
  243. LPARAM - Not used
  244. Usage : Called from MFC.
  245. ============================================================*/
  246. {
  247. COLORREF color = RGB( 0, 0, 0 );
  248. color = m_color.GetColour();
  249. GetParent()->SendMessage( urm_SETCURRENTFONTCOLOR, 0, ( LPARAM ) color );
  250. return 0;
  251. }
  252. /////////////////////////////////////////////////////////////////////////////
  253. // CRRECToolbar UI updaters
  254. void CRRECToolbar::SetFontName( const CString& font )
  255. /* ============================================================
  256. Function : CRRECToolbar::SetFontName
  257. Description : Selects the font name "font" in the font
  258. name combo on the toolbar.
  259. Access : Public
  260. Return : void
  261. Parameters : none
  262. Usage : Call to set the selected font name
  263. ============================================================*/
  264. {
  265. if( m_font.m_hWnd )
  266. m_font.SelectFontName( font );
  267. }
  268. void CRRECToolbar::SetFontSize( int size )
  269. /* ============================================================
  270. Function : CRRECToolbar::SetFontSize
  271. Description : Selects the font size "size" in the font
  272. size combo on the toolbar.
  273. Access : Public
  274. Return : void
  275. Parameters : none
  276. Usage : Call to set the selected font size
  277. ============================================================*/
  278. {
  279. if( m_size.m_hWnd )
  280. m_size.SelectSize( size );
  281. }
  282. void CRRECToolbar::SetFontColor( COLORREF color )
  283. /* ============================================================
  284. Function : CRRECToolbar::SetFontColor
  285. Description : Selects the font color "color" in the font
  286. color picker on the toolbar.
  287. Access : Public
  288. Return : void
  289. Parameters : none
  290. Usage : Call to set the color picker color
  291. ============================================================*/
  292. {
  293. if( m_color.m_hWnd )
  294. m_color.SetColour( color );
  295. }