1 //
2 // "$Id$"
3 //
4 // Mac header file for the Fast Light Tool Kit (FLTK).
5 //
6 // Copyright 1998-2016 by Bill Spitzak and others.
7 //
8 // This library is free software. Distribution and use rights are outlined in
9 // the file "COPYING" which should have been included with this file. If this
10 // file is missing or damaged, see the license at:
11 //
12 // http://www.fltk.org/COPYING.php
13 //
14 // Please report all bugs and problems on the following page:
15 //
16 // http://www.fltk.org/str.php
17 //
18
19 // Do not directly include this file, instead use <FL/x.H>. It will
20 // include this file if "__APPLE__" is defined. This is to encourage
21 // portability of even the system-specific code...
22 #ifndef FL_DOXYGEN
23
24 #if !defined(Fl_X_H)
25 # error "Never use <FL/mac.H> directly; include <FL/x.H> instead."
26 #endif // !Fl_X_H
27
28 #ifdef __OBJC__
29 @class FLWindow; // a subclass of the NSWindow Cocoa class
30 typedef FLWindow *Window;
31 #else
32 typedef class FLWindow *Window; // pointer to the FLWindow objective-c class
33 #endif // __OBJC__
34
35 #if !(defined(FL_LIBRARY) || defined(FL_INTERNALS)) // this part is used when compiling an application program
36 # include <FL/Fl_Widget.H>
37
38 typedef struct flCocoaRegion* Fl_Region;
39 typedef struct CGContext* CGContextRef;
40 typedef struct OpaquePMPrintSettings* PMPrintSettings;
41 typedef struct OpaquePMPageFormat* PMPageFormat;
42 typedef struct OpaquePMPrintSession* PMPrintSession;
43 typedef struct CGImage* CGImageRef;
44 typedef struct __CFData* CFMutableDataRef; // used in Fl_Copy_Surface.H
45 typedef CGContextRef Fl_Offscreen;
46
47 #else // this part must be compiled when building the FLTK libraries
48
49 // Standard MacOS C/C++ includes...
50 #include <ApplicationServices/ApplicationServices.h>
51 #undef check // because of Fl::check()
52
53 #ifndef MAC_OS_X_VERSION_10_4
54 #define MAC_OS_X_VERSION_10_4 1040
55 #endif
56 #ifndef MAC_OS_X_VERSION_10_5
57 #define MAC_OS_X_VERSION_10_5 1050
58 #endif
59 #ifndef MAC_OS_X_VERSION_10_6
60 #define MAC_OS_X_VERSION_10_6 1060
61 #endif
62 #ifndef MAC_OS_X_VERSION_10_7
63 #define MAC_OS_X_VERSION_10_7 1070
64 #endif
65 #ifndef MAC_OS_X_VERSION_10_8
66 #define MAC_OS_X_VERSION_10_8 1080
67 #endif
68 #ifndef MAC_OS_X_VERSION_10_9
69 #define MAC_OS_X_VERSION_10_9 1090
70 #endif
71 #ifndef MAC_OS_X_VERSION_10_10
72 #define MAC_OS_X_VERSION_10_10 101000
73 #endif
74 #ifndef MAC_OS_X_VERSION_10_11
75 #define MAC_OS_X_VERSION_10_11 101100
76 #endif
77 #ifndef MAC_OS_X_VERSION_10_12
78 #define MAC_OS_X_VERSION_10_12 101200
79 #endif
80 #ifndef MAC_OS_X_VERSION_10_13
81 #define MAC_OS_X_VERSION_10_13 101300
82 #endif
83 #ifndef MAC_OS_X_VERSION_10_14
84 #define MAC_OS_X_VERSION_10_14 101400
85 #endif
86
87 #ifndef NSINTEGER_DEFINED // appears with 10.5 in NSObjCRuntime.h
88 #if defined(__LP64__) && __LP64__
89 typedef long NSInteger;
90 typedef unsigned long NSUInteger;
91 #else
92 typedef int NSInteger;
93 typedef unsigned int NSUInteger;
94 #endif
95 #endif
96
97 #ifdef __OBJC__
98 @class NSCursor;
99 @class NSOpenGLPixelFormat;
100 @class NSOpenGLContext;
101 #else
102 class NSCursor;
103 class NSOpenGLPixelFormat;
104 class NSOpenGLContext;
105 #endif // __OBJC__
106
107 typedef CGContextRef Fl_Offscreen;
108 #if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_4
109 typedef CGImageAlphaInfo CGBitmapInfo;
110 #endif
111
112 typedef struct flCocoaRegion {
113 int count;
114 CGRect *rects;
115 } *Fl_Region; // a region is the union of a series of rectangles
116
117 # include "Fl_Window.H"
118 # include "../src/Fl_Font.H"
119
120 // Some random X equivalents
121 struct XPoint { int x, y; };
122 struct XRectangle {int x, y, width, height;};
123 #ifndef CGFLOAT_DEFINED //appears with 10.5 in CGBase.h
124 #if defined(__LP64__) && __LP64__
125 typedef double CGFloat;
126 #else
127 typedef float CGFloat;
128 #endif
129 #endif // CGFLOAT_DEFINED
130
131 extern CGRect fl_cgrectmake_cocoa(int x, int y, int w, int h);
XRectangleRegion(int x,int y,int w,int h)132 inline Fl_Region XRectangleRegion(int x, int y, int w, int h) {
133 Fl_Region R = (Fl_Region)malloc(sizeof(*R));
134 R->count = 1;
135 R->rects = (CGRect *)malloc(sizeof(CGRect));
136 *(R->rects) = fl_cgrectmake_cocoa(x, y, w, h);
137 return R;
138 }
XDestroyRegion(Fl_Region r)139 inline void XDestroyRegion(Fl_Region r) {
140 if(r) {
141 free(r->rects);
142 free(r);
143 }
144 }
145 extern NSCursor *fl_default_cursor;
146
147 // This object contains all mac-specific stuff about a window:
148 // WARNING: this object is highly subject to change!
149 class Fl_X {
150
151 public:
152 Window xid; // pointer to the Cocoa window object (FLWindow*)
153 Fl_Offscreen other_xid; // pointer for offscreen bitmaps (overlay window)
154 Fl_Window *w; // FLTK window for
155 Fl_Region region;
156 #if FLTK_ABI_VERSION < 10304
157 Fl_Region subRegion; // for ABI compatibility, recycled to replace subRect_
158 #endif
159 Fl_X *next; // chain of mapped windows
160 #if FLTK_ABI_VERSION < 10304
161 Fl_X *xidChildren; // useless with true subwindows, recycled to replace mapped_to_retina_
162 Fl_X *xidNext; // useless with true subwindows
163 #endif
164 int wait_for_expose;
165 NSCursor *cursor;
166 static Fl_X* first;
i(const Fl_Window * w)167 static Fl_X* i(const Fl_Window* w) {return w->i;}
168 static int fake_X_wm(const Fl_Window*,int&,int&,int&,int&,int&,int,int,int,int);
169 static void make(Fl_Window*);
170 void flush();
171 static void set_high_resolution(bool);
172 #if FLTK_ABI_VERSION >= 10304
subRect()173 CGRect* subRect() { return subRect_; } // getter
subRect(CGRect * r)174 void subRect(CGRect *r) { subRect_ = r; } // setter
175 #else
subRect()176 CGRect* subRect() { return (CGRect*)subRegion; } // getter
subRect(CGRect * r)177 void subRect(CGRect *r) { subRegion = (Fl_Region)r; } // setter
178 #endif
179 bool mapped_to_retina(); // is window mapped to retina display?
180 void mapped_to_retina(bool); // sets whether window is mapped to retina display
181 bool changed_resolution(); // did window just moved to display with another resolution?
182 void changed_resolution(bool);// sets whether window just moved to display with another resolution
183 bool in_windowDidResize(); // is window performing windowDidResize?
184 void in_windowDidResize(bool); // sets whether window is performing windowDidResize
185 // Quartz additions:
186 CGContextRef gc; // graphics context (NULL when using QD)
187 static void q_fill_context(); // fill a Quartz context with current FLTK state
188 static void q_clear_clipping(); // remove all clipping from a Quartz context
189 static void q_release_context(Fl_X *x=0); // free all resources associated with fl_gc
190 static void q_begin_image(CGRect&, int x, int y, int w, int h);
191 static void q_end_image();
192 // Cocoa additions
193 static NSOpenGLPixelFormat *mode_to_NSOpenGLPixelFormat(int mode, const int*); // computes NSOpenGLPixelFormat from Gl window's mode
194 static NSOpenGLContext* create_GLcontext_for_window(NSOpenGLPixelFormat *pixelformat, NSOpenGLContext *shared_ctx, Fl_Window *window);
195 static void GLcontext_update(NSOpenGLContext*);
196 static void GLcontext_flushbuffer(NSOpenGLContext*);
197 static void GLcontext_release(NSOpenGLContext*);
198 static void GLcontext_makecurrent(NSOpenGLContext*);
199 static void GL_cleardrawable(void);
200 static void gl_start(NSOpenGLContext*);
201 void destroy(void);
202 void map(void);
203 void unmap(void);
204 void collapse(void);
205 WindowRef window_ref(void); // useless with cocoa GL windows
206 void set_key_window(void);
207 // OS X doesn't have per window icons
set_default_icons(const Fl_RGB_Image * [],int)208 static void set_default_icons(const Fl_RGB_Image*[], int) {};
set_icons()209 void set_icons() {};
210 int set_cursor(Fl_Cursor);
211 int set_cursor(const Fl_RGB_Image*, int, int);
212 static CGImageRef CGImage_from_window_rect(Fl_Window *win, int x, int y, int w, int h);
213 static unsigned char *bitmap_from_window_rect(Fl_Window *win, int x, int y, int w, int h, int *bytesPerPixel);
214 static Fl_Region intersect_region_and_rect(Fl_Region current, int x,int y,int w, int h);
215 static void *get_carbon_function(const char *name);
216 static void screen_work_area(int &X, int &Y, int &W, int &H, int n); // compute work area of a given screen
217 static int next_marked_length; // next length of marked text after current marked text will have been replaced
218 static int insertion_point_location(int *px, int *py, int *pheight); // computes window coordinates & height of insertion point
219 static const int CoreText_threshold; // Mac OS version from which the Core Text API is used to display text
220 static Fl_Fontdesc* calc_fl_fonts(void); // computes the fl_fonts global variable
221 static int dnd(int use_selection); // call Fl_X::dnd(1) to support text dragging
222 static int calc_mac_os_version(void); // computes the fl_mac_os_version global variable
223 static void clip_to_rounded_corners(CGContextRef gc, int w, int h);
224 static void *get_titlebar_layer(Fl_Window *win);
225 static void draw_layer_to_context(void *layer, CGContextRef ctxt, int w, int h);
226 private:
227 #if FLTK_ABI_VERSION >= 10304
228 CGRect* subRect_; // makes sure subwindow remains inside its parent window
229 // stores 3 binary flags: whether window is mapped to retina display; whether resolution just changed;
230 // whether window is OpenGL and is currently being resized.
231 unsigned mapped_to_retina_;
232 #else
233 bool subwindow; // for ABI compatibility, useless with true subwindows
234 #endif
235 };
236
237 extern Window fl_window;
238
239 #endif // FL_LIBRARY || FL_INTERNALS
240
241 typedef CGImageRef Fl_Bitmask;
242
243 extern CGContextRef fl_gc;
244
245 extern Window fl_xid(const Fl_Window*);
246 extern Fl_Window* fl_find(Window xid);
247 void fl_clip_region(Fl_Region);
248
249 extern Fl_Bitmask fl_create_bitmask(int w, int h, const uchar *data);
250 extern Fl_Bitmask fl_create_alphamask(int w, int h, int d, int ld, const uchar *data);
251 extern void fl_delete_bitmask(Fl_Bitmask bm);
252 extern Fl_Offscreen fl_create_offscreen(int w, int h);
253 extern void fl_copy_offscreen(int x,int y,int w,int h, Fl_Offscreen gWorld, int srcx,int srcy);
254 extern void fl_delete_offscreen(Fl_Offscreen gWorld);
255 extern void fl_begin_offscreen(Fl_Offscreen gWorld);
256 extern void fl_end_offscreen();
257
258 extern int fl_parse_color(const char* p, uchar& r, uchar& g, uchar& b);
259 extern void fl_open_display();
260
261 #endif // FL_DOXYGEN
262 /** \file
263 Mac OS X-specific symbols.
264 */
265
266 /** \defgroup group_macosx Mac OS X-specific symbols
267 Mac OS X-specific symbols declared in <FL/x.H> or <FL/gl.h>
268 \sa \ref osissues_macos
269 @{ */
270
271 /** @brief Register a function called for each file dropped onto an application icon.
272 \e cb will be called with a single Unix-style file name and path.
273 If multiple files were dropped, \e cb will be called multiple times.
274 */
275 extern void fl_open_callback(void (*cb)(const char *));
276
277 /**
278 * \brief Attaches a callback to the "About myprog" item of the system application menu.
279 *
280 * \param cb a callback that will be called by "About myprog" menu item
281 * with NULL 1st argument.
282 * \param user_data a pointer transmitted as 2nd argument to the callback.
283 * \param shortcut optional shortcut to attach to the "About myprog" menu item (e.g., FL_META+'a')
284 */
285 extern void fl_mac_set_about( Fl_Callback *cb, void *user_data, int shortcut = 0);
286
287 /** \brief The version number of the running Mac OS X (e.g., 100604 for 10.6.4)
288 */
289 extern int fl_mac_os_version;
290
291 /** Determines whether cmd-Q or the "Quit xxx" item of application menu terminates the app or only the event loop.
292 By default, fl_mac_quit_early = 1, and cmd-Q or "Quit xxx" terminate the app when all windows are closed
293 without Fl::run() returning. If fl_mac_quit_early is set to 0, cmd-Q or "Quit xxx" terminate only the event loop
294 when all windows are closed, and Fl::run() returns.
295 \note This OS-specific variable will not be part of the API of FLTK 1.4.
296 */
297 extern int fl_mac_quit_early;
298
299 /** The system menu bar.
300 */
301 extern class Fl_Sys_Menu_Bar *fl_sys_menu_bar;
302
303 struct Fl_Menu_Item;
304
305 class Fl_Mac_App_Menu {
306 public:
307 /** Localizable text for the "About xxx" application menu item */
308 static const char *about;
309 /** Localizable text for the "Print Front Window" application menu item.
310 This menu item won't be displayed if Fl_Mac_App_Menu::print
311 is set to an empty string.
312 */
313 static const char *print;
314 /** Localizable text for the "Services" application menu item */
315 static const char *services;
316 /** Localizable text for the "Hide xxx" application menu item */
317 static const char *hide;
318 /** Localizable text for the "Hide Others" application menu item */
319 static const char *hide_others;
320 /** Localizable text for the "Show All" application menu item */
321 static const char *show;
322 /** Localizable text for the "Quit xxx" application menu item */
323 static const char *quit;
324 static void custom_application_menu_items(const Fl_Menu_Item *m);
325 };
326
327 /** @} */
328
329 //
330 // End of "$Id$".
331 //
332
333