comparison win/dw.c @ 1051:6919854298fd

Added dw_font_choose() on Windows for 2.1.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Thu, 02 Jun 2011 06:18:37 +0000
parents 05ff61fd60d7
children 2f79f183ff03
comparison
equal deleted inserted replaced
1050:48f43c975533 1051:6919854298fd
3993 void API dw_window_reparent(HWND handle, HWND newparent) 3993 void API dw_window_reparent(HWND handle, HWND newparent)
3994 { 3994 {
3995 SetParent(handle, newparent); 3995 SetParent(handle, newparent);
3996 } 3996 }
3997 3997
3998 LOGFONT _get_logfont(char *fontname)
3999 {
4000 int Italic, Bold;
4001 char *myFontName;
4002 int z, size = 9;
4003 LOGFONT lf;
4004 for(z=0;z<strlen(fontname);z++)
4005 {
4006 if(fontname[z]=='.')
4007 break;
4008 }
4009 size = atoi(fontname) + 5; /* no idea why this 5 needs to be added */
4010 Italic = instring(" Italic", &fontname[z+1]);
4011 Bold = instring(" Bold", &fontname[z+1]);
4012 lf.lfHeight = size;
4013 lf.lfWidth = 0;
4014 lf.lfEscapement = 0;
4015 lf.lfOrientation = 0;
4016 lf.lfItalic = Italic ? TRUE : FALSE;
4017 lf.lfUnderline = 0;
4018 lf.lfStrikeOut = 0;
4019 lf.lfWeight = Bold ? FW_BOLD : FW_NORMAL;
4020 lf.lfCharSet = DEFAULT_CHARSET;
4021 lf.lfOutPrecision = 0;
4022 lf.lfClipPrecision = 0;
4023 lf.lfQuality = DEFAULT_QUALITY;
4024 lf.lfPitchAndFamily = DEFAULT_PITCH | FW_DONTCARE;
4025 /*
4026 * remove any font modifiers
4027 */
4028 myFontName = strdup(&fontname[z+1]);
4029 if(Italic)
4030 myFontName[Italic] = 0;
4031 if(Bold)
4032 myFontName[Bold] = 0;
4033 strcpy(lf.lfFaceName, myFontName);
4034 free(myFontName);
4035 return lf;
4036 }
4037
3998 HFONT _acquire_font(HWND handle, char *fontname) 4038 HFONT _acquire_font(HWND handle, char *fontname)
3999 { 4039 {
4000 HFONT hfont = 0; 4040 HFONT hfont = 0;
4001 4041
4002 if(fontname != DefaultFont && fontname[0]) 4042 if(fontname != DefaultFont && fontname[0])
4003 { 4043 {
4004 int Italic, Bold; 4044 LOGFONT lf = _get_logfont(fontname);
4005 char *myFontName;
4006 int z, size = 9;
4007 LOGFONT lf;
4008 #if 0
4009 HDC hDC = GetDC(handle);
4010 #endif
4011 for(z=0;z<strlen(fontname);z++)
4012 {
4013 if(fontname[z]=='.')
4014 break;
4015 }
4016 size = atoi(fontname) + 5; /* no idea why this 5 needs to be added */
4017 Italic = instring(" Italic", &fontname[z+1]);
4018 Bold = instring(" Bold", &fontname[z+1]);
4019 #if 0
4020 lf.lfHeight = -MulDiv(size, GetDeviceCaps(hDC, LOGPIXELSY), 72);
4021 #endif
4022 lf.lfHeight = size;
4023 lf.lfWidth = 0;
4024 lf.lfEscapement = 0;
4025 lf.lfOrientation = 0;
4026 lf.lfItalic = Italic ? TRUE : FALSE;
4027 lf.lfUnderline = 0;
4028 lf.lfStrikeOut = 0;
4029 lf.lfWeight = Bold ? FW_BOLD : FW_NORMAL;
4030 lf.lfCharSet = DEFAULT_CHARSET;
4031 lf.lfOutPrecision = 0;
4032 lf.lfClipPrecision = 0;
4033 lf.lfQuality = DEFAULT_QUALITY;
4034 lf.lfPitchAndFamily = DEFAULT_PITCH | FW_DONTCARE;
4035 /*
4036 * remove any font modifiers
4037 */
4038 myFontName = strdup(&fontname[z+1]);
4039 if(Italic)
4040 myFontName[Italic] = 0;
4041 if(Bold)
4042 myFontName[Bold] = 0;
4043 strcpy(lf.lfFaceName, myFontName);
4044 free(myFontName);
4045
4046 hfont = CreateFontIndirect(&lf); 4045 hfont = CreateFontIndirect(&lf);
4047 #if 0
4048 ReleaseDC(handle, hDC);
4049 #endif
4050 } 4046 }
4051 if(!hfont) 4047 if(!hfont)
4052 hfont = GetStockObject(DEFAULT_GUI_FONT); 4048 hfont = GetStockObject(DEFAULT_GUI_FONT);
4053 return hfont; 4049 return hfont;
4054 } 4050 }
4106 if(oldfont) 4102 if(oldfont)
4107 DeleteObject(oldfont); 4103 DeleteObject(oldfont);
4108 return 0; 4104 return 0;
4109 } 4105 }
4110 4106
4111 /* 4107 /* Allows the user to choose a font using the system's font chooser dialog.
4112 * Gets the font used by a specified window (widget) handle. 4108 * Parameters:
4113 * Parameters: 4109 * currfont: current font
4114 * handle: The window (widget) handle. 4110 * Returns:
4115 * fontname: Name and size of the font in the form "size.fontname" 4111 * A malloced buffer with the selected font or NULL on error.
4116 */ 4112 */
4117 char * API dw_window_get_font(HWND handle) 4113 char * API dw_font_choose(char *currfont)
4118 { 4114 {
4119 HFONT oldfont = NULL; 4115 CHOOSEFONT cf = { 0 };
4116 LOGFONT lf = { 0 };
4120 char *str = NULL; 4117 char *str = NULL;
4121 char *bold = ""; 4118 char *bold = "";
4122 char *italic = ""; 4119 char *italic = "";
4123 LOGFONT lf = { 0 }; 4120
4124 Box *thisbox; 4121 if(currfont && *currfont)
4125 char tmpbuf[100]; 4122 lf = _get_logfont(currfont);
4126 4123
4127 GetClassName(handle, tmpbuf, 99); 4124 cf.lStructSize = sizeof(cf);
4128 if ( strnicmp( tmpbuf, FRAMECLASSNAME, strlen(FRAMECLASSNAME)+1) == 0 ) 4125 cf.Flags = CF_SCREENFONTS;
4129 { 4126 cf.lpLogFont = &lf;
4130 /* groupbox */ 4127
4131 thisbox = (Box *)GetWindowLongPtr( handle, GWLP_USERDATA ); 4128 if(ChooseFont(&cf))
4132 if ( thisbox && thisbox->grouphwnd != (HWND)NULL )
4133 {
4134 handle = thisbox->grouphwnd;
4135 }
4136 }
4137 oldfont = (HFONT)SendMessage(handle, WM_GETFONT, 0, 0);
4138 if ( GetObject( oldfont, sizeof(lf), &lf ) )
4139 { 4129 {
4140 str = (char *)malloc( 100 ); 4130 str = (char *)malloc( 100 );
4141 if ( str ) 4131 if ( str )
4142 { 4132 {
4143 int height; 4133 int height;
4153 * added 5 (in _acquire_font() above - don't know why ) 4143 * added 5 (in _acquire_font() above - don't know why )
4154 */ 4144 */
4155 height = lf.lfHeight - 5; 4145 height = lf.lfHeight - 5;
4156 sprintf( str, "%d.%s%s%s", height, lf.lfFaceName, bold, italic ); 4146 sprintf( str, "%d.%s%s%s", height, lf.lfFaceName, bold, italic );
4157 } 4147 }
4158 else 4148 }
4159 str = ""; 4149 return str;
4160 } 4150 }
4161 else 4151
4162 str = ""; 4152 /*
4153 * Gets the font used by a specified window (widget) handle.
4154 * Parameters:
4155 * handle: The window (widget) handle.
4156 * fontname: Name and size of the font in the form "size.fontname"
4157 */
4158 char * API dw_window_get_font(HWND handle)
4159 {
4160 HFONT oldfont = NULL;
4161 char *str = NULL;
4162 char *bold = "";
4163 char *italic = "";
4164 LOGFONT lf = { 0 };
4165 Box *thisbox;
4166 char tmpbuf[100];
4167
4168 GetClassName(handle, tmpbuf, 99);
4169 if ( strnicmp( tmpbuf, FRAMECLASSNAME, strlen(FRAMECLASSNAME)+1) == 0 )
4170 {
4171 /* groupbox */
4172 thisbox = (Box *)GetWindowLongPtr( handle, GWLP_USERDATA );
4173 if ( thisbox && thisbox->grouphwnd != (HWND)NULL )
4174 {
4175 handle = thisbox->grouphwnd;
4176 }
4177 }
4178 oldfont = (HFONT)SendMessage(handle, WM_GETFONT, 0, 0);
4179 if ( GetObject( oldfont, sizeof(lf), &lf ) )
4180 {
4181 str = (char *)malloc( 100 );
4182 if ( str )
4183 {
4184 int height;
4185 if ( lf.lfWeight > FW_MEDIUM )
4186 bold = " Bold";
4187 if ( lf.lfItalic )
4188 italic = " Italic";
4189 if ( lf.lfHeight <= 0 )
4190 height = abs (lf.lfHeight );
4191 else
4192 /*
4193 * we subtract 5 from a positive font height, because we (presumably)
4194 * added 5 (in _acquire_font() above - don't know why )
4195 */
4196 height = lf.lfHeight - 5;
4197 sprintf( str, "%d.%s%s%s", height, lf.lfFaceName, bold, italic );
4198 }
4199 }
4163 if ( oldfont ) 4200 if ( oldfont )
4164 DeleteObject( oldfont ); 4201 DeleteObject( oldfont );
4165 #if 0
4166 {
4167 HWND hwnd=NULL; // owner window
4168 HDC hdc; // display device context of owner window
4169
4170 CHOOSEFONT cf; // common dialog box structure
4171 LOGFONT lf; // logical font structure
4172 HFONT hfont, hfontPrev;
4173
4174 // Initialize CHOOSEFONT
4175 ZeroMemory(&cf, sizeof(cf));
4176 cf.lStructSize = sizeof (cf);
4177 cf.hwndOwner = hwnd;
4178 cf.lpLogFont = &lf;
4179 cf.Flags = CF_SCREENFONTS | CF_EFFECTS;
4180
4181 if (ChooseFont(&cf)==TRUE)
4182 {
4183 hfont = CreateFontIndirect(cf.lpLogFont);
4184 }
4185 }
4186 #endif
4187 return str; 4202 return str;
4188 } 4203 }
4189 4204
4190 /* 4205 /*
4191 * Sets the colors used by a specified window (widget) handle. 4206 * Sets the colors used by a specified window (widget) handle.