| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "views/controls/tabbed_pane/native_tabbed_pane_gtk.h" |
| 6 |
| 7 #include <gtk/gtk.h> |
| 8 |
| 9 #include "app/gfx/canvas.h" |
| 10 #include "app/gfx/font.h" |
| 11 #include "app/resource_bundle.h" |
| 12 #include "base/logging.h" |
| 13 #include "base/stl_util-inl.h" |
| 14 #include "base/string_util.h" |
| 15 #include "views/controls/tabbed_pane/tabbed_pane.h" |
| 16 #include "views/fill_layout.h" |
| 17 #include "views/widget/root_view.h" |
| 18 #include "views/widget/widget_gtk.h" |
| 19 |
| 20 namespace views { |
| 21 |
| 22 //////////////////////////////////////////////////////////////////////////////// |
| 23 // NativeTabbedPaneGtk, public: |
| 24 |
| 25 NativeTabbedPaneGtk::NativeTabbedPaneGtk(TabbedPane* tabbed_pane) |
| 26 : NativeControlGtk(), |
| 27 tabbed_pane_(tabbed_pane) { |
| 28 set_focus_view(tabbed_pane); |
| 29 } |
| 30 |
| 31 NativeTabbedPaneGtk::~NativeTabbedPaneGtk() { |
| 32 } |
| 33 |
| 34 //////////////////////////////////////////////////////////////////////////////// |
| 35 // NativeTabbedPaneGtk, NativeTabbedPaneWrapper implementation: |
| 36 |
| 37 void NativeTabbedPaneGtk::AddTab(const std::wstring& title, View* contents) { |
| 38 AddTabAtIndex(GetTabCount(), title, contents, true); |
| 39 } |
| 40 |
| 41 void NativeTabbedPaneGtk::AddTabAtIndex(int index, const std::wstring& title, |
| 42 View* contents, |
| 43 bool select_if_first_tab) { |
| 44 DCHECK(native_view()); |
| 45 DoAddTabAtIndex(index, title, contents, select_if_first_tab); |
| 46 } |
| 47 |
| 48 View* NativeTabbedPaneGtk::RemoveTabAtIndex(int index) { |
| 49 int tab_count = GetTabCount(); |
| 50 DCHECK(index >= 0 && index < tab_count); |
| 51 |
| 52 if (index < (tab_count - 1)) { |
| 53 // Select the next tab. |
| 54 SelectTabAt(index + 1); |
| 55 } else { |
| 56 // We are the last tab, select the previous one. |
| 57 if (index > 0) { |
| 58 SelectTabAt(index - 1); |
| 59 } else { |
| 60 // last tab. nothing to select. |
| 61 } |
| 62 } |
| 63 View* removed_tab = GetTabViewAt(index); |
| 64 |
| 65 gtk_notebook_remove_page(GTK_NOTEBOOK(native_view()), index); |
| 66 |
| 67 return removed_tab; |
| 68 } |
| 69 |
| 70 void NativeTabbedPaneGtk::SelectTabAt(int index) { |
| 71 DCHECK((index >= 0) && (index < GetTabCount())); |
| 72 gtk_notebook_set_current_page(GTK_NOTEBOOK(native_view()), index); |
| 73 } |
| 74 |
| 75 int NativeTabbedPaneGtk::GetTabCount() { |
| 76 return gtk_notebook_get_n_pages(GTK_NOTEBOOK(native_view())); |
| 77 } |
| 78 |
| 79 int NativeTabbedPaneGtk::GetSelectedTabIndex() { |
| 80 return gtk_notebook_get_current_page(GTK_NOTEBOOK(native_view())); |
| 81 } |
| 82 |
| 83 View* NativeTabbedPaneGtk::GetSelectedTab() { |
| 84 return GetTabViewAt(GetSelectedTabIndex()); |
| 85 } |
| 86 |
| 87 View* NativeTabbedPaneGtk::GetView() { |
| 88 return this; |
| 89 } |
| 90 |
| 91 void NativeTabbedPaneGtk::SetFocus() { |
| 92 Focus(); |
| 93 } |
| 94 |
| 95 gfx::NativeView NativeTabbedPaneGtk::GetTestingHandle() const { |
| 96 return native_view(); |
| 97 } |
| 98 |
| 99 //////////////////////////////////////////////////////////////////////////////// |
| 100 // NativeTabbedPaneGtk, NativeControlGtk override: |
| 101 |
| 102 void NativeTabbedPaneGtk::CreateNativeControl() { |
| 103 GtkWidget* widget = gtk_notebook_new(); |
| 104 gtk_notebook_set_tab_pos(GTK_NOTEBOOK(widget), GTK_POS_TOP); |
| 105 g_signal_connect(G_OBJECT(widget), "switch-page", |
| 106 G_CALLBACK(CallSwitchPage), this); |
| 107 NativeControlCreated(widget); |
| 108 } |
| 109 |
| 110 //////////////////////////////////////////////////////////////////////////////// |
| 111 // NativeTabbedPaneGtk, private: |
| 112 void NativeTabbedPaneGtk::DoAddTabAtIndex(int index, const std::wstring& title, |
| 113 View* contents, |
| 114 bool select_if_first_tab) { |
| 115 int tab_count = GetTabCount(); |
| 116 DCHECK(index <= tab_count); |
| 117 |
| 118 WidgetGtk* page_container = new WidgetGtk(WidgetGtk::TYPE_CHILD); |
| 119 page_container->Init(NULL, gfx::Rect()); |
| 120 page_container->SetContentsView(contents); |
| 121 page_container->Show(); |
| 122 |
| 123 GtkWidget* page = page_container->GetNativeView(); |
| 124 |
| 125 // increment ref count not to delete on remove below |
| 126 g_object_ref(page); |
| 127 // detach parent from the page so that we can add it to notebook |
| 128 GtkWidget* parent = gtk_widget_get_parent(page); |
| 129 gtk_container_remove(GTK_CONTAINER(parent), page); |
| 130 |
| 131 GtkWidget* label = gtk_label_new(WideToUTF8(title).c_str()); |
| 132 gtk_widget_show(label); |
| 133 gtk_notebook_insert_page(GTK_NOTEBOOK(native_view()), |
| 134 page, |
| 135 label, |
| 136 index); |
| 137 g_object_unref(page); |
| 138 |
| 139 if (tab_count == 0 && select_if_first_tab) |
| 140 gtk_notebook_set_current_page(GTK_NOTEBOOK(native_view()), 0); |
| 141 } |
| 142 |
| 143 View* NativeTabbedPaneGtk::GetTabViewAt(int index) { |
| 144 DCHECK(index <= GetTabCount()); |
| 145 GtkWidget* page = gtk_notebook_get_nth_page(GTK_NOTEBOOK(native_view()), index
); |
| 146 WidgetGtk* widget = WidgetGtk::GetViewForNative(page); |
| 147 DCHECK(widget && widget->GetRootView()->GetChildViewCount() == 1); |
| 148 return widget->GetRootView()->GetChildViewAt(0); |
| 149 } |
| 150 |
| 151 void NativeTabbedPaneGtk::OnSwitchPage(int selected_tab_index) { |
| 152 TabbedPane::Listener* listener = tabbed_pane_->listener(); |
| 153 if (listener != NULL) |
| 154 listener->TabSelectedAt(selected_tab_index); |
| 155 } |
| 156 |
| 157 // static |
| 158 void NativeTabbedPaneGtk::CallSwitchPage(GtkNotebook* widget, |
| 159 GtkNotebookPage* page, |
| 160 guint selected_tab_index, |
| 161 NativeTabbedPaneGtk* tabbed_pane) { |
| 162 tabbed_pane->OnSwitchPage(selected_tab_index); |
| 163 } |
| 164 |
| 165 //////////////////////////////////////////////////////////////////////////////// |
| 166 // NativeTabbedPaneWrapper, public: |
| 167 |
| 168 // static |
| 169 NativeTabbedPaneWrapper* NativeTabbedPaneWrapper::CreateNativeWrapper( |
| 170 TabbedPane* tabbed_pane) { |
| 171 return new NativeTabbedPaneGtk(tabbed_pane); |
| 172 } |
| 173 |
| 174 } // namespace views |
| OLD | NEW |