CRhinoCommand::result CCommandSamplePan::RunCommand( const CRhinoCommandContext& context )
{
  const CRhinoAppViewSettings& view_settings = RhinoApp().AppSettings().ViewSettings();
  double d = view_settings.m_pan_increment;
  if (view_settings.m_pan_reverse_keyboard)
    d = -d;

  CRhinoGetOption go;
  go.SetCommandPrompt(L"Select pan option");
  go.AcceptNothing();

  const int down_option_index  = go.AddCommandOption(RHCMDOPTNAME(L"Down"));
  const int left_option_index  = go.AddCommandOption(RHCMDOPTNAME(L"Left"));
  const int right_option_index = go.AddCommandOption(RHCMDOPTNAME(L"Right"));
  const int up_option_index    = go.AddCommandOption(RHCMDOPTNAME( L"Up"));
  const int in_option_index    = go.AddCommandOption(RHCMDOPTNAME(L"In"));
  const int out_option_index   = go.AddCommandOption(RHCMDOPTNAME(L"Out"));

  for(;;)
  {
    CRhinoGet::result res = go.GetOption();

    if (res != CRhinoGet::option)
      break;

    CRhinoView* view = go.View(); 
    const CRhinoCommandOption* option = go.Option();

    if (0 != view && 0 != option && 0.0 != d)
    {
      CRhinoViewport* viewport = &(view->ActiveViewport());
      if (0 != viewport && viewport->View().m_bLockedProjection)
        viewport = &(view->MainViewport());

      if (0 != viewport)
      {
        if (down_option_index == option->m_option_index)
          viewport->DownUpDolly(-d);
        else if (up_option_index == option->m_option_index)
          viewport->DownUpDolly(d);
        else if (left_option_index == option->m_option_index)
          viewport->LeftRightDolly(-d);
        else if (right_option_index == option->m_option_index)
          viewport->LeftRightDolly(d);
        else if (in_option_index == option->m_option_index)
          viewport->InOutDolly(d);
        else if (out_option_index == option->m_option_index)
          viewport->InOutDolly(-d);
      }

      view->Redraw();
      RhinoApp().SetActiveView(view);
    }
  }  
  
  return CRhinoCommand::success;
}
CRhinoCommand::result CCommandSampleTabbedDockBar::RunCommand( const CRhinoCommandContext& context )
{
  ON_UUID tabId = CSampleTabbedDockBarDialog::ID();

  if( context.IsInteractive() )
  {
    CRhinoTabbedDockBarDialog::OpenDockbarTab( tabId );
  }
  else
  {
    bool bVisible = CRhinoTabbedDockBarDialog::IsTabVisible( tabId );
 
    ON_wString str;
    str.Format( L"%s is %s. New value", LocalCommandName(), bVisible ? L"visible" : L"hidden" );
 
    CRhinoGetOption go;
    go.SetCommandPrompt( str );
    int h_option = go.AddCommandOption( RHCMDOPTNAME(L"Hide") );
    int s_option = go.AddCommandOption( RHCMDOPTNAME(L"Show") );
    int t_option = go.AddCommandOption( RHCMDOPTNAME(L"Toggle") );
    go.GetOption();
    if( go.CommandResult() != CRhinoCommand::success )
      return go.CommandResult();
 
    const CRhinoCommandOption* option = go.Option();
    if( 0 == option )
      return CRhinoCommand::failure;
 
    int option_index = option->m_option_index;

    if( h_option == option_index && bVisible )
      CRhinoTabbedDockBarDialog::ShowDockbarTab( tabId, false );
    else if( s_option == option_index && !bVisible  )
      CRhinoTabbedDockBarDialog::ShowDockbarTab( tabId, true );
    else if( t_option == option_index )
      CRhinoTabbedDockBarDialog::ShowDockbarTab( tabId, !bVisible );
  }

  return CRhinoCommand::success;
}
CRhinoCommand::result CCommandSampleMenuToggle::RunCommand( const CRhinoCommandContext& context )
{
  BOOL bVisible = SampleMenuPlugIn().IsSampleMenuVisible();
 
  ON_wString str;
  str.Format( L"%s is %s. New value", LocalCommandName(), bVisible ? L"visible" : L"hidden" );
 
  CRhinoGetOption go;
  go.SetCommandPrompt( str );
  int h_option = go.AddCommandOption( RHCMDOPTNAME(L"Hide") );
  int s_option = go.AddCommandOption( RHCMDOPTNAME(L"Show") );
  int t_option = go.AddCommandOption( RHCMDOPTNAME(L"Toggle") );
  go.GetOption();
  if( go.CommandResult() != CRhinoCommand::success )
    return go.CommandResult();
 
  const CRhinoCommandOption* option = go.Option();
  if( 0 == option )
    return CRhinoCommand::failure;
 
  int option_index = option->m_option_index;

  if( h_option == option_index && bVisible )
    SampleMenuPlugIn().HideSampleMenu();
  else if( s_option == option_index && !bVisible  )
    SampleMenuPlugIn().ShowSampleMenu();
  else if( t_option == option_index )
  {
    if( bVisible )
      SampleMenuPlugIn().HideSampleMenu();
    else
      SampleMenuPlugIn().ShowSampleMenu();
  }

  return CRhinoCommand::success;
}
CRhinoCommand::result CCommandSampleConvertQuadsToTriangles::RunCommand( const CRhinoCommandContext& context )
{
  CRhinoGetObject go;
  go.SetCommandPrompt( L"Select meshes to convert" );
  go.SetGeometryFilter( CRhinoObject::mesh_object );
  if( go.GetObjects(1,0) != CRhinoGet::object )
    return CRhinoCommand::cancel;
  
  CRhinoGetOption gs;
  gs.AcceptNothing();
  gs.SetCommandPrompt( L"Delete input?" );
  gs.SetDefaultString( (m_delete_input)?L"Yes":L"No" );
  int n_index = gs.AddCommandOption( RHCMDOPTNAME(L"No") );
  int y_index = gs.AddCommandOption( RHCMDOPTNAME(L"Yes") );
  switch( gs.GetOption() )
  {
    case CRhinoGet::option:
      if( gs.Option()->m_option_index == n_index )
        m_delete_input = false;
      else
        m_delete_input = true;
      break;
    
    case CRhinoGet::string:
    case CRhinoGet::nothing:
      break;
    
    default:
      return CRhinoCommand::cancel;
  }
  
  int num_converted = 0;
  for( int i = 0; i < go.ObjectCount(); i++ )
  {
    CRhinoObjRef objref = go.Object(i);
    if( const CRhinoObject* pObject = objref.Object() )
    {
      if( const ON_Mesh* pMesh = ON_Mesh::Cast(pObject->Geometry()) )
      {
        ON_Mesh mesh(*pMesh);
        if( mesh.QuadCount() <= 0 )
          continue;
        
        if( !mesh.ConvertQuadsToTriangles() )
          continue;
        
        const CRhinoMeshObject* mesh_object = NULL;
        if( m_delete_input )
          mesh_object = context.m_doc.ReplaceObject( objref, mesh );
        else
          mesh_object = context.m_doc.AddMeshObject( mesh );
        
        if( mesh_object )
          num_converted++;
      }
    }
  }
  
  if( num_converted > 0 )
    context.m_doc.Redraw();
  
  RhinoApp().Print( L"%d meshes selected, %d meshes converted.\n", go.ObjectCount(), num_converted );
  
  return CRhinoCommand::success;
}