Mercurial > dwindows
comparison mac/dw.m @ 1146:9d97610b2140
Adding dw_pixmap_set_font() which is equivalent to dw_window_set_font() except for pixmaps.
When drawing to pixmaps, normally the font is obtained from the associated window handle.
This will allow overriding of the font for this pixmap, and will allow setting fonts on
pixmaps that don't have associated window handles.
This commit has the Mac support. Other platforms coming in the next hour.
author | bsmith@81767d24-ef19-dc11-ae90-00e081727c95 |
---|---|
date | Sun, 11 Sep 2011 21:36:07 +0000 |
parents | 5878dbfafbe2 |
children | 091ed7c20b3f |
comparison
equal
deleted
inserted
replaced
1145:9c47a0245872 | 1146:9d97610b2140 |
---|---|
4801 } | 4801 } |
4802 _DWLastDrawable = handle; | 4802 _DWLastDrawable = handle; |
4803 } | 4803 } |
4804 if(pixmap) | 4804 if(pixmap) |
4805 { | 4805 { |
4806 NSFont *font = nil; | 4806 NSFont *font = pixmap->font; |
4807 DWRender *render = pixmap->handle; | 4807 DWRender *render = pixmap->handle; |
4808 if([render isMemberOfClass:[DWRender class]]) | 4808 if(!font && [render isMemberOfClass:[DWRender class]]) |
4809 { | 4809 { |
4810 font = [render font]; | 4810 font = [render font]; |
4811 } | 4811 } |
4812 image = (id)pixmap->image; | 4812 image = (id)pixmap->image; |
4813 [NSGraphicsContext saveGraphicsState]; | 4813 [NSGraphicsContext saveGraphicsState]; |
6070 if(total > 0) | 6070 if(total > 0) |
6071 { | 6071 { |
6072 retval = pos / total; | 6072 retval = pos / total; |
6073 } | 6073 } |
6074 return retval; | 6074 return retval; |
6075 } | |
6076 | |
6077 /* Internal function to convert fontname to NSFont */ | |
6078 NSFont *_dw_font_by_name(char *fontname) | |
6079 { | |
6080 char *fontcopy = strdup(fontname); | |
6081 char *name = strchr(fontcopy, '.'); | |
6082 NSFont *font = nil; | |
6083 | |
6084 if(name) | |
6085 { | |
6086 int size = atoi(fontcopy); | |
6087 *name = 0; | |
6088 name++; | |
6089 font = [NSFont fontWithName:[ NSString stringWithUTF8String:name ] size:(float)size]; | |
6090 } | |
6091 free(fontcopy); | |
6092 return font; | |
6075 } | 6093 } |
6076 | 6094 |
6077 /* | 6095 /* |
6078 * Create a bitmap object to be packed. | 6096 * Create a bitmap object to be packed. |
6079 * Parameters: | 6097 * Parameters: |
6274 pixmap->image = image; | 6292 pixmap->image = image; |
6275 pixmap->handle = handle; | 6293 pixmap->handle = handle; |
6276 return pixmap; | 6294 return pixmap; |
6277 } | 6295 } |
6278 | 6296 |
6297 | |
6298 /* | |
6299 * Sets the font used by a specified pixmap. | |
6300 * Normally the pixmap font is obtained from the associated window handle. | |
6301 * However this can be used to override that, or for pixmaps with no window. | |
6302 * Parameters: | |
6303 * pixmap: Handle to a pixmap returned by dw_pixmap_new() or | |
6304 * passed to the application via a callback. | |
6305 * fontname: Name and size of the font in the form "size.fontname" | |
6306 */ | |
6307 int API dw_pixmap_set_font(HPIXMAP pixmap, char *fontname) | |
6308 { | |
6309 if(pixmap) | |
6310 { | |
6311 NSFont *font = _dw_font_by_name(fontname); | |
6312 | |
6313 if(font) | |
6314 { | |
6315 NSFont *oldfont = pixmap->font; | |
6316 [font retain]; | |
6317 pixmap->font = font; | |
6318 if(oldfont) | |
6319 [oldfont release]; | |
6320 return DW_ERROR_NONE; | |
6321 } | |
6322 } | |
6323 return DW_ERROR_GENERAL; | |
6324 } | |
6325 | |
6279 /* | 6326 /* |
6280 * Destroys an allocated pixmap. | 6327 * Destroys an allocated pixmap. |
6281 * Parameters: | 6328 * Parameters: |
6282 * pixmap: Handle to a pixmap returned by | 6329 * pixmap: Handle to a pixmap returned by |
6283 * dw_pixmap_new.. | 6330 * dw_pixmap_new.. |
6284 */ | 6331 */ |
6285 void API dw_pixmap_destroy(HPIXMAP pixmap) | 6332 void API dw_pixmap_destroy(HPIXMAP pixmap) |
6286 { | 6333 { |
6287 if ( pixmap ) | 6334 if(pixmap) |
6288 { | 6335 { |
6289 NSBitmapImageRep *image = (NSBitmapImageRep *)pixmap->image; | 6336 NSBitmapImageRep *image = (NSBitmapImageRep *)pixmap->image; |
6290 [image release]; | 6337 NSFont *font = pixmap->font; |
6291 free(pixmap); | 6338 [image release]; |
6339 [font release]; | |
6340 free(pixmap); | |
6292 } | 6341 } |
6293 } | 6342 } |
6294 | 6343 |
6295 /* | 6344 /* |
6296 * Copies from one item to another. | 6345 * Copies from one item to another. |
7262 } | 7311 } |
7263 /* Set the window back to a normal window */ | 7312 /* Set the window back to a normal window */ |
7264 [window setLevel:NSNormalWindowLevel]; | 7313 [window setLevel:NSNormalWindowLevel]; |
7265 [window setHidesOnDeactivate:NO]; | 7314 [window setHidesOnDeactivate:NO]; |
7266 } | 7315 } |
7267 } | |
7268 | |
7269 NSFont *_dw_font_by_name(char *fontname) | |
7270 { | |
7271 char *fontcopy = strdup(fontname); | |
7272 char *name = strchr(fontcopy, '.'); | |
7273 NSFont *font = nil; | |
7274 | |
7275 if(name) | |
7276 { | |
7277 int size = atoi(fontcopy); | |
7278 *name = 0; | |
7279 name++; | |
7280 font = [NSFont fontWithName:[ NSString stringWithUTF8String:name ] size:(float)size]; | |
7281 } | |
7282 free(fontcopy); | |
7283 return font; | |
7284 } | 7316 } |
7285 | 7317 |
7286 /* Allows the user to choose a font using the system's font chooser dialog. | 7318 /* Allows the user to choose a font using the system's font chooser dialog. |
7287 * Parameters: | 7319 * Parameters: |
7288 * currfont: current font | 7320 * currfont: current font |