OSDN Git Service

d3e44c7fdd8c3c6885fba123a22d1591ac50f8e5
[tortoisegit/TortoiseGitJp.git] / src / TortoiseMerge / RightView.cpp
1 // TortoiseMerge - a Diff/Patch program\r
2 \r
3 // Copyright (C) 2006-2008 - TortoiseSVN\r
4 \r
5 // This program is free software; you can redistribute it and/or\r
6 // modify it under the terms of the GNU General Public License\r
7 // as published by the Free Software Foundation; either version 2\r
8 // of the License, or (at your option) any later version.\r
9 \r
10 // This program is distributed in the hope that it will be useful,\r
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13 // GNU General Public License for more details.\r
14 \r
15 // You should have received a copy of the GNU General Public License\r
16 // along with this program; if not, write to the Free Software Foundation,\r
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.\r
18 //\r
19 #include "StdAfx.h"\r
20 #include "Resource.h"\r
21 #include "AppUtils.h"\r
22 #include ".\rightview.h"\r
23 \r
24 IMPLEMENT_DYNCREATE(CRightView, CBaseView)\r
25 \r
26 CRightView::CRightView(void)\r
27 {\r
28         m_pwndRight = this;\r
29         m_nStatusBarID = ID_INDICATOR_RIGHTVIEW;\r
30 }\r
31 \r
32 CRightView::~CRightView(void)\r
33 {\r
34 }\r
35 \r
36 bool CRightView::OnContextMenu(CPoint point, int /*nLine*/, DiffStates state)\r
37 {\r
38         if (!this->IsWindowVisible())\r
39                 return false;\r
40 \r
41         CMenu popup;\r
42         if (popup.CreatePopupMenu())\r
43         {\r
44 #define ID_USEBLOCK 1\r
45 #define ID_USEFILE 2\r
46 #define ID_USETHEIRANDYOURBLOCK 3\r
47 #define ID_USEYOURANDTHEIRBLOCK 4\r
48 #define ID_USEBOTHTHISFIRST 5\r
49 #define ID_USEBOTHTHISLAST 6\r
50 \r
51                 UINT uEnabled = MF_ENABLED;\r
52                 if ((m_nSelBlockStart == -1)||(m_nSelBlockEnd == -1))\r
53                         uEnabled |= MF_DISABLED | MF_GRAYED;\r
54                 CString temp;\r
55 \r
56                 bool bImportantBlock = true;\r
57                 switch (state)\r
58                 {\r
59                 case DIFFSTATE_UNKNOWN:\r
60                         bImportantBlock = false;\r
61                         break;\r
62                 }\r
63 \r
64                 if (!m_pwndBottom->IsWindowVisible())\r
65                 {\r
66                         temp.LoadString(IDS_VIEWCONTEXTMENU_USEOTHERBLOCK);\r
67                 }\r
68                 else\r
69                         temp.LoadString(IDS_VIEWCONTEXTMENU_USETHISBLOCK);\r
70                 popup.AppendMenu(MF_STRING | uEnabled | (bImportantBlock ? MF_ENABLED : MF_DISABLED|MF_GRAYED), ID_USEBLOCK, temp);\r
71 \r
72                 if (!m_pwndBottom->IsWindowVisible())\r
73                 {\r
74                         temp.LoadString(IDS_VIEWCONTEXTMENU_USEOTHERFILE);\r
75                 }\r
76                 else\r
77                         temp.LoadString(IDS_VIEWCONTEXTMENU_USETHISFILE);\r
78                 popup.AppendMenu(MF_STRING | MF_ENABLED, ID_USEFILE, temp);\r
79 \r
80                 if (m_pwndBottom->IsWindowVisible())\r
81                 {\r
82                         temp.LoadString(IDS_VIEWCONTEXTMENU_USEYOURANDTHEIRBLOCK);\r
83                         popup.AppendMenu(MF_STRING | uEnabled | (bImportantBlock ? MF_ENABLED : MF_DISABLED|MF_GRAYED), ID_USEYOURANDTHEIRBLOCK, temp);\r
84                         temp.LoadString(IDS_VIEWCONTEXTMENU_USETHEIRANDYOURBLOCK);\r
85                         popup.AppendMenu(MF_STRING | uEnabled | (bImportantBlock ? MF_ENABLED : MF_DISABLED|MF_GRAYED), ID_USETHEIRANDYOURBLOCK, temp);\r
86                 }\r
87                 else\r
88                 {\r
89                         temp.LoadString(IDS_VIEWCONTEXTMENU_USEBOTHTHISFIRST);\r
90                         popup.AppendMenu(MF_STRING | uEnabled | (bImportantBlock ? MF_ENABLED : MF_DISABLED|MF_GRAYED), ID_USEBOTHTHISFIRST, temp);\r
91                         temp.LoadString(IDS_VIEWCONTEXTMENU_USEBOTHTHISLAST);\r
92                         popup.AppendMenu(MF_STRING | uEnabled | (bImportantBlock ? MF_ENABLED : MF_DISABLED|MF_GRAYED), ID_USEBOTHTHISLAST, temp);\r
93                 }\r
94 \r
95                 popup.AppendMenu(MF_SEPARATOR, NULL);\r
96 \r
97                 temp.LoadString(IDS_EDIT_COPY);\r
98                 popup.AppendMenu(MF_STRING | (HasTextSelection() ? MF_ENABLED : MF_DISABLED|MF_GRAYED), ID_EDIT_COPY, temp);\r
99                 if (!m_bCaretHidden)\r
100                 {\r
101                         temp.LoadString(IDS_EDIT_CUT);\r
102                         popup.AppendMenu(MF_STRING | (HasTextSelection() ? MF_ENABLED : MF_DISABLED|MF_GRAYED), ID_EDIT_CUT, temp);\r
103                         temp.LoadString(IDS_EDIT_PASTE);\r
104                         popup.AppendMenu(MF_STRING | (CAppUtils::HasClipboardFormat(CF_UNICODETEXT)||CAppUtils::HasClipboardFormat(CF_TEXT) ? MF_ENABLED : MF_DISABLED|MF_GRAYED), ID_EDIT_PASTE, temp);\r
105                 }\r
106 \r
107                 // if the context menu is invoked through the keyboard, we have to use\r
108                 // a calculated position on where to anchor the menu on\r
109                 if ((point.x == -1) && (point.y == -1))\r
110                 {\r
111                         CRect rect;\r
112                         GetWindowRect(&rect);\r
113                         point = rect.CenterPoint();\r
114                 }\r
115                 int cmd = popup.TrackPopupMenu(TPM_RETURNCMD | TPM_LEFTALIGN | TPM_NONOTIFY, point.x, point.y, this, 0);\r
116                 viewstate rightstate;\r
117                 viewstate bottomstate;\r
118                 viewstate leftstate;\r
119                 switch (cmd)\r
120                 {\r
121                 case ID_EDIT_COPY:\r
122                         OnEditCopy();\r
123                         return true;\r
124                 case ID_EDIT_CUT:\r
125                         OnEditCopy();\r
126                         RemoveSelectedText();\r
127                         return false;\r
128                 case ID_EDIT_PASTE:\r
129                         PasteText();\r
130                         return false;\r
131                 case ID_USEFILE:\r
132                         {\r
133                                 UseFile(false);\r
134                         } \r
135                         break;\r
136                 case ID_USEBLOCK:\r
137                         {\r
138                                 UseBlock(false);\r
139                         } \r
140                 break;\r
141                 case ID_USEYOURANDTHEIRBLOCK:\r
142                         {\r
143                                 UseYourAndTheirBlock(rightstate, bottomstate, leftstate);\r
144                                 CUndo::GetInstance().AddState(leftstate, rightstate, bottomstate, m_ptCaretPos);\r
145                         }\r
146                         break;\r
147                 case ID_USETHEIRANDYOURBLOCK:\r
148                         {\r
149                                 UseTheirAndYourBlock(rightstate, bottomstate, leftstate);\r
150                                 CUndo::GetInstance().AddState(leftstate, rightstate, bottomstate, m_ptCaretPos);\r
151                         }\r
152                         break;\r
153                 case ID_USEBOTHTHISFIRST:\r
154                         {\r
155                                 UseBothRightFirst(rightstate, leftstate);\r
156                                 CUndo::GetInstance().AddState(leftstate, rightstate, bottomstate, m_ptCaretPos);\r
157                         }\r
158                         break;\r
159                 case ID_USEBOTHTHISLAST:\r
160                         {\r
161                                 UseBothLeftFirst(rightstate, leftstate);\r
162                                 CUndo::GetInstance().AddState(leftstate, rightstate, bottomstate, m_ptCaretPos);\r
163                         }\r
164                         break;\r
165                 default:\r
166                         return false;\r
167                 } // switch (cmd) \r
168         } // if (popup.CreatePopupMenu()) \r
169         return false;\r
170 }\r
171 \r
172 void CRightView::UseFile(bool refreshViews /* = true */)\r
173 {\r
174         viewstate rightstate;\r
175         viewstate bottomstate;\r
176         viewstate leftstate;\r
177         if (m_pwndBottom->IsWindowVisible())\r
178         {\r
179                 for (int i=0; i<GetLineCount(); i++)\r
180                 {\r
181                         bottomstate.difflines[i] = m_pwndBottom->m_pViewData->GetLine(i);\r
182                         m_pwndBottom->m_pViewData->SetLine(i, m_pViewData->GetLine(i));\r
183                         bottomstate.linestates[i] = m_pwndBottom->m_pViewData->GetState(i);\r
184                         m_pwndBottom->m_pViewData->SetState(i, m_pViewData->GetState(i));\r
185                         if (m_pwndBottom->IsLineConflicted(i))\r
186                                 m_pwndBottom->m_pViewData->SetState(i, DIFFSTATE_CONFLICTRESOLVED);\r
187                 }\r
188                 m_pwndBottom->SetModified();\r
189         }\r
190         else\r
191         {\r
192                 for (int i=0; i<GetLineCount(); i++)\r
193                 {\r
194                         rightstate.difflines[i] = m_pViewData->GetLine(i);\r
195                         m_pViewData->SetLine(i, m_pwndLeft->m_pViewData->GetLine(i));\r
196                         DiffStates state = m_pwndLeft->m_pViewData->GetState(i);\r
197                         switch (state)\r
198                         {\r
199                         case DIFFSTATE_CONFLICTEMPTY:\r
200                         case DIFFSTATE_UNKNOWN:\r
201                         case DIFFSTATE_EMPTY:\r
202                                 rightstate.linestates[i] = m_pViewData->GetState(i);\r
203                                 m_pViewData->SetState(i, state);\r
204                                 break;\r
205                         case DIFFSTATE_YOURSADDED:\r
206                         case DIFFSTATE_IDENTICALADDED:\r
207                         case DIFFSTATE_NORMAL:\r
208                         case DIFFSTATE_THEIRSADDED:\r
209                         case DIFFSTATE_ADDED:\r
210                         case DIFFSTATE_CONFLICTADDED:\r
211                         case DIFFSTATE_CONFLICTED:\r
212                         case DIFFSTATE_CONFLICTED_IGNORED:\r
213                         case DIFFSTATE_IDENTICALREMOVED:\r
214                         case DIFFSTATE_REMOVED:\r
215                         case DIFFSTATE_THEIRSREMOVED:\r
216                         case DIFFSTATE_YOURSREMOVED:\r
217                                 rightstate.linestates[i] = m_pViewData->GetState(i);\r
218                                 m_pViewData->SetState(i, DIFFSTATE_NORMAL);\r
219                                 leftstate.linestates[i] = m_pwndLeft->m_pViewData->GetState(i);\r
220                                 m_pwndLeft->m_pViewData->SetState(i, DIFFSTATE_NORMAL);\r
221                                 break;\r
222                         default:\r
223                                 break;\r
224                         }\r
225                         SetModified();\r
226                         if (m_pwndLocator)\r
227                                 m_pwndLocator->DocumentUpdated();\r
228                 }\r
229         }\r
230         CUndo::GetInstance().AddState(leftstate, rightstate, bottomstate, m_ptCaretPos);\r
231         if (refreshViews)\r
232                 RefreshViews();\r
233 }\r
234 \r
235 void CRightView::UseBlock(bool refreshViews /* = true */)\r
236 {\r
237         viewstate rightstate;\r
238         viewstate bottomstate;\r
239         viewstate leftstate;\r
240 \r
241         if ((m_nSelBlockStart == -1)||(m_nSelBlockEnd == -1))\r
242                 return;\r
243         if (m_pwndBottom->IsWindowVisible())\r
244         {\r
245                 for (int i=m_nSelBlockStart; i<=m_nSelBlockEnd; i++)\r
246                 {\r
247                         bottomstate.difflines[i] = m_pwndBottom->m_pViewData->GetLine(i);\r
248                         m_pwndBottom->m_pViewData->SetLine(i, m_pViewData->GetLine(i));\r
249                         bottomstate.linestates[i] = m_pwndBottom->m_pViewData->GetState(i);\r
250                         m_pwndBottom->m_pViewData->SetState(i, m_pViewData->GetState(i));\r
251                         if (m_pwndBottom->IsLineConflicted(i))\r
252                         {\r
253                                 if (m_pViewData->GetState(i) == DIFFSTATE_CONFLICTEMPTY)\r
254                                         m_pwndBottom->m_pViewData->SetState(i, DIFFSTATE_CONFLICTRESOLVEDEMPTY);\r
255                                 else\r
256                                         m_pwndBottom->m_pViewData->SetState(i, DIFFSTATE_CONFLICTRESOLVED);\r
257                         }\r
258                 }\r
259                 m_pwndBottom->SetModified();\r
260         }\r
261         else\r
262         {\r
263                 for (int i=m_nSelBlockStart; i<=m_nSelBlockEnd; i++)\r
264                 {\r
265                         rightstate.difflines[i] = m_pViewData->GetLine(i);\r
266                         m_pViewData->SetLine(i, m_pwndLeft->m_pViewData->GetLine(i));\r
267                         DiffStates state = m_pwndLeft->m_pViewData->GetState(i);\r
268                         switch (state)\r
269                         {\r
270                         case DIFFSTATE_ADDED:\r
271                         case DIFFSTATE_CONFLICTADDED:\r
272                         case DIFFSTATE_CONFLICTED:\r
273                         case DIFFSTATE_CONFLICTED_IGNORED:\r
274                         case DIFFSTATE_CONFLICTEMPTY:\r
275                         case DIFFSTATE_IDENTICALADDED:\r
276                         case DIFFSTATE_NORMAL:\r
277                         case DIFFSTATE_THEIRSADDED:\r
278                         case DIFFSTATE_UNKNOWN:\r
279                         case DIFFSTATE_YOURSADDED:\r
280                         case DIFFSTATE_EMPTY:\r
281                                 rightstate.linestates[i] = m_pViewData->GetState(i);\r
282                                 m_pViewData->SetState(i, state);\r
283                                 break;\r
284                         case DIFFSTATE_IDENTICALREMOVED:\r
285                         case DIFFSTATE_REMOVED:\r
286                         case DIFFSTATE_THEIRSREMOVED:\r
287                         case DIFFSTATE_YOURSREMOVED:\r
288                                 rightstate.linestates[i] = m_pViewData->GetState(i);\r
289                                 m_pViewData->SetState(i, DIFFSTATE_ADDED);\r
290                                 break;\r
291                         default:\r
292                                 break;\r
293                         }\r
294                 }\r
295                 SetModified();\r
296         }\r
297         CUndo::GetInstance().AddState(leftstate, rightstate, bottomstate, m_ptCaretPos);\r
298         if (refreshViews)\r
299                 RefreshViews();\r
300 }\r
301 \r
302 void CRightView::UseLeftBeforeRight(bool refreshViews /* = true */)\r
303 {\r
304         viewstate rightstate;\r
305         viewstate bottomstate;\r
306         viewstate leftstate;\r
307         UseBothLeftFirst(rightstate, leftstate);\r
308         CUndo::GetInstance().AddState(leftstate, rightstate, bottomstate, m_ptCaretPos);\r
309         if (refreshViews)\r
310                 RefreshViews();\r
311 }\r
312 \r
313 void CRightView::UseRightBeforeLeft(bool refreshViews /* = true */)\r
314 {\r
315         viewstate rightstate;\r
316         viewstate bottomstate;\r
317         viewstate leftstate;\r
318         UseBothRightFirst(rightstate, leftstate);\r
319         CUndo::GetInstance().AddState(leftstate, rightstate, bottomstate, m_ptCaretPos);\r
320         if (refreshViews)\r
321                 RefreshViews();\r
322 }\r