help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: desktop height?


From: Chris McMahan
Subject: Re: desktop height?
Date: 30 Jan 2003 09:40:57 -0500
User-agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.2

Bruce Ingalls <bingalls.NO_SPAM@fit-zones.com> writes:

> Does anyone know a way to calculate the height of the desktop in elisp?
> 
> I'd like Emacs to start up with maximum height.
> For that matter, I'd like XEmacs, and various OSes to work.
> 
> In Linux X Window, the following works reasonably well:
> 
> (/ (- (x-display-pixel-height) 50) (frame-char-height)))
> 
> I can get reasonably close with
> 
> (/ (x-display-pixel-height)
>   (* (/ (font-default-size-for-device) 2) 3))
> 
> which works in w32 XEmacs as
> 
> (/ (x-display-pixel-height)
>   (* (/ (string-to-number (font-default-size-for-device)) 2) 3))
> 
> or w32 Emacs as
> 
> (/ (x-display-pixel-height) 18)
> 
> I'd also like to hear of testing on other OSes.
> If someone has a better solution, please send it!
> Thanks ahead.
> 

Sorry about the length of this post. I recently rewrote these
functions, and thought I would share them within the context of this
thread :)

I've got the following code with sets the initial height based on the
current screen's resolution (working under WindowsXP with Emacs 21.2).
This assumes the font (defined in my .emacs as MY_FONT) is already
set. This is necessary to correctly return the frame-char-height.

  ;;;------------------------------------------------------------
  ;;; initial window position settings, height and vertical position is
  ;;; automatically calculated from within the .emacs-config file 
  ;;; The top left corner is 0 0, the bottom right is -1 -1
  (defconst MY_INITIAL_WIDTH   120)
  (defconst MY_INITIAL_LEFT   -15)
  
  ;; set an adjustment to account for window manager frames and title
  ;; bar when calculating the window height. This accounts for the
  ;; menubar as well
  (defconst MY_HEIGHT_ADJUST
    (if (not menu-bar-mode)
        (setq MY_HEIGHT_ADJUST 30)
      (setq MY_HEIGHT_ADJUST 50))
    "Arbitrary adjustment factor to account for window managers and
    menubar")
  
  ;; use the screen resolution, font height, and whether or not
  ;; there's a menubar to calculate the height and center the frame
  (defconst MY_INITIAL_HEIGHT
    (/ (- (x-display-pixel-height) MY_HEIGHT_ADJUST)
       (frame-char-height))
    "Calculate the initial height of the frame based on the screen
    resolution and character height")
  
  ;;; set the height of the frame so the top can then be calculated to
  ;;; center it
  (set-frame-height (selected-frame) MY_INITIAL_HEIGHT)
  
  ;;; calculate the top to center the frame
  (defconst MY_INITIAL_TOP
    (/ (- (x-display-pixel-height)
          (+ (frame-pixel-height) MY_HEIGHT_ADJUST)) 2)
    "Calculate the initial top of the frame based on the screen
    resolution and character height")
  
  ;;; now that the initial frame info is known, set the size and
  ;;; position for subsequent windows
  (defconst MY_DEFAULT_HEIGHT (- MY_INITIAL_HEIGHT 15))
  (defconst MY_DEFAULT_TOP    (+ MY_INITIAL_TOP 45))
  (defconst MY_DEFAULT_LEFT   (- MY_INITIAL_LEFT 150))
  (defconst MY_DEFAULT_WIDTH   MY_INITIAL_WIDTH)
  
    ;;; Set the font and frame size for the initial frame.
    (progn
      (setq initial-frame-alist
            `(
              (top . ,MY_INITIAL_TOP)
              (left . ,MY_INITIAL_LEFT)
              (width . ,MY_INITIAL_WIDTH)
              (height . ,MY_INITIAL_HEIGHT)
              (internal-border-width . 1)
              (cursor-type . t)
              (font . ,MY_FONT)
              )
            )
  
    ;;; Set the default font and frame size
      (setq default-frame-alist
            `(
              (top . ,MY_DEFAULT_TOP)
              (left . ,MY_DEFAULT_LEFT)
              (width . ,MY_DEFAULT_WIDTH)
              (height . ,MY_DEFAULT_HEIGHT)
              (internal-border-width . 1)
              (cursor-type . t)
              (font . ,MY_FONT)
              )
            )
      ))

  ;;;------------------------------------------------------------


Here's some macros I also have to adjust the frame when a new font is
selected, to enlarge the frame to fill most of the current screen, and
to return the frame back to its initial settings:


;;;======================================================================
;;; frame sizing functions
;;;======================================================================

;;;==============================
(defun enlarge-frame ()
  "Enlarge the selected frame to fill a sizeable portion of the screen,
based on the current screen resolution"
  (interactive)

  ;; Set the frame size
  ;; set the new width, with a little space on the sides
  (setq lframe-width
                (- (/ (x-display-pixel-width) (frame-char-width)) 10))
  
  ;; set the new height, allowing for title bars (8)
  (setq lframe-height
                (/ (- (x-display-pixel-height) MY_HEIGHT_ADJUST)
                   (frame-char-height)))

  ;; apply to the selected frame
  (set-frame-size (selected-frame)
                                  lframe-width
                                  lframe-height)

  ;; center the new frame horizontally and vertically
  ;; set the left position, centering the frame and allowing for frame edges 
(10)
  (setq lframe-left
                (/ (- (x-display-pixel-width)
                          (+ (frame-pixel-width) 10)) 2))

  ;; set the top position, centering the frame and allowing for title bars (50)
  (setq lframe-top 
        (/ (- (x-display-pixel-height)
                  (+ (frame-pixel-height) MY_HEIGHT_ADJUST)) 2))

  ;; now apply the changes
  (set-frame-position (selected-frame)
                                          lframe-left
                                          lframe-top)
  )

;;;==============================
(defun adjust-frame ()
  "Adjust the selected frame based to the user's initial width, but
adjust the height based on the current screen resolution"

  (interactive)
  ;; set the width and height
  (set-frame-size
    (selected-frame)
        MY_INITIAL_WIDTH
        (/ (- (x-display-pixel-height) MY_HEIGHT_ADJUST)
           (frame-char-height)))

  ;; set the frame position
  (set-frame-position
    (selected-frame)
    MY_INITIAL_LEFT
        ;; need to account for the title bar and menu when centering
        ;; vertically
        (/ (- (x-display-pixel-height)
                  (+ (frame-pixel-height) MY_HEIGHT_ADJUST)) 2))
  )

;;;==============================
(defun fix-frame ()
  "Restore the frame back to the initial height, width and position as
set up in the .emacs

uses the global variables:
   MY_INITIAL_WIDTH
   MY_INITIAL_HEIGHT
   MY_INITIAL_LEFT
   MY_INITIAL_RIGHT

defined in the .emacs file"
  (interactive)
  (set-frame-size (selected-frame) MY_INITIAL_WIDTH MY_INITIAL_HEIGHT)
  (set-frame-position (selected-frame) MY_INITIAL_LEFT MY_INITIAL_TOP)
  )





-- 
     (.   .)
  =ooO=(_)=Ooo========================
  Chris McMahan | cmcmahan-AT-one.net
  ====================================


reply via email to

[Prev in Thread] Current Thread [Next in Thread]