OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/libgtk2ui/gtk2_ui.h" | 5 #include "chrome/browser/ui/libgtk2ui/gtk2_ui.h" |
6 | 6 |
7 #include <math.h> | 7 #include <math.h> |
8 #include <set> | 8 #include <set> |
9 | 9 |
10 #include <pango/pango.h> | 10 #include <pango/pango.h> |
11 #include <X11/Xlib.h> | |
12 #include <gio/gio.h> | |
13 | 11 |
14 #include "base/command_line.h" | 12 #include "base/command_line.h" |
15 #include "base/debug/leak_annotations.h" | 13 #include "base/debug/leak_annotations.h" |
16 #include "base/environment.h" | 14 #include "base/environment.h" |
17 #include "base/i18n/rtl.h" | 15 #include "base/i18n/rtl.h" |
18 #include "base/logging.h" | 16 #include "base/logging.h" |
19 #include "base/nix/mime_util_xdg.h" | 17 #include "base/nix/mime_util_xdg.h" |
20 #include "base/nix/xdg_util.h" | 18 #include "base/nix/xdg_util.h" |
21 #include "base/stl_util.h" | 19 #include "base/stl_util.h" |
22 #include "base/strings/string_split.h" | 20 #include "base/strings/string_split.h" |
(...skipping 22 matching lines...) Expand all Loading... |
45 #include "third_party/skia/include/core/SkColor.h" | 43 #include "third_party/skia/include/core/SkColor.h" |
46 #include "third_party/skia/include/core/SkShader.h" | 44 #include "third_party/skia/include/core/SkShader.h" |
47 #include "ui/base/resource/resource_bundle.h" | 45 #include "ui/base/resource/resource_bundle.h" |
48 #include "ui/gfx/canvas.h" | 46 #include "ui/gfx/canvas.h" |
49 #include "ui/gfx/display.h" | 47 #include "ui/gfx/display.h" |
50 #include "ui/gfx/geometry/rect.h" | 48 #include "ui/gfx/geometry/rect.h" |
51 #include "ui/gfx/geometry/size.h" | 49 #include "ui/gfx/geometry/size.h" |
52 #include "ui/gfx/image/image.h" | 50 #include "ui/gfx/image/image.h" |
53 #include "ui/gfx/skbitmap_operations.h" | 51 #include "ui/gfx/skbitmap_operations.h" |
54 #include "ui/gfx/skia_util.h" | 52 #include "ui/gfx/skia_util.h" |
55 #include "ui/gfx/x/x11_types.h" | |
56 #include "ui/native_theme/native_theme.h" | 53 #include "ui/native_theme/native_theme.h" |
57 #include "ui/resources/grit/ui_resources.h" | 54 #include "ui/resources/grit/ui_resources.h" |
58 #include "ui/views/controls/button/label_button.h" | 55 #include "ui/views/controls/button/label_button.h" |
59 #include "ui/views/controls/button/label_button_border.h" | 56 #include "ui/views/controls/button/label_button_border.h" |
60 #include "ui/views/linux_ui/window_button_order_observer.h" | 57 #include "ui/views/linux_ui/window_button_order_observer.h" |
61 #include "ui/views/resources/grit/views_resources.h" | 58 #include "ui/views/resources/grit/views_resources.h" |
62 | 59 |
63 #if defined(USE_GCONF) | 60 #if defined(USE_GCONF) |
64 #include "chrome/browser/ui/libgtk2ui/gconf_listener.h" | 61 #include "chrome/browser/ui/libgtk2ui/gconf_listener.h" |
65 #endif | 62 #endif |
(...skipping 311 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
377 LOG(WARNING) << "Unexpected gtk-xft-rgba \"" << rgba << "\""; | 374 LOG(WARNING) << "Unexpected gtk-xft-rgba \"" << rgba << "\""; |
378 params.subpixel_rendering = gfx::FontRenderParams::SUBPIXEL_RENDERING_NONE; | 375 params.subpixel_rendering = gfx::FontRenderParams::SUBPIXEL_RENDERING_NONE; |
379 } | 376 } |
380 | 377 |
381 g_free(hint_style); | 378 g_free(hint_style); |
382 g_free(rgba); | 379 g_free(rgba); |
383 | 380 |
384 return params; | 381 return params; |
385 } | 382 } |
386 | 383 |
387 double GetBaseDPI() { | 384 double GetDPI() { |
388 XDisplay* xdisplay = gfx::GetXDisplay(); | |
389 int xscreen = DefaultScreen(xdisplay); | |
390 return (DisplayHeight(xdisplay, xscreen) * 25.4) / | |
391 DisplayHeightMM(xdisplay, xscreen); | |
392 } | |
393 | |
394 double GetFontDPI() { | |
395 GtkSettings* gtk_settings = gtk_settings_get_default(); | 385 GtkSettings* gtk_settings = gtk_settings_get_default(); |
396 CHECK(gtk_settings); | 386 CHECK(gtk_settings); |
397 gint gtk_dpi = -1; | 387 gint gtk_dpi = -1; |
398 g_object_get(gtk_settings, "gtk-xft-dpi", >k_dpi, NULL); | 388 g_object_get(gtk_settings, "gtk-xft-dpi", >k_dpi, NULL); |
399 | 389 |
400 // GTK multiplies the DPI by 1024 before storing it. | 390 // GTK multiplies the DPI by 1024 before storing it. |
401 return (gtk_dpi > 0) ? gtk_dpi / 1024.0 : GetBaseDPI(); | 391 return (gtk_dpi > 0) ? gtk_dpi / 1024.0 : 96.0; |
402 } | 392 } |
403 | 393 |
404 // Queries GTK for its font DPI setting and returns the number of pixels in a | 394 // Queries GTK for its font DPI setting and returns the number of pixels in a |
405 // point. | 395 // point. |
406 double GetPixelsInPoint(float device_scale_factor) { | 396 double GetPixelsInPoint(float device_scale_factor) { |
407 double dpi = GetFontDPI(); | 397 double dpi = GetDPI(); |
408 | 398 |
409 // Take device_scale_factor into account — if Chrome already scales the | 399 // Take device_scale_factor into account — if Chrome already scales the |
410 // entire UI up by 2x, we should not also scale up. | 400 // entire UI up by 2x, we should not also scale up. |
411 dpi /= device_scale_factor; | 401 dpi /= device_scale_factor; |
412 | 402 |
413 // There are 72 points in an inch. | 403 // There are 72 points in an inch. |
414 return dpi / 72.0; | 404 return dpi / 72.0; |
415 } | 405 } |
416 | 406 |
417 views::LinuxUI::NonClientMiddleClickAction GetDefaultMiddleClickAction() { | 407 views::LinuxUI::NonClientMiddleClickAction GetDefaultMiddleClickAction() { |
418 scoped_ptr<base::Environment> env(base::Environment::Create()); | 408 scoped_ptr<base::Environment> env(base::Environment::Create()); |
419 switch (base::nix::GetDesktopEnvironment(env.get())) { | 409 switch (base::nix::GetDesktopEnvironment(env.get())) { |
420 case base::nix::DESKTOP_ENVIRONMENT_KDE4: | 410 case base::nix::DESKTOP_ENVIRONMENT_KDE4: |
421 // Starting with KDE 4.4, windows' titlebars can be dragged with the | 411 // Starting with KDE 4.4, windows' titlebars can be dragged with the |
422 // middle mouse button to create tab groups. We don't support that in | 412 // middle mouse button to create tab groups. We don't support that in |
423 // Chrome, but at least avoid lowering windows in response to middle | 413 // Chrome, but at least avoid lowering windows in response to middle |
424 // clicks to avoid surprising users who expect the KDE behavior. | 414 // clicks to avoid surprising users who expect the KDE behavior. |
425 return views::LinuxUI::MIDDLE_CLICK_ACTION_NONE; | 415 return views::LinuxUI::MIDDLE_CLICK_ACTION_NONE; |
426 default: | 416 default: |
427 return views::LinuxUI::MIDDLE_CLICK_ACTION_LOWER; | 417 return views::LinuxUI::MIDDLE_CLICK_ACTION_LOWER; |
428 } | 418 } |
429 } | 419 } |
430 | 420 |
431 double GetGnomeTextScalingFactor() { | |
432 const char kDesktopInterfaceSchema[] = "org.gnome.desktop.interface"; | |
433 for (const gchar* const* schemas = g_settings_list_schemas(); *schemas; | |
434 schemas++) { | |
435 if (!strcmp(kDesktopInterfaceSchema, static_cast<const char*>(*schemas))) { | |
436 GSettings* settings = g_settings_new(kDesktopInterfaceSchema); | |
437 double scale = g_settings_get_double(settings, "text-scaling-factor"); | |
438 g_object_unref(settings); | |
439 return scale; | |
440 } | |
441 } | |
442 // Fallback if the schema does not exist. | |
443 return GetFontDPI() / GetBaseDPI(); | |
444 } | |
445 | |
446 } // namespace | 421 } // namespace |
447 | 422 |
448 Gtk2UI::Gtk2UI() | 423 Gtk2UI::Gtk2UI() |
449 : default_font_size_pixels_(0), | 424 : default_font_size_pixels_(0), |
450 default_font_style_(gfx::Font::NORMAL), | 425 default_font_style_(gfx::Font::NORMAL), |
451 middle_click_action_(GetDefaultMiddleClickAction()), | 426 middle_click_action_(GetDefaultMiddleClickAction()), |
452 device_scale_factor_(1.0) { | 427 device_scale_factor_(1.0) { |
453 GtkInitFromCommandLine(*base::CommandLine::ForCurrentProcess()); | 428 GtkInitFromCommandLine(*base::CommandLine::ForCurrentProcess()); |
454 } | 429 } |
455 | 430 |
(...skipping 999 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1455 | 1430 |
1456 void Gtk2UI::UpdateDeviceScaleFactor(float device_scale_factor) { | 1431 void Gtk2UI::UpdateDeviceScaleFactor(float device_scale_factor) { |
1457 device_scale_factor_ = device_scale_factor; | 1432 device_scale_factor_ = device_scale_factor; |
1458 GtkStyle* label_style = gtk_rc_get_style(fake_label_.get()); | 1433 GtkStyle* label_style = gtk_rc_get_style(fake_label_.get()); |
1459 UpdateDefaultFont(label_style->font_desc); | 1434 UpdateDefaultFont(label_style->font_desc); |
1460 } | 1435 } |
1461 | 1436 |
1462 float Gtk2UI::GetDeviceScaleFactor() const { | 1437 float Gtk2UI::GetDeviceScaleFactor() const { |
1463 if (gfx::Display::HasForceDeviceScaleFactor()) | 1438 if (gfx::Display::HasForceDeviceScaleFactor()) |
1464 return gfx::Display::GetForcedDeviceScaleFactor(); | 1439 return gfx::Display::GetForcedDeviceScaleFactor(); |
1465 // Linux chrome does not support dynamnic scale factor change. Use the | 1440 const int kCSSDefaultDPI = 96; |
1466 // value obtanied during startup. The value is rounded to 1 decimal, e.g. | 1441 float scale = GetDPI() / kCSSDefaultDPI; |
1467 // to 1.4, for safety. | 1442 // Round to 1 decimal, e.g. to 1.4. |
1468 static float device_scale_factor = | 1443 return roundf(scale * 10) / 10; |
1469 roundf(GetGnomeTextScalingFactor() * 10) / 10; | |
1470 return device_scale_factor; | |
1471 } | 1444 } |
1472 | 1445 |
1473 } // namespace libgtk2ui | 1446 } // namespace libgtk2ui |
1474 | 1447 |
1475 views::LinuxUI* BuildGtk2UI() { | 1448 views::LinuxUI* BuildGtk2UI() { |
1476 return new libgtk2ui::Gtk2UI; | 1449 return new libgtk2ui::Gtk2UI; |
1477 } | 1450 } |
OLD | NEW |