Code covered by the BSD License  

Highlights from
GUI Scripter

from GUI Scripter by Martin Dale
Creates an M-File from a Visual Basic 6.0 Project GUI Interface

scriptcontrol( vb, fid, controltype )
function scriptcontrol( vb, fid, controltype )
%SCRIPTCONTROL Summary of this function goes here
%   Scripts a control from a Visual Basic Object
%   Font variations and pictures on buttons are supported.

%   Martin Dale, July 2002
%   Copyright 2002   All Rights Reserved.
%   Version 1.00   9 June 2002
%   Contact Martin.Dale@Physics.org


switch(controltype)
case('radiobutton')
    vbname='optionbutton';
    
case{'slider'}
    bars=fieldnames(vb.vscrollbar);
    for i=1:length(bars)
        eval(['vb.scrollbar.',bars{i},'=vb.vscrollbar.',bars{i},';']);
    end
    
    bars=fieldnames(vb.hscrollbar);
    for i=1:length(bars)
        eval(['vb.scrollbar.',bars{i},'=vb.hscrollbar.',bars{i},';']);
    end
    vbname='scrollbar';

    
case('popupmenu')
    vbname='combobox';
    
case('pushbutton')
    vbname='commandbutton';
    
case('edit')
    vbname='textbox';
  
case('axes')
    vbname='picturebox';
case('text')
    vbname='label';

otherwise
    vbname=controltype;
end

    
controls=fieldnames(eval(['vb.',vbname]));
for i=1:length(controls)
   this=eval(['vb.',vbname,'.',char(controls(i))]);
   info_fields=fieldnames(this);

   fprintf(fid,'\n'); 
   %create structure with all control information (height width etc) to store in userdata property
   for j=1:length(info_fields)
      str=['control_info.',info_fields{j},'=''',num2str(eval(['this.',info_fields{j}])),''';'];
      fprintf(fid,[str,'\n']);
   end

   str=['formsize=get(',this.virtualparent,',''position'')*20;'];
   fprintf(fid,[str,'\n']);

   %get position relative to virtual parent
   if isempty([findstr(this.virtualparent,'form') findstr(this.virtualparent,'frm')])
      % set the position of the control relative to its virtual parent i.e. a frame
      str=['pos=[formsize(1)+',num2str(this.left),', formsize(2)+formsize(4)-',num2str(this.top),'-',num2str(this.height),', ',num2str(this.width),',',num2str(this.height),']/20;'];
   else
      % set the position of the control relative to its parent i.e. a figure window
      str=['pos=[',num2str(this.left),', formsize(4)-',num2str(this.top),'-',num2str(this.height),',',num2str(this.width),',',num2str(this.height),']/20;'];   
   end
   fprintf(fid,[str,'\n']);

   switch(controltype)
   case{'popupmenu','listbox'}
       this.caption=script_getlist(this.list, this.binary_offset);
       for item=1:length(this.caption)
           str=[this.handle,'.caption{',num2str(item),'}=''',this.caption{item},''';'];
           fprintf(fid,[str,'\n']);
       end
       str=[this.handle,'=uicontrol(''Style'',''',controltype,''',''String'',',this.handle,'.caption,''Units'',''Points'',''Position'',pos,''userdata'',control_info,''backgroundcolor'',''white'');'];
       fprintf(fid,[str,'\n']);
       
   case('slider')
       str1=[this.handle,'=uicontrol(''Style'',''slider'',''Units'',''Points'',''Position'',pos,''userdata'',control_info);'];
       fprintf(fid,[str1,'\n']);

   case('edit')
       str1=[this.handle,'=uicontrol(''Style'',''edit'',''String'',''',this.text,''',''Units'',''Points'',''Position'',pos,''userdata'',control_info,''backgroundcolor'',''white'');'];
       fprintf(fid,[str1,'\n']);

   case('axes')
       str1=[this.handle,'=axes(''Units'',''Points'',''Position'',pos,''userdata'',control_info);'];
       fprintf(fid,[str1,'\n']);

   otherwise
       this.caption = strrep(this.caption,'&','');
       str1=[this.handle,'=uicontrol(''Style'',''',controltype,''',''String'',''',this.caption,''',''Units'',''Points'',''Position'',pos,''userdata'',control_info);'];
       fprintf(fid,[str1,'\n']);
   end

   str2=['handlelist.',this.handle,'=',this.handle,';'];
   fprintf(fid,[str2,'\n']);
   
   if strcmp(controltype,'radiobutton')    
       str2=['set(',this.handle,',''Callback'',','''script_radiogroup(gco)'');'];
       str3=['parent_data=get(',this.virtualparent,',''userdata'');'];
       str4=['if ~isfield(parent_data,''radiolist'')'];
       str5=['parent_data.radiolist(1)=',this.handle,';'];
       str6=['else'];
       str7=['parent_data.radiolist(end+1)=',this.handle,';'];
       str8=['end'];              
       fprintf(fid,[str2,'\n',str3,'\n',str4,'\n',str5,'\n',str6,'\n',str7,'\n',str8,'\n']);
       
       str=['set(',this.virtualparent,',''userdata'',parent_data);'];
       fprintf(fid,[str,'\n']);
   end
   
   if strcmp(controltype,'frame')
       % place name at top of frame
       str1=[this.handle,'_label=uicontrol(''Style'',''text'',''String'',''',this.caption,''',''BackGroundColor'',get(',this.handle,',''BackGroundColor''),''Units'',''Points'');'];
       str2=['ext=get(',this.handle,'_label,''Extent'');'];
       str3=['set(',this.handle,'_label,''Position'',[pos(1)+9, pos(2)+pos(4)-0.5*ext(4), ext(3), ext(4)]);'];
       fprintf(fid,[str1,'\n',str2,'\n',str3,'\n']);

       str2=['handlelist.',this.handle,'_label','=',this.handle,'_label;'];
       fprintf(fid,[str2,'\n']);
              
       str1=['control_info.framelabel=''',this.handle,'_label'';'];
       str2=['set(',this.handle,',''userdata'',control_info);'];
       fprintf(fid,[str1,'\n',str2,'\n']);
       
   end

   
   %start of processing for other properties of the control, i.e. font, tag, tooltips etc.
   if isfield(this,'tag')
      str=['set(',this.handle,',''tag'',''',this.tag,''');'];
      fprintf(fid,[str,'\n']);
   end
   if isfield(this,'tooltiptext')
      str=['set(',this.handle,',''tooltipstring'',''',this.tooltiptext,''');'];
      fprintf(fid,[str,'\n']);
   end
   
   if isfield(this,'name')
      str=['set(',this.handle,',''fontname'',''',this.name,''',''fontsize'',',num2str(round(this.size)),');'];
      fprintf(fid,[str,'\n']);
      if this.weight==700
         this.weight='bold';
      else
         this.weight='normal';
      end
      if this.italic==-1
         this.italic='italic';
      else
         this.italic='normal';
      end
      str=['set(',this.handle,',''fontweight'',''',this.weight,''',''fontangle'',''',this.italic,''');'];
      fprintf(fid,[str,'\n']);
   end
   
   %if picture is a field then stick the CData on the object
   if isfield(this,'picture')
      [X, map]=VB_ReadBMP(this.picture, this.binary_offset);
      X=ind2rgb(X,map);
      imwrite(X, ['.\matlab_gui\',this.handle,'.bmp'],'bmp');
      str1=['[X,map]=imread(''',this.handle,'.bmp'');'];
      str2=['set(',this.handle,',''CData'',X);'];
      str3=['set(',this.handle,',''string'','''');'];
      fprintf(fid,[str1,'\n',str2,'\n',str3,'\n']);
   end
   
   str=['clear control_info;'];
   fprintf(fid, [str,'\n\n']);
end
return;

Contact us at files@mathworks.com