summaryrefslogtreecommitdiffstats
path: root/chrome/browser/gtk/options/options_layout_gtk.cc
blob: d0ae714f2382603ea486c11ca0d09a92334f2304 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright (c) 2006-2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/gtk/options/options_layout_gtk.h"

#include "chrome/browser/gtk/gtk_util.h"

// If the height of screen is equal or shorter than this, we will use
// a more compact option layout.
const int kCompactScreenHeight = 600;

// Default option layout builder follows GNOME HIG, which uses header and
// spacing to group options.
class DefaultOptionsLayoutBuilderGtk : public OptionsLayoutBuilderGtk {
 public:
  explicit DefaultOptionsLayoutBuilderGtk();

  void AddOptionGroup(const std::string& title, GtkWidget* content,
                      bool expandable);
  void AddWidget(GtkWidget* content, bool expandable);
 private:
  DISALLOW_COPY_AND_ASSIGN(DefaultOptionsLayoutBuilderGtk);
};

DefaultOptionsLayoutBuilderGtk::DefaultOptionsLayoutBuilderGtk() {
  page_ = gtk_vbox_new(FALSE, gtk_util::kContentAreaSpacing);
  gtk_container_set_border_width(GTK_CONTAINER(page_),
                                 gtk_util::kContentAreaBorder);
}

void DefaultOptionsLayoutBuilderGtk::AddOptionGroup(const std::string& title,
                                                    GtkWidget* content,
                                                    bool expandable) {
  GtkWidget* title_label = gtk_util::CreateBoldLabel(title);

  GtkWidget* group = gtk_vbox_new(FALSE, gtk_util::kControlSpacing);
  gtk_box_pack_start(GTK_BOX(group), title_label, FALSE, FALSE, 0);
  gtk_container_add(GTK_CONTAINER(group), gtk_util::IndentWidget(content));

  AddWidget(group, expandable);
}

void DefaultOptionsLayoutBuilderGtk::AddWidget(GtkWidget* content,
                                               bool expandable) {
  gtk_box_pack_start(GTK_BOX(page_), content, expandable, expandable, 0);
}

// Compact layout builder uses table to layout label and content horizontally.
class CompactOptionsLayoutBuilderGtk : public OptionsLayoutBuilderGtk {
 public:
  explicit CompactOptionsLayoutBuilderGtk();

  void AddOptionGroup(const std::string& title, GtkWidget* content,
                      bool expandable);
  void AddWidget(GtkWidget* content, bool expandable);
 private:
  GtkWidget *table_;
  guint row_;

  DISALLOW_COPY_AND_ASSIGN(CompactOptionsLayoutBuilderGtk);
};

CompactOptionsLayoutBuilderGtk::CompactOptionsLayoutBuilderGtk() {
  row_ = 0;
  table_ = NULL;

  page_ = gtk_vbox_new(FALSE, gtk_util::kContentAreaSpacing);
  gtk_container_set_border_width(GTK_CONTAINER(page_),
                                 gtk_util::kContentAreaBorder);
}

void CompactOptionsLayoutBuilderGtk::AddOptionGroup(const std::string& title,
                                             GtkWidget* content,
                                             bool expandable) {
  if (!table_) {
    // Create a new table to contain option groups
    table_ = gtk_table_new(0, 2, FALSE);
    gtk_table_set_col_spacing(GTK_TABLE(table_), 0, gtk_util::kLabelSpacing);
    gtk_table_set_row_spacings(GTK_TABLE(table_),
                               gtk_util::kContentAreaSpacing);

    gtk_container_set_border_width(GTK_CONTAINER(table_),
                                   gtk_util::kContentAreaBorder);
    gtk_box_pack_start(GTK_BOX(page_), table_, TRUE, TRUE, 0);
  }

  GtkWidget* title_label = gtk_util::CreateBoldLabel(title);

  gtk_table_resize(GTK_TABLE(table_), row_ + 1, 2);
  gtk_misc_set_alignment(GTK_MISC(title_label), 1, 0);

  gtk_table_attach(GTK_TABLE(table_), title_label,
                   0, 1, row_, row_ + 1,
                   GTK_FILL, GTK_FILL,
                   0, 0);
  gtk_table_attach(GTK_TABLE(table_), content,
                   1, 2, row_, row_ + 1,
                   expandable ?
                   GTK_FILL : GtkAttachOptions(GTK_FILL | GTK_EXPAND),
                   GTK_FILL,
                   0, 0);
  row_++;
}

void CompactOptionsLayoutBuilderGtk::AddWidget(GtkWidget* content,
                                               bool expandable) {
  gtk_box_pack_start(GTK_BOX(page_), content, expandable, expandable, 0);

  // Let AddOptionGroup create a new table and append after this widget
  table_ = NULL;
}

OptionsLayoutBuilderGtk* OptionsLayoutBuilderGtk::Create() {
    return new DefaultOptionsLayoutBuilderGtk();
}

OptionsLayoutBuilderGtk*
OptionsLayoutBuilderGtk::CreateOptionallyCompactLayout() {
  gint screen_height = gdk_screen_get_height(gdk_screen_get_default());
  if (screen_height <= kCompactScreenHeight)
    return new CompactOptionsLayoutBuilderGtk();
  else
    return new DefaultOptionsLayoutBuilderGtk();
}