Custom data row drawing: e.g. adding icons to a drop box

General help with the Ecere Cross Platform GUI toolkit: Window, common controls, events, etc.
Help with the 2D Graphics library: Surface, Display, Bitmap, Font and others.
Post Reply
jerome
Site Admin
Posts: 608
Joined: Sat Jan 16, 2010 11:16 pm

Custom data row drawing: e.g. adding icons to a drop box

Post by jerome »

Hey guys,

Here is some sample code showing how to define a custom data type for use in data controls.

Image

It overrides the data type's OnDisplay() and OnGetString() methods to show the little flags next to each country.

I had to do a little patch for the DataBox to pass along the 'fieldData' (used to store the flag collection), so I included a new ecere.dll in there. A future release of Ecere should contain this fix. Bug me if you want to run this on Linux.

Grabbed the flags from http://www.famfamfam.com/lab/icons/flags/, thanks to thexa4 for pointing us there, and thanks to naji for asking about this :)

Here is a stripped down version of the code:

Code: Select all

import "ecere"
 
static String countryNames[CountryCodes] =
{
   "(none)"
   // Country names are here
};
 
class FlagCollection
{
   Array<BitmapResource> flags { };
   public property Window window { set { for(b : flags) { b.window = value; } } };
   FlagCollection()
   {
      CountryCodes c;
      flags.size = CountryCodes::enumSize;
      for(c = AD; c <= ZW; c++)
      {
         char tmp[10];
         char * s = ((char *(*)())(void *)class(CountryCodes).base._vTbl[4])(class(CountryCodes), &c, tmp, null, null);
         char fn[MAX_LOCATION];
         sprintf(fn, ":flags/%s.png", s);
         strlwr(fn);
         flags[c] = { fn };
         incref flags[c];
      }      
   }
   ~FlagCollection() { flags.Free(); }
}
 
enum CountryCodes
{
   none /* Country codes are here */;
 
   char * OnGetString(char * tempString, void * fieldData, bool * needClass)
   {
      return countryNames[this];
   }
 
   void OnDisplay(Surface surface, int x, int y, int width, FlagCollection flagCollection, Alignment alignment, DataDisplayFlags flags)
   {
      Bitmap icon = (this != none && flagCollection) ? flagCollection.flags[this].bitmap : null;
      int w = 8 + 16;
      if(icon)
         surface.Blit(icon, x + (16 - icon.width) / 2,y+2,0,0, icon.width, icon.height);
      class::OnDisplay(surface, x + w, y, width - w, null, alignment, flags);
   }
};
 
class CustomDataTypesForm : Window
{
   text = "Custom Data Types";
   background = activeBorder;
   borderStyle = sizable;
   hasMaximize = true;
   hasMinimize = true;
   hasClose = true;
   size = { 216, 112 };
 
   CountryCodes country;
 
   FlagCollection flags { this };
   SavingDataBox dropBox
   {
      this, text = "Please Select Your Country", size = { 176, 24 }, position = { 16, 40 },
      data = &country, type = class(CountryCodes), fieldData = flags
   };
   Label lblDropBox { this, font = { "Tahoma", 10, bold = true }, position = { 16, 16 }, labeledWindow = dropBox };
}
 
CustomDataTypesForm form {};
Enjoy.

Jerome
Attachments
flags.png
(10.37 KiB) Not downloaded yet
flags.7z
Countries & Flags Fun
(903.38 KiB) Downloaded 956 times
redj
Posts: 105
Joined: Sun Jan 17, 2010 10:03 am

Re: Custom data row drawing: e.g. adding icons to a drop box

Post by redj »

very nice :)
Post Reply