How to create a customized frame - Part 3 (Phoenix)
Keywords : Shaped frame, Customized frame, Customized button, Transparency, Roll, Unroll, Fade in, Fade out, Shape, Region, BufferedPaintDC, BufferedDC, ClientDC, GCDC, AutoBufferedPaintDCFactory, Config, AcceleratorTable.
Contents
Demonstrating (only Windows) :
Tested py3.x, wx4.x and Win10.
Are you ready to use some samples ?
Test, modify, correct, complete, improve and share your discoveries !
Frame without resize border and colored panel :
First example
1 # sample_one.py
2
3 import sys
4 import os
5 import platform
6 import wx
7 import wx.lib.fancytext as fancytext
8 import wx.lib.agw.flatmenu as FM
9 from wx.lib.agw.artmanager import ArtManager, RendererBase, DCSaver
10 from wx.lib.agw.fmresources import ControlFocus, ControlPressed
11 import wx.dataview as dv
12 import wx.lib.colourdb
13 import rndshapedbitmapbutton as SBB
14 import data
15
16 musicdata = sorted(data.musicdata.items())
17 musicdata = [[str(k)] + list(v) for k,v in musicdata]
18
19 # def SwitchRGBtoBGR
20 # def CreateBackgroundBitmap
21 # class MyMenuRenderer
22 # class MyListCtrlPnl
23 # class MyStatusBar
24 # class MyTitleBar
25 # class MyAboutDlg
26 # class MyPopupMenu
27 # class MyWindow
28 # class MyButtonBox
29 # class MyTitleBarPnl
30 # class MyMainPnl
31 # class MyFrame
32 # class MyApp
33
34 ID_FULLSCREEN = wx.NewIdRef()
35 ID_MAIN_PNL = wx.NewIdRef()
36 ID_BTN_FULLSCREEN = wx.NewIdRef()
37 ID_BTN_ABOUT = wx.NewIdRef()
38 ID_BTN_QUIT = wx.NewIdRef()
39 ID_HELLO = wx.NewIdRef()
40
41 #---------------------------------------------------------------------------
42
43 def SwitchRGBtoBGR(colour):
44 """
45 ...
46 """
47
48 return wx.Colour(colour.Blue(), colour.Green(), colour.Red())
49
50 #---------------------------------------------------------------------------
51
52 def CreateBackgroundBitmap():
53 """
54 ...
55 """
56
57 mem_dc = wx.MemoryDC()
58 bmp = wx.Bitmap(121, 300)
59 mem_dc.SelectObject(bmp)
60
61 mem_dc.Clear()
62
63 # Colour the menu face with background colour.
64 mem_dc.SetPen(wx.Pen("#a0a0a0", 0))
65 mem_dc.SetBrush(wx.Brush("#a19f94"))
66 mem_dc.DrawRectangle(0, 15, 121, 300)
67 mem_dc.DrawLine(0, 0, 300, 0)
68
69 mem_dc.SelectObject(wx.NullBitmap)
70 return bmp
71
72 #---------------------------------------------------------------------------
73
74 class MyMenuRenderer(FM.FMRenderer):
75 """
76 Thanks to Andrea Gavana.
77 A custom renderer class for FlatMenu.
78 """
79 def __init__(self):
80 FM.FMRenderer.__init__(self)
81
82 #-----------------------------------------------------------------------
83
84 def DrawMenuButton(self, dc, rect, state):
85 """
86 Draws the highlight on a FlatMenu.
87 """
88
89 self.DrawButton(dc, rect, state)
90
91
92 def DrawMenuBarButton(self, dc, rect, state):
93 """
94 Draws the highlight on a FlatMenuBar.
95 """
96
97 self.DrawButton(dc, rect, state)
98
99
100 def DrawButton(self, dc, rect, state, colour=None):
101 """
102 ...
103 """
104
105 if state == ControlFocus:
106 penColour = SwitchRGBtoBGR(ArtManager.Get().FrameColour())
107 brushColour = SwitchRGBtoBGR(ArtManager.Get().BackgroundColour())
108 elif state == ControlPressed:
109 penColour = SwitchRGBtoBGR(ArtManager.Get().FrameColour())
110 brushColour = SwitchRGBtoBGR(ArtManager.Get().HighlightBackgroundColour())
111 else: # ControlNormal, ControlDisabled, default.
112 penColour = SwitchRGBtoBGR(ArtManager.Get().FrameColour())
113 brushColour = SwitchRGBtoBGR(ArtManager.Get().BackgroundColour())
114
115 # Draw the button borders.
116 dc = wx.GCDC(dc)
117 dc.SetPen(wx.Pen(penColour))
118 dc.SetBrush(wx.Brush(brushColour))
119 dc.DrawRoundedRectangle(rect.x, rect.y, rect.width, rect.height-3, 4)
120
121
122 def DrawMenuBarBackground(self, dc, rect):
123 """
124 ...
125 """
126
127 # For office style, we simple draw a rectangle
128 # with a gradient colouring.
129 vertical = ArtManager.Get().GetMBVerticalGradient()
130
131 dcsaver = DCSaver(dc)
132
133 # Fill with gradient.
134 startColour = self.menuBarFaceColour
135 endColour = ArtManager.Get().LightColour(startColour, 0)
136
137 dc.SetPen(wx.Pen(endColour))
138 dc.SetBrush(wx.Brush(endColour))
139 dc.DrawRectangle(rect)
140
141
142 def DrawToolBarBg(self, dc, rect):
143 """
144 ...
145 """
146
147 if not ArtManager.Get().GetRaiseToolbar():
148 return
149
150 # Fill with gradient.
151 startColour = self.menuBarFaceColour()
152 dc.SetPen(wx.Pen(startColour))
153 dc.SetBrush(wx.Brush(startColour))
154 dc.DrawRectangle(0, 0, rect.GetWidth(), rect.GetHeight())
155
156 #---------------------------------------------------------------------------
157
158 class MyListCtrlPnl(wx.Panel):
159 """
160 Thanks to Robin Dunn.
161 """
162 def __init__(self, parent):
163 wx.Panel.__init__(self, parent, -1)
164
165 # Create the listctrl.
166 self.dvlc = dv.DataViewListCtrl(self,
167 style=wx.BORDER_SIMPLE|
168 dv.DV_ROW_LINES|
169 dv.DV_HORIZ_RULES|
170 dv.DV_VERT_RULES)
171
172 # Give it some columns.
173 # The ID col we'll customize a bit :
174 self.dvlc.AppendTextColumn("Id", width=40)
175 self.dvlc.AppendTextColumn("Artist", width=170)
176 self.dvlc.AppendTextColumn("Title", width=260)
177 self.dvlc.AppendTextColumn("Genre", width=80)
178
179 # Load the data. Each item (row) is added as a sequence
180 # of values whose order matches the columns.
181 for itemvalues in musicdata:
182 self.dvlc.AppendItem(itemvalues)
183
184 self.dvlc.SetBackgroundColour("gray50")
185 self.dvlc.SetForegroundColour("gray10")
186
187 # Set the layout so the listctrl fills the panel.
188 self.Sizer = wx.BoxSizer()
189 self.Sizer.Add(self.dvlc, 1, wx.EXPAND)
190
191 #---------------------------------------------------------------------------
192
193 class MyStatusBar(wx.StatusBar) :
194 """
195 Thanks to ???.
196 Simple custom colorized StatusBar.
197 """
198 def __init__(self, parent, id) :
199 wx.StatusBar.__init__(self, parent, id)
200
201 #------------
202
203 # Return config file.
204 self.config = wx.GetApp().GetConfig()
205
206 #------------
207
208 # Simplified init method.
209 self.SetProperties()
210 self.BindEvents()
211
212 #-----------------------------------------------------------------------
213
214 def SetProperties(self):
215 """
216 ...
217 """
218
219 # Read config file.
220 self.SetBackgroundColour(wx.Colour(self.config.Read("Color1")))
221
222
223 def BindEvents(self):
224 """
225 Bind some events to an events handler.
226 """
227
228 self.Bind(wx.EVT_PAINT, self.OnPaint)
229
230
231 def OnPaint(self, event) :
232 """
233 ...
234 """
235
236 dc = wx.BufferedPaintDC(self)
237 self.Draw(dc)
238
239
240 def Draw(self, dc) :
241 """
242 ...
243 """
244
245 # Read config file.
246 dc.SetBackground(wx.Brush(self.config.Read("Color1")))
247 dc.Clear()
248
249 textVertOffset = 3 # Perfect for MSW.
250 textHorzOffset = 5 # Arbitrary - looks nicer.
251
252 dc.SetTextForeground(wx.BLACK)
253 dc.DrawText(self.GetStatusText(), textHorzOffset, textVertOffset)
254
255 #---------------------------------------------------------------------------
256
257 class MyTitleBar(wx.Control):
258 """
259 Thanks to Cody Precord.
260 """
261 def __init__(self, parent, label, size):
262 style = (wx.NO_BORDER)
263 super(MyTitleBar, self).__init__(parent,
264 style=style)
265
266 #------------
267
268 # Return config file.
269 self.config = wx.GetApp().GetConfig()
270
271 #------------
272
273 # Attributes.
274 self.parent = parent
275 self.label = label
276 self.size = size
277
278 #------------
279
280 # Simplified init method.
281 self.SetBackground()
282 self.SetProperties(label, size)
283 self.BindEvents()
284
285 #-----------------------------------------------------------------------
286
287 def SetBackground(self):
288 """
289 ...
290 """
291
292 self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM)
293
294
295 def SetProperties(self, label, size):
296 """
297 ...
298 """
299
300 self.label = label
301 self.size = size
302
303 self.label_font = self.GetFont()
304 self.label_font.SetFamily(wx.SWISS)
305 self.label_font.SetPointSize(size)
306 self.label_font.SetWeight(wx.BOLD)
307 self.SetFont(self.label_font)
308
309
310 def CreateCtrls(self):
311 """
312 ...
313 """
314
315 w, h = self.GetSize()
316 w1, h1 = self.GetClientSize()
317
318 #------------
319
320 # Load an icon bitmap for titlebar.
321 # bmp = wx.Bitmap(os.path.join(self.bitmaps_dir,
322 # "icon_app.png"),
323 # type=wx.BITMAP_TYPE_PNG)
324
325 # self.ico = wx.StaticBitmap(self, -1, bmp)
326 # self.ico.SetBackgroundColour(wx.Colour(self.config.Read("Color1")))
327 # self.ico.SetPosition((300, 1))
328 # self.ico.SetToolTip("This is a customized icon.")
329 # self.ico.SetBackgroundStyle(wx.BG_STYLE_CUSTOM)
330
331
332 def BindEvents(self):
333 """
334 Bind some events to an events handler.
335 """
336
337 # self.ico.Bind(wx.EVT_RIGHT_DOWN, self.OnRightDown)
338 # self.ico.Bind(wx.EVT_LEFT_DOWN, self.OnRightDown)
339
340 self.Bind(wx.EVT_PAINT, self.OnPaint)
341 self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
342 self.Bind(wx.EVT_LEFT_UP, self.OnLeftUp)
343
344
345 def OnRightDown(self, event):
346 """
347 ...
348 """
349
350 self.PopupMenu(MyPopupMenu(self), event.GetPosition())
351
352 print("Right down.")
353
354
355 def OnLeftDown(self, event):
356 """
357 ...
358 """
359
360 self.GetTopLevelParent().OnLeftDown(event)
361
362
363 def OnLeftUp(self, event):
364 """
365 ...
366 """
367
368 self.GetTopLevelParent().OnLeftUp(event)
369
370
371 def SetLabel(self, label):
372 """
373 ...
374 """
375
376 self.label = label
377 self.Refresh()
378
379
380 def DoGetBestSize(self):
381 """
382 ...
383 """
384
385 dc = wx.ClientDC(self)
386 dc.SetFont(self.GetFont())
387
388 textWidth, textHeight = dc.GetTextExtent(self.label)
389 spacing = 10
390 totalWidth = textWidth + (spacing)
391 totalHeight = textHeight + (spacing)
392
393 best = wx.Size(totalWidth, totalHeight)
394 self.CacheBestSize(best)
395
396 return best
397
398
399 def GetLabel(self):
400 """
401 ...
402 """
403
404 return self.label
405
406
407 def GetLabelColor(self):
408 """
409 ...
410 """
411
412 return self.foreground
413
414
415 def GetLabelSize(self):
416 """
417 ...
418 """
419
420 return self.size
421
422
423 def SetLabelColour(self, colour):
424 """
425 ...
426 """
427
428 self.labelColour = colour
429
430
431 def OnPaint(self, event):
432 """
433 ...
434 """
435
436 dc = wx.BufferedPaintDC(self)
437 gcdc = wx.GCDC(dc)
438
439 gcdc.Clear()
440
441 # Setup the GraphicsContext.
442 gc = gcdc.GetGraphicsContext()
443
444 # Get the working size we can draw in.
445 width, height = self.GetSize()
446
447 # Use the GCDC to draw the text.
448 # Read config file.
449 brush = self.config.Read("Color1")
450 gcdc.SetPen(wx.Pen(brush, 1))
451 gcdc.SetBrush(wx.Brush(brush))
452 gcdc.DrawRectangle(0, 0, width, height)
453
454 # Get the system font.
455 gcdc.SetFont(self.GetFont())
456
457 textWidth, textHeight = gcdc.GetTextExtent(self.label)
458 tposx, tposy = ((width/2)-(textWidth/2), (height/3)-(textHeight/3))
459
460 tposx += 0
461 tposy += 4
462
463 # Set position and text color.
464 gcdc.SetTextForeground("black")
465 gcdc.DrawText(self.label, int(tposx), int(tposy+1))
466
467 gcdc.SetTextForeground(self.labelColour)
468 gcdc.DrawText(self.label, int(tposx), int(tposy))
469
470 #---------------------------------------------------------------------------
471
472 class MyAboutDlg(wx.Frame):
473 """
474 Thanks to Robin Dunn.
475 """
476 def __init__(self, parent):
477 style = (wx.FRAME_SHAPED | wx.NO_BORDER |
478 wx.CLIP_CHILDREN | wx.STAY_ON_TOP |
479 wx.SYSTEM_MENU | wx.CLOSE_BOX |
480 wx.NO_FULL_REPAINT_ON_RESIZE)
481 wx.Frame.__init__(self,
482 parent,
483 id=-1,
484 title="About...",
485 style=style)
486
487 #------------
488
489 # Attributes.
490 self.SetTransparent(0)
491 self.opacity_in = 0
492 self.opacity_out = 255
493 self.deltaN = -70
494 self.hasShape = False
495 self.delta = wx.Point(0,0)
496
497 #------------
498
499 # Return config file.
500 self.config = wx.GetApp().GetConfig()
501 # Return application name.
502 self.app_name = wx.GetApp().GetAppName()
503 # Return bitmaps folder.
504 self.bitmaps_dir = wx.GetApp().GetBitmapsDir()
505 # Return icons folder.
506 self.icons_dir = wx.GetApp().GetIconsDir()
507
508 #------------
509
510 # Simplified init method.
511 self.SetProperties()
512 self.OnTimerIn(self)
513 self.CreateCtrls()
514 self.BindEvents()
515
516 #------------
517
518 self.CenterOnParent(wx.BOTH)
519 self.GetParent().Enable(False)
520
521 #------------
522
523 self.Show(True)
524
525 #------------
526
527 self.eventLoop = wx.GUIEventLoop()
528 self.eventLoop.Run()
529
530 #-----------------------------------------------------------------------
531
532 def SetProperties(self):
533 """
534 Set the dialog properties (title, icon, transparency...).
535 """
536
537 frameIcon = wx.Icon(os.path.join(self.icons_dir,
538 "icon_wxWidgets.ico"),
539 type=wx.BITMAP_TYPE_ICO)
540 self.SetIcon(frameIcon)
541
542
543 def OnTimerIn(self, evt):
544 """
545 Thanks to Pascal Faut.
546 """
547
548 self.timer1 = wx.Timer(self, -1)
549 self.timer1.Start(1)
550 self.Bind(wx.EVT_TIMER, self.AlphaCycle1, self.timer1)
551
552 print("Fade-in was launched.")
553
554
555 def OnTimerOut(self, evt):
556 """
557 Thanks to Pascal Faut.
558 """
559
560 self.timer2 = wx.Timer(self, -1)
561 self.timer2.Start(1)
562 self.Bind(wx.EVT_TIMER, self.AlphaCycle2, self.timer2)
563
564 print("Fade-out was launched.")
565
566
567 def AlphaCycle1(self, *args):
568 """
569 Thanks to Pascal Faut.
570 """
571
572 self.opacity_in += self.deltaN
573 if self.opacity_in <= 0:
574 self.deltaN = -self.deltaN
575 self.opacity_in = 0
576
577 if self.opacity_in >= 255:
578 self.deltaN = -self.deltaN
579 self.opacity_in = 255
580
581 self.timer1.Stop()
582
583 self.SetTransparent(self.opacity_in)
584
585 print("Fade in = {}/255".format(self.opacity_in))
586
587
588 def AlphaCycle2(self, *args):
589 """
590 Thanks to Pascal Faut.
591 """
592
593 self.opacity_out += self.deltaN
594 if self.opacity_out >= 255:
595 self.deltaN = -self.deltaN
596 self.opacity_out = 255
597
598 if self.opacity_out <= 0:
599 self.deltaN = -self.deltaN
600 self.opacity_out = 0
601
602 self.timer2.Stop()
603
604 wx.CallAfter(self.Destroy)
605
606 self.SetTransparent(self.opacity_out)
607
608 print("Fade out = {}/255".format(self.opacity_out))
609
610
611 def CreateCtrls(self):
612 """
613 Make widgets for my dialog.
614 """
615
616 # Load a background bitmap.
617 self.bmp = wx.Bitmap(os.path.join(self.bitmaps_dir,
618 "skin_about_3.png"),
619 type=wx.BITMAP_TYPE_PNG)
620 mask = wx.Mask(self.bmp, wx.RED)
621 self.bmp.SetMask(mask)
622
623 #------------
624
625 self.SetClientSize((self.bmp.GetWidth(), self.bmp.GetHeight()))
626
627 #------------
628
629 if wx.Platform == "__WXGTK__":
630 # wxGTK requires that the window be created before you can
631 # set its shape, so delay the call to SetWindowShape until
632 # this event.
633 self.Bind(wx.EVT_WINDOW_CREATE, self.SetWindowShape)
634 else:
635 # On wxMSW and wxMac the window has already
636 # been created, so go for it.
637 self.SetWindowShape()
638
639
640 def BindEvents(self):
641 """
642 Bind all the events related to my dialog.
643 """
644
645 # Bind some events to an events handler.
646 self.Bind(wx.EVT_LEFT_UP, self.OnLeftUp)
647 self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
648 self.Bind(wx.EVT_RIGHT_UP, self.OnCloseWindow) # Panel right clic.
649 self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)
650 self.Bind(wx.EVT_WINDOW_CREATE, self.SetWindowShape)
651 self.Bind(wx.EVT_MOTION, self.OnMouseMove)
652 self.Bind(wx.EVT_KEY_UP, self.OnKeyUp)
653 self.Bind(wx.EVT_PAINT, self.OnPaint)
654 self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
655
656
657 def SetWindowShape(self, event=None):
658 """
659 ...
660 """
661
662 # Use the bitmap's mask to determine the region.
663 r = wx.Region(self.bmp)
664 self.hasShape = self.SetShape(r)
665
666
667 def OnEraseBackground(self, event):
668 """
669 ...
670 """
671
672 dc = event.GetDC()
673 if not dc:
674 dc = wx.ClientDC(self)
675 rect = self.GetUpdateRegion().GetBox()
676 dc.SetClippingRect(rect)
677
678
679 def OnLeftDown(self, event):
680 """
681 ...
682 """
683
684 self.CaptureMouse()
685 x, y = self.ClientToScreen(event.GetPosition())
686 originx, originy = self.GetPosition()
687 dx = x - originx
688 dy = y - originy
689 self.delta = ((dx, dy))
690
691
692 def OnLeftUp(self, evt):
693 """
694 ...
695 """
696
697 if self.HasCapture():
698 self.ReleaseMouse()
699
700
701 def OnMouseMove(self, event):
702 """
703 ...
704 """
705
706 if event.Dragging() and event.LeftIsDown():
707 x, y = self.ClientToScreen(event.GetPosition())
708 fp = (x - self.delta[0], y - self.delta[1])
709 self.Move(fp)
710
711
712 def OnPaint(self, event):
713 """
714 ...
715 """
716
717 dc = wx.AutoBufferedPaintDCFactory(self)
718 dc.SetBackground(wx.Brush("BLACK"))
719 dc.Clear()
720
721 #------------
722
723 # These are strings.
724 py_version = sys.version.split()[0]
725
726 str1 = (('<font style="normal" family="default" color="yellow" size="10" weight="bold">'
727 'Programming : </font>'
728 '<font style="normal" family="default" color="white" size="10" weight="normal">'
729 'Python {}</font>').format(py_version)) # Python 3.7.2
730
731 str2 = (('<font style="normal" family="default" color="orange" size="10" weight="bold">'
732 'GUI toolkit : </font>'
733 '<font style="normal" family="default" color="white" size="10" weight="normal">'
734 'wxPython {}</font>').format(wx.VERSION_STRING)) # wxPython 4.0.4
735
736 str3 = (('<font style="normal" family="default" color="brown" size="10" weight="bold">'
737 'Library : </font>'
738 '<font style="normal" family="default" color="white" size="10" weight="normal">'
739 '{}</font>').format(wx.GetLibraryVersionInfo().VersionString)) # wxWidgets 3.0.5
740
741 str4 = (('<font style="normal" family="default" color="cyan" size="10" weight="bold">'
742 'Operating system : </font>'
743 '<font style="normal" family="default" color="white" size="10" weight="normal">'
744 '{}</font>').format(platform.system())) # Windows
745
746 str5 = (('<font style="normal" family="default" color="white" size="9" weight="normal">'
747 '{}</font>').format(self.app_name)) # Custom Gui
748
749 str6 = (('<font style="normal" family="default" color="black" size="8" weight="normal">'
750 'Right clic or Esc for Exit</font>'))
751
752 #------------
753
754 # Get the working size we can draw in.
755 bw, bh = self.GetSize()
756
757 # Use the GCDC to draw the text.
758 # Draw the about borders.
759 dc.SetPen(wx.Pen(wx.Colour(200, 200, 200), 20))
760 # Read config file.
761 dc.SetBrush(wx.Brush(self.config.Read("Color1")))
762 # or
763 # dc.SetBrush(wx.Brush(wx.TRANSPARENT_BRUSH))
764 dc.DrawRectangle(0, 0, bw, bh)
765
766 #------------
767
768 # Draw text.
769 # Need width to calculate x position of str1.
770 tw, th = fancytext.GetExtent(str1, dc)
771 # Centered text.
772 fancytext.RenderToDC(str1, dc, (bw-tw)/2, 30)
773
774 #------------
775
776 # Need width to calculate x position of str2.
777 tw, th = fancytext.GetExtent(str2, dc)
778 # Centered text.
779 fancytext.RenderToDC(str2, dc, (bw-tw)/2, 50)
780
781 #------------
782
783 # Need width to calculate x position of str3.
784 tw, th = fancytext.GetExtent(str3, dc)
785 # Centered text.
786 fancytext.RenderToDC(str3, dc, (bw-tw)/2, 70)
787
788 #------------
789
790 # Need width to calculate x position of str4.
791 tw, th = fancytext.GetExtent(str4, dc)
792 # Centered text.
793 fancytext.RenderToDC(str4, dc, (bw-tw)/2, 90)
794
795 #------------
796
797 # Need width to calculate x position of str5.
798 tw, th = fancytext.GetExtent(str5, dc)
799 # Centered text.
800 fancytext.RenderToDC(str5, dc, (bw-tw)/2, 130)
801
802 #------------
803
804 # Need width to calculate x position of str6.
805 tw, th = fancytext.GetExtent(str6, dc)
806 # Centered text.
807 fancytext.RenderToDC(str6, dc, (bw-tw)/2, 190)
808
809
810 def OnKeyUp(self, event):
811 """
812 ...
813 """
814
815 if event.GetKeyCode() == wx.WXK_ESCAPE:
816 self.OnCloseWindow(event)
817
818 event.Skip()
819
820
821 def OnCloseWindow(self, event):
822 """
823 ...
824 """
825
826 self.GetParent().Enable(True)
827 self.eventLoop.Exit()
828 self.Destroy()
829
830 #---------------------------------------------------------------------------
831
832 class MyPopupMenu(wx.Menu):
833 """
834 Thanks to Robin Dunn.
835 """
836 def __init__(self, parent):
837 wx.Menu.__init__(self)
838
839 #------------
840
841 # Attributes.
842 self.parent = parent
843
844 #------------
845
846 # Returns bitmaps folder.
847 self.bitmaps_dir = wx.GetApp().GetBitmapsDir()
848
849 #------------
850
851 # Simplified init method.
852 self.CreatePopupMenu()
853 self.BindEvents()
854
855 #-----------------------------------------------------------------------
856
857 def CreatePopupMenu(self, event=None):
858 """
859 This method is called by the base class when it needs to popup
860 the menu for the default EVT_RIGHT_DOWN event. Just create
861 the menu how you want it and return it from this function,
862 the base class takes care of the rest.
863 """
864
865 bmp = wx.Bitmap(os.path.join(self.bitmaps_dir,
866 "item_about.png"),
867 type=wx.BITMAP_TYPE_PNG)
868
869 item = wx.MenuItem(self, id=wx.ID_ABOUT, text=" About")
870 item.SetBitmap(bmp)
871 self.Append(item)
872 self.AppendSeparator()
873
874 #------------
875
876 bmp = wx.Bitmap(os.path.join(self.bitmaps_dir,
877 "item_exit.png"),
878 type=wx.BITMAP_TYPE_PNG)
879
880 if True or "__WXMSW__" in wx.PlatformInfo:
881 font = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT)
882 font.SetWeight(wx.BOLD)
883
884 item = wx.MenuItem(self, id=wx.ID_EXIT, text=" Exit")
885 item.SetBitmap(bmp)
886 item.SetFont(font)
887 self.Append(item)
888
889 return self
890
891
892 def BindEvents(self):
893 """
894 Bind some events to an events handler.
895 """
896
897 # Bind the menu events to an events handler.
898 self.Bind(wx.EVT_MENU, self.OnAbout, id=wx.ID_ABOUT)
899 self.Bind(wx.EVT_MENU, self.OnClose, id=wx.ID_EXIT)
900
901
902 def OnAbout(self, event):
903 """
904 ...
905 """
906
907 self.mainFrame = wx.GetApp().GetTopWindow()
908 self.mainFrame.OnAbout(self)
909
910 print("About icon was clicked.")
911
912
913 def OnClose(self, event):
914 """
915 ...
916 """
917
918 self.mainFrame = wx.GetApp().GetTopWindow()
919 self.mainFrame.OnCloseWindow(self)
920
921 print("Close icon was clicked.")
922
923 #---------------------------------------------------------------------------
924
925 class MyWindow(wx.Control):
926 """
927 Thanks to Cody Precord.
928 """
929 def __init__(self, parent, label,
930 foreground, background,
931 normal, pressed=None):
932 style = (wx.BORDER_NONE)
933 super(MyWindow, self).__init__(parent,
934 -1,
935 style=style)
936
937 #------------
938
939 # Attributes.
940 self.label = label
941 self.foreground = foreground
942 self.background = background
943
944 if wx.Platform == "__WXGTK__":
945 self.color = "#9e9d9d"
946
947 else:
948 self.color = "#b1b1b0"
949
950 self.normal = normal
951 self.pressed = pressed
952
953 self._clicked = False
954
955 #------------
956
957 self.region = wx.Region(normal, wx.Colour(0, 0, 0, 0))
958
959 #------------
960
961 # Simplified init method.
962 self.SetProperties(label, foreground, background)
963 self.BindEvents()
964
965 #-----------------------------------------------------------------------
966
967 def SetProperties(self, label, foreground, background):
968 """
969 ...
970 """
971
972 self.label = label
973 self.foreground = foreground
974 self.background = background
975 self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM)
976
977
978 def BindEvents(self):
979 """
980 Bind some events to an events handler.
981 """
982
983 self.Bind(wx.EVT_SET_FOCUS, self.OnSetFocus)
984 self.Bind(wx.EVT_KILL_FOCUS, self.OnKillFocus)
985 self.Bind(wx.EVT_PAINT, self.OnPaint)
986 self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
987 self.Bind(wx.EVT_LEFT_DCLICK, self.OnLeftDclick)
988 self.Bind(wx.EVT_LEFT_UP, self.OnLeftUp)
989 self.Bind(wx.EVT_MOTION, self.OnMotion)
990 self.Bind(wx.EVT_LEAVE_WINDOW, self.OnLeave)
991 self.Bind(wx.EVT_KEY_UP, self.OnKeyUp)
992
993
994 def SetLabel(self, label):
995 """
996 ...
997 """
998
999 self.label = label
1000 self.Refresh()
1001
1002
1003 def DoGetBestSize(self):
1004 """
1005 ...
1006 """
1007
1008 return self.normal.GetSize()
1009
1010
1011 def GetLabel(self):
1012 """
1013 ...
1014 """
1015
1016 return self.label
1017
1018
1019 def GetLabelColor(self):
1020 """
1021 ...
1022 """
1023
1024 return self.foreground
1025
1026
1027 def Enable(self, *args, **kwargs):
1028 """
1029 ...
1030 """
1031
1032 super(MyWindow, self).Enable(*args, **kwargs)
1033 self.Refresh()
1034
1035
1036 def Disable(self, *args, **kwargs):
1037 """
1038 ...
1039 """
1040
1041 super(MyWindow, self).Disable(*args, **kwargs)
1042 self.Refresh()
1043
1044
1045 def PostEvent(self):
1046 """
1047 ...
1048 """
1049
1050 event = wx.CommandEvent()
1051 event.SetEventObject(self)
1052 event.SetEventType(wx.EVT_BUTTON.typeId)
1053 wx.PostEvent(self, event)
1054
1055
1056 def OnSize(self, event):
1057 """
1058 ...
1059 """
1060
1061 event.Skip()
1062 self.Refresh()
1063
1064
1065 def GetBackground(self):
1066 """
1067 ...
1068 """
1069
1070 return self.background
1071
1072
1073 def OnPaint(self, event):
1074 """
1075 ...
1076 """
1077
1078 dc = wx.BufferedPaintDC(self)
1079 dc.Clear()
1080 gcdc = wx.GCDC(dc)
1081
1082 # Set the background color.
1083 if wx.Platform == "__WXGTK__":
1084 gcdc.SetBackground(wx.Brush("grey55"))
1085 else:
1086 gcdc.SetBackground(wx.Brush(self.background))
1087
1088 gcdc.Clear()
1089
1090 # Get the working rectangle we can draw in.
1091 rect = self.GetClientRect()
1092
1093 # Font size and style.
1094 fontSize = self.GetFont().GetPointSize()
1095
1096 if wx.Platform == "__WXGTK__":
1097 boldFont = wx.Font(fontSize-1, wx.DEFAULT,
1098 wx.NORMAL, wx.NORMAL, False, "")
1099 else:
1100 boldFont = wx.Font(fontSize+2, wx.DEFAULT,
1101 wx.NORMAL, wx.BOLD, False, "")
1102
1103 if wx.Platform == "__WXMSW__":
1104 pen = wx.Pen(self.color, 1, wx.SOLID)
1105
1106 else:
1107 pen = wx.Pen(self.color, 1, wx.SOLID)
1108
1109 gcdc.SetPen(pen)
1110
1111 x, y = self.GetSize()
1112 # x, y , width, height, radius
1113 gcdc.DrawRoundedRectangle (0, 0, 71, 25, 3)
1114
1115 bitmap = self.normal
1116
1117 w, h = bitmap.GetWidth(), bitmap.GetHeight()
1118
1119 if self.clicked:
1120 bitmap = self.pressed or bitmap
1121 if not self.IsEnabled():
1122 bitmap = self.normal or bitmap
1123
1124 # Draw a bitmap with an alpha channel.
1125 # image, x, y, transparency.
1126 dc.DrawBitmap(bitmap, 0, 0, True)
1127
1128 if wx.Platform == "__WXGTK__":
1129 # Add the Caption.
1130 # White text - Shadow.
1131 rect = wx.Rect(rect.x, rect.y+3,
1132 rect.width, 22)
1133
1134 else:
1135 rect = wx.Rect(rect.x, rect.y+3,
1136 rect.width, 20)
1137
1138 dc.SetFont(boldFont)
1139 dc.SetTextForeground(wx.WHITE)
1140 dc.DrawLabel(self.label, rect, wx.ALIGN_CENTER)
1141
1142 if wx.Platform == "__WXGTK__":
1143 # Add the Caption.
1144 # Black text.
1145 rect = wx.Rect(rect.x, rect.y,
1146 rect.width, 21)
1147
1148 else:
1149 rect = wx.Rect(rect.x, rect.y,
1150 rect.width, 19)
1151
1152 gcdc.SetFont(boldFont)
1153 # Get the text color.
1154 dc.SetTextForeground(self.foreground)
1155 dc.DrawLabel(self.label, rect, wx.ALIGN_CENTER)
1156
1157
1158 def SetClicked(self, clicked):
1159 """
1160 ...
1161 """
1162
1163 if clicked != self._clicked:
1164 self._clicked = clicked
1165 self.Refresh()
1166
1167
1168 def GetClicked(self):
1169 """
1170 ...
1171 """
1172
1173 return self._clicked
1174
1175
1176 clicked = property(GetClicked, SetClicked)
1177 def OnLeftDown(self, event):
1178 """
1179 ...
1180 """
1181
1182 x, y = event.GetPosition()
1183 if self.region.Contains(x, y):
1184 self.clicked = True
1185 self.SetFocus()
1186
1187
1188 def OnLeftDclick(self, event):
1189 """
1190 ...
1191 """
1192
1193 self.OnLeftDown(event)
1194
1195
1196 def OnLeftUp(self, event):
1197 """
1198 ...
1199 """
1200
1201 if self.clicked:
1202 x, y = event.GetPosition()
1203 if self.region.Contains(x, y):
1204 self.PostEvent()
1205 self.clicked = False
1206
1207
1208 def OnMotion(self, event):
1209 """
1210 ...
1211 """
1212
1213 if self.clicked:
1214 x, y = event.GetPosition()
1215 if not self.region.Contains(x, y):
1216 self.clicked = False
1217
1218
1219 def OnLeave(self, event):
1220 """
1221 ...
1222 """
1223
1224 self.clicked = False
1225
1226
1227 def OnSetFocus(self, event):
1228 """
1229 ...
1230 """
1231
1232 self.color = "white"
1233 self.Refresh()
1234
1235
1236 def OnKillFocus(self, event):
1237 """
1238 ...
1239 """
1240
1241 if wx.Platform == "__WXGTK__":
1242 self.color = "#9e9d9d"
1243
1244 else:
1245 self.color = "#b1b1b0"
1246
1247 self.Refresh()
1248
1249
1250 def OnKeyUp(self, event):
1251 """
1252 ...
1253 """
1254
1255 if event.GetKeyCode() == wx.WXK_SPACE:
1256 self.PostEvent()
1257 return
1258
1259 elif event.GetKeyCode() == wx.WXK_ESCAPE:
1260 self.GetTopLevelParent().OnCloseWindow(True)
1261
1262 event.Skip()
1263
1264 #---------------------------------------------------------------------------
1265
1266 class MyButtonBox(wx.Panel):
1267 """
1268 Thanks to Cody Precord.
1269 """
1270 def __init__(self, parent, caption):
1271 style = (wx.NO_BORDER | wx.TAB_TRAVERSAL)
1272 super(MyButtonBox, self).__init__(parent,
1273 style=style)
1274
1275 #------------
1276
1277 # Attributes.
1278 self._caption = caption
1279
1280 #------------
1281
1282 # Simplified init method.
1283 self.DoLayout()
1284
1285 #-----------------------------------------------------------------------
1286
1287 def DoLayout(self):
1288 """
1289 ...
1290 """
1291
1292 self._csizer = wx.BoxSizer(wx.VERTICAL)
1293 msizer = wx.BoxSizer(wx.HORIZONTAL)
1294 msizer.Add(self._csizer, 0 )
1295 self.SetSizer(msizer)
1296
1297
1298 def DoGetBestSize(self):
1299 """
1300 ...
1301 """
1302
1303 size = super(MyButtonBox, self).DoGetBestSize()
1304
1305 # Compensate for wide caption labels.
1306 tw = self.GetTextExtent(self._caption)[0]
1307 size.SetWidth(max(size.width, size.height))
1308 return size
1309
1310
1311 def AddItem(self, item):
1312 """
1313 Add a window or sizer item to the MyButtonBox.
1314 """
1315
1316 self._csizer.Add(item, 0 )
1317
1318 #---------------------------------------------------------------------------
1319
1320 class MyTitleBarPnl(wx.Panel):
1321 """
1322 Thanks to Cody Precord.
1323 """
1324 def __init__(self, parent, id, size):
1325 style = (wx.NO_BORDER)
1326 super(MyTitleBarPnl, self).__init__(parent,
1327 id,
1328 size=size,
1329 style=style)
1330
1331 #------------
1332
1333 # Attributes.
1334 self.parent = parent
1335
1336 #------------
1337
1338 # Return config file.
1339 self.config = wx.GetApp().GetConfig()
1340 # Return application name.
1341 self.app_name = wx.GetApp().GetAppName()
1342 # Return bitmaps folder.
1343 self.bitmaps_dir = wx.GetApp().GetBitmapsDir()
1344
1345 #------------
1346
1347 # Simplified init method.
1348 self.SetProperties()
1349 self.CreateCtrls()
1350 self.BindEvents()
1351
1352 #-----------------------------------------------------------------------
1353
1354 def SetProperties(self):
1355 """
1356 ...
1357 """
1358
1359 # Read config file.
1360 self.SetBackgroundColour(wx.Colour(self.config.Read("Color1")))
1361
1362
1363 def CreateCtrls(self):
1364 """
1365 ...
1366 """
1367
1368 w, h = self.GetClientSize()
1369 print("MyTitleBarPnl :", self.GetClientSize())
1370
1371 #------------
1372 #------------
1373
1374 # Add titleBar.
1375 self.titleBar = MyTitleBar(self,
1376 label=self.app_name,
1377 size=10)
1378 self.titleBar.SetPosition((0, 0))
1379 self.titleBar.SetSize((w, 31))
1380 self.titleBar.SetLabelColour("white")
1381 self.titleBar.SetToolTip("This is a customized title bar.")
1382
1383 #------------
1384 #------------
1385
1386 # Gloss Buttons box (titleBar).
1387 self.box3 = MyButtonBox(self.titleBar, "") # Button Exit.
1388 self.box4 = MyButtonBox(self.titleBar, "") # Button Maximize.
1389 self.box5 = MyButtonBox(self.titleBar, "") # Button Reduce.
1390 self.box6 = MyButtonBox(self.titleBar, "") # Button Roll.
1391
1392 # Gloss Buttons bitmap.
1393 bmpa = wx.Bitmap(os.path.join(self.bitmaps_dir,
1394 "btn_gloss_exit_normal_5.png"),
1395 type=wx.BITMAP_TYPE_PNG)
1396
1397 bmpb = wx.Bitmap(os.path.join(self.bitmaps_dir,
1398 "btn_gloss_exit_selected_5.png"),
1399 type=wx.BITMAP_TYPE_PNG)
1400
1401 bmpc = wx.Bitmap(os.path.join(self.bitmaps_dir,
1402 "btn_gloss_maximize_normal_5.png"),
1403 type=wx.BITMAP_TYPE_PNG)
1404
1405 bmpd = wx.Bitmap(os.path.join(self.bitmaps_dir,
1406 "btn_gloss_maximize_selected_5.png"),
1407 type=wx.BITMAP_TYPE_PNG)
1408
1409 bmpe = wx.Bitmap(os.path.join(self.bitmaps_dir,
1410 "btn_gloss_reduce_normal_5.png"),
1411 type=wx.BITMAP_TYPE_PNG)
1412
1413 bmpf = wx.Bitmap(os.path.join(self.bitmaps_dir,
1414 "btn_gloss_reduce_selected_5.png"),
1415 type=wx.BITMAP_TYPE_PNG)
1416
1417 bmpg = wx.Bitmap(os.path.join(self.bitmaps_dir,
1418 "btn_gloss_roll_normal_5.png"),
1419 type=wx.BITMAP_TYPE_PNG)
1420
1421 bmph = wx.Bitmap(os.path.join(self.bitmaps_dir,
1422 "btn_gloss_roll_selected_5.png"),
1423 type=wx.BITMAP_TYPE_PNG)
1424
1425 self.btn3 = MyWindow(self.box3, "", "black", "#9e9d9d", bmpa, bmpb)
1426 self.btn3.SetToolTip("This is a customized gloss button.")
1427
1428 self.btn4 = MyWindow(self.box4, "", "black", "#9e9d9d", bmpc, bmpd)
1429 self.btn4.SetToolTip("This is a customized gloss button.")
1430
1431 self.btn5 = MyWindow(self.box5, "", "black", "#9e9d9d", bmpe, bmpf)
1432 self.btn5.SetToolTip("This is a customized gloss button.")
1433
1434 self.btn6 = MyWindow(self.box6, "", "black", "#9e9d9d", bmpg, bmph)
1435 self.btn6.SetToolTip("This is a customized gloss button.")
1436
1437 self.box3.AddItem(self.btn3)
1438 self.box4.AddItem(self.btn4)
1439 self.box5.AddItem(self.btn5)
1440 self.box6.AddItem(self.btn6)
1441
1442 #------------
1443 #------------
1444
1445 w, h = self.GetSize()
1446
1447 #------------
1448
1449 self.box3.SetSize((25, 19))
1450 x1, y1 = self.box3.GetSize()
1451 self.box3.SetPosition((x1-18, h-6-y1))
1452
1453 #------------
1454
1455 self.box4.SetSize((25, 19))
1456 self.box4.SetPosition(((x1*2)-18, h-6-y1))
1457
1458 #------------
1459
1460 self.box5.SetSize((25, 19))
1461 self.box5.SetPosition(((x1*3)-18, h-6-y1))
1462
1463 #------------
1464
1465 self.box6.SetSize((25, 19))
1466 self.box6.SetPosition(((x1*4)-18, h-6-y1))
1467
1468
1469 def BindEvents(self):
1470 """
1471 Bind some events to an events handler.
1472 """
1473
1474 self.btn3.Bind(wx.EVT_BUTTON, self.OnBtnClose)
1475 self.btn4.Bind(wx.EVT_BUTTON, self.OnFullScreen)
1476 self.btn5.Bind(wx.EVT_BUTTON, self.OnIconfiy)
1477 self.btn6.Bind(wx.EVT_BUTTON, self.OnRoll)
1478
1479 self.Bind(wx.EVT_SIZE, self.OnResize)
1480
1481
1482 def OnResize(self, event):
1483 """
1484 ...
1485 """
1486
1487 w, h = self.GetClientSize()
1488 print("MyTitleBarPnl :", self.GetClientSize())
1489
1490 #------------
1491 #------------
1492
1493 self.titleBar.SetSize((w, 24))
1494
1495 #------------
1496
1497 x1, y1 = self.box3.GetSize()
1498
1499 self.box3.SetPosition((x1-18, h-6-y1))
1500 self.box4.SetPosition(((x1*2)-18, h-6-y1))
1501 self.box5.SetPosition(((x1*3)-18, h-6-y1))
1502 self.box6.SetPosition(((x1*4)-18, h-6-y1))
1503
1504 #------------
1505
1506 self.Refresh()
1507
1508 print("On resize was clicked.")
1509
1510
1511 def OnRoll(self, event):
1512 """
1513 ...
1514 """
1515
1516 self.GetParent().OnRoll(True)
1517
1518 print("Roll/unRoll button was clicked.")
1519
1520
1521 def OnIconfiy(self, event):
1522 """
1523 ...
1524 """
1525
1526 self.GetParent().OnIconfiy(self)
1527
1528 print("Iconfiy button was clicked.")
1529
1530
1531 def OnFullScreen(self, event):
1532 """
1533 ...
1534 """
1535
1536 self.GetParent().OnFullScreen(self)
1537
1538 print("FullScreen button was clicked.")
1539
1540
1541 def OnBtnClose(self, event):
1542 """
1543 ...
1544 """
1545
1546 self.GetParent().OnCloseWindow(self)
1547
1548 print("Close button was clicked.")
1549
1550 #---------------------------------------------------------------------------
1551
1552 class MyMainPnl(wx.Panel):
1553 """
1554 ...
1555 """
1556 def __init__(self, parent, id, size):
1557 style = (wx.NO_BORDER | wx.TAB_TRAVERSAL)
1558 super(MyMainPnl, self).__init__(parent,
1559 id=int(ID_MAIN_PNL),
1560 size=size,
1561 style=style)
1562
1563 #------------
1564
1565 # Attributes.
1566 self.parent = parent
1567
1568 #------------
1569
1570 # Return config file.
1571 self.config = wx.GetApp().GetConfig()
1572 # Return bitmaps folder.
1573 self.bitmaps_dir = wx.GetApp().GetBitmapsDir()
1574
1575 #------------
1576
1577 # Colourdb.
1578 wx.lib.colourdb.updateColourDB()
1579
1580 # Create a colour list from the colourdb database.
1581 self.colour_list = wx.lib.colourdb.getColourList()
1582
1583 #------------
1584
1585 # Simplified init method.
1586 self.SetProperties()
1587 self.CreateCtrls()
1588 self.DoLayout()
1589 self.BindEvents()
1590
1591 #-----------------------------------------------------------------------
1592
1593 def SetProperties(self):
1594 """
1595 ...
1596 """
1597
1598 if wx.Platform == "__WXMSW__":
1599 self.SetDoubleBuffered(True)
1600
1601 # Read config file.
1602 self.SetBackgroundColour(self.config.Read("Color1"))
1603
1604
1605 def CreateCtrls(self):
1606 """
1607 ...
1608 """
1609
1610 w, h = self.GetClientSize()
1611 print("MyMainPnl :", self.GetClientSize())
1612
1613 #------------
1614
1615 font = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT)
1616 font.SetWeight(wx.BOLD)
1617
1618 #------------
1619 #------------
1620
1621 # Create data list control.
1622 self.list = MyListCtrlPnl(self)
1623
1624 #------------
1625 #------------
1626
1627 self.txt = wx.StaticText(self,
1628 -1,
1629 label="Hello World !")
1630 self.txt.SetForegroundColour("white")
1631 self.txt.SetFont(font)
1632
1633 #------------
1634 #------------
1635
1636 # Thanks to MCOW.
1637 # Buttons.
1638 self.btn1 = SBB.ShapedBitmapButton(self, ID_BTN_FULLSCREEN,
1639 bitmap=wx.Bitmap(os.path.join(self.bitmaps_dir,
1640 "adbRnd.png"),
1641 type=wx.BITMAP_TYPE_PNG),
1642
1643 pressedBmp=wx.Bitmap(os.path.join(self.bitmaps_dir,
1644 "adbRnd-pressed.png"),
1645 type=wx.BITMAP_TYPE_PNG),
1646
1647 hoverBmp=wx.Bitmap(os.path.join(self.bitmaps_dir,
1648 "adbRnd-hover.png"),
1649 type=wx.BITMAP_TYPE_PNG),
1650
1651 disabledBmp=wx.Bitmap(os.path.join(self.bitmaps_dir,
1652 "adbRnd-disabled.png"),
1653 type=wx.BITMAP_TYPE_PNG),
1654
1655 label="Fullscreen",
1656 labelForeColour=wx.WHITE,
1657 labelPosition=(35, 8),
1658 labelFont=wx.Font(9,
1659 wx.FONTFAMILY_DEFAULT,
1660 wx.FONTSTYLE_NORMAL,
1661 wx.FONTWEIGHT_BOLD),
1662 style=wx.BORDER_NONE)
1663
1664 self.btn1.SetFocus() # Necessary for Linux.
1665
1666 #------------
1667
1668 self.btn2 = SBB.ShapedBitmapButton(self, ID_BTN_ABOUT,
1669 bitmap=wx.Bitmap(os.path.join(self.bitmaps_dir,
1670 "adbRnd.png"),
1671 type=wx.BITMAP_TYPE_PNG),
1672
1673 pressedBmp=wx.Bitmap(os.path.join(self.bitmaps_dir,
1674 "adbRnd-pressed.png"),
1675 type=wx.BITMAP_TYPE_PNG),
1676
1677 hoverBmp=wx.Bitmap(os.path.join(self.bitmaps_dir,
1678 "adbRnd-hover.png"),
1679 type=wx.BITMAP_TYPE_PNG),
1680
1681 disabledBmp=wx.Bitmap(os.path.join(self.bitmaps_dir,
1682 "adbRnd-disabled.png"),
1683 type=wx.BITMAP_TYPE_PNG),
1684
1685 label="About",
1686 labelForeColour=wx.WHITE,
1687 labelPosition=(50, 8),
1688 labelFont=wx.Font(9,
1689 wx.FONTFAMILY_DEFAULT,
1690 wx.FONTSTYLE_NORMAL,
1691 wx.FONTWEIGHT_BOLD),
1692 style=wx.BORDER_NONE)
1693
1694 #------------
1695
1696 self.btn3 = SBB.ShapedBitmapButton(self, ID_BTN_QUIT,
1697 bitmap=wx.Bitmap(os.path.join(self.bitmaps_dir,
1698 "adbRnd.png"),
1699 type=wx.BITMAP_TYPE_PNG),
1700
1701 pressedBmp=wx.Bitmap(os.path.join(self.bitmaps_dir,
1702 "adbRnd-pressed.png"),
1703 type=wx.BITMAP_TYPE_PNG),
1704
1705 hoverBmp=wx.Bitmap(os.path.join(self.bitmaps_dir,
1706 "adbRnd-hover.png"),
1707 type=wx.BITMAP_TYPE_PNG),
1708
1709 disabledBmp=wx.Bitmap(os.path.join(self.bitmaps_dir,
1710 "adbRnd-disabled.png"),
1711 type=wx.BITMAP_TYPE_PNG),
1712
1713 label="Quit",
1714 labelForeColour=wx.WHITE,
1715 labelPosition=(50, 8),
1716 labelFont=wx.Font(9,
1717 wx.FONTFAMILY_DEFAULT,
1718 wx.FONTSTYLE_NORMAL,
1719 wx.FONTWEIGHT_BOLD),
1720 style=wx.BORDER_NONE)
1721
1722 #------------
1723 #------------
1724
1725 self.line = wx.StaticLine(self, -1,
1726 pos=(0, 70),
1727 size=(-1, -1),
1728 style=wx.LI_HORIZONTAL)
1729
1730 #------------
1731 #------------
1732
1733 # Create a choice widget.
1734 self.choice = wx.Choice(self,
1735 -1,
1736 pos=(-1, -1),
1737 size=(131, -1),
1738 choices=self.colour_list,
1739 style=wx.CB_SORT)
1740
1741 # Select item 141 in choice list to show.
1742 # self.choice.SetSelection(141) # CORNSILK4
1743 # Read config file.
1744 self.choice.SetSelection(self.config.ReadInt("Item1"))
1745
1746 item = self.choice.GetCurrentSelection()
1747 bgcolour = self.choice.GetStringSelection()
1748 print("Item : %s , %s" % (item, bgcolour))
1749
1750
1751 def DoLayout(self):
1752 """
1753 ...
1754 """
1755
1756 txtSizer = wx.BoxSizer(wx.VERTICAL)
1757 txtSizer.Add(self.txt, 0, wx.LEFT, 13)
1758
1759 sizerList = wx.BoxSizer(wx.VERTICAL)
1760 sizerList.Add(self.list, 1,
1761 wx.LEFT|wx.TOP|wx.BOTTOM|
1762 wx.EXPAND|wx.ALIGN_TOP, 10)
1763
1764 sizer = wx.BoxSizer(wx.VERTICAL)
1765 sizer.Add(txtSizer, 0, wx.BOTTOM, 0)
1766 sizer.Add(self.btn1, 1, wx.ALL, 10)
1767 sizer.Add(self.btn2, 1, wx.ALL, 10)
1768 sizer.Add(self.line, 0, wx.EXPAND|wx.ALL, 10)
1769 sizer.Add(self.choice, 0, wx.ALL, 10)
1770 sizer.Add(self.btn3, 1, wx.ALL, 10)
1771
1772 mainSizer = wx.BoxSizer(wx.HORIZONTAL)
1773 mainSizer.Add(sizerList, 1,
1774 wx.LEFT|wx.TOP|wx.BOTTOM|wx.EXPAND, 5)
1775 mainSizer.Add(sizer, 0, wx.ALIGN_BOTTOM|wx.ALL, 5)
1776
1777 self.SetSizer(mainSizer)
1778 self.Layout()
1779
1780
1781 def BindEvents(self):
1782 """
1783 Bind some events to an events handler.
1784 """
1785
1786 self.choice.Bind(wx.EVT_CHOICE, self.OnChoice)
1787
1788 self.Bind(wx.EVT_ENTER_WINDOW, self.OnInfo, id=ID_MAIN_PNL)
1789 self.btn1.Bind(wx.EVT_ENTER_WINDOW, self.OnInfo, id=ID_BTN_FULLSCREEN)
1790 self.btn2.Bind(wx.EVT_ENTER_WINDOW, self.OnInfo, id=ID_BTN_ABOUT)
1791 self.btn3.Bind(wx.EVT_ENTER_WINDOW, self.OnInfo, id=ID_BTN_QUIT)
1792
1793 self.btn1.Bind(wx.EVT_BUTTON, self.OnFullScreen)
1794 self.btn2.Bind(wx.EVT_BUTTON, self.OnAbout)
1795 self.btn3.Bind(wx.EVT_BUTTON, self.OnBtnClose)
1796
1797
1798 def OnInfo(self, event):
1799 """
1800 ...
1801 """
1802
1803 event_id = event.GetId()
1804
1805 if event_id == ID_MAIN_PNL:
1806 self.GetParent().SetStatusText(text="Hello world !")
1807
1808 elif event_id == ID_BTN_FULLSCREEN:
1809 self.GetParent().SetStatusText(text="Fullscreen")
1810
1811 elif event_id == ID_BTN_ABOUT:
1812 self.GetParent().SetStatusText(text="About")
1813
1814 elif event_id == ID_BTN_QUIT:
1815 self.GetParent().SetStatusText(text="Quit the program")
1816
1817 else:
1818 # Tell the event system to continue
1819 # looking for an event handler, so the
1820 # default handler will get called.
1821 event.Skip()
1822
1823
1824 def OnChoice(self, event):
1825 """
1826 ...
1827 """
1828
1829 item = event.GetSelection()
1830 bgcolour = self.choice.GetStringSelection()
1831 print("New item : %s , %s" % (item, bgcolour))
1832
1833 # Change colour of the panel to the selected colour...
1834 self.SetBackgroundColour(bgcolour)
1835
1836 # Write config file.
1837 self.config.WriteInt("Item1", self.choice.GetCurrentSelection())
1838 self.config.Write("Color1", self.choice.GetStringSelection())
1839 self.config.Flush()
1840
1841 self.Refresh()
1842
1843 print("OnChoice button was clicked.")
1844
1845
1846 def OnAbout(self, event):
1847 """
1848 ...
1849 """
1850
1851 self.GetParent().OnAbout(self)
1852
1853 print("FullScreen button was clicked.")
1854
1855
1856 def OnFullScreen(self, event):
1857 """
1858 ...
1859 """
1860
1861 self.GetParent().OnFullScreen(self)
1862
1863 print("FullScreen button was clicked.")
1864
1865
1866 def OnBtnClose(self, event):
1867 """
1868 ...
1869 """
1870
1871 self.GetParent().OnCloseWindow(self)
1872
1873 print("Close button was clicked.")
1874
1875 #---------------------------------------------------------------------------
1876
1877 class MyFrame(wx.Frame):
1878 """
1879 Thanks to Robin Dunn.
1880 """
1881 def __init__(self):
1882 style = (wx.CLIP_CHILDREN | wx.CLOSE_BOX |
1883 wx.MINIMIZE_BOX | wx.SYSTEM_MENU |
1884 wx.BORDER_SIMPLE | wx.NO_FULL_REPAINT_ON_RESIZE|
1885 wx.STAY_ON_TOP)
1886 super(MyFrame, self).__init__(None,
1887 -1,
1888 title="",
1889 style=style)
1890
1891 #------------
1892
1893 wx.SystemOptions.SetOption("msw.remap", "0")
1894
1895 #------------
1896
1897 # Attributes.
1898 self.SetTransparent(0)
1899 self.opacity_in = 0
1900 self.opacity_out = 255
1901 self.deltaN = -70
1902 self.delta = wx.Point(0,0)
1903
1904 #------------
1905
1906 # Return config file.
1907 self.config = wx.GetApp().GetConfig()
1908 # Return application name.
1909 self.app_name = wx.GetApp().GetAppName()
1910 # Return bitmaps folder.
1911 self.bitmaps_dir = wx.GetApp().GetBitmapsDir()
1912 # Return icons folder.
1913 self.icons_dir = wx.GetApp().GetIconsDir()
1914
1915 #------------
1916
1917 # Colourdb.
1918 wx.lib.colourdb.updateColourDB()
1919
1920 # Create a colour list from the colourdb database.
1921 self.colour_list = wx.lib.colourdb.getColourList()
1922
1923 #------------
1924
1925 # Simplified init method.
1926 self.SetProperties()
1927 self.OnTimerIn(self)
1928 self.CreateMenu()
1929 self.CreateStatusBar()
1930 self.CreateCtrls()
1931 self.BindEvents()
1932 self.DoLayout()
1933
1934 #------------
1935
1936 # Thanks to Ray Pasco.
1937 # Initialize to the current state.
1938 self.unrolledFrameClientSize_size = self.GetClientSize()
1939 self.isRolled = False
1940
1941 #------------
1942
1943 self.CenterOnScreen(wx.BOTH)
1944
1945 #------------
1946
1947 self.Show(True)
1948
1949 #-----------------------------------------------------------------------
1950
1951 def SetProperties(self):
1952 """
1953 ...
1954 """
1955
1956 self.SetTitle(self.app_name)
1957 self.SetClientSize((600, 380))
1958 self.SetMinSize((340, 83))
1959
1960 # Read config file.
1961 self.SetBackgroundColour(self.config.Read("Color1"))
1962
1963 #------------
1964
1965 frameIcon = wx.Icon(os.path.join(self.icons_dir,
1966 "icon_wxWidgets.ico"),
1967 type=wx.BITMAP_TYPE_ICO)
1968 self.SetIcon(frameIcon)
1969
1970
1971 def OnTimerIn(self, evt):
1972 """
1973 Thanks to Pascal Faut.
1974 """
1975
1976 self.timer1 = wx.Timer(self, -1)
1977 self.timer1.Start(1)
1978 self.Bind(wx.EVT_TIMER, self.AlphaCycle1, self.timer1)
1979
1980 print("Fade-in was launched.")
1981
1982
1983 def OnTimerOut(self, evt):
1984 """
1985 Thanks to Pascal Faut.
1986 """
1987
1988 self.timer2 = wx.Timer(self, -1)
1989 self.timer2.Start(1)
1990 self.Bind(wx.EVT_TIMER, self.AlphaCycle2, self.timer2)
1991
1992 print("Fade-out was launched.")
1993
1994
1995 def AlphaCycle1(self, *args):
1996 """
1997 Thanks to Pascal Faut.
1998 """
1999
2000 self.opacity_in += self.deltaN
2001 if self.opacity_in <= 0:
2002 self.deltaN = -self.deltaN
2003 self.opacity_in = 0
2004
2005 if self.opacity_in >= 255:
2006 self.deltaN = -self.deltaN
2007 self.opacity_in = 255
2008
2009 self.timer1.Stop()
2010
2011 self.SetTransparent(self.opacity_in)
2012
2013 print("Fade in = {}/255".format(self.opacity_in))
2014
2015
2016 def AlphaCycle2(self, *args):
2017 """
2018 Thanks to Pascal Faut.
2019 """
2020
2021 self.opacity_out += self.deltaN
2022 if self.opacity_out >= 255:
2023 self.deltaN = -self.deltaN
2024 self.opacity_out = 255
2025
2026 if self.opacity_out <= 0:
2027 self.deltaN = -self.deltaN
2028 self.opacity_out = 0
2029
2030 self.timer2.Stop()
2031 wx.CallAfter(self.Destroy)
2032 wx.Exit()
2033
2034 self.SetTransparent(self.opacity_out)
2035
2036 print("Fade out = {}/255".format(self.opacity_out))
2037
2038
2039 def CreateMenu(self):
2040 """
2041 ...
2042 """
2043
2044 # FlatMenuBar.
2045 self.menuBar = FM.FlatMenuBar(self,
2046 wx.ID_ANY,
2047 32, 4,
2048 options=FM.FM_OPT_IS_LCD)
2049
2050 # FM.StyleDefault or FM.Style2007
2051 # FM.StyleXP or FM.StyleVista
2052 self.newMyTheme = self.menuBar.GetRendererManager().AddRenderer(MyMenuRenderer())
2053 self.menuBar.GetRendererManager().SetTheme(self.newMyTheme)
2054
2055 #------------
2056
2057 # Set an icon to the exit/help menu item.
2058 exitImg = wx.Bitmap(os.path.join(self.bitmaps_dir,
2059 "item_exit.png"),
2060 type=wx.BITMAP_TYPE_PNG)
2061
2062 helpImg = wx.Bitmap(os.path.join(self.bitmaps_dir,
2063 "item_about.png"),
2064 type=wx.BITMAP_TYPE_PNG)
2065
2066 #------------
2067 #------------
2068
2069 # File Menu.
2070 self.file_menu = FM.FlatMenu()
2071
2072 # Create the menu items.
2073 item = FM.FlatMenuItem(self.file_menu,
2074 ID_FULLSCREEN,
2075 "&Fullscreen\tCtrl+F",
2076 "Fullscreen",
2077 wx.ITEM_NORMAL,
2078 None)
2079 item.SetTextColour("black")
2080 self.file_menu.AppendItem(item)
2081 self.file_menu.AppendSeparator()
2082
2083
2084 item = FM.FlatMenuItem(self.file_menu,
2085 wx.ID_EXIT,
2086 "&Quit\tCtrl+Q",
2087 "Quit the program",
2088 wx.ITEM_NORMAL,
2089 None,
2090 exitImg)
2091 # Demonstrate how to set custom font
2092 # and text colour to a FlatMenuItem.
2093 item.SetFont(wx.Font(-1, wx.FONTFAMILY_DEFAULT,
2094 wx.FONTSTYLE_NORMAL,
2095 wx.FONTWEIGHT_BOLD,
2096 False, ""))
2097 item.SetTextColour("#d34725")
2098
2099 self.file_menu.AppendItem(item)
2100
2101 #------------
2102
2103 # Add Create background bitmap.
2104 self.file_menu.SetBackgroundBitmap(CreateBackgroundBitmap())
2105
2106 #------------
2107 #------------
2108
2109 # Help Menu.
2110 self.help_menu = FM.FlatMenu()
2111
2112 # Create the menu items.
2113 item = FM.FlatMenuItem(self.help_menu,
2114 wx.ID_ABOUT,
2115 "&About\tCtrl+A",
2116 "About",
2117 wx.ITEM_NORMAL,
2118 None,
2119 helpImg)
2120 item.SetTextColour("black")
2121 self.help_menu.AppendItem(item)
2122 self.help_menu.AppendSeparator()
2123
2124 item = FM.FlatMenuItem(self.help_menu,
2125 ID_HELLO,
2126 "Hello !",
2127 "Hello !",
2128 wx.ITEM_NORMAL,
2129 None)
2130 item.SetTextColour("black")
2131 self.help_menu.AppendItem(item)
2132
2133 #------------
2134
2135 # Add Create background bitmap.
2136 self.help_menu.SetBackgroundBitmap(CreateBackgroundBitmap())
2137
2138 #------------
2139 #------------
2140
2141 # Menu background color.
2142 # Read config file.
2143 self.menuBar.SetBackgroundColour(wx.Colour(self.config.Read("Color1")))
2144
2145 # Add menu to the menu bar.
2146 self.menuBar.Append(self.file_menu, "&File")
2147 self.menuBar.Append(self.help_menu, "&Help")
2148
2149
2150 def CreateStatusBar(self):
2151 """
2152 ...
2153 """
2154
2155 self.sb = MyStatusBar(self, -1)
2156 # Read config file.
2157 self.sb.SetBackgroundColour(wx.Colour(self.config.Read("Color1")))
2158 self.sb.SetStatusText("Hello world !")
2159
2160 self.SetStatusBar(self.sb)
2161
2162
2163 def CreateCtrls(self):
2164 """
2165 ...
2166 """
2167
2168 w, h = self.GetClientSize()
2169 print("MyFrame :", self.GetClientSize())
2170
2171 #------------
2172 #------------
2173
2174 self.titleBarPnl = MyTitleBarPnl(self, -1, (w, 31))
2175 self.titleBarPnl.SetPosition((0, 0))
2176 print("self.titleBarPnl :", self.titleBarPnl.GetSize())
2177
2178 #------------
2179
2180 self.mainPnl = MyMainPnl(self, -1, (w, h-31))
2181 self.mainPnl.SetPosition((0, 31))
2182 print("self.mainPnl :", self.mainPnl.GetSize())
2183
2184
2185 def BindEvents(self):
2186 """
2187 ...
2188 """
2189
2190 self.titleBarPnl.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
2191
2192 self.Bind(wx.EVT_SIZE, self.OnResize)
2193 self.Bind(wx.EVT_LEFT_UP, self.OnLeftUp)
2194 self.Bind(wx.EVT_MOTION, self.OnMouseMove)
2195 self.Bind(wx.EVT_CHAR_HOOK, self.OnKeyUp)
2196 self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
2197
2198 self.Bind(wx.EVT_MENU, self.OnFullScreen, id=ID_FULLSCREEN)
2199 self.Bind(wx.EVT_MENU, self.OnAbout, id=wx.ID_ABOUT)
2200 self.Bind(wx.EVT_MENU, self.OnCloseWindow, id=wx.ID_EXIT)
2201
2202
2203 def DoLayout(self):
2204 """
2205 ...
2206 """
2207
2208 # MainSizer is the top-level one that manages everything.
2209 mainSizer = wx.BoxSizer(wx.VERTICAL)
2210
2211 mbSizer = wx.BoxSizer(wx.HORIZONTAL)
2212 mbSizer.Add(self.menuBar, 1, wx.ALL, 0)
2213
2214 mainSizer.Add(self.titleBarPnl, 1, wx.EXPAND, 0)
2215 mainSizer.Add(mbSizer, 0, wx.EXPAND, 0)
2216 mainSizer.Add(self.mainPnl, 1, wx.EXPAND, 0)
2217
2218 # Finally, tell the panel to use the sizer for layout.
2219 self.SetSizer(mainSizer)
2220 self.Layout()
2221
2222
2223 def OnRoll(self, event) :
2224 """
2225 Thanks to Ray Pasco.
2226 """
2227
2228 if not bool(self.isRolled) :
2229 # Set the flag to the state we want regardless of whether
2230 # or not it's in currently in the opposite state.
2231 self.RollUnRoll(wantToRoll=True)
2232
2233 elif self.isRolled : # UnRoll.
2234 # Set the flag to the state we want regardless of whether
2235 # or not it's in currently in the opposite state.
2236 self.RollUnRoll(wantToRoll=False)
2237
2238 print("OnRoll :", self.GetClientSize())
2239
2240
2241 def RollUnRoll(self, wantToRoll) :
2242 """
2243 Thanks to Ray Pasco.
2244 """
2245
2246 # Save the current size only if the Frame is not rolled up.
2247 if not bool(self.isRolled) :
2248 self.unrolledFrameClientSize_size = self.GetClientSize()
2249
2250 if bool(wantToRoll) : # UnRoll.
2251 # Set size (52).
2252 self.SetClientSize((self.unrolledFrameClientSize_size[0], 52))
2253 # Set to match this new state.
2254 self.isRolled = True
2255
2256 else : # Roll
2257 self.SetClientSize(self.unrolledFrameClientSize_size)
2258 # Set to match this new state.
2259 self.isRolled = False
2260
2261 print("RollUnRoll :", self.GetClientSize())
2262
2263
2264 def OnAbout(self, event):
2265 """
2266 ...
2267 """
2268
2269 self.dialog = MyAboutDlg(self)
2270
2271
2272 def OnLeftDown(self, event):
2273 """
2274 ...
2275 """
2276
2277 self.CaptureMouse()
2278 x, y = self.ClientToScreen(event.GetPosition())
2279 originx, originy = self.GetPosition()
2280 dx = x - originx
2281 dy = y - originy
2282 self.delta = ((dx, dy))
2283
2284
2285 def OnLeftUp(self, evt):
2286 """
2287 ...
2288 """
2289
2290 if self.HasCapture():
2291 self.ReleaseMouse()
2292
2293
2294 def OnMouseMove(self, event):
2295 """
2296 ...
2297 """
2298
2299 if event.Dragging() and event.LeftIsDown():
2300 x, y = self.ClientToScreen(event.GetPosition())
2301 fp = (x - self.delta[0], y - self.delta[1])
2302 self.Move(fp)
2303
2304
2305 def OnIconfiy(self, event):
2306 """
2307 ...
2308 """
2309
2310 self.Iconize()
2311
2312
2313 def OnResize(self, event):
2314 """
2315 ...
2316 """
2317
2318 w, h = self.GetClientSize()
2319 print("self :", self.GetClientSize())
2320
2321 #------------
2322 #------------
2323
2324 self.titleBarPnl.SetSize((w, 31))
2325 self.menuBar.SetSize((w, -1))
2326 self.mainPnl.SetSize((w, h-54))
2327
2328 #------------
2329
2330 self.Refresh()
2331
2332
2333 def OnFullScreen(self, event):
2334 """
2335 ...
2336 """
2337
2338 self.ShowFullScreen(not self.IsFullScreen(),
2339 wx.BORDER_SIMPLE)
2340
2341
2342 def OnKeyUp(self, event):
2343 """
2344 ...
2345 """
2346
2347 if event.GetKeyCode() == wx.WXK_ESCAPE:
2348 # Close the frame, no action.
2349 self.OnCloseWindow(event)
2350 event.Skip()
2351
2352
2353 def OnBtnClose(self, event):
2354 """
2355 ...
2356 """
2357
2358 self.Close()
2359
2360
2361 def OnCloseWindow(self, event):
2362 """
2363 Quit this application.
2364 """
2365
2366 # self.Destroy()
2367 self.OnTimerOut(self)
2368
2369 print("Exit application.")
2370
2371 #---------------------------------------------------------------------------
2372
2373 class MyApp(wx.App):
2374 """
2375 Thanks to Andrea Gavana.
2376 """
2377 def OnInit(self):
2378
2379 #------------
2380
2381 self.locale = wx.Locale(wx.LANGUAGE_ENGLISH)
2382
2383 #------------
2384
2385 self.SetAppName("Custom Gui 1")
2386
2387 #------------
2388
2389 self.installDir = os.path.split(os.path.abspath(sys.argv[0]))[0]
2390
2391 #------------
2392
2393 frame = MyFrame()
2394 self.SetTopWindow(frame)
2395 frame.Show(True)
2396 return True
2397
2398 #-----------------------------------------------------------------------
2399
2400 def GetInstallDir(self):
2401 """
2402 Returns the installation directory for my application.
2403 """
2404
2405 return self.installDir
2406
2407
2408 def GetIconsDir(self):
2409 """
2410 Returns the icons directory for my application.
2411 """
2412
2413 icons_dir = os.path.join(self.installDir, "icons")
2414 return icons_dir
2415
2416
2417 def GetBitmapsDir(self):
2418 """
2419 Returns the bitmaps directory for my application.
2420 """
2421
2422 bitmaps_dir = os.path.join(self.installDir, "bitmaps")
2423 return bitmaps_dir
2424
2425
2426 def GetConfig(self):
2427 """
2428 Returns the config file for my application.
2429 """
2430
2431 config = wx.FileConfig(appName="Custom Gui",
2432 localFilename=os.path.join(self.installDir,
2433 "options"))
2434 return config
2435
2436 #---------------------------------------------------------------------------
2437
2438 def main():
2439 app = MyApp(False)
2440 app.MainLoop()
2441
2442 #---------------------------------------------------------------------------
2443
2444 if __name__ == "__main__" :
2445 main()
Second example
1 # sample_two.py
2
3 import sys
4 import os
5 import platform
6 import wx
7 from wx.adv import TaskBarIcon as TaskBarIcon
8 import wx.lib.fancytext as fancytext
9 import wx.lib.agw.flatmenu as FM
10 from wx.lib.agw.artmanager import ArtManager, RendererBase, DCSaver
11 from wx.lib.agw.fmresources import ControlFocus, ControlPressed
12 import wx.dataview as dv
13 import wx.lib.colourdb
14 import rectshapedbitmapbutton as SBB
15 import rectshapedbitmapbuttonTwo as SBBTwo
16 import data
17
18 musicdata = sorted(data.musicdata.items())
19 musicdata = [[str(k)] + list(v) for k,v in musicdata]
20
21 # def SwitchRGBtoBGR
22 # def CreateBackgroundBitmap
23 # class MyMenuRenderer
24 # class MyTaskBarIcon
25 # class MyListCtrlPnl
26 # class MyStatusBar
27 # class MyTitleBar
28 # class MyAboutDlg
29 # class MyPopupMenu
30 # class MyTitleBarPnl
31 # class MyMainPnl
32 # class MyFrame
33 # class MyApp
34
35 ID_FULLSCREEN = wx.NewIdRef()
36 ID_MAIN_PNL = wx.NewIdRef()
37 ID_BTN_FULLSCREEN = wx.NewIdRef()
38 ID_BTN_ABOUT = wx.NewIdRef()
39 ID_BTN_QUIT = wx.NewIdRef()
40 ID_HELLO = wx.NewIdRef()
41
42 #---------------------------------------------------------------------------
43
44 def SwitchRGBtoBGR(colour):
45 """
46 ...
47 """
48
49 return wx.Colour(colour.Blue(), colour.Green(), colour.Red())
50
51 #---------------------------------------------------------------------------
52
53 def CreateBackgroundBitmap():
54 """
55 ...
56 """
57
58 mem_dc = wx.MemoryDC()
59 bmp = wx.Bitmap(121, 300)
60 mem_dc.SelectObject(bmp)
61
62 mem_dc.Clear()
63
64 # Colour the menu face with background colour.
65 mem_dc.SetPen(wx.Pen("#a0a0a0", 0))
66 mem_dc.SetBrush(wx.Brush("#48b4dc"))
67 mem_dc.DrawRectangle(0, 15, 121, 300)
68 mem_dc.DrawLine(0, 0, 300, 0)
69
70 mem_dc.SelectObject(wx.NullBitmap)
71 return bmp
72
73 #---------------------------------------------------------------------------
74
75 class MyMenuRenderer(FM.FMRenderer):
76 """
77 Thanks to Andrea Gavana.
78 A custom renderer class for FlatMenu.
79 """
80 def __init__(self):
81 FM.FMRenderer.__init__(self)
82
83 #-----------------------------------------------------------------------
84
85 def DrawMenuButton(self, dc, rect, state):
86 """
87 Draws the highlight on a FlatMenu.
88 """
89
90 self.DrawButton(dc, rect, state)
91
92
93 def DrawMenuBarButton(self, dc, rect, state):
94 """
95 Draws the highlight on a FlatMenuBar.
96 """
97
98 self.DrawButton(dc, rect, state)
99
100
101 def DrawButton(self, dc, rect, state, colour=None):
102 """
103 ...
104 """
105
106 if state == ControlFocus:
107 penColour = SwitchRGBtoBGR(ArtManager.Get().FrameColour())
108 brushColour = SwitchRGBtoBGR(ArtManager.Get().BackgroundColour())
109 elif state == ControlPressed:
110 penColour = SwitchRGBtoBGR(ArtManager.Get().FrameColour())
111 brushColour = SwitchRGBtoBGR(ArtManager.Get().HighlightBackgroundColour())
112 else: # ControlNormal, ControlDisabled, default.
113 penColour = SwitchRGBtoBGR(ArtManager.Get().FrameColour())
114 brushColour = SwitchRGBtoBGR(ArtManager.Get().BackgroundColour())
115
116 # Draw the button borders.
117 dc = wx.GCDC(dc)
118 dc.SetPen(wx.Pen(penColour))
119 dc.SetBrush(wx.Brush(brushColour))
120 dc.DrawRoundedRectangle(rect.x, rect.y, rect.width, rect.height-3, 4)
121
122
123 def DrawMenuBarBackground(self, dc, rect):
124 """
125 ...
126 """
127
128 # For office style, we simple draw a rectangle
129 # with a gradient colouring.
130 vertical = ArtManager.Get().GetMBVerticalGradient()
131
132 dcsaver = DCSaver(dc)
133
134 # Fill with gradient.
135 startColour = self.menuBarFaceColour
136 endColour = ArtManager.Get().LightColour(startColour, 0)
137
138 dc.SetPen(wx.Pen(endColour))
139 dc.SetBrush(wx.Brush(endColour))
140 dc.DrawRectangle(rect)
141
142
143 def DrawToolBarBg(self, dc, rect):
144 """
145 ...
146 """
147
148 if not ArtManager.Get().GetRaiseToolbar():
149 return
150
151 # Fill with gradient.
152 startColour = self.menuBarFaceColour()
153 dc.SetPen(wx.Pen(startColour))
154 dc.SetBrush(wx.Brush(startColour))
155 dc.DrawRectangle(0, 0, rect.GetWidth(), rect.GetHeight())
156
157 #---------------------------------------------------------------------------
158
159 class MyTaskBarIcon(TaskBarIcon):
160 TBMENU_RESTORE = wx.NewIdRef()
161 TBMENU_CLOSE = wx.NewIdRef()
162 TBMENU_REMOVE = wx.NewIdRef()
163 def __init__(self, frame):
164 TaskBarIcon.__init__(self, wx.adv.TBI_DOCK)
165 self.frame = frame
166
167 #------------
168
169 # Return icons folder.
170 self.icons_dir = wx.GetApp().GetIconsDir()
171
172 #------------
173
174 frameIcon = wx.Icon(os.path.join(self.icons_dir,
175 "icon_wxWidgets.ico"),
176 type=wx.BITMAP_TYPE_ICO)
177 self.SetIcon(frameIcon)
178
179 # Bind some events.
180 self.Bind(wx.adv.EVT_TASKBAR_LEFT_DCLICK, self.OnTaskBarActivate)
181 self.Bind(wx.EVT_MENU, self.OnTaskBarActivate, id=self.TBMENU_RESTORE)
182 self.Bind(wx.EVT_MENU, self.OnTaskBarClose, id=self.TBMENU_CLOSE)
183 self.Bind(wx.EVT_MENU, self.OnTaskBarRemove, id=self.TBMENU_REMOVE)
184
185
186 def CreatePopupMenu(self):
187 """
188 This method is called by the base class when it needs to popup
189 the menu for the default EVT_RIGHT_DOWN event. Just create
190 the menu how you want it and return it from this function,
191 the base class takes care of the rest.
192 """
193 menu = wx.Menu()
194 menu.Append(self.TBMENU_RESTORE, "Restore main frame")
195 menu.Append(self.TBMENU_CLOSE, "Close main frame")
196 menu.AppendSeparator()
197 menu.Append(self.TBMENU_REMOVE, "Remove the TB Icon")
198 return menu
199
200
201 def OnTaskBarActivate(self, evt):
202 if self.frame.IsIconized():
203 self.frame.Iconize(False)
204 if not self.frame.IsShown():
205 self.frame.Show(True)
206 self.frame.Raise()
207
208
209 def OnTaskBarClose(self, evt):
210 wx.CallAfter(self.frame.Close)
211
212
213 def OnTaskBarRemove(self, evt):
214 self.RemoveIcon()
215
216 #---------------------------------------------------------------------------
217
218 class MyListCtrlPnl(wx.Panel):
219 """
220 Thanks to Robin Dunn.
221 """
222 def __init__(self, parent):
223 wx.Panel.__init__(self, parent, -1)
224
225 # Create the listctrl.
226 self.dvlc = dv.DataViewListCtrl(self,
227 style=wx.BORDER_SIMPLE|
228 dv.DV_ROW_LINES|
229 dv.DV_HORIZ_RULES|
230 dv.DV_VERT_RULES)
231
232 # Give it some columns.
233 # The ID col we'll customize a bit :
234 self.dvlc.AppendTextColumn("Id", width=40)
235 self.dvlc.AppendTextColumn("Artist", width=170)
236 self.dvlc.AppendTextColumn("Title", width=260)
237 self.dvlc.AppendTextColumn("Genre", width=80)
238
239 # Load the data. Each item (row) is added as a sequence
240 # of values whose order matches the columns.
241 for itemvalues in musicdata:
242 self.dvlc.AppendItem(itemvalues)
243
244 self.dvlc.SetBackgroundColour("yellow")
245 self.dvlc.SetForegroundColour("gray10")
246
247 # Set the layout so the listctrl fills the panel.
248 self.Sizer = wx.BoxSizer()
249 self.Sizer.Add(self.dvlc, 1, wx.EXPAND)
250
251 #---------------------------------------------------------------------------
252
253 class MyStatusBar(wx.StatusBar) :
254 """
255 Thanks to ???.
256 Simple custom colorized StatusBar.
257 """
258 def __init__(self, parent, id) :
259 wx.StatusBar.__init__(self, parent, id)
260
261 #------------
262
263 # Return config file.
264 self.config = wx.GetApp().GetConfig()
265
266 #------------
267
268 # Simplified init method.
269 self.SetProperties()
270 self.BindEvents()
271
272 #-----------------------------------------------------------------------
273
274 def SetProperties(self):
275 """
276 ...
277 """
278
279 # Read config file.
280 self.SetBackgroundColour(wx.Colour(self.config.Read("Color2")))
281
282
283 def BindEvents(self):
284 """
285 Bind some events to an events handler.
286 """
287
288 self.Bind(wx.EVT_PAINT, self.OnPaint)
289
290
291 def OnPaint(self, event) :
292 """
293 ...
294 """
295
296 dc = wx.BufferedPaintDC(self)
297 self.Draw(dc)
298
299
300 def Draw(self, dc) :
301 """
302 ...
303 """
304
305 # Read config file.
306 dc.SetBackground(wx.Brush(self.config.Read("Color2")))
307 dc.Clear()
308
309 textVertOffset = 3 # Perfect for MSW.
310 textHorzOffset = 5 # Arbitrary - looks nicer.
311
312 dc.SetTextForeground(wx.BLACK)
313 dc.DrawText(self.GetStatusText(), textHorzOffset, textVertOffset)
314
315 #---------------------------------------------------------------------------
316
317 class MyTitleBar(wx.Control):
318 """
319 Thanks to Cody Precord.
320 """
321 def __init__(self, parent, label, size):
322 style = (wx.NO_BORDER)
323 super(MyTitleBar, self).__init__(parent,
324 style=style)
325
326 #------------
327
328 # Return config file.
329 self.config = wx.GetApp().GetConfig()
330 # Return bitmaps folder.
331 self.bitmaps_dir = wx.GetApp().GetBitmapsDir()
332
333 #------------
334
335 # Attributes.
336 self.parent = parent
337 self.label = label
338 self.size = size
339
340 #------------
341
342 # Simplified init method.
343 self.SetBackground()
344 self.SetProperties(label, size)
345 self.CreateCtrls()
346 self.DoLayout()
347 self.BindEvents()
348
349 #-----------------------------------------------------------------------
350
351 def SetBackground(self):
352 """
353 ...
354 """
355
356 self.SetBackgroundColour(wx.Colour(self.config.Read("Color2")))
357 self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM)
358
359
360 def SetProperties(self, label, size):
361 """
362 ...
363 """
364
365 self.label = label
366 self.size = size
367
368 self.label_font = self.GetFont()
369 self.label_font.SetFamily(wx.SWISS)
370 self.label_font.SetPointSize(size)
371 self.label_font.SetWeight(wx.BOLD)
372 self.SetFont(self.label_font)
373
374
375 def CreateCtrls(self):
376 """
377 ...
378 """
379
380 w, h = self.GetSize()
381 w1, h1 = self.GetClientSize()
382
383 #------------
384
385 # Load an icon bitmap for titlebar.
386 # bmp = wx.Bitmap(os.path.join(self.bitmaps_dir,
387 # "icon_app.png"),
388 # type=wx.BITMAP_TYPE_PNG)
389
390 # self.ico = wx.StaticBitmap(self, -1, bmp)
391 # self.ico.SetBackgroundColour(wx.Colour(self.config.Read("Color2")))
392 # self.ico.SetPosition((300, 1))
393 # self.ico.SetToolTip("This is a customized icon.")
394 # self.ico.SetBackgroundStyle(wx.BG_STYLE_CUSTOM)
395
396 #------------
397
398 # Button Exit.
399 self.btn3 = SBBTwo.ShapedBitmapButton(self, -1,
400 bitmap=wx.Bitmap(os.path.join(self.bitmaps_dir,
401 "btn_gloss_exit_normal_1.png"),
402 type=wx.BITMAP_TYPE_PNG),
403
404 pressedBmp=wx.Bitmap(os.path.join(self.bitmaps_dir,
405 "btn_gloss_exit_selected_1.png"),
406 type=wx.BITMAP_TYPE_PNG),
407
408 hoverBmp=wx.Bitmap(os.path.join(self.bitmaps_dir,
409 "btn_gloss_exit_normal_1.png"),
410 type=wx.BITMAP_TYPE_PNG),
411
412 disabledBmp=wx.Bitmap(os.path.join(self.bitmaps_dir,
413 "btn_gloss_exit_normal_1.png"),
414 type=wx.BITMAP_TYPE_PNG),
415
416 label="",
417 labelForeColour=wx.WHITE,
418 labelFont=wx.Font(9,
419 wx.FONTFAMILY_DEFAULT,
420 wx.FONTSTYLE_NORMAL,
421 wx.FONTWEIGHT_BOLD),
422 style=wx.BORDER_NONE)
423
424 #------------
425
426 # Button Maximize.
427 self.btn4 = SBBTwo.ShapedBitmapButton(self, -1,
428 bitmap=wx.Bitmap(os.path.join(self.bitmaps_dir,
429 "btn_gloss_maximize_normal_1.png"),
430 type=wx.BITMAP_TYPE_PNG),
431
432 pressedBmp=wx.Bitmap(os.path.join(self.bitmaps_dir,
433 "btn_gloss_maximize_selected_1.png"),
434 type=wx.BITMAP_TYPE_PNG),
435
436 hoverBmp=wx.Bitmap(os.path.join(self.bitmaps_dir,
437 "btn_gloss_maximize_normal_1.png"),
438 type=wx.BITMAP_TYPE_PNG),
439
440 disabledBmp=wx.Bitmap(os.path.join(self.bitmaps_dir,
441 "btn_gloss_maximize_normal_1.png"),
442 type=wx.BITMAP_TYPE_PNG),
443
444 label="",
445 labelForeColour=wx.WHITE,
446 labelFont=wx.Font(9,
447 wx.FONTFAMILY_DEFAULT,
448 wx.FONTSTYLE_NORMAL,
449 wx.FONTWEIGHT_BOLD),
450 style=wx.BORDER_NONE)
451
452 #------------
453
454 # Thanks to MCOW.
455 # Button Reduce.
456 self.btn5 = SBBTwo.ShapedBitmapButton(self, -1,
457 bitmap=wx.Bitmap(os.path.join(self.bitmaps_dir,
458 "btn_gloss_reduce_normal_1.png"),
459 type=wx.BITMAP_TYPE_PNG),
460
461 pressedBmp=wx.Bitmap(os.path.join(self.bitmaps_dir,
462 "btn_gloss_reduce_selected_1.png"),
463 type=wx.BITMAP_TYPE_PNG),
464
465 hoverBmp=wx.Bitmap(os.path.join(self.bitmaps_dir,
466 "btn_gloss_reduce_normal_1.png"),
467 type=wx.BITMAP_TYPE_PNG),
468
469 disabledBmp=wx.Bitmap(os.path.join(self.bitmaps_dir,
470 "btn_gloss_reduce_normal_1.png"),
471 type=wx.BITMAP_TYPE_PNG),
472
473 label="",
474 labelForeColour=wx.WHITE,
475 labelFont=wx.Font(9,
476 wx.FONTFAMILY_DEFAULT,
477 wx.FONTSTYLE_NORMAL,
478 wx.FONTWEIGHT_BOLD),
479 style=wx.BORDER_NONE)
480
481 #------------
482
483 # Button Roll.
484 self.btn6 = SBBTwo.ShapedBitmapButton(self, -1,
485 bitmap=wx.Bitmap(os.path.join(self.bitmaps_dir,
486 "btn_gloss_roll_normal_1.png"),
487 type=wx.BITMAP_TYPE_PNG),
488
489 pressedBmp=wx.Bitmap(os.path.join(self.bitmaps_dir,
490 "btn_gloss_roll_selected_1.png"),
491 type=wx.BITMAP_TYPE_PNG),
492
493 hoverBmp=wx.Bitmap(os.path.join(self.bitmaps_dir,
494 "btn_gloss_roll_normal_1.png"),
495 type=wx.BITMAP_TYPE_PNG),
496
497 disabledBmp=wx.Bitmap(os.path.join(self.bitmaps_dir,
498 "btn_gloss_roll_normal_1.png"),
499 type=wx.BITMAP_TYPE_PNG),
500
501 label="",
502 labelForeColour=wx.WHITE,
503 labelFont=wx.Font(9,
504 wx.FONTFAMILY_DEFAULT,
505 wx.FONTSTYLE_NORMAL,
506 wx.FONTWEIGHT_BOLD),
507 style=wx.BORDER_NONE)
508
509
510 def DoLayout(self):
511 """
512 ...
513 """
514
515 mainSizer = wx.BoxSizer(wx.HORIZONTAL)
516
517 #------------
518
519 mainSizer.Add(self.btn3, 0, wx.LEFT | wx.TOP, 4)
520 mainSizer.Add(self.btn4, 0, wx.LEFT | wx.TOP, 4)
521 mainSizer.Add(self.btn5, 0, wx.LEFT | wx.TOP, 4)
522 mainSizer.Add(self.btn6, 0, wx.LEFT | wx.TOP, 4)
523
524 #------------
525
526 self.SetSizer(mainSizer)
527 self.Layout()
528
529
530 def BindEvents(self):
531 """
532 Bind some events to an events handler.
533 """
534
535 # self.ico.Bind(wx.EVT_RIGHT_DOWN, self.OnRightDown)
536 # self.ico.Bind(wx.EVT_LEFT_DOWN, self.OnRightDown)
537
538 self.Bind(wx.EVT_PAINT, self.OnPaint)
539 self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
540 self.Bind(wx.EVT_LEFT_UP, self.OnLeftUp)
541
542 self.btn3.Bind(wx.EVT_BUTTON, self.OnBtnClose)
543 self.btn4.Bind(wx.EVT_BUTTON, self.OnFullScreen)
544 self.btn5.Bind(wx.EVT_BUTTON, self.OnIconfiy)
545 self.btn6.Bind(wx.EVT_BUTTON, self.OnRoll)
546
547
548 def OnRightDown(self, event):
549 """
550 ...
551 """
552
553 self.PopupMenu(MyPopupMenu(self), event.GetPosition())
554
555 print("Right down.")
556
557
558 def OnLeftDown(self, event):
559 """
560 ...
561 """
562
563 self.GetTopLevelParent().OnLeftDown(event)
564
565
566 def OnLeftUp(self, event):
567 """
568 ...
569 """
570
571 self.GetTopLevelParent().OnLeftUp(event)
572
573
574 def SetLabel(self, label):
575 """
576 ...
577 """
578
579 self.label = label
580 self.Refresh()
581
582
583 def DoGetBestSize(self):
584 """
585 ...
586 """
587
588 dc = wx.ClientDC(self)
589 dc.SetFont(self.GetFont())
590
591 textWidth, textHeight = dc.GetTextExtent(self.label)
592 spacing = 10
593 totalWidth = textWidth + (spacing)
594 totalHeight = textHeight + (spacing)
595
596 best = wx.Size(totalWidth, totalHeight)
597 self.CacheBestSize(best)
598
599 return best
600
601
602 def GetLabel(self):
603 """
604 ...
605 """
606
607 return self.label
608
609
610 def GetLabelColor(self):
611 """
612 ...
613 """
614
615 return self.foreground
616
617
618 def GetLabelSize(self):
619 """
620 ...
621 """
622
623 return self.size
624
625
626 def SetLabelColour(self, colour):
627 """
628 ...
629 """
630
631 self.labelColour = colour
632
633
634 def OnPaint(self, event):
635 """
636 ...
637 """
638
639 dc = wx.BufferedPaintDC(self)
640 gcdc = wx.GCDC(dc)
641
642 gcdc.Clear()
643
644 # Setup the GraphicsContext.
645 gc = gcdc.GetGraphicsContext()
646
647 # Get the working size we can draw in.
648 width, height = self.GetSize()
649
650 # Use the GCDC to draw the text.
651 # Read config file.
652 brush = self.config.Read("Color2")
653 gcdc.SetPen(wx.Pen(brush, 1))
654 gcdc.SetBrush(wx.Brush(brush))
655 gcdc.DrawRectangle(0, 0, width, height)
656
657 # Get the system font.
658 gcdc.SetFont(self.GetFont())
659
660 textWidth, textHeight = gcdc.GetTextExtent(self.label)
661 tposx, tposy = ((width/2)-(textWidth/2), (height/3)-(textHeight/3))
662
663 tposx += 0
664 tposy += 4
665
666 # Set position and text color.
667 gcdc.SetTextForeground("white")
668 gcdc.DrawText(self.label, int(tposx), int(tposy+1))
669
670 gcdc.SetTextForeground(self.labelColour)
671 gcdc.DrawText(self.label, int(tposx), int(tposy))
672
673
674 def OnRoll(self, event):
675 """
676 ...
677 """
678
679 self.GetTopLevelParent().OnRoll(True)
680
681 print("Roll/unRoll button was clicked.")
682
683
684 def OnIconfiy(self, event):
685 """
686 ...
687 """
688
689 self.GetTopLevelParent().OnIconfiy(self)
690
691 print("Iconfiy button was clicked.")
692
693
694 def OnFullScreen(self, event):
695 """
696 ...
697 """
698
699 self.GetTopLevelParent().OnFullScreen(self)
700
701 print("FullScreen button was clicked.")
702
703
704 def OnBtnClose(self, event):
705 """
706 ...
707 """
708
709 self.GetTopLevelParent().OnCloseWindow(self)
710
711 print("Close button was clicked.")
712
713 #---------------------------------------------------------------------------
714
715 class MyAboutDlg(wx.Frame):
716 """
717 Thanks to Robin Dunn.
718 """
719 def __init__(self, parent):
720 style = (wx.FRAME_SHAPED | wx.NO_BORDER |
721 wx.CLIP_CHILDREN | wx.STAY_ON_TOP |
722 wx.SYSTEM_MENU | wx.CLOSE_BOX |
723 wx.NO_FULL_REPAINT_ON_RESIZE)
724 wx.Frame.__init__(self,
725 parent,
726 id=-1,
727 title="About...",
728 style=style)
729
730 #------------
731
732 # Attributes.
733 self.SetTransparent(0)
734 self.opacity_in = 0
735 self.opacity_out = 255
736 self.deltaN = -70
737 self.hasShape = False
738 self.delta = wx.Point(0,0)
739
740 #------------
741
742 # Return config file.
743 self.config = wx.GetApp().GetConfig()
744 # Return application name.
745 self.app_name = wx.GetApp().GetAppName()
746 # Return bitmaps folder.
747 self.bitmaps_dir = wx.GetApp().GetBitmapsDir()
748 # Return icons folder.
749 self.icons_dir = wx.GetApp().GetIconsDir()
750
751 #------------
752
753 # Simplified init method.
754 self.SetProperties()
755 self.OnTimerIn(self)
756 self.CreateCtrls()
757 self.BindEvents()
758
759 #------------
760
761 self.CenterOnParent(wx.BOTH)
762 self.GetParent().Enable(False)
763
764 #------------
765
766 self.Show(True)
767
768 #------------
769
770 self.eventLoop = wx.GUIEventLoop()
771 self.eventLoop.Run()
772
773 #-----------------------------------------------------------------------
774
775 def SetProperties(self):
776 """
777 Set the dialog properties (title, icon, transparency...).
778 """
779
780 frameIcon = wx.Icon(os.path.join(self.icons_dir,
781 "icon_wxWidgets.ico"),
782 type=wx.BITMAP_TYPE_ICO)
783 self.SetIcon(frameIcon)
784
785
786 def OnTimerIn(self, evt):
787 """
788 Thanks to Pascal Faut.
789 """
790
791 self.timer1 = wx.Timer(self, -1)
792 self.timer1.Start(1)
793 self.Bind(wx.EVT_TIMER, self.AlphaCycle1, self.timer1)
794
795 print("Fade-in was launched.")
796
797
798 def OnTimerOut(self, evt):
799 """
800 Thanks to Pascal Faut.
801 """
802
803 self.timer2 = wx.Timer(self, -1)
804 self.timer2.Start(1)
805 self.Bind(wx.EVT_TIMER, self.AlphaCycle2, self.timer2)
806
807 print("Fade-out was launched.")
808
809
810 def AlphaCycle1(self, *args):
811 """
812 Thanks to Pascal Faut.
813 """
814
815 self.opacity_in += self.deltaN
816 if self.opacity_in <= 0:
817 self.deltaN = -self.deltaN
818 self.opacity_in = 0
819
820 if self.opacity_in >= 255:
821 self.deltaN = -self.deltaN
822 self.opacity_in = 255
823
824 self.timer1.Stop()
825
826 self.SetTransparent(self.opacity_in)
827
828 print("Fade in = {}/255".format(self.opacity_in))
829
830
831 def AlphaCycle2(self, *args):
832 """
833 Thanks to Pascal Faut.
834 """
835
836 self.opacity_out += self.deltaN
837 if self.opacity_out >= 255:
838 self.deltaN = -self.deltaN
839 self.opacity_out = 255
840
841 if self.opacity_out <= 0:
842 self.deltaN = -self.deltaN
843 self.opacity_out = 0
844
845 self.timer2.Stop()
846
847 wx.CallAfter(self.Destroy)
848
849 self.SetTransparent(self.opacity_out)
850
851 print("Fade out = {}/255".format(self.opacity_out))
852
853
854 def CreateCtrls(self):
855 """
856 Make widgets for my dialog.
857 """
858
859 # Load a background bitmap.
860 self.bmp = wx.Bitmap(os.path.join(self.bitmaps_dir,
861 "skin_about_3.png"),
862 type=wx.BITMAP_TYPE_PNG)
863 mask = wx.Mask(self.bmp, wx.RED)
864 self.bmp.SetMask(mask)
865
866 #------------
867
868 self.SetClientSize((self.bmp.GetWidth(), self.bmp.GetHeight()))
869
870 #------------
871
872 if wx.Platform == "__WXGTK__":
873 # wxGTK requires that the window be created before you can
874 # set its shape, so delay the call to SetWindowShape until
875 # this event.
876 self.Bind(wx.EVT_WINDOW_CREATE, self.SetWindowShape)
877 else:
878 # On wxMSW and wxMac the window has already
879 # been created, so go for it.
880 self.SetWindowShape()
881
882
883 def BindEvents(self):
884 """
885 Bind all the events related to my dialog.
886 """
887
888 # Bind some events to an events handler.
889 self.Bind(wx.EVT_LEFT_UP, self.OnLeftUp)
890 self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
891 self.Bind(wx.EVT_RIGHT_UP, self.OnCloseWindow) # Panel right clic.
892 self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)
893 self.Bind(wx.EVT_WINDOW_CREATE, self.SetWindowShape)
894 self.Bind(wx.EVT_MOTION, self.OnMouseMove)
895 self.Bind(wx.EVT_KEY_UP, self.OnKeyUp)
896 self.Bind(wx.EVT_PAINT, self.OnPaint)
897 self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
898
899
900 def SetWindowShape(self, event=None):
901 """
902 ...
903 """
904
905 # Use the bitmap's mask to determine the region.
906 r = wx.Region(self.bmp)
907 self.hasShape = self.SetShape(r)
908
909
910 def OnEraseBackground(self, event):
911 """
912 ...
913 """
914
915 dc = event.GetDC()
916 if not dc:
917 dc = wx.ClientDC(self)
918 rect = self.GetUpdateRegion().GetBox()
919 dc.SetClippingRect(rect)
920
921
922 def OnLeftDown(self, event):
923 """
924 ...
925 """
926
927 self.CaptureMouse()
928 x, y = self.ClientToScreen(event.GetPosition())
929 originx, originy = self.GetPosition()
930 dx = x - originx
931 dy = y - originy
932 self.delta = ((dx, dy))
933
934
935 def OnLeftUp(self, evt):
936 """
937 ...
938 """
939
940 if self.HasCapture():
941 self.ReleaseMouse()
942
943
944 def OnMouseMove(self, event):
945 """
946 ...
947 """
948
949 if event.Dragging() and event.LeftIsDown():
950 x, y = self.ClientToScreen(event.GetPosition())
951 fp = (x - self.delta[0], y - self.delta[1])
952 self.Move(fp)
953
954
955 def OnPaint(self, event):
956 """
957 ...
958 """
959
960 dc = wx.AutoBufferedPaintDCFactory(self)
961 dc.SetBackground(wx.Brush("BLACK"))
962 dc.Clear()
963
964 #------------
965
966 # These are strings.
967 py_version = sys.version.split()[0]
968
969 str1 = (('<font style="normal" family="default" color="yellow" size="10" weight="bold">'
970 'Programming : </font>'
971 '<font style="normal" family="default" color="black" size="10" weight="normal">'
972 'Python {}</font>').format(py_version)) # Python 3.7.2
973
974 str2 = (('<font style="normal" family="default" color="orange" size="10" weight="bold">'
975 'GUI toolkit : </font>'
976 '<font style="normal" family="default" color="black" size="10" weight="normal">'
977 'wxPython {}</font>').format(wx.VERSION_STRING)) # wxPython 4.0.4
978
979 str3 = (('<font style="normal" family="default" color="brown" size="10" weight="bold">'
980 'Library : </font>'
981 '<font style="normal" family="default" color="black" size="10" weight="normal">'
982 '{}</font>').format(wx.GetLibraryVersionInfo().VersionString)) # wxWidgets 3.0.5
983
984 str4 = (('<font style="normal" family="default" color="blue" size="10" weight="bold">'
985 'Operating system : </font>'
986 '<font style="normal" family="default" color="black" size="10" weight="normal">'
987 '{}</font>').format(platform.system())) # Windows
988
989 str5 = (('<font style="normal" family="default" color="black" size="9" weight="normal">'
990 '{}</font>').format(self.app_name)) # Custom Gui
991
992 str6 = (('<font style="normal" family="default" color="black" size="8" weight="normal">'
993 'Right clic or Esc for Exit</font>'))
994
995 #------------
996
997 # Get the working size we can draw in.
998 bw, bh = self.GetSize()
999
1000 # Use the GCDC to draw the text.
1001 # Draw the about borders.
1002 dc.SetPen(wx.Pen(wx.Colour(240, 240, 240), 20))
1003 # Read config file.
1004 dc.SetBrush(wx.Brush(self.config.Read("Color2")))
1005 # or
1006 # dc.SetBrush(wx.Brush(wx.TRANSPARENT_BRUSH))
1007 dc.DrawRectangle(0, 0, bw, bh)
1008
1009 #------------
1010
1011 # Draw text.
1012 # Need width to calculate x position of str1.
1013 tw, th = fancytext.GetExtent(str1, dc)
1014 # Centered text.
1015 fancytext.RenderToDC(str1, dc, (bw-tw)/2, 30)
1016
1017 #------------
1018
1019 # Need width to calculate x position of str2.
1020 tw, th = fancytext.GetExtent(str2, dc)
1021 # Centered text.
1022 fancytext.RenderToDC(str2, dc, (bw-tw)/2, 50)
1023
1024 #------------
1025
1026 # Need width to calculate x position of str3.
1027 tw, th = fancytext.GetExtent(str3, dc)
1028 # Centered text.
1029 fancytext.RenderToDC(str3, dc, (bw-tw)/2, 70)
1030
1031 #------------
1032
1033 # Need width to calculate x position of str4.
1034 tw, th = fancytext.GetExtent(str4, dc)
1035 # Centered text.
1036 fancytext.RenderToDC(str4, dc, (bw-tw)/2, 90)
1037
1038 #------------
1039
1040 # Need width to calculate x position of str5.
1041 tw, th = fancytext.GetExtent(str5, dc)
1042 # Centered text.
1043 fancytext.RenderToDC(str5, dc, (bw-tw)/2, 130)
1044
1045 #------------
1046
1047 # Need width to calculate x position of str6.
1048 tw, th = fancytext.GetExtent(str6, dc)
1049 # Centered text.
1050 fancytext.RenderToDC(str6, dc, (bw-tw)/2, 190)
1051
1052
1053 def OnKeyUp(self, event):
1054 """
1055 ...
1056 """
1057
1058 if event.GetKeyCode() == wx.WXK_ESCAPE:
1059 self.OnCloseWindow(event)
1060
1061 event.Skip()
1062
1063
1064 def OnCloseWindow(self, event):
1065 """
1066 ...
1067 """
1068
1069 self.GetParent().Enable(True)
1070 self.eventLoop.Exit()
1071 self.Destroy()
1072
1073 #---------------------------------------------------------------------------
1074
1075 class MyPopupMenu(wx.Menu):
1076 """
1077 Thanks to Robin Dunn.
1078 """
1079 def __init__(self, parent):
1080 wx.Menu.__init__(self)
1081
1082 #------------
1083
1084 # Attributes.
1085 self.parent = parent
1086
1087 #------------
1088
1089 # Returns bitmaps folder.
1090 self.bitmaps_dir = wx.GetApp().GetBitmapsDir()
1091
1092 #------------
1093
1094 # Simplified init method.
1095 self.CreatePopupMenu()
1096 self.BindEvents()
1097
1098 #-----------------------------------------------------------------------
1099
1100 def CreatePopupMenu(self, event=None):
1101 """
1102 This method is called by the base class when it needs to popup
1103 the menu for the default EVT_RIGHT_DOWN event. Just create
1104 the menu how you want it and return it from this function,
1105 the base class takes care of the rest.
1106 """
1107
1108 bmp = wx.Bitmap(os.path.join(self.bitmaps_dir,
1109 "item_about.png"),
1110 type=wx.BITMAP_TYPE_PNG)
1111
1112 item = wx.MenuItem(self, id=wx.ID_ABOUT, text=" About")
1113 item.SetBitmap(bmp)
1114 self.Append(item)
1115 self.AppendSeparator()
1116
1117 #------------
1118
1119 bmp = wx.Bitmap(os.path.join(self.bitmaps_dir,
1120 "item_exit.png"),
1121 type=wx.BITMAP_TYPE_PNG)
1122
1123 if True or "__WXMSW__" in wx.PlatformInfo:
1124 font = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT)
1125 font.SetWeight(wx.BOLD)
1126
1127 item = wx.MenuItem(self, id=wx.ID_EXIT, text=" Exit")
1128 item.SetBitmap(bmp)
1129 item.SetFont(font)
1130 self.Append(item)
1131
1132 return self
1133
1134
1135 def BindEvents(self):
1136 """
1137 Bind some events to an events handler.
1138 """
1139
1140 # Bind the menu events to an events handler.
1141 self.Bind(wx.EVT_MENU, self.OnAbout, id=wx.ID_ABOUT)
1142 self.Bind(wx.EVT_MENU, self.OnClose, id=wx.ID_EXIT)
1143
1144
1145 def OnAbout(self, event):
1146 """
1147 ...
1148 """
1149
1150 self.mainFrame = wx.GetApp().GetTopWindow()
1151 self.mainFrame.OnAbout(self)
1152
1153 print("About icon was clicked.")
1154
1155
1156 def OnClose(self, event):
1157 """
1158 ...
1159 """
1160
1161 self.mainFrame = wx.GetApp().GetTopWindow()
1162 self.mainFrame.OnCloseWindow(self)
1163
1164 print("Close icon was clicked.")
1165
1166 #---------------------------------------------------------------------------
1167
1168 class MyTitleBarPnl(wx.Panel):
1169 """
1170 Thanks to Cody Precord.
1171 """
1172 def __init__(self, parent, id, size):
1173 style = (wx.NO_BORDER)
1174 super(MyTitleBarPnl, self).__init__(parent,
1175 id,
1176 size=size,
1177 style=style)
1178
1179 #------------
1180
1181 # Attributes.
1182 self.parent = parent
1183
1184 #------------
1185
1186 # Return config file.
1187 self.config = wx.GetApp().GetConfig()
1188 # Return application name.
1189 self.app_name = wx.GetApp().GetAppName()
1190 # Return bitmaps folder.
1191 self.bitmaps_dir = wx.GetApp().GetBitmapsDir()
1192
1193 #------------
1194
1195 # Simplified init method.
1196 self.SetProperties()
1197 self.CreateCtrls()
1198 self.BindEvents()
1199
1200 #-----------------------------------------------------------------------
1201
1202 def SetProperties(self):
1203 """
1204 ...
1205 """
1206
1207 # Read config file.
1208 self.SetBackgroundColour(wx.Colour(self.config.Read("Color2")))
1209
1210
1211 def CreateCtrls(self):
1212 """
1213 ...
1214 """
1215
1216 w, h = self.GetClientSize()
1217 print("MyTitleBarPnl :", self.GetClientSize())
1218
1219 #------------
1220 #------------
1221
1222 # Add titleBar.
1223 self.titleBar = MyTitleBar(self,
1224 label=self.app_name,
1225 size=10)
1226 self.titleBar.SetPosition((0, 0))
1227 self.titleBar.SetSize((w, 31))
1228 self.titleBar.SetLabelColour("black")
1229 self.titleBar.SetToolTip("This is a customized title bar.")
1230
1231
1232 def BindEvents(self):
1233 """
1234 Bind some events to an events handler.
1235 """
1236
1237 self.Bind(wx.EVT_SIZE, self.OnResize)
1238
1239
1240 def OnResize(self, event):
1241 """
1242 ...
1243 """
1244
1245 w, h = self.GetClientSize()
1246 print("MyTitleBarPnl :", self.GetClientSize())
1247
1248 #------------
1249 #------------
1250
1251 self.titleBar.SetSize((w, 24))
1252
1253 #------------
1254
1255 self.Refresh()
1256
1257 print("On resize was clicked.")
1258
1259 #---------------------------------------------------------------------------
1260
1261 class MyMainPnl(wx.Panel):
1262 """
1263 ...
1264 """
1265 def __init__(self, parent, id, size):
1266 style = (wx.NO_BORDER | wx.TAB_TRAVERSAL)
1267 super(MyMainPnl, self).__init__(parent,
1268 id=int(ID_MAIN_PNL),
1269 size=size,
1270 style=style)
1271
1272 #------------
1273
1274 # Attributes.
1275 self.parent = parent
1276
1277 #------------
1278
1279 # Return config file.
1280 self.config = wx.GetApp().GetConfig()
1281 # Return bitmaps folder.
1282 self.bitmaps_dir = wx.GetApp().GetBitmapsDir()
1283
1284 #------------
1285
1286 # Colourdb.
1287 wx.lib.colourdb.updateColourDB()
1288
1289 # Create a colour list from the colourdb database.
1290 self.colour_list = wx.lib.colourdb.getColourList()
1291
1292 #------------
1293
1294 # Simplified init method.
1295 self.SetProperties()
1296 self.CreateCtrls()
1297 self.DoLayout()
1298 self.BindEvents()
1299
1300 #-----------------------------------------------------------------------
1301
1302 def SetProperties(self):
1303 """
1304 ...
1305 """
1306
1307 if wx.Platform == "__WXMSW__":
1308 self.SetDoubleBuffered(True)
1309
1310 # Read config file.
1311 self.SetBackgroundColour(self.config.Read("Color2"))
1312
1313
1314 def CreateCtrls(self):
1315 """
1316 ...
1317 """
1318
1319 w, h = self.GetClientSize()
1320 print("MyMainPnl :", self.GetClientSize())
1321
1322 #------------
1323
1324 font = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT)
1325 font.SetWeight(wx.BOLD)
1326
1327 #------------
1328 #------------
1329
1330 # Create data list control.
1331 self.list = MyListCtrlPnl(self)
1332
1333 #------------
1334 #------------
1335
1336 self.txt = wx.StaticText(self,
1337 -1,
1338 label="Hello World !")
1339 self.txt.SetForegroundColour("grey50")
1340 self.txt.SetFont(font)
1341
1342 #------------
1343 #------------
1344
1345 # Thanks to MCOW.
1346 # Buttons.
1347 self.btn1 = SBB.ShapedBitmapButton(self, ID_BTN_FULLSCREEN,
1348 bitmap=wx.Bitmap(os.path.join(self.bitmaps_dir,
1349 "adbRnd.png"),
1350 type=wx.BITMAP_TYPE_PNG),
1351
1352 pressedBmp=wx.Bitmap(os.path.join(self.bitmaps_dir,
1353 "adbRnd-pressed.png"),
1354 type=wx.BITMAP_TYPE_PNG),
1355
1356 hoverBmp=wx.Bitmap(os.path.join(self.bitmaps_dir,
1357 "adbRnd-hover.png"),
1358 type=wx.BITMAP_TYPE_PNG),
1359
1360 disabledBmp=wx.Bitmap(os.path.join(self.bitmaps_dir,
1361 "adbRnd-disabled.png"),
1362 type=wx.BITMAP_TYPE_PNG),
1363
1364 label="Fullscreen",
1365 labelForeColour=wx.WHITE,
1366 labelPosition=(35, 8),
1367 labelFont=wx.Font(9,
1368 wx.FONTFAMILY_DEFAULT,
1369 wx.FONTSTYLE_NORMAL,
1370 wx.FONTWEIGHT_BOLD),
1371 style=wx.BORDER_NONE)
1372
1373 self.btn1.SetFocus() # Necessary for Linux.
1374
1375 #------------
1376
1377 self.btn2 = SBB.ShapedBitmapButton(self, ID_BTN_ABOUT,
1378 bitmap=wx.Bitmap(os.path.join(self.bitmaps_dir,
1379 "adbRnd.png"),
1380 type=wx.BITMAP_TYPE_PNG),
1381
1382 pressedBmp=wx.Bitmap(os.path.join(self.bitmaps_dir,
1383 "adbRnd-pressed.png"),
1384 type=wx.BITMAP_TYPE_PNG),
1385
1386 hoverBmp=wx.Bitmap(os.path.join(self.bitmaps_dir,
1387 "adbRnd-hover.png"),
1388 type=wx.BITMAP_TYPE_PNG),
1389
1390 disabledBmp=wx.Bitmap(os.path.join(self.bitmaps_dir,
1391 "adbRnd-disabled.png"),
1392 type=wx.BITMAP_TYPE_PNG),
1393
1394 label="About",
1395 labelForeColour=wx.WHITE,
1396 labelPosition=(50, 8),
1397 labelFont=wx.Font(9,
1398 wx.FONTFAMILY_DEFAULT,
1399 wx.FONTSTYLE_NORMAL,
1400 wx.FONTWEIGHT_BOLD),
1401 style=wx.BORDER_NONE)
1402
1403 #------------
1404
1405 self.btn3 = SBB.ShapedBitmapButton(self, ID_BTN_QUIT,
1406 bitmap=wx.Bitmap(os.path.join(self.bitmaps_dir,
1407 "adbRnd.png"),
1408 type=wx.BITMAP_TYPE_PNG),
1409
1410 pressedBmp=wx.Bitmap(os.path.join(self.bitmaps_dir,
1411 "adbRnd-pressed.png"),
1412 type=wx.BITMAP_TYPE_PNG),
1413
1414 hoverBmp=wx.Bitmap(os.path.join(self.bitmaps_dir,
1415 "adbRnd-hover.png"),
1416 type=wx.BITMAP_TYPE_PNG),
1417
1418 disabledBmp=wx.Bitmap(os.path.join(self.bitmaps_dir,
1419 "adbRnd-disabled.png"),
1420 type=wx.BITMAP_TYPE_PNG),
1421
1422 label="Quit",
1423 labelForeColour=wx.WHITE,
1424 labelPosition=(50, 8),
1425 labelFont=wx.Font(9,
1426 wx.FONTFAMILY_DEFAULT,
1427 wx.FONTSTYLE_NORMAL,
1428 wx.FONTWEIGHT_BOLD),
1429 style=wx.BORDER_NONE)
1430
1431 #------------
1432 #------------
1433
1434 self.line = wx.StaticLine(self, -1,
1435 pos=(0, 70),
1436 size=(-1, -1),
1437 style=wx.LI_HORIZONTAL)
1438
1439 #------------
1440 #------------
1441
1442 # Create a choice widget.
1443 self.choice = wx.Choice(self,
1444 -1,
1445 pos=(-1, -1),
1446 size=(131, -1),
1447 choices=self.colour_list,
1448 style=wx.CB_SORT)
1449
1450 # Select item 163 in choice list to show.
1451 # self.choice.SetSelection(163) # GOLD
1452 # Read config file.
1453 self.choice.SetSelection(self.config.ReadInt("Item2"))
1454
1455 item = self.choice.GetCurrentSelection()
1456 bgcolour = self.choice.GetStringSelection()
1457 print("Item : %s , %s" % (item, bgcolour))
1458
1459
1460 def DoLayout(self):
1461 """
1462 ...
1463 """
1464
1465 txtSizer = wx.BoxSizer(wx.VERTICAL)
1466 txtSizer.Add(self.txt, 0, wx.LEFT, 13)
1467
1468 sizerList = wx.BoxSizer(wx.VERTICAL)
1469 sizerList.Add(self.list, 1,
1470 wx.LEFT|wx.TOP|wx.BOTTOM|
1471 wx.EXPAND|wx.ALIGN_TOP, 10)
1472
1473 sizer = wx.BoxSizer(wx.VERTICAL)
1474 sizer.Add(txtSizer, 0, wx.BOTTOM, 0)
1475 sizer.Add(self.btn1, 1, wx.ALL, 10)
1476 sizer.Add(self.btn2, 1, wx.ALL, 10)
1477 sizer.Add(self.line, 0, wx.EXPAND|wx.ALL, 10)
1478 sizer.Add(self.choice, 0, wx.ALL, 10)
1479 sizer.Add(self.btn3, 1, wx.ALL, 10)
1480
1481 mainSizer = wx.BoxSizer(wx.HORIZONTAL)
1482 mainSizer.Add(sizerList, 1,
1483 wx.LEFT|wx.TOP|wx.BOTTOM|wx.EXPAND, 5)
1484 mainSizer.Add(sizer, 0, wx.ALIGN_BOTTOM|wx.ALL, 5)
1485
1486 self.SetSizer(mainSizer)
1487 self.Layout()
1488
1489
1490 def BindEvents(self):
1491 """
1492 Bind some events to an events handler.
1493 """
1494
1495 self.choice.Bind(wx.EVT_CHOICE, self.OnChoice)
1496
1497 self.Bind(wx.EVT_ENTER_WINDOW, self.OnInfo, id=ID_MAIN_PNL)
1498 self.btn1.Bind(wx.EVT_ENTER_WINDOW, self.OnInfo, id=ID_BTN_FULLSCREEN)
1499 self.btn2.Bind(wx.EVT_ENTER_WINDOW, self.OnInfo, id=ID_BTN_ABOUT)
1500 self.btn3.Bind(wx.EVT_ENTER_WINDOW, self.OnInfo, id=ID_BTN_QUIT)
1501
1502 self.btn1.Bind(wx.EVT_BUTTON, self.OnFullScreen)
1503 self.btn2.Bind(wx.EVT_BUTTON, self.OnAbout)
1504 self.btn3.Bind(wx.EVT_BUTTON, self.OnBtnClose)
1505
1506
1507 def OnInfo(self, event):
1508 """
1509 ...
1510 """
1511
1512 event_id = event.GetId()
1513
1514 if event_id == ID_MAIN_PNL:
1515 self.GetParent().SetStatusText(text="Hello world !")
1516
1517 elif event_id == ID_BTN_FULLSCREEN:
1518 self.GetParent().SetStatusText(text="Fullscreen")
1519
1520 elif event_id == ID_BTN_ABOUT:
1521 self.GetParent().SetStatusText(text="About")
1522
1523 elif event_id == ID_BTN_QUIT:
1524 self.GetParent().SetStatusText(text="Quit the program")
1525
1526 else:
1527 # Tell the event system to continue
1528 # looking for an event handler, so the
1529 # default handler will get called.
1530 event.Skip()
1531
1532
1533 def OnChoice(self, event):
1534 """
1535 ...
1536 """
1537
1538 item = event.GetSelection()
1539 bgcolour = self.choice.GetStringSelection()
1540 print("New item : %s , %s" % (item, bgcolour))
1541
1542 # Change colour of the panel to the selected colour...
1543 self.SetBackgroundColour(bgcolour)
1544
1545 # Write config file.
1546 self.config.WriteInt("Item2", self.choice.GetCurrentSelection())
1547 self.config.Write("Color2", self.choice.GetStringSelection())
1548 self.config.Flush()
1549
1550 self.Refresh()
1551
1552 print("OnChoice button was clicked.")
1553
1554
1555 def OnAbout(self, event):
1556 """
1557 ...
1558 """
1559
1560 self.GetParent().OnAbout(self)
1561
1562 print("FullScreen button was clicked.")
1563
1564
1565 def OnFullScreen(self, event):
1566 """
1567 ...
1568 """
1569
1570 self.GetParent().OnFullScreen(self)
1571
1572 print("FullScreen button was clicked.")
1573
1574
1575 def OnBtnClose(self, event):
1576 """
1577 ...
1578 """
1579
1580 self.GetParent().OnCloseWindow(self)
1581
1582 print("Close button was clicked.")
1583
1584 #---------------------------------------------------------------------------
1585
1586 class MyFrame(wx.Frame):
1587 """
1588 Thanks to Robin Dunn.
1589 """
1590 def __init__(self):
1591 style = (wx.CLIP_CHILDREN | wx.CLOSE_BOX |
1592 wx.MINIMIZE_BOX | wx.SYSTEM_MENU |
1593 wx.BORDER_SIMPLE | wx.NO_FULL_REPAINT_ON_RESIZE|
1594 wx.STAY_ON_TOP)
1595 super(MyFrame, self).__init__(None,
1596 -1,
1597 title="",
1598 style=style)
1599
1600 #------------
1601
1602 wx.SystemOptions.SetOption("msw.remap", "0")
1603
1604 #------------
1605
1606 # Attributes.
1607 self.SetTransparent(0)
1608 self.opacity_in = 0
1609 self.opacity_out = 255
1610 self.deltaN = -70
1611 self.delta = wx.Point(0,0)
1612
1613 #------------
1614
1615 # Return config file.
1616 self.config = wx.GetApp().GetConfig()
1617 # Return application name.
1618 self.app_name = wx.GetApp().GetAppName()
1619 # Return bitmaps folder.
1620 self.bitmaps_dir = wx.GetApp().GetBitmapsDir()
1621 # Return icons folder.
1622 self.icons_dir = wx.GetApp().GetIconsDir()
1623
1624 #------------
1625
1626 # Colourdb.
1627 wx.lib.colourdb.updateColourDB()
1628
1629 # Create a colour list from the colourdb database.
1630 self.colour_list = wx.lib.colourdb.getColourList()
1631
1632 #------------
1633
1634 # Simplified init method.
1635 self.SetProperties()
1636 self.OnTimerIn(self)
1637 self.CreateMenu()
1638 self.CreateStatusBar()
1639 self.CreateCtrls()
1640 self.BindEvents()
1641 self.DoLayout()
1642
1643 #------------
1644
1645 try:
1646 self.tbicon = MyTaskBarIcon(self)
1647 except:
1648 self.tbicon = None
1649
1650 #------------
1651
1652 # Thanks to Ray Pasco.
1653 # Initialize to the current state.
1654 self.unrolledFrameClientSize_size = self.GetClientSize()
1655 self.isRolled = False
1656
1657 #------------
1658
1659 self.CenterOnScreen(wx.BOTH)
1660
1661 #------------
1662
1663 self.Show(True)
1664
1665 #-----------------------------------------------------------------------
1666
1667 def SetProperties(self):
1668 """
1669 ...
1670 """
1671
1672 self.SetTitle(self.app_name)
1673 self.SetClientSize((600, 380))
1674 self.SetMinSize((340, 83))
1675
1676 # Read config file.
1677 self.SetBackgroundColour(self.config.Read("Color2"))
1678
1679 #------------
1680
1681 frameIcon = wx.Icon(os.path.join(self.icons_dir,
1682 "icon_wxWidgets.ico"),
1683 type=wx.BITMAP_TYPE_ICO)
1684 self.SetIcon(frameIcon)
1685
1686
1687 def OnTimerIn(self, evt):
1688 """
1689 Thanks to Pascal Faut.
1690 """
1691
1692 self.timer1 = wx.Timer(self, -1)
1693 self.timer1.Start(1)
1694 self.Bind(wx.EVT_TIMER, self.AlphaCycle1, self.timer1)
1695
1696 print("Fade-in was launched.")
1697
1698
1699 def OnTimerOut(self, evt):
1700 """
1701 Thanks to Pascal Faut.
1702 """
1703
1704 self.timer2 = wx.Timer(self, -1)
1705 self.timer2.Start(1)
1706 self.Bind(wx.EVT_TIMER, self.AlphaCycle2, self.timer2)
1707
1708 print("Fade-out was launched.")
1709
1710
1711 def AlphaCycle1(self, *args):
1712 """
1713 Thanks to Pascal Faut.
1714 """
1715
1716 self.opacity_in += self.deltaN
1717 if self.opacity_in <= 0:
1718 self.deltaN = -self.deltaN
1719 self.opacity_in = 0
1720
1721 if self.opacity_in >= 255:
1722 self.deltaN = -self.deltaN
1723 self.opacity_in = 255
1724
1725 self.timer1.Stop()
1726
1727 self.SetTransparent(self.opacity_in)
1728
1729 print("Fade in = {}/255".format(self.opacity_in))
1730
1731
1732 def AlphaCycle2(self, *args):
1733 """
1734 Thanks to Pascal Faut.
1735 """
1736
1737 self.opacity_out += self.deltaN
1738 if self.opacity_out >= 255:
1739 self.deltaN = -self.deltaN
1740 self.opacity_out = 255
1741
1742 if self.opacity_out <= 0:
1743 self.deltaN = -self.deltaN
1744 self.opacity_out = 0
1745
1746 self.timer2.Stop()
1747 wx.CallAfter(self.Destroy)
1748 wx.Exit()
1749
1750 self.SetTransparent(self.opacity_out)
1751
1752 print("Fade out = {}/255".format(self.opacity_out))
1753
1754
1755 def CreateMenu(self):
1756 """
1757 ...
1758 """
1759
1760 # FlatMenuBar.
1761 self.menuBar = FM.FlatMenuBar(self,
1762 wx.ID_ANY,
1763 32, 4,
1764 options=FM.FM_OPT_IS_LCD)
1765
1766 # FM.StyleDefault or FM.Style2007
1767 # FM.StyleXP or FM.StyleVista
1768 self.newMyTheme = self.menuBar.GetRendererManager().AddRenderer(MyMenuRenderer())
1769 self.menuBar.GetRendererManager().SetTheme(self.newMyTheme)
1770
1771 #------------
1772
1773 # Set an icon to the exit/help menu item.
1774 exitImg = wx.Bitmap(os.path.join(self.bitmaps_dir,
1775 "item_exit.png"),
1776 type=wx.BITMAP_TYPE_PNG)
1777
1778 helpImg = wx.Bitmap(os.path.join(self.bitmaps_dir,
1779 "item_about.png"),
1780 type=wx.BITMAP_TYPE_PNG)
1781
1782 #------------
1783 #------------
1784
1785 # File Menu.
1786 self.file_menu = FM.FlatMenu()
1787
1788 # Create the menu items.
1789 item = FM.FlatMenuItem(self.file_menu,
1790 ID_FULLSCREEN,
1791 "&Fullscreen\tCtrl+F",
1792 "Fullscreen",
1793 wx.ITEM_NORMAL,
1794 None)
1795 item.SetTextColour("black")
1796 self.file_menu.AppendItem(item)
1797 self.file_menu.AppendSeparator()
1798
1799
1800 item = FM.FlatMenuItem(self.file_menu,
1801 wx.ID_EXIT,
1802 "&Quit\tCtrl+Q",
1803 "Quit the program",
1804 wx.ITEM_NORMAL,
1805 None,
1806 exitImg)
1807 # Demonstrate how to set custom font
1808 # and text colour to a FlatMenuItem.
1809 item.SetFont(wx.Font(-1, wx.FONTFAMILY_DEFAULT,
1810 wx.FONTSTYLE_NORMAL,
1811 wx.FONTWEIGHT_BOLD,
1812 False, ""))
1813 item.SetTextColour("#d34725")
1814
1815 self.file_menu.AppendItem(item)
1816
1817 #------------
1818
1819 # Add Create background bitmap.
1820 self.file_menu.SetBackgroundBitmap(CreateBackgroundBitmap())
1821
1822 #------------
1823 #------------
1824
1825 # Help Menu.
1826 self.help_menu = FM.FlatMenu()
1827
1828 # Create the menu items.
1829 item = FM.FlatMenuItem(self.help_menu,
1830 wx.ID_ABOUT,
1831 "&About\tCtrl+A",
1832 "About",
1833 wx.ITEM_NORMAL,
1834 None,
1835 helpImg)
1836 item.SetTextColour("black")
1837 self.help_menu.AppendItem(item)
1838 self.help_menu.AppendSeparator()
1839
1840 item = FM.FlatMenuItem(self.help_menu,
1841 ID_HELLO,
1842 "Hello !",
1843 "Hello !",
1844 wx.ITEM_NORMAL,
1845 None)
1846 item.SetTextColour("black")
1847 self.help_menu.AppendItem(item)
1848
1849 #------------
1850
1851 # Add Create background bitmap.
1852 self.help_menu.SetBackgroundBitmap(CreateBackgroundBitmap())
1853
1854 #------------
1855 #------------
1856
1857 # Menu background color.
1858 # Read config file.
1859 self.menuBar.SetBackgroundColour(wx.Colour(self.config.Read("Color2")))
1860
1861 # Add menu to the menu bar.
1862 self.menuBar.Append(self.file_menu, "&File")
1863 self.menuBar.Append(self.help_menu, "&Help")
1864
1865
1866 def CreateStatusBar(self):
1867 """
1868 ...
1869 """
1870
1871 self.sb = MyStatusBar(self, -1)
1872 # Read config file.
1873 self.sb.SetBackgroundColour(wx.Colour(self.config.Read("Color2")))
1874 self.sb.SetStatusText("Hello world !")
1875
1876 self.SetStatusBar(self.sb)
1877
1878
1879 def CreateCtrls(self):
1880 """
1881 ...
1882 """
1883
1884 w, h = self.GetClientSize()
1885 print("MyFrame :", self.GetClientSize())
1886
1887 #------------
1888 #------------
1889
1890 self.titleBarPnl = MyTitleBarPnl(self, -1, (w, 31))
1891 self.titleBarPnl.SetPosition((0, 0))
1892 print("self.titleBarPnl :", self.titleBarPnl.GetSize())
1893
1894 #------------
1895
1896 self.mainPnl = MyMainPnl(self, -1, (w, h-31))
1897 self.mainPnl.SetPosition((0, 31))
1898 print("self.mainPnl :", self.mainPnl.GetSize())
1899
1900
1901 def BindEvents(self):
1902 """
1903 ...
1904 """
1905
1906 self.titleBarPnl.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
1907
1908 self.Bind(wx.EVT_SIZE, self.OnResize)
1909 self.Bind(wx.EVT_LEFT_UP, self.OnLeftUp)
1910 self.Bind(wx.EVT_MOTION, self.OnMouseMove)
1911 self.Bind(wx.EVT_CHAR_HOOK, self.OnKeyUp)
1912 self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
1913
1914 self.Bind(wx.EVT_MENU, self.OnFullScreen, id=ID_FULLSCREEN)
1915 self.Bind(wx.EVT_MENU, self.OnAbout, id=wx.ID_ABOUT)
1916 self.Bind(wx.EVT_MENU, self.OnCloseWindow, id=wx.ID_EXIT)
1917
1918
1919 def DoLayout(self):
1920 """
1921 ...
1922 """
1923
1924 # MainSizer is the top-level one that manages everything.
1925 mainSizer = wx.BoxSizer(wx.VERTICAL)
1926
1927 mbSizer = wx.BoxSizer(wx.HORIZONTAL)
1928 mbSizer.Add(self.menuBar, 1, wx.ALL, 0)
1929
1930 mainSizer.Add(self.titleBarPnl, 1, wx.EXPAND, 0)
1931 mainSizer.Add(mbSizer, 0, wx.EXPAND, 0)
1932 mainSizer.Add(self.mainPnl, 1, wx.EXPAND, 0)
1933
1934 # Finally, tell the panel to use the sizer for layout.
1935 self.SetSizer(mainSizer)
1936 self.Layout()
1937
1938
1939 def OnRoll(self, event) :
1940 """
1941 Thanks to Ray Pasco.
1942 """
1943
1944 if not bool(self.isRolled) :
1945 # Set the flag to the state we want regardless of whether
1946 # or not it's in currently in the opposite state.
1947 self.RollUnRoll(wantToRoll=True)
1948
1949 elif self.isRolled : # UnRoll.
1950 # Set the flag to the state we want regardless of whether
1951 # or not it's in currently in the opposite state.
1952 self.RollUnRoll(wantToRoll=False)
1953
1954 print("OnRoll :", self.GetClientSize())
1955
1956
1957 def RollUnRoll(self, wantToRoll) :
1958 """
1959 Thanks to Ray Pasco.
1960 """
1961
1962 # Save the current size only if the Frame is not rolled up.
1963 if not bool(self.isRolled) :
1964 self.unrolledFrameClientSize_size = self.GetClientSize()
1965
1966 if bool(wantToRoll) : # UnRoll.
1967 # Set size (52).
1968 self.SetClientSize((self.unrolledFrameClientSize_size[0], 52))
1969 # Set to match this new state.
1970 self.isRolled = True
1971
1972 else : # Roll
1973 self.SetClientSize(self.unrolledFrameClientSize_size)
1974 # Set to match this new state.
1975 self.isRolled = False
1976
1977 print("RollUnRoll :", self.GetClientSize())
1978
1979
1980 def OnAbout(self, event):
1981 """
1982 ...
1983 """
1984
1985 self.dialog = MyAboutDlg(self)
1986
1987
1988 def OnLeftDown(self, event):
1989 """
1990 ...
1991 """
1992
1993 self.CaptureMouse()
1994 x, y = self.ClientToScreen(event.GetPosition())
1995 originx, originy = self.GetPosition()
1996 dx = x - originx
1997 dy = y - originy
1998 self.delta = ((dx, dy))
1999
2000
2001 def OnLeftUp(self, evt):
2002 """
2003 ...
2004 """
2005
2006 if self.HasCapture():
2007 self.ReleaseMouse()
2008
2009
2010 def OnMouseMove(self, event):
2011 """
2012 ...
2013 """
2014
2015 if event.Dragging() and event.LeftIsDown():
2016 x, y = self.ClientToScreen(event.GetPosition())
2017 fp = (x - self.delta[0], y - self.delta[1])
2018 self.Move(fp)
2019
2020
2021 def OnIconfiy(self, event):
2022 """
2023 ...
2024 """
2025
2026 self.Iconize()
2027
2028
2029 def OnResize(self, event):
2030 """
2031 ...
2032 """
2033
2034 w, h = self.GetClientSize()
2035 print("self :", self.GetClientSize())
2036
2037 #------------
2038 #------------
2039
2040 self.titleBarPnl.SetSize((w, 31))
2041 self.menuBar.SetSize((w, -1))
2042 self.mainPnl.SetSize((w, h-52))
2043
2044 #------------
2045
2046 self.Refresh()
2047
2048
2049 def OnFullScreen(self, event):
2050 """
2051 ...
2052 """
2053
2054 self.ShowFullScreen(not self.IsFullScreen(),
2055 wx.BORDER_SIMPLE)
2056
2057
2058 def OnKeyUp(self, event):
2059 """
2060 ...
2061 """
2062
2063 if event.GetKeyCode() == wx.WXK_ESCAPE:
2064 # Close the frame, no action.
2065 self.OnCloseWindow(event)
2066 event.Skip()
2067
2068
2069 def OnBtnClose(self, event):
2070 """
2071 ...
2072 """
2073
2074 self.Close()
2075
2076
2077 def OnCloseWindow(self, event):
2078 """
2079 Quit this application.
2080 """
2081
2082 if self.tbicon is not None:
2083 self.tbicon.Destroy()
2084
2085 #------------
2086
2087 # self.Destroy()
2088 self.OnTimerOut(self)
2089
2090 print("Exit application.")
2091
2092 #---------------------------------------------------------------------------
2093
2094 class MyApp(wx.App):
2095 """
2096 Thanks to Andrea Gavana.
2097 """
2098 def OnInit(self):
2099
2100 #------------
2101
2102 self.locale = wx.Locale(wx.LANGUAGE_ENGLISH)
2103
2104 #------------
2105
2106 self.SetAppName("Custom Gui 2")
2107
2108 #------------
2109
2110 self.installDir = os.path.split(os.path.abspath(sys.argv[0]))[0]
2111
2112 #------------
2113
2114 frame = MyFrame()
2115 self.SetTopWindow(frame)
2116 frame.Show(True)
2117 return True
2118
2119 #-----------------------------------------------------------------------
2120
2121 def GetInstallDir(self):
2122 """
2123 Returns the installation directory for my application.
2124 """
2125
2126 return self.installDir
2127
2128
2129 def GetIconsDir(self):
2130 """
2131 Returns the icons directory for my application.
2132 """
2133
2134 icons_dir = os.path.join(self.installDir, "icons")
2135 return icons_dir
2136
2137
2138 def GetBitmapsDir(self):
2139 """
2140 Returns the bitmaps directory for my application.
2141 """
2142
2143 bitmaps_dir = os.path.join(self.installDir, "bitmaps")
2144 return bitmaps_dir
2145
2146
2147 def GetConfig(self):
2148 """
2149 Returns the config file for my application.
2150 """
2151
2152 config = wx.FileConfig(appName="Custom Gui",
2153 localFilename=os.path.join(self.installDir,
2154 "options"))
2155 return config
2156
2157 #---------------------------------------------------------------------------
2158
2159 def main():
2160 app = MyApp(False)
2161 app.MainLoop()
2162
2163 #---------------------------------------------------------------------------
2164
2165 if __name__ == "__main__" :
2166 main()
Third example
1 # sample_three.py
2
3 import sys
4 import os
5 import platform
6 import wx
7 import wx.lib.fancytext as fancytext
8 import wx.lib.agw.flatmenu as FM
9 from wx.lib.agw.artmanager import ArtManager, RendererBase, DCSaver
10 from wx.lib.agw.fmresources import ControlFocus, ControlPressed
11 import wx.dataview as dv
12 import wx.lib.colourdb
13 import rndshapedbitmapbutton as SBB
14 import data
15
16 musicdata = sorted(data.musicdata.items())
17 musicdata = [[str(k)] + list(v) for k,v in musicdata]
18
19 # def SwitchRGBtoBGR
20 # def CreateBackgroundBitmap
21 # class MyMenuRenderer
22 # class MyListCtrlPnl
23 # class MyStatusBar
24 # class MyTitleBar
25 # class MyAboutDlg
26 # class MyPopupMenu
27 # class MyWindow
28 # class MyButtonBox
29 # class MyTitleBarPnl
30 # class MyMainPnl
31 # class MyFrame
32 # class MyApp
33
34 ID_FULLSCREEN = wx.NewIdRef()
35 ID_MAIN_PNL = wx.NewIdRef()
36 ID_BTN_FULLSCREEN = wx.NewIdRef()
37 ID_BTN_ABOUT = wx.NewIdRef()
38 ID_BTN_QUIT = wx.NewIdRef()
39 ID_HELLO = wx.NewIdRef()
40
41 #---------------------------------------------------------------------------
42
43 def SwitchRGBtoBGR(colour):
44 """
45 ...
46 """
47
48 return wx.Colour(colour.Blue(), colour.Green(), colour.Red())
49
50 #---------------------------------------------------------------------------
51
52 def CreateBackgroundBitmap():
53 """
54 ...
55 """
56
57 mem_dc = wx.MemoryDC()
58 bmp = wx.Bitmap(121, 300)
59 mem_dc.SelectObject(bmp)
60
61 mem_dc.Clear()
62
63 # Colour the menu face with background colour.
64 mem_dc.SetPen(wx.Pen("#a0a0a0", 0))
65 mem_dc.SetBrush(wx.Brush("#fba12b"))
66 mem_dc.DrawRectangle(0, 15, 121, 300)
67 mem_dc.DrawLine(0, 0, 300, 0)
68
69 mem_dc.SelectObject(wx.NullBitmap)
70 return bmp
71
72 #---------------------------------------------------------------------------
73
74 class MyMenuRenderer(FM.FMRenderer):
75 """
76 Thanks to Andrea Gavana.
77 A custom renderer class for FlatMenu.
78 """
79 def __init__(self):
80 FM.FMRenderer.__init__(self)
81
82 #-----------------------------------------------------------------------
83
84 def DrawMenuButton(self, dc, rect, state):
85 """
86 Draws the highlight on a FlatMenu.
87 """
88
89 self.DrawButton(dc, rect, state)
90
91
92 def DrawMenuBarButton(self, dc, rect, state):
93 """
94 Draws the highlight on a FlatMenuBar.
95 """
96
97 self.DrawButton(dc, rect, state)
98
99
100 def DrawButton(self, dc, rect, state, colour=None):
101 """
102 ...
103 """
104
105 if state == ControlFocus:
106 penColour = SwitchRGBtoBGR(ArtManager.Get().FrameColour())
107 brushColour = SwitchRGBtoBGR(ArtManager.Get().BackgroundColour())
108 elif state == ControlPressed:
109 penColour = SwitchRGBtoBGR(ArtManager.Get().FrameColour())
110 brushColour = SwitchRGBtoBGR(ArtManager.Get().HighlightBackgroundColour())
111 else: # ControlNormal, ControlDisabled, default.
112 penColour = SwitchRGBtoBGR(ArtManager.Get().FrameColour())
113 brushColour = SwitchRGBtoBGR(ArtManager.Get().BackgroundColour())
114
115 # Draw the button borders.
116 dc = wx.GCDC(dc)
117 dc.SetPen(wx.Pen(penColour))
118 dc.SetBrush(wx.Brush(brushColour))
119 dc.DrawRoundedRectangle(rect.x, rect.y, rect.width, rect.height-3, 4)
120
121
122 def DrawMenuBarBackground(self, dc, rect):
123 """
124 ...
125 """
126
127 # For office style, we simple draw a rectangle
128 # with a gradient colouring.
129 vertical = ArtManager.Get().GetMBVerticalGradient()
130
131 dcsaver = DCSaver(dc)
132
133 # Fill with gradient.
134 startColour = self.menuBarFaceColour
135 endColour = ArtManager.Get().LightColour(startColour, 0)
136
137 dc.SetPen(wx.Pen(endColour))
138 dc.SetBrush(wx.Brush(endColour))
139 dc.DrawRectangle(rect)
140
141
142 def DrawToolBarBg(self, dc, rect):
143 """
144 ...
145 """
146
147 if not ArtManager.Get().GetRaiseToolbar():
148 return
149
150 # Fill with gradient.
151 startColour = self.menuBarFaceColour()
152 dc.SetPen(wx.Pen(startColour))
153 dc.SetBrush(wx.Brush(startColour))
154 dc.DrawRectangle(0, 0, rect.GetWidth(), rect.GetHeight())
155
156 #---------------------------------------------------------------------------
157
158 class MyListCtrlPnl(wx.Panel):
159 """
160 Thanks to Robin Dunn.
161 """
162 def __init__(self, parent):
163 wx.Panel.__init__(self, parent, -1)
164
165 # Create the listctrl.
166 self.dvlc = dv.DataViewListCtrl(self,
167 style=wx.BORDER_SIMPLE|
168 dv.DV_ROW_LINES|
169 dv.DV_HORIZ_RULES|
170 dv.DV_VERT_RULES)
171
172 # Give it some columns.
173 # The ID col we'll customize a bit :
174 self.dvlc.AppendTextColumn("Id", width=40)
175 self.dvlc.AppendTextColumn("Artist", width=170)
176 self.dvlc.AppendTextColumn("Title", width=260)
177 self.dvlc.AppendTextColumn("Genre", width=80)
178
179 # Load the data. Each item (row) is added as a sequence
180 # of values whose order matches the columns.
181 for itemvalues in musicdata:
182 self.dvlc.AppendItem(itemvalues)
183
184 self.dvlc.SetBackgroundColour("#96cdf7")
185 self.dvlc.SetForegroundColour("#626262")
186
187 # Set the layout so the listctrl fills the panel.
188 self.Sizer = wx.BoxSizer()
189 self.Sizer.Add(self.dvlc, 1, wx.EXPAND)
190
191 #---------------------------------------------------------------------------
192
193 class MyStatusBar(wx.StatusBar) :
194 """
195 Thanks to ???.
196 Simple custom colorized StatusBar.
197 """
198 def __init__(self, parent, id) :
199 wx.StatusBar.__init__(self, parent, id)
200
201 #------------
202
203 # Return config file.
204 self.config = wx.GetApp().GetConfig()
205
206 #------------
207
208 # Simplified init method.
209 self.SetProperties()
210 self.BindEvents()
211
212 #-----------------------------------------------------------------------
213
214 def SetProperties(self):
215 """
216 ...
217 """
218
219 # Read config file.
220 self.SetBackgroundColour(wx.Colour(self.config.Read("Color3")))
221
222
223 def BindEvents(self):
224 """
225 Bind some events to an events handler.
226 """
227
228 self.Bind(wx.EVT_PAINT, self.OnPaint)
229
230
231 def OnPaint(self, event) :
232 """
233 ...
234 """
235
236 dc = wx.BufferedPaintDC(self)
237 self.Draw(dc)
238
239
240 def Draw(self, dc) :
241 """
242 ...
243 """
244
245 # Read config file.
246 dc.SetBackground(wx.Brush(self.config.Read("Color3")))
247 dc.Clear()
248
249 textVertOffset = 3 # Perfect for MSW.
250 textHorzOffset = 5 # Arbitrary - looks nicer.
251
252 dc.SetTextForeground(wx.BLACK)
253 dc.DrawText(self.GetStatusText(), textHorzOffset, textVertOffset)
254
255 #---------------------------------------------------------------------------
256
257 class MyTitleBar(wx.Control):
258 """
259 Thanks to Cody Precord.
260 """
261 def __init__(self, parent, label, size):
262 style = (wx.NO_BORDER)
263 super(MyTitleBar, self).__init__(parent,
264 style=style)
265
266 #------------
267
268 # Return config file.
269 self.config = wx.GetApp().GetConfig()
270
271 #------------
272
273 # Attributes.
274 self.parent = parent
275 self.label = label
276 self.size = size
277
278 #------------
279
280 # Simplified init method.
281 self.SetBackground()
282 self.SetProperties(label, size)
283 self.BindEvents()
284
285 #-----------------------------------------------------------------------
286
287 def SetBackground(self):
288 """
289 ...
290 """
291
292 self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM)
293
294
295 def SetProperties(self, label, size):
296 """
297 ...
298 """
299
300 self.label = label
301 self.size = size
302
303 self.label_font = self.GetFont()
304 self.label_font.SetFamily(wx.SWISS)
305 self.label_font.SetPointSize(size)
306 self.label_font.SetWeight(wx.BOLD)
307 self.SetFont(self.label_font)
308
309
310 def BindEvents(self):
311 """
312 Bind some events to an events handler.
313 """
314
315 self.Bind(wx.EVT_PAINT, self.OnPaint)
316 self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
317 self.Bind(wx.EVT_LEFT_UP, self.OnLeftUp)
318
319
320 def OnLeftDown(self, event):
321 """
322 ...
323 """
324
325 self.GetTopLevelParent().OnLeftDown(event)
326
327
328 def OnLeftUp(self, event):
329 """
330 ...
331 """
332
333 self.GetTopLevelParent().OnLeftUp(event)
334
335
336 def SetLabel(self, label):
337 """
338 ...
339 """
340
341 self.label = label
342 self.Refresh()
343
344
345 def DoGetBestSize(self):
346 """
347 ...
348 """
349
350 dc = wx.ClientDC(self)
351 dc.SetFont(self.GetFont())
352
353 textWidth, textHeight = dc.GetTextExtent(self.label)
354 spacing = 10
355 totalWidth = textWidth + (spacing)
356 totalHeight = textHeight + (spacing)
357
358 best = wx.Size(totalWidth, totalHeight)
359 self.CacheBestSize(best)
360
361 return best
362
363
364 def GetLabel(self):
365 """
366 ...
367 """
368
369 return self.label
370
371
372 def GetLabelColor(self):
373 """
374 ...
375 """
376
377 return self.foreground
378
379
380 def GetLabelSize(self):
381 """
382 ...
383 """
384
385 return self.size
386
387
388 def SetLabelColour(self, colour):
389 """
390 ...
391 """
392
393 self.labelColour = colour
394
395
396 def OnPaint(self, event):
397 """
398 ...
399 """
400
401 dc = wx.BufferedPaintDC(self)
402 gcdc = wx.GCDC(dc)
403
404 gcdc.Clear()
405
406 # Setup the GraphicsContext.
407 gc = gcdc.GetGraphicsContext()
408
409 # Get the working size we can draw in.
410 width, height = self.GetSize()
411
412 # Use the GCDC to draw the text.
413 # Read config file.
414 brush = self.config.Read("Color3")
415 gcdc.SetPen(wx.Pen(brush, 1))
416 gcdc.SetBrush(wx.Brush(brush))
417 gcdc.DrawRectangle(0, 0, width, height)
418
419 # Get the system font.
420 gcdc.SetFont(self.GetFont())
421
422 textWidth, textHeight = gcdc.GetTextExtent(self.label)
423 tposx, tposy = ((width/2)-(textWidth/2), (height/3)-(textHeight/3))
424
425 tposx += 0
426 tposy += 4
427
428 # Set position and text color.
429 gcdc.SetTextForeground("black")
430 gcdc.DrawText(self.label, int(tposx), int(tposy+1))
431
432 gcdc.SetTextForeground(self.labelColour)
433 gcdc.DrawText(self.label, int(tposx), int(tposy))
434
435 #---------------------------------------------------------------------------
436
437 class MyAboutDlg(wx.Frame):
438 """
439 Thanks to Robin Dunn.
440 """
441 def __init__(self, parent):
442 style = (wx.FRAME_SHAPED | wx.NO_BORDER |
443 wx.CLIP_CHILDREN | wx.STAY_ON_TOP |
444 wx.SYSTEM_MENU | wx.CLOSE_BOX |
445 wx.NO_FULL_REPAINT_ON_RESIZE)
446 wx.Frame.__init__(self,
447 parent,
448 id=-1,
449 title="About...",
450 style=style)
451
452 #------------
453
454 # Attributes.
455 self.SetTransparent(0)
456 self.opacity_in = 0
457 self.opacity_out = 255
458 self.deltaN = -70
459 self.hasShape = False
460 self.delta = wx.Point(0,0)
461
462 #------------
463
464 # Return config file.
465 self.config = wx.GetApp().GetConfig()
466 # Return application name.
467 self.app_name = wx.GetApp().GetAppName()
468 # Return bitmaps folder.
469 self.bitmaps_dir = wx.GetApp().GetBitmapsDir()
470 # Return icons folder.
471 self.icons_dir = wx.GetApp().GetIconsDir()
472
473 #------------
474
475 # Simplified init method.
476 self.SetProperties()
477 self.OnTimerIn(self)
478 self.CreateCtrls()
479 self.BindEvents()
480
481 #------------
482
483 self.CenterOnParent(wx.BOTH)
484 self.GetParent().Enable(False)
485
486 #------------
487
488 self.Show(True)
489
490 #------------
491
492 self.eventLoop = wx.GUIEventLoop()
493 self.eventLoop.Run()
494
495 #-----------------------------------------------------------------------
496
497 def SetProperties(self):
498 """
499 Set the dialog properties (title, icon, transparency...).
500 """
501
502 frameIcon = wx.Icon(os.path.join(self.icons_dir,
503 "icon_wxWidgets.ico"),
504 type=wx.BITMAP_TYPE_ICO)
505 self.SetIcon(frameIcon)
506
507
508 def OnTimerIn(self, evt):
509 """
510 Thanks to Pascal Faut.
511 """
512
513 self.timer1 = wx.Timer(self, -1)
514 self.timer1.Start(1)
515 self.Bind(wx.EVT_TIMER, self.AlphaCycle1, self.timer1)
516
517 print("Fade-in was launched.")
518
519
520 def OnTimerOut(self, evt):
521 """
522 Thanks to Pascal Faut.
523 """
524
525 self.timer2 = wx.Timer(self, -1)
526 self.timer2.Start(1)
527 self.Bind(wx.EVT_TIMER, self.AlphaCycle2, self.timer2)
528
529 print("Fade-out was launched.")
530
531
532 def AlphaCycle1(self, *args):
533 """
534 Thanks to Pascal Faut.
535 """
536
537 self.opacity_in += self.deltaN
538 if self.opacity_in <= 0:
539 self.deltaN = -self.deltaN
540 self.opacity_in = 0
541
542 if self.opacity_in >= 255:
543 self.deltaN = -self.deltaN
544 self.opacity_in = 255
545
546 self.timer1.Stop()
547
548 self.SetTransparent(self.opacity_in)
549
550 print("Fade in = {}/255".format(self.opacity_in))
551
552
553 def AlphaCycle2(self, *args):
554 """
555 Thanks to Pascal Faut.
556 """
557
558 self.opacity_out += self.deltaN
559 if self.opacity_out >= 255:
560 self.deltaN = -self.deltaN
561 self.opacity_out = 255
562
563 if self.opacity_out <= 0:
564 self.deltaN = -self.deltaN
565 self.opacity_out = 0
566
567 self.timer2.Stop()
568
569 wx.CallAfter(self.Destroy)
570
571 self.SetTransparent(self.opacity_out)
572
573 print("Fade out = {}/255".format(self.opacity_out))
574
575
576 def CreateCtrls(self):
577 """
578 Make widgets for my dialog.
579 """
580
581 # Load a background bitmap.
582 self.bmp = wx.Bitmap(os.path.join(self.bitmaps_dir,
583 "skin_about_3.png"),
584 type=wx.BITMAP_TYPE_PNG)
585 mask = wx.Mask(self.bmp, wx.RED)
586 self.bmp.SetMask(mask)
587
588 #------------
589
590 self.SetClientSize((self.bmp.GetWidth(), self.bmp.GetHeight()))
591
592 #------------
593
594 if wx.Platform == "__WXGTK__":
595 # wxGTK requires that the window be created before you can
596 # set its shape, so delay the call to SetWindowShape until
597 # this event.
598 self.Bind(wx.EVT_WINDOW_CREATE, self.SetWindowShape)
599 else:
600 # On wxMSW and wxMac the window has already
601 # been created, so go for it.
602 self.SetWindowShape()
603
604
605 def BindEvents(self):
606 """
607 Bind all the events related to my dialog.
608 """
609
610 # Bind some events to an events handler.
611 self.Bind(wx.EVT_LEFT_UP, self.OnLeftUp)
612 self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
613 self.Bind(wx.EVT_RIGHT_UP, self.OnCloseWindow) # Panel right clic.
614 self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)
615 self.Bind(wx.EVT_WINDOW_CREATE, self.SetWindowShape)
616 self.Bind(wx.EVT_MOTION, self.OnMouseMove)
617 self.Bind(wx.EVT_KEY_UP, self.OnKeyUp)
618 self.Bind(wx.EVT_PAINT, self.OnPaint)
619 self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
620
621
622 def SetWindowShape(self, event=None):
623 """
624 ...
625 """
626
627 # Use the bitmap's mask to determine the region.
628 r = wx.Region(self.bmp)
629 self.hasShape = self.SetShape(r)
630
631
632 def OnEraseBackground(self, event):
633 """
634 ...
635 """
636
637 dc = event.GetDC()
638 if not dc:
639 dc = wx.ClientDC(self)
640 rect = self.GetUpdateRegion().GetBox()
641 dc.SetClippingRect(rect)
642
643
644 def OnLeftDown(self, event):
645 """
646 ...
647 """
648
649 self.CaptureMouse()
650 x, y = self.ClientToScreen(event.GetPosition())
651 originx, originy = self.GetPosition()
652 dx = x - originx
653 dy = y - originy
654 self.delta = ((dx, dy))
655
656
657 def OnLeftUp(self, evt):
658 """
659 ...
660 """
661
662 if self.HasCapture():
663 self.ReleaseMouse()
664
665
666 def OnMouseMove(self, event):
667 """
668 ...
669 """
670
671 if event.Dragging() and event.LeftIsDown():
672 x, y = self.ClientToScreen(event.GetPosition())
673 fp = (x - self.delta[0], y - self.delta[1])
674 self.Move(fp)
675
676
677 def OnPaint(self, event):
678 """
679 ...
680 """
681
682 dc = wx.AutoBufferedPaintDCFactory(self)
683 dc.SetBackground(wx.Brush("BLACK"))
684 dc.Clear()
685
686 #------------
687
688 # These are strings.
689 py_version = sys.version.split()[0]
690
691 str1 = (('<font style="normal" family="default" color="yellow" size="10" weight="bold">'
692 'Programming : </font>'
693 '<font style="normal" family="default" color="white" size="10" weight="normal">'
694 'Python {}</font>').format(py_version)) # Python 3.7.2
695
696 str2 = (('<font style="normal" family="default" color="orange" size="10" weight="bold">'
697 'GUI toolkit : </font>'
698 '<font style="normal" family="default" color="white" size="10" weight="normal">'
699 'wxPython {}</font>').format(wx.VERSION_STRING)) # wxPython 4.0.4
700
701 str3 = (('<font style="normal" family="default" color="brown" size="10" weight="bold">'
702 'Library : </font>'
703 '<font style="normal" family="default" color="white" size="10" weight="normal">'
704 '{}</font>').format(wx.GetLibraryVersionInfo().VersionString)) # wxWidgets 3.0.5
705
706 str4 = (('<font style="normal" family="default" color="cyan" size="10" weight="bold">'
707 'Operating system : </font>'
708 '<font style="normal" family="default" color="white" size="10" weight="normal">'
709 '{}</font>').format(platform.system())) # Windows
710
711 str5 = (('<font style="normal" family="default" color="white" size="9" weight="normal">'
712 '{}</font>').format(self.app_name)) # Custom Gui
713
714 str6 = (('<font style="normal" family="default" color="black" size="8" weight="normal">'
715 'Right clic or Esc for Exit</font>'))
716
717 #------------
718
719 # Get the working size we can draw in.
720 bw, bh = self.GetSize()
721
722 # Use the GCDC to draw the text.
723 # Draw the about borders.
724 dc.SetPen(wx.Pen(wx.Colour(148, 195, 240), 20))
725 # Read config file.
726 dc.SetBrush(wx.Brush(self.config.Read("Color3")))
727 # or
728 # dc.SetBrush(wx.Brush(wx.TRANSPARENT_BRUSH))
729 dc.DrawRectangle(0, 0, bw, bh)
730
731 #------------
732
733 # Draw text.
734 # Need width to calculate x position of str1.
735 tw, th = fancytext.GetExtent(str1, dc)
736 # Centered text.
737 fancytext.RenderToDC(str1, dc, (bw-tw)/2, 30)
738
739 #------------
740
741 # Need width to calculate x position of str2.
742 tw, th = fancytext.GetExtent(str2, dc)
743 # Centered text.
744 fancytext.RenderToDC(str2, dc, (bw-tw)/2, 50)
745
746 #------------
747
748 # Need width to calculate x position of str3.
749 tw, th = fancytext.GetExtent(str3, dc)
750 # Centered text.
751 fancytext.RenderToDC(str3, dc, (bw-tw)/2, 70)
752
753 #------------
754
755 # Need width to calculate x position of str4.
756 tw, th = fancytext.GetExtent(str4, dc)
757 # Centered text.
758 fancytext.RenderToDC(str4, dc, (bw-tw)/2, 90)
759
760 #------------
761
762 # Need width to calculate x position of str5.
763 tw, th = fancytext.GetExtent(str5, dc)
764 # Centered text.
765 fancytext.RenderToDC(str5, dc, (bw-tw)/2, 130)
766
767 #------------
768
769 # Need width to calculate x position of str6.
770 tw, th = fancytext.GetExtent(str6, dc)
771 # Centered text.
772 fancytext.RenderToDC(str6, dc, (bw-tw)/2, 190)
773
774
775 def OnKeyUp(self, event):
776 """
777 ...
778 """
779
780 if event.GetKeyCode() == wx.WXK_ESCAPE:
781 self.OnCloseWindow(event)
782
783 event.Skip()
784
785
786 def OnCloseWindow(self, event):
787 """
788 ...
789 """
790
791 self.GetParent().Enable(True)
792 self.eventLoop.Exit()
793 self.Destroy()
794
795 #---------------------------------------------------------------------------
796
797 class MyPopupMenu(wx.Menu):
798 """
799 Thanks to Robin Dunn.
800 """
801 def __init__(self, parent):
802 wx.Menu.__init__(self)
803
804 #------------
805
806 # Attributes.
807 self.parent = parent
808
809 #------------
810
811 # Returns bitmaps folder.
812 self.bitmaps_dir = wx.GetApp().GetBitmapsDir()
813
814 #------------
815
816 # Simplified init method.
817 self.CreatePopupMenu()
818 self.BindEvents()
819
820 #-----------------------------------------------------------------------
821
822 def CreatePopupMenu(self, event=None):
823 """
824 This method is called by the base class when it needs to popup
825 the menu for the default EVT_RIGHT_DOWN event. Just create
826 the menu how you want it and return it from this function,
827 the base class takes care of the rest.
828 """
829
830 bmp = wx.Bitmap(os.path.join(self.bitmaps_dir,
831 "item_about.png"),
832 type=wx.BITMAP_TYPE_PNG)
833
834 item = wx.MenuItem(self, id=wx.ID_ABOUT, text=" About")
835 item.SetBitmap(bmp)
836 self.Append(item)
837 self.AppendSeparator()
838
839 #------------
840
841 bmp = wx.Bitmap(os.path.join(self.bitmaps_dir,
842 "item_exit.png"),
843 type=wx.BITMAP_TYPE_PNG)
844
845 if True or "__WXMSW__" in wx.PlatformInfo:
846 font = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT)
847 font.SetWeight(wx.BOLD)
848
849 item = wx.MenuItem(self, id=wx.ID_EXIT, text=" Exit")
850 item.SetBitmap(bmp)
851 item.SetFont(font)
852 self.Append(item)
853
854 return self
855
856
857 def BindEvents(self):
858 """
859 Bind some events to an events handler.
860 """
861
862 # Bind the menu events to an events handler.
863 self.Bind(wx.EVT_MENU, self.OnAbout, id=wx.ID_ABOUT)
864 self.Bind(wx.EVT_MENU, self.OnClose, id=wx.ID_EXIT)
865
866
867 def OnAbout(self, event):
868 """
869 ...
870 """
871
872 self.mainFrame = wx.GetApp().GetTopWindow()
873 self.mainFrame.OnAbout(self)
874
875 print("About icon was clicked.")
876
877
878 def OnClose(self, event):
879 """
880 ...
881 """
882
883 self.mainFrame = wx.GetApp().GetTopWindow()
884 self.mainFrame.OnCloseWindow(self)
885
886 print("Close icon was clicked.")
887
888 #---------------------------------------------------------------------------
889
890 class MyWindow(wx.Control):
891 """
892 Thanks to Cody Precord.
893 """
894 def __init__(self, parent, label,
895 foreground, background,
896 normal, pressed=None):
897 style = (wx.BORDER_NONE)
898 super(MyWindow, self).__init__(parent,
899 -1,
900 style=style)
901
902 #------------
903
904 # Attributes.
905 self.label = label
906 self.foreground = foreground
907 self.background = background
908
909 if wx.Platform == "__WXGTK__":
910 self.color = "#9e9d9d"
911
912 else:
913 self.color = "#b1b1b0"
914
915 self.normal = normal
916 self.pressed = pressed
917
918 self._clicked = False
919
920 #------------
921
922 self.region = wx.Region(normal, wx.Colour(0, 0, 0, 0))
923
924 #------------
925
926 # Simplified init method.
927 self.SetProperties(label, foreground, background)
928 self.BindEvents()
929
930 #-----------------------------------------------------------------------
931
932 def SetProperties(self, label, foreground, background):
933 """
934 ...
935 """
936
937 self.label = label
938 self.foreground = foreground
939 self.background = background
940 self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM)
941
942
943 def BindEvents(self):
944 """
945 Bind some events to an events handler.
946 """
947
948 self.Bind(wx.EVT_SET_FOCUS, self.OnSetFocus)
949 self.Bind(wx.EVT_KILL_FOCUS, self.OnKillFocus)
950 self.Bind(wx.EVT_PAINT, self.OnPaint)
951 self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
952 self.Bind(wx.EVT_LEFT_DCLICK, self.OnLeftDclick)
953 self.Bind(wx.EVT_LEFT_UP, self.OnLeftUp)
954 self.Bind(wx.EVT_MOTION, self.OnMotion)
955 self.Bind(wx.EVT_LEAVE_WINDOW, self.OnLeave)
956 self.Bind(wx.EVT_KEY_UP, self.OnKeyUp)
957
958
959 def SetLabel(self, label):
960 """
961 ...
962 """
963
964 self.label = label
965 self.Refresh()
966
967
968 def DoGetBestSize(self):
969 """
970 ...
971 """
972
973 return self.normal.GetSize()
974
975
976 def GetLabel(self):
977 """
978 ...
979 """
980
981 return self.label
982
983
984 def GetLabelColor(self):
985 """
986 ...
987 """
988
989 return self.foreground
990
991
992 def Enable(self, *args, **kwargs):
993 """
994 ...
995 """
996
997 super(MyWindow, self).Enable(*args, **kwargs)
998 self.Refresh()
999
1000
1001 def Disable(self, *args, **kwargs):
1002 """
1003 ...
1004 """
1005
1006 super(MyWindow, self).Disable(*args, **kwargs)
1007 self.Refresh()
1008
1009
1010 def PostEvent(self):
1011 """
1012 ...
1013 """
1014
1015 event = wx.CommandEvent()
1016 event.SetEventObject(self)
1017 event.SetEventType(wx.EVT_BUTTON.typeId)
1018 wx.PostEvent(self, event)
1019
1020
1021 def OnSize(self, event):
1022 """
1023 ...
1024 """
1025
1026 event.Skip()
1027 self.Refresh()
1028
1029
1030 def GetBackground(self):
1031 """
1032 ...
1033 """
1034
1035 return self.background
1036
1037
1038 def OnPaint(self, event):
1039 """
1040 ...
1041 """
1042
1043 dc = wx.BufferedPaintDC(self)
1044 dc.Clear()
1045 gcdc = wx.GCDC(dc)
1046
1047 # Set the background color.
1048 if wx.Platform == "__WXGTK__":
1049 gcdc.SetBackground(wx.Brush("grey55"))
1050 else:
1051 gcdc.SetBackground(wx.Brush(self.background))
1052
1053 gcdc.Clear()
1054
1055 # Get the working rectangle we can draw in.
1056 rect = self.GetClientRect()
1057
1058 # Font size and style.
1059 fontSize = self.GetFont().GetPointSize()
1060
1061 if wx.Platform == "__WXGTK__":
1062 boldFont = wx.Font(fontSize-1, wx.DEFAULT,
1063 wx.NORMAL, wx.NORMAL, False, "")
1064 else:
1065 boldFont = wx.Font(fontSize+2, wx.DEFAULT,
1066 wx.NORMAL, wx.BOLD, False, "")
1067
1068 if wx.Platform == "__WXMSW__":
1069 pen = wx.Pen(self.color, 1, wx.SOLID)
1070
1071 else:
1072 pen = wx.Pen(self.color, 1, wx.SOLID)
1073
1074 gcdc.SetPen(pen)
1075
1076 x, y = self.GetSize()
1077 # x, y , width, height, radius
1078 gcdc.DrawRoundedRectangle (0, 0, 71, 25, 3)
1079
1080 bitmap = self.normal
1081
1082 w, h = bitmap.GetWidth(), bitmap.GetHeight()
1083
1084 if self.clicked:
1085 bitmap = self.pressed or bitmap
1086 if not self.IsEnabled():
1087 bitmap = self.normal or bitmap
1088
1089 # Draw a bitmap with an alpha channel.
1090 # image, x, y, transparency.
1091 dc.DrawBitmap(bitmap, 0, 0, True)
1092
1093 if wx.Platform == "__WXGTK__":
1094 # Add the Caption.
1095 # White text - Shadow.
1096 rect = wx.Rect(rect.x, rect.y+3,
1097 rect.width, 22)
1098
1099 else:
1100 rect = wx.Rect(rect.x, rect.y+3,
1101 rect.width, 20)
1102
1103 dc.SetFont(boldFont)
1104 dc.SetTextForeground(wx.WHITE)
1105 dc.DrawLabel(self.label, rect, wx.ALIGN_CENTER)
1106
1107 if wx.Platform == "__WXGTK__":
1108 # Add the Caption.
1109 # Black text.
1110 rect = wx.Rect(rect.x, rect.y,
1111 rect.width, 21)
1112
1113 else:
1114 rect = wx.Rect(rect.x, rect.y,
1115 rect.width, 19)
1116
1117 gcdc.SetFont(boldFont)
1118 # Get the text color.
1119 dc.SetTextForeground(self.foreground)
1120 dc.DrawLabel(self.label, rect, wx.ALIGN_CENTER)
1121
1122
1123 def SetClicked(self, clicked):
1124 """
1125 ...
1126 """
1127
1128 if clicked != self._clicked:
1129 self._clicked = clicked
1130 self.Refresh()
1131
1132
1133 def GetClicked(self):
1134 """
1135 ...
1136 """
1137
1138 return self._clicked
1139
1140
1141 clicked = property(GetClicked, SetClicked)
1142 def OnLeftDown(self, event):
1143 """
1144 ...
1145 """
1146
1147 x, y = event.GetPosition()
1148 if self.region.Contains(x, y):
1149 self.clicked = True
1150 self.SetFocus()
1151
1152
1153 def OnLeftDclick(self, event):
1154 """
1155 ...
1156 """
1157
1158 self.OnLeftDown(event)
1159
1160
1161 def OnLeftUp(self, event):
1162 """
1163 ...
1164 """
1165
1166 if self.clicked:
1167 x, y = event.GetPosition()
1168 if self.region.Contains(x, y):
1169 self.PostEvent()
1170 self.clicked = False
1171
1172
1173 def OnMotion(self, event):
1174 """
1175 ...
1176 """
1177
1178 if self.clicked:
1179 x, y = event.GetPosition()
1180 if not self.region.Contains(x, y):
1181 self.clicked = False
1182
1183
1184 def OnLeave(self, event):
1185 """
1186 ...
1187 """
1188
1189 self.clicked = False
1190
1191
1192 def OnSetFocus(self, event):
1193 """
1194 ...
1195 """
1196
1197 self.color = "white"
1198 self.Refresh()
1199
1200
1201 def OnKillFocus(self, event):
1202 """
1203 ...
1204 """
1205
1206 if wx.Platform == "__WXGTK__":
1207 self.color = "#9e9d9d"
1208
1209 else:
1210 self.color = "#b1b1b0"
1211
1212 self.Refresh()
1213
1214
1215 def OnKeyUp(self, event):
1216 """
1217 ...
1218 """
1219
1220 if event.GetKeyCode() == wx.WXK_SPACE:
1221 self.PostEvent()
1222 return
1223
1224 elif event.GetKeyCode() == wx.WXK_ESCAPE:
1225 self.GetTopLevelParent().OnCloseWindow(True)
1226
1227 event.Skip()
1228
1229 #---------------------------------------------------------------------------
1230
1231 class MyButtonBox(wx.Panel):
1232 """
1233 Thanks to Cody Precord.
1234 """
1235 def __init__(self, parent, caption):
1236 style = (wx.NO_BORDER | wx.TAB_TRAVERSAL)
1237 super(MyButtonBox, self).__init__(parent,
1238 style=style)
1239
1240 #------------
1241
1242 # Attributes.
1243 self._caption = caption
1244
1245 #------------
1246
1247 # Simplified init method.
1248 self.DoLayout()
1249
1250 #-----------------------------------------------------------------------
1251
1252 def DoLayout(self):
1253 """
1254 ...
1255 """
1256
1257 self._csizer = wx.BoxSizer(wx.VERTICAL)
1258 msizer = wx.BoxSizer(wx.HORIZONTAL)
1259 msizer.Add(self._csizer, 0 )
1260 self.SetSizer(msizer)
1261
1262
1263 def DoGetBestSize(self):
1264 """
1265 ...
1266 """
1267
1268 size = super(MyButtonBox, self).DoGetBestSize()
1269
1270 # Compensate for wide caption labels.
1271 tw = self.GetTextExtent(self._caption)[0]
1272 size.SetWidth(max(size.width, size.height))
1273 return size
1274
1275
1276 def AddItem(self, item):
1277 """
1278 Add a window or sizer item to the MyButtonBox.
1279 """
1280
1281 self._csizer.Add(item, 0 )
1282
1283 #---------------------------------------------------------------------------
1284
1285 class MyTitleBarPnl(wx.Panel):
1286 """
1287 Thanks to Cody Precord.
1288 """
1289 def __init__(self, parent, id, size):
1290 style = (wx.NO_BORDER)
1291 super(MyTitleBarPnl, self).__init__(parent,
1292 id,
1293 size=size,
1294 style=style)
1295
1296 #------------
1297
1298 # Attributes.
1299 self.parent = parent
1300
1301 #------------
1302
1303 # Return config file.
1304 self.config = wx.GetApp().GetConfig()
1305 # Return application name.
1306 self.app_name = wx.GetApp().GetAppName()
1307 # Return bitmaps folder.
1308 self.bitmaps_dir = wx.GetApp().GetBitmapsDir()
1309
1310 #------------
1311
1312 # Simplified init method.
1313 self.SetProperties()
1314 self.CreateCtrls()
1315 self.BindEvents()
1316
1317 #-----------------------------------------------------------------------
1318
1319 def SetProperties(self):
1320 """
1321 ...
1322 """
1323
1324 # Read config file.
1325 self.SetBackgroundColour(wx.Colour(self.config.Read("Color3")))
1326
1327
1328 def CreateCtrls(self):
1329 """
1330 ...
1331 """
1332
1333 w, h = self.GetClientSize()
1334 print("MyTitleBarPnl :", self.GetClientSize())
1335
1336 #------------
1337 #------------
1338
1339 # Add titleBar.
1340 self.titleBar = MyTitleBar(self,
1341 label=self.app_name,
1342 size=10)
1343 self.titleBar.SetPosition((0, 0))
1344 self.titleBar.SetSize((w, 31))
1345 self.titleBar.SetLabelColour("white")
1346 self.titleBar.SetToolTip("This is a customized title bar.")
1347
1348 # Load an icon bitmap for titlebar. #-------> Add bloc here.
1349 bmp = wx.Bitmap(os.path.join(self.bitmaps_dir,
1350 "icon_app.png"),
1351 type=wx.BITMAP_TYPE_PNG)
1352
1353 self.ico = wx.StaticBitmap(self.titleBar, -1, bmp) #-------> Add bloc here.
1354 self.ico.SetBackgroundColour(wx.Colour(self.config.Read("Color3")))
1355 self.ico.SetPosition((8, 7))
1356 self.ico.SetToolTip("This is a customized icon.")
1357 self.ico.SetBackgroundStyle(wx.BG_STYLE_CUSTOM)
1358
1359 #------------
1360 #------------
1361
1362 # Gloss Buttons box (titleBar).
1363 self.box3 = MyButtonBox(self.titleBar, "") # Button Exit.
1364 self.box4 = MyButtonBox(self.titleBar, "") # Button Maximize.
1365 self.box5 = MyButtonBox(self.titleBar, "") # Button Reduce.
1366 self.box6 = MyButtonBox(self.titleBar, "") # Button Roll.
1367
1368 # Gloss Buttons bitmap.
1369 bmpa = wx.Bitmap(os.path.join(self.bitmaps_dir,
1370 "btn_gloss_exit_normal_5.png"),
1371 type=wx.BITMAP_TYPE_PNG)
1372
1373 bmpb = wx.Bitmap(os.path.join(self.bitmaps_dir,
1374 "btn_gloss_exit_selected_5.png"),
1375 type=wx.BITMAP_TYPE_PNG)
1376
1377 bmpc = wx.Bitmap(os.path.join(self.bitmaps_dir,
1378 "btn_gloss_maximize_normal_5.png"),
1379 type=wx.BITMAP_TYPE_PNG)
1380
1381 bmpd = wx.Bitmap(os.path.join(self.bitmaps_dir,
1382 "btn_gloss_maximize_selected_5.png"),
1383 type=wx.BITMAP_TYPE_PNG)
1384
1385 bmpe = wx.Bitmap(os.path.join(self.bitmaps_dir,
1386 "btn_gloss_reduce_normal_5.png"),
1387 type=wx.BITMAP_TYPE_PNG)
1388
1389 bmpf = wx.Bitmap(os.path.join(self.bitmaps_dir,
1390 "btn_gloss_reduce_selected_5.png"),
1391 type=wx.BITMAP_TYPE_PNG)
1392
1393 bmpg = wx.Bitmap(os.path.join(self.bitmaps_dir,
1394 "btn_gloss_roll_normal_5.png"),
1395 type=wx.BITMAP_TYPE_PNG)
1396
1397 bmph = wx.Bitmap(os.path.join(self.bitmaps_dir,
1398 "btn_gloss_roll_selected_5.png"),
1399 type=wx.BITMAP_TYPE_PNG)
1400
1401 self.btn3 = MyWindow(self.box3, "", "black", "#9e9d9d", bmpa, bmpb)
1402 self.btn3.SetToolTip("This is a customized gloss button.")
1403
1404 self.btn4 = MyWindow(self.box4, "", "black", "#9e9d9d", bmpc, bmpd)
1405 self.btn4.SetToolTip("This is a customized gloss button.")
1406
1407 self.btn5 = MyWindow(self.box5, "", "black", "#9e9d9d", bmpe, bmpf)
1408 self.btn5.SetToolTip("This is a customized gloss button.")
1409
1410 self.btn6 = MyWindow(self.box6, "", "black", "#9e9d9d", bmpg, bmph)
1411 self.btn6.SetToolTip("This is a customized gloss button.")
1412
1413 self.box3.AddItem(self.btn3)
1414 self.box4.AddItem(self.btn4)
1415 self.box5.AddItem(self.btn5)
1416 self.box6.AddItem(self.btn6)
1417
1418 #------------
1419 #------------
1420
1421 w, h = self.GetSize()
1422
1423 #------------
1424
1425 self.box3.SetSize((25, 19))
1426 x1, y1 = self.box3.GetSize()
1427 self.box3.SetPosition((w-7-x1, h-6-y1))
1428
1429 #------------
1430
1431 self.box4.SetSize((25, 19))
1432 self.box4.SetPosition((w-7-(x1*2), h-6-y1))
1433
1434 #------------
1435
1436 self.box5.SetSize((25, 19))
1437 self.box5.SetPosition((w-7-(x1*3), h-6-y1))
1438
1439 #------------
1440
1441 self.box6.SetSize((25, 19))
1442 self.box6.SetPosition((w-7-(x1*4), h-6-y1))
1443
1444
1445 def BindEvents(self):
1446 """
1447 Bind some events to an events handler.
1448 """
1449
1450 self.ico.Bind(wx.EVT_RIGHT_DOWN, self.OnRightDown)
1451 self.ico.Bind(wx.EVT_LEFT_DOWN, self.OnRightDown)
1452
1453 self.btn3.Bind(wx.EVT_BUTTON, self.OnBtnClose)
1454 self.btn4.Bind(wx.EVT_BUTTON, self.OnFullScreen)
1455 self.btn5.Bind(wx.EVT_BUTTON, self.OnIconfiy)
1456 self.btn6.Bind(wx.EVT_BUTTON, self.OnRoll)
1457
1458 self.Bind(wx.EVT_SIZE, self.OnResize)
1459
1460
1461 def OnRightDown(self, event):
1462 """
1463 ...
1464 """
1465
1466 self.PopupMenu(MyPopupMenu(self), event.GetPosition())
1467
1468 print("Right down.")
1469
1470
1471 def OnResize(self, event):
1472 """
1473 ...
1474 """
1475
1476 w, h = self.GetClientSize()
1477 print("MyTitleBarPnl :", self.GetClientSize())
1478
1479 #------------
1480 #------------
1481
1482 self.titleBar.SetSize((w, 31))
1483
1484 #------------
1485
1486 x1, y1 = self.box3.GetSize()
1487
1488 self.box3.SetPosition((w-7-x1, h-6-y1))
1489 self.box4.SetPosition((w-7-(x1*2), h-6-y1))
1490 self.box5.SetPosition((w-7-(x1*3), h-6-y1))
1491 self.box6.SetPosition((w-7-(x1*4), h-6-y1))
1492
1493 #------------
1494
1495 self.Refresh()
1496
1497 print("On resize was clicked.")
1498
1499
1500 def OnRoll(self, event):
1501 """
1502 ...
1503 """
1504
1505 self.GetParent().OnRoll(True)
1506
1507 print("Roll/unRoll button was clicked.")
1508
1509
1510 def OnIconfiy(self, event):
1511 """
1512 ...
1513 """
1514
1515 self.GetParent().OnIconfiy(self)
1516
1517 print("Iconfiy button was clicked.")
1518
1519
1520 def OnFullScreen(self, event):
1521 """
1522 ...
1523 """
1524
1525 self.GetParent().OnFullScreen(self)
1526
1527 print("FullScreen button was clicked.")
1528
1529
1530 def OnBtnClose(self, event):
1531 """
1532 ...
1533 """
1534
1535 self.GetParent().OnCloseWindow(self)
1536
1537 print("Close button was clicked.")
1538
1539 #---------------------------------------------------------------------------
1540
1541 class MyMainPnl(wx.Panel):
1542 """
1543 ...
1544 """
1545 def __init__(self, parent, id, size):
1546 style = (wx.NO_BORDER | wx.TAB_TRAVERSAL)
1547 super(MyMainPnl, self).__init__(parent,
1548 id=int(ID_MAIN_PNL),
1549 size=size,
1550 style=style)
1551
1552 #------------
1553
1554 # Attributes.
1555 self.parent = parent
1556
1557 #------------
1558
1559 # Return config file.
1560 self.config = wx.GetApp().GetConfig()
1561 # Return bitmaps folder.
1562 self.bitmaps_dir = wx.GetApp().GetBitmapsDir()
1563
1564 #------------
1565
1566 # Colourdb.
1567 wx.lib.colourdb.updateColourDB()
1568
1569 # Create a colour list from the colourdb database.
1570 self.colour_list = wx.lib.colourdb.getColourList()
1571
1572 #------------
1573
1574 # Simplified init method.
1575 self.SetProperties()
1576 self.CreateCtrls()
1577 self.DoLayout()
1578 self.BindEvents()
1579
1580 #-----------------------------------------------------------------------
1581
1582 def SetProperties(self):
1583 """
1584 ...
1585 """
1586
1587 if wx.Platform == "__WXMSW__":
1588 self.SetDoubleBuffered(True)
1589
1590 # Read config file.
1591 self.SetBackgroundColour(self.config.Read("Color3"))
1592
1593
1594 def CreateCtrls(self):
1595 """
1596 ...
1597 """
1598
1599 w, h = self.GetClientSize()
1600 print("MyMainPnl :", self.GetClientSize())
1601
1602 #------------
1603
1604 font = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT)
1605 font.SetWeight(wx.BOLD)
1606
1607 #------------
1608 #------------
1609
1610 # Create data list control.
1611 self.list = MyListCtrlPnl(self)
1612
1613 #------------
1614 #------------
1615
1616 self.txt = wx.StaticText(self,
1617 -1,
1618 label="Hello World !")
1619 self.txt.SetForegroundColour("white")
1620 self.txt.SetFont(font)
1621
1622 #------------
1623 #------------
1624
1625 # Thanks to MCOW.
1626 # Buttons.
1627 self.btn1 = SBB.ShapedBitmapButton(self, ID_BTN_FULLSCREEN,
1628 bitmap=wx.Bitmap(os.path.join(self.bitmaps_dir,
1629 "adbRnd.png"),
1630 type=wx.BITMAP_TYPE_PNG),
1631
1632 pressedBmp=wx.Bitmap(os.path.join(self.bitmaps_dir,
1633 "adbRnd-pressed.png"),
1634 type=wx.BITMAP_TYPE_PNG),
1635
1636 hoverBmp=wx.Bitmap(os.path.join(self.bitmaps_dir,
1637 "adbRnd-hover.png"),
1638 type=wx.BITMAP_TYPE_PNG),
1639
1640 disabledBmp=wx.Bitmap(os.path.join(self.bitmaps_dir,
1641 "adbRnd-disabled.png"),
1642 type=wx.BITMAP_TYPE_PNG),
1643
1644 label="Fullscreen",
1645 labelForeColour=wx.WHITE,
1646 labelPosition=(35, 8),
1647 labelFont=wx.Font(9,
1648 wx.FONTFAMILY_DEFAULT,
1649 wx.FONTSTYLE_NORMAL,
1650 wx.FONTWEIGHT_BOLD),
1651 style=wx.BORDER_NONE)
1652
1653 self.btn1.SetFocus() # Necessary for Linux.
1654
1655 #------------
1656
1657 self.btn2 = SBB.ShapedBitmapButton(self, ID_BTN_ABOUT,
1658 bitmap=wx.Bitmap(os.path.join(self.bitmaps_dir,
1659 "adbRnd.png"),
1660 type=wx.BITMAP_TYPE_PNG),
1661
1662 pressedBmp=wx.Bitmap(os.path.join(self.bitmaps_dir,
1663 "adbRnd-pressed.png"),
1664 type=wx.BITMAP_TYPE_PNG),
1665
1666 hoverBmp=wx.Bitmap(os.path.join(self.bitmaps_dir,
1667 "adbRnd-hover.png"),
1668 type=wx.BITMAP_TYPE_PNG),
1669
1670 disabledBmp=wx.Bitmap(os.path.join(self.bitmaps_dir,
1671 "adbRnd-disabled.png"),
1672 type=wx.BITMAP_TYPE_PNG),
1673
1674 label="About",
1675 labelForeColour=wx.WHITE,
1676 labelPosition=(50, 8),
1677 labelFont=wx.Font(9,
1678 wx.FONTFAMILY_DEFAULT,
1679 wx.FONTSTYLE_NORMAL,
1680 wx.FONTWEIGHT_BOLD),
1681 style=wx.BORDER_NONE)
1682
1683 #------------
1684
1685 self.btn3 = SBB.ShapedBitmapButton(self, ID_BTN_QUIT,
1686 bitmap=wx.Bitmap(os.path.join(self.bitmaps_dir,
1687 "adbRnd.png"),
1688 type=wx.BITMAP_TYPE_PNG),
1689
1690 pressedBmp=wx.Bitmap(os.path.join(self.bitmaps_dir,
1691 "adbRnd-pressed.png"),
1692 type=wx.BITMAP_TYPE_PNG),
1693
1694 hoverBmp=wx.Bitmap(os.path.join(self.bitmaps_dir,
1695 "adbRnd-hover.png"),
1696 type=wx.BITMAP_TYPE_PNG),
1697
1698 disabledBmp=wx.Bitmap(os.path.join(self.bitmaps_dir,
1699 "adbRnd-disabled.png"),
1700 type=wx.BITMAP_TYPE_PNG),
1701
1702 label="Quit",
1703 labelForeColour=wx.WHITE,
1704 labelPosition=(50, 8),
1705 labelFont=wx.Font(9,
1706 wx.FONTFAMILY_DEFAULT,
1707 wx.FONTSTYLE_NORMAL,
1708 wx.FONTWEIGHT_BOLD),
1709 style=wx.BORDER_NONE)
1710
1711 #------------
1712 #------------
1713
1714 self.line = wx.StaticLine(self, -1,
1715 pos=(0, 70),
1716 size=(-1, -1),
1717 style=wx.LI_HORIZONTAL)
1718
1719 #------------
1720 #------------
1721
1722 # Create a choice widget.
1723 self.choice = wx.Choice(self,
1724 -1,
1725 pos=(-1, -1),
1726 size=(131, -1),
1727 choices=self.colour_list,
1728 style=wx.CB_SORT)
1729
1730 # Select item 141 in choice list to show.
1731 # self.choice.SetSelection(141) # DEEPSKYBLUE3
1732 # Read config file.
1733 self.choice.SetSelection(self.config.ReadInt("Item3"))
1734
1735 item = self.choice.GetCurrentSelection()
1736 bgcolour = self.choice.GetStringSelection()
1737 print("Item : %s , %s" % (item, bgcolour))
1738
1739
1740 def DoLayout(self):
1741 """
1742 ...
1743 """
1744
1745 txtSizer = wx.BoxSizer(wx.VERTICAL)
1746 txtSizer.Add(self.txt, 0, wx.LEFT, 13)
1747
1748 sizerList = wx.BoxSizer(wx.VERTICAL)
1749 sizerList.Add(self.list, 1,
1750 wx.LEFT|wx.TOP|wx.BOTTOM|
1751 wx.EXPAND|wx.ALIGN_TOP, 10)
1752
1753 sizer = wx.BoxSizer(wx.VERTICAL)
1754 sizer.Add(txtSizer, 0, wx.BOTTOM, 0)
1755 sizer.Add(self.btn1, 1, wx.ALL, 10)
1756 sizer.Add(self.btn2, 1, wx.ALL, 10)
1757 sizer.Add(self.line, 0, wx.EXPAND|wx.ALL, 10)
1758 sizer.Add(self.choice, 0, wx.ALL, 10)
1759 sizer.Add(self.btn3, 1, wx.ALL, 10)
1760
1761 mainSizer = wx.BoxSizer(wx.HORIZONTAL)
1762 mainSizer.Add(sizerList, 1,
1763 wx.LEFT|wx.TOP|wx.BOTTOM|wx.EXPAND, 5)
1764 mainSizer.Add(sizer, 0, wx.ALIGN_BOTTOM|wx.ALL, 5)
1765
1766 self.SetSizer(mainSizer)
1767 self.Layout()
1768
1769
1770 def BindEvents(self):
1771 """
1772 Bind some events to an events handler.
1773 """
1774
1775 self.choice.Bind(wx.EVT_CHOICE, self.OnChoice)
1776
1777 self.Bind(wx.EVT_ENTER_WINDOW, self.OnInfo, id=ID_MAIN_PNL)
1778 self.btn1.Bind(wx.EVT_ENTER_WINDOW, self.OnInfo, id=ID_BTN_FULLSCREEN)
1779 self.btn2.Bind(wx.EVT_ENTER_WINDOW, self.OnInfo, id=ID_BTN_ABOUT)
1780 self.btn3.Bind(wx.EVT_ENTER_WINDOW, self.OnInfo, id=ID_BTN_QUIT)
1781
1782 self.btn1.Bind(wx.EVT_BUTTON, self.OnFullScreen)
1783 self.btn2.Bind(wx.EVT_BUTTON, self.OnAbout)
1784 self.btn3.Bind(wx.EVT_BUTTON, self.OnBtnClose)
1785
1786
1787 def OnInfo(self, event):
1788 """
1789 ...
1790 """
1791
1792 event_id = event.GetId()
1793
1794 if event_id == ID_MAIN_PNL:
1795 self.GetParent().SetStatusText(text="Hello world !")
1796
1797 elif event_id == ID_BTN_FULLSCREEN:
1798 self.GetParent().SetStatusText(text="Fullscreen")
1799
1800 elif event_id == ID_BTN_ABOUT:
1801 self.GetParent().SetStatusText(text="About")
1802
1803 elif event_id == ID_BTN_QUIT:
1804 self.GetParent().SetStatusText(text="Quit the program")
1805
1806 else:
1807 # Tell the event system to continue
1808 # looking for an event handler, so the
1809 # default handler will get called.
1810 event.Skip()
1811
1812
1813 def OnChoice(self, event):
1814 """
1815 ...
1816 """
1817
1818 item = event.GetSelection()
1819 bgcolour = self.choice.GetStringSelection()
1820 print("New item : %s , %s" % (item, bgcolour))
1821
1822 # Change colour of the panel to the selected colour...
1823 self.SetBackgroundColour(bgcolour)
1824
1825 # Write config file.
1826 self.config.WriteInt("Item3", self.choice.GetCurrentSelection())
1827 self.config.Write("Color3", self.choice.GetStringSelection())
1828 self.config.Flush()
1829
1830 self.Refresh()
1831
1832 print("OnChoice button was clicked.")
1833
1834
1835 def OnAbout(self, event):
1836 """
1837 ...
1838 """
1839
1840 self.GetParent().OnAbout(self)
1841
1842 print("FullScreen button was clicked.")
1843
1844
1845 def OnFullScreen(self, event):
1846 """
1847 ...
1848 """
1849
1850 self.GetParent().OnFullScreen(self)
1851
1852 print("FullScreen button was clicked.")
1853
1854
1855 def OnBtnClose(self, event):
1856 """
1857 ...
1858 """
1859
1860 self.GetParent().OnCloseWindow(self)
1861
1862 print("Close button was clicked.")
1863
1864 #---------------------------------------------------------------------------
1865
1866 class MyFrame(wx.Frame):
1867 """
1868 Thanks to Robin Dunn.
1869 """
1870 def __init__(self):
1871 style = (wx.CLIP_CHILDREN | wx.CLOSE_BOX |
1872 wx.MINIMIZE_BOX | wx.SYSTEM_MENU |
1873 wx.BORDER_SIMPLE | wx.NO_FULL_REPAINT_ON_RESIZE|
1874 wx.STAY_ON_TOP)
1875 super(MyFrame, self).__init__(None,
1876 -1,
1877 title="",
1878 style=style)
1879
1880 #------------
1881
1882 wx.SystemOptions.SetOption("msw.remap", "0")
1883
1884 #------------
1885
1886 # Attributes.
1887 self.SetTransparent(0)
1888 self.opacity_in = 0
1889 self.opacity_out = 255
1890 self.deltaN = -70
1891 self.delta = wx.Point(0,0)
1892
1893 #------------
1894
1895 # Return config file.
1896 self.config = wx.GetApp().GetConfig()
1897 # Return application name.
1898 self.app_name = wx.GetApp().GetAppName()
1899 # Return bitmaps folder.
1900 self.bitmaps_dir = wx.GetApp().GetBitmapsDir()
1901 # Return icons folder.
1902 self.icons_dir = wx.GetApp().GetIconsDir()
1903
1904 #------------
1905
1906 # Colourdb.
1907 wx.lib.colourdb.updateColourDB()
1908
1909 # Create a colour list from the colourdb database.
1910 self.colour_list = wx.lib.colourdb.getColourList()
1911
1912 #------------
1913
1914 # Simplified init method.
1915 self.SetProperties()
1916 self.OnTimerIn(self)
1917 self.CreateMenu()
1918 self.CreateStatusBar()
1919 self.CreateCtrls()
1920 self.BindEvents()
1921 self.DoLayout()
1922
1923 #------------
1924
1925 # Thanks to Ray Pasco.
1926 # Initialize to the current state.
1927 self.unrolledFrameClientSize_size = self.GetClientSize()
1928 self.isRolled = False
1929
1930 #------------
1931
1932 self.CenterOnScreen(wx.BOTH)
1933
1934 #------------
1935
1936 self.Show(True)
1937
1938 #-----------------------------------------------------------------------
1939
1940 def SetProperties(self):
1941 """
1942 ...
1943 """
1944
1945 self.SetTitle(self.app_name)
1946 self.SetClientSize((600, 380))
1947 self.SetMinSize((340, 83))
1948
1949 # Read config file.
1950 self.SetBackgroundColour(self.config.Read("Color3"))
1951
1952 #------------
1953
1954 frameIcon = wx.Icon(os.path.join(self.icons_dir,
1955 "icon_wxWidgets.ico"),
1956 type=wx.BITMAP_TYPE_ICO)
1957 self.SetIcon(frameIcon)
1958
1959
1960 def OnTimerIn(self, evt):
1961 """
1962 Thanks to Pascal Faut.
1963 """
1964
1965 self.timer1 = wx.Timer(self, -1)
1966 self.timer1.Start(1)
1967 self.Bind(wx.EVT_TIMER, self.AlphaCycle1, self.timer1)
1968
1969 print("Fade-in was launched.")
1970
1971
1972 def OnTimerOut(self, evt):
1973 """
1974 Thanks to Pascal Faut.
1975 """
1976
1977 self.timer2 = wx.Timer(self, -1)
1978 self.timer2.Start(1)
1979 self.Bind(wx.EVT_TIMER, self.AlphaCycle2, self.timer2)
1980
1981 print("Fade-out was launched.")
1982
1983
1984 def AlphaCycle1(self, *args):
1985 """
1986 Thanks to Pascal Faut.
1987 """
1988
1989 self.opacity_in += self.deltaN
1990 if self.opacity_in <= 0:
1991 self.deltaN = -self.deltaN
1992 self.opacity_in = 0
1993
1994 if self.opacity_in >= 255:
1995 self.deltaN = -self.deltaN
1996 self.opacity_in = 255
1997
1998 self.timer1.Stop()
1999
2000 self.SetTransparent(self.opacity_in)
2001
2002 print("Fade in = {}/255".format(self.opacity_in))
2003
2004
2005 def AlphaCycle2(self, *args):
2006 """
2007 Thanks to Pascal Faut.
2008 """
2009
2010 self.opacity_out += self.deltaN
2011 if self.opacity_out >= 255:
2012 self.deltaN = -self.deltaN
2013 self.opacity_out = 255
2014
2015 if self.opacity_out <= 0:
2016 self.deltaN = -self.deltaN
2017 self.opacity_out = 0
2018
2019 self.timer2.Stop()
2020 wx.CallAfter(self.Destroy)
2021 wx.Exit()
2022
2023 self.SetTransparent(self.opacity_out)
2024
2025 print("Fade out = {}/255".format(self.opacity_out))
2026
2027
2028 def CreateMenu(self):
2029 """
2030 ...
2031 """
2032
2033 # FlatMenuBar.
2034 self.menuBar = FM.FlatMenuBar(self,
2035 wx.ID_ANY,
2036 32, 4,
2037 options=FM.FM_OPT_IS_LCD)
2038
2039 # FM.StyleDefault or FM.Style2007
2040 # FM.StyleXP or FM.StyleVista
2041 self.newMyTheme = self.menuBar.GetRendererManager().AddRenderer(MyMenuRenderer())
2042 self.menuBar.GetRendererManager().SetTheme(self.newMyTheme)
2043
2044 #------------
2045
2046 # Set an icon to the exit/help menu item.
2047 exitImg = wx.Bitmap(os.path.join(self.bitmaps_dir,
2048 "item_exit.png"),
2049 type=wx.BITMAP_TYPE_PNG)
2050
2051 helpImg = wx.Bitmap(os.path.join(self.bitmaps_dir,
2052 "item_about.png"),
2053 type=wx.BITMAP_TYPE_PNG)
2054
2055 #------------
2056 #------------
2057
2058 # File Menu.
2059 self.file_menu = FM.FlatMenu()
2060
2061 # Create the menu items.
2062 item = FM.FlatMenuItem(self.file_menu,
2063 ID_FULLSCREEN,
2064 "&Fullscreen\tCtrl+F",
2065 "Fullscreen",
2066 wx.ITEM_NORMAL,
2067 None)
2068 item.SetTextColour("black")
2069 self.file_menu.AppendItem(item)
2070 self.file_menu.AppendSeparator()
2071
2072
2073 item = FM.FlatMenuItem(self.file_menu,
2074 wx.ID_EXIT,
2075 "&Quit\tCtrl+Q",
2076 "Quit the program",
2077 wx.ITEM_NORMAL,
2078 None,
2079 exitImg)
2080 # Demonstrate how to set custom font
2081 # and text colour to a FlatMenuItem.
2082 item.SetFont(wx.Font(-1, wx.FONTFAMILY_DEFAULT,
2083 wx.FONTSTYLE_NORMAL,
2084 wx.FONTWEIGHT_BOLD,
2085 False, ""))
2086 item.SetTextColour("#d34725")
2087
2088 self.file_menu.AppendItem(item)
2089
2090 #------------
2091
2092 # Add Create background bitmap.
2093 self.file_menu.SetBackgroundBitmap(CreateBackgroundBitmap())
2094
2095 #------------
2096 #------------
2097
2098 # Help Menu.
2099 self.help_menu = FM.FlatMenu()
2100
2101 # Create the menu items.
2102 item = FM.FlatMenuItem(self.help_menu,
2103 wx.ID_ABOUT,
2104 "&About\tCtrl+A",
2105 "About",
2106 wx.ITEM_NORMAL,
2107 None,
2108 helpImg)
2109 item.SetTextColour("black")
2110 self.help_menu.AppendItem(item)
2111 self.help_menu.AppendSeparator()
2112
2113 item = FM.FlatMenuItem(self.help_menu,
2114 ID_HELLO,
2115 "Hello !",
2116 "Hello !",
2117 wx.ITEM_NORMAL,
2118 None)
2119 item.SetTextColour("black")
2120 self.help_menu.AppendItem(item)
2121
2122 #------------
2123
2124 # Add Create background bitmap.
2125 self.help_menu.SetBackgroundBitmap(CreateBackgroundBitmap())
2126
2127 #------------
2128 #------------
2129
2130 # Menu background color.
2131 # Read config file.
2132 self.menuBar.SetBackgroundColour(wx.Colour(self.config.Read("Color3")))
2133
2134 # Add menu to the menu bar.
2135 self.menuBar.Append(self.file_menu, "&File")
2136 self.menuBar.Append(self.help_menu, "&Help")
2137
2138
2139 def CreateStatusBar(self):
2140 """
2141 ...
2142 """
2143
2144 self.sb = MyStatusBar(self, -1)
2145 # Read config file.
2146 self.sb.SetBackgroundColour(wx.Colour(self.config.Read("Color3")))
2147 self.sb.SetStatusText("Hello world !")
2148
2149 self.SetStatusBar(self.sb)
2150
2151
2152 def CreateCtrls(self):
2153 """
2154 ...
2155 """
2156
2157 w, h = self.GetClientSize()
2158 print("MyFrame :", self.GetClientSize())
2159
2160 #------------
2161 #------------
2162
2163 self.titleBarPnl = MyTitleBarPnl(self, -1, (w, 31))
2164 self.titleBarPnl.SetPosition((0, 0))
2165 print("self.titleBarPnl :", self.titleBarPnl.GetSize())
2166
2167 #------------
2168
2169 self.mainPnl = MyMainPnl(self, -1, (w, h-31))
2170 self.mainPnl.SetPosition((0, 31))
2171 print("self.mainPnl :", self.mainPnl.GetSize())
2172
2173
2174 def BindEvents(self):
2175 """
2176 ...
2177 """
2178
2179 self.titleBarPnl.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
2180
2181 self.Bind(wx.EVT_SIZE, self.OnResize)
2182 self.Bind(wx.EVT_LEFT_UP, self.OnLeftUp)
2183 self.Bind(wx.EVT_MOTION, self.OnMouseMove)
2184 self.Bind(wx.EVT_CHAR_HOOK, self.OnKeyUp)
2185 self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
2186
2187 self.Bind(wx.EVT_MENU, self.OnFullScreen, id=ID_FULLSCREEN)
2188 self.Bind(wx.EVT_MENU, self.OnAbout, id=wx.ID_ABOUT)
2189 self.Bind(wx.EVT_MENU, self.OnCloseWindow, id=wx.ID_EXIT)
2190
2191
2192 def DoLayout(self):
2193 """
2194 ...
2195 """
2196
2197 # MainSizer is the top-level one that manages everything.
2198 mainSizer = wx.BoxSizer(wx.VERTICAL)
2199
2200 mbSizer = wx.BoxSizer(wx.HORIZONTAL)
2201 mbSizer.Add(self.menuBar, 1, wx.ALL, 0)
2202
2203 mainSizer.Add(self.titleBarPnl, 1, wx.EXPAND, 0)
2204 mainSizer.Add(mbSizer, 0, wx.EXPAND, 0)
2205 mainSizer.Add(self.mainPnl, 1, wx.EXPAND, 0)
2206
2207 # Finally, tell the panel to use the sizer for layout.
2208 self.SetSizer(mainSizer)
2209 self.Layout()
2210
2211
2212 def OnRoll(self, event) :
2213 """
2214 Thanks to Ray Pasco.
2215 """
2216
2217 if not bool(self.isRolled) :
2218 # Set the flag to the state we want regardless of whether
2219 # or not it's in currently in the opposite state.
2220 self.RollUnRoll(wantToRoll=True)
2221
2222 elif self.isRolled : # UnRoll.
2223 # Set the flag to the state we want regardless of whether
2224 # or not it's in currently in the opposite state.
2225 self.RollUnRoll(wantToRoll=False)
2226
2227 print("OnRoll :", self.GetClientSize())
2228
2229
2230 def RollUnRoll(self, wantToRoll) :
2231 """
2232 Thanks to Ray Pasco.
2233 """
2234
2235 # Save the current size only if the Frame is not rolled up.
2236 if not bool(self.isRolled) :
2237 self.unrolledFrameClientSize_size = self.GetClientSize()
2238
2239 if bool(wantToRoll) : # UnRoll.
2240 # Set size (52).
2241 self.SetClientSize((self.unrolledFrameClientSize_size[0], 52))
2242 # Set to match this new state.
2243 self.isRolled = True
2244
2245 else : # Roll
2246 self.SetClientSize(self.unrolledFrameClientSize_size)
2247 # Set to match this new state.
2248 self.isRolled = False
2249
2250 print("RollUnRoll :", self.GetClientSize())
2251
2252
2253 def OnAbout(self, event):
2254 """
2255 ...
2256 """
2257
2258 self.dialog = MyAboutDlg(self)
2259
2260
2261 def OnLeftDown(self, event):
2262 """
2263 ...
2264 """
2265
2266 self.CaptureMouse()
2267 x, y = self.ClientToScreen(event.GetPosition())
2268 originx, originy = self.GetPosition()
2269 dx = x - originx
2270 dy = y - originy
2271 self.delta = ((dx, dy))
2272
2273
2274 def OnLeftUp(self, evt):
2275 """
2276 ...
2277 """
2278
2279 if self.HasCapture():
2280 self.ReleaseMouse()
2281
2282
2283 def OnMouseMove(self, event):
2284 """
2285 ...
2286 """
2287
2288 if event.Dragging() and event.LeftIsDown():
2289 x, y = self.ClientToScreen(event.GetPosition())
2290 fp = (x - self.delta[0], y - self.delta[1])
2291 self.Move(fp)
2292
2293
2294 def OnIconfiy(self, event):
2295 """
2296 ...
2297 """
2298
2299 self.Iconize()
2300
2301
2302 def OnResize(self, event):
2303 """
2304 ...
2305 """
2306
2307 w, h = self.GetClientSize()
2308 print("self :", self.GetClientSize())
2309
2310 #------------
2311 #------------
2312
2313 self.titleBarPnl.SetSize((w, 31))
2314 self.menuBar.SetSize((w, -1))
2315 self.mainPnl.SetSize((w, h-54))
2316
2317 #------------
2318
2319 self.Refresh()
2320
2321
2322 def OnFullScreen(self, event):
2323 """
2324 ...
2325 """
2326
2327 self.ShowFullScreen(not self.IsFullScreen(),
2328 wx.BORDER_SIMPLE)
2329
2330
2331 def OnKeyUp(self, event):
2332 """
2333 ...
2334 """
2335
2336 if event.GetKeyCode() == wx.WXK_ESCAPE:
2337 # Close the frame, no action.
2338 self.OnCloseWindow(event)
2339 event.Skip()
2340
2341
2342 def OnBtnClose(self, event):
2343 """
2344 ...
2345 """
2346
2347 self.Close()
2348
2349
2350 def OnCloseWindow(self, event):
2351 """
2352 Quit this application.
2353 """
2354
2355 # self.Destroy()
2356 self.OnTimerOut(self)
2357
2358 print("Exit application.")
2359
2360 #---------------------------------------------------------------------------
2361
2362 class MyApp(wx.App):
2363 """
2364 Thanks to Andrea Gavana.
2365 """
2366 def OnInit(self):
2367
2368 #------------
2369
2370 self.locale = wx.Locale(wx.LANGUAGE_ENGLISH)
2371
2372 #------------
2373
2374 self.SetAppName("Custom Gui 3")
2375
2376 #------------
2377
2378 self.installDir = os.path.split(os.path.abspath(sys.argv[0]))[0]
2379
2380 #------------
2381
2382 frame = MyFrame()
2383 self.SetTopWindow(frame)
2384 frame.Show(True)
2385 return True
2386
2387 #-----------------------------------------------------------------------
2388
2389 def GetInstallDir(self):
2390 """
2391 Returns the installation directory for my application.
2392 """
2393
2394 return self.installDir
2395
2396
2397 def GetIconsDir(self):
2398 """
2399 Returns the icons directory for my application.
2400 """
2401
2402 icons_dir = os.path.join(self.installDir, "icons")
2403 return icons_dir
2404
2405
2406 def GetBitmapsDir(self):
2407 """
2408 Returns the bitmaps directory for my application.
2409 """
2410
2411 bitmaps_dir = os.path.join(self.installDir, "bitmaps")
2412 return bitmaps_dir
2413
2414
2415 def GetConfig(self):
2416 """
2417 Returns the config file for my application.
2418 """
2419
2420 config = wx.FileConfig(appName="Custom Gui",
2421 localFilename=os.path.join(self.installDir,
2422 "options"))
2423 return config
2424
2425 #---------------------------------------------------------------------------
2426
2427 def main():
2428 app = MyApp(False)
2429 app.MainLoop()
2430
2431 #---------------------------------------------------------------------------
2432
2433 if __name__ == "__main__" :
2434 main()
Download source
Additional Information
Link :
https://hasenj.wordpress.com/2009/04/14/making-a-fancy-window-in-wxpython/
https://stackoverflow.com/questions/12087140/custom-window-frame-and-window-appearance-with-wxpython
https://github.com/Metallicow/MCOW
https://github.com/Eseeme/wxpython-custom-button
- - - - -
https://wiki.wxpython.org/TitleIndex
Thanks to
Robin Dunn, Cody Precord, Andrea Gavana, Metallicow, Mike Driscoll, Ray Pasco, Pascal Faut., Vegaseat (DaniWeb), Jan Bodnar (ZetCode), Alain Delgrange, Daniel Ramos, ActiveState, the wxPython community...
Thanks also to all known contributors or anonymous that I forgot.
And finally, congratulations to the many forums and blogs for all the available examples and the help which is the strength of wxPython.
About this page
Date (d/m/y) Person (bot) Comments :
10/10/18 - Ecco (Created page for wxPython Phoenix).
Comments
- blah, blah, blah....