Skip to content
hzhou edited this page Mar 12, 2012 · 5 revisions

GUI is one of the biggest progress we have made in computer history. Watching how our kids approaches computer, there is no doubt that forcing them back to console terminal is very silly. However, the current implementations of GUI are either very complicated -- Win32, X Window, or forces adoption of heavily layered object oriented programming system. The current dominant GUI system -- .NET, Object-C, Java -- are all object oriented languages. Although they still allow usage of non-OOP languages, it has to step around the OOP layers, resulting the same conclusion -- GUI programming is complicated and difficult.

If we take the metaphor that invention of GUI as the invention of wheel, then currently we have a couple of dominant wheels around the world, and each wheel are detail specified up to the restrictions that the design of the car has to follow. In fact, once we decided on using one of these wheels, the whole city, whole country has to be built around this choice.

We don't want our kids learn programming minus GUI, yet we hate to force our kids' vision to a limited programming paradigm. We believe we should return GUI to its basic idea of Model-View-Controller, just as we should learn the wheel as a round object with an axis.

With above philosophy, we started a Win32 module in MyDef to facilitate programming using Win32 API.

page: hello, basic_frame
    subcode: main_fn_list
	# NON-GUI Part
	# Any C code functions that lives outside GUI can be defined here
	# The Model part of the MVP will be defined here as well.

########## GUI Part ##################
fncode: WinMain
    $call win_main
    $call win_loop

# ---- Controller part for the main window -----------------
subcode: main_on_paint
    $g_font font_main
    $g_moveto 10, 10
    $g_text "Hello World!"
    # We could use straight gdi functions here.
    # By supplying shortcuts, we could normalize the graphic drawing routines so the code can be possibly reused on any drawing applications

subcode: main_on_click
    MessageBox(hwnd, "Button clicked!", "MsgBox", MB_OK)

subcode: main_on_file_open
    MessageBox(hwnd, "Menu -> File -> Open!", "MsgBox", MB_OK)

# ---- View part for  the main window ------------------
resource: view_main
    style: WS_OVERLAPPEDWINDOW
    size: 500, 350
    menu: main
    text: Hello World from MyDef
    label
        text: static label
        position: 5, 100
        size: 200, 20
        font: font_main
    button
        text: push me
        position: 5, 200
        size: 80, 40
        action: on_click
    editor
        position: 120, 50
        size: -10, -10

# ---- Controller part for the editor window -----------------
subcode: editor_on_paint
    $g_font font_editor
    TextOut(hdc, 10, 10, "MyDef Editor", 12)

resource: view_editor
    style: WS_CHILD|WS_VISIBLE|WS_BORDER

# ---- View part for the editor window -----------------------
resource: menu_main
    File
        Open
            action: on_file_open
        Close
        Exit
        ----
        Recent &Files
            File 1
            File 2
            File 3
    Edit
        Cut, Copy, Paste

#### Other resource definitions, defaults #################
resource: font_main
    lfFaceName: "Arial"
    lfHeight: 24

resource: font_editor
    lfFaceName: "Courier New"
    lfHeight: 16

Save to hello.def, run mydef_make.pl, select "win32" as module type, then run make, it will output hello.c.

Fire up "Visual Studio Command Prompt" (I suppose we could use mingw as well, but I haven't tested), get to the current directory and run cl /c hello.c followed with link /SUBSYSTEM:WINDOWS hello.obj user32.lib gdi32.lib, you should have hello.exe, which runs to produce the following:

Win32Hello.png

Certainly it will help a lot to have a GUI IDE that allows GUI design of the layout and selection of color, font, etc. We'll work on that or some one help us.

Clone this wiki locally