1 #include "studiox_includes.h"
2 #include "message_dialog.h"
3 
4 #ifdef _DEBUG
5 #define new DEBUG_NEW
6 #endif
7 
8 BEGIN_MESSAGE_MAP(message_dialog, express_dialog)
9     ON_BN_CLICKED(IDCANCEL, &message_dialog::OnCancel)
10     ON_BN_CLICKED(IDOK, &message_dialog::OnOK)
11     ON_WM_NCHITTEST()
12     ON_WM_CLOSE()
13     ON_WM_SIZE()
14     ON_WM_PAINT()
15     ON_MESSAGE(STUDIO_TEST, OnTestMessage)
16 END_MESSAGE_MAP()
17 
18 enum message_dialog_test_commands
19 {
20     TEST_YES_TO_MESSAGE_DIALOG = 1,
21     TEST_NO_TO_MESSAGE_DIALOG,
22     TEST_CLOSE_MESSAGE_DIALOG
23 };
24 
25 extern CFont MediumFont;
26 extern CFont NormalFont;
27 
IMPLEMENT_DYNAMIC(message_dialog,express_dialog)28 IMPLEMENT_DYNAMIC(message_dialog, express_dialog)
29 ///////////////////////////////////////////////////////////////////////////////
30 message_dialog::message_dialog(const char *caption, const char *msg, bool type, CWnd *pParent)
31     :express_dialog(message_dialog::IDD, pParent)
32 {
33     IconId = IDB_WARNING;
34     yesandno = type;
35 
36     mMessage = CString(msg);
37     SetTitleText(CString(caption));
38 }
39 
40 /////////////////////////////////////////////////////////////////////////
41 //////
~message_dialog()42 message_dialog::~message_dialog()
43 {
44 
45 }
46 
47 ///////////////////////////////////////////////////////////////////////////////
OnInitDialog()48 BOOL message_dialog::OnInitDialog()
49 {
50     express_dialog::OnInitDialog();
51 
52     // TODO:  Add extra initialization here
53     if (!mMessage.IsEmpty())
54     {
55         AddCStaticControl(mMessage);
56     }
57 
58     if (yesandno)
59     {
60         AddCancelButton(_T("No"));
61         AddSaveButton(_T("Yes"));
62     }
63     else
64     {
65         AddSaveButton(_T("OK"));
66     }
67 
68     return TRUE;  // return TRUE unless you set the focus to a control
69                   // EXCEPTION: OCX Property Pages should return FALSE
70 }
71 
72 ///////////////////////////////////////////////////////////////////////////////
AddCStaticControl(CString text)73 void message_dialog::AddCStaticControl(CString text)
74 {
75 CRect   client;
76 CRect   size;
77 CString string_process;
78 CString temp_string;
79 CSize   temp_size;
80 int     index;
81 
82     int     i_count = 0;
83     int     index_count = 0;
84     CString string_left = text;
85     CDC    *pDC = this->GetDC();
86     CFont *old_font = pDC->SelectObject(&NormalFont);
87     CSize   tsize = pDC->GetTextExtent(text);
88     pDC->SelectObject(old_font);
89     int     temp_width = 0;
90     int     count = text.Replace(_T("\n"), _T("\n"));
91 
92     GetClientRect(&client);
93 
94     size.top = client.top + m_title_bar_height + 8;
95     size.left = client.left + 16;
96     size.bottom = size.top + tsize.cy;
97     size.right = client.right - 16;
98 
99     /*Large window while it's not enough.*/
100     while (i_count <= count)
101     {
102         index = 0;
103         index_count = string_left.Find(_T("\n"));
104         if (index_count == -1)
105         {
106             string_process = string_left;
107         }
108         else
109         {
110             string_process = string_left.Left(index_count);
111             string_left = string_left.Mid(index_count + 1);
112         }
113 
114         while (index != -1)
115         {
116             index = string_process.Find(' ');
117             temp_string = string_process.Left(index);
118             string_process = string_process.Mid(index + 1);
119             temp_size = pDC->GetTextExtent(temp_string);
120             temp_width += temp_size.cx;
121 
122             if (temp_size.cx > size.right)
123             {
124                 size.right = temp_size.cx + 16;
125                 temp_width = 0;
126                 size.bottom += temp_size.cy;
127                 continue;
128             }
129 
130             if (temp_width < size.right)
131             {
132                 continue;
133             }
134             else
135             {
136                 size.right = temp_width + 1;
137                 temp_width = 0;
138                 size.bottom += temp_size.cy;
139             }
140         }
141         temp_size = pDC->GetTextExtent(string_process);
142         if (temp_size.cx > size.right)
143         {
144             size.right = temp_size.cx + 16;
145         }
146         size.bottom += tsize.cy;
147         i_count++;
148     }
149 
150     size.bottom += tsize.cy + 1;
151     client.right = size.right + 16;
152     client.bottom = size.bottom + m_status_bar_height + 16;
153 
154     MoveWindow(client);
155     CenterWindow();
156 
157     this->ReleaseDC(pDC);
158 
159     msg_text.Create(mMessage, WS_CHILD | WS_VISIBLE | SS_LEFT, size, this);
160     msg_text.SetFont(&NormalFont);
161 
162     msg_text.ShowWindow(TRUE);
163 }
164 
165 ///////////////////////////////////////////////////////////////////////////////
OnTestMessage(WPARAM wParam,LPARAM lParam)166 LRESULT message_dialog::OnTestMessage(WPARAM wParam, LPARAM lParam)
167 {
168     switch (wParam)
169     {
170     case TEST_YES_TO_MESSAGE_DIALOG:
171         message_dialog::OnOK();
172         break;
173 
174     case TEST_NO_TO_MESSAGE_DIALOG:
175     case TEST_CLOSE_MESSAGE_DIALOG:
176         message_dialog::OnCancel();
177 
178         break;
179     }
180 
181     return 0;
182 }