Little Dialog-wrapper for PythonScript
-
@Ekopalypse said in Little Dialog-wrapper for PythonScript:
If you need to add an additional control at a later time, then all you have to do is copy the newly generated line.
Sure, easy enough for one control, one line.
I was more thinking about how I tend to do things… on a greater than 1 line basis. :-)but that is already the case today as well.
Today we don’t have to think about script concurrency, because PS mostly prevents it. Anyway, just thought I’d raise the question.
-
oh no, it’s not a concurrency issue, it’s more like the callbacks we use today - event driven.
The script starts the dialog and ends. But the class that manages the dialog stays active until the dialog is closed. -
@Ekopalypse said in Little Dialog-wrapper for PythonScript:
And then I thought, what if … trailer … :-D
Any alpha / beta version ready to test yet? I keep checking your repo but don’t see any new commits. I have a pretty easy use case in mind - an edit box with checkboxes for regex and case insensitive - all for a filter search lifted from @Alan-Kilborn 's work!
Integrating my supreme Notepad++ experience by standing on the shoulders of giants!
Cheers.
-
Hi, I have quite a lot on my plate privately at the moment, but I promise to upload a new version this weekend.
It’s actually going quite well and if you stick to what’s currently implemented, there shouldn’t be any difficulties. Sorry for the delays. -
Repo has been updated, but be warned that I’m not completely satisfied yet, which means it’s not API stable. Further updates could break things that currently work.
-
There was a typo that prevented the dialog from being created.
I should have tested it before, sorry. Is fixed now. -
Great! I have my small example:
from WinDialog import Button, Dialog, CheckBoxButton, Label, TextBox from WinDialog.controls.button import BM, BST from WinDialog.win_helper import SendMessage user_input = '' REGEX = True IGNORECASE = True class FilerLinesEditDlg(Dialog): def __init__(self): super().__init__( title='Filter Lines Editing', size=(250, 75), center = True) self.label = Label( title='Filter for:' , position=(10, 12), size=(30, 11) ) self.edit = TextBox( position=(45, 10), size=(195, 14) ) self.case = CheckBoxButton(title='Case Sensitive' , position=(45, 30), size=(80, 14) ) self.regex = CheckBoxButton(title='Regular Expression' , position=(145, 30), size=(80, 14) ) self.ok = Button( title='OK' , position=(130, 55), size=(50, 11) ) self.cancel = Button( title='Cancel' , position=(187, 55), size=(50, 11) ) self.ok.on_click = self.on_ok self.cancel.on_click = self.on_cancel self.case.on_click = self.on_case self.regex.on_click = self.on_regex self.show() def initialize(self): global IGNORECASE global REGEX SendMessage(self.case.hwnd, BM.SETCHECK, IGNORECASE, 0) SendMessage(self.regex.hwnd, BM.SETCHECK, REGEX, 0) def on_ok(self): global user_input user_input = self.edit.get_text() self.terminate() def on_cancel(self): global user_input user_input = None self.terminate() def on_case(self): global IGNORECASE check = SendMessage(self.case.hwnd, BM.GETCHECK, 0, 0) if check & BST.CHECKED: IGNORECASE = True else: IGNORECASE = False def on_regex(self): global REGEX check = SendMessage(self.regex.hwnd, BM.GETCHECK, 0, 0) if check & BST.CHECKED: REGEX = True else: REGEX = False FilerLinesEditDlg() print(f"User: {user_input}") print(f"Case: {IGNORECASE}") print(f"Regex: {REGEX}")
It seems to run correctly and prints status to the PythonScript console. I plan to integrate this into a larger script.
I’m not sure I did the checkbox stuff correctly. It works, but I had to import some extra classes and the
SendMessage
function from your helper library. Did I do this right?Cheers.
-
You can do it this way, but I personally would put the globals as attributes in the class to avoid globals in the first place.
For the GETCHECK, because the checkbox is a “boolean” value, I would do something like: …... def on_case(self): self.IGNORECASE = not self.IGNORECASE ... dlg = FilerLinesEditDlg() print(f"User: {dlg.user_input}") print(f"Case: {dlg.IGNORECASE}") print(f"Regex: {dlg.REGEX}")
As for the SETCHECK, the SendMessage function is currently required.
The next version, uploaded this weekend, will have a checkbox button method setCheckState (that’s the same kind of naming PS does … so it seems to make the most sense, right?). And now that I think about it, getCheckState makes sense too because of the 3-state checkboxes. Also, the next version will be able to handle IDOK and IDCANCEL, which means you can close a dialog with ESC and trigger the execution of the edit with ENTER. -
@Ekopalypse said in Little Dialog-wrapper for PythonScript:
will have a checkbox button method setCheckState (that’s the same kind of naming PS does … so it seems to make the most sense, right?). And now that I think about it, getCheckState makes sense too because of the 3-state checkboxes. Also, the next version will be able to handle IDOK and IDCANCEL, which means you can close a dialog with ESC and trigger the execution of the edit with ENTER.
This just keeps getting better!
I updated the globals to a return class that I pass in and then examine on output. The issue is I want this as a prompt in a larger already existing PythonScript and class so the “globals” are just globals in this example - in my project, they are attributes of the existing class that the dialog will need to know to set on startup and then return in case of modifications in the dialog. The globals were just a “cheat” way to illustrate a “complete self-contained example” to post here. But point taken - I am avoiding globals in the finished product.
The
on_case
recommendation works as well very nicely and certainly simplifies code - removes the need for me obtainingBM.GETCHECK
as well.It’s Memorial Day weekend in the States so I’ll be stepping away from all this for the long weekend. I do hope you take time to relax this - and any weekend for that matter. I’m in no rush for the updates, but will keep checking back the repo to see when they in fact do arrive.
UPDATE:
The updated script from above with fixes discussed. Also add thec#_
prefixes to the controls to force the TABSTOP order - seems to be alphabetical:from Npp import editor #---------- from WinDialog import Button, CheckBoxButton, DefaultButton, Dialog, Label, TextBox from WinDialog.controls.button import BM from WinDialog.win_helper import SendMessage #---------- class Returns(object): def __init__(self, U=None, I=False, R=False): self.user_input = U self.IGNORECASE = I self.REGEX = R class FilerLinesEditDlg(Dialog): def __init__(self, ret=Returns()): super().__init__( title='Filter Lines Editing', center = True, size=(250, 75) ) self.label = Label( title='Filter for:' , position=(10, 12), size=(30, 11) ) self.c2_edit = TextBox( position=(45, 10), size=(195, 14) ) self.c3_case = CheckBoxButton(title='Case Sensitive' , position=(45, 30), size=(80, 14) ) self.c4_regex = CheckBoxButton(title='Regular Expression' , position=(145, 30), size=(80, 14) ) self.c1_ok = DefaultButton( title='OK' , position=(130, 55), size=(50, 11) ) self.c5_cancel = Button( title='Cancel' , position=(187, 55), size=(50, 11) ) self.ret = ret self.c1_ok.on_click = self.on_ok self.c5_cancel.on_click = self.on_cancel self.c3_case.on_click = self.on_case self.c4_regex.on_click = self.on_regex self.show() def initialize(self): self.c2_edit.set_text(self.ret.user_input) SendMessage(self.c3_case.hwnd, BM.SETCHECK, self.ret.IGNORECASE, 0) SendMessage(self.c4_regex.hwnd, BM.SETCHECK, self.ret.REGEX, 0) def on_ok(self): self.ret.user_input = self.c2_edit.get_text() self.terminate() def on_cancel(self): self.ret.user_input = None self.terminate() def on_case(self): self.ret.IGNORECASE = not self.ret.IGNORECASE def on_regex(self): self.ret.REGEX = not self.ret.REGEX #-----^^^^----- user_input = editor.getSelText() IGNORECASE = False REGEX = True #-----vvvv----- ret = Returns(user_input, IGNORECASE, REGEX) FilerLinesEditDlg(ret) user_input = ret.user_input IGNORECASE = ret.IGNORECASE REGEX = ret.REGEX #-----^^^^----- if ret.user_input is None: print("EXIT") else: print(f"User: {user_input}") print(f"Case: {IGNORECASE}") print(f"Regex: {REGEX}")
Cheers.
-
Don’t worry, the weekends are for family commitments. I’ll just publish what I’ve managed to do during the week.
As for the WS_TABSTOP, I thought that the order of creation of the controls determines the order … I’ll have to check that. -
@Michael-Vincent said in Little Dialog-wrapper for PythonScript:
Also add the c#_ prefixes to the controls to force the TABSTOP order
Confused me at first; I’m wondering what the heck C# has to do with this…
Finally I notice this is what you meant:
-
The issue is here.
dir
always returns a sorted list.Replacing it with
for item in self.__dict__.keys():
seems to behave as expected. The first control created that has the style ws_tabstop gets the initial focus. -
First beta version released.
Sorry for breaking existing code, but from now on this should not happen.The current state of the project is what I currently support.
If you encounter issues where the dialog box or controls do not work as described or advertised,
please report them by submitting a “git issue”. I will diligently investigate the issue and work on a solution.However, please note that any requests for additional functionality beyond what is currently offered will be considered feature requests.
While I appreciate user feedback, I prioritize implementing features that benefit me personally and align with the overall goals of the project,
which is to be a simple wrapper over DialogBoxIndirectParam Windows API.
Thank you for your understanding."