Page 1 of 1
Making a gadget have a bold font
Posted: Fri Sep 02, 2005 5:59 am
by PB
Code updated for 5.20+
Thanks to Sparkie and Xombie for providing the original code upon which this procedure is derived.
Here's my procedure that works after checking the rest of this thread for problems.
Code: Select all
Procedure SetGadgetFontStyle(gad,bold,italic,underline,strikeout)
font=SendMessage_(GadgetID(gad),#WM_GETFONT,0,0) : GetObject_(font,SizeOf(LOGFONT),lg.LOGFONT)
If bold=1 : lg\lfWeight=#FW_BOLD : EndIf : lg\lfItalic=italic : lg\lfUnderline=underline : lg\lfStrikeOut=strikeout
font=CreateFontIndirect_(lg) : SendMessage_(GadgetID(gad),#WM_SETFONT,font,1)
EndProcedure
If OpenWindow(0,300,250,300,200,"test",#PB_Window_SystemMenu)
FrameGadget(1,10,10,200,150,"This will be italic") : SetGadgetFontStyle(1,0,1,0,0)
ButtonGadget(2,30,50,120,25,"And this is bold") : SetGadgetFontStyle(2,1,0,0,0)
TextGadget(3,30,100,120,25,"And this is a mix!") : SetGadgetFontStyle(3,1,1,1,1)
Repeat : Until WaitWindowEvent()=#PB_Event_CloseWindow
EndIf
Posted: Fri Sep 02, 2005 6:27 am
by dagcrack
Very nice! Thanks for sharing!

Posted: Fri Sep 02, 2005 8:12 am
by Xombie
Yeah, it is nice

I was looking at the title and thought, "Sweet - this will help me out a bit in my project" and then read that you got some code ideas from something I did. And I'm thinking... what? Where?
Thanks for the code ^_^ Not surprised to see Sparkie being credited for something but it's odd seeing myself credited

Posted: Fri Sep 02, 2005 8:29 am
by PB
>
read that you got some code ideas from something I did. And I'm
> thinking... what? Where?
From here:
viewtopic.php?t=12853
All I did was modify your "frankenstein-ed" function to make it work. 
Posted: Fri Sep 02, 2005 7:12 pm
by utopiomania
Thanks for sharing! It seems to solve one problem I stumbled over in my current project, which
is to highligt some text without messing with users preferred system fonts and size!
However:
Code: Select all
r=LoadFont(#PB_Any,PeekS(@lf\lfFacename),lf\lfHeight,#PB_Font_Bold)
Nothing happens if I change the flag (=,#PB_Font_
Bold) to
_Italic, or
_Underline, or if I remove it altogether?? from the function call . Why is that ??

Posted: Fri Sep 02, 2005 7:47 pm
by Trond
Not to speak about the font face and font size. The font gets set to MS Sans Serif 10 bold. No matter what font face or size the gadget had before and no matter what you substitute #PB_Font_Bold with. I thought that point was to preserve the existing font?
Here is a way that is more customizable, and also portable:
Code: Select all
Procedure MakeGadgetItalic(gadget)
Static font
If IsFont(font) = 0
font = LoadFont(#PB_Any, "Tahoma", 10, #PB_Font_Italic)
EndIf
UseFont(font)
SetGadgetFont(gadget, FontID())
EndProcedure
If OpenWindow(1,300,250,400,200,#PB_Window_SystemMenu,"Window")
CreateGadgetList(WindowID())
Frame3DGadget(1,10,10,200,100,"This will be italic") : MakeGadgetItalic(1)
ButtonGadget(2,30,50,120,25,"And so will this") : MakeGadgetItalic(2)
Repeat : Until WaitWindowEvent()=#PB_EventCloseWindow
EndIf
Posted: Sat Sep 03, 2005 7:17 am
by PB
> The font gets set to MS Sans Serif 10 bold
Eek, you're right!
> I thought that point was to preserve the existing font?
That
was the intention, but as I didn't test it with anything else, I thought it
was preserving the current gadget font. Not so. Hmm, sorry about that, all.
I will have a bit of a play with it and see what comes.

Posted: Sat Sep 03, 2005 3:13 pm
by Sub-Routine
I use this to keep the current font:
Code: Select all
FontID3 = LoadFont(3, "", "9", #PB_Font_Bold)
A size does have to be specified, or it comes out really tiny
Rand
Posted: Sat Sep 03, 2005 3:21 pm
by PB
> I use this to keep the current font:
> FontID3 = LoadFont(3, "", "9", #PB_Font_Bold)
Awesome!

Now
that's what we were looking for!

Posted: Sat Sep 03, 2005 7:23 pm
by utopiomania
No, not if I have to specify a font size. I need to add the bold attribute to whatever font and size
the user has selected as his/hers system default.

Posted: Sat Sep 03, 2005 7:33 pm
by Sub-Routine
I was disappointed that I have to specify a size, but since they have to fit a gadget and I am sizing the gadget...
Shouldn't there be a Registry entry for the default font?
Rand
Posted: Mon Sep 05, 2005 6:12 pm
by utopiomania
I played around a bit with PB's code and got this to work the way I want to. (but have no idea why it works

)
Code: Select all
Procedure MakeGadgetBold(gad)
lf.LOGFONT : cur=SendMessage_(GadgetID(gad),#WM_GETFONT,0,0)
GetObject_(cur,SizeOf(LOGFONT),@lf.LOGFONT)
If LoadFont(#PB_Any,PeekS(@lf\lfFacename),lf\lfHeight,#PB_Font_Underline)
SetGadgetFont(gad,FontID())
EndIf
EndProcedure
or
.
.
Procedure SetGadgetFontAttrib(Id,Flag)
Lf.LOGFONT
GetObject_(SendMessage_(GadgetID(Id),#WM_GETFONT,0,0),SizeOf(LOGFONT),@Lf.LOGFONT)
If LoadFont(#PB_Any,PeekS(@Lf\lfFacename),Lf\lfHeight,Flag)
SetGadgetFont(Id,FontID())
EndIf
EndProcedure
Posted: Tue Sep 06, 2005 12:05 pm
by PB
Thanks utopiomania, works great!

Posted: Tue Sep 06, 2005 1:48 pm
by Trond
Umm, I don't want to destroy the fun for you, but it doesn't preserve the font style. In other words, you can't have two procedures, one for setgadgetitalic and one for setgadgetbold and use these on the same gadget to get both bold and italic text.
Posted: Tue Sep 06, 2005 1:59 pm
by PB
I know, which is why utopiomania gave the second Procedure which lets you
set many styles with one call.
