Recent

Author Topic: Grabbing bitmaps with fpGUI  (Read 4740 times)

RolandC

  • Guest
Grabbing bitmaps with fpGUI
« on: June 14, 2016, 09:21:51 am »
Hello!

I made a graphical chessboard based upon fpGUI and BGRABitmap. I would like to change the method for moving pieces: I would like to grab the pieces with the mouse.

I succeeded to do that in a LCL based project. How could I get the same effect with fpGUI?

I attach the two projects.

Graeme

  • Hero Member
  • *****
  • Posts: 1430
    • Graeme on the web
Re: Grabbing bitmaps with fpGUI
« Reply #1 on: June 14, 2016, 12:31:37 pm »
Which version of fpGUI are you using? The latest released fpGUI v1.4.1 or the develop branch.

The reason I ask, DND support has changed slightly in the develop branch. Either way, take a look at the drag and drop demo included with fpGUI. It can be found at <fpgui>/examples/gui/drag_n_drop/
--
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

RolandC

  • Guest
Re: Grabbing bitmaps with fpGUI
« Reply #2 on: June 14, 2016, 01:14:11 pm »
Thank you for your answer. I use v1.4.1.

OK, I will study the drag and drop demo.

Graeme

  • Hero Member
  • *****
  • Posts: 1430
    • Graeme on the web
Re: Grabbing bitmaps with fpGUI
« Reply #3 on: June 14, 2016, 01:34:18 pm »
OK, I will study the drag and drop demo.
There are lots of code comments to help explain the code. If you have any further questions, don't hesitate to ask here (or preferably in the official fpGUI support newsgroup).

In summary, fpGUI's drag-n-drop is pretty straight forward. A form has a DNDEnabled property that must be set to True to enable DND for that form. Each widget has the following properties:

    AcceptDrops: boolean
    OnDragEnter
    OnDragLeave
    OnDragDrop
    OnDragStartDetected

Those should be pretty self explanatory. DND data is sent around inside a TfpgDrag instance and normally uses mime types to identify different types of data. The receiving widget will check the available mime types and then decide if it will accept the drop or not.

Here is an example OnDragStartDetected event handler that initiates a DND action. This example comes from the DND example included with fpGUI. For your own applications or internal DND actions, you can define your own mime types - it's just a string type.

Code: Pascal  [Select]
  1. procedure TMainForm.LabelDragStartDetected(Sender: TObject);
  2. var
  3.   m: TfpgMimeData;
  4.   d: TfpgDrag;
  5. begin
  6.   m := TfpgMimeData.Create;
  7.  
  8.   { via convenience properties }
  9.   m.Text := 'My name is Earl';
  10.   m.HTML := 'My name is <b>Earl</b>';
  11.   { Could also have used the generic SetData function }
  12. //  m.SetData('text/plain', 'My name is Earl');
  13. //  m.SetData('text/html', 'My name is <b>Earl</b>');
  14.  
  15.   { tell TfpgDrag who is the Source of the drag }
  16.   d := TfpgDrag.Create(Sender as TfpgWindow);
  17.  
  18.   { TfpgDrag now takes ownership of TfpgMimeData }
  19.   d.MimeData := m;
  20.  
  21.   { TfpgDrag instance will be freed later when DND action is completed }
  22.   d.Execute([daCopy]);
  23. end;
  24.  

Here is an example of a OnDragEnter event handler. In this example, the widget only accepts data with the mime type 'text/plain'.

Code: Pascal  [Select]
  1. procedure TMainForm.Edit1DragEnter(Sender, Source: TObject;
  2.   AMimeList: TStringList; var AMimeChoice: TfpgString;
  3.   var ADropAction: TfpgDropAction; var Accept: Boolean);
  4. var
  5.   s: string;
  6. begin
  7.   ShowMimeList(AMimeList);
  8.   s := 'text/plain';
  9.   if chkAccept.Checked then
  10.     Accept := False
  11.   else
  12.     Accept := AMimeList.IndexOf(s) > -1;
  13.   if Accept then
  14.   begin
  15.     if AMimeChoice <> s then
  16.       AMimeChoice := s;
  17.   end;
  18. end;
  19.  
« Last Edit: June 14, 2016, 01:37:58 pm by Graeme »
--
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

RolandC

  • Guest
Re: Grabbing bitmaps with fpGUI
« Reply #4 on: June 14, 2016, 02:08:33 pm »
Thank you very much for that detailed explanation. I will study all that. If I don't succeed to solve my problem, I will ask the question here or in the official fpGUI support newsgroup.
« Last Edit: June 14, 2016, 02:10:47 pm by RolandC »