~ubuntu-branches/ubuntu/saucy/python-scipy/saucy

« back to all changes in this revision

Viewing changes to Lib/sandbox/xplt/src/play/win/mfcapp.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Ondrej Certik
  • Date: 2008-06-16 22:58:01 UTC
  • mfrom: (2.1.24 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080616225801-irdhrpcwiocfbcmt
Tags: 0.6.0-12
* The description updated to match the current SciPy (Closes: #489149).
* Standards-Version bumped to 3.8.0 (no action needed)
* Build-Depends: netcdf-dev changed to libnetcdf-dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * mfcapp.cpp -- $Id: mfcapp.cpp 685 2003-03-08 15:26:51Z travo $
3
 
 * MFC implementation of play, boss and worker thread classes
4
 
 *
5
 
 * Copyright (c) 2000.  See accompanying LEGAL file for details.
6
 
 */
7
 
 
8
 
#include "mfcapp.h"
9
 
#include "mfcterm.h"
10
 
#include "mfcres.h"
11
 
 
12
 
#include "playw.h"
13
 
#include "pstdlib.h"
14
 
 
15
 
static int w_result = 0;
16
 
 
17
 
static CWinThread *the_worker = 0;
18
 
static CMultiDocTemplate *mfc_template = 0;
19
 
 
20
 
static void on_view(mfc_term_view *view);
21
 
static void on_update_view(mfc_term_view *view, CCmdUI *ui);
22
 
static int mfc_showing = 0;
23
 
static void mfc_show();
24
 
static char *mfc_cpy(HANDLE heap, const char *text, long len);
25
 
static int mfc_run_hack();
26
 
static int (*w_on_launch)(int, char **)= 0;
27
 
 
28
 
static void mfc_quit(void);
29
 
static int mfc_quitting = 0;
30
 
static HWND mfc_parent(int width, int height, char *title, int hints);
31
 
 
32
 
static void
33
 
mfc_quit(void)
34
 
{
35
 
  if (!mfc_quitting)
36
 
    the_boss.m_pMainWnd->SendMessage(WM_CLOSE, 0, 0);
37
 
  mfc_quitting = 1;
38
 
}
39
 
 
40
 
/*------------------------------------------------------------------------*/
41
 
/* boss thread (main application) */
42
 
 
43
 
class mfc_worker : public CWinThread {
44
 
  DECLARE_DYNCREATE(mfc_worker)
45
 
public:
46
 
  mfc_worker();
47
 
  virtual BOOL InitInstance();
48
 
  virtual int Run();
49
 
  virtual BOOL PreTranslateMessage(MSG* pmsg);
50
 
  virtual BOOL OnIdle(LONG count);
51
 
  virtual int ExitInstance();
52
 
  int run_hack();
53
 
};
54
 
 
55
 
BEGIN_MESSAGE_MAP(mfc_boss, CWinApp)
56
 
  ON_COMMAND(ID_APP_ABOUT, on_about)
57
 
  ON_COMMAND(ID_SIGINT, on_sigint)
58
 
  ON_COMMAND(ID_VIEW_TERM, on_view_term)
59
 
  ON_COMMAND(ID_VIEW_HIST, on_view_hist)
60
 
  ON_UPDATE_COMMAND_UI(ID_VIEW_TERM, on_update_view_term)
61
 
  ON_UPDATE_COMMAND_UI(ID_VIEW_HIST, on_update_view_hist)
62
 
  ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
63
 
  ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
64
 
  ON_COMMAND(ID_FILE_PRINT_SETUP, CWinApp::OnFilePrintSetup)
65
 
END_MESSAGE_MAP()
66
 
 
67
 
static int w_argc = 0;
68
 
static char **w_argv = 0;
69
 
 
70
 
mfc_boss::mfc_boss(int (*on_launch)(int, char **))
71
 
{
72
 
  w_on_launch = on_launch;
73
 
}
74
 
 
75
 
class mfc_frame_wnd : public CMDIFrameWnd
76
 
{
77
 
  DECLARE_DYNCREATE(mfc_frame_wnd)
78
 
public:
79
 
  mfc_frame_wnd();
80
 
  virtual LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lParam);
81
 
};
82
 
 
83
 
IMPLEMENT_DYNCREATE(mfc_frame_wnd, CMDIFrameWnd)
84
 
 
85
 
mfc_frame_wnd::mfc_frame_wnd()
86
 
{
87
 
}
88
 
static int oops = 0;
89
 
LRESULT
90
 
mfc_frame_wnd::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) 
91
 
{
92
 
  LRESULT result = 0;
93
 
  if (message==ID_CALL_FUNC) {
94
 
    void (*func)(void *arg)= (void (*)(void *))lParam;
95
 
    void *arg = (void *)wParam;
96
 
    func(arg);
97
 
  } else {
98
 
    if (message==WM_QUERYNEWPALETTE && w_screen.active)
99
 
      return ::SendMessage(w_screen.active->w, message, wParam, lParam);
100
 
    result = CMDIFrameWnd::WindowProc(message, wParam, lParam);
101
 
  }
102
 
  return result;
103
 
}
104
 
 
105
 
BOOL
106
 
mfc_boss::InitInstance()
107
 
{
108
 
  if (!AfxOleInit()) {  /* all this for richedit... */
109
 
    AfxMessageBox(IDP_OLE_INIT_FAILED);
110
 
    return 0;
111
 
  }
112
 
 
113
 
#ifdef _AFXDLL
114
 
  Enable3dControls();
115
 
#else
116
 
  Enable3dControlsStatic();
117
 
#endif
118
 
 
119
 
  SetRegistryKey(m_pszAppName); // AFX_IDS_APP_TITLE, considered IDR_MAINFRAME
120
 
  LoadStdProfileSettings(10); // Load standard INI file options (including MRU)
121
 
 
122
 
  mfc_template =
123
 
    new CMultiDocTemplate(IDR_EDITFRAME,
124
 
                          RUNTIME_CLASS(mfc_edit_doc),
125
 
                          RUNTIME_CLASS(mfc_edit_child),
126
 
                          RUNTIME_CLASS(mfc_edit_view));
127
 
  AddDocTemplate(mfc_template);
128
 
 
129
 
  /* create the main window, but don't show it until
130
 
   * the terminal window or a graphics child window is created
131
 
   * -- MFC Run method kills process if no main window */
132
 
  CMDIFrameWnd* fw = new mfc_frame_wnd;
133
 
  if (!fw->LoadFrame(IDR_MAINFRAME)) return 0;
134
 
  m_pMainWnd = fw;
135
 
  m_pMainWnd->DragAcceptFiles();
136
 
 
137
 
  EnableShellOpen();
138
 
  RegisterShellFileTypes(0);  /* do not register for printing */
139
 
 
140
 
  /* some command line stuff needs to be merged with w_init... */
141
 
  CCommandLineInfo cmd_info;
142
 
  ParseCommandLine(cmd_info);
143
 
  /* prevent opening an empty untitled document at startup */
144
 
  if (cmd_info.m_nShellCommand == CCommandLineInfo::FileNew)
145
 
    cmd_info.m_nShellCommand = CCommandLineInfo::FileNothing;
146
 
  if (!ProcessShellCommand(cmd_info)) return 0;
147
 
 
148
 
  the_worker=
149
 
    AfxBeginThread(RUNTIME_CLASS(mfc_worker), THREAD_PRIORITY_BELOW_NORMAL);
150
 
  return 1;
151
 
}
152
 
 
153
 
int
154
 
mfc_boss::ExitInstance()
155
 
{
156
 
  the_worker->PostThreadMessage(WM_QUIT,0,0);
157
 
  Sleep(200);
158
 
  if (!mfc_quitting) {
159
 
    mfc_quitting = 1;
160
 
    w_sigint(0);
161
 
    Sleep(200);
162
 
  }
163
 
  CWinApp::ExitInstance();
164
 
  return w_result;
165
 
}
166
 
 
167
 
void
168
 
mfc_boss::on_about()
169
 
{
170
 
  CDialog about(IDD_ABOUTBOX);
171
 
  about.DoModal();
172
 
}
173
 
 
174
 
void
175
 
mfc_boss::on_sigint()
176
 
{
177
 
  mfc_reset_stdin();
178
 
  if (!w_sigint(1)) mfc_quit();
179
 
}
180
 
 
181
 
void
182
 
mfc_boss::on_view_term()
183
 
{
184
 
  on_view(term_view);
185
 
}
186
 
 
187
 
void
188
 
mfc_boss::on_view_hist()
189
 
{
190
 
  on_view(hist_view);
191
 
}
192
 
 
193
 
void
194
 
mfc_boss::on_update_view_term(CCmdUI *ui)
195
 
{
196
 
  on_update_view(term_view, ui);
197
 
}
198
 
 
199
 
void
200
 
mfc_boss::on_update_view_hist(CCmdUI *ui)
201
 
{
202
 
  on_update_view(hist_view, ui);
203
 
}
204
 
 
205
 
static void
206
 
on_view(mfc_term_view *view)
207
 
{
208
 
  if (view && IsWindow(view->m_hWnd)) {
209
 
    CMDIFrameWnd *fw = (CMDIFrameWnd *)(the_boss.m_pMainWnd);
210
 
    if (view->is_visible) {
211
 
      CMDIChildWnd *cw = (CMDIChildWnd *)fw->GetActiveFrame();
212
 
      if (view == cw->GetActiveView()) fw->MDINext();
213
 
      view->GetParent()->ShowWindow(SW_HIDE);
214
 
    } else {
215
 
      view->GetParent()->ShowWindow(SW_SHOW);
216
 
          fw->MDIActivate(view->GetParent());
217
 
    }
218
 
    view->is_visible = !view->is_visible;
219
 
    SendMessage(fw->m_hWndMDIClient, WM_MDIREFRESHMENU, 0, 0);
220
 
  }
221
 
}
222
 
 
223
 
static void
224
 
on_update_view(mfc_term_view *view, CCmdUI *ui)
225
 
{
226
 
  if (view && IsWindow(view->m_hWnd)) {
227
 
    ui->Enable(1);
228
 
    ui->SetCheck(view->is_visible);
229
 
  } else {
230
 
    ui->Enable(0);
231
 
  }
232
 
}
233
 
 
234
 
/*------------------------------------------------------------------------*/
235
 
/* worker thread */
236
 
 
237
 
IMPLEMENT_DYNCREATE(mfc_worker, CWinThread)
238
 
 
239
 
mfc_worker::mfc_worker()
240
 
{
241
 
}
242
 
 
243
 
BOOL
244
 
mfc_worker::InitInstance()
245
 
{
246
 
  w_initialize(the_boss.m_hInstance, the_boss.m_pMainWnd->m_hWnd,
247
 
               mfc_quit, mfc_stdinit, mfc_parent);
248
 
 
249
 
  /* crack command line into argc, argv */
250
 
  HANDLE heap = GetProcessHeap();
251
 
  LPSTR cmd_line = the_boss.m_lpCmdLine;
252
 
  char module_name[1028];
253
 
  DWORD len = GetModuleFileName((HMODULE)0, module_name, 1024);
254
 
  module_name[len] = '\0';
255
 
  w_argc = 0;
256
 
  w_argv = (char**)HeapAlloc(heap, HEAP_GENERATE_EXCEPTIONS, sizeof(char *)*9);
257
 
  w_argv[w_argc++] = mfc_cpy(heap, w_unixpath(module_name), len);
258
 
  if (cmd_line) {
259
 
    char *c = cmd_line;
260
 
    char delim;
261
 
    for (;;) {
262
 
      while (c[0]==' ' || c[0]=='\t' || c[0]=='\r' || c[0]=='\n') c++;
263
 
      delim = c[0];
264
 
      if (!delim) break;
265
 
      cmd_line = c;
266
 
      if (delim=='"' || delim=='\'') {
267
 
        cmd_line = ++c;
268
 
        while (c[0] && c[0]!=delim) c++;
269
 
      } else {
270
 
        while (c[0] && c[0]!=' ' && c[0]!='\t' &&
271
 
               c[0]!='\r' && c[0]!='\n') c++;
272
 
        delim = 'x';
273
 
      }
274
 
      if (w_argc>1 || cmd_line[0]!='-'||cmd_line[1]!='n'||cmd_line[2]!='o'||
275
 
          cmd_line[3]!='m'||cmd_line[4]!='d'||cmd_line[5]!='i') {
276
 
        if (!(w_argc&7))
277
 
          w_argv = (char **)HeapReAlloc(heap, HEAP_GENERATE_EXCEPTIONS,
278
 
                                       w_argv, sizeof(char *)*(2*w_argc+1));
279
 
        w_argv[w_argc++] = mfc_cpy(heap, cmd_line, c - cmd_line);
280
 
      } else {
281
 
        w_no_mdi = 1;
282
 
      }
283
 
      if (c[0] == delim) c++;
284
 
      cmd_line = c;
285
 
    }
286
 
  }
287
 
  w_argv[w_argc] = 0;
288
 
 
289
 
  return 1;
290
 
}
291
 
 
292
 
static char *
293
 
mfc_cpy(HANDLE heap, const char *text, long len)
294
 
{
295
 
  if (!len) while (text[len]) len++;
296
 
  char *buf = (char *)HeapAlloc(heap, HEAP_GENERATE_EXCEPTIONS, len+1);
297
 
  char *buffer = buf;
298
 
  while (len--) *buf++= *text++;
299
 
  buf[0] = '\0';
300
 
  return buffer;
301
 
}
302
 
 
303
 
int
304
 
mfc_worker::Run()
305
 
{
306
 
  return w_protect(mfc_run_hack);
307
 
}
308
 
 
309
 
int
310
 
mfc_worker::run_hack()
311
 
{
312
 
  if (w_on_launch) {
313
 
    int (*on_launch)(int, char **)= w_on_launch;
314
 
    w_on_launch = 0;
315
 
    p_mminit();
316
 
    w_pollinit();
317
 
    int result = on_launch(w_argc, w_argv);
318
 
    if (result) return result;
319
 
  }
320
 
  return CWinThread::Run();
321
 
}
322
 
 
323
 
static int
324
 
mfc_run_hack()
325
 
{
326
 
  return ((mfc_worker *)the_worker)->run_hack();
327
 
}
328
 
 
329
 
BOOL
330
 
mfc_worker::PreTranslateMessage(MSG* pmsg)
331
 
{
332
 
  return w_app_msg(pmsg) || CWinThread::PreTranslateMessage(pmsg);
333
 
}
334
 
 
335
 
BOOL
336
 
mfc_worker::OnIdle(LONG count)
337
 
{
338
 
  return CWinThread::OnIdle(count) || w_work_idle();
339
 
}
340
 
 
341
 
int
342
 
mfc_worker::ExitInstance()
343
 
{
344
 
  w_result = w_on_quit();
345
 
  CWinThread::ExitInstance();
346
 
  AfxEndThread(w_result, 1);  // simply returning doesn't delete the_worker
347
 
  return w_result;            // not reached
348
 
}
349
 
 
350
 
/*------------------------------------------------------------------------*/
351
 
 
352
 
static void
353
 
mfc_show()
354
 
{
355
 
  if (!(mfc_showing&1)) {
356
 
    CMDIFrameWnd *fw = (CMDIFrameWnd *)(the_boss.m_pMainWnd);
357
 
    fw->ShowWindow(the_boss.m_nCmdShow);
358
 
    fw->UpdateWindow();
359
 
    mfc_showing |= 1;
360
 
  }
361
 
}
362
 
 
363
 
/* ARGSUSED */
364
 
void
365
 
mfc_term_init(void *arg)
366
 
{
367
 
  mfc_show();
368
 
 
369
 
  if (!(mfc_showing&2)) {
370
 
    mfc_edit_doc *doc = new mfc_edit_doc(mfc_template, 0);
371
 
    term_view = (mfc_term_view *)doc->GetView();
372
 
    term_view->is_visible = 1;
373
 
    mfc_template->InitialUpdateFrame((CFrameWnd*)term_view->GetParent(),
374
 
                                     doc, 1);
375
 
    doc = new mfc_edit_doc(mfc_template, 1);
376
 
    hist_view = (mfc_term_view *)doc->GetView();
377
 
    hist_view->is_visible = 0;
378
 
    mfc_template->InitialUpdateFrame((CFrameWnd*)hist_view->GetParent(),
379
 
                                     doc, 0);
380
 
 
381
 
    mfc_showing |= 2;
382
 
  }
383
 
}
384
 
 
385
 
/*------------------------------------------------------------------------*/
386
 
 
387
 
class mfc_child : public CMDIChildWnd
388
 
{
389
 
  DECLARE_DYNCREATE(mfc_child)
390
 
public:
391
 
  mfc_child();
392
 
  int width, height, hints;
393
 
  HWND child;
394
 
 
395
 
protected:
396
 
  int precreated;
397
 
  virtual BOOL PreCreateWindow( CREATESTRUCT& cs );
398
 
  virtual ~mfc_child();
399
 
 
400
 
  afx_msg void OnParentNotify(UINT msg, LPARAM lp);
401
 
  afx_msg void OnDestroy();
402
 
  afx_msg void OnSetFocus(CWnd *w);
403
 
  afx_msg void OnSize(UINT type, int cx, int cy);
404
 
  afx_msg void OnMDIActivate(BOOL activate, CWnd* aw, CWnd* dw);
405
 
 
406
 
  DECLARE_MESSAGE_MAP()
407
 
};
408
 
 
409
 
struct w_parent_args {
410
 
  char *title;
411
 
  int width, height, hints;
412
 
  HWND handle;
413
 
};
414
 
static void mfc_window(struct w_parent_args *args);
415
 
 
416
 
static HWND
417
 
mfc_parent(int width, int height, char *title, int hints)
418
 
{
419
 
  if (mfc_showing) {
420
 
    struct w_parent_args args;
421
 
    args.title = title;
422
 
    args.width = width;
423
 
    args.height = height;
424
 
    args.hints = hints;
425
 
    args.handle = 0;
426
 
    the_boss.m_pMainWnd->SendMessage(ID_CALL_FUNC, (WPARAM)&args,
427
 
                                     (LPARAM)mfc_window);
428
 
    return args.handle;
429
 
  } else {
430
 
    return 0;
431
 
  }
432
 
}
433
 
 
434
 
static void
435
 
mfc_window(struct w_parent_args *args)
436
 
{
437
 
  mfc_child *gw = new mfc_child;
438
 
  gw->width = args->width;
439
 
  gw->height = args->height;
440
 
  gw->hints = args->hints;
441
 
  gw->child = 0;
442
 
  gw->Create(0, args->title);
443
 
  args->handle = gw->m_hWnd;
444
 
}
445
 
 
446
 
BEGIN_MESSAGE_MAP(mfc_child, CMDIChildWnd)
447
 
  ON_WM_PARENTNOTIFY()
448
 
  ON_WM_DESTROY()
449
 
  ON_WM_SETFOCUS()
450
 
  ON_WM_SIZE()
451
 
  ON_WM_MDIACTIVATE()
452
 
END_MESSAGE_MAP()
453
 
 
454
 
IMPLEMENT_DYNCREATE(mfc_child, CMDIChildWnd)
455
 
 
456
 
mfc_child::mfc_child()
457
 
{
458
 
  precreated = 0;
459
 
}
460
 
 
461
 
mfc_child::~mfc_child()
462
 
{
463
 
}
464
 
 
465
 
BOOL
466
 
mfc_child::PreCreateWindow(CREATESTRUCT& cs)
467
 
{
468
 
  if (!precreated) {
469
 
    RECT rect;
470
 
    rect.left = 0;
471
 
    rect.top = 0;
472
 
    rect.right = cs.cx = width;
473
 
    rect.bottom = cs.cy = height;
474
 
    if (AdjustWindowRect(&rect, cs.style, 0)) {
475
 
      cs.cx = rect.right - rect.left + 4;  // is 4 really some variable?
476
 
      cs.cy = rect.bottom - rect.top + 4;
477
 
    }
478
 
    if (hints & P_NORESIZE)
479
 
      cs.style &= ~(WS_THICKFRAME | WS_MAXIMIZEBOX);
480
 
    precreated = 1;
481
 
  }
482
 
  return CMDIChildWnd::PreCreateWindow(cs);
483
 
}
484
 
 
485
 
void
486
 
mfc_child::OnDestroy() 
487
 
{
488
 
  CMDIChildWnd::OnDestroy();
489
 
  CMDIFrameWnd *fw = (CMDIFrameWnd *)the_boss.m_pMainWnd;
490
 
  ::SendMessage(fw->m_hWndMDIClient, WM_MDIREFRESHMENU, 0, 0);
491
 
}
492
 
 
493
 
void
494
 
mfc_child::OnSetFocus(CWnd *w) 
495
 
{
496
 
  /* first call to OnSetFocus occurs before child is created */
497
 
  if (child) {
498
 
    ::SetFocus(child);
499
 
    CMDIFrameWnd *fw = (CMDIFrameWnd *)the_boss.m_pMainWnd;
500
 
    ::SendMessage(fw->m_hWndMDIClient, WM_MDIREFRESHMENU, 0, 0);
501
 
  } else {
502
 
    CWnd::OnSetFocus(w);
503
 
  }
504
 
  /* apparently AttachThreadInput in mfc_worker::InitInstance unneeded? */
505
 
}
506
 
 
507
 
void
508
 
mfc_child::OnParentNotify(UINT msg, LPARAM lp)
509
 
{
510
 
  if (msg==WM_CREATE) {
511
 
    child = (HWND)lp;
512
 
    ::SetFocus(child);  /* mfc_child (this) already has focus */
513
 
  }
514
 
  CWnd::OnParentNotify(msg, lp);
515
 
}
516
 
 
517
 
void
518
 
mfc_child::OnSize(UINT type, int cx, int cy)
519
 
{
520
 
  RECT r;
521
 
  GetClientRect(&r);
522
 
  ::MoveWindow(child, 0,0, r.right,r.bottom, 1);
523
 
  CMDIChildWnd::OnSize(type, cx, cy);
524
 
}
525
 
 
526
 
void
527
 
mfc_child::OnMDIActivate(BOOL activate, CWnd* aw, CWnd* dw)
528
 
{
529
 
  if (child) {
530
 
    p_win *pw = child? (p_win *)GetWindowLong(child, GWL_USERDATA) : 0;
531
 
    if (pw && activate && pw->palette) pw->s->active = pw;
532
 
  }
533
 
  CMDIChildWnd::OnMDIActivate(activate, aw, dw);
534
 
}
535
 
 
536
 
/*------------------------------------------------------------------------*/