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