KiCad PCB EDA Suite
Loading...
Searching...
No Matches
panel_printer_list.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) Jean-Pierre Charras, jp.charras at wanadoo.fr
5 * Copyright (C) KiCad Developers, see AUTHORS.TXT for contributors.
6 *
7 *
8 * This program is free software: you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation, either version 3 of the License, or (at your
11 * option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
23
24// Define CUPS_LIST_PRINTERS to allow printer selection in PANEL_PRINTER_LIST
25// on Unices. It needs cups and cups dev files
26// Currently not tested and not finished for cups, so disable it
27// However the code (from wxWidgets forum) is kept here, just in case
28//#define CUPS_LIST_PRINTERS
29
30#ifdef __WXMSW__
31 #include <Windows.h>
32#elif defined( CUPS_LIST_PRINTERS )
33 #include <cups/cups.h>
34#endif
35
36// GetPrinterList code comes from samples on:
37// https://forums.wxwidgets.org/viewtopic.php?t=13251
38// https://forums.wxwidgets.org/viewtopic.php?t=43930
39static bool GetPrinterList(wxArrayString &aPrinterList, wxString &aDefaultPrinterName)
40{
41 aPrinterList.Empty();
42 aDefaultPrinterName.Empty();
43
44#ifdef __WXMSW__
45 DWORD dwSize, dwPrinters;
46 BYTE *pBuffer;
47
48 DWORD szz = 255;
49 WCHAR c[256];
50 GetDefaultPrinter(&c[0], &szz);
51 aDefaultPrinterName = c;
52
53 ::EnumPrinters(PRINTER_ENUM_LOCAL, NULL, 5, NULL, 0, &dwSize, &dwPrinters);
54
55 if (GetLastError() != ERROR_INSUFFICIENT_BUFFER || dwSize == 0)
56 return false;
57
58 pBuffer = new BYTE[dwSize];
59 ::EnumPrinters(PRINTER_ENUM_LOCAL, NULL, 5, pBuffer, dwSize, &dwSize, &dwPrinters);
60
61 if (dwPrinters != 0)
62 {
63 PRINTER_INFO_5 *pPrnInfo = (PRINTER_INFO_5 *)pBuffer;
64
65 for (UINT i = 0; i < dwPrinters; i++)
66 {
67 aPrinterList.Add(pPrnInfo->pPrinterName);
68 pPrnInfo++;
69 }
70 }
71 if (pBuffer)
72 {
73 delete[] pBuffer;
74 pBuffer = NULL;
75 }
76
77 return true;
78#elif defined( CUPS_LIST_PRINTERS )
79 cups_dest_t* dests;
80 int num_dests = cupsGetDests(&dests);
81
82 for (int i = 0; i < num_dests; i++)
83 {
84 wxString sz;
85
86 if (dests[i].instance)
87 sz = wxString::Format("%s%s", dests[i].name, dests[i].instance);
88 else
89 sz = wxString::Format("%s", dests[i].name);
90
91 if (dests[i].is_default)
92 aDefaultPrinterName = sz;
93
94 aPrinterList.Add(sz);
95 }
96
97 //free memory from cups data
98 cupsFreeDests(num_dests, dests);
99
100 if(aPrinterList.GetCount())
101 return true;
102
103 return false;
104#else
105 return false;
106#endif
107}
108
109
111
112PANEL_PRINTER_LIST::PANEL_PRINTER_LIST( wxWindow* aParent, wxWindowID id,
113 const wxPoint& pos, const wxSize& size, long style,
114 const wxString& name ):
115 PANEL_PRINTER_LIST_BASE( aParent, id, pos, size, style, name )
116{
117
119
120 if( m_printer_list.size() )
121 {
123 m_stPrinterState->SetLabel( wxEmptyString );
124
125 bool selected = false;
126
127 if( !m_selectedPrinterName.IsEmpty() )
128 {
129 for( size_t ii = 0; ii < m_printer_list.GetCount(); ii++ )
130 {
132 {
133 m_choicePrinter->SetSelection( ii );
134 selected = true;
135 break;
136 }
137 }
138 }
139
140 if( !selected )
141 {
142 for( size_t ii = 0; ii < m_printer_list.GetCount(); ii++ )
143 {
145 {
146 m_choicePrinter->SetSelection( ii );
148 break;
149 }
150 }
151 }
152
154 m_stPrinterState->SetLabel( _( "Default printer" ) );
155 }
156}
157
158
160{
161}
162
163
165{
166 return m_choicePrinter->GetCount() > 0;
167}
168
169
170void PANEL_PRINTER_LIST::onPrinterChoice( wxCommandEvent& event )
171{
172 int select = m_choicePrinter->GetSelection();
173
174 if( m_choicePrinter->GetString( select ) == m_defaultPrinterName )
175 m_stPrinterState->SetLabel( _( "Default printer" ) );
176 else
177 m_stPrinterState->SetLabel( wxEmptyString );
178
179 m_selectedPrinterName = m_choicePrinter->GetString( select );
180}
181
182
184{
185 if( AsPrintersAvailable() )
186 {
187 int select = m_choicePrinter->GetSelection();
188 m_selectedPrinterName = m_choicePrinter->GetString( select );
189 }
190 else
191 m_selectedPrinterName.Empty();
192
194}
const char * name
Definition: DXF_plotter.cpp:59
Class PANEL_PRINTER_LIST_BASE.
wxArrayString m_printer_list
PANEL_PRINTER_LIST(wxWindow *aParent, wxWindowID id=wxID_ANY, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxSize(378, 109), long style=wxTAB_TRAVERSAL, const wxString &name=wxEmptyString)
static wxString m_selectedPrinterName
void onPrinterChoice(wxCommandEvent &event) override
#define _(s)
static bool GetPrinterList(wxArrayString &aPrinterList, wxString &aDefaultPrinterName)