~onli/simdock/master

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
/*  
 *   Copyright 2007 Simone Della Longa <simonedll@yahoo.it>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "main.h"


using namespace std;

enum
{
  ID_Quit = 1,
  ID_About = 2,
  ID_blur_Timer = 3,
  ID_mouse_Timer = 4,
  ID_Settings = 5,
  ID_Add = 6,
  ID_Edit = 7,
  ID_Delete = 8,
  ID_hover_Timer = 9,
  ID_Keep = 10,
};


BEGIN_EVENT_TABLE (MyFrame, wxFrame)
EVT_MIDDLE_DOWN (MyFrame::OnMiddleDown)
EVT_MIDDLE_UP (MyFrame::OnMiddleUp)
EVT_LEFT_UP (MyFrame::OnLeftUp)
EVT_LEFT_DOWN (MyFrame::OnLeftDown)
EVT_RIGHT_DOWN (MyFrame::OnRightClick)
EVT_CONTEXT_MENU (MyFrame::OnContextMenu) 
EVT_MOTION (MyFrame::OnMouseMove)
// EVT_MOVE (MyFrame::OnFrameMove)
EVT_CLOSE (MyFrame::OnClose)
EVT_LEAVE_WINDOW (MyFrame::OnMouseLeave)
EVT_ENTER_WINDOW (MyFrame::OnMouseEnter)
EVT_TIMER (ID_blur_Timer, MyFrame::OnBlurTimerTick)
EVT_TIMER (ID_hover_Timer, MyFrame::OnHoverTimerTick)
/*
* Menu 
*/
EVT_MENU (ID_Quit, MyFrame::OnQuit)
EVT_MENU (ID_About, MyFrame::OnAbout)
EVT_MENU (ID_Settings, MyFrame::OnSettings)
EVT_MENU (ID_Add, MyFrame::OnAdd)
EVT_MENU (ID_Keep, MyFrame::OnKeep)
EVT_MENU (ID_Edit, MyFrame::OnEdit) 
EVT_MENU (ID_Delete, MyFrame::OnDelete)
/*
* Painting 
*/
EVT_PAINT (MyFrame::OnPaint)
EVT_ERASE_BACKGROUND (MyFrame::OnBackground) 
END_EVENT_TABLE ()

wxSize
ImageToShadow (int w, int h, int scaling)
{
  return wxSize (w, h / scaling);
}

double
zoom (int stand_dev, float dist_center, int maximum_size)
{
  return maximum_size * exp (-pow (dist_center, 2) / pow (stand_dev, 2));
}



void
fade (wxImage * img, const int &intensity)
{
  register unsigned int len = img->GetWidth () * img->GetHeight ();
  unsigned char *v = img->GetAlpha ();

  unsigned char *v2 = (unsigned char *) malloc (sizeof (unsigned char) * len);
  register unsigned int i;
  for (i = 0; i < len; i++)
    {
      if ((int) v[i] <= intensity)
	v2[i] = (unsigned char) 0;
      else
	v2[i] = (unsigned char) ((int) v[i] - intensity);
    }
  img->SetAlpha (v2);
}

/* Recursive binary search. Images are sorted by position.
 * given a point and a portion of the image list returns 
 * the closest image to the point.
 */
int
positionToId (const wxPoint & p, ImagesArray * list, int min, int max)
{
  if (min == max)
    {
      return max;
    }

  int mid = (min + max) / 2;

  if (p.x > (*list)[mid]->x)
    {
      if (p.x < (*list)[mid]->x + (*list)[mid]->w)
	return mid;
      else
	return positionToId (p, list, mid + 1, max);
    }

  return positionToId (p, list, min, mid);

}

/*
 * Draws the given bitmap starting from (x,y) stretching it to the given
 * width and height 
 */
void
drawBmp (wxDC * dc, const wxBitmap & bmp, const int &x, const int &y,
	 const int &w, const int &h)
{
  double xFactor = w / (double) bmp.GetWidth ();
  double yFactor = h / (double) bmp.GetHeight ();
  dc->SetUserScale (xFactor, yFactor);
  //the last parameter anables transparency, used when no X-background
  //paixmap found
  dc->DrawBitmap (bmp, wxCoord (x / xFactor), wxCoord (y / yFactor), true);
  dc->SetUserScale (1, 1);
}

void drawTooltip(wxDC * dc, wxString tooltip, simImage* hoveringIcon)
{
    wxPoint iconCenter = hoveringIcon->center();
    int iconWidth = hoveringIcon->img.GetWidth();
    int iconHeight = hoveringIcon->img.GetHeight();
    wxCoord* textWidth = new wxCoord();
    wxCoord* textHeight = new wxCoord();
    dc->GetTextExtent(tooltip, textWidth, textHeight);

    int x = iconCenter.x - ((int)*textWidth /2);
    int y = iconHeight +  5;

    dc->DrawText(tooltip, x , y);
}




MyFrame::MyFrame (wxWindow * parent, simSettings * set, ImagesArray * array,
		  wxWindowID id, const wxString & title, const wxPoint & pos,
		  const wxSize & size, long style):
wxFrame (parent, id, title, pos, size, style)
{
    settings = set;
    ImagesList = array;
    markBitmap = NULL;
    appBackground = NULL;
    src_dc = NULL;
    backImage = NULL;
    /*
   * ------------ Initializing About menu ----------------- 
   */
    info = new wxAboutDialogInfo ();
    info->SetVersion (_T (SIMDOCK_VERSION));
    info->SetCopyright (_T
                  (" (C) 2007 Simone Della Longa <simonedll@yahoo.it>\n"
               "This program is free software; you can redistribute it and/or modify\n"
               "it under the terms of the GNU General Public License as published by\n"
               "the Free Software Foundation; either version 2 of the License, or\n"
               "any later version.\n"));
    info->AddDeveloper (_T ("Author: Simone Della Longa\n"
                          "Contributor: Marco Garzola\n"));

    info->SetName (_T ("SimDock"));
    info->SetWebSite (_T (SIMDOCK_WEBSITE));
    /*
    * ------------ Initializing About menu -----------------
    */

    blurTimer = new wxTimer (this, ID_blur_Timer);
    blurTimer->Start (TIMER_TIMEOUT_BLUR);
    hoverTimer = new wxTimer(this, ID_hover_Timer);
    /*
    * Menu stuff 
    */
    clickedID = -1;
    middleClicked = false;

    /*
    * End menu stuff 
    */
    disposed = false;

    dragging = false;
    draggedID = -1;

    showTooltip = false;
}


void
MyFrame::SetBG (wxImage * newImg)
{
  if (appBackground)
    {
      appBackground->Destroy ();
    }
  appBackground = newImg;

}

wxImage *
MyFrame::GetBG ()
{
    return appBackground;
}

void
MyFrame::SetWallpaper (wxBitmap * newBitmap)
{
    if (backImage)
        delete (backImage);
    if (src_dc)
        delete src_dc;

    backImage = newBitmap;
    if (backImage->GetHeight() > 0) { 
        src_dc = new wxMemoryDC(*backImage);
    }
    Refresh(true);
}

wxBitmap *
MyFrame::GetWallpaper ()
{
  return backImage;
}


  
void MyFrame::SetMarkBitmap (wxBitmap * newBmp)
{
  	if (markBitmap)
  	{
  		delete markBitmap;
  	}
  	markBitmap = newBmp;
  	
  }
  wxBitmap * MyFrame::GetMarkBitmap ()
  {
  	return markBitmap;
}

wxMenu* MyFrame::GetPopMenu()
{
    popMenu = new wxMenu;
    popMenu->Append (ID_Settings, _T ("S&ettings"));
    popMenu->Append (ID_Add, _T ("A&dd Launcher"));
    popMenu->AppendSeparator ();
    if (hoveringIcon != None && hoveringIcon->task) {
        popMenu->Append (ID_Keep, _T ("&Keep as Launcher"));
    }
    EditMenuItem = popMenu->Append (ID_Edit, _T ("Ed&it Launcher"));
    DeleteMenuItem = popMenu->Append (ID_Delete, _T ("Dele&te Launcher"));
    popMenu->AppendSeparator ();
    popMenu->Append (ID_About, _T ("&About..."));
    popMenu->Append (ID_Quit, _T ("E&xit"));
    if (hoveringIcon == None || hoveringIcon->task) {
        DeleteMenuItem->Enable (false);
        EditMenuItem->Enable (false);
    }
    return popMenu;
}

void
MyFrame::updateSize ()
{
  /*
   * This is approximately the maximim width reachable stretching
   * images. It is hard to get a 100% exact maximum width since it
   * depends on the configuration. See "main.h" 
   */
  int width =
    (settings->ICONW + settings->SPACER) * (ImagesList->GetCount () - 1) +
    settings->LEFT_BORDER + settings->RIGHT_BORDER;
  int height = settings->MAXSIZE + settings->BOTTOM_BORDER;

  SetSize (width, height);
  if (settings->AUTO_POSITION)
  {
  	wxSize sz = wxGetDisplaySize();
  	Move(sz.GetWidth() /2 - width /2,GetPosition().y);
  }
  
}

void
MyFrame::OnMouseMove (wxMouseEvent & event)
{
    hoverTimer->Stop();
    showTooltip = false;
    if (middleClicked)
    {
        wxPoint framePos = this->GetScreenPosition ();
        framePos.x += event.m_x - middleClick.x;
        framePos.y += event.m_y - middleClick.y;
        this->Move (framePos);
        moving = true;
        return;
    }
    if (dragging)
    {
        moving = true;
        draggedPos.x = event.m_x;
        draggedPos.y = event.m_y;
        Refresh (false);
        return;
    }

    int position = 0;
    for (unsigned int i = 0; i < ImagesList->GetCount (); i++)
        {
        simImage *img = (*ImagesList)[i];
        if (img->isIn (event.m_x, event.m_y))
        {
            position = i;
            if (hoveringIcon != img) {
                OnMouseLeaveIcon(event);
                OnMouseEnterIcon(event, img);
            }
        }
        int x = event.m_x - (img->x + img->w / 2);
        int y = event.m_y - (img->y + img->h / 2);

        int distance = (int) sqrt (x * x + y * y);
        RefreshSizes (img, distance);
    }
    if (hoveringIcon != None) {
        hoverTimer->Start(3000, wxTIMER_ONE_SHOT);
    }

    appSize = PositionIcons (GetClientSize (), position, settings, ImagesList);

    Refresh (false);
}


void
MyFrame::OnMiddleDown (wxMouseEvent & event) {
    middleClick = wxPoint (event.m_x, event.m_y);
    middleClicked = true;
}

void
MyFrame::OnMiddleUp (wxMouseEvent & event) {
    if (! moving) {
        if (hoveringIcon != None) {
            /* process identifier */
            int pid;		
            pid = fork ();
            if (pid < 0) {
                wxDialog dlg (this, -1, wxT ("Damn, could not fork...."));
                dlg.ShowModal ();
            }

            if (pid == 0) {
                system (wx2std (hoveringIcon->link).c_str ());
                exit (0);
            }
            
            hoveringIcon->status = STATUS_INCREASING;

            if (! blurTimer->IsRunning()) {
                blurTimer->Start (TIMER_TIMEOUT_BLUR);
            }
        } 
           //else "Not hovering an icon";
    } 
    // else "Already moving";
    middleClicked = false;
    moving = false;
}

void
MyFrame::OnRightClick (wxMouseEvent & event)
{
    showTooltip = false;
    wxPoint p = event.GetPosition ();

    clickedID = -1;
    for (unsigned int i = 0; i < ImagesList->GetCount (); i++)
    {
        simImage *img = (*ImagesList)[i];
        if (img->isIn (p.x, p.y))
        {
            clickedID = i;
            break;
        }
    }
    event.Skip ();
    Refresh(false);
}

void
MyFrame::OnContextMenu (wxContextMenuEvent & event)
{
    showTooltip = false;
    hoverTimer->Stop();

    // prevent the menu on being under the mouse-pointer, which lead to
    // activating exit on a single short right-click
    wxPoint relativeCursorPosition = wxPoint();
    relativeCursorPosition.x = wxGetMousePosition().x - this->GetScreenPosition().x;
    relativeCursorPosition.y = wxGetMousePosition().y - this->GetScreenPosition().y;

    relativeCursorPosition.x += 2;
    PopupMenu (MyFrame::GetPopMenu(), relativeCursorPosition);
}

void
MyFrame::OnQuit (wxCommandEvent & WXUNUSED (event))
{
  Close (TRUE);
}

void
MyFrame::OnAbout (wxCommandEvent & WXUNUSED (event))
{
  wxGenericAboutBox (*info);
}

void
MyFrame::OnSettings (wxCommandEvent & WXUNUSED (event))
{
    simSettings initSettings = *settings;

    settingsDialog = new SettingsDialog (this, &initSettings);
    settingsDialog->Show();

    // the setting-window spawns wrongly only on the workspace simdock
    // was started on, so show it now on the current workspace
    GdkWindow * settingsWindow = gtk_widget_get_window(settingsDialog->GetHandle());
    gdk_x11_window_move_to_current_desktop(settingsWindow);
    settingsDialog->ShowModal();
}

void
MyFrame::OnAdd (wxCommandEvent & event)
{
  simImage *sim = new simImage ();
  LauncherDialog *dlg = new LauncherDialog (this, sim);
  if (dlg->ShowModal () == wxID_OK)
    {
      if (dlg->saveChanges ())
	{

	  sim->w = settings->ICONW;
	  sim->h = settings->ICONH;
	  sim->y =
	    (settings->MAXSIZE + settings->BOTTOM_BORDER) - settings->ICONH -
	    settings->BOTTOM_BORDER;
	  ImagesList->Add (sim);
	  appSize = PositionIcons (GetClientSize (), 0, settings, ImagesList);
	  updateSize();

	  // New icon will fade :p
	  // Disabled. Unefficient
	  // timer->Start (timer_timeout);
	  wxGetApp ().launchersModified = true;
	  Refresh (false);

	}
      else
	delete sim;

    }
  else
    delete sim;

  dlg->Destroy ();

}

void MyFrame::OnKeep(wxCommandEvent & event) {
    if (clickedID < 0 || (unsigned int) clickedID > ImagesList->GetCount ())
    {
        cout << "Error! invalid ClickedID value! " << clickedID << endl;
        return;
    }
    simImage* newLauncher = new simImage();
    newLauncher = (*ImagesList)[clickedID];
    
    taskInfo ti;
    //it is quite possible that TaskInfo cant find the data of the window
    //(e.g. xterm)
	if (ti.Init(newLauncher->getWindow())) {
        newLauncher->link = ti.path;
        wxString home = wxGetHomeDir ();
        wxString dirPath = home + _T ("/") + _T (CONF_DIR);
        wxString iconPath = dirPath + _T ("/") + ti.name + _T(".png");
        ti.icon.SaveFile(iconPath, wxBITMAP_TYPE_PNG);
        newLauncher->img_link = iconPath;
        wxGetApp ().launchersModified = true;
        newLauncher->task = false;
    } else {
        cout << "Not enough data to create launcher.";
    }
}

void
MyFrame::OnEdit (wxCommandEvent & event)
{
  if (clickedID < 0 || (unsigned int) clickedID > ImagesList->GetCount ())
    {
      cout << "Error! invalid ClickedID value! " << clickedID << endl;
      return;
    }

  LauncherDialog *dlg = new LauncherDialog (this, (*ImagesList)[clickedID]);
  if (dlg->ShowModal () == wxID_OK)
    {
      dlg->saveChanges ();
      Refresh (false);
      wxGetApp ().launchersModified = true;

    }

  dlg->Destroy ();

}

void
MyFrame::OnDelete (wxCommandEvent & event)
{
    if (clickedID < 0 || (unsigned int) clickedID > ImagesList->GetCount ())
    {
        cout << "Error! invalid ClickedID value! " << clickedID << endl;
        return;
    }
    simImage* oldLauncher = (*ImagesList)[clickedID];
    if (oldLauncher->windowCount() > 0) {
        oldLauncher->task = true;
    } else {
        delete (*ImagesList)[clickedID];
        ImagesList->RemoveAt (clickedID);
    }
    wxGetApp ().launchersModified = true;
    updateSize();
    Refresh (false);
}

void
MyFrame::OnMouseLeave (wxMouseEvent & event)
{
#ifdef SIMDOCK_DEBUG
    cout << "OnMouseLeave" << endl;
#endif
    showTooltip = false;
    OnMouseLeaveIcon(event);
    for (unsigned int i = 0; i < ImagesList->GetCount (); i++)
    {
        simImage *img = (*ImagesList)[i];
        RefreshSizes (img, INT_MAX);
    }

    appSize = PositionIcons (GetClientSize (), 0, settings, ImagesList);
    /*
     * int style = this->GetWindowStyle(); style = style & wxSTAY_ON_TOP;
     * this->SetWindowStyle(style); 
     */
    /*if (!wxGetApp ().onTop)
    {
        SetWindowStyleFlag (frameOptions);
          // Lower ();
    }*/
    Refresh (false);
}

void
MyFrame::OnMouseEnter (wxMouseEvent & event)
{
#ifdef SIMDOCK_DEBUG
  cout << "OnmouseEnter" << endl;
#endif

  // Raise ();
  /*
   * int style = this->GetWindowStyle(); style = style | wxSTAY_ON_TOP;
   * this->SetWindowStyle(style); 
   */
  // MakeModal(true);
  if (!wxGetApp ().onTop)
    {
      // SetWindowStyleFlag(frameOptions |wxSTAY_ON_TOP);
      Refresh (false);
    }
}

//Pseudo-event, called manually
void MyFrame::OnMouseEnterIcon (wxMouseEvent & event, simImage* img)
{
    hoveringIcon = img;
}

//Pseudo-event, called manually
void MyFrame::OnMouseLeaveIcon (wxMouseEvent & event)
{
    hoveringIcon = None;
    
}


void
MyFrame::OnLeftDown (wxMouseEvent & event)
{
    simImage *img = this->getClickedIcon(event);
    if (img != None) {
        dragging = true;
        draggedID = positionToId(wxPoint (event.m_x, event.m_y), ImagesList, 0,
                            ImagesList->GetCount () - 1);
        draggedPos.x = event.m_x;
        draggedPos.y = event.m_y;
        Refresh (false);
        return;
    }

}

simImage*
MyFrame::getClickedIcon(wxMouseEvent & event) {
    for (unsigned int i = 0; i < ImagesList->GetCount (); i++) {
        simImage *img = (*ImagesList)[i];
        if (img->isIn (event.m_x, event.m_y)) {
            return img;
        }
    }
    return None;
}



void
MyFrame::OnLeftUp (wxMouseEvent & event) {
    if (dragging && moving) {
        int id = positionToId (wxPoint (event.m_x, event.m_y), ImagesList, 0,
                                ImagesList->GetCount () - 1);
        simImage *oldImg = (*ImagesList)[draggedID];
        ImagesList->RemoveAt (draggedID);
        ImagesList->Insert (oldImg, id);

        dragging = false;
        moving = false;
        //it's possible the launchers were moved, save new order:
        wxGetApp().launchersModified = true;
        appSize = PositionIcons (GetClientSize (), id, settings, ImagesList);
        Refresh (false);
        return;
    }
    dragging = false;
    for (unsigned int i = 0; i < ImagesList->GetCount (); i++) {
        simImage *img = (*ImagesList)[i];
        if (img->isIn (event.m_x, event.m_y)) {
            if (img->windowCount() > 0) {
                if (settings->ENABLE_MINIMIZE && img->active) {
                    if (img->allNotMinimized()) {
                        img->cycleMinimize = true;
                    }
                    if (img->allMinimized()) {
                        img->cycleMinimize = false;
                    }
                    
                    if (img->cycleMinimize) {
                        tasks_minimize(img->getWindow());
                    } else {
                        tasks_raise(img->getWindow());
                    }
                } else {
                    tasks_raise(img->getWindow());
                }
                return;
            }

            /* process identifier */
            int pid;		

            pid = fork ();
            if (pid < 0) {
                wxDialog dlg (this, -1, wxT ("Damn, could not fork...."));
                dlg.ShowModal ();
            }

            if (pid == 0) {
                system (wx2std (img->link).c_str ());
                exit (0);
            }
            
            img->status = STATUS_INCREASING;

            if (! blurTimer->IsRunning()) {
                blurTimer->Start (TIMER_TIMEOUT_BLUR);
            }
            return;

        }
    }
}

void
MyFrame::OnFrameMove (wxMoveEvent & event)
{
  cout << event.GetPosition ().x << "," << event.GetPosition ().y << endl;
}



#if 0
void
fade (wxImage * img, const int &intensity)
{
  if (intensity == 0)
    return;
  /*
   * Get the full alpha channel and modify it instead of changing every
   * single bit? will it be faster? Look above .. :( 
   */
  unsigned char v;
  for (int i = 0; i < img->GetWidth (); i++)
    {
      for (int j = 0; j < img->GetHeight (); j++)
	{
	  v = img->GetAlpha (i, j);
	  if ((int) v <= intensity)
	    {
	      v = (unsigned char) intensity;
	    }
	  unsigned char v2 = (unsigned char) ((int) v - intensity);
	  img->SetAlpha (i, j, v2);
	}
    }
}
#endif


void
MyFrame::OnBackground (wxEraseEvent & event)
{

}




void
MyFrame::OnClose (wxCloseEvent & event)
{
  lastPosition = GetPosition ();
  disposed = true;
  Destroy ();
}


void
MyFrame::OnBlurTimerTick (wxTimerEvent & event)
{
  bool changed = false;		// Some node is changing status
  for (unsigned int i = 0; i < ImagesList->GetCount (); i++)
    {
      simImage *img = (*ImagesList)[i];

      switch (img->status)
	{
	case STATUS_NONE:
	  break;
	default:
	  img->handleStatus ();
	  changed = true;
	  RefreshRect (wxRect (img->x, img->y, img->w, img->h), false);
	  wxSize shadowSize =
	    ImageToShadow (img->w, img->h, settings->REFLEX_SCALING);
	  RefreshRect (wxRect
		       (img->x, img->y + img->h, shadowSize.GetWidth (),
			shadowSize.GetHeight ()));
	  break;
	}
    }

  if (!changed)
    blurTimer->Stop ();
  
    /*if (changed)
        Refresh(true); */
   

}

void MyFrame::OnHoverTimerTick(wxTimerEvent & event)
{
    showTooltip = true;
    Refresh(true);
}





/*
 * Changes simImage y (Not X) position and Widht, Height according to the
 * mouse distance. Return TRUE if size was changed, false otherwise 
 */
void
MyFrame::RefreshSizes (simImage * img, int distance)
{
  int newW = settings->ICONW;
  int newH = settings->ICONH;

  if (distance == 0)
    {
      newW = settings->MAXSIZE;
      newH = settings->MAXSIZE;
    }
  else
    {
      if (distance < settings->RANGE)
	{

	  /*
	   * int diff = distance * ICONW / RANGE; //(RANGE - MINIMUM);
	   * newW = MAXSIZE - diff; 
	   */
	  newW = (int) zoom (settings->RANGE, distance, settings->MAXSIZE);

	  if (newW < settings->ICONW)
	    newW = settings->ICONW;
	  newH = newW;

	}
    }
  if (img->w != newW)
    {
      img->y = GetClientSize ().GetHeight () - newH - settings->BOTTOM_BORDER;
      img->w = newW;
      img->h = newH;
    }
}

void
MyFrame::OnPaint (wxPaintEvent & event)
{
    wxPaintDC paint_dc (this);
    wxBufferedDC dc (&paint_dc);
    wxPoint framePos = this->GetScreenPosition ();
    wxSize sz = GetClientSize ();

  /*
   * -------------- Version using background bitmap ------------ 
   */
    dc.Blit (0, 0, sz.GetWidth (), sz.GetHeight (), src_dc, framePos.x,
	   framePos.y);

    /*------------ Version using Screen device context BitBlit ---------------- */
  /*
   * wxScreenDC screen; dc.Blit(0, 0, sz.GetWidth(), sz.GetHeight(),
   * &screen, framePos.x, framePos.y); 
   */

  // dc.DrawBitmap(wxBitmap(*appBackground),0,sz.GetHeight () -
  // BG_HEIGHT);

    drawBmp (&dc, wxBitmap (*appBackground), 0,
	   sz.GetHeight () - settings->BG_HEIGHT, appSize.GetWidth (),
	   settings->BG_HEIGHT);


  /*
   * wxScreenDC scrDC; //
   * scrDC.StartDrawingOnTop(&wxRect(framePos.x-10,framePos.y-10,ICONW,ICONH));
   * scrDC.StartDrawingOnTop();
   * 
   * // drawBmp (&scrDC, wxBitmap (ImagesList[0]->img), framePos.x-10,
   * framePos.y-10, ICONW,ICONH);
   * 
   * scrDC.DrawBitmap (wxBitmap (ImagesList[0]->img), framePos.x-10,
   * framePos.y-10 );
   * 
   * scrDC.EndDrawingOnTop();
   * 
   */


    for (unsigned int i = 0; i < ImagesList->GetCount (); i++)
    {
        simImage *img = (*ImagesList)[i];

        if (img->status != STATUS_NONE)
        {

            wxImage wxImage2 (img->img);
            fade (&wxImage2, img->blur * 20);

            wxBitmap bmp (wxImage2);
            drawBmp (&dc, bmp, img->x, img->y, img->w, img->h);
        }
        else
        {
            drawBmp (&dc, wxBitmap (img->img), img->x, img->y, img->w, img->h);
        }
        wxSize shadowSize;
        if (settings->SHOW_REFLEXES)
        {
            wxImage wxImage3 (img->reflex);
            fade (&wxImage3, settings->REFLEX_ALPHA + img->blur * 10);
            shadowSize =
                ImageToShadow (img->w, img->h, settings->REFLEX_SCALING);
            drawBmp (&dc, wxBitmap (wxImage3), img->x, img->y + img->h,
                shadowSize.GetWidth (), shadowSize.GetHeight ());
        }
		if (img->windowCount() > 0)
        {
            drawBmp(&dc,*markBitmap,img->x+img->w/2 -markBitmap->GetWidth()/2,img->y+img->h+shadowSize.GetHeight (), markBitmap->GetWidth(),5);
				
        }
    }


    if (dragging && moving)
    {
      simImage *img = (*ImagesList)[draggedID];
      drawBmp (&dc, wxBitmap (img->img), draggedPos.x,
	       draggedPos.y, settings->ICONW, settings->ICONH);
    }

    if (showTooltip && hoveringIcon != None) {
        wxString tooltip = hoveringIcon->name;
        drawTooltip(&dc, tooltip, hoveringIcon);
    }

}