[lazarus] Patch win32: combobox setting text/label

Micha Nelissen M.Nelissen at student.tue.nl
Wed Aug 27 05:13:35 EDT 2003


Hi,

Attached patch fixes:
1) Renames SendMessage to Windows.SendMessage; clearer and less overhead
2) Implements LM_SETLABEL, ie. TCustomComboBox.SetText.
3) when clearing combobox, remembers set text.

Major reduction in errors in object inspector.

Regards,

Micha.



Index: win32int.pp
===================================================================
RCS file: /FPC/CVS/projects/lazarus/lcl/interfaces/win32/win32int.pp,v
retrieving revision 1.35
diff -u -w -r1.35 win32int.pp
--- win32int.pp	26 Aug 2003 08:12:33 -0000	1.35
+++ win32int.pp	27 Aug 2003 09:08:32 -0000
@@ -77,7 +77,7 @@

     Procedure CreateComponent(Sender: TObject);
     Function RecreateWnd(Sender: TObject): Integer; virtual;
-    Function  GetText(Sender: TComponent; var Data: String): Boolean; virtual;
+    Function  GetText(Sender: TComponent; Handle: HWND; var Data: String): Boolean; virtual;
     Procedure SetLabel(Sender: TObject; Data: Pointer);
     Procedure AddChild(Parent, Child: HWND);
     Procedure ResizeChild(Sender: TObject; Left, Top, Width, Height: Integer);
Index: win32listsl.inc
===================================================================
RCS file: /FPC/CVS/projects/lazarus/lcl/interfaces/win32/win32listsl.inc,v
retrieving revision 1.16
diff -u -w -r1.16 win32listsl.inc
--- win32listsl.inc	26 Aug 2003 08:12:33 -0000	1.16
+++ win32listsl.inc	27 Aug 2003 09:08:32 -0000
@@ -86,7 +86,7 @@
                //Get edit and item sizes
                Windows.GetClientRect(FWin32List, at R);
                FEditHeight:= R.Bottom;
-               FItemHeight:= SendMessage(FWin32List,CB_GETITEMHEIGHT,0,0);
+               FItemHeight:= Windows.SendMessage(FWin32List,CB_GETITEMHEIGHT,0,0);
                FDDownCount:= TComboBox(FSender).DropDownCount;
                If FDDownCount = 0 then FDDownCount:= 8;
     	       end;
@@ -141,19 +141,28 @@
 Procedure TWin32ListStringList.Assign(Source: TPersistent);
 Var
   Counter: Integer;
+  EditText: String;
 Begin
   { Do not call inherited Assign as it does things we do not want to happen }
   If Source Is TStrings Then
   Begin
-    SendMessage(FWin32List,FFlagResetContent, 0, 0); // Clear;
+    // save any text in edit box
+    if FSender.FCompStyle=csComboBox then
+      CNSendMessage(LM_GETTEXT, FSender, @EditText);
+
+    Windows.SendMessage(FWin32List, FFlagResetContent, 0, 0);
     For Counter := TStrings(Source).Count - 1 DownTo 0 Do
-      SendMessage(FWin32List,FFlagAddString, 0, LPARAM(PChar(TStrings(Source)[Counter]))); //Insert
+      Windows.SendMessage(FWin32List, FFlagAddString, 0, LPARAM(PChar(TStrings(Source)[Counter]))); //Insert
+
     if FSender.FCompStyle=csComboBox Then
     begin
       if Count = 0 then
         SetComboHeight(FSender,FSender.Left,FSender.Top,FSender.Width,FEditHeight + FItemHeight + 2)
       else
         SetComboHeight(FSender,FSender.Left,FSender.Top,FSender.Width,FEditHeight + FDDownCount*FItemHeight + 2);
+
+      // restore text in edit box
+      CNSendMessage(LM_SETLABEL, FSender, PChar(EditText));
     end;
   End
   Else
@@ -174,8 +183,8 @@
     Raise Exception.Create('Out of bounds.')
   Else
   Begin
-  	Getmem(Item,SendMessage(FWin32List,FFlagGetTextLen,Index,0)+1);
-    SendMessage(FWin32List, FFlagGetText, Index, LPARAM(Item));
+  	Getmem(Item,Windows.SendMessage(FWin32List,FFlagGetTextLen,Index,0)+1);
+    Windows.SendMessage(FWin32List, FFlagGetText, Index, LPARAM(Item));
   End;
   Result := StrPas(Item);
   Dispose(Item);
@@ -189,7 +198,7 @@
  ------------------------------------------------------------------------------}
 Function TWin32ListStringList.GetCount: Integer;
 Begin
-  Result := SendMessage(FWin32List, FFlagGetCount, 0, 0);
+  Result := Windows.SendMessage(FWin32List, FFlagGetCount, 0, 0);
 End;

 {------------------------------------------------------------------------------
@@ -203,7 +212,7 @@
   if FSender.FCompStyle=csComboBox Then
     SetComboHeight(FSender,FSender.Left,FSender.Top,FSender.Width,
                    FEditHeight + FItemHeight + 2);
-  SendMessage(FWin32List,FFlagResetContent, 0, 0);
+  Windows.SendMessage(FWin32List,FFlagResetContent, 0, 0);
 End;

 {------------------------------------------------------------------------------
@@ -218,7 +227,7 @@
   and (GetCount <= 1) Then
     SetComboHeight(FSender,FSender.Left,FSender.Top,FSender.Width,
                    FEditHeight + FItemHeight + 2);
-  SendMessage(FWin32List,FFlagDeleteString, Index, 0);
+  Windows.SendMessage(FWin32List,FFlagDeleteString, Index, 0);
 End;

 {------------------------------------------------------------------------------
@@ -234,9 +243,9 @@
     SetComboHeight(FSender,FSender.Left,FSender.Top,FSender.Width,
                    FEditHeight + FDDownCount*FItemHeight + 2);
   If FSorted Then
-    SendMessage(FWin32List,FFlagAddString, 0, LPARAM(PChar(S)))
+    Windows.SendMessage(FWin32List,FFlagAddString, 0, LPARAM(PChar(S)))
   Else
-    SendMessage(FWin32List,FFlagInsertString, Index, LPARAM(PChar(S)));
+    Windows.SendMessage(FWin32List,FFlagInsertString, Index, LPARAM(PChar(S)));
 End;

 {*************************************************************}
@@ -315,7 +324,7 @@
  ------------------------------------------------------------------------------}
 Procedure TWin32CListStringList.Clear;
 Begin
-  SendMessage(FWin32CList, LB_RESETCONTENT, 0, 0);
+  Windows.SendMessage(FWin32CList, LB_RESETCONTENT, 0, 0);
 End;

 {------------------------------------------------------------------------------
@@ -326,7 +335,7 @@
  ------------------------------------------------------------------------------}
 procedure TWin32CListStringList.Delete(Index: Integer);
 begin
-  SendMessage(FWin32CList, LB_DELETESTRING, Index, 0);
+  Windows.SendMessage(FWin32CList, LB_DELETESTRING, Index, 0);
 end;

 {------------------------------------------------------------------------------
@@ -343,8 +352,8 @@
     Raise Exception.Create('Out of bounds.')
   Else
   Begin
-  	Getmem(Item,SendMessage(FWin32CList,LB_GETTEXTLEN,Index,0)+1);
-    SendMessage(FWin32CList, LB_GETTEXT, Index, LPARAM(Item));
+  	Getmem(Item,Windows.SendMessage(FWin32CList,LB_GETTEXTLEN,Index,0)+1);
+    Windows.SendMessage(FWin32CList, LB_GETTEXT, Index, LPARAM(Item));
     Result := StrPas(Item);
     Dispose(Item);
   End;
@@ -358,7 +367,7 @@
  ------------------------------------------------------------------------------}
 Function TWin32CListStringList.GetCount: Integer;
 Begin
-  Result := SendMessage(FWin32CList, LB_GETCOUNT, 0, 0);
+  Result := Windows.SendMessage(FWin32CList, LB_GETCOUNT, 0, 0);
 End;

 {------------------------------------------------------------------------------
@@ -369,7 +378,7 @@
  ------------------------------------------------------------------------------}
 Function TWin32CListStringList.GetObject(Index: Integer): TObject;
 Begin
-  HWND(Result) := SendMessage(FWin32CList, LB_GETITEMDATA, Index, 0);
+  HWND(Result) := Windows.SendMessage(FWin32CList, LB_GETITEMDATA, Index, 0);
 End;

 {------------------------------------------------------------------------------
@@ -381,9 +390,9 @@
 Procedure TWin32CListStringList.Insert(Index: Integer; Const S: String);
 Begin
   If FSorted Then
-    SendMessage(FWin32CList,LB_ADDSTRING, 0, LPARAM(PChar(S)))
+    Windows.SendMessage(FWin32CList,LB_ADDSTRING, 0, LPARAM(PChar(S)))
   Else
-    SendMessage(FWin32CList,LB_INSERTSTRING, Index, LPARAM(PChar(S)));
+    Windows.SendMessage(FWin32CList,LB_INSERTSTRING, Index, LPARAM(PChar(S)));
 End;

 {------------------------------------------------------------------------------
@@ -394,7 +403,7 @@
  ------------------------------------------------------------------------------}
 Procedure TWin32CListStringList.PutObject(Index: Integer; AObject: TObject);
 Begin
-  SendMessage(FWin32CList, LB_SETITEMDATA, Index, LPARAM(AObject));
+  Windows.SendMessage(FWin32CList, LB_SETITEMDATA, Index, LPARAM(AObject));
 End;

 {$IFDEF H_PLUS}
Index: win32object.inc
===================================================================
RCS file: /FPC/CVS/projects/lazarus/lcl/interfaces/win32/win32object.inc,v
retrieving revision 1.96
diff -u -w -r1.96 win32object.inc
--- win32object.inc	26 Aug 2003 08:12:33 -0000	1.96
+++ win32object.inc	27 Aug 2003 09:08:33 -0000
@@ -182,7 +182,7 @@
   Retrieves the text from a Windows control. This is a replacement for
   the LM_GetText message.
  ------------------------------------------------------------------------------}
-Function TWin32Object.GetText(Sender: TComponent; Var Data: String): Boolean;
+Function TWin32Object.GetText(Sender: TComponent; Handle: HWND; var Data: String): Boolean;
 Var
   CapLen: Cardinal;
   Caption: PChar;
@@ -192,20 +192,21 @@
   Assert(False, Format('Trace:TWin32Object.GetText - Start --> %S', [Sender.ClassName]));
   Data := '';
   Result := True;
-  Case (Sender as TControl).FCompStyle Of
+  Case TControl(Sender).FCompStyle Of
     csComboBox:
     Begin
-      CapLen := SendMessage((Sender As TWinControl).Handle, CB_GETLBTEXTLEN, CNSendMessage(LM_GETITEMINDEX, Sender, Nil), 0);
-      Caption := StrAlloc(CapLen + 1);
-      SendMessage((Sender As TWinControl).Handle, CB_GETLBTEXT, CNSendMessage(LM_GETITEMINDEX, Sender, Nil), LPARAM(Caption));
+      // + 1 = terminating null character
+      CapLen := Windows.SendMessage(Handle, WM_GETTEXTLENGTH, 0, 0) + 1;
+      Caption := StrAlloc(CapLen);
+      Windows.SendMessage(Handle, WM_GETTEXT, CapLen, LPARAM(Caption));
       Data := StrPas(Caption);
       StrDispose(Caption);
     End;
     csEdit, csMemo:
     Begin
-      CapLen := GetWindowTextLength((Sender As TWinControl).Handle);
+      CapLen := GetWindowTextLength(Handle);
       Caption := StrAlloc(CapLen + 1);
-      GetWindowText((Sender As TWinControl).Handle, Caption, CapLen + 1);
+      GetWindowText(Handle, Caption, CapLen + 1);
       Data := StrPas(Caption);
       StrDispose(Caption);
     End;
@@ -231,7 +232,6 @@
     Else
       Result := False;
   End;
-  // Result := Data <> '';
 End;

 {------------------------------------------------------------------------------
@@ -311,7 +311,10 @@
     Begin
       Assert(False, Format('Trace:TWin32Object.SetLabel - Got %S', [CS_To_String(TControl(Sender).FCompStyle)]));
       Assert(False, Format('Trace:TWin32Object.SetLabel - label --> %S', [String(PChar(Data))]));
-      Assert(False, 'Trace:TWin32Object.SetLabel - I''m not sure if this''ll work');
+      if TCustomComboBox(Sender).Style = csDropDownList then
+        Windows.SendMessage(Handle, CB_SELECTSTRING, -1, LPARAM(Data))
+      else
+        Windows.SendMessage(Handle, WM_SETTEXT, 0, LPARAM(Data));
     End;
     csMemo:
     Begin
@@ -844,7 +847,7 @@
         End;
         LM_GETTEXT :
         Begin
-          Result := Integer(GetText(Sender As TComponent,PString(Data)^));
+          Result := Integer(GetText(TComponent(Sender), Handle, PString(Data)^));
         End;
         LM_GETITEMINDEX :
         Begin
@@ -1805,6 +1808,10 @@
   //TCI: TC_ITEM;
 Const
   BitsPerPixel: Array[Boolean] Of Cardinal = (3, 1);
+  ComboBoxStyles: array[TComboBoxStyle] of DWORD = (
+    CBS_DROPDOWN, CBS_SIMPLE, CBS_DROPDOWNLIST,
+    CBS_DROPDOWNLIST or CBS_OWNERDRAWFIXED,
+    CBS_DROPDOWNLIST or CBS_OWNERDRAWVARIABLE);
 Begin
   Assert(False, 'Trace:CreateComponent - Start');
   Assert(False, 'Trace:CreateComponent - Value of Sender is $' + IntToHex(LongInt(Sender), 8));
@@ -1933,7 +1940,8 @@
     End;
     csComboBox:
     Begin
-      Window := CreateWindow('COMBOBOX', Nil, Flags Or CBS_DROPDOWN or WS_VSCROLL, Left, Top, Width, Height, Parent, HMENU(Nil), HInstance, Nil);
+      Flags := Flags or ComboBoxStyles[TCustomComboBox(Sender).Style];
+      Window := CreateWindow('COMBOBOX', Nil, Flags or WS_VSCROLL or CBS_AUTOHSCROLL or CBS_HASSTRINGS, Left, Top, Width, Height, Parent, HMENU(Nil), HInstance, Nil);
       SetProp(Window, 'Lazarus', Sender);
     End;
     csImage:





More information about the Lazarus mailing list