GUI/Maximum window dimensions

< GUI
Revision as of 11:38, 23 January 2012 by rosettacode>Dkf (moved GUI/Max Window Dimensions to GUI/Maximum window dimensions: Capitalization policy)

The task is to determine the maximum height and width of a window that can fit within the physical display area of the screen without srolling. This is effectively the screen size (not the total desktop area, which could be bigger than the screen display area) in pixels minus any adjustments for window decorations and menubars. The idea is to determine the physical display parameters for the maximum height and width of the usable display area in pixels (without scrolling). The values calculated should represent the usable desktop area of a window maximized to fit the the screen.

Task
GUI/Maximum window dimensions
You are encouraged to solve this task according to the task description, using any language you may know.

Considerations

Multiple Monitors

For multiple monitors, the values calculated should represent the size of the usable display area on the monitor which is related to the task (ie the monitor which would display a window if such instructions were given).

Tiling Window Managers

For a tiling window manager, the values calculated should represent the maximum height and width of the display area of the maximum size a window can be created (without scrolling). This would typically be a full screen window (minus any areas occupied by desktop bars), unless the window manager has restrictions that prevents the creation of a full screen window, in which case the values represent the usable area of the desktop that occupies the maximum permissible window size (without scrolling).

Gambas

Overview

In gambas, the trick to determining the maximum window size that will fit on the screen is to create a form that is maximized and then query its dimensions from within a Form_Resize() event. Note that the form can be invisible during this process, and typically we would use the main modal window (FMain in this example).

Creating the form

From with the project create a form (FMain) with the following properties set:

<lang gambas>FMain.Maximized = True FMain.Visible = False ' The form can be invisible</lang>

From within the projectview, rightclick the FMain form and select Edit class from the contextmenu. This will display a form class file (FMain.class) as follows:

<lang gambas>PUBLIC SUB _new()

END

PUBLIC SUB Form_Open()

END</lang>

Adding the form resize event

We can now add a Form_Resize() event to the class file with the necessary code to obtain the screen dimensions as follows:

<lang gambas>PUBLIC SUB Form_Resize()

 PRINT "The maximum window size that can be used without scrolling is "; FMain.Width; " x "; FMain.Height  

END</lang>

Icon and Unicon

Raise and query a hidden window. <lang Icon>link graphics

procedure main() # Window size

W  := WOpen("canvas=hidden") dh := WAttrib("displayheight") dw := WAttrib("displaywidth") WClose(W)

write("The display size is w=",dw,", h=",dh) end</lang>


This example is in need of improvement:

Need to handle window borders which will vary from system to system and handle or comment on additional requirements.

Mathematica

Example output on a 1280x1024 system. <lang Mathematica>Differences@Transpose@SystemInformation["Devices"]1, 2, 1, 1, 2 ->Template:1260, 951</lang>

PicoLisp

The following works on ErsatzLisp, the Java version of PicoLisp. <lang PicoLisp>(let Frame (java "javax.swing.JFrame" T "Window")

  (java Frame 'setExtendedState
     (java (public "javax.swing.JFrame" 'MAXIMIZED_BOTH)) )
  (java Frame 'setVisible T)
  (wait 200)
  (let Size (java (java Frame 'getContentPane) 'getSize)
     (prinl "Width: " (java (public Size 'width)))
     (prinl "Height: " (java (public Size 'height))) )
  (java Frame 'dispose) )</lang>

Output (on a 1024x768 screen):

Width: 1010
Height: 735

PureBasic

<lang PureBasic>If OpenWindow(0, 0, 0, 5, 5, "", #PB_Window_Maximize + #PB_Window_Invisible)

 maxX = WindowWidth(0)
 maxY = WindowHeight(0)
 CloseWindow(0)
 MessageRequester("Result", "Maximum Window Width: " + Str(maxX) + ", Maximum Window Height: " + Str(maxY))

EndIf</lang> Sample output for a screen area 1600 x 1200:

Maximum Window Width: 1600, Maximum Window Height: 1181

Tcl

Library: Tk

<lang tcl>package require Tk proc maxSize {} {

   # Need a dummy window; max window can be changed by scripts
   set top .__defaultMaxSize__
   if {![winfo exists $top]} {
       toplevel $top
       wm withdraw $top
   }
   # Default max size of window is value we want
   return [wm maxsize $top]

}</lang> On this system, that returns 1440 836. Further discussion of related matters, including platform limitations, is on the Tcler's Wiki.

Visual Basic

Method 1

The first method involves querying the screen dimensions and then subtracting pixels used by the frame and desktop bars:

<lang vb>TYPE syswindowstru

 screenheight AS INTEGER
 screenwidth AS INTEGER
 maxheight AS INTEGER
 maxwidth AS INTEGER

END TYPE

DIM syswindow AS syswindowstru

' Determine the height and width of the screen

syswindow.screenwidth = Screen.Width / Screen.TwipsPerPixelX syswindow.screenheight=Screen.Height / Screen.TwipsPerPixelY

' Make adjustments for window decorations and menubars</lang>

Method 2

The alternative method is to create a form that is maximized and then query its dimensions (similar to the method used in gambas).