OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/gtk/bookmark_editor_gtk.h" | 5 #include "chrome/browser/gtk/bookmark_editor_gtk.h" |
6 | 6 |
7 #include <gtk/gtk.h> | 7 #include <gtk/gtk.h> |
8 | 8 |
9 #include "app/l10n_util.h" | 9 #include "app/l10n_util.h" |
10 #include "app/menus/simple_menu_model.h" | |
11 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
12 #include "base/logging.h" | 11 #include "base/logging.h" |
13 #include "base/string_util.h" | 12 #include "base/string_util.h" |
14 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
15 #include "chrome/browser/bookmarks/bookmark_model.h" | 14 #include "chrome/browser/bookmarks/bookmark_model.h" |
16 #include "chrome/browser/bookmarks/bookmark_utils.h" | 15 #include "chrome/browser/bookmarks/bookmark_utils.h" |
17 #include "chrome/browser/gtk/bookmark_tree_model.h" | 16 #include "chrome/browser/gtk/bookmark_tree_model.h" |
18 #include "chrome/browser/gtk/bookmark_utils_gtk.h" | 17 #include "chrome/browser/gtk/bookmark_utils_gtk.h" |
19 #include "chrome/browser/gtk/gtk_theme_provider.h" | 18 #include "chrome/browser/gtk/gtk_theme_provider.h" |
20 #include "chrome/browser/gtk/gtk_util.h" | 19 #include "chrome/browser/gtk/gtk_util.h" |
21 #include "chrome/browser/gtk/menu_gtk.h" | |
22 #include "chrome/browser/history/history.h" | 20 #include "chrome/browser/history/history.h" |
23 #include "chrome/browser/net/url_fixer_upper.h" | 21 #include "chrome/browser/net/url_fixer_upper.h" |
24 #include "chrome/browser/profile.h" | 22 #include "chrome/browser/profile.h" |
25 #include "gfx/gtk_util.h" | 23 #include "gfx/gtk_util.h" |
26 #include "googleurl/src/gurl.h" | 24 #include "googleurl/src/gurl.h" |
27 #include "grit/chromium_strings.h" | 25 #include "grit/chromium_strings.h" |
28 #include "grit/generated_resources.h" | 26 #include "grit/generated_resources.h" |
29 #include "grit/locale_settings.h" | 27 #include "grit/locale_settings.h" |
30 | 28 |
31 namespace { | 29 namespace { |
32 | 30 |
33 // Background color of text field when URL is invalid. | 31 // Background color of text field when URL is invalid. |
34 const GdkColor kErrorColor = GDK_COLOR_RGB(0xFF, 0xBC, 0xBC); | 32 const GdkColor kErrorColor = GDK_COLOR_RGB(0xFF, 0xBC, 0xBC); |
35 | 33 |
36 // Preferred initial dimensions, in pixels, of the folder tree. | 34 // Preferred initial dimensions, in pixels, of the folder tree. |
37 static const int kTreeWidth = 300; | 35 static const int kTreeWidth = 300; |
38 static const int kTreeHeight = 150; | 36 static const int kTreeHeight = 150; |
39 | 37 |
40 } // namespace | 38 } // namespace |
41 | 39 |
42 class BookmarkEditorGtk::ContextMenuController | |
43 : public menus::SimpleMenuModel::Delegate { | |
44 public: | |
45 explicit ContextMenuController(BookmarkEditorGtk* editor) | |
46 : editor_(editor), | |
47 running_menu_for_root_(false) { | |
48 menu_model_.reset(new menus::SimpleMenuModel(this)); | |
49 menu_model_->AddItemWithStringId(COMMAND_EDIT, IDS_EDIT); | |
50 menu_model_->AddItemWithStringId( | |
51 COMMAND_NEW_FOLDER, | |
52 IDS_BOOMARK_EDITOR_NEW_FOLDER_MENU_ITEM); | |
53 menu_.reset(new MenuGtk(NULL, menu_model_.get())); | |
54 } | |
55 virtual ~ContextMenuController() {} | |
56 | |
57 void RunMenu() { | |
58 const BookmarkNode* selected_node = GetSelectedNode(); | |
59 if (selected_node) | |
60 running_menu_for_root_ = selected_node->GetParent()->IsRoot(); | |
61 menu_->PopupAsContext(gtk_get_current_event_time()); | |
62 } | |
63 | |
64 void Cancel() { | |
65 editor_ = NULL; | |
66 menu_->Cancel(); | |
67 } | |
68 | |
69 private: | |
70 enum ContextMenuCommand { | |
71 COMMAND_EDIT, | |
72 COMMAND_NEW_FOLDER | |
73 }; | |
74 | |
75 // Overridden from menus::SimpleMenuModel::Delegate: | |
76 virtual bool IsCommandIdEnabled(int command_id) const { | |
77 return !(command_id == COMMAND_EDIT && running_menu_for_root_) && | |
78 (editor_ != NULL); | |
79 } | |
80 | |
81 virtual bool IsCommandIdChecked(int command_id) const { | |
82 return false; | |
83 } | |
84 | |
85 virtual bool GetAcceleratorForCommandId(int command_id, | |
86 menus::Accelerator* accelerator) { | |
87 return false; | |
88 } | |
89 | |
90 virtual void ExecuteCommand(int command_id) { | |
91 if (!editor_) | |
92 return; | |
93 | |
94 switch (command_id) { | |
95 case COMMAND_EDIT: { | |
96 GtkTreeIter iter; | |
97 if (!gtk_tree_selection_get_selected(editor_->tree_selection_, | |
98 NULL, | |
99 &iter)) { | |
100 return; | |
101 } | |
102 | |
103 GtkTreePath* path = gtk_tree_model_get_path( | |
104 GTK_TREE_MODEL(editor_->tree_store_), &iter); | |
105 gtk_tree_view_expand_to_path(GTK_TREE_VIEW(editor_->tree_view_), path); | |
106 | |
107 // Make the folder name editable. | |
108 gtk_tree_view_set_cursor(GTK_TREE_VIEW(editor_->tree_view_), path, | |
109 gtk_tree_view_get_column(GTK_TREE_VIEW(editor_->tree_view_), 0), | |
110 TRUE); | |
111 | |
112 gtk_tree_path_free(path); | |
113 break; | |
114 } | |
115 case COMMAND_NEW_FOLDER: | |
116 editor_->NewFolder(); | |
117 break; | |
118 default: | |
119 NOTREACHED(); | |
120 break; | |
121 } | |
122 } | |
123 | |
124 int64 GetRowIdAt(GtkTreeModel* model, GtkTreeIter* iter) { | |
125 GValue value = { 0, }; | |
126 gtk_tree_model_get_value(model, iter, bookmark_utils::ITEM_ID, &value); | |
127 int64 id = g_value_get_int64(&value); | |
128 g_value_unset(&value); | |
129 return id; | |
130 } | |
131 | |
132 const BookmarkNode* GetNodeAt(GtkTreeModel* model, GtkTreeIter* iter) { | |
133 int64 id = GetRowIdAt(model, iter); | |
134 return (id > 0) ? editor_->bb_model_->GetNodeByID(id) : NULL; | |
135 } | |
136 | |
137 const BookmarkNode* GetSelectedNode() { | |
138 GtkTreeModel* model; | |
139 GtkTreeIter iter; | |
140 if (!gtk_tree_selection_get_selected(editor_->tree_selection_, | |
141 &model, | |
142 &iter)) { | |
143 return NULL; | |
144 } | |
145 | |
146 return GetNodeAt(model, &iter); | |
147 } | |
148 | |
149 // The model and view for the right click context menu. | |
150 scoped_ptr<menus::SimpleMenuModel> menu_model_; | |
151 scoped_ptr<MenuGtk> menu_; | |
152 | |
153 // The context menu was brought up for. Set to NULL when the menu is canceled. | |
154 BookmarkEditorGtk* editor_; | |
155 | |
156 // If true, we're running the menu for the bookmark bar or other bookmarks | |
157 // nodes. | |
158 bool running_menu_for_root_; | |
159 | |
160 DISALLOW_COPY_AND_ASSIGN(ContextMenuController); | |
161 }; | |
162 | |
163 // static | 40 // static |
164 void BookmarkEditor::Show(gfx::NativeWindow parent_hwnd, | 41 void BookmarkEditor::Show(gfx::NativeWindow parent_hwnd, |
165 Profile* profile, | 42 Profile* profile, |
166 const BookmarkNode* parent, | 43 const BookmarkNode* parent, |
167 const EditDetails& details, | 44 const EditDetails& details, |
168 Configuration configuration) { | 45 Configuration configuration) { |
169 DCHECK(profile); | 46 DCHECK(profile); |
170 BookmarkEditorGtk* editor = | 47 BookmarkEditorGtk* editor = |
171 new BookmarkEditorGtk(parent_hwnd, profile, parent, details, | 48 new BookmarkEditorGtk(parent_hwnd, profile, parent, details, |
172 configuration); | 49 configuration); |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
297 if (details_.type == EditDetails::EXISTING_NODE) | 174 if (details_.type == EditDetails::EXISTING_NODE) |
298 selected_id = details_.existing_node->GetParent()->id(); | 175 selected_id = details_.existing_node->GetParent()->id(); |
299 else if (parent_) | 176 else if (parent_) |
300 selected_id = parent_->id(); | 177 selected_id = parent_->id(); |
301 tree_store_ = bookmark_utils::MakeFolderTreeStore(); | 178 tree_store_ = bookmark_utils::MakeFolderTreeStore(); |
302 bookmark_utils::AddToTreeStore(bb_model_, selected_id, tree_store_, | 179 bookmark_utils::AddToTreeStore(bb_model_, selected_id, tree_store_, |
303 &selected_iter); | 180 &selected_iter); |
304 tree_view_ = bookmark_utils::MakeTreeViewForStore(tree_store_); | 181 tree_view_ = bookmark_utils::MakeTreeViewForStore(tree_store_); |
305 gtk_widget_set_size_request(tree_view_, kTreeWidth, kTreeHeight); | 182 gtk_widget_set_size_request(tree_view_, kTreeWidth, kTreeHeight); |
306 tree_selection_ = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree_view_)); | 183 tree_selection_ = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree_view_)); |
307 g_signal_connect(tree_view_, "button-press-event", | |
308 G_CALLBACK(OnTreeViewButtonPressEventThunk), this); | |
309 | 184 |
310 GtkTreePath* path = NULL; | 185 GtkTreePath* path = NULL; |
311 if (selected_id) { | 186 if (selected_id) { |
312 path = gtk_tree_model_get_path(GTK_TREE_MODEL(tree_store_), | 187 path = gtk_tree_model_get_path(GTK_TREE_MODEL(tree_store_), |
313 &selected_iter); | 188 &selected_iter); |
314 } else { | 189 } else { |
315 // We don't have a selected parent (Probably because we're making a new | 190 // We don't have a selected parent (Probably because we're making a new |
316 // bookmark). Select the first item in the list. | 191 // bookmark). Select the first item in the list. |
317 path = gtk_tree_path_new_from_string("0"); | 192 path = gtk_tree_path_new_from_string("0"); |
318 } | 193 } |
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
514 can_close = FALSE; | 389 can_close = FALSE; |
515 } else { | 390 } else { |
516 gtk_widget_modify_base(url_entry_, GTK_STATE_NORMAL, NULL); | 391 gtk_widget_modify_base(url_entry_, GTK_STATE_NORMAL, NULL); |
517 } | 392 } |
518 } | 393 } |
519 gtk_dialog_set_response_sensitive(GTK_DIALOG(dialog_), | 394 gtk_dialog_set_response_sensitive(GTK_DIALOG(dialog_), |
520 GTK_RESPONSE_ACCEPT, can_close); | 395 GTK_RESPONSE_ACCEPT, can_close); |
521 } | 396 } |
522 | 397 |
523 void BookmarkEditorGtk::OnNewFolderClicked(GtkWidget* button) { | 398 void BookmarkEditorGtk::OnNewFolderClicked(GtkWidget* button) { |
524 NewFolder(); | |
525 } | |
526 | |
527 gboolean BookmarkEditorGtk::OnTreeViewButtonPressEvent(GtkWidget* widget, | |
528 GdkEventButton* event) { | |
529 if (event->button == 3) | |
530 ShowContextMenu(); | |
531 | |
532 return FALSE; | |
533 } | |
534 | |
535 void BookmarkEditorGtk::ShowContextMenu() { | |
536 if (!menu_controller_.get()) | |
537 menu_controller_.reset(new ContextMenuController(this)); | |
538 | |
539 menu_controller_->RunMenu(); | |
540 } | |
541 | |
542 void BookmarkEditorGtk::NewFolder() { | |
543 GtkTreeIter iter; | 399 GtkTreeIter iter; |
544 if (!gtk_tree_selection_get_selected(tree_selection_, | 400 if (!gtk_tree_selection_get_selected(tree_selection_, |
545 NULL, | 401 NULL, |
546 &iter)) { | 402 &iter)) { |
547 NOTREACHED() << "Something should always be selected if New Folder " << | 403 NOTREACHED() << "Something should always be selected if New Folder " << |
548 "is clicked"; | 404 "is clicked"; |
549 return; | 405 return; |
550 } | 406 } |
551 | 407 |
552 GtkTreeIter new_item_iter; | 408 GtkTreeIter new_item_iter; |
553 AddNewGroup(&iter, &new_item_iter); | 409 AddNewGroup(&iter, &new_item_iter); |
554 | 410 |
555 GtkTreePath* path = gtk_tree_model_get_path( | 411 GtkTreePath* path = gtk_tree_model_get_path( |
556 GTK_TREE_MODEL(tree_store_), &new_item_iter); | 412 GTK_TREE_MODEL(tree_store_), &new_item_iter); |
557 gtk_tree_view_expand_to_path(GTK_TREE_VIEW(tree_view_), path); | 413 gtk_tree_view_expand_to_path(GTK_TREE_VIEW(tree_view_), path); |
558 | 414 |
559 // Make the folder name editable. | 415 // Make the folder name editable. |
560 gtk_tree_view_set_cursor(GTK_TREE_VIEW(tree_view_), path, | 416 gtk_tree_view_set_cursor(GTK_TREE_VIEW(tree_view_), path, |
561 gtk_tree_view_get_column(GTK_TREE_VIEW(tree_view_), 0), | 417 gtk_tree_view_get_column(GTK_TREE_VIEW(tree_view_), 0), |
562 TRUE); | 418 TRUE); |
563 | 419 |
564 gtk_tree_path_free(path); | 420 gtk_tree_path_free(path); |
565 } | 421 } |
OLD | NEW |