]> git.saurik.com Git - wxWidgets.git/blob - contrib/samples/ogl/studio/studio.cpp
glibc's vswprintf doesn't nul terminate on truncation.
[wxWidgets.git] / contrib / samples / ogl / studio / studio.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: Studio.cpp
3 // Purpose: Studio application class
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 27/7/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // 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 #include "wx/mdi.h"
22 #endif
23
24 #include "wx/ogl/ogl.h" // base header of OGL, includes and adjusts wx/deprecated/setup.h
25
26 #include "wx/config.h"
27 #include "wx/laywin.h"
28 #include "wx/helpwin.h"
29
30 #include "studio.h"
31 #include "view.h"
32 #include "doc.h"
33 #include "mainfrm.h"
34 #include "cspalette.h"
35 #include "project.h"
36 #include "symbols.h"
37
38 #include "bitmaps/new.xpm"
39 #include "bitmaps/open.xpm"
40 #include "bitmaps/save.xpm"
41 #include "bitmaps/copy.xpm"
42 #include "bitmaps/cut.xpm"
43 #include "bitmaps/paste.xpm"
44 #include "bitmaps/print.xpm"
45 #include "bitmaps/help.xpm"
46 #include "bitmaps/undo.xpm"
47 #include "bitmaps/redo.xpm"
48
49 #include "bitmaps/alignl.xpm"
50 #include "bitmaps/alignr.xpm"
51 #include "bitmaps/alignt.xpm"
52 #include "bitmaps/alignb.xpm"
53 #include "bitmaps/horiz.xpm"
54 #include "bitmaps/vert.xpm"
55 #include "bitmaps/copysize.xpm"
56 #include "bitmaps/linearrow.xpm"
57 #include "bitmaps/newpoint.xpm"
58 #include "bitmaps/cutpoint.xpm"
59 #include "bitmaps/straight.xpm"
60
61 #include "studio.xpm"
62
63 IMPLEMENT_APP(csApp)
64
65 csApp::csApp()
66 {
67 m_docManager = NULL;
68 m_diagramPalette = NULL;
69 m_diagramToolBar = NULL;
70 m_projectTreeCtrl = NULL;
71 m_diagramPaletteSashWindow = NULL;
72 m_projectSashWindow = NULL;
73 m_symbolDatabase = NULL;
74 m_pointSizeComboBox = NULL;
75 m_zoomComboBox = NULL;
76 m_shapeEditMenu = NULL;
77
78 // Configuration
79 m_mainFramePos.x = 20;
80 m_mainFramePos.y = 20;
81 m_mainFrameSize.x = 500;
82 m_mainFrameSize.y = 400;
83 m_gridStyle = csGRID_STYLE_INVISIBLE;
84 m_gridSpacing = 5;
85 }
86
87 csApp::~csApp()
88 {
89 }
90
91 // Initialise this in OnInit, not statically
92 bool csApp::OnInit(void)
93 {
94 #if wxUSE_WX_RESOURCES
95 if (!wxResourceParseFile(_T("studio_resources.wxr")))
96 {
97 wxMessageBox(_T("Could not find or parse resource file: studio_resources.wxr"), _T("Studio"));
98 return false;
99 }
100 #endif
101
102 #if wxUSE_MS_HTML_HELP && !defined(__WXUNIVERSAL__)
103 m_helpController = new wxWinHelpController;
104 #else
105 m_helpController = new wxHtmlHelpController;
106 #endif
107
108 m_helpController->Initialize(_T("studio.hlp"));
109
110 ReadOptions();
111
112 wxOGLInitialize();
113
114 InitSymbols();
115
116 //// Create a document manager
117 m_docManager = new wxDocManager;
118
119 //// Create a template relating drawing documents to their views
120 (void) new wxDocTemplate(m_docManager, _T("Diagram"), _T("*.dia"), wxEmptyString, _T("dia"), _T("Diagram Doc"), _T("Diagram View"),
121 CLASSINFO(csDiagramDocument), CLASSINFO(csDiagramView));
122
123 // Create the main frame window.
124 // Note that we use a frame style that doesn't have wxCLIP_CHILDREN in it
125 // (the default frame style contains wxCLIP_CHILDREN), otherwise the client
126 // area doesn't refresh properly when we change its position, under Windows.
127
128 #define wxDEFAULT_FRAME_STYLE_NO_CLIP \
129 (wxDEFAULT_FRAME_STYLE & ~wxCLIP_CHILDREN)
130
131 csFrame* frame = new csFrame(m_docManager, NULL, wxID_ANY, _T("OGL Studio"), m_mainFramePos, m_mainFrameSize,
132 wxDEFAULT_FRAME_STYLE_NO_CLIP | wxHSCROLL | wxVSCROLL);
133
134 // Give it an icon
135 frame->SetIcon(wxIcon(studio_xpm));
136
137 // Make a menubar
138 wxMenu *fileMenu = new wxMenu;
139
140 fileMenu->Append(wxID_NEW, _T("&New...\tCtrl+N"));
141 fileMenu->Append(wxID_OPEN, _T("&Open...\tCtrl+O"));
142
143 fileMenu->AppendSeparator();
144
145 fileMenu->Append(wxID_PRINT, _T("&Print...\tCtrl+P"));
146 fileMenu->Append(wxID_PRINT_SETUP, _T("Print &Setup..."));
147 fileMenu->Append(wxID_PREVIEW, _T("Print Pre&view"));
148 fileMenu->AppendSeparator();
149 fileMenu->Append(wxID_EXIT, _T("E&xit"));
150
151 // A history of files visited. Use this menu.
152 m_docManager->FileHistoryUseMenu(fileMenu);
153
154 wxMenu *viewMenu = new wxMenu;
155 viewMenu->Append(ID_CS_SETTINGS, _T("&Settings..."));
156
157 wxMenu *helpMenu = new wxMenu;
158 helpMenu->Append(wxID_HELP, _T("&Help Contents\tF1"));
159 helpMenu->Append(ID_CS_ABOUT, _T("&About"));
160
161 wxMenuBar *menuBar = new wxMenuBar;
162
163 menuBar->Append(fileMenu, _T("&File"));
164 menuBar->Append(viewMenu, _T("&View"));
165 menuBar->Append(helpMenu, _T("&Help"));
166
167 // Associate the menu bar with the frame
168 frame->SetMenuBar(menuBar);
169
170 // Load the file history
171 wxConfig config(_T("OGL Studio"), _T("wxWidgets"));
172 m_docManager->FileHistoryLoad(config);
173
174 #if wxUSE_STATUSBAR
175 frame->CreateStatusBar();
176 #endif // wxUSE_STATUSBAR
177
178 // The ordering of these is important for layout purposes
179 CreateDiagramToolBar(frame);
180 CreatePalette(frame);
181
182 /*
183 CreateProjectWindow(frame);
184 FillProjectTreeCtrl();
185 */
186
187 // Create the shape editing menu
188 m_shapeEditMenu = new ShapeEditMenu;
189 m_shapeEditMenu->Append(ID_CS_EDIT_PROPERTIES, _T("Edit properties"));
190 m_shapeEditMenu->AppendSeparator();
191 m_shapeEditMenu->Append(ID_CS_ROTATE_CLOCKWISE, _T("Rotate clockwise"));
192 m_shapeEditMenu->Append(ID_CS_ROTATE_ANTICLOCKWISE, _T("Rotate anticlockwise"));
193 m_shapeEditMenu->AppendSeparator();
194 m_shapeEditMenu->Append(ID_CS_CUT, _T("Cut"));
195
196 frame->Show(true);
197
198 SetTopWindow(frame);
199
200 return true;
201 }
202
203 int csApp::OnExit(void)
204 {
205 WriteOptions();
206
207 delete m_symbolDatabase;
208 m_symbolDatabase = NULL;
209
210 delete m_docManager;
211 m_docManager = NULL;
212
213 delete m_shapeEditMenu;
214 m_shapeEditMenu = NULL;
215
216 delete m_helpController;
217 m_helpController = NULL;
218
219 wxOGLCleanUp();
220
221 return 0;
222 }
223
224 /*
225 * Centralised code for creating a document frame.
226 * Called from view.cpp, when a view is created.
227 */
228
229 wxMDIChildFrame *csApp::CreateChildFrame(wxDocument *doc, wxView *view, wxMenu** editMenuRet)
230 {
231 //// Make a child frame
232 csMDIChildFrame *subframe = new csMDIChildFrame(doc, view, ((wxDocMDIParentFrame*)GetTopWindow()), wxID_ANY, _T("Child Frame"),
233 wxPoint(10, 10), wxSize(300, 300), wxDEFAULT_FRAME_STYLE);
234
235 #ifdef __WXMSW__
236 subframe->SetIcon(wxString(_T("chart")));
237 #endif
238 #ifdef __X__
239 subframe->SetIcon(wxIcon(_T("doc.xbm")));
240 #endif
241
242 //// Make a menubar
243 wxMenu *fileMenu = new wxMenu;
244
245 fileMenu->Append(wxID_NEW, _T("&New...\tCtrl+N"));
246 fileMenu->Append(wxID_OPEN, _T("&Open...\tCtrl+O"));
247 fileMenu->Append(wxID_CLOSE, _T("&Close\tCtrl+W"));
248 fileMenu->Append(wxID_SAVE, _T("&Save\tCtrl+S"));
249 fileMenu->Append(wxID_SAVEAS, _T("Save &As...\tF12"));
250
251 fileMenu->AppendSeparator();
252 fileMenu->Append(wxID_PRINT, _T("&Print...\tCtrl+P"));
253 fileMenu->Append(wxID_PRINT_SETUP, _T("Print &Setup..."));
254 fileMenu->Append(wxID_PREVIEW, _T("Print Pre&view"));
255
256 fileMenu->AppendSeparator();
257 fileMenu->Append(wxID_EXIT, _T("E&xit"));
258
259 wxMenu* editMenu = new wxMenu;
260 editMenu->Append(wxID_UNDO, _T("&Undo\tCtrl+Z"));
261 editMenu->Append(wxID_REDO, _T("&Redo\tCtrl+Y"));
262 editMenu->AppendSeparator();
263 editMenu->Append(wxID_CUT, _T("Cu&t\tCtrl+X"));
264 editMenu->Append(wxID_COPY, _T("&Copy\tCtrl+C"));
265 editMenu->Append(wxID_PASTE, _T("&Paste\tCtrl+V"));
266 editMenu->Append(wxID_DUPLICATE, _T("&Duplicate\tCtrl+D"));
267 editMenu->AppendSeparator();
268 editMenu->Append(wxID_CLEAR, _T("Cle&ar\tDelete"));
269 editMenu->Append(ID_CS_SELECT_ALL, _T("Select A&ll\tCtrl+A"));
270 editMenu->AppendSeparator();
271 editMenu->Append(ID_CS_EDIT_PROPERTIES, _T("Edit P&roperties..."));
272
273 *editMenuRet = editMenu;
274
275 m_docManager->FileHistoryUseMenu(fileMenu);
276 m_docManager->FileHistoryAddFilesToMenu(fileMenu);
277
278 doc->GetCommandProcessor()->SetEditMenu(editMenu);
279
280 wxMenu *viewMenu = new wxMenu;
281 viewMenu->Append(ID_CS_SETTINGS, _T("&Settings..."));
282
283 wxMenu *helpMenu = new wxMenu;
284 helpMenu->Append(wxID_HELP, _T("&Help Contents\tF1"));
285 helpMenu->Append(ID_CS_ABOUT, _T("&About"));
286
287 wxMenuBar *menuBar = new wxMenuBar;
288
289 menuBar->Append(fileMenu, _T("&File"));
290 menuBar->Append(editMenu, _T("&Edit"));
291 menuBar->Append(viewMenu, _T("&View"));
292 menuBar->Append(helpMenu, _T("&Help"));
293
294 //// Associate the menu bar with the frame
295 subframe->SetMenuBar(menuBar);
296
297 return subframe;
298 }
299
300 // Creates a canvas. Called by OnInit as a child of the main window
301 csCanvas *csApp::CreateCanvas(wxView *view, wxMDIChildFrame *parent)
302 {
303 int width, height;
304 parent->GetClientSize(&width, &height);
305
306 // Non-retained canvas
307 csCanvas *canvas = new csCanvas((csDiagramView*) view, parent, 1000, wxPoint(0, 0), wxSize(width, height), wxSUNKEN_BORDER);
308
309 wxColour bgColour(_T("WHITE"));
310 canvas->SetBackgroundColour(bgColour);
311
312 wxCursor cursor(wxCURSOR_HAND);
313 canvas->SetCursor(cursor);
314
315 // Give it scrollbars
316 canvas->SetScrollbars(20, 20, 100, 100);
317
318 return canvas;
319 }
320
321 void csApp::InitToolBar(wxToolBar* toolBar)
322 {
323 wxBitmap* bitmaps[10];
324
325 bitmaps[0] = new wxBitmap( new_xpm );
326 bitmaps[1] = new wxBitmap( open_xpm );
327 bitmaps[2] = new wxBitmap( save_xpm );
328 bitmaps[3] = new wxBitmap( copy_xpm );
329 bitmaps[4] = new wxBitmap( cut_xpm );
330 bitmaps[5] = new wxBitmap( paste_xpm );
331 bitmaps[6] = new wxBitmap( print_xpm );
332 bitmaps[7] = new wxBitmap( help_xpm );
333 bitmaps[8] = new wxBitmap( undo_xpm );
334 bitmaps[9] = new wxBitmap( redo_xpm );
335
336 toolBar->AddTool(wxID_NEW, *bitmaps[0], wxNullBitmap, false, wxDefaultCoord, wxDefaultCoord, NULL, _T("New file"));
337 toolBar->AddTool(wxID_OPEN, *bitmaps[1], wxNullBitmap, false, wxDefaultCoord, wxDefaultCoord, NULL, _T("Open file"));
338 toolBar->AddTool(wxID_SAVE, *bitmaps[2], wxNullBitmap, false, wxDefaultCoord, wxDefaultCoord, NULL, _T("Save file"));
339 toolBar->AddSeparator();
340 toolBar->AddTool(wxID_PRINT, *bitmaps[6], wxNullBitmap, false, wxDefaultCoord, wxDefaultCoord, NULL, _T("Print"));
341 toolBar->AddSeparator();
342 toolBar->AddTool(wxID_COPY, *bitmaps[3], wxNullBitmap, false, wxDefaultCoord, wxDefaultCoord, NULL, _T("Copy"));
343 toolBar->AddTool(wxID_CUT, *bitmaps[4], wxNullBitmap, false, wxDefaultCoord, wxDefaultCoord, NULL, _T("Cut"));
344 toolBar->AddTool(wxID_PASTE, *bitmaps[5], wxNullBitmap, false, wxDefaultCoord, wxDefaultCoord, NULL, _T("Paste"));
345 toolBar->AddSeparator();
346 toolBar->AddTool(wxID_UNDO, *bitmaps[8], wxNullBitmap, false, wxDefaultCoord, wxDefaultCoord, NULL, _T("Undo"));
347 toolBar->AddTool(wxID_REDO, *bitmaps[9], wxNullBitmap, false, wxDefaultCoord, wxDefaultCoord, NULL, _T("Redo"));
348 toolBar->AddSeparator();
349 toolBar->AddTool(wxID_HELP, *bitmaps[7], wxNullBitmap, false, wxDefaultCoord, wxDefaultCoord, NULL, _T("Help"));
350
351 toolBar->Realize();
352
353 toolBar->EnableTool(wxID_COPY, false);
354 toolBar->EnableTool(wxID_PASTE, false);
355 toolBar->EnableTool(wxID_PRINT, false);
356 toolBar->EnableTool(wxID_UNDO, false);
357 toolBar->EnableTool(wxID_REDO, false);
358
359 int i;
360 for (i = 0; i < 10; i++)
361 delete bitmaps[i];
362 }
363
364 // Create and initialise the diagram toolbar
365 void csApp::CreateDiagramToolBar(wxFrame* parent)
366 {
367 // First create a layout window
368 wxSashLayoutWindow* win = new wxSashLayoutWindow(parent, ID_LAYOUT_WINDOW_DIAGRAM_TOOLBAR, wxDefaultPosition, wxSize(200, 30), wxNO_BORDER|wxSW_3D|wxCLIP_CHILDREN);
369 win->SetDefaultSize(wxSize(10000, 30));
370 win->SetOrientation(wxLAYOUT_HORIZONTAL);
371 win->SetAlignment(wxLAYOUT_TOP);
372 win->SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE));
373
374 m_diagramToolBarSashWindow = win;
375 m_diagramToolBarSashWindow->Show(false);
376
377 // Create the actual toolbar
378 m_diagramToolBar = new wxToolBar(win, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTB_HORIZONTAL|wxNO_BORDER|wxTB_FLAT);
379
380 wxBitmap* bitmaps[11];
381
382 bitmaps[0] = new wxBitmap( alignl_xpm );
383 bitmaps[1] = new wxBitmap( alignr_xpm );
384 bitmaps[2] = new wxBitmap( alignt_xpm );
385 bitmaps[3] = new wxBitmap( alignb_xpm );
386 bitmaps[4] = new wxBitmap( horiz_xpm );
387 bitmaps[5] = new wxBitmap( vert_xpm );
388 bitmaps[6] = new wxBitmap( copysize_xpm );
389 bitmaps[7] = new wxBitmap( linearrow_xpm );
390 bitmaps[8] = new wxBitmap( newpoint_xpm );
391 bitmaps[9] = new wxBitmap( cutpoint_xpm );
392 bitmaps[10] = new wxBitmap( straight_xpm );
393
394 m_diagramToolBar->AddTool(DIAGRAM_TOOLBAR_ALIGNL, *bitmaps[0], wxNullBitmap, false, wxDefaultCoord, wxDefaultCoord, NULL, _T("Align left"));
395 m_diagramToolBar->AddTool(DIAGRAM_TOOLBAR_ALIGNR, *bitmaps[1], wxNullBitmap, false, wxDefaultCoord, wxDefaultCoord, NULL, _T("Align right"));
396 m_diagramToolBar->AddTool(DIAGRAM_TOOLBAR_ALIGNT, *bitmaps[2], wxNullBitmap, false, wxDefaultCoord, wxDefaultCoord, NULL, _T("Align top"));
397 m_diagramToolBar->AddTool(DIAGRAM_TOOLBAR_ALIGNB, *bitmaps[3], wxNullBitmap, false, wxDefaultCoord, wxDefaultCoord, NULL, _T("Align bottom"));
398 m_diagramToolBar->AddTool(DIAGRAM_TOOLBAR_ALIGN_HORIZ, *bitmaps[4], wxNullBitmap, false, wxDefaultCoord, wxDefaultCoord, NULL, _T("Align horizontally"));
399 m_diagramToolBar->AddTool(DIAGRAM_TOOLBAR_ALIGN_VERT, *bitmaps[5], wxNullBitmap, false, wxDefaultCoord, wxDefaultCoord, NULL, _T("Align vertically"));
400 m_diagramToolBar->AddTool(DIAGRAM_TOOLBAR_COPY_SIZE, *bitmaps[6], wxNullBitmap, false, wxDefaultCoord, wxDefaultCoord, NULL, _T("Copy size"));
401 m_diagramToolBar->AddSeparator();
402 m_diagramToolBar->AddTool(DIAGRAM_TOOLBAR_LINE_ARROW, *bitmaps[7], wxNullBitmap, true, wxDefaultCoord, wxDefaultCoord, NULL, _T("Toggle arrow"));
403 m_diagramToolBar->AddTool(DIAGRAM_TOOLBAR_NEW_POINT, *bitmaps[8], wxNullBitmap, false, wxDefaultCoord, wxDefaultCoord, NULL, _T("New line point"));
404 m_diagramToolBar->AddTool(DIAGRAM_TOOLBAR_CUT_POINT, *bitmaps[9], wxNullBitmap, false, wxDefaultCoord, wxDefaultCoord, NULL, _T("Cut line point"));
405 m_diagramToolBar->AddTool(DIAGRAM_TOOLBAR_STRAIGHTEN, *bitmaps[10], wxNullBitmap, false, wxDefaultCoord, wxDefaultCoord, NULL, _T("Straighten lines"));
406
407 m_diagramToolBar->Realize();
408
409 int i;
410 for (i = 0; i < 11; i++)
411 delete bitmaps[i];
412
413 // Create a combobox for point size
414 int maxPointSize = 40;
415 wxString *pointSizes = new wxString[maxPointSize];
416 for (i = 1; i <= maxPointSize; i++)
417 {
418 pointSizes[i-1].Printf(_T("%d"), i);
419 }
420
421 int controlX = 260;
422 int pointSizeW = 40;
423 int pointSizeH = 18;
424 int zoomW = 60;
425 int zoomH = 18;
426 #ifdef __WXMOTIF__
427 controlX += 75;
428 pointSizeW = 60;
429 pointSizeH = 22;
430 zoomW = 60;
431 zoomH = 22;
432 #endif
433
434 m_pointSizeComboBox = new wxComboBox(m_diagramToolBar, ID_WINDOW_POINT_SIZE_COMBOBOX,
435 wxEmptyString, wxPoint(controlX, 1), wxSize(pointSizeW, pointSizeH), maxPointSize, pointSizes);
436 delete[] pointSizes;
437
438 #ifdef __WXGTK__
439 m_diagramToolBar->AddControl(m_pointSizeComboBox);
440 #endif
441
442 m_pointSizeComboBox->SetSelection(10 - 1);
443
444 // Create a combobox for zooming
445 int maxZoom = 200;
446 int minZoom = 5;
447 int increment = 5;
448 int noStrings = (maxZoom - minZoom)/5 ;
449 wxString *zoomStrings = new wxString[noStrings];
450 for (i = 0; i < noStrings; i ++)
451 {
452 zoomStrings[noStrings - i - 1].Printf(_T("%d%%"), (i*increment + minZoom));
453 }
454
455 controlX += pointSizeW + 10;
456
457 m_zoomComboBox = new wxComboBox(m_diagramToolBar, ID_WINDOW_ZOOM_COMBOBOX,
458 wxEmptyString, wxPoint(controlX, 1), wxSize(zoomW, zoomH), noStrings, zoomStrings);
459 delete[] zoomStrings;
460
461 #ifdef __WXGTK__
462 m_diagramToolBar->AddControl(m_zoomComboBox);
463 #endif
464
465 // i = (zoom - minZoom)/increment
466 // index = noStrings - i - 1
467 // 100%
468 i = (100 - minZoom)/increment;
469 m_zoomComboBox->SetSelection(noStrings - i - 1);
470 }
471
472 // Read/write configuration information
473 bool csApp::ReadOptions()
474 {
475 wxConfig config(_T("OGL Studio"), _T("wxWidgets"));
476
477 config.Read(_T("mainX"), & m_mainFramePos.x);
478 config.Read(_T("mainY"), & m_mainFramePos.y);
479 config.Read(_T("mainWidth"), & m_mainFrameSize.x);
480 config.Read(_T("mainHeight"), & m_mainFrameSize.y);
481 config.Read(_T("gridStyle"), & m_gridStyle);
482 config.Read(_T("gridSpacing"), & m_gridSpacing);
483
484 return true;
485 }
486
487 bool csApp::WriteOptions()
488 {
489 wxConfig config(_T("OGL Studio"), _T("wxWidgets"));
490
491 config.Write(_T("mainX"), (long) m_mainFramePos.x);
492 config.Write(_T("mainY"), (long) m_mainFramePos.y);
493 config.Write(_T("mainWidth"), (long) m_mainFrameSize.x);
494 config.Write(_T("mainHeight"), (long) m_mainFrameSize.y);
495 config.Write(_T("gridStyle"), (long) m_gridStyle);
496 config.Write(_T("gridSpacing"), (long) m_gridSpacing);
497
498 m_docManager->FileHistorySave(config);
499
500 return true;
501 }
502