码迷,mamicode.com
首页 > 其他好文 > 详细

ImageViewer

时间:2014-12-18 11:36:26      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:style   blog   ar   io   color   sp   for   on   div   

  1 //ImageViewer.cpp
  2 
  3 /*
  4 Copyright 2000-2004 The VCF Project.
  5 Please see License.txt in the top level directory
  6 where you installed the VCF.
  7 */
  8 
  9 
 10 #include "vcf/ApplicationKit/ApplicationKit.h"
 11 #include "vcf/ApplicationKit/ControlsKit.h"
 12 #include "vcf/ApplicationKit/EtchedBorder.h"
 13 
 14 using namespace VCF;
 15 
 16 
 17 class ImageViewerWindow : public Window {
 18 public:
 19     ImageViewerWindow() : currentImage_(NULL){
 20         setCaption( "ImageViewer" );
 21 
 22         //lets create a menu
 23 
 24         //create the menu bar - this will hold the menu items
 25         MenuBar* menuBar = new MenuBar();
 26 
 27         //set the window‘s menu bar
 28         setMenuBar( menuBar );
 29 
 30         //add the menu to this window‘s components
 31         addComponent( menuBar );
 32 
 33         /**
 34         create menu items, first arguemtn is the menu item name,
 35         then the parent,
 36         then the owning menu bar
 37         */
 38         MenuItem* fileMenu = new DefaultMenuItem( "File", menuBar->getRootMenuItem(), menuBar );
 39         MenuItem* fileOpenImageMenu = new DefaultMenuItem( "Open Image...", fileMenu, menuBar );
 40 
 41         //add our event handler to the menu item
 42         fileOpenImageMenu->MenuItemClicked +=
 43             new ClassProcedure1<MenuItemEvent*,ImageViewerWindow>( this,&ImageViewerWindow::openImage, "ImageViewerWindow::openImage" );
 44 
 45 
 46 
 47         //set the border of the window, this will give us a nice etched border
 48         EtchedBorder* bdr = new EtchedBorder();
 49         bdr->setEdgeStyle( GraphicsContext::etSunken );
 50         setBorder( bdr );
 51 
 52     }
 53 
 54     virtual ~ImageViewerWindow(){
 55         if ( NULL != currentImage_ ) {
 56             delete currentImage_;
 57         }
 58     };
 59 
 60 
 61     virtual void paint( GraphicsContext* ctx ) {
 62         Window::paint(ctx);
 63 
 64         ctx->setClippingRect( &ctx->getViewableBounds() );
 65 
 66         Rect r = getClientBounds();
 67 
 68         if ( NULL != currentImage_ ) {
 69             /**
 70             if we have an image, draw it centered within the available client
 71             rect
 72             */
 73             Rect imageRect(0,0,currentImage_->getWidth(),currentImage_->getHeight());
 74 
 75             imageRect.offset( r.getWidth()/2 - imageRect.getWidth()/2,
 76                                 r.getHeight()/2 - imageRect.getHeight()/2 );
 77 
 78             ctx->drawImageWithinBounds( &imageRect, currentImage_ );
 79         }
 80     }
 81 
 82     void openImage( MenuItemEvent* e ) {
 83         CommonFileOpenDialog dlg( this );
 84 
 85         //get the available image loader extensions
 86         std::vector< std::pair<String,String> > contentTypes;
 87 
 88         /**
 89         this will get a list of all current available types that
 90         can currently be loaded by the VCF. The list is a series
 91         of std::pair objects. the std::pair.first element is a string
 92         that represents the file extension, and the std::pair.second
 93         represents a string that is the mime type for the extension
 94         */
 95         GraphicsToolkit::getAvailableImageTypes( contentTypes );
 96         std::vector< std::pair<String,String> >::iterator it = contentTypes.begin();
 97 
 98         /*
 99         For each type, add a new filter to the dialog
100         */
101         while ( it != contentTypes.end() ) {
102             std::pair<String,String>& type = *it;
103 
104             dlg.addFilter( type.second + " (*." + type.first + " )", "*." + type.first );
105             it ++;
106         }
107 
108 
109         if ( dlg.execute() ) {
110             if ( NULL != currentImage_ ) {
111                 //delete our old image
112                 delete currentImage_;
113             }
114 
115             //create a new image from the file name
116             currentImage_ = GraphicsToolkit::createImage( dlg.getFileName() );
117             repaint(); //repaint ourselves to update the new image
118         }
119     }
120 
121     Image* currentImage_;
122 };
123 
124 
125 
126 
127 class ImageViewerApplication : public Application {
128 public:
129 
130     ImageViewerApplication( int argc, char** argv ) : Application(argc, argv) {
131 
132     }
133 
134     virtual bool initRunningApplication(){
135         bool result = Application::initRunningApplication();
136 
137         Window* mainWindow = new ImageViewerWindow();
138         setMainWindow(mainWindow);
139         mainWindow->setBounds( &Rect( 100.0, 100.0, 500.0, 500.0 ) );
140         mainWindow->show();
141 
142         return result;
143     }
144 
145 
146 
147 };
148 
149 
150 int main(int argc, char *argv[])
151 {
152     Application* app = new ImageViewerApplication( argc, argv );
153 
154     Application::main();
155 
156     return 0;
157 }
158 
159 
160 /**
161 $Id: ImageViewer.cpp 3175 2007-08-16 21:22:56Z ddiego $
162 */

 

ImageViewer

标签:style   blog   ar   io   color   sp   for   on   div   

原文地址:http://www.cnblogs.com/elitiwin/p/4171188.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!