Author |
Topic: Font selection dialogue (Read 2105 times) |
|
Nick
New Member
member is offline


Gender: 
Posts: 33
|
 |
Font selection dialogue
« Thread started on: Sep 1st, 2011, 1:58pm » |
|
I am 'playing' with the TEXTEDIT.BBC file in the examples library. The code which initialises the font selection dialogue is:
REM. Create and initialise data structures for dialogue boxes: DIM Fn% 255, Lf% 59 (snipped...)
DIM cf{lStructSize%, hwndOwner%, hdc%, lpLogFont%, \ \ iPointSize%, flags%, rgbColors%, lCustData%, \ \ lpfnHook%, lpTemplateName%, hInstance%, lpszStyle%, \ \ nFontType{l&, h&}, pad{l&, h&}, nSizeMin%, nSizeMax%} cf.lStructSize% = DIM(cf{}) cf.hwndOwner% = @hwnd% cf.lpLogFont% = Lf% cf.flags% = 1
********************************************** **********************************************
The font selection dialogue opens fine, EXCEPT that it should display the current font being used in the edit control. (It presently has a blank in the font name for example)
I have tried to understand the way to send initial flags and values (particularly cf.lpLogFont%) to the dialogue box, but all my attempts have not worked. (yes, I have set the CF_INITTOLOGFONTSTRUCT flag...)
Thanks
Nick
From MSDN:
1.To specify the initial values for the Font, Font Style, Size, Strikeout, and Underline dialog box controls:
2.Set the CF_INITTOLOGFONTSTRUCT flag in the Flags member, along with members of the LOGFONT structure that is pointed to by the lpLogFont, to specify the initial values for the font attributes.
|
|
Logged
|
|
|
|
admin
Administrator
member is offline


Posts: 1145
|
 |
Re: Font selection dialogue
« Reply #1 on: Sep 1st, 2011, 3:47pm » |
|
on Sep 1st, 2011, 1:58pm, Nick wrote:The font selection dialogue opens fine, EXCEPT that it should display the current font being used in the edit control. |
|
To initialise the font selection to the current font you can do:Get the handle of the current font by sending a WM_GETFONT message to the edit control. Fill in a LOGFONT structure from the font handle by calling the GetObject API. Initialise the ChooseFont dialogue by pointing the lpLogFont member at your LOGFONT structure and setting the CF_INITTOLOGFONTSTRUCT flag. Richard.
|
|
Logged
|
|
|
|
Nick
New Member
member is offline


Gender: 
Posts: 33
|
 |
Re: Font selection dialogue
« Reply #2 on: Sep 1st, 2011, 4:40pm » |
|
on Sep 1st, 2011, 3:47pm, Richard Russell wrote:Fill in a LOGFONT structure from the font handle by calling the GetObject API. |
|
Bear with me Richard - I am struggling to 'translate' what I read in the winapi with the BBC4W syntax, and I have probably been staring at this for too long!
DEF PROCtryitout LOCAL F%,R% SYS "SendMessage", hRichEdit%, WM_GETFONT, 0, 0 TO F% DIM Lf_temp% 59 DIM cf_temp{lStructSize%, hwndOwner%, hdc%, lpLogFont%, \ \ iPointSize%, flags%, rgbColors%, lCustData%, \ \ lpfnHook%, lpTemplateName%, hInstance%, lpszStyle%, \ \ nFontType{l&, h&}, pad{l&, h&}, nSizeMin%, nSizeMax%} cf_temp.flags%=CF_EFFECTS OR CF_INITTOLOGFONTSTRUCT OR CF_NOSCRIPTSEL OR 1 cf_temp.lStructSize% = DIM(cf{}) cf_temp.hwndOwner% = hRichEdit% cf_temp.lpLogFont% = Lf_temp% SYS "SendMessage", hRichEdit%, WM_GETFONT, 0, 0 TO F% SYS "GetObject", (F%) TO cf_test{} cf.lpLogFont% = cf_temp.lpLogFont% SYS "ChooseFont", cf{} TO R% REM etcetera with font dialogue open.... ENDPROC
It's the red line that bombs out ( "Bad use of structure" )
I probably need to learn something fundamental here about 'piping' system call results into BBC arrays!

Thanks
Nick
|
« Last Edit: Sep 1st, 2011, 4:50pm by Nick » |
Logged
|
|
|
|
admin
Administrator
member is offline


Posts: 1145
|
 |
Re: Font selection dialogue
« Reply #3 on: Sep 2nd, 2011, 08:46am » |
|
on Sep 1st, 2011, 4:40pm, Nick wrote:I am struggling to 'translate' what I read in the winapi with the BBC4W syntax |
|
Not really, the mistake is more fundamental than one of syntax. The GetObject API takes three parameters, yet in your code you only give it one parameter! Check the MSDN page for GetObject here:
http://msdn.microsoft.com/en-us/library/dd144904.aspx
The three parameters are hgdiobj, cbBuffer and lpvObject; if you provide them it will work fine.
Richard.
|
|
Logged
|
|
|
|
Nick
New Member
member is offline


Gender: 
Posts: 33
|
 |
Re: Font selection dialogue
« Reply #4 on: Sep 4th, 2011, 10:37am » |
|
on Sep 2nd, 2011, 08:46am, Richard Russell wrote:Not really, the mistake is more fundamental than one of syntax. The GetObject API takes three parameters, yet in your code you only give it one parameter! |
|
OK - thanks. The perils of working on something late at night!
It now works fine - in authoring mode. BUT when I compile and run the programme, the results are different! I set up my own LOGFONT structure ("MS_logfont" below). I initialise the rich edit control, then set the font using the following:
SYS "SendMessage", hRichEdit%, WM_GETFONT, 0, 0 TO F% IF F% SYS "DeleteObject", F% cf.lpLogFont%=MS_logfont SYS "CreateFontIndirect", cf.lpLogFont% TO F% PRINT TAB(0,5)" CreateFontIndirect result: "F% WAIT 100 SYS "SendMessage", hRichEdit%, WM_SETFONT, F%, 1
In authoring mode, the control appears (in some default font or other), then after 1 second it uses my chosen font. This is a very obvious change because I have set my default to 22 point. In other words, after 1 second you can see the font get bigger. If at that point you launch a select font dialogue, it correctly shows that the control is using my choice of initial font.
BUT when I run the compiled code, after one second the control font goes to something else (in fact smaller).
I put the PRINT statement in so I could see that the system in both cases returned a font object. I put the WAIT statement in to see if perhaps there was some timing slew that occurs at runtime - I wanted to see if introducing a delay made any difference. It doesn't!
By the way, I tried posting my code into the supplied TEXTEDIT.BBC file in the library - precisely the same behaviour was observed. This rules out all the additional code in my "Textbox" programme... I can send you the modified TEXTEDIT.BBC file if you like...
Is there something different about variables and system calls between authoring mode and runtime??
Thanks
Nick
|
« Last Edit: Sep 4th, 2011, 10:49am by Nick » |
Logged
|
|
|
|
admin
Administrator
member is offline


Posts: 1145
|
 |
Re: Font selection dialogue
« Reply #5 on: Sep 4th, 2011, 11:19am » |
|
on Sep 4th, 2011, 10:37am, Nick wrote:Is there something different about variables and system calls between authoring mode and runtime |
|
Not really, in fact the environments are virtually identical.
Unfortunately you don't show the code in which you initialise MS_logfont, which seems likely to be the source of the problem.
Quote:I can send you the modified TEXTEDIT.BBC file if you like |
|
Please do.
Richard.
|
|
Logged
|
|
|
|
Nick
New Member
member is offline


Gender: 
Posts: 33
|
 |
Re: Font selection dialogue
« Reply #6 on: Sep 4th, 2011, 1:26pm » |
|
on Sep 4th, 2011, 11:19am, Richard Russell wrote:Not really, in fact the environments are virtually identical.
Unfortunately you don't show the code in which you initialise MS_logfont, which seems likely to be the source of the problem. |
|
Almost certainly! But if that is the case, I would be grateful to know why the 'incorrectness' does not manifest itself in the SAME way in authoring and runtime...?
Anyway, the code for the modified TEXTEDIT.BBC is below..
Thanks!
Nick
REM. A simple text editor in BBC BASIC for Windows IF HIMEM <= PAGE+12288 THEN HIMEM = PAGE+10000 INSTALL @lib$+"WINLIB5" EM_GETMODIFY = &B8 EM_SETMODIFY = &B9 EM_SETSEL = &B1 EM_UNDO = &C7 ES_AUTOHSCROLL = &80 ES_MULTILINE = &4 WM_CLEAR = &303 WM_COPY = &301 WM_CUT = &300 WM_GETFONT = &31 WM_GETTEXT = &D WM_GETTEXTLENGTH = &E WM_PASTE = &302 WM_SETFONT = &30 WM_SETTEXT = &C WS_VSCROLL = &200000 REM. Set up menus: AM$ = "AppendMenu" SYS "CreatePopupMenu" TO H% SYS AM$, H%, 0, 20, "&New"+CHR$9+"Ctrl+N" SYS AM$, H%, 0, 21, "&Open"+CHR$9+"Ctrl+O" SYS AM$, H%, 0, 22, "&Save"+CHR$9+"Ctrl+S" SYS AM$, H%, 0, 23, "Save &As" SYS AM$, H%, &800, 0, 0 SYS AM$, H%, 0, 24, "E&xit" SYS "CreatePopupMenu" TO E% SYS AM$, E%, 0, 30, "&Undo"+CHR$9+"Ctrl+Z" SYS AM$, E%, &800, 0, 0 SYS AM$, E%, 0, 31, "Cu&t"+CHR$9+"Ctrl+X" SYS AM$, E%, 0, 32, "&Copy"+CHR$9+"Ctrl+C" SYS AM$, E%, 0, 33, "&Paste"+CHR$9+"Ctrl+V" SYS AM$, E%, 0, 34, "De&lete" SYS AM$, E%, &800, 0, 0 SYS AM$, E%, 0, 35, "Select &All"+CHR$9+"Ctrl+A" SYS "CreatePopupMenu" TO O% SYS AM$, O%, 0, 40, "Set &Font" SYS "CreateMenu" TO M% SYS "SetMenu", @hwnd%, M% SYS AM$, M%, 16, H%, "&File" SYS AM$, M%, 16, E%, "&Edit" SYS AM$, M%, 16, O%, "&Options" SYS "DrawMenuBar", @hwnd% REM. Create edit window: Hedit% = FN_createwindow("EDIT", "", 0, 0, @vdu.tr%, @vdu.tb%, 0, \ \ WS_VSCROLL + ES_AUTOHSCROLL + ES_MULTILINE, 0) SYS "SetWindowText", Hedit%, "This is some initial text" REM. Create keyboard shortcuts: nsc% = 4 DIM accel{(nsc%-1) fVirt&, pad&, key{l&,h&}, cmd{l&,h&}} accel{(0)}.key.l& = 14 : accel{(0)}.cmd.l& = 20 : REM Ctrl+N = New accel{(1)}.key.l& = 15 : accel{(1)}.cmd.l& = 21 : REM Ctrl+O = Open accel{(2)}.key.l& = 19 : accel{(2)}.cmd.l& = 22 : REM Ctrl+S = Save accel{(3)}.key.l& = 1 : accel{(3)}.cmd.l& = 35 : REM Ctrl+A = Select All SYS "CreateAcceleratorTable", accel{(0)}, nsc% TO haccel% @haccel% = haccel% @hwacc% = @hwnd% REM. Create and initialise data structures for dialogue boxes: DIM Fn% 255, Lf% 59 DIM fs{lStructSize%, hwndOwner%, hInstance%, lpstrFilter%, \ \ lpstrCustomFilter%, nMaxCustFilter%, nFilterIndex%, \ \ lpstrFile%, nMaxFile%, lpstrFileTitle%, \ \ nMaxFileTitle%, lpstrInitialDir%, lpstrTitle%, \ \ flags%, nFileOffset{l&, h&}, nFileExtension{l&, h&}, \ \ lpstrDefExt%, lCustData%, lpfnHook%, lpTemplateName%} ff$ = "TXT files"+CHR$0+"*.TXT"+CHR$0+"All files"+CHR$0+"*.*"+CHR$0+CHR$0 ex$ = "txt" fs.lStructSize% = DIM(fs{}) fs.hwndOwner% = @hwnd% fs.lpstrFilter% = !^ff$ fs.lpstrDefExt% = !^ex$ fs.lpstrFile% = Fn% fs.nMaxFile% = 256 fs.flags% = 6 DIM cf{lStructSize%, hwndOwner%, hdc%, lpLogFont%, \ \ iPointSize%, flags%, rgbColors%, lCustData%, \ \ lpfnHook%, lpTemplateName%, hInstance%, lpszStyle%, \ \ nFontType{l&, h&}, pad{l&, h&}, nSizeMin%, nSizeMax%} cf.lStructSize% = DIM(cf{}) cf.hwndOwner% = @hwnd% cf.lpLogFont% = Lf% cf.flags% = 1 PROCmakelogfontdefault REM. Set up 'interrupts': Menu% = 0 ON CLOSE IF FNchanged PROCexit ELSE RETURN ON MOVE PROCmove(@msg%, @wparam%, @lparam%) : RETURN ON SYS Menu% = @wparam% : RETURN ON ERROR SYS "MessageBox", @hwnd%, REPORT$, 0, 48 REM. Main loop: REPEAT K% = 0 : SWAP K%, Menu% CASE K% AND &FFFF OF WHEN 20: IF FNchanged PROCnew WHEN 21: IF FNchanged PROCload WHEN 22: IF FNsave PROCunchanged WHEN 23: IF FNsaveas PROCunchanged WHEN 24: IF FNchanged PROCexit WHEN 30: SYS "SendMessage", Hedit%, EM_UNDO, 0, 0 WHEN 31: SYS "SendMessage", Hedit%, WM_CUT, 0, 0 WHEN 32: SYS "SendMessage", Hedit%, WM_COPY, 0, 0 WHEN 33: SYS "SendMessage", Hedit%, WM_PASTE, 0, 0 WHEN 34: SYS "SendMessage", Hedit%, WM_CLEAR, 0, 0 WHEN 35: SYS "SendMessage", Hedit%, EM_SETSEL, 0, -1 WHEN 40: PROCfont ENDCASE PROC_setfocus(Hedit%) WAIT 1 UNTIL FALSE END DEF PROCexit PROC_closewindow(Hedit%) QUIT DEF PROCmove(M%, W%, L%) : PRIVATE S% : IF S% ENDPROC S% = TRUE IF M%=5 SYS "MoveWindow", Hedit%, 0, 0, L% AND &FFFF, L% >> 16, 1 S% = FALSE ENDPROC DEF PROCfont : LOCAL F%, R% SYS "ChooseFont", cf{} TO R% IF R% THEN SYS "SendMessage", Hedit%, WM_GETFONT, 0, 0 TO F% IF F% SYS "DeleteObject", F% SYS "CreateFontIndirect", cf.lpLogFont% TO F% SYS "SendMessage", Hedit%, WM_SETFONT, F%, 1 ENDIF ENDPROC DEF PROCnew : LOCAL F% ?Fn% = 0 : PROCtitle SYS "SendMessage", Hedit%, WM_SETTEXT, 0, ^F% ENDPROC DEF PROCload : LOCAL F%, L% SYS "GetOpenFileName", fs{} TO F% IF F% PROCtitle ELSE ENDPROC F% = OPENIN$$Fn% IF F% THEN L% = EXT#F% : CLOSE #F% SYS "GlobalAlloc", 0, L%+1 TO F% OSCLI "LOAD """+$$Fn%+""" "+STR$~F%+"+"+STR$~L% F%?L% = 0 SYS "SendMessage", Hedit%, WM_SETTEXT, 0, F% TO L% SYS "GlobalFree", F% IF L% = 0 ERROR 100, "File "+$$Fn%+" too big" ELSE ERROR 101, "Can't open file "+$$Fn% ENDIF ENDPROC DEF FNsaveas : LOCAL F%, L% SYS "GetSaveFileName", fs{} TO F% IF F% PROCtitle ELSE = FALSE DEF FNsave : LOCAL F%, L% : IF ?Fn% = 0 THEN = FNsaveas SYS "SendMessage", Hedit%, WM_GETTEXTLENGTH, 0, 0 TO L% SYS "GlobalAlloc", 0, L%+1 TO F% SYS "SendMessage", Hedit%, WM_GETTEXT, L%+1, F% OSCLI "SAVE """+$$Fn%+""" "+STR$~F%+"+"+STR$~L% SYS "GlobalFree", F% = TRUE DEF FNchanged : LOCAL R% SYS "SendMessage", Hedit%, EM_GETMODIFY, 0, 0 TO R% IF R% = 0 THEN = TRUE SYS "MessageBox", @hwnd%, "Save changes?", "TEXTEDIT", 35 TO R% IF R% = 6 IF FNs PROCunchanged : = TRUE IF R% = 7 PROCunchanged : = TRUE = FALSE DEF PROCtitle SYS "SetWindowText", @hwnd%, "TEXTEDIT - "+$$Fn% ENDPROC DEF PROCunchanged SYS "SendMessage", Hedit%, EM_SETMODIFY, 0, 0 ENDPROC DEF PROCmakelogfontdefault REM ************************* UPGRADE - include Font name, style etc as initialisable parameters REM from the main application later... REM Creates a LOGFONT structure in MS_logfont for initialising the dialog box REM At the end, MS_logfont is a pointer to the start of the memory block for MS_logfont REM This is a really dumb way to do it, but time is pressing... REM DATA for Microsoft Sans Serif 22 regular REM This data came from a WM_GETFONT call when the control is configured for MSSF regular 22... DIM MS_logfont 59 LOCAL v%,ptr% DATA &E3,&FF,&FF,&FF,0,0,0,0,0,0,0,0,0,0,0,0,&90,&1,0,0,0,0,0,0,&3,&2,\ \&1,&22,&4D,&69,&63,&72,&6F,&73,&6F,&66,&74,&20,&53,&61,&6E,&73,&20,&53,&65, \ \&72,&69,&66,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ptr%=MS_logfont FOR x=0 TO 59: READ v%: ptr%?x=v% : NEXT SYS "SendMessage",Hedit%, WM_GETFONT, 0, 0 TO F% IF F% SYS "DeleteObject", F% cf.lpLogFont%=MS_logfont SYS "CreateFontIndirect", cf.lpLogFont% TO F% PRINT TAB(0,5)" CreateFontIndirect result: "F% WAIT 100 SYS "SendMessage", Hedit%, WM_SETFONT, F%, 1 SYS "SetWindowText", Hedit%, "This is some initial text at the end of PROCmakelogfontdefault" ENDPROC
|
|
Logged
|
|
|
|
admin
Administrator
member is offline


Posts: 1145
|
 |
Re: Font selection dialogue
« Reply #7 on: Sep 4th, 2011, 2:46pm » |
|
on Sep 4th, 2011, 1:26pm, Nick wrote:Almost certainly! But if that is the case, I would be grateful to know why the 'incorrectness' does not manifest itself in the SAME way in authoring and runtime...? |
|
You are not permitted to split a DATA statement between multiple lines using the line continuation character '\'. This is explicitly stated in the documentation:
Quote:You can use the line continuation character almost anywhere that a space would be allowed, but not:
within a quoted (constant) string in a star command (use OSCLI instead) in a REM statement in a DATA statement immediately after the initial keyword of a statement |
|
I am surprised that you didn't receive an error message, but it is absolutely not compatible with compiling/crunching.
Anyway, I can hardly think of a more horrible thing than initialising a LOGFONT structure from a load of constants in a DATA statement! This is how I would do it:
Code: DIM logfont{lfHeight%, lfWidth%, lfEscapement%, lfOrientation%, lfWeight%, \
\ lfItalic&, lfUnderline&, lfStrikeOut&, lfCharSet&, lfOutPrecision&, \
\ lfClipPrecision&, lfQuality&, lfPitchAndFamily&, lfFaceName&(31)}
logfont.lfHeight% = -29
logfont.lfWeight% = FW_NORMAL
logfont.lfOutPrecision& = OUT_STROKE_PRECIS
logfont.lfClipPrecision& = CLIP_STROKE_PRECIS
logfont.lfQuality& = DRAFT_QUALITY
logfont.lfPitchAndFamily& = FF_SWISS OR VARIABLE_PITCH
logfont.lfFaceName&() = "Microsoft Sans Serif" That actually conveys some meaning!
Richard.
|
|
Logged
|
|
|
|
Nick
New Member
member is offline


Gender: 
Posts: 33
|
 |
Re: Font selection dialogue
« Reply #8 on: Sep 4th, 2011, 5:53pm » |
|
on Sep 4th, 2011, 2:46pm, Richard Russell wrote:You are not permitted to split a DATA statement between multiple lines using the line continuation character '\'. This is explicitly stated in the documentation: |
|
Yet again, "RTFM!"....thanks.
Quote:Anyway, I can hardly think of a more horrible thing than initialising a LOGFONT structure from a load of constants in a DATA statement! |
|
Also very much agreed!
(I did REM to myself; "This is a really dumb way to do it, but time is pressing..."). But it was part of my learning curve - I intended to come back and do redo that bit - at least for now, I could be sure of the values being passed.
Remember, I am not used to this level of access to variables - it is truly amazing. (The last time I got deeply involved using BBC Basic was 25 years ago!)
I am still gtting my head (back) around the different indirection operators. I wanted to get the functionality (vaguely) right before coming back to make things more orthodox!
This now works perfectly - thank you so much.
Nick
|
|
Logged
|
|
|
|
|