window.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/cocoa/window.h
  3. // Purpose: wxWindowCocoa
  4. // Author: David Elliott
  5. // Modified by:
  6. // Created: 2002/12/26
  7. // Copyright: (c) 2002 David Elliott
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. #ifndef __WX_COCOA_WINDOW_H__
  11. #define __WX_COCOA_WINDOW_H__
  12. #include "wx/cocoa/NSView.h"
  13. #ifdef __OBJC__
  14. #import <Foundation/NSGeometry.h>
  15. #endif //def __OBJC__
  16. // We can only import Foundation/NSGeometry.h from Objective-C code but it's
  17. // nice to be able to use NSPoint and NSRect in the declarations of helper
  18. // methods so we must define them as opaque structs identically to the way
  19. // they are defined by the real header.
  20. // NOTE: We specifically use these regardless of C++ or Objective-C++ mode so
  21. // the compiler will complain if we got the definitions wrong. In regular
  22. // C++ mode there is no way to know if we got the definitons right so
  23. // we depend on at least one Objective-C++ file including this header.
  24. #if defined(__LP64__) || defined(NS_BUILD_32_LIKE_64)
  25. typedef struct CGPoint NSPoint;
  26. typedef struct CGRect NSRect;
  27. #else
  28. typedef struct _NSPoint NSPoint;
  29. typedef struct _NSRect NSRect;
  30. #endif
  31. DECLARE_WXCOCOA_OBJC_CLASS(NSAffineTransform);
  32. class wxWindowCocoaHider;
  33. class wxWindowCocoaScrollView;
  34. class wxCocoaTrackingRectManager;
  35. // ========================================================================
  36. // wxWindowCocoa
  37. // ========================================================================
  38. class WXDLLIMPEXP_CORE wxWindowCocoa: public wxWindowBase, protected wxCocoaNSView
  39. {
  40. DECLARE_DYNAMIC_CLASS(wxWindowCocoa)
  41. wxDECLARE_NO_COPY_CLASS(wxWindowCocoa);
  42. DECLARE_EVENT_TABLE()
  43. friend wxWindow *wxWindowBase::GetCapture();
  44. friend class wxWindowCocoaScrollView;
  45. friend class wxCocoaTrackingRectManager;
  46. // ------------------------------------------------------------------------
  47. // initialization
  48. // ------------------------------------------------------------------------
  49. public:
  50. wxWindowCocoa() { Init(); }
  51. inline wxWindowCocoa(wxWindow *parent, wxWindowID winid,
  52. const wxPoint& pos = wxDefaultPosition,
  53. const wxSize& size = wxDefaultSize,
  54. long style = 0,
  55. const wxString& name = wxPanelNameStr)
  56. {
  57. Init();
  58. Create(parent, winid, pos, size, style, name);
  59. }
  60. virtual ~wxWindowCocoa();
  61. bool Create(wxWindow *parent, wxWindowID winid,
  62. const wxPoint& pos = wxDefaultPosition,
  63. const wxSize& size = wxDefaultSize,
  64. long style = 0,
  65. const wxString& name = wxPanelNameStr);
  66. protected:
  67. void Init();
  68. // ------------------------------------------------------------------------
  69. // Cocoa specifics
  70. // ------------------------------------------------------------------------
  71. public:
  72. // Returns the content NSView (where children are added, drawing performed)
  73. inline WX_NSView GetNSView() { return m_cocoaNSView; }
  74. // Returns the NSView suitable for use as a subview
  75. WX_NSView GetNSViewForSuperview() const;
  76. // Returns the NSView that may be hidden/is being hidden
  77. WX_NSView GetNSViewForHiding() const;
  78. // Returns the NSView for non-client drawing
  79. virtual WX_NSView GetNonClientNSView() { return GetNSViewForSuperview(); }
  80. // Add/remove children
  81. void CocoaAddChild(wxWindowCocoa *child);
  82. void CocoaRemoveFromParent(void);
  83. #ifdef __OBJC__
  84. // Returns an autoreleased NSAffineTransform which can be applied
  85. // to a graphics context currently using the view's coordinate system
  86. // (such as the one locked when drawRect is called or after a call
  87. // to [NSView lockFocus]) such that further drawing is done using
  88. // the wxWidgets coordinate system.
  89. WX_NSAffineTransform CocoaGetWxToBoundsTransform();
  90. #endif //def __OBJC__
  91. protected:
  92. // actually enable/disable the cocoa control, overridden by subclasses
  93. virtual void CocoaSetEnabled(bool WXUNUSED(enable)) { }
  94. void CocoaCreateNSScrollView();
  95. void InitMouseEvent(wxMouseEvent &event, WX_NSEvent cocoaEvent);
  96. virtual wxWindow* GetWxWindow() const;
  97. virtual void Cocoa_FrameChanged(void);
  98. virtual void Cocoa_synthesizeMouseMoved(void);
  99. virtual bool Cocoa_drawRect(const NSRect &rect);
  100. virtual bool Cocoa_mouseDown(WX_NSEvent theEvent);
  101. virtual bool Cocoa_mouseDragged(WX_NSEvent theEvent);
  102. virtual bool Cocoa_mouseUp(WX_NSEvent theEvent);
  103. virtual bool Cocoa_mouseMoved(WX_NSEvent theEvent);
  104. virtual bool Cocoa_mouseEntered(WX_NSEvent theEvent);
  105. virtual bool Cocoa_mouseExited(WX_NSEvent theEvent);
  106. virtual bool Cocoa_rightMouseDown(WX_NSEvent theEvent);
  107. virtual bool Cocoa_rightMouseDragged(WX_NSEvent theEvent);
  108. virtual bool Cocoa_rightMouseUp(WX_NSEvent theEvent);
  109. virtual bool Cocoa_otherMouseDown(WX_NSEvent theEvent);
  110. virtual bool Cocoa_otherMouseDragged(WX_NSEvent theEvent);
  111. virtual bool Cocoa_otherMouseUp(WX_NSEvent theEvent);
  112. virtual bool Cocoa_resetCursorRects();
  113. virtual bool Cocoa_viewDidMoveToWindow();
  114. virtual bool Cocoa_viewWillMoveToWindow(WX_NSWindow newWindow);
  115. void SetNSView(WX_NSView cocoaNSView);
  116. WX_NSView m_cocoaNSView;
  117. wxWindowCocoaHider *m_cocoaHider;
  118. wxWindowCocoaScrollView *m_wxCocoaScrollView;
  119. bool m_isInPaint;
  120. wxCocoaTrackingRectManager *m_visibleTrackingRectManager;
  121. static wxWindow *sm_capturedWindow;
  122. virtual void CocoaReplaceView(WX_NSView oldView, WX_NSView newView);
  123. void SetInitialFrameRect(const wxPoint& pos, const wxSize& size);
  124. #ifdef __OBJC__
  125. inline NSRect MakeDefaultNSRect(const wxSize& size)
  126. {
  127. // NOTE: position is 10,10 to make it "obvious" that it's out of place
  128. return NSMakeRect(10.0,10.0,WidthDefault(size.x),HeightDefault(size.y));
  129. }
  130. // These functions translate NSPoint or NSRect between the coordinate
  131. // system of Cocoa's boudns rect and wx's coordinate system.
  132. NSPoint CocoaTransformBoundsToWx(NSPoint pointBounds);
  133. NSRect CocoaTransformBoundsToWx(NSRect rectBounds);
  134. NSPoint CocoaTransformWxToBounds(NSPoint pointWx);
  135. NSRect CocoaTransformWxToBounds(NSRect rectWx);
  136. #endif //def __OBJC__
  137. static wxPoint OriginInWxDisplayCoordinatesForRectInCocoaScreenCoordinates(NSRect windowFrame);
  138. static NSPoint OriginInCocoaScreenCoordinatesForRectInWxDisplayCoordinates(wxCoord x, wxCoord y, wxCoord width, wxCoord height, bool keepOriginVisible);
  139. // ------------------------------------------------------------------------
  140. // Implementation
  141. // ------------------------------------------------------------------------
  142. public:
  143. /* Pure Virtuals */
  144. // Raise the window to the top of the Z order
  145. virtual void Raise();
  146. // Lower the window to the bottom of the Z order
  147. virtual void Lower();
  148. // Set the focus to this window
  149. virtual void SetFocus();
  150. // Warp the pointer the given position
  151. virtual void WarpPointer(int x_pos, int y_pos) ;
  152. // Change the window's cursor
  153. virtual bool SetCursor( const wxCursor &cursor );
  154. // Send the window a refresh event
  155. virtual void Refresh(bool eraseBack = true, const wxRect *rect = NULL);
  156. // Set/get the window's font
  157. virtual bool SetFont(const wxFont& f);
  158. // inline virtual wxFont& GetFont() const;
  159. virtual void SetLabel(const wxString& label);
  160. virtual wxString GetLabel() const;
  161. // label handling
  162. // Get character size
  163. virtual int GetCharHeight() const;
  164. virtual int GetCharWidth() const;
  165. virtual void DoGetTextExtent(const wxString& string, int *x, int *y,
  166. int *descent = NULL,
  167. int *externalLeading = NULL,
  168. const wxFont *theFont = NULL) const;
  169. // Scroll stuff
  170. virtual void SetScrollbar(int orient, int pos, int thumbVisible,
  171. int range, bool refresh = true);
  172. virtual void SetScrollPos(int orient, int pos, bool refresh = true);
  173. virtual int GetScrollPos(int orient) const;
  174. virtual int GetScrollThumb(int orient) const;
  175. virtual int GetScrollRange(int orient) const;
  176. virtual void ScrollWindow(int dx, int dy, const wxRect *rect = NULL);
  177. virtual void DoSetVirtualSize(int x, int y);
  178. // Get the private handle (platform-dependent)
  179. virtual WXWidget GetHandle() const;
  180. // Convert client to screen coordinates
  181. virtual void DoClientToScreen(int *x, int *y) const;
  182. // Convert screen to client coordinates
  183. virtual void DoScreenToClient(int *x, int *y) const;
  184. // Capture/release mouse
  185. virtual void DoCaptureMouse();
  186. virtual void DoReleaseMouse();
  187. // Get window position, relative to parent (or screen if no parent)
  188. virtual void DoGetPosition(int *x, int *y) const;
  189. // Get overall window size
  190. virtual void DoGetSize(int *width, int *height) const;
  191. // Get/set client (application-useable) size
  192. virtual void DoGetClientSize(int *width, int *height) const;
  193. virtual void DoSetClientSize(int width, int size);
  194. // Set this window's tooltip
  195. virtual void DoSetToolTip( wxToolTip *tip );
  196. // Set the size of the wxWindow (the contentView of an NSWindow)
  197. // wxTopLevelWindow will override this and set the NSWindow size
  198. // such that the contentView will be this size
  199. virtual void CocoaSetWxWindowSize(int width, int height);
  200. // Set overall size and position
  201. virtual void DoSetSize(int x, int y, int width, int height, int sizeFlags = wxSIZE_AUTO);
  202. virtual void DoMoveWindow(int x, int y, int width, int height);
  203. // Popup a menu
  204. virtual bool DoPopupMenu(wxMenu *menu, int x, int y);
  205. /* Other implementation */
  206. // NOTE: typically Close() is not virtual, but we want this for Cocoa
  207. virtual bool Close( bool force = false );
  208. virtual bool Show( bool show = true );
  209. virtual void DoEnable( bool enable );
  210. virtual bool IsDoubleBuffered() const { return true; }
  211. };
  212. #endif // __WX_COCOA_WINDOW_H__