OSDN Git Service

TortoiseMerge Basic Support Git patch created by format patch
[tortoisegit/TortoiseGitJp.git] / src / TortoiseMerge / RightView.cpp
1 // TortoiseMerge - a Diff/Patch program\r
2 \r
3 // Copyright (C) 2006-2009 - 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                         m_pwndBottom->m_pViewData->SetLineEnding(i, m_pViewData->GetLineEnding(i));\r
186                         if (m_pwndBottom->IsLineConflicted(i))\r
187                                 m_pwndBottom->m_pViewData->SetState(i, DIFFSTATE_CONFLICTRESOLVED);\r
188                 }\r
189                 m_pwndBottom->SetModified();\r
190         }\r
191         else\r
192         {\r
193                 for (int i=0; i<GetLineCount(); i++)\r
194                 {\r
195                         rightstate.difflines[i] = m_pViewData->GetLine(i);\r
196                         m_pViewData->SetLine(i, m_pwndLeft->m_pViewData->GetLine(i));\r
197                         m_pViewData->SetLineEnding(i, m_pwndLeft->m_pViewData->GetLineEnding(i));\r
198                         DiffStates state = m_pwndLeft->m_pViewData->GetState(i);\r
199                         switch (state)\r
200                         {\r
201                         case DIFFSTATE_CONFLICTEMPTY:\r
202                         case DIFFSTATE_UNKNOWN:\r
203                         case DIFFSTATE_EMPTY:\r
204                                 rightstate.linestates[i] = m_pViewData->GetState(i);\r
205                                 m_pViewData->SetState(i, state);\r
206                                 break;\r
207                         case DIFFSTATE_YOURSADDED:\r
208                         case DIFFSTATE_IDENTICALADDED:\r
209                         case DIFFSTATE_NORMAL:\r
210                         case DIFFSTATE_THEIRSADDED:\r
211                         case DIFFSTATE_ADDED:\r
212                         case DIFFSTATE_CONFLICTADDED:\r
213                         case DIFFSTATE_CONFLICTED:\r
214                         case DIFFSTATE_CONFLICTED_IGNORED:\r
215                         case DIFFSTATE_IDENTICALREMOVED:\r
216                         case DIFFSTATE_REMOVED:\r
217                         case DIFFSTATE_THEIRSREMOVED:\r
218                         case DIFFSTATE_YOURSREMOVED:\r
219                                 rightstate.linestates[i] = m_pViewData->GetState(i);\r
220                                 m_pViewData->SetState(i, DIFFSTATE_NORMAL);\r
221                                 leftstate.linestates[i] = m_pwndLeft->m_pViewData->GetState(i);\r
222                                 m_pwndLeft->m_pViewData->SetState(i, DIFFSTATE_NORMAL);\r
223                                 break;\r
224                         default:\r
225                                 break;\r
226                         }\r
227                         SetModified();\r
228                         if (m_pwndLocator)\r
229                                 m_pwndLocator->DocumentUpdated();\r
230                 }\r
231         }\r
232         CUndo::GetInstance().AddState(leftstate, rightstate, bottomstate, m_ptCaretPos);\r
233         if (refreshViews)\r
234                 RefreshViews();\r
235 }\r
236 \r
237 void CRightView::UseBlock(bool refreshViews /* = true */)\r
238 {\r
239         viewstate rightstate;\r
240         viewstate bottomstate;\r
241         viewstate leftstate;\r
242 \r
243         if ((m_nSelBlockStart == -1)||(m_nSelBlockEnd == -1))\r
244                 return;\r
245         if (m_pwndBottom->IsWindowVisible())\r
246         {\r
247                 for (int i=m_nSelBlockStart; i<=m_nSelBlockEnd; i++)\r
248                 {\r
249                         bottomstate.difflines[i] = m_pwndBottom->m_pViewData->GetLine(i);\r
250                         m_pwndBottom->m_pViewData->SetLine(i, m_pViewData->GetLine(i));\r
251                         bottomstate.linestates[i] = m_pwndBottom->m_pViewData->GetState(i);\r
252                         m_pwndBottom->m_pViewData->SetState(i, m_pViewData->GetState(i));\r
253                         m_pwndBottom->m_pViewData->SetLineEnding(i, EOL_AUTOLINE);\r
254                         if (m_pwndBottom->IsLineConflicted(i))\r
255                         {\r
256                                 if (m_pViewData->GetState(i) == DIFFSTATE_CONFLICTEMPTY)\r
257                                         m_pwndBottom->m_pViewData->SetState(i, DIFFSTATE_CONFLICTRESOLVEDEMPTY);\r
258                                 else\r
259                                         m_pwndBottom->m_pViewData->SetState(i, DIFFSTATE_CONFLICTRESOLVED);\r
260                         }\r
261                 }\r
262                 m_pwndBottom->SetModified();\r
263         }\r
264         else\r
265         {\r
266                 for (int i=m_nSelBlockStart; i<=m_nSelBlockEnd; i++)\r
267                 {\r
268                         rightstate.difflines[i] = m_pViewData->GetLine(i);\r
269                         m_pViewData->SetLine(i, m_pwndLeft->m_pViewData->GetLine(i));\r
270                         m_pViewData->SetLineEnding(i, EOL_AUTOLINE);\r
271                         DiffStates state = m_pwndLeft->m_pViewData->GetState(i);\r
272                         switch (state)\r
273                         {\r
274                         case DIFFSTATE_ADDED:\r
275                         case DIFFSTATE_CONFLICTADDED:\r
276                         case DIFFSTATE_CONFLICTED:\r
277                         case DIFFSTATE_CONFLICTED_IGNORED:\r
278                         case DIFFSTATE_CONFLICTEMPTY:\r
279                         case DIFFSTATE_IDENTICALADDED:\r
280                         case DIFFSTATE_NORMAL:\r
281                         case DIFFSTATE_THEIRSADDED:\r
282                         case DIFFSTATE_UNKNOWN:\r
283                         case DIFFSTATE_YOURSADDED:\r
284                         case DIFFSTATE_EMPTY:\r
285                                 rightstate.linestates[i] = m_pViewData->GetState(i);\r
286                                 m_pViewData->SetState(i, state);\r
287                                 break;\r
288                         case DIFFSTATE_IDENTICALREMOVED:\r
289                         case DIFFSTATE_REMOVED:\r
290                         case DIFFSTATE_THEIRSREMOVED:\r
291                         case DIFFSTATE_YOURSREMOVED:\r
292                                 rightstate.linestates[i] = m_pViewData->GetState(i);\r
293                                 m_pViewData->SetState(i, DIFFSTATE_ADDED);\r
294                                 break;\r
295                         default:\r
296                                 break;\r
297                         }\r
298                 }\r
299                 SetModified();\r
300         }\r
301         CUndo::GetInstance().AddState(leftstate, rightstate, bottomstate, m_ptCaretPos);\r
302         if (refreshViews)\r
303                 RefreshViews();\r
304 }\r
305 \r
306 void CRightView::UseLeftBeforeRight(bool refreshViews /* = true */)\r
307 {\r
308         viewstate rightstate;\r
309         viewstate bottomstate;\r
310         viewstate leftstate;\r
311         UseBothLeftFirst(rightstate, leftstate);\r
312         CUndo::GetInstance().AddState(leftstate, rightstate, bottomstate, m_ptCaretPos);\r
313         if (refreshViews)\r
314                 RefreshViews();\r
315 }\r
316 \r
317 void CRightView::UseRightBeforeLeft(bool refreshViews /* = true */)\r
318 {\r
319         viewstate rightstate;\r
320         viewstate bottomstate;\r
321         viewstate leftstate;\r
322         UseBothRightFirst(rightstate, leftstate);\r
323         CUndo::GetInstance().AddState(leftstate, rightstate, bottomstate, m_ptCaretPos);\r
324         if (refreshViews)\r
325                 RefreshViews();\r
326 }\r