OSDN Git Service

b1021b10e809f5fb324db5cf5d587baa00e9802e
[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         CCommentMaker commentMaker;
231         std::vector<TCHAR> chat(commentEdit.GetWindowTextLengthW() + 1);
232         commentEdit.GetWindowTextW(&chat[0],chat.size());
233         commentMaker.SetChatString(&chat[0]);
234         if(this->anonymousButton.GetCheck() == BST_CHECKED){
235
236                 commentMaker.AddMail(ANONYMOUS_MAILCOMMAND);
237         }
238         BOOL rslt = FALSE;
239         NLIB_RESULT code = nicoLiveStream.SendChat(commentMaker);
240
241         switch(code){
242
243         case NLIB_ERR_OK:
244
245                 rslt = TRUE;
246
247                 break;
248
249         case NLIB_ERR_NOT_LIVECONNECT:
250
251                 ::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);
252
253                 rslt = FALSE;
254                 break;
255
256         default:
257                 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));
258                 UnexpectedErrorMessageShow(TEXT("\83R\83\81\83\93\83g\8f\91\82«\8d\9e\82Ý\92\86\82É"),NLIB_GetErrorMessage(code));
259                 rslt = FALSE;
260                 break;
261
262         }
263
264         
265         
266
267         return rslt;
268 }
269
270
271
272 VOID CCommentWriteWindow::ChangeSize(SIZE &size){
273
274         SIZE commentAnonymousButtonSize;
275         SIZE commentWriteButtonSize;
276         commentWriteButton.GetIdealSize(&commentWriteButtonSize);
277         anonymousButton.GetIdealSize(&commentAnonymousButtonSize);
278
279         RECT commentPosBoxRect;
280         RECT commentSizeBoxRect;
281         RECT commentColorBoxRect;
282         RECT anonymousButtonRect;
283         RECT commentEditRect;
284         RECT commentWriteButtonRect;
285
286         RECT thisWindowRect;
287         this->GetWindowRect(&thisWindowRect);
288
289         commentPosBoxRect.left = COMMENTWRITEWINDOW_SIDESEPARATESIZE;
290         this->commentPosBoxCS.Lock();
291         commentPosBoxRect.right = commentPosBoxRect.left + CalcComboBoxWidth<CommentPosInfo>(commentPosBox);
292         this->commentPosBoxCS.Unlock();
293
294         commentSizeBoxRect.left = commentPosBoxRect.right + COMMENTWRITEWINDOW_SIDESEPARATESIZE;
295         this->commentSizeBoxCS.Lock();
296         commentSizeBoxRect.right = commentSizeBoxRect.left + CalcComboBoxWidth<CommentSizeInfo>(commentSizeBox);
297         this->commentSizeBoxCS.Unlock();
298
299         commentColorBoxRect.left = commentSizeBoxRect.right + COMMENTWRITEWINDOW_SIDESEPARATESIZE;
300         commentColorBoxRect.right = commentColorBoxRect.left + commentColorBoxSize.cx;
301
302         CDC cdc(anonymousButton.GetDC());
303         SIZE anonymousStringSize;
304         ::GetTextExtentPoint32(cdc,CHECKBUTTON_HIDDENSTRING,_tcslen(CHECKBUTTON_HIDDENSTRING),&anonymousStringSize);
305         
306         anonymousButtonRect.left = commentColorBoxRect.right + COMMENTWRITEWINDOW_SIDESEPARATESIZE;
307         anonymousButtonRect.right = anonymousButtonRect.left + commentAnonymousButtonSize.cx - anonymousStringSize.cx;
308
309         commentEditRect.left = anonymousButtonRect.right + COMMENTWRITEWINDOW_SIDESEPARATESIZE;
310         
311         commentWriteButtonRect.right = size.cx - COMMENTWRITEWINDOW_SIDESEPARATESIZE;
312         commentWriteButtonRect.left = commentWriteButtonRect.right - commentWriteButtonSize.cx;
313
314         commentEditRect.right = commentWriteButtonRect.left - COMMENTWRITEWINDOW_SIDESEPARATESIZE;
315
316         
317
318         //\88Ú\93®\8f\88\97\9d
319         HDWP hDwp = BeginDeferWindowPos(6);
320         
321         commentPosBox.DeferWindowPos(hDwp,*this,
322                 commentPosBoxRect.left,COMMENTWRITEWINDOW_SEPARATEFSIZE.top,commentPosBoxRect.right - commentPosBoxRect.left,commentWriteInternalHeight,
323                 SWP_NOZORDER);
324
325         commentSizeBox.DeferWindowPos(hDwp,*this,
326                 commentSizeBoxRect.left,COMMENTWRITEWINDOW_SEPARATEFSIZE.top,commentSizeBoxRect.right - commentSizeBoxRect.left,commentWriteInternalHeight,
327                 SWP_NOZORDER);
328
329         commentColorBox.DeferWindowPos(hDwp,*this,
330                 commentColorBoxRect.left,COMMENTWRITEWINDOW_SEPARATEFSIZE.top,commentColorBoxRect.right - commentColorBoxRect.left,commentWriteInternalHeight,
331                 SWP_NOZORDER);
332
333         anonymousButton.DeferWindowPos(hDwp,*this,
334                 anonymousButtonRect.left,COMMENTWRITEWINDOW_SEPARATEFSIZE.top,anonymousButtonRect.right - anonymousButtonRect.left,commentWriteInternalHeight - CHECKBUTTON_SUBSCRIPT_HEIGHT,
335                 SWP_NOZORDER);
336
337         commentEdit.DeferWindowPos(hDwp,*this,
338                 commentEditRect.left,COMMENTWRITEWINDOW_SEPARATEFSIZE.top,commentEditRect.right - commentEditRect.left,commentWriteInternalHeight,
339                 SWP_NOZORDER);
340
341         commentWriteButton.DeferWindowPos(hDwp,*this,
342                 commentWriteButtonRect.left,COMMENTWRITEWINDOW_SEPARATEFSIZE.top,commentWriteButtonRect.right - commentWriteButtonRect.left,commentWriteInternalHeight,
343                 SWP_NOZORDER);
344
345         EndDeferWindowPos(hDwp);
346
347         return;
348 }
349
350 VOID CCommentWriteWindow::OnDrawItem(UINT wParam,LPDRAWITEMSTRUCT lParam){
351         
352         
353
354         if(lParam->hwndItem == commentColorBox.m_hWnd && commentColorBox.GetCount() > 0){
355                 
356                 
357
358                 RECT drawRect = lParam->rcItem;
359                 UINT_PTR itemNo = 0;
360                 INT_PTR curselNo = commentColorBox.GetCurSel();
361                 CDCHandle dcHandle = lParam->hDC;
362                 CBrush bkBrush;
363                 const UINT_PTR width = 1;
364                 drawRect.left += width;
365                 drawRect.top += width;
366                 drawRect.right -= width;
367                 drawRect.bottom -= width;
368
369                 if(lParam->itemID == ((UINT)-1)){
370                         itemNo = curselNo == CB_ERR ? 0 : curselNo;
371                 } else {
372                         itemNo = lParam->itemID;
373                 }
374                 
375                 CommentInfo<COLORREF> &commentColor = *(CommentInfo<COLORREF> *)commentColorBox.GetItemDataPtr(itemNo);
376                 
377                 
378                 if(lParam->itemState & (ODS_FOCUS | ODS_COMBOBOXEDIT)){
379                         
380                         
381                         
382                         bkBrush.CreateSolidBrush(RGB(160,160,160));
383                                 
384                 } else {
385
386                         bkBrush.CreateSolidBrush(RGB(255,255,255));
387
388                 }
389
390
391                 
392                 
393                 CBrush brush;
394                 CPen cpen;
395
396                 brush.CreateSolidBrush(commentColor.userData);
397                 dcHandle.SelectBrush(brush);
398                 dcHandle.FillRect(&lParam->rcItem,bkBrush);
399                 dcHandle.SelectBrush(brush);
400                 dcHandle.SelectPen((HPEN)::GetStockObject(BLACK_PEN));
401                 dcHandle.Rectangle(&drawRect);
402                 
403         }
404
405         return;
406 }
407
408 LRESULT CCommentWriteWindow::OnEraseBkGnd(HDC wParam){
409
410         return TRUE;
411 }
412
413
414 LRESULT CCommentWriteWindow::OnButton(UINT hiWparam,INT_PTR loWparam,HWND lParam){
415
416         if(lParam == commentWriteButton){
417                 
418                 this->SendChat();
419
420         }
421
422         return TRUE;
423 }
424
425 VOID CCommentWriteWindow::OnMeasureItem(UINT wParam,LPMEASUREITEMSTRUCT lParam){
426
427         switch(lParam->CtlID){
428
429         case COMMENTCOLOR_BOX:
430                 {
431                         SIZE itemSize;
432                         CPaintDC pdc(*this);
433                         pdc.SelectFont(AtlGetDefaultGuiFont());
434                         LPCTSTR sample = TEXT(" ");
435                         ::GetTextExtentPoint32(pdc,sample,_tcslen(sample),&itemSize);
436                         lParam->itemHeight = itemSize.cy;
437
438                 }
439                 break;
440
441         }
442
443         
444
445         return;
446 }
447
448
449
450
451 VOID CCommentWriteWindow::OnSendChatResult(){
452
453         commentSubEdit.AgreeEnter();
454         return;
455 }
456
457
458
459
460
461
462 //
463 //CSubChatEdit
464 ////////////////////////////////////////
465
466 void CSubChatEdit::OnKeyDown(TCHAR wParam, UINT upLParam,UINT loLParam){
467         if(wParam == VK_RETURN){
468                                         
469                 cs.Lock();
470                                         
471                 if(untilConnectingFlag == FALSE){
472                         untilConnectingFlag = TRUE;
473                         ;
474
475                         if(self.SendChat() != TRUE){
476
477                                 _beginthread(agreeEnterKeyThread,0,this);
478
479                         }
480
481                 
482
483         
484                 }
485                                         
486                 cs.Unlock();
487                                         
488                                         
489         }
490         return;
491 }
492
493
494 VOID CSubChatEdit::AgreeEnter(){
495         cs.Lock();
496         untilConnectingFlag = FALSE;
497         cs.Unlock();
498 }
499
500 }