diff options
| author | Matthias Melcher <fltk@matthiasm.com> | 2011-01-07 01:01:04 +0000 |
|---|---|---|
| committer | Matthias Melcher <fltk@matthiasm.com> | 2011-01-07 01:01:04 +0000 |
| commit | 7dc05cb20ee92e38b3d1fbd88664e572d73a8d54 (patch) | |
| tree | c1a4f03879c8c371a67e26c1213f45c5ee0ffebb | |
| parent | d6bffb20a3b75037d7e5811fdca87d0040873188 (diff) | |
First attempt at finding the screen pixel sizes. Can't test Xinerame, MSWindows, or X11 yet.
git-svn-id: file:///fltk/svn/fltk/branches/branch-1.3@8204 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
| -rw-r--r-- | FL/Fl.H | 3 | ||||
| -rw-r--r-- | FL/mac.H | 2 | ||||
| -rw-r--r-- | src/Fl_cocoa.mm | 3 | ||||
| -rw-r--r-- | src/screen_xywh.cxx | 67 | ||||
| -rw-r--r-- | test/hello.cxx | 3 |
5 files changed, 72 insertions, 6 deletions
@@ -773,7 +773,8 @@ public: screen_xywh(X, Y, W, H, e_x_root, e_y_root); } static void screen_xywh(int &X, int &Y, int &W, int &H, int mx, int my); - static void screen_xywh(int &X, int &Y, int &W, int &H, int n); + static void screen_xywh(int &X, int &Y, int &W, int &H, int n); + static void screen_dpi(float &h, float &v, int n=0); /** @} */ @@ -145,7 +145,7 @@ public: void contains_GL_subwindow(void); void set_key_window(void); void set_cursor(Fl_Cursor); - static int screen_init(XRectangle screens[]); + static int screen_init(XRectangle screens[], float dpi[]); static CGImageRef CGImage_from_window_rect(Fl_Window *win, int x, int y, int w, int h); static unsigned char *bitmap_from_window_rect(Fl_Window *win, int x, int y, int w, int h, int *bytesPerPixel); static Fl_Region intersect_region_and_rect(Fl_Region current, int x,int y,int w, int h); diff --git a/src/Fl_cocoa.mm b/src/Fl_cocoa.mm index 4dedbcf3e..a61efb0a2 100644 --- a/src/Fl_cocoa.mm +++ b/src/Fl_cocoa.mm @@ -2715,7 +2715,7 @@ void Fl_X::set_cursor(Fl_Cursor c) cursor = icrsr; } -int Fl_X::screen_init(XRectangle screens[]) +int Fl_X::screen_init(XRectangle screens[], float dpi[]) { NSAutoreleasePool *localPool = [[NSAutoreleasePool alloc] init]; NSArray *a = [NSScreen screens]; @@ -2728,6 +2728,7 @@ int Fl_X::screen_init(XRectangle screens[]) screens[num_screens].y = int(r.size.height - (r.origin.y + r.size.height)); screens[num_screens].width = int(r.size.width); screens[num_screens].height = int(r.size.height); + dpi[num_screens] = float([[a objectAtIndex:i] userSpaceScaleFactor])*72.0f; num_screens ++; if (num_screens >= 16) break; } diff --git a/src/screen_xywh.cxx b/src/screen_xywh.cxx index da219d9c0..e7c48c22d 100644 --- a/src/screen_xywh.cxx +++ b/src/screen_xywh.cxx @@ -56,17 +56,29 @@ typedef BOOL (WINAPI* fl_gmi_func)(HMONITOR, LPMONITORINFO); static fl_gmi_func fl_gmi = NULL; // used to get a proc pointer for GetMonitorInfoA static RECT screens[16]; +static int dpi[16][2] = { { 0.0f, 0.0f } }; static BOOL CALLBACK screen_cb(HMONITOR mon, HDC, LPRECT r, LPARAM) { if (num_screens >= 16) return TRUE; - MONITORINFO mi; + MONITORINFOEX mi; mi.cbSize = sizeof(mi); // GetMonitorInfo(mon, &mi); // (but we use our self-aquired function pointer instead) if (fl_gmi(mon, &mi)) { screens[num_screens] = mi.rcWork; + + // find the pixel size + if (mi.cbSize == sizeof(mi)) { + HDC screen = CreateDC(mi.szDevice, NULL, NULL, NULL); + if (screen) { + dpi[num_screens][0] = (float)GetDeviceCaps(screen, LOGPIXELSX); + dpi[num_screens][1] = (float)GetDeviceCaps(screen, LOGPIXELSY); + } + ReleaseDC(); + } + num_screens ++; } return TRUE; @@ -105,16 +117,18 @@ static void screen_init() { num_screens = 1; } #elif defined(__APPLE__) -XRectangle screens[16]; +static XRectangle screens[16]; +static float dpi[16]; static void screen_init() { - num_screens = Fl_X::screen_init(screens); + num_screens = Fl_X::screen_init(screens, dpi); } #elif HAVE_XINERAMA # include <X11/extensions/Xinerama.h> // Screen data... static XineramaScreenInfo *screens; +static float dpi[2]; static void screen_init() { if (!fl_display) fl_open_display(); @@ -122,10 +136,22 @@ static void screen_init() { if (XineramaIsActive(fl_display)) { screens = XineramaQueryScreens(fl_display, &num_screens); } else num_screens = 1; + + int mm = DisplayWidthMM(fl_display, fl_screen); + dpi[0] = mm ? monitor.w()*25.4f/mm : 0.0f; + mm = DisplayHeightMM(fl_display, fl_screen); + dpi[1] = mm ? monitor.h()*25.4f/mm : dpi[0]; } #else +static XRectangle screen; +static float dpi[2]; static void screen_init() { num_screens = 1; + if (!fl_display) fl_open_display(); + int mm = DisplayWidthMM(fl_display, fl_screen); + dpi[0] = mm ? monitor.w()*25.4f/mm : 0.0f; + mm = DisplayHeightMM(fl_display, fl_screen); + dpi[1] = mm ? monitor.h()*25.4f/mm : dpi[0]; } #endif // WIN32 @@ -252,6 +278,41 @@ void Fl::screen_xywh(int &X, int &Y, int &W, int &H, int n) { } +/** + Gets the screen resolution in dots-per-inch for the given screen. + \param[out] h, v horizontal and vertical resolution + \param[in] n the screen number (0 to Fl::screen_count() - 1) + \see void screen_xywh(int &x, int &y, int &w, int &h, int mx, int my) + */ +void Fl::screen_dpi(float &h, float &v, int n) +{ + if (!num_screens) screen_init(); + h = v = 0.0f; + +#ifdef WIN32 + if (n >= 0 && n < num_screens) { + h = float(dpi[n][0]); + v = float(dpi[n][1]); + } +#elif defined(__APPLE__) + if (n >= 0 && n < num_screens) { + h = v = dpi[n]; + } +#elif HAVE_XINERAMA + if (n >= 0 && n < num_screens) { + h = dpi[0]; + v = dpi[1]; + } +#else + if (n >= 0 && n < num_screens) { + h = dpi[0]; + v = dpi[1]; + } +#endif // WIN32 +} + + + // // End of "$Id$". // diff --git a/test/hello.cxx b/test/hello.cxx index 954d01eb4..e4372e49a 100644 --- a/test/hello.cxx +++ b/test/hello.cxx @@ -30,6 +30,9 @@ #include <FL/Fl_Box.H> int main(int argc, char **argv) { + float h, v; + Fl::screen_dpi(h, v); + printf("Screen res is %g x %g ppi\n", h, v); Fl_Window *window = new Fl_Window(340,180); Fl_Box *box = new Fl_Box(20,40,300,100,"Hello, World!"); box->box(FL_UP_BOX); |
