~ubuntu-branches/ubuntu/raring/codeblocks/raring-proposed

« back to all changes in this revision

Viewing changes to src/plugins/contrib/wxSmithContribItems/wxthings/wxthings/src/dropdown.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Cosme Domínguez Díaz
  • Date: 2010-08-09 04:38:38 UTC
  • mfrom: (1.1.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 4.
  • Revision ID: james.westby@ubuntu.com-20100809043838-a59ygguym4eg0jgw
Tags: 10.05-0ubuntu1
* New upstream release. Closes (LP: #322350)
 - Switch to dpkg-source 3.0 (quilt) format
 - Remove unneeded README.source
 - Add debian/get-source-orig script that removes all
   Windows prebuilt binaries
* Bump Standards-Version to 3.9.1
 - Stop shipping *.la files
* debian/control
 - Add cdbs package as Build-Depend
 - Add libbz2-dev and zlib1g-dev packages as
   Build-Depends (needed by libhelp_plugin.so)
 - Remove dpatch package of Build-Depends
 - Add codeblocks-contrib-debug package
 - Split architecture-independent files of codeblocks
   package in codeblocks-common package
* debian/rules
 - Switch to CDBS rules system
 - Add parallel build support
 - Add a call to debian/get-source-orig script
 - Use lzma compression (saves 23,5 MB of free space)
* debian/patches
 - Refresh 01_codeblocks_plugin_path
 - Add 02_no_Makefiles_in_debian_dir to remove any link
   in codeblocks build system to deleted Makefiles of debian directory
 - Drop 02_ftbfs_gcc44 and 03_ftbfs_glib221 (merged in upstream)
* debian/watch
 - Update to use the new host (berlios.de)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/////////////////////////////////////////////////////////////////////////////
2
 
// Name:        DropDownBase
3
 
// Purpose:     base class for a control like a combobox
4
 
// Author:      John Labenski
5
 
// Modified by:
6
 
// Created:     11/05/2002
7
 
// RCS-ID:
8
 
// Copyright:   (c) John Labenki
9
 
// Licence:     wxWidgets licence
10
 
/////////////////////////////////////////////////////////////////////////////
11
 
 
12
 
#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13
 
    #pragma implementation "dropdown.h"
14
 
#endif
15
 
 
16
 
// For compilers that support precompilation, includes "wx/wx.h".
17
 
#include "wx/wxprec.h"
18
 
 
19
 
#ifdef __BORLANDC__
20
 
    #pragma hdrstop
21
 
#endif
22
 
 
23
 
#ifndef WX_PRECOMP
24
 
    #include "wx/control.h"
25
 
    #include "wx/bitmap.h"
26
 
#endif // WX_PRECOMP
27
 
 
28
 
#include "wx/timer.h"
29
 
#include "wx/things/dropdown.h"
30
 
#include "wx/things/toggle.h"
31
 
#include "wx/renderer.h"
32
 
 
33
 
/* XPM */
34
 
static const char *down_arrow_xpm_data[] = {
35
 
/* columns rows colors chars-per-pixel */
36
 
"7 4 2 1",
37
 
"  c None",
38
 
"a c Black",
39
 
/* pixels */
40
 
"aaaaaaa",
41
 
" aaaaa ",
42
 
"  aaa  ",
43
 
"   a   "};
44
 
 
45
 
#define IDD_DROPDOWN_BUTTON 100
46
 
 
47
 
static wxBitmap s_dropdownBitmap; // all buttons share the same bitmap
48
 
 
49
 
// ==========================================================================
50
 
// DropDownPopup
51
 
// ==========================================================================
52
 
#define USE_POPUP_TIMER 0 // FIXME after 2.5.4 we don't need either
53
 
#define USE_POPUP_IDLE  0
54
 
 
55
 
IMPLEMENT_DYNAMIC_CLASS( DropDownPopup, wxPopupTransientWindow )
56
 
 
57
 
BEGIN_EVENT_TABLE(DropDownPopup, wxPopupTransientWindow)
58
 
    EVT_KEY_DOWN(DropDownPopup::OnKeyDown)
59
 
    EVT_MOUSE_EVENTS(DropDownPopup::OnMouse)
60
 
#if USE_POPUP_TIMER
61
 
    EVT_TIMER( wxID_ANY, DropDownPopup::OnTimer )
62
 
#endif // USE_POPUP_TIMER
63
 
#if USE_POPUP_IDLE
64
 
    EVT_IDLE( DropDownPopup::OnIdle ) // use Connect/Disconnect instead
65
 
#endif // USE_POPUP_IDLE
66
 
END_EVENT_TABLE()
67
 
 
68
 
bool DropDownPopup::Create(DropDownBase *parent, int style)
69
 
{
70
 
    m_owner = parent;
71
 
    return wxPopupTransientWindow::Create(parent, style);
72
 
}
73
 
 
74
 
void DropDownPopup::Init()
75
 
{
76
 
    m_owner          = NULL;
77
 
    m_childWin       = NULL;
78
 
    m_timer          = NULL;
79
 
    m_popped_handler = false;
80
 
}
81
 
 
82
 
DropDownPopup::~DropDownPopup()
83
 
{
84
 
    StopTimer();
85
 
}
86
 
 
87
 
void DropDownPopup::StartTimer()
88
 
{
89
 
#if USE_POPUP_TIMER
90
 
    if (!m_timer)
91
 
        m_timer = new wxTimer(this, wxID_ANY);
92
 
 
93
 
    m_timer->Start(200, false);
94
 
#endif // USE_POPUP_TIMER
95
 
}
96
 
 
97
 
void DropDownPopup::StopTimer()
98
 
{
99
 
    if (m_timer)
100
 
    {
101
 
        if (m_timer->IsRunning())
102
 
            m_timer->Stop();
103
 
        delete m_timer;
104
 
        m_timer = NULL;
105
 
    }
106
 
}
107
 
 
108
 
void DropDownPopup::PushPopupHandler(wxWindow* child)
109
 
{
110
 
    if (child && m_handlerPopup && m_popped_handler)
111
 
    {
112
 
        m_popped_handler = false;
113
 
 
114
 
        if (child->GetEventHandler() != (wxEvtHandler*)m_handlerPopup)
115
 
            child->PushEventHandler((wxEvtHandler*)m_handlerPopup);
116
 
        if (!child->HasCapture())
117
 
            child->CaptureMouse();
118
 
 
119
 
        child->SetFocus();
120
 
    }
121
 
}
122
 
void DropDownPopup::PopPopupHandler(wxWindow* child)
123
 
{
124
 
    if (child && m_handlerPopup && !m_popped_handler)
125
 
    {
126
 
        m_popped_handler = true;
127
 
 
128
 
        if (child->GetEventHandler() == (wxEvtHandler*)m_handlerPopup)
129
 
            child->PopEventHandler(false);
130
 
        if (child->HasCapture())
131
 
            child->ReleaseMouse();
132
 
 
133
 
        child->SetFocus();
134
 
    }
135
 
}
136
 
 
137
 
void DropDownPopup::OnTimer( wxTimerEvent &WXUNUSED(event) )
138
 
{
139
 
    if (!IsShown()) return;
140
 
 
141
 
    m_mouse = ScreenToClient(wxGetMousePosition());
142
 
 
143
 
    wxWindow *child = GetChild();
144
 
    if (!child) return; // nothing to do
145
 
 
146
 
    wxRect clientRect(GetClientRect());
147
 
    //wxPrintf(wxT("**DropDownPopup::OnTimer mouse %d %d -- %d %d %d\n"), m_mouse.x, m_mouse.y, m_popped_handler, m_child, m_handlerPopup); fflush(stdout);
148
 
    // pop the event handler if inside the child window or
149
 
    // restore the event handler if not in the child window
150
 
    if (clientRect.Inside(m_mouse))
151
 
        PopPopupHandler(child);
152
 
    else
153
 
        PushPopupHandler(child);
154
 
}
155
 
 
156
 
void DropDownPopup::OnIdle( wxIdleEvent& event )
157
 
{
158
 
    if (IsShown())
159
 
    {
160
 
        m_mouse = ScreenToClient(wxGetMousePosition());
161
 
        wxPrintf(wxT("OnIdle mouse %d %d\n"), m_mouse.x, m_mouse.y);
162
 
 
163
 
        wxWindow *child = GetChild();
164
 
        if (!child) return; // nothing to do
165
 
 
166
 
        wxRect clientRect(GetClientRect());
167
 
        //wxPrintf(wxT("**DropDownPopup::OnIdle mouse %d %d -- %d %d %d\n"), m_mouse.x, m_mouse.y, m_popped_handler, m_child, m_handlerPopup); fflush(stdout);
168
 
        // pop the event handler if inside the child window or
169
 
        // restore the event handler if not in the child window
170
 
        if (clientRect.Inside(m_mouse))
171
 
            PopPopupHandler(child);
172
 
        else
173
 
            PushPopupHandler(child);
174
 
    }
175
 
    event.Skip();
176
 
}
177
 
 
178
 
void DropDownPopup::OnMouse( wxMouseEvent& event )
179
 
{
180
 
    m_mouse = event.GetPosition();
181
 
    event.Skip();
182
 
}
183
 
 
184
 
void DropDownPopup::OnKeyDown( wxKeyEvent &event )
185
 
{
186
 
    if (GetChild() && GetChild()->ProcessEvent(event))
187
 
        event.Skip(false);
188
 
    else
189
 
        event.Skip(true);
190
 
}
191
 
 
192
 
void DropDownPopup::SetChild(wxWindow *win)
193
 
{
194
 
    m_childWin = win;
195
 
}
196
 
 
197
 
void DropDownPopup::Popup(wxWindow *focus)
198
 
{
199
 
    wxPopupTransientWindow::Popup(focus);
200
 
 
201
 
#if USE_POPUP_IDLE
202
 
    Connect(wxID_ANY, wxEVT_IDLE,
203
 
           (wxObjectEventFunction)(wxEventFunction)(wxIdleEventFunction)
204
 
            &DropDownPopup::OnIdle, 0, this);
205
 
#endif //USE_POPUP_IDLE
206
 
 
207
 
#if USE_POPUP_TIMER
208
 
    // start the timer to track the mouse position
209
 
    StartTimer();
210
 
#endif //USE_POPUP_TIMER
211
 
}
212
 
 
213
 
void DropDownPopup::Dismiss()
214
 
{
215
 
#if USE_POPUP_IDLE
216
 
    Disconnect(wxID_ANY, wxEVT_IDLE,
217
 
               (wxObjectEventFunction)(wxEventFunction)(wxIdleEventFunction)
218
 
               &DropDownPopup::OnIdle, 0, this);
219
 
#endif //USE_POPUP_IDLE
220
 
 
221
 
#if USE_POPUP_TIMER
222
 
    StopTimer();
223
 
#endif //USE_POPUP_TIMER
224
 
 
225
 
    // restore the event handler if necessary for the base class Dismiss
226
 
    wxWindow *child = GetChild();
227
 
    if (child) PushPopupHandler(child);
228
 
 
229
 
    m_popped_handler = false;
230
 
 
231
 
    wxPopupTransientWindow::Dismiss();
232
 
}
233
 
 
234
 
bool DropDownPopup::ProcessLeftDown( wxMouseEvent &event )
235
 
{
236
 
    m_mouse = event.GetPosition();
237
 
    //wxPrintf(wxT("DropDownPopup::ProcessLeftDown %d %d\n"), m_mouse.x, m_mouse.y); fflush(stdout);
238
 
 
239
 
    if (m_popped_handler) return true; // shouldn't ever get here, but just in case
240
 
 
241
 
    StopTimer();
242
 
 
243
 
    // don't let the click on the dropdown button actually press it
244
 
    wxCustomButton *dropBut = m_owner->GetDropDownButton();
245
 
    if (dropBut)
246
 
    {
247
 
        wxPoint dropMousePt = dropBut->ScreenToClient(ClientToScreen(m_mouse));
248
 
        if (dropBut->HitTest(dropMousePt) == wxHT_WINDOW_INSIDE)
249
 
        {
250
 
            m_ignore_popup = true;
251
 
            Dismiss();
252
 
            return true;
253
 
        }
254
 
    }
255
 
 
256
 
    if (GetClientRect().Inside(m_mouse))
257
 
        return false;
258
 
 
259
 
    Dismiss();
260
 
    return true;
261
 
}
262
 
 
263
 
// ============================================================================
264
 
// DropDownBase
265
 
// ============================================================================
266
 
 
267
 
IMPLEMENT_DYNAMIC_CLASS( DropDownBase, wxControl )
268
 
 
269
 
BEGIN_EVENT_TABLE(DropDownBase, wxControl)
270
 
    EVT_BUTTON(IDD_DROPDOWN_BUTTON, DropDownBase::OnDropButton)
271
 
    EVT_SIZE( DropDownBase::OnSize )
272
 
END_EVENT_TABLE()
273
 
 
274
 
DropDownBase::~DropDownBase()
275
 
{
276
 
}
277
 
 
278
 
void DropDownBase::Init()
279
 
{
280
 
    m_popupWin = NULL;
281
 
    m_dropdownButton = NULL;
282
 
}
283
 
 
284
 
bool DropDownBase::Create( wxWindow* parent, wxWindowID id,
285
 
                           const wxPoint& pos, const wxSize& size,
286
 
                           long style, const wxValidator& val,
287
 
                           const wxString& name)
288
 
{
289
 
    if (!wxControl::Create(parent,id,pos,size,wxNO_BORDER|wxCLIP_CHILDREN|style,val,name))
290
 
        return false;
291
 
 
292
 
    if (!s_dropdownBitmap.Ok())
293
 
        s_dropdownBitmap = wxBitmap(down_arrow_xpm_data);
294
 
 
295
 
    m_dropdownButton = new wxCustomButton(this, IDD_DROPDOWN_BUTTON,
296
 
                                                  s_dropdownBitmap,
297
 
                                                                                  wxDefaultPosition,
298
 
                                                                                  wxSize(DROPDOWN_DROP_WIDTH, wxDefaultCoord),
299
 
                                                                                  wxCUSTBUT_BUTTON);
300
 
 
301
 
    return true;
302
 
}
303
 
 
304
 
void DropDownBase::OnSize( wxSizeEvent& event )
305
 
{
306
 
    event.Skip();
307
 
/*
308
 
    if (!m_dropdownButton) return;
309
 
 
310
 
        wxSize size = GetClientSize();
311
 
    wxPrintf(wxT("DropDownBase %d %d\n"), size.x, size.y);
312
 
 
313
 
        wxSize dropSize = m_dropdownButton->GetSize();
314
 
    m_dropdownButton->SetSize(size.x-dropSize.x, 0, dropSize.x, size.y);
315
 
*/
316
 
}
317
 
 
318
 
void DropDownBase::DoSetSize(int x, int y, int width, int height, int sizeFlags)
319
 
{
320
 
    wxSize curSize( GetSize() );
321
 
 
322
 
    if (width == -1)
323
 
        width = curSize.GetWidth();
324
 
    if (height == -1)
325
 
        height = curSize.GetHeight();
326
 
 
327
 
    wxControl::DoSetSize(x, y, width, height, sizeFlags);
328
 
 
329
 
        wxSize dropSize = m_dropdownButton->GetSize();
330
 
    m_dropdownButton->SetSize(width-dropSize.x, 0, dropSize.x, height);
331
 
}
332
 
 
333
 
wxSize DropDownBase::DoGetBestSize() const
334
 
{
335
 
    return wxSize(95, DROPDOWN_DROP_HEIGHT);
336
 
}
337
 
 
338
 
bool DropDownBase::ShowPopup()
339
 
{
340
 
    int x = 0, y = GetSize().y;
341
 
    ClientToScreen( &x, &y );
342
 
 
343
 
    // control too low, can't show scrollbar, don't bother displaying
344
 
    wxRect displayRect = wxGetClientDisplayRect();
345
 
    if (displayRect.GetBottom() - y < DROPDOWN_DROP_HEIGHT) return false;
346
 
 
347
 
    int width = GetSize().x;
348
 
    int height = DoGetBestDropHeight(displayRect.GetBottom() - y);
349
 
    if (height < 1) return false;
350
 
 
351
 
        m_popupWin = new DropDownPopup(this);
352
 
 
353
 
    m_popupWin->SetSize(x, y, width, height);
354
 
    if (m_popupWin->GetChild())
355
 
        m_popupWin->GetChild()->SetSize(width, height);
356
 
 
357
 
    //wxPrintf(wxT("ShowPopup %d %d, %d %d -- %d\n"), width, height, m_popupWin->GetSize().x, m_popupWin->GetSize().y, m_popupWin->GetMinHeight());
358
 
 
359
 
    return DoShowPopup();
360
 
}
361
 
 
362
 
bool DropDownBase::DoShowPopup()
363
 
{
364
 
    if (m_popupWin)
365
 
    {
366
 
        if (m_popupWin->GetChild())
367
 
            m_popupWin->GetChild()->SetSize(m_popupWin->GetClientSize());
368
 
 
369
 
        m_popupWin->Popup(this);
370
 
        return true;
371
 
    }
372
 
 
373
 
    return false;
374
 
}
375
 
 
376
 
void DropDownBase::HidePopup()
377
 
{
378
 
    if (m_popupWin)
379
 
    {
380
 
        m_popupWin->Dismiss();
381
 
        m_popupWin->Destroy();
382
 
        m_popupWin = NULL;
383
 
    }
384
 
 
385
 
    m_dropdownButton->Refresh(true);  // MSW help in toolbar
386
 
}
387
 
 
388
 
bool DropDownBase::IsPopupShown()
389
 
{
390
 
    return m_popupWin && m_popupWin->IsShown();
391
 
}
392
 
 
393
 
void DropDownBase::OnDropButton( wxCommandEvent &WXUNUSED(event))
394
 
{
395
 
    if (m_popupWin && m_popupWin->m_ignore_popup)
396
 
    {
397
 
        m_popupWin->m_ignore_popup = false;
398
 
        return;
399
 
    }
400
 
 
401
 
    if (IsPopupShown())
402
 
        HidePopup();
403
 
    else
404
 
        ShowPopup();
405
 
}