Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
variable scope : want to be sure
#1
Hi Gintaras,

as posted in BitDuJour, i struggle with a project now.

I want to be more informed about a special case:

I have a toolbar linked to a program.
This toolbar contains several macros in it (5 at the moment).

1) The first function in toolbar collects datas from the program interface (some texts, some buttons coordinates etc etc).
What is correct way to have those variables initialized when i launch the first macro or function, and then make them available for
*any* macros involved in the toolbar only, and not others?

2) How to declare a variable that can't be modified after (some static thing)

3) how to declare variable that can be modified by any other macros/functions?

4) can I reset the variables by running the first macro again, or is it mandatory to restart the toolbar?

5) how QM handle memory for those specific variables once I close the linked program or the toolbar? automatic or I must write some cleaning code?

Thanks,
laurent
#2
QM does not have a toolbar scope for variables.
In this case you can use global variables.
All global variables are available in all macros. Use unique names to avoid conflicts.
QM does not have read-only variables.
If need to initialize/clear variables when the toolbar is created/destroyed, you can do it in a toolbar hook function. Then also can allocate/attach non-global variables to the toolbar, but it is not easy.

Toolbar Toolbar47
Code:
Copy      Help
;/hook WndProc_ToolbarHook3
test :out g_tb47.text
Function WndProc_ToolbarHook3
Code:
Copy      Help
;/
function# hWnd message wParam lParam

;OutWinMsg message wParam lParam ;;uncomment to see received messages

;to avoid many global variables, use a user-defined type
type TB47VARIABLES
,x y
,str'text
TB47VARIABLES+ g_tb47

sel message
,case WM_INITDIALOG
,;you can initialize global variables here, or in other macro
,g_tb47.x=5
,g_tb47.text="toolbar"
,
,case WM_DESTROY
,;clear global variables here
,g_tb47.text.all

As always, use lock where a global variable may be accessed by multiple threads simultaneously.
#3
yep, thought about this, but using hook for initializing only a variable is not cheap....

I made a workaround, initializing the global variable when the main linked program is created, then locked it to prevent multiple use of routine on activation.

Others specificities for variable is useful.

Hope you'll sell much licences.

Back to code now....
#4
OK, just to be sure I did it the right way.

1) I made a macro triggered by the program window the toolbar is linked to.

int hwnd=val(_command)
int+ MyWindow=hwnd

But I wonder if directly using this is valid:

int+ MyWindow=val(_command)

2) The toolbar uses 5 macros or functions. In each one:

int+ MyWindow
Acc a.Find(MyWindow "TEXT" "" "class=TEdit[]xy=659 815" 0x1004)

Must I declare int+ MyWindow in each macro/function before using it or directly use MyWindow variable?

Thanks
#5
1. Valid.
2. int+ MyWindow is optional if variable MyWindow already exists when the macro runs first time.

If the macro or function always runs from the toolbar, you can use GetToolbarOwner.
Toolbar Toolbar48
Trigger !a"Untitled - Notepad" "Notepad"     Help - how to add the trigger to the macro
Code:
Copy      Help
Macro2121 :mac "Macro2121" "" GetToolbarOwner(TriggerWindow)
Function253 :Function253
Macro Macro2121
Code:
Copy      Help
;\
function hwnd
outw hwnd
Function Function253
Code:
Copy      Help
outw GetToolbarOwner(TriggerWindow)
#6
Yes!!!!

perfect solution, was what I looked for, as I suppose that TriggerWindow is both constant and
always available.

So, as long the toolbar exist, that means the linked window is active, so i don't need
anymore hook, int+ nor int w=find.. in any function used by the toolbar.

Brilliant workaround, much flexible.

Thanks Gintaras!!!
#7
Just puzzled by the syntax of first example:

noted macro, but have function statement in it.
Was expecting somthing like _command instead....

int hwnd=_command
#8
This example sends window handle in first argument, not in _command variable.
#9
Ok, will test...
#10
or
Toolbar Toolbar48
Trigger !a"Untitled - Notepad" "Notepad"     Help - how to add the trigger to the macro
Code:
Copy      Help
Macro2122 :mac "Macro2122" _command
Macro Macro2122
Code:
Copy      Help
;\
outw GetToolbarOwner(TriggerWindow)

_command in toolbar is toolbar handle as string. As well as in functions called from toolbar.
When using mac, the macro runs in other thread, therefore _command is empty if we don't pass it with mac.
TriggerWindow is same as val(_command).
#11
Ok, much clearer now.
Will play with this to achieve my goal...
#12
hmm..i'd like to tweak a bit more

my program is multi tabbed, and main window title changes depending on the tab selection.

it can be "Program - first module" "Program - second module" "Program - third module" or "Program - fourth module" for first tab, 2nd and so on.

The toolbar is unique for the main program, and I don't want to create 4 toolbars.

How to link, for instance, toolbar to tabs 1 and 3 and not show it on tab 2 and 4.

Filtering on "Program - " part of name only shows toolbar for all tabs, as expected but not desired,
and i did not find a way to use regex.
#13
Regex can be used to show but not hide the TB.
Use a hook function.

Toolbar Toolbar hide in some tabs
Trigger !a"Firefox" "MozillaWindowClass"     Help - how to add the trigger to the macro
Code:
Copy      Help
;/hook WndProc_ToolbarHook_hide_in_some_tabs
Function WndProc_ToolbarHook_hide_in_some_tabs
Code:
Copy      Help
;/
function# hWnd message wParam lParam

;OutWinMsg message wParam lParam ;;uncomment to see received messages

sel message
,case WM_INITDIALOG
,SetTimer hWnd 1 500 0
,
,case WM_TIMER
,str s.getwintext(GetToolbarOwner(hWnd)); err
,out s ;;delete this
,sel s 2
,,case ["Quick Macros *","* Stack Overflow - *"]
,,if(!hid(hWnd)) hid hWnd
,,case else
,,if(hid(hWnd)) hid- hWnd
,
,case WM_DESTROY
,

err+
#14
Perfect, was losing time trying to do that without hooking the toolbar.
Seems like I can't, but will be valuable for learning.

Thanks.


Forum Jump:


Users browsing this thread: 1 Guest(s)