Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(573)

Side by Side Diff: chrome/browser/chromeos/input_method/candidate_window.cc

Issue 8573035: Implement unittest for CandidateWindowView. (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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/chromeos/input_method/candidate_window.h" 5 #include "chrome/browser/chromeos/input_method/candidate_window.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <string> 8 #include <string>
9 #include <vector> 9 #include <vector>
10 10
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/memory/scoped_ptr.h" 12 #include "base/memory/scoped_ptr.h"
13 #include "base/message_loop.h" 13 #include "base/message_loop.h"
14 #include "base/observer_list.h" 14 #include "base/observer_list.h"
15 #include "base/string_util.h" 15 #include "base/string_util.h"
16 #include "base/stringprintf.h" 16 #include "base/stringprintf.h"
17 #include "base/timer.h" 17 #include "base/timer.h"
18 #include "base/utf_string_conversions.h" 18 #include "base/utf_string_conversions.h"
19 #include "chrome/browser/chromeos/input_method/ibus_ui_controller.h" 19 #include "chrome/browser/chromeos/input_method/ibus_ui_controller.h"
20 #include "chrome/browser/chromeos/input_method/candidate_window_view.h"
Yusuke Sato 2011/11/17 06:07:39 wrong order
Seigo Nonaka 2011/11/17 07:52:50 Done.
20 #include "grit/generated_resources.h" 21 #include "grit/generated_resources.h"
21 #include "ui/base/l10n/l10n_util.h" 22 #include "ui/base/l10n/l10n_util.h"
22 #include "ui/gfx/canvas.h" 23 #include "ui/gfx/canvas.h"
23 #include "ui/gfx/font.h" 24 #include "ui/gfx/font.h"
24 #include "ui/gfx/screen.h" 25 #include "ui/gfx/screen.h"
25 #include "ui/views/window/non_client_view.h" 26 #include "ui/views/window/non_client_view.h"
26 #include "views/controls/label.h" 27 #include "views/controls/label.h"
27 #include "views/controls/textfield/textfield.h" 28 #include "views/controls/textfield/textfield.h"
28 #include "views/events/event.h" 29 #include "views/events/event.h"
29 #include "views/layout/fill_layout.h" 30 #include "views/layout/fill_layout.h"
(...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after
306 annotation_label->SetText( 307 annotation_label->SetText(
307 UTF8ToUTF16(lookup_table.annotations[index])); 308 UTF8ToUTF16(lookup_table.annotations[index]));
308 annotation_column_width = 309 annotation_column_width =
309 std::max(annotation_column_width, 310 std::max(annotation_column_width,
310 annotation_label->GetPreferredSize().width()); 311 annotation_label->GetPreferredSize().width());
311 } 312 }
312 313
313 return annotation_column_width; 314 return annotation_column_width;
314 } 315 }
315 316
317 } // namespace
318
316 // HidableArea is used as an area to place optional information that can be 319 // HidableArea is used as an area to place optional information that can be
317 // turned displaying off if it is unnecessary. 320 // turned displaying off if it is unnecessary.
318 class HidableArea : public views::View { 321 class HidableArea : public views::View {
319 public: 322 public:
320 HidableArea() { 323 HidableArea() {
321 // |place_holder_| will be deleted by scoped_ptr, rather than 324 // |place_holder_| will be deleted by scoped_ptr, rather than
322 // the standard owning relation of views::View. 325 // the standard owning relation of views::View.
323 // 326 //
324 // This is because we swap the contents of HidableArea between 327 // This is because we swap the contents of HidableArea between
325 // |place_holder_| (to show nothing) and |contents_| (to show something). 328 // |place_holder_| (to show nothing) and |contents_| (to show something).
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
405 size.set_width(minWidth_); 408 size.set_width(minWidth_);
406 } 409 }
407 return size; 410 return size;
408 } 411 }
409 412
410 private: 413 private:
411 views::Label* label_; 414 views::Label* label_;
412 int minWidth_; 415 int minWidth_;
413 }; 416 };
414 417
415 } // namespace
416
417 class CandidateView;
418
419 // CandidateWindowView is the main container of the candidate window UI.
420 class CandidateWindowView : public views::View {
421 public:
422 // The object can be monitored by the observer.
423 class Observer {
424 public:
425 virtual ~Observer() {}
426 // The function is called when a candidate is committed.
427 // See comments at NotifyCandidateClicked() in chromeos_input_method_ui.h
428 // for details about the parameters.
429 virtual void OnCandidateCommitted(int index, int button, int flag) = 0;
430 };
431
432 explicit CandidateWindowView(views::Widget* parent_frame);
433 virtual ~CandidateWindowView() {}
434 void Init();
435
436 // Adds the given observer. The ownership is not transferred.
437 void AddObserver(Observer* observer) {
438 observers_.AddObserver(observer);
439 }
440
441 // Removes the given observer.
442 void RemoveObserver(Observer* observer) {
443 observers_.RemoveObserver(observer);
444 }
445
446 // Selects the candidate specified by the index in the current page
447 // (zero-origin). Changes the appearance of the selected candidate,
448 // updates the information in the candidate window as needed.
449 void SelectCandidateAt(int index_in_page);
450
451 // The function is called when a candidate is being dragged. From the
452 // given point, locates the candidate under the mouse cursor, and
453 // selects it.
454 void OnCandidatePressed(const gfx::Point& point);
455
456 // Commits the candidate currently being selected.
457 void CommitCandidate();
458
459 // Hides the lookup table.
460 void HideLookupTable();
461
462 // Hides the auxiliary text.
463 void HideAuxiliaryText();
464
465 // Hides the preedit text.
466 void HidePreeditText();
467
468 // Hides whole the candidate window.
469 void HideAll();
470
471 // Shows the lookup table.
472 void ShowLookupTable();
473
474 // Shows the auxiliary text.
475 void ShowAuxiliaryText();
476
477 // Shows the preedit text.
478 void ShowPreeditText();
479
480 // Updates the auxiliary text.
481 void UpdateAuxiliaryText(const std::string& utf8_text);
482
483 // Updates the preedit text.
484 void UpdatePreeditText(const std::string& utf8_text);
485
486 // Returns true if we should update candidate views in the window. For
487 // instance, if we are going to show the same candidates as before, we
488 // don't have to update candidate views. This happens when the user just
489 // moves the cursor in the same page in the candidate window.
490 bool ShouldUpdateCandidateViews(
491 const InputMethodLookupTable& old_table,
492 const InputMethodLookupTable& new_table);
493
494 // Updates candidates of the candidate window from |lookup_table|.
495 // Candidates are arranged per |orientation|.
496 void UpdateCandidates(const InputMethodLookupTable& lookup_table);
497
498 // Resizes and moves the parent frame. The two actions should be
499 // performed consecutively as resizing may require the candidate window
500 // to move. For instance, we may need to move the candidate window from
501 // below the cursor to above the cursor, if the candidate window becomes
502 // too big to be shown near the bottom of the screen. This function
503 // needs to be called when the visible contents of the candidate window
504 // are modified.
505 void ResizeAndMoveParentFrame();
506
507 // Returns the horizontal offset used for placing the vertical candidate
508 // window so that the first candidate is aligned with the the text being
509 // converted like:
510 //
511 // XXX <- The user is converting XXX
512 // +-----+
513 // |1 XXX|
514 // |2 YYY|
515 // |3 ZZZ|
516 //
517 // Returns 0 if no candidate is present.
518 int GetHorizontalOffset();
519
520 void set_cursor_location(const gfx::Rect& cursor_location) {
521 cursor_location_ = cursor_location;
522 }
523
524 const gfx::Rect& cursor_location() const { return cursor_location_; }
525
526 protected:
527 // Override View::VisibilityChanged()
528 virtual void VisibilityChanged(View* starting_from, bool is_visible) OVERRIDE;
529
530 // Override View::OnBoundsChanged()
531 virtual void OnBoundsChanged(const gfx::Rect& previous_bounds) OVERRIDE;
532
533 private:
534 // Initializes the candidate views if needed.
535 void MaybeInitializeCandidateViews(
536 const InputMethodLookupTable& lookup_table);
537
538 // Returns the appropriate area (header or footer) to put auxiliary texts.
539 InformationTextArea* GetAuxiliaryTextArea();
540
541 // The lookup table (candidates).
542 InputMethodLookupTable lookup_table_;
543
544 // The index in the current page of the candidate currently being selected.
545 int selected_candidate_index_in_page_;
546
547 // The observers of the object.
548 ObserverList<Observer> observers_;
549
550 // The parent frame.
551 views::Widget* parent_frame_;
552
553 // Views created in the class will be part of tree of |this|, so these
554 // child views will be deleted when |this| is deleted.
555
556 // The preedit area is where the preedit text is shown, if it is needed
557 // in cases such as the focus is on a plugin that doesn't support in-line
558 // preedit drawing.
559 InformationTextArea* preedit_area_;
560 // The header area is where the auxiliary text is shown, if the
561 // orientation is horizontal. If the auxiliary text is not provided, we
562 // show nothing. For instance, we show pinyin text like "zhong'guo".
563 InformationTextArea* header_area_;
564 // The candidate area is where candidates are rendered.
565 HidableArea* candidate_area_;
566 // The candidate views are used for rendering candidates.
567 std::vector<CandidateView*> candidate_views_;
568 // The footer area is where the auxiliary text is shown, if the
569 // orientation is vertical. Usually the auxiliary text is used for
570 // showing candidate number information like 2/19.
571 InformationTextArea* footer_area_;
572
573 // Current columns width in |candidate_area_|.
574 int previous_shortcut_column_width_;
575 int previous_candidate_column_width_;
576 int previous_annotation_column_width_;
577
578 // The last cursor location.
579 gfx::Rect cursor_location_;
580 };
581
582 // CandidateRow renderes a row of a candidate. 418 // CandidateRow renderes a row of a candidate.
583 class CandidateView : public views::View { 419 class CandidateView : public views::View {
584 public: 420 public:
585 CandidateView(CandidateWindowView* parent_candidate_window, 421 CandidateView(CandidateWindowView* parent_candidate_window,
586 int index_in_page, 422 int index_in_page,
587 InputMethodLookupTable::Orientation orientation); 423 InputMethodLookupTable::Orientation orientation);
588 virtual ~CandidateView() {} 424 virtual ~CandidateView() {}
589 // Initializes the candidate view with the given column widths. 425 // Initializes the candidate view with the given column widths.
590 // A width of 0 means that the column is resizable. 426 // A width of 0 means that the column is resizable.
591 void Init(int shortcut_column_width, 427 void Init(int shortcut_column_width,
(...skipping 1229 matching lines...) Expand 10 before | Expand all | Expand 10 after
1821 CandidateWindowController::~CandidateWindowController() { 1657 CandidateWindowController::~CandidateWindowController() {
1822 delete impl_; 1658 delete impl_;
1823 } 1659 }
1824 1660
1825 bool CandidateWindowController::Init() { 1661 bool CandidateWindowController::Init() {
1826 return impl_->Init(); 1662 return impl_->Init();
1827 } 1663 }
1828 1664
1829 } // namespace input_method 1665 } // namespace input_method
1830 } // namespace chromeos 1666 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698