OSDN Git Service

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