FireBreath  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Pages
PluginWindowMacCG.cpp
1 /**********************************************************\
2 Original Author: Georg Fritzsche
3 
4 Created: Mar 26, 2010
5 License: Dual license model; choose one of two:
6  New BSD License
7  http://www.opensource.org/licenses/bsd-license.php
8  - or -
9  GNU Lesser General Public License, version 2.1
10  http://www.gnu.org/licenses/lgpl-2.1.html
11 
12 Copyright 2010 Georg Fritzsche, Firebreath development team
13 \**********************************************************/
14 
15 #include "ConstructDefaultPluginWindows.h"
16 #include "PluginEvents/AttachedEvent.h"
17 #include "PluginEvents/DrawingEvents.h"
18 #include "PluginEvents/MacEventCocoa.h"
19 #include "PluginEvents/MacEventCarbon.h"
20 #include "Mac/PluginWindowMacCG.h"
21 
22 #include "PluginWindowMacCG.h"
23 #include <Carbon/Carbon.h>
24 
25 #if __MAC_OS_X_VERSION_MAX_ALLOWED < 1050
26 typedef float CGFloat; // hack for 10.4 sdk
27 #endif
28 
29 using namespace FB;
30 
31 FB::PluginWindowMacCG* FB::createPluginWindowMacCG()
32 {
33  return new PluginWindowMacCG();
34 }
35 
36 PluginWindowMacCG::PluginWindowMacCG()
37  : PluginWindowMac(), m_cgContext(), m_count(0)
38 {
39 }
40 
41 PluginWindowMacCG::~PluginWindowMacCG()
42 {
43 }
44 
45 NPError PluginWindowMacCG::SetWindow(NPWindow* window)
46 {
47  NP_CGContext *context = (NP_CGContext*) window->window;
48 // FBLOG_TRACE("PluginCore", "PluginWindowMacCG::SetWindow() NP_CGContext=%p", context);
49  if (context) { // This is all irrelevant with cocoa; context will be NULL
50  if ((m_cgContext.context != context->context)
51  || (m_cgContext.window != context->window))
52  {
53  m_cgContext = *context;
54  m_x = window->x;
55  m_y = window->y;
56  m_width = window->width;
57  m_height = window->height;
58  m_clipTop = window->clipRect.top;
59  m_clipLeft = window->clipRect.left;
60  m_clipBottom = window->clipRect.bottom;
61  m_clipRight = window->clipRect.right;
62 
63  ChangedEvent evt;
64  SendEvent(&evt);
65  }
66  }
67  return PluginWindowMac::SetWindow(window);
68 }
69 
70 void PluginWindowMacCG::DrawLabel(CGContextRef cgContext, FB::Rect bounds) {
71  UnsignedWide currentTime; Microseconds(&currentTime);
72  m_frames.push_front(UnsignedWideToUInt64(currentTime));
73  if (m_frames.size() > 100) m_frames.pop_back(); // only keep the last 100 frames.
74  float fps = m_frames.size() / ((m_frames.front() - m_frames.back()) / 1000000.0);
75 
76  CGContextSaveGState(cgContext);
77  CGColorSpaceRef rgbSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
78 
79  CGFloat boxColorValues[4] = {0.0, 0.0, 0.0, 1.0};
80  CGColorRef boxColor = CGColorCreate(rgbSpace, boxColorValues);
81  CGContextSetFillColorWithColor(cgContext, boxColor);
82  CGContextFillRect(cgContext, CGRectMake(0, 0, 400, 24));
83  CGColorRelease(boxColor);
84 
85  char label[256];
86  sprintf(label, "CoreGraphics %.01f fps #%lld", fps, m_count++);
87  CGFloat txtColorValues[4] = {0.0, 1.0, 0.0, 1.0};
88  CGColorRef txtColor = CGColorCreate(rgbSpace, txtColorValues);
89  CGContextSetStrokeColorWithColor(cgContext, txtColor);
90  CGContextSetFillColorWithColor(cgContext, txtColor);
91  CGColorRelease(txtColor);
92  CGColorSpaceRelease(rgbSpace);
93  CGContextSetTextMatrix(cgContext, CGAffineTransformMake(1.0, 0.0, 0.0, -1.0, 0.0, 0.0)); // Flip the text right-side up.
94  CGContextSelectFont(cgContext, "Helvetica", 24, kCGEncodingMacRoman);
95  CGContextShowTextAtPoint(cgContext, 0, 19, label, strlen(label));
96 
97  CGContextRestoreGState(cgContext);
98 }
99 
100 bool PluginWindowMacCG::SendEvent(PluginEvent* evt) {
101  // Let the plugin have first shot at drawing.
102  bool rval = PluginWindowMac::SendEvent(evt);
103  // Now, if we are supposed to draw the label...
104  if (m_drawLabel) {
105  // Firebreath gives plugins the chance to handle raw MacEventCocoa and MacEventCarbon events first.
106  // So if we have a CoreGraphicsDraw then the plugin didn't say they processed the raw event.
107  if (evt->validType<CoreGraphicsDraw>()) {
108  FB::CoreGraphicsDraw *event = evt->get<CoreGraphicsDraw>();
109  DrawLabel(event->context, event->bounds);
110  } else if (rval && evt->validType<MacEventCocoa>()) {
111  FB::MacEventCocoa *event = evt->get<MacEventCocoa>();
112  if (NPCocoaEventDrawRect == event->msg.type) {
113  FB::Rect bounds = { 0, 0, static_cast<int32_t>(event->msg.data.draw.height),
114  static_cast<int32_t>(event->msg.data.draw.width) };
115  DrawLabel(event->msg.data.draw.context, bounds);
116  }
117  } else if (rval && evt->validType<MacEventCarbon>() && rval) {
118  FB::MacEventCarbon *event = evt->get<MacEventCarbon>();
119  if (updateEvt == event->msg.what)
120  DrawLabel(m_cgContext.context, getWindowPosition());
121  }
122  }
123  return rval;
124 }
125 
Fired when a PluginEventSink has changed in some fundamental way that the plugin needs to know about...
Definition: AttachedEvent.h:50
Carbon Mac event.
Cocoa mac event.
Definition: MacEventCocoa.h:37
T * get()
Dynamic cast the pluginEvent to the specified type and throw an exception if it is not a subtype of t...
Definition: PluginEvent.h:60
bool validType()
Returns true if the object can be cast to the specified type.
Definition: PluginEvent.h:73
Plugin event base class.
Definition: PluginEvent.h:36
Mac CoreGraphics Draw event.
Definition: DrawingEvents.h:88