From: Luc Moulinier on
Hello !

I'm writing a new widget using the Tk C API. For Windows porting, I
need to change the font in the current DC. Looking at the Tk source
code, I found that :

hdc = TkWinGetDrawableDC(display,d,&state);
fontPtr = (WinFont *) squarePtr->tkfont;
lastSubFontPtr = &fontPtr->subFontArray[0];
SelectObject(hdc, lastSubFontPtr->hFont);
.....
DrawText(hdc, lpchText, length, &rc, DT_CENTER);
TkWinReleaseDrawableDC(d, hdc, &state);

should do the job. But wish crashes at the SelectObject level. I
checked that squarePtr->tkfont is well initialized. If I remove the
SelectObject statement, the text is printed nicely, but ... not with
the font I want !

Any clue ?
luc
From: Bezoar on
On Jul 20, 10:18 am, Luc Moulinier <luc.moulin...(a)igbmc.fr> wrote:
> Hello !
>
> I'm writing a new widget using the Tk C API. For Windows porting, I
> need to change the font in the current DC. Looking at the Tk source
> code, I found that :
>
>   hdc        = TkWinGetDrawableDC(display,d,&state);
>   fontPtr = (WinFont *) squarePtr->tkfont;
>   lastSubFontPtr = &fontPtr->subFontArray[0];
>   SelectObject(hdc, lastSubFontPtr->hFont);
>    .....
>   DrawText(hdc, lpchText, length, &rc, DT_CENTER);
>   TkWinReleaseDrawableDC(d, hdc, &state);
>
>   should do the job. But wish crashes at the SelectObject level. I
> checked that squarePtr->tkfont is well initialized. If I remove the
> SelectObject statement, the text is printed nicely, but ... not with
> the font I want !
>
> Any clue ?
> luc

Its been a while since I looked at C but which has higher precedence
the & or the -> . In the statement &fontPtr->subFontArray[0] are you
getting (&fontPtr)->subFontArray[0] ) or are you getting &(fontPtr-
>subFontArray[0]) . I believe you want the latter by the names of
the variables. Since its the first element why not use fontPtr-
>subFontArray
instead of &(fontPtr->subFontArray[0]) . If you are pointing to the
wrong place when
setting lastSubFontPtr then you wont get to the hfont member.
From: jr4412 on
On Jul 21, 3:29 am, Bezoar <cwjo...(a)gmail.com> wrote:
> On Jul 20, 10:18 am, Luc Moulinier <luc.moulin...(a)igbmc.fr> wrote:
>
>
>
> > Hello !
>
> > I'm writing a new widget using the Tk C API. For Windows porting, I
> > need to change the font in the current DC. Looking at the Tk source
> > code, I found that :
>
> >   hdc        = TkWinGetDrawableDC(display,d,&state);
> >   fontPtr = (WinFont *) squarePtr->tkfont;
> >   lastSubFontPtr = &fontPtr->subFontArray[0];
> >   SelectObject(hdc, lastSubFontPtr->hFont);
> >    .....
> >   DrawText(hdc, lpchText, length, &rc, DT_CENTER);
> >   TkWinReleaseDrawableDC(d, hdc, &state);
>
> >   should do the job. But wish crashes at the SelectObject level. I
> > checked that squarePtr->tkfont is well initialized. If I remove the
> > SelectObject statement, the text is printed nicely, but ... not with
> > the font I want !
>
> > Any clue ?
> > luc
>
> Its been a while since I looked at C but which has higher precedence
> the & or the -> . In the statement &fontPtr->subFontArray[0] are you
> getting  (&fontPtr)->subFontArray[0] ) or are you getting &(fontPtr->subFontArray[0]) .   I believe you want the latter by the names of
>
> the variables.  Since its the first element why not use fontPtr->subFontArray
>
> instead of &(fontPtr->subFontArray[0]) . If you are pointing to the
> wrong place when
> setting lastSubFontPtr then you wont get to the hfont member.

order of precedence: [], ->, &. I'd say your "&(fontPtr-
>subFontArray[0])" interpretation is correct.
From: Luc Moulinier on
Thnaks for answering.

I've checked your suggestions, but it doesn't change anything, Wish
still crashes. I don't thing it's the C syntax taht is wrong, it comes
from the Tk sources directly...
After investigating a little bit more, it seems to me (but I'm not an
expert at all !!) that it has something to do with 1) tkfont to
windows font, 2) or the windws DC.

I've checked that my tkfont is properly set (checked with issuing
Tk_NameOfFont, and metrics) The font is allocated through
Tk_AllocFontFromObj (font is Courier BTW).
The DC is defined by call ti TkWinGetDrawalbeDC. As said above, I can
draw onto this DC, but not assign font. I'm sure I miss something but
what ??!!??
luc
From: Luc Moulinier on
Actually, I corrected the code , but the behavior remains the same :

hdc = TkWinGetDrawableDC(display,d,&state);
fontPtr = (WinFont *) squarePtr->tkfont;
lastSubFontPtr = &fontPtr->subFontArray[0];
oldFont = SelectObject(hdc, lastSubFontPtr->hFont);
.....
DrawText(hdc, lpchText, length, &rc, DT_CENTER);
SelectObject(hdc, oldFont);
TkWinReleaseDrawableDC(d, hdc, &state);