Wednesday, April 24, 2013

Fullscreen on Snow Leopard

On previous OSX versions it was of course already possible and quite easy to make a fullscreen application with RealBasic. Toggle the fullscreen property of the Window, this will make the window cover the whole screen.

Two small niggles still remained: the grow-icon and a menubar hiding glitch.

If the application window is re-sizable when not in fullscreen, the 'grow icon' is still visible when the window is fullscreen. Grabbing this also allows the window to be resized when 'officially' in fullscreen mode. One way to solve this is to maintain two Window objects, one for windowed and one non-sizable for the fullscreen view. Another, easier solution is to use the SetWindowGrowTab function from the excellent collection of CarbonDeclares. (Yes, with Cocoa these will be obsolete ultimately, but now still useful.)

When toggling the fullscreen mode, call:
   SetWindowGrowTab( MyWindow,  Not MyWindow.FullScreen)

The function itself from CarbonDeclares:
Sub SetWindowGrowTab(w as window, b as boolean)
  // Added 11/13/2001 by Jarvis Badgley
  // Renamed 11/16/2001 by Jarvis Badgley from DisableWindowMinimizeWidget
  
  const attrib = 16

  dim err as integer
  #if TargetCarbon then
    Declare Function ChangeWindowAttributes Lib "Carbon" (window as WindowPtr, setTheseAttributes as Integer, clearTheseAttributes as Integer) as Integer
    
    if b then
      err = ChangeWindowAttributes(w, attrib, 0)
    else
      err = ChangeWindowAttributes(w, 0, attrib)
    end
    
  #endif // TargetCarbon
End Sub



The second niggle is that after the user selects a menu item by pressing the shortcut key (e.g. Cmd-O) , the menubar comes out of hiding and does not go back. The simple hack to work around this is to toggle the MenuBarVisible state. Call this hack at the end of every menu handler.


  If MyWindow.FullScreen Then
    MyWindow.MenuBarVisible = True //brings state in sync with OS again, otherwise the hide won't trigger
    MyWindow.MenuBarVisible = Not MyWindow.FullScreen
  End if
 

There are likely other or better ways to avoid these small niggles, but these worked. These allow the application to have a 'clean' fullscreen option for it's window also on older versions of OSX.


Thursday, April 18, 2013

More versatile Canvas for showing images

To show a picture, the quick RealBasic way is to add a Canvas object to the window and assign a Picture to the Backdrop property of the Canvas. This will draw the image in the canvas space, stretched to the size of the canvas.

To have a bit more control over how the image is drawn, especially when using pre-drawn graphics as part of the user interface, this ImageCanvas control is made. It adds an Image property and allows selecting the mode of drawing, i.e. how the picture will be drawn.

The new ImageCanvas component is included in the example project. Use the menu to find and open an image file, or drag-and-drop an image file on the window. Use the controls to change the drawing mode and see how it changes the way the picture is drawn.
And of course; browse the code to see how it works and tweak as needed...


Tuesday, April 9, 2013

Automatic file naming code snippet

For some applications, it can be useful to be able to save a file without asking for a filename first with a dialog. Much like e.g. taking a screenshot on a Mac, this ends up on the Desktop with a filename that contains the time of the file, e.g. "Screen shot 2013-04-09 at 10:10 AM".

This code snippet generates a FolderItem with a filename generated in the same way:

Function AutonamedFile(prefix As String, extension As String) As FolderItem
  Dim f As FolderItem
  Dim D As Date
  Dim i As Integer
  
  D = New Date
  
  f = SpecialFolder.Desktop.Child(prefix + D.SQLDate+ " at " + Format(D.Hour,"00")+"."+Format(D.Minute,"00")+"."+Format(D.Second,"00")+ "."+extension)
  
  If f.Exists Then
    i = 1
    Do
      f = SpecialFolder.Desktop.Child(prefix + D.SQLDate+ " at " + Format(D.Hour,"00")+"."+Format(D.Minute,"00")+"."+Format(D.Second,"00")+" " + Str(i)+ "."+extension)
      i = i+1
    Loop Until Not f.Exists
  End if
  
  Return f
  
End Function


Copy-paste into RealStudio as a method. For saving e.g. a Picture (MyPicture) the same way that the screenshot function in the Mac does it, call with the prefix and extension like so:

Dim f As FolderItem

f = AutonamedFile("Screen shot", "png")
MyPicture.Save(f, Picture.SaveAsPNG)


Thursday, April 4, 2013

Harvey balls (or a progress indicator)

For a continuous progress indicator with a custom design, a Rotator object. Of course based on a Canvas with custom drawing and a set of properties to set the look and the mode of the Rotator.  In design it basically is a Harvey ball.



The object is included in an example project, showing how the object is called and can be used.

Look in the code of the Paint event to see the drawing. As can be seen in the screenshot, the Canvas has no transparency on Windows in some cases (GDI+). If that is troubling for an application, the following code snippet can be uncommented:


    If Self.TrueWindow.HasBackColor Then
      g.ForeColor = Self.TrueWindow.BackColor
    Else
      g.ForeColor = FillColor
    End
    g.FillRect(0,0,g.Width,g.Height)
   
Look for it in (of course) the Paint event.