OpenCores
URL https://opencores.org/ocsvn/openrisc_me/openrisc_me/trunk

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [services/] [gfx/] [mw/] [v2_0/] [src/] [mwin/] [mwobjects/] [mwobjects.cc] - Blame information for rev 27

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 27 unneback
/*
2
  Copyright (C) Chris Johns (ccj@acm.org)
3
 
4
  Microwindows C++ Wrappers.
5
 
6
 */
7
 
8
#include <algorithm>
9
#include <stdarg.h>
10
#include <stdio.h>
11
#include <mwobjects.h>
12
 
13
namespace MicroWindowsObjects
14
{
15
 
16
  static Application *the_application = 0;
17
 
18
  WindowClass::WindowClass ()
19
  {
20
    wclass.style         = CS_DBLCLKS | CS_VREDRAW | CS_HREDRAW;
21
    wclass.lpfnWndProc   = 0;
22
    wclass.lpfnWndProc   = 0;
23
    wclass.cbClsExtra    = 0;
24
    wclass.cbWndExtra    = 0;
25
    wclass.hInstance     = 0;
26
    wclass.hIcon         = 0;
27
    wclass.hCursor       = 0;
28
    wclass.hbrBackground = 0;
29
    wclass.lpszMenuName  = 0;
30
    wclass.lpszClassName = 0;
31
  }
32
 
33
  WindowClass::WindowClass (LPCSTR    lpszClassName,
34
                            UINT      style,
35
                            int       cbClsExtra,
36
                            int       cbWndExtra,
37
                            HINSTANCE hInstance,
38
                            HICON     hIcon,
39
                            HCURSOR   hCursor,
40
                            HBRUSH    hbrBackground,
41
                            LPCSTR    lpszMenuName)
42
  {
43
    wclass.style         = style;
44
    wclass.lpfnWndProc   = (WNDPROC) Window::WndProc;
45
    wclass.cbClsExtra    = cbClsExtra;
46
    wclass.cbWndExtra    = cbWndExtra + sizeof (Window *);
47
    wclass.hInstance     = hInstance;
48
    wclass.hIcon         = hIcon;
49
    wclass.hCursor       = hCursor;
50
    wclass.hbrBackground = hbrBackground;
51
    wclass.lpszMenuName  = lpszMenuName;
52
    wclass.lpszClassName = lpszClassName;
53
  }
54
 
55
  ATOM WindowClass::register_class ()
56
  {
57
    return RegisterClass (&wclass);
58
  }
59
 
60
  Window::Window ()
61
    : hwnd (0)
62
  {
63
  }
64
 
65
  Window::Window (DWORD     dwExStyle,
66
                  LPCSTR    lpClassName,
67
                  LPCSTR    lpWindowName,
68
                  DWORD     dwStyle,
69
                  int       x,
70
                  int       y,
71
                  int       nWidth,
72
                  int       nHeight,
73
                  HWND      hwndParent,
74
                  HMENU     hMenu,
75
                  HINSTANCE hInstance,
76
                  LPVOID    lpParam)
77
    : hwnd (0)
78
  {
79
    create (dwExStyle,
80
            lpClassName,
81
            lpWindowName,
82
            dwStyle,
83
            x, y, nWidth, nHeight,
84
            hwndParent,
85
            hMenu,
86
            hInstance,
87
            lpParam);
88
  }
89
 
90
  Window::~Window ()
91
  {
92
    destory ();
93
  }
94
 
95
  HWND
96
  Window::create (DWORD     dwExStyle,
97
                  LPCSTR    lpClassName,
98
                  LPCSTR    lpWindowName,
99
                  DWORD     dwStyle,
100
                  int       x,
101
                  int       y,
102
                  int       nWidth,
103
                  int       nHeight,
104
                  HWND      hwndParent,
105
                  HMENU     hMenu,
106
                  HINSTANCE hInstance,
107
                  LPVOID    lpParam)
108
  {
109
    lpCreateParams = lpParam;
110
 
111
    lpParam = (LPVOID) this;
112
 
113
    hwnd = ::CreateWindowEx (dwExStyle,
114
                             lpClassName,
115
                             lpWindowName,
116
                             dwStyle,
117
                             x, y, nWidth, nHeight,
118
                             hwndParent,
119
                             hMenu,
120
                             hInstance,
121
                             lpParam);
122
    return hwnd;
123
  }
124
 
125
  BOOL
126
  Window::destory ()
127
  {
128
    if (hwnd)
129
    {
130
      HWND old_hwnd = hwnd;
131
      hwnd = 0;
132
      return ::DestroyWindow (old_hwnd);
133
    }
134
    return 0;
135
  }
136
 
137
  bool
138
  Window::attach (const int fd, FileDescriptor& file_descriptor)
139
  {
140
    //
141
    // The user must set the fd before being added to the set.
142
    //
143
 
144
    if ((fd < 0) || (fd >= FD_SETSIZE) || (file_descriptor.file_desc != -1))
145
      return false;
146
 
147
    //
148
    // If this fd is already taken do not add another.
149
    //
150
 
151
    file_descriptor.file_desc = fd;
152
    file_descriptor.window    = this;
153
 
154
    if (file_descriptors.find (&file_descriptor) != file_descriptors.end ())
155
    {
156
      file_descriptor.file_desc = -1;
157
      file_descriptor.window    = 0;
158
      return false;
159
    }
160
 
161
    file_descriptors.insert (&file_descriptor);
162
 
163
    return true;
164
  }
165
 
166
  bool
167
  Window::detach (FileDescriptor& file_descriptor)
168
  {
169
    //
170
    // The user must set the fd before being added to the set.
171
    //
172
 
173
    if ((file_descriptor.fd () < 0) || (file_descriptor.fd () >= FD_SETSIZE))
174
      return false;
175
 
176
    //
177
    // If this fd is already taken do not add another.
178
    //
179
 
180
    if (file_descriptors.find (&file_descriptor) == file_descriptors.end ())
181
      return false;
182
 
183
    file_descriptor.disable_read ();
184
    file_descriptor.disable_write ();
185
    file_descriptor.disable_except ();
186
 
187
    file_descriptors.erase (&file_descriptor);
188
 
189
    file_descriptor.file_desc = -1;
190
    file_descriptor.window    = 0;
191
 
192
    return true;
193
  }
194
 
195
  struct eq_fd
196
  {
197
    const int fd;
198
    eq_fd (const int fd) : fd (fd) {}
199
    bool operator() (const FileDescriptor* f1) const
200
      { return (f1->fd () == fd); }
201
  };
202
 
203
  LRESULT
204
  Window::message_handler (UINT   msg,
205
                           WPARAM wp,
206
                           LPARAM lp)
207
  {
208
    switch (msg)
209
    {
210
      case WM_FDINPUT:
211
      case WM_FDOUTPUT:
212
      case WM_FDEXCEPT:
213
 
214
      {
215
        //
216
        // The iterator provides a reference to the object pointer
217
        // as the set contains pointers.
218
        //
219
 
220
        set<FileDescriptor*>::iterator file_descriptor;
221
 
222
        file_descriptor = find_if (file_descriptors.begin (),
223
                                   file_descriptors.end (),
224
                                   eq_fd ((int) wp));
225
 
226
        if (file_descriptor != file_descriptors.end ())
227
        {
228
          switch (msg)
229
          {
230
            case WM_FDINPUT:
231
              return (*file_descriptor)->read ();
232
 
233
            case WM_FDOUTPUT:
234
              return (*file_descriptor)->write ();
235
 
236
            case WM_FDEXCEPT:
237
              return (*file_descriptor)->except ();
238
          }
239
        }
240
      }
241
      break;
242
 
243
      default:
244
        break;
245
    }
246
 
247
    return ::DefWindowProc (hwnd, msg, wp, lp);
248
  }
249
 
250
  bool
251
  Window::ltint::operator () (const FileDescriptor* f1,
252
                              const FileDescriptor* f2) const
253
  {
254
    return f1->fd () < f2->fd ();
255
  }
256
 
257
  LRESULT CALLBACK
258
  Window::WndProc (HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
259
  {
260
    int    offset  = GetClassLong (hwnd, GCL_CBWNDEXTRA) - sizeof (Window *);
261
    Window *window = 0;
262
 
263
    if (msg == WM_CREATE)
264
    {
265
      LPCREATESTRUCT cs = (LPCREATESTRUCT) lp;
266
 
267
      window = dynamic_cast<Window*>((Window*) cs->lpCreateParams);
268
 
269
      if (window)
270
      {
271
        window->hwnd = hwnd;
272
        SetWindowLong (hwnd, offset, (DWORD) window);
273
        cs->lpCreateParams = window->lpCreateParams;
274
      }
275
    }
276
    else
277
    {
278
      window = dynamic_cast<Window*>((Window*) GetWindowLong (hwnd, offset));
279
    }
280
 
281
    if (window)
282
      return window->message_handler (msg, wp, lp);
283
 
284
    return ::DefWindowProc (hwnd, msg, wp, lp);
285
  }
286
 
287
  Paint::Paint (HWND hwnd)
288
    : hwnd (hwnd),
289
      draw_3d (false),
290
      drawing (false),
291
      r (0, 0, 0, 0),
292
      text_format (0)
293
  {
294
  }
295
 
296
  Paint::Paint (HWND hwnd, LPARAM lpParam)
297
    : hwnd (hwnd),
298
      draw_3d (false),
299
      drawing (false),
300
      r (0, 0, 0, 0),
301
      text_format (0)
302
  {
303
    POINTSTOPOINT (pt, lpParam);
304
  }
305
 
306
  Paint::~Paint ()
307
  {
308
    end ();
309
  }
310
 
311
  void
312
  Paint::begin (bool init_3d, bool draw_3d_in_mem)
313
  {
314
    if (!drawing && hwnd)
315
    {
316
      hdc = ::BeginPaint (hwnd, &ps);
317
 
318
      r.get_client (hwnd);
319
 
320
      if (init_3d)
321
        initialise_3d (draw_3d_in_mem);
322
 
323
      drawing = true;
324
    }
325
  }
326
 
327
  void
328
  Paint::end ()
329
  {
330
    if (drawing)
331
    {
332
      paint_3d ();
333
 
334
      ::EndPaint (hwnd, &ps);
335
 
336
      drawing = false;
337
    }
338
  }
339
 
340
  int
341
  Paint::text_out (int x, int y, const char *format, ...)
342
  {
343
    Rect    rect (x, y, x + 100, y + 100);
344
    va_list arg;
345
 
346
    va_start (arg, format);
347
    vsnprintf (format_buf, TEXT_BUF_SIZE, format, arg);
348
    format_buf[TEXT_BUF_SIZE - 1] = '\0';
349
 
350
    return ::DrawText (hdc, format_buf, -1, rect, text_format);
351
  }
352
 
353
  FileDescriptor::FileDescriptor ()
354
    : file_desc (-1),
355
      window (0)
356
  {
357
  }
358
 
359
  FileDescriptor::~FileDescriptor ()
360
  {
361
    if (window)
362
      window->detach (*this);
363
  }
364
 
365
  bool
366
  FileDescriptor::enable_read ()
367
  {
368
    if (!read_is_enabled && window && (file_desc != -1))
369
    {
370
      ::MwRegisterFdInput (*window, file_desc);
371
      read_is_enabled = true;
372
      return true;
373
    }
374
    return false;
375
  }
376
 
377
  bool
378
  FileDescriptor::disable_read ()
379
  {
380
    if (read_is_enabled && window && (file_desc != -1))
381
    {
382
      ::MwUnregisterFdInput (*window, file_desc);
383
      read_is_enabled = false;
384
      return true;
385
    }
386
    return false;
387
  }
388
 
389
 
390
  bool
391
  FileDescriptor::enable_write ()
392
  {
393
    if (!write_is_enabled && window && (file_desc != -1))
394
    {
395
      ::MwRegisterFdOutput (*window, file_desc);
396
      write_is_enabled = true;
397
      return true;
398
    }
399
    return false;
400
  }
401
 
402
  bool
403
  FileDescriptor::disable_write ()
404
  {
405
    if (write_is_enabled && window && (file_desc != -1))
406
    {
407
      ::MwUnregisterFdOutput (*window, file_desc);
408
      write_is_enabled = false;
409
      return true;
410
    }
411
    return false;
412
  }
413
 
414
  bool
415
  FileDescriptor::enable_except ()
416
  {
417
    if (!except_is_enabled && window && (file_desc != -1))
418
    {
419
      ::MwRegisterFdExcept (*window, file_desc);
420
      except_is_enabled = true;
421
      return true;
422
    }
423
    return false;
424
  }
425
 
426
  bool
427
  FileDescriptor::disable_except ()
428
  {
429
    if (except_is_enabled && window && (file_desc != -1))
430
    {
431
      ::MwUnregisterFdExcept (*window, file_desc);
432
      except_is_enabled = false;
433
      return true;
434
    }
435
    return false;
436
  }
437
 
438
  LRESULT
439
  FileDescriptor::read ()
440
  {
441
    return 0;
442
  }
443
 
444
  LRESULT
445
  FileDescriptor::write ()
446
  {
447
    return 0;
448
  }
449
 
450
  LRESULT
451
  FileDescriptor::except ()
452
  {
453
    return 0;
454
  }
455
 
456
  Application::Application ()
457
    : background (0)
458
  {
459
    if (!the_application)
460
      the_application = this;
461
  }
462
 
463
  Application::Application (MWIMAGEHDR& background)
464
    : background (&background)
465
  {
466
    if (!the_application)
467
      the_application = this;
468
  }
469
 
470
  Application::~Application ()
471
  {
472
  }
473
 
474
  int
475
  Application::initialise ()
476
  {
477
    return 0;
478
  }
479
 
480
  int
481
  Application::shutdown ()
482
  {
483
    return 0;
484
  }
485
 
486
  int WINAPI
487
  Application::WinMain (HINSTANCE hInstance,
488
                        HINSTANCE hPrevInstance,
489
                        LPSTR     lpCmdLine,
490
                        int       nShowCmd)
491
  {
492
    if (the_application)
493
    {
494
      int result;
495
 
496
      MwRegisterButtonControl (0);
497
 
498
      result = the_application->initialise ();
499
 
500
      if (result)
501
        return result;
502
 
503
      //
504
      // Set background wallpaper
505
      //
506
 
507
      if (the_application->background)
508
        MwSetDesktopWallpaper (the_application->background);
509
 
510
      MSG msg;
511
 
512
      //
513
      // type ESC to quit...
514
      //
515
 
516
      while (GetMessage (&msg, 0, 0, 0))
517
      {
518
        TranslateMessage (&msg);
519
        DispatchMessage (&msg);
520
      }
521
 
522
      result = the_application->shutdown ();
523
 
524
      if (result)
525
        return result;
526
 
527
      return 0;
528
    }
529
 
530
    return 1;
531
  }
532
 
533
};
534
 
535
//
536
//  Global Microwindows WinMain () routine with "C" linkage
537
//
538
 
539
extern "C"
540
{
541
  int WinMain (HINSTANCE hInstance,
542
               HINSTANCE hPrevInstance,
543
               LPSTR     lpCmdLine,
544
               int       nShowCmd)
545
  {
546
    return MicroWindowsObjects::Application::WinMain (hInstance,
547
                                                      hPrevInstance,
548
                                                      lpCmdLine,
549
                                                      nShowCmd);
550
  }
551
};

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.