Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ExampleSizing.cpp
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003-2009 (see file CONTACT for details)
5 Multimediale Systeme der Informationsverarbeitung
6 Institut fuer Informatik
7 Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 
26 /**
27  @example ExampleSizing.cpp
28  @relates GLProjectionParametersPerspective, SceneOpenSceneGraph
29  @brief Example for usage ContextWX, GLProjections and Scenes
30  @ingroup g_examples
31  @author MIP
32 */
33 
34 #include <Base/Common/BIASpragmaStart.hh>
35 #include <wx/wx.h>
36 #include <wx/aboutdlg.h>
37 #include <wx/artprov.h>
38 #include <wx/filename.h>
39 #include <Base/Common/BIASpragmaEnd.hh>
40 
41 #include <Base/Image/Image.hh>
42 #include <Base/Image/ImageIO.hh>
43 #include <Base/Common/FileHandling.hh>
44 #include <Gui/StringConv.hh>
45 
46 using namespace BIAS;
47 using namespace std;
48 
49 /////////////////////////////////////
50 ////////// the window //////////
51 /////////////////////////////////////
52 /** \cond HIDDEN_SYMBOLS */
53 class SizingFrame : public wxFrame
54 {
55 public:
56 
57  SizingFrame(wxWindow *parent, const wxString& title);
58  void OnOpen(wxCommandEvent &event);
59  void OnQuit(wxCommandEvent &event);
60  void OnScreenshot(wxCommandEvent& event);
61  void OnViewSize(wxCommandEvent& event);
62  void OnAbout(wxCommandEvent &event);
63  void OnScreenInfo(wxCommandEvent &event);
64  void OnResize(wxSizeEvent &event);
65 
66  void LateInit();
67 
68 protected:
69 
70  wxStaticBitmap* StaticBitmap_;
71  wxImage* Img_;
72 
73  bool SizeLocked_;
74 
75  enum {
76  ID_Whatever = wxID_HIGHEST + 42,
77  ID_Screenshot,
78  ID_VS_Free,
79  ID_VS_640,
80  ID_VS_800,
81  ID_VS_16_9,
82  ID_VS_16_10,
83  ID_ScreenInfo
84  };
85 
86 private:
87 
88  void DumpSizes_();
89  void FitToImageSize_();
90 
91  wxMenuBar* Menubar_;
92  wxToolBar* Toolbar_;
93  wxStatusBar* Statusbar_;
94  DECLARE_EVENT_TABLE()
95 
96 };
97 /** \endcond */
98 BEGIN_EVENT_TABLE(SizingFrame, wxFrame)
99  EVT_MENU(wxID_OPEN, SizingFrame::OnOpen)
100  EVT_MENU(wxID_EXIT, SizingFrame::OnQuit)
101  EVT_MENU(wxID_ABOUT, SizingFrame::OnAbout)
102  EVT_MENU(ID_Screenshot, SizingFrame::OnScreenshot)
103  EVT_MENU(ID_VS_Free, SizingFrame::OnViewSize)
104  EVT_MENU(ID_VS_640, SizingFrame::OnViewSize)
105  EVT_MENU(ID_VS_800, SizingFrame::OnViewSize)
106  EVT_MENU(ID_VS_16_9, SizingFrame::OnViewSize)
107  EVT_MENU(ID_VS_16_10, SizingFrame::OnViewSize)
108  EVT_MENU(ID_ScreenInfo, SizingFrame::OnScreenInfo)
109  EVT_SIZE(SizingFrame::OnResize)
110 END_EVENT_TABLE()
111 
112 SizingFrame::SizingFrame(wxWindow *parent, const wxString& title)
113 : wxFrame(parent, wxID_ANY, title),
114  SizeLocked_(false)
115 {
116  Img_ = new wxImage(640, 480);
117  Img_->SetRGB(wxRect(50,50,100,100), 255, 255, 0);
118  StaticBitmap_ = new wxStaticBitmap(this, wxID_ANY, wxBitmap(*Img_));
119  // create a status bar
120  Statusbar_ = this->CreateStatusBar( 2, wxST_SIZEGRIP, wxID_ANY );
121  const int numFields = 2;
122  Statusbar_->SetFieldsCount(numFields);
123  const int widths[numFields] = { -1, 170 };
124  Statusbar_->SetStatusWidths(numFields, widths);
125 
126  // create a tool bar
127  Toolbar_ = this->CreateToolBar( wxTB_HORIZONTAL, wxID_ANY );
128  Toolbar_->SetToolBitmapSize( wxSize( 24,24 ) );
129  Toolbar_->SetToolSeparation( 0 );
130  Toolbar_->SetToolPacking( 0 );
131  Toolbar_->AddTool( ID_ScreenInfo, wxT("Screen Info"),
132  wxArtProvider::GetIcon(wxART_INFORMATION,wxART_TOOLBAR) );
133  Toolbar_->Realize();
134 
135  // create a menu bar
136  wxMenu *menuFile = new wxMenu;
137  menuFile->Append(wxID_OPEN);
138  menuFile->AppendSeparator();
139  menuFile->Append(wxID_EXIT);
140  wxMenu *menuEdit = new wxMenu;
141  menuEdit->Append(ID_Screenshot, wxString(wxT("&Screenshot")) + wxT('\t') + wxT("F3"));
142  wxMenu *menuView = new wxMenu;
143  menuView->AppendRadioItem(ID_VS_Free, wxString(wxT("View Size &Free")) + wxT('\t') + wxT("CTRL-SHIFT-0"));
144  menuView->AppendRadioItem(ID_VS_640, wxString(wxT("View Size 4:3 &small")) + wxT('\t') + wxT("CTRL-SHIFT-1"));
145  menuView->AppendRadioItem(ID_VS_800, wxString(wxT("View Size 4:3 &medium")) + wxT('\t') + wxT("CTRL-SHIFT-2"));
146  menuView->AppendRadioItem(ID_VS_16_9, wxString(wxT("View Size 16:&9 medium")) + wxT('\t') + wxT("CTRL-SHIFT-3"));
147  menuView->AppendRadioItem(ID_VS_16_10, wxString(wxT("View Size 16:&10 medium")) + wxT('\t') + wxT("CTRL-SHIFT-4"));
148  wxMenu *menuHelp = new wxMenu;
149  menuHelp->Append(wxID_ABOUT);
150  Menubar_ = new wxMenuBar;
151  Menubar_->Append(menuFile, wxT("&File"));
152  Menubar_->Append(menuEdit, wxT("&Edit"));
153  Menubar_->Append(menuView, wxT("&View"));
154  Menubar_->Append(menuHelp, wxT("&Help"));
155 
156  SetMenuBar(Menubar_);
157  wxBoxSizer* mainSizer = new wxBoxSizer(wxVERTICAL);
158  mainSizer->Add(StaticBitmap_, 1, wxEXPAND|wxALL|wxALIGN_CENTRE, 0);
159  SetMinSize(wxSize(555,333));
160  SetSizer(mainSizer);
161  mainSizer->Fit(this);
162  Layout();
163 
164 }
165 
166 void SizingFrame::
167 LateInit()
168 {
169  FitToImageSize_();
170 }
171 
172 void SizingFrame::
173 OnScreenInfo(wxCommandEvent &event)
174 {
175  wxString msg = wxT("Window size is ");
176  msg << GetSize().GetWidth() << wxT(" x ")
177  << GetSize().GetHeight();
178  msg << wxT("\n") << wxT("Client size is ");
179  msg << GetClientSize().GetWidth() << wxT(" x ")
180  << GetClientSize().GetHeight();
181 
182  wxMessageBox(msg, wxT("Example Sizing - Screen Info"),
183  wxOK | wxICON_INFORMATION, this);
184 
185 }
186 
187 void SizingFrame::
188 OnResize(wxSizeEvent &event)
189 {
190  cout << "size event: window size \t("
191  << GetSize().GetWidth() << " x " << GetSize().GetHeight() << ")\n";
192  event.Skip();
193 }
194 
195 
196 void SizingFrame::
197 OnOpen(wxCommandEvent &event)
198 {
199  event.Skip();
200 }
201 
202 void SizingFrame::
203 OnQuit(wxCommandEvent &event)
204 {
205  Close();
206 }
207 
208 void SizingFrame::
209 OnViewSize(wxCommandEvent& event)
210 {
211  FitToImageSize_();
212 }
213 
214 
215 void SizingFrame::
216 OnScreenshot(wxCommandEvent& event)
217 {
218  wxMessageBox(wxT("Saving screenshot not implemented!"), wxT("Information"),
219  wxOK | wxICON_INFORMATION, this);
220 }
221 
222 
223 void SizingFrame::
224 OnAbout(wxCommandEvent &event)
225 {
226  wxString desc;
227  desc << wxT(" This example is part of the\n");
228  desc << wxT(" BIAS library (Basic ImageAlgorithmS)\n") << wxT("\n\n");
229  wxString lic = wxT("BIAS is free software; you can redistribute it and/or modify\n \
230 it under the terms of the GNU Lesser General Public Licence as published by\n \
231 the Free Software Foundation; either version 2.1 of the Licence, or\n \
232 (at your option) any later version.\n \
233 \n \
234 BIAS is distributed in the hope that it will be useful,\n \
235 but WITHOUT ANY WARRANTY; without even the implied warranty of\n \
236 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n \
237 GNU Lesser General Public Licence for more details.\n \
238 \n \
239 You should have received a copy of the GNU Lesser General Public Licence\n \
240 along with BIAS; if not, write to the Free Software\n \
241 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\n");
242 
243  wxAboutDialogInfo aboutdlg;
244  aboutdlg.SetDescription( desc );
245  aboutdlg.SetLicence( lic );
246  aboutdlg.SetCopyright( wxT("Copyright (C) 2003-2009\n \
247  Multimediale Systeme der Informationsverarbeitung\n \
248  Institut fuer Informatik\n \
249  Christian-Albrechts-Universitaet Kiel") );
250  aboutdlg.SetWebSite( wxT("http://www.mip.informatik.uni-kiel.de/") );
251 
252  wxAboutBox( aboutdlg );
253 }
254 
255 
256 void SizingFrame::
257 DumpSizes_()
258 {
259  static unsigned run = 0;
260  cout << "****** loads of sizes (no. " << run++ << ") *****\n";
261  cout << "size locked? " << boolalpha << SizeLocked_ << endl;
262  cout << "window size \t(" << GetSize().GetWidth() << " x " << GetSize().GetHeight() << ")\n";
263  cout << "client size \t(" << GetClientSize().GetWidth() << " x " << GetClientSize().GetHeight() << ")\n";
264  //cout << "menubar size \t(" << Menubar_->GetSize().GetWidth() << " x " << Menubar_->GetSize().GetHeight() << ")\n";
265  //cout << "toolbar size \t(" << Toolbar_->GetSize().GetWidth() << " x " << Toolbar_->GetSize().GetHeight() << ")\n";
266  //cout << "statusbar size \t(" << Statusbar_->GetSize().GetWidth() << " x " << Statusbar_->GetSize().GetHeight() << ")\n";
267  cout << "static bitmap size \t(" << StaticBitmap_->GetSize().GetWidth() << " x " << StaticBitmap_->GetSize().GetHeight() << ")\n\n";
268 
269 }
270 
271 
272 void SizingFrame::
273 FitToImageSize_()
274 {
275  //DumpSizes_();
276  //wxSize windowSize = this->GetSize();
277  wxSize clientSize = this->GetClientSize();
278  wxSize staticBitmapSize = StaticBitmap_->GetSize();
279  SizeLocked_ = false;
280 
281  if ( Menubar_->IsChecked(ID_VS_640) ) {
282  SizeLocked_ = true;
283  Img_->Resize(wxSize(640, 480), wxPoint(0,0), 0, 255, 0);
284  StaticBitmap_->SetBitmap(wxBitmap(*Img_));
285  }
286 
287  if ( Menubar_->IsChecked(ID_VS_800) ) {
288  SizeLocked_ = true;
289  Img_->Resize(wxSize(800, 600), wxPoint(0,0), 255, 0, 0);
290  StaticBitmap_->SetBitmap(wxBitmap(*Img_));
291  }
292 
293  if ( Menubar_->IsChecked(ID_VS_16_9) ) {
294  SizeLocked_ = true;
295  Img_->Resize(wxSize(800, 450), wxPoint(0,0), 0, 0, 255);
296  StaticBitmap_->SetBitmap(wxBitmap(*Img_));
297  }
298 
299  if ( Menubar_->IsChecked(ID_VS_16_10) ) {
300  SizeLocked_ = true;
301  Img_->Resize(wxSize(800, 500), wxPoint(0,0), 0, 255, 255);
302  StaticBitmap_->SetBitmap(wxBitmap(*Img_));
303  }
304 
305  if ( Menubar_->IsChecked(ID_VS_Free) ) {
306  Img_->Resize(clientSize, wxPoint(0,0), 255, 0, 255);
307  StaticBitmap_->SetBitmap(wxBitmap(*Img_));
308  SizeLocked_ = false;
309  }
310 
311  //int height =
312  //Menubar_->GetSize().GetHeight() + Toolbar_->GetSize().GetHeight()
313  //+ Statusbar_->GetSize().GetHeight();
314 
315  if ( SizeLocked_ ) {
316  StaticBitmap_->SetSize(staticBitmapSize);
317  }
318  else {
319  /// do nothing
320  }
321 
322  clientSize = StaticBitmap_->GetSize();
323  this->SetClientSize(clientSize);
324  cout << "++++ before fitting ++++" << endl;
325  DumpSizes_();
326  Fit();
327  //Layout();
328  //Update();
329  cout << "++++ after fitting ++++" << endl;
330  DumpSizes_();
331 }
332 
333 
334 ////////////////////////////////////////
335 ////////// main routine //////////
336 ////////////////////////////////////////
337 /** \cond HIDDEN_SYMBOLS*/
338 class SizingApp : public wxApp
339 {
340 public:
341  bool OnInit();
342 };
343 
344 
345 bool SizingApp::OnInit()
346 {
347  wxInitAllImageHandlers();
348  SizingFrame* mainFrame =
349  new SizingFrame(NULL, wxT("Example Sizing"));
350  mainFrame->Show();
351  mainFrame->LateInit();
352  return true;
353 }
354 
355 IMPLEMENT_APP(SizingApp)
356 /** \endcond */