shaped.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: shaped.cpp
  3. // Purpose: Shaped Window sample
  4. // Author: Robin Dunn
  5. // Modified by:
  6. // Created: 28-Mar-2003
  7. // Copyright: (c) Robin Dunn
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. // ============================================================================
  11. // declarations
  12. // ============================================================================
  13. // ----------------------------------------------------------------------------
  14. // headers
  15. // ----------------------------------------------------------------------------
  16. // For compilers that support precompilation, includes "wx/wx.h".
  17. #include "wx/wxprec.h"
  18. #ifdef __BORLANDC__
  19. #pragma hdrstop
  20. #endif
  21. // for all others, include the necessary headers
  22. #ifndef WX_PRECOMP
  23. #include "wx/app.h"
  24. #include "wx/log.h"
  25. #include "wx/frame.h"
  26. #include "wx/panel.h"
  27. #include "wx/stattext.h"
  28. #include "wx/menu.h"
  29. #include "wx/layout.h"
  30. #include "wx/msgdlg.h"
  31. #include "wx/image.h"
  32. #endif
  33. #include "wx/dcclient.h"
  34. #include "wx/graphics.h"
  35. #include "wx/image.h"
  36. #ifndef wxHAS_IMAGES_IN_RESOURCES
  37. #include "../sample.xpm"
  38. #endif
  39. // ----------------------------------------------------------------------------
  40. // constants
  41. // ----------------------------------------------------------------------------
  42. // menu ids
  43. enum
  44. {
  45. Show_Shaped = 100,
  46. Show_Transparent,
  47. // must be consecutive and in the same order as wxShowEffect enum elements
  48. Show_Effect_First,
  49. Show_Effect_Roll = Show_Effect_First,
  50. Show_Effect_Slide,
  51. Show_Effect_Blend,
  52. Show_Effect_Expand,
  53. Show_Effect_Last = Show_Effect_Expand
  54. };
  55. // ----------------------------------------------------------------------------
  56. // private classes
  57. // ----------------------------------------------------------------------------
  58. // Define a new application type, each program should derive a class from wxApp
  59. class MyApp : public wxApp
  60. {
  61. public:
  62. // override base class virtuals
  63. // ----------------------------
  64. // this one is called on application startup and is a good place for the app
  65. // initialization (doing it here and not in the ctor allows to have an error
  66. // return: if OnInit() returns false, the application terminates)
  67. virtual bool OnInit();
  68. };
  69. // Main frame just contains the menu items invoking the other tests
  70. class MainFrame : public wxFrame
  71. {
  72. public:
  73. MainFrame();
  74. private:
  75. void OnShowShaped(wxCommandEvent& event);
  76. void OnShowTransparent(wxCommandEvent& event);
  77. void OnShowEffect(wxCommandEvent& event);
  78. void OnExit(wxCommandEvent& event);
  79. wxDECLARE_EVENT_TABLE();
  80. };
  81. // Define a new frame type: this is going to the frame showing the
  82. // effect of wxFRAME_SHAPED
  83. class ShapedFrame : public wxFrame
  84. {
  85. public:
  86. // ctor(s)
  87. ShapedFrame(wxFrame *parent);
  88. void SetWindowShape();
  89. // event handlers (these functions should _not_ be virtual)
  90. void OnDoubleClick(wxMouseEvent& evt);
  91. void OnLeftDown(wxMouseEvent& evt);
  92. void OnLeftUp(wxMouseEvent& evt);
  93. void OnMouseMove(wxMouseEvent& evt);
  94. void OnExit(wxMouseEvent& evt);
  95. void OnPaint(wxPaintEvent& evt);
  96. private:
  97. enum ShapeKind
  98. {
  99. Shape_None,
  100. Shape_Star,
  101. #if wxUSE_GRAPHICS_CONTEXT
  102. Shape_Circle,
  103. #endif // wxUSE_GRAPHICS_CONTEXT
  104. Shape_Max
  105. } m_shapeKind;
  106. wxBitmap m_bmp;
  107. wxPoint m_delta;
  108. // any class wishing to process wxWidgets events must use this macro
  109. wxDECLARE_EVENT_TABLE();
  110. };
  111. // Define a new frame type: this is going to the frame showing the
  112. // effect of wxWindow::SetTransparent and of
  113. // wxWindow::SetBackgroundStyle(wxBG_STYLE_TRANSPARENT)
  114. class SeeThroughFrame : public wxFrame
  115. {
  116. public:
  117. void Create();
  118. private:
  119. // event handlers (these functions should _not_ be virtual)
  120. void OnDoubleClick(wxMouseEvent& evt);
  121. void OnPaint(wxPaintEvent& evt);
  122. // any class wishing to process wxWidgets events must use this macro
  123. wxDECLARE_EVENT_TABLE();
  124. };
  125. class EffectFrame : public wxFrame
  126. {
  127. public:
  128. EffectFrame(wxWindow *parent,
  129. wxShowEffect effect,
  130. // TODO: add menu command to the main frame to allow changing
  131. // these parameters
  132. unsigned timeout = 1000)
  133. : wxFrame(parent, wxID_ANY,
  134. wxString::Format("Frame shown with %s effect",
  135. GetEffectName(effect)),
  136. wxDefaultPosition, wxSize(450, 300)),
  137. m_effect(effect),
  138. m_timeout(timeout)
  139. {
  140. new wxStaticText(this, wxID_ANY,
  141. wxString::Format("Effect: %s", GetEffectName(effect)),
  142. wxPoint(20, 20));
  143. new wxStaticText(this, wxID_ANY,
  144. wxString::Format("Timeout: %ums", m_timeout),
  145. wxPoint(20, 60));
  146. ShowWithEffect(m_effect, m_timeout);
  147. Connect(wxEVT_CLOSE_WINDOW, wxCloseEventHandler(EffectFrame::OnClose));
  148. }
  149. private:
  150. static const char *GetEffectName(wxShowEffect effect)
  151. {
  152. static const char *names[] =
  153. {
  154. "none",
  155. "roll to left",
  156. "roll to right",
  157. "roll to top",
  158. "roll to bottom",
  159. "slide to left",
  160. "slide to right",
  161. "slide to top",
  162. "slide to bottom",
  163. "fade",
  164. "expand",
  165. };
  166. wxCOMPILE_TIME_ASSERT( WXSIZEOF(names) == wxSHOW_EFFECT_MAX,
  167. EffectNamesMismatch );
  168. return names[effect];
  169. }
  170. void OnClose(wxCloseEvent& event)
  171. {
  172. HideWithEffect(m_effect, m_timeout);
  173. event.Skip();
  174. }
  175. wxShowEffect m_effect;
  176. unsigned m_timeout;
  177. };
  178. // ============================================================================
  179. // implementation
  180. // ============================================================================
  181. // ----------------------------------------------------------------------------
  182. // the application class
  183. // ----------------------------------------------------------------------------
  184. IMPLEMENT_APP(MyApp)
  185. // `Main program' equivalent: the program execution "starts" here
  186. bool MyApp::OnInit()
  187. {
  188. if ( !wxApp::OnInit() )
  189. return false;
  190. wxInitAllImageHandlers();
  191. new MainFrame;
  192. // success: wxApp::OnRun() will be called which will enter the main message
  193. // loop and the application will run. If we returned false here, the
  194. // application would exit immediately.
  195. return true;
  196. }
  197. // ----------------------------------------------------------------------------
  198. // main frame
  199. // ----------------------------------------------------------------------------
  200. wxBEGIN_EVENT_TABLE(MainFrame, wxFrame)
  201. EVT_MENU(Show_Shaped, MainFrame::OnShowShaped)
  202. EVT_MENU(Show_Transparent, MainFrame::OnShowTransparent)
  203. EVT_MENU_RANGE(Show_Effect_First, Show_Effect_Last, MainFrame::OnShowEffect)
  204. EVT_MENU(wxID_EXIT, MainFrame::OnExit)
  205. wxEND_EVENT_TABLE()
  206. MainFrame::MainFrame()
  207. : wxFrame(NULL, wxID_ANY, "wxWidgets Shaped Sample",
  208. wxDefaultPosition, wxSize(200, 100))
  209. {
  210. SetIcon(wxICON(sample));
  211. wxMenuBar * const mbar = new wxMenuBar;
  212. wxMenu * const menuFrames = new wxMenu;
  213. menuFrames->Append(Show_Shaped, "Show &shaped window\tCtrl-S");
  214. menuFrames->Append(Show_Transparent, "Show &transparent window\tCtrl-T");
  215. menuFrames->AppendSeparator();
  216. menuFrames->Append(Show_Effect_Roll, "Show &rolled effect\tCtrl-R");
  217. menuFrames->Append(Show_Effect_Slide, "Show s&lide effect\tCtrl-L");
  218. menuFrames->Append(Show_Effect_Blend, "Show &fade effect\tCtrl-F");
  219. menuFrames->Append(Show_Effect_Expand, "Show &expand effect\tCtrl-E");
  220. menuFrames->AppendSeparator();
  221. menuFrames->Append(wxID_EXIT, "E&xit");
  222. mbar->Append(menuFrames, "&Show");
  223. SetMenuBar(mbar);
  224. Show();
  225. }
  226. void MainFrame::OnShowShaped(wxCommandEvent& WXUNUSED(event))
  227. {
  228. ShapedFrame *shapedFrame = new ShapedFrame(this);
  229. shapedFrame->Show(true);
  230. }
  231. void MainFrame::OnShowTransparent(wxCommandEvent& WXUNUSED(event))
  232. {
  233. if (IsTransparentBackgroundSupported())
  234. {
  235. SeeThroughFrame *seeThroughFrame = new SeeThroughFrame;
  236. seeThroughFrame->Create();
  237. seeThroughFrame->Show(true);
  238. }
  239. else
  240. wxMessageBox(wxS("transparent window requires a composited screen"));
  241. }
  242. void MainFrame::OnShowEffect(wxCommandEvent& event)
  243. {
  244. int effect = event.GetId();
  245. static wxDirection direction = wxLEFT;
  246. direction = (wxDirection)(((int)direction)<< 1);
  247. if ( direction > wxDOWN )
  248. direction = wxLEFT ;
  249. wxShowEffect eff;
  250. switch ( effect )
  251. {
  252. case Show_Effect_Roll:
  253. switch ( direction )
  254. {
  255. case wxLEFT:
  256. eff = wxSHOW_EFFECT_ROLL_TO_LEFT;
  257. break;
  258. case wxRIGHT:
  259. eff = wxSHOW_EFFECT_ROLL_TO_RIGHT;
  260. break;
  261. case wxTOP:
  262. eff = wxSHOW_EFFECT_ROLL_TO_TOP;
  263. break;
  264. case wxBOTTOM:
  265. eff = wxSHOW_EFFECT_ROLL_TO_BOTTOM;
  266. break;
  267. default:
  268. wxFAIL_MSG( "invalid direction" );
  269. return;
  270. }
  271. break;
  272. case Show_Effect_Slide:
  273. switch ( direction )
  274. {
  275. case wxLEFT:
  276. eff = wxSHOW_EFFECT_SLIDE_TO_LEFT;
  277. break;
  278. case wxRIGHT:
  279. eff = wxSHOW_EFFECT_SLIDE_TO_RIGHT;
  280. break;
  281. case wxTOP:
  282. eff = wxSHOW_EFFECT_SLIDE_TO_TOP;
  283. break;
  284. case wxBOTTOM:
  285. eff = wxSHOW_EFFECT_SLIDE_TO_BOTTOM;
  286. break;
  287. default:
  288. wxFAIL_MSG( "invalid direction" );
  289. return;
  290. }
  291. break;
  292. case Show_Effect_Blend:
  293. eff = wxSHOW_EFFECT_BLEND;
  294. break;
  295. case Show_Effect_Expand:
  296. eff = wxSHOW_EFFECT_EXPAND;
  297. break;
  298. default:
  299. wxFAIL_MSG( "invalid effect" );
  300. return;
  301. }
  302. new EffectFrame(this, eff, 1000);
  303. }
  304. void MainFrame::OnExit(wxCommandEvent& WXUNUSED(event))
  305. {
  306. Close();
  307. }
  308. // ----------------------------------------------------------------------------
  309. // shaped frame
  310. // ----------------------------------------------------------------------------
  311. wxBEGIN_EVENT_TABLE(ShapedFrame, wxFrame)
  312. EVT_LEFT_DCLICK(ShapedFrame::OnDoubleClick)
  313. EVT_LEFT_DOWN(ShapedFrame::OnLeftDown)
  314. EVT_LEFT_UP(ShapedFrame::OnLeftUp)
  315. EVT_MOTION(ShapedFrame::OnMouseMove)
  316. EVT_RIGHT_UP(ShapedFrame::OnExit)
  317. EVT_PAINT(ShapedFrame::OnPaint)
  318. wxEND_EVENT_TABLE()
  319. // frame constructor
  320. ShapedFrame::ShapedFrame(wxFrame *parent)
  321. : wxFrame(parent, wxID_ANY, wxEmptyString,
  322. wxDefaultPosition, wxSize(100, 100),
  323. 0
  324. | wxFRAME_SHAPED
  325. | wxSIMPLE_BORDER
  326. | wxFRAME_NO_TASKBAR
  327. | wxSTAY_ON_TOP
  328. )
  329. {
  330. m_shapeKind = Shape_Star;
  331. m_bmp = wxBitmap(wxT("star.png"), wxBITMAP_TYPE_PNG);
  332. SetSize(wxSize(m_bmp.GetWidth(), m_bmp.GetHeight()));
  333. SetToolTip(wxT("Right-click to close, double click to cycle shape"));
  334. SetWindowShape();
  335. }
  336. void ShapedFrame::SetWindowShape()
  337. {
  338. switch ( m_shapeKind )
  339. {
  340. case Shape_None:
  341. SetShape(wxRegion());
  342. break;
  343. case Shape_Star:
  344. SetShape(wxRegion(m_bmp, *wxWHITE));
  345. break;
  346. #if wxUSE_GRAPHICS_CONTEXT
  347. case Shape_Circle:
  348. {
  349. wxGraphicsPath
  350. path = wxGraphicsRenderer::GetDefaultRenderer()->CreatePath();
  351. path.AddCircle(m_bmp.GetWidth()/2, m_bmp.GetHeight()/2, 30);
  352. SetShape(path);
  353. }
  354. break;
  355. #endif // wxUSE_GRAPHICS_CONTEXT
  356. case Shape_Max:
  357. wxFAIL_MSG( "invalid shape kind" );
  358. break;
  359. }
  360. }
  361. void ShapedFrame::OnDoubleClick(wxMouseEvent& WXUNUSED(evt))
  362. {
  363. m_shapeKind = static_cast<ShapeKind>((m_shapeKind + 1) % Shape_Max);
  364. SetWindowShape();
  365. }
  366. void ShapedFrame::OnLeftDown(wxMouseEvent& evt)
  367. {
  368. CaptureMouse();
  369. wxPoint pos = ClientToScreen(evt.GetPosition());
  370. wxPoint origin = GetPosition();
  371. int dx = pos.x - origin.x;
  372. int dy = pos.y - origin.y;
  373. m_delta = wxPoint(dx, dy);
  374. }
  375. void ShapedFrame::OnLeftUp(wxMouseEvent& WXUNUSED(evt))
  376. {
  377. if (HasCapture())
  378. {
  379. ReleaseMouse();
  380. }
  381. }
  382. void ShapedFrame::OnMouseMove(wxMouseEvent& evt)
  383. {
  384. wxPoint pt = evt.GetPosition();
  385. if (evt.Dragging() && evt.LeftIsDown())
  386. {
  387. wxPoint pos = ClientToScreen(pt);
  388. Move(wxPoint(pos.x - m_delta.x, pos.y - m_delta.y));
  389. }
  390. }
  391. void ShapedFrame::OnExit(wxMouseEvent& WXUNUSED(evt))
  392. {
  393. Close();
  394. }
  395. void ShapedFrame::OnPaint(wxPaintEvent& WXUNUSED(evt))
  396. {
  397. wxPaintDC dc(this);
  398. dc.DrawBitmap(m_bmp, 0, 0, true);
  399. }
  400. // ----------------------------------------------------------------------------
  401. // see-through frame
  402. // ----------------------------------------------------------------------------
  403. wxBEGIN_EVENT_TABLE(SeeThroughFrame, wxFrame)
  404. EVT_LEFT_DCLICK(SeeThroughFrame::OnDoubleClick)
  405. EVT_PAINT(SeeThroughFrame::OnPaint)
  406. wxEND_EVENT_TABLE()
  407. void SeeThroughFrame::Create()
  408. {
  409. SetBackgroundStyle(wxBG_STYLE_TRANSPARENT);
  410. wxFrame::Create(NULL, wxID_ANY, "Transparency test: double click here",
  411. wxPoint(100, 30), wxSize(300, 300),
  412. wxDEFAULT_FRAME_STYLE |
  413. wxFULL_REPAINT_ON_RESIZE |
  414. wxSTAY_ON_TOP);
  415. SetBackgroundColour(*wxWHITE);
  416. }
  417. // Paints a grid of varying hue and alpha
  418. void SeeThroughFrame::OnPaint(wxPaintEvent& WXUNUSED(evt))
  419. {
  420. wxPaintDC dc(this);
  421. dc.SetPen(wxNullPen);
  422. int xcount = 8;
  423. int ycount = 8;
  424. float xstep = 1. / xcount;
  425. float ystep = 1. / ycount;
  426. int width = GetClientSize().GetWidth();
  427. int height = GetClientSize().GetHeight();
  428. for ( float x = 0.; x < 1.; x += xstep )
  429. {
  430. for ( float y = 0.; y < 1.; y += ystep )
  431. {
  432. wxImage::RGBValue v = wxImage::HSVtoRGB(wxImage::HSVValue(x, 1., 1.));
  433. dc.SetBrush(wxBrush(wxColour(v.red, v.green, v.blue,
  434. (int)(255*(1. - y)))));
  435. int x1 = (int)(x * width);
  436. int y1 = (int)(y * height);
  437. int x2 = (int)((x + xstep) * width);
  438. int y2 = (int)((y + ystep) * height);
  439. dc.DrawRectangle(x1, y1, x2 - x1, y2 - y1);
  440. }
  441. }
  442. }
  443. void SeeThroughFrame::OnDoubleClick(wxMouseEvent& WXUNUSED(evt))
  444. {
  445. SetBackgroundStyle(wxBG_STYLE_PAINT);
  446. SetTransparent(255);
  447. SetTitle("Opaque");
  448. Refresh();
  449. }