OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [host/] [tools/] [configtool/] [common/] [win32/] [messagebox.cpp] - Blame information for rev 790

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
// ####ECOSHOSTGPLCOPYRIGHTBEGIN####                                        
2
// -------------------------------------------                              
3
// This file is part of the eCos host tools.                                
4
// Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.            
5
//
6
// This program is free software; you can redistribute it and/or modify     
7
// it under the terms of the GNU General Public License as published by     
8
// the Free Software Foundation; either version 2 or (at your option) any   
9
// later version.                                                           
10
//
11
// This program is distributed in the hope that it will be useful, but      
12
// WITHOUT ANY WARRANTY; without even the implied warranty of               
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU        
14
// General Public License for more details.                                 
15
//
16
// You should have received a copy of the GNU General Public License        
17
// along with this program; if not, write to the                            
18
// Free Software Foundation, Inc., 51 Franklin Street,                      
19
// Fifth Floor, Boston, MA  02110-1301, USA.                                
20
// -------------------------------------------                              
21
// ####ECOSHOSTGPLCOPYRIGHTEND####                                          
22
// MessageBox.cpp : implementation file
23
//
24
//
25
//===========================================================================
26
//#####DESCRIPTIONBEGIN####
27
//
28
// Author(s):   sdf
29
// Contact(s):  sdf
30
// Date:                1998/10/06
31
// Version:             0.01
32
// Purpose:     
33
// Description: This is the implementation of the messagebox class
34
// Requires:    
35
// Provides:    
36
// See also:    
37
// Known bugs:  
38
// Usage:       
39
//
40
//####DESCRIPTIONEND####
41
//
42
//===========================================================================
43
 
44
//
45
 
46
#include "stdafx.h"
47
#include "MessageBox.h"
48
 
49
#ifdef _DEBUG
50
#define new DEBUG_NEW
51
#undef THIS_FILE
52
static char THIS_FILE[] = __FILE__;
53
#endif
54
 
55
/////////////////////////////////////////////////////////////////////////////
56
// CMessageBox dialog
57
 
58
// Must be global because InitModalIndirect saves the pointer
59
CMessageBox::DLGDATA CMessageBox::DlgData = {
60
        {       DS_MODALFRAME | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU,
61
                0,                 // No controls yet
62
                0,0,0,0}, // Fix up size and position later
63
                0,0,0};   // Default menu, class and title
64
 
65
void CMessageBox::Init()
66
{
67
        m_pFont=CFont::FromHandle(HFONT(GetStockObject(DEFAULT_GUI_FONT)));
68
        m_nFocusButton=-1;
69
        m_nEscapeButton=-1;
70
        m_nJustify=SS_LEFT;
71
        m_nDefaultButton=0;
72
        m_hIcon=NULL;
73
        m_pStaticText=NULL;
74
        m_pStaticIcon=NULL;
75
        m_bTopMost=false;
76
        m_bModeless=false;
77
        m_bDialogCreated=false;
78
        m_pParentNotify=NULL;
79
        m_crText=GetSysColor(COLOR_BTNTEXT);
80
        InitModalIndirect (&DlgData.tmpl,NULL);
81
}
82
 
83
CMessageBox::CMessageBox()
84
        : CDialog()
85
{
86
        Init();
87
        m_strCaption=_T("Error");
88
}
89
 
90
CMessageBox::CMessageBox(const CString &strText,const CString &strCaption/*=_T("Error")*/,UINT Flag/*=MB_OK*/)
91
        : CDialog()
92
{
93
        Init();
94
 
95
        m_strText=strText;
96
        m_strCaption=strCaption;
97
        m_nDefaultButton=((Flag&MB_DEFMASK)>>8);
98
        m_bTopMost=(0!=(Flag&MB_SYSTEMMODAL));
99
        // Use flag to select from amongst standard combinations and
100
        // to select icon.
101
 
102
        switch(Flag&MB_TYPEMASK){
103
                case MB_OK:
104
                        AddButton(_T("OK"),IDOK);
105
                        break;
106
                case MB_OKCANCEL:
107
                        AddButton(_T("OK"),IDOK);
108
                        AddButton(_T("Cancel"),IDCANCEL);
109
                        break;
110
                case MB_ABORTRETRYIGNORE:
111
                        AddButton(_T("&Abort"),IDABORT);
112
                        AddButton(_T("&Retry"),IDRETRY);
113
                        AddButton(_T("&Ignore"),IDIGNORE);
114
                        break;
115
                case MB_YESNOCANCEL:
116
                        AddButton(_T("&Yes"),IDYES);
117
                        AddButton(_T("&No"),IDNO);
118
                        AddButton(_T("Cancel"),IDCANCEL);
119
                        break;
120
                case MB_YESNO:
121
                        AddButton(_T("&Yes"),IDYES);
122
                        AddButton(_T("&No"),IDNO);
123
                        break;
124
                case MB_RETRYCANCEL:
125
                        AddButton(_T("&Retry"),IDRETRY);
126
                        AddButton(_T("Cancel"),IDCANCEL);
127
                        break;
128
                case MB_YESNOALL: //13
129
                        AddButton(_T("&Yes"),IDYES);
130
                        AddButton(_T("&No"),IDNO);
131
                        AddButton(_T("Yes &All"),IDYESALL);
132
                        AddButton(_T("No A&ll"),IDNOALL);
133
                        break;
134
                case MB_YESNOALLCANCEL: //14
135
                        AddButton(_T("&Yes"),IDYES);
136
                        AddButton(_T("&No"),IDNO);
137
                        AddButton(_T("Yes &All"),IDYESALL);
138
                        AddButton(_T("No A&ll"),IDNOALL);
139
                        AddButton(_T("Cancel"),IDCANCEL);
140
                        break;
141
                default:
142
                        ASSERT(FALSE);
143
        }
144
 
145
        if(Flag&MB_HELP){
146
                AddButton(_T("&Help"),IDHELP);
147
        }
148
 
149
        switch(Flag&MB_ICONMASK){
150
                case MB_ICONHAND:
151
                        m_hIcon=AfxGetApp()->LoadStandardIcon(IDI_HAND);
152
                        break;
153
                case MB_ICONQUESTION:
154
                        m_hIcon=AfxGetApp()->LoadStandardIcon(IDI_QUESTION);
155
                        break;
156
                case MB_ICONEXCLAMATION:
157
                        m_hIcon=AfxGetApp()->LoadStandardIcon(IDI_EXCLAMATION);
158
                        break;
159
                case MB_ICONASTERISK:
160
                        m_hIcon=AfxGetApp()->LoadStandardIcon(IDI_ASTERISK);
161
                        break;
162
                case 0:
163
                        break;
164
                default:
165
                        ASSERT(FALSE);
166
                        break;
167
        }
168
}
169
 
170
BEGIN_MESSAGE_MAP(CMessageBox, CDialog)
171
        //{{AFX_MSG_MAP(CMessageBox)
172
        ON_WM_FONTCHANGE()
173
        ON_WM_CTLCOLOR()
174
        ON_WM_CLOSE()
175
        //}}AFX_MSG_MAP
176
        ON_CONTROL_RANGE(BN_CLICKED, 1, 0xFFFF, OnButton)
177
END_MESSAGE_MAP()
178
 
179
/////////////////////////////////////////////////////////////////////////////
180
// CMessageBox message handlers
181
 
182
BOOL CMessageBox::OnInitDialog()
183
{
184
        // Create buttons as required
185
        ASSERT(ButtonCount()>0);
186
 
187
        SetWindowText(m_strCaption);
188
 
189
        if(-1==m_nEscapeButton||IDCANCEL!=m_arBInfo[m_nEscapeButton].m_id){
190
                // No cancel button
191
                CMenu *pMenu=GetSystemMenu(FALSE);
192
                pMenu->RemoveMenu(SC_CLOSE,MF_BYCOMMAND);
193
        }
194
 
195
        CDialog::OnInitDialog();
196
        CDC *pDC=GetDC();
197
        CFont *pOldFont=pDC->SelectObject(m_pFont);
198
        TEXTMETRIC tm;
199
        pDC->GetTextMetrics(&tm);
200
 
201
        int cxDLU=tm.tmAveCharWidth;
202
        int cyDLU=tm.tmHeight;
203
        int nButtonWidth= (60*cxDLU)/4;         // width of a button
204
        int nButtonHeight=(14*cyDLU)/8;         // height of a button
205
        int cxButtonSep=   (4*cxDLU)/4;         // horizontal button separation
206
        int cxTextButtonSep=(10*cxDLU)/4;       // horizontal separation between text and icon
207
        int cyTextButtonSep=(10*cyDLU)/8;       // vertical separation between text and buttons
208
        int cxBorder=   (7*cxDLU)/4;            // horizontal separation between buttons and border
209
        int cyBorder=   (7*cyDLU)/8;            // vertical separation between buttons and border
210
        int cxIcon=GetSystemMetrics(SM_CXICON); // width of an icon
211
        int cyIcon=GetSystemMetrics(SM_CYICON); // height of an icon
212
        int nTotalButtonWidth=(ButtonCount()*nButtonWidth)+(ButtonCount()-1)*cxButtonSep;
213
        int cxText=max(50,nTotalButtonWidth-(m_hIcon?(cxIcon+cxTextButtonSep):0));
214
        int cyText=0;
215
 
216
        // Size the text control according to the maximum line length
217
        LPCTSTR c=m_strText;
218
        while(*c){
219
                PTCHAR d=_tcsstr(c,_T("\r\n"));
220
                int nCount;
221
                if(d){
222
                        *d=_TCHAR('\0');
223
                        nCount=d-c;
224
                } else {\
225
                        nCount=_tcslen(c);
226
                }
227
                cxText=max(cxText,pDC->GetTextExtent(c,nCount).cx);
228
                cyText+=tm.tmHeight;
229
                if(d){
230
                        *d=_TCHAR('\r');
231
                        c=d+2;
232
                } else {
233
                        break;
234
                }
235
        }
236
 
237
        // If vertical extent of text is less than that of the icon, difference between the two
238
        int cyTextExtra= (m_hIcon && cyText<cyIcon)?cyIcon-cyText:0;
239
 
240
        pDC->SelectObject(pOldFont);
241
        ReleaseDC(pDC);
242
 
243
        // Set dialog box size
244
        {
245
                int cx=(2*cxBorder)+cxText+cxButtonSep+2*GetSystemMetrics(SM_CXDLGFRAME);
246
                if(m_hIcon){
247
                        cx+=cxIcon+cxTextButtonSep;
248
                }
249
                int cy=(2*cyBorder)+cyText+cyTextExtra+cyTextButtonSep+nButtonHeight+
250
                        GetSystemMetrics(SM_CYCAPTION)+2*GetSystemMetrics(SM_CYDLGFRAME);
251
                UINT flags=SWP_NOMOVE;
252
                if(!m_bTopMost){
253
                        flags|=SWP_NOZORDER;
254
                }
255
                SetWindowPos(&wndTopMost,0,0,cx,cy,flags);
256
        }
257
 
258
        // Create a static control for the icon
259
        if(m_hIcon){
260
                m_pStaticIcon=new CStatic;
261
                m_pStaticIcon->Create(NULL,WS_CHILD|WS_VISIBLE|SS_ICON,
262
                        CRect(cxBorder,cyBorder,cxBorder+cxIcon,cyBorder+cyIcon), this);
263
                m_pStaticIcon->SetIcon(m_hIcon);
264
        }
265
 
266
        // Create a static control for the text
267
        {
268
                int cx=m_hIcon?cxIcon+cxTextButtonSep:0;
269
                m_pStaticText=new CStatic;
270
                m_pStaticText->Create(m_strText,WS_CHILD|WS_VISIBLE|m_nJustify|SS_NOPREFIX,
271
                        CRect(cxBorder+cx,cyBorder+cyTextExtra/2,cxBorder+cx+cxText,cyBorder+cyText+cyTextExtra/2), this);
272
                m_pStaticText->SetFont(m_pFont);
273
        }
274
 
275
        // Create the buttons
276
        CRect rcClient;
277
        GetClientRect(rcClient);
278
        CRect rect;
279
        rect.left=(rcClient.Width()-nTotalButtonWidth)/2;
280
        rect.right=rect.left+nButtonWidth;
281
        rect.bottom=rcClient.bottom-cyBorder;
282
        rect.top=rect.bottom-nButtonHeight;
283
 
284
        ASSERT(m_nDefaultButton<ButtonCount());
285
 
286
        for(unsigned i=0;i<ButtonCount();i++){
287
                CButton *pWnd=new CButton;
288
                m_arBInfo[i].m_pWnd=pWnd;
289
                UINT id=m_arBInfo[i].m_id;
290
                UINT style=WS_CHILD|WS_VISIBLE|WS_TABSTOP;
291
                if(!m_arBInfo[i].m_bEnabled){
292
                        style|=WS_DISABLED;
293
                }
294
                if(0==i){
295
                        style|=WS_GROUP;
296
                }
297
                style|=(m_nDefaultButton==i)?BS_DEFPUSHBUTTON:BS_PUSHBUTTON;
298
 
299
                pWnd->Create(m_arBInfo[i].m_strCaption,style,rect,this,id);
300
                pWnd->SetFont(m_pFont);
301
                if(m_nDefaultButton==i){
302
                        pWnd->SetFocus();
303
                }
304
                rect.left+=nButtonWidth+cxButtonSep;
305
                rect.right+=nButtonWidth+cxButtonSep;
306
        }
307
 
308
        m_nFocusButton=m_nDefaultButton;
309
        m_bDialogCreated=true;
310
        return FALSE;
311
}
312
 
313
void CMessageBox::OnButton(UINT id)
314
{
315
        if(-1!=IndexOf(id)){
316
                if(m_bModeless){
317
                        if(NULL!=m_pParentNotify){
318
                                m_pParentNotify->PostMessage(m_nParentNotifcationMessage,MAKEWPARAM(id,m_nParentNotifcationwParamHigh),0);
319
                                DestroyWindow();
320
                        }
321
                } else {
322
                        EndDialog(id);
323
                }
324
        }
325
}
326
 
327
BOOL CMessageBox::PreTranslateMessage(MSG* pMsg)
328
{
329
        if(pMsg->message==WM_KEYDOWN){
330
                switch(pMsg->wParam){
331
                        case VK_ESCAPE:
332
                                if(-1!=m_nEscapeButton){
333
                                        OnButton(m_arBInfo[m_nEscapeButton].m_id);
334
                                }
335
                                return TRUE;
336
                        default:
337
                                break;
338
                }
339
        }
340
    if( IsDialogMessage( pMsg ) )
341
                return TRUE;
342
        else
343
        return CDialog::PreTranslateMessage( pMsg );
344
}
345
 
346
void CMessageBox::AddButton(const CString & strCaption, UINT id, bool bEnabled/*=true*/)
347
{
348
#ifdef _DEBUG
349
        ASSERT(-1==IndexOf(id));
350
        for(unsigned int i=0;i<ButtonCount();i++){
351
                if(0==m_arBInfo[i].m_strCaption.Compare(strCaption)){
352
                        ASSERT(FALSE);
353
                }
354
        }
355
#endif
356
        if(bEnabled){
357
                if(IDCANCEL==id || (IDOK==id && -1==m_nEscapeButton)){
358
                        m_nEscapeButton=ButtonCount();
359
                }
360
        }
361
        CButtonInfo info(id,bEnabled,strCaption);
362
        m_arBInfo.Add(info);
363
}
364
 
365
CMessageBox::~CMessageBox()
366
{
367
        for(unsigned int i=0;i<ButtonCount();i++){
368
                deleteZ(m_arBInfo[i].m_pWnd);
369
        }
370
        deleteZ(m_pStaticText);
371
        deleteZ(m_pStaticIcon);
372
}
373
 
374
void CMessageBox::SetDefaultButton(UINT nIndex)
375
{
376
        ASSERT(nIndex<ButtonCount());
377
        m_nDefaultButton=nIndex;
378
}
379
 
380
void CMessageBox::OnFontChange()
381
{
382
        CDialog::OnFontChange();
383
 
384
        m_pFont=CFont::FromHandle(HFONT(GetStockObject(DEFAULT_GUI_FONT)));
385
        for(unsigned int i=0;i<ButtonCount();i++){
386
                Button(i).SetFont(m_pFont);
387
        }
388
        m_pStaticText->SetFont(m_pFont);
389
}
390
 
391
 
392
 
393
HBRUSH CMessageBox::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
394
{
395
        HBRUSH hbr=CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
396
        switch(nCtlColor){
397
                case CTLCOLOR_STATIC:
398
                        pDC->SetTextColor(m_crText);
399
                        break;
400
                default:
401
                        break;
402
        }
403
        return hbr;
404
}
405
 
406
 
407
BOOL CMessageBox::Create(CWnd *pWnd,UINT msg,WORD wParamHigh)
408
{
409
        m_bModeless=true;
410
        if(0!=msg){
411
                ASSERT(NULL!=pWnd);
412
                m_pParentNotify=pWnd;
413
                m_nParentNotifcationMessage=msg;
414
                m_nParentNotifcationwParamHigh=wParamHigh;
415
        }
416
        return CreateIndirect (&DlgData.tmpl,pWnd);
417
}
418
 
419
int CMessageBox::IndexOf(UINT id)
420
{
421
        for(unsigned int i=0;i<ButtonCount();i++){
422
                if(m_arBInfo[i].m_id==id){
423
                        return (signed)i;
424
                }
425
        }
426
        return -1;
427
}
428
 
429
void CMessageBox::PostNcDestroy()
430
{
431
        if(m_bModeless){
432
                delete this;
433
        } else {
434
                CDialog::PostNcDestroy();
435
        }
436
}
437
 
438
void CMessageBox::OnClose()
439
{
440
        OnButton(IDCANCEL);
441
}
442
 
443
void CMessageBox::SetCaption (const CString &strCaption)
444
{
445
        m_strCaption=strCaption;
446
        if(m_bDialogCreated){
447
                SetWindowText(strCaption);
448
        }
449
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.