1 // Copyright 2010-2018, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 //     * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 //     * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 //     * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 
30 #ifndef MOZC_RENDERER_UNIX_CANDIDATE_WINDOW_H_
31 #define MOZC_RENDERER_UNIX_CANDIDATE_WINDOW_H_
32 
33 #include <memory>
34 
35 #include "renderer/table_layout_interface.h"
36 #include "renderer/unix/cairo_factory_interface.h"
37 #include "renderer/unix/const.h"
38 #include "renderer/unix/draw_tool_interface.h"
39 #include "renderer/unix/gtk_window_base.h"
40 #include "renderer/unix/text_renderer_interface.h"
41 #include "testing/base/public/gunit_prod.h"
42 
43 namespace mozc {
44 namespace client {
45 class SendCommandInterface;
46 }  // namespace client
47 namespace renderer {
48 namespace gtk {
49 
50 class CandidateWindow : public GtkWindowBase {
51  public:
52   // GtkWindowBase (and super class) has ownership of all arguments.
53   CandidateWindow(TableLayoutInterface *table_layout,
54                   TextRendererInterface *text_renderer,
55                   DrawToolInterface *draw_tool,
56                   GtkWrapperInterface *gtk,
57                   CairoFactoryInterface *cairo_factory);
~CandidateWindow()58   virtual ~CandidateWindow() {}
59 
60   virtual Size Update(const commands::Candidates &candidates);
61   virtual Rect GetCandidateColumnInClientCord() const;
62   virtual void Initialize();
63 
64   // usage type for each column.
65   enum COLUMN_TYPE {
66     COLUMN_SHORTCUT = 0,  // show shortcut key
67     COLUMN_GAP1,          // padding region
68     COLUMN_CANDIDATE,     // show candidate string
69     COLUMN_GAP2,          // padding region
70     COLUMN_DESCRIPTION,   // show description message
71     NUMBER_OF_COLUMNS,    // number of columns. (this item should be last)
72   };
73 
74   virtual bool SetSendCommandInterface(
75       client::SendCommandInterface *send_command_interface);
76   virtual void ReloadFontConfig(const string &font_description);
77 
78  protected:
79   // Callbacks
80   bool OnPaint(GtkWidget *widget, GdkEventExpose *event);
81 
82   void OnMouseLeftUp(const Point &pos);
83 
84   // Returns zero oriented row index which covers the argument position.
85   // If this function returns negative value, it means there are no row to
86   // cover the argument position.
87   // The reason for virtual function is for testing.
88   virtual int GetSelectedRowIndex(const Point &pos) const;
89 
90  private:
91   void DrawBackground();
92   void DrawShortcutBackground();
93   void DrawSelectedRect();
94   void DrawCells();
95   void DrawInformationIcon();
96   void DrawVScrollBar();
97   void DrawFooter();
98   void DrawFrame();
99 
100   // Draws footer separator and updates footer content area rectangle.
101   void DrawFooterSeparator(Rect *footer_content_area);
102 
103   // Draws footer index into specified rectangle and updates content area to
104   // rest one.
105   void DrawFooterIndex(Rect *footer_content_rect);
106 
107   // Draws logo into specified rectangle and updates content area to rest one.
108   void DrawLogo(Rect *footer_content_rect);
109 
110   // Draws footer label.
111   void DrawFooterLabel(const Rect &footer_content_rect);
112 
113   // Make strings to be displayed based on candidates. If there is no string to
114   // be displayed, string will be empty.
115   static void GetDisplayString(
116       const commands::Candidates::Candidate &candidate,
117       string *shortcut, string *value, string *description);
118 
119   void UpdateScrollBarSize();
120   void UpdateFooterSize();
121   void UpdateGap1Size();
122 
123   void UpdateCandidatesSize(bool *has_description);
124   void UpdateGap2Size(bool has_description);
125 
126   // TODO(nona): Remove FRIEND_TEST
127   FRIEND_TEST(CandidateWindowTest, DrawBackgroundTest);
128   FRIEND_TEST(CandidateWindowTest, DrawShortcutBackgroundTest);
129   FRIEND_TEST(CandidateWindowTest, DrawSelectedRectTest);
130   FRIEND_TEST(CandidateWindowTest, DrawCellsTest);
131   FRIEND_TEST(CandidateWindowTest, DrawInformationIconTest);
132   FRIEND_TEST(CandidateWindowTest, DrawVScrollBarTest);
133   FRIEND_TEST(CandidateWindowTest, DrawFooterTest);
134   FRIEND_TEST(CandidateWindowTest, DrawFrameTest);
135   FRIEND_TEST(CandidateWindowTest, GetDisplayStringTest);
136   FRIEND_TEST(CandidateWindowTest, DrawFooterSeparatorTest);
137   FRIEND_TEST(CandidateWindowTest, DrawFooterIndexTest);
138   FRIEND_TEST(CandidateWindowTest, DrawLogoTest);
139   FRIEND_TEST(CandidateWindowTest, DrawFooterLabelTest);
140   FRIEND_TEST(CandidateWindowTest, UpdateScroolBarSizeTest);
141   FRIEND_TEST(CandidateWindowTest, UpdateFooterSizeTest);
142   FRIEND_TEST(CandidateWindowTest, UpdateGap1SizeTest);
143   FRIEND_TEST(CandidateWindowTest, UpdateCandidatesSizeTest);
144   FRIEND_TEST(CandidateWindowTest, UpdateGap2SizeTest);
145   FRIEND_TEST(CandidateWindowTest, OnMouseLeftUpTest);
146   FRIEND_TEST(CandidateWindowTest, GetSelectedRowIndexTest);
147 
148   commands::Candidates candidates_;
149   std::unique_ptr<TableLayoutInterface> table_layout_;
150   std::unique_ptr<TextRendererInterface> text_renderer_;
151   std::unique_ptr<DrawToolInterface> draw_tool_;
152   std::unique_ptr<CairoFactoryInterface> cairo_factory_;
153   client::SendCommandInterface *send_command_interface_;
154   DISALLOW_COPY_AND_ASSIGN(CandidateWindow);
155 };
156 
157 }  // namespace gtk
158 }  // namespace renderer
159 }  // namespace mozc
160 #endif  // MOZC_RENDERER_UNIX_CANDIDATE_WINDOW_H_
161