COCOA quests?

Mac OSX specific forum
mestnyi
Addict
Addict
Posts: 851
Joined: Mon Nov 25, 2013 6:41 am

Re: COCOA quests?

Post by mestnyi »

Thank you, it works now, but there is one problem, I updated the previous post, see what can be done.
mestnyi
Addict
Addict
Posts: 851
Joined: Mon Nov 25, 2013 6:41 am

Re: COCOA quests?

Post by mestnyi »

I understand that no one knows how to get the height of the drawn text using cocoa?
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3728
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: COCOA quests?

Post by wilbert »

mestnyi wrote:I understand that no one knows how to get the height of the drawn text using cocoa?
It's already in the code you posted.

Code: Select all

CocoaMessage(@Size, NSString, "sizeWithAttributes:", Attributes)
macOS 10.15 Catalina, Windows 10
mestnyi
Addict
Addict
Posts: 851
Joined: Mon Nov 25, 2013 6:41 am

Re: COCOA quests?

Post by mestnyi »

And why does it return the same result if the font is large or small?

Code: Select all

Procedure stringHeight(string.s)
  Protected NSSize.NSSize, Attributes, NSDictionary
  
  NSString = CocoaMessage(0, 0, "NSString stringWithString:$", @string)
  
  ;NSDictionary = CocoaMessage(0, 0, "NSDictionary dictionaryWithObject:",  DrawingFont, "forKey:$", @"NSFontAttributeName")
  ;CocoaMessage(NSSize, NSString, "sizeWithAttributes:", NSDictionary)
  
  CocoaMessage(@Attributes, 0, "NSMutableDictionary dictionaryWithCapacity:", 2)
  CocoaMessage(@NSSize, NSString, "sizeWithAttributes:", Attributes)
    
  ProcedureReturn NSSize\height
EndProcedure

LoadFont(1, "Lucida Grande", 12)
LoadFont(2, "Helvetica Neue", 12)

If OpenWindow(0, 0, 0, 200, 200, "2DDrawing Example", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    If CreateImage(0, 200, 200, 24, $FFFFFF) And StartDrawing(ImageOutput(0))
        
      DrawingFont(FontID(1))
      DrawText(30,50, "PUREBASIC")
      Debug TextHeight("a") ; 16
      
      DrawingFont(FontID(2))
      DrawText(100,50, "PUREBASIC")
      Debug TextHeight("a") ; 18
      
      ;-----
      
      DrawingFont(FontID(1))
      DrawText(30,80, "PUREBASIC")
      Debug StringHeight("a") ; 16
      
      DrawingFont(FontID(2))
      DrawText(100,80, "PUREBASIC")
      Debug StringHeight("a") ; 
      
      StopDrawing() 
      ImageGadget(0, 0, 0, 200, 200, ImageID(0))      
    EndIf
    
    Repeat
      Event = WaitWindowEvent()
    Until Event = #PB_Event_CloseWindow
  EndIf
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3728
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: COCOA quests?

Post by wilbert »

mestnyi wrote:And why does it return the same result if the font is large or small?

Code: Select all

Procedure StringHeight(String.s)
  Protected NSString, Attributes, NSSize.NSSize
  NSString = CocoaMessage(0, 0, "NSString stringWithString:$", @String)
  Attributes = CocoaMessage(0, 0, "NSDictionary dictionaryWithObject:", DrawingFont, "forKey:$", @"NSFont")
  CocoaMessage(@NSSize, NSString, "sizeWithAttributes:", Attributes)
  ProcedureReturn NSSize\height
EndProcedure
macOS 10.15 Catalina, Windows 10
mestnyi
Addict
Addict
Posts: 851
Joined: Mon Nov 25, 2013 6:41 am

Re: COCOA quests?

Post by mestnyi »

Code: Select all

CocoaMessage(0, 0, "NSDictionary dictionaryWithObject:",  DrawingFont, "forKey:$", @"NSFontAttributeName")
So it didn’t work because of him. :|
But it is not much faster than the built-in.
Why height is fixed regardless of text?
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3728
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: COCOA quests?

Post by wilbert »

mestnyi wrote:

Code: Select all

CocoaMessage(0, 0, "NSDictionary dictionaryWithObject:",  DrawingFont, "forKey:$", @"NSFontAttributeName")
So it didn’t work because of him. :|
Yes, that's right.
mestnyi wrote:Why height is fixed regardless of text?
I'm not sure I understand what you mean.
Does Windows return the real size of the glyphs so a "g" returns a bigger height compared to an "o" ?
macOS 10.15 Catalina, Windows 10
mestnyi
Addict
Addict
Posts: 851
Joined: Mon Nov 25, 2013 6:41 am

Re: COCOA quests?

Post by mestnyi »

This was a general question that I do not understand. :)
Yes, I mean, why does textheight () have a parameter
if the return value is independent of the input parameter
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3728
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: COCOA quests?

Post by wilbert »

mestnyi wrote:Yes, I mean, why does textheight () have a parameter
if the return value is independent of the input parameter
It isn't independent. It's useful for multiline text.

Code: Select all

Debug TextHeight("a"+#CRLF$+"b")
macOS 10.15 Catalina, Windows 10
mestnyi
Addict
Addict
Posts: 851
Joined: Mon Nov 25, 2013 6:41 am

Re: COCOA quests?

Post by mestnyi »

what is it for mac os who knows?

Code: Select all

Define Message.MSG
While GetMessage_( Message, 0, 0, 0 )
  Select Message
      
  EndSelect
Wend
mestnyi
Addict
Addict
Posts: 851
Joined: Mon Nov 25, 2013 6:41 am

Re: COCOA quests?

Post by mestnyi »

mestnyi wrote:what is it for mac os who knows?

Code: Select all

Define Message.MSG
While GetMessage_( Message, 0, 0, 0 )
  Select Message
      
  EndSelect
Wend
Help me find a replacement for this cocoa remedy.

Code: Select all

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3728
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: COCOA quests?

Post by wilbert »

mestnyi wrote:Help me find a replacement for this cocoa remedy.

Code: Select all

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
Can you explain a bit more ?
I don't understand what you are looking for.
macOS 10.15 Catalina, Windows 10
mestnyi
Addict
Addict
Posts: 851
Joined: Mon Nov 25, 2013 6:41 am

Re: COCOA quests?

Post by mestnyi »

wilbert wrote:
mestnyi wrote:Help me find a replacement for this cocoa remedy.

Code: Select all

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
Can you explain a bit more ?
I don't understand what you are looking for.
Of course I will try to explain.
Since Fred forbade the use of such a construction

Code: Select all

Procedure func()
Protected event
 Repeat 
   event = WaitWindowEvent() 
 Until event = #PB_Event_CloseWindow
EndProcedure

BindGadgetEvent(#gadget, @func())
in window i could do like this

Code: Select all

Procedure func()
Protected Message.MSG
While GetMessage_( Message, 0, 0, 0 )
Wend
EndProcedure

BindGadgetEvent(#gadget, @func())
I hope this explains.

So I cannot use this.
https://github.com/mestnyi33/Widget/blo ... essage).pb
inside procedure.
https://github.com/mestnyi33/Widget/blo ... (close).pb
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3728
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: COCOA quests?

Post by wilbert »

I'm still not exactly clear what you want.
If you want to monitor for window close, you can use NSWindowWillCloseNotification (NSNotificationCenter).

If you want to remove events from the queue, you can use
nextEventMatchingMask:untilDate:inMode:dequeue:

Besides BindGadgetEvent there's also BindEvent.
I don't know if that's something you can use.

Code: Select all

Procedure WindowCloseEvent()
  CloseWindow(0)
  End
EndProcedure
  

OpenWindow(0, 0, 0, 320, 170, "Window", #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_SizeGadget)

BindEvent(#PB_Event_CloseWindow, @WindowCloseEvent(), 0)
  
EditorGadget(0, 10, 10, 300, 150)
  
Repeat
  Event = WaitWindowEvent()
ForEver
macOS 10.15 Catalina, Windows 10
User avatar
Shardik
Addict
Addict
Posts: 1831
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: COCOA quests?

Post by Shardik »

If mestnyi wants to get rid of PureBasic's event loop, he might try a permanent run loop:

Code: Select all

CocoaMessage(0, CocoaMessage(0, 0, "NSApplication sharedApplication"), "run")
This is a working example:

Code: Select all

Procedure WindowCloseEvent()
  CloseWindow(0)
  End
EndProcedure

OpenWindow(0, 270, 100, 250, 100, "Permanent loop demo")
BindEvent(#PB_Event_CloseWindow, @WindowCloseEvent(), 0)

CocoaMessage(0, CocoaMessage(0, 0, "NSApplication sharedApplication"), "run")
Post Reply