display.cc

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2009-2012 John W. Eaton
00004 
00005 This file is part of Octave.
00006 
00007 Octave is free software; you can redistribute it and/or modify it
00008 under the terms of the GNU General Public License as published by the
00009 Free Software Foundation; either version 3 of the License, or (at your
00010 option) any later version.
00011 
00012 Octave is distributed in the hope that it will be useful, but WITHOUT
00013 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00015 for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Octave; see the file COPYING.  If not, see
00019 <http://www.gnu.org/licenses/>.
00020 
00021 */
00022 
00023 #ifdef HAVE_CONFIG_H
00024 #include <config.h>
00025 #endif
00026 
00027 #include <cstdlib>
00028 
00029 #if defined (OCTAVE_USE_WINDOWS_API)
00030 #include <windows.h>
00031 #elif defined (HAVE_FRAMEWORK_CARBON)
00032 #include <Carbon/Carbon.h>
00033 #elif defined (HAVE_X_WINDOWS)
00034 #include <X11/Xlib.h>
00035 #endif
00036 
00037 #include "singleton-cleanup.h"
00038 
00039 #include "display.h"
00040 #include "error.h"
00041 
00042 display_info *display_info::instance = 0;
00043 
00044 void
00045 display_info::init (bool query)
00046 {
00047   if (query)
00048     {
00049 #if defined (OCTAVE_USE_WINDOWS_API)
00050 
00051       HDC hdc = GetDC (0);
00052 
00053       if (hdc)
00054         {
00055           dp = GetDeviceCaps (hdc, BITSPIXEL);
00056 
00057           ht = GetDeviceCaps (hdc, VERTRES);
00058           wd = GetDeviceCaps (hdc, HORZRES);
00059 
00060           double ht_mm = GetDeviceCaps (hdc, VERTSIZE);
00061           double wd_mm = GetDeviceCaps (hdc, HORZSIZE);
00062 
00063           rx = wd * 25.4 / wd_mm;
00064           ry = ht * 25.4 / ht_mm;
00065         }
00066       else
00067         warning ("no graphical display found");
00068 
00069 #elif defined (HAVE_FRAMEWORK_CARBON)
00070 
00071       CGDirectDisplayID display = CGMainDisplayID ();
00072 
00073       if (display)
00074         {
00075           dp = CGDisplayBitsPerPixel (display);
00076 
00077           ht = CGDisplayPixelsHigh (display);
00078           wd = CGDisplayPixelsWide (display);
00079 
00080           CGSize sz_mm = CGDisplayScreenSize (display);
00081 
00082           // On modern Mac systems (>= 10.5) CGSize is a struct keeping 2
00083           // CGFloat values, but the CGFloat typedef is not present on
00084           // older systems, so use double instead.
00085           double ht_mm = sz_mm.height;
00086           double wd_mm = sz_mm.width;
00087 
00088           rx = wd * 25.4 / wd_mm;
00089           ry = ht * 25.4 / ht_mm;
00090         }
00091       else
00092         warning ("no graphical display found");
00093 
00094 #elif defined (HAVE_X_WINDOWS)
00095 
00096       const char *display_name = getenv ("DISPLAY");
00097 
00098       if (display_name && *display_name)
00099         {
00100           Display *display = XOpenDisplay (display_name);
00101 
00102           if (display)
00103             {
00104               Screen *screen = DefaultScreenOfDisplay (display);
00105 
00106               if (screen)
00107                 {
00108                   dp = DefaultDepthOfScreen (screen);
00109 
00110                   ht = HeightOfScreen (screen);
00111                   wd = WidthOfScreen (screen);
00112 
00113                   int screen_number = XScreenNumberOfScreen (screen);
00114 
00115                   double ht_mm = DisplayHeightMM (display, screen_number);
00116                   double wd_mm = DisplayWidthMM (display, screen_number);
00117 
00118                   rx = wd * 25.4 / wd_mm;
00119                   ry = ht * 25.4 / ht_mm;
00120                 }
00121               else
00122                 warning ("X11 display has no default screen");
00123 
00124               XCloseDisplay (display);
00125             }
00126           else
00127             warning ("unable to open X11 DISPLAY");
00128         }
00129       else
00130         warning ("X11 DISPLAY environment variable not set");
00131 #else
00132 
00133       warning ("no graphical display found");
00134 
00135 #endif
00136     }
00137 }
00138 
00139 bool
00140 display_info::instance_ok (bool query)
00141 {
00142   bool retval = true;
00143 
00144   if (! instance)
00145     {
00146       instance = new display_info (query);
00147 
00148       if (instance)
00149         singleton_cleanup_list::add (cleanup_instance);
00150     }
00151 
00152   if (! instance)
00153     {
00154       ::error ("unable to create display_info object!");
00155 
00156       retval = false;
00157     }
00158 
00159   return retval;
00160 }
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines