Page 1 of 1

DrawText_() API bug??

Posted: Sun Apr 23, 2006 8:04 pm
by Trond
As you can see, tabs aren't expanded even with the #DT_EXPANDTABS flag. But the measurement takes this into account! Is this a bug in the api command? (Or what did I miss?)

Code: Select all

OpenWindow(0, 0, 0, 640, 480, "", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
CreateGadgetList(WindowID(0))
CreateImage(0, 300, 300)
hDC = StartDrawing(ImageOutput(0))
  Box(0, 0, 300, 300, #White)
  
  SelectObject_(hDC, GetStockObject_(#DEFAULT_GUI_FONT))
  SelectObject_(hDC, GetStockObject_(#HOLLOW_BRUSH))
  
  Text.s = "Test text!" + #TAB$ + "Ctrl+S"
  Rect.RECT
  DrawText_(hDC, Text, Len(Text), @Rect, #DT_CALCRECT | #DT_EXPANDTABS)
  DrawText_(hDC, Text, Len(Text), @Rect, #DT_EXPANDTABS)
  Box(0, 0, Rect\Right, Rect\Bottom)
  
StopDrawing()
ImageGadget(0, 10, 10, 100, 100, ImageID(0))


Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Break
  EndSelect
ForEver

Posted: Sun Apr 23, 2006 9:21 pm
by srod
Seems to be a problem with the device context returned by StartDrawing().

The following works by creating a memory dc and selecting the image into it.

Code: Select all

OpenWindow(0, 0, 0, 640, 480, "", #PB_Window_ScreenCentered | #PB_Window_SystemMenu) 
CreateGadgetList(WindowID(0)) 
CreateImage(0, 300, 300) 

  wdc=GetDC_(WindowID(0))
  hdc=CreateCompatibleDC_(wdc) 
  SelectObject_(hdc,ImageID(0))   
  SelectObject_(hDC, GetStockObject_(#DEFAULT_GUI_FONT)) 
  SelectObject_(hDC, GetStockObject_(#HOLLOW_BRUSH)) 
  
  Text.s = "Test text!" + #TAB$ + "Ctrl+S" 
  DrawText_(hDC, Text, Len(Text), @Rect.rect, #DT_CALCRECT | #DT_EXPANDTABS) 
  DrawText_(hDC, Text, Len(Text), @Rect, #DT_EXPANDTABS) 
  DeleteDC_(hdc)
  ReleaseDC_(WindowID(0),wdc)
  ImageGadget(0, 10, 10, 100, 100, ImageID(0)) 

Repeat 
  Select WaitWindowEvent() 
    Case #PB_Event_CloseWindow 
      Break 
  EndSelect 
ForEver
I must admit to not understanding why your original code doesn't work; except to think that there must be something with the original device context!

Posted: Sun Apr 23, 2006 9:50 pm
by va!n
Just take a look to the API command TabbedTextOut_ maybe this will solve the problem? Btw, a small note about the #DT_TABSTOP flag! This flag will set the number of cahrs for each TabStop to the hight-byte. (trying to understand how to get this work). However, maybe the tip with using another API call may help you?

Code: Select all

OpenWindow(0, 0, 0, 640, 480, "", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
CreateGadgetList(WindowID(0))
CreateImage(0, 300, 300)

  wdc=GetDC_(WindowID(0))
  hdc=CreateCompatibleDC_(wdc)
  SelectObject_(hdc,ImageID(0))   
  SelectObject_(hDC, GetStockObject_(#DEFAULT_GUI_FONT))
  SelectObject_(hDC, GetStockObject_(#HOLLOW_BRUSH))
 
  Text.s = "Test text!" + #TAB$ + "Ctrl+S"
  DrawText_(hDC, Text, Len(Text), @Rect.rect, #DT_CALCRECT | #DT_EXPANDTABS)
; DrawText_(hDC, Text, Len(Text), @Rect, #DT_EXPANDTABS)
  
  pDC = TabbedTextOut_(hDC,40,10,Text.s,Len(Text),1,0,7)
  DeleteDC_(hdc)
  ReleaseDC_(WindowID(0),wdc)
  ImageGadget(0, 10, 10, 100, 100, ImageID(0))

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Break
  EndSelect
ForEver

Posted: Sun Apr 23, 2006 9:57 pm
by srod
I tried this one with the same problem. Everything works fine if avoiding the hdc returned from StartDrawing().

btw, I would keep away from the #DT_TABSTOP flag as the value you set for the tab width (residing in the high byte of the low word) can clash with other #DT_ flags. To set the tab width, DrawTextEx_() is probably the easiest function to use and uses a formatting rectangle - unlike TabbedTextOut_().

Posted: Mon Apr 24, 2006 6:09 pm
by Trond
Thank you both for looking at my problem. I want to use DrawText_() because of the formatting rectangle and its measurement capabilities.

Posted: Mon Apr 24, 2006 7:53 pm
by ABBKlaus
The text alignment mode for the device context must include the TA_LEFT, TA_TOP, and TA_NOUPDATECP flags.

Code: Select all

OpenWindow(0, 0, 0, 640, 480, "", #PB_Window_ScreenCentered | #PB_Window_SystemMenu) 
CreateGadgetList(WindowID(0)) 
CreateImage(0, 300, 300) 
hDC = StartDrawing(ImageOutput(0)) 
  SetTextAlign_(hDC,#TA_LEFT|#TA_TOP|#TA_NOUPDATECP)
  Box(0, 0, 300, 300, #White) 
  
  SelectObject_(hDC, GetStockObject_(#DEFAULT_GUI_FONT)) 
  SelectObject_(hDC, GetStockObject_(#HOLLOW_BRUSH)) 
  Text.s = "Test text!" + #TAB$ + "Ctrl+S" 
  Rect.RECT 
  DrawText_(hDC, Text, Len(Text), @Rect, #DT_CALCRECT | #DT_EXPANDTABS) 
  DrawText_(hDC, Text, Len(Text), @Rect, #DT_LEFT|#DT_EXPANDTABS) 
  Box(0, 0, Rect\Right, Rect\Bottom) 
  
StopDrawing() 
ImageGadget(0, 10, 10, 100, 100, ImageID(0)) 


Repeat 
  Select WaitWindowEvent() 
    Case #PB_Event_CloseWindow 
      Break 
  EndSelect 
ForEver

Posted: Mon Apr 24, 2006 8:02 pm
by Trond
Where did you read that?

Edit:
The default values are TA_LEFT, TA_TOP, and TA_NOUPDATECP.
I shouldn't need to set that since it's the default.

Posted: Mon Apr 24, 2006 11:02 pm
by ABBKlaus
i read it in the DrawText API reference http://msdn.microsoft.com/library/en-us ... t_0odw.asp
i guess the default is TA_LEFT and TA_TOP but not TA_NOUPDATECP !
Where did you read that ?

But did it solve your problem :?:

Posted: Tue Apr 25, 2006 6:48 pm
by Trond
ABBKlaus wrote:i read it in the DrawText API reference http://msdn.microsoft.com/library/en-us ... t_0odw.asp
i guess the default is TA_LEFT and TA_TOP but not TA_NOUPDATECP !
Where did you read that ?

But did it solve your problem :?:
I read it in the SetTextAlign API reference http://msdn.microsoft.com/library/en-us ... frame=true .

It sort of did solve my problem, but not completely. The real problem was with my owner-drawn menus. The width calculation seems to return too much if I measure a string with a tab character. So I made this to test if that actually was the case. And then I ran into this little problem.

Posted: Tue Apr 25, 2006 8:30 pm
by ABBKlaus
the default after StartDrawing() is TA_LEFT / TA_TOP / TA_UPDATECP.
So i think you have to ask Fred how he initializes it :twisted:

Code: Select all

;TA=1 is the default after GetTextAlign_(hDC)
TA=GetTextAlign_(hDC)

Select TA & 6 ; LEFT / RIGHT / CENTER
  Case #TA_CENTER ; 6
    Debug "#TA_CENTER"
  Case #TA_RIGHT ; 2
    Debug "#TA_RIGHT"
  Default
    Debug "#TA_LEFT"
EndSelect

Select TA & 24
  Case #TA_BOTTOM ; 8
    Debug "#TA_BOTTOM"
  Case #TA_BASELINE ; 24
    Debug "#TA_BASELINE"
  Default
    Debug "#TA_TOP"
EndSelect

Select TA & 1
  Case #TA_UPDATECP
    Debug "#TA_UPDATECP"
  Default
    Debug "#TA_NOUPDATECP"
EndSelect
[edit] wrong code sorry :oops: