]> git.saurik.com Git - wxWidgets.git/blame - samples/dialogs/dialogs.cpp
Small modification
[wxWidgets.git] / samples / dialogs / dialogs.cpp
CommitLineData
457814b5
JS
1/////////////////////////////////////////////////////////////////////////////
2// Name: dialogs.cpp
3// Purpose: Common dialogs demo
4// Author: Julian Smart
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id$
8// Copyright: (c) Julian Smart and Markus Holzem
9// Licence: wxWindows license
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
13#pragma implementation
14#pragma interface
15#endif
16
17// For compilers that support precompilation, includes "wx/wx.h".
18#include "wx/wxprec.h"
19
20#ifdef __BORLANDC__
21#pragma hdrstop
22#endif
23
24#ifndef WX_PRECOMP
25#include "wx/wx.h"
26#endif
27
28#include <wx/colordlg.h>
29#include <wx/filedlg.h>
30#include <wx/dirdlg.h>
31#include <wx/fontdlg.h>
32#include <wx/choicdlg.h>
33
2049ba38 34#if !defined(__WXMSW__) || USE_GENERIC_DIALOGS_IN_MSW
457814b5
JS
35#include <wx/generic/colrdlgg.h>
36#include <wx/generic/fontdlgg.h>
37#endif
38
39#include "dialogs.h"
40
41IMPLEMENT_APP(MyApp)
42
43MyCanvas *myCanvas = NULL;
44
45// A macro needed for some compilers (AIX) that need 'main' to be defined
46// in the application itself.
47IMPLEMENT_WXWIN_MAIN
48
49// `Main program' equivalent, creating windows and returning main app frame
50bool MyApp::OnInit(void)
51{
52 m_canvasTextColour = wxColour("BLACK");
66bd6b93 53 m_canvasFont = *wxNORMAL_FONT;
457814b5
JS
54
55 // Create the main frame window
56 MyFrame *frame = new MyFrame(NULL, "wxWindows dialogs example", wxPoint(50, 50), wxSize(400, 300));
57
58 // Make a menubar
59 wxMenu *file_menu = new wxMenu;
60
61 file_menu->Append(DIALOGS_CHOOSE_COLOUR, "&Choose colour");
62
2049ba38 63#if !defined(__WXMSW__) || USE_GENERIC_DIALOGS_IN_MSW
457814b5
JS
64 file_menu->Append(DIALOGS_CHOOSE_COLOUR_GENERIC, "Choose colour (&generic)");
65#endif
66
67 file_menu->AppendSeparator();
68 file_menu->Append(DIALOGS_CHOOSE_FONT, "Choose &font");
69
2049ba38 70#if !defined(__WXMSW__) || USE_GENERIC_DIALOGS_IN_MSW
457814b5
JS
71 file_menu->Append(DIALOGS_CHOOSE_FONT_GENERIC, "Choose f&ont (generic)");
72
73#endif
74 file_menu->AppendSeparator();
75 file_menu->Append(DIALOGS_MESSAGE_BOX, "&Message box");
76 file_menu->Append(DIALOGS_TEXT_ENTRY, "Text &entry");
77 file_menu->Append(DIALOGS_SINGLE_CHOICE, "&Single choice");
78 file_menu->AppendSeparator();
79 file_menu->Append(DIALOGS_FILE_OPEN, "&Open file");
80 file_menu->Append(DIALOGS_FILE_SAVE, "Sa&ve file");
81 file_menu->Append(DIALOGS_DIR_CHOOSE, "&Choose a directory");
82 file_menu->AppendSeparator();
83 file_menu->Append(wxID_EXIT, "E&xit");
84 wxMenuBar *menu_bar = new wxMenuBar;
85 menu_bar->Append(file_menu, "&File");
86 frame->SetMenuBar(menu_bar);
87
88 myCanvas = new MyCanvas(frame);
89 myCanvas->SetBackgroundColour(*wxWHITE);
90
91 frame->Centre(wxBOTH);
92
93 // Show the frame
94 frame->Show(TRUE);
95
96 SetTopWindow(frame);
97
98 return TRUE;
99}
100
101// My frame constructor
102MyFrame::MyFrame(wxWindow *parent, const wxString& title, const wxPoint& pos, const wxSize& size):
103 wxFrame(parent, -1, title, pos, size)
104{}
105
d355d3fe 106void MyFrame::ChooseColour(wxCommandEvent& WXUNUSED(event) )
457814b5
JS
107{
108 wxColourData data;
109 data.SetChooseFull(TRUE);
110 for (int i = 0; i < 16; i++)
111 {
112 wxColour colour(i*16, i*16, i*16);
113 data.SetCustomColour(i, colour);
114 }
115
116 wxColourDialog *dialog = new wxColourDialog(this, &data);
117 if (dialog->ShowModal() == wxID_OK)
118 {
119 wxColourData retData = dialog->GetColourData();
120 wxColour col = retData.GetColour();
121// wxBrush *brush = wxTheBrushList->FindOrCreateBrush(&col, wxSOLID);
122 myCanvas->SetBackgroundColour(col);
123 myCanvas->Clear();
124 myCanvas->Refresh();
125 }
126 dialog->Close();
127}
128
d355d3fe 129void MyFrame::ChooseFont(wxCommandEvent& WXUNUSED(event) )
457814b5
JS
130{
131 wxFontData data;
132 data.SetInitialFont(wxGetApp().m_canvasFont);
133 data.SetColour(wxGetApp().m_canvasTextColour);
134
135 wxFontDialog *dialog = new wxFontDialog(this, &data);
136 if (dialog->ShowModal() == wxID_OK)
137 {
138 wxFontData retData = dialog->GetFontData();
139 wxGetApp().m_canvasFont = retData.GetChosenFont();
140 wxGetApp().m_canvasTextColour = retData.GetColour();
141 myCanvas->Refresh();
142 }
143 dialog->Close();
144}
145
2049ba38 146#if !defined(__WXMSW__) || USE_GENERIC_DIALOGS_IN_MSW
d355d3fe 147void MyFrame::ChooseColourGeneric(wxCommandEvent& WXUNUSED(event))
457814b5
JS
148{
149 wxColourData data;
150 data.SetChooseFull(TRUE);
151 for (int i = 0; i < 16; i++)
152 {
153 wxColour colour(i*16, i*16, i*16);
154 data.SetCustomColour(i, colour);
155 }
156
157 wxGenericColourDialog *dialog = new wxGenericColourDialog(this, &data);
158 if (dialog->ShowModal() == wxID_OK)
159 {
160 wxColourData retData = dialog->GetColourData();
161 wxColour col = retData.GetColour();
162// wxBrush *brush = wxTheBrushList->FindOrCreateBrush(&col, wxSOLID);
163 myCanvas->SetBackgroundColour(col);
164 myCanvas->Clear();
165 myCanvas->Refresh();
166 }
167 dialog->Close();
168}
169
d355d3fe 170void MyFrame::ChooseFontGeneric(wxCommandEvent& WXUNUSED(event) )
457814b5
JS
171{
172 wxFontData data;
173 data.SetInitialFont(wxGetApp().m_canvasFont);
174 data.SetColour(wxGetApp().m_canvasTextColour);
175
176 wxGenericFontDialog *dialog = new wxGenericFontDialog(this, &data);
177 if (dialog->ShowModal() == wxID_OK)
178 {
179 wxFontData retData = dialog->GetFontData();
180 wxGetApp().m_canvasFont = retData.GetChosenFont();
181 wxGetApp().m_canvasTextColour = retData.GetColour();
182 myCanvas->Refresh();
183 }
184 dialog->Close();
185}
186#endif
187
d355d3fe 188void MyFrame::MessageBox(wxCommandEvent& WXUNUSED(event) )
457814b5
JS
189{
190 wxMessageDialog dialog(this, "This is a message box\nA long, long string to test out the message box properly",
191 "Message box text", wxYES_NO|wxCANCEL);
192
193 dialog.ShowModal();
194}
195
d355d3fe 196void MyFrame::TextEntry(wxCommandEvent& WXUNUSED(event) )
457814b5
JS
197{
198 wxTextEntryDialog dialog(this, "This is a small sample\nA long, long string to test out the text entrybox",
199 "Please enter a string", "Default value", wxOK|wxCANCEL);
200
201 if (dialog.ShowModal() == wxID_OK)
202 {
203 wxMessageDialog dialog2(this, dialog.GetValue(), "Got string");
204 dialog2.ShowModal();
205 }
206}
207
d355d3fe 208void MyFrame::SingleChoice(wxCommandEvent& WXUNUSED(event) )
457814b5 209{
ef77f91e
JS
210 const wxString choices[] = { "One", "Two", "Three", "Four", "Five" } ;
211 int n = 5;
457814b5 212
ef77f91e
JS
213 wxSingleChoiceDialog dialog(this, "This is a small sample\nA single-choice convenience dialog",
214 "Please select a value", n, (const wxString *)choices);
457814b5 215
ef77f91e
JS
216 dialog.SetSelection(2);
217
218 if (dialog.ShowModal() == wxID_OK)
219 {
220 wxMessageDialog dialog2(this, dialog.GetStringSelection(), "Got string");
221 dialog2.ShowModal();
222 }
457814b5
JS
223}
224
d355d3fe 225void MyFrame::FileOpen(wxCommandEvent& WXUNUSED(event) )
457814b5
JS
226{
227 wxFileDialog dialog(this, "Testing open file dialog", "", "", "*.txt", 0);
228
229 if (dialog.ShowModal() == wxID_OK)
230 {
231 wxMessageDialog dialog2(this, dialog.GetPath(), "Selected path");
232 dialog2.ShowModal();
233 }
234}
235
d355d3fe 236void MyFrame::FileSave(wxCommandEvent& WXUNUSED(event) )
457814b5
JS
237{
238 wxFileDialog dialog(this, "Testing save file dialog", "", "",
239 "Text files (*.txt)|*.txt|Document files (*.doc)|*.doc",
240 wxSAVE|wxOVERWRITE_PROMPT);
241
242 if (dialog.ShowModal() == wxID_OK)
243 {
244 char buf[400];
245 sprintf(buf, "%s, filter %d", (const char *)dialog.GetPath(), dialog.GetFilterIndex());
246 wxMessageDialog dialog2(this, wxString(buf), "Selected path");
247 dialog2.ShowModal();
248 }
249}
250
d355d3fe 251void MyFrame::DirChoose(wxCommandEvent& WXUNUSED(event) )
457814b5 252{
d355d3fe 253#ifndef __WXGTK__
457814b5
JS
254 wxDirDialog dialog(this, "Testing directory picker", "");
255
256 if (dialog.ShowModal() == wxID_OK)
257 {
258 wxMessageDialog dialog2(this, dialog.GetPath(), "Selected path");
259 dialog2.ShowModal();
260 }
d355d3fe 261#endif
457814b5
JS
262}
263
d355d3fe 264void MyFrame::OnExit(wxCommandEvent& WXUNUSED(event) )
457814b5
JS
265{
266 Close(TRUE);
267}
268
d355d3fe 269void MyCanvas::OnPaint(wxPaintEvent& WXUNUSED(event) )
457814b5
JS
270{
271 wxPaintDC dc(this);
272 dc.SetFont(wxGetApp().m_canvasFont);
273 dc.SetTextForeground(wxGetApp().m_canvasTextColour);
274 dc.SetBackgroundMode(wxTRANSPARENT);
275 dc.DrawText("wxWindows common dialogs test application", 10, 10);
276}
277
278BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
279 EVT_PAINT(MyCanvas::OnPaint)
280END_EVENT_TABLE()
281
282BEGIN_EVENT_TABLE(MyFrame, wxFrame)
283 EVT_MENU(DIALOGS_CHOOSE_COLOUR, MyFrame::ChooseColour)
284 EVT_MENU(DIALOGS_CHOOSE_FONT, MyFrame::ChooseFont)
285 EVT_MENU(DIALOGS_MESSAGE_BOX, MyFrame::MessageBox)
286 EVT_MENU(DIALOGS_TEXT_ENTRY, MyFrame::TextEntry)
287 EVT_MENU(DIALOGS_SINGLE_CHOICE, MyFrame::SingleChoice)
288 EVT_MENU(DIALOGS_FILE_OPEN, MyFrame::FileOpen)
289 EVT_MENU(DIALOGS_FILE_SAVE, MyFrame::FileSave)
290 EVT_MENU(DIALOGS_DIR_CHOOSE, MyFrame::DirChoose)
2049ba38 291#if !defined(__WXMSW__) || USE_GENERIC_DIALOGS_IN_MSW
457814b5
JS
292 EVT_MENU(DIALOGS_CHOOSE_COLOUR_GENERIC, MyFrame::ChooseColourGeneric)
293 EVT_MENU(DIALOGS_CHOOSE_FONT_GENERIC, MyFrame::ChooseFontGeneric)
294#endif
295 EVT_MENU(wxID_EXIT, MyFrame::OnExit)
296END_EVENT_TABLE()
297