]> git.saurik.com Git - wxWidgets.git/blob - samples/minifram/minifram.cpp
9cd56f413e2e02016d504c06dda01e1c30af1719
[wxWidgets.git] / samples / minifram / minifram.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: minifram.cpp
3 // Purpose: wxMiniFrame sample
4 // Author: Robert Roebling
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Robert Roebling
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #ifndef WX_PRECOMP
20 #include "wx/wx.h"
21 #endif
22
23 #include "wx/toolbar.h"
24 #include "minifram.h"
25
26 #if !defined(__WXMSW__) && !defined(__WXPM__)
27 #include "mondrian.xpm"
28 #endif
29 #include "bitmaps/new.xpm"
30 #include "bitmaps/open.xpm"
31 #include "bitmaps/save.xpm"
32 #include "bitmaps/copy.xpm"
33 #include "bitmaps/cut.xpm"
34 // #include "bitmaps/paste.xpm"
35 #include "bitmaps/print.xpm"
36 #include "bitmaps/preview.xpm"
37 #include "bitmaps/help.xpm"
38
39
40 // start wxWidgets
41
42 IMPLEMENT_APP(MyApp)
43
44 // globas
45
46 MyMainFrame *main_frame = (MyMainFrame*) NULL;
47 MyMiniFrame *mini_frame = (MyMiniFrame*) NULL;
48 bool mini_frame_exists = false;
49 wxButton *button = (wxButton*) NULL;
50
51 #define ID_SET_SIZE_TO_150_150 100
52 #define ID_SET_SIZE_TO_200_200 101
53 #define ID_SET_MAX_SIZE_TO_150_150 102
54 #define ID_SET_MAX_SIZE_TO_300_300 103
55
56 // The `main program' equivalent, creating the windows and returning the
57 // main frame
58 bool MyApp::OnInit()
59 {
60 if ( !wxApp::OnInit() )
61 return false;
62
63 // Create the main frame window
64 main_frame = new MyMainFrame((wxFrame *) NULL, wxID_ANY, _T("wxFrame sample"),
65 wxPoint(100, 100), wxSize(300, 200));
66
67 main_frame->SetSizeHints( 100,100, 400,400 );
68
69 wxMenu *file_menu = new wxMenu;
70 file_menu->Append(wxID_EXIT, _T("E&xit\tAlt-Q"));
71 file_menu->Append(ID_SET_SIZE_TO_150_150, _T("Set frame size to 150,150\tF2"));
72 file_menu->Append(ID_SET_SIZE_TO_200_200, _T("Set frame size to 200,200\tF3"));
73 file_menu->Append(ID_SET_MAX_SIZE_TO_150_150, _T("Set frame max size to 150,150\tF4"));
74 file_menu->Append(ID_SET_MAX_SIZE_TO_300_300, _T("Set frame max size to 300,300\tF5"));
75
76 wxMenuBar *menu_bar = new wxMenuBar;
77 menu_bar->Append(file_menu, _T("&File"));
78 main_frame->SetMenuBar(menu_bar);
79
80 main_frame->Connect( ID_SET_SIZE_TO_150_150, wxID_ANY,
81 wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MyMainFrame::OnSetSize_150_150) );
82 main_frame->Connect( ID_SET_SIZE_TO_200_200, wxID_ANY,
83 wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MyMainFrame::OnSetSize_200_200) );
84 main_frame->Connect( ID_SET_MAX_SIZE_TO_150_150, wxID_ANY,
85 wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MyMainFrame::OnSetMaxSize_150_150) );
86 main_frame->Connect( ID_SET_MAX_SIZE_TO_300_300, wxID_ANY,
87 wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MyMainFrame::OnSetMaxSize_300_300) );
88
89 main_frame->CreateToolBar(wxNO_BORDER|wxTB_VERTICAL, ID_TOOLBAR);
90 InitToolbar(main_frame->GetToolBar());
91
92 button = new wxButton( main_frame, ID_REPARENT, _T("Press to reparent!") );
93
94 // Create the mini frame window
95 mini_frame = new MyMiniFrame( main_frame, wxID_ANY, _T("wxMiniFrame sample"),
96 wxPoint(100, 100), wxSize(220, 100));
97 mini_frame_exists = true;
98
99 mini_frame->CreateToolBar(wxNO_BORDER|wxTB_HORIZONTAL|wxTB_FLAT, ID_TOOLBAR);
100 InitToolbar(mini_frame->GetToolBar());
101
102 main_frame->SetIcon(wxICON(mondrian));
103 mini_frame->SetIcon(wxICON(mondrian));
104
105 SetTopWindow(main_frame);
106
107 main_frame->Show(true);
108 mini_frame->Show(true);
109
110 return true;
111 }
112
113 bool MyApp::InitToolbar(wxToolBar* toolBar)
114 {
115 toolBar->SetMargins(5, 5);
116
117 // Set up toolbar
118 wxBitmap* toolBarBitmaps[8];
119
120 toolBarBitmaps[0] = new wxBitmap( new_xpm );
121 toolBarBitmaps[1] = new wxBitmap( open_xpm );
122 toolBarBitmaps[2] = new wxBitmap( save_xpm );
123 toolBarBitmaps[3] = new wxBitmap( copy_xpm );
124 toolBarBitmaps[4] = new wxBitmap( cut_xpm );
125 // toolBarBitmaps[5] = new wxBitmap( paste_xpm );
126 toolBarBitmaps[5] = new wxBitmap( preview_xpm );
127 toolBarBitmaps[6] = new wxBitmap( print_xpm );
128 toolBarBitmaps[7] = new wxBitmap( help_xpm );
129
130 int width = 16;
131 int currentX = 5;
132
133 toolBar->AddTool(wxID_NEW, *(toolBarBitmaps[0]), wxNullBitmap, false, currentX, wxDefaultCoord, (wxObject *) NULL, _T("New file"));
134 currentX += width + 5;
135 toolBar->AddTool(wxID_OPEN, *(toolBarBitmaps[1]), wxNullBitmap, false, currentX, wxDefaultCoord, (wxObject *) NULL, _T("Open file"));
136 currentX += width + 5;
137 toolBar->AddTool(wxID_SAVE, *(toolBarBitmaps[2]), wxNullBitmap, false, currentX, wxDefaultCoord, (wxObject *) NULL, _T("Save file"));
138 currentX += width + 5;
139 toolBar->AddSeparator();
140 toolBar->AddTool(wxID_COPY, *(toolBarBitmaps[3]), wxNullBitmap, false, currentX, wxDefaultCoord, (wxObject *) NULL, _T("Copy"));
141 currentX += width + 5;
142 toolBar->AddTool(wxID_CUT, *(toolBarBitmaps[4]), wxNullBitmap, false, currentX, wxDefaultCoord, (wxObject *) NULL, _T("Cut"));
143 currentX += width + 5;
144 toolBar->AddTool(wxID_PASTE, *(toolBarBitmaps[5]), wxNullBitmap, false, currentX, wxDefaultCoord, (wxObject *) NULL, _T("Paste"));
145 currentX += width + 5;
146 toolBar->AddSeparator();
147 toolBar->AddTool(wxID_PRINT, *(toolBarBitmaps[6]), wxNullBitmap, false, currentX, wxDefaultCoord, (wxObject *) NULL, _T("Reparent the button"));
148 currentX += width + 5;
149 toolBar->AddSeparator();
150 toolBar->AddTool(wxID_HELP, *(toolBarBitmaps[7]), wxNullBitmap, false, currentX, wxDefaultCoord, (wxObject *) NULL, _T("Help"));
151
152 toolBar->Realize();
153
154 toolBar->EnableTool( wxID_HELP, false );
155
156 // Can delete the bitmaps since they're reference counted
157 int i;
158 for (i = 0; i < 8; i++)
159 delete toolBarBitmaps[i];
160
161 return true;
162 }
163
164 // MyMiniFrame
165
166 BEGIN_EVENT_TABLE(MyMiniFrame, wxMiniFrame)
167 EVT_BUTTON (ID_REPARENT, MyMiniFrame::OnReparent)
168 EVT_MENU (wxID_PRINT, MyMiniFrame::OnReparent)
169 END_EVENT_TABLE()
170
171 MyMiniFrame::MyMiniFrame(wxFrame* parent, wxWindowID id, const wxString& title, const wxPoint& pos,
172 const wxSize& size ) :
173 wxMiniFrame(parent, id, title, pos, size )
174 {
175 }
176
177 bool MyMiniFrame::Destroy()
178 {
179 // make it known that the miniframe is no more
180 mini_frame_exists = false;
181 return wxMiniFrame::Destroy();
182 }
183
184 void MyMiniFrame::OnReparent(wxCommandEvent& WXUNUSED(event))
185 {
186 button->Reparent( main_frame );
187
188 // we need to force the frame to size its (new) child correctly
189 main_frame->SendSizeEvent();
190 }
191
192 // MyMainFrame
193
194 BEGIN_EVENT_TABLE(MyMainFrame, wxFrame)
195 EVT_MENU (wxID_EXIT, MyMainFrame::OnExit)
196 EVT_BUTTON (ID_REPARENT, MyMainFrame::OnReparent)
197 EVT_MENU (wxID_PRINT, MyMainFrame::OnReparent)
198 END_EVENT_TABLE()
199
200 MyMainFrame::MyMainFrame(wxFrame* parent, wxWindowID id, const wxString& title, const wxPoint& pos,
201 const wxSize& size ) :
202 wxFrame(parent, id, title, pos, size )
203 {
204 }
205
206 void MyMainFrame::OnExit(wxCommandEvent&)
207 {
208 Close();
209 }
210
211 void MyMainFrame::OnReparent(wxCommandEvent& WXUNUSED(event))
212 {
213 // practical jokers might find satisfaction in reparenting the button
214 // after closing the mini_frame. We'll have the last laugh.
215 if (! mini_frame_exists)
216 wxMessageBox(_T("The miniframe no longer exists.\n")
217 _T("You don't want to make this button an orphan, do you?"),
218 _T("You got to be kidding"));
219 else
220 {
221 button->Reparent( mini_frame );
222
223 // same as above
224 mini_frame->SendSizeEvent();
225 }
226 }
227
228 void MyMainFrame::OnSetSize_150_150(wxCommandEvent& WXUNUSED(event))
229 {
230 SetSize( 150, 150 );
231 }
232
233 void MyMainFrame::OnSetSize_200_200(wxCommandEvent& WXUNUSED(event))
234 {
235 SetSize( 200, 200 );
236 }
237
238 void MyMainFrame::OnSetMaxSize_150_150(wxCommandEvent& WXUNUSED(event))
239 {
240 SetSizeHints( -1, -1, 150, 150 );
241 }
242
243 void MyMainFrame::OnSetMaxSize_300_300(wxCommandEvent& WXUNUSED(event))
244 {
245 SetSizeHints( -1, -1, 300, 300 );
246 }