Debian Codegraph @

src/platform/mac/agg_platform_support.cpp

raw
1 //----------------------------------------------------------------------------
2 // Anti-Grain Geometry - Version 2.4
3 // Copyright (C) 2002-2005 Maxim Shemanarev (McSeem)
4 // Copyright (C) 2003 Hansruedi Baer (MacOS support)
5 //
6 // Permission to copy, use, modify, sell and distribute this software
7 // is granted provided this copyright notice appears in all copies.
8 // This software is provided "as is" without express or implied
9 // warranty, and with no claim as to its suitability for any purpose.
10 //
11 //----------------------------------------------------------------------------
12 // Contact: [email protected]
13 // [email protected]
14 // http://www.antigrain.com
15 // [email protected]
16 //----------------------------------------------------------------------------
17 //
18 // class platform_support
19 //
20 //----------------------------------------------------------------------------
21 //
22 // Note:
23 // I tried to retain the original structure for the Win32 platform as far
24 // as possible. Currently, not all features are implemented but the examples
25 // should work properly.
26 // HB
27 //----------------------------------------------------------------------------
28
29 #include <Carbon.h>
30 #if defined(__MWERKS__)
31 #include "console.h"
32 #endif
33 #include <cstring>
34 #include <unistd.h>
35 #include "platform/agg_platform_support.h"
36 #include "platform/mac/agg_mac_pmap.h"
37 #include "util/agg_color_conv_rgb8.h"
38
39
40 namespace agg
41 {
42
43 pascal OSStatus DoWindowClose (EventHandlerCallRef nextHandler, EventRef theEvent, void* userData);
44 pascal OSStatus DoWindowDrawContent (EventHandlerCallRef nextHandler, EventRef theEvent, void* userData);
45 pascal OSStatus DoAppQuit (EventHandlerCallRef nextHandler, EventRef theEvent, void* userData);
46 pascal OSStatus DoMouseDown (EventHandlerCallRef nextHandler, EventRef theEvent, void* userData);
47 pascal OSStatus DoMouseUp (EventHandlerCallRef nextHandler, EventRef theEvent, void* userData);
48 pascal OSStatus DoMouseDragged (EventHandlerCallRef nextHandler, EventRef theEvent, void* userData);
49 pascal OSStatus DoKeyDown (EventHandlerCallRef nextHandler, EventRef theEvent, void* userData);
50 pascal OSStatus DoKeyUp (EventHandlerCallRef nextHandler, EventRef theEvent, void* userData);
51 pascal void DoPeriodicTask (EventLoopTimerRef theTimer, void* userData);
52
53
54 //------------------------------------------------------------------------
55 class platform_specific
56 {
57 public:
58 platform_specific(pix_format_e format, bool flip_y);
59
60 void create_pmap(unsigned width, unsigned height,
61 rendering_buffer* wnd);
62
63 void display_pmap(WindowRef window, const rendering_buffer* src);
64 bool load_pmap(const char* fn, unsigned idx,
65 rendering_buffer* dst);
66
67 bool save_pmap(const char* fn, unsigned idx,
68 const rendering_buffer* src);
69
70 unsigned translate(unsigned keycode);
71
72 pix_format_e m_format;
73 pix_format_e m_sys_format;
74 bool m_flip_y;
75 unsigned m_bpp;
76 unsigned m_sys_bpp;
77 WindowRef m_window;
78 pixel_map m_pmap_window;
79 pixel_map m_pmap_img[platform_support::max_images];
80 unsigned m_keymap[256];
81 unsigned m_last_translated_key;
82 int m_cur_x;
83 int m_cur_y;
84 unsigned m_input_flags;
85 bool m_redraw_flag;
86 UnsignedWide m_sw_freq;
87 UnsignedWide m_sw_start;
88 };
89
90
91 //------------------------------------------------------------------------
92 platform_specific::platform_specific(pix_format_e format, bool flip_y) :
93 m_format(format),
94 m_sys_format(pix_format_undefined),
95 m_flip_y(flip_y),
96 m_bpp(0),
97 m_sys_bpp(0),
98 m_window(nil),
99 m_last_translated_key(0),
100 m_cur_x(0),
101 m_cur_y(0),
102 m_input_flags(0),
103 m_redraw_flag(true)
104 {
105 std::memset(m_keymap, 0, sizeof(m_keymap));
106
107 //Keyboard input is not yet fully supported nor tested
108 //m_keymap[VK_PAUSE] = key_pause;
109 m_keymap[kClearCharCode] = key_clear;
110
111 //m_keymap[VK_NUMPAD0] = key_kp0;
112 //m_keymap[VK_NUMPAD1] = key_kp1;
113 //m_keymap[VK_NUMPAD2] = key_kp2;
114 //m_keymap[VK_NUMPAD3] = key_kp3;
115 //m_keymap[VK_NUMPAD4] = key_kp4;
116 //m_keymap[VK_NUMPAD5] = key_kp5;
117 //m_keymap[VK_NUMPAD6] = key_kp6;
118 //m_keymap[VK_NUMPAD7] = key_kp7;
119 //m_keymap[VK_NUMPAD8] = key_kp8;
120 //m_keymap[VK_NUMPAD9] = key_kp9;
121 //m_keymap[VK_DECIMAL] = key_kp_period;
122 //m_keymap[VK_DIVIDE] = key_kp_divide;
123 //m_keymap[VK_MULTIPLY] = key_kp_multiply;
124 //m_keymap[VK_SUBTRACT] = key_kp_minus;
125 //m_keymap[VK_ADD] = key_kp_plus;
126
127 m_keymap[kUpArrowCharCode] = key_up;
128 m_keymap[kDownArrowCharCode] = key_down;
129 m_keymap[kRightArrowCharCode] = key_right;
130 m_keymap[kLeftArrowCharCode] = key_left;
131 //m_keymap[VK_INSERT] = key_insert;
132 m_keymap[kDeleteCharCode] = key_delete;
133 m_keymap[kHomeCharCode] = key_home;
134 m_keymap[kEndCharCode] = key_end;
135 m_keymap[kPageUpCharCode] = key_page_up;
136 m_keymap[kPageDownCharCode] = key_page_down;
137
138 //m_keymap[VK_F1] = key_f1;
139 //m_keymap[VK_F2] = key_f2;
140 //m_keymap[VK_F3] = key_f3;
141 //m_keymap[VK_F4] = key_f4;
142 //m_keymap[VK_F5] = key_f5;
143 //m_keymap[VK_F6] = key_f6;
144 //m_keymap[VK_F7] = key_f7;
145 //m_keymap[VK_F8] = key_f8;
146 //m_keymap[VK_F9] = key_f9;
147 //m_keymap[VK_F10] = key_f10;
148 //m_keymap[VK_F11] = key_f11;
149 //m_keymap[VK_F12] = key_f12;
150 //m_keymap[VK_F13] = key_f13;
151 //m_keymap[VK_F14] = key_f14;
152 //m_keymap[VK_F15] = key_f15;
153
154 //m_keymap[VK_NUMLOCK] = key_numlock;
155 //m_keymap[VK_CAPITAL] = key_capslock;
156 //m_keymap[VK_SCROLL] = key_scrollock;
157
158 switch(m_format)
159 {
160 case pix_format_gray8:
161 m_sys_format = pix_format_gray8;
162 m_bpp = 8;
163 m_sys_bpp = 8;
164 break;
165
166 case pix_format_rgb565:
167 case pix_format_rgb555:
168 m_sys_format = pix_format_rgb555;
169 m_bpp = 16;
170 m_sys_bpp = 16;
171 break;
172
173 case pix_format_rgb24:
174 case pix_format_bgr24:
175 m_sys_format = pix_format_rgb24;
176 m_bpp = 24;
177 m_sys_bpp = 24;
178 break;
179
180 case pix_format_bgra32:
181 case pix_format_abgr32:
182 case pix_format_argb32:
183 case pix_format_rgba32:
184 m_sys_format = pix_format_argb32;
185 m_bpp = 32;
186 m_sys_bpp = 32;
187 break;
188 }
189 ::Microseconds(&m_sw_freq);
190 ::Microseconds(&m_sw_start);
191 }
192
193
194 //------------------------------------------------------------------------
195 void platform_specific::create_pmap(unsigned width,
196 unsigned height,
197 rendering_buffer* wnd)
198 {
199 m_pmap_window.create(width, height, org_e(m_bpp));
200 wnd->attach(m_pmap_window.buf(),
201 m_pmap_window.width(),
202 m_pmap_window.height(),
203 m_flip_y ?
204 -m_pmap_window.row_bytes() :
205 m_pmap_window.row_bytes());
206 }
207
208
209 //------------------------------------------------------------------------
210 void platform_specific::display_pmap(WindowRef window, const rendering_buffer* src)
211 {
212 if(m_sys_format == m_format)
213 {
214 m_pmap_window.draw(window);
215 }
216 else
217 {
218 pixel_map pmap_tmp;
219 pmap_tmp.create(m_pmap_window.width(),
220 m_pmap_window.height(),
221 org_e(m_sys_bpp));
222
223 rendering_buffer rbuf_tmp;
224 rbuf_tmp.attach(pmap_tmp.buf(),
225 pmap_tmp.width(),
226 pmap_tmp.height(),
227 m_flip_y ?
228 -pmap_tmp.row_bytes() :
229 pmap_tmp.row_bytes());
230
231 switch(m_format)
232 {
233 case pix_format_gray8:
234 return;
235
236 case pix_format_rgb565:
237 color_conv(&rbuf_tmp, src, color_conv_rgb565_to_rgb555());
238 break;
239
240 case pix_format_bgr24:
241 color_conv(&rbuf_tmp, src, color_conv_bgr24_to_rgb24());
242 break;
243
244 case pix_format_abgr32:
245 color_conv(&rbuf_tmp, src, color_conv_abgr32_to_argb32());
246 break;
247
248 case pix_format_bgra32:
249 color_conv(&rbuf_tmp, src, color_conv_bgra32_to_argb32());
250 break;
251
252 case pix_format_rgba32:
253 color_conv(&rbuf_tmp, src, color_conv_rgba32_to_argb32());
254 break;
255 }
256 pmap_tmp.draw(window);
257 }
258 }
259
260
261 //------------------------------------------------------------------------
262 bool platform_specific::save_pmap(const char* fn, unsigned idx,
263 const rendering_buffer* src)
264 {
265 if(m_sys_format == m_format)
266 {
267 return m_pmap_img[idx].save_as_qt(fn);
268 }
269 else
270 {
271 pixel_map pmap_tmp;
272 pmap_tmp.create(m_pmap_img[idx].width(),
273 m_pmap_img[idx].height(),
274 org_e(m_sys_bpp));
275
276 rendering_buffer rbuf_tmp;
277 rbuf_tmp.attach(pmap_tmp.buf(),
278 pmap_tmp.width(),
279 pmap_tmp.height(),
280 m_flip_y ?
281 -pmap_tmp.row_bytes() :
282 pmap_tmp.row_bytes());
283 switch(m_format)
284 {
285 case pix_format_gray8:
286 return false;
287
288 case pix_format_rgb565:
289 color_conv(&rbuf_tmp, src, color_conv_rgb565_to_rgb555());
290 break;
291
292 case pix_format_rgb24:
293 color_conv(&rbuf_tmp, src, color_conv_rgb24_to_bgr24());
294 break;
295
296 case pix_format_abgr32:
297 color_conv(&rbuf_tmp, src, color_conv_abgr32_to_bgra32());
298 break;
299
300 case pix_format_argb32:
301 color_conv(&rbuf_tmp, src, color_conv_argb32_to_bgra32());
302 break;
303
304 case pix_format_rgba32:
305 color_conv(&rbuf_tmp, src, color_conv_rgba32_to_bgra32());
306 break;
307 }
308 return pmap_tmp.save_as_qt(fn);
309 }
310 return true;
311 }
312
313
314
315 //------------------------------------------------------------------------
316 bool platform_specific::load_pmap(const char* fn, unsigned idx,
317 rendering_buffer* dst)
318 {
319 pixel_map pmap_tmp;
320 if(!pmap_tmp.load_from_qt(fn)) return false;
321
322 rendering_buffer rbuf_tmp;
323 rbuf_tmp.attach(pmap_tmp.buf(),
324 pmap_tmp.width(),
325 pmap_tmp.height(),
326 m_flip_y ?
327 -pmap_tmp.row_bytes() :
328 pmap_tmp.row_bytes());
329
330 m_pmap_img[idx].create(pmap_tmp.width(),
331 pmap_tmp.height(),
332 org_e(m_bpp),
333 0);
334
335 dst->attach(m_pmap_img[idx].buf(),
336 m_pmap_img[idx].width(),
337 m_pmap_img[idx].height(),
338 m_flip_y ?
339 -m_pmap_img[idx].row_bytes() :
340 m_pmap_img[idx].row_bytes());
341
342 switch(m_format)
343 {
344 case pix_format_gray8:
345 return false;
346 break;
347
348 case pix_format_rgb555:
349 switch(pmap_tmp.bpp())
350 {
351 case 16: color_conv(dst, &rbuf_tmp, color_conv_rgb555_to_rgb555()); break;
352 case 24: color_conv(dst, &rbuf_tmp, color_conv_rgb24_to_rgb555()); break;
353 case 32: color_conv(dst, &rbuf_tmp, color_conv_argb32_to_rgb555()); break;
354 }
355 break;
356
357 case pix_format_rgb565:
358 switch(pmap_tmp.bpp())
359 {
360 case 16: color_conv(dst, &rbuf_tmp, color_conv_rgb555_to_rgb565()); break;
361 case 24: color_conv(dst, &rbuf_tmp, color_conv_rgb24_to_rgb565()); break;
362 case 32: color_conv(dst, &rbuf_tmp, color_conv_argb32_to_rgb565()); break;
363 }
364 break;
365
366 case pix_format_rgb24:
367 switch(pmap_tmp.bpp())
368 {
369 case 16: color_conv(dst, &rbuf_tmp, color_conv_rgb555_to_rgb24()); break;
370 case 24: color_conv(dst, &rbuf_tmp, color_conv_rgb24_to_rgb24()); break;
371 case 32: color_conv(dst, &rbuf_tmp, color_conv_argb32_to_rgb24()); break;
372 }
373 break;
374
375 case pix_format_bgr24:
376 switch(pmap_tmp.bpp())
377 {
378 case 16: color_conv(dst, &rbuf_tmp, color_conv_rgb555_to_bgr24()); break;
379 case 24: color_conv(dst, &rbuf_tmp, color_conv_rgb24_to_bgr24()); break;
380 case 32: color_conv(dst, &rbuf_tmp, color_conv_argb32_to_bgr24()); break;
381 }
382 break;
383
384 case pix_format_abgr32:
385 switch(pmap_tmp.bpp())
386 {
387 case 16: color_conv(dst, &rbuf_tmp, color_conv_rgb555_to_abgr32()); break;
388 case 24: color_conv(dst, &rbuf_tmp, color_conv_rgb24_to_abgr32()); break;
389 case 32: color_conv(dst, &rbuf_tmp, color_conv_argb32_to_abgr32()); break;
390 }
391 break;
392
393 case pix_format_argb32:
394 switch(pmap_tmp.bpp())
395 {
396 case 16: color_conv(dst, &rbuf_tmp, color_conv_rgb555_to_argb32()); break;
397 case 24: color_conv(dst, &rbuf_tmp, color_conv_rgb24_to_argb32()); break;
398 case 32: color_conv(dst, &rbuf_tmp, color_conv_argb32_to_argb32()); break;
399 }
400 break;
401
402 case pix_format_bgra32:
403 switch(pmap_tmp.bpp())
404 {
405 case 16: color_conv(dst, &rbuf_tmp, color_conv_rgb555_to_bgra32()); break;
406 case 24: color_conv(dst, &rbuf_tmp, color_conv_rgb24_to_bgra32()); break;
407 case 32: color_conv(dst, &rbuf_tmp, color_conv_argb32_to_bgra32()); break;
408 }
409 break;
410
411 case pix_format_rgba32:
412 switch(pmap_tmp.bpp())
413 {
414 case 16: color_conv(dst, &rbuf_tmp, color_conv_rgb555_to_rgba32()); break;
415 case 24: color_conv(dst, &rbuf_tmp, color_conv_rgb24_to_rgba32()); break;
416 case 32: color_conv(dst, &rbuf_tmp, color_conv_argb32_to_rgba32()); break;
417 }
418 break;
419 }
420
421 return true;
422 }
423
424
425
426
427
428
429
430
431 //------------------------------------------------------------------------
432 unsigned platform_specific::translate(unsigned keycode)
433 {
434 return m_last_translated_key = (keycode > 255) ? 0 : m_keymap[keycode];
435 }
436
437
438
439 //------------------------------------------------------------------------
440 platform_support::platform_support(pix_format_e format, bool flip_y) :
441 m_specific(new platform_specific(format, flip_y)),
442 m_format(format),
443 m_bpp(m_specific->m_bpp),
444 m_window_flags(0),
445 m_wait_mode(true),
446 m_flip_y(flip_y),
447 m_initial_width(10),
448 m_initial_height(10)
449 {
450 std::strcpy(m_caption, "Anti-Grain Geometry Application");
451 }
452
453
454 //------------------------------------------------------------------------
455 platform_support::~platform_support()
456 {
457 delete m_specific;
458 }
459
460
461
462 //------------------------------------------------------------------------
463 void platform_support::caption(const char* cap)
464 {
465 std::strcpy(m_caption, cap);
466 if(m_specific->m_window)
467 {
468 SetWindowTitleWithCFString (m_specific->m_window, CFStringCreateWithCStringNoCopy (nil, cap, kCFStringEncodingASCII, nil));
469 }
470 }
471
472
473
474 //------------------------------------------------------------------------
475 static unsigned get_key_flags(UInt32 wflags)
476 {
477 unsigned flags = 0;
478
479 if(wflags & shiftKey) flags |= kbd_shift;
480 if(wflags & controlKey) flags |= kbd_ctrl;
481
482 return flags;
483 }
484
485
486 //------------------------------------------------------------------------
487 void platform_support::message(const char* msg)
488 {
489 SInt16 item;
490 Str255 p_msg;
491
492 ::CopyCStringToPascal (msg, p_msg);
493 ::StandardAlert (kAlertPlainAlert, (const unsigned char*) "\013AGG Message", p_msg, NULL, &item);
494 //::StandardAlert (kAlertPlainAlert, (const unsigned char*) "\pAGG Message", p_msg, NULL, &item);
495 }
496
497
498 //------------------------------------------------------------------------
499 void platform_support::start_timer()
500 {
501 ::Microseconds (&(m_specific->m_sw_start));
502 }
503
504
505 //------------------------------------------------------------------------
506 double platform_support::elapsed_time() const
507 {
508 UnsignedWide stop;
509 ::Microseconds(&stop);
510 return double(stop.lo -
511 m_specific->m_sw_start.lo) * 1e6 /
512 double(m_specific->m_sw_freq.lo);
513 }
514
515
516 //------------------------------------------------------------------------
517 bool platform_support::init(unsigned width, unsigned height, unsigned flags)
518 {
519 if(m_specific->m_sys_format == pix_format_undefined)
520 {
521 return false;
522 }
523
524 m_window_flags = flags;
525
526 // application
527 EventTypeSpec eventType;
528 EventHandlerUPP handlerUPP;
529
530 eventType.eventClass = kEventClassApplication;
531 eventType.eventKind = kEventAppQuit;
532
533 handlerUPP = NewEventHandlerUPP(DoAppQuit);
534
535 InstallApplicationEventHandler (handlerUPP, 1, &eventType, nil, nil);
536
537 eventType.eventClass = kEventClassMouse;
538 eventType.eventKind = kEventMouseDown;
539 handlerUPP = NewEventHandlerUPP(DoMouseDown);
540 InstallApplicationEventHandler (handlerUPP, 1, &eventType, this, nil);
541
542 eventType.eventKind = kEventMouseUp;
543 handlerUPP = NewEventHandlerUPP(DoMouseUp);
544 InstallApplicationEventHandler (handlerUPP, 1, &eventType, this, nil);
545
546 eventType.eventKind = kEventMouseDragged;
547 handlerUPP = NewEventHandlerUPP(DoMouseDragged);
548 InstallApplicationEventHandler (handlerUPP, 1, &eventType, this, nil);
549
550 eventType.eventClass = kEventClassKeyboard;
551 eventType.eventKind = kEventRawKeyDown;
552 handlerUPP = NewEventHandlerUPP(DoKeyDown);
553 InstallApplicationEventHandler (handlerUPP, 1, &eventType, this, nil);
554
555 eventType.eventKind = kEventRawKeyUp;
556 handlerUPP = NewEventHandlerUPP(DoKeyUp);
557 InstallApplicationEventHandler (handlerUPP, 1, &eventType, this, nil);
558
559 eventType.eventKind = kEventRawKeyRepeat;
560 handlerUPP = NewEventHandlerUPP(DoKeyDown); // 'key repeat' is translated to 'key down'
561 InstallApplicationEventHandler (handlerUPP, 1, &eventType, this, nil);
562
563 WindowAttributes windowAttrs;
564 Rect bounds;
565
566 // window
567 windowAttrs = kWindowCloseBoxAttribute | kWindowCollapseBoxAttribute | kWindowStandardHandlerAttribute;
568 SetRect (&bounds, 0, 0, width, height);
569 OffsetRect (&bounds, 100, 100);
570 CreateNewWindow (kDocumentWindowClass, windowAttrs, &bounds, &m_specific->m_window);
571
572 if(m_specific->m_window == nil)
573 {
574 return false;
575 }
576
577 // I assume the text is ASCII.
578 // Change to kCFStringEncodingMacRoman, kCFStringEncodingISOLatin1, kCFStringEncodingUTF8 or what else you need.
579 SetWindowTitleWithCFString (m_specific->m_window, CFStringCreateWithCStringNoCopy (nil, m_caption, kCFStringEncodingASCII, nil));
580
581 eventType.eventClass = kEventClassWindow;
582 eventType.eventKind = kEventWindowClose;
583
584 handlerUPP = NewEventHandlerUPP(DoWindowClose);
585 InstallWindowEventHandler (m_specific->m_window, handlerUPP, 1, &eventType, this, NULL);
586
587 eventType.eventKind = kEventWindowDrawContent;
588 handlerUPP = NewEventHandlerUPP(DoWindowDrawContent);
589 InstallWindowEventHandler (m_specific->m_window, handlerUPP, 1, &eventType, this, NULL);
590
591 // Periodic task
592 // Instead of an idle function I use the Carbon event timer.
593 // You may decide to change the wait value which is currently 50 milliseconds.
594 EventLoopRef mainLoop;
595 EventLoopTimerUPP timerUPP;
596 EventLoopTimerRef theTimer;
597
598 mainLoop = GetMainEventLoop();
599 timerUPP = NewEventLoopTimerUPP (DoPeriodicTask);
600 InstallEventLoopTimer (mainLoop, 0, 50 * kEventDurationMillisecond, timerUPP, this, &theTimer);
601
602 m_specific->create_pmap(width, height, &m_rbuf_window);
603 m_initial_width = width;
604 m_initial_height = height;
605 on_init();
606 on_resize(width, height);
607 m_specific->m_redraw_flag = true;
608
609 ShowWindow (m_specific->m_window);
610 SetPortWindowPort (m_specific->m_window);
611
612 return true;
613 }
614
615
616 //------------------------------------------------------------------------
617 int platform_support::run()
618 {
619
620 RunApplicationEventLoop ();
621 return true;
622 }
623
624
625 //------------------------------------------------------------------------
626 const char* platform_support::img_ext() const { return ".bmp"; }
627
628 //------------------------------------------------------------------------
629 const char* platform_support::full_file_name(const char* file_name)
630 {
631 return file_name;
632 }
633
634 //------------------------------------------------------------------------
635 bool platform_support::load_img(unsigned idx, const char* file)
636 {
637 if(idx < max_images)
638 {
639 char fn[1024];
640 std::strcpy(fn, file);
641 int len = std::strlen(fn);
642 #if defined(__MWERKS__)
643 if(len < 4 || stricmp(fn + len - 4, ".BMP") != 0)
644 #else
645 if(len < 4 || strncasecmp(fn + len - 4, ".BMP", 4) != 0)
646 #endif
647 {
648 std::strcat(fn, ".bmp");
649 }
650 return m_specific->load_pmap(fn, idx, &m_rbuf_img[idx]);
651 }
652 return true;
653 }
654
655
656
657 //------------------------------------------------------------------------
658 bool platform_support::save_img(unsigned idx, const char* file)
659 {
660 if(idx < max_images)
661 {
662 char fn[1024];
663 std::strcpy(fn, file);
664 int len = std::strlen(fn);
665 #if defined(__MWERKS__)
666 if(len < 4 || stricmp(fn + len - 4, ".BMP") != 0)
667 #else
668 if(len < 4 || strncasecmp(fn + len - 4, ".BMP", 4) != 0)
669 #endif
670 {
671 std::strcat(fn, ".bmp");
672 }
673 return m_specific->save_pmap(fn, idx, &m_rbuf_img[idx]);
674 }
675 return true;
676 }
677
678
679
680 //------------------------------------------------------------------------
681 bool platform_support::create_img(unsigned idx, unsigned width, unsigned height)
682 {
683 if(idx < max_images)
684 {
685 if(width == 0) width = m_specific->m_pmap_window.width();
686 if(height == 0) height = m_specific->m_pmap_window.height();
687 m_specific->m_pmap_img[idx].create(width, height, org_e(m_specific->m_bpp));
688 m_rbuf_img[idx].attach(m_specific->m_pmap_img[idx].buf(),
689 m_specific->m_pmap_img[idx].width(),
690 m_specific->m_pmap_img[idx].height(),
691 m_flip_y ?
692 -m_specific->m_pmap_img[idx].row_bytes() :
693 m_specific->m_pmap_img[idx].row_bytes());
694 return true;
695 }
696 return false;
697 }
698
699
700 //------------------------------------------------------------------------
701 void platform_support::force_redraw()
702 {
703 Rect bounds;
704
705 m_specific->m_redraw_flag = true;
706 // on_ctrl_change ();
707 on_draw();
708
709 SetRect(&bounds, 0, 0, m_rbuf_window.width(), m_rbuf_window.height());
710 InvalWindowRect(m_specific->m_window, &bounds);
711 }
712
713
714
715 //------------------------------------------------------------------------
716 void platform_support::update_window()
717 {
718 m_specific->display_pmap(m_specific->m_window, &m_rbuf_window);
719 }
720
721
722 //------------------------------------------------------------------------
723 void platform_support::on_init() {}
724 void platform_support::on_resize(int sx, int sy) {}
725 void platform_support::on_idle() {}
726 void platform_support::on_mouse_move(int x, int y, unsigned flags) {}
727 void platform_support::on_mouse_button_down(int x, int y, unsigned flags) {}
728 void platform_support::on_mouse_button_up(int x, int y, unsigned flags) {}
729 void platform_support::on_key(int x, int y, unsigned key, unsigned flags) {}
730 void platform_support::on_ctrl_change() {}
731 void platform_support::on_draw() {}
732 void platform_support::on_post_draw(void* raw_handler) {}
733
734
735 //------------------------------------------------------------------------
736 pascal OSStatus DoWindowClose (EventHandlerCallRef nextHandler, EventRef theEvent, void* userData)
737 {
738 userData;
739
740 QuitApplicationEventLoop ();
741
742 return CallNextEventHandler (nextHandler, theEvent);
743 }
744
745
746 //------------------------------------------------------------------------
747 pascal OSStatus DoAppQuit (EventHandlerCallRef nextHandler, EventRef theEvent, void* userData)
748 {
749 userData;
750
751 return CallNextEventHandler (nextHandler, theEvent);
752 }
753
754
755 //------------------------------------------------------------------------
756 pascal OSStatus DoMouseDown (EventHandlerCallRef nextHandler, EventRef theEvent, void* userData)
757 {
758 Point wheresMyMouse;
759 UInt32 modifier;
760
761 GetEventParameter (theEvent, kEventParamMouseLocation, typeQDPoint, NULL, sizeof(Point), NULL, &wheresMyMouse);
762 GlobalToLocal (&wheresMyMouse);
763 GetEventParameter (theEvent, kEventParamKeyModifiers, typeUInt32, NULL, sizeof(UInt32), NULL, &modifier);
764
765 platform_support * app = reinterpret_cast<platform_support*>(userData);
766
767 app->m_specific->m_cur_x = wheresMyMouse.h;
768 if(app->flip_y())
769 {
770 app->m_specific->m_cur_y = app->rbuf_window().height() - wheresMyMouse.v;
771 }
772 else
773 {
774 app->m_specific->m_cur_y = wheresMyMouse.v;
775 }
776 app->m_specific->m_input_flags = mouse_left | get_key_flags(modifier);
777
778 app->m_ctrls.set_cur(app->m_specific->m_cur_x,
779 app->m_specific->m_cur_y);
780 if(app->m_ctrls.on_mouse_button_down(app->m_specific->m_cur_x,
781 app->m_specific->m_cur_y))
782 {
783 app->on_ctrl_change();
784 app->force_redraw();
785 }
786 else
787 {
788 if(app->m_ctrls.in_rect(app->m_specific->m_cur_x,
789 app->m_specific->m_cur_y))
790 {
791 if(app->m_ctrls.set_cur(app->m_specific->m_cur_x,
792 app->m_specific->m_cur_y))
793 {
794 app->on_ctrl_change();
795 app->force_redraw();
796 }
797 }
798 else
799 {
800 app->on_mouse_button_down(app->m_specific->m_cur_x,
801 app->m_specific->m_cur_y,
802 app->m_specific->m_input_flags);
803 }
804 }
805
806 return CallNextEventHandler (nextHandler, theEvent);
807 }
808
809
810 //------------------------------------------------------------------------
811 pascal OSStatus DoMouseUp (EventHandlerCallRef nextHandler, EventRef theEvent, void* userData)
812 {
813 Point wheresMyMouse;
814 UInt32 modifier;
815
816 GetEventParameter (theEvent, kEventParamMouseLocation, typeQDPoint, NULL, sizeof(Point), NULL, &wheresMyMouse);
817 GlobalToLocal (&wheresMyMouse);
818 GetEventParameter (theEvent, kEventParamKeyModifiers, typeUInt32, NULL, sizeof(UInt32), NULL, &modifier);
819
820 platform_support * app = reinterpret_cast<platform_support*>(userData);
821
822 app->m_specific->m_cur_x = wheresMyMouse.h;
823 if(app->flip_y())
824 {
825 app->m_specific->m_cur_y = app->rbuf_window().height() - wheresMyMouse.v;
826 }
827 else
828 {
829 app->m_specific->m_cur_y = wheresMyMouse.v;
830 }
831 app->m_specific->m_input_flags = mouse_left | get_key_flags(modifier);
832
833 if(app->m_ctrls.on_mouse_button_up(app->m_specific->m_cur_x,
834 app->m_specific->m_cur_y))
835 {
836 app->on_ctrl_change();
837 app->force_redraw();
838 }
839 app->on_mouse_button_up(app->m_specific->m_cur_x,
840 app->m_specific->m_cur_y,
841 app->m_specific->m_input_flags);
842
843 return CallNextEventHandler (nextHandler, theEvent);
844 }
845
846
847 //------------------------------------------------------------------------
848 pascal OSStatus DoMouseDragged (EventHandlerCallRef nextHandler, EventRef theEvent, void* userData)
849 {
850 Point wheresMyMouse;
851 UInt32 modifier;
852
853 GetEventParameter (theEvent, kEventParamMouseLocation, typeQDPoint, NULL, sizeof(Point), NULL, &wheresMyMouse);
854 GlobalToLocal (&wheresMyMouse);
855 GetEventParameter (theEvent, kEventParamKeyModifiers, typeUInt32, NULL, sizeof(UInt32), NULL, &modifier);
856
857 platform_support * app = reinterpret_cast<platform_support*>(userData);
858
859 app->m_specific->m_cur_x = wheresMyMouse.h;
860 if(app->flip_y())
861 {
862 app->m_specific->m_cur_y = app->rbuf_window().height() - wheresMyMouse.v;
863 }
864 else
865 {
866 app->m_specific->m_cur_y = wheresMyMouse.v;
867 }
868 app->m_specific->m_input_flags = mouse_left | get_key_flags(modifier);
869
870
871 if(app->m_ctrls.on_mouse_move(
872 app->m_specific->m_cur_x,
873 app->m_specific->m_cur_y,
874 (app->m_specific->m_input_flags & mouse_left) != 0))
875 {
876 app->on_ctrl_change();
877 app->force_redraw();
878 }
879 else
880 {
881 app->on_mouse_move(app->m_specific->m_cur_x,
882 app->m_specific->m_cur_y,
883 app->m_specific->m_input_flags);
884 }
885
886 return CallNextEventHandler (nextHandler, theEvent);
887 }
888
889
890 //------------------------------------------------------------------------
891 pascal OSStatus DoKeyDown (EventHandlerCallRef nextHandler, EventRef theEvent, void* userData)
892 {
893 char key_code;
894 UInt32 modifier;
895
896 GetEventParameter (theEvent, kEventParamKeyMacCharCodes, typeChar, NULL, sizeof(char), NULL, &key_code);
897 GetEventParameter (theEvent, kEventParamKeyModifiers, typeUInt32, NULL, sizeof(UInt32), NULL, &modifier);
898
899 platform_support * app = reinterpret_cast<platform_support*>(userData);
900
901 app->m_specific->m_last_translated_key = 0;
902 switch(modifier)
903 {
904 case controlKey:
905 app->m_specific->m_input_flags |= kbd_ctrl;
906 break;
907
908 case shiftKey:
909 app->m_specific->m_input_flags |= kbd_shift;
910 break;
911
912 default:
913 app->m_specific->translate(key_code);
914 break;
915 }
916
917 if(app->m_specific->m_last_translated_key)
918 {
919 bool left = false;
920 bool up = false;
921 bool right = false;
922 bool down = false;
923
924 switch(app->m_specific->m_last_translated_key)
925 {
926 case key_left:
927 left = true;
928 break;
929
930 case key_up:
931 up = true;
932 break;
933
934 case key_right:
935 right = true;
936 break;
937
938 case key_down:
939 down = true;
940 break;
941
942 //On a Mac, screenshots are handled by the system.
943 case key_f2:
944 app->copy_window_to_img(agg::platform_support::max_images - 1);
945 app->save_img(agg::platform_support::max_images - 1, "screenshot");
946 break;
947 }
948
949
950 if(app->m_ctrls.on_arrow_keys(left, right, down, up))
951 {
952 app->on_ctrl_change();
953 app->force_redraw();
954 }
955 else
956 {
957 app->on_key(app->m_specific->m_cur_x,
958 app->m_specific->m_cur_y,
959 app->m_specific->m_last_translated_key,
960 app->m_specific->m_input_flags);
961 }
962 }
963
964 return CallNextEventHandler (nextHandler, theEvent);
965 }
966
967
968 //------------------------------------------------------------------------
969 pascal OSStatus DoKeyUp (EventHandlerCallRef nextHandler, EventRef theEvent, void* userData)
970 {
971 char key_code;
972 UInt32 modifier;
973
974 GetEventParameter (theEvent, kEventParamKeyMacCharCodes, typeChar, NULL, sizeof(char), NULL, &key_code);
975 GetEventParameter (theEvent, kEventParamKeyModifiers, typeUInt32, NULL, sizeof(UInt32), NULL, &modifier);
976
977 platform_support * app = reinterpret_cast<platform_support*>(userData);
978
979 app->m_specific->m_last_translated_key = 0;
980 switch(modifier)
981 {
982 case controlKey:
983 app->m_specific->m_input_flags &= ~kbd_ctrl;
984 break;
985
986 case shiftKey:
987 app->m_specific->m_input_flags &= ~kbd_shift;
988 break;
989 }
990
991 return CallNextEventHandler (nextHandler, theEvent);
992 }
993
994
995 //------------------------------------------------------------------------
996 pascal OSStatus DoWindowDrawContent (EventHandlerCallRef nextHandler, EventRef theEvent, void* userData)
997 {
998 platform_support * app = reinterpret_cast<platform_support*>(userData);
999
1000 if(app)
1001 {
1002 if(app->m_specific->m_redraw_flag)
1003 {
1004 app->on_draw();
1005 app->m_specific->m_redraw_flag = false;
1006 }
1007 app->m_specific->display_pmap(app->m_specific->m_window, &app->rbuf_window());
1008 }
1009
1010 return CallNextEventHandler (nextHandler, theEvent);
1011 }
1012
1013
1014 //------------------------------------------------------------------------
1015 pascal void DoPeriodicTask (EventLoopTimerRef theTimer, void* userData)
1016 {
1017 platform_support * app = reinterpret_cast<platform_support*>(userData);
1018
1019 if(!app->wait_mode())
1020 app->on_idle();
1021 }
1022
1023
1024 }
1025
1026
1027
1028
1029 //----------------------------------------------------------------------------
1030 int agg_main(int argc, char* argv[]);
1031
1032
1033 // Hm. Classic MacOS does not know command line input.
1034 // CodeWarrior provides a way to mimic command line input.
1035 // The function 'ccommand' can be used to get the command
1036 // line arguments.
1037 //----------------------------------------------------------------------------
1038 int main(int argc, char* argv[])
1039 {
1040 #if defined(__MWERKS__)
1041 // argc = ccommand (&argv);
1042 #endif
1043
1044 // Check if we are launched by double-clicking under OSX
1045 // Get rid of extra argument, this will confuse the standard argument parsing
1046 // calls used in the examples to get the name of the image file to be used
1047 if ( argc >= 2 && std::strncmp(argv[1], "-psn", 4) == 0 ) {
1048 argc = 1;
1049 }
1050
1051 launch:
1052 return agg_main(argc, argv);
1053 }