]> git.saurik.com Git - wxWidgets.git/blob - samples/minifram/minifram.cpp
1066f8eeb584e2e108a24468de3d2086ba8699bc
[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 // The `main program' equivalent, creating the windows and returning the
52 // main frame
53 bool MyApp::OnInit()
54 {
55 // Create the main frame window
56 main_frame = new MyMainFrame((wxFrame *) NULL, wxID_ANY, _T("wxFrame sample"),
57 wxPoint(100, 100), wxSize(300, 200));
58
59 // main_frame->SetMinSize( wxSize(100,100) );
60 // main_frame->SetMaxSize( wxSize(400,400) );
61 // same as
62 main_frame->SetSizeHints( 100,100, 400,400 );
63
64 main_frame->CreateToolBar(wxNO_BORDER|wxTB_VERTICAL, ID_TOOLBAR);
65 InitToolbar(main_frame->GetToolBar());
66
67 button = new wxButton( main_frame, ID_REPARENT, _T("Press to reparent!") );
68
69 // Create the mini frame window
70 mini_frame = new MyMiniFrame( main_frame, wxID_ANY, _T("wxMiniFrame sample"),
71 wxPoint(100, 100), wxSize(220, 100));
72 mini_frame_exists = true;
73
74 mini_frame->CreateToolBar(wxNO_BORDER|wxTB_HORIZONTAL|wxTB_FLAT, ID_TOOLBAR);
75 InitToolbar(mini_frame->GetToolBar());
76
77 main_frame->SetIcon(wxICON(mondrian));
78 mini_frame->SetIcon(wxICON(mondrian));
79
80 SetTopWindow(main_frame);
81
82 main_frame->Show(true);
83 mini_frame->Show(true);
84
85 return true;
86 }
87
88 bool MyApp::InitToolbar(wxToolBar* toolBar)
89 {
90 toolBar->SetMargins(5, 5);
91
92 // Set up toolbar
93 wxBitmap* toolBarBitmaps[8];
94
95 toolBarBitmaps[0] = new wxBitmap( new_xpm );
96 toolBarBitmaps[1] = new wxBitmap( open_xpm );
97 toolBarBitmaps[2] = new wxBitmap( save_xpm );
98 toolBarBitmaps[3] = new wxBitmap( copy_xpm );
99 toolBarBitmaps[4] = new wxBitmap( cut_xpm );
100 // toolBarBitmaps[5] = new wxBitmap( paste_xpm );
101 toolBarBitmaps[5] = new wxBitmap( preview_xpm );
102 toolBarBitmaps[6] = new wxBitmap( print_xpm );
103 toolBarBitmaps[7] = new wxBitmap( help_xpm );
104
105 int width = 16;
106 int currentX = 5;
107
108 toolBar->AddTool(wxID_NEW, *(toolBarBitmaps[0]), wxNullBitmap, false, currentX, wxDefaultCoord, (wxObject *) NULL, _T("New file"));
109 currentX += width + 5;
110 toolBar->AddTool(wxID_OPEN, *(toolBarBitmaps[1]), wxNullBitmap, false, currentX, wxDefaultCoord, (wxObject *) NULL, _T("Open file"));
111 currentX += width + 5;
112 toolBar->AddTool(wxID_SAVE, *(toolBarBitmaps[2]), wxNullBitmap, false, currentX, wxDefaultCoord, (wxObject *) NULL, _T("Save file"));
113 currentX += width + 5;
114 toolBar->AddSeparator();
115 toolBar->AddTool(wxID_COPY, *(toolBarBitmaps[3]), wxNullBitmap, false, currentX, wxDefaultCoord, (wxObject *) NULL, _T("Copy"));
116 currentX += width + 5;
117 toolBar->AddTool(wxID_CUT, *(toolBarBitmaps[4]), wxNullBitmap, false, currentX, wxDefaultCoord, (wxObject *) NULL, _T("Cut"));
118 currentX += width + 5;
119 toolBar->AddTool(wxID_PASTE, *(toolBarBitmaps[5]), wxNullBitmap, false, currentX, wxDefaultCoord, (wxObject *) NULL, _T("Paste"));
120 currentX += width + 5;
121 toolBar->AddSeparator();
122 toolBar->AddTool(wxID_PRINT, *(toolBarBitmaps[6]), wxNullBitmap, false, currentX, wxDefaultCoord, (wxObject *) NULL, _T("Reparent the button"));
123 currentX += width + 5;
124 toolBar->AddSeparator();
125 toolBar->AddTool(wxID_HELP, *(toolBarBitmaps[7]), wxNullBitmap, false, currentX, wxDefaultCoord, (wxObject *) NULL, _T("Help"));
126
127 toolBar->Realize();
128
129 toolBar->EnableTool( wxID_HELP, false );
130
131 // Can delete the bitmaps since they're reference counted
132 int i;
133 for (i = 0; i < 8; i++)
134 delete toolBarBitmaps[i];
135
136 return true;
137 }
138
139 // MyMiniFrame
140
141 BEGIN_EVENT_TABLE(MyMiniFrame, wxMiniFrame)
142 EVT_CLOSE ( MyMiniFrame::OnCloseWindow)
143 EVT_BUTTON (ID_REPARENT, MyMiniFrame::OnReparent)
144 EVT_MENU (wxID_PRINT, MyMiniFrame::OnReparent)
145 END_EVENT_TABLE()
146
147 MyMiniFrame::MyMiniFrame(wxFrame* parent, wxWindowID id, const wxString& title, const wxPoint& pos,
148 const wxSize& size ) :
149 wxMiniFrame(parent, id, title, pos, size )
150 {
151 }
152
153 void MyMiniFrame::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
154 {
155 // make it known that the miniframe is no more
156 mini_frame_exists = false;
157 Destroy();
158 }
159
160 void MyMiniFrame::OnReparent(wxCommandEvent& WXUNUSED(event))
161 {
162 button->Reparent( main_frame );
163
164 // we need to force the frame to size its (new) child correctly
165 main_frame->SendSizeEvent();
166 }
167
168 // MyMainFrame
169
170 BEGIN_EVENT_TABLE(MyMainFrame, wxFrame)
171 EVT_CLOSE ( MyMainFrame::OnCloseWindow)
172 EVT_BUTTON (ID_REPARENT, MyMainFrame::OnReparent)
173 EVT_MENU (wxID_PRINT, MyMainFrame::OnReparent)
174 END_EVENT_TABLE()
175
176 MyMainFrame::MyMainFrame(wxFrame* parent, wxWindowID id, const wxString& title, const wxPoint& pos,
177 const wxSize& size ) :
178 wxFrame(parent, id, title, pos, size )
179 {
180 }
181
182 void MyMainFrame::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
183 {
184 Destroy();
185 }
186
187 void MyMainFrame::OnReparent(wxCommandEvent& WXUNUSED(event))
188 {
189 // practical jokers might find satisfaction in reparenting the button
190 // after closing the mini_frame. We'll have the last laugh.
191 if (! mini_frame_exists)
192 wxMessageBox(_T("The miniframe no longer exists.\n")
193 _T("You don't want to make this button an orphan, do you?"),
194 _T("You got to be kidding"));
195 else
196 {
197 button->Reparent( mini_frame );
198
199 // same as above
200 mini_frame->SendSizeEvent();
201 }
202 }