AddPrivateFont wxpython

mail@pastecode.io avatar
unknown
python
2 years ago
13 kB
7
Indexable
Never
# Font source: https://www.fontspace.com/youth-touch-font-f30771

# ---------------------------------------------------------------------------


class MyTitleBar(wx.Control):
    """
    Thanks to Cody Precord.
    """

    def __init__(self, parent, label, size):
        style = wx.BORDER_NONE
        super(MyTitleBar, self).__init__(parent, style=style)

        # ------------

        # Return bitmaps folder.
        self.bitmaps_dir = wx.GetApp().GetBitmapsDir()

        # ------------

        # Attributes.
        self.parent = parent
        self.label = label
        self.size = size

        # ------------

        # Simplified init method.
        self.SetBackground()
        self.SetProperties(label, size)
        self.CreateMenu()
        self.CreateCtrls()
        self.BindEvents()
        self.DoLayout()

    # -----------------------------------------------------------------------

    def SetBackground(self):
        """
        ...
        """

        self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM)
        self.SetBackgroundColour(wx.WHITE)

    def SetProperties(self, label, size):
        """
        ...
        """

        # f = wx.Font(
        #     pointSize=18,
        #     family=wx.FONTFAMILY_DEFAULT,
        #     style=wx.FONTSTYLE_NORMAL,
        #     weight=wx.FONTWEIGHT_NORMAL,
        #     underline=False,
        #     faceName="Colchester Black",
        #     encoding=wx.FONTENCODING_DEFAULT,
        # )
        # Button1.SetFont(f)

        self.label = label
        self.size = size

        try:
            gFileDir = os.path.dirname(os.path.abspath(__file__))
        except:
            gFileDir = os.path.dirname(os.path.abspath(sys.argv[0]))
        gDataDir = os.path.join(gFileDir, "myfonts")

        print("gDataDir: ", gDataDir)

        filename = os.path.join(gDataDir, "YouthTouchDemoRegular-4VwY.ttf")
        wx.Font.AddPrivateFont(filename)

        print("filename: ", filename)

        # self.label_font = self.GetFont()
        # self.label_font.SetPointSize(18)
        # self.label_font.SetFamily(wx.FONTFAMILY_DEFAULT)
        # self.label_font.SetStyle(wx.FONTSTYLE_NORMAL)
        # self.label_font.SetWeight(wx.FONTWEIGHT_NORMAL)
        # self.label_font.SetUnderlined(False)
        # self.label_font.SetFaceName("Colchester Black")
        # self.label_font.SetEncoding(wx.FONTENCODING_DEFAULT)
        # self.SetFont(self.label_font)

        # self.label_font = self.GetFont()
        # self.label_font.SetFamily(wx.FONTFAMILY_DEFAULT)
        # self.label_font.SetPointSize(18)
        # self.label_font.SetWeight(wx.BOLD)
        # self.label_font.AddPrivateFont(filename)
        # self.label_font.SetEncoding(wx.FONTENCODING_DEFAULT)
        # self.SetFont(self.label_font)

        self.label_font = self.GetFont()
        self.label_font.AddPrivateFont(filename)
        self.SetFont(self.label_font)

    def CreateMenu(self):
        """
        ...
        """

        self.menuBar = MyMenuBar(self)

    def CreateCtrls(self):
        """
        ...
        """

        w, h = self.GetSize()
        w1, h1 = self.GetClientSize()

        # ------------

        # Load an icon bitmap for titlebar.
        # bmp = wx.Bitmap(os.path.join(self.bitmaps_dir,
        #                              "icon_app.png"),
        #                 type=wx.BITMAP_TYPE_PNG)

        # self.ico = wx.StaticBitmap(self, -1, bmp)
        # self.ico.SetBackgroundColour(wx.Colour(wx.WHITE))
        # self.ico.SetPosition((300, 1))
        # self.ico.SetToolTip("This is a customized icon.")
        # self.ico.SetBackgroundStyle(wx.BG_STYLE_CUSTOM)

        # ------------

        # Button Exit.
        self.btn3 = SBBTwo.ShapedBitmapButton(
            self,
            -1,
            bitmap=wx.Bitmap(
                os.path.join(self.bitmaps_dir, "btn_gloss_exit_normal_1.png"),
                type=wx.BITMAP_TYPE_PNG,
            ),
            pressedBmp=wx.Bitmap(
                os.path.join(self.bitmaps_dir, "btn_gloss_exit_selected_1.png"),
                type=wx.BITMAP_TYPE_PNG,
            ),
            hoverBmp=wx.Bitmap(
                os.path.join(self.bitmaps_dir, "btn_gloss_exit_normal_1.png"),
                type=wx.BITMAP_TYPE_PNG,
            ),
            disabledBmp=wx.Bitmap(
                os.path.join(self.bitmaps_dir, "btn_gloss_exit_normal_1.png"),
                type=wx.BITMAP_TYPE_PNG,
            ),
            label="",
            labelForeColour=wx.WHITE,
            labelFont=wx.Font(
                9, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD
            ),
            style=wx.BORDER_NONE,
        )

        # ------------

        # Button Maximize.
        self.btn4 = SBBTwo.ShapedBitmapButton(
            self,
            -1,
            bitmap=wx.Bitmap(
                os.path.join(self.bitmaps_dir, "btn_gloss_maximize_normal_1.png"),
                type=wx.BITMAP_TYPE_PNG,
            ),
            pressedBmp=wx.Bitmap(
                os.path.join(self.bitmaps_dir, "btn_gloss_maximize_selected_1.png"),
                type=wx.BITMAP_TYPE_PNG,
            ),
            hoverBmp=wx.Bitmap(
                os.path.join(self.bitmaps_dir, "btn_gloss_maximize_normal_1.png"),
                type=wx.BITMAP_TYPE_PNG,
            ),
            disabledBmp=wx.Bitmap(
                os.path.join(self.bitmaps_dir, "btn_gloss_maximize_normal_1.png"),
                type=wx.BITMAP_TYPE_PNG,
            ),
            label="",
            labelForeColour=wx.WHITE,
            labelFont=wx.Font(
                9, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD
            ),
            style=wx.BORDER_NONE,
        )

        # ------------

        # Thanks to MCOW.
        # Button Reduce.
        self.btn5 = SBBTwo.ShapedBitmapButton(
            self,
            -1,
            bitmap=wx.Bitmap(
                os.path.join(self.bitmaps_dir, "btn_gloss_reduce_normal_1.png"),
                type=wx.BITMAP_TYPE_PNG,
            ),
            pressedBmp=wx.Bitmap(
                os.path.join(self.bitmaps_dir, "btn_gloss_reduce_selected_1.png"),
                type=wx.BITMAP_TYPE_PNG,
            ),
            hoverBmp=wx.Bitmap(
                os.path.join(self.bitmaps_dir, "btn_gloss_reduce_normal_1.png"),
                type=wx.BITMAP_TYPE_PNG,
            ),
            disabledBmp=wx.Bitmap(
                os.path.join(self.bitmaps_dir, "btn_gloss_reduce_normal_1.png"),
                type=wx.BITMAP_TYPE_PNG,
            ),
            label="",
            labelForeColour=wx.WHITE,
            labelFont=wx.Font(
                9, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD
            ),
            style=wx.BORDER_NONE,
        )

        # ------------

        # Button Roll.
        self.btn6 = SBBTwo.ShapedBitmapButton(
            self,
            -1,
            bitmap=wx.Bitmap(
                os.path.join(self.bitmaps_dir, "btn_gloss_roll_normal_1.png"),
                type=wx.BITMAP_TYPE_PNG,
            ),
            pressedBmp=wx.Bitmap(
                os.path.join(self.bitmaps_dir, "btn_gloss_roll_selected_1.png"),
                type=wx.BITMAP_TYPE_PNG,
            ),
            hoverBmp=wx.Bitmap(
                os.path.join(self.bitmaps_dir, "btn_gloss_roll_normal_1.png"),
                type=wx.BITMAP_TYPE_PNG,
            ),
            disabledBmp=wx.Bitmap(
                os.path.join(self.bitmaps_dir, "btn_gloss_roll_normal_1.png"),
                type=wx.BITMAP_TYPE_PNG,
            ),
            label="",
            labelForeColour=wx.WHITE,
            labelFont=wx.Font(
                9, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD
            ),
            style=wx.BORDER_NONE,
        )

    def BindEvents(self):
        """
        Bind some events to an events handler.
        """

        # self.ico.Bind(wx.EVT_RIGHT_DOWN, self.OnRightDown)
        # self.ico.Bind(wx.EVT_LEFT_DOWN, self.OnRightDown)

        self.Bind(wx.EVT_PAINT, self.OnPaint)
        self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
        self.Bind(wx.EVT_LEFT_UP, self.OnLeftUp)

        self.btn3.Bind(wx.EVT_BUTTON, self.OnBtnClose)
        self.btn4.Bind(wx.EVT_BUTTON, self.OnFullScreen)
        self.btn5.Bind(wx.EVT_BUTTON, self.OnIconfiy)
        self.btn6.Bind(wx.EVT_BUTTON, self.OnRoll)

        self.Bind(wx.EVT_MENU, self.OnFullScreen, id=ID_FULLSCREEN)
        self.Bind(wx.EVT_MENU, self.OnBtnClose, id=wx.ID_EXIT)

    def DoLayout(self):
        """
        ...
        """

        mainSizer = wx.BoxSizer(wx.HORIZONTAL)

        # ------------

        mainSizer.Add(self.btn3, 0, wx.LEFT, 4)
        mainSizer.Add(self.btn4, 0, wx.LEFT, 4)
        mainSizer.Add(self.btn5, 0, wx.LEFT, 4)
        mainSizer.Add(self.btn6, 0, wx.LEFT, 4)
        mainSizer.Add((10, 1), 0, wx.ALL, 0)
        mainSizer.Add(self.menuBar, 1)
        mainSizer.Add((20, 1), 0, wx.EXPAND, 0)

        # ------------

        self.SetSizer(mainSizer)
        self.Layout()

    def OnLeftDown(self, event):
        """
        ...
        """

        self.GetTopLevelParent().OnLeftDown(event)

    def OnLeftUp(self, event):
        """
        ...
        """

        self.GetTopLevelParent().OnLeftUp(event)

    def SetLabel(self, label):
        """
        ...
        """

        self.label = label
        self.Refresh()

    def DoGetBestSize(self):
        """
        ...
        """

        dc = wx.ClientDC(self)
        dc.SetFont(self.GetFont())

        textWidth, textHeight = dc.GetTextExtent(self.label)
        spacing = 10
        totalWidth = textWidth + (spacing)
        totalHeight = textHeight + (spacing)

        best = wx.Size(totalWidth, totalHeight)
        self.CacheBestSize(best)

        return best

    def GetLabel(self):
        """
        ...
        """

        return self.label

    def GetLabelColor(self):
        """
        ...
        """

        return self.foreground

    def GetLabelSize(self):
        """
        ...
        """

        return self.size

    def SetLabelColour(self, colour):
        """
        ...
        """

        self.labelColour = colour

    def OnPaint(self, event):
        """
        ...
        """

        dc = wx.BufferedPaintDC(self)
        gcdc = wx.GCDC(dc)

        gcdc.Clear()

        # Setup the GraphicsContext.
        gc = gcdc.GetGraphicsContext()

        # Get the working size we can draw in.
        width, height = self.GetSize()

        # Use the GCDC to draw the text.
        brush = wx.WHITE
        gcdc.SetPen(wx.Pen(brush, 1))
        gcdc.SetBrush(wx.Brush(brush))
        gcdc.DrawRectangle(0, 0, width, height)

        # Get the system font.
        gcdc.SetFont(self.GetFont())

        textWidth, textHeight = gcdc.GetTextExtent(self.label)
        tposx, tposy = ((width / 2) - (textWidth / 2), (height / 3) - (textHeight / 3))

        tposx += 0
        tposy += 0

        # Set position and text color.
        if tposx <= 100:
            gcdc.SetTextForeground("white")
            gcdc.DrawText("", int(tposx), int(tposy + 1))

            gcdc.SetTextForeground(self.labelColour)
            gcdc.DrawText("", int(tposx), int(tposy))

        else:
            gcdc.SetTextForeground("white")
            gcdc.DrawText(self.label, int(tposx), int(tposy + 1))

            gcdc.SetTextForeground(self.labelColour)
            gcdc.DrawText(self.label, int(tposx), int(tposy))

    def OnRoll(self, event):
        """
        ...
        """

        self.GetTopLevelParent().OnRoll(True)

        print("Roll/unRoll button was clicked.")

    def OnIconfiy(self, event):
        """
        ...
        """

        self.GetTopLevelParent().OnIconfiy(self)

        print("Iconfiy button was clicked.")

    def OnFullScreen(self, event):
        """
        ...
        """

        self.GetTopLevelParent().OnFullScreen(self)

        print("FullScreen button was clicked.")

    def OnBtnClose(self, event):
        """
        ...
        """

        self.GetTopLevelParent().OnCloseWindow(self)

        print("Close button was clicked.")


# ---------------------------------------------------------------------------