OSDN Git Service

2012/01/24 2:19:30
[nlite/nlite.git] / nlite / nlite_commentWrite.cpp
1 #include "stdafx.h"
2 #include "nlite_include.h"
3
4 namespace nlite{
5
6
7 const static SIZE commentColorBoxSize = {35,0};
8
9 const static LPCTSTR ANONYMOUS_MAILCOMMAND = TEXT("184");
10 const static LPCTSTR ANONYMOUS_BOTTONSTRING = ANONYMOUS_MAILCOMMAND;
11
12 const static LPCTSTR WRITE_BOTTONSTRING = TEXT("\8f\91\82«\8d\9e\82Ý");
13 const static LPCTSTR CHECKBUTTON_HIDDENSTRING = TEXT(" ");
14
15 const static RECT COMMENTWRITEWINDOW_SEPARATEFSIZE = {0,5,0,5};
16 const static UINT_PTR COMMENTWRITEWINDOW_SIDESEPARATESIZE = 5;
17 const static UINT_PTR INIT_COMMENTWRITE_MAXLENGTH = 60;
18 const static UINT_PTR CHECKBUTTON_SUBSCRIPT_HEIGHT = 10;
19
20 const static COLORREF MAX_COLOR = 0xFFFFFF;
21
22 namespace generalUser{
23         const CommentColorInfo COMMENT_COLORLIST[] = {
24                 {TEXT("white"),RGB(0xFF,0xFF,0xFF)},            //\94\92
25                 {TEXT("red"),RGB(0xFF,0x00,0x00)},                      //\90Ô
26                 {TEXT("pink"),RGB(0xFF,0x80,0x80)},                     //\83s\83\93\83N
27                 {TEXT("orange"),RGB(0xFF,0xC0,0x00)},           //\83I\83\8c\83\93\83W
28                 {TEXT("yellow"),RGB(0xFF,0xFF,0x00)},           //\89©\90F
29                 {TEXT("green"),RGB(0x00,0xFF,0x00)},            //\97Î
30                 {TEXT("cyan"),RGB(0x00,0xFF,0xFF)},                     //\8a¦\90F
31                 {TEXT("blue"),RGB(0x00,0x00,0xFF)},                     //\90Â
32                 {TEXT("purple"),RGB(0xC0,0x00,0xFF)},           //\8e\87
33         
34         };
35
36         const CommentSizeInfo COMMENT_SIZELIST[] = {
37                 {TEXT("\95\81\92Ê\81@"),TEXT("medium")},                        //\95\81\92Ê\83T\83C\83Y
38                 {TEXT("\8f¬\81@"),TEXT("small")}                            //\8f¬\83T\83C\83Y
39         };
40
41
42         const CommentPosInfo COMMENT_POSLIST[] = {
43                 {TEXT("\92Ê\8fí\81@"),TEXT("naka")}                           //\92Ê\8fí\95\\8e¦
44         };
45 }
46
47 namespace premiumUser{
48
49         const CommentColorInfo COMMENT_COLORLIST[] = {
50                 {TEXT("black"),RGB(0x00,0x00,0x00)},            //\8d\95
51                 {TEXT("white2"),RGB(0xCC,0xCC,0x99)},           //\94\92\82»\82Ì\82Q
52                 {TEXT("red2"),RGB(0xCC,0x00,0x33)},                     //\90Ô\82»\82Ì\82Q
53                 {TEXT("pink2"),RGB(0xFF,0x33,0xCC)},            //\83s\83\93\83N\82»\82Ì\82Q
54                 {TEXT("orange2"),RGB(0xFF,0x66,0x00)},          //\83I\83\8c\83\93\83W\82»\82Ì\82Q
55                 {TEXT("yellow2"),RGB(0x99,0x99,0x00)},          //\89©\90F\82»\82Ì\82Q
56                 {TEXT("green2"),RGB(0x00,0xCC,0x66)},           //\97Î\82»\82Ì\82Q
57                 {TEXT("cyan2"),RGB(0x00,0xCC,0xCC)},            //\8a¦\90F\82»\82Ì\82Q
58                 {TEXT("blue2"),RGB(0x33,0x99,0xFF)},            //\90Â\82»\82Ì\82Q
59                 {TEXT("purple2"),RGB(0x66,0x33,0xCC)}           //\8e\87\82»\82Ì\82Q
60         //      {TEXT("black2"),RGB(0x66,0x66,0x66)}            //\8d\95\82»\82Ì\82Q\82Í\82È\82¢\81H
61         };
62
63         const CommentSizeInfo COMMENT_SIZELIST[] = {
64                 {TEXT("\91å\81@"),TEXT("big")}                                      //\91å\83T\83C\83Y
65         };
66
67
68
69         const CommentPosInfo COMMENT_POSLIST[] = {
70                 {TEXT("\89º\81@"),TEXT("shita")},                           //\89º\82É\95\\8e¦
71                 {TEXT("\8fã\81@"),TEXT("ue")}                                       //\8fã\82É\95\\8e¦
72         };
73 }
74
75
76
77
78
79 template<typename T>
80 static VOID addItemList(CComboBox &combo,T * colorList,UINT_PTR length){
81
82         UINT_PTR startItemcount = combo.GetCount();
83         for(UINT_PTR index = startItemcount; index < length;index++){
84
85                 combo.AddString(colorList[index - startItemcount].name);
86                 combo.SetItemDataPtr(index,(LPVOID)&colorList[index - startItemcount]);
87
88         }
89
90         return;
91 }
92
93
94
95 template<typename T>
96 static UINT_PTR CalcComboBoxWidth(CComboBox &combo){
97
98         CPaintDC pdc(combo);
99         SIZE size;
100         UINT_PTR rslt = 0;
101         UINT_PTR itemCount = combo.GetCount();
102         LPCTSTR name;
103         for(UINT_PTR index = 0;index < itemCount;index++){
104                 name = ((T*)combo.GetItemDataPtr(index))->name;
105                 ::GetTextExtentPoint32(pdc,name,_tcslen(name),&size);
106                 rslt = rslt < (UINT_PTR)size.cx ? size.cx : rslt;
107         }
108
109         return rslt;
110 }
111
112 CCommentWriteWindow::CCommentWriteWindow(CNicoLiveStream &in_nicoLiveStream):
113 commentWriteHeight(0),
114 commentWriteInternalHeight(0),
115 commentWriteMaxLength(INIT_COMMENTWRITE_MAXLENGTH),
116 nicoLiveStream(in_nicoLiveStream),
117 commentSubEdit(*this)
118 {}
119
120
121
122 LRESULT CCommentWriteWindow::OnCreate(LPCREATESTRUCT lpReateStruct){
123
124         commentPosBox.Create(m_hWnd,0,WC_COMBOBOX,WS_CHILD | WS_VISIBLE | CBS_DROPDOWNLIST ,0,COMMENTPOS_BOX);
125         commentSizeBox.Create(m_hWnd,0,WC_COMBOBOX,WS_CHILD | WS_VISIBLE | CBS_DROPDOWNLIST,0,COMMENTSIZE_BOX);
126         commentColorBox.Create(m_hWnd,0,WC_COMBOBOX,WS_CHILD | WS_VISIBLE  | CBS_DROPDOWNLIST | CBS_OWNERDRAWFIXED        ,0,COMMENTCOLOR_BOX);
127         anonymousButton.Create(m_hWnd,0,WC_BUTTON,WS_CHILD | WS_VISIBLE | BS_CHECKBOX | BS_AUTOCHECKBOX ,0,ANONYMOUS_BUTTON);
128         commentEdit.Create(m_hWnd,0,WC_EDIT,WS_CHILD | WS_VISIBLE | WS_BORDER | ES_AUTOHSCROLL,0,COMMENT_EDIT);
129         commentWriteButton.Create(m_hWnd,0,WC_BUTTON,WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,0,COMMENTWRITE_BOTTON);
130         commentPosBox.SetFont(AtlGetDefaultGuiFont());
131         commentSizeBox.SetFont(AtlGetDefaultGuiFont());
132         commentColorBox.SetFont(AtlGetDefaultGuiFont());
133
134         
135         anonymousButton.SetWindowTextW(CHECKBUTTON_HIDDENSTRING);
136         anonymousButton.SetFont(AtlGetDefaultGuiFont());
137         commentEdit.SetFont(AtlGetDefaultGuiFont());
138         commentEdit.SetLimitText(commentWriteMaxLength);
139         commentWriteButton.SetWindowTextW(WRITE_BOTTONSTRING);
140         commentWriteButton.SetFont(AtlGetDefaultGuiFont());
141         commentEdit.SetWindowTextW(TEXT(""));
142         
143         addItemList(commentColorBox,generalUser::COMMENT_COLORLIST,ARRAY_LENGTH(generalUser::COMMENT_COLORLIST));
144         addItemList(commentSizeBox,generalUser::COMMENT_SIZELIST,ARRAY_LENGTH(generalUser::COMMENT_SIZELIST));
145         addItemList(commentPosBox,generalUser::COMMENT_POSLIST,ARRAY_LENGTH(generalUser::COMMENT_POSLIST));
146         commentColorBox.SetCurSel(0);
147         commentSizeBox.SetCurSel(0);
148         commentPosBox.SetCurSel(0);
149         CFont font;
150         font = AtlGetDefaultGuiFont();
151         CPaintDC pdc(*this);
152         SIZE thisSize;
153         LPCTSTR sample = TEXT(" ");
154         commentSubEdit.SubclassWindow(commentEdit);
155         GetTextExtentPoint32(pdc,sample,_tcslen(sample),&thisSize);
156         commentWriteHeight = thisSize.cy + (CHECKBUTTON_SUBSCRIPT_HEIGHT / 2) + COMMENTWRITEWINDOW_SEPARATEFSIZE.top + COMMENTWRITEWINDOW_SEPARATEFSIZE.bottom;
157         commentWriteInternalHeight = commentWriteHeight - (COMMENTWRITEWINDOW_SEPARATEFSIZE.top + COMMENTWRITEWINDOW_SEPARATEFSIZE.bottom);
158         return 0;
159 }
160
161 VOID CCommentWriteWindow::OnDestroy(){
162         commentPosBox.DestroyWindow();
163         commentSizeBox.DestroyWindow();
164         commentColorBox.DestroyWindow();
165         anonymousButton.DestroyWindow();
166         commentEdit.DestroyWindow();
167         commentWriteButton.DestroyWindow();
168
169 }
170
171 VOID CCommentWriteWindow::OnSizing(UINT wParam,LPRECT lParam){
172
173         RECT client;
174         GetClientRect(&client);
175
176         ChangeSize( _WTYPES_NS::CSize(client.right - client.left,client.bottom - client.top));
177         return;
178 }
179
180 LRESULT CCommentWriteWindow::OnSize(UINT wParam,SIZE lParam){
181         ChangeSize(lParam);
182         return 0;
183 }
184 VOID CCommentWriteWindow::OnPaint(HDC hdc){
185
186         CPaintDC pdc(*this);
187         RECT clientRect;
188         this->GetClientRect(&clientRect);
189
190         pdc.FillRect(&clientRect,COLOR_BTNFACE);
191
192         
193         RECT anonymousRect;
194         SIZE anonymousStringSize;
195
196         anonymousButton.GetWindowRect(&anonymousRect);
197         CPoint anonymousPoint(anonymousRect.left,anonymousRect.top);
198         ::ScreenToClient(*this,&anonymousPoint);
199         anonymousRect.right -= anonymousRect.left;
200         anonymousRect.bottom -= anonymousRect.top;
201         anonymousRect.left = anonymousPoint.x;
202         anonymousRect.top = anonymousPoint.y + anonymousRect.bottom;
203
204
205         ::GetTextExtentPoint32(pdc,ANONYMOUS_BOTTONSTRING,_tcslen(ANONYMOUS_BOTTONSTRING),&anonymousStringSize);
206         anonymousRect.right = anonymousRect.left + anonymousStringSize.cx;
207         anonymousRect.bottom = anonymousRect.top + anonymousStringSize.cy;
208
209         LOGFONT anonymousLogFont = {0};
210         anonymousLogFont.lfHeight = CHECKBUTTON_SUBSCRIPT_HEIGHT;
211         anonymousLogFont.lfCharSet = SHIFTJIS_CHARSET;
212
213         CFont anonymousFont = CreateFontIndirect(&anonymousLogFont);
214         pdc.SelectFont(anonymousFont);
215         pdc.SetBkColor(GetSysColor(COLOR_BTNFACE));
216         pdc.DrawText(ANONYMOUS_BOTTONSTRING,_tcslen(ANONYMOUS_BOTTONSTRING),&anonymousRect,0);
217         
218         return;
219 }
220
221 static VOID agreeEnterKeyThread(LPVOID userData){
222
223         Sleep(1000);
224         ((CSubChatEdit*)userData)->AgreeEnter();
225
226         return;
227 }
228
229 BOOL CCommentWriteWindow::SendChat(){
230         this->commentEdit.SetReadOnly(TRUE);
231         CCommentMaker commentMaker;
232         std::vector<TCHAR> chat(commentEdit.GetWindowTextLengthW() + 1);
233         commentEdit.GetWindowTextW(&chat[0],chat.size());
234         commentMaker.SetChatString(&chat[0]);
235         if(this->anonymousButton.GetCheck() == BST_CHECKED){
236
237                 commentMaker.AddMail(ANONYMOUS_MAILCOMMAND);
238         }
239         BOOL rslt = FALSE;
240         NLIB_RESULT code = nicoLiveStream.SendChat(commentMaker);
241
242         switch(code){
243
244         case NLIB_ERR_OK:
245
246                 rslt = TRUE;
247                 this->commentEdit.SetWindowText(TEXT(""));
248                 break;
249
250         case NLIB_ERR_NOT_LIVECONNECT:
251
252                 ::MessageBox(NULL,NLIB_GetErrorMessage(code),TEXT("\83R\83\81\83\93\83g\8f\91\82«\8d\9e\82Ý\83G\83\89\81["),MB_ICONERROR | MB_OK);
253
254                 rslt = FALSE;
255                 break;
256
257         default:
258                 errprint(TEXT("\83R\83\81\83\93\83g\8f\91\82«\8d\9e\82Ý\92\86\82É\83G\83\89\81[:%s"),NLIB_GetErrorMessage(code));
259                 UnexpectedErrorMessageShow(TEXT("\83R\83\81\83\93\83g\8f\91\82«\8d\9e\82Ý\92\86\82É"),NLIB_GetErrorMessage(code));
260                 rslt = FALSE;
261                 break;
262
263         }
264
265         
266         
267
268         return rslt;
269 }
270
271
272
273 VOID CCommentWriteWindow::ChangeSize(SIZE &size){
274
275         SIZE commentAnonymousButtonSize;
276         SIZE commentWriteButtonSize;
277         commentWriteButton.GetIdealSize(&commentWriteButtonSize);
278         anonymousButton.GetIdealSize(&commentAnonymousButtonSize);
279
280         RECT commentPosBoxRect;
281         RECT commentSizeBoxRect;
282         RECT commentColorBoxRect;
283         RECT anonymousButtonRect;
284         RECT commentEditRect;
285         RECT commentWriteButtonRect;
286
287         RECT thisWindowRect;
288         this->GetWindowRect(&thisWindowRect);
289
290         commentPosBoxRect.left = COMMENTWRITEWINDOW_SIDESEPARATESIZE;
291         this->commentPosBoxCS.Lock();
292         commentPosBoxRect.right = commentPosBoxRect.left + CalcComboBoxWidth<CommentPosInfo>(commentPosBox);
293         this->commentPosBoxCS.Unlock();
294
295         commentSizeBoxRect.left = commentPosBoxRect.right + COMMENTWRITEWINDOW_SIDESEPARATESIZE;
296         this->commentSizeBoxCS.Lock();
297         commentSizeBoxRect.right = commentSizeBoxRect.left + CalcComboBoxWidth<CommentSizeInfo>(commentSizeBox);
298         this->commentSizeBoxCS.Unlock();
299
300         commentColorBoxRect.left = commentSizeBoxRect.right + COMMENTWRITEWINDOW_SIDESEPARATESIZE;
301         commentColorBoxRect.right = commentColorBoxRect.left + commentColorBoxSize.cx;
302
303         CDC cdc(anonymousButton.GetDC());
304         SIZE anonymousStringSize;
305         ::GetTextExtentPoint32(cdc,CHECKBUTTON_HIDDENSTRING,_tcslen(CHECKBUTTON_HIDDENSTRING),&anonymousStringSize);
306         
307         anonymousButtonRect.left = commentColorBoxRect.right + COMMENTWRITEWINDOW_SIDESEPARATESIZE;
308         anonymousButtonRect.right = anonymousButtonRect.left + commentAnonymousButtonSize.cx - anonymousStringSize.cx;
309
310         commentEditRect.left = anonymousButtonRect.right + COMMENTWRITEWINDOW_SIDESEPARATESIZE;
311         
312         commentWriteButtonRect.right = size.cx - COMMENTWRITEWINDOW_SIDESEPARATESIZE;
313         commentWriteButtonRect.left = commentWriteButtonRect.right - commentWriteButtonSize.cx;
314
315         commentEditRect.right = commentWriteButtonRect.left - COMMENTWRITEWINDOW_SIDESEPARATESIZE;
316
317         
318
319         //\88Ú\93®\8f\88\97\9d
320         HDWP hDwp = BeginDeferWindowPos(6);
321         
322         commentPosBox.DeferWindowPos(hDwp,*this,
323                 commentPosBoxRect.left,COMMENTWRITEWINDOW_SEPARATEFSIZE.top,commentPosBoxRect.right - commentPosBoxRect.left,commentWriteInternalHeight,
324                 SWP_NOZORDER);
325
326         commentSizeBox.DeferWindowPos(hDwp,*this,
327                 commentSizeBoxRect.left,COMMENTWRITEWINDOW_SEPARATEFSIZE.top,commentSizeBoxRect.right - commentSizeBoxRect.left,commentWriteInternalHeight,
328                 SWP_NOZORDER);
329
330         commentColorBox.DeferWindowPos(hDwp,*this,
331                 commentColorBoxRect.left,COMMENTWRITEWINDOW_SEPARATEFSIZE.top,commentColorBoxRect.right - commentColorBoxRect.left,commentWriteInternalHeight,
332                 SWP_NOZORDER);
333
334         anonymousButton.DeferWindowPos(hDwp,*this,
335                 anonymousButtonRect.left,COMMENTWRITEWINDOW_SEPARATEFSIZE.top,anonymousButtonRect.right - anonymousButtonRect.left,commentWriteInternalHeight - CHECKBUTTON_SUBSCRIPT_HEIGHT,
336                 SWP_NOZORDER);
337
338         commentEdit.DeferWindowPos(hDwp,*this,
339                 commentEditRect.left,COMMENTWRITEWINDOW_SEPARATEFSIZE.top,commentEditRect.right - commentEditRect.left,commentWriteInternalHeight,
340                 SWP_NOZORDER);
341
342         commentWriteButton.DeferWindowPos(hDwp,*this,
343                 commentWriteButtonRect.left,COMMENTWRITEWINDOW_SEPARATEFSIZE.top,commentWriteButtonRect.right - commentWriteButtonRect.left,commentWriteInternalHeight,
344                 SWP_NOZORDER);
345
346         EndDeferWindowPos(hDwp);
347
348         return;
349 }
350
351 VOID CCommentWriteWindow::OnDrawItem(UINT wParam,LPDRAWITEMSTRUCT lParam){
352         
353         
354
355         if(lParam->hwndItem == commentColorBox.m_hWnd && commentColorBox.GetCount() > 0){
356                 
357                 
358
359                 RECT drawRect = lParam->rcItem;
360                 UINT_PTR itemNo = 0;
361                 INT_PTR curselNo = commentColorBox.GetCurSel();
362                 CDCHandle dcHandle = lParam->hDC;
363                 CBrush bkBrush;
364                 const UINT_PTR width = 1;
365                 drawRect.left += width;
366                 drawRect.top += width;
367                 drawRect.right -= width;
368                 drawRect.bottom -= width;
369
370                 if(lParam->itemID == ((UINT)-1)){
371                         itemNo = curselNo == CB_ERR ? 0 : curselNo;
372                 } else {
373                         itemNo = lParam->itemID;
374                 }
375                 
376                 CommentInfo<COLORREF> &commentColor = *(CommentInfo<COLORREF> *)commentColorBox.GetItemDataPtr(itemNo);
377                 
378                 
379                 if(lParam->itemState & (ODS_FOCUS | ODS_COMBOBOXEDIT)){
380                         
381                         
382                         
383                         bkBrush.CreateSolidBrush(RGB(160,160,160));
384                                 
385                 } else {
386
387                         bkBrush.CreateSolidBrush(RGB(255,255,255));
388
389                 }
390
391
392                 
393                 
394                 CBrush brush;
395                 CPen cpen;
396
397                 brush.CreateSolidBrush(commentColor.userData);
398                 dcHandle.SelectBrush(brush);
399                 dcHandle.FillRect(&lParam->rcItem,bkBrush);
400                 dcHandle.SelectBrush(brush);
401                 dcHandle.SelectPen((HPEN)::GetStockObject(BLACK_PEN));
402                 dcHandle.Rectangle(&drawRect);
403                 
404         }
405
406         return;
407 }
408
409 LRESULT CCommentWriteWindow::OnEraseBkGnd(HDC wParam){
410
411         return TRUE;
412 }
413
414
415 LRESULT CCommentWriteWindow::OnButton(UINT hiWparam,INT_PTR loWparam,HWND lParam){
416
417         if(lParam == commentWriteButton){
418                 
419                 this->SendChat();
420
421         }
422
423         return TRUE;
424 }
425
426 VOID CCommentWriteWindow::OnMeasureItem(UINT wParam,LPMEASUREITEMSTRUCT lParam){
427
428         switch(lParam->CtlID){
429
430         case COMMENTCOLOR_BOX:
431                 {
432                         SIZE itemSize;
433                         CPaintDC pdc(*this);
434                         pdc.SelectFont(AtlGetDefaultGuiFont());
435                         LPCTSTR sample = TEXT(" ");
436                         ::GetTextExtentPoint32(pdc,sample,_tcslen(sample),&itemSize);
437                         lParam->itemHeight = itemSize.cy;
438
439                 }
440                 break;
441
442         }
443
444         
445
446         return;
447 }
448
449
450
451
452 VOID CCommentWriteWindow::OnSendChatResult(){
453
454         commentSubEdit.AgreeEnter();
455         return;
456 }
457
458
459
460
461
462
463 //
464 //CSubChatEdit
465 ////////////////////////////////////////
466
467 void CSubChatEdit::OnKeyDown(TCHAR wParam, UINT upLParam,UINT loLParam){
468         if(wParam == VK_RETURN){
469                                         
470                 cs.Lock();
471                                         
472                 if(untilConnectingFlag == FALSE){
473                         untilConnectingFlag = TRUE;
474                         
475
476                         if(self.SendChat() != TRUE){
477
478                                 _beginthread(agreeEnterKeyThread,0,this);
479
480                         }
481                         
482                 
483
484         
485                 }
486                                         
487                 cs.Unlock();
488                                         
489                                         
490         }
491         return;
492 }
493
494
495 VOID CSubChatEdit::AgreeEnter(){
496         cs.Lock();
497         self.commentEdit.SetReadOnly(FALSE);
498         untilConnectingFlag = FALSE;
499         cs.Unlock();
500 }
501
502 }