blob: ff84e6ae3a98cec981f0a19bfcdfdd592135b850 [file] [log] [blame]
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
2 *
3 * This is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This software is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16 * USA.
17 */
18//
19// TXMsgBox.h
20//
21// A TXMsgBox is a specialised pop-up dialog window, designed to present
22// the user with a small amount of textual information, and potentially to
23// obtain their response.
24// TXMsgBoxes are always modal, and may have an Ok button, Ok+Cancel buttons,
25// or Yes+No buttons.
26// The MsgBox helper function creates a TXMsgBox on the fly, runs it, and
27// returns the result.
28//
29
30#ifndef __TXMSGBOX_H__
31#define __TXMSGBOX_H__
32
33#include "TXDialog.h"
34#include "TXLabel.h"
35#include "TXButton.h"
36
37enum TXMsgBoxFlags {
38 MB_OK = 0,
39 MB_OKCANCEL = 1,
40 MB_YESNO = 4,
41 MB_ICONERROR = 0x10,
42 MB_ICONQUESTION = 0x20,
43 MB_ICONWARNING = 0x30,
44 MB_ICONINFORMATION = 0x40,
45 MB_DEFBUTTON1 = 0,
46 MB_DEFBUTTON2 = 0x100
47};
48
49class TXMsgBox : public TXDialog, public TXButtonCallback {
50public:
51 TXMsgBox(Display* dpy, const char* text, unsigned int flags, const char* title=0)
52 : TXDialog(dpy, 1, 1, "Message", true),
53 textLabel(dpy, "", this),
54 okButton(dpy, "OK", this, this, 60),
55 cancelButton(dpy, "Cancel", this, this, 60)
56 {
57 textLabel.xPad = 8;
58 textLabel.move(0, yPad*4);
59 textLabel.setText(text);
60 resize(textLabel.width(),
61 textLabel.height() + okButton.height() + yPad*12);
62
63 switch (flags & 0x30) {
64 case MB_ICONERROR:
65 toplevel("Error", this); break;
66 case MB_ICONQUESTION:
67 toplevel("Question", this); break;
68 case MB_ICONWARNING:
69 toplevel("Warning", this); break;
70 case MB_ICONINFORMATION:
71 toplevel("Information", this); break;
72 default:
73 if (title)
74 toplevel(title, this);
75 break;
76 };
77
78 switch (flags & 0x7) {
79 default:
80 okButton.move((width() - okButton.width()) / 2,
81 height() - yPad*4 - okButton.height());
82 cancelButton.unmap();
83 break;
84 case MB_OKCANCEL:
85 case MB_YESNO:
86
87 okButton.move(((width()/2) - okButton.width()) / 2,
88 height() - yPad*4 - okButton.height());
89 cancelButton.move(((width()*3/2) - cancelButton.width()) / 2,
90 height() - yPad*4 - cancelButton.height());
91 if ((flags & 0x7) == MB_YESNO) {
92 okButton.setText("Yes");
93 cancelButton.setText("No");
94 }
95 break;
96 };
97
98 setBorderWidth(1);
99 }
100
101 virtual void buttonActivate(TXButton* b) {
102 ok = (b == &okButton);
103 done = true;
104 unmap();
105 }
106
107 TXLabel textLabel;
108 TXButton okButton;
109 TXButton cancelButton;
110};
111
112#endif