comp.lang.ada
 help / color / mirror / Atom feed
* Win32 dialogs/resource grief (longish)
@ 2000-03-01  0:00 John English
  2000-03-01  0:00 ` David Botton
  2000-03-02  0:00 ` Jerry van Dijk
  0 siblings, 2 replies; 6+ messages in thread
From: John English @ 2000-03-01  0:00 UTC (permalink / raw)


I have a problem with an apparent bug to do with using resources
with the Win32 binding -- long post, sorry, but it's stripped as
far down as I can manage. I'm using Gnat 3.12 here. Any advice
*much* appreciated -- I'm running out of walls to bang my head
against :-(

I have two simple dialogs: one with a label, combo box and OK/Cancel
buttons, another with a label and Yes/No/Cancel buttons. They work
fine individually, but not together. I've tried putting the dialogs
in the same resource file and in separate files; it makes no difference.

The test program (test_dialogs.adb) looks like this:

  with Test_YesNo;            -- 1
  with Test_Combo;            -- 2
  procedure Test_Dialogs is
  begin
    Test_YesNo.Do_Dialog;     -- 3
    Test_Combo.Do_Dialog;     -- 4
  end Test_Dialogs;

The Yes/No dialog fails unless both lines 2 and 4 are commented out.
Commenting out line 4 only has no effect. I've also seen the combo
box dialog fail unless lines 1 and 3 are commented out. The fact that
the "with" clause needs to be commented out strikes me as significant,
but I don't know exactly what it signifies -- any ideas, anyone? Using
"GetLastError" and "FormatError" to find out what the error in the call
to DialogBox is, I get one of two messages: "The specified procedure
could not be found", or more puzzlingly, "The operation completed
successfully" (this when DialogBox returns -1 to signify failure!).

The resources were compiled using the Borland resource compiler "brcc32"
from the BC++ 5.02 distribution and converted to COFF files using
res2coff
from Gnat 3.12. Here's the compilation sequence:

  brcc32 combodlg.rc
  res2coff -i combodlg.res -o combodlg.o
  brcc32 yesnodlg.rc
  res2coff -i yesnodlg.res -o yesnodlg.o
  gnatmake test_dialogs.adb

The packages specs referenced on lines 1 and 2 of Test_Dialogs are as
follows:

  package Test_Combo is
    procedure Do_Dialog;
  end Test_Combo;

  package Test_YesNo is
    procedure Do_Dialog;
  end Test_YesNo;

The bodies are as follows:

  with Win32, Win32.WinMain, Win32.WinDef, Win32.WinUser,
       Ada.Text_IO, System, Interfaces.C, Test_Common;
  use  Test_Common;
  package body Test_Combo is
    pragma Linker_Options("combodlg.o");
    use type Win32.Char_Array;

    COMBOBOX  : constant := 1020;  -- combobox control ID from
combodlg.rc

    function ComboDlg_Handler (hwnd   : Win32.WinDef.HWND;
                               msg    : Win32.UINT;
                               wParam : Win32.WPARAM;
                               lParam : Win32.LPARAM) return Win32.BOOL;
    pragma Convention (Stdcall, ComboDlg_Handler);

    Result  : Integer;
    Options : constant array (1..3) of Win32.Char_Array(1..6) := 
              ("Yes  " & Win32.NUL,
               "No   " & Win32.NUL,
               "Maybe" & Win32.NUL);

    function ComboDlg_Handler (hwnd   : Win32.WinDef.HWND;
                               msg    : Win32.UINT;
                               wParam : Win32.WPARAM;
                               lParam : Win32.LPARAM) return Win32.BOOL
is
      Dummy : Win32.LRESULT;
    begin
      case msg is
        when Win32.WinUser.WM_INITDIALOG =>
          for I in Options'Range loop
            Dummy := Win32.WinUser.SendMessage 
                        (Win32.WinUser.GetDlgItem(hwnd,COMBOBOX),
                         Win32.WinUser.CB_ADDSTRING, 0,
                         CP(Options(I)));
          end loop;
          return Win32.TRUE;
        when Win32.WinUser.WM_COMMAND =>
          case wParam is
            when Win32.WinUser.IDOK =>
              Result := Integer(Win32.WinUser.SendMessage 
                                 
(Win32.WinUser.GetDlgItem(hwnd,COMBOBOX),
                                   Win32.WinUser.CB_GETCURSEL, 0, 0));
              return Win32.WinUser.EndDialog (hwnd, Win32.TRUE);
            when Win32.WinUser.IDCANCEL =>
              return Win32.WinUser.EndDialog (hwnd, Win32.FALSE);
            when others =>
              return Win32.FALSE;
          end case;
        when others =>
          return Win32.FALSE;
      end case;                         
    end ComboDlg_Handler;

    procedure Do_Dialog is
      I : Interfaces.C.Int;
    begin
      Ada.Text_IO.Put_Line ("--- Combo box dialog test ---");
      I := Win32.WinUser.DialogBox
                  (Win32.WinMain.Get_hInstance,
                   CP("ComboDlg"),
                   System.Null_Address,
                   ComboDlg_Handler'Access);
      Ada.Text_IO.Put_Line ("Return value:" &
Integer'Image(Integer(I)));
      Ada.Text_IO.Put_Line ("Result:" & Integer'Image(Result));
    end Do_Dialog;
  end Test_Combo;

  with Win32, Win32.WinDef, Win32.WinMain, Win32.WinUser, Interfaces.C,
       Ada.Text_IO, Ada.Unchecked_Conversion, System, Test_Common;
  use  Test_Common;
  package body Test_YesNo is
    pragma Linker_Options("yesnodlg.o");
    use type Interfaces.C.Unsigned;

    function YesNoDlg_Handler (hwnd   : Win32.WinDef.HWND;
                               msg    : Win32.UINT;
                               wParam : Win32.WPARAM;
                               lParam : Win32.LPARAM) return Win32.BOOL;
    pragma Convention (Stdcall, YesNoDlg_Handler);

    Result : Boolean;

    function YesNoDlg_Handler (hwnd   : Win32.WinDef.HWND;
                               msg    : Win32.UINT;
                               wParam : Win32.WPARAM;
                               lParam : Win32.LPARAM) return Win32.BOOL
is
    begin
      case msg is
        when Win32.WinUser.WM_INITDIALOG =>
          return Win32.TRUE;
        when Win32.WinUser.WM_COMMAND =>
          case wParam is
            when Win32.WinUser.IDYES | Win32.WinUser.IDNO =>
              Result := (wParam = Win32.WinUser.IDYES);
              return Win32.WinUser.EndDialog (hwnd, Win32.TRUE);
            when Win32.WinUser.IDCANCEL =>
              return Win32.WinUser.EndDialog (hwnd, Win32.FALSE);
            when others =>
              return Win32.FALSE;
          end case;
        when others =>
          return Win32.FALSE;
      end case;                         
    end YesNoDlg_Handler;

    procedure Do_Dialog is
      I : Interfaces.C.Int;
    begin
      Ada.Text_IO.Put_Line ("--- Yes/no dialog test ---");
      I := Win32.WinUser.DialogBox
                  (Win32.WinMain.Get_hInstance,
                   CP("YesNoDlg"),
                   System.Null_Address,
                   YesNoDlg_Handler'Access);
      Ada.Text_IO.Put_Line ("Return value:" &
Integer'Image(Integer(I)));
      Ada.Text_IO.Put_Line ("Result: " & Boolean'Image(Result));
    end Do_Dialog;
  end Test_YesNo;

The package Test_Common contains a couple of type conversion routines:

  with Win32, Interfaces.C;
  package Test_Common is
    function CP(C_Str : Win32.CHAR_Array) return Interfaces.C.LONG;
    function CP(C_Str : Win32.CHAR_Array) return Win32.LPCSTR;
  end Test_Common;

  with Win32, System, Interfaces.C, Ada.Unchecked_Conversion;
  package body Test_Common is
    use type Interfaces.C.Unsigned_Long;
  
    function CP(C_Str : Win32.CHAR_Array) return Interfaces.C.LONG is
      function UC is new Ada.Unchecked_Conversion
                             (System.Address,Interfaces.C.LONG);
    begin
      return UC(C_Str(C_Str'First)'Address);
    end CP;

    function CP(C_Str : Win32.CHAR_Array) return Win32.LPCSTR is
      function UC is new Ada.Unchecked_Conversion
                             (System.Address,Win32.LPCSTR);
    begin
      return UC(C_Str(C_Str'First)'Address);
    end CP;
  end Test_Common;

The resources are defined thusly:

  /*--- yesnodlg.rc ---*/
  YesNoDlg DIALOG 6, 15, 166, 39
  STYLE DS_3DLOOK | DS_CENTER | 0x200L | WS_POPUP | WS_VISIBLE |
WS_CAPTION
  CAPTION "Yes/no dialog"
  FONT 9, "Arial"
  {
   LTEXT "Press a button:", 1004, 8, 4, 131, 12
   DEFPUSHBUTTON "Yes", IDYES, 12, 20, 42, 13
   PUSHBUTTON "No", IDNO, 64, 20, 42, 13
   PUSHBUTTON "Cancel", IDCANCEL, 116, 20, 42, 13
  }

  /*--- combodlg.rc ---*/
  ComboDlg DIALOG 6, 15, 147, 56
  STYLE DS_3DLOOK | DS_CENTER | 0x200L | WS_POPUP | WS_VISIBLE |
WS_CAPTION
  CAPTION "Input required"
  FONT 9, "Arial"
  {
   LTEXT "Choose a value:", 1002, 8, 4, 131, 12
   COMBOBOX 1020, 7, 18, 132, 78, CBS_DROPDOWNLIST | WS_CHILD |
WS_VISIBLE | WS_TABSTOP
   DEFPUSHBUTTON "OK", IDOK, 23, 36, 42, 13
   PUSHBUTTON "Cancel", IDCANCEL, 83, 36, 42, 13
  }

Please help! This is driving me mad!

-----------------------------------------------------------------
 John English              | mailto:je@brighton.ac.uk
 Senior Lecturer           | http://www.it.bton.ac.uk/staff/je
 Dept. of Computing        | ** NON-PROFIT CD FOR CS STUDENTS **
 University of Brighton    |    -- see http://burks.bton.ac.uk
-----------------------------------------------------------------




^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2000-03-02  0:00 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-03-01  0:00 Win32 dialogs/resource grief (longish) John English
2000-03-01  0:00 ` David Botton
2000-03-02  0:00 ` Jerry van Dijk
2000-03-01  0:00   ` David Botton
2000-03-02  0:00     ` John English
2000-03-02  0:00       ` David Botton

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox