1 
2 #include <wx/wx.h>
3 //#include "wx/wxprec.h"
4 #include <wx/dialog.h>
5 #include <wx/utils.h>
6 #include <wx/string.h>
7 #include <wx/bitmap.h>
8 #include <wx/timer.h>
9 #include <wx/dcclient.h>
10 #include <wx/region.h>
11 #include <wx/dcbuffer.h>
12 #include "ToasterBoxWindow.h"
13 #include "../gui/wxbackgroundimage.h"
14 #include "../images/notif_bg.png.h"
15 #include "../uiutils.h"
16 
17 #ifndef __WXMSW__
18 	typedef wxClientDC DCType;
19 #else
20 	typedef wxBufferedPaintDC DCType;
21 #endif
22 
23 long ToasterBoxWindow::count = 0;
24 
25 //BEGIN_EVENT_TABLE (ToasterBoxWindow, wxFrame)
26 //   EVT_KILL_FOCUS (ToasterBoxWindow::RejectFocus)
27 //   EVT_SET_FOCUS (ToasterBoxWindow::RejectFocus)
28 //END_EVENT_TABLE ()
29 
ToasterBoxWindow(wxWindow * parent,wxTimer * _parent2)30 ToasterBoxWindow::ToasterBoxWindow(wxWindow* parent, wxTimer *_parent2):
31 	ToasterBase(parent,  wxNO_BORDER|wxSTAY_ON_TOP|wxFRAME_NO_TASKBAR),
32 	startTime( wxGetLocalTime() ),
33 	parent2( _parent2 ),
34 	sleepTime( 10 ),
35 	pauseTime( 1700 ),
36 	textColor( *wxWHITE ),
37 	popupText( _T("Change Me!") ),
38 	m_background_bitmap( charArr2wxBitmap( notif_bg_png, sizeof(notif_bg_png) ) ),
39 	shrink(false)
40 {
41 	SetBackgroundStyle(wxBG_STYLE_CUSTOM);
42 	count++;
43 	//the size we want the dialog to be
44 	wxSize dialogSize(150, 170);
45 	bottomRight = wxPoint(wxGetDisplaySize().GetWidth(),wxGetDisplaySize().GetHeight());
46 	SetSize(bottomRight.x, bottomRight.y, dialogSize.GetWidth(), dialogSize.GetHeight());
47 
48 	ToasterBase::Connect( wxEVT_ERASE_BACKGROUND, (wxObjectEventFunction)& ToasterBoxWindow::OnEraseBackground);
49 	ToasterBase::Connect( wxEVT_PAINT, (wxObjectEventFunction)& ToasterBoxWindow::OnPaint);
50 #ifndef __WXMSW__
51 	PushEventHandler( new wxBackgroundBitmap( m_background_bitmap ) );
52 #endif
53 }
54 
SetPopupBitmap(wxBitmap & bitmap)55 void ToasterBoxWindow::SetPopupBitmap(wxBitmap& bitmap)
56 {
57     sbm.Create(this, 0, bitmap, wxPoint(0,0));
58 }
59 
SetPopupBitmap(wxString _bitmapFile)60 void ToasterBoxWindow::SetPopupBitmap(wxString _bitmapFile)
61 {
62   if(!_bitmapFile.IsEmpty())
63   {
64     wxBitmap bm = wxBitmap();
65     bm.LoadFile(_bitmapFile, wxBITMAP_TYPE_BMP);
66     if(bm.Ok())
67       sbm.Create(this, 0, bm, wxPoint(0,0));
68   }
69   //wxBitmap bm2 = wxBitmap();
70   //bm2.LoadFile("back2.bmp", wxBITMAP_TYPE_BMP);
71   //wxRegion reg(bm2);
72   //wxLogDebug("%i", SetShape(*reg));
73 }
74 
75 
SetPopupSize(int x,int y)76 void ToasterBoxWindow::SetPopupSize(int x, int y)
77 {
78   SetSize(bottomRight.x, bottomRight.y,
79     x, y);
80 }
81 
SetPopupPosition(int x,int y)82 void ToasterBoxWindow::SetPopupPosition(int x, int y)
83 {
84   dialogTop = wxPoint(x,y);
85   bottomRight = wxPoint(dialogTop.x + GetSize().GetWidth(),
86     dialogTop.y + GetSize().GetHeight());
87 }
88 
89 
90 //_shrink indicates if we should to transformations on
91 //the text to make it "pretty"
SetPopupText(wxString _text,bool _shrink)92 void ToasterBoxWindow::SetPopupText(wxString _text, bool _shrink)
93 {
94   popupText = _text;
95   shrink = _shrink;
96 }
97 
98 //activate the frame, raise it and set the timer
Play()99 bool ToasterBoxWindow::Play()
100 {
101   //do some checks to make sure this window is valid
102   if(bottomRight.x < 1 || bottomRight.y < 1)
103     return false;
104   if(GetSize().GetWidth() < 50 || GetSize().GetWidth() < 50)
105   {
106     //toasterbox launches into a endless loop for some reason
107     //when you try to make the window too small.
108     return false;
109   }
110   ScrollUp();
111   Start(pauseTime, true);
112   return true;
113 }
114 
115 //return to the user if their text all fit or not
DoesTextFit()116 bool ToasterBoxWindow::DoesTextFit()
117 {
118   wxCoord w, h;
119   wxClientDC dc(this);
120   dc.GetTextExtent(popupText, &w, &h);
121   if(w > GetSize().GetWidth() || h > GetSize().GetHeight())
122     return false;
123   return true;
124 }
125 
SetPopupBackgroundColor(int r,int g,int b)126 void ToasterBoxWindow::SetPopupBackgroundColor(int r, int g, int b)
127 {
128   if(wxColor(r,g,b).Ok())
129     SetBackgroundColour(wxColor(r,g,b));
130   else
131 	wxLogDebug(_T("%s: %d,%d,%d"), _T("invalid color"), r, g, b);
132 }
133 
SetPopupTextColor(int r,int g,int b)134 void ToasterBoxWindow::SetPopupTextColor(int r, int g, int b)
135 {
136   if(wxColor(r,g,b).Ok())
137     textColor = wxColor(r,g,b);
138 }
139 
140 //private functions
ScrollUp()141 void ToasterBoxWindow::ScrollUp()
142 {
143   wxLogDebug(_T("%s"), _T("Raising"));
144   SetSize(dialogTop.x, dialogTop.y, GetSize().GetWidth(),GetSize().GetHeight()	);
145   Show(true);
146 //
147 //
148 //  //walk the Y value up in a raise motion
149 //  int windowSize = 0;
150 //  for(int i = bottomRight.y; i > dialogTop.y; i -= step)
151 //  {
152 //    if(i < dialogTop.y)
153 //      i = dialogTop.y;
154 //    windowSize += step;
155 //    SetSize(dialogTop.x, i, GetSize().GetWidth(),
156 //      windowSize);
157 //	//Update();
158 //	  wxMilliSleep(sleepTime);
159 //  }
160 
161 //
162   Update();
163   DrawText();
164   Update();
165 }
166 
ScrollDown()167 void ToasterBoxWindow::ScrollDown( )
168 {
169   wxLogDebug(_T("%s"), _T("Lowering"));
170 //
171 //  //walk down the Y value
172 //  int windowSize = GetSize().GetHeight();
173 //  for(int i = dialogTop.y; i < bottomRight.y; i += step)
174 //  {
175 //    if(i > bottomRight.y)
176 //      i = bottomRight.y;
177 //    windowSize -= step;
178 //    SetSize(dialogTop.x, i, GetSize().GetWidth(),
179 //      windowSize);
180 //    //Update();
181 //	  wxMilliSleep(sleepTime);
182 //
183 //  }
184   Hide();
185   if(parent2)
186     parent2->Notify();
187   else
188 	wxLogDebug(_T("%s"), _T("not valid parent"));
189 }
190 #include "../settings.h"
DrawText()191 void ToasterBoxWindow::DrawText()
192 {
193 	DCType dc( this );
194 #ifdef __WXMSW__
195 //	dc.SetBackground( *wxBLACK_BRUSH );
196 //	dc.Clear();
197 	dc.DrawBitmap(m_background_bitmap, 0, 0, false);
198 #endif
199   //width and height of text
200   wxCoord w = 0, h = 0;
201   //where we will set the text
202   wxCoord x = 0, y = 0;
203   //border from sides and top to text (in pixels)
204   int border_right = 7;
205   int border_left = 102;
206 	if (sbm.GetBitmap().IsOk()) {
207 		border_left += sbm.GetBitmap().GetWidth();
208 	}
209   //how much space between text lines
210   int textPadding = 4;
211   //how much can we have on a line?
212   int textColumns = 0;
213   //how many lines can we fit vertically
214   float textLines = 1;
215   //the popupText after our transformations (if we do any)
216   wxString pText = GetPopupText();
217 	wxFont outFont( 9, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_BOLD );
218 	dc.SetFont( outFont );
219 
220   dc.GetTextExtent(pText, &w, &h);
221 
222   //shrink the text to fit in the popup box
223   if(shrink)
224   {
225 	while((w +(border_right + border_left))> GetSize().GetWidth())
226     {
227       pText = pText.Left(pText.Length()-1);
228       dc.GetTextExtent(pText, &w, &h);
229     }
230     textColumns = pText.Length();
231     //figure out how many lines of text we have
232     textLines = (GetPopupText().Length() / textColumns) +.9;
233   }
234 
235   //center the text
236 //  if(w < GetSize().GetWidth())
237 //    x = (GetSize().GetWidth() - w) / 2;
238 //  if(h < GetSize().GetHeight())
239 //    y = (GetSize().GetHeight() - h) / 2;
240 
241   dc.SetTextForeground(textColor);
242 
243 //  //if we have multiple lines
244   if(textLines > 1)
245   {
246     //how many lines we can fit in the height available
247     float howManyLinesFit = GetSize().GetHeight() / (h+textPadding);
248     if(textLines < howManyLinesFit)
249       howManyLinesFit = textLines;
250     y = (GetSize().GetHeight() - ((h+textPadding) * howManyLinesFit)) / 2;
251     for(int i = 0; i < howManyLinesFit; i++)
252     {
253 	  dc.DrawText(GetPopupText().Mid((i*textColumns), textColumns), x + border_left,y);
254       //move the text down a line
255       y += h+textPadding;
256     }
257   }
258   else
259     dc.DrawText(pText, x,y);
260 
261 }
262 
Notify()263 void ToasterBoxWindow::Notify()
264 {
265   wxLogDebug(_T("Been up for: %i"), wxGetLocalTime() - startTime);
266   ScrollDown( );
267 }
268 
PrintInfo()269 void ToasterBoxWindow::PrintInfo()
270 {
271   wxLogDebug(_T("brX:%i brY:%i szW:%i szH:%i"),
272     bottomRight.x, bottomRight.y, GetSize().GetWidth(),
273     GetSize().GetHeight());
274 }
275 
OnPaint(wxPaintEvent &)276 void ToasterBoxWindow::OnPaint( wxPaintEvent& /*event*/ )
277 {
278 	DrawText();
279 }
280 
281 /// wxEVT_ERASE_BACKGROUND event handler for ID_WXGRADIENTBUTTON
OnEraseBackground(wxEraseEvent &)282 void ToasterBoxWindow::OnEraseBackground( wxEraseEvent& /*event*/ )
283 {
284 	DrawText();
285 }
286