September 26, 2022, 12:06:37 AM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


5b. Adding Code

Started by LarryMc, October 14, 2014, 08:25:57 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

LarryMc

In this section we will address the creation of our applications parent window along with the basic message handler to go with it.

In a Single File application it would look something like this:
window w1
openwindow w1, 0, 0, 800, 700, @CAPTION|@SYSMENU, 0, "myApp", &w1_Handler

WAITUNTIL iswindowclosed(w1)
END

SUB w1_Handler(),INT
  SELECT @MESSAGE
CASE @IDCREATE
centerwindow w1
case @idcontrol
SELECT @controlid

ENDSELECT
CASE @IDCLOSEWINDOW
closewindow w1
endselect
return 0
endsub


Referring to our original scope:
1. A WINDOWS based application containing some edit controls and buttons.
  a) the window setup in a subroutine in its own file
  b) the window message handler is in the main window


To comply with 1a. above we place the OPENWINDOW line in the mainsetup.iwb file in its own subroutine.
This will result in the mainsetup.iwb file looking like this:
$include "globals.iwb"

global sub InitMain()

openwindow w1, 0, 0, 800, 700, @CAPTION|@SYSMENU, 0, "myApp", &w1_Handler

return
endsub

Notice the use of the GLOBAL keyword preceding SUB.
This is a requirement for any subroutine that will be called from a different source file.

To comply with 1b. above we place the remaining code in the main.iwb file. To that we add a call to the InitMain routine
This will result in the main.iwb file looking like this:
$MAIN
$include "globals.iwb"

InitMain()



WAITUNTIL iswindowclosed(w1)
END

global SUB w1_Handler(),INT
  SELECT @MESSAGE
CASE @IDCREATE
centerwindow w1
case @idcontrol
SELECT @controlid
'CASE ID_GRID1
' SELECT @NOTIFYCODE

' ENDSELECT
ENDSELECT
CASE @IDCLOSEWINDOW
closewindow w1
endselect
return 0
endsub


Notice that we added the Global keyword for the same reason as previously stated.

Looking at the two source files we just edited you see that we have referenced the window w1 but we haven't added a declaration statement for it.  The question is where do we put.  Since w1 needs to be Project Global we'll put the declaration in the globals.iwb file which will now look like this:
AUTODEFINE "OFF" /* I always use this - personal preference */
$include "windowssdk.inc"  /* I always use this - personal preference */
'$include "Commctrl.inc"
'$include "ctl.inc"

/******* structure definitions ******************************************/
'$INCLUDE "struct.inc"

/******* end of structure definitions ***********************************/

/******* constants definitions ******************************************/
'$INCLUDE "const.inc"

/******* end of constants definitions ***********************************/

/******* extern definitions ******************************************/
'$INCLUDE "extern.inc"

/******* end of extern definitions ***********************************/

projectglobal "on"
window w1


projectglobal "off"


If you try to compile the project at this point you will get two errors.
The compiler will say it can't find InitMain() and w1_handler()
We declared the SUBs as being GLOBAL so why can't they be found?

By declaring the SUBs as GLOBAL we are only saying they are available to other source files.
What we haven't done is tell the other source files that the subroutines are available.

To take care of this we go back to the globals.iwb file
We add two lines to the externals section of the file which looks like this.
/******* extern definitions ******************************************/
'$INCLUDE "extern.inc"
declare extern w1_Handler(),INT
declare extern InitMain()
/******* end of extern definitions ***********************************/

And since the globals.iwb file is included in all the source files other than itself that takes care of letting all the source files know that the subroutines are available.

You may notice that with this arrangement we will wind up with
declare extern InitMain()
global sub InitMain()
in the same source file at compile time.
The same will occur with
declare extern w1_Handler(),INT
global sub w1_Handler

When this situation occurs the compiler just ignores the 'declare extern' lines.

If all he previous instructions have been followed you can now compile the application.
Running the program will result in the main window opening and being centered in the screen.

If you have tried previously to compile and nothing appeared to happen you may get a 'can not open myApp.exe' error.
If so, open task manager, and close all the myApp.exe entries then recompile and run.

We will revisit these two source files as we flesh out the rest of our application

This completes 5b. Adding Code
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library