]> git.saurik.com Git - wxWidgets.git/blob - samples/resource/resource.cpp
*** empty log message ***
[wxWidgets.git] / samples / resource / resource.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: resource.cpp
3 // Purpose: Dialog resource sample
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 #endif
15
16 // For compilers that support precompilation, includes "wx/wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #ifndef WX_PRECOMP
24 #include "wx/wx.h"
25 #endif
26
27 #include "wx/resource.h"
28
29 #include <ctype.h>
30 #include "resource.h"
31
32 // If we wanted to demonstrate total platform independence,
33 // then we'd use the dynamic file loading form for all platforms.
34 // But this shows how to embed the wxWindows resources
35 // in the program code/executable for UNIX and Windows
36 // platforms.
37
38 // If you have a Windows compiler that can cope with long strings,
39 // then you can always use the #include form for simplicity.
40
41 // NOTE: Borland's brc32.exe resource compiler doesn't recognize
42 // the TEXT resource, for some reason, so either run-time file loading
43 // or file inclusion should be used.
44
45 #if defined(__WINDOWS__) && !defined(__BORLANDC__) && !defined(__GNUWIN32__)
46 // Under Windows, some compilers can't include
47 // a whole .wxr file. So we use a .rc user-defined resource
48 // instead. dialog1 will point to the whole .wxr 'file'.
49 static char *dialog1 = NULL;
50 static char *menu1 = NULL;
51 #else
52 // Other platforms should have sensible compilers that
53 // cope with long strings.
54 #include "dialog1.wxr"
55 #include "menu1.wxr"
56 #endif
57
58 // Declare two frames
59 MyFrame *frame = NULL;
60
61 IMPLEMENT_APP(MyApp)
62
63 // Testing of ressources
64 MyApp::MyApp()
65 {
66 }
67
68 // The `main program' equivalent, creating the windows and returning the
69 // main frame
70 bool MyApp::OnInit(void)
71 {
72 #if defined(__WINDOWS__) && !defined(__BORLANDC__)
73 // Load the .wxr 'file' from a .rc resource, under Windows.
74 dialog1 = wxLoadUserResource("dialog1");
75 menu1 = wxLoadUserResource("menu1");
76 // All resources in the file (only one in this case) get parsed
77 // by this call.
78 wxResourceParseString(dialog1);
79 wxResourceParseString(menu1);
80 #else
81 // Simply parse the data pointed to by the variable dialog1.
82 // If there were several resources, there would be several
83 // variables, and this would need to be called several times.
84 wxResourceParseData(dialog1);
85 wxResourceParseData(menu1);
86 #endif
87
88 // Create the main frame window
89 frame = new MyFrame(NULL, -1, "wxWindows Resource Sample", wxPoint(0, 0), wxSize(300, 250));
90
91 // Give it a status line
92 frame->CreateStatusBar(2);
93
94 /*
95 // Make a menubar
96 wxMenu *file_menu = new wxMenu;
97
98 file_menu->Append(RESOURCE_TEST1, "&Dialog box test", "Test dialog box resource");
99 file_menu->Append(RESOURCE_QUIT, "E&xit", "Quit program");
100
101 wxMenuBar *menu_bar = new wxMenuBar;
102
103 menu_bar->Append(file_menu, "&File");
104 */
105
106 wxMenuBar *menu_bar = wxResourceCreateMenuBar("menu1");
107
108 // Associate the menu bar with the frame
109 frame->SetMenuBar(menu_bar);
110
111 // Make a panel
112 frame->panel = new wxWindow(frame, -1, wxPoint(0, 0), wxSize(400, 400), 0, "MyMainFrame");
113 frame->Show(TRUE);
114
115 SetTopWindow(frame);
116
117 return TRUE;
118 }
119
120 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
121 EVT_MENU(RESOURCE_QUIT, MyFrame::OnQuit)
122 EVT_MENU(RESOURCE_TEST1, MyFrame::OnTest1)
123 END_EVENT_TABLE()
124
125 // Define my frame constructor
126 MyFrame::MyFrame(wxWindow *parent, const wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size):
127 wxFrame(parent, id, title, pos, size)
128 {
129 panel = NULL;
130 }
131
132 void MyFrame::OnQuit(wxCommandEvent& event)
133 {
134 Close(TRUE);
135 }
136
137 void MyFrame::OnTest1(wxCommandEvent& event)
138 {
139 MyDialog *dialog = new MyDialog;
140 if (dialog->LoadFromResource(this, "dialog1"))
141 {
142 wxTextCtrl *text = (wxTextCtrl *)wxFindWindowByName("multitext3", dialog);
143 if (text)
144 text->SetValue("wxWindows resource demo");
145 dialog->SetModal(TRUE);
146 dialog->ShowModal();
147 }
148 dialog->Close(TRUE);
149 }
150
151 bool MyFrame::OnClose(void)
152 {
153 Show(FALSE);
154
155 return TRUE;
156 }
157
158 BEGIN_EVENT_TABLE(MyDialog, wxDialog)
159 EVT_BUTTON(RESOURCE_OK, MyDialog::OnOk)
160 EVT_BUTTON(RESOURCE_CANCEL, MyDialog::OnCancel)
161 END_EVENT_TABLE()
162
163
164 void MyDialog::OnOk(wxCommandEvent& event)
165 {
166 EndModal(RESOURCE_OK);
167 }
168
169 void MyDialog::OnCancel(wxCommandEvent& event)
170 {
171 EndModal(RESOURCE_CANCEL);
172 }
173
174