summaryrefslogtreecommitdiffstats
path: root/chrome/browser/views/bookmark_table_view.cc
blob: c1b402354b374017aa63bee5565968f0e83859ea (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
// Copyright (c) 2006-2008 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/views/bookmark_table_view.h"

#include "app/drag_drop_types.h"
#include "app/gfx/chrome_canvas.h"
#include "app/gfx/chrome_font.h"
#include "app/os_exchange_data.h"
#include "app/resource_bundle.h"
#include "base/base_drag_source.h"
#include "chrome/browser/bookmarks/bookmark_utils.h"
#include "chrome/browser/bookmarks/bookmark_model.h"
#include "chrome/browser/bookmarks/bookmark_table_model.h"
#include "chrome/browser/profile.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/pref_service.h"
#include "grit/generated_resources.h"
#include "views/view_constants.h"

namespace {

// Height of the drop indicator used when dropping between rows.
const int kDropHighlightHeight = 2;

int GetWidthOfColumn(const std::vector<views::TableColumn>& columns,
                     const std::vector<int> widths,
                     int column_id) {
  for (size_t i = 0; i < columns.size(); ++i) {
    if (columns[i].id == column_id)
      return widths[i];
  }
  NOTREACHED();
  return -1;
}

}  // namespace

void BookmarkTableView::DropInfo::Scrolled() {
  view_->UpdateDropInfo();
}

BookmarkTableView::BookmarkTableView(Profile* profile,
                                     BookmarkTableModel* model)
    : views::TableView(model, std::vector<views::TableColumn>(),
                       views::ICON_AND_TEXT, false, true, true),
      profile_(profile),
      show_path_column_(false) {
  UpdateColumns();
}

bool BookmarkTableView::CanDrop(const OSExchangeData& data) {
  if (!parent_node_ || !profile_->GetBookmarkModel()->IsLoaded())
    return false;

  BookmarkDragData drag_data;
  if (!drag_data.Read(data))
    return false;

  // Don't allow the user to drop an ancestor of the parent node onto the
  // parent node. This would create a cycle, which is definitely a no-no.
  std::vector<BookmarkNode*> nodes = drag_data.GetNodes(profile_);
  for (size_t i = 0; i < nodes.size(); ++i) {
    if (parent_node_->HasAncestor(nodes[i]))
      return false;
  }

  drop_info_.reset(new DropInfo(this));
  drop_info_->SetData(drag_data);
  return true;
}

void BookmarkTableView::OnDragEntered(const views::DropTargetEvent& event) {
}

int BookmarkTableView::OnDragUpdated(const views::DropTargetEvent& event) {
  if (!parent_node_ || !drop_info_.get()) {
    drop_info_.reset(NULL);
    return false;
  }

  drop_info_->Update(event);
  return UpdateDropInfo();
}

void BookmarkTableView::OnDragExited() {
  SetDropPosition(DropPosition());
  drop_info_.reset();
}

int BookmarkTableView::OnPerformDrop(const views::DropTargetEvent& event) {
  OnPerformDropImpl();
  int drop_operation = drop_info_->drop_operation();
  SetDropPosition(DropPosition());
  drop_info_.reset();
  return drop_operation;
}

BookmarkTableModel* BookmarkTableView::bookmark_table_model() const {
  return static_cast<BookmarkTableModel*>(model());
}

void BookmarkTableView::SaveColumnConfiguration() {
  PrefService* prefs = profile_->GetPrefs();
  if (!prefs)
    return;

  if (show_path_column_) {
    prefs->SetInteger(prefs::kBookmarkTableNameWidth2,
                      GetColumnWidth(IDS_BOOKMARK_TABLE_TITLE));
    prefs->SetInteger(prefs::kBookmarkTableURLWidth2,
                      GetColumnWidth(IDS_BOOKMARK_TABLE_URL));
    prefs->SetInteger(prefs::kBookmarkTablePathWidth,
                      GetColumnWidth(IDS_BOOKMARK_TABLE_PATH));
  } else {
    prefs->SetInteger(prefs::kBookmarkTableNameWidth1,
                      GetColumnWidth(IDS_BOOKMARK_TABLE_TITLE));
    prefs->SetInteger(prefs::kBookmarkTableURLWidth1,
                      GetColumnWidth(IDS_BOOKMARK_TABLE_URL));
  }
}

void BookmarkTableView::SetAltText(const std::wstring& alt_text) {
  if (alt_text == alt_text_)
    return;

  alt_text_ = alt_text;
  if (!GetNativeControlHWND())
    return;

  RECT alt_text_bounds = GetAltTextBounds().ToRECT();
  InvalidateRect(GetNativeControlHWND(), &alt_text_bounds, FALSE);
}

void BookmarkTableView::SetShowPathColumn(bool show_path_column) {
  if (show_path_column == show_path_column_)
    return;

  SaveColumnConfiguration();

  show_path_column_ = show_path_column;
  UpdateColumns();
}

void BookmarkTableView::PostPaint() {
  PaintAltText();

  if (!drop_info_.get() || drop_info_->position().index == -1 ||
      drop_info_->position().on) {
    return;
  }

  RECT bounds = GetDropBetweenHighlightRect(drop_info_->position().index);
  HDC dc = GetDC(GetNativeControlHWND());
  HBRUSH brush = CreateSolidBrush(GetSysColor(COLOR_WINDOWTEXT));
  FillRect(dc, &bounds, brush);
  DeleteObject(brush);
  ReleaseDC(GetNativeControlHWND(), dc);
}

LRESULT BookmarkTableView::OnNotify(int w_param, LPNMHDR l_param) {
  switch(l_param->code) {
    case LVN_BEGINDRAG:
      BeginDrag();
      return 0;  // Return value doesn't matter for this message.
  }

  return TableView::OnNotify(w_param, l_param);
}

int BookmarkTableView::UpdateDropInfo() {
  DropPosition position = CalculateDropPosition(drop_info_->last_y());

  drop_info_->set_drop_operation(CalculateDropOperation(position));

  if (drop_info_->drop_operation() == DragDropTypes::DRAG_NONE)
    position = DropPosition();

  SetDropPosition(position);

  return drop_info_->drop_operation();
}

void BookmarkTableView::BeginDrag() {
  std::vector<BookmarkNode*> nodes_to_drag;
  for (TableView::iterator i = SelectionBegin(); i != SelectionEnd(); ++i)
    nodes_to_drag.push_back(bookmark_table_model()->GetNodeForRow(*i));
  if (nodes_to_drag.empty())
    return;  // Nothing to drag.

  // Reverse the nodes so that they are put on the clipboard in visual order.
  // We do this as SelectionBegin starts at the end of the visual order.
  std::reverse(nodes_to_drag.begin(), nodes_to_drag.end());

  scoped_refptr<OSExchangeData> data = new OSExchangeData;
  BookmarkDragData(nodes_to_drag).Write(profile_, data);
  scoped_refptr<BaseDragSource> drag_source(new BaseDragSource);
  DWORD effects;
  DoDragDrop(data, drag_source,
             DROPEFFECT_LINK | DROPEFFECT_COPY | DROPEFFECT_MOVE, &effects);
}

int BookmarkTableView::CalculateDropOperation(const DropPosition& position) {
  if (drop_info_->data().IsFromProfile(profile_)) {
    // Data from the same profile. Prefer move, but do copy if the user wants
    // that.
    if (drop_info_->is_control_down())
      return DragDropTypes::DRAG_COPY;

    int real_drop_index;
    BookmarkNode* drop_parent = GetDropParentAndIndex(position,
                                                      &real_drop_index);
    if (!bookmark_utils::IsValidDropLocation(
        profile_, drop_info_->data(), drop_parent, real_drop_index)) {
      return DragDropTypes::DRAG_NONE;
    }
    return DragDropTypes::DRAG_MOVE;
  }
  // We're going to copy, but return an operation compatible with the source
  // operations so that the user can drop.
  return bookmark_utils::PreferredDropOperation(
      drop_info_->source_operations(),
      DragDropTypes::DRAG_COPY | DragDropTypes::DRAG_LINK);
}

void BookmarkTableView::OnPerformDropImpl() {
  int drop_index;
  BookmarkNode* drop_parent = GetDropParentAndIndex(
      drop_info_->position(), &drop_index);
  BookmarkModel* model = profile_->GetBookmarkModel();
  int min_selection;
  int max_selection;
  // If the data is not from this profile we return an operation compatible
  // with the source. As such, we need to need to check the data here too.
  if (!drop_info_->data().IsFromProfile(profile_) ||
      drop_info_->drop_operation() == DragDropTypes::DRAG_COPY) {
    bookmark_utils::CloneDragData(model, drop_info_->data().elements,
                                  drop_parent, drop_index);
    min_selection = drop_index;
    max_selection = drop_index +
        static_cast<int>(drop_info_->data().elements.size());
  } else {
    // else, move.
    std::vector<BookmarkNode*> nodes = drop_info_->data().GetNodes(profile_);
    if (nodes.empty())
      return;

    for (size_t i = 0; i < nodes.size(); ++i) {
      model->Move(nodes[i], drop_parent, drop_index);
      // Reset the drop_index, just in case the index didn't really change.
      drop_index = drop_parent->IndexOfChild(nodes[i]) + 1;
    }
    min_selection = drop_parent->IndexOfChild(nodes[0]);
    max_selection = min_selection + static_cast<int>(nodes.size());
  }
  if (drop_info_->position().on) {
    // The user dropped on a folder, select it.
    int index = parent_node_->IndexOfChild(drop_parent);
    if (index != -1)
      Select(index);
  } else if (min_selection < RowCount() && max_selection <= RowCount()) {
    // Select the moved/copied rows.
    Select(min_selection);
    if (min_selection + 1 < max_selection) {
      // SetSelectedState doesn't send notification, so we manually do it.
      for (int i = min_selection + 1; i < max_selection; ++i)
        SetSelectedState(i, true);
      if (observer())
        observer()->OnSelectionChanged();
    }
  }
}

void BookmarkTableView::SetDropPosition(const DropPosition& position) {
  if (drop_info_->position().equals(position))
    return;

  UpdateDropIndicator(drop_info_->position(), false);

  drop_info_->set_position(position);

  UpdateDropIndicator(drop_info_->position(), true);
}

void BookmarkTableView::UpdateDropIndicator(const DropPosition& position,
                                            bool turn_on) {
  if (position.index == -1)
    return;

  if (position.on) {
    ListView_SetItemState(GetNativeControlHWND(), position.index,
                          turn_on ? LVIS_DROPHILITED : 0, LVIS_DROPHILITED);
  } else {
    RECT bounds = GetDropBetweenHighlightRect(position.index);
    InvalidateRect(GetNativeControlHWND(), &bounds, FALSE);
  }
}

BookmarkTableView::DropPosition
    BookmarkTableView::CalculateDropPosition(int y) {
  HWND hwnd = GetNativeControlHWND();
  int row_count = RowCount();
  int top_index = ListView_GetTopIndex(hwnd);
  if (row_count == 0 || top_index < 0)
    return DropPosition(0, false);

  for (int i = top_index; i < row_count; ++i) {
    RECT bounds;
    ListView_GetItemRect(hwnd, i, &bounds, LVIR_BOUNDS);
    if (y < bounds.top)
      return DropPosition(i, false);
    if (y < bounds.bottom) {
      if (bookmark_table_model()->GetNodeForRow(i)->is_folder()) {
        if (y < bounds.top + views::kDropBetweenPixels)
          return DropPosition(i, false);
        if (y >= bounds.bottom - views::kDropBetweenPixels)
          return DropPosition(i + 1, false);
        return DropPosition(i, true);
      }
      if (y < (bounds.bottom - bounds.top) / 2 + bounds.top)
        return DropPosition(i, false);
      return DropPosition(i + 1, false);
    }
  }
  return DropPosition(row_count, false);
}

BookmarkNode* BookmarkTableView::GetDropParentAndIndex(
    const DropPosition& position,
    int* index) {
  if (position.on) {
    BookmarkNode* parent = parent_node_->GetChild(position.index);
    *index = parent->GetChildCount();
    return parent;
  }
  *index = position.index;
  return parent_node_;
}

RECT BookmarkTableView::GetDropBetweenHighlightRect(int index) {
  RECT bounds = { 0 };
  if (RowCount() == 0) {
    bounds.top = content_offset();
    bounds.left = 0;
    bounds.right = width();
  } else if (index >= RowCount()) {
    ListView_GetItemRect(GetNativeControlHWND(), index - 1, &bounds,
                         LVIR_BOUNDS);
    bounds.top = bounds.bottom - kDropHighlightHeight / 2;
  } else {
    ListView_GetItemRect(GetNativeControlHWND(), index, &bounds, LVIR_BOUNDS);
    bounds.top -= kDropHighlightHeight / 2;
  }
  bounds.bottom = bounds.top + kDropHighlightHeight;
  return bounds;
}

void BookmarkTableView::UpdateColumns() {
  PrefService* prefs = profile_->GetPrefs();
  views::TableColumn name_column =
      views::TableColumn(IDS_BOOKMARK_TABLE_TITLE, views::TableColumn::LEFT,
                         -1);
  views::TableColumn url_column =
      views::TableColumn(IDS_BOOKMARK_TABLE_URL, views::TableColumn::LEFT, -1);
  views::TableColumn path_column =
      views::TableColumn(IDS_BOOKMARK_TABLE_PATH, views::TableColumn::LEFT, -1);

  std::vector<views::TableColumn> columns;
  if (show_path_column_) {
    int name_width = -1;
    int url_width = -1;
    int path_width = -1;
    if (prefs) {
      name_width = prefs->GetInteger(prefs::kBookmarkTableNameWidth2);
      url_width = prefs->GetInteger(prefs::kBookmarkTableURLWidth2);
      path_width = prefs->GetInteger(prefs::kBookmarkTablePathWidth);
    }
    if (name_width != -1 && url_width != -1 && path_width != -1) {
      name_column.width = name_width;
      url_column.width = url_width;
      path_column.width = path_width;
    } else {
      name_column.percent = .5;
      url_column.percent = .25;
      path_column.percent= .25;
    }
    columns.push_back(name_column);
    columns.push_back(url_column);
    columns.push_back(path_column);
  } else {
    int name_width = -1;
    int url_width = -1;
    if (prefs) {
      name_width = prefs->GetInteger(prefs::kBookmarkTableNameWidth1);
      url_width = prefs->GetInteger(prefs::kBookmarkTableURLWidth1);
    }
    if (name_width != -1 && url_width != -1) {
      name_column.width = name_width;
      url_column.width = url_width;
    } else {
      name_column.percent = .5;
      url_column.percent = .5;
    }
    columns.push_back(name_column);
    columns.push_back(url_column);
  }
  SetColumns(columns);
  for (size_t i = 0; i < columns.size(); ++i)
    SetColumnVisibility(columns[i].id, true);
  OnModelChanged();
}

void BookmarkTableView::PaintAltText() {
  if (alt_text_.empty())
    return;

  HDC dc = GetDC(GetNativeControlHWND());
  ChromeFont font = GetAltTextFont();
  gfx::Rect bounds = GetAltTextBounds();
  ChromeCanvas canvas(bounds.width(), bounds.height(), false);
  // Pad by 1 for halo.
  canvas.DrawStringWithHalo(alt_text_, font, SK_ColorDKGRAY, SK_ColorWHITE, 1,
                            1, bounds.width() - 2, bounds.height() - 2,
                            l10n_util::DefaultCanvasTextAlignment());
  canvas.getTopPlatformDevice().drawToHDC(dc, bounds.x(), bounds.y(), NULL);
  ReleaseDC(GetNativeControlHWND(), dc);
}

gfx::Rect BookmarkTableView::GetAltTextBounds() {
  static const int kXOffset = 16;
  DCHECK(GetNativeControlHWND());
  CRect client_rect;
  GetClientRect(GetNativeControlHWND(), client_rect);
  ChromeFont font = GetAltTextFont();
  // Pad height by 2 for halo.
  return gfx::Rect(kXOffset, content_offset(), client_rect.Width() - kXOffset,
                   std::max(kImageSize, font.height() + 2));
}

ChromeFont BookmarkTableView::GetAltTextFont() {
  return ResourceBundle::GetSharedInstance().GetFont(ResourceBundle::BaseFont);
}