]> git.saurik.com Git - wxWidgets.git/blob - src/html/mod_image.cpp
more reasonable default size
[wxWidgets.git] / src / html / mod_image.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: mod_image.cpp
3 // Purpose: wxHtml module for displaying images
4 // Author: Vaclav Slavik
5 // Copyright: (c) 1999 Vaclav Slavik
6 // Licence: wxWindows Licence
7 /////////////////////////////////////////////////////////////////////////////
8
9 #ifdef __GNUG__
10 #pragma implementation
11 #endif
12
13 #include <wx/wxprec.h>
14
15 #include "wx/defs.h"
16 #if wxUSE_HTML
17
18 #ifdef __BORDLANDC__
19 #pragma hdrstop
20 #endif
21
22 #ifndef WXPRECOMP
23 #include <wx/wx.h>
24 #endif
25
26 #include <wx/html/forcelink.h>
27 #include <wx/html/mod_templ.h>
28
29 #include <wx/wxhtml.h>
30 #include <wx/image.h>
31
32 FORCE_LINK_ME(mod_image)
33
34
35 //--------------------------------------------------------------------------------
36 // wxHtmlImageCell
37 // Image/bitmap
38 //--------------------------------------------------------------------------------
39
40 class wxHtmlImageCell : public wxHtmlCell
41 {
42 public:
43 wxBitmap *m_Image;
44
45 wxHtmlImageCell(wxFSFile *input, int w = -1, int h = -1, int align = HTML_ALIGN_BOTTOM);
46 ~wxHtmlImageCell() {if (m_Image) delete m_Image;}
47 void Draw(wxDC& dc, int x, int y, int view_y1, int view_y2);
48 };
49
50
51
52 //--------------------------------------------------------------------------------
53 // wxHtmlImageCell
54 //--------------------------------------------------------------------------------
55
56 wxHtmlImageCell::wxHtmlImageCell(wxFSFile *input, int w, int h, int align) : wxHtmlCell()
57 {
58 wxImage *img;
59 int ww, hh;
60 wxString m = input -> GetMimeType();
61 wxInputStream *s = input -> GetStream();
62
63 #if wxVERSION_NUMBER < 2100
64 /* NOTE : use this *old* code only if you have old 2.0.1 wxWindows distribution
65 and don't want to upgrade it with stuffs from add-on/wxwin201 */
66 if (wxMimeTypesManager::IsOfType(m, "image/png")) img = new wxImage(*s, wxBITMAP_TYPE_PNG);
67 else if (wxMimeTypesManager::IsOfType(m, "image/jpeg")) img = new wxImage(*s, wxBITMAP_TYPE_JPEG);
68 else if (wxMimeTypesManager::IsOfType(m, "image/bmp")) img = new wxImage(*s, wxBITMAP_TYPE_BMP);
69 else if (wxMimeTypesManager::IsOfType(m, "image/gif")) img = new wxImage(*s, wxBITMAP_TYPE_GIF);
70 else if (wxMimeTypesManager::IsOfType(m, "image/tiff")) img = new wxImage(*s, wxBITMAP_TYPE_TIF);
71 else if (wxMimeTypesManager::IsOfType(m, "image/xpm")) img = new wxImage(*s, wxBITMAP_TYPE_XPM);
72 else if (wxMimeTypesManager::IsOfType(m, "image/xbm")) img = new wxImage(*s, wxBITMAP_TYPE_XBM);
73 else img = NULL;
74 #else
75 img = new wxImage(*s, m);
76 #endif
77
78 m_Image = NULL;
79 if (img && (img -> Ok())) {
80 ww = img -> GetWidth();
81 hh = img -> GetHeight();
82 if (w != -1) m_Width = w; else m_Width = ww;
83 if (h != -1) m_Height = h; else m_Height = hh;
84 if ((m_Width != ww) || (m_Height != hh)) {
85 wxImage img2 = img -> Scale(m_Width, m_Height);
86 m_Image = new wxBitmap(img2.ConvertToBitmap());
87 }
88 else
89 m_Image = new wxBitmap(img -> ConvertToBitmap());
90 delete img;
91 }
92 switch (align) {
93 case HTML_ALIGN_TOP :
94 m_Descent = m_Height; break;
95 case HTML_ALIGN_CENTER :
96 m_Descent = m_Height / 2; break;
97 case HTML_ALIGN_BOTTOM : default :
98 m_Descent = 0; break;
99 }
100 }
101
102
103
104 void wxHtmlImageCell::Draw(wxDC& dc, int x, int y, int view_y1, int view_y2)
105 {
106 if (m_Image)
107 dc.DrawBitmap(*m_Image, x + m_PosX, y + m_PosY, TRUE);
108 wxHtmlCell::Draw(dc, x, y, view_y1, view_y2);
109 }
110
111
112
113
114
115 //--------------------------------------------------------------------------------
116 // tag handler
117 //--------------------------------------------------------------------------------
118
119 TAG_HANDLER_BEGIN(IMG, "IMG")
120
121 TAG_HANDLER_PROC(tag)
122 {
123 if (tag.HasParam("SRC")) {
124 int w = -1, h = -1;
125 int al;
126 wxFSFile *str;
127 wxString tmp = tag.GetParam("SRC");
128
129 str = m_WParser -> GetFS() -> OpenFile(tmp);
130 if (tag.HasParam("WIDTH")) tag.ScanParam("WIDTH", "%i", &w);
131 if (tag.HasParam("HEIGHT")) tag.ScanParam("HEIGHT", "%i", &h);
132 al = HTML_ALIGN_BOTTOM;
133 if (tag.HasParam("ALIGN")) {
134 wxString alstr = tag.GetParam("ALIGN");
135 alstr.MakeUpper(); // for the case alignment was in ".."
136 if (alstr == "TEXTTOP") al = HTML_ALIGN_TOP;
137 else if ((alstr == "CENTER") || (alstr == "ABSCENTER")) al = HTML_ALIGN_CENTER;
138 }
139 if (str) {
140 wxHtmlCell *cel = new wxHtmlImageCell(str, w, h, al);
141 cel -> SetLink(m_WParser -> GetLink());
142 m_WParser -> GetContainer() -> InsertCell(cel);
143 delete str;
144 }
145 }
146
147 return FALSE;
148 }
149
150 TAG_HANDLER_END(IMAGE)
151
152
153
154 TAGS_MODULE_BEGIN(Image)
155
156 TAGS_MODULE_ADD(IMG)
157
158 TAGS_MODULE_END(Image)
159
160
161 #endif