| 1 | # |
| 2 | # "$Id: widgets.py 28 2003-07-16 20:00:27Z andreasheld $" |
| 3 | # |
| 4 | # Widgets test program for pyFLTK the Python bindings |
| 5 | # for the Fast Light Tool Kit (FLTK). |
| 6 | # |
| 7 | # FLTK copyright 1998-1999 by Bill Spitzak and others. |
| 8 | # pyFLTK copyright 2003 by Andreas Held and others. |
| 9 | # Copyright 2024 - Gonzalo Garramuño. All rights reserved. |
| 10 | # |
| 11 | # This library is free software you can redistribute it and/or |
| 12 | # modify it under the terms of the GNU Library General Public |
| 13 | # License, version 2.0 as published by the Free Software Foundation. |
| 14 | # |
| 15 | # This library is distributed in the hope that it will be useful, |
| 16 | # but WITHOUT ANY WARRANTY without even the implied warranty of |
| 17 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 18 | # Library General Public License for more details. |
| 19 | # |
| 20 | # You should have received a copy of the GNU Library General Public |
| 21 | # License along with this library if not, write to the Free Software |
| 22 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
| 23 | # USA. |
| 24 | # |
| 25 | # Please report all bugs and problems to "[email protected]". |
| 26 | # |
| 27 | |
| 28 | |
| 29 | from fltk import * |
| 30 | |
| 31 | # |
| 32 | # Constants |
| 33 | # |
| 34 | ROUND_SIZE = 5 |
| 35 | |
| 36 | def cb_menu(o, data): |
| 37 | print('Menu value=',o.value()) |
| 38 | |
| 39 | class RoundMenuButton(Fl_Menu_Button): |
| 40 | def __init__(self, X, Y, W, H, L = None): |
| 41 | super().__init__(X, Y, W, H, L) |
| 42 | self.box(FL_NO_BOX) |
| 43 | self.align(FL_ALIGN_INSIDE | FL_ALIGN_CENTER) |
| 44 | self.labelsize(16) |
| 45 | self.labelcolor(FL_WHITE) |
| 46 | |
| 47 | def handle(self, event): |
| 48 | if event == FL_PUSH: |
| 49 | # Turn Left Mouse Button clicks into Right Mouse Button clicks |
| 50 | if Fl.event_button1(): |
| 51 | Fl.e_keysym.fset(FL_Button + 3) |
| 52 | print(f"Fl.e_keysym is now: {Fl.e_keysym.fget()}") |
| 53 | return super().handle(event) |
| 54 | |
| 55 | def draw(self): |
| 56 | X = self.x() + ROUND_SIZE |
| 57 | Y = self.y() |
| 58 | W = self.w() - ROUND_SIZE * 2 |
| 59 | H = self.h() |
| 60 | |
| 61 | fl_color(self.color()-5) |
| 62 | |
| 63 | fl_rounded_rectf(X, Y, W, H, ROUND_SIZE) |
| 64 | |
| 65 | self.draw_label() |
| 66 | |
| 67 | |
| 68 | if __name__ == "__main__": |
| 69 | win = Fl_Window(640, 480) |
| 70 | menu = RoundMenuButton( 30, 20, 300, 30, "Menu should appear with LMB or RMB") |
| 71 | menu.add('Do something') |
| 72 | menu.add('Do another') |
| 73 | menu.callback(cb_menu, None) |
| 74 | win.show() |
| 75 | Fl.run() |